mel

Fixing MEL FOR loop


I have been stuck doing this for if loop for the last few days.

Basically, it's a loop that starts at the height of 6 and decreases within the FOR loop, when the height (y) hits 1 I want it to start increasing by 1.

At the moment the loop hits 1, the height increases by 1 but then because it loops back around it does not increase the next shape by another 1.

I know its a bit confusing but I hope I can grab some help!

Thanks!

int $f = 6; 
for ($e = 24 ; $e <= 31; $e++) 
{
if ($f <= 1) {
string $currentObject = $objects[$e];
select -r $currentObject ;
setAttr ($currentObject+".sy") ($f++); 

}

else {
string $currentObject = $objects[$e];
select -r $currentObject ;
setAttr ($currentObject+".sy") ($f--); 

}
}

Solution

  • If I understand your problem correctly, you only want to switch the direction of increment or decrement at the extremes, not between, i. e. you need another variable that codes the direction (incrementing or decrementing). I called that $diff in this code:

    int $f = 6; 
    int $diff = -1;
    for ($e = 24 ; $e <= 31; $e++) 
    {
      if ($f <= 1) {
        $diff = 1;
      } else if ($f >= 6) {
        $diff = -1;
      }
      string $currentObject = $objects[$e];
      select -r $currentObject ;
      setAttr ($currentObject+".sy") ($f);
      $f = $f + $diff; 
    }