Home Coding, Scripting, Shaders

MEL how to loop through a list and update the value each time.

polycount sponsor
Offline / Send Message
malcolm polycount sponsor
Hi guys, I'm trying to take my selection of 5 objects and print the name of first object, then when I run the code a second time print the name of the next object and so on. This works until it gets to the end of the list, but after that the scripts dies and I'd like it to go back to the beginning of array and print the names. I'm sure I need to use a for loop here, but I can't figure out what that looks like as it would need to update the counter with each run of the script. Could I have some help please.

$myList = `ls -sl`;
int $myCounter;
print $myList[$myCounter++];

Replies

  • Klaudio2U
    Options
    Offline / Send Message
    Klaudio2U polycounter lvl 8
    Yes, the loops are way to go i guess. The only thing to consider additionally is to store two values at the beginning.
    One int would be the counter as you already did and the second one would be a total count of selected objects....this will allow you to compare counter value with total count of the object and once they are equal (reached the end of the list) you print all the names and reset the counter int so that it can start again.

  • malcolm
    Options
    Offline / Send Message
    malcolm polycount sponsor
    I couldn't figure out how to solve this with a loop, I was able to get it to work after many hours of trial and error by using an if else statement.
    Here's how I got it working by getting a list of selected objects and then counting through them each time you run the script, when it reaches the last item in the list it resets back to the first item.

    string $sel[] = `ls -sl`;
    int $num_obj = size($sel);
    global int $countThis;

        if ($countThis!=$num_obj)
        {
            $countThis = $countThis +1;
            print ($countThis + " yes increment");
        }   

        else
        {
            $countThis = 0;
            print ($countThis + " no increment");
        }

    And here's another version that does the same thing, but instead of a returning a number it returns the current UV set and counts through them

    $maQueryUVSetName = `polyUVSet -q -allUVSets`;
    int $maNumb_UVSets = size($maQueryUVSetName);
    $maUV_ReduceByOne = $maNumb_UVSets -1;
    global int $maCountThisUVSet;

        if ($maCountThisUVSet<$maUV_ReduceByOne)
        {
            $maCountThisUVSet = $maCountThisUVSet +1;
            polyUVSet -currentUVSet -uvSet $maQueryUVSetName[$maCountThisUVSet];
            print ("UVs set to " + $maQueryUVSetName[$maCountThisUVSet]);
        }   

        else
        {
            $maCountThisUVSet = 0;
            polyUVSet -currentUVSet -uvSet $maQueryUVSetName[$maCountThisUVSet];
            print ("UVs set to " + $maQueryUVSetName[$maCountThisUVSet]);
        }
  • poopipe
    Options
    Offline / Send Message
    poopipe grand marshal polycounter
    um
    you want to iterate through the selection and print information about each thing right ? 

    I don't do mel (I will literally fight you if you try to make me) but according to google this should give you a start

    $things = `ls-sl`;

    for ($i in $things) {

        print $i;

    }



    or if you're being sensible and use python/pymel instead you want this logic (which is the same but not totally impenetrable)

    import pymel as pm
    things = pm.selected()
    for thing in things:
        print thing



    *caveat - didn't test it, also ive had some wine


  • malcolm
    Options
    Offline / Send Message
    malcolm polycount sponsor
    Thanks for you help, but no I want to print the first thing in the list, then the second time I run the script print the second thing in the list, and so on, when it reaches the end of the list I want it to loop back and print the first thing. I figured it out after bashing my head for two days, I posted the solutions above for the community.
Sign In or Register to comment.