pysdic_blender.BlenderMesh#

class BlenderMesh(mesh, *, displacement=None, texture=None, texture_range=(0.0, 255.0), interpolation='Linear', smooth=False)[source]#

Bases: object

The geometry and the texture of a surface to render with Blender, extracted from a pysdic.Mesh.

The mesh must be a 3D triangle_3 mesh: a linear triangle is planar, so the surface rendered by Blender is exactly the surface of the mesh (a non-planar quadrangle would be split by Blender in its own way). Convert the other meshes first, e.g. with pysdic.quadrangle_4_to_triangle_3().

The extracted data:

  • coordinates \((N_v, 3)\): the vertices, displaced by displacement;

  • faces \((N_e, 3)\): the connectivity (each triangle is a polygon of Blender, its loops in the order of its vertices);

  • uv \((N_e, 3, 2)\): the UV coordinates of the vertices of each triangle, from pysdic.Mesh.uvmap (OpenGL convention \((u, v) \in [0, 1]^2\), \(v\) upwards, as in Blender);

  • albedo \((H_t, W_t)\): the texture mapped to the reflectance in \([0, 1]\), \(\rho = (g - g_{min}) / (g_{max} - g_{min})\) with texture_range \((g_{min}, g_{max})\), clipped to \([0, 1]\). The first row of the texture is \(v = 1\) (same convention as pysdic.generate_synthetic_image()).

The texture is multiplied by the base color of the material of the mesh (see BlenderMaterialBSDF). The object is immutable (read-only copies).

Export. to_dict() / to_json() save the extracted data above (not the pysdic.Mesh); from_dict() / from_json() rebuild the Blender mesh from them. With a texture, the file contains all the \(H_t \times W_t\) reflectances (a large JSON file for a large texture).

Parameters:
  • mesh (pysdic.Mesh) – A 3D triangle_3 mesh, with a UV map if a texture is given.

  • displacement (Optional[ArrayLike], optional) – The displacement \((N_v, 3)\) added to the vertices (e.g. the displacement of a frame). By default, the vertices of the mesh.

  • texture (Optional[Union[pysdic.Image, ArrayLike]], optional) – The grayscale texture \((H_t, W_t)\) (or \((H_t, W_t, 1)\)), e.g. from pysdic.compute_wave_speckle(). By default, no texture (uniform base color).

  • texture_range (Tuple[float, float], optional (default: (0.0, 255.0))) – The gray levels mapped to the reflectances 0 and 1.

  • interpolation (str, optional (default: "Linear")) – The interpolation of the texture by Blender: "Closest", "Linear" or "Cubic".

  • smooth (bool, optional (default: False)) – If True, the shading interpolates the normals of the vertices (smooth surface); else each triangle is shaded with its own normal (exact geometry of the mesh).

Raises:
  • TypeError – If mesh is not a pysdic.Mesh.

  • ValueError – If the mesh is not a 3D triangle_3 mesh, has non-finite vertices, has no UV map with a texture, or an input has a wrong shape.

Examples

from pysdic_blender import BlenderMesh

blender_mesh = BlenderMesh(mesh, displacement=analysis.get_displacement(1), texture=speckle)
blender_mesh.to_json("mesh_frame_1.json")
same_mesh = BlenderMesh.from_json("mesh_frame_1.json")
property albedo: ndarray | None#

The reflectance \((H_t, W_t)\) of the texture in \([0, 1]\) (float64, read-only), or None without texture.

\(\rho = \text{clip}((g - g_{min}) / (g_{max} - g_{min}), 0, 1)\) with texture_range. The first row is \(v = 1\) (pysdic convention): the rows are flipped when the texture is given to Blender (first row at \(v = 0\)). It is not multiplied by the base color of the material, which is applied in BlenderExperiment.add_mesh().

property coordinates: ndarray#

the vertices of the pysdic.Mesh plus displacement, in the unit of the scene (the unit of the cameras and the lights).

Type:

The vertices \((N_v, 3)\) given to Blender (float64, read-only)

property faces: ndarray#

the indices (from 0) in coordinates of the vertices of each triangle, in the order of pysdic.Mesh.elements.

Each triangle is a polygon of Blender whose loops follow this order: the normal of the triangle is given by the right-hand rule, \((\vec{X}_1 - \vec{X}_0) \times (\vec{X}_2 - \vec{X}_0)\). Blender renders both sides of the faces.

Type:

The triangles \((N_e, 3)\) (int64, read-only)

classmethod from_dict(data)[source]#

Create a mesh from a dictionary of to_dict().

coordinates and faces are required; uv and albedo are None if absent, interpolation is "Linear" and smooth is False if absent. albedo is the reflectance in \([0, 1]\) (no texture_range). type and description are ignored, an unknown key raises a KeyError.

Parameters:

data (Dict[str, Any])

Return type:

BlenderMesh

classmethod from_json(path)[source]#

Create a mesh from a JSON file (see from_dict()).

Parameters:

path (str)

Return type:

BlenderMesh

has_same_topology(other)[source]#

Check if another mesh has the same triangles and UV coordinates (only the vertices can differ), i.e. if it can replace this one in a scene (see BlenderExperiment.update_mesh()).

Parameters:

other (BlenderMesh)

Return type:

bool

property interpolation: str#

The interpolation of the texture by Blender (interpolation of the Image Texture node): "Closest" (nearest texel), "Linear" (bilinear) or "Cubic" (bicubic). Outside \([0, 1]^2\), the texture is extended with its border texels.

property n_faces: int#

The number of triangles \(N_e\).

property n_vertices: int#

The number of vertices \(N_v\).

property smooth: bool#

Whether the shading interpolates the normals of the vertices (use_smooth of the polygons of Blender). Only the normal used for the shading changes, not the geometry: the silhouette and the shadows stay those of the flat triangles.

to_dict(description=None)[source]#

Export the mesh to a dictionary: {"type": "BlenderMesh", "description": ..., "coordinates", "faces", "uv", "albedo", "interpolation", "smooth"} (nested lists; uv and albedo only if not None).

The data are those given to Blender (displaced vertices, reflectances of the texture), not the pysdic.Mesh nor the gray levels of the texture.

Parameters:

description (str | None)

Return type:

Dict[str, Any]

to_json(path, description=None)[source]#

Export the mesh to a JSON file (see to_dict()).

Parameters:
  • path (str)

  • description (str | None)

Return type:

None

property uv: ndarray | None#

The UV coordinates \((N_e, 3, 2)\) (float64, read-only), or None without UV map.

uv[i, j] is the \((u, v)\) of the vertex faces[i, j] in the triangle \(i\) (a value per loop of Blender, so a vertex can have different UV coordinates in different triangles, e.g. along a seam). OpenGL convention: \((u, v) \in [0, 1]^2\), \(u\) to the right and \(v\) upwards in the texture.