Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* CRweightTools.mel Cody Ritchie 2001-2003
- A selection of tools to modify weights on meshes.
- Usage: To use it just select the mesh you want the weights transferred onto
- and type CRbakeWeights in the command line. The bones that you want transferred
- over MUST already be bound to the mesh, but they can have zero weight. The simplest
- thing is to create a single extra dummy bone, select that and your entire skeleton,
- and bind it to your mesh. Weight your entire mesh to the dummy bone. Now run the script
- and it should transfer the weights from the wrap to the mesh, as best as it can. It's
- usually pretty damn close results (as best as mathematically possible - sometimes is
- not possible with some deformers to exactly match the exact deformation).
- */
- global proc CRbakeWeights()
- {
- string $sel[]=`ls -sl`;
- for ($node in $sel)
- {
- string $shape[]=`listRelatives -f -s $node`;
- if (`nodeType $shape[0]`=="mesh") CRdoBakeWeights $node 0.001;
- else print ("node: "+$node+" is not a mesh\n");
- }
- }
- global proc CRdoBakeWeights(string $mesh, float $pruneVal)
- {
- waitCursor -state on;
- global string $gMainProgressBar;
- progressBar -e -bp -ii false -st ("Baking weights Stage 1 of 4") -pr 0 -max 100 $gMainProgressBar;
- string $hist[]=`listHistory $mesh`;
- string $boneList[]=`ls -type "joint" $hist`;
- string $verts[]=`ls -fl ($mesh+".vtx[*]")`;
- int $numVerts=`size($verts)`;
- float $vertPos[];
- int $doIt=1;
- for ($i=0;$i<$numVerts;$i++)
- {
- float $pos[]=`xform -q -ws -t $verts[$i]`;
- $vertPos[$i]=$pos[0];
- }
- float $weights[];
- string $axis[]={".tx",".ty",".tz"};
- int $stepSize=35.0/`size($boneList)`;
- progressBar -e -bp -ii false -st ("Baking weights Stage 1 of 4") -s 10 -max 100 $gMainProgressBar;
- for ($i=0;$i<`size($boneList)`;$i++)
- {
- string $children[]=`listRelatives -f -c $boneList[$i]`;
- int $lockStates[];
- for ($j=0;$j<3;$j++)
- {
- $lockStates[$j]=`getAttr -l ($boneList[$i]+$axis[$j])`;
- setAttr -l 0 ($boneList[$i]+$axis[$j]);
- for ($k=0;$k<`size($children)`;$k++)
- {
- $lockStates[(3*$k)+3+$j]=`getAttr -l ($children[$k]+$axis[$j])`;
- setAttr -l 0 ($children[$k]+$axis[$j]);
- }
- }
- xform -r -ws -t 1 0 0 $boneList[$i];
- for ($child in $children) xform -r -ws -t -1 0 0 $child;
- for ($j=0;$j<$numVerts;$j++)
- {
- float $newPos[]=`xform -q -ws -t $verts[$j]`;
- $weights[($numVerts*$i)+$j]=$newPos[0]-$vertPos[$j];
- }
- xform -r -ws -t -1 0 0 $boneList[$i];
- for ($child in $children) xform -r -ws -t 1 0 0 $child;
- for ($j=0;$j<3;$j++)
- {
- setAttr -l $lockStates[$j] ($boneList[$i]+$axis[$j]);
- for ($k=0;$k<`size($children)`;$k++) setAttr -l $lockStates[(3*$k)+3+$j] ($children[$k]+$axis[$j]);
- }
- clear $lockStates;
- clear $children;
- progressBar -e -st ("Baking weights Stage 2 of 4") -s $stepSize $gMainProgressBar;
- }
- delete -ch $mesh;
- string $newCluster[]=`skinCluster -mi 2 -dr 4 -tsb -ibp $boneList $mesh`;
- setAttr ($newCluster[0]+".normalizeWeights") false;
- int $vertStepSize=($numVerts/10.0);
- int $count=0;
- for ($j=0;$j<$numVerts;$j++)
- {
- if(`progressBar -query -isCancelled $gMainProgressBar`) break;
- string $skinCmd="skinPercent";
- for ($i=0;$i<`size($boneList)`;$i++)
- {
- if ($weights[($numVerts*$i)+$j]>$pruneVal)
- $skinCmd+=(" -tv "+$boneList[$i]+" "+$weights[($numVerts*$i)+$j]);
- else
- $skinCmd+=(" -tv "+$boneList[$i]+" 0");
- }
- $skinCmd+=(" "+$newCluster[0]+" "+$verts[$j]+";\n");
- eval ($skinCmd);
- if ($count++==$vertStepSize){progressBar -e -st ("Baking weights Stage 3 of 4") -s 5 $gMainProgressBar;$count=0;}
- }
- progressBar -e -st ("Baking weights Stage 4 of 4") -s 5 $gMainProgressBar;
- setAttr ($newCluster[0]+".normalizeWeights") true;
- skinPercent -nrm 1 $newCluster[0] $verts;
- progressBar -e -st ("Baking weights Stage 4 of 4") -pr 100 $gMainProgressBar;
- progressBar -e -ep $gMainProgressBar;
- waitCursor -state off;
- }//bake weights
Add Comment
Please, Sign In to add comment