7

Lets assume you have 1 object with 4 modifiers added to it. I am curious how one would approach the task to apply all or to delete all modifiers of a just selected object that has many modifiers already added.

The console reads out the modifier delete as

bpy.ops.object.modifier_remove(modifier="Box Right ")

So "Box Right " I might have to replace with something like active.object.modifier.name and then work each modifier off?

This would be very helpful when you want to generate a static mesh for animation or 3D printing.

Claas Kuhnen
  • 1,727
  • 4
  • 18
  • 40
  • related, very: http://blender.stackexchange.com/questions/7196/how-to-get-a-new-mesh-with-modifiers-applied-using-blender-python-api?rq=1 – zeffii Jan 07 '16 at 23:37
  • Ok let me try to figure this own. thank you! – Claas Kuhnen Jan 07 '16 at 23:45
  • 1
    A short way to apply and remove all modifiers on the active objects is bpy.ops.object.convert(target='MESH') this is the operator called when using Alt-C -> Mesh from curve – sambler Jan 08 '16 at 15:06

1 Answers1

16

Modifier removal

To remove a modifier from an object.

obj below is a reference to an object either obtained via

obj = bpy.context.active_object
obj = bpy.context.object
# or
obj = bpy.data.objects[name]

If you want to remove a modifier it is done by name, not by type. You could have multiple array modifiers, but only want to remove a specific one.

modifier_to_remove = obj.modifiers.get("Bevel")
obj.modifiers.remove(modifier_to_remove)

# or as a one-liner
obj.modifiers.remove(obj.modifiers.get("Bevel"))

# or remove all modifiers from an object
obj.modifiers.clear()

Modifier Apply

Using an operator

As sambler pointed out

# operates on all selected objects (not just the scene's _active_object_)
bpy.ops.object.convert(target='MESH')

lower level

If you want to apply all modifiers it's covered here: How to get a new mesh with modifiers applied using Blender Python API? - This involves using obj.to_mesh and there are a few things to consider, it:

  • generates a new mesh from your current obj (leaves current obj.data intact)
  • does not remove the modifiers automatically from the object. (unlike the Apply button on the Modifiers panel, or Convert to Mesh).

This snippet will:

"""
- make a reference to the current obj.data ( i call it old_mesh)
- make a new Mesh ( new_mesh) using the effect of the combined modifiers on `obj`
- remove all modifiers from `obj`
- assign the new Mesh to obj.data 
- remove old_mesh from the blend
"""

in code:

import bpy

context = bpy.context
scene = context.scene
obj = context.object

# get a reference to the current obj.data
old_mesh = obj.data

# settings for to_mesh
apply_modifiers = True
settings = 'PREVIEW'
new_mesh = obj.to_mesh(scene, apply_modifiers, settings)

# object will still have modifiers, remove them
obj.modifiers.clear()

# assign the new mesh to obj.data 
obj.data = new_mesh

# remove the old mesh from the .blend
bpy.data.meshes.remove(old_mesh)
zeffii
  • 39,634
  • 9
  • 103
  • 186
  • modifiers.clear() is really great. i used to remove modifiers sequentially, but if you have many modifiers it can cause the whole stack to update per removal. – patmo141 Mar 24 '18 at 15:11
  • Is there a way to make this work with the SURFACE_DEFORM mod? Also related to this, is there are way to bind a surface deform mod without bpy.ops.object.surfacedeform_bind()? – MACHIN3 Jun 10 '18 at 13:20