Selecting random objects in selection MEL script

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

THREAD CLOSED
 
Thread Tools Search this Thread Display Modes
  02 February 2013
Selecting random objects in selection MEL script

Hello.

I need help with MEL script that i found in cgsociety archives.



/*
randomSubSelect by Nicolas d'Haussy
Description : Randomly selects a given percentage of objects into your current selection. Cool tool for randomizing object manipulations.
Usage : Source this script and select many objects type "randomSubSelect(75)" for 75%.
*/



// Checks if a INT variable is contained in the given array

global proc int isIntInArray(int $array[], int $int) {

	for($arrayInt in $array) {

		if($int == $arrayInt) { return 1; }
	}

	return 0;
}



global proc randomSubSelect(float $percentage) {

	string $selection[] = `ls -sl`;
	int $objectsToSelectNb = ( ($percentage/100) * `size $selection`);
	print $objectsToSelectNb ;

	select -cl;
	int $n = 0;
	int $numberArray[];

	while(`size $numberArray` < $objectsToSelectNb) {

		int $randomNb = `rand 0 $objectsToSelectNb`;

		if(! `isIntInArray $numberArray $randomNb`) {

			select -add $selection[$randomNb];

			$numberArray[$n] = $randomNb;
			$n++;
		}
	}
}


What i need is percentage of objects in my selection to be selected(rest deselected), but this script selects objects in quite logical(not random) fashion. If I duplicate cube 100 times and execute this script on them(for example i want 60% to be randomly selected) it selects cube1 to cube60.

How could i change this script so it selects randomly?

Help would be much appreciated.
Thanks

Last edited by kristapovic : 02 February 2013 at 11:33 PM.
 
  02 February 2013
Hi I can't help you in MEL but I've wrote a python script for you.

Just execute it !

I hope it help.


  """Script by DrWeeny"""
  """Random Selector V001"""
  """http://dr.weeny.blogspot.com"""
  
  import random
  import maya.cmds as cmds
  
  
  def selector(*arg):
  	RandomSelection = []
  	mySel = cmds.ls(sl=True)
  	PercentEnter= cmds.intFieldGrp(Percent, q=True, value1=True)
  	
  	New_sel_nb = len(mySel)*int(PercentEnter)/100
  	
  	if len(mySel)== 0:
  		raise RuntimeError, ("Nothing has been selected")
  	
  	while len(RandomSelection) < New_sel_nb:
  		randomNumber = random.randrange(0, len(mySel),1)
  	
  		RandomSelection.append(mySel[randomNumber])
  	
  		RandomSelection = list(set(RandomSelection))
  		
  	cmds.select(RandomSelection, r=True)
  
  if cmds.window("UI", exists = True):
  	cmds.deleteUI("UI")
  		
  window = cmds.window("UI", s=False)
  cmds.columnLayout()
  Percent = cmds.intFieldGrp( numberOfFields=1, label='Select', extraLabel='Percent', value1=0)
  ScriptButton = cmds.button(label ='Go !', c=selector)
  cmds.showWindow( window )
  


I've uploaded it in mediafire :
http://www.mediafire.com/?goslec21ht0ubex

--- EDIT ---

I may add comments to the script tomorrow (or correct it if you find a bug.)
__________________
DrWeeny
My Blog
My Vimeo
LinkedIn

Last edited by DrWeeny : 02 February 2013 at 01:39 AM.
 
  02 February 2013
Hi DrWeeny!

Thanks for the help. First line seems to have error, but using my super limited scripting skills i managed to make it work.

I had to change first line like so

import select randomly as RandomSelection


And after I close the script, I can't reopen it. I have to restart maya to make it work again, but other then that main function works perfectly. Thank you very much.
 
  02 February 2013
Did you try to download the mediafire file of the script ? You should not have to modify anything.

I tried here on three computers (maya 2012/2013), it works perfectly.

Cheers !
__________________
DrWeeny
My Blog
My Vimeo
LinkedIn
 
  02 February 2013
Yea, I download from mediafire. Ether way this is great script. Thanks.
 
  02 February 2013
i've written this, which is almost the same as your script:
global proc JPL_randomSelec (float $perc)
{
	string $allObj [] = `ls -l -sl`;

	//important to declare this array as an empty array if you try this script in the scriptEditor:
	//as everything is declared as global, you would have always the same selection
	string $randomSelec [] = {};
	float $objectsToSelectNbFloat = (($perc/100) * `size $allObj`);
	int $objectsToSelectNb = $objectsToSelectNbFloat;

	if (size ($allObj))//very important to avoid infinite loop
	{
		while (size ($randomSelec) < $objectsToSelectNb)
		{
		    int $random = `rand (size ($allObj))`;
		    if (!stringArrayContains ($allObj [$random], $randomSelec))
		    	$randomSelec [size ($randomSelec)] = $allObj [$random];
		}

		select -r $randomSelec;
	}
}

//JPL_randomSelec (50);

Last edited by depakeen : 02 February 2013 at 05:23 PM.
 
  03 March 2013
Hi everyone,
just a little contribution, here is a selection script I did some times ago.
With this, it is possible to remember your selection, and select randomly with a given percentage.
It comes with a UI.
Enjoy !

