Announcements

Between mid-October and November, the content on AREA will be relocated to the Autodesk Community M&E Hub and the Autodesk Community Gallery. Learn more HERE.

Need help from MEL experts!

Need help from MEL experts!

Anonymous
Not applicable
489 Views
10 Replies
Message 1 of 11

Need help from MEL experts!

Anonymous
Not applicable
I'm writing a simple script which should deselect some custom (entered) percentage of selected objects.
It works with 20% selection now:

//Create multiple objects (10 polySpheres for example) and slect them. Run script.
//---------------------------------------------------------------------------------
//Do selection
string $currentSelection[] = `ls -selection -flatten`;
//Get a number of items in array
int $NumItems = size($currentSelection);

$cicle = $NumItems*0.8; //Enter a part ofdeselection. I'll make it worked with entered percent later...
for($i=0;$i<$cicle;++$i) {

$rnd = rand ($NumItems);//random number for selection
int $num = $rnd; //made result integer
$selectedObject = $currentSelection;
select -d $selectedObject;

}

The problem is, that script select more than 20% of objects. I think it happens becouse random number can be the same multiple times. How to avoid this? How to check is this object where deselected or not?

I'm new in MEL, this is my first script actually.
Thank you for help ASAP.
0 Likes
490 Views
10 Replies
Replies (10)
Message 2 of 11

Anonymous
Not applicable

string $currentSelection[] = `ls -selection -flatten`;
int $numItems = size($currentSelection);

for($i=0;$i<$numItems;++$i) {
// get a random integer between 0 and 4
int $chance = rand(0,5);
// if it's 0 (a 20% chance),
if (!$chance){
// deselect an object
select -d $currentSelection;
}
}

$currentSelection = `ls -selection -flatten`;
print ("Selected " + `size $currentSelection` + " out of " + $numItems + " objects\n");
0 Likes
Message 3 of 11

Anonymous
Not applicable
Thank you for your reply, but the problem remains. If you try run your script multiple times, you'll see, that it deselect different amount of objects each time. (I've tried with 10). I need in this script deselect 20% of objects (2 objects in this case), but different each time. What could you say on this?

Kind regards.
0 Likes
Message 4 of 11

Anonymous
Not applicable
Thank you for your reply, but the problem remains. If you try run your script multiple times, you’ll see, that it deselect different amount of objects each time. (I’ve tried with 10). I need in this script deselect 20% of objects (2 objects in this case), but different each time. What could you say on this?

Kind regards.
0 Likes
Message 5 of 11

Anonymous
Not applicable
change the loop

work out how many items you want to deselect
randomise the order of the result from the selection list
remove the first number of items until you have all the items you'd like deselected.

or possibly:

work out how many items you want to deselect put it in an int
cycle over the selection list like THNKR does, reducing that int if you deselect an object. (only over and over until you finish all your needed deselections)
0 Likes
Message 6 of 11

Anonymous
Not applicable
Thank you for participating in this post.
I'm very sorry, but I didn't understood you. May be you can share a code with me?
0 Likes
Message 7 of 11

haggi
Enthusiast
Enthusiast
IN your code there is only one small problem:

string $currentSelection[] = `ls -selection -flatten`;
int $NumItems = size($currentSelection);
int $cicle = $NumItems*0.8;
for($i=0;$i<$cicle;++$i) {
int $num = rand ($NumItems);
$selectedObject = $currentSelection;
select -d $selectedObject;
}


The problem is the constant $NumItems Value. This way you can easily get the same Number twice exactly as you assumed. It is not a big problem if you have hundreds of objects, but with such a small number it will be problematic. If you switch the line to:
$currentSelection = `ls -sl`;
int $num = rand (size($currentSelection));

the procedure should work better.
0 Likes
Message 8 of 11

Anonymous
Not applicable
I think, that there should be conditional statement. If current object in cicle is deselected, than go and randomly choose another one. I don't know the syntax of this code. Im I wrong?
0 Likes
Message 9 of 11

Anonymous
Not applicable
Sorry about that.

funny part is, the rand isn't very random,
it's a random seed(maybe you could do something like
use rands instead of rand and give it a seed of machine time?),
so the same objects get deselected if you run it on the same selection.

string $currentSelection[] = `ls -selection -flatten`;
int $numItems = size($currentSelection);

// figure out how many deselects you want to make
int $numDeselects = $numItems * .2;
print ("\n\n***************\n");
print ($numItems + " objects selected.\n");
print ("Deselecting " + $numDeselects + " objects.\n");

// Keep track of the deselects made starting it with 0
int $i=0;
while($i<$numDeselects ) {
// get a random integer between 0 and 4
int $chance = rand(0,5);
// if it's 0 (a 20% chance),
if (!$chance){
// deselect an object
select -d $currentSelection;
// add up your deselects so you can
// get out of the while loop eventually
++$i;
}
}

$currentSelection = `ls -selection -flatten`;
print ("Selected " + `size $currentSelection` + " out of " + $numItems + " objects\n");
print ("***************\n");
0 Likes
Message 10 of 11

Anonymous
Not applicable
Thank you, it looks interesting. I'm trying to run your script and receive an Error:
// Error: string $currentSelection[] = `ls -selection -flatten`;
//
// Error: Invalid redeclaration of variable "$currentSelection" as a different type. //
0 Likes
Message 11 of 11

Anonymous
Not applicable
You could go through and change that variable a little so it doesn't clash with one you're already using.
If you restart Maya, the variables will be reset.
0 Likes