trimesh.visual.material module

material.py

Store visual materials as objects.

class trimesh.visual.material.Material(*args, **kwargs)

Bases: ABC

__init__(*args, **kwargs)
copy()
abstract property main_color

The “average” color of this material.

Returns:

color – Average color of this material.

Return type:

(4,) uint8

property name
class trimesh.visual.material.MultiMaterial(materials=None, **kwargs)

Bases: Material

__init__(materials=None, **kwargs)

Wrapper for a list of Materials.

Parameters:

materials (Optional[List[Material]]) – List of materials with which the container to be initialized.

add(material)

Adds new material to the container.

Parameters:

material (Material) – The material to be added.

get(idx)

Get material by index.

Parameters:

idx (int) – Index of the material to be retrieved.

Return type:

The material on the given index.

property main_color

The “average” color of this material.

Returns:

color – Average color of this material.

Return type:

(4,) uint8

to_pbr()

TODO : IMPLEMENT

class trimesh.visual.material.PBRMaterial(name=None, emissiveFactor=None, emissiveTexture=None, baseColorFactor=None, metallicFactor=None, roughnessFactor=None, normalTexture=None, occlusionTexture=None, baseColorTexture=None, metallicRoughnessTexture=None, doubleSided=False, alphaMode=None, alphaCutoff=None, **kwargs)

Bases: Material

Create a material for physically based rendering as specified by GLTF 2.0: https://git.io/fhkPZ

Parameters with Texture in them must be PIL.Image objects

__init__(name=None, emissiveFactor=None, emissiveTexture=None, baseColorFactor=None, metallicFactor=None, roughnessFactor=None, normalTexture=None, occlusionTexture=None, baseColorTexture=None, metallicRoughnessTexture=None, doubleSided=False, alphaMode=None, alphaCutoff=None, **kwargs)
property alphaCutoff

Specifies the cutoff threshold when in MASK alpha mode. If the alpha value is greater than or equal to this value then it is rendered as fully opaque, otherwise, it is rendered as fully transparent. A value greater than 1.0 will render the entire material as fully transparent. This value MUST be ignored for other alpha modes. When alphaMode is not defined, this value MUST NOT be defined.

Returns:

alphaCutoff – Value of cutoff.

Return type:

float

property alphaMode

The material alpha rendering mode enumeration specifying the interpretation of the alpha value of the base color.

Returns:

alphaMode – One of ‘OPAQUE’, ‘MASK’, ‘BLEND’

Return type:

str

property baseColorFactor

The factors for the base color of the material. This value defines linear multipliers for the sampled texels of the base color texture.

Returns:

color – RGBA color

Return type:

(4,) uint8

property baseColorTexture

The base color texture image.

Returns:

image – Color texture.

Return type:

PIL.Image

copy()
property doubleSided

Specifies whether the material is double sided.

Returns:

doubleSided – Specifies whether the material is double sided.

Return type:

bool

property emissiveFactor

The factors for the emissive color of the material. This value defines linear multipliers for the sampled texels of the emissive texture.

Returns:

emissiveFactor – Ech element in the array MUST be greater than or equal to 0 and less than or equal to 1.

Return type:

(3,) float

property emissiveTexture

The emissive texture.

Returns:

image – Emissive texture.

Return type:

PIL.Image

property main_color

The “average” color of this material.

Returns:

color – Average color of this material.

Return type:

(4,) uint8

property metallicFactor

The factor for the metalness of the material. This value defines a linear multiplier for the sampled metalness values of the metallic-roughness texture.

Returns:

metallicFactor – How metally is the material

Return type:

float

property metallicRoughnessTexture

The metallic-roughness texture.

Returns:

image – Metallic-roughness texture.

Return type:

PIL.Image

property name
property normalTexture

The normal map texture.

Returns:

image – Normal texture.

Return type:

PIL.Image

property occlusionTexture

The occlusion texture.

Returns:

image – Occlusion texture.

Return type:

PIL.Image

property roughnessFactor

The factor for the roughness of the material. This value defines a linear multiplier for the sampled roughness values of the metallic-roughness texture.

Returns:

roughnessFactor – Roughness of material.

Return type:

float

to_color(uv)

Get the rough color at a list of specified UV coordinates.

Parameters:

uv ((n, 2) float) – UV coordinates on the material

Return type:

colors

to_simple()

Get a copy of the current PBR material as a simple material.

Returns:

simple – Contains material information in a simple manner

Return type:

SimpleMaterial

class trimesh.visual.material.SimpleMaterial(image=None, diffuse=None, ambient=None, specular=None, glossiness=None, **kwargs)

Bases: Material

Hold a single image texture.

__init__(image=None, diffuse=None, ambient=None, specular=None, glossiness=None, **kwargs)
property glossiness
property main_color

Return the most prominent color.

to_color(uv)
to_obj(name=None)

Convert the current material to an OBJ format material.

Parameters:

name (str or None) – Name to apply to the material

Returns:

  • tex_name (str) – Name of material

  • mtl_name (str) – Name of mtl file in files

  • files (dict) – Data as {file name : bytes}

to_pbr()

Convert the current simple material to a PBR material.

Returns:

pbr – Contains material information in PBR format.

Return type:

PBRMaterial

trimesh.visual.material.color_image(color: ndarray[Any, dtype[uint8]] | None = None)

Generate an image with one color.

Parameters:

color – Optional uint8 color

Returns:

A (2, 2) RGBA image with the specified color.

Return type:

image

trimesh.visual.material.empty_material(color: ndarray[Any, dtype[uint8]] | None = None) SimpleMaterial

Return an empty material set to a single color

Parameters:

color (None or (3,) uint8) – RGB color

Returns:

material – Image is a a four pixel RGB

Return type:

SimpleMaterial

trimesh.visual.material.pack(materials, uvs, deduplicate=True, padding: int = 2, max_tex_size_individual=8192, max_tex_size_fused=8192)

Pack multiple materials with texture into a single material.

UV coordinates outside of the 0.0-1.0 range will be coerced into this range using a “wrap” behavior (i.e. modulus).

Alpha blending and backface culling settings are not supported! Returns a material with alpha values set, but alpha blending disabled.

Parameters:
  • materials ((n,) Material) – List of multiple materials

  • uvs ((n, m, 2) float) – Original UV coordinates

  • padding (int) – Number of pixels to pad each image with.

  • max_tex_size_individual (int) – Maximum size of each individual texture.

  • max_tex_size_fused (int | None) – Maximum size of the combined texture. Individual texture size will be reduced to fit. Set to None to allow infinite size.

Returns:

  • material (SimpleMaterial) – Combined material.

  • uv ((p, 2) float) – Combined UV coordinates in the 0.0-1.0 range.