Hey Paul,
Hmm... first off, your virtual node class needs to inherit from a 'real',
> non-abstract, non-virtual class - in your example, it's inheriting from
> pm.nt.MyNode (which I'm surprised even works - essentially, it's inheriting
> from the node that is created from itself??)
>
Sorry, I simplified the example a little bit, pm.nt.MyNode was just a place
holder for a plugin node. In my case the declaration is actually:
class CrowdGroupNode(pm.nt.CROWD_GroupNode)
Also, the create method needs to return the string name of the created node
> (unless you also add a postCreate callback, in which case it can return
> whatever the postCreate can handle), which should be noted in the latest
> documentation for virtualClasses.register (and in the customClasses.py).
>
Ah okay, wasn't sure if this was the case, I updated my working example to
follow your code.
>
> Lastly, in the create method itself, you should not use pymel code - stick
> to maya.cmds stuff. Also, you cannot simply do createNode('MyNode'), as
> these are 'virtual' classes that only pymel knows about. You would have to
> do createNode('myRealBaseClass').
>
Bad example, I'm not actually referencing the virtual class, but the plugin
node, in the following example pm.createNode('CROWD_GroupNode') creates the
correct node type in Maya
Here's the entire example:
import maya.cmds as cmds
import pymel.core as pm
import pymel.internal.factories as factories
pm.loadPlugin('CROWD_GroupNode')
class CrowdGroupNode(pm.nt.CROWD_GroupNode):
@classmethod
def createVirtual(cls, **kwargs):
print 'CREATING MY NODE!'
return cmds.createNode('CROWD_GroupNode', **kwargs)
@classmethod
def _isVirtual( cls, obj, name ):
return True
def test(self):
print 'MY NODE!'
# It doesn't look like factories.virtualClasses is available in pymel 1.0.0
# And the current register command doesn't have a create kwarg
factories.registerVirtualClass(CrowdGroupNode, nameRequired=False)
Thanks!
Justin
On Tue, Nov 1, 2011 at 9:42 AM, Paul Molodowitch <[email protected]> wrote:
> Hmm... first off, your virtual node class needs to inherit from a 'real',
> non-abstract, non-virtual class - in your example, it's inheriting from
> pm.nt.MyNode (which I'm surprised even works - essentially, it's
inheriting
> from the node that is created from itself??)
> Also, the create method needs to return the string name of the created
node
> (unless you also add a postCreate callback, in which case it can return
> whatever the postCreate can handle), which should be noted in the latest
> documentation for virtualClasses.register (and in the customClasses.py).
> Lastly, in the create method itself, you should not use pymel code - stick
> to maya.cmds stuff. Also, you cannot simply do createNode('MyNode'), as
> these are 'virtual' classes that only pymel knows about. You would have to
> do createNode('myRealBaseClass').
> Here's an example which should work:
>
> import maya.cmds as cmds
>
> import pymel.core as pm
>
> import pymel.internal.factories as factories
>
> class MyNode(pm.nt.Locator):
>
> @classmethod
>
> def createVirtual(cls, **kwargs):
>
> print 'CREATING MY NODE!'
>
> return cmds.createNode('locator', **kwargs)
>
> @classmethod
>
> def _isVirtual( cls, obj, name ):
>
> return True
>
> def test(self):
>
> print 'MY NODE!'
>
> factories.virtualClasses.register(MyNode, create='createVirtual')
>
> myNode = MyNode()
>
> - Paul
>
> On Tue, Nov 1, 2011 at 9:12 AM, Justin Rosen <[email protected]>
wrote:
>>
>> Thanks Seth! Again, I'm using pymel that comes with 2011
>> pymel.__version__ == 1.0.0'
>>
>> I was able to get a small working example that works with pm.createNode:
>>
>> class MyNode(pm.nt.MyNode):
>> @classmethod
>> def _isVirtual( cls, obj, name ):
>> return True
>>
>> def test(self):
>> print 'MY NODE!'
>>
>> pymel.internal.factories.registerVirtualClass(MyNode)
>>
>> myNode = pm.createNode('MyNode')
>> myNode.test()
>>
>>
>> Although, I'd like to be able to create my nodes via a call to
>> MyNode(). To do this I added the classmethod createVirtual:
>>
>> class MyNode(pm.nt.MyNode):
>> @classmethod
>> def createVirtual(cls, **kwargs):
>> print 'CREATING MY NODE!'
>> return pm.createNode('MyNode', **kwargs)
>>
>> @classmethod
>> def _isVirtual( cls, obj, name ):
>> return True
>>
>> def test(self):
>> print 'MY NODE!'
>>
>> myNode = MyNode()
>> # Error: ValueError: unexpect result locator1 returned by spaceLocator #
>>
>> Am I missing something here?
>>
>> Thanks,
>> Justin
>>
>> On Tue, Nov 1, 2011 at 12:24 AM, Justin <[email protected]> wrote:
>> > Thanks! The first example seemed a bit overkill which is why I asked
>> > the question, but the second example is exactly what I need (Except
I'm not
>> > on the trunk):
>> >
>> > # make sure Mayatomr plugin is loaded ore the Mib_amb_occlusion might
>> > not exist
>> > loadPlugin('Mayatomr')
>> > class Mib_amb_occlusion(Mib_amb_occlusion):
>> > """This is an example of how to replace a node. Use this technique
>> > with care"""
>> > def occlude(self):
>> > print "occluding!"
>> >
>> > # the callback always returns True, so we always replace the default
>> > with our own.
>> > Mib_amb_occlusion.registerVirtualSubClass( lambda *args: True,
>> > nameRequired=False )
>> >
>> > def testMib():
>> > n = createNode('mib_amb_occlusion')
>> > n.occlude()
>> >
>> > But, I get the following error:
>> > # Error: AttributeError: file
>> >
/Applications/Autodesk/maya2011/Maya.app/Contents/Frameworks/Python.framework/Versions/Current/lib/python2.6/site-packages/pymel/core/general.py
>> > line 53: type object 'Mib_amb_occlusion' has no attribute '_isVirtual'
#
>> >
>> > I'm running the version of pcl that comes with 2011. I assume there
>> > have been a bunch of changes since then that has made this process
easier,
>> > not requiring _isVirtual to be defined or in the trunks case a
callback to
>> > be specified? Can I accomplish the same task of replacing the mental
ray
>> > node with the version of pcl that comes with Maya2011?
>> >
>> > Thanks,
>> > Justin
>> >
>> >
>> > On Oct 31, 2011, at 11:53 PM, Justin Israel wrote:
>> >
>> >> This seems to be a really straight forward example located here:
>> >> http://pymel.googlecode.com/svn/trunk/examples/customClasses.py
>> >>
>> >> It outlines how to subclass, and what class methods must be provided.
I
>> >> believe the use of the id attribute is for the validation test
function, in
>> >> order to determine if the incoming object type is the right kind. In
this
>> >> case they use the joint id attribute from the original Joint class to
check.
>> >> If its a joint then allow this subclass to be returned. Technically
you
>> >> could perform any test you see fit as long as you stick to the api to
do it
>> >> and not pymel calls.
>> >>
>> >>
>> >>
>> >>
>> >> On Oct 31, 2011, at 7:51 PM, Justin <[email protected]> wrote:
>> >>
>> >>> Hey all, It's been a super long time since I've been on this list!
>> >>>
>> >>> I was wondering if I could get a quick example on how to accomplish a
>> >>> simple wrapping of a pymel node as described here:
>> >>> http://code.google.com/p/pymel/issues/detail?id=62
>> >>>
>> >>> allow users to add their own methods to any node. users can now
easily
>> >>> import pymel and then subclass nodes to do what they like, but
perhaps
>> >>> there's a way to register these user subclass modules to be loaded
by pymel,
>> >>> such that they are found within the pymel namespace.
>> >>>
>> >>>
>> >>>
>> >>> import pymel.core as pm
>> >>>
>> >>> # Load plugin
>> >>> pm.loadPlugin('pointless')
>> >>>
>> >>> # Print Node Type
>> >>> print pm.nt.PointlessViewer
>> >>>
>> >>> How would I subclass this and add my own methods?
>> >>>
>> >>> class PointlessViewerNode(pm.nt.PointlessViewer):
>> >>> def myMethod(self):
>> >>> # Do something with my node
>> >>> pass
>> >>>
>> >>> Do I have to follow the example under pymel.examples.customClasses.py
?
>> >>> Where an extra attribute is added, ie cls._PointlessClassID? I'm
not sure
>> >>> I need this extra attribute, I just want to add additional methods
to the
>> >>> existing pymel node returned when creating nodes.
>> >>>
>> >>> viewer = pm.createNode('pointlessViewer')
>> >>> viewer.myMethod()
>> >>>
>> >>>
>> >>> Thanks,
>> >>> Justin
>> >>>
>> >>>
>> >>>
>> >>>
>> >>> --
>> >>> view archives: http://groups.google.com/group/python_inside_maya
>> >>> change your subscription settings:
>> >>> http://groups.google.com/group/python_inside_maya/subscribe
>> >>
>> >> --
>> >> view archives: http://groups.google.com/group/python_inside_maya
>> >> change your subscription settings:
>> >> http://groups.google.com/group/python_inside_maya/subscribe
>> >
>> >
>>
>> --
>> view archives: http://groups.google.com/group/python_inside_maya
>> change your subscription settings:
>> http://groups.google.com/group/python_inside_maya/subscribe
>
> --
> view archives: http://groups.google.com/group/python_inside_maya
> change your subscription settings:
> http://groups.google.com/group/python_inside_maya/subscribe
>
--
view archives: http://groups.google.com/group/python_inside_maya
change your subscription settings:
http://groups.google.com/group/python_inside_maya/subscribe