2
$\begingroup$

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 actual 0.5. I don't get what's wrong with my code. Can someone please help me with this?

[EDIT] I forgot to mention that I work with Blender 2.78b. Sorry for that.

$\endgroup$

1 Answer 1

1
$\begingroup$

You have two problems.
First you are looping through all the objects, not just the mesh objects. So for example when the loop gets to a camera it can not add the decimate modifier, and the script brakes.

Second issue with the data not being available (modDec.face_count). This is yet another scene update issue.

Here is the changed code.

import bpy

for obj in bpy.data.objects:
    if obj.type == "MESH":
        modDec = obj.modifiers.new("Decimate", type = "DECIMATE")
        bpy.context.scene.update()

        while modDec.face_count >= 1000:
            modDec.ratio *= 0.5
            bpy.context.scene.update()

Notice the if obj.type == "MESH": simple if statement that is only working on the mesh objects.

The real change is the two bpy.context.scene.update() lines. You need two because of the while statement.
Last thing I changed is your if(modDec.face_count > 1000): line. You need to keep changing the ratio while the face_count is bigger then 1000.

The way you were setting the ratio it did not actually get the face count down below 1000.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .