Skip to content

Instantly share code, notes, and snippets.

@SEVEZ
Created August 29, 2015 11:30
Show Gist options
  • Save SEVEZ/6182539b1c7ba3a98635 to your computer and use it in GitHub Desktop.
Save SEVEZ/6182539b1c7ba3a98635 to your computer and use it in GitHub Desktop.
Bake wrap and ffd weights to skinCluster
///////////////////////////////////////////////////////////////////////////////////////////////////////////
//This script was written by Ben Bathen.
//It was based on an idea presented By David Stripinis in his tutorial "Complex Character Setup In Maya For Games."
//It was completed April 5, 2003.
//This script bakes all the weights from wrap deformers and lattices into a skincluster on a mesh.
//This is helpful because a wrap deformer can have alot less vertices than a high Res mesh.
//Also you can edit the geomety on the high res mesh at any time without losing your weighting.
//select the root, then select the mesh, then execute the script. Works on only one mesh at a time.
global proc bakeToSkin ()
{
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//this section puts the selected objects into strings.
//put the selected objects into a string
string $sel[] = `ls -sl`;
//put the selected objects into a string
string $root = $sel[0];
string $hiMesh = $sel[1];
//check to see if more than 2 objects are selected or if one of them does not match the required nodeType.
int $selCount = `size $sel`;
if ($selCount != 2)
{
error "select the root of the deformation skeleton and a single polygon mesh";
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//This section declares a float array to store the joint weights in.
float $weights[];
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//this section finds every joint parented under the root.
string $joints[] = `listRelatives -c -type "joint" -ad $root`;
int $jointCount = `size $joints`;
$joints[$jointCount] = $root;
$jointCount = `size $joints`;
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//this section puts the original Y coordinate of every vertex on the mesh into an array.
float $origin[];
int $vertCount[];
$vertCount = `polyEvaluate -v $hiMesh`;
for ( $i = 0; $i < $vertCount[0]; $i++ )
{
//place the xyz coordinates of each vertex into a float array
float $tfa[] = `xform -q -ws -t ($hiMesh + ".vtx[" + $i +"]")`;
//extract the y coordinate from the float array and place into another float array
$origin[$i] = $tfa[1];
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
//This Section Moves each individual Joint
//calculates how much each vertex has moved and derives a weight
//wich is stored in the float array which we created earlier.
//All weight for every joint are stored in a single float array;
int $i;
int $b;
int $c;
int $d = 0;
float $delta[];
for ($i = 0; $i < $jointCount; $i++)
{
string $parent[] = `listRelatives -p $joints[$i]`;
int $parentExists = `size $parent`;
string $kids[] = `listRelatives -c $joints[$i]`;
int $kidsExist =`size $kids`;
if ( $parentExists != 0 )
{
parent -w $joints[$i];
}
if ( $kidsExist != 0 )
{
parent -w $kids;
}
select -r $joints[$i];
move -r 0 1 0 ;
refresh;
//create a variable for each joint which will contain its weighting info.
for ( $b = 0; $b < $jointCount; $b++ );
{
float $yCoordinates[];
float $xyzCoordinates[];
//find the vector position of each vertex on the mesh
for ( $c = 0; $c < $vertCount[0]; $c++ )
{
//place the xyz coordinates of each vertex into a float array
$xyzCoordinates = `xform -q -ws -t ($hiMesh + ".vtx[" + $c +"]")`;
//extract the y coordinate from the float array and place into another float array which will hold all the Y coordinates for the joint being evaluated.
$yCoordinates[$c] = $xyzCoordinates[1];
//calculate the change in position of each vertex.
if ($yCoordinates[$c] - $origin[$c] < 0)
{
$delta[$c] = 0;
}
else
{
$delta[$c] = $yCoordinates[$c] - $origin[$c];
}
$weights[$c +($d * $vertCount[0])] = $delta[$c];
}
$d = $d + 1;
}
move -r 0 (-1) 0 ;
if ( $kidsExist != 0 )
{
parent $kids $joints[$i];
}
if ( $parentExists != 0 )
{
parent $joints[$i] $parent;
}
refresh;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//This section deletes history on the mesh. Then binds it to the skeleton.
select -r $hiMesh;
DeleteHistory;
delete -ch;
select -r $root;
select -add $hiMesh;
string $skinCluster[] = `skinCluster -mi 3 -dr 4`;
setAttr ($skinCluster[0] + ".normalizeWeights") 0;
print "made it to section 4";
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//this section sets the skinweights on the new skincluster.
$d = 0;
for ($i = 0; $i < $jointCount; $i++)
{
for($c = 0; $c < $vertCount[0]; $c++)
{
int $vertexWeightIndex = $c +($d * $vertCount[0]);
float $Value = $weights[$vertexWeightIndex];
string $vert = ($hiMesh + ".vtx[" + $c + "]");
string $skin = $skinCluster[0];
string $cmd = ("skinPercent -tv " + $joints[$i] + " " + $Value + " " + $skin + " " + $vert);
eval $cmd;
//print "\n";
//print $cmd;
refresh;
}
$d = $d + 1;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//This section normalizes all the weights;
setAttr ($skinCluster[0] + ".normalizeWeights") true;
select -r $hiMesh;
skinPercent -normalize true $skinCluster[0];
print "Congratulations you just set weights on a high res mesh";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment