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.

相关推荐
明月看潮生1 天前
青少年编程与数学 02-022 专业应用软件简介 03 三维建模及动画软件:Autodesk Maya
青少年编程·动画·maya·三维建模·编程与数学·应用软件
渲染101专业云渲染15 天前
云端算力革命:川翔云电脑如何重新定义创作自由
云计算·电脑·blender·maya·houdini
成都渲染101云渲染66661 个月前
Autodesk Maya运动设计概述
maya
Maya动画技术1 个月前
局部放大maya的视图HUD文字大小的方法
maya·修改视图hud文字大小
成都渲染101云渲染66662 个月前
【无标题】
maya
成都渲染101云渲染66662 个月前
blender云渲染指南2025版
前端·javascript·网络·blender·maya
渲染101专业云渲染2 个月前
川翔云电脑32G大显存集群机器上线!
云计算·电脑·blender·maya·houdini
子燕若水3 个月前
修改maya小部件操作器(manipulator,那个带有箭头和圆环的小部件坐标轴)
maya
子燕若水3 个月前
Maya软件中的约束基础:提高角色动画制作效率的关键技术
maya
子燕若水3 个月前
maya调整全局关节显示大小
maya