//
// Post Transform Mel Script
//
global proc PlaceOnNurbsRI() {
string $tformNode = `rman ctxGetObject`;
  
// Setting the seed number of the random number generator is very
// important because the getNurbsData() function calls rand().
int    $nodeNumber = `match "[0-9]+" $tformNode`;
if($nodeNumber != "") {
    seed(int($nodeNumber));
    }
  
// Bounding box is only relevant if the transform node is not a group.
string $children[] = `listRelatives -children $tformNode`;
string $shapeNode = $children[0];
  
string $attr;
$attr = `rmanGetAttrName "ppon_archive"`;
string $ppon_archive = `getAttr($tformNode + "." + $attr)`;
  
$attr = `rmanGetAttrName "ppon_hyper_node"`;
string $ppon_hyper_node = `getAttr($tformNode + "." + $attr)`;
  
$attr = `rmanGetAttrName "ppon_density"`;
int $ppon_density = `getAttr($tformNode + "." + $attr)`;
  
$attr = `rmanGetAttrName "ppon_threshold"`;
float $ppon_threshold = `getAttr($tformNode + "." + $attr)`;
  
// Use of Pixar's custom RenderMan Studio procedures begins here.
RiArchiveRecord("structure", "RLF Inject SurfaceShading");
vector $colors[];
vector $positions[];
vector $normals[];
getNurbsData($ppon_density, $ppon_hyper_node, $shapeNode, $colors, $positions, $normals);
int $n;
  
for($n = 0; $n < $ppon_density; $n++) {
    vector $rgb = $colors[$n];
    vector $xyz = $positions[$n];
    vector $nor = $normals[$n];
    float $gray = ($rgb.x + $rgb.y + $rgb.z)/3;
    if($gray < $ppon_threshold) {
          RiTransformBegin();
            RiTranslate($xyz.x, $xyz.y, $xyz.z);
            float $ang[] = aimY($nor);
            RiRotate($ang[1], 0,0,1);
            RiRotate($ang[0], 1,0,0);
            RiReadArchive($ppon_archive);
          RiTransformEnd();
       }
    }
}