Testing the identity of a plug - what is allowed? #compute #pPlug

41 views
Skip to first unread message

mitch.c...@gmail.com

unread,
Nov 25, 2013, 5:26:14 AM11/25/13
to python_in...@googlegroups.com
Hello everyone,

I have a plugin that defines several output plugs:

output_t_qqq = OpenMaya.MObject()
output_t_rrr = OpenMaya.MObject()
output_t_sss = OpenMaya.MObject()
output_t_ttt = OpenMaya.MObject()

These output plugs get added correctly as an attributes to the class, in the nodeInitializer function:

myClass.output_t_qqq = numericAttributeFn.createPoint( 'qqq_translate_out', 'qqq_t_out' )
numericAttributeFn.setWritable( False )
numericAttributeFn.setStorable( False )
numericAttributeFn.setHidden( True )
numericAttributeFn.setReadable(True)
myClass.addAttribute( myClass.output_t_qqq )

etc...

Here's my question: I want to optimize my compute function by executing a particular block if the plug name is .._qqq, or .._rrr, etc. I thought I could do this :

def compute(self, pPlug, pDataBlock):
params = ['qqq', 'rrr', 'sss', 'ttt']
for param in params:
if (pPlug == myClass + '.output_t_' + param):
# do math for the _qqq output attr.

However Maya complains, at the if statement:

"unsupported operand type(s) for +: 'type' and 'str'"


So.... performing string concatenation in an expression, inside compute() is illegal in some way. What's the legal way of doing this?

Mitch

satoh

unread,
Nov 25, 2013, 1:07:07 PM11/25/13
to python_in...@googlegroups.com
in your compute myClass is a (uninstanced) class not a string.
anyway, the easiest way is just check the plug like:
if pPlug == myClass.output_t_qqq:
    do stuff
elif pPlug == myClass.output_t_rrr:
    do other stuff
...



--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/e05523fd-ab1f-4832-ad0f-f11ce5e1c010%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

mitch.c...@gmail.com

unread,
Nov 26, 2013, 2:47:06 AM11/26/13
to python_in...@googlegroups.com
Hey su, thanks for the note. Your explanation makes sense from a semantic perspective. My plugin has twelve outputs. I was looking for a way to structure my compute() so that evaluating one output would not mean evaluating all of them, for performance reasons. Your suggested solution would allow that I think, but at the cost of my compute() getting pretty bloated with repeated blocks of functionally equivalent code.

It seems to me the trade-off is between inefficient code (in the sense of how much ink is being spilled) which provides very efficient compute() execution, versus efficient code where one generic compute function can handle every output - and evaluate every output, every time some input plug is dirtied and the node is evaluated.

So taking this another step further: if dealing with an uninstanced class is a necessary condition for referring to a plug, are there any other ways to test the identity of the plug passed to compute()? A quick scan of the API docs suggests there is no other way than to refer to it explicitly, but I'd love to be proven wrong.

Mitch

Justin Israel

unread,
Nov 26, 2013, 5:21:35 AM11/26/13
to python_in...@googlegroups.com
I'm not clear on the problem. Isn't it just a matter of doing:

def compute(self, pPlug, pDataBlock):
   if pPlug == self.output_t_qqq :
       # do math for the _qqq output attr.

It wasn't a matter of string concatenating being "illegal" inside a compute function. Its just that you were trying to add together incompatible types.

A working equivalent of your example might look like this:

def compute(self, pPlug, pDataBlock):

    params = ['qqq', 'rrr', 'sss', 'ttt']

    for param in params:
        if pPlug == getattr(self, 'output_t_%s' % param):
            # do math for the _qqq output attr.

or

def compute(self, pPlug, pDataBlock):

    params = set([
        self.output_t_qqq,
        self.output_t_rrr,
        self.output_t_sss,
        self.output_t_ttt,
    ])

    if pPlug in params:
        # do math for the _qqq output attr.
Reply all
Reply to author
Forward
0 new messages