Developer docs / AI Fabric tutorials

Blender render farm

  • Batch / map
  • Image DSL

Take it for a spin

The whole program in one file, ready to run.

blender_render_farm.py
import bladekit

app = bladekit.app("blender-render-farm")

# Blender as a Python module: the `bpy` wheel bundles the full Blender runtime,
# Cycles renderer included, so no `blender` binary to apt-install and no .blend
# file to upload. We build the scene procedurally, right in Python.
#
# bpy wheels are pinned to a single interpreter version (bpy 5.x = Python 3.13),
# so the image pins its Python to match. This is the one tutorial that does NOT
# use the platform default of 3.12 (bpy leaves no choice), so blade run here
# needs a 3.13 client venv: uv venv -p 3.13 && uv pip install bladekit
# The apt packages are the X/GL shared libraries the wheel dlopens at import
# time on a headless box.
image = (
    bladekit.Image.debian_slim(python_version="3.13")
    .apt_install(
        "libgl1", "libegl1", "libxi6", "libxrender1",
        "libxkbcommon0", "libsm6", "libxxf86vm1", "libxfixes3",
    )
    .pip_install("bpy")
)


@app.function(gpu="RTX-A4500", image=image, cpu=4, memory=8192, timeout=600)
def render_frame(frame: int) -> bytes:
    """Build the demo scene, render one frame with Cycles, return the PNG."""
    import math
    import os
    import tempfile

    import bpy

    # Start from an empty scene (no default cube).
    bpy.ops.wm.read_factory_settings(use_empty=True)
    scene = bpy.context.scene

    # A red metallic sphere on a gray floor.
    bpy.ops.mesh.primitive_uv_sphere_add(radius=1.0, location=(0, 0, 1))
    sphere = bpy.context.object
    bpy.ops.object.shade_smooth()
    mat = bpy.data.materials.new("Red")
    mat.use_nodes = True
    bsdf = mat.node_tree.nodes["Principled BSDF"]
    bsdf.inputs["Base Color"].default_value = (0.8, 0.1, 0.1, 1.0)
    bsdf.inputs["Metallic"].default_value = 0.6
    bsdf.inputs["Roughness"].default_value = 0.3
    sphere.data.materials.append(mat)

    bpy.ops.mesh.primitive_plane_add(size=20, location=(0, 0, 0))

    bpy.ops.object.light_add(type="SUN", location=(4, -4, 8))
    bpy.context.object.data.energy = 4.0

    # Orbit the camera around the sphere: one full turn over the animation.
    angle = frame / 24 * 2 * math.pi
    bpy.ops.object.camera_add(location=(6 * math.cos(angle), 6 * math.sin(angle), 3))
    camera = bpy.context.object
    target = bpy.data.objects.new("Target", None)
    scene.collection.objects.link(target)
    target.location = (0, 0, 1)
    camera.constraints.new(type="TRACK_TO").target = target
    scene.camera = camera

    # Cycles settings: small demo frame; raise resolution/samples for real work.
    scene.render.engine = "CYCLES"
    scene.cycles.samples = 16
    scene.render.resolution_x = 256
    scene.render.resolution_y = 256
    scene.render.image_settings.file_format = "PNG"

    # Render on the GPU (CUDA); fall back to CPU if no device is found.
    prefs = bpy.context.preferences.addons["cycles"].preferences
    prefs.compute_device_type = "CUDA"
    prefs.get_devices()
    cuda = [d for d in prefs.devices if d.type == "CUDA"]
    if cuda:
        for d in prefs.devices:
            d.use = d.type == "CUDA"
        scene.cycles.device = "GPU"
    print(f"frame {frame}: rendering on {cuda[0].name if cuda else 'CPU'}")

    out = os.path.join(tempfile.mkdtemp(), f"frame_{frame:04d}.png")
    scene.render.filepath = out
    bpy.ops.render.render(write_still=True)

    with open(out, "rb") as f:
        return f.read()


@app.local_entrypoint()
def main():
    import pathlib

    total_frames = 24  # one full camera orbit, 1 second at 24 fps
    frames = list(range(1, total_frames + 1))

    # .map() fans the frames out across parallel GPU containers; each one
    # renders a single frame and returns the PNG bytes.
    out_dir = pathlib.Path("renders")
    out_dir.mkdir(exist_ok=True)
    ok = 0
    for frame, png in zip(frames, render_frame.map(frames)):
        (out_dir / f"frame_{frame:04d}.png").write_bytes(png)
        ok += 1
    print(f"Rendered {ok}/{total_frames} frames into ./renders/")
    print("Stitch them: ffmpeg -framerate 24 -i renders/frame_%04d.png orbit.mp4")

Run it

  1. Run blade run blender_render_farm.py. No .blend upload needed: the scene is built procedurally with the bpy module (Blender as a pip package, Cycles included).
  2. All 24 frames render in parallel GPU containers via .map(). Wall time is roughly one frame's render plus container startup. Cycles drives CUDA directly through bpy: no X server, no EGL setup.
  3. PNG frames land in ./renders/ on your machine; each container returns its frame as bytes.
  4. Stitch the animation: ffmpeg -framerate 24 -i renders/frame_%04d.png orbit.mp4.
  5. Scale up: raise resolution_x/y and cycles.samples for real quality, and bump total_frames. The farm pattern stays the same.

Concepts used: Run, Image build

Contact the team

Loading the form…