trimesh.exchange.gltf.extensions

gltf_extensions.py

Extension registry for glTF import/export with scope-based handlers. Each scope has a TypedDict defining the context passed to handlers.

class trimesh.exchange.gltf.extensions.MaterialContext

Bases: TypedDict

Context for material scope handlers.

data: dict[str, Any]
images: list
parse_textures: Callable[[...], dict[str, Any]]
class trimesh.exchange.gltf.extensions.PrimitiveContext

Bases: TypedDict

Context for primitive scope handlers (post-load).

accessors: list
data: dict[str, Any]
mesh_kwargs: dict
primitive: dict
class trimesh.exchange.gltf.extensions.PrimitiveExportContext

Bases: TypedDict

Context for primitive_export scope handlers (during export).

buffer_items: OrderedDict
include_normals: bool
mesh: Any
name: str
primitive: dict
tree: dict
class trimesh.exchange.gltf.extensions.PrimitivePreprocessContext

Bases: TypedDict

Context for primitive_preprocess scope handlers (pre-load).

accessors: list
data: dict[str, Any]
primitive: dict
views: list
class trimesh.exchange.gltf.extensions.TextureSourceContext

Bases: TypedDict

Context for texture_source scope handlers.

data: dict[str, Any]
trimesh.exchange.gltf.extensions.handle_extensions(*, extensions: dict[str, Any] | None, scope: Literal['material', 'texture_source', 'primitive', 'primitive_preprocess', 'primitive_export'], **kwargs) Any

Process extensions dict for a given scope, calling registered handlers.

Parameters:
  • extensions – The “extensions” dict from a glTF element, or None.

  • scope – Handler scope to invoke.

  • **kwargs

    Scope-specific arguments that will be combined with extension data into a typed context dict. Required kwargs by scope:

    • material: parse_textures, images

    • texture_source: (none)

    • primitive: primitive, mesh_kwargs, accessors

    • primitive_preprocess: primitive, accessors, views

    • primitive_export: mesh, name, tree, buffer_items, primitive, include_normals

Returns:

Dict of {extension_name: result} for most scopes. For scopes ending in “_source”, returns first non-None result. For “primitive” scope, automatically merges results into mesh_kwargs.

Return type:

results

trimesh.exchange.gltf.extensions.register_handler(name: str, scope: Literal['material', 'texture_source', 'primitive', 'primitive_preprocess', 'primitive_export']) Callable[[Callable[[Any], Any]], Callable[[Any], Any]]

Decorator to register a handler for a glTF extension.

Parameters:
  • name – Extension name, e.g. “KHR_materials_pbrSpecularGlossiness”.

  • scope – Handler scope, e.g. “material”, “texture_source”, “primitive”.

Returns:

Function that registers the handler and returns it unchanged.

Return type:

decorator

Example

>>> @register_handler("MY_extension", scope="material")
... def my_handler(context: MaterialContext) -> Optional[Dict]:
...     data = context["data"]
...     images = context["images"]
...     return {"baseColorFactor": [1, 0, 0, 1]}