Thursday, July 3, 2014

Smooth Toggle and Duplicate Skinned Mesh Mel scripts

Hi,

As i was working on sculpting facial shapes of a character and i wanted to duplicate the skinned mesh i found it a little time consuming each time unlocking all the translate and rotate channels so i wrote a short Mel script to do this, it requires first selecting the mesh we wish to copy and then the default mesh, it creates a clean copy.  (on a side note this may also be helpful making a clean mesh to export to ZBrush for further editing.

Also as i was working i found it a little time consuming having to switch divisions of a smoothed mesh (polyFaceSmooth) to 0 each time i wanted to make a duplicate.  So i wrote a short Mel script that toggles between divisions quickly, it needs the shape selected so just select your mesh hit the down arrow then run script.

Hope they are helpful
cheers,
Nate

modify at your own risk:
global proc string[] na_duplicateSelectedUsingDefaultMesh(){
//so we have a clean version to start sculpting correctives etc
//requires first selecting single blendshape then base mesh

string $defaultArg, $default;
string $blendArg, $blend;
string $defaultAr[], $blendAr[];

if(size(`ls -sl`) == 2){
string $sel[] = `ls -sl`;
$blendArg = $sel[0];
$defaultArg = $sel[1];
}
else{error("Select Blendshape then Default Mesh!!!");}

//so not stepping over any work
string $defaultAr[] = `duplicate -rr $defaultArg`;
$default = $defaultAr[0];
string $blendAr[] = `duplicate -rr $blendArg`;
$blend = $blendAr[0];

//make a clean version it uses default settings of first thing selected
select -r $blend;
select -add $default;
string $blendNameAr[] = `blendShape`;
setAttr ($blendNameAr[0]+"."+$blend) 1;

//cleanup and return result
string $resultAr[] = `duplicate -rr $default`;
string $result = $resultAr[0];
delete(`pointConstraint -offset 0 0 0 -weight 1 $blend $result`);
delete $default;//only deleting copies so not messing up scene
delete $blend;
//end cleanup

//could return result here
return $resultAr;
}
na_duplicateSelectedUsingDefaultMesh();


//07-03-2014 -- 07-07-2014   -- added support selecting transform, fixed a finding smooth node bug
global proc na_toggleSmoothOnShape(){
//Toggle smooth division of selected between none and smooth
//assumes shape selected assumes shape has exactly one smooth node
if(size(`ls -sl`) == 1){
string $selAr[] = `ls -sl`;
string $shape = "";

//so it can work with selecting transform or shape
if( `objectType $selAr[0]`  != "mesh" ){
 string $tryThisAr[] = `pickWalk -d down`;//ignoring if doesnt have shape
 if( `objectType $tryThisAr[0]` == "mesh"){$shape = $tryThisAr[0];}
 else{error("Requires single Shape selected");}
}
else{$shape = $selAr[0];}
//

string $allHistory[] = `listHistory $shape`;
string $smoothNodeAr[] = `ls -type polySmoothFace $allHistory`;

if(size($smoothNodeAr) == 1){
string $smoothNode = $smoothNodeAr[0];
//toggle between 0 and 2 divisions
int $currentDivision = getAttr ($smoothNode+".divisions");
if($currentDivision != 0){setAttr ($smoothNode+".divisions") 0;}
else{setAttr ($smoothNode+".divisions") 2;}
}else{error("Requires one polySmoothFace on selected shape!!!");}
}

}
na_toggleSmoothOnShape();