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.

相关推荐
梨轻巧2 天前
Maya绑定:手臂拉伸效果 大概思路
maya
梨轻巧3 天前
Maya绑定:蒙皮和权重使用、经验
maya
梨轻巧3 天前
Maya绑定:小球挤压拉伸变形详细绑定(晶格、簇、测量工具、节点编辑器)
maya
梨轻巧3 天前
Maya绑定:渲染编辑器Hypershade简单使用,给小球添加材质纹理
材质·maya
梨轻巧14 天前
Maya基础:烘焙动画
maya
雪下的新火14 天前
多软件共修:模型重拓扑&模型减面
经验分享·blender·3dmax·maya·三维建模·重拓扑·c4d
梨轻巧18 天前
Maya绑定基础: FK 和 IK 介绍和使用
maya
共享笔记18 天前
Autodesk Maya 2026.2 全新功能详解:MotionMaker AI 动画、LookdevX 材质增强、USD 工作流优化
maya·autodesk
渲染101专业云渲染2 个月前
Blender 云渲染高效流程:渲染 101 集群加速实战
服务器·分布式·blender·maya·houdini
阿拉丁的梦2 个月前
【3D大比拼第一集】--max,maya,c4d,blender的命令搜索功能
blender·maya