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.

MEL renaming

Posted by on Nov 19, 2009 in code, MEL, python | No Comments

Maya has a built in re-naming tool, Rename Selected Objects, on the right side of the Status Line. Select Rename from the menu of line input operations, to the left of the input box.

image of Maya's Rename selected objects dialog

Basic rename script using Python:

#rename selected Maya nodes. Adds 4 digit padding to numbered name.
import maya.cmds as mc
mySel = mc.ls(sl=True)
count = 0
for obj in mySel:
	count +=1
	#customize name below
	mc.rename( obj, 'myObj2_' + str('%04d' % count))

Below are various methods to rename using MEL:

Simple approach

int $i; //Select the object(s) that you want to rename string $rename[] = `ls -sl`; int $selSize = `size $rename`; for ($i =0; $i <$selSize ; $i++){ //Change newName_, below, to the name that you want to replace. rename ($rename[$i]) ("newName_" + ($i +1)); }

Search and replace.
Designed to find and replace part of the name of similarly named nodes.

searchReplaceNames "myNode" "newNode" "selected";

This will change names in a selection. For example you can change 100 selected nodes names from “myNode000 – myNode100” to “newNode000 – etc”. You can replace “selected” with “hierarchy” to rename all of the nodes in a selected hierarchy, or “all” to rename all nodes in the scene.

Search and replace 2

string $search = "myNode"; string $replace = "newNode"; string $Objs[] = `ls -sl`; for($obj in $Objs) { if(`startsWith $obj $search`) { rename $obj (`substitute $search $obj $replace`); } }

Rename and add custom number

//by pappapierre
//catch the object(s) current names, and clear your selection
string $sel[] = `ls -sl`;
select -cl;

//declare a string variable which will become your new name prefix

string $newNamePrefix = "myNewName";

//now run a loop to rename each elemnt in your selection list in the order
//they were selected
for($i=0 ; $i<size($sel); $i++){

//and rename your objects accordingly
rename $sel[$i] ($newNamePrefix + ($i+1));

}
//you can start your numbers anywhere by replacing the "1" in ($i+1) with
//your desired starting number.

Rename shape nodes:

//script 1. rename shapes unique names
string $rename[] = `ls -sl -dag -shapes`;

for ($i = 0; $i<size ($rename); $i++) {

rename($rename[$i]) ("myNewShapeName_" + ($i +1));

}

//script 2. remnameShapes (all the same name)

for ($i in `ls -sl -dag -shapes`) {

rename $i "myShape";

}

//script 3. rename types

for ($i in `ls -sl -dag -type “nCloth”`) {

rename $i "myShape";

Load More