Home Technical Talk

What means "for ($c in $allObjects)" condition?

sukhpreet
null
string $sel[] = `ls -sl`; 
string $allDecendants[] = `listRelatives -fullPath  -allDescendents $sel`;
string $allObjects[] = stringArrayCatenate($sel, $allDecendants);
float $lastKey = -98989898989;
for ($c in $allObjects){
    string $allAttrs[] = `listAnimatable $c`;
    for ($attr in $allAttrs){
        float $keyLst[] = `keyframe -q -tc $attr`;
        if (size($keyLst) > 0){ 
            if ($keyLst[(size($keyLst))-1] > $lastKey)
            $lastKey = $keyLst[(size($keyLst))-1];
        }
    }
}
playbackOptions -min 1 -ast 1;
playbackOptions -aet $lastKey -max $lastKey;
print ("last keyframe is " + $lastKey);

Replies

  • renderhjs
    Options
    Offline / Send Message
    renderhjs sublime tool
    not sure where this is from but some languages use the $ sign as a variable placeholder (e.g. perl, php, maxscript,..). Some programmers that had exposure to web or backend languages tend to use the $ syntax in other languages even when it is not mandatory.
    One of the design goals with Perl's variables for example was to make them visually distinguishable from builtins, reserved words hence why variables used the prefix '$'

    The for part is also known as a for each loop. 
    for ($c in $allObjects){<br>...<br>}

    The script creates a local variable $c and uses it to iterate through the values of $allObjects. Within the { } brackets the $c variable changes on each iteration to the next item of the $allObjects collection.

    Hope that explains, personally I find the above code hard to read, to many $ signs and inconsistent ' ' signs. Is this perl in Modo?
  • RN
    Options
    Offline / Send Message
    RN sublime tool
    That's Maya's MEL language.
    I don't like it either.
  • sprunghunt
    Options
    Offline / Send Message
    sprunghunt polycounter
    That's what's known as an Iterator. It's an inbuilt function that cycles through all the parts of an array:

    https://en.wikipedia.org/wiki/Iterator

    the same thing in python looks like this:

    mylist = [1, 2, 4, 56, 6, 7, 34, 99]<br><br>for line in mylist:<br>&nbsp; &nbsp; print line
Sign In or Register to comment.