trimesh.visual

visual

Handle visual properties for meshes, including color and texture

Classes

TextureVisuals([uv, material, image])

ColorVisuals([mesh, face_colors, vertex_colors])

Store color information about a mesh.

Functions

random_color([dtype])

Return a random RGB color using datatype specified.

to_rgba(colors[, dtype])

Convert a single or multiple RGB colors to RGBA colors.

create_visual(**kwargs)

Create Visuals object from keyword arguments.

interpolate(values[, color_map, dtype])

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

linear_color_map(values[, color_range])

Linearly interpolate between two colors.

uv_to_color(uv, image)

Get the color in a texture image.

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

Bases: trimesh.visual.base.Visuals

Store color information about a mesh.

Methods

concatenate(other, *args)

Concatenate two or more ColorVisuals objects into a single object.

copy()

Return a copy of the current ColorVisuals object.

crc()

A checksum for the current visual object and its parent mesh.

face_subset(face_index)

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

to_texture()

update_faces(mask)

Apply a mask to remove or duplicate face properties

update_vertices(mask)

Apply a mask to remove or duplicate vertex properties.

Attributes

defined

Are any colors defined for the current mesh.

face_colors

Colors defined for each face of a mesh.

kind

What color mode has been set.

main_color

What is the most commonly occurring color.

transparency

Does the current object contain any transparency.

vertex_colors

Return the colors for each vertex of a mesh

concatenate(other, *args)

Concatenate two or more ColorVisuals objects into a single object.

Parameters
  • other (ColorVisuals) – Object to append

  • *args (ColorVisuals objects) –

Returns

result – object and others in the order it was passed.

Return type

ColorVisuals object containing information from current

copy()

Return a copy of the current ColorVisuals object.

Returns

copied – Contains the same information as self

Return type

ColorVisuals

crc()

A checksum for the current visual object and its parent mesh.

Returns

crc – Checksum of data in visual object and its parent mesh

Return type

int

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

Return type

(len(mesh.faces), 4) uint8, RGBA color for each face

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

Bases: trimesh.visual.base.Visuals

Methods

concatenate(others)

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

copy()

Return a copy of the current TextureVisuals object.

crc()

Get a CRC of the stored data.

face_subset(face_index)

Get a copy of

to_color()

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

update_faces(mask)

Apply a mask to remove or duplicate face properties

update_vertices(mask)

Apply a mask to remove or duplicate vertex properties.

Attributes

defined

Check if any data is stored

kind

Return the type of visual data stored

uv

Get the stored UV coordinates.

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()

Return a copy of the current TextureVisuals object.

Returns

copied – Contains the same information in a new object

Return type

TextureVisuals

crc()

Get a CRC of the stored data.

Returns

crc – Hash of items in self._data

Return type

int

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

update_vertices(mask)

Apply a mask to remove or duplicate vertex properties.

property uv

Get the stored UV coordinates.

Returns

uv – Pixel position per- vertex

Return type

(n, 2) float

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, 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.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.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.to_rgba(colors, dtype=<class 'numpy.uint8'>)

Convert a single or multiple RGB colors to RGBA colors.

Parameters

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

Returns

colors – (4,) single RGBA color

Return type

(n, 4) list of RGBA colors

trimesh.visual.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) float