Sunday, March 16, 2008

Ball Tracking





Recently finished Nick Pisca's MEL scripting workshop and found MEL is a lot of fun. Seems much smarter than Rhino in a lot of ways. Things like declarations and arrays are much easier although the case sensitive stuff gets me all the time. We didn't cover procedures in the workshop but got to hit a lot of ground including scripting material assignment and rendering.


Here's a simple tracking script where planes duplicate and deform while following an animated object. I'll post the video sometime in the future.



//Tracking script
//
//Written by: Sky Milner
//Date: 3-03-08
//Contributor: Nick Pisca
//
//This script duplicates selected objects based on the
//location of a target.
//
//Setup:
//First make an object and call it "Target", Animate target as you like, make 6 nurbsPlanes
//
//
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++



string $Selection[] = {"nurbsPlane1", "nurbsPlane2", "nurbsPlane2", "nurbsPlane3", "nurbsPlane3","nurbsPlane4","nurbsPlane4","nurbsPlane5","nurbsPlane5","nurbsPlane6", "nurbsPlane6","nurbsPlane1"};
int $SelSize = size($Selection);
int $SquareCounter = 0;

for ($i = 0; $i < currentnumber =" 0;" selectioncounter =" 0;" firstobject =" $Selection[$CurrentNumber];" secondobject =" $Selection[$CurrentNumber+1];" vecfirst =" <<$XYZfirst[0],">> ;

float $XYZsecond[] = `pointPosition ($SecondObject + ".cv [0] [0]")` ;
vector $VecSecond = <<$XYZsecond[0], $XYZsecond[1], $XYZsecond[2]>> ;

float $XYZtarget[] = `pointPosition ("Target.cv [0] [0]")`;
vector $VecTarget = <<$XYZtarget[0], $XYZtarget[1], $XYZtarget[2]>>;


$Between = $VecFirst - $VecSecond;

//Duplicate and move into position

duplicate -rr -n ("series"+$i+"square"+$SquareCounter) $Selection[($CurrentNumber)];

string $Duplicate = ("series"+$i+"square"+$SquareCounter);


//setAttr ($Duplicate+ ".translate") ($XYZfirst[0] + $Between.x/(-2)) ($XYZfirst[1] + $Between.y / (-2) ) ($XYZfirst[2] + $Between.z / (-2));

xform -r -t ($Between.x/(-2)) ($Between.y / (-2)) ($Between.z / (-2)) $Duplicate;


float $XYZduplicate[] = `pointPosition ($Duplicate + ".cv [0] [0]")`;
vector $Vecduplicate = <<$XYZduplicate[0], $XYZduplicate[1], $XYZduplicate[2]>>;

$Between2 = $VecTarget - $Vecduplicate;

//setAttr ($Duplicate+ ".translate") ($XYZduplicate[0] + $Between2.x/4) ($XYZduplicate[1] + $Between2.y/2) ($XYZduplicate[2] + $Between2.z/4);

xform -r -t ($Between2.x/4) ($Between2.y/2) ($Between2.z/4) $Duplicate;


//Scale Duplicate

float $scalefactor = ((mag($Between2)*.01)+.75);

//setAttr ($Duplicate+ ".scaleX") $scalefactor;
//setAttr ($Duplicate+ ".scaleY") $scalefactor;
//setAttr ($Duplicate+ ".scaleZ") $scalefactor;


//Manipulate cv[0]

float $PointX = `getAttr ($Duplicate + ".cv[0].xValue")`;
float $PointY = `getAttr ($Duplicate + ".cv[0].yValue")`;
float $PointZ = `getAttr ($Duplicate + ".cv[0].zValue")`;

vector $VecPoint = <<$PointX, $PointY, $PointZ>>;

$Between3 = $VecTarget - $VecPoint;

setAttr ($Duplicate+ ".cv[0].xValue") ($PointX + $Between3.x/(10));
setAttr ($Duplicate+ ".cv[0].yValue") ($PointY + $Between3.y/(10));
setAttr ($Duplicate+ ".cv[0].zValue") ($PointZ + $Between3.z/(10));

//setAttr ($Duplicate + ".objectColor") $SquareCounter;

$TempSelection [$SelectionCounter] = $FirstObject;
$TempSelection [$SelectionCounter+1] = $Duplicate;
$TempSelection [$SelectionCounter+2] = $SecondObject;
$TempSelection [$SelectionCounter+3] = $Duplicate;


$CurrentNumber = $CurrentNumber+2;
$SelectionCounter = $SelectionCounter + 4;
$SquareCounter++;
} while ($CurrentNumber < $SelSize); currentTime $i; $Selection = $TempSelection; $SelSize = size($Selection); if ($SelSize > 100) {
for ($s = 0; $s < selsize =" 100;" iii =" 0;" sizejump =" size($jump);" nicklambert = "nicklambert" nicklambertsg = "nicklambert" nicklambertoutcolor = "nicklambert" nicklambertsgss = "nicklambert" nicklamberttrans = "nicklambert" iiiend =" $iii" iiiend2 =" $iii" iii="$iii" i="0;" seriessize =" size($series);" s =" 0;" curshader =" $Shader[0];">

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) ; // ----------------------------------------------------------------
// ----------------------------------------------------------------