//
// Post Transform Mel Script
//
global proc rgbMeshArchivesRI() {
// 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));
    }
else
    seed(1);
  
// Bounding box is only relevant if the transform node is not a group.
string $children[] = `listRelatives -children $tformNode`;
string $shapeNode = $children[0];
float $bb_width = -1, $bb_height = -1, $bb_depth = -1;
if(size($children) == 1) {
    $bb_width =  `getAttr ($shapeNode + ".boundingBoxSizeX")`;
    $bb_height = `getAttr ($shapeNode + ".boundingBoxSizeY")`;
    $bb_depth =  `getAttr ($shapeNode + ".boundingBoxSizeZ")`;
    }
  
string $attr;
$attr = `rmanGetAttrName "rma_path"`;
string $rma_path = `getAttr($tformNode + "." + $attr)`;
  
$attr = `rmanGetAttrName "rma_radial_offset"`;
float $rma_radial_offset = `getAttr($tformNode + "." + $attr)`;
  
$attr = `rmanGetAttrName "rma_use_normals"`;
int $rma_use_normals = `getAttr($tformNode + "." + $attr)`;
  
$attr = `rmanGetAttrName "rma_jitter"`;
float $rma_jitter = `getAttr($tformNode + "." + $attr)`;
  
$attr = `rmanGetAttrName "rma_yrot"`;
float $rma_yrot = `getAttr($tformNode + "." + $attr)`;
  
$attr = `rmanGetAttrName "rma_scale"`;
float $rma_scale = `getAttr($tformNode + "." + $attr)`;
  
$attr = `rmanGetAttrName "rma_input_node"`;
string $rma_input_node = `getAttr($tformNode + "." + $attr)`;
  
$attr = `rmanGetAttrName "rma_threshold"`;
float $rma_threshold = `getAttr($tformNode + "." + $attr)`;
  
// To use the shader of a "Custom Shading Group that can be
// assigned to the transform node of your geometry.
RiArchiveRecord("structure", "RLF Inject SurfaceShading");
  
vector $verts[], $norms[], $rgbs[];
getVertices($tformNode, $verts);
getNormals($tformNode, $norms);
getVertexRGB($tformNode, $rgbs, $rma_input_node);
  
if(size($verts) == 0 || size($norms) == 0)
    return;
int $n;
vector $vert, $norm;
float  $rot[], $x, $y, $z, $yangle, $xscale, $yscale, $zscale;
  
for($n = 0; $n < size($verts); $n++) {
    vector $rgb = $rgbs[$n];
    // We use the grayscale value to decide if an archive should
    // be placed on the mesh
    if( ($rgb.x + $rgb.y + $rgb.z)/3 > $rma_threshold)
        continue;
    $vert = $verts[$n];
    $x = $vert.x * $rma_radial_offset;
    $y = $vert.y * $rma_radial_offset;
    $z = $vert.z * $rma_radial_offset;
    // Apply position jitter
    $x += rand(-$rma_jitter, $rma_jitter);
    $y += rand(-$rma_jitter, $rma_jitter);
    $z += rand(-$rma_jitter, $rma_jitter);
  
    RiAttributeBegin();
        RiTranslate($x, $y, $z);
        // Alignment to the mesh formal
        if($rma_use_normals) {
            $norm = $norms[$n];
            $rot = aimY($norm);
            RiRotate($rot[1], 0,0,1);
            RiRotate($rot[0], 1,0,0);        
            }
        // Apply Y axis rotational jitter
        if($rma_yrot > 0) {
            $yangle = rand(-$rma_yrot, $rma_yrot);
            RiRotate($yangle, 0, 1, 0);
            }
        // Ditto the randomized scaling
        $xscale = rand(1, 1 + $rma_scale);
        $yscale = rand(1, 1 + $rma_scale);
        $zscale = rand(1, 1 + $rma_scale);
        RiScale($xscale, $yscale, $zscale);
        RiReadArchive($rma_path);
    RiAttributeEnd();
    }
}