Below is a simple, simple mel script which is trying to reset a variable to a new value via an IF statement. As you can see FROG is 10 and COW is 400. The if statement checks to see if FROG is greater than or equal to 10, which it is - so it then sets COW to a new value... but when the if statement finishes and COW is printed, it is NOT the new value... WHY ?
just copy and paste this into your script editor and hit enter - watch and see as variables get lost in the 'if statement triangle'
int $FROG = 10; int $COW = 400;
print ("++++++++++++nSTARTING with FROG: " + $FROG + ", and COW: " + $COW + "n");
if ($FROG >= 10) { int $COW = 6100; print ("FROG is greater than or equal to 10.n Setting COW to new value: " + $COW + "n"); } else { print ("COW is now: " + $COW + "n"); int $COW = 3; }
print ("COW is now: " + $COW + "n ++++++++++++++nnn");
Boy is Maya picky! I solved my own problem and thought I would share...
it seems my error was WITHIN the if statement... I was re-declaring COW using 'int'. . . if I just declared the variable like this: $COW = 6100; rather than int $COW = 6100;
This has to do with scope. In the if statement you are declaring a varible named $COW and it's scope is only availabel in the if statement. When you exit the if statement you are back to the $COW that you set outside of the if statement. To not have this behaivor all you have to do is rewrite your script as follows.
//------------------start int $FROG = 10; int $COW = 400;
print ("++++++++++++nSTARTING with FROG: " + $FROG + ", and COW: " + $COW + "n");
if ($FROG >= 10) { $COW = 6100; print ("FROG is greater than or equal to 10.n Setting COW to new value: " + $COW + "n"); } else { print ("COW is now: " + $COW + "n"); $COW = 3; }
print ("COW is now: " + $COW + "n ++++++++++++++nnn");