CODE SAMPLES
The following code creates a spinning helix of spheres oriented in a circle.
for($i = 0; $i < 72; $i++)
{
//create two spheres offset 4 units from each other
sphere -ch on -o on -po 0 -ax 0 1 0 -r 1 -nsp 4 -n ("sphereA_" + $i);
move -a 2 0 0 ;
sphere -ch on -o on -po 0 -ax 0 1 0 -r 1 -nsp 4 -n ("sphereB_" + $i);
move -a -2 0 0 ;
//group spheres to move pivot for both to origin
select -r ("sphereA_" + $i);
select -tgl ("sphereB_" + $i);
group -n ("group" + $i);
xform -os -piv 0 0 0;
//rotate group (45 * $i) to create starting position, then freeze transform information
rotate -r -os 0 (45 * $i) 0 ;
makeIdentity -apply true -t 1 -r 1 -s 1 -n 0;
//expression to rotate group
expression -s ("group" + $i + ".rotateY = time * 50 ;") -o ("group" + $i) -ae 1 -uc all ;
//circular arrangement
move -a 12 0 0;
//group again to return pivot to origin
group -n ("group" + $i + "-");
xform -os -piv 0 0 0;
//rotate group for circular orientation
rotate -a 0 0 (5 * $i);
}
The following code creates a grid of child spheres whose scale is modified by a master sphere. The master sphere is dynamically animated
using a nail constraint and turbulence field. The closer the master sphere gets to the child spheres, the larger they become.
//set playback time to 1000 frames
playbackOptions -min 1 -max 1000 ;
int $name = 0;
//create the master sphere
polySphere -r 1 -sa 8 -sh 8 -n "masterSphere";
int $numRows = 13;
//use for loop to create child spheres and set up dynamic relationship
for($i = -12; $i < $numRows; $i++)
{
for($j = -12; $j < $numRows; $j++)
{
//create spheres
polySphere -r .1 -n ("slave_" + $name) -sx 8 -sy 8; move -a $i 0 $j;
//create distanceBetween nodes and hook them up to the master sphere
shadingNode -asUtility distanceBetween -n ("distanceBetween" + $name);
connectAttr -f ("slave_" + $name + ".translate") ("distanceBetween" + $name + ".point1");
connectAttr -f masterSphere.translate ("distanceBetween" + $name + ".point2");
//expressions to set the scale relationship up
expression -s ("slave_" + $name + ".scaleX = `clamp 1 15 (15 - distanceBetween" + $name + ".distance)`;");
expression -s ("slave_" + $name + ".scaleY = `clamp 1 15 (15 - distanceBetween" + $name + ".distance)`;");
expression -s ("slave_" + $name + ".scaleZ = `clamp 1 15 (15 - distanceBetween" + $name + ".distance)`;");
$name += 1;
}
}
select -r masterSphere ;
move -r 0 10 0 ;
//create and assign nail constraint and turbulence
constrain -nail -name "masterSphereContraint#" -p 0 0 0 ;
turbulence -pos 0 0 0 -m 25 -att 0 -f 1 -phaseX 0 -phaseY 0 -phaseZ 0 -noiseLevel 0 -noiseRatio 0.707 -mxd -1 -vsh none -vex 0 -vof 0 0 0 -vsw 360 -tsr 0.5 ;
connectDynamic -f turbulenceField1 masterSphere;
The following code analyzes a selected piece of polygonal geometry to get the area of each face. The faces are then sorted into tenth percentiles,
and assigned to a one of ten colored materials. The smallest faces are colored dark purple, then blue, green, and so on up to the largest faces, which
are assigned to a red material. (note - this code will only work on contiguous pieces of geometry)
//get name of selected item
string $sel[] = `ls -sl`;
string $selection = $sel[0];
//create materials
proc createMat(float $r, float $g, float $b, string $name)
{
shadingNode -asShader lambert;
rename lambert2 $name ;
sets -renderable true -noSurfaceShader true -empty -name ($name + "SG");
connectAttr -f ($name + ".outColor") ($name + "SG.surfaceShader");
setAttr ($name + ".color") -type double3 $r $g $b ;
}
createMat(1,0,0,"red");
createMat(1,.5,0,"orange");
createMat(1,1,0,"yellow");
createMat(0.686,1,0.373,"yellowGreen");
createMat(0,.75,0,"green");
createMat(.25,1,.7,"blueGreen");
createMat(0,.5,1,"blue");
createMat(.25,0,1,"bluePurple");
createMat(.5,0,1,"purple");
createMat(.15,0,.3,"darkPurple");
//once materials are created, reselect the initial object
select -r $selection;
//get number of faces
int $numFaceArray[] = `polyEvaluate -face`;
int $numFaces = $numFaceArray[0];
//create array to hold areas
float $areas[];
//clear array
clear($areas);
//use for loop to get face areas
for($i = 0; $i < $numFaces; $i++)
{
//separate each face to its own mesh to get area
polyChipOff -dup on -ch on ($selection + ".f[" + $i +"]");
polySeparate -ch on $selection;
//get area
select -r "polySurface2";
float $faceArea[] = `polyEvaluate -area`;
//store area
$areas[$i] = $faceArea[0];
//delete poly
select -r "polySurface2";
doDelete;
//polySeparate duplicates the geo and parents the new geo to the original shape node
//unparent, delete original node, and rename new geo
//unparent
select -r "polySurface1";
parent -w;
//delete original node
select -r $selection;
doDelete;
//rename new geo
select -r "polySurface1";
rename $selection;
//the processes above generate a lot of undos - this deletes the undos to keep memory available
flushUndo;
}
//sort array to get highest and lowest values
float $sortedAreas[] = sort($areas);
print $sortedAreas;
//get highest and lowest values, find the range, then divide by 10
float $low = $sortedAreas[0];
float $high = $sortedAreas[(`size($sortedAreas)` - 1)];
float $range = $high - $low;
float $tenthOfRange = $range/10;
//using area array, figure out where each face is relative to the rest, and assign it the correct material
for($i = 0; $i < $numFaces; $i++)
{
if($areas[$i] < $tenthOfRange)
{
select -r ($selection + ".f[" + $i + "]") ;
sets -e -forceElement redSG;
}
if($areas[$i] > $tenthOfRange && $areas[$i] < ($tenthOfRange * 2))
{
select -r ($selection + ".f[" + $i + "]") ;
sets -e -forceElement orangeSG;
}
if($areas[$i] > ($tenthOfRange * 2 ) && $areas[$i] < ($tenthOfRange * 3))
{
select -r ($selection + ".f[" + $i + "]") ;
sets -e -forceElement yellowSG;
}
if($areas[$i] > ($tenthOfRange * 3 ) && $areas[$i] < ($tenthOfRange * 4))
{
select -r ($selection + ".f[" + $i + "]") ;
sets -e -forceElement yellowGreenSG;
}
if($areas[$i] > ($tenthOfRange * 4 ) && $areas[$i] < ($tenthOfRange * 5))
{
select -r ($selection + ".f[" + $i + "]") ;
sets -e -forceElement greenSG;
}
if($areas[$i] > ($tenthOfRange * 5 ) && $areas[$i] < ($tenthOfRange * 6))
{
select -r ($selection + ".f[" + $i + "]") ;
sets -e -forceElement blueGreenSG;
}
if($areas[$i] > ($tenthOfRange * 6 ) && $areas[$i] < ($tenthOfRange * 7))
{
select -r ($selection + ".f[" + $i + "]") ;
sets -e -forceElement blueSG;
}
if($areas[$i] > ($tenthOfRange * 7 ) && $areas[$i] < ($tenthOfRange * 8))
{
select -r ($selection + ".f[" + $i + "]") ;
sets -e -forceElement bluePurpleSG;
}
if($areas[$i] > ($tenthOfRange * 8 ) && $areas[$i] < ($tenthOfRange * 9))
{
select -r ($selection + ".f[" + $i + "]") ;
sets -e -forceElement purpleSG;
}
if ($areas[$i] > ($tenthOfRange * 9))
{
select -r ($selection + ".f[" + $i + "]") ;
sets -e -forceElement darkPurpleSG;
}
}

No comments yet.