trimesh.exchange.gltf module

gltf.py

Provides GLTF 2.0 exports of trimesh.Trimesh objects as GL_TRIANGLES, and trimesh.Path2D/Path3D as GL_LINES

trimesh.exchange.gltf.export_glb(scene, include_normals=None, unitize_normals=True, tree_postprocessor=None, buffer_postprocessor=None, extension_webp=False)

Export a scene as a binary GLTF (GLB) file.

Parameters:
  • scene (trimesh.Scene) – Input geometry

  • extras (JSON serializable) – Will be stored in the extras field.

  • include_normals (bool) – Include vertex normals in output file?

  • tree_postprocessor (func) – Custom function to (in-place) post-process the tree before exporting.

  • extension_webp (bool) – Export textures as webP using EXT_texture_webp extension.

Returns:

exported – Exported result in GLB 2.0

Return type:

bytes

trimesh.exchange.gltf.export_gltf(scene, include_normals=None, merge_buffers=False, unitize_normals=True, tree_postprocessor=None, embed_buffers=False, extension_webp=False)

Export a scene object as a GLTF directory.

This puts each mesh into a separate file (i.e. a buffer) as opposed to one larger file.

Parameters:
  • scene (trimesh.Scene) – Scene to be exported

  • include_normals (None or bool) – Include vertex normals

  • merge_buffers (bool) – Merge buffers into one blob.

  • unitize_normals – GLTF requires unit normals, however sometimes people want to include non-unit normals for shading reasons.

  • resolver (trimesh.resolvers.Resolver) – If passed will use to write each file.

  • tree_postprocesser (None or callable) – Run this on the header tree before exiting.

  • embed_buffers (bool) – Embed the buffer into JSON file as a base64 string in the URI

  • extension_webp (bool) – Export textures as webP (using glTF’s EXT_texture_webp extension).

Returns:

export – Format: {file name : file data}

Return type:

dict

trimesh.exchange.gltf.get_schema()

Get a copy of the GLTF 2.0 schema with references resolved.

Returns:

schema – A copy of the GLTF 2.0 schema without external references.

Return type:

dict

trimesh.exchange.gltf.load_glb(file_obj: IO | BytesIO | StringIO | BinaryIO | TextIO, resolver: Resolver | Mapping | None = None, ignore_broken: bool = False, merge_primitives: bool = False, skip_materials: bool = False, **mesh_kwargs)

Load a GLTF file in the binary GLB format into a trimesh.Scene.

Implemented from specification: https://github.com/KhronosGroup/glTF/tree/master/specification/2.0

Parameters:
  • file_obj (file- like object) – Containing GLB data

  • resolver (trimesh.visual.Resolver) – Object which can be used to load other files by name

  • ignore_broken (bool) – If there is a mesh we can’t load and this is True don’t raise an exception but return a partial result

  • merge_primitives (bool) – If True, each GLTF ‘mesh’ will correspond to a single Trimesh object.

  • skip_materials (bool) – If true, will not load materials (if present).

Returns:

kwargs – Kwargs to instantiate a trimesh.Scene

Return type:

dict

trimesh.exchange.gltf.load_gltf(file_obj: IO | BytesIO | StringIO | BinaryIO | TextIO | None = None, resolver: Resolver | Mapping | None = None, ignore_broken: bool = False, merge_primitives: bool = False, skip_materials: bool = False, **mesh_kwargs)

Load a GLTF file, which consists of a directory structure with multiple files.

Parameters:
  • file_obj (None or file-like) – Object containing header JSON, or None

  • resolver (trimesh.visual.Resolver) – Object which can be used to load other files by name

  • ignore_broken (bool) – If there is a mesh we can’t load and this is True don’t raise an exception but return a partial result

  • merge_primitives (bool) – If True, each GLTF ‘mesh’ will correspond to a single Trimesh object

  • skip_materials (bool) – If true, will not load materials (if present).

  • **mesh_kwargs (dict) – Passed to mesh constructor

Returns:

kwargs – Arguments to create scene

Return type:

dict

trimesh.exchange.gltf.validate(header)

Validate a GLTF 2.0 header against the schema.

Returns result from: jsonschema.validate(header, schema=get_schema())

Parameters:

header (dict) – Populated GLTF 2.0 header

:raises err : jsonschema.exceptions.ValidationError: If the tree is an invalid GLTF2.0 header