如何在将文件引用到场景时触发回调

How to trigger a callback on referencing a file into the scene

在 Maya 中,我想 运行 在任何时候引用新文件后进行特定的回调。我想在 python.

中执行此操作

根据文档,这应该是可能的:https://help.autodesk.com/view/MAYAUL/2018/ENU/?guid=__py_ref_class_open_maya_1_1_m_scene_message_html

但是当我为 kAfterLoadReference 或 kAfterLoadReferenceAndRecordEdits 注册回调时,该函数永远不会被触发。我连接了一个调试器并设置了一个断点,而回调函数甚至从未被输入过。我已经尝试了 addCallbackaddReferenceCallback 函数。

kAfterImport 按预期工作。我是不是漏掉了一些明显的东西,还是真的坏了?

已在 Maya 2018、2019 和 2020 中检查

#create a new file called cube.ma, has a cube in it
TEST_SCENE_NAME = 'cube.ma'

cmds.file(new=1, f=1)
cmds.polyCube()
cmds.file(rn=TEST_SCENE_NAME)
cmds.file(s=1, f=1, type='mayaAscii')
cmds.file(new=1, f=1)

#register callbacks
import maya.api.OpenMaya as om2

def onReferenceLoad(clientData=None):
    print '#'*20
    print 'This is a callback'
    print '#'*20

id1 = om2.MSceneMessage.addReferenceCallback(om2.MSceneMessage.kAfterLoadReferenceAndRecordEdits, onReferenceLoad)
id2 = om2.MSceneMessage.addCallback(om2.MSceneMessage.kAfterLoadReferenceAndRecordEdits, onReferenceLoad)

id3 = om2.MSceneMessage.addReferenceCallback(om2.MSceneMessage.kAfterLoadReference, onReferenceLoad)
id4 = om2.MSceneMessage.addCallback(om2.MSceneMessage.kAfterLoadReference, onReferenceLoad)

id5 = om2.MSceneMessage.addCallback(om2.MSceneMessage.kAfterImportReference, onReferenceLoad)
id6 = om2.MSceneMessage.addCallback(om2.MSceneMessage.kAfterImport, onReferenceLoad)

#would expect this to trigger the callback method 5 times, doesn't trigger at all
print 'Referencing the test file'
cmds.file(TEST_SCENE_NAME, r=1, type='mayaAscii')

#correctly triggers the one callback registered
print 'Importing the test file'
cmds.file(TEST_SCENE_NAME, i=1)

#unregister callbacks
om2.MSceneMessage.removeCallbacks([id1,id2,id3,id4,id5,id6])

在使用这些回调创建或导入引用时我也没有得到任何结果,但是这个应该能满足您的期望:

om2.MSceneMessage.addCallback(om2.MSceneMessage.kAfterCreateReference, onReferenceLoad)

创建新引用或从卸载状态加载时触发。