Read animation values from a text file

By Luca Pataracchia, Autodesk (formerly Alias) Toronto Support.

This procedure will read an ASCII file (use an explicit path) and will key translation values for the specified object ($objectName).

The file must be laid out in the following format:

frameNumber Xtranslation Ytranslation Ztranslation

For example:

7 2 4 6
10 3.7 3.6 9.3 
20 7.4 5.7 3.9
24 4.2 6.789 2.457
32 16.2 3.45 9.75 

This script is an example of how you can import animation from an ascii file. This proc can be changed depending on what kind of animation you need to import. You would have to modify the script to suit the format of your ascii file.

global proc getAnim(string $fileName, string $objectName)
{
 //open the file for reading
 $fileId=`fopen $fileName "r"`;
 
 //get the first line of text
 string $nextLine = `fgetline $fileId`;
 
 //while $nextline is not emtpy(end of file) do the following 
 
 while ( size( $nextLine ) > 0 ) {
 	
 //tokenize(split) line into separate elements of an array 
 	string $rawAnimArray[];
 tokenize ($nextLine, " ",$rawAnimArray);
 //place each element of the array into separate variables 
 print $rawAnimArray;
 float $frame=$rawAnimArray[0];
 float $x=$rawAnimArray[1];
 float $y=$rawAnimArray[2];
 float $z=$rawAnimArray[3];
 
 //change the currentTime and set keys for tx, ty, tz
 currentTime $frame ;
 setAttr ($objectName+".tx") $x;
 setKeyframe ($objectName+".tx");
 setAttr ($objectName+".ty") $y;
 setKeyframe ($objectName+".ty");
 setAttr ($objectName+".tz") $z;
 setKeyframe ($objectName+".tz");
 //get the next line in the ascii file. 
 $nextLine = `fgetline $fileId`;
 }
 //close file 
 fclose $fileId;
}