trimesh.visual

visual

Handle visual properties for meshes, including color and texture

class trimesh.visual.ColorVisuals(mesh=None, face_colors: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes] | None = None, vertex_colors: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes] | None = None)

Bases: Visuals

Store color information about a mesh.

__init__(mesh=None, face_colors: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes] | None = None, vertex_colors: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes] | None = 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: Iterable[Visuals] | Visuals, *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() ColorVisuals

Return a copy of the current ColorVisuals object.

Returns:

copied – Contains the same information as self

Return type:

ColorVisuals

property defined: bool

Are any colors defined for the current mesh.

Returns:

defined – Are colors defined or not.

Return type:

bool

property face_colors: ndarray[tuple[int, ...], dtype[uint8]]

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: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes])

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: str | None

What color mode has been set.

Returns:

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

Return type:

str or None

property main_color: ndarray[tuple[int, ...], dtype[uint8]]

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: bool

Does the current object contain any transparency.

Returns:

transparency

Return type:

bool, does the current visual contain transparency

update_faces(mask: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes])

Apply a mask to remove or duplicate face properties

update_vertices(mask: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes])

Apply a mask to remove or duplicate vertex properties.

property vertex_colors: ndarray[tuple[int, ...], dtype[uint8]]

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.TextureVisuals(uv=None, material=None, image=None, face_materials=None)

Bases: Visuals

__init__(uv=None, material=None, image=None, face_materials=None)

Store a single material and per-vertex UV coordinates for a mesh.

If passed UV coordinates and a single image it will create a SimpleMaterial for the image.

Parameters:
  • uv ((n, 2) float) – UV coordinates for the mesh

  • material (Material) – Store images and properties

  • image (PIL.Image) – Can be passed to automatically create material

concatenate(others)

Concatenate this TextureVisuals object with others and return the result without modifying this visual.

Parameters:

others ((n,) Visuals) – Other visual objects to concatenate

Returns:

concatenated – Concatenated visual objects

Return type:

TextureVisuals

copy(uv=None)

Return a copy of the current TextureVisuals object.

Returns:

copied – Contains the same information in a new object

Return type:

TextureVisuals

property defined

Check if any data is stored

Returns:

defined – Are UV coordinates and images set?

Return type:

bool

face_subset(face_index)

Get a copy of

property kind

Return the type of visual data stored

Returns:

kind – What type of visuals are defined

Return type:

str

to_color()

Convert textured visuals to a ColorVisuals with vertex color calculated from texture.

Returns:

vis – Contains vertex color from texture

Return type:

trimesh.visuals.ColorVisuals

update_faces(mask)

Apply a mask to remove or duplicate face properties, not applicable to texture visuals.

update_vertices(mask)

Apply a mask to remove or duplicate vertex properties.

Parameters:

mask ((len(vertices),) bool or (n,) int) – Mask which can be used like: vertex_attribute[mask]

property uv

Get the stored UV coordinates.

Returns:

uv – Pixel position per-vertex.

Return type:

(n, 2) float or None

trimesh.visual.create_visual(**kwargs)

Create Visuals object from keyword arguments.

Parameters:
  • face_colors ((n, 3|4) uint8) – Face colors

  • vertex_colors ((n, 3|4) uint8) – Vertex colors

  • mesh (trimesh.Trimesh) – Mesh object

Returns:

visuals – Visual object created from arguments

Return type:

ColorVisuals

trimesh.visual.interpolate(values: ~collections.abc.Buffer | ~numpy._typing._array_like._SupportsArray[~numpy.dtype[~typing.Any]] | ~numpy._typing._nested_sequence._NestedSequence[~numpy._typing._array_like._SupportsArray[~numpy.dtype[~typing.Any]]] | bool | int | float | complex | str | bytes | ~numpy._typing._nested_sequence._NestedSequence[bool | int | float | complex | str | bytes], color_map: str | None = None, dtype: ~numpy.dtype[~typing.Any] | None | type[~typing.Any] | ~numpy._typing._dtype_like._SupportsDType[~numpy.dtype[~typing.Any]] | str | tuple[~typing.Any, int] | tuple[~typing.Any, ~typing.SupportsIndex | ~collections.abc.Sequence[~typing.SupportsIndex]] | list[~typing.Any] | ~numpy._typing._dtype_like._DTypeDict | tuple[~typing.Any, ~typing.Any] = <class 'numpy.uint8'>) ndarray[tuple[int, ...], dtype[_ScalarType_co]]

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.linear_color_map(values: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes], color_range: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes] | None = None) ndarray[tuple[int, ...], dtype[uint8]]

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.random_color(dtype: ~numpy.dtype[~typing.Any] | None | type[~typing.Any] | ~numpy._typing._dtype_like._SupportsDType[~numpy.dtype[~typing.Any]] | str | tuple[~typing.Any, int] | tuple[~typing.Any, ~typing.SupportsIndex | ~collections.abc.Sequence[~typing.SupportsIndex]] | list[~typing.Any] | ~numpy._typing._dtype_like._DTypeDict | tuple[~typing.Any, ~typing.Any] = <class 'numpy.uint8'>, count: int | ~numpy.integer | ~numpy.unsignedinteger | None = None) ndarray[tuple[int, ...], dtype[_ScalarType_co]]

Return a random RGB color using datatype specified.

Parameters:
  • dtype – Color type of result.

  • count – If passed return (count, 4) colors instead of a single (4,) color.

Returns:

color – Random color or colors that look “OK”

Return type:

(4,) or (count, 4)

trimesh.visual.to_rgba(colors: ~collections.abc.Buffer | ~numpy._typing._array_like._SupportsArray[~numpy.dtype[~typing.Any]] | ~numpy._typing._nested_sequence._NestedSequence[~numpy._typing._array_like._SupportsArray[~numpy.dtype[~typing.Any]]] | bool | int | float | complex | str | bytes | ~numpy._typing._nested_sequence._NestedSequence[bool | int | float | complex | str | bytes] | None | tuple, dtype: ~numpy.dtype[~typing.Any] | None | type[~typing.Any] | ~numpy._typing._dtype_like._SupportsDType[~numpy.dtype[~typing.Any]] | str | tuple[~typing.Any, int] | tuple[~typing.Any, ~typing.SupportsIndex | ~collections.abc.Sequence[~typing.SupportsIndex]] | list[~typing.Any] | ~numpy._typing._dtype_like._DTypeDict | tuple[~typing.Any, ~typing.Any] = <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.uv_to_color(uv, image) ndarray[tuple[int, ...], dtype[uint8]]

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.uv_to_interpolated_color(uv: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes], image) ndarray[tuple[int, ...], dtype[uint8]]

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