Skip to content

Commit

Permalink
feat: add hard-apply mode to apply_modifiers utility, and exclude reg…
Browse files Browse the repository at this point in the history
…ular multi segment edge bevels from normal usage
  • Loading branch information
tristan-hm committed Jun 2, 2022
1 parent 00be593 commit a5ee288
Showing 1 changed file with 28 additions and 11 deletions.
39 changes: 28 additions & 11 deletions utils/apply_modifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'}


Expand All @@ -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():
Expand Down

0 comments on commit a5ee288

Please sign in to comment.