trimesh.exchange.gltf

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, tree_postprocessor=None)

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.

Returns

exported – Exported result in GLB 2.0

Return type

bytes

trimesh.exchange.gltf.export_gltf(scene, include_normals=None, merge_buffers=False, tree_postprocessor=None)

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.

  • 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.

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, resolver=None, ignore_broken=False, merge_primitives=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

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

Returns

kwargs – Kwargs to instantiate a trimesh.Scene

Return type

dict

trimesh.exchange.gltf.load_gltf(file_obj=None, resolver=None, ignore_broken=False, merge_primitives=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

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

Returns

kwargs – Arguments to create scene

Return type

dict

trimesh.exchange.gltf.specular_to_pbr(specularFactor=None, glossinessFactor=None, specularGlossinessTexture=None, diffuseTexture=None, diffuseFactor=None, **kwargs)

TODO : implement specular to PBR as done in Javascript here: https://github.com/KhronosGroup/glTF/blob/89427b26fcac884385a2e6d5803d917ab5d1b04f/extensions/2.0/Archived/KHR_materials_pbrSpecularGlossiness/examples/convert-between-workflows-bjs/js/babylon.pbrUtilities.js#L33-L64

Convert the KHR_materials_pbrSpecularGlossiness to a metallicRoughness visual.

Parameters

...

Returns

  • kwargs (dict) – Constructor args for a PBRMaterial object.

  • if specularFactor is None – oneMinus = 1

  • else – oneMinus = 1 - max(specularFactor)

  • dielectricSpecular = np.array([0.04, 0.04, 0.04])

trimesh.exchange.gltf.unique_name(start, contains)

Deterministically generate a unique name not contained in a dict. Will create names of the form “start_10” and increment accordingly.

Parameters
  • start (str) – Initial guess for name

  • contains (dict, set, or list) – Bundle of existing names we cannot use.

Returns

unique – A name that is not contained in contains

Return type

str

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