global proc getNormals(string $tnode, vector $data[]) {
int $count = size($data);
// Step 1: Decide what is selected.
string $vertices[] = `filterExpand -sm 31`;
// Individual vertices have NOT been selected so we
// go ahead as usual...
if(size($vertices) == 0) {
string $shapes[] = `listRelatives -shapes $tnode`;
if(size($shapes) == 1) {
int $num_vertices[] = `polyEvaluate -v $tnode`;
string $shape = $shapes[0];
float $norms[];
vector $ave;
for($n = 0; $n < $num_vertices[0]; $n++) {
$vertexStr = $shape + ".vtx[" + $n + "]";
$norms = `polyNormalPerVertex -q -xyz $vertexStr`;
$ave = getAverageNormal($norms);
$data[$count] = << $ave.x, $ave.y, $ave.z >>;
$count++;
}
}
else // tnode is a group node
{
string $children[] = `listRelatives -children $tnode`;
for($child in $children) {
getNormals($child, $data);
}
}
}
else
{
float $norms[];
vector $ave;
for($vert in $vertices) {
$norms = `polyNormalPerVertex -q -xyz $vert`;
$ave = getAverageNormal($norms);
$data[$count] = << $ave.x, $ave.y, $ave.z >>;
$count++;
}
}
}
// Used by getNormals()
global proc vector getAverageNormal(float $norms[]) {
float $x = 0, $y = 0, $z = 0;
int $numComps = size($norms);
int $numNorms = $numComps/3;
//print("num norms = " + size($norms) + "\n");
if($numNorms == 1)
return <<$norms[0],$norms[1],$norms[2]>>;
for($n = 0; $n <= $numComps - 3; $n += 3) {
$x += $norms[$n];
$y += $norms[$n + 1];
$z += $norms[$n + 2];
}
return <<$x/$numNorms, $y/$numNorms, $z/$numNorms >>;
}