PDA

View Full Version : code simplification


extragrey
07-10-2009, 04:00 PM
it's definitely possible to simplify this script, any suggestions ?
Thanks in advance for any help.

//----------------------------------------------- circle grid
for ($x=0 ; $x<20 ; $x++)
{
for ($y=0 ; $y<20 ; $y++)
{
circle -r 5;
move (10*$x) (10*$y) 0;
}
}
select -ado;
group -name circleGrid1;

//----------------------------------------------- inputs
string $attractor[]=`spaceLocator`;
move -r 50 120 0;
select -r circleGrid1;
string $objects[]=`ls -sl`;
select -deselect;

for ( $i=1 ; $i<401 ; $i++)
{
//----------------------------------------------- circle XYZ center coodinates
float $transObjX=`getAttr ("nurbsCircle"+$i+".translateX")`;
float $transObjY=`getAttr ("nurbsCircle"+$i+".translateY")`;
float $transObjZ=`getAttr ("nurbsCircle"+$i+".translateZ")`;
float $transObjXYZ[]={$transObjX,$transObjY,$transObjZ};

//----------------------------------------------- spaceLocator coordinates
float $transLocX=`getAttr ("locator1.translateX")`;
float $transLocY=`getAttr ("locator1.translateY")`;
float $transLocZ=`getAttr ("locator1.translateZ")`;
float $transLocXYZ[]={$transLocX,$transLocY,$transLocZ};

//----------------------------------------------- distance between circle and locator
float $distanceX=$transObjX-$transLocX;
float $distanceY=$transObjY-$transLocY;
float $distanceZ=$transObjZ-$transLocZ;
float $distance =sqrt(($distanceX*$distanceX)+($distanceY*$distanceY)+
($distanceZ+$distanceZ));

//----------------------------------------------- distance threshold
float $threshold=110;
float $amount=1-(($threshold-$distance)/$threshold);

//----------------------------------------------- scaling
select ("nurbsCircle"+$i);
scale -relative $amount $amount $amount ;
}

Joviex
07-12-2009, 09:22 AM
//----------------------------------------------- circle grid
string $attractor[] = `spaceLocator`;
move -r 50 120 0;
float $transLoc[] =`getAttr ("locator1.translate")`;
float $transObj[];
float $distarray[3];
float $distance;
float $threshold=110;
float $amount;

for ($x=0 ; $x < 20 ; $x++)
{
for ($y=0 ; $y < 20 ; $y++)
{
$name = `circle -r 5`;
move (10*$x) (10*$y) 0;
$transObj =`getAttr ($name[0]+".translate")`;

for($i=0; $i<2; $i++)
{
$distarray[$i] = $transObj[$i] - $transLoc[$i];
$distarray[$i] *= $distarray[$i];
}
$distance = sqrt($distarray[0] + $distarray[1] + $distarray[2]);
$amount = 1 - (($threshold-$distance)/$threshold);
scale -relative $amount $amount $amount;
}
}
select -ado;
group -name circleGrid1;

J/K =D Hope that helps

extragrey
07-13-2009, 11:16 AM
someday, when I'll grow wiser, I will write a cookie.mel for you )

Robert Bateman
07-13-2009, 12:01 PM
Not really a big fan of scripts like that. For a start mel really isn't a great computational language, and secondly you have nodes that can do that computation for you within the DG. If you actually use those node instead (and treat mel purely as a way of hooking stuff up) you'll have an end result that is far more useful. Run the script, select the locator and move it around, and you'll soon see what i mean (also see threshold attr added to the locator). Make sure you do not have the attribute editor open when you select the locator though! (i.e. Ctrl+A so that the channel box is visible). Just my 2 cents...


string $attractor[] = `spaceLocator`;
move -r 50 120 0;
addAttr -ln "threshold" -at double -dv 110 ($attractor[0]);
setAttr -e-keyable true ($attractor[0]+".threshold");

proc genCircleAndConnect(string $tm)
{
string $dist = `createNode "distanceBetween"`;
// will do (threshold - distance)
string $pma = `createNode "plusMinusAverage"`;
// will divide the result of (threshold - distance) by threshold
string $md = `createNode "multiplyDivide"`;
// will do the 1 - part
string $om = `createNode "plusMinusAverage"`;
$circle = `circle -r 5`;
string $parents[] = `listRelatives -p $circle[0]`;
connectAttr -f ($parents[0]+".worldMatrix") ($dist+".im1");
connectAttr -f ($tm+".worldMatrix") ($dist+".im2");

setAttr ($pma+".op") 2;
connectAttr -f ($tm+".threshold") ($pma+".i1[0]");
connectAttr -f ($dist+".d") ($pma+".i1[1]");

setAttr ($md+".op") 2;
connectAttr -f ($pma+".o1") ($md+".i1x");
connectAttr -f ($tm+".threshold") ($md+".i2x");

setAttr ($om+".op") 2;
setAttr ($om+".i1[0]") 1.0;
connectAttr -f ($md+".ox") ($om+".i1[1]");
connectAttr -f ($om+".o1") ($parents[0]+".sx");
connectAttr -f ($om+".o1") ($parents[0]+".sy");

select -r $circle;
}

for ($x=0 ; $x < 20 ; $x++)
{
for ($y=0 ; $y < 20 ; $y++)
{
genCircleAndConnect($attractor[0]);
move (10*$x) (10*$y) 0;
}
}
select -ado;
group -name circleGrid1;

extragrey
07-13-2009, 04:53 PM
well... much more flexible than I can ever imagine... you are right, what was before - now seems excrement.
I'll try to think in "nodes" direction.

Greatly appreciate.

CGTalk Moderation
07-13-2009, 04:53 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.