TypeHandle question

Browsing through the API for DirectButton I came across getClassType(), well its actually on the NodePath, and it returns a TypeHandle.

Can this TypeHandle be used to test a NodePath to find out if the node is a PGButton or PGTop? Is this the right way to find out what the node is, or perhaps there is an easier way?

I did try a few things but I could not figure out where to get the TypeHandle to test against it :frowning:

Thanks in advance.

I just found that I can do…

 if theNodePath.node().getClassType().getName() == 'PGButton': 

But Im sure there is a much more elegant solution to this :slight_smile:

That works perfectly well; you don’t need to change anything.

Note that getClassType() returns the type of the pointer, whereas getType() returns the type of the actual instance. In Python, there’s almost no distinction, but in C++ it makes a big difference.

The intended design for determining the type of a node (which works equally well in C++ and Python) is to do something like this:

if theNodePath.node().isOfType(PGButton.getClassType()):

This has the advantage that it also accepts things that inherit from PGButton, if there were any such class–probably doesn’t matter for PGButton, but sometimes there are cases when it does matter.

Also, if you’re searching the graph for a node of a particular type, I trust you know that you can do this:

nodePath = rootNodePath.find('**/+PGButton')

which will return the first descendent of rootNodePath found (that is, the highest node) that is of type PGButton.

Thanks for the info David. I will use the isOfType() as it look more elegant and easier to read. I did not realise you could call functions on PGButton as I thought it would just be a name. Please can you explain what type PGButton is?

One other question,

DirectButton inherits from DirectGuiWidget which contains getHeight() and getWidth(). When I call theNodePath.node().getHeight() I get an error saying “PGButton instance has no attribute ‘getHeight’”, so I presume not all functions are inherited?

I tried looking through the API and can get hold of the X, Y, Z positions fine but I would also like to be able to get the width and height.

Any information greatly appreciated.

PGButton is a class, and you’re right–normally you don’t call methods on a class; normally you can only call methods on an instance of a class. But Python treats a “class” as an object in its own right, and it is possible to define class methods, which can then be called on the class itself–or on any instance of that class. This is similar to the concept of a C++ “static” method.

DirectButton does in fact inherit the getHeight() and getWidth() methods. The problem is that you don’t have a DirectButton, you only have a PGButton.

DirectButton is a Python object. It contains a PGButton, which is a C++ object. You won’t find the DirectButton in the scene graph, since it’s not really there–there are no Python objects in the scene graph at all. The best you can find in the scene graph is the C++ part of the DirectButton.

This dichotomy between Python and C++ objects is unfortunate and confusing, and it bites everyone in the backside eventually. But once you get your head around it, it’s not too hard to keep clear in your head what gets stored in the scene graph and what doesn’t.

So if you want to query the getHeight() and getWidth() methods on your DirectButton, or do any other Python-specific operations, you will need to keep a handle to the DirectButton itself, somewhere outside of the scene graph, and query that object instead.


Thanks for the incredibly quick reply :slight_smile:

I didnt realise PGButton was in pandac :blush: Have not got that far through the API yet :slight_smile:

I understand about the static class functions, I tried that when I was mucking about with multiple windows. Its very handy to know.

I think my biggest misunderstanding was the scenegraph, which has probably been throwing me off a little :cry:

Ive had a quick scan through DirectGuiBase.py and that holds the key 8)

Thanks again for the info.