Listing 3 (mesh2CloudRI.mel)


global proc mesh2CloudRI()
{
// Get the names of the shape and transform nodes
string $shapeNode = `rman ctxGetObject`;
string $parents[] = `listRelatives -parent $shapeNode`;
string $tformNode = $parents[0];
  
// The values from the interface
string $attr;
$attr = `rmanGetAttrName "m2c_cache"`;
int $m2c_cache = `getAttr($shapeNode + "." + $attr)`;
  
// User has selected "Disable"
if($m2c_cache == 2) {
    print("Blobby will not be generated - Disable is active");
    return;
    }
$attr = `rmanGetAttrName "m2c_size"`;
float $m2c_size = `getAttr($shapeNode + "." + $attr)`;
  
$attr = `rmanGetAttrName "m2c_height"`;
float $m2c_height = `getAttr($shapeNode + "." + $attr)`;
  
$attr = `rmanGetAttrName "m2c_jitter"`;
float $m2c_jitter = `getAttr($shapeNode + "." + $attr)`;
  
$attr = `rmanGetAttrName "m2c_volume"`;
int $m2c_volume = `getAttr($shapeNode + "." + $attr)`;
  
$attr = `rmanGetAttrName "m2c_previs"`;
int $m2c_previs = `getAttr($shapeNode + "." + $attr)`;
  
$attr = `rmanGetAttrName "m2c_dup"`;
int $m2c_dup = `getAttr($shapeNode + "." + $attr)`;
if($m2c_dup < 1)
    $m2c_dup = 1;
  
//--------- Primvars ----------------------------------
$attr = `rmanGetAttrName "m2c_Prims0"`;
string $primStr0 = `getAttr($shapeNode + "." + $attr)`;
  
$attr = `rmanGetAttrName "m2c_Prims1"`;
string $primStr1 = `getAttr($shapeNode + "." + $attr)`;
string $allPrims = $primStr0 + ";" + $primStr1;
// Cleanup...
$allPrims = substituteAllString($allPrims, "\"", " ");
$allPrims = substituteAllString($allPrims, "[", " ");
$allPrims = substituteAllString($allPrims, "]", " ");
string $names[] = getPrimvarNames($allPrims);
//-----------------------------------------------------
  
// Get the number of vertices of the polymesh
int  $num[] = `polyEvaluate -v $tformNode`;
  
// The path to the rib file containing the description of the
// Blobby consists of "RIB_Archive" directory, the scene name,
// the shape name of the polymesh and the frame number, for
// example,
//        RIB_Archive/bathroom/shaving_cream.0001.rib
string $projPath = `workspace -q -rootDirectory`;
string $sceneName = `file -q -sceneName -shortName`;
if(size($sceneName) == 0)
    $sceneName = "untitled";
int    $frame = `currentTime -q`;
string $fStr = "." + $frame;
if($frame < 10) 
    $fStr = ".000" + $frame;
else if($frame < 100)
    $fStr = ".00" + $frame;
  
string $archiveName = $sceneName +"_"+ $shapeNode + $fStr + ".rib";
string $archivePath = $projPath + "RIB_Archive";
string $path = $archivePath + "/"+ $archiveName;
int    $canWrite = 1;
  
if(!`filetest -r $archivePath`) {
    print("Error: Your project directory does not have a RIB_Archive\n");
    print("       directory in which the Blobby archive can be saved.\n");
    print("       Manually create RIB_Archive and try again.\n");
    $canWrite = 0;
    }
  
// User has selected "Compute"
if($m2c_cache == 0 && $canWrite) {
    int    $fileid = `fopen $path "w"`;
  
    // Begin writing the Blobby to the archive
    fprint($fileid, "Blobby " + $num[0] + " [\n");
        
    if($m2c_volume && !$m2c_previs)
        fprint($fileid, "8\n");
            
    // Make each blob an ellipsoid and provide its array
    // index. The indices monotonously increment by 16.
    for($n = 0; $n < $num[0]; $n++)
        fprint($fileid, "1001 " + ($n * 16) + "\n");
      
    // Add the blending code "0" and the number of blobs to blend.
    fprint($fileid, "0 " + $num[0]);
      
    // Specify the indices of all the blobs.
    for($n = 0; $n < $num[0]; $n++)
        fprint($fileid, " " + $n);
    fprint($fileid, "]\n");
    fprint($fileid, "[\n");
        
    // Specify the transformations of each blob
    string $row1, $row2, $row3;
    for($n = 0; $n < $num[0]; $n++) {
        string $vert = $shapeNode + ".vtx[" + $n + "]";
        $pos = `pointPosition -local $vert`;
        $row1 = $m2c_size + " 0 0 0 ";
        $row2 = " 0 " + $m2c_size + " 0 0 ";
        $row3 = " 0 0 " + $m2c_size + " 0 ";
        float $xpos = $pos[0];
        float $ypos = $pos[1];
        float $zpos = $pos[2];
            
        if($m2c_jitter > 0) {
            seed($n);
            $xpos += rand(-$m2c_jitter, $m2c_jitter);
            $ypos += rand(-$m2c_jitter, $m2c_jitter);
            $zpos += rand(-$m2c_jitter, $m2c_jitter);
            }
        $ypos *= $m2c_height;
        $row4 = $xpos + " " +  $ypos + " " +  $zpos + " 1\n";
        fprint($fileid, $row1 + $row2 + $row3 + $row4);
        }
    fprint($fileid, "]\n");
    fprint($fileid, "[\"\"]\n");
    // Grab the primvars
    string $str = getPrimVars($names, $allPrims, $num[0]);
    fprint($fileid, $str);
    fclose $fileid;
    print("Writing (" + $m2c_dup + ") Blobby archive for shape named \"" + $shapeNode + "\" to:\n");
    print("    \"" + $path + "\"\n");
    }
else if($m2c_cache)
    {
    print("Attempting to reuse:\n");
    print("    \"" + $path + "\"\n");    
    }    
// Tell prman to render the Blobby
RiTransformBegin();
    if($m2c_previs)
        RiSurface("defaultsurface");
    if($m2c_previs)
        $m2c_dup = 1;
    RiReadArchive($path);
    for($n = 1; $n < $m2c_dup; $n++) {
        RiIfBegin("$user:pass_class == 'Final'");
            RiScale(0.99, 0.99, 0.99);
            RiReadArchive($path);
        RiIfEnd();
        }
RiTransformEnd();
// Make sure the polymesh is totally invisible
RiAttribute "visibility" "int camera" 0;
RiAttribute "visibility" "int transmission" 0; 
RiAttribute "visibility" "int diffuse" 0; 
RiAttribute "visibility" "int specular" 0; 
RiAttribute "visibility" "int photon" 0; 
}
  
