Notifications
Clear all

[Closed] whats wrong with this script?

wondering if anyone can spot the funny business here.

I’m trying to make a script which automaticly adds modifiers to a bunch of selected shapes.
the reason why is because i dont want them to be instanced, and i dont want the noise modifiers to be consitent across all objects.

the script will eventually create a stacked stone wall.

in my script i want to remove any other modifiers that already exist (defined by me, not just everything), which will then be added again. the script is designed to be applied
to a bunch of shapes

in my script, it runs fine the first time on a plain shape. but when you run it on a shape with all the modifiers on it, it doesn’t properly remove the subdivide modifier, and therefor can’t create the extrude modifier on top because you can’t apply an extrude to a mesh.

for obj in selection do
(
for i = 1 to obj.modifiers.count do
(
–delete modifiers first
if (classof obj.modifiers[i] == Noisemodifier or
classof obj.modifiers[i] == Subdivide or
classof obj.modifiers[i] == Extrude)
do
(
print “del”
deleteModifier obj i
)–end if

)--end i loop

–/*
addModifier obj (extrude())
obj.modifiers[#Extrude].amount= random 25 75

addModifier obj (subdivide ())
obj.modifiers[#Subdivide].size = 15


addModifier obj (Noisemodifier()) -- not instance
obj.modifiers[#Noise].strength= [0,0,5]
obj.modifiers[#Noise].fractal = on
obj.modifiers[#Noise].scale = 50

)

thanks for any help!

2 Replies
 JHN

Hi acgraham,
For better readability post your code in a code container (button # in the edit window) it reads so much better.

For your problem. You are deleting modifiers from top of stack to bottom by modifier count. If you delete a modifier so will the modifier count be changed since they are linked… so you need to reverse that process deleting from the bottom going up. Cause the top one is always nr. 1… deleting the topmost makes the next one number one…


objs = selection as array
for o in objs do
(
	mods = o.modifiers
	for m in mods.count to 1 by -1 do --delete modifiers bottom to top
	(
		if (isKindOf mods[m] Noisemodifier) or (isKindOf mods[m] subdivide) or (isKindOf mods[m] Extrude) then
		(
			--format "deleting % | modifier %
" o.name mods[m]
			deleteModifier o mods[m]
		)--end if
	)--end for loop

	ex = Extrude()
	addModifier o ex
	ex.amount= random 25 75

	sub = subdivide ()
	addModifier o sub
	sub.size = 15

	n = Noisemodifier()
	addModifier o n
	n.strength= [0,0,5]
	n.fractal = on
	n.scale = 50

)

Should work.

-Johan

ahh right. thats why. cheers for that!