您是为Maya编写python脚本的初学者。我正在尝试编写一个脚本来自动打开Maya文件及其引用的过程。通常当父文件和引用文件位于不同的目标中时,Maya无法打开正在引用的文件,您必须浏览文件名才能打开它。我试图自动化这个。当用户尝试打开文件时,应该打开它的所有引用。到目前为止,我已经得到了这个,但主要部分是我感到困惑的。
import pymel.api as api
def callFunc():
print "hello world" # just a print cmd to check
print "registering a file reference call back"
cb = api.MSceneMessage_addCallback(api.MSceneMessage.kAfterOpen, callFunc())
def callbackOff():
api.MSceneMessage.removeCallback(cb)
因此,当调用函数callFunc()时,这就是所有操作发生的地方。现在我不知道该怎么办。
答案 0 :(得分:2)
除非有特殊原因要使用pymel,否则我会使用常规的Maya命令:
import maya.cmds as cmds
import os
def openFileAndRemapRefs():
multipleFilters = "Maya Files (*.ma *.mb);;Maya ASCII (*.ma);;Maya Binary (*.mb);;All Files (*.*)"
# Choose file to open
filename = cmds.fileDialog2(fileFilter=multipleFilters, dialogStyle=2, fileMode=1)
# Open file with no reference loaded
cmds.file( filename[0], open=True, force=True );
# Dir containing the references
refDir = 'C:/References'
# A list of any references found in the scene
references = cmds.ls(type='reference')
# For each reference found in scene, load it with the path leading up to it replaced
for ref in references:
refFilepath = cmds.referenceQuery(ref, f=True)
refFilename = os.path.basename( refFilepath )
print 'Reference ' + ref + ' found at: ' + cmds.referenceQuery(ref, f=True)
cmds.file( os.path.join(refDir, refFilename), loadReference=ref, options='v=0;')
openFileAndRemapRefs()
有关fileDialog2
和file
的更多选项,请查看http://download.autodesk.com/global/docs/maya2014/en_us/CommandsPython/index.html上的Maya Python文档