Notifications
Clear all

[Closed] Random Selection – Need Unique Array

Hello Everyone,

I wanted to select random polygons of an object so made a script, it takes the percentage of the total polygons to be selected from the user… Also the script works fine, but If user puts 50 in textbox, the result is less than 50 percent…:

rollout rnd_ply “Random Polygon Selector” width:250 height:150
(
groupBox grp1 “Videep’s Random Polygon Selector” pos:[5,5] width:240 height:140
label lbl1 “Percentage to be Selected (approx.)” pos:[25,35] width:120 height:30
editText text “” pos:[180,35] width:50 height:30
button btn1 “Select” pos:[90,95] width:85 height:35
on btn1 pressed do
(
if selection.count == 1 then
(
c = text.text as integer
a = getNumFaces $
b = a * (c * .01)

		for i = 1 to b do
		(
			y = random 1 b
			firstArray = #(y)
		)

		for i = 1 to b do
		(
			y = random 1 a
			firstArray[i] = (y)
		)

polyop.SetFaceSelection $ firstArray

	)
	Else
	Messagebox "Please Select one EditablePoly"
)

)
createdialog rnd_ply

11 Replies

You forgot check duplicate face on each random


			local firstArray = #()
			while (firstArray.count<b and firstArray.count<a) do
			(
				y = random 1 a
				if ((firstArray as bitarray)[y]) then
				(
				)
				else
					append firstArray y
			)
			polyop.SetFaceSelection $ firstArray

Tried your code, Returns an error :

– Compile error: no local declarations at top level: firstArray
– In line: local firstArray =

This is just a section of full code.
please put my code replacing your previous code

I did exactly what you said earlier…

Replaced a part of my code with yours, thats when It gave me the error…

the error come from wrong declaring variable
Try the full code


rollout rnd_ply "Random Polygon Selector" width:250 height:150
(
	groupBox grp1 "Videep's Random Polygon Selector" pos:[5,5] width:240 height:140
	label lbl1 "Percentage to be Selected (approx.)" pos:[25,35] width:120 height:30
	editText text "" pos:[180,35] width:50 height:30
	button btn1 "Select" pos:[90,95] width:85 height:35
	on btn1 pressed do
	(
		if selection.count == 1 then
		(
			c = text.text as integer
			a = getNumFaces $
			b = a * (c * .01)


			local firstArray = #()
			while (firstArray.count<b and firstArray.count<a) do
			(
				y = random 1 a
				if ((firstArray as bitarray)[y]) then
				(
				)
				else
					append firstArray y
			)
			polyop.SetFaceSelection $ firstArray

		)
		else
		Messagebox "Please Select one EditablePoly"
	)
)
createdialog rnd_ply

Awesome!!! You are a Genius!!!

Can you also explain your part of code??

It would be a great help to me.

That is just basic function of programming and maxscript. You should try to research by yourself, it will help you learing maxscript faster. Try to make debug with maxscript listener and review the maxscript reference first.

Hey!!

Thank You for your tip. I will definitely do it.

I was just confused with the following line:

while (firstArray.count<b and firstArray.count<a) do

what does this line mean??
At first it says if elements in firstArray are less than b and then it says if elements in second array are less than a … so what will computer read ???

i don’t want to disappoint anyone but the basic algorithm is wrong. if you try to randomly collect 99% of 1,000,000 faces using this algorithm probably you will not see the end.

Page 1 / 2