Notifications
Clear all

[Closed] Nested loops

Hey,

I’ve created this script to assign random colors to a set of selected objects. The problem is that right now I can assign random colors (diffuse color of the Standard material) to a maximum of 72 objects (which is =24 X 3 since the marerial editor has a maximum of 24 slots and I have repeated the for loop for three times).

I need help with the code so that I can create a nested for loop so that it is not on a no. of objects count dependatnt.

Thanks,

Vikram

My code is as follows :


rollout ranMat "Random Color" width:162 height:300

(

spinner spn1 "R:" pos:[12,28] width:44 height:16 type:#integer range:[0,255,0]

spinner spn2 "G:" pos:[64,28] width:44 height:16 type:#integer range:[0,255,0]

spinner spn3 "B:" pos:[120,28] width:44 height:16 type:#integer range:[0,255,0]

spinner spn4 "R:" pos:[12,78] width:44 height:16 type:#integer range:[0,255,255]

spinner spn5 "G:" pos:[64,78] width:44 height:16 type:#integer range:[0,255,255]

spinner spn6 "B:" pos:[120,78] width:44 height:16 type:#integer range:[0,255,255]

button btn1 "Randomise Material Color" pos:[15,105] width:140 height:24

label lbl1 "From :" pos:[12,10] width:39 height:14

label lbl2 "To :" pos:[13,57] width:34 height:17

label lb3 "Works only on selected objects" pos:[8,135]



on btn1 pressed do

(

for j = 1 to 24 do

(

meditMaterials[j] = Standardmaterial ()

meditMaterials[j].name = "Unused" + (j as string) 

)



--messagebox (spn1.value as string)

if

(

spn1.value > spn4.value OR

spn2.value > spn5.value OR

spn3.value > spn6.value 

)

then

(

messagebox "The From value has to be lesser than the To value"

)

(

obc = $.count

aaa = $ as array



try

(

for i=1 to obc do

(

meditMaterials[i].diffuse = random (color (spn1.value) (spn2.value) (spn3.value)) (color (spn4.value) (spn5.value) (spn6.value))

aaa[i].material = meditMaterials[i]

meditMaterials[i].name = "Random" + (i as string)

if i == 24 then

(

--i=1

for j = 1 to 24 do

(



k = j + i

meditMaterials[j] = Standardmaterial ()

meditMaterials[j].diffuse = random (color (spn1.value) (spn2.value) (spn3.value)) (color (spn4.value) (spn5.value) (spn6.value))

aaa[k].material = meditMaterials[j]

meditMaterials[j].name = "Random" + (k as string)

if i == 24 then

(

for l = 1 to 24 do

(

m = l + k

meditMaterials[l] = Standardmaterial ()

meditMaterials[l].diffuse = random (color (spn1.value) (spn2.value) (spn3.value)) (color (spn4.value) (spn5.value) (spn6.value))

aaa[m].material = meditMaterials[l]

meditMaterials[l].name = "Random" + (m as string)

)

) 

)

)

)

) 

catch()

)



)



) 

createDialog ranMat width:170 height:155


2 Replies

why you need to use the medit materials slots? can´t you randimize the materials directly into objects? something like:

for i = 1 to selection.count do
(
mat = standardmaterial()
mat.diffuse = random (your spinner values)
selection[i].material = mat
)

sorry if i missed something on your question and sorry my bad english…
Gustavo

I can swear i had tried that before. But it worked now, Thanks a lot for the help. Really.