I'm attempting to write my first add on. It cycles through all the objects in the scene and applies all the modifiers each object has.

I've set up the panels correctly so when I press a button it calls the following method:

def applyAllModifiers():
    context = bpy.context
    scene = context.scene
    settings = 'PREVIEW'
    apply_modifiers = True

    for ob in bpy.context.selected_objects:
        print ("Applying modifiers on object " + ob.name)
        bpy.context.scene.objects.active = bpy.data.objects[ob.name]
        numModifiers = len(ob.modifiers)
        if numModifiers != 0:

            # Get all the info we need from the object
            objName = ob.name
            objLocation = ob.location
            objRotation = ob.rotation_euler
            objScale = ob.scale
            mesh = ob.to_mesh(scene, apply_modifiers, settings)

            # Delete the current object

            # Create a new object with all modifers applied
            new_object = bpy.data.objects.new(objName, mesh)
            new_object.location = objLocation
            new_object.rotation_euler = objRotation
            new_object.scale = objScale
            new_object.select = False
            print ("   applied {} modifiers".format(numModifiers))

            ob.select = False
            print ("   object has no modifiers")


It works if I have a single object selected, but if I have multiple objects selected it will randomly move some objects to (0,0,0). I can't seem to get any consistent behavior when I have more than 1 object selected. I'm not really sure this is even the best approach to what I'm trying to do.

Any help would be appreciated. Thanks!


1 Answer 1


based on this answer https://blender.stackexchange.com/a/7197

import bpy

context = bpy.context

for ob in bpy.context.selected_objects:
    n = len(ob.modifiers)
    if (n) and (ob.type not in ('LAMP','CAMERA','ARMATURE','EMPTY')):
        print ("Applying " + str(n) +  " modifiers on object " + ob.name)
        oldmesh = ob.data
        ob.data  = ob.to_mesh(context.scene,True, 'PREVIEW')

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .