Search code examples
pythonc++callbackmayamaya-api

Open Maya callBacks in Python or C++ query all callbacks in Memory


Hello, I'm wondering if there is a way to query all **User Created callbacks in Maya?, And in the same way, if I can query what objects or nodes are connected to them. I want to manage better the cleanliness in the memory at every time and not let any callback lost in memory.**

import maya.cmds as cmds
import maya.OpenMaya as OpenMaya
##########################################
class MayaSignals(object):
    def __init__(self):
        super(MayaSignals)       
        self.callback_list = []

    def setCallback(self, node):
        sel = OpenMaya.MSelectionList()
        sel.add(node)
        obj = OpenMaya.MObject()
        sel.getDependNode(0, obj)       
        idx = OpenMaya.MNodeMessage.addAttributeChangedCallback(obj, self.reading_node)
        self.callback_list.append(idx)

    def removeAllCallback(self):
        for idx,itm in enumerate(self.callback_list):
            print(idx, 'removed')
            OpenMaya.MMessage.removeCallback(itm)

    def reading_node(self, msg, plug, otherplug, *clientData):
        if msg & OpenMaya.MNodeMessage.kAttributeSet:
            print ('Attribute Changed On: %s' % plug.name())
        if msg & OpenMaya.MNodeMessage.kConnectionMade:
            print('Connection Created On: %s' % plug.name()) 
        if msg & maya.OpenMaya.MNodeMessage.kConnectionBroken:
            print ("Broken Connection On: %s" % plug.name())

################################################################################################        
node = cmds.ls(sl=True)[0]         #< ------ Node string type

#signal = MayaSignals()            # class to manage the callbacks
#signal.setCallback(node)
signal # # #                       # to keep the instance alive
signal.removeAllCallback()         # remove all into the array 


# ---------------------------------------------`-`---------------------------------------------- #

Here, I created an example where I tag an object to a callback, and print something when some conditions happen, also. add to array every callback ID to delete it later.

I just need a light, even if is in C++ I could check on the docs about the recommendations here.


Solution

  • There's no way of doing what you want. The closest you can come is to use MMessage::nodeCallbacks to retrieve the callback ids registered against a specific node.