I’ve read up on some of the guidelines for NodePath subclassing, and thought I would write up a few code examples to illustrate what’s going on. I’ve now come to a point where I also want to be able to attach classes to NodePaths, so I’ve come up with a slight variation to the circular reference method described in this post: [url]Inheriting from NodePath]
I didn’t get the results that I expected however, and now I’m not sure I understand the whole thing as well as I did. I’m hoping someone can help me understand!
Here’s my code:
import direct.directbase.DirectStart from panda3d.core import NodePath class PandaObject( object ): def __init__( self, *args ): self.np = NodePath( *args ) self.np.setPythonTag( 'PandaObject', self ) def __del__( self ): print 'deleted!' @staticmethod def Get( np ): return np.getPythonTag( 'PandaObject' ) def AddPandaObject( name ): # Create an instance of our class, stick the node path into the scene # graph pObj = PandaObject( name ) pObj.np.reparentTo( render ) def Destroy( name ): # Find the node in the scene graph, attempt to remove it - note how the # destructor message isn't printed np = render.find( name ) print np np.removeNode() def DestroyViaStaticMethod( name ): # Find the node in the scene graph, get the object using the static method # then attempt to remove the node path pObj = PandaObject.Get( render.find( name ) ) print pObj.np pObj.np.removeNode() print 'Result for removing normally: ' AddPandaObject( 'first' ) Destroy( 'first' ) print 'Result for removing via static method: ' AddPandaObject( 'second' ) DestroyViaStaticMethod( 'second' ) run()
So basically I want to use a static method to turn a NodePath back into its parent class. When I remove the node in the Destroy() function, the destructor message doesn’t print - which is to be expected because I haven’t broken the circular reference by clearing the tag. Since my class hasn’t been destroyed I assume that the NodePath hasn’t been either, but I’ll never be able to test that properly.
However when I destroy via the static method, I do see the destructor message. I haven’t broken the circular reference though, so how can this be? If my class has been destroyed, has the NodePath been too? If so, why?
Also - is there anyway to ‘see’ all the NodePaths that exist even if they aren’t attached to anything? That would be super handy to figure out some of this stuff