Reputation: 811
Hi a beginner in writing python scripts for Maya. I am trying to write a script that automates the process of opening a Maya file along with its references. Usually when the parent file and reference files are in different destinations Maya cannot open a file being referenced and you have to browse the filename to open it. I am trying to automate this. When a user tries to open a file it should open with all its references. So far I have got this but the main part is what I am confused about.
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)
So when the function callFunc() is called, this is where all the action happens. Now I don't know how to proceed.
Upvotes: 0
Views: 11149
Reputation: 10281
Unless there's a specific reason to use pymel, I'd go with regular Maya commands:
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()
For more options on fileDialog2
and file
, check out the Maya Python docs at http://download.autodesk.com/global/docs/maya2014/en_us/CommandsPython/index.html
Upvotes: 2