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
Solved! Go to Solution.
Solved by mcw0. Go to Solution.
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.
Thanks! However, it's not selecting anything and I'm not getting any errors...
Vic
Can't find what you're looking for? Ask the community or share your knowledge.