Guest User

Maya CRbakeWeights.mel

a guest
Jan 21st, 2020
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 KB | None | 0 0
  1. /* CRweightTools.mel Cody Ritchie 2001-2003
  2.  
  3. A selection of tools to modify weights on meshes.
  4.  
  5. Usage: To use it just select the mesh you want the weights transferred onto
  6. and type CRbakeWeights in the command line. The bones that you want transferred
  7. over MUST already be bound to the mesh, but they can have zero weight. The simplest
  8. thing is to create a single extra dummy bone, select that and your entire skeleton,
  9. and bind it to your mesh. Weight your entire mesh to the dummy bone. Now run the script
  10. and it should transfer the weights from the wrap to the mesh, as best as it can. It's
  11. usually pretty damn close results (as best as mathematically possible - sometimes is
  12. not possible with some deformers to exactly match the exact deformation).
  13.  
  14. */
  15. global proc CRbakeWeights()
  16. {
  17. string $sel[]=`ls -sl`;
  18. for ($node in $sel)
  19. {
  20. string $shape[]=`listRelatives -f -s $node`;
  21. if (`nodeType $shape[0]`=="mesh") CRdoBakeWeights $node 0.001;
  22. else print ("node: "+$node+" is not a mesh\n");
  23. }
  24. }
  25.  
  26. global proc CRdoBakeWeights(string $mesh, float $pruneVal)
  27. {
  28. waitCursor -state on;
  29. global string $gMainProgressBar;
  30. progressBar -e -bp -ii false -st ("Baking weights Stage 1 of 4") -pr 0 -max 100 $gMainProgressBar;
  31. string $hist[]=`listHistory $mesh`;
  32. string $boneList[]=`ls -type "joint" $hist`;
  33. string $verts[]=`ls -fl ($mesh+".vtx[*]")`;
  34. int $numVerts=`size($verts)`;
  35. float $vertPos[];
  36. int $doIt=1;
  37. for ($i=0;$i<$numVerts;$i++)
  38. {
  39. float $pos[]=`xform -q -ws -t $verts[$i]`;
  40. $vertPos[$i]=$pos[0];
  41. }
  42. float $weights[];
  43. string $axis[]={".tx",".ty",".tz"};
  44. int $stepSize=35.0/`size($boneList)`;
  45. progressBar -e -bp -ii false -st ("Baking weights Stage 1 of 4") -s 10 -max 100 $gMainProgressBar;
  46. for ($i=0;$i<`size($boneList)`;$i++)
  47. {
  48. string $children[]=`listRelatives -f -c $boneList[$i]`;
  49. int $lockStates[];
  50. for ($j=0;$j<3;$j++)
  51. {
  52. $lockStates[$j]=`getAttr -l ($boneList[$i]+$axis[$j])`;
  53. setAttr -l 0 ($boneList[$i]+$axis[$j]);
  54. for ($k=0;$k<`size($children)`;$k++)
  55. {
  56. $lockStates[(3*$k)+3+$j]=`getAttr -l ($children[$k]+$axis[$j])`;
  57. setAttr -l 0 ($children[$k]+$axis[$j]);
  58. }
  59. }
  60. xform -r -ws -t 1 0 0 $boneList[$i];
  61. for ($child in $children) xform -r -ws -t -1 0 0 $child;
  62. for ($j=0;$j<$numVerts;$j++)
  63. {
  64. float $newPos[]=`xform -q -ws -t $verts[$j]`;
  65. $weights[($numVerts*$i)+$j]=$newPos[0]-$vertPos[$j];
  66. }
  67. xform -r -ws -t -1 0 0 $boneList[$i];
  68. for ($child in $children) xform -r -ws -t 1 0 0 $child;
  69. for ($j=0;$j<3;$j++)
  70. {
  71. setAttr -l $lockStates[$j] ($boneList[$i]+$axis[$j]);
  72. for ($k=0;$k<`size($children)`;$k++) setAttr -l $lockStates[(3*$k)+3+$j] ($children[$k]+$axis[$j]);
  73. }
  74. clear $lockStates;
  75. clear $children;
  76. progressBar -e -st ("Baking weights Stage 2 of 4") -s $stepSize $gMainProgressBar;
  77. }
  78. delete -ch $mesh;
  79. string $newCluster[]=`skinCluster -mi 2 -dr 4 -tsb -ibp $boneList $mesh`;
  80. setAttr ($newCluster[0]+".normalizeWeights") false;
  81. int $vertStepSize=($numVerts/10.0);
  82. int $count=0;
  83. for ($j=0;$j<$numVerts;$j++)
  84. {
  85. if(`progressBar -query -isCancelled $gMainProgressBar`) break;
  86. string $skinCmd="skinPercent";
  87. for ($i=0;$i<`size($boneList)`;$i++)
  88. {
  89. if ($weights[($numVerts*$i)+$j]>$pruneVal)
  90. $skinCmd+=(" -tv "+$boneList[$i]+" "+$weights[($numVerts*$i)+$j]);
  91. else
  92. $skinCmd+=(" -tv "+$boneList[$i]+" 0");
  93. }
  94. $skinCmd+=(" "+$newCluster[0]+" "+$verts[$j]+";\n");
  95. eval ($skinCmd);
  96. if ($count++==$vertStepSize){progressBar -e -st ("Baking weights Stage 3 of 4") -s 5 $gMainProgressBar;$count=0;}
  97. }
  98. progressBar -e -st ("Baking weights Stage 4 of 4") -s 5 $gMainProgressBar;
  99. setAttr ($newCluster[0]+".normalizeWeights") true;
  100. skinPercent -nrm 1 $newCluster[0] $verts;
  101. progressBar -e -st ("Baking weights Stage 4 of 4") -pr 100 $gMainProgressBar;
  102. progressBar -e -ep $gMainProgressBar;
  103. waitCursor -state off;
  104. }//bake weights
Add Comment
Please, Sign In to add comment