Blender Rendering (pysdic-blender)#

pysdic.generate_synthetic_image() renders the exact gray levels of a textured mesh, without lighting. To render the same scene with lighting, shading and the reflections of a material, a separate package, pysdic-blender, drives Blender (Cycles renderer) from the objects of pysdic.

Important

pysdic-blender is not part of pysdic and has no documentation of its own: this page is its reference.

It is a separate package because bpy (Blender as a Python module) exists for only one version of Python per Blender release: pysdic-blender requires Python 3.13 (bpy 5.1), while pysdic supports Python 3.11 and later. Use a dedicated environment for the rendering.

When can a scene be rendered by Blender?#

Blender only knows the pinhole camera model and splits non-planar polygons its own way. A scene is accepted only if:

The pose of the camera is converted from the OpenCV convention (\(z\) forward, \(y\) down) to the Blender convention (\(z\) backward, \(y\) up): with the world-to-camera rotation \(R\) and translation \(\vec{t}\) of the extrinsic, the center of the camera is \(\vec{C} = -R^T \vec{t}\) and its orientation in Blender is \(R^T \, \text{diag}(1, -1, -1)\).

Objects#

Object

Role

BlenderCamera(camera, *, pixel_size=0.01, clip_start=1e-3, clip_end=1e6)

A Blender camera from a pysdic.Camera (OpenCV pinhole only).

BlenderMesh(mesh, texture=...)

A Blender mesh from a triangle_3 pysdic.Mesh with its UV map and its texture (float image, “Non-Color”).

BlenderMaterialBSDF and get_*_material()

The material of a mesh (Principled BSDF): get_matte_paint_material, get_steel_material, get_iron_material, get_copper_material, get_titanium_material, get_mirror_material.

BlenderSpotLight(position, direction, ...), BlenderSpotLight.looking_at(position, target, ...)

A spot light.

BlenderExperiment

The Blender scene: add_camera, add_mesh (with a material), update_mesh (e.g. a displaced mesh at each frame), add_spotlight, set_background, remove_*, and render(camera, *, n_samples, ...) which returns the rendered image (scene-linear values).

render_blender_image

Render one image in a single call.

Note

Blender has a single scene per process: only one BlenderExperiment is active at a time (a new experiment resets the scene).

Example#

import numpy
from pysdic import Camera, PerspectiveExtrinsic, PinholeIntrinsic, compute_wave_speckle, create_3d_triangle_3_heightmap
from pysdic_blender import BlenderCamera, BlenderExperiment, BlenderMesh, BlenderSpotLight, get_matte_paint_material

camera = Camera(
    180, 240,
    extrinsic=PerspectiveExtrinsic.from_rt(rvec=[numpy.pi, 0.0, 0.0], tvec=[0.0, 0.0, 5.0]),
    intrinsic=PinholeIntrinsic.from_matrix([[300.0, 0.0, 119.5], [0.0, 300.0, 89.5], [0.0, 0.0, 1.0]]),
)
plate = create_3d_triangle_3_heightmap(x_bounds=(-2.5, 2.5), y_bounds=(-2.0, 2.0), n_x=11, n_y=9)
x, y = numpy.linspace(-2.5, 2.5, 500), numpy.linspace(2.0, -2.0, 400)
speckle = compute_wave_speckle(numpy.stack(numpy.meshgrid(x, y), axis=-1), wavelength=0.15)

experiment = BlenderExperiment(background_strength=0.1)
experiment.add_camera("camera", BlenderCamera(camera))
experiment.add_mesh("plate", BlenderMesh(plate, texture=speckle), material=get_matte_paint_material())
experiment.add_spotlight("spot", BlenderSpotLight.looking_at([3.0, 0.0, 6.0], [0.0, 0.0, 0.0], energy=2000.0))
image = experiment.render("camera", n_samples=64)  # (180, 240), scene-linear

See also

pysdic.generate_synthetic_image() (Synthetic Images) for the rendering without lighting, available in pysdic with any camera model and any mesh.