I would like to add a triangulate modifier to all of my objects on top of all my other modifiers. This is different from Ctrl+L link modifiers because this overwrites modifier data. Either an option I'm missing or a bit of python scripting would be super helpful. Thank you!
Adding a modifier to the top of the stack of multiple objects without overwriting previous modifiers
1 Answer
$\begingroup$
$\endgroup$
Heres one, this works on all selected mesh objects:
import bpy
class AddTri(bpy.types.Operator):
"""Add triangulate modifier on top of stack"""
bl_idname = "object.add_tri"
bl_label = "Add Triangulate Modifier"
@classmethod
def poll(cls, context):
return context.active_object is not None
def execute(self, context):
act_obj = context.active_object #record active object
for obj in context.selected_objects:
if obj.type == 'MESH':
obj.select_set(True)
context.view_layer.objects.active = obj # make object active for bpy.ops to work on specific object
mod = obj.modifiers
md = mod.new("Triangulate", "TRIANGULATE")
# quad_method: 'BEAUTY' 'FIXED' 'FIXED_ALTERNATE' 'SHORTEST_DIAGONAL'
md.quad_method = 'SHORTEST_DIAGONAL'
# ngon_method: 'BEAUTY' 'CLIP'
md.ngon_method = 'BEAUTY'
md.min_vertices = 4
md.keep_custom_normals = False
# modifier UI settings
md.show_expanded = False
md.show_in_editmode = True
md.show_on_cage = True
bpy.ops.object.modifier_move_to_index(modifier=md.name, index=0) # bring modifier on top of stack
context.view_layer.objects.active = act_obj # make old active object active again
return {'FINISHED'}
def register():
bpy.utils.register_class(AddTri)
def unregister():
bpy.utils.unregister_class(AddTri)
if __name__ == "__main__":
register()
# test call
bpy.ops.object.add_tri()