Notifications
Clear all

[Closed] Random Objects

Hi All …
is it possible to create a function that create random number of objects (Box, Sphere,…), like if we put it in an array … what order well choose a random object or more than one object and created it in the viewport …
Thank u all in advance
best regards

4 Replies

not entirely sure what you’re after, but say you have 3 objects in your scene that you want to randomly pick from… select those objects, and…


-- get the selected objects into an array
objArray = getCurrentSelection()

-- now create a loop to create, say, 100 random objects..
-- seed the random number generator first
seed 0
for i = 1 to 100 do (
  -- pick a random number between 1 and the number of objects in our array
  randomIndex = random 1 objArray.count

  -- and use that to pick an object from that array
  srcObj = objArray[randomIndex]

  -- now create an instance of that object...
  targObj = instance srcObj

  -- and move it a little
  targObj.pos = [10*i,0,0]
)
-- done?

thanks ZeBoxx2 … it’s working great … but what i was thinking is like a button every time u press it will give a different object from the objects in the array or more than one object if we want … is it possible ?

hope it’s clear …

thanks

sure… you end up with something like this ( except less ugly – it’s just test code )


rollout test "test" width:200 (
	button btn_get "get selected objects"
	spinner spn_numobjs "number of objects" range:[0,100,0] type:#integer enabled:false
	button btn_create "create" enabled:false

	local objArray

	on btn_get pressed do (
		objArray = getCurrentSelection()
		if (objArray.count > 0) then (
			btn_get.text = objArray.count as string + " objects stored"
			spn_numobjs.range = [1,objArray.count,amax spn_numobjs.value 1]
			spn_numobjs.enabled = true
			btn_create.enabled = true
		)
		else (
			btn_get.text = "get selected objects"
			spn_numobjs.range = [0,100,0]
			spn_numobjs.enabled = false
			btn_create.enabled = false
		)
	)

	on btn_create pressed do (
		for i = 1 to spn_numobjs.value do (
			local randomIndex = random 1 objArray.count
			local srcObj = objArray[randomIndex]
			if (isValidNode srcObj) then (
				local targObj = instance srcObj
				targObj.pos = [(i-1)*20,0,0]
			)
			else (
				format "Warning - object index % is not valid; %
" randomIndex srcObj
			)
		)
	)
)
createDialog test

amazing man thx a lot … i’ll modify it little and i’ll show it to u …
thx a lot again …