Post Transform Mel Script
//
  
  
global proc string GetArchivePath(string $shape) {
    string $scene = `file -q -sceneName -shortName`;
    string $scene = `substitute ":" $scene "_"`;
    if(size($scene) == 0)
        $scene = "untitled";
    else
        $scene = `substring $scene 1 (size($scene) - 3)`;
        
    int    $frame = `currentTime -q`;
    string $fStr = "." + $frame;
    if($frame < 10) 
        $fStr = ".000" + $frame;
    else if($frame < 100)
        $fStr = ".00" + $frame;
    else if($frame < 1000)
        $fStr = ".0" + $frame;
    string $archive = $scene +"_"+ $shape + $fStr + ".rib";
    return "data/"+ $archive;;
    }
    
    
//----------------------------------------------------------
  
global proc vector getNormalVector (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];
        }
  
    float  $vtxNormalAverage[] = <<$x/$numNorms, $y/$numNorms, $z/$numNorms >>;
  
    float $xDegree, $yDegree, $zDegree;
  
    if ($vtxNormalAverage[0] == 0 && $vtxNormalAverage[2] == 0){
    $zDegree = ($vtxNormalAverage[1] > 0) ? 90 : -90;
    return <<0, 0, $zDegree>>;        
    }
                    
    else if ($vtxNormalAverage[1] == 0 && $vtxNormalAverage[2] == 0){
    $zDegree = ($vtxNormalAverage[0] > 0) ? 0 : 180;
    return <<0, 0, $zDegree>>;
    }
        
    else if ($vtxNormalAverage[0] == 0 && $vtxNormalAverage[1] == 0){
    $yDegree = ($vtxNormalAverage[2] > 0) ? -90 : 90;
    return <<0, $yDegree, 0>>;
    }
        
    else if ($vtxNormalAverage[0] == 0){
    $yDegree = atan($vtxNormalAverage[2]/$vtxNormalAverage[1])*57.29;
    $zDegree = ($vtxNormalAverage[1] > 0) ? 90 : -90;
    return <<0, $yDegree, $zDegree>>;
    }
            
    else if ($vtxNormalAverage[1] == 0){
    $yDegree = atan($vtxNormalAverage[2]/$vtxNormalAverage[0])*57.29;
    $zDegree = ($vtxNormalAverage[0] > 0) ? 0 : 180;
    return <<0, $yDegree, $zDegree>>;
    }
    
    else if ($vtxNormalAverage[2] == 0){
    $yDegree = ($vtxNormalAverage[0] > 0) ? 0 : 180;
    $zDegree = atan($vtxNormalAverage[1]/$vtxNormalAverage[0])*57.29;
    return <<$xDegree, 0, $zDegree>>;
    }
        
    else {
    $xDegree = atan($vtxNormalAverage[1]/$vtxNormalAverage[2])*57.29;
    $yDegree = atan($vtxNormalAverage[2]/$vtxNormalAverage[0])*57.29;
    $zDegree = atan($vtxNormalAverage[1]/$vtxNormalAverage[0])*57.29;
    return <<$xDegree, $yDegree, $zDegree>>;
    }
  
}
  
  
global proc batRI() {
// 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 "bat_rad"`;
float $bat_rad = `getAttr($tformNode + "." + $attr)`;
  
$attr = `rmanGetAttrName "bat_jitter"`;
float $bat_jitter = `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 $locator = `rman ctxGetObject`;
    float $xLocator = `getAttr $locator.tx`;
    float $yLocator = `getAttr $locator.ty`;
    float $zLocator = `getAttr $locator.tz`;
    
    //looking for trunk, if it exits, it's going to skip creaing it
    $ls = `ls`;
    string $trunkFlag;
    for ($items in $ls){
        if ($items == "trunk"){
            $trunkFlag = true;
            break;
    }
    else    
    $trunkFlag = false;
    }
    
    string $shapeNode[] = ($trunkFlag == false) ? `polyCylinder -ch on -o on -r 0.1 -sa 6 -sh 6 -sc 1 -cuv 3 -n "trunk"` : `rman ctxGetObject`;
    move ($xLocator, $yLocator + 1, $zLocator);
        
    //string $shapeNode = `RiReadArchive "data/branch.rib"`;
    select $shapeNode[0];
    int $vertices[] = `polyEvaluate -v`;
    for($n = 0; $n < $vertices[0]; $n++){
        RiTransformBegin();
            string $vtx = $shapeNode[0] + ".vtx[" + $n + "]";
            float $vtxPosition[] = `pointPosition -l $vtx`;
            float $vtxNormal[] = `polyNormalPerVertex -q -xyz $vtx`;
            float $vtxNormalVector[] = getNormalVector ($vtxNormal);
            float $jitter = `rand 0 $bat_jitter`;
            
                //if ($vtxPosition[1] > $bb_height/3){
                RiTranslate($vtxPosition[0], $vtxPosition[1], $vtxPosition[2]);
                RiRotate($vtxNormalVector[0], 1, 0, 0);
                RiRotate($vtxNormalVector[1], 0, 1, 0);
                RiRotate($vtxNormalVector[2], 0, 0, 1);
                RiScale($bat_rad, $bat_rad, $bat_rad);
                RiReadArchive "data/leaf.rib";
                //nonLinear -type bend -lowBound 0 -highBound 1 -curvature 30 -geometry $readArchive; 
                //}
        RiTransformEnd();
    }
  
RiTransformEnd();
RiIdentity();
  
}