Expression, referring to objects by concatenating strings

Expression, referring to objects by concatenating strings

daniel_uppling
Contributor Contributor
937 Views
4 Replies
Message 1 of 5

Expression, referring to objects by concatenating strings

daniel_uppling
Contributor
Contributor

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).

screen01.jpg

 

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:

screen02.jpg

 

And at frame 10 after hitting the Edit button:

screen03.jpg

 

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

0 Likes
Accepted solutions (2)
938 Views
4 Replies
Replies (4)
Message 2 of 5

jmreinhart
Advisor
Advisor
Accepted solution
pCylinder1.translateX = 1

So unfortunately there is no way to use the expression "=" and concatenated strings. It's just not how expressions works. If you go to preferences>animation>evaluation, and change it to DG, then it should update when you scrub through the timeline.

0 Likes
Message 3 of 5

daniel_uppling
Contributor
Contributor

Hi,

Thank you for taking the time to reply! That worked, it now updates every frame. So is the setAttr method the best/the proper way to go?  Feel like a noob (which is probably correct).

0 Likes
Message 4 of 5

jmreinhart
Advisor
Advisor
Accepted solution

setAttr is the only way to do it if you NEED to use  loop inside an expression. For something simple like this you should just use nodes though. The "time" node can be used in place of "frame".

Message 5 of 5

daniel_uppling
Contributor
Contributor

Hi,

 

Thank you for that. I'm not that familiar yet as to what is possible to do with nodes. But I'll look into that at some point.

I appreciate your help!

 

Thank you.

0 Likes