Skip to content

Instantly share code, notes, and snippets.

@tokejepsen
Created December 17, 2015 10:12
Show Gist options
  • Save tokejepsen/d8e4ea049f8c75b5dd99 to your computer and use it in GitHub Desktop.
Save tokejepsen/d8e4ea049f8c75b5dd99 to your computer and use it in GitHub Desktop.
Mixamo Import Animation
import traceback
import pymel.core
import maya.mel
def importAnim():
sel = pymel.core.ls(selection=True)
if not sel:
pymel.core.system.error('Nothing is selected.')
return
pymel.core.loadPlugin('fbxmaya.mll', quiet=True)
basicFilter = "*.fbx"
path = pymel.core.fileDialog2(fileFilter=basicFilter, dialogStyle=1, fileMode=1)
if not path:
return
path = path[0]
maya.mel.eval('FBXImport -file "{0}";'.format(path))
ns = ''
try:
ns = sel[0].name().split(':')[:-1]
ns = ':'.join(ns) + ':'
except:
pass
mapping = {'mixamorig:Hips': 'Root_CTRL', 'mixamorig:Spine': 'Spine_CTRL', 'mixamorig:Spine1': 'Spine1_CTRL',
'mixamorig:Spine2': 'Spine2_CTRL', 'mixamorig:Neck': 'Neck_CTRL', 'mixamorig:Head': 'Head_CTRL',
'mixamorig:LeftShoulder': 'LeftShoulder_CTRL', 'mixamorig:LeftArm': 'LeftArm_FK_CTRL',
'mixamorig:LeftForeArm': 'LeftForeArm_FK_CTRL', 'mixamorig:LeftHand': 'LeftHand_FK_CTRL',
'mixamorig:LeftHandThumb1': 'LeftHandThumb1_CTRL', 'mixamorig:LeftHandThumb2': 'LeftHandThumb2_CTRL',
'mixamorig:LeftHandThumb3': 'LeftHandThumb3_CTRL', 'mixamorig:LeftHandIndex1': 'LeftHandIndex1_CTRL',
'mixamorig:LeftHandIndex2': 'LeftHandIndex2_CTRL', 'mixamorig:LeftHandIndex3': 'LeftHandIndex3_CTRL',
'mixamorig:LeftHandMiddle1': 'LeftHandMiddle1_CTRL', 'mixamorig:LeftHandMiddle2': 'LeftHandMiddle2_CTRL',
'mixamorig:LeftHandMiddle3': 'LeftHandMiddle3_CTRL', 'mixamorig:LeftHandRing1': 'LeftHandRing1_CTRL',
'mixamorig:LeftHandRing2': 'LeftHandRing2_CTRL', 'mixamorig:LeftHandRing3': 'LeftHandRing3_CTRL',
'mixamorig:LeftHandPinky1': 'LeftHandPinky1_CTRL', 'mixamorig:LeftHandPinky2': 'LeftHandPinky2_CTRL',
'mixamorig:LeftHandPinky3': 'LeftHandPinky3_CTRL', 'mixamorig:RightShoulder': 'RightShoulder_CTRL',
'mixamorig:RightArm': 'RightArm_FK_CTRL', 'mixamorig:RightForeArm': 'RightForeArm_FK_CTRL',
'mixamorig:RightHand': 'RightHand_FK_CTRL', 'mixamorig:RightHandThumb1': 'RightHandThumb1_CTRL',
'mixamorig:RightHandThumb2': 'RightHandThumb2_CTRL', 'mixamorig:RightHandThumb3': 'RightHandThumb3_CTRL',
'mixamorig:RightHandIndex1': 'RightHandIndex1_CTRL', 'mixamorig:RightHandIndex2': 'RightHandIndex2_CTRL',
'mixamorig:RightHandIndex3': 'RightHandIndex3_CTRL', 'mixamorig:RightHandMiddle1': 'RightHandMiddle1_CTRL',
'mixamorig:RightHandMiddle2': 'RightHandMiddle2_CTRL', 'mixamorig:RightHandMiddle3': 'RightHandMiddle3_CTRL',
'mixamorig:RightHandRing1': 'RightHandRing1_CTRL', 'mixamorig:RightHandRing2': 'RightHandRing2_CTRL',
'mixamorig:RightHandRing3': 'RightHandRing3_CTRL', 'mixamorig:RightHandPinky1': 'RightHandPinky1_CTRL',
'mixamorig:RightHandPinky2': 'RightHandPinky2_CTRL', 'mixamorig:RightHandPinky3': 'RightHandPinky3_CTRL',
'mixamorig:LeftUpLeg': 'LeftUpLeg_FK_CTRL', 'mixamorig:LeftLeg': 'LeftLeg_FK_CTRL',
'mixamorig:LeftFoot': 'LeftFoot_FK_CTRL', 'mixamorig:LeftToeBase': 'LeftToeBase_FK_CTRL',
'mixamorig:RightUpLeg': 'RightUpLeg_FK_CTRL', 'mixamorig:RightLeg': 'RightLeg_FK_CTRL',
'mixamorig:RightFoot': 'RightFoot_FK_CTRL', 'mixamorig:RightToeBase': 'RightToeBase_FK_CTRL'}
attrs = ['tx', 'ty', 'tz', 'rx', 'ry', 'rz']
for key in mapping:
src = pymel.core.PyNode(key)
try:
dst = pymel.core.PyNode(mapping[key])
except:
dst = pymel.core.PyNode(ns + mapping[key])
for attr in attrs:
count = pymel.core.keyframe(src, attribute=attr, query=True, keyframeCount=True)
if count:
keys = pymel.core.keyframe(src.name() + '.' + attr, index=(0, count - 1), query=True)
pymel.core.copyKey(src.name(), time=(keys[0], keys[-1]), attribute=attr, option="curve")
pymel.core.pasteKey(dst.name(), attribute=attr)
root = pymel.core.PyNode('mixamorig:Hips')
root_ctrl = None
try:
root_ctrl = pymel.core.PyNode('Root_CTRL')
except:
root_ctrl = pymel.core.PyNode(ns + 'Root_CTRL')
pymel.core.select(root_ctrl)
anim_layer = pymel.core.animation.animLayer(addSelectedObjects=True)
t = root.getTranslation(space='world')
root_ctrl.setTranslation(t, space='world')
r = root.getRotation(space='world')
root_ctrl.setRotation(r, space='world')
pymel.core.setKeyframe(root_ctrl, animLayer=anim_layer.name())
try:
pymel.core.PyNode('LeftFoot_Options').FK_IK.set(0)
pymel.core.PyNode('RightFoot_Options').FK_IK.set(0)
except:
pymel.core.PyNode(ns + 'LeftFoot_Options').FK_IK.set(0)
pymel.core.PyNode(ns + 'RightFoot_Options').FK_IK.set(0)
pymel.core.delete('mixamorig:Hips')
try:
importAnim()
except:
print traceback.format_exc()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment