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 Python入门:创建locator、getAttr() 获取属性、setAttr() 设置属性、定位xform()
python·maya
梨轻巧5 天前
Maya动画基础:路径动画
maya
梨轻巧9 天前
Maya骨骼绑定插件:AdvancedSkeleton5.550安装
maya
梨轻巧9 天前
Maya动画基础:自动关键帧切换、曲线图编辑器、摄影机录制动画
maya
云澈ovo15 天前
光线追踪加速:NVIDIA OptiX在Maya中的着色器编译优化
maya·着色器
梨轻巧1 个月前
Maya绑定:手臂拉伸效果 大概思路
maya
梨轻巧1 个月前
Maya绑定:蒙皮和权重使用、经验
maya
梨轻巧1 个月前
Maya绑定:小球挤压拉伸变形详细绑定(晶格、簇、测量工具、节点编辑器)
maya
梨轻巧1 个月前
Maya绑定:渲染编辑器Hypershade简单使用,给小球添加材质纹理
材质·maya
梨轻巧2 个月前
Maya基础:烘焙动画
maya