pysdic_blender.BlenderExperiment#
- class BlenderExperiment(*, background_color=(1.0, 1.0, 1.0), background_strength=0.0)[source]#
Bases:
objectA 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 withrender(), 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 ofbpysupports 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, seechannel_weightsinrender()), or the RGB image withcolor=True. Map it to gray levels and quantize it in pysdic (e.g. a gain, thenpysdic.add_gaussian_noise()andpysdic.quantize_image()), as forpysdic.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 uniquename.- Parameters:
name (str)
arealight (BlenderAreaLight)
- Return type:
None
- add_camera(name, camera)[source]#
Add a camera (see
BlenderCamera) to the scene, under a uniquename, with its depth of field if it has one (dof.aperture_fstopanddof.focus_distance, circular aperture).- Parameters:
name (str)
camera (BlenderCamera)
- Return type:
None
- add_mesh(name, mesh, *, material=None)[source]#
Add a mesh (see
BlenderMesh) to the scene with a material (Principled BSDF, seeBlenderMaterialBSDF; 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:
name (str)
mesh (BlenderMesh)
material (BlenderMaterialBSDF | None)
- Return type:
None
- add_spotlight(name, spotlight)[source]#
Add a spot light (see
BlenderSpotLight) to the scene, under a uniquename: a sphere of radiusspotlight.radius(shadow_soft_size), without the soft falloff of Blender (physically correct sphere).- Parameters:
name (str)
spotlight (BlenderSpotLight)
- Return type:
None
- remove_arealight(name)[source]#
Remove the area light named
namefrom the scene.- Parameters:
name (str)
- Return type:
None
- remove_camera(name)[source]#
Remove the camera named
namefrom 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
namefrom 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_weightsignored); 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 withcolor=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 exactlyn_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 withdevice="GPU").
- Returns:
The linear intensity \((H, W)\) (float64) weighted by
channel_weights, or the linear RGB image \((H, W, 3)\) (float64) withcolor=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:
- 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
colorand astrength(non-negative, 0 for no ambient light).- Parameters:
color (ArrayLike)
strength (Real)
- Return type:
None
- 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 (seeBlenderMesh.has_same_topology()), e.g. the mesh displaced by the displacement of another frame. The texture and the material are kept.- Parameters:
name (str)
mesh (BlenderMesh)
- Return type:
None