pysdic_blender.BlenderMesh#
- class BlenderMesh(mesh, *, displacement=None, texture=None, texture_range=(0.0, 255.0), interpolation='Linear', smooth=False)[source]#
Bases:
objectThe geometry and the texture of a surface to render with Blender, extracted from a
pysdic.Mesh.The mesh must be a 3D
triangle_3mesh: 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. withpysdic.quadrangle_4_to_triangle_3().The extracted data:
coordinates\((N_v, 3)\): the vertices, displaced bydisplacement;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, frompysdic.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})\) withtexture_range\((g_{min}, g_{max})\), clipped to \([0, 1]\). The first row of the texture is \(v = 1\) (same convention aspysdic.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 thepysdic.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 3Dtriangle_3mesh, 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. frompysdic.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
meshis not apysdic.Mesh.ValueError – If the mesh is not a 3D
triangle_3mesh, 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 inBlenderExperiment.add_mesh().
- property coordinates: ndarray#
the vertices of the
pysdic.Meshplusdisplacement, 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
coordinatesof the vertices of each triangle, in the order ofpysdic.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().coordinatesandfacesare required;uvandalbedoare None if absent,interpolationis"Linear"andsmoothis False if absent.albedois the reflectance in \([0, 1]\) (notexture_range).typeanddescriptionare ignored, an unknown key raises aKeyError.- Parameters:
- Return type:
- classmethod from_json(path)[source]#
Create a mesh from a JSON file (see
from_dict()).- Parameters:
path (str)
- Return type:
- 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:
- property interpolation: str#
The interpolation of the texture by Blender (
interpolationof 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 smooth: bool#
Whether the shading interpolates the normals of the vertices (
use_smoothof 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;uvandalbedoonly if not None).The data are those given to Blender (displaced vertices, reflectances of the texture), not the
pysdic.Meshnor the gray levels of the texture.
- 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 vertexfaces[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.