pysdic_blender.BlenderExperiment#

class BlenderExperiment(*, background_color=(1.0, 1.0, 1.0), background_strength=0.0)[source]#

Bases: object

A Blender scene of cameras, meshes, spot lights and area lights, rendered with Cycles.

The objects of pysdic are converted when they are added (see BlenderCamera, BlenderMesh, BlenderMaterialBSDF, BlenderSpotLight, BlenderAreaLight). The scene is rendered from one camera at a time with render(), which returns the image as an array.

For a sequence of frames, update the scene between the renders, e.g. the vertices of a mesh displaced by the displacement of each frame (update_mesh()).

Important

Requires bpy (Blender as a Python module), imported at the creation of the first experiment. Each release of bpy supports exactly one version of Python.

Blender has one global state: creating an experiment clears all the data of Blender (factory settings, empty scene), and only the last created experiment can be used (the previous ones raise an error).

Colors. Cycles renders in RGB (scene-linear Rec.709, float32): the colors come from the materials (base_color, tints, emission), the lights (color) and the world. The render goes through a lossless 32-bit float OpenEXR file (exact copy of the float32 buffer of Cycles, without the tone curve of the view transform) and is returned as a linear intensity: by default a weighted sum of the RGB channels (a monochrome sensor, see channel_weights in render()), or the RGB image with color=True. Map it to gray levels and quantize it in pysdic (e.g. a gain, then pysdic.add_gaussian_noise() and pysdic.quantize_image()), as for pysdic.generate_synthetic_image(): Blender never quantizes the render.

Parameters:
  • background_color (ArrayLike, optional (default: (1.0, 1.0, 1.0))) – The RGB color of the world (ambient light).

  • background_strength (Real, optional (default: 0.0)) – The strength of the ambient light (0: only the spot lights).

Examples

from pysdic_blender import BlenderCamera, BlenderExperiment, BlenderMesh, BlenderSpotLight, get_matte_paint_material

experiment = BlenderExperiment(background_strength=0.2)
experiment.add_camera("left", BlenderCamera(left_camera))
experiment.add_camera("right", BlenderCamera(right_camera))
experiment.add_spotlight("spot", BlenderSpotLight.looking_at([0.0, 0.0, 600.0], [0.0, 0.0, 0.0], energy=5e5))
experiment.add_mesh("specimen", BlenderMesh(mesh, texture=speckle), material=get_matte_paint_material())

images = {}
for frame, displacement in enumerate(displacements):
    experiment.update_mesh("specimen", BlenderMesh(mesh, displacement=displacement))
    images[frame] = [experiment.render(name, n_samples=256) for name in ("left", "right")]
add_arealight(name, arealight)[source]#

Add an area light (see BlenderAreaLight) to the scene, under a unique name.

Parameters:
Return type:

None

add_camera(name, camera)[source]#

Add a camera (see BlenderCamera) to the scene, under a unique name, with its depth of field if it has one (dof.aperture_fstop and dof.focus_distance, circular aperture).

Parameters:
Return type:

None

add_mesh(name, mesh, *, material=None)[source]#

Add a mesh (see BlenderMesh) to the scene with a material (Principled BSDF, see BlenderMaterialBSDF; by default the default material of Blender).

The texture of the mesh, multiplied by the base color of the material, is the base color of the shader: a float image of Blender with the reflectances (linear values, no color transform).

Parameters:
Return type:

None

add_spotlight(name, spotlight)[source]#

Add a spot light (see BlenderSpotLight) to the scene, under a unique name: a sphere of radius spotlight.radius (shadow_soft_size), without the soft falloff of Blender (physically correct sphere).

Parameters:
Return type:

None

property arealight_names: Tuple[str, ...]#

The names of the area lights.

property camera_names: Tuple[str, ...]#

The names of the cameras.

get_arealight(name)[source]#

The area light named name.

Parameters:

name (str)

Return type:

BlenderAreaLight

get_camera(name)[source]#

The camera named name.

Parameters:

name (str)

Return type:

BlenderCamera

get_mesh(name)[source]#

The mesh named name (its last update).

Parameters:

name (str)

Return type:

BlenderMesh

get_spotlight(name)[source]#

The spot light named name.

Parameters:

name (str)

Return type:

BlenderSpotLight

property mesh_names: Tuple[str, ...]#

The names of the meshes.

remove_arealight(name)[source]#

Remove the area light named name from the scene.

Parameters:

name (str)

Return type:

None

remove_camera(name)[source]#

Remove the camera named name from the scene.

Parameters:

name (str)

Return type:

None

remove_mesh(name)[source]#

Remove the mesh named name (with its material and texture) from the scene.

Parameters:

name (str)

Return type:

None

remove_spotlight(name)[source]#

Remove the spot light named name from the scene.

Parameters:

name (str)

Return type:

None

render(camera, *, color=False, channel_weights=(0.3333333333333333, 0.3333333333333333, 0.3333333333333333), n_samples=128, filter_width=1.5, seed=0, device='CPU', gpu_backend='CUDA')[source]#

Render the scene from a camera with Cycles.

Parameters:
  • camera (str) – The name of the camera.

  • color (bool, optional (default: False)) – If True, return the RGB image \((H, W, 3)\) (color camera, channel_weights ignored); else the intensity \((H, W)\) of a monochrome sensor.

  • channel_weights (ArrayLike, optional (default: (1/3, 1/3, 1/3))) – The sensitivity \((w_R, w_G, w_B)\) of the monochrome sensor to the RGB channels (non-negative, not all zero, not normalized): the intensity is \(I = w_R R + w_G G + w_B B\), by default the mean of the channels. E.g. (0.2126, 0.7152, 0.0722) for the Rec.709 luminance. Ignored with color=True.

  • n_samples (Integral, optional (default: 128)) – The number of samples of every pixel (the noise of Cycles decreases as \(1 / \sqrt{n}\)). The render is unbiased: no denoising, no adaptive sampling (every pixel gets exactly n_samples), no clamping of the samples and no filtering of the glossy reflections (blur_glossy = 0), contrary to the defaults of Blender.

  • filter_width (Real, optional (default: 1.5)) – The width in pixels of the pixel filter of Cycles (Blackman-Harris): the blur of the render.

  • seed (Integral, optional (default: 0)) – The seed of the sampling of Cycles (reproducible renders).

  • device (str, optional (default: "CPU")) – "CPU" or "GPU".

  • gpu_backend (str, optional (default: "CUDA")) – The backend of the GPU devices: "CUDA", "OPTIX", "HIP", "METAL" or "ONEAPI" (only with device="GPU").

Returns:

The linear intensity \((H, W)\) (float64) weighted by channel_weights, or the linear RGB image \((H, W, 3)\) (float64) with color=True, with the rows of the image from top to bottom (pysdic convention). The values are those of the float32 buffer of Cycles (no quantization, no clipping).

Return type:

numpy.ndarray

set_background(color=(1.0, 1.0, 1.0), strength=0.0)[source]#

Set the world of the scene: a uniform ambient light of an RGB color and a strength (non-negative, 0 for no ambient light).

Parameters:
  • color (ArrayLike)

  • strength (Real)

Return type:

None

property spotlight_names: Tuple[str, ...]#

The names of the spot lights.

update_mesh(name, mesh)[source]#

Replace the vertices of a mesh of the scene by those of mesh, which must have the same triangles and UV coordinates (see BlenderMesh.has_same_topology()), e.g. the mesh displaced by the displacement of another frame. The texture and the material are kept.

Parameters:
Return type:

None