Using Subclass of PandaNode

Hi!

I am trying to replace PandaNode with a subclass (to add some properties).
The problem that I encounter is that even though I am passing an instance of my subclass to attachNewNode, calling node() afterwards returns an instance of PandaNode and not my subclass.

This code illustrates this:

from pandac.PandaModules import NodePath
from pandac.PandaModules import PandaNode


class TestClass(PandaNode):

    def __init__(self, *args, **kwargs):
        PandaNode.__init__(self, *args, **kwargs)

class SubClass(TestClass):

    def __init__(self, *args, **kwargs):
        TestClass.__init__(self, *args, **kwargs)

base = TestClass('Base')
sub = SubClass('Sub')

np = NodePath('Hello World')

print 'before class: ', sub.__class__
np.attachNewNode(sub)
print 'after class: ', np.node().__class__
print 'are equal? ', sub == np.node()

The output I get is:

before class:  <class '__main__.SubClass'>
after class:  <type 'libpanda.PandaNode'>
are equal?  False

I tried looking into the C++ code for NodePath and PandaNode but it’s couldn’t get any probable reason out for this.

Any help will be appreciated :slight_smile:

Shashank

You found out a fact. Search the forums to find several discussion about it. To store informations (for example a objectId) in a pandanode use setTag or setPythonTag. i dont recommend using setPythonTag (if you forget to delete the tag, python cannot free the memory).

Thanks for the response Hypnos!

Unfortunately I am unable to find any discussions on this topic in the forums.
May be this is because I am looking at this problem from a different perspective and am not getting the keywords right.

All I want here is that the NodePath give me back, on calling node(), the same PandaNode(or its subclass) instance that I passed to it at attachNewNode.

Can any one shed some light on this behaviour or may be point me to some previous discussions on this issue?

Shashank

Well, that’s tricky, because PandaNode is implemented in C++. Basically you are asking for a way to upcast a C++ class to a Python class, and I don’t think it’s possible to add that functionality to Panda.

I suggest making your classes wrappers rather than inheriting from PandaNode.
Another way that is frequently used is to store the Python class on the C++ class using setPythonTag. When you have the C++ class you’ll always be able to ‘upcast’ it to the Python class using getPythonTag. (Be careful there though - since you’re creating a circular dependency then.)