help: apply modiifer options with python


(stargeizer) #1

Hi people:

I’m having a little problem with cleaning some meshes with python. I’m trying to:

  • remove doubles
  • clean the mesh from degenerate geometry
  • delete any custom mesh normals imported from another software
  • convert quads to tris
  • decimate the object using the planar option with angle of 2º

So far, so good… The problem is when i try to setup the modifier options. It doesn’t work

The script:


import bpy

if bpy.context.selected_objects != []:
    for obj in bpy.context.selected_objects:
        if obj.type == 'MESH':
            print(obj.name)
            bpy.context.scene.objects.active = obj
            bpy.ops.object.editmode_toggle()
            bpy.ops.mesh.select_all(action='SELECT')
            bpy.ops.mesh.remove_doubles()
            bpy.ops.mesh.dissolve_degenerate()
            bpy.ops.mesh.customdata_custom_splitnormals_clear()
            bpy.ops.mesh.normals_make_consistent(inside=False)
            bpy.ops.mesh.tris_convert_to_quads()
            bpy.ops.object.editmode_toggle()
            bpy.ops.object.modifier_add(type='DECIMATE')
            bpy.data.objects[obj.name].modifiers["Decimate"].decimate_type["Planar"]
            bpy.data.objects[obj.name].modifiers["Decimate"].angle_limit[2]
            bpy.ops.object.modifier_apply(type='DECIMATE')

Blender throws me an error at the line 17 (bpy.data.objects[obj.name]…) saying: “TypeError: string indices must be integers”. I know the code ask for a string or a number, obj.name has the name of the mesh, but i’m somewhat confused by the error.

What i’m doing wrong??.. And thanks for the answers.


(Darcvizer) #2

17 line replace
bpy.context.object.modifiers[“Decimate”].decimate_type = ‘DISSOLVE’

import bpy



sel = bpy.context.selected_objects
for i in sel:
    bpy.ops.object.mode_set(mode='EDIT')
    bpy.ops.mesh.select_all(action='SELECT')
    bpy.ops.mesh.remove_doubles()
    bpy.ops.mesh.select_all(action='SELECT')
    bpy.ops.mesh.dissolve_degenerate()
    bpy.ops.mesh.select_all(action='SELECT')
    bpy.ops.mesh.customdata_custom_splitnormals_clear()
    bpy.ops.mesh.select_all(action='SELECT')
    bpy.ops.mesh.normals_make_consistent(inside=False)
    bpy.ops.mesh.select_all(action='SELECT')
    bpy.ops.mesh.tris_convert_to_quads()
    bpy.ops.mesh.select_all(action='DESELECT')
    bpy.ops.object.mode_set(mode='OBJECT')
    bpy.context.scene.objects.active = i
    bpy.ops.object.modifier_add(type='DECIMATE')
    bpy.context.object.modifiers["Decimate"].decimate_type = 'DISSOLVE'
    bpy.context.object.modifiers["Decimate"].angle_limit = 0.034907
    bpy.ops.object.convert(target='MESH')



(stargeizer) #3

Thanks… It works nicely.