Automates Blender 3D workflows via Python scripting API (bpy). Use when generating 3D assets, automating rendering, batch processing models, or creating procedural content for games and visualization.
Automate Blender 3D modeling, rendering, and asset generation using Python scripting.
Master Blender automation:
python scripts/create-scene.py
blender scene.blend --background --python scripts/batch-render.py
python scripts/export-assets.py input/ output/
graph TD
A[Python Script] --> B{Blender API}
B -->|Scene| C[Create Objects]
B -->|Material| D[Apply Shaders]
B -->|Render| E[Generate Images]
B -->|Export| F[Save Assets]
C --> G[Mesh Data]
D --> G
G --> H[Complete Scene]
H --> E
H --> F
E --> I[PNG/EXR Output]
F --> J[FBX/GLTF Output]
style I fill:#99ff99
style J fill:#99ff99
# Blender comes with Python bundled
# Access via blender executable
# Windows
C:\Program Files\Blender Foundation\Blender\blender.exe
# Mac
/Applications/Blender.app/Contents/MacOS/Blender
# Linux
/usr/bin/blender
# Interactive mode
blender --python script.py
# Background mode (no GUI)
blender --background --python script.py
# With .blend file
blender myfile.blend --background --python script.py
import bpy
import math
# Clear existing scene
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete()
# Create object
bpy.ops.mesh.primitive_cube_add(location=(0, 0, 0))
cube = bpy.context.active_object
# Modify object
cube.scale = (2, 2, 2)
cube.rotation_euler = (0, 0, math.radians(45))
# Save
bpy.ops.wm.save_as_mainfile(filepath='output.blend')
import bpy
# Cube
bpy.ops.mesh.primitive_cube_add(
size=2,
location=(0, 0, 0)
)
# Sphere
bpy.ops.mesh.primitive_uv_sphere_add(
radius=1,
location=(3, 0, 0),
segments=32,
ring_count=16
)
# Cylinder
bpy.ops.mesh.primitive_cylinder_add(
radius=1,
depth=2,
location=(-3, 0, 0)
)
# Torus
bpy.ops.mesh.primitive_torus_add(
major_radius=1,
minor_radius=0.25,
location=(0, 3, 0)
)
import bpy
# Create mesh data
vertices = [
(0, 0, 0),
(1, 0, 0),
(1, 1, 0),
(0, 1, 0)
]
edges = []
faces = [(0, 1, 2, 3)]
# Create mesh
mesh = bpy.data.meshes.new(name="CustomMesh")
mesh.from_pydata(vertices, edges, faces)
mesh.update()
# Create object from mesh
obj = bpy.data.objects.new("CustomObject", mesh)
# Link to scene
bpy.context.collection.objects.link(obj)
# Add subdivision surface
obj = bpy.context.active_object
modifier = obj.modifiers.new(name="Subsurf", type='SUBSURF')
modifier.levels = 2
modifier.render_levels = 3
# Add array modifier
array = obj.modifiers.new(name="Array", type='ARRAY')
array.count = 5
array.relative_offset_displace = (1.5, 0, 0)
# Apply modifier
bpy.ops.object.modifier_apply(modifier="Subsurf")
import bpy
# Create material
material = bpy.data.materials.new(name="MyMaterial")
material.use_nodes = True
# Get node tree
nodes = material.node_tree.nodes
links = material.node_tree.links
# Clear default nodes
nodes.clear()
# Add Principled BSDF
bsdf = nodes.new(type='ShaderNodeBsdfPrincipled')
bsdf.location = (0, 0)
# Set properties
bsdf.inputs['Base Color'].default_value = (0.8, 0.2, 0.2, 1.0)
bsdf.inputs['Metallic'].default_value = 0.5
bsdf.inputs['Roughness'].default_value = 0.3
# Add output
output = nodes.new(type='ShaderNodeOutputMaterial')
output.location = (300, 0)
# Connect nodes
links.new(bsdf.outputs['BSDF'], output.inputs['Surface'])
# Assign to object
obj = bpy.context.active_object
if obj.data.materials:
obj.data.materials[0] = material