Re: AddtoSelection scripting question

Date : Fri, 11 Jan 2008 11:49:17 -0500
To : XSI(at)Softimage.COM
From : "Ajit Menon" <xsiajit(at)gmail.com>
Subject : Re: AddtoSelection scripting question
Here's the JS again, renamed as a text document:
 
I've attached the entire script.
Basically u select any number of objects in your scene (any kind) and when you run it, enter a percentage value, say 20%.
The script is supposed to generate unique random objects and select only 20% of the selected items.
 
I made a small error when I sent in the original script... my loop actually runs from 1 to arrayLength coz I assumed that u have to do at least one SelectObject before I can use AddToSelection...
correct me if I am wrong though.
 
thanks again though!


 
On 1/11/08, Ajit Menon <xsiajit(at)gmail.com> wrote:
I've attached the entire script.
Basically u select any number of objects in your scene (any kind) and when you run it, enter a percentage value, say 20%.
The script is supposed to generate unique random objects and select only 20% of the selected items.
 
I made a small error when I sent in the original script... my loop actually runs from 1 to arrayLength coz I assumed that u have to do at least one SelectObject before I can use AddToSelection...
correct me if I am wrong though.
 
thanks again though!


 
On 1/11/08, Ponthieux, Joey <j.ponthieux(at)nasa.gov > wrote:
Someone correct me if I'm wrong here but aren't you adding the same
object back to the Selection Object?

When you use SelectObj it places whatever into Selection, in this case
it looks as though objArray[0] gets placed in Selection(0), but without
knowing what the content of objArray is this could be questionable.

Then you set up a loop, which starts at 0 and you attempt to iterate
through objArray ultimately adding
objArray[0] back into Selection where it already exists.

Joey Ponthieux
NCI Information Systems Inc.
NASA Langley Research Center
____________________________________________________________
Opinions stated here-in are strictly those of the author and
do not represent the opinions of NASA or any other party.



Ajit Menon wrote:
> Hi all
> I have a simple selection script which however every now and then
> gives me an error:
>
> // WARNING : 3030-SELE-AddToSelection - Command was cancelled.
>
> I have no idea why it's doing it coz it seems random. Here's that part
> of the script:
>
>
> SelectObj(objArray[0], null, true);
>
> for (s=0;s<arrayLength;s++)
> {
>
> // logmessage("Selected Array index is :"+sel);
> // logmessage("Selected Object is :"+objArray[sel]);
> AddToSelection (objArray[s], null, true);
> }
>
>
>
> Thanks in advance
> Ajit
---
Unsubscribe? Mail Majordomo(at)Softimage.COM with the following text in body:
unsubscribe xsi



// Fad Select Percentage Script
// Ajit Menon and Antn Marini Jan 2008
// Thanks to Anton for writing the unique random number routine
// This script randomly selects objects based on an inputted percentage value
// 

var mySceneRoot;

var myppg = ActiveSceneRoot.AddProperty("CustomProperty" , true , "Random Selection" ) ;
myppg.AddParameter3( "Percentage_Value" , siInt4 , 0 , 0 , 100 , false) ;
	
var ppgGET = InspectObj(myppg, null , "Random Selection" , siModal) ;

if ( !ppgGET){
		//collect the values from the ppg
		var percentage = myppg.Percentage_Value.value ;
		
var p="";
var selList=GetValue("SelectionList");
var max=selList.count;
var arrayLength = Math.floor(percentage*max/100);
logmessage("It should select "+arrayLength+" objects");
var myArray = [arrayLength];
var objArray = [max];

myArray[0] = -1;

for (i=0;i<max;i++)
    {   
    	 p=selection(i).fullname;
       objArray[i]= p;
	// logmessage("Object "+i+" is "+p);
     }

for(j=0;j<arrayLength;j++)
	{
		setRandom(j);
		//logmessage("index number :" + j + " is : " + myArray[j]);
		
	}	
}

SelectObj(objArray[myArray[0]], null, true);
logmessage ("--------------------------------------------------------------------------")
for (s=1;s<arrayLength;s++)
	{
	sel =myArray[s];
	//logmessage("Selected Array index is :"+sel);
	logmessage("Selected Object is :"+objArray[sel]);
	AddToSelection (objArray[sel], null, true);

	}
	

	
function checkForDupes(number, index)
{
	for(i=0;i<arrayLength;i++)
	{			
	//logmessage("checking index" + i);

	//logmessage(" calling checkForDupes - index is: " + i + " number is "+ number);
		if(i != index)
		{
			if(myArray[i] == number)
			{
				return 1; // is a dupe
			}
			else
			{
			}
		}
	}
	return 0;
}

function setRandom(index)
{
	rand = Math.floor(Math.random()*max+1);
	//logmessage("calling setrandom with random number :" + rand);
	myArray[index] = rand;
	
//logmessage("now index contains " + myArray[index]);
	
	if(checkForDupes(rand, index))
	{
//		logmessage("is a dupe");
		setRandom(index);
	}
	else
	{
	}
}

Search the XSI List archives here or use the advanced search form to search across mailing lists. Searching help is available.
This site supposedly brought to you by Benjamin Grosser and the Imaging Technology Group.