Mel Script - Sampling Animation Data [API]

 

 

 

Sampling animation data involves taking samples of transformations every n number of frames. Within mel you can use the currentTime function to set the current position on the timeline. For example,

currentTime 18;

sets the current position on the timeline to frame 18.

 

 

 

 

 

Sampling World Space Bone Matrices

By building a list of all transforms we can sample the world space matrix transforms of each bone using the xform command. For example :

 

 

 

 
		// this function prints out all world space matrices for all bones in
		// the scene for each step inbetween the frame range.
		//
		proc sampleAnimations(float $start,float $end,float $step) {
			$bones = `ls -type "transform"`;
			for( ; $start <= $end; $start += $step ) {
		
				// set the current frame
				currentTime($start);
		
				// print all bone matrices
				print("frame " + $start + "\n");
		
				for($bone in $bones) {
		
					// get bone matrix in world space
					$m = `xform -q -ws -m $bone`;
		
					// print matrix & bone name
					print("\t"+$bone+"\n");
					print("\t\t"+$m[0] +" "+ $m[1] +" "+ $m[2] +" "+ $m[3] +"\n"+ 
						  "\t\t"+$m[4] +" "+ $m[5] +" "+ $m[6] +" "+ $m[7] +"\n"+ 
						  "\t\t"+$m[8] +" "+ $m[9] +" "+ $m[10]+" "+ $m[11]+"\n"+ 
						  "\t\t"+$m[12]+" "+ $m[13]+" "+ $m[14]+" "+ $m[15]+"\n");
		
				}
			}
		}			
	

 

 

 

 

Sampling Local Space Transformations

To sample the bones using local translation, scale and rotation values, we can do something like this ....

 

 

 

 
		// this function prints out all world space matrices for all bones in
		// the scene for each step inbetween the frame range.
		//
		proc sampleAnimations(float $start,float $end,float $step) {
			$bones = `ls -type "transform"`;
			for( ; $start <= $end; $start += $step ) {
		
				// set the current frame
				currentTime($start);
	
				// print all bone matrices
				print("frame " + $start + "\n");
		
				for($bone in $bones) {
		
					// get bone matrix in object space
					$scale = `xform -q -os -r -scale $bone`;
					$rotate = `xform -q -os -rotation $bone`;
					$translate = `xform -q -os -translation $bone`;
		
					// print bone name
					print("\t"+$bone+"\n");
					print("\t\tscale "+
					      $scale[0]+" "+$scale[1]+" "+$scale[2]+"\n"+ 
						  "\t\trotate "+
					      $rotate[0]+" "+$rotate[1]+" "+$rotate[2]+"\n"+
						  "\t\ttranslate "+
						  $translate[0]+" "+$translate[1]+" "+$translate[2]+"\n");
		
				}
			}
		}