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++) {
$vertex_text = $shape + ".vtx[" + $n + "]";
$norms = `polyNormalPerVertex -q -xyz $vertex_text`;
$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 // here we get selected vertex normals
{
float $norms[];
vector $ave;
for($vert in $vertices) {
$norms = `polyNormalPerVertex -q -xyz $vertices`;
$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;
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 >>;
}