Maya监听 DAG events

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.

相关推荐
sduerfh1 个月前
pytorch3d导入maya相机位姿踩坑
pytorch·3d·maya
SHCQMY2 个月前
Maya清理空组上的Anim
maya
renwen15792 个月前
Maya---骨骼绑定
maya·骨骼绑定
q567315232 个月前
从Maya对象中获取单个坐标值
开发语言·数据库·sqlite·maya
吴梓穆3 个月前
Maya学习笔记:项目设置和快捷键
maya
renwen15793 个月前
Maya没有Arnold材质球
材质·maya
吴梓穆3 个月前
Maya学习笔记:物体的层级关系
maya
蜉蝣19983 个月前
CAD 3dsmax maya等autodesk系列专用卸载修复工具AutoRemove,一键完全彻底卸载删除软件的专用卸载工具
3dsmax·3dmax·maya·cad·inventor·revit
阿拉丁的梦3 个月前
maya-vray渲染蒙版
maya
阿拉丁的梦3 个月前
maya的重命名物体和材质工具(带ai过程)
python·maya