Sub topic 15
Scripting Extrude and Inset with Python
This chapter assumes that you can navigate Blender, switch between Object Mode and Edit Mode, select mesh components, and use basic transform keys. It also assumes that you can run Python from Blender's Scripting workspace or Python Console.
Blender provides two useful Python approaches:
bpy.opscalls an operator, similar to choosing a command from the Blender interface.bmeshedits the mesh data directly and gives you more control over the resulting geometry.
Calling Blender operators with `bpy.ops`
An operator depends on Blender's current context. The correct object must be active, Blender must be in Edit Mode, and the required faces must be selected.
The following code performs the same operation as pressing E on selected faces and moving the new geometry 1 unit along the Z axis:
import bpy
# Run with a mesh object active and in Edit Mode, with one or more faces selected.
bpy.ops.mesh.extrude_region_move(
TRANSFORM_OT_translate={"value": (0.0, 0.0, 1.0)}
)The vector (0.0, 0.0, 1.0) moves the extrusion only along the Z axis. Change it to (0.0, 2.0, 0.0) to move it along Y instead.
You can also inset the selected faces:
import bpy
# Run in Edit Mode with faces selected.
bpy.ops.mesh.inset(thickness=0.1, depth=0.0)Here, thickness controls the border width. A positive depth moves the inset faces inward or outward, depending on the value and face orientation. An operator is convenient for short scripts, but it can fail when the mode, selection, or active object is wrong.
> callout note > If an operator reports a context error, check the active object, current mode, selection mode, and selected components before changing the code. >
Editing mesh data with `bmesh`
bmesh is Blender's mesh-editing API. It works well when a script must inspect or modify individual vertices, edges, or faces.
This example extrudes selected faces and translates only the newly created vertices:
import bpy
import bmesh
obj = bpy.context.edit_object
me = obj.data
bm = bmesh.from_edit_mesh(me)
faces = [f for f in bm.faces if f.select]
ret = bmesh.ops.extrude_face_region(bm, geom=faces)
new_verts = [e for e in ret["geom"] if isinstance(e, bmesh.types.BMVert)]
bmesh.ops.translate(bm, vec=(0.0, 0.0, 1.0), verts=new_verts)
bmesh.update_edit_mesh(me)bmesh.from_edit_mesh() connects the script to the mesh currently being edited. The extrusion operation returns the geometry it created. Filtering that result for BMVert objects prevents the script from moving the original vertices accidentally.
Always call bmesh.update_edit_mesh() after changing an Edit Mode mesh. Without it, the viewport might not display the changes immediately.
Choosing an approach
| Need | Recommended approach |
|---|---|
| Reproduce one interface command | `bpy.ops` |
| Control newly created geometry | `bmesh` |
| Work with selected Edit Mode faces | Either approach |
| Run without relying on selection context | Prepare the mesh and use `bmesh` |
Operation flow
Common scripting errors
Duplicate geometry often comes from calling an extrusion twice or translating both the returned vertices and the original selection. Confirm which elements an operation returns before transforming them.
An empty faces list means that no faces were selected when the script ran. A None edit object usually means Blender is in Object Mode or the active object is not a mesh.
Use small scripts first. Create one extrusion, inspect the result, and then add inset or loop-building operations.
Exercise
Which API is the better choice when a script must move only the vertices created by an extrusion?
Show answer
Use bmesh. Its extrusion result identifies the newly created geometry, so the script can transform only those vertices.