ScriptSpot

Forum topic · General Scripting

loop through objects

By CGough · 2011-10-07

Description

Hi.

I no doubt know this is a easy fix but and help would be most apreciated.

I can apply and randomise a UVW map easily enough but I'm having trouble counting the objects selected and looping through the script for each object.

The general idea is to say randomise a stone texture over several tiles.

My poor ass script is as follows :

arr = $.count
mapSize = 500

for i = 1 to arr do
(
modPanel.addModToSelection (Uvwmap ()) ui:off
$.modifiers[#UVWMapping].maptype = 4
$.modifiers[#UVWMapping].length = mapSize
$.modifiers[#UVWMapping].width = mapSize
$.modifiers[#UVWMapping].height = mapSize
$.modifiers[#UVWMapping].gizmo.rotation = (quat (random -20 20) (random -20 20) (random -20 20) (random -20 20))

maxOps.CollapseNode $ off

)

Any Help would be most apreciated.

Comments (1)

2 loop manners exist

Anubis · 2011-10-07

You can loop through in any collection in 2 ways - by-item and by-index. If you use by-index for selection w'd looks like:

for i=1 to selection.count do ( selection[i]... )

But if you like to use

modPanel.addModToSelection

then you'd need to select only 1 object at a time. So in this case more appropriate w'd be to loop by-item.

mapSize = 500
theObjs = selection as array
clearSelection()
max modify mode

for obj in theObjs do
(
	select obj
	uvwMod = Uvwmap maptype:4
	uvwMod.length = uvwMod.width = uvwMod.height = mapSize
	angVals = for i=1 to 4 collect (random -20 20)
	rotVal = (quat angVals[1] angVals[2] angVals[3] angVals[4])
	uvwMod.gizmo.rotation = rotVal
	modPanel.addModToSelection uvwMod ui:off
	maxOps.CollapseNode obj off
)

hope this help