| name | blender-python-api |
| description | Create and manipulate 3D models using Blender's Python API (bpy). Use for mesh modeling, animation, rendering, and batch processing. Requires Blender installation; scripts run inside Blender or via command line. |
Blender Python API
Automate Blender using the bpy Python module.
Environment
Scripts run inside Blender's Python environment:
import bpy
import bmesh
from mathutils import Vector, Matrix
Creating Objects
import bpy
# Create cube
bpy.ops.mesh.primitive_cube_add(size=2, location=(0, 0, 0))
# Create cylinder
bpy.ops.mesh.primitive_cylinder_add(radius=1, depth=2, location=(5, 0, 0))
# Create sphere
bpy.ops.mesh.primitive_uv_sphere_add(radius=1, location=(0, 5, 0))
Object Manipulation
import bpy
obj = bpy.context.active_object
# Transform
obj.location = (1, 2, 3)
obj.rotation_euler = (0, 0, 0.785) # radians
obj.scale = (2, 2, 2)
# Rename
obj.name = "MyObject"
Mesh Editing with BMesh
import bpy
import bmesh
obj = bpy.context.active_object
bm = bmesh.from_edit_mesh(obj.data)
# Add vertices
v1 = bm.verts.new((0, 0, 0))
v2 = bm.verts.new((1, 0, 0))
v3 = bm.verts.new((0.5, 1, 0))
# Create face
bm.faces.new([v1, v2, v3])
bmesh.update_edit_mesh(obj.data)
Boolean Operations
import bpy
# Add boolean modifier
obj = bpy.context.active_object
mod = obj.modifiers.new(name="Boolean", type='BOOLEAN')
mod.operation = 'DIFFERENCE'
mod.object = bpy.data.objects["Cutter"]
# Apply modifier
bpy.ops.object.modifier_apply(modifier="Boolean")
Export
import bpy
# Export STL
bpy.ops.export_mesh.stl(filepath="model.stl")
# Export OBJ
bpy.ops.export_scene.obj(filepath="model.obj")
# Export FBX
bpy.ops.export_scene.fbx(filepath="model.fbx")
Command Line
# Run script headless
blender --background --python script.py
# Render frame
blender -b file.blend -o //output -f 1