// Generated by Cutter v7.7.8 at 11:14:55 on the 9.23.2017.
// The source document on which this mel script is based is,
//    "/Users/malcolm/Documents/maya/projects/RfM_mel/particleInstancer.rman"
// Cutter software by Malcolm Kesson (all rights reserved).
// 
// Pre Shape Mel Script
//
global proc particleInstancerRI() {
// Get the name of the shape node
string $shapeNode = `rman ctxGetObject`;
string $parents[] = `listRelatives -parent $shapeNode`;
string $tformNode = $parents[0];
  
// The node may hava a number in its name that we can use 
// to set the random number generator
int    $nodeNumber = `match "[0-9]+" $shapeNode`;    
if($nodeNumber != "") {
    seed(int($nodeNumber));
    }
// Bounding box...
float $bb_width =  `getAttr ($shapeNode + ".boundingBoxSizeX")`;
float $bb_height = `getAttr ($shapeNode + ".boundingBoxSizeY")`;
float $bb_depth =  `getAttr ($shapeNode + ".boundingBoxSizeZ")`;
  
string $attr;
$attr = `rmanGetAttrName "pi_ignore"`;
$attr = `rmanGetFullSharedGeometricAttrName $shapeNode $attr`;
int $pi_ignore = `getAttr $attr`;
  
$attr = `rmanGetAttrName "pi_archive"`;
$attr = `rmanGetFullSharedGeometricAttrName $shapeNode $attr`;
string $pi_archive = `getAttr $attr`;
  
$attr = `rmanGetAttrName "pi_xyzOffset"`;
$attr = `rmanGetFullSharedGeometricAttrName $shapeNode $attr`;
float $pi_xyzOffset = `getAttr $attr`;
  
$attr = `rmanGetAttrName "pi_scaleMin"`;
$attr = `rmanGetFullSharedGeometricAttrName $shapeNode $attr`;
float $pi_scaleMin = `getAttr $attr`;
  
$attr = `rmanGetAttrName "pi_scaleMax"`;
$attr = `rmanGetFullSharedGeometricAttrName $shapeNode $attr`;
float $pi_scaleMax = `getAttr $attr`;
  
$attr = `rmanGetAttrName "pi_xrotMin"`;
$attr = `rmanGetFullSharedGeometricAttrName $shapeNode $attr`;
float $pi_xrotMin = `getAttr $attr`;
  
$attr = `rmanGetAttrName "pi_xrotMax"`;
$attr = `rmanGetFullSharedGeometricAttrName $shapeNode $attr`;
float $pi_xrotMax = `getAttr $attr`;
  
$attr = `rmanGetAttrName "pi_yrotMin"`;
$attr = `rmanGetFullSharedGeometricAttrName $shapeNode $attr`;
float $pi_yrotMin = `getAttr $attr`;
  
$attr = `rmanGetAttrName "pi_yrotMax"`;
$attr = `rmanGetFullSharedGeometricAttrName $shapeNode $attr`;
float $pi_yrotMax = `getAttr $attr`;
  
$attr = `rmanGetAttrName "pi_zrotMin"`;
$attr = `rmanGetFullSharedGeometricAttrName $shapeNode $attr`;
float $pi_zrotMin = `getAttr $attr`;
  
$attr = `rmanGetAttrName "pi_zrotMax"`;
$attr = `rmanGetFullSharedGeometricAttrName $shapeNode $attr`;
float $pi_zrotMax = `getAttr $attr`;
  
$attr = `rmanGetAttrName "pi_cull"`;
$attr = `rmanGetFullSharedGeometricAttrName $shapeNode $attr`;
float $pi_cull = `getAttr $attr`;
  
$attr = `rmanGetAttrName "pi_useRotations"`;
$attr = `rmanGetFullSharedGeometricAttrName $shapeNode $attr`;
int $pi_useRotations = `getAttr $attr`;
  
if($pi_ignore == 1 || size(strip($pi_archive)) == 0) {
    print("Warning: particleInstancerRI an archive directory has not been specified.\n");
    return;
    }
string  $projPath = `workspace -q -rootDirectory`;
string  $dirFullPath = $projPath + $pi_archive;
  
int     $dirExists = `filetest -r $dirFullPath`;
if($dirExists == 0) {
    print("Warning: particleInstancerRI the archives folder \"" + $dirFullPath + "\" cannot be found.\n");
    return;
    }
int     $isDirectory = `filetest -d $dirFullPath`;
if($isDirectory == 0) {
    print("Warning: particleInstancerRI the archives folder must be a directory.\n");
    return;
    }
  
// List all MEL files in the user's script directory
string $archives[] = `getFileList -folder $dirFullPath -filespec "*.rib"`;
if(size($archives) == 0) {
    print("Warning: particleInstancerRI the archives folder \"" + $dirFullPath + "\" does NOT contain any ribs.\n");
    return;
    }
// Knowing the names of each rib we make their full paths
for($n = 0; $n < size($archives); $n++) {
    string $name = $archives[$n];
    $archives[$n] = $dirFullPath + "/" + $name;
    }    
    
// Use of Pixar's custom RenderMan Studio procedures begins here.
int     $num = `particle -q -ct $tformNode`; 
float   $rot[], $x, $y, $z;
int     $useRotations = `getAttr ($tformNode + ".computeRotation")`;
int     $numParticlesUsed = 0;
for($n = 0; $n < $num; $n++) {
    if(rand(0.0, 1.0) <= $pi_cull)
        continue;
    $numParticlesUsed++;
    $pStr = $tformNode + ".pt[" + $n + "]";
    $pos = `getParticleAttr -at position $pStr`;
    RiTransformBegin();
        $x = $pos[0] + rand(-$pi_xyzOffset, $pi_xyzOffset);
        $y = $pos[1] + rand(-$pi_xyzOffset, $pi_xyzOffset);
        $z = $pos[2] + rand(-$pi_xyzOffset, $pi_xyzOffset);
        RiTranslate($x, $y, $z);
        
        RiRotate(rand($pi_xrotMin, $pi_xrotMax), 1,0,0);
        RiRotate(rand($pi_yrotMin, $pi_yrotMax), 0,1,0);
        RiRotate(rand($pi_zrotMin, $pi_zrotMax), 0,0,1);
        
        if($pi_useRotations) {
            if ( catchQuiet( $rot = `getParticleAttr -at rotationPP $pStr`  ) ) {
                  RiRotate($rot[0], 1,0,0);
                RiRotate($rot[1], 0,1,0);
                RiRotate($rot[2], 0,0,1);
                  }
            }
        
        float $scale = rand($pi_scaleMin, $pi_scaleMax);
        RiScale($scale, $scale, $scale);    
        
        // Assign a custom attribute that can be used
        // OSL pattern shader.
        RiAttribute("user", "float particle_id", $n);
  
        // Pick an archive randomly
        int $random_index = trunc(rand(0, size($archives)));
        RiReadArchive($archives[$random_index]);
    RiTransformEnd();
    }
print("Info: ParticleInstancerRI - number of particles used was " + $numParticlesUsed + "\n");
// Make the proxy object inactive
RiAttribute "visibility" "int camera" 0;
RiAttribute "visibility" "int indirect" 0;
RiAttribute "visibility" "int transmission" 0;
}