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.
Source code: pysdic-blender on GitHub
Installation:
pip install pysdic-blender(installspysdicandbpy)
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 camera follows the OpenCV pinhole model: a
pysdic.PerspectiveExtrinsic, apysdic.PinholeIntrinsicwithout skew and apysdic.NoDistortion(checked on test points, an error is raised otherwise);the mesh is a
triangle_3mesh with a UV map (convert a quadrangle mesh withpysdic.quadrangle_4_to_triangle_3()).
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 |
|---|---|
|
A Blender camera from a |
|
A Blender mesh from a |
|
The material of a mesh (Principled BSDF): |
|
A spot light. |
|
The Blender scene: |
|
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.