Skip to content

Instantly share code, notes, and snippets.

@tokejepsen
Last active December 21, 2015 04:19
Show Gist options
  • Save tokejepsen/6249096 to your computer and use it in GitHub Desktop.
Save tokejepsen/6249096 to your computer and use it in GitHub Desktop.
converts/retargets fbx to maya
import os
import maya.cmds as cmds
import maya.mel as mel
def exportClip(filePath):
cmds.select('grandpa:rig:character_Ctrl_Reference',hierarchy=True)
objs=cmds.ls(selection=True)
#splitting filePath
fileName=os.path.basename(filePath)
dirPath=os.path.dirname(filePath)
#exporting trax clip
animCurves=[]
for obj in objs:
curves=cmds.listConnections(obj, source=True, type="animCurve")
if curves!=None:
for c in curves:
animCurves.append(c)
startTime=cmds.playbackOptions(q=True,minTime=True)
endTime=cmds.playbackOptions(q=True,maxTime=True)
charSet=cmds.character(objs)
animClip=cmds.clip(charSet,sc=0,leaveOriginal=True,allAbsolute=True,startTime=startTime,endTime=endTime)
sourceClip=cmds.clip(animClip,q=True,scn=True)
charClip=cmds.character(charSet,q=True,library=True)
cmds.select(charClip,sourceClip,animCurves,r=True)
oldFileName=cmds.file(dirPath+'/'+fileName.replace('.ma','.clip.ma'),type='mayaAscii',exportSelected=True)
#load plugin
cmds.loadPlugin('fbxmaya.mll',quiet=True)
#collecting fbx files
mainDir='M:/00719_grandpa/assets/Animation/ros_bips'
fbxFiles=[]
for r,d,f in os.walk(mainDir):
for files in f:
if files.endswith(".fbx"):
fbxFiles.append(os.path.join(r,files))
fbxFiles=fbxFiles[86:-1]
for fbx in fbxFiles:
count=fbxFiles.index(fbx)
print 'Doing %s: %s of %s' % (fbx,count+1,len(fbxFiles))
#input/output data
inputDir=fbx
outputDir=os.path.dirname(fbx)
outputDir+='/'+os.path.basename(fbx.replace('.fbx','.ma'))
#importing setup file
cmd='file -import -type "mayaAscii" -ra true -mergeNamespacesOnClash false -namespace ":" -options "v=0;" -pr "M:/00719_grandpa/assets/Characters/Grandpa/publish/bip_setup.mocap.v001.ma";'
mel.eval(cmd)
#replace reference
cmds.file(fbx,loadReference='attack01RN')
#setting timeline
lastKey=int(cmds.findKeyframe( 'mocap:Bip001FBXASC032LFBXASC032Foot', which="last" ))
#firstKey=int(cmds.findKeyframe( 'mocap:Bip001FBXASC032LFBXASC032Foot', which="first" ))
firstKey=0
cmds.playbackOptions(min=firstKey)
cmds.playbackOptions(max=lastKey)
cmds.playbackOptions(ast=firstKey)
cmds.playbackOptions(aet=lastKey)
cmds.currentTime(int(lastKey)/2)
#baking anim
mel.eval('HIKCharacterControlsTool;')
mel.eval('hikBakeToControlRig 0;')
#remove mocap
cmds.file(fbx,removeReference=True)
#delete HIK
cmd='hikEnableCharacter( "grandpa:rig:character", 2 );deleteCharacter("Character1");'
mel.eval(cmd)
#setting control input
mel.eval('mayaHIKsetRigInput( "grandpa:rig:character" );')
#exporting clip
exportClip(outputDir)
#saving file and reset
cmds.file( rename=outputDir.replace('.ma','.anim.ma') )
cmds.file(save=True)
cmds.file(newFile=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment