trimesh.resolvers

resolvers.py

Provides a common interface to load assets referenced by name like MTL files, texture images, etc. Assets can be from ZIP archives, web assets, or a local file path.

class trimesh.resolvers.FilePathResolver(source)

Bases: Resolver

Resolve files from a source path on the file system.

__init__(source)

Resolve files based on a source path.

Parameters:

source (str) – File path where mesh was loaded from

get(name)

Get an asset.

Parameters:

name (str) – Name of the asset

Returns:

data – Loaded data from asset

Return type:

bytes

keys()

List all files available to be loaded.

Yields:

name (str) – Name of a file which can be accessed.

namespaced(namespace)

Return a resolver which changes the root of the resolver by an added namespace.

Parameters:

namespace (str) – Probably a subdirectory

Returns:

resolver – Resolver with root directory changed.

Return type:

FilePathResolver

write(name, data)

Write an asset to a file path.

Parameters:
  • name (str) – Name of the file to write

  • data (str or bytes) – Data to write to the file

class trimesh.resolvers.GithubResolver(repo, branch=None, commit=None, save=None)

Bases: Resolver

__init__(repo, branch=None, commit=None, save=None)

Get files from a remote Github repository by downloading a zip file with the entire branch or a specific commit.

Parameters:
  • repo (str) – In the format of owner/repo

  • branch (str) – The remote branch you want to get files from.

  • commit (str) – The full commit hash: pass either this OR branch.

  • save (None or str) – A path if you want to save results locally.

get(key)
keys()

List the available files in the repository.

Returns:

keys – Keys available to the resolved.

Return type:

iterable

namespaced(namespace)

Return a “sub-resolver” with a root namespace.

Parameters:

namespace (str) – The root of the key to clip off, i.e. if this resolver has key a/b/c you can get ‘a/b/c’ with resolver.namespaced(‘a/b’).get(‘c’)

Returns:

resolver – Namespaced resolver.

Return type:

Resolver

write(name, data)
property zipped
  • opened zip file

  • locally saved zip file

  • retrieve zip file and saved

class trimesh.resolvers.Resolver(*args, **kwargs)

Bases: ABC

The base class for resolvers.

abstract __init__(*args, **kwargs)
abstract get(key)
abstract namespaced(namespace)
abstract write(name, data)
class trimesh.resolvers.WebResolver(url)

Bases: Resolver

Resolve assets from a remote URL.

__init__(url)

Resolve assets from a base URL.

Parameters:

url (str) – Location where a mesh was stored or directory where mesh was stored

get(name)

Get a resource from the remote site.

Parameters:

name (str) – Asset name, i.e. ‘quadknot.obj.mtl’

namespaced(namespace)

Return a namespaced version of current resolver.

Parameters:

namespace (str) – URL fragment

Returns:

resolver – With sub-url: https://example.com/{namespace}

Return type:

WebResolver

write(key, value)
class trimesh.resolvers.ZipResolver(archive=None, namespace=None)

Bases: Resolver

Resolve files inside a ZIP archive.

__init__(archive=None, namespace=None)

Resolve files inside a ZIP archive as loaded by trimesh.util.decompress

Parameters:
  • archive (dict) – Contains resources as file object

  • namespace (None or str) – If passed will only show keys that start with this value and this substring must be removed for any get calls.

export()

Export the contents of the current archive as a ZIP file.

Returns:

compressed – Compressed data in ZIP format.

Return type:

bytes

get(name)

Get an asset from the ZIP archive.

Parameters:

name (str) – Name of the asset

Returns:

data – Loaded data from asset

Return type:

bytes

keys()

Get the available keys in the current archive.

Returns:

keys – Keys in the current archive.

Return type:

iterable

namespaced(namespace)

Return a “sub-resolver” with a root namespace.

Parameters:

namespace (str) – The root of the key to clip off, i.e. if this resolver has key a/b/c you can get ‘a/b/c’ with resolver.namespaced(‘a/b’).get(‘c’)

Returns:

resolver – Namespaced resolver.

Return type:

Resolver

write(key, value)

Store a value in the current archive.

Parameters:
  • key (hashable) – Key to store data under.

  • value (str, bytes, file-like) – Value to store.

trimesh.resolvers.nearby_names(name, namespace=None)

Try to find nearby variants of a specified name.

Parameters:

name (str) – Initial name.

Yields:

nearby (str) – Name that is a lightly permutated version of the initial name.