trimesh.visual.color module

color.py

Hold and deal with visual information about meshes.

There are lots of ways to encode visual information, and the goal of this architecture is to make it possible to define one, and then transparently get the others. The two general categories are:

  1. colors, defined for a face, vertex, or material

  2. textures, defined as an image and UV coordinates for each vertex

This module only implements diffuse colors at the moment.

Goals

  1. If nothing is defined sane defaults should be returned

  2. If a user alters or sets a value, that is considered user data and should be saved and treated as such.

  3. Only one ‘mode’ of visual (vertex or face) is allowed at a time and setting or altering a value should automatically change the mode.

class trimesh.visual.color.ColorVisuals(mesh=None, face_colors=None, vertex_colors=None)

Bases: Visuals

Store color information about a mesh.

__init__(mesh=None, face_colors=None, vertex_colors=None)

Store color information about a mesh.

Parameters:
  • mesh (Trimesh) – Object that these visual properties are associated with

  • colors (face) – Colors per-face

  • vertex_colors ((n,3|4) or (3,) or (4,) uint8) – Colors per-vertex

concatenate(other, *args)

Concatenate two or more ColorVisuals objects into a single object.

Parameters:
  • other (ColorVisuals) – Object to append

  • *args (ColorVisuals objects)

Returns:

result – Containing information from current object and others in the order it was passed.

Return type:

ColorVisuals

copy()

Return a copy of the current ColorVisuals object.

Returns:

copied – Contains the same information as self

Return type:

ColorVisuals

property defined

Are any colors defined for the current mesh.

Returns:

defined – Are colors defined or not.

Return type:

bool

property face_colors

Colors defined for each face of a mesh.

If no colors are defined, defaults are returned.

Returns:

colors – RGBA color for each face

Return type:

(len(mesh.faces), 4) uint8

face_subset(face_index)

Given a mask of face indices, return a sliced version.

Parameters:

face_index ((n,) int, mask for faces) – (n,) bool, mask for faces

Returns:

visual

Return type:

ColorVisuals object containing a subset of faces.

property kind

What color mode has been set.

Returns:

mode – One of (‘face’, ‘vertex’, None)

Return type:

str or None

property main_color

What is the most commonly occurring color.

Returns:

color

Return type:

(4,) uint8, most common color

to_texture()

Convert the current ColorVisuals object to a texture with a SimpleMaterial defined.

Returns:

visual – Copy of the current visuals as a texture.

Return type:

trimesh.visual.TextureVisuals

property transparency

Does the current object contain any transparency.

Returns:

transparency

Return type:

bool, does the current visual contain transparency

update_faces(mask)

Apply a mask to remove or duplicate face properties

update_vertices(mask)

Apply a mask to remove or duplicate vertex properties.

property vertex_colors

Return the colors for each vertex of a mesh

Returns:

colors

Return type:

(len(mesh.vertices), 4) uint8, color for each vertex

class trimesh.visual.color.VertexColor(colors=None, obj=None)

Bases: Visuals

Create a simple visual object to hold just vertex colors for objects such as PointClouds.

__init__(colors=None, obj=None)

Create a vertex color visual

concatenate(other)

Concatenate this visual object with another VertexVisuals.

Parameters:

other (VertexColors or ColorVisuals) – Other object to concatenate

Returns:

concate – Object with both colors

Return type:

VertexColor

copy()

Return a copy of the current visuals

property kind
update_faces(mask)
update_vertices(mask)
property vertex_colors
trimesh.visual.color.color_to_uv(vertex_colors)

Pack vertex colors into UV coordinates and a simple image material

Parameters:

vertex_colors ((n, 4) float) – Array of vertex colors.

Returns:

  • material (SimpleMaterial) – Material containing color information.

  • uv ((n, 2) float) – Normalized UV coordinates

trimesh.visual.color.colors_to_materials(colors, count=None)

Convert a list of colors into a list of unique materials and material indexes.

Parameters:
  • colors ((n, 3) or (n, 4) float) – RGB or RGBA colors

  • count (int) – Number of entities to apply color to

Returns:

  • diffuse ((m, 4) int) – Colors

  • index ((count,) int) – Index of each color

trimesh.visual.color.face_to_vertex_color(mesh, face_colors, dtype=<class 'numpy.uint8'>)

Convert face colors into vertex colors.

Parameters:
  • mesh (trimesh.Trimesh object)

  • face_colors ((n, (3,4)) int, face colors)

  • dtype (data type of output)

Returns:

vertex_colors

Return type:

(m,4) dtype, colors for each vertex

trimesh.visual.color.hex_to_rgba(color)

Turn a string hex color to a (4,) RGBA color.

Parameters:

color (str, hex color)

Returns:

rgba

Return type:

(4,) np.uint8, RGBA color

trimesh.visual.color.interpolate(values, color_map=None, dtype=<class 'numpy.uint8'>)

Given a 1D list of values, return interpolated colors for the range.

Parameters:
  • values ((n, ) float) – Values to be interpolated over

  • color_map (None, or str) – Key to a colormap contained in: matplotlib.pyplot.colormaps() e.g: ‘viridis’

Returns:

interpolated – Interpolated RGBA colors

Return type:

(n, 4) dtype

trimesh.visual.color.linear_color_map(values, color_range=None)

Linearly interpolate between two colors.

If colors are not specified the function will interpolate between 0.0 values as red and 1.0 as green.

Parameters:
  • values ((n, ) float) – Values to interpolate

  • color_range (None, or (2, 4) uint8) – What colors should extrema be set to

Returns:

colors – RGBA colors for interpolated values

Return type:

(n, 4) uint8

trimesh.visual.color.random_color(dtype=<class 'numpy.uint8'>)

Return a random RGB color using datatype specified.

Parameters:

dtype (numpy dtype of result)

Returns:

color

Return type:

(4,) dtype, random color that looks OK

trimesh.visual.color.to_float(colors: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes]) ndarray[tuple[int, ...], dtype[float64]]

Convert integer colors to 0.0-1.0 floating point colors

Parameters:

colors ((n, d) int) – Integer colors

Returns:

as_float – Float colors 0.0 - 1.0

Return type:

(n, d) float

trimesh.visual.color.to_rgba(colors, dtype=<class 'numpy.uint8'>) ndarray[tuple[int, ...], dtype[_ScalarType_co]]

Convert a single or multiple RGB colors to RGBA colors.

Parameters:

colors ((n, 3) or (n, 4) array) – RGB or RGBA colors or None

Returns:

colors – (4,) single RGBA color

Return type:

(n, 4) list of RGBA colors

trimesh.visual.color.uv_to_color(uv, image)

Get the color in a texture image.

Parameters:
  • uv ((n, 2) float) – UV coordinates on texture image

  • image (PIL.Image) – Texture image

Returns:

colors – RGBA color at each of the UV coordinates

Return type:

(n, 4) uint4

trimesh.visual.color.uv_to_interpolated_color(uv, image)

Get the color from texture image using bilinear sampling.

Parameters:
  • uv ((n, 2) float) – UV coordinates on texture image

  • image (PIL.Image) – Texture image

Returns:

colors – RGBA color at each of the UV coordinates.

Return type:

(n, 4) uint8

trimesh.visual.color.vertex_to_face_color(vertex_colors, faces)

Convert a list of vertex colors to face colors.

Parameters:
  • vertex_colors ((n,(3,4)), colors)

  • faces ((m,3) int, face indexes)

Returns:

face_colors

Return type:

(m,4) colors