diff --git a/__init__.py b/__init__.py index 86785bf..f8654d7 100644 --- a/__init__.py +++ b/__init__.py @@ -81,11 +81,21 @@ def copy_attributes(a, b): ###################################################### def apply_modifier(target_object=None, target_modifiers=None): + """ + Applies the given list of modifiers (or all if target_modifiers is None) to the given object. + If no object is supplied, the currently active object is assumed. + This function expects Object Mode, and changes the selection and active object. + """ if target_object is None: obj_src = bpy.context.window.view_layer.objects.active else: obj_src = target_object + # Without this guard, direct invocation of apply_modifier can cause an odd situation. + # Even if the only selected object is the target, it causes a change in behaviour. + # An extra unwanted shape key, named after the object, is created. + bpy.ops.object.select_all(action='DESELECT') + if target_modifiers is None: target_modifiers = [] for x in obj_src.modifiers: @@ -248,25 +258,26 @@ def poll(cls, context): return obj and obj.type == 'MESH' def execute(self, context): - obj = context.window.view_layer.objects.active - + obj = context.object + if self.modifier_names and len(self.modifier_names) > 0: bpy.ops.object.select_all(action='DESELECT') str_targets = [] - for i in range(len(self.modifier_names)): + usable_slots = min(len(self.modifier_names), 32) + for i in range(usable_slots): if self.flags[i] and obj.modifiers[self.modifier_names[i]]: str_targets.append(self.modifier_names[i]) apply_modifier(target_object=obj, target_modifiers=str_targets) obj.select_set(True) - else: - self.modifier_names = tuple(i.name for i in obj.modifiers) - self.flags = tuple(False for i in range(32)) return {'FINISHED'} def invoke(self, context, event): wm = context.window_manager + obj = context.object + self.modifier_names = tuple(i.name for i in obj.modifiers) + self.flags = tuple(False for i in range(32)) return wm.invoke_props_dialog(self) def draw(self, context): @@ -275,7 +286,8 @@ def draw(self, context): layout = self.layout col = layout.column() - for i in range(len(self.modifier_names)): + usable_slots = min(len(self.modifier_names), 32) + for i in range(usable_slots): col.prop(self, "flags", text=self.modifier_names[i], index=i)