Hey all, I've been using sort of a hacky method for extending pymel, and 
I've just now been trying to get my head around subclassing with virtual 
classes to upgrade my code.

The way I've been doing it up to now is not inheriting directly from pymel 
node classes, but rather creating a custom class and storing the pyNode 
object in a member variable, and then overriding the __getattr__ method to 
have access to the pyNode methods as well as my own custom methods. It 
looks something like this:


class MyTransform(object):

def __init__(self):

 self.__pyNode = pm.createNode('transform')

 #any sort of custom stuff, adding attributes and assigning member 
variables.

def someCustomMethod(self):

#do custom stuff

def __getattr__(self, attr):

if attr in self.__dict__: 

return self.__dict__[attr]

else:

return getattr(self.__pyNode, attr)


It's then easy to inherit from this, for example MyJoint could be inherited 
from MyTransform and have access to someCustomMethod, as well as the 
methods that are unique to the pymel Joint class. Of course the downside is 
that it's not a real subclass, just kind of a wrapper, and it has some 
limitations.

Anyway, I got the virtual class stuff working from the examples, but as I 
started converting my first couple classes I've run into some snags that I 
wasn't sure about the best way to get around. The way I'm approaching this 
conversion is I'm replacing __init__ with _preCreateVirtual, createVirtual, 
and _postCreateVirtual, more or less. But some of the things I was doing in 
my __init__ methods, such as setting member variables and calling methods 
from this class and parent classes, doesn't seem to be possible. And 
neither is adding an __init__ method, because special methods aren't 
allowed in virtual classes. I'm kind of pushing the limit of my current 
python knowledge with this stuff, so I'm not really sure where to go next. 
My only thought was to create a wrapper function for each class to actually 
call it and then assign or edit any member variables or do anything else I 
can't figure out how to do during creation. But is there some better way of 
dealing with this that I'm not understanding?

-- 
view archives: http://groups.google.com/group/python_inside_maya
change your subscription settings: 
http://groups.google.com/group/python_inside_maya/subscribe

Reply via email to