View Full Version : how do i make dynamic variable names in expressions?

03 March 2006, 08:49 PM
hi i'm very new to maya and mel scripts! i've been snooping around here for tips and clues but i'm still stuck...

i want to create around 50 locators and for each one i create, i want to assign an unique variable to some position values. i don't want to define and write out all for all 50 locators, is there a way to create dynamic variable names using a 'for' loop?

so my expression is currently (for locator1):

vector $pos_loc1 = <<locator1.translateX, locator1.translateY, locator1.translateZ>>;

so i don't want to write out:

vector $pos_loc2 = <<locator2.translateX, locator2.translateY, locator2.translateZ>>;
vector $pos_loc3 = <<locator3.translateX, locator3.translateY, locator3.translateZ>>;
..... etc etc


03 March 2006, 11:57 PM
you can create dynamic variables by doing something like this:
warning this use getAttr MEL command

// create variables and add value to them
int $i;
float $x; float $y; float $z;
for ( $i=1; $i <= 50 ;$i++ )
$x = `getAttr( ("locator"+$i+".translateX") )`;
$y = `getAttr( ("locator"+$i+".translateY") )`;
$z = `getAttr( ("locator"+$i+".translateZ") )`;
$command = "vector $pos_loc"+ $i +" = << "+$x+","+$y+","+$z+">>;";
eval( $command );

// read values from the dynamic created variables
for ( $i=1; $i <= 50 ;$i++ )
$command = "vector $tempVector = ($pos_loc"+$i+")";
eval( $command );
print( $tempVector.x+" : " );
print( $tempVector.y+" : " );
print( $tempVector.z+"\n" );

03 March 2006, 05:38 PM
thank you! :)

CGTalk Moderation
03 March 2006, 05:38 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.