[Maya-Python] List all registered callbacks

2,491 views
Skip to first unread message

Marcus Ottosson

unread,
Apr 23, 2018, 11:06:17 PM4/23/18
to python_in...@googlegroups.com

Hi all,

With the Maya Python API (both 1.0 and 2.0) you can register callbacks, like so:

from maya.api import OpenMaya as om
om.MDGMessage.addTimeChangeCallback(...)

It’ll hand you an ID you can later use to deregister it.

def myCallback(time, clientData):
   pass

eventId = om.MDGMessage.addTimeChangeCallback(myCallback, None)
om.MMessage.removeCallback(eventId)

But what do you do when you haven’t kept track of the ID? I’m debugging a potential leak of events being registered but not unregistered and am looking for something along the lines of..

for func, eventId in om.MMessage.listCallbacks():
  print("EventID %d belongs to %s" % (func, eventId))

# EventID 153 belongs to 'myCallback'

I’m also curious about what events are registered per default and by third-party plug-ins or scripts and figure this would be an intuitive way of finding out.

How can I list all currently registered events?

Thanks,
Marcus

Robert White

unread,
Apr 24, 2018, 12:20:11 AM4/24/18
to Python Programming for Autodesk Maya
I'm not sure there is a simple way.
I went looking the last time I was doing serious work with callbacks, and eventually just settled on wrapping them with my own custom classes that could handle de-registration as needed.

The closest I could find was `MMessage.nodeCallbacks` which will return a list of callbacks associated with a node, but that won't track things like scene, ui, or other non-node based callbacks.

Justin Israel

unread,
Apr 24, 2018, 12:24:46 AM4/24/18
to python_in...@googlegroups.com
Yea, I wasn't aware of anything that would give you a list like this. I always considered the ids the same as raw pointers. Basically, don't leak them :-)
I like the idea Robert has of wrapping the ids into "smart" ids as in the vein of RAII

Shot in the dark, but maybe they don't expose a way to list the ids for the reason that it would give people a chance to muck with other registered callback? But, even if you could get a list of ids, what would you do with them? Callbacks can be registered by both C++ and Python, which means the callbacks would end up being opaque pointers to the caller.

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/3aa9077b-70c7-4607-a70c-aeeb1a53ee92%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Marcus Ottosson

unread,
Apr 25, 2018, 9:29:23 AM4/25/18
to python_in...@googlegroups.com

But, even if you could get a list of ids, what would you do with them?

I just need to tell whether they are growing in numbers or not. To help with debugging.

I went looking the last time I was doing serious work with callbacks, and eventually just settled on wrapping them with my own custom classes that could handle de-registration as needed.

Mm, I do this too, and they probably aren’t the problem, I’m just trying to make sure. How odd there wouldn’t be a registry of sorts. Who knows how many callbacks are running, could come from anywhere, especially the more complex scripts, frameworks and plug-ins.

Thanks anyway guys, and if someone is reading this from the future, do chime in with the solution!


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.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA3szE_D%3DsbzMoAdUm0Rihn7sm9hk_MJk-N4M%2B_tPgpPXw%40mail.gmail.com.

justin hidair

unread,
Apr 28, 2018, 4:10:24 PM4/28/18
to Python Programming for Autodesk Maya
I use MCallbackIdArray, and loop over it to delete  the callbacks , or even better you can pass the array for that...

Justin Israel

unread,
Apr 28, 2018, 9:36:23 PM4/28/18
to python_in...@googlegroups.com


On Sun, Apr 29, 2018, 8:10 AM justin hidair <justin...@gmail.com> wrote:
I use MCallbackIdArray, and loop over it to delete  the callbacks , or even better you can pass the array for that...


But Marcus wasn't having an issue storing his own callback ids (MCallbackIdArray is just an array) 
The question was how to track the global list of all registered callbacks. I imagine Marcus can store his own. 

--
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/1348d863-fdce-4532-98d0-b60b5672d577%40googlegroups.com.

justin hidair

unread,
Apr 28, 2018, 9:57:08 PM4/28/18
to python_in...@googlegroups.com

Sorry I misunderstood the question , dude on stackoverflow says no : https://stackoverflow.com/questions/46896907/open-maya-callbacks-in-python-or-c-query-all-callbacks-in-memory  , and as suggested it makes no sense to show it to clients, because they’d try to kill it and open a black hole , even if internally the id could be recognize it will be unmaintainable for the devs

 

Sent from Mail for Windows 10

--
You received this message because you are subscribed to a topic in the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python_inside_maya/dap89EvycNA/unsubscribe.
To unsubscribe from this group and all its topics, 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/CAPGFgA0uSP8zVsGRXorJ8QJq1nA_5iES2A1q7EyABApEgSj-Fw%40mail.gmail.com.

Reply all
Reply to author
Forward
0 new messages