PDA

View Full Version : Adding expression to multiple attributes on multiple objects


SStone
01-29-2013, 10:28 PM
Hey Guys,

I'm trying to randomly animate the scale and trans values of several spheres. The expression I'm currently using is " = noise(time*3)*.6;" however after painstakingly applying it to the trans.x of all of my objects (like 40) I realized that I need it to be random so they don't all look the same. Additionally I want to apply to all 3 trans values in addition to the scale. What I'm trying to achieve is a very subtle, smooth wiggle in position and a very subtle pulse in the scale. I'm at a dead end. Any help would be appreciated.

I also tried variations of "=random(1.0)" in the expression but maya is saying it doesn't recognize "random". I began using this page to create this. http://area.autodesk.com/blogs/duncan/a_couple_of_fun_maya_tips

smtjffr
02-09-2013, 11:25 PM
I used a script from (phix314 http://forums.cgsociety.org/archive/index.php/t-814117.html) and manipulated it to my needs and I feel sure you could expand it to fit yours. It adds the expression to all selected objects.

proc float transY(){
float $time = `currentTime -query`;
float $sin = rand(10 + sin($time)) * 2;
return $sin;
}

proc applyExpressionToSelection(){
string $sel[] = `ls -sl`;
for($object in $sel){
expression -s ($object + ".translateY = transY()") -o $object -ae 1 -uc all;
}
}
applyExpressionToSelection();

smtjffr
02-09-2013, 11:30 PM
I used the code from (phix 314 http://forums.cgsociety.org/archive/index.php/t-814117.html)and manipulated it to my needs. I feel sure you could use it to fit yours. This simply applies an expression to all the selected objects. Here I just wanted to randomly translateY. Put this in your script editor, mmb drag it to your shelf and it will make a button you can click once you have the geometry selected.

proc float transY(){
float $time = `currentTime -query`;
float $sin = rand(10 + sin($time)) * 2;
return $sin;
}

proc applyExpressionToSelection(){
string $sel[] = `ls -sl`;
for($object in $sel){
expression -s ($object + ".translateY = transY()") -o $object -ae 1 -uc all;
}
}
applyExpressionToSelection();

Azrail
02-11-2013, 09:11 AM
This one will do the trick, but once you save, quit and than reopen Maya, the expression will throw an error, since it won't find the transY() procedure, so you'll either need to save that procedure as a mel file somewhere, where maya can find it, or just write the whole code in the expression string:

proc applyExpressionToSelection()
{
string $sel[] = `ls -sl`;
int $id = 0;
for($object in $sel)
{
string $cmd = "vector $offset = dnoise(<<1,1,1>> * time*3 + " + $id + ");\n";
$cmd += ($object + ".translateX = $offset.x;\n");
$cmd += ($object + ".translateY = $offset.y;\n");
$cmd += ($object + ".translateZ = $offset.z;\n");
$id += 1;
expression -s $cmd -o $object -ae 1 -uc all;
}
}
applyExpressionToSelection();

also its better to use noise/dnoise since they will always return the same value for each evaluation, and rand() wil return a different value every time, so your animation will be different on every run.

smtjffr
02-14-2013, 02:00 PM
True it will throw an error once you re-open maya so it is best to save as a mel file, or you will have to re-apply the expression each time you open the file unless you bake the animation. I saved a separate file and baked the animation in it so I could revert back to the original if needed.

CGTalk Moderation
02-14-2013, 02:00 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.