PDA

View Full Version : why is my mel wrong


photons
11-13-2005, 03:43 AM
guys iam new toscripting in mel i am looking for a practical way to learn it i think it more effective.

I have knowledge of programmingthe but in mel i dont know the tecnique where to declare what and why like variables when uare using expression editor--do they get re initialized or get multiple declaration....can u give a brief intro about it would be really helpful
ok this is a small program i made to check the looping concept

int $a =1;
if($a<100){
print("\na is less than 100 it is "+$a);
$a++;
}
else
{
print("its reach " $a " it a number below it");
}


now i type this in script editor and run it and i get only one print line of the text where i was expecting the print command to be executed 99times-- one after the other......i think it is get reset again when the first part of if statement is executed how do i avoid this have i declared the variable in the wrong place or i should declare it some where else

support me i am going bonkers!!@@#@#@#!@!


regards
The photon Master


one more thing : most of the link on mel that are mentioned on cgtalk i find them dead can any one please pass me a active link on mel for biggners or any thing similar to that

wrend
11-13-2005, 04:40 AM
what you have is just a condition statement, not a looping construct. have a look in the mel help documentation under something like 'flow control' where they lay out clearly the various looping constructs (for, while, do, etc). best way to learn is to look at alias' scripts, and other peoples (there are litterally thousands on www.highend3d.com ...but never believe they are the prime examples, as some are pretty poor).

photons
11-13-2005, 07:19 AM
thanx mate i think i will take ur advise ....but can u solve this problem how do i declare variables in expression editor ...i will look in documentation from my side but can u tell me more about expression editor ....i hope i make some sense.....
than mate for the help...
regards
The Photon Master

Segmoria
11-13-2005, 02:40 PM
Loop using a for statement:

for ($a = 1; $a<100; $a++)
{
print("\na is less than 100 it is " + $a);
}

YourDaftPunk
11-13-2005, 04:49 PM
Also your line:
print("its reach " $a " it a number below it");

Should be
print("its reach " + $a + " it a number below it");

I'd use the 'for' loop as well, but you structured it almost like a 'while' loop. Yours:
int $a =1;
if($a<100){
print("\na is less than 100 it is "+$a);
$a++;
}

Would have worked if it was:
int $a =1;
while($a<100){
print("\na is less than 100 it is "+$a);
$a++;
}

Jhavna
11-17-2005, 03:33 PM
I have knowledge of programmingthe but in mel i dont know the tecnique where to declare what and why like variables when uare using expression editor--do they get re initialized or get multiple declaration....can u give a brief intro about it would be really helpful
ok this is a small program i made to check the looping concept
t

In answer to this, variables declared like that in the script editor are global to Maya. So say you declare $i_x somewhere. If you use it anywhere else (in MEL) $i_x will reference the variable you declared earlier. They are only destroyed when Maya exits. So make sure you set variables explicitly before using them, otherwise strange thigns may happen if you are using a variable that's been decalred elsewhere.

To keep variables local enclose scripts in {}'s so you would have


{
$a = 1;
while ($a < 100)
{
echo ("Running loop iteration " + $a);
$a++;
}
}


Now $a will be destroyed when that script is executed (or when it runs past the } and goes out of scope)

If I'd declared $a elsewhere and not called $a = 1 here (and not enclosed the script in {}), $a could have been anything, so it's always good to set variables before using them

(technically $a could be anything even if it's newly declared with int $a; There's no guarantees that it will be 0 in this case)

Hope that helps too :)

CGTalk Moderation
11-17-2005, 03:33 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.