Bottom Page

Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Python in MAYA
Right basically I've got to submit this tomorrow and I'm new to Python and have no idea what  any of it means. I've been provided with a MAYA file and this script that is used within it. I know the code is not yet complete, but could anyone possibly add comments to all of the necessary lines of code explaining what they do within MAYA? would be a great help thanks (Also sorry about the format of the script not being correct and not showing indents and such correctly, I can't add the text with it keeping its original layout)

import maya.cmds as mc #imports MEL commands as Python wrapper
import os #importing operate system preferences
import copy
dirPath = ""#declares variable as string 

def importFBX(_filepath): #defines importFBX as a file directory 
global dirPath
cmds.file(_filepath, i = True, type = "FBX")
path, filename = os.path.split(_filepath) #runs file
return True

except RuntimeError:

_filepath = dirPath + "\\" +_filepath
cmds.file(_filepath, i = True, type = "FBX")
path, filename = os.path.split(_filepath)
return True

except RuntimeError as er:
print "unable to import FBX file: %s . File may be missing or corrupted" % _filepath
print er
return False

def cd(_newPath):
global dirPath 
dirPath = str(_newPath)

def loadExpressions(_path = ""):
global dirPath
expressions = {}  # dictionary of dictionaries
f = open(_path + '\\expressions.txt', 'r')  

currentExpressionId = ''
currentTarget = ''
currentExpression = {}

for line in f:
line = line[:-1]
print "line:" + line
print "exId:" + currentExpressionId
print "skId:" + currentTarget

if line == "EXID":        
currentExpressionId = ''

elif currentExpressionId == '':        
currentExpressionId = str(line)

elif currentTarget == '':   
strs = str(line).split(':')
num = len(strs)
currentTarget = strs[num-1]

currentExpression[currentTarget] = float(line)
expressions[currentExpressionId] = {}
expressions[currentExpressionId] = copy.deepcopy(currentExpression)
currentTarget = ''
print("ShapeKey added")
print ("expression dict")
print (expressions)
print ("------")
return expressions

def writeExpression(name, _path = ""):
global dirPath
if _path == "":
_path = dirPath + '\expressions.txt'

f = open(_path, 'a')  
f.write("EXID\n" + str(name) + "\n")

blendShapes = = 'blendShape', an=True)
print (blendShapes)

for blend in blendShapes:

def generateExpressionString(_blend):
global dirPath
expressionString = ""
expressions = mc.listAttr(_blend + '.w', m = True)
#print (expressions)

for i in expressions:
weight = mc.getAttr(_blend + '.' + i)
expressionString += _blend.lstrip(":") + ".%s\n" % i 
expressionString += "%s\n" % weight

return expressionString

def generateBlendShapeTarget(_expressionID, _expression, _basemodel):
global dirPath
print('Building %s expression' %_expressionID)

for AU in _expression: # Action Unit
mc.setAttr(AU, _expression[AU])
except RuntimeError as err:
# try:
# with parent name appended 
# will need to do a search for it

tempObjs = mc.duplicate( _basemodel , n=_expressionID)  
tempObjId = mc.bakePartialHistory( tempObjs, preCache=True )

for AU in _expression: # Action Unit
mc.setAttr(AU, 0.0)
except RuntimeError as err:

print('successfully built %s expression' %_expressionID)
return tempObjs

def buildShapeKeyExpressions():
global dirPath   #defines "buildshapekeyExpressions as a global variable so it can be called upon in the entire script
objs = selection=True, sn=True)

if len(objs) == 0: clear=True )
geometry =
transforms = mc.listRelatives(geometry, p=True, path=True), r=True)

objs = selection=True, sn=True)

if len(objs) == 0:
mc.error("no poly objects detected") 

expressions = loadExpressions(dirPath)
oldexpressions = blendShapes = = 'blendShape', an=True)

finalObj = mc.polyUnite( *objs, o=True, ch=True, n='character' );
except RuntimeError:
print("Error: unable to Unite objects, are non-poly obects selected?")# print error message clear=True )

print("creating BlendShape targets")

newObjs = [];
for exID in expressions:

newObjs = generateBlendShapeTarget(exID, expressions[exID], finalObj[0]) newObjs[0], add=True ) #create a new blend shape and selects it finalObj[0], add=True ) #
mc.blendShape(n='Expressions') # build shape keys, last selected object is base finalObj[0], d=True ) # deselect the final obj

for old in oldexpressions:, add=True)#loop that selects old and adds a true value

mc.delete( selection=True, sn=True)) # delete all inbetween objects clear=True )
print("Done :)")#displays "Done :)"

cmds.showWindow()#shows window 

CreateWindow()#creates window
Larz60+ wrote Apr-24-2017, 11:21 PM:
You need to re-post using code tags. see BBCODE
Without it you might as well post the back of an oatmeal box

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  PySide2 Hotkey works only once in Maya LalaGhost 0 73 May-19-2020, 05:05 PM
Last Post: LalaGhost
  Code Clean-up and Maya/Python Slicing issues mvvthology 1 510 Aug-05-2019, 10:43 PM
Last Post: Yoriz
  Maya Python Noob Question Iurii_Ledin 2 847 Jun-08-2018, 09:09 PM
Last Post: Iurii_Ledin

Forum Jump:

Users browsing this thread: 1 Guest(s)