mesh#

k3d.factory.mesh(vertices, indices, normals=[], color=255, colors=[], attribute=[], color_map=None, color_range=[], wireframe=False, flat_shading=True, opacity=1.0, texture=None, texture_file_format=None, volume=[], volume_bounds=[], opacity_function=[], side='front', uvs=None, name=None, group=None, custom_data=None, compression_level=0, triangles_attribute=[], **kwargs)[source]#

Create a Mesh drawable from 3D triangles.

Parameters
  • vertices (array_like) – Array of triangle vertices, float (x, y, z) coordinate triplets.

  • indices (array_like) – Array of vertex indices. int triplets of indices from vertices array.

  • normals (array_like, optional) – Array of vertex normals: float (x, y, z) coordinate triples. Normals are used when flat_shading is false. If the normals are not specified here, normals will be automatically computed.

  • color (int, optional) – Hex color of the vertices when colors is empty, by default _default_color.

  • colors (list, optional) – Array of Hex colors when attribute, color_map and color_range are empty, by default [].

  • attribute (list, optional) – List of values used to apply color_map, by default [].

  • color_map (list, optional) – List of float quadruplets (attribute value, R, G, B) sorted by attribute value, by default None. The first quadruplet should have value 0.0, the last 1.0; R, G, B are RGB color components in the range 0.0 to 1.0.

  • color_range (list, optional) – [min_value, max_value] pair determining the levels of color attribute mapped to 0 and 1 in the colormap, by default [].

  • wireframe (bool, optional) – Display the mesh as wireframe, by default False.

  • flat_shading (bool, optional) – Display the mesh with flat shading, by default True.

  • opacity (float, optional) – Opacity of the mesh, by default 1.0.

  • texture (bytes, optional) – Image data in a specific format, by default None.

  • texture_file_format (str, optional) – Format of the data, , by default None. It should be the second part of MIME format of type ‘image/’,e.g. ‘jpeg’, ‘png’, ‘gif’, ‘tiff’.

  • volume (list, optional) – 3D array of float, by default [].

  • volume_bounds (list, optional) – 6-element tuple specifying the bounds of the volume data (x0, x1, y0, y1, z0, z1), by default [].

  • opacity_function (list, optional) – float tuples (attribute value, opacity) sorted by attribute value, by default []. The first tuples should have value 0.0, the last 1.0; opacity is in the range 0.0 to 1.0.

  • side ({'front', 'back', 'double'}, optional) – Side to render, by default “front”.

  • uvs (array_like, optional) – float uvs for the texturing corresponding to each vertex, by default None.

  • name (str, optional) – Object name, by default None.

  • group (str, optional) – Name of a group, by default None.

  • custom_data (dict) – A object with custom data attached to object.

  • compression_level (int, optional) – Level of data compression [-1, 9], by default 0.

  • triangles_attribute (list, optional) – _description_, by default []

  • **kwargs – For other keyword-only arguments, see process_transform_arguments.

Returns

Mesh Drawable

Return type

Mesh

See also

Examples#

Basic#

import k3d

vertices = [[1, 1, 1], [1, -1, -1], [-1, 1, -1], [-1, -1, 1]]
indices = [[0, 1, 2], [0, 2, 3], [0, 3, 1], [3, 2, 1]]

plt_tetra = k3d.mesh(vertices, indices,
                     colors=[0x32ff31, 0x37d3ff, 0xbc53ff, 0xffc700])

plot = k3d.plot()
plot += plt_tetra
plot.display()

Colormap#

Attention

color_map must be used along with attribute and color_range in order to work correctly.

import k3d
import numpy as np
from k3d.colormaps import matplotlib_color_maps
from matplotlib.tri import Triangulation

n_radii = 8
n_angles = 36

radii = np.linspace(0.125, 1.0, n_radii, dtype=np.float32)
angles = np.linspace(0, 2 * np.pi, n_angles, endpoint=False, dtype=np.float32)[..., np.newaxis]

x = np.append(np.float32(0), (radii * np.cos(angles)).flatten())
y = np.append(np.float32(0), (radii * np.sin(angles)).flatten())
z = np.sin(-x * y)

vertices = np.vstack([x, y, z]).T
indices = Triangulation(x, y).triangles.astype(np.uint32)

plt_mesh = k3d.mesh(vertices, indices,
                    color_map=matplotlib_color_maps.Jet,
                    attribute=z,
                    color_range=[-1.1, 2.01])

plot = k3d.plot()
plot += plt_mesh
plot.display()