From a5ee2886f588f9f820f9b808edbffae2e4e5ba53 Mon Sep 17 00:00:00 2001 From: Tristan Strathearn Date: Thu, 2 Jun 2022 10:21:02 +1000 Subject: [PATCH] feat: add hard-apply mode to apply_modifiers utility, and exclude regular multi segment edge bevels from normal usage --- utils/apply_modifiers.py | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/utils/apply_modifiers.py b/utils/apply_modifiers.py index 484d722..b6a8206 100644 --- a/utils/apply_modifiers.py +++ b/utils/apply_modifiers.py @@ -25,7 +25,8 @@ class ND_OT_apply_modifiers(bpy.types.Operator): bl_idname = "nd.apply_modifiers" bl_label = "Apply Modifiers" - bl_description = "Prepare the selected object(s) for destructive operations by applying applicable modifiers" + bl_description = """Prepare the selected object(s) for destructive operations by applying applicable modifiers +SHIFT — Hard apply (apply all modifiers)""" bl_options = {'UNDO'} @@ -41,26 +42,42 @@ def execute(self, context): return {'FINISHED'} + + def invoke(self, context, event): + self.hard_apply = event.shift + + return self.execute(context) + def collapse_modifiers(self, obj): safe_mod_types = ['WEIGHTED_NORMAL', 'TRIANGULATE', 'NODES'] + + mods = [mod for mod in obj.modifiers] + mods_to_remove = [] + + if self.hard_apply: + mods_to_remove = [mod.name for mod in mods] + + if not self.hard_apply: + for mod in mods: + if mod.type in safe_mod_types: + continue - mods = [(mod.name, mod.type, mod) for mod in obj.modifiers] - for name, type, mod in mods: - if type in safe_mod_types: - continue + if "— ND WNB" in mod.name: + continue - if "— ND WNB" in name: - continue + if mod.type == 'BEVEL' and mod.affect == 'EDGES' and mod.limit_method == 'ANGLE': + if mod.segments > 1 or (mod.segments == 1 and mod.harden_normals): + continue - if type == 'BEVEL' and mod.segments == 1 and mod.harden_normals: - continue + mods_to_remove.append(mod.name) + for mod_name in mods_to_remove: try: - bpy.ops.object.modifier_apply({'object': obj}, modifier=name) + bpy.ops.object.modifier_apply({'object': obj}, modifier=mod_name) except: # If the modifier is disabled, just remove it. - bpy.ops.object.modifier_remove({'object': obj}, modifier=name) + bpy.ops.object.modifier_remove({'object': obj}, modifier=mod_name) def register():