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.

相关推荐
吴梓穆5 天前
Maya学习笔记:项目设置和快捷键
maya
renwen15795 天前
Maya没有Arnold材质球
材质·maya
吴梓穆11 天前
Maya学习笔记:物体的层级关系
maya
蜉蝣199817 天前
CAD 3dsmax maya等autodesk系列专用卸载修复工具AutoRemove,一键完全彻底卸载删除软件的专用卸载工具
3dsmax·3dmax·maya·cad·inventor·revit
阿拉丁的梦21 天前
maya-vray渲染蒙版
maya
阿拉丁的梦21 天前
maya的重命名物体和材质工具(带ai过程)
python·maya
斯裕科技22 天前
新升级|优化航拍/倾斜模型好消息,支持处理多套贴图模型!
unity·ue5·3dsmax·虚拟现实·maya·增强现实
renwen15791 个月前
maya无法导出fbx
maya
Rjqjfcom1 个月前
Maya 2025.1新功能及安装教程和下载
人工智能·经验分享·3dsmax·photoshop·maya
广州虚拟动力-动捕&虚拟主播1 个月前
如何用3D人脸扫描设备建模面部细节,打造逼真3D虚拟人脸?
科技·游戏·3d·ue5·动画·娱乐·maya