Home Technical Talk

Maya mel script - iterate over array problem / save face count in integer variable

polycounter lvl 12
Offline / Send Message
Pinned
Hupie polycounter lvl 12
Hey there,

I'm having a small problem and somehow I can find a solution on the Internet. I'm trying to iterate over an array of objects and display face count  with a for loop (not for in loop).

I tried various syntax's but somehow I am not able to save the face count into a integer variable, what am I doing wrong?

string $pObjs[] = `ls -sl`; //list selection<br>$objCount = size($pObjs); //object count<br><br>for( $i=0; $i<$objCount; $i++ ){<br>   int $facecount = polyEvaluate -f $pObjs[$i];<br>   print ($facecount);<br>}<br>

output:

// Error:    int $facecount = (polyEvaluate -f $pObjs[$i]);
 //
// Error: Line 5.35: Invalid use of Maya object "polyEvaluate". //
// Error:    int $facecount = (polyEvaluate -f $pObjs[$i]);
 //
// Error: Line 5.43: Invalid use of Maya object "f". //

Replies

  • Klaudio2U
    Offline / Send Message
    Klaudio2U polycounter lvl 8
    polyEvaluate is a command so it goes in those single quotes as you use in: string $pObjs[] = `ls -sl`;

    Instead of this:
    int $facecount = polyEvaluate -f $pObjs[$i];
    it goes like this:
    int $facecount = `polyEvaluate -f $pObjs[$i]`;


    Since i am not in front of the computer i cant give it a test if that’s the only problem.
  • Axi5
    Offline / Send Message
    Axi5 interpolator
    Oddly enough, despite the documentation saying that polyEvaluate -f returns an int:

    // Error: line 5: Cannot convert data of type int[] to type int. // 

    Convert the type to an array and you'll be good to go:

    int $facecount[] = `polyEvaluate -f $pObjs[$i]`;

    EDIT:
    Had an odd thought that the command would return an array of ints when you had multiple objects selected. It would have meant that you could skip the setup/loop. Unfortunately when I was expecting 400, 16 I was instead given 416. So it summed them together to give me a single int in an array.

    Does anyone have any idea why that's the case?

    I'm not a MEL person I usually stick to Python so maybe this is just normal?
  • throttlekitty
    Offline / Send Message
    throttlekitty ngon master
    I'm stumped on that too. Was hoping that getting some sleep would help... but....

    $facecount is a regular integer, getting a new value each time the loop runs, I don't get why maya thinks that it should be an array.
  • Bartalon
    Offline / Send Message
    Bartalon polycounter lvl 12
    Perhaps it has something to do with certain other polyEvaluate flags returning array values, so all return values must be an array.  From what I understand of MEL, a variable can't be redefined once it's been set as something like it can in Python.

    That's probably why `ls -sl` always returns an array for a single object also... As annoying as that is.
  • Hupie
    Offline / Send Message
    Hupie polycounter lvl 12
    Thank you for all the valuable answers! I think you are right about the return type, Bartalon.

    I found out that the real problem I was having is that I was using the single quote character (') instead of the accent grave character (`) when trying to type my own polyEvaluate command.
Sign In or Register to comment.