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
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.
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.