Apply Modifier Operator Not Working

Hey, i’m having trouble using the apply_modifer operator, and after some Google searches and experimentation I wasn’t able to find any answers, so I’m posting here!

In the Array Sketch add-on i’m working on, I have a class that converts the Sketch Object to a Mesh, and in order to fit into peoples workflows better, whatever they may be, I only want to apply the modifiers that are used for the Sketch Object. I do use apply_modifier in other areas of the add-on though, and something wrong i’m doing here is preventing me from making more progress in other areas.

Using bpy.ops.object.convert(target=‘MESH’, keep_original=False) works, but it applies every modifier, which is not the solution i’m after.

I’ve posted the whole class, and the def FocusObject(), just in case i’ve missed something crucial:

# Convert strokes to meshes.
class ASKETCH_StrokesToMeshes(bpy.types.Operator):
    """Converts the stroke object to a mesh"""
    bl_idname = "object.asketch_strokes_to_meshes"
    bl_label = "Array Sketch Stroke To Meshes"
    
    @classmethod
    def poll(cls, context):
        for obj in context.selected_objects:
            return obj.name.find(".SKO") != -1 or obj.name.find(".SKO") != -1
    
    
    def execute(self, context):
        # Get selected objects
        strokes_to_convert = []
        print("Selected Objects:")
        print(len(bpy.context.selected_objects))
        
        for stroke in bpy.context.selected_objects:
            if stroke.name.find(".SKO") != -1:
                strokes_to_convert.append(stroke)
        
        print("Objects in Convert Queue:")
        print(len(strokes_to_convert))
        
        for stroke in strokes_to_convert:
            print("Converting Stroke:")
            print(stroke.name)
            
            #Just select the curve now
            FocusObject(stroke.name)
            
            #Now apply all it's modifiers
            print(len(bpy.data.objects[stroke.name].modifiers))
            
            for mod in bpy.data.objects[stroke.name].modifiers:
                print(mod.name)
                print(mod.type)
                
                if mod.type == "ARRAY":
                    bpy.ops.object.modifier_apply(apply_as="DATA", modifier="ARRAY")
                elif mod.type == "CURVE":
                    bpy.ops.object.modifier_apply(apply_as="DATA", modifier='CURVE')
                elif mod.type == "MIRROR":
                    bpy.ops.object.modifier_apply(apply_as="DATA", modifier='MIRROR')
                    #stroke.modifiers.remove(modifier)
                    
                print(len(bpy.data.objects[stroke.name].modifiers))

            # Now find and delete the corresponding curve
            stroke_curve_name = stroke.name.replace(".SKO", ".SKC")
            FocusObject(stroke_curve_name)
            bpy.ops.object.delete()
            
            # Rename the curve to remove it from being considered an Object Sketch
            FocusObject(stroke.name)
            stroke.name = "ASKETCH Object"
            
        
        
        return {"FINISHED"}


def FocusObject(targetName):
    
    #### Select and make target active
    bpy.ops.object.select_all(action='DESELECT')  
    bpy.context.scene.objects.active = bpy.data.objects[targetName]
    bpy.ops.object.select_pattern(pattern=targetName) 

When running the code and adding more log printing than is present here, the modifier types can be detected and the code is clearly ‘passed’, but nothing happens, and the array count remains the same. Any help? :C

If nothing helps, you could try this:

import bpy

scene = bpy.context.scene
ob = bpy.context.object

mod_types = {'ARRAY', 'CURVE', 'MIRROR'}
mod_active = [mod.show_viewport for mod in ob.modifiers]

for mod in ob.modifiers:
    if mod.type not in mod_types:
        mod.show_viewport = False
        
me = ob.to_mesh(scene, True, 'PREVIEW')

for mod, active in zip(ob.modifiers, mod_active):
    if mod.type in mod_types:
        ob.modifiers.remove(mod)
    else:
        mod.show_viewport = active
        
# Note: this only swaps the object's data, but doesn't remove the original mesh
ob.data = me

It creates a new mesh datablock and should only apply the defined modifier types.

That worked perfectly, thank you so much! :smiley:

Just keep in mind that there will be an additional mesh datablock each time you run it like this.

You may want to remove the original mesh