I am trying to create an automated LOD process within Blender, that takes a mesh, creates a group using the mesh's name, renames the mesh to '_LOD0', adds a Decimation modifier and then creates 3 more meshes with progressively higher Decimation modifiers and LOD numbers:
This seems to work fine for one object selected, but if I have multiple objects selected it puts all of the objects in the same group, and makes more objects than I want, and also doesn't add the decimate modifiers correctly:
I know that bpy.ops isn't the most optimized way to do this, but that's not a huge concern for me, I just need it to work. If anyone can point me in the right direction on how to achieve these goals it would be much appreciated, thanks!
import bpy
if bpy.context.selected_objects != []:
for obj in bpy.context.selected_objects:
bpy.context.scene.objects.active = obj
bpy.context.object.show_wire = True
bpy.context.object.show_all_edges = True
bpy.ops.group.create(name=bpy.context.object.name)
bpy.ops.object.modifier_add(type='DECIMATE')
bpy.context.object.modifiers["Decimate"].use_collapse_triangulate = True
bpy.context.scene.rno_bool_keepIndex = False
bpy.context.scene.rno_str_subfix = "_LOD0"
bpy.ops.object.rno_add_subfix_prefix()
bpy.ops.object.duplicate_move(OBJECT_OT_duplicate={"linked":False, "mode":'TRANSLATION'}, TRANSFORM_OT_translate={"value":(0, 0.1, 0), "constraint_axis":(False, True, False), "constraint_orientation":'GLOBAL', "mirror":False, "proportional":'DISABLED', "proportional_edit_falloff":'SMOOTH', "proportional_size":1, "snap":False, "snap_target":'CLOSEST', "snap_point":(0, 0, 0), "snap_align":False, "snap_normal":(0, 0, 0), "gpencil_strokes":False, "texture_space":False, "remove_on_cancel":False, "release_confirm":False, "use_accurate":False})
bpy.context.object.modifiers["Decimate"].ratio = 0.5
bpy.ops.mesh.customdata_custom_splitnormals_clear()
bpy.context.object.data.auto_smooth_angle = 0.785398
bpy.context.scene.rno_str_old_string = "LOD0.001"
bpy.context.scene.rno_str_new_string = "LOD1"
bpy.ops.object.rno_replace_in_name()
bpy.ops.object.duplicate_move(OBJECT_OT_duplicate={"linked":False, "mode":'TRANSLATION'}, TRANSFORM_OT_translate={"value":(0, 0.1, 0), "constraint_axis":(False, True, False), "constraint_orientation":'GLOBAL', "mirror":False, "proportional":'DISABLED', "proportional_edit_falloff":'SMOOTH', "proportional_size":1, "snap":False, "snap_target":'CLOSEST', "snap_point":(0, 0, 0), "snap_align":False, "snap_normal":(0, 0, 0), "gpencil_strokes":False, "texture_space":False, "remove_on_cancel":False, "release_confirm":False, "use_accurate":False})
bpy.context.object.modifiers["Decimate"].ratio = 0.25
bpy.context.object.data.auto_smooth_angle = 1.53589
bpy.context.scene.rno_str_old_string = "LOD1.001"
bpy.context.scene.rno_str_new_string = "LOD2"
bpy.ops.object.rno_replace_in_name()
bpy.ops.object.duplicate_move(OBJECT_OT_duplicate={"linked":False, "mode":'TRANSLATION'}, TRANSFORM_OT_translate={"value":(0, 0.1, 0), "constraint_axis":(False, True, False), "constraint_orientation":'GLOBAL', "mirror":False, "proportional":'DISABLED', "proportional_edit_falloff":'SMOOTH', "proportional_size":1, "snap":False, "snap_target":'CLOSEST', "snap_point":(0, 0, 0), "snap_align":False, "snap_normal":(0, 0, 0), "gpencil_strokes":False, "texture_space":False, "remove_on_cancel":False, "release_confirm":False, "use_accurate":False})
bpy.context.object.modifiers["Decimate"].ratio = 0.1
bpy.context.object.data.auto_smooth_angle = 2.0944
bpy.context.scene.rno_str_old_string = "LOD2.001"
bpy.context.scene.rno_str_new_string = "LOD3"
bpy.ops.object.rno_replace_in_name()
(It may not matter for what I'm asking, but I'm referencing a batch renaming addon in my code called 'vtools_renameObjects' - dropbox)


obj.show_wire = Truebut you have elected to usebpy.context.object.show_wire = TrueIMO I would ditch the code and use API methods for 90% of it. Including the rename addon. The quick fix (given you say it works for single object, and you have come this far... we do get attached to cpde) would be copy the selected objectsobs = context.selected_objects, deselect all then in loop select and set active on a per object basis at loop start, deselect at loop end. – batFINGER Feb 16 '19 at 09:35bpy.opsis un-optimised it's thatbpy.opsis intended to be run from the interface, not from scripts. This may end up being more complex that modifying the underlying data as batFinger suggested. See: https://blender.stackexchange.com/questions/2848/why-avoid-bpy-ops – Ray Mairlot Feb 16 '19 at 16:08