To apply an object modifier with the Blender Python API we can use the “bpy.ops.object.modifier_apply” operator. However, it processes only the active object.
If we need to apply a modifier, for example – Subdivision Surface, to several selected objects, we need to make each of them active, and then call the “modifier_apply” operator.
Let’s loop through all the selected objects, make each of them active, and if the Subdivision Surface modifier is in the list of its modifiers, apply it.
1 2 3 4 5 6 7 8 9 |
import bpy for obj in bpy.context.selected_objects: bpy.context.view_layer.objects.active = obj for modifier in obj.modifiers: if modifier.type == 'SUBSURF': bpy.ops.object.modifier_apply( modifier=modifier.name ) |
If we need to apply a modifier of another type – change the type of the modifier in the condition:
1 |
if modifier.type == 'SUBSURF': |
We can get the type of the active modifier on the currently active object by executing the following code:
1 2 3 |
import bpy print(bpy.context.object.modifiers.active.type) |