Feedback

What's your question?

By: Asked

How do I determine the number of selected objects in maya?

Hi I have what I'm sure is a very basic question. I am trying to make a quick and dirty utility that can import nasty poser clothes and sort them out for maya.

most poser clothes are made of lots of bits and my code will pull these in and sort them out but sometimes poser clothes are one piece of geometry and because polyunite expects more than one piece it hangs the script.

I figure I need an if statement to determine if what I have just imported and selected is greater than 1. then run the appropriate code block?

it's my first try at mel so I'm not even sure if its the right approach.

here is what I have so far(just to give you an idea of what I'm trying to do as this script is not working)

thanks in advance

      proc int onOpen(string $filename,string $type) {
       print($filename+"\n");
       print($type+"\n");
       return true;
    }


    fileBrowser( "onOpen", "Open", "", 0 );

     string $pos[];

     $pos = `SelectAllGeometry`;

     if ($pos > 1) {

          polyUnite;
          DeleteHistory;
          selectType -controlVertex true;
          polyMergeVertex  -d 0.0003;
          polySoftEdge -a 180;
          FrameSelected;
          fitPanel -selected;
          DisplayShadedAndTextured;
          DeleteHistory;

} else {

          selectType -controlVertex true;
          polyMergeVertex  -d 0.0003;
          polySoftEdge -a 180;
          FrameSelected;
          fitPanel -selected;
          DisplayShadedAndTextured;
          DeleteHistory;

}

Add comment viewed 393 times Latest activity 10 months ago

or Cancel

1 answer

  • 1

thnkr [ Editor ]

Use size to figure out how many objects are selected, then do a simple test on whether the number is non-zero.

I mostly use something like this to check for selection

string $sel[] = `ls -sl`;
if (!`size $sel`) {
    print "nothing is selected\n";
}
else{
    print "we're OK\n";
}

but if i want to do something more elaborate depending on whether I have nothing, 1, 2 or more objects slected, I might use a switch statement

string $sel[] = `ls -sl`;
switch (`size $sel`) {
    case 0 : 
        print "nothing is selected\n";
        break;
    case 1 : 
        print "one object\n";
        break;
    case 2 : 
        print "two objects\n";
        break;
    default : 
        print "too many objects\n";
        break;
}
NN comments
style45
-

Hi Thanks THNKR,

your code has helped me to move forward. I have added a selection type and also questioned whether the selection is more than 1 as this will always be the case.

If there is one mesh I need to run the block without polyUnite if there are any number or meshes greater than 1 then I need to run the block with polyUnite.

with my new code it still hangs on poly unite? interestingly I know I’m importing something that has multiple meshes so it shouldn’t be flagging that anyway.

maybe I’m not cut out for scripting. :)

style45
-

{

    string $sel[] = `ls -type "mesh"`;


    if( size($sel) == 1 ) 
    {

my code block without polyUnite

    } 
    else {

my code block with polyUnite

    }
}
style45
-
{

    string $sel[] = `ls -type "mesh"`;


    if( size($sel) == 1 ) 
    {

my code block without polyUnite

    } 
    else {

my code block with polyUnite

    }
}
or Cancel