Random object selection

Hey there!

I need a piece of code that would execute random object selection among all objects belonging to a particular layer. And it's supposed to be one object. Also I need a snippet for children selection.

Thanks in advance.

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Artie's picture

miauu, you've made my day!

miauu, you've made my day!
I really appreciate your effort.

Artie's picture

Thank you very much, miauu!

That's fabulous! Thank you very much, miauu!
Would be even geater if you wrote how to do the following:

1) Say, I have 5 layers each containing different objects.
2) I've selected all or a bunch of objects from each layer.
3) I want to select 5 random objects out of the current selection (one random object per each layer)

Thanks again!

miauu's picture

.

(
	selObjsArr = selection as array
	if selObjsArr.count != 0 then
	(		
		layersArr = #()
		for o in selObjsArr do
		(
			selObjLayer = o.layer
			appendIfUnique layersArr selObjLayer
		)
		objToSelArr = #()
		for layer in layersArr do
		(
			layer.nodes &theNodes
			append objToSelArr (theNodes[random 1 theNodes.count])
		)
		select objToSelArr
	)
	else
		messagebox "Select at least one object" title:"Empty Selection"
)
miauu's picture

.

The code below works with objects. How to use it:
1- select one object that belongs to the desired layer
2- run the script - random object from that layer will be selected.
You can run the script as many times as you want and it will select random object from the same layer
If you don't know which object to which layer belong, then in layer manager select all objects from the desired layer.

1- select several(or all) objects from desired layer
2- run the script - random objects from the selected will be selected
This works with objects that belongs to different layers, so you can select onbjects from different layers and the script will wors only with the selected objects.

(
	if selection.count != 0 then
	(
		if selection.count == 1 then
		(
			--	"select all objects from the layer to which the selected object belong"
			selObjLayer = (selection[1]).layer
			selObjLayer.nodes &theNodes
			select theNodes[random 1 theNodes.count]
		)
		else
		(
			selObjsArr = selection as array
			select selObjsArr[random 1 selObjsArr.count]
		)
	)
	else
		messagebox "Select at least one object" title:"Empty Selection"
)

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.