Python custom node optimisation

154 views
Skip to first unread message

casf01

unread,
Nov 24, 2017, 8:21:22 AM11/24/17
to Python Programming for Autodesk Maya
Good morning ( it's 8 am for me :) )

I've been writing a custom node that only update the ouput on specific condition and I was wondering how I should handle it in the compute method to be the most effective and costless

this is approximately what I got with some pseudo code
def compute (self, plug, data):

if plug == myNode.output:

input = data.inputValue(myNode.input).asVector()
output = data.inputValue(myNode.output)

for n in input:

if n >= 0.0:

# do something

# send my output
data.setClean(plug)

else:
return None  

I know this code doesn't make much sense but what I want to know is:
  1. Should I put my data.setClean(plug) inside the if statement or it doesnt matter.  What I want is once the if is True then use this value as output and remove the dirty flag so I'm guessing this is the right way!?
  2. Should I use a return after my data.setClean(plug) or the rest of the loop won't be evaluated anyway since the node isnt marked as dirty anymore?
  3. How can I effectively benchmark my compute method?
Thanks in advance

Marcus Ottosson

unread,
Nov 24, 2017, 8:41:43 AM11/24/17
to python_in...@googlegroups.com

Some notes from me.

def compute (self, plug, data):
    if
 plug == myNode.output:

        # Best not to override reserved keywords like input()
        input_ = data.inputValue(myNode.input).asVector()
        output = data.inputValue(myNode.output)

        didSomething = False

        for n in input:
            if n >= 0.0
:
                didSomething = True

                # do something
                # send my output


        if didSomething:
            data.setClean(plug)

    else:
        return OpenMaya.kUnknownParameter
  1. Assuming setClean isn’t aggregated and optimised by Maya under the hood like Qt does with signals (unlikely) then it’s probably best to defer calling it, and call it as few times as possible. In your snippet, it’d be called for every n in input.
  2. I think the return value should be something along these lines, as least that’s what I’m reading here but could be wrong, or it could only apply to API 1.0 or 2.0.

casf01

unread,
Nov 24, 2017, 9:27:53 AM11/24/17
to Python Programming for Autodesk Maya
Thanks I did the change suggested, althought I shouldve mentionned that im using the API 2.0

and I can't get the  return OpenMaya.kUnknownParameter with maya.api.OpenMaya. It return me an error saying 'module' object has no attribute 'kUnknownParameter'. Which is strange because the python api 2.0 example here explicitly use it:

Im a bit confused... and tbh I don't even know why I used return None, I guess I saw this somewhere and used it.

Marcus Ottosson

unread,
Nov 24, 2017, 10:20:48 AM11/24/17
to python_in...@googlegroups.com

That is strange! Also getting that here on 2018.1.​

from maya.api import OpenMaya
OpenMaya.kUnknownParameter
# Error: 'module' object has no attribute 'kUnknownParameter'
# Traceback (most recent call last):
#   File "<maya console>", line 1, in <module>
# AttributeError: 'module' object has no attribute 'kUnknownParameter' #

Angelo Sta. Catalina

unread,
Nov 24, 2017, 12:53:31 PM11/24/17
to python_in...@googlegroups.com
Maya made the change to remove MStatus from the python api so that users handle exceptions through using native python methods. I forget which version they made this change but you can read about it here (Under Exceptions vs MStatus topic).

casf01

unread,
Nov 24, 2017, 1:11:19 PM11/24/17
to Python Programming for Autodesk Maya
Users writing plug-ins in Python should raise exceptions rather than return MStatus values, unless they want their compute() method to indicate that it is not going to handle a plug. In this case, it should return maya.OpenMaya.kUnknownParameter.

Interesting,  thanks for pointing that out
Reply all
Reply to author
Forward
0 new messages