Wednesday, March 5, 2008

Next up is MEL!!!






Nicholas Pisca's workshop at SCI-Arc. I guess MEL is more forgiving than rvb. No Option Explicit, no Dim, and so forth... Sky, Ye, I'm waiting for your scripts too, ok?


The following is a basic script to display certain information from multiple existing geometries. The challenge was to figure out the closest object for each one. I had to use two "integer counters" ($HCounter and $JCounter) and the orange portion (in the script) was the key for completing this script.






//----------------------------------------------------------------
//----------------------------------------------------------------
// print information and the closest obj for each obj

//Author: yukie hirashima
//Contributor: sky
//Date: 03/05/2008
//compatibility: Maya8.5


SelectAll; //don't have to select manually

string $Sel[] = `ls-sl` ;
int $SelSize = `size($Sel)` ; //Max Array
int $JCounter = 0;

do {

string $closest ;
string $CurrentRef = $Sel[$JCounter] ;

float $ValScaleX = `getAttr ($Sel[$JCounter] + ".scaleX")` ;
print ("\n" ) ;
print ("\n" ) ;
print ($Sel[$JCounter] + " Attribute" + "/" + "\n") ;
print ("Objext name: " + $Sel[$JCounter] + " has an scaleX value of " + $ValScaleX + "\n" ) ;

float $ValRotateY = `getAttr ($Sel[$JCounter] + ".rotateY")` ;
print ("Objext name: " + $Sel[$JCounter] + " has an rotateY value of " + $ValRotateY + "\n" ) ;

float $ValTranslateZ = `getAttr ($Sel[$JCounter] + ".translateZ")` ;
print ("Objext name: " + $Sel[$JCounter] + " has an translateZ value of " + $ValTranslateZ + "\n" ) ;



//comparig the closest for each one
int $HCounter = 1;

//creating mini array
SelectAll; //don't have to select manually
select -tgl $CurrentRef ;
string $miniSel[] = `ls-sl` ;
int $miniSelSize = `size($miniSel)` ; //max array

//creating $PreviousDist for the first round
float $XYZVals01[] = `getAttr ($CurrentRef + ".translate")` ;
vector $XYZVec01 = <<$XYZVals01[0], $XYZVals01[1], $XYZVals01[2]>> ;

float $XYZVals02[] = `getAttr ($miniSel[$HCounter-1] + ".translate")` ;
vector $XYZVec02 = <<$XYZVals02[0], $XYZVals02[1], $XYZVals02[2]>> ;

$DiffVec = $XYZVec01 - $XYZVec02 ;
float $PreviousDist = mag($DiffVec) ;
//print ($PreviousDist + "\n") ; //TESTPRINT $PreviousDist


do {

//creating mini array for each round EXCLUDING $CurrentRef

SelectAll; //don't have to select manually
select -tgl $CurrentRef ;
string $miniSel[] = `ls-sl` ;
int $miniSelSize = `size($miniSel)` ; //Max Array
print (" now vs. " + $miniSel[$HCounter-1] + ": "); //TESTPRINT comparison


//get a distance from vector

float $XYZVals01[] = `getAttr ($CurrentRef + ".translate")` ;
vector $XYZVec01 = <<$XYZVals01[0], $XYZVals01[1], $XYZVals01[2]>> ;

float $XYZVals02[] = `getAttr ($miniSel[$HCounter-1] + ".translate")` ;
vector $XYZVec02 = <<$XYZVals02[0], $XYZVals02[1], $XYZVals02[2]>> ;

$DiffVec = $XYZVec01 - $XYZVec02 ;
float $CurrentDist = mag($DiffVec) ;
print ("Distance btw " + $CurrentRef + " & " + $miniSel[$HCounter-1] + " is " + $CurrentDist + "\n") ;


if ($CurrentDist <= $PreviousDist) {
$closest = $miniSel[$HCounter-1] ;
//print ("\n" + $CurrentDist) ; //TESTPRINT $CurrentDist
$PreviousDist= $CurrentDist ; // prepareing for the next round

}


$HCounter++ ;
} while ($HCounter < $SelSize);
print ($CurrentRef + " is closest to " + $closest + "\n") ;

$JCounter++ ;
} while ($JCounter < $SelSize) ; // ----------------------------------------------------------------
// ----------------------------------------------------------------