Importing keyframe data to Maya

Posted by on May 16, 2010 in code, how to, Maya, MEL, python | One Comment

Use this python script to import keys to a single channel of each node in a group of selected objects. This setup, below, will import properly formatted keys to the .translateY attribute of selected objects.

This script will apply unique keys to each selected node if there is a corresponding number of data files in the source directory.

import os
import maya.cmds as mc
## add path to keyframe data files - just the source directory that contains them.
rootdir='/path/to/keys'
## "ty", in line 13, refers to the Maya attribute channel of the selected objects. Modify this attribute as necessary.
objs= mc.ls(sl=True)
for subdir, dirs, files in os.walk(rootdir):
    for thisFile, o in zip(files, objs):
        file = open((os.path.join(rootdir, thisFile)), 'r')
        lines = file.readlines()
        file.close()
        for i in range(len(lines)):
            mc.setKeyframe(o, at='ty', v=float(lines[i]), t=i, itt='linear', ott='linear')

See a sample of properly formatted keyframe data :
http://oliverwolfson.com/scripts/keyFrameData.txt

Also see http://oliverwolfson.com/extracting-raw-key-frame-data/

NEW! The script below will do the same as the script above, 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 a shell script on your After Effects keyframes before importing.

This script will apply unique keys to each selected node if there is a corresponding number of data files in the source directory.

##AE Import
import os
import maya.cmds as mc
## add path to keyframe data files - just the source directory that contains them.
rootdir='/Volumes/500GB/Work/Code/python/AEkeysImport/soundkeys'
## below is the attribute that you want to keyframe
theAttribute = 'translateY'
count = 0
objs= mc.ls(sl=True)
for subdir, dirs, files in os.walk(rootdir):
	dataFiles = [each for each in files if each.endswith('.txt')]
	for thisFile, o in zip(dataFiles, objs):
		theName = objs[count]
		count +=1
		dataFile = open((os.path.join(rootdir, thisFile)), 'r')
		lines = dataFile.readlines()
		dataFile.close()
		mylines = lines[10:-4]
		for eachLine in mylines:
			data = eachLine.split('\t')
			theFrame = data[1]
			theValue = data[2]
			mc.setKeyframe( o, v=float(theValue), at=theAttribute, t =float(theFrame))

Note: When pasting the keys from After Effects, be sure to use a code text editor like BB Edit or Text Wrangler. These apps will save your .txt files with UNIX line returns which is necessary to make this script work properly.

Technically the line breaks are invisible, but using the ‘tr’ command, in a UNIX shell, you could display them as any character you like, eg:

cat /path/some/file.txt | tr '\n' 'U'

In the above example, if the input file uses UNIX style line returns, \n ,each line will have a U at the end.

Email this to someoneTweet about this on TwitterShare on FacebookShare on Google+Share on LinkedInShare on StumbleUponShare on RedditDigg this

1 Comment

  1. Aloe Design
    February 7, 2012

    Script works as long as the file path is not too long (maya limitation). Also, for me it only worked on translational channels and I wasn’t able to use it on rotation “unexpected indent.” You don’t really need to use a script though to import keys. If you have the data in the same way you describe here but name it .mov instead of .txt then you can go to select the channel, go to the maya import option box, pick “move,” add the desired channel to that dialog and then bam, same result.

Leave a Reply