Sub topic 14
Adding the Same Modifiers with a Python Script
This chapter assumes that you can navigate Blender's 3D Viewport, switch between Object Mode and Edit Mode, edit mesh components, and identify an object's origin, vertices, edges, faces, and normals. It also assumes that you understand that the modifier stack evaluates from top to bottom.
Blender's Python API exposes modifiers through object.modifiers. Calling new() creates a modifier and adds it to the bottom of the stack. Therefore, the order of the new() calls is important.
A script for a predictable modifier stack
The following script expects a mesh object to be selected and active. It removes that object's existing modifiers, then adds Mirror, Array, Solidify, and Subdivision Surface in that order.
import bpy
obj = bpy.context.active_object
if obj is None or obj.type != 'MESH':
raise RuntimeError("Select a mesh object before running this script.")
# Remove existing modifiers so this example has a known stack.
for modifier in list(obj.modifiers):
obj.modifiers.remove(modifier)
# 1. Mirror
mirror = obj.modifiers.new(name="Mirror", type='MIRROR')
mirror.use_axis[0] = True
mirror.use_axis[1] = False
mirror.use_axis[2] = False
mirror.use_clip = True
mirror.use_mirror_merge = True
mirror.merge_threshold = 0.001
# 2. Array
array = obj.modifiers.new(name="Array", type='ARRAY')
array.fit_type = 'FIXED_COUNT'
array.count = 5
array.use_relative_offset = True
array.relative_offset_displace[0] = 1.1
array.relative_offset_displace[1] = 0.0
array.relative_offset_displace[2] = 0.0
array.use_constant_offset = False
array.use_object_offset = False
array.use_merge_vertices = False
# 3. Solidify
solidify = obj.modifiers.new(name="Solidify", type='SOLIDIFY')
solidify.thickness = 0.05
solidify.offset = -1.0
solidify.use_even_offset = True
# 4. Subdivision Surface
subdivision = obj.modifiers.new(name="Subdivision", type='SUBSURF')
subdivision.subdivision_type = 'CATMULL_CLARK'
subdivision.levels = 1
subdivision.render_levels = 2Run the script in Blender's Scripting workspace or in the Python Console. The active object must contain only the half of the mesh that you want to model when Mirror is used. If you run Mirror on a complete object, Blender mirrors the complete object and may create an unwanted duplicate.
The Array settings above use relative offset. A factor of 1.1 spaces each copy by 1.1 times the object's bounding-box width along X. To use a fixed distance instead, change the offset settings:
array.use_relative_offset = False
array.use_constant_offset = True
array.constant_offset_displace = (2.0, 0.0, 0.0)An object offset uses another object's transform as the spacing pattern. Create an Empty, assign it to array.offset_object, and set array.use_object_offset = True. The Empty's rotation and scale can then control the direction and growth of the copies.
Common scripting errors
- Using
bpy.context.objectwhen the wrong object is active. Check the active object before changing it. - Adding Subdivision Surface before Solidify when you intended to smooth a closed shell. The order changes both the shape and the polygon count.
- Setting
array.use_constant_offsetandarray.use_relative_offsettoTruewithout understanding that both offsets are then evaluated together. - Expecting
merge_thresholdto repair distant vertices. It only merges vertices close to the mirror plane. - Applying modifiers in a script without deciding whether the generated geometry should remain editable.
Exercise
What change makes the script create three array copies using a fixed 2-meter X spacing instead of five copies using relative spacing?
Show answer
Set array.count = 3, set array.use_relative_offset = False, set array.use_constant_offset = True, and set array.constant_offset_displace = (2.0, 0.0, 0.0).