Scripts for skeleton copy

来源:互联网 发布:手机屏幕录像软件ios 编辑:程序博客网 时间:2024/05/16 06:07

Copy the animation information form on skeleton to another. The two skeleton must be of same structure.

Function writes array data to a file
   1: // write the given array to a file
   2: proc writeArray(int $fileHnd, float $array[])
   3: {
   4:     float $v;
   5:     for($v in $array)
   6:     {
   7:         fwrite $fileHnd $v;
   8:     }
   9: }
Output the animation information of each joint
   1: // root joint: $rootNode
   2: // output file: $filename
   3: // animation range: $startFrame - $endFrame
   4: // world or object space: $outputWS
   5: proc outputJoints(string $rootNode, string $filename, float $startFrame, float $endFrame, int $outputWS)
   6: {
   7:     // open the file
   8:     int $fileHnd=`fopen $filename w`;
   9:     // check whether open correctly
  10:     if($fileHnd == 0)
  11:     {
  12:         error("Unable to open output file "+$filename+" for writing");
  13:         return;
  14:     }
  15:     
  16:     // get a list of all the child joints
  17:     string $childNodes[]=`listRelatives -fullPath -type joint -allDescendents $rootNode`;
  18:     // root node first, followed by child nodes
  19:     string $rn[]={$rootNode};
  20:     string $nodes[]=stringArrayCatenate($rn, $childNodes);
  21:     
  22:     // store the current time to return later
  23:     float $cTime=`currentTime -query`;
  24:     
  25:     // decide which space is used
  26:     string $spaceFlag=($outputWS)?"-worldSpace":"-objectSpace";
  27:     
  28:     print("/nOutputting joints...");
  29:     float $t;
  30:     // iterate over every frame
  31:     for($t=$startFrame; $t<=$endFrame;++$t)
  32:     {
  33:         // set current time to $t, without updating the scene
  34:         currentTime -update false $t;
  35:         // output
  36:         fwrite $fileHnd $t;
  37:         print("/nFrame: "+$t);
  38:         
  39:         for($node in $nodes)
  40:         {
  41:             fwrite $fileHnd $node;
  42:             print("/n Joint: "+$node);
  43:             
  44:             // translation, rotation, scale infromation
  45:             float $pos[]=`xform $spaceFlag -query -translation $node`;
  46:             float $rot[]=`xform $spaceFlag -query -rotation $node`;
  47:             float $scl[]=`xform $spaceFlag -query -relative -scale $node`;
  48:             
  49:             writeArray($fileHnd, $pos);
  50:             writeArray($fileHnd, $rot);
  51:             writeArray($fileHnd, $scl);
  52:             
  53:             print("/n pos=["+$pos[0]+" "+$pos[1]+" "+$pos[2]+"]");
  54:             print("/n rot=["+$rot[0]+" "+$rot[1]+" "+$rot[2]+"]");
  55:             print("/n scl=["+$scl[0]+" "+$scl[1]+" "+$scl[2]+"]");
  56:         }
  57:     }
  58:     
  59:     // restore current time to the previous time
  60:     currentTime -update false $cTime;
  61:     
  62:     // close the file handle
  63:     fclose $fileHnd;
  64: }
Scale the size of the skeleton
   1: // scale skeleton script
   2: proc scaleSkeleton(string $rootNode, float $scale)
   3: {
   4:     // get all the child nodes
   5:     // using fullpath to avoid name conflicts
   6:     string $childs[]=`listRelatives -fullPath -type joint -allDescendents $rootNode`;
   7:     
   8:     for($child in $childs)
   9:     {
  10:         // retrive the relative position to its parent
  11:         float $pos[]=`joint -query -relative -position $child`;
  12:         
  13:         // scale the relative position
  14:         $pos[0]*=$scale;
  15:         $pos[1]*=$scale;
  16:         $pos[2]*=$scale;
  17:         // update the positoin
  18:         joint -edit -relative -position $pos[0] $pos[1] $pos[1] $child;
  19:     }
  20: }
