Skip to content

Modifiers

We've shown use of modifiers in earlier chapters, especially for basic mesh editing. Modifiers are a very nice way of generating scene content or altering geometry, as they provide a non-destructive way to set up a series of operations. Modifier parameters can also easily be changed to see their effect.

In the Python API there is good support to work with modifiers. The main entry point is to add a modifier to a scene object through the modifiers attribute. Suppose we have a scene with a Cube and Suzanne mesh, and we want to do a boolean subtraction of the monkey head from the Cube.

We add a modifier to the Cube, and set its parameters:

>>> o = bpy.data.objects['Cube']
# Add a Boolean modifier on the object and set its parameters
>>> mod = o.modifiers.new(name='boolmod', type='BOOLEAN')
>>> mod.object = bpy.data.objects['Suzanne']
>>> mod.operation = 'DIFFERENCE'

# At this point the modifier is all set up. We hide
# the object we subtract to make the boolean result visible.
>>> bpy.data.objects['Suzanne'].hide_viewport = True

>>> list(o.modifiers)
[bpy.data.objects['Cube'].modifiers["boolmod"]]

# Could remove the modifier with
# o.modifiers.remove(o.modifiers[0])

In the viewport the result of the modifier is directly visible:

We can even continue changing the modifier parameters from Python and see the result (which is analogue to setting the value through the UI):

>>> mod.operation = 'INTERSECT'

Applying a modifier, in essence updating an object to the result of the modifier and removing the modifier itself, needs to be done using an operator:

# We need to pass the name of modifier to apply
>>> bpy.ops.object.modifier_apply(modifier="boolmod")
{'FINISHED'}

>>> list(o.modifiers)
[]

Last update: 07 March 2024 09:51:11