hi chad,
thank you for the new example.
it helped me to understand the methodology.
i modified it for it works with the '_nodeType' value.
that took me a little time to find how i can get the value of a
attribute with the api (because i never use the api before)
the code modified.
from pymel import *
#-------------------------------------------------------------------------------
class CustomJointBase(Joint):
""" this is an example of how to create your own subdivisions of
existing nodes. """
@classmethod
def createVirtual(cls, **kwargs):
"""
This method is called when no argument is passed to the class,
such as:
>>> LegJoint(name='right)
LegJoint(u'right')
this method must be a classmethod or staticmethod. If you
don't know what that means, just make sure you have
@classmethod above your createVirtual method, as in this
example.
"""
# create a joint
j = joint(**kwargs)
# add the identifying attribute. the attribute name will be
set on subclasses of this class
j.addAttr('_nodeType',dt='string')
j._nodeType.set(cls._jointClassID)
return j
@classmethod
def callback( cls, obj, name ):
"""This is the callback for the determining if a Joint should
become a "virtual" LegJoint or JawJoint, etc.
The name of the method is unimportant, and it could have
actually been a regular function instead of a class method.
Notice that this method is a classmethod, which means it gets
passed the class as "cls" instead of an instance as "self".
PyMEL code should not be used inside the callback, only API.
"""
# obj is either an MObject or an MDagPath, depending on
whether this class is a subclass of DependNode or DagNode,
respectively.
fn = api.MFnDependencyNode(obj)
try:
# NOTE: MFnDependencyNode.hasAttribute fails if the
attribute does not exist, so we have to try/except it.
# the _jointClassID is stored on subclass of
CustomJointBase
plug = fn.findPlug('_nodeType')
value = plug.asString()
return value == cls._jointClassID
except: pass
return False
class LegJoint(CustomJointBase):
_jointClassID = 'jointType_leg'
def kick(self):
print "kicking"
class JawJoint(CustomJointBase):
_jointClassID = 'jointType_jaw'
def munch(self):
print "munching"
# we don't need to register CustomJointBase because it's just an
abstract class to help us easily make our other virutal nodes
LegJoint.registerVirtualSubClass( LegJoint.callback,
nameRequired=False )
JawJoint.registerVirtualSubClass( JawJoint.callback,
nameRequired=False )
def testJoint():
Joint()
Joint()
LegJoint()
JawJoint()
# now list the joints and see which ones are our special joints
res = ls(type='joint')
for x in res:
if isinstance(x, LegJoint ):
x.kick()
elif isinstance(x, JawJoint ):
x.munch()
colas
--~--~---------~--~----~------------~-------~--~----~
http://groups.google.com/group/python_inside_maya
-~----------~----~----~----~------~----~------~--~---