.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_gallery/example_blender_stereo_sequence.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr__gallery_example_blender_stereo_sequence.py: .. _example_blender_stereo_sequence: Stereo images of a deforming specimen ===================================== .. contents:: Table of Contents :local: :depth: 1 :backlinks: top A stereo DIC test needs the images of both cameras at each frame of the deformation. This example builds the scene once (two cameras, two lights, a speckled plate), then updates the vertices of the plate with the displacement of each frame and renders both cameras. The setup is saved to JSON files to regenerate the images later. .. important:: Requires ``pysdic-blender`` (``pip install pysdic-blender``), which installs ``bpy``. .. GENERATED FROM PYTHON SOURCE LINES 24-28 Cameras and specimen -------------------- Two cameras at ±15° around the :math:`y` axis, 500 mm from a plate of 100 x 80 mm (in mm). .. GENERATED FROM PYTHON SOURCE LINES 28-53 .. code-block:: Python import numpy from scipy.spatial.transform import Rotation from pysdic import Camera, PerspectiveExtrinsic, PinholeIntrinsic, compute_wave_speckle, create_3d_triangle_3_heightmap def stereo_camera(angle): # Camera 500 mm from the origin, rotated by `angle` around the y axis, looking at the origin rotation = Rotation.from_rotvec([0.0, angle, 0.0]).as_matrix() @ numpy.diag([1.0, -1.0, -1.0]) rotation = rotation.T # World-to-camera position = 500.0 * numpy.array([numpy.sin(angle), 0.0, numpy.cos(angle)]) return Camera( 360, 480, extrinsic=PerspectiveExtrinsic.from_rt( rvec=Rotation.from_matrix(rotation).as_rotvec().tolist(), tvec=(-rotation @ position).tolist() ), intrinsic=PinholeIntrinsic.from_matrix([[2200.0, 0.0, 239.5], [0.0, 2200.0, 179.5], [0.0, 0.0, 1.0]]), ) cameras = {"left": stereo_camera(numpy.radians(-15.0)), "right": stereo_camera(numpy.radians(15.0))} plate = create_3d_triangle_3_heightmap(x_bounds=(-50.0, 50.0), y_bounds=(-40.0, 40.0), n_x=101, n_y=81) x, y = numpy.linspace(-50.0, 50.0, 1000), numpy.linspace(40.0, -40.0, 800) # First row: v = 1 speckle = compute_wave_speckle(numpy.stack(numpy.meshgrid(x, y), axis=-1), wavelength=0.8) .. GENERATED FROM PYTHON SOURCE LINES 54-59 Displacements ------------- At the frame :math:`k`, a stretching of :math:`1.5 k` % along :math:`x` and an out-of-plane bulge of :math:`2 k` mm in the center of the plate. .. GENERATED FROM PYTHON SOURCE LINES 59-68 .. code-block:: Python coordinates = numpy.asarray(plate.coordinates) displacements = {} for frame in range(4): displacement = numpy.zeros_like(coordinates) displacement[:, 0] = 0.015 * frame * coordinates[:, 0] displacement[:, 2] = 2.0 * frame * numpy.exp(-(coordinates[:, 0] ** 2 + coordinates[:, 1] ** 2) / (2 * 20.0 ** 2)) displacements[frame] = displacement .. GENERATED FROM PYTHON SOURCE LINES 69-74 The scene --------- Each camera has a spot light next to it (radius of 20 mm). The texture and the material are given once, with the mesh of the frame 0. .. GENERATED FROM PYTHON SOURCE LINES 74-87 .. code-block:: Python from pysdic_blender import BlenderCamera, BlenderExperiment, BlenderMesh, BlenderSpotLight, get_matte_paint_material experiment = BlenderExperiment(background_strength=0.03) for name, camera in cameras.items(): blender_camera = BlenderCamera(camera) experiment.add_camera(name, blender_camera) experiment.add_spotlight( f"spot_{name}", BlenderSpotLight.looking_at(blender_camera.center + [0.0, 80.0, 0.0], [0.0, 0.0, 0.0], energy=1e6, radius=20.0), ) experiment.add_mesh("plate", BlenderMesh(plate, displacement=displacements[0], texture=speckle), material=get_matte_paint_material()) .. GENERATED FROM PYTHON SOURCE LINES 88-94 Rendering the frames -------------------- :meth:`pysdic_blender.BlenderExperiment.update_mesh` only moves the vertices (same triangles and UV map): the texture follows the material points. One gain is used for all the images, so that the gray levels of the frames are comparable. .. GENERATED FROM PYTHON SOURCE LINES 94-113 .. code-block:: Python images = {} for frame, displacement in displacements.items(): experiment.update_mesh("plate", BlenderMesh(plate, displacement=displacement)) for name in cameras: images[frame, name] = experiment.render(name, n_samples=128, seed=frame) gain = 220.0 / numpy.percentile(images[0, "left"], 99.5) import matplotlib.pyplot as plt fig, axes = plt.subplots(2, len(displacements), figsize=(4 * len(displacements), 6)) for (frame, name), image in images.items(): ax = axes[list(cameras).index(name), frame] ax.imshow(numpy.clip(gain * image, 0.0, 255.0), cmap="gray", vmin=0, vmax=255) ax.set_title(f"Frame {frame}, {name}") ax.axis("off") fig.tight_layout() plt.show() .. image-sg:: /_gallery/images/sphx_glr_example_blender_stereo_sequence_001.png :alt: Frame 0, left, Frame 1, left, Frame 2, left, Frame 3, left, Frame 0, right, Frame 1, right, Frame 2, right, Frame 3, right :srcset: /_gallery/images/sphx_glr_example_blender_stereo_sequence_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 114-121 Saving the setup ---------------- The cameras, the lights and the meshes are exported to JSON files and read back without the pysdic objects (the cameras then have ``camera = None``). A mesh file contains the reflectances of its texture: the mesh of a frame is exported with the texture (the meshes given to ``update_mesh`` have none). .. GENERATED FROM PYTHON SOURCE LINES 121-137 .. code-block:: Python import os import tempfile folder = tempfile.mkdtemp(prefix="pysdic_blender_setup_") for name in cameras: experiment.get_camera(name).to_json(os.path.join(folder, f"camera_{name}.json"), description=f"Camera {name}") experiment.get_spotlight(f"spot_{name}").to_json(os.path.join(folder, f"spot_{name}.json")) BlenderMesh(plate, displacement=displacements[3], texture=speckle).to_json( os.path.join(folder, "plate_frame_3.json"), description="Plate at the frame 3" ) camera_left = BlenderCamera.from_json(os.path.join(folder, "camera_left.json")) print(camera_left, camera_left.camera) print(BlenderMesh.from_json(os.path.join(folder, "plate_frame_3.json"))) print(sorted(os.listdir(folder))) .. rst-class:: sphx-glr-script-out .. code-block:: none BlenderCamera(480x360 pixels, lens=22.000 mm, sensor_fit='HORIZONTAL', shift=(-0.0000, 0.0000)) None BlenderMesh(8181 vertices, 16000 triangles, texture 1000x800) ['camera_left.json', 'camera_right.json', 'plate_frame_3.json', 'spot_left.json', 'spot_right.json'] .. rst-class:: sphx-glr-timing **Total running time of the script:** (1 minutes 8.494 seconds) .. _sphx_glr_download__gallery_example_blender_stereo_sequence.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: example_blender_stereo_sequence.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: example_blender_stereo_sequence.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: example_blender_stereo_sequence.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_