Maya 2020 fundamentals - modelling the real world
Get halfway through a model and find it's an unworkable mess? Can't add edge loops where you need them? Can't subdivide a mesh properly? If any of this sounds familiar check this course out.
# 1 31-08-2015 , 10:41 PM
Registered User
Join Date: Dec 2013
Posts: 2

Expression Loop

I am wanting to write an expression to assign the value of a custom attribute to other objects in my scene.

For example, I have a scene with 4 cubes named pCube1 to pCube4, and I have an object named "Control" with a custom attribute named "Range." I would like to use a loop to iteratively assign the value of Control.Range to the translateY of each cube.

I realize this script will not work, but this is the basic idea:

for($i=1; $i<5; $i++)
{
"pCube" + $i.translateY = Control.Range;

}

How should the be scripted?

Thanks.

# 2 01-09-2015 , 07:06 PM
EduSciVis-er
Join Date: Dec 2005
Location: Toronto
Posts: 3,374
Do you want to assign the value of the control object or do you want a permanent connection between them? In other words, if after you run the script, you change Control.Range, do you want the pCube translate to update?

If it's the former, then getting the value would be:

Code:
for($i=1; $i<5; $i++)
{
 float $controlRangeValue = `getAttr Control.Range`;
 setAttr ("pCube" + $i + ".translateY") $controlRangeValue; 

}
If the latter, then connecting the attributes woudl be:
Code:
for($i=1; $i<5; $i++)
{
 connectAttr Control.Range ("pCube" + $i + ".translateY"); 
}

# 3 01-09-2015 , 07:34 PM
Gen's Avatar
Super Moderator
Join Date: Dec 2006
Location: South FL
Posts: 3,522
You could use a "for-in" loop to act on a list of items (an array) that you've selected.

Code:
// This is pretty basic listing that stores ALL objects user selected
// with no filters or checks
// so select the right objects or write more code >.>

string $objList[] = `ls -sl`;
float $range = `getAttr "Control.range"`;

for ( $obj in $objList ){

	setAttr ($obj + ".translateY") $range;

}
If the value on these cubes need to be automatically or regularly updated, you could consider using the "connectAttr" command instead of "setAttr".


Code:
string $objList[] = `ls -sl`;

for ( $obj in $objList ){

	connectAttr "Control.range" ($obj + ".translateY"); 
	
}


- Genny
__________________
::|| My CG Blog ||::
::|| My Maya FAQ ||::
# 4 01-09-2015 , 08:18 PM
Gen's Avatar
Super Moderator
Join Date: Dec 2006
Location: South FL
Posts: 3,522
Dammit Stwert I hit submit, walked away to come back and see that you beat me to it LOL.


- Genny
__________________
::|| My CG Blog ||::
::|| My Maya FAQ ||::
# 5 01-09-2015 , 11:08 PM
EduSciVis-er
Join Date: Dec 2005
Location: Toronto
Posts: 3,374
I wanted to post something jerkish to that effect, but then realized you contributed a meaningful addition to the thread. I'll let it slide this time . user added image

# 6 02-09-2015 , 04:56 AM
NextDesign's Avatar
Technical Director
Join Date: Feb 2004
Posts: 2,988
One thing to remember is that an expression is updated at every frame change. Putting loops into an expression is a very easy way to destroy your scene performance. It is almost always a better idea to try and implement your logic using node networks instead.


Imagination is more important than knowledge.
# 7 02-09-2015 , 04:16 PM
EduSciVis-er
Join Date: Dec 2005
Location: Toronto
Posts: 3,374
Oh good call ND, I didn't catch on that it was an expression rather than a simple script. I would probably still do a connectAttr script (run once) instead of an expression. As you say, it's not the best use for an expression.

# 8 13-09-2015 , 01:33 AM
Registered User
Join Date: Dec 2013
Posts: 2
Thanks everyone for the responses. Stwerts second code block is what I was looking for. Cheers!

Posting Rules Forum Rules
You may not post new threads | You may not post replies | You may not post attachments | You may not edit your posts | BB code is On | Smilies are On | [IMG] code is On | HTML code is Off

Similar Threads