PDA

View Full Version : Random Selection - Need Unique Array


videep
12-22-2010, 06:44 PM
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

batigolnguyen
12-23-2010, 06:20 AM
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

videep
12-23-2010, 04:55 PM
Tried your code, Returns an error :

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

batigolnguyen
12-24-2010, 02:12 AM
This is just a section of full code.
please put my code replacing your previous code

videep
12-24-2010, 03:26 AM
I did exactly what you said earlier...

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

batigolnguyen
12-24-2010, 03:56 AM
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

videep
12-24-2010, 08:46 PM
Awesome!!! You are a Genius!!!

Can you also explain your part of code??

It would be a great help to me.

batigolnguyen
12-25-2010, 11:32 AM
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.

videep
12-25-2010, 12:16 PM
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 ???

denisT
12-25-2010, 06:24 PM
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


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.

denisT
12-26-2010, 07:03 AM
if percent < 100.0 then
(
faces = node.verts as bitarray as array
sel = #{}
num = (faces.count as float)*percent/100.0
for k=1 to num do
(
id = random 1 faces.count
sel[faces[id]] = on
deleteitem faces id
)
node.selectedfaces = sel
)
else node.selectedfaces = node.faces


if percent is more than 50 it's faster to collect faces to deselect...

batigolnguyen
12-26-2010, 11:20 AM
Hi Videep,

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

--> firstArray.count<b: check for enough percentage of faces or not
--> firstArray.count<a: check for reaching maximum of faces

Hi denisT,

I understand what you meant, I just made it as fast as I can. I'm sure that is completely right.
But that algorithm didn't optimize, so it will become very slow for big amount of faces. Anyway, thanks for you comments, I think it's really helpful for Videep, because he's new with maxscript and programming :thumbsup:

CGTalk Moderation
12-26-2010, 11:20 AM
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.