/ Generated by Cutter v7.0.2 at 10:29:36 on the 10.6.2015.
// The source document on which this mel script is based is,
//    "/home/mshari20/mount/stuhome/maya/projects/RfM_mel/treeUI.rman"
// Cutter software by Malcolm Kesson (all rights reserved).
// 
// Pre Shape Mel Script
//
  
source "aimY.mel";
source "getNormals.mel";
  
global proc treeRI() {
// Get the name of the transform node
string $tformNode = `rman ctxGetObject`;
  
// The node may hava a number in its name that we can use to set the
// random number generator
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`;
float $bb_width = -1, $bb_height = -1, $bb_depth = -1;
if(size($children) == 1) {
    string $shapeNode = $children[0];
    $bb_width =  `getAttr ($shapeNode + ".boundingBoxSizeX")`;
    $bb_height = `getAttr ($shapeNode + ".boundingBoxSizeY")`;
    $bb_depth =  `getAttr ($shapeNode + ".boundingBoxSizeZ")`;
    }
  
string $attr;
$attr = `rmanGetAttrName "ss_radius"`;
float $ss_radius = `getAttr($tformNode + "." + $attr)`;
  
$attr = `rmanGetAttrName "ss_random"`;
float $ss_random = `getAttr($tformNode + "." + $attr)`;
  
$attr = `rmanGetAttrName "ss_density"`;
float $ss_density = `getAttr($tformNode + "." + $attr)`;
  
// To use the shader of a "Custom Shading Group that can be
// assigned to the transform node of your geometry uncomment
// the next line.
RiArchiveRecord("structure", "RLF Inject SurfaceShading");
string $shapes[] = `listRelatives -ad -type "shape" $tformNode`;
  
RiTransformBegin();
    
    //string $shapeNode = `listRelatives -ad -type "shape" $tformNode`;
    string $shapeNode = `rman ctxGetObject`;
    int $vertices[] = `polyEvaluate -v $shapeNode`;
    for($n = 0; $n < $vertices[0]; $n++){
        RiTransformBegin();
            string $vtx = $shapeNode + ".vtx[" + $n + "]";
            float $vtxPosition[] = `pointPosition -l $vtx`;
            float $vtxNormal[] = `polyNormalPerVertex -q -xyz $vtx`;
            float $vtxNormalVector[] = getAverageNormal ($vtxNormal);
            float $vtxDirection[] = aimY ($vtxNormalVector);
            float $randomSize = `rand ($ss_radius - $ss_random) ($ss_radius + $ss_random)`;
            $randomSize = ($randomSize < 0) ? 0 : $randomSize;
            if ($vtxPosition[1] > (-$bb_height/2 + $bb_height - $bb_height * $ss_density)){
                RiTranslate($vtxPosition[0], $vtxPosition[1], $vtxPosition[2]);
                RiRotate($vtxDirection[1], 0, 0, 1);
                RiRotate($vtxDirection[0], 1, 0, 0);
                RiScale($randomSize, $randomSize, $randomSize);
                RiReadArchive "data/leaf.rib";
                //nonLinear -type bend -lowBound 0 -highBound 1 -curvature 30 -geometry $readArchive; 
                }
        RiTransformEnd();
    }
  
RiTransformEnd();
  
}