//----------------------------------------------
// getPrimvarNames
//----------------------------------------------
global proc string[] getPrimvarNames(string $input)
{
string  $names[];
string  $items[], $buf[];
int     $n = 0;
  
tokenize($input, ";", $items);
for($item in $items) {
    tokenize(strip($item), $buf);
    if(size($buf[0]))
        $names[$n++] = strip($buf[0]);
    }    
return $names;
}
  
//----------------------------------------------
// getPrimVars
//----------------------------------------------
// seed() should be called before getting a sequence of primvars.
global proc string getPrimVars(string $names[], string $input, int $count)
{
string $result = "";
for($n = 0; $n < size($names); $n++)
    $result += getPrimvarStr($names[$n], $input, $count) + "\n";
return $result;
}
  
//----------------------------------------------
// getPrimvarStr
//----------------------------------------------
global proc string getPrimvarStr(string $name, string $input, int $count)
{
string  $item, $items[], $buf[], $dataStr;
int     $num, $i;
float   $r0,$r1,$r2,$r3,$r4,$r5;
string  $result = "", $datatype = "";
  
tokenize($input, ";", $items);
for($item in $items) {
    $item = strip($item);
    if(startsWith($item, $name + " ")) {
        $dataStr = substring($item, size($name) + 1, size($item));
        $num = tokenize(strip($dataStr), $buf);
        if(size($buf) == 0) {
            print("Error: the primvar \"" + $name + "\" does not have a value!\n");
            return "";
            }
  
        switch($num) { 
            case 1: $val = float($buf[0]);
                    for($i = 0; $i < $count; $i++)
                        $result += ($val + " ");
                    $datatype = "float";
                    break;
            case 2: $r0 = float($buf[0]);
                    $r1 = float($buf[1]);
                    for($i = 0; $i < $count; $i++) {
                        $val = rand($r0, $r1);
                        $result += $val + " ";
                        }
                    $datatype = "float";
                    break;
            case 3: $r0 = float($buf[0]);
                    $r1 = float($buf[1]);
                    $r2 = float($buf[2]);
                    $vec = <<$r0, $r1, $r2>>;
                    for($i = 0; $i < $count; $i++)
                        $result += $vec + " ";
                    $datatype = "color";
                    break;
            case 6: $r0 = float($buf[0]);
                    $r1 = float($buf[1]);
                    $r2 = float($buf[2]);
                    $r3 = float($buf[3]);
                    $r4 = float($buf[4]);
                    $r5 = float($buf[5]);
                    vector $c0 = <<$r0, $r1, $r2>>;
                    vector $c1 = <<$r3, $r4, $r5>>;
                    vector $c;
                    for($i = 0; $i < $count; $i++) {
                        $c = rand($c0, $c1);
                        $result += $c + " ";
                        }
                    $datatype = "color";
                    break;
                }
  
        if(isPrimitive($name))    
            $result = "\"" + $name + "\" [" + $result + "]";
        else
            $result = "\"" + $datatype + " " + $name + "\" [" + $result + "]";
        return $result;
        }
    }    
return $result;
}
  
//----------------------------------------------
// isPrimitive
//----------------------------------------------
global proc int isPrimitive(string $name)
{
int $result = 0;
string $prim[] = {"P","N","Cs","Os","time"};
for($n = 0; $n < size($prim); $n++)
    if($name == $prim[$n])
        return 1;
return $result;
}





© 2002- Malcolm Kesson. All rights reserved.