Copy skeleton motion
   1: // copy skeleton motion script
   2: proc copySkeletonMotion(string $srcRootNode, string $destRootNode)
   3: {
   4:     // the world space positions of the two root nodes
   5:     float $srcPos[]=`xform -query -worldSpace -translation $srcRootNode`;
   6:     float $destPos[]=`xform -query -worldSpace -translation $destRootNode`;
   7:     
   8:     // retrieve all the child nodes of the source skeleton
   9:     string $srcNodes[]=`listRelatives -fullPath -allDescendents $srcRootNode`;
  10:     // the root node is added to the end
  11:     $srcNodes[size($srcNodes)]=$srcRootNode;
  12:     
  13:     // retrieve all the child nodes of the destination skeleton
  14:     string $destNodes[]=`listRelatives -fullPath -allDescendents $destRootNode`;
  15:     // the root node is added to the end
  16:     $destNodes[size($destNodes)]=$destRootNode;
  17:     
  18:     // exit if the hierachy is different
  19:     if(size($srcNodes)!=size($destNodes))
  20:     {
  21:         error "Source skeleton and destination skeleton are structurely different";
  22:         return ;
  23:     }
  24:     
  25:     // animated parameters want to copy, add more if you like
  26:     string $attrs[]={"translateX", "translateY", "translateZ",
  27:                     "scaleX", "scaleY", "scaleZ",
  28:                 "rotateX", "rotateY", "rotateZ"};
  29:     
  30:     int $i;
  31:     // iterate all the nodes
  32:     for($i=0;$i < size($srcNodes);++$i)
  33:     {
  34:         // iterate all the attributes to copy
  35:         for($attr in $attrs)
  36:         {
  37:             // retrieve the incoming connection to the current attribute
  38:             // if exists, the attribute is controlled by another node
  39:             string $inPlugs[]=`listConnections -plugs yes -destination yes ($srcNodes[$i]+"."+$attr)`;
  40:             
  41:             if(size($inPlugs))
  42:             {
  43:                 // the form for a plug is <node_name>.<attribute_name>
  44:                 string $tokens[];
  45:                 tokenize $inPlugs[0] "." $tokens;
  46:                 string $inNode=$tokens[0];
  47:                 string $inAttr=$tokens[1];
  48:                 
  49:                 // given the incoming node, duplicate it
  50:                 string $dupInNodes[]=`duplicate -upstreamNodes $inNode`;
  51:                 // connect the new duplicate node's output attribute to the destination node's attribute
  52:                 connectAttr -force ($dupInNodes[0]+"."+$inAttr) ($destNodes[$i]+"."+$attr);
  53:                 /*
  54:                 If you would like to have both skeletons always to have the same animation:
  55:                 connectAttr -force ($srcNodes[$i]+"."+$inAttr) ($destNodes[$i]+"."+$attr);
  56:                 */
  57:             }else
  58:             {
  59:                 $res=`getAttr ($srcNodes[$i]+"."+$attr)`;
  60:                 setAttr ($destNodes[$i]+"."+$attr) $res;
  61:             }
  62:         }
  63:     }
  64:     
  65:     // move the destination skeleton to its original position
  66:     // a new transformation node
  67:     string $moveRoot;
  68:     // get the parent of the root node
  69:     string $parentNodes[]=`listRelatives -parent $destRootNode`;
  70:     // determine if the parent is one of the _moveSkeleton nodes this procedure have created
  71:     string $found=`match "_moveSkeleton" $parentNodes[0]`;
  72:     // if is, set current parent to moveRoot
  73:     if(size($found))
  74:         $moveRoot=$parentNodes[0];
  75:     else
  76:         //group destination nodes under moveRoot
  77:         $moveRoot=`group -name "_moveSkeleton" -world $destRootNode`;
  78:         
  79:     move -worldSpace ($destPos[0]-$srcPos[0]) ($destPos[1]-$srcPos[1]) ($destPos[2]-$srcPos[2]) $moveRoot;
  80: }
原创粉丝点击