How to work with C++ classes at python

        node1  =  DirectFrame(parent = aspect2d)
        node2  =  DirectFrame(parent = node1)
        for x in node1.getChildrenAsList():
           print x.__class__ # Here we have object of NodePath
           print x == node2 # Here we have True

So, question is, how to convert NodePath to original class?
Target of this action is to view x.dict (NodePath as C++ class haven’t variable dictation)

Search the forums for setPythonTag.


Thank you for your answer.
But, I have function that’s working with result of getChildren function, and haven’t access to real nodes, like:

def PrintNodeVariables(Node):
   print Node.__dict__ # Will fail, when Node.__class__ == NodePath
   for child in Node.getChildrenAsList():

Is here way to receive original variable?

Yes. This is a commonly asked question. The answer is already described in several forum threads, such as this one and this one and many others.

The problem is that NodePath is a handle, not the node itself. There can be many different NodePaths that reference the same node. In particular, the DirectFrame object that you create in the beginning, which is a specialization of NodePath, is one of the NodePaths that references the node it contains. But when you call getChildrenAsList(), you are getting a new list of NodePaths, none of which is the same NodePath as the original DirectFrame.

But you can store a pointer to the original DirectFrame on the node itself. That’s what setPythonTag is for.

        node1  =  DirectFrame(parent = aspect2d)
        node2  =  DirectFrame(parent = node1)
        node2.setPythonTag('frame', node2)
        for x in node1.getChildrenAsList():
           y = x.getPythonTag('frame')
           print y.__class__


I read this threads before, and if i have access to original panda objects it’s works.
But in function like in my previous post, I have only list of NodePath handlers. And I need to convert them, without any outside information (like tags, dictionary of {id:object} or something other).

Still not quite following you. You’re saying that for some reason you’re not allowed to assign tags to your NodePaths, and you can’t put your NodePaths in a dictionary for later lookup? Then I’m sorry, there’s no solution for you.

This is not an issue of converting the type of the NodePath to its correct type. The object you are getting back is a NodePath, it is not a DirectFrame. It is a different object than the DirectFrame you created originally. It happens to be a handle to the same node internally, but it is nevertheless a different object, and there is no way to “convert” it to an object that it is not. The only thing you can do is maintain some association with the original object, for instance using a dictionary or using a tag, so that you can map a new NodePath object back to the original object.


For the record, for C++ classes, there is x.DtoolClassDict:

print NodePath.DtoolClassDict


def getClassDict(cls):
  if hasattr(cls, DtoolClassDict)
    return cls.DtoolClassDict
    return cls.__dict__

print getClassDict(NodePath)

Well, it exists, but it’s an implementation detail and is not intended to be publicly accessible. We reserve the right to take it away again in the future. If you rely on it, you risk writing code that will fail to work on future versions of Panda.

In any case, the DtoolClassDict is also specific to the particular NodePath you get back from a call, and is not global to all NodePaths that share the same node (unlike the setPythonTag).


Thanks, it will helps on this time.
But I’m really confused, that there are no way to get python-handler from c+±handler that handles to one python object.
I look up for type convert, but found that it works only when to classes have(just 4 bytes) to original python-object. And users can use it like:

def getClassDict(cls):
   return cls.OriginalPythonObject.__dict__

print getClassDict(NodePath)

David, I see you really strong fan of “SetPythonTag” =)

Actually, I’m not; there are other problems caused by setPythonTag. But I agree you are confused. I’ll say it again: the problem is not one of type conversion, but that you are dealing with different objects.