Notifications
Clear all

[Closed] help with random delete script

I am starting to learn max script, and to teach myself I’m trying to write some simple scripts.
I just finish writing this script to delete objects from a selection on a random basis based on a percentage given.
Initially I set it up with a fixed value and had no problems.
this is the script:
numb= selection.count
perc2=50
perc=(numb*perc2)/100– calculates the porcentage base on the amount of obj selected
– crete array of numbers from 1 to the numbers selected on a random order “arrayshuffle”
arrayshuffle=#()
shuffle=0
arrayshuffle.count
– loops until the number of elements on the array is equal to the number of objects selected
while arrayshuffle.count < numb do
(
x=random 1 numb – a random value from 1 to the number of objetcs
y=findItem arrayshuffle x –checks if the x value exist on the array… if not returns 0
if y==0 do
(
append arrayshuffle x
)
)
arrayshuffle
objarray= selection as array– creates an array of all objects selected
for i = 1 to perc do
(
x = arrayshuffle[i]
delete objarray[x]
)

Then I wanted to have the value enter on a rollout. and adde some lines. Now the script works but when you undo it crashes everytime.(the version above does not crash)
Does anybody know what’s wrong with it? or can help me figure it out?

Thanks in advance

The script with the rollout:
rollout delete_random “Delete Random”
(
edittext percentage “%”
button okay “ok”
on okay pressed do
(
–number of selected objects
numb= selection.count
perc2=percentage.text as number– coverts the value enter to numbers
perc=(numb*perc2)/100– calculates the porcentage base on the amount of obj selected
print perc
– crete array of numbers from 1 to the numbers selected on a random order “arrayshuffle”
arrayshuffle=#()
shuffle=0
– loops until the number of elements on the array is equal to the number of objects selected
while arrayshuffle.count < numb do
(
x=random 1 numb – a random value from 1 to the number of objetcs
y=findItem arrayshuffle x –checks if the x value exist on the array… if not returns 0
if y==0 do
(
append arrayshuffle x
)
)
objarray= selection as array– creates an array of all objects selected
for i = 1 to perc do
(
x = arrayshuffle[i]– gives x the value of one of the numbers on the random array
delete objarray[x]– deletes a random unique object from the selection array
)
)
)
createdialog delete_random

2 Replies

Hi there, for me it seems ur script a little overcomplicated so i created new one which does the same ->

rollout deleteRandom "Dr.Random"
(
button delR "delete"  height:18  width:60 pos:[5,10]
spinner perc "%" range:[1,100,50] type:#integer fieldwidth:35 pos:[70,10]

	on delR pressed do if selection.count > 1 do
	(
		local selA	  = selection as array,
			  numObj  = selA.count,
			  toDel   = ceil (numObj*perc.value/100),
			  deleted = 0
		with undo "delRandom" on 
		(  
			while (deleted < toDel) do
			(
			numb = random 1 numObj
			if  isDeleted selA[numb] != true do (
				delete selA[numb]
				deleted +=1)
			)
		)
	)
)createDialog deleteRandom 130 40

sorry for my poor english:)
hope that helps.

Pats. That’s great!! it works perfect… and its so much simpler… I did not know about the isdeleted function, that simplifies things a lot.

Thanks a lot!!