PDA

View Full Version : Help optimizing an expression


CiaranM
03-22-2007, 03:56 PM
Hi there,
I've written this little expression to generate a 'travelling wave' along a row of objects (looks even cooler when applied to a large matrix of objects!)


int $x, $i, $numBalls;
float $ballPosition, $freq, $amp, $wavelength, $yShift;

select -ado "ball*";
select -d -adn;
string $objects[] = `ls -sl`;
$numBalls = size($objects);
select -d;


for ($i = 0; $i < $numBalls; $i++)
{
$yShift = 1;
$amp = 5;
$freq = 3;
$wavelength = ((time - $i)*$freq);
$ballPosition = ($amp * (cos($wavelength)) + $yShift);
setAttr ($objects[$i]+".ty") $ballPosition;
}


However I'm not happy that I am controlling each object's Y translation using a setAttr command. I expect that this slows down the execution of the expression and is not recommended. How can I do this without setAttr? I can't seem to get the syntax right. Or is it not possible?

Any help is appreciated!

Robken
03-22-2007, 04:55 PM
in an expression you don't need to use setAttr and getAttr

you can just do a direct equation

for example

instead of
setAttr (sphere1.ty) sin(time);

you can do
sphere1.ty = sin(time)

I don't know if it'll make it faster, but it might.

CiaranM
03-22-2007, 06:18 PM
in an expression you don't need to use setAttr and getAttr
you can just do a direct equation


Thanks, I realise that.
But, my problem is that I'm forced to use setAttr, because I can't find the correct syntax to construct the equation. I'm trying to retrieve the object name from an array and apply a variable to that element's Y translate. Because of the problem this expression is solving, I 'm using an array. But, how do I apply an expression to an element of an array?

e.g.

ball4.translateY = $ballPosition;

easy! But:


($objects[$i] + ".translateY") = $ballPosition;

I can't get the syntax right here, despite many combinations. So, I'm forced to use:


setAttr ($objects[$i]+"ty") $ballPosition;


which works well, but I'm curious if there's a "correct" way of doing this. I'm sure I just don't know the syntax.

Thanks!

isoparmB
03-23-2007, 03:27 AM
int $x, $i, $numBalls;
float $ballPosition, $freq, $amp, $wavelength, $yShift;

// Put this outside the for in loop so it doesn't have to calculate differently on each iteration
$yShift = 1;
$amp = 5;
$freq = 3;
// This is more simple.
string $objects[] = `ls -et transform "ball*"`;
string $item;

// Use for-in loops if you're iterating through a string array.
for ($item in $objects)
{
$wavelength = ((time - $i)*$freq);
$ballPosition = ($amp * (cos($wavelength)) + $yShift);
setAttr ($item + ".ty") $ballPosition;
};


In this instance, using a setAttr would be unavoidable, because you're parsing through a list.

CiaranM
03-25-2007, 04:32 AM
Hey, thanks for that. Just what I needed to know!
Also, the for..in tip is great!

Thanks.

CGTalk Moderation
03-25-2007, 04:32 AM
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.