global proc selectPercent(int $percent)
{
	intSliderGrp -e -v $percent daValue;
	KernFade();
	global string $KernSelectKeep[];
	// string $getSel = `textField -q -tx KernSelectKeepField`;
	string $sel[];
	if(size($KernSelectKeep)==0)
	{
		$sel = `ls -sl -l`;
	} else {
		$sel = $KernSelectKeep;
	}
    string $newSel[] = $sel;
	int $numberOfItemsSelected = size($sel);
	int $numberOfItemsToSelect = ($numberOfItemsSelected * $percent * 0.01);
    int $i = 0;
	string $items[];
	select -r $sel;
	for ($i=0;$i<($numberOfItemsSelected - $numberOfItemsToSelect);$i++)
	{
		$remaining = size($newSel);
		int $rand =  rand(0,$remaining);
		$items[$i] = $newSel[$rand];
		stringArrayRemoveAtIndex($rand, $newSel);
	}
	$newSel = stringArrayRemove($items, $newSel);
	
    select -r $newSel;
    print ("original => " + size($sel) + " objects selected, result => " + size($newSel) + " objects selected, value => " + $percent + "%");
}

global proc KernFade()
{
    $value = `intSliderGrp -q -v daValue`;
	text -e -l ("Percent : "+$value+"%") daMsg;
    float $red = (0.1625 + ($value*0.00375));
    float $green = (0.5 - ($value*0.00375));
    if ($red>0.5) $red=0.5;
    if ($red<0.2) $red=0.2;
    if ($green>0.5) $green=0.5;
    if ($green<0.2) $green=0.2;
    window -e -bgc $red $green 0.2 KernSelectUI;
	iconTextButton -style "textOnly" -e -bgc $green $red 0.2 KernSelectAction;
	iconTextButton -style "textOnly" -e -bgc $green $red 0.2 KernSelectForgetBtn;
	iconTextButton -style "textOnly" -e -bgc $red $green 0.2 KernSelectKeepBtn;
}
global proc KernSelectClear()
{
	global string $KernSelectKeep[];
	clear $KernSelectKeep;
}
global proc KernSelectUI()
{
if (`window -ex KernSelectUI`) deleteUI KernSelectUI;
    window -title "KernSelectUI ToolBox" -bgc 0.2 0.5 0.2 -rtf 1 KernSelectUI;
        columnLayout -adjustableColumn true daSelectColumn;
				flowLayout -columnSpacing 0;
					iconTextButton -style "textOnly" -w 30 -l "10%" -bgc 0.2 0.4625 0.2 -c "selectPercent(10);";
					iconTextButton -style "textOnly" -w 30 -l "20%" -bgc 0.2375 0.425 0.2 -c "selectPercent(20);";
					iconTextButton -style "textOnly" -w 30 -l "30%" -bgc 0.275 0.3875 0.2 -c "selectPercent(30);";
					iconTextButton -style "textOnly" -w 30 -l "40%" -bgc 0.3125 0.35 0.2 -c "selectPercent(40);";
					iconTextButton -style "textOnly" -w 30 -l "50%" -bgc 0.35 0.3125 0.2 -c "selectPercent(50);";
					iconTextButton -style "textOnly" -w 30 -l "60%" -bgc 0.3875 0.275 0.2 -c "selectPercent(60);";
					iconTextButton -style "textOnly" -w 30 -l "70%" -bgc 0.425 0.2375 0.2 -c "selectPercent(70);";
					iconTextButton -style "textOnly" -w 30 -l "80%" -bgc 0.4625 0.2 0.2 -c "selectPercent(80);";
					iconTextButton -style "textOnly" -w 30 -l "90%" -bgc 0.5 0.2 0.2 -c "selectPercent(90);";
					setParent ..;
				columnLayout -adjustableColumn true daAttrColumn;
				text -l "Percent : 10%" daMsg;
				intSliderGrp -field false
					-minValue 0 -maxValue 100
					-fieldMinValue 0 -fieldMaxValue 100
					-dc "KernFade()"
					-cc "KernFade()"
					-h 24
					-value 10 daValue;
				rowLayout -numberOfColumns 2 -adj 2;
					columnLayout -adjustableColumn true;
						iconTextButton -style "textOnly" -l "keep selection" -w 100 -vis 1 -bgc 0.2 0.5 0.2 -c "global string $KernSelectKeep[]; $KernSelectKeep = `ls -sl -l`;iconTextButton -e -vis 0 KernSelectKeepBtn;iconTextButton -e -vis 1 KernSelectForgetBtn;" KernSelectKeepBtn;
						iconTextButton -style "textOnly" -l "forget selection" -w 100 -vis 0 -bgc 0.5 0.2 0.2  -c "clear $KernSelectKeep; iconTextButton -e -vis 0 KernSelectForgetBtn;iconTextButton -e -vis 1 KernSelectKeepBtn;" KernSelectForgetBtn;
						setParent ..;
					iconTextButton -style "textOnly" -l "Select" -bgc 0.5 0.2 0.2 -c "$value = `intSliderGrp -q -v daValue`; selectPercent($value);" KernSelectAction;
					setParent ..;
				setParent ..;
				rowLayout -numberOfColumns 2 -adj 1;
					iconTextButton -style "textOnly" -l "close" -h 15 -c "deleteUI KernSelectUI";
					iconTextButton -style "textOnly" -l "refresh" -h 15 -w 150 -c "source KernSelect.mel; KernSelectUI();"; 
				setParent ..;
			setParent daSelectColumn;
    showWindow KernSelectUI;
	window -e -w 271 -h 107 KernSelectUI;
	scriptJob -uid KernSelectUI KernSelectClear;
	KernFade();
}
KernSelectUI();
__________________
Seuls les sages et les fous forceront l'huis des mondes.
 
  03 March 2013
Thread automatically closed

This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.
__________________
CGTalk Policy/Legalities
Note that as CGTalk Members, you agree to the terms and conditions of using this website.
 
Thread Closed share thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
CGSociety
Society of Digital Artists
www.cgsociety.org

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump
Miscellaneous

All times are GMT. The time now is 09:56 AM.


Powered by vBulletin
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.