PDA

View Full Version : How to control array in mel?


yogeshsherman
10-29-2010, 03:00 PM
actually I want to do this thing in mel
if my statement set true "3" then that third statement should run to maya memory (source it) , so that i can use this this array in for loop but maya don't take it in it's memory but if i put simple command in, "if else" function like sphere or polcube then maya source this thing so any of you guys know any way with which i can source(store) this variable with my control. Following is the code i have written so far and it is not working the way I wan't it to be.


int $numbers=3;

if ($numbers==3)
{
string $numbers[] = {"red", "green","blue"};
}
else if ($numbers==2)
{
string $numbers[] = {"red", "green",};
}
else if ($numbers==1)
{
string $numbers[] = {"red"};
}
else
{
string $numbers[] = {"red", "green","blue","yellow};

}

NaughtyNathan
10-29-2010, 03:14 PM
it's all about scope. any variable you DECLARE inside { } ONLY exists inside those brackets. you need to declare the array outside any of that scope and then simply fill it inside the { }

secondly, you have two variables called tha same thing $numbers which is not ideal, and is an error in the same scope.

int $numbers=3; // keep your variable names different!
string $colors[] = {}; // declare the array outside of any if { } scope

if ($numbers==3)
{
$colors = {"red", "green","blue"}; // now simply fill-in the array contents
}
else if ($numbers==2)
{
$colors = {"red", "green",};
}
else if ($numbers==1)
{
$colors = {"red"};
}
else
{
$colors = {"red", "green","blue","yellow};
}

This would also be a good example of when to use the switch..case style
:nathaN

mduvekot
10-30-2010, 03:27 AM
I'd do this:


string $numbers[] = {"red", "green","blue","yellow"};
int $num = 3;
while ($num < `size $numbers`) {
stringArrayRemoveAtIndex ((`size $numbers`-1), $numbers);
}

yogeshsherman
10-31-2010, 04:16 AM
NaughtyNathan you are right now the script works perefectly the way i wanted.Thank you.

mduvekot you are also right we can write the script in this way too but according to your way we don't have that much control,because through your way only one thing is removed in order.

CGTalk Moderation
10-31-2010, 04:16 AM
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.