Advertisement
Guest User

Untitled

a guest
May 9th, 2017
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 25.85 KB | None | 0 0
  1. //yzWeightSave2
  2. //manage your skinClusters with black magic
  3. //now works with clusters!
  4.  
  5.  
  6. //yzWeight algo lib ----------------------------------------------------
  7. proc float roundoff(float $value, int $dec) {
  8. // $value == value to round off
  9. // $dec == number of decimal places
  10. $bump=.5;
  11. if($value<0) $bump=-0.5;
  12.  
  13. $value=trunc($value*pow(10,$dec)+$bump)/pow(10,$dec);
  14. return $value;
  15. }
  16.  
  17. //get order from the x highest value in list
  18. proc int[] getSortIndex(float $list[], int $max) {
  19. //work list
  20. float $listTmp[]=$list;
  21.  
  22. //reverse sort
  23. float $listSortTmp[]=sort($list);
  24. float $listSort[];
  25. for($i=(size($listSortTmp)-1); $i>=0; $i--)
  26. $listSort[size($listSort)]=$listSortTmp[$i];
  27.  
  28. int $sort[];
  29. for($i=0; $i<size($listSort); $i++) {
  30. for($j=0; $j<size($listTmp); $j++) {
  31. if($listSort[$i]==$listTmp[$j]) {
  32. $sort[$i]=$j;
  33. $listTmp[$j]=-1;
  34. break;
  35. }
  36. }
  37. if(size($sort)==$max) break;
  38. }
  39. return $sort;
  40. }
  41.  
  42. proc float[] sortFloat(float $list[], int $sort[]) {
  43. float $listNew[];
  44. for($i=0; $i<size($sort); $i++) $listNew[$i]=$list[$sort[$i]];
  45. return $listNew;
  46. }
  47.  
  48. proc int[] sortInt(int $list[], int $sort[]) {
  49. int $listNew[];
  50. for($i=0; $i<size($sort); $i++) $listNew[$i]=$list[$sort[$i]];
  51. return $listNew;
  52. }
  53.  
  54. proc int stringArrayCompare(string $a[], string $b[]) {
  55. if(size($a)!=size($b)) return 0;
  56. for($i=0;$i<size($a);$i++) if($a[$i]!=$b[$i]) return 0;
  57. return 1;
  58. }
  59.  
  60. proc string[] getPointList(string $node) {
  61. //return `filterExpand -sm 31 -sm 28 -sm 36 -sm 47 -sm 46 ($node+".cp[*]")`;
  62. return `ls -fl ($node+".cp[*]")`;
  63. }
  64.  
  65.  
  66. //yzWeight lib ------------------------------------------------------------
  67.  
  68. proc string[] getParticleInfList(string $pcl) {
  69. $size=`getAttr -s ($pcl+".skinWeightList")`;
  70. string $infList[];
  71. if($size>1) $infList=getAttr($pcl+".skinWeightList[0:"+($size-1)+"]");
  72. else $infList[0]=getAttr($pcl+".skinWeightList[0]");
  73. return $infList;
  74. }
  75.  
  76. proc string[] getParticleClusterList(string $pcl) {
  77. $size=`getAttr -s ($pcl+".clusterList")`;
  78. string $infList[];
  79. if($size>1) $infList=getAttr($pcl+".clusterList[0:"+($size-1)+"]");
  80. else $infList[0]=getAttr($pcl+".clusterList[0]");
  81. return $infList;
  82. }
  83.  
  84.  
  85. proc resetSkinCluster(string $skin, int $ids[]) {
  86. int $cSize=`getAttr -s ($skin+".wl")`;
  87. if(size($ids)) $cSize=size($ids);
  88. int $iSize=size(`skinCluster -q -inf $skin`);
  89.  
  90. string $zero="";
  91. for($i=0;$i<$iSize;$i++) $zero+=" 0";
  92.  
  93. //progress bar
  94. int $amount=0; float $processing=0;
  95. progressWindow -title ("reset "+$skin) -progress 0 -status ("cv: 0/"+$cSize+" ("+$iSize+" influences)");
  96.  
  97. for($i=0;$i<$cSize;$i++) {
  98. $id=$i;
  99. if(size($ids)) $id=$ids[$i];
  100.  
  101. string $cmd=("setAttr "+$skin+".wl["+$id+"].w[0:"+($iSize-1)+"]"+$zero);
  102. eval $cmd;
  103.  
  104. //progress bar
  105. $processing=((float)$i/$cSize)*100;
  106. $amount=(int)$processing;
  107. progressWindow -e -progress $amount -status ("cv: "+$i+"/"+$cSize+" ("+$iSize+" influences)");
  108. }
  109. progressWindow -endProgress;
  110. }
  111.  
  112. global proc float[] getWeightID(string $pclShape, int $id) {
  113.  
  114. float $wList[];
  115.  
  116. $w=0;
  117. while(attributeExists(("w"+$w+"W"),$pclShape)) {//www combo
  118. float $weight[]=`getParticleAttr -array 1 -at ("w"+$w+"W") ($pclShape+".pt["+$id+"]")`;
  119. float $index[]=`getParticleAttr -array 1 -at ("i"+$w+"W") ($pclShape+".pt["+$id+"]")`;
  120. $wList[(int)$index[0]]=$weight[0];
  121. $w++;
  122. }
  123.  
  124. return $wList;
  125. }
  126.  
  127. proc int[] getRemap(string $old[], string $new[]) {
  128. int $remap[];
  129. for($i=0; $i<size($new); $i++) {
  130. for($j=0; $j<size($old); $j++) {
  131. if($old[$j]==$new[$i]) {
  132. $remap[$i]=$j;
  133. break;
  134. }
  135. else $remap[$i]=-1;
  136. }
  137. }
  138. return $remap;
  139. }
  140.  
  141. proc yzDeoptCluster(string $clst, string $mesh) {
  142.  
  143. string $clstSet[]=`listConnections -type objectSet $clst`;
  144. string $ptIn[]=`sets -q $clstSet[0]`;
  145. $ptIn=`ls -fl $ptIn`;
  146. string $ptAll[]=`ls -fl ($mesh+".cp[*]")`;
  147. string $ptOut[]=stringArrayRemove($ptIn, $ptAll);
  148.  
  149. if(size($ptOut)) {
  150. sets -fe $clstSet[0] $ptOut;
  151. percent -v 0 $clst $ptOut;
  152. }
  153. }
  154.  
  155. proc string getShape(string $mesh) {
  156. string $shapes[]=`listRelatives -pa -s $mesh`;
  157. for($shape in $shapes) {
  158. if(getAttr($shape+".intermediateObject")==0) return $shape;
  159. }
  160. return "";
  161. }
  162.  
  163. //yzWeight core ----------------------------------------------------------
  164.  
  165. global proc string yzWeightParticleBind(string $pcl, string $geo, int $mode, int $src) {
  166. //$mode: 0 is normal bind, 1 is null bind (all influences to 0)
  167. //$src: 0 from skin, 1 from clusters
  168.  
  169. //check args
  170. if($src==0) if(!attributeExists("skinWeightList",$pcl)) error "particle node is not a skin weight backup";
  171. if($src==1) if(!attributeExists("clusterList",$pcl)) error "particle node is not a cluster weight backup";
  172.  
  173. //processing
  174. string $infList[];
  175. int $maxInf;
  176.  
  177. if($src==0) {
  178. $infList=getParticleInfList($pcl);
  179. $maxInf=getAttr($pcl+".skinMaxInf");
  180. }
  181. if($src==1) {
  182. $infList=getParticleClusterList($pcl);
  183. $maxInf=getAttr($pcl+".clusterMaxInf");
  184. }
  185.  
  186. //new skin
  187. string $hasSkin[]=`listHistory -lv 2 $geo`;
  188. $hasSkin=`ls -et skinCluster $hasSkin`;
  189. if(size($hasSkin)) error ($geo+" has already a skinCluster");
  190.  
  191. //bind
  192. string $newSkin[];
  193.  
  194. if(nodeType($infList[0])=="joint")
  195. $newSkin=`skinCluster -omi 1 -mi $maxInf -tsb $infList[0] $geo`;
  196. else {
  197. select $infList[0];
  198. string $dummy=`joint`;
  199. $newSkin=`skinCluster -omi 1 -mi $maxInf -tsb $dummy $geo`;
  200. disconnectAttr ($dummy+".worldMatrix") ($newSkin[0]+".matrix[0]");
  201. connectAttr ($infList[0]+".worldMatrix") ($newSkin[0]+".matrix[0]");
  202. delete $dummy;
  203. if(!attributeExists("liw",$infList[0])) addAttr -at "bool" -sn "liw" -ln "lockInfluenceWeights" $infList[0];
  204. connectAttr ($infList[0]+".liw") ($newSkin[0]+".lw[0]");
  205. }
  206.  
  207. if($mode==1)
  208. resetSkinCluster $newSkin[0] {};
  209.  
  210. string $cmd="";
  211. if($mode==1) $cmd="skinCluster -e -lw true -wt 0 ";
  212. else $cmd="skinCluster -e ";
  213.  
  214. for($i=1; $i<size($infList); $i++) {
  215.  
  216. if(!objExists($infList[$i])) {
  217. warning($infList[$i]+" is missing. ->skipped");
  218. continue;
  219. }
  220. $cmd+=("-ai "+$infList[$i]+" ");
  221.  
  222. //influence objects
  223. string $shapes[]=`listRelatives -pa -s $infList[$i]`;
  224. if(size($shapes)) if(0) { //bloque a cause de je sais pas encore comment faire :)
  225. string $base[]=`duplicate $shapes[0]`;
  226. $base[0]=`rename $base[0] ($base[0]+"Base")`;
  227. string $baseShape[]=`listRelatives -pa -s $base[0]`;
  228. hide $base;
  229.  
  230. $cmd+=("-bsh "+$baseShape[0]+" -ug ");
  231.  
  232. setAttr ($newSkin[0]+".useComponents") 1;
  233. }
  234. }
  235. $cmd+=($newSkin[0]+";");
  236. eval $cmd;
  237.  
  238. if($mode==1)
  239. for($inf0 in $infList)
  240. if(attributeExists("liw",$inf0)) setAttr ($inf0+".liw") 0;
  241.  
  242.  
  243. //exit
  244. if($src==1) setAttr ($newSkin[0]+".normalizeWeights") 0;
  245. return $newSkin[0];
  246. }
  247.  
  248. global proc yzWeightRestoreClusters(string $pcl, string $geo, int $ids[], int $src) {
  249.  
  250. string $sel[]=`ls -sl`;
  251.  
  252. //check args
  253. string $srcList="clusterList";
  254. if($src==0) $srcList="skinWeightList";
  255. if(!attributeExists($srcList,$pcl)) error "particle node is not a weight backup";
  256.  
  257.  
  258. //get infos
  259. string $pclShape[]=`listRelatives -pa -s $pcl`;
  260.  
  261. int $maxInf;
  262. string $infList[];
  263. if($src==0) {
  264. $maxInf=getAttr($pcl+".skinMaxInf");
  265. $infList=getParticleInfList($pcl);
  266. } else {
  267. $maxInf=getAttr($pcl+".clusterMaxInf");
  268. $infList=getParticleClusterList($pcl);
  269. }
  270.  
  271. if(size($geo)==0) $geo=getAttr($pcl+".skinGeometry");
  272. string $pointList[]=getPointList($geo);
  273.  
  274. string $geoClusters[]=`listHistory $geo`;
  275. $geoClusters=`ls -et cluster $geoClusters`;
  276.  
  277.  
  278. //build valid cluster index for restore
  279. string $clusterList[];
  280.  
  281. int $clstRestoreID[], $ID=0;
  282. string $clstRestore[];
  283.  
  284. for($i=0; $i<$maxInf; $i++) {
  285. $clstRestoreID[$i]=-1; //do not restore
  286.  
  287. if(objExists($infList[$i])) {
  288. string $clst[]=`listConnections -type "cluster" $infList[$i]`;
  289. $clusterList[$i]=$clst[0];
  290.  
  291. $clstRestoreID[$i]=$ID;
  292. }
  293. }
  294.  
  295. //clean up cluster to be restored
  296. for($clst in $clstRestore) {
  297. yzDeoptCluster($clst, $geo);
  298. }
  299.  
  300. //restore weights
  301. if($src==1) {
  302. for($i=0; $i<$maxInf; $i++) {
  303. if($clstRestoreID[$i]!=-1) {
  304. float $wList[]=getAttr($pclShape[0]+".c"+$i+"W");
  305. for($p=0; $p<size($pointList); $p++) {
  306. percent -v $wList[$p] $clusterList[$i] $pointList[$p];
  307. }
  308. }
  309. }
  310. } else {
  311. for($i=0; $i<$maxInf; $i++) {
  312. float $iList[]=getAttr($pclShape[0]+".i"+$i+"W");
  313. float $wList[]=getAttr($pclShape[0]+".w"+$i+"W");
  314.  
  315. for($p=0; $p<size($pointList); $p++) {
  316. int $c=$iList[$p];
  317. if(objExists($clusterList[$c])) if(stringArrayContains($clusterList[$c], $geoClusters)) {
  318. percent -v $wList[$p] $clusterList[$c] $pointList[$p];
  319. }
  320. }
  321. }
  322. }
  323. }
  324.  
  325.  
  326. global proc yzWeightRestore(string $pcl, string $geo, int $ids[], int $src) {
  327.  
  328. string $sel[]=`ls -sl`;
  329.  
  330. //check args
  331. string $srcList="skinWeightList";
  332. if($src==1) $srcList="clusterList";
  333. if(!attributeExists($srcList,$pcl)) error "particle node is not a weight backup";
  334.  
  335. //get infos
  336. string $pclShape[]=`listRelatives -pa -s $pcl`;
  337. int $maxInf;
  338. string $infList[];
  339. if($src==0) {
  340. $maxInf=getAttr($pcl+".skinMaxInf");
  341. $infList=getParticleInfList($pcl);
  342. } else {
  343. $maxInf=getAttr($pcl+".clusterMaxInf");
  344. $infList=getParticleClusterList($pcl);
  345. }
  346.  
  347. if(size($geo)==0) $geo=getAttr($pcl+".skinGeometry");
  348. string $pointList[]=getPointList($geo);
  349.  
  350. //has skin
  351. string $skin[]=`listHistory $geo`;
  352. $skin=`ls -et skinCluster $skin`;
  353. //print "-skin: ";print $skin;
  354. //skin if not
  355. int $doReset=1;
  356. if(size($skin)==0) {
  357. $skin[0]=yzWeightParticleBind($pcl, $geo, 1, $src);
  358. $doReset=0;
  359. }
  360.  
  361. //remap?
  362. int $doRemap=0;
  363. string $infListNew[]=`skinCluster -q -inf $skin[0]`;
  364. if(!stringArrayCompare($infList,$infListNew))
  365. $doRemap=1;
  366.  
  367.  
  368. $iSize=size($infList);
  369. $cSize=size($pointList);
  370. if(size($ids)) $cSize=size($ids);
  371.  
  372. //get old inf list for remap
  373. int $remap[];
  374. if($doRemap) {
  375. string $infListOld[]=`skinCluster -q -inf $skin[0]`;
  376. $remap=getRemap($infListOld, $infList);
  377. }
  378.  
  379. //reskin
  380. if($doReset) resetSkinCluster($skin[0], $ids);
  381.  
  382. //transfer new values
  383. int $amount=0; float $processing=0;
  384. progressWindow -title ("importing "+$pcl) -progress $amount -status ("maxinf: 0/"+$maxInf);
  385.  
  386. if($src==0) {
  387. for($i=0;$i<$maxInf;$i++) {
  388. float $iList[]=getAttr($pclShape[0]+".i"+$i+"W");
  389. float $wList[]=getAttr($pclShape[0]+".w"+$i+"W");
  390. for($w=0;$w<$cSize;$w++) {
  391. if(size($ids)) $w=$ids[$w];
  392. if($doRemap) $iList[$w]=$remap[(int)$iList[$w]];
  393. if($iList[$w]!=-1)
  394. setAttr ($skin[0]+".wl["+$w+"].w["+$iList[$w]+"]") $wList[$w];
  395. }
  396. //progress bar
  397. $processing=((float)$i/$maxInf)*100;
  398. $amount=(int)$processing;
  399. progressWindow -e -progress $amount -status ("maxinf: "+$i+"/"+$maxInf);
  400. }
  401. } else {
  402. for($i=0;$i<$iSize;$i++) {
  403. float $cList[]=getAttr($pclShape[0]+".c"+$i+"W");
  404. for($w=0;$w<$cSize;$w++) {
  405. $imap=$i;
  406. if(size($ids)) $w=$ids[$w];
  407. if($doRemap) $imap=$remap[(int)$i];
  408. if($cList[$w]!=-1)
  409. setAttr ($skin[0]+".wl["+$w+"].w["+$imap+"]") $cList[$w];
  410. }
  411. //progress bar
  412. $processing=((float)$i/$maxInf)*100;
  413. $amount=(int)$processing;
  414. progressWindow -e -progress $amount -status ("maxinf: "+$i+"/"+$maxInf);
  415. }
  416.  
  417. }
  418. progressWindow -endProgress;
  419.  
  420. //normalize skin
  421. if($src==0) skinPercent -normalize true $skin[0] $geo;
  422.  
  423. //exit
  424. select $sel;
  425. }
  426.  
  427.  
  428. global proc string[] yzWeightStore(string $node, int $doStoreGeo) {
  429.  
  430. //got skin?
  431. string $skin[]=`listHistory $node`;
  432. $skin=`ls -et skinCluster $skin`;
  433.  
  434. //got clusters?
  435. string $clusters[]=`listHistory $node`;
  436. $clusters=`ls -et cluster $clusters`;
  437.  
  438. if(size($skin)<1 && size($clusters)<1) error ($node+" has no skin/clusters..");
  439. int $doSkin, $doClusters;
  440. if(size($skin)) $doSkin=1;
  441. if(size($clusters)) $doClusters=1;
  442.  
  443.  
  444. //get infos
  445. string $pointList[]=getPointList($node);
  446.  
  447. //skin infos
  448. string $infList[];
  449. int $maxInf;
  450. if($doSkin) {
  451. $infList=`skinCluster -q -inf $skin[0]`;
  452. $maxInf=getAttr($skin[0]+".mi");
  453. if(getAttr($skin[0]+".mmi")==0) $maxInf=5;
  454. if(size($infList)<$maxInf) $maxInf=size($infList);
  455. }
  456.  
  457. $iSize=size($infList);
  458. $cSize=size($pointList);
  459.  
  460. //clusters infos
  461. string $infListCluster[], $clusterSet[];
  462. for($i=0; $i<size($clusters); $i++) {
  463. string $handle[]=`listConnections ($clusters[$i]+".matrix")`;
  464. $infListCluster[$i]=$handle[0];
  465.  
  466. string $deformSet[]=`listConnections ($clusters[$i]+".msg")`;
  467. $clusterSet[$i]=$deformSet[0];
  468. }
  469.  
  470.  
  471. //get membership lists
  472. string $tmpMatrix=`createNode transform -n "yzWeightStoreTmp"`;
  473. addAttr -ln "skin" -at "long" -m;
  474. for($cl=0; $cl<size($clusters); $cl++)
  475. addAttr -ln ("cluster"+$cl) -at "long" -m;
  476. addAttr -ln "clusterInf" -at "long" -m;
  477.  
  478. //string $skinSet[]=`listConnections ($skin[0]+".msg")`;
  479. for($cl=0; $cl<size($clusters); $cl++) {
  480. string $clusterPoints[]=`sets -q $clusterSet[$cl]`;
  481. $clusterPoints=`filterExpand -sm 31 -sm 28 -sm 36 -sm 47 -sm 46 $clusterPoints`;
  482.  
  483. for($c=0; $c<$cSize; $c++) {
  484. setAttr ($tmpMatrix+".cluster"+$cl+"["+$c+"]") 0;
  485. if(stringArrayContains($pointList[$c], $clusterPoints))
  486. setAttr ($tmpMatrix+".cluster"+$cl+"["+$c+"]") 1;
  487. }
  488. }
  489.  
  490.  
  491. //pcl node
  492. $name=`match "[^|]*$" $node`;
  493. string $pcl[]=`particle -n ($name+"Weight#")`;
  494. $pcl=stringArrayCatenate({$pcl[0]},`listRelatives -pa -s $pcl[0]`);
  495. $pcl[1]=rename($pcl[1],$pcl[0]+"Shape");
  496.  
  497. //copy mesh
  498. string $copy;
  499. int $doDye=0;
  500.  
  501. if($doStoreGeo) {
  502. string $dupe[]=`duplicate $node`;
  503. string $shape=getShape($dupe[0]);
  504. if(nodeType($shape)=="mesh") $doDye=1;
  505.  
  506. if(!`pluginInfo -q -l decomposeMatrix`) loadPlugin decomposeMatrix;
  507. $dmx=`createNode decomposeMatrix`;
  508. $copy=`createNode transform -p $pcl[0] -n ($name+"Copy#")`;
  509.  
  510. connectAttr ($node+".wm") ($dmx+".imat");
  511. connectAttr ($dmx+".ot") ($copy+".t");
  512. connectAttr ($dmx+".or") ($copy+".r");
  513. connectAttr ($dmx+".os") ($copy+".s");
  514. connectAttr ($dmx+".osh") ($copy+".sh");
  515. refresh;
  516. delete $dmx;
  517.  
  518. parent -r -s $shape $copy;
  519. delete $dupe;
  520.  
  521. setAttr ($copy+".overrideEnabled") 1;
  522. setAttr ($copy+".overrideDisplayType") 2;
  523. setAttr ($copy+".displayColors") 1;
  524. polyOptions -colorMaterialChannel "ambient+diffuse" $copy;
  525. setAttr ($copy+".materialBlend") 0;
  526.  
  527. source "assignSG";
  528. assignSG("lambert1", $copy);
  529. }
  530.  
  531.  
  532. //attributes
  533. addAttr -ln "skinGeometry" -dt "string" $pcl[0];
  534. setAttr -type "string" ($pcl[0]+".skinGeometry") $node;
  535.  
  536. if($doSkin) {
  537. addAttr -ln "skinWeightList" -dt "string" -m -im 0 $pcl[0];
  538. for($i=0; $i<$iSize; $i++)
  539. setAttr -type "string" ($pcl[0]+".skinWeightList["+$i+"]") $infList[$i];
  540.  
  541. addAttr -ln "skinMaxInf" -at "long" -dv $maxInf $pcl[0];
  542. }
  543.  
  544. if($doClusters) {
  545. addAttr -ln "clusterList" -dt "string" -m -im 0 $pcl[0];
  546. for($i=0; $i<size($clusters); $i++)
  547. setAttr -type "string" ($pcl[0]+".clusterList["+$i+"]") $infListCluster[$i];
  548. addAttr -ln "clusterMaxInf" -at "long" $pcl[0];
  549. setAttr ($pcl[0]+".clusterMaxInf") (size($clusters));
  550. addAttr -ln "clusterSkinMaxInf" -at "long" $pcl[0];
  551. }
  552.  
  553. //attr tables
  554. if($doSkin) {
  555. for($i=0; $i<$maxInf; $i++) {
  556. addAttr -k 1 -ln ("i"+$i+"W") -dt doubleArray $pcl[1];
  557. addAttr -k 1 -ln ("i"+$i+"W0") -dt doubleArray $pcl[1];
  558. addAttr -k 1 -ln ("w"+$i+"W") -dt doubleArray $pcl[1];
  559. addAttr -k 1 -ln ("w"+$i+"W0") -dt doubleArray $pcl[1];
  560. }
  561. }
  562. if($doClusters) {
  563. for($i=0; $i<size($clusters); $i++) {
  564. addAttr -k 1 -ln ("c"+$i+"W") -dt doubleArray $pcl[1];
  565. addAttr -k 1 -ln ("c"+$i+"W0") -dt doubleArray $pcl[1];
  566. }
  567. }
  568.  
  569. //associate random colors to influences
  570. vector $wColor[];
  571. $colorSize=$iSize;
  572. if(!$doSkin) $colorSize=size($clusters);
  573.  
  574. for($i=0; $i<$colorSize; $i++)
  575. $wColor[$i]=<<rand(1),rand(1),rand(1)>>;
  576.  
  577. //display
  578. addAttr -ln "rgbPP" -dt vectorArray $pcl[1];
  579. addAttr -ln "rgbPP0" -dt vectorArray $pcl[1];
  580. addAttr -is true -ln "pointSize" -at long -min 1 -max 60 -dv 4 $pcl[1];
  581. addAttr -is true -ln "pointSize0" -at long -min 1 -max 60 -dv 4 $pcl[1];
  582.  
  583. string $cmdColor=("setAttr "+$copy+".vclr[0:"+(size($pointList)-1)+"].vrgb ");
  584. string $cmdAlpha=("setAttr "+$copy+".vclr[0:"+(size($pointList)-1)+"].vxal ");
  585.  
  586. int $iList[]; for($i=0; $i<$iSize; $i++) $iList[$i]=$i;
  587.  
  588.  
  589. //emit pcl from weights
  590. for($c=0; $c<$cSize; $c++) {
  591. int $amount=0; float $processing=0;
  592. progressWindow -title ("exporting "+$pcl[0]) -progress $amount -status ("cv: 1/"+$cSize);
  593.  
  594. //get weight location
  595. float $pos[]=`xform -q -ws -t $pointList[$c]`;
  596. vector $color=<<0,0,0>>;
  597.  
  598. //emit cmd
  599. $cmd=("emit -o "+$pcl[0]+" -pos "+$pos[0]+" "+$pos[1]+" "+$pos[2]+" ");
  600.  
  601.  
  602. //skin part
  603. if($doSkin) {
  604. //get weight
  605. float $wList[]=`skinPercent -q -v $skin[0] $pointList[$c]`;
  606. int $iListTmp[]=$iList;
  607.  
  608. if($iSize>$maxInf) {
  609. int $sort[]=getSortIndex($wList, $maxInf);
  610. $wList=sortFloat($wList, $sort);
  611. $iListTmp=sortInt($iList, $sort);
  612. }
  613.  
  614. for($w=0; $w<size($wList); $w++) {
  615. //$wList[$w]=roundoff($wList[$w], 4); //4 decimals only, prune law
  616. $cmd+=("-at i"+$w+"W -fv "+$iListTmp[$w]+" ");
  617. $cmd+=("-at w"+$w+"W -fv "+$wList[$w]+" ");
  618. $color+=$wColor[$iListTmp[$w]]*$wList[$w];
  619. }
  620. }
  621.  
  622. //clusters part
  623. if($doClusters) {
  624. for($cl=0; $cl<size($clusters); $cl++) {
  625. float $wList[]=`percent -q -v $clusters[$cl] $pointList[$c]`;
  626. if(!getAttr($tmpMatrix+".cluster"+$cl+"["+$c+"]"))
  627. $wList={0};
  628.  
  629. $cmd+=("-at c"+$cl+"W -fv "+$wList[0]+" ");
  630.  
  631. if($wList[0]>0) setAttr ($tmpMatrix+".clusterInf["+$c+"]") (getAttr($tmpMatrix+".clusterInf["+$c+"]")+1);
  632.  
  633. if(!$doSkin) $color+=$wColor[$cl]*$wList[0];
  634. }
  635. }
  636.  
  637. //mesh color
  638. if($doDye) {
  639. $cmdColor+=($color.x+" "+$color.y+" "+$color.z+" ");
  640. $cmdAlpha+="1 ";
  641. }
  642.  
  643. $cmd+=("-at rgbPP -vv "+$color.x+" "+$color.y+" "+$color.z+";");
  644. eval $cmd;
  645.  
  646. $processing+=((float)$c/$cSize)*100;
  647. $amount=(int)$processing;
  648. progressWindow -e -progress $amount -status ("cv: "+($c+1)+"/"+$cSize);
  649. }
  650.  
  651. //color mesh
  652. if($doDye) {
  653. eval $cmdColor;
  654. eval $cmdAlpha;
  655. setAttr ($copy+".displayImmediate") 1;
  656. setAttr ($copy+".displayImmediate") 0;
  657. }
  658.  
  659. //cluster maxInf lookup
  660. if($doClusters) {
  661. int $maxInf=0;
  662. for($c=0; $c<$cSize; $c++) {
  663. $clusterInf=getAttr($tmpMatrix+".clusterInf["+$c+"]");
  664. if($clusterInf>$maxInf) $maxInf=$clusterInf;
  665. }
  666. setAttr ($pcl[0]+".clusterSkinMaxInf") $maxInf;
  667. }
  668. delete $tmpMatrix;
  669.  
  670. //cache
  671. particle -e -cache 0 $pcl[1];
  672. saveInitialState $pcl[0];
  673.  
  674.  
  675. //exit
  676. progressWindow -endProgress;
  677. return $pcl;
  678. }
  679.  
  680. //UI ------------------------------------------------------------------------------------------------------
  681.  
  682. global proc yzwsStoreCB() {
  683. string $sel[]=`ls -sl -et transform`;
  684. string $weights[];
  685.  
  686. int $doStoreGeo=`checkBox -q -v yzwsStoreGeo`;
  687.  
  688. for($s0 in $sel) {
  689. //got milk?
  690. string $history[]=`listHistory $s0`;
  691. string $skin[]=`ls -et skinCluster $history`;
  692. string $clusters[]=`ls -et cluster $history`;
  693.  
  694. if(size($skin)<1 && size($clusters)<1) continue;
  695.  
  696. string $pcl[]=yzWeightStore($s0, $doStoreGeo);
  697. $weights[size($weights)]=$pcl[0];
  698. }
  699. select $weights;
  700. }
  701.  
  702. global proc yzwsRestoreCB(int $mode) {
  703. string $sel[]=`ls -sl -fl`;
  704. string $obj[]=`ls -et transform $sel`;
  705. string $pts[]=stringArrayRemove($obj, $sel);
  706.  
  707.  
  708. int $src=$mode;
  709. int $switchSrc=`checkBox -q -v yzwsSource`;
  710. if($switchSrc) $src=1-$src;
  711.  
  712. string $wList="skinWeightList";
  713. if($src==1) $wList="clusterList";
  714.  
  715.  
  716. string $weights[], $geo[];
  717. int $ids[];
  718.  
  719. for($s0 in $obj) {
  720. //find particles
  721. if(attributeExists($wList,$s0)) {
  722. $weights[size($weights)]=$s0;
  723. continue;
  724. }
  725.  
  726. //find geometries
  727. string $shapes[]=`listRelatives -pa -s $s0`;
  728. $isGeo=0;
  729. if(size($shapes)) {
  730. $type=nodeType($shapes[0]);
  731. if($type=="mesh" || $type=="nurbsSurface" || $type=="nurbsCurve") {
  732. $geo[size($geo)]=$s0;
  733. $isGeo=1;
  734. }
  735. if($isGeo) continue;
  736. }
  737. }
  738.  
  739. //find points
  740. for($p=0; $p<size($pts); $p++) {
  741. string $from=`match "^[^\.]*" $pts[$p]`;
  742. $geo[size($geo)]=$from;
  743.  
  744. string $id=substituteAll("]", $pts[$p], "" );
  745. $id=match("[0-9]+$", $id);
  746. $ids[size($ids)]=(int)$id;
  747. }
  748.  
  749. $geo=stringArrayRemoveDuplicates($geo);
  750. if(size($geo)>1 && size($ids)) error "points from different geomtries selected";
  751.  
  752. print "-weights\n"; print $weights;
  753. print "-geo:\n"; print $geo;
  754. print "-ids:\n"; print $ids;
  755.  
  756.  
  757. if(size($weights)==0) {
  758. if($src==0) error "no skin weights selected";
  759. if($src==1) error "no cluster weights selected";
  760. }
  761. if(size($weights)==1) {
  762. if(size($geo)) for($geo0 in $geo)
  763. if($mode==0) yzWeightRestore($weights[0], $geo0, $ids, $src );
  764. else yzWeightRestoreClusters($weights[0], $geo0, $ids, $src );
  765. else
  766. if($mode==0) yzWeightRestore($weights[0], "", {}, $src );
  767. else yzWeightRestoreClusters($weights[0], "", {}, $src );
  768. }
  769. if(size($weights)>1) {
  770. if(size($geo)==0) for($w0 in $weights)
  771. if($mode==0) yzWeightRestore($w0, "", {}, $src );
  772. else yzWeightRestoreClusters($w0, "", {}, $src );
  773. if(size($weights)==size($geo)) for($w=0; $w<size($weights); $w++)
  774. if($mode==0) yzWeightRestore($weights[$w], $geo[$w], {}, $src);
  775. else yzWeightRestoreClusters($weights[$w], $geo[$w], {}, $src);
  776. if(size($weights)!=size($geo) && size($geo)!=0) error "bad selection";
  777. }
  778. }
  779.  
  780. global proc yzwsLoadParticleCB() {
  781.  
  782. string $sel[]=`ls -sl`;
  783. string $shapes[]=`listRelatives -pa -s $sel[0]`;
  784.  
  785. //divers check pour bien loader :)
  786. if(size($shapes)==0) error "node is not a particle Weight";
  787. if(nodeType($shapes[0])!="particle") error "node is not a particle Weight";
  788.  
  789. string $list;
  790. string $attrList[]={"clusterList", "skinWeightList"};
  791.  
  792. for($attr0 in $attrList) {
  793. if( attributeExists ($attr0, $sel[0]) ) $list=$attr0;
  794. }
  795. if( size($list)==0 ) error "node is not a particle Weight";
  796.  
  797.  
  798. textField -e -tx $sel[0] yzwsPcl;
  799.  
  800. //load des names
  801. string $infs[];
  802. $infSize=`getAttr -s ($sel[0]+"."+$list)`;
  803.  
  804. textScrollList -e -ra yzwsInfs;
  805. for($i=0; $i<$infSize; $i++) {
  806. $inf=getAttr($sel[0]+"."+$list+"["+$i+"]");
  807. if(!objExists($inf)) $inf=("//"+$inf);
  808. textScrollList -e -a $inf yzwsInfs;
  809. }
  810. }
  811.  
  812. global proc string yzwsGetList() {
  813. string $pcl=`textField -q -text yzwsPcl`;
  814. string $names[]=`textScrollList -q -ai yzwsInfs`;
  815. $names[0]=substituteAll("//",$names[0],"");
  816. $names[0]=substituteAll(" ",$names[0],"");
  817.  
  818. if(attributeExists("skinWeightList", $pcl)) {
  819. string $inf=getAttr($pcl+".skinWeightList[0]");
  820. if($inf==$names[0]) return "skinWeightList";
  821. }
  822. if(attributeExists("clusterList", $pcl)) {
  823. string $inf=getAttr($pcl+".clusterList[0]");
  824. if($inf==$names[0]) return "clusterList";
  825. }
  826. return "";
  827. }
  828.  
  829. global proc yzswSelectInfCB() {
  830. string $name[]=`textScrollList -q -si yzwsInfs`;
  831. $name[0]=substituteAll("//",$name[0],"");
  832. $name[0]=substituteAll(" ",$name[0],"");
  833. textField -e -tx $name[0] yzwsRen;
  834. }
  835.  
  836. global proc yzwsRenameInfCB(int $mode) {
  837. string $pcl=`textField -q -text yzwsPcl`;
  838. string $weightList=yzwsGetList();
  839.  
  840. string $str, $obj;
  841. if($mode==0) {
  842. string $pouet[]=`ls -sl`;
  843. $str=$pouet[0];
  844. $obj=$str;
  845. } else {
  846. $str=`textField -q -tx yzwsRen`;
  847. $str=substituteAll("//",$str,"");
  848. $str=substituteAll(" ",$str,"");
  849. $obj=$str;
  850. if(!objExists($obj)) $str=("//"+$str);
  851. }
  852.  
  853. //update UI
  854. string $sel[]=`textScrollList -q -si yzwsInfs`;
  855. int $index[]=`textScrollList -q -sii yzwsInfs`;
  856. textScrollList -e -rii $index[0] yzwsInfs;
  857. textScrollList -e -ap $index[0] $str yzwsInfs;
  858.  
  859. $sel[0]=$str;
  860. textScrollList -e -si $sel yzwsInfs;
  861.  
  862.  
  863. //update pcl
  864. setAttr -type "string" ($pcl+"."+$weightList+"["+($index[0]-1)+"]") $obj;
  865. }
  866.  
  867. global proc yzwsRenameNsCB() {
  868. string $ns=`textField -q -tx yzwsRenNs`;
  869. string $nodes[]=`textScrollList -q -si yzwsInfs`;
  870. int $index[]=`textScrollList -q -sii yzwsInfs`;
  871. string $pcl=`textField -q -text yzwsPcl`;
  872. string $weightList=yzwsGetList();
  873.  
  874. for($i=0; $i<size($nodes); $i++) {
  875. string $str=$nodes[$i];
  876. $str=substituteAll("//",$str,"");
  877. $str=substituteAll(" ",$str,"");
  878. $str=match("[^\:]*$", $str);
  879.  
  880. if($ns!="") $str=($ns+":"+$str);
  881. string $obj=$str;
  882. if(!objExists($obj)) $str=("//"+$str);
  883.  
  884. textScrollList -e -rii $index[$i] yzwsInfs;
  885. textScrollList -e -ap $index[$i] $str yzwsInfs;
  886. $nodes[$i]=$str;
  887.  
  888. //update pcl
  889. setAttr -type "string" ($pcl+"."+$weightList+"["+($index[$i]-1)+"]") $obj;
  890. }
  891.  
  892. textScrollList -e -si $nodes yzwsInfs;
  893. }
  894.  
  895.  
  896.  
  897. global proc yzWeightSaveUI() {
  898.  
  899. if (`window -exists yzWeightUI`)
  900. deleteUI yzWeightUI;
  901.  
  902. $wUI=208;
  903. $hUI=399; //300+10(window)+27(tabs)
  904.  
  905. window
  906. -t "yzWeightSave2 - beta"
  907. -mb true
  908. -s 1
  909. -rtf false
  910. yzWeightUI;
  911.  
  912. string $mainFrame=`columnLayout -adj true`;
  913.  
  914. text -fn "boldLabelFont" -bgc 1 .6 0 -al "left" " weights:";
  915.  
  916.  
  917.  
  918. rowColumnLayout -numberOfColumns 2 -cw 1 100 -cw 2 100;
  919. button -label "store" -c "yzwsStoreCB; yzwsLoadParticleCB;";
  920. checkBox -label "store geometry" -align "left" yzwsStoreGeo;
  921.  
  922. button -label "restore skin" -c "yzwsRestoreCB 0";
  923. button -label "restore clusters" -c "yzwsRestoreCB 1";
  924. radioButtonGrp -en 0 -numberOfRadioButtons 2 -label "" -labelArray2 "id" "space" -cw3 40 40 50 -adj 1 -cat 1 "right" 0 -sl 1 yzwsMode;
  925. checkBox -label "switch source" -align "left" yzwsSource;
  926. setParent ..;
  927.  
  928. text -fn "boldLabelFont" -bgc 1 .6 0 -al "left" " backup:";
  929.  
  930. rowColumnLayout -numberOfColumns 2 -cw 1 100 -cw 2 100;
  931. button -label "load particles" -c "yzwsLoadParticleCB;";
  932. textField -ed 0 -text "" yzwsPcl;
  933. setParent ..;
  934.  
  935.  
  936. text -fn "boldLabelFont" -bgc 1 .6 0 -al "left" " influences:";
  937.  
  938. rowColumnLayout -numberOfColumns 2 -cw 1 100 -cw 2 100;
  939. button -label "rename influence" -c "yzwsRenameInfCB(1);";
  940. textField -text "" -ec "yzwsRenameInfCB(1);" yzwsRen;
  941. button -label "namespace" -c "yzwsRenameNsCB;";
  942. textField -text "" -ec "yzwsRenameNsCB;" yzwsRenNs;
  943. setParent ..;
  944. button -label "rename from selected" -c "yzwsRenameInfCB(0);";
  945.  
  946.  
  947. textScrollList -nr 15 -allowMultiSelection true -sc "yzswSelectInfCB;" yzwsInfs;
  948.  
  949.  
  950. //affichage
  951. showWindow yzWeightUI;
  952. window -e -w $wUI -h $hUI yzWeightUI;
  953. }
  954.  
  955. yzWeightSaveUI;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement