PDA

View Full Version : Applying one expression to many many objects


dbsmith
10-08-2009, 06:59 AM
Hey there, I have a fairly simple expression thats controlling the translateZ attribute of an object.

How can I copy this expression to other multiple objects automatically (i.e. not one by one by hand - I have thousands of objects to apply it to)?

I'm sure there's a simple way or script to do this.

Thanks!

phix314
10-08-2009, 07:59 AM
Hah these are always fun. Whether or not it's "correct" is up for debate though.

I just created a buncha cubes, moved em around, froze translate and ran this.



proc float moveZ(){
float $time = `currentTime -query`;
float $sin = `sin $time`;
return $sin;
}

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


Granted this is a super simple example, but it sounds like what you're doing is simple as well.

trancor
10-08-2009, 09:12 AM
phix pretty much has it.

I'd add that if you wanted to make a change in the future to these expressions it might be really tedious. You could just run phix's script again to recreate the expressions per object to update them but if you had all of the objects effected by one expression I think it would be most useful. You'd be able to go in and make one change and everything would update right nice.


string $objects[]={"your", "objects", "listed", "here"};
float $value=sin(frame)*10;
for($obj in $objects){
setAttr ($obj+".translateZ") $value;
}


only ish is that it isn't directly connected from the expression now, but if you change the frame your on, it will still animate as though it was. Again, another reason I suggest doing this in one expression is that, what if you want to add some noise or variation to those thousands of objects, and lets say there is a common trend in the naming convention like I hope there would be.


string $objects[]=`ls -type transform "moveObj*"`;
//The * will find all objects moveObj1 moveObj2 moveObj10246
//-type transform will find only transforms and not shapes
float $value;
for($x=0; $x<size($objects); ++$x){
$value=noise($x*40+time)*4;
setAttr ($objects[$x]+".translateZ") $value;
}

This will randomly move your objects around smoothly but randomly along their z axis based on the number the for loop is up to, which could create a neat effect for you. Which would also allow you to throw in some more random math into the effect. like you could use rand or you can go more organized like sin or cos or tan or log to create waves or smooth moves to one number. There is alot you can do with this, I prefer this method in my scripting. (I didn't test my script, so if something messed up, just post back, or any questions, feel free)

Pyrokinesis
10-08-2009, 12:42 PM
dbsmith, Can you be a little more specific about what you are trying to do?
You want to move thousands of objects in Z...

What are you really trying to do?
How do you want them to move in Z, randomly, specifically, oscillate, constant?
Should their translation be driven by an action... or time?
Are we moving objects of a specific type, name, do they have anything in common?

Perhaps sharing your expression would help.

There are so many ways to do things in Maya, the more you know about what you want to do, the easier it is to find the most elegant solution. Otherwise we are kind of coding blind.

phix314
10-08-2009, 05:13 PM
Thanks trancor, explanations are always a help.


...
Perhaps sharing your expression would help.

There are so many ways to do things in Maya, the more you know about what you want to do, the easier it is to find the most elegant solution. Otherwise we are kind of coding blind.

I think the question was the application to many, not the actual Z- expression.

dbsmith
10-08-2009, 07:59 PM
Thanks heaps guys, I'll try that out this weekend.

Pyrokinesis, yeah the expression itself isn't really relevant, it's more the copying of an expression (whatever it is) into many objects. So it could've been any expression into any object's keyable values. But thanks for wanting to help :)

CGTalk Moderation
10-08-2009, 07:59 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.