In Maya, the OpenMaya API provides several classes to listen to different kinds of events including DAG events. MNodeMessage class is responsible for tracking the changes of DAG nodes.
Here's an example of how to set up a callback to listen for name changes of a DAG node:
import maya.OpenMaya as om
def nodeChangedCallback(msg, plug, otherPlug, userData):
if msg & om.MNodeMessage.kConnectionMade:
print("Connection made.")
elif msg & om.MNodeMessage.kConnectionBroken:
print("Connection broken.")
# More event types can be handled here
# Suppose the DAG node we're interested in is "pCube1"
node = om.MSelectionList()
om.MGlobal.getSelectionListByName("pCube1", node)
obj = om.MObject()
node.getDependNode(0, obj)
callbackId = om.MNodeMessage.addNodeChangedCallback(obj, nodeChangedCallback)
In the code above, nodeChangedCallback
will be called when connect/disconnect events occur to "pCube1".
To stop listening, you can use om.MMessage.removeCallback(callbackId)
. Please remember to remove your callbacks when they're no longer needed because they will persist across different sessions in Maya, and you might encounter performance issues or unexpected behavior if you don't.
For listening to DAG-related changes like child added/removed, parent added/removed etc, instead of addNodeChangedCallback
, methods like addParentAddedCallback
, addParentRemovedCallback
, addChildAddedCallback
, addChildRemovedCallback
etc in MNodeMessage
can be used.
This should give you a basic idea. For a complete list of available messages/callbacks, refer to the official Maya API documentation. Please note that API-based solutions might be a bit overkill for something simple or if you don't plan to use it extensively, as it's much lower-level compared to using python commands or mel commands directly.