[Maya-Python] Listen on attribute change

2,308 views
Skip to first unread message

Marcus Ottosson

unread,
Apr 28, 2016, 4:25:54 PM4/28/16
to python_in...@googlegroups.com

Hi all,

I’ve managed to devise a method of listening in on an attribute changing, so I can respond appropriately. But I’m interested in whether there’s a more efficient way. Efficiency is my main concern in this case.

import os
import time
from maya import cmds, OpenMaya as om

# Setup scene
cmds.file(new=True, force=True)
cmds.polyCube()

# Find cube as MObject
# Is this the optimal route?
dag_path = om.MDagPath()
selection = om.MSelectionList()
selection.add("pCube1")
selection.getDagPath(0, dag_path)
m_obj = dag_path.node()

# Callback listening to changes to ANY attribute
# Is this efficient?
def callback(message_type, plug, other_plug, client_data):
    if not message_type & om.MNodeMessage.kAttributeSet:
        return

    if "translateX" in plug.name():
        print("Translate X changed!")

job = om.MNodeMessage.addAttributeChangedCallback(m_obj, callback)
#om.MMessage.removeCallback(job)

The obvious issue is how I’m listening for changes on any attribute change, rather than just the one I’m interested in, and then filtering it down. The Python callback is triggered a few more times than I’d like.

In an empty scene, without this callback, Maya is running at 1200-1300 fps (0.8ms/frame) whereas with this callback it’s running closer to 400 (2.5ms/frame).

Any ideas?

Best,
Marcus

--
Marcus Ottosson
konstr...@gmail.com

Justin Israel

unread,
Apr 28, 2016, 5:06:44 PM4/28/16
to python_in...@googlegroups.com
Out of curiosity, what are the performance numbers like when your callback simply does nothing and passes? I am wondering if the required checks in your function body are slow, or if simply using a python-based callback is the slow part in the first place.

Justin

--
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/CAFRtmOBppomMtcXWQvayoC07ht658JB2H5uZVWtnxcyHWrYynw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

yury nedelin

unread,
Apr 28, 2016, 11:39:57 PM4/28/16
to python_in...@googlegroups.com
Is this better then a scriptJob?
Yury

Justin Israel

unread,
Apr 29, 2016, 7:38:22 AM4/29/16
to python_in...@googlegroups.com


On Fri, 29 Apr 2016 3:39 PM yury nedelin <yned...@gmail.com> wrote:
Is this better then a scriptJob?
Yury

isn't a scriptJob the same thing (the c++ API being exposed to mel/python) 

Unless maybe you are suggesting that a Mel callback might have less overhead? 

yury nedelin

unread,
Apr 29, 2016, 10:19:35 AM4/29/16
to python_in...@googlegroups.com

I was just wondering how will this method compare to build- in scriptJob.

Tom Norman

unread,
Apr 29, 2016, 3:30:26 PM4/29/16
to Python Programming for Autodesk Maya
A scriptjob is better if you need to catch attribute changed for a specific attribute (translateX in this case).

 If you need to code to run when ANY attribute of a node changes, the API callback is the better bet. (Single callback vs. a scriptjob for each settable attribute of the node)
Justin


To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

--
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_maya+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
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_maya+unsub...@googlegroups.com.

--
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_maya+unsub...@googlegroups.com.

Justin Israel

unread,
Apr 29, 2016, 4:07:14 PM4/29/16
to python_in...@googlegroups.com
On Sat, Apr 30, 2016 at 7:30 AM Tom Norman <tomnor...@gmail.com> wrote:
A scriptjob is better if you need to catch attribute changed for a specific attribute (translateX in this case).

 If you need to code to run when ANY attribute of a node changes, the API callback is the better bet. (Single callback vs. a scriptjob for each settable attribute of the node)

That would make sense, yea. If the scriptJob ends up doing the routing and filtering of the message on the C++ side and delivering it to your python callback only after it fully matches, then that would reduce the python overhead. 
 
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

--
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.

For more options, visit https://groups.google.com/d/optout.

--
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.

--
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.

--
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/b634aad2-de3f-4845-9d47-363ef5458eb5%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages