|
//GridModel.mel
global string $master[]; // instances refer to this sphere
global string $input; // reference to the inputs
global string $groupName; // group = master + instances
global int $numRows,
$numCols,
$divAxis,
$divHeight;
//--------------------------------------------------------
// To prevent the accumulation of "orphan" groups any
// empty instances of "GMGroup" are deleted from the scene.
//--------------------------------------------------------
global proc GridModel(int $rows, int $cols,
float $xSpace, float $ySpace, float $sphRad)
{
global string $groupName;
global string $master[];
global string $input;
global int $numRows, $numCols, $divAxis, $divHeight;
deleteEmptyGMGroups();
$divAxis = 8;
$divHeight = 8;
// Create the master sphere
$master = `polySphere -r $sphRad -sh $divHeight -sa $divAxis`;
$input = getShapeInput($master[0]);
$numRows = $rows;
$numCols = $cols;
string $items[], $inst[];
int $n = 0, $x, $y;
for($x = 0; $x < $cols; $x++) {
for($y = 0; $y < $rows; $y++) {
$inst = `instance`;
$items[$n] = $inst[0];
move ($xSpace * $x) ($ySpace * $y) 0;
$n++;
}
}
// Ensure the first item in the group is the master sphere.
$groupName = `group -n "GMGroup" $master $items`;
setAttr ($master[0] + ".visibility") 0;
}
//--------------------------------------------------------
// Applies XY spacing to all members of the current group.
//--------------------------------------------------------
global proc applySpacing(float $xSpace, float $ySpace)
{
global string $groupName;
global int $numRows, $numCols;
if(nodeExists($groupName) == 0) {
print("A new Grid must be made.\n");
return;
}
string $items[] = `listRelatives -c $groupName`;
if(size($items) > 0)
select $groupName;
int $n = 1, $x, $y;
for($x = 0; $x < $numCols; $x++) {
for($y = 0; $y < $numRows; $y++) {
move ($xSpace * $x) ($ySpace * $y) 0 $items[$n];
$n++;
}
}
}
//--------------------------------------------------------
//
//--------------------------------------------------------
global proc applySubdivisions(int $sa, int $sh)
{
global int $divAxis, $divHeight;
global string $input;
if($sa == -1 || $sh == -1) {
$sa = $divAxis;
$sh = $divHeight;
}
setAttr ($input + ".sa") $sa;
setAttr ($input + ".sh") $sh;
}
//--------------------------------------------------------
// Receives the name of the transform node. For example,
// given "pSphere1" will return "polySphere1".
//--------------------------------------------------------
global proc string getShapeInput(string $tnode)
{
// get the shape node
string $shp[] = `listRelatives -shapes $tnode`;
string $cons[] = `listConnections $shp[0]`;
if(size($cons) == 0)
return "";
int $lastIndex = size($cons) - 1;
return $cons[$lastIndex];
}
//--------------------------------------------------------
// Utility to remove empty (orphan) groups
//--------------------------------------------------------
global proc deleteEmptyGMGroups()
{
string $GMGroups[] = `ls "GMGroup*"`;
for($n = 0; $n < size($GMGroups); $n++) {
string $gmGroup = $GMGroups[$n];
string $items[] = `listRelatives -c $gmGroup`;
if(size($items) == 0 || size($items) == 1) {
print("Deleting empty group: " + $gmGroup + "\n");
delete $gmGroup;
}
}
}
//--------------------------------------------------------
// Simple check to see if an "object" is in the scene.
//--------------------------------------------------------
global proc int nodeExists(string $name)
{
string $item[] = `ls $name`;
if(size($item) == 0)
return 0;
else
return 1;
}
|