Hey guys thanks. Jason your site has been my primary source of information on this, thanks for that.
Unless I am just misunderstanding something, I see the problem being that because _postCreateVirtual and all these methods are classmethods, you don't really have access to the actual python object that's being created (There's no self argument). So whereas normally I would store temporary data in a member variable, I can't actually set the value of member variables within the _postCreateVirtual method. I also can't call any other methods that aren't classmethods. And again, maybe I'm not using this the way it was meant to be used, but here's an example: I've got a Transform subclass, which is inherited from nt.Transform. Based on what that transform is being used for (a control, group, identity node, etc) it has attributes set, and then methods which query that information to do things. For example setting it to draw a certain color, or set it to a specific rotation order. This is all stuff that could be done in _postCreateVirtual I suppose, but the benefit of having them as separate methods is having access to them after the node is created, and also being able to have access to them in any inherited classes. Any other custom classes that are transforms, like joints or ik handles, should inherit from the custom transform subclass in addition to their respective pymel nodetype so they can leverage those same methods, and it would be nice to have access to those methods during node creation. But I think I've come to the conclusion that I need to look at virtual classes a bit differently to how I've currently structured my code. I can always keep the node creation simple, and call the virtualClass's custom methods in whatever parent function is creating the node. The other thing I've been doing is using classes to represent more than just a single node, but small hierarchies and networks of nodes, which I think might kind of break the paradigm of virtual classes. For example I've got a JointChain class, which inherits from a custom Joint class, but is an iterator and represents a hierarchy of joints, and has methods for dealing with the chain as a whole. Hopefully that wasn't too much gibberish, I supposed I'm just still trying to understand how virtual classes are meant to be used and whether or not it will be worth updating a lot of code to incorporate that. Thanks again! On Thursday, October 18, 2012 6:04:09 AM UTC+13, Count Zer0 wrote: > > Did you check the work Seth and I did?: > > http://www.jason-parks.com/artoftech/?p=225 > > What "things" could you not do in your __init__ method. Doesn't > _postCreateVirtual essentially replace the __init__ method? > > We've used this technique successfully on a couple of classes so far. > Though we haven't put it through its paces too seriously. I'm about to > extend the factory created nt.hikCharacterNode class to wrap all the > non-documented MEL methods to the new HIK character node. After that, I > should have an even more familiar knowledge of the subclassing process. > > -jason > > On Saturday, October 13, 2012 1:23:09 AM UTC-7, Morgan wrote: >> >> 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
