Announcements

Starting in December, we will archive content from the community that is 10 years and older. This FAQ provides more information.

Community
Maya Programming
Welcome to Autodesk’s Maya Forums. Share your knowledge, ask questions, and explore popular Maya SDK topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Random Select Objects in Group by Percentage in Mel?

6 REPLIES 6
SOLVED
Reply
Message 1 of 7
victorbennett
3521 Views, 6 Replies

Random Select Objects in Group by Percentage in Mel?

Can someone help me figure out how to write out a way to randomly select objects that are in a group by a percentage in Mel?  I need to embed this into another script I'm making. 

 

Thanks!

 

Vic

6 REPLIES 6
Message 2 of 7
mcw0
in reply to: victorbennett

string $objects[] = `listRelatives -type "transform" -pa "yourGroup"`;  //  FIRST GET OBJECTS IN GROUP

int $num = size($objects);  //  NUMBER OF OBJECTS

float $percent = 50.0;  //  NOW SET YOUR PERCENTAGE OF OBJECTS TO SELECT

int $numToSelect = ($percent/100.0) * $num;  //  ACTUAL NUMBER OF OBJECTS TO SELECT

//  USE A RANDOM NUMBER FUNCTION TO DETERMINE IF AN OBJECT IS SELECTED OR NOT

float $threshold = $percent/100.0;  //  SINCE THIS EXAMPLE USES 50%, A THRESHOLD OF .5 IS USED

string $list[];  //  RECORD THE OBJECTS TO SELECT

while(size($list) < $numToSelect)  //  KEEP SELECTING UNTIL DESIRED PERCENTAGE IS ACHIEVED

{

    for($object in $objects)  //  LOOP THROUGH YOUR OBJECTS TO DETERMINE WHICH ARE SELECTED

    {

        if(rand(1) > $threshold)

            $list[size($list)] = $object;

    }

    $objects = stringArrayRemove($list, $objects);  //  REMOVE SELECTED OBJECTS FROM YOUR ORIGINAL LIST

}

 

 

This was typed without trying it in Maya.  But the logic is there.  And I'm pretty sure it will work.  Using a "while" conditional is always hazardous, but in this case, you will eventually hit your percentage so it shouldn't go into an endless loop.  Good luck.

Message 3 of 7
victorbennett
in reply to: mcw0

Thanks!  However, it's not selecting anything and I'm not getting any errors...

 

Vic

Message 4 of 7
mcw0
in reply to: victorbennett

I'm so sorry.  I forgot the last line!!!

 

select -r $list;

Message 5 of 7
victorbennett
in reply to: mcw0

Works great.  Thanks!

Message 6 of 7
shmelov
in reply to: mcw0

This topic made my day. Many thanks to autor, and especialy to mcw0. That script solved a big problem for me!

Message 7 of 7
victorbennett
in reply to: shmelov

So glad it helped!

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report