- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi,
I'm trying to refer to multiple objects in a expression and then do different stuff to them, by using a for loop and concatenating the names of the object with the variable ($i) of the for loop.
In my example scene I have a 10 cylinders named pCylinder1, pCylinder2, pCylinder3... etc. (will be many more than 10 objects in the end).
In the for loop the variable $i runs from 1 to 10.
The only way I could find to refer to objects this way was to use the setAttr function.
This is what I did (I used the frame function in this example to have some change over time):
for($i=1; $i<11; $i++)
{
setAttr("pCylinder" + $i + ".translateX") ($i+frame);
}
So basically what this does is:
loop 1: "pCylinder1" translates to X 1 + frame number
loop 2: "pCylinder2" translates to X 2 + frame number
loop 3 "pCylinder3" translates to X 3 + frame number
etc
The script kind of works. This is what it looks like at frame 1 (like expected).
But when I scrub to another frame, in this case frame 10 the objects doesn't move until I hit Edit. It doesn't seem to evaluate by simply scrubbing. I read somewhere that I need to enable "Evaluation: Always" in the dropdown. This is already enabled. This is what it looks like after scrubbing to frame 10:
And at frame 10 after hitting the Edit button:
So, I'm not dead set on using setAttr and it would be nice if everything updated automatically every frame, so if there's another way to refer to concatenated names, or to have the setAttr function to update every frame, I would love to know about it. For example to make this...
pCylinder1.translateX = 1
...into something like this somehow, without using setAttr:
"pCylinder" + $i + ".translateX" = $i //doesn't work by the way
Hopefully someone can help me to figure this out.
Thanks in advance,
Daniel
Solved! Go to Solution.