在maya中,每当引用新文件时,我都想运行某个回调。我想用python做这个。
根据文件,这应该是可能的:https://help.autodesk.com/view/MAYAUL/2018/ENU/?guid=__py_ref_class_open_maya_1_1_m_scene_message_html
但是,当我为kAfterLoadReference或kAfterLoadReferenceAndRecordEdits注册回调时,函数永远不会被触发。我连接了一个调试器并设置了一个断点,回调函数甚至从未被输入。我已经尝试了addCallback
和addReferenceCallback
函数。
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)
当创建新引用或从卸载状态加载时,它将触发。