Hi ladies and gentlemen,
I’m adding a decimate modifier to all objects in my scene and try to change the ratio-property afterwards on demand. I want all my objects to have a maximum of 1000 polygons, so my code for Blender looks as follows:
for obj in bpy.data.objects:
modDec = obj.modifiers.new("dec", type = "DECIMATE")
if (modDec.face_count > 1000):
modDec.ratio = 1000 / modDec.face_count
I actually thought this was pretty simple and straightforward but it won’t work. All my objects get the modifier but the ratio remains “1”. If I leave out the if-statement and just write something like modDec.ratio = 0.5 it works…the objects get the modifier and the ratio is actuall 0.5. I don’t get what’s wrong with my code. Can someone please help me with this?
Correlation is indirect. For example given you have 10000 polys, ratio/faces are as follows:
0.75 = 8800
0.5 = 6000
0.25 = 3750
0.1 = 1800
This might complicate your task as predicting the magic number needed to get 1000. What you can do though is run a few passes as such:
import bpy
sel=[s for s in bpy.context.selected_objects if s.type=='MESH'] #select only MESH type
for obj in sel:
modDec=None
for m in obj.modifiers: #find a DECIMATE modifier
if m.type=="DECIMATE":
modDec=m
break
if not modDec: modDec = obj.modifiers.new("Decimate", type = "DECIMATE")
bpy.context.scene.update() # need to update scene to get correct values
modDec.ratio=1 # let's set it to 1 to read face count. Otherwise would have to collapse mesh
bpy.context.scene.update() # update in order to read
face_count= modDec.face_count
print (face_count)
# rough first estimate
modDec.ratio=1000/face_count
D=modDec.ratio/10.0
# we can try run in a few passes
for p in range(0,10):
modDec.ratio-=D
bpy.context.scene.update()
if modDec.face_count<=1000: break
print ("Passes: ", p)