OpenMaya Callbacks instead of Maya ScriptJobs

Whenever you want something to happen when your selection in Maya changes, the first thing that comes into your mind might be Maya ScriptJobs.
However I find them very slow and not reliable, especially since they are wrapped in mel.

Better use a callback from OpenMaya’s messaging system.

This is how you do it:

You will have to create a function that is called every time the selection changes. Make it run fast and with lots of error checking to avoid making your tool too slow.
In your function, get the selection and evaluate what you want to do with it.
The function could look something like this:

Don’t worry, you don’t have to use maya API but you can use pymel or even maya.cmds instead.
In my example I just print the names of the selected nodes however I would avoid any prints as they are slowing down Maya.

As the next step you have to tell Maya to run this function when the selection has changed. Just connect it with the OpenMaya callback like so:

When you don’t need it anymore you’ll have to disable the function call by disconnecting from the callback like this:

Things to avoid

  • too many tools that use the same type of callbacks
  • forgetting to disconnect the callback when your tool closes
  • print statements in the callback function
  • heavy code or checks in your function which could slow down your function

Try it out and see for yourself how nice they are.
In the maya documentation you will find loads of other callback messages you can connect to.

Please follow and like:

Leave a comment

Your email address will not be published.

*

2 thoughts on “OpenMaya Callbacks instead of Maya ScriptJobs”