Tagged: keyframes

Python import/export Maya keyframes script

These are fairly rudimentary python scripts, to export keys from Maya and import them back into Maya. Creates a key at every frame. May be interesting to people learning Python.

Export Maya Keys
[py]
import maya.cmds as mc
# Select all nodes with keys to export
# Customize the path below so that it points to, or creates, the file where you want to store the keyframe data
myFileObject=open(‘/mydataDir/data.txt’, ‘w’)
obs = mc.ls(sl=True)
theData = []
minTime = mc.playbackOptions(query=True, minTime=True)
maxTime = mc.playbackOptions(query=True, maxTime=True)
attributes = [‘translateX’, ‘translateY’, ‘translateZ’, ‘rotateX’, ‘rotateY’, ‘rotateZ’, ‘scaleX’, ‘scaleY’, ‘scaleZ’, ‘visibility’]
for time in range(minTime -1, maxTime +1):
mc.currentTime(time)
count = 0
for selection in obs:
name = obs[count]
count +=1
for theAttribute in attributes:
myAtF = mc.getAttr(selection + ‘.’ + theAttribute)
myAt = str(myAtF)
myTime = str(time)
theData.append(myAt + ‘ ‘ + myTime + ‘ ‘ + theAttribute + ‘ ‘ + name + ‘ \n’)
for lines in theData:
myFileObject.writelines(lines)
myFileObject.close()
[/py]

Import Maya Keys
This will import keys to nodes which are named identically to the nodes that were selected when the keys were exported.
[py]
import maya.cmds as mc
# Customize the path below so that it points to the file where you have exported the keyframe data
myFileObject=open(‘/mydataDir/data.txt’, ‘r’)
theLines = myFileObject.readlines()
count = 0
for line in theLines:
theLine = theLines[count]
theSplit = str.split(theLine)
theValue = theSplit[0]
theFrame = theSplit[1]
theAttribute = theSplit[2]
theName = theSplit[3]
mc.setKeyframe( theName, v=float(theValue), at=theAttribute, t =float(theFrame))
count +=1
myFileObject.close()
[/py]

Extracting raw keyframe data from After Effects keyframe data

You can easily “export” keyframe data from After Effects by copying the key frames in the timeline, then pasting them to a text file.

We want to extract raw key frame data, from After Effects formatted key fame data, via shell script, so that it can be used by Maya or other animation apps.

After Effects keyframe data looks like this:

Adobe After Effects 8.0 Keyframe Data Units Per Second 29.97 Source Width 900 Source Height 506 Source Pixel Aspect Ratio 1 Comp Pixel Aspect Ratio 1 Effects Sound Keys #1 Output 1 #22 Frame 1 0.000261479 2 0.00608461 3 0.0153011 4 0.0274689 5 0.0395869 6 0.0493024 7 0.0562797 8 0.0557284

We need something more like this:

0.000261479 0.00608461 0.0153011 0.0274689 0.0395869 0.0493024 0.0562797 0.0557284

Use this shell script to extract the keys: AfterEffectsKeyExtract.sh
Read this for a detailed explanation of the script.
Usage (in the terminal): path/to/AfterEffectsKeyExtract.sh path/to/keyFrameData.txt


Use this shell script to batch process several files:
BatchExtract.sh
Usage (in the terminal): path/to/BatchExtract.sh path/to/directory

Also see: http://oliverwolfson.com/importing-keyframe-data-to-maya/

Here is a primer on shell scripting. You can run a shell script on a Mac or in Linux, through the Terminal, or on a PC with a app like Cygwin.

UPDATE! The NEW! The python script, linked here, will do the same, import After Effects Sound Keys keyframes, from .txt files, to Maya, but it will also take care of the formatting, so there is no need to run the shell script on your After Effects keyframes before importing. Look for the NEW! script.