/*************************************************** Daniel Loran 07-09-2006 Lab 4 Illustrates building skeletons programmatically, applying an animation to the first skeleton "arm1" and then copying the animation from "arm1" to all the skeletons while preserving their position in space. main() has the necessary calls for demonstration ****************************************************/ proc buildArm(string $name){ // build arm with finger #1 eval("joint -position 0 0 -5 -name joint1" + $name); eval("joint -position 0 0 0 -name joint2" + $name); eval("joint -position 5 0 0 -name joint3" + $name); eval("joint -position 5 2 0 -name joint4" + $name); eval("joint -position 7 2 0 -name joint5" + $name); // finger #2 eval("select -replace joint3" + $name); eval("joint -position 5 -2 0 -name joint6" + $name); eval("joint -position 7 -2 0 -name joint7" + $name); // finger #3 eval("select -replace joint3" + $name); eval("joint -position 5 0 2 -name joint8" + $name); eval("joint -position 7 0 2 -name joint9" + $name); // finger #4 eval("select -replace joint3" + $name); eval("joint -position 5 0 -2 -name joint10" + $name); eval("joint -position 7 0 -2 -name joint11" + $name); // group to create root transform node with name = $name eval("group -name arm1 -world joint1" + $name); } proc buildArms(){ string $name; $name = "arm1"; buildArm($name); rotate -relative 0 -90 0 $name; move -relative -8 0 -4 $name; $name = "arm2"; buildArm($name); rotate -relative 0 180 0 $name; move -relative 1 0 -3 $name; $name = "arm3"; buildArm($name); rotate -relative 0 90 0 $name; move -relative 0 0 6 $name; $name = "arm4"; buildArm($name); move -relative -9 0 5 $name; select -clear; } /***************************************** Apply animation1 on arm1 ******************************************/ proc animation1(){ playbackOptions -minTime 1 -maxTime 90; setKeyframe -time 1 -value 0 "joint1arm1.rotateX"; setKeyframe -time 30 -value -90 "joint1arm1.rotateX"; setKeyframe -time 30 -value -90 "joint3arm1.rotateX"; setKeyframe -time 60 -value -360 "joint3arm1.rotateX"; setKeyframe -time 60 -value -90 "joint1arm1.rotateX"; setKeyframe -time 90 -value -180 "joint1arm1.rotateX"; } proc copySkeletonMotion( string $srcRootNode, string $destRootNode ) { float $srcPos[] = `xform -query -worldSpace -translation $srcRootNode`; float $stcRot[] = `xform -query -worldSpace -rotation $srcRootNode`; float $destPos[] = `xform -query -worldSpace -translation $destRootNode`; float $destRot[] = `xform -query -worldSpace -rotation $destRootNode`; string $srcNodes[] = `listRelatives -fullPath -children -allDescendents $srcRootNode`; $srcNodes[ size($srcNodes) ] = $srcRootNode; string $destNodes[] = `listRelatives -fullPath -children -allDescendents $destRootNode`; $destNodes[ size($destNodes) ] = $destRootNode; if( size($srcNodes) != size($destNodes) ){ error "Source skeleton and destination skeleton are structurely different"; return; } string $attrs[] = { "translateX", "translateY", "translateZ", "scaleX", "scaleY", "scaleZ", "rotateX", "rotateY", "rotateZ" }; int $i; for( $i=0; $i < size($srcNodes); $i++ ){ for( $attr in $attrs ){ string $inPlugs[] = `listConnections -plugs yes -source yes -destination no ($srcNodes[$i] + "." + $attr)`; if( size($inPlugs) ){ string $tokens[]; tokenize $inPlugs[0] "." $tokens; string $inNode = $tokens[0]; string $inAttr = $tokens[1]; string $dupInNodes[] = `duplicate -upstreamNodes $inNode`; connectAttr -force ($dupInNodes[0] + "." + $inAttr) ($destNodes[$i] + "." + $attr); } else{ $res = `getAttr ($srcNodes[$i] + "." + $attr)`; setAttr ($destNodes[$i] + "." + $attr) $res; } } } // move destination node to the initial position xform -translation $destPos[0] $destPos[1] $destPos[2] $destRootNode; xform -rotation $destRot[0] $destRot[1] $destRot[2] $destRootNode; } proc main(){ buildArms(); animation1(); copySkeletonMotion("arm1", "arm2"); copySkeletonMotion("arm1", "arm3"); copySkeletonMotion("arm1", "arm4"); }