Friday, March 21, 2014

Tip: Python to transfer imported animation curves onto scene

Happy Friday,

I was trying to reuse some animation i had saved in a different scene file where the animation was saved as animation curves but i didnt want to have to go by hand and select each animator control on my current scene and connect it up to the animation curves (.output attribute), so i wrote a short script that begins to try to do that.  The script still has a few bugs but its a useful start.

It works after user selects the animation curves (not animator controls) that were just imported from other scene.   Then running the script it copies over the animation data onto the current scene as long as it finds control. (some issues it still doesnt address is how to handle various ways of importing animation curves like either resolving by filename or name space ... also it will bug out if there is already animation on the animator control we want to put animation onto.

#cpAnimCurve copy selected animation curves onto current scene animator controls
#@author Nathaniel Anozie


#modify at own risk

#last updated: 03/21/2014 -- initial release

import maya.cmds as cmds
import sys

def cpAnimCurve():
    allSelAnimCurve =, type = 'animCurve')
    curve = ""
    for curve in allSelAnimCurve:
        #curve = "r_low_eyeLid_anim_translateZ"
        curveParts = curve.split("_")
        lenCurveParts = len(curveParts) #do error checking, should be at least 2
        if( lenCurveParts < 2):
            sys.exit("Animation Curve should have at least two parts in name separated by _")
        channel = ""
        animObject = ""
        channel = curveParts[lenCurveParts-1]
        animObject = "_".join(curveParts[:lenCurveParts-1])
        #connect the channel to animObject if it exists
        if( not cmds.objExists(animObject+'.'+channel) ):
            cmds.connectAttr( curve+'.'+'output', animObject+'.'+channel, force=True  ) #add error checking on locked channel


Hope this is helpful,