PDA

View Full Version : Declaring variables dynamically


mrp86
02-16-2011, 11:11 AM
So I'm in a bit of a bind, basically I want the ability to declare a variable based on the value of another variable, so if $i is '1' then declare array1[] if $i is 5 declare array5[] etc.

I've tried:


string ("$array" + $n)[];


but no luck. Is this possible? Technically I could probably use a matrix If not..

btw, the purpose is to take a list of duplicated groups, list the curves in the group and then create a list for each curve so that I can apply a loft to it. Atm looks a bit like as follows (obv not finished, but gives you an idea of what I'm trying to do):


global proc makeTrack(){

// Get Selection (select the controlNode)

string $sel[] = `ls -sl`;
if (size($sel) != 1){
print "More than 1 object selected \n";
stop;
}

string $cNode = $sel[0];

// Get name of the pieces Node

string $piecesNode = `getAttr ($cNode + ".piecesNode")`;
string $pieces[] = `listRelatives -type "transform" $piecesNode`;

// For each piece, create a list of each nurbs object
for ($i = 0; $i < (size($pieces)); ++$i){
string $nurbs[] = `ls -dag -type "nurbsCurve" $pieces[$i]`;
for ($n = 0; $n < (size($nurbs)); ++$n){
string ("$trackArray" + $n)[];
("$trackArray" + $n)[$i] = $nurbs[$n];
}
}

// Create a loft for each nurbs list
string $lofts[];

for ($i = 0; $i < (size($nurbs)); ++$i){
string $lofts[] = `loft ("trackArray" + $i)`;
}

// Cleanup lofts into a group
// Parent lofts group to Control Node
}


Kind Regards,

Pete

NaughtyNathan
02-16-2011, 11:35 AM
you can't really do this in MEL (unless you eval global variables and you don't really want that..)

do yourself and all your end-users a favour Pete and learn python instead of MEL. ;)

the only real option here is to serialise all the lists into a string using stringArrayToString and then you can effectively have a list of lists in MEL.

:nathaN

btw, a matrix (in MEL) can only store floats!

mrp86
02-16-2011, 11:44 AM
you can't really do this in MEL (unless you eval global variables and you don't really want that..)

do yourself and all your end-users a favour Pete and learn python instead of MEL. ;)

the only real option here is to serialise all the lists into a string using stringArrayToString and then you can effectively have a list of lists in MEL.

:nathaN

Ahh well cheers anyway, how would you write it out in python? (The declaration of variables based on a variable bit)

Pete

goleafsgo
02-16-2011, 11:57 AM
Can you just do your loft at the end of every for loop? Instead of building an array-of-arrays and then looping over the entire thing after.

Something like:


string $lofts[];

for ($i = 0; $i < (size($pieces)); ++$i)
{
string $nurbs[] = `ls -dag -type "nurbsCurve" $pieces[$i]`;

$lofts[(`size $lofts`)] = `loft $nurbs`;
}


This way you would still end up with an array of all the loft nodes that you created, but not the separated array-of-arrays of curves.

NaughtyNathan
02-16-2011, 12:03 PM
Ahh well cheers anyway, how would you write it out in python? (The declaration of variables based on a variable bit)

you wouldn't. python allows for far more complex and useful variable structures so you would use a dictionary or a list of lists (a 2D array)

:nathaN

mrp86
02-16-2011, 12:07 PM
you wouldn't. python allows for far more complex and useful variable structures so you would use a dictionary or a list of lists (a 2D array)

:nathaN

Gotcha. Running through the syntax and learning it now.

Cheers for your help,

Pete

mrp86
02-16-2011, 01:37 PM
Can you just do your loft at the end of every for loop? Instead of building an array-of-arrays and then looping over the entire thing after.

Something like:


string $lofts[];

for ($i = 0; $i < (size($pieces)); ++$i)
{
string $nurbs[] = `ls -dag -type "nurbsCurve" $pieces[$i]`;

$lofts[(`size $lofts`)] = `loft $nurbs`;
}


This way you would still end up with an array of all the loft nodes that you created, but not the separated array-of-arrays of curves.

I don't think that would work, the loft needs to run on ever instance of the curve within each piece, i.e. if every piece has curveA and curveB, all of the curve A's need to be lofted together. The above script would just loft curveA and curveB together in each piece.

Imagine a series of duplicated fence posts, and then you want to link the wires together, where the wires have been defined by a nurbs shape.

Pete

mrp86
02-16-2011, 01:37 PM
Thanks though!

mrp86
02-16-2011, 03:04 PM
Ok.. so sort of solved and run into another problem.

This is my new script:


global proc makeTrack(){

// Get Selection (select the controlNode)

string $sel[] = `ls -sl`;
if (size($sel) != 1){
print "More than 1 object selected \n";
stop;
}

string $cNode = $sel[0];

// Get name of the pieces Node

string $piecesNode = `getAttr ($cNode + ".piecesNode")`;
string $pieces[] = `listRelatives -type "transform" $piecesNode`;
string $nurbs[] = `ls -dag -type "nurbsCurve" $pieces[0]`;
string $loft[];
string $lofts[];

// For each piece, create a list of each nurbs object
for ($i = 0; $i < (size($nurbs)); ++$i){
for ($x = 0; $x < (size($pieces)); ++$x){
$nurbs = `ls -dag -type "nurbsCurve" $pieces[$x]`;
$loft[$x] = $nurbs[$i];
}

// Create a loft for each nurbs list
string $t[] = `loft $loft`;
$lofts[$i] = $t[0];
}

// Cleanup lofts into a group
// Parent lofts group to Control Node
}


However, the problem I have now is that because every curve is instanced, the return of $nurbs always comes back as the two original curves, how can I select the specific instance?

My current alternative is to duplicate and delete each curve.

Pete

CGTalk Moderation
02-16-2011, 03:04 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.