I examined the actor class to see how it subclasses NodePath and extracted it into a python class that you can easily subclass from. I’m not sure how robust this subclassing method is though. Hypnos warned me of problems with collisions but I’ve gotten this working with one of the collision samples in the manual.
This snipplet goes to the public domain
This first class lets you easily subclass NodePath instead of having an attribute for a NodePath in your class.
from pandac.PandaModules import NodePath, PandaNode, GeomNode
class SubNodePath(NodePath):
"""
A convenience class for sub classing NodePath. Derived from how the Actor class subclasses NodePath.
"""
def __init__(self, name=None):
"""A convenience class for subclassing NodePath"""
# Initialise nodepath essence
NodePath.__init__(self)
# Creates a flattenable nodepath
if name:
root = PandaNode(name)
else:
root = PandaNode('SubNodePath')
self.assign(NodePath(root))
self.__geomNode = NodePath(self)
if name:
NodePath.setName(self, name)
def removeNode(self):
# remove all its children
if(self.__geomNode):
self.__geomNode.removeChildren()
self.__geomNode.removeNode()
self.__geomNode = None
if not self.isEmpty():
NodePath.removeNode(self)
This second class is an example of a NodePath subclass that has been given a pythonic position property.
class PyNodePath(SubNodePath):
def __init__(self, name=None):
"""
A NodePath subclass with pythonic positions attributes.
NodePath.pos will give you the node's world position while
NodePath.pos = 1, 2, 3 will set it. NodePath.pos = NodePath, 1, 2, 3
sets the position relative to the given nodepath. The other properties
are NodePath.x, NodePath.y, NodePath.z.
"""
super(PyNodePath, self).__init__(name)
def __setProperty(self, funcName, args):
"""Used for property setting functions that could
have more than one argument"""
func = getattr(NodePath, funcName)
if type(args) is tuple:
return func(self, *args)
else:
return func(self, args)
# Position attributes
def __getPos(self):
return NodePath.getPos(self)
def __setPos(self, args):
return self.__setProperty('setPos', args)
pos = property(__getPos, __setPos)
def __getX(self):
return NodePath.getX(self)
def __setX(self, args):
return self.__setProperty('setX', args)
x = property(__getX, __setX)
def __getY(self):
return NodePath.getY(self)
def __setY(self, args):
return self.__setProperty('setY', args)
y = property(__getY, __setY)
def __getZ(self):
return NodePath.getZ
def __setZ(self, args):
return self.__setProperty('setZ', args)
z = property(__getZ, __setZ)
EDIT NOTE
I’ve edited this post to address pro-rsoft’s comment below.