trimesh.exchange package

Submodules

trimesh.exchange.assimp module

trimesh.exchange.assimp.load_cyassimp(file_obj, file_type=None, resolver=None, **kwargs)

Load a file using the cyassimp bindings.

The easiest way to install these is with conda: conda install -c menpo/label/master cyassimp

file_obj: str, or file object

File path or object containing mesh data

file_typestr

File extension, aka ‘stl’

resolvertrimesh.visual.resolvers.Resolver

Used to load referenced data (like texture files)

kwargsdict

Passed through to mesh constructor

Returns
meshes(n,) list of dict

Contain kwargs for Trimesh constructor

trimesh.exchange.assimp.load_pyassimp(file_obj, file_type=None, resolver=None, **kwargs)

Use the pyassimp library to load a mesh from a file object and type or file name if file_obj is a string

file_obj: str, or file object

File path or object containing mesh data

file_typestr

File extension, aka ‘stl’

resolvertrimesh.visual.resolvers.Resolver

Used to load referenced data (like texture files)

kwargsdict

Passed through to mesh constructor

Returns
scenetrimesh.Scene

Native trimesh copy of assimp scene

trimesh.exchange.assimp.loader(file_obj, file_type=None, resolver=None, **kwargs)

Use the pyassimp library to load a mesh from a file object and type or file name if file_obj is a string

file_obj: str, or file object

File path or object containing mesh data

file_typestr

File extension, aka ‘stl’

resolvertrimesh.visual.resolvers.Resolver

Used to load referenced data (like texture files)

kwargsdict

Passed through to mesh constructor

Returns
scenetrimesh.Scene

Native trimesh copy of assimp scene

trimesh.exchange.dae module

trimesh.exchange.dae.export_collada(mesh, **kwargs)

Export a mesh or a list of meshes as a COLLADA .dae file.

Parameters
mesh: Trimesh object or list of Trimesh objects

The mesh(es) to export.

Returns
export: str, string of COLLADA format output
trimesh.exchange.dae.load_collada(file_obj, resolver=None, **kwargs)

Load a COLLADA (.dae) file into a list of trimesh kwargs.

Parameters
file_objfile object

Containing a COLLADA file

resolvertrimesh.visual.Resolver or None

For loading referenced files, like texture images

kwargs**

Passed to trimesh.Trimesh.__init__

Returns
loadedlist of dict

kwargs for Trimesh constructor

trimesh.exchange.dae.load_zae(file_obj, resolver=None, **kwargs)

Load a ZAE file, which is just a zipped DAE file.

Parameters
file_objfile object

Contains ZAE data

resolvertrimesh.visual.Resolver

Resolver to load additional assets

kwargsdict

Passed to load_collada

Returns
loadeddict

Results of loading

trimesh.exchange.export module

trimesh.exchange.export.export_dict(mesh, encoding=None)

Export a mesh to a dict

Parameters
meshTrimesh object

Mesh to be exported

encodingstr, or None

‘base64’

trimesh.exchange.export.export_dict64(mesh)

Export a mesh as a dictionary, with data encoded to base64.

trimesh.exchange.export.export_json(mesh)
trimesh.exchange.export.export_mesh(mesh, file_obj, file_type=None, **kwargs)

Export a Trimesh object to a file- like object, or to a filename

file_objstr, file-like

Where should mesh be exported to

file_typestr or None

Represents file type (eg: ‘stl’)

Returns
exportedbytes or str

Result of exporter

trimesh.exchange.export.export_msgpack(mesh)
trimesh.exchange.export.export_off(mesh, digits=10)

Export a mesh as an OFF file, a simple text format

Parameters
meshtrimesh.Trimesh

Geometry to export

digitsint

Number of digits to include on floats

Returns
exportstr

OFF format output

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, extras=None, include_normals=False)

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

Parameters
scene: trimesh.Scene

Input geometry

extrasJSON serializable

Will be stored in the extras field

include_normalsbool

Include vertex normals in output file?

Returns
exportedbytes

Exported result in GLB 2.0

trimesh.exchange.gltf.export_gltf(scene, extras=None, include_normals=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
scenetrimesh.Scene

Scene to be exported

Returns
exportdict

Format: {file name : file data}

trimesh.exchange.gltf.load_glb(file_obj, resolver=None, **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_objfile- like object

Containing GLB data

Returns
kwargsdict

Kwargs to instantiate a trimesh.Scene

trimesh.exchange.gltf.load_gltf(file_obj=None, resolver=None, **mesh_kwargs)

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

Parameters
file_objNone or file-like

Object containing header JSON, or None

resolvertrimesh.visual.Resolver

Object which can be used to load other files by name

**mesh_kwargsdict

Passed to mesh constructor

Returns
kwargsdict

Arguments to create scene

trimesh.exchange.load module

trimesh.exchange.load.available_formats()

Get a list of all available loaders

Returns
loaderslist

Extensions of available loaders i.e. ‘stl’, ‘ply’, ‘dxf’, etc.

trimesh.exchange.load.load(file_obj, file_type=None, resolver=None, **kwargs)

Load a mesh or vectorized path into objects: Trimesh, Path2D, Path3D, Scene

file_objstr, or file- like object

The source of the data to be loadeded

file_type: str

What kind of file type do we have (eg: ‘stl’)

resolvertrimesh.visual.Resolver

Object to load referenced assets like materials and textures

kwargs**

Passed to geometry __init__

Returns
geometryTrimesh, Path2D, Path3D, Scene

Loaded geometry as trimesh classes

trimesh.exchange.load.load_compressed(file_obj, file_type=None, resolver=None, mixed=False, **kwargs)

Given a compressed archive load all the geometry that we can from it.

Parameters
file_objopen file-like object

Containing compressed data

file_typestr

Type of the archive file

mixedbool

If False, for archives containing both 2D and 3D data will only load the 3D data into the Scene.

Returns
scenetrimesh.Scene

Geometry loaded in to a Scene object

trimesh.exchange.load.load_kwargs(*args, **kwargs)

Load geometry from a properly formatted dict or kwargs

trimesh.exchange.load.load_remote(url, **kwargs)

Load a mesh at a remote URL into a local trimesh object.

This must be called explicitly rather than automatically from trimesh.load to ensure users don’t accidentally make network requests.

Parameters
urlstring

URL containing mesh file

**kwargspassed to load
trimesh.exchange.load.mesh_formats()

Get a list of mesh formats

Returns
loaderslist

Extensions of available mesh loaders i.e. ‘stl’, ‘ply’, etc.

trimesh.exchange.load.parse_file_args(file_obj, file_type, resolver=None, **kwargs)

Given a file_obj and a file_type try to turn them into a file-like object and a lowercase string of file type.

Parameters
file_obj: str: if string represents a file path, returns

file_obj: an ‘rb’ opened file object of the path file_type: the extension from the file path

str: if string is NOT a path, but has JSON-like special characters

file_obj: the same string passed as file_obj file_type: set to ‘json’

Returns
file_obj: loadable object
file_type: str, lower case of the type of file (eg ‘stl’, ‘dae’, etc)
metadata: dict, any metadata
opened: bool, did we open the file or not

trimesh.exchange.misc module

trimesh.exchange.misc.load_dict(data, **kwargs)

Load multiple input types into kwargs for a Trimesh constructor. Tries to extract keys: ‘faces’ ‘vertices’ ‘face_normals’ ‘vertex_normals’

Parameters
data: accepts multiple forms

-dict: has keys for vertices and faces as (n,3) numpy arrays -dict: has keys for vertices/faces (n,3) arrays encoded as dicts/base64

with trimesh.util.array_to_encoded/trimesh.util.encoded_to_array

-str: json blob as dict with either straight array or base64 values -file object: json blob of dict

file_type: not used
Returns
loaded: dict with keys

-vertices: (n,3) float -faces: (n,3) int -face_normals: (n,3) float (optional)

trimesh.exchange.misc.load_msgpack(blob, **kwargs)

Load a dict packed with msgpack into kwargs for a Trimesh constructor

Parameters
blobbytes

msgpack packed dict containing keys ‘vertices’ and ‘faces’

Returns
loadeddict

Keyword args for Trimesh constructor, aka mesh=trimesh.Trimesh(**loaded)

trimesh.exchange.misc.load_off(file_obj, **kwargs)

Load an OFF file into the kwargs for a Trimesh constructor

Parameters
file_objfile object

Contains an OFF file

Returns
loadeddict

kwargs for Trimesh constructor

trimesh.exchange.openctm module

trimesh.exchange.openctm.load_ctm(file_obj, file_type=None, **kwargs)

Load OpenCTM files from a file object.

Parameters
file_objopen file- like object
Returns
loadeddict
kwargs for a Trimesh constructor:
{vertices: (n,3) float, vertices

faces: (m,3) int, indexes of vertices}

trimesh.exchange.ply module

trimesh.exchange.ply.element_colors(element)

Given an element, try to extract RGBA color from properties and return them as an (n,3|4) array.

Parameters
element: dict, containing color keys
Returns
colors: (n,(3|4)
signal: float, estimate of range
trimesh.exchange.ply.elements_to_kwargs(elements, fix_texture, image)

Given an elements data structure, extract the keyword arguments that a Trimesh object constructor will expect.

Parameters
elements: OrderedDict object, with fields and data loaded
Returns
kwargs: dict, with keys for Trimesh constructor.

eg: mesh = trimesh.Trimesh(**kwargs)

trimesh.exchange.ply.export_draco(mesh)

Export a mesh using Google’s Draco compressed format.

Only works if draco_encoder is in your PATH: https://github.com/google/draco

Parameters
meshTrimesh object
Returns
datastr or bytes

DRC file bytes

trimesh.exchange.ply.export_ply(mesh, encoding='binary', vertex_normal=None)

Export a mesh in the PLY format.

Parameters
meshTrimesh object
encoding[‘ascii’|’binary_little_endian’]
vertex_normalinclude vertex normals
Returns
exportbytes of result
trimesh.exchange.ply.load_draco(file_obj, **kwargs)

Load a mesh from Google’s Draco format.

Parameters
file_objfile- like object

Contains data

Returns
kwargsdict

Keyword arguments to construct a Trimesh object

trimesh.exchange.ply.load_ply(file_obj, resolver=None, fix_texture=True, *args, **kwargs)

Load a PLY file from an open file object.

file_objan open file- like object

Source data, ASCII or binary PLY

resolvertrimesh.visual.resolvers.Resolver

Object which can resolve assets

fix_texturebool

If True, will re- index vertices and faces so vertices with different UV coordinates are disconnected.

Returns
mesh_kwargsdict

Data which can be passed to Trimesh constructor, eg: a = Trimesh(**mesh_kwargs)

trimesh.exchange.ply.parse_header(file_obj)

Read the ASCII header of a PLY file, and leave the file object at the position of the start of data but past the header.

Parameters
file_objopen file object

Positioned at the start of the file

Returns
elementscollections.OrderedDict

Fields and data types populated

is_asciibool

Whether the data is ASCII or binary

image_nameNone or str

File name of TextureFile

trimesh.exchange.ply.ply_ascii(elements, file_obj)

Load data from an ASCII PLY file into an existing elements data structure.

Parameters
elements: OrderedDict object, populated from the file header.

object will be modified to add data by this function.

file_obj: open file object, with current position at the start

of the data section (past the header)

trimesh.exchange.ply.ply_binary(elements, file_obj)

Load the data from a binary PLY file into the elements data structure.

Parameters
elements: OrderedDict object, populated from the file header.

object will be modified to add data by this function.

file_obj: open file object, with current position at the start

of the data section (past the header)

trimesh.exchange.stl module

exception trimesh.exchange.stl.HeaderError

Bases: Exception

trimesh.exchange.stl.export_stl(mesh)

Convert a Trimesh object into a binary STL file.

mesh: Trimesh object

Returns
export: bytes, representing mesh in binary STL form
trimesh.exchange.stl.export_stl_ascii(mesh)

Convert a Trimesh object into an ASCII STL file.

mesh : trimesh.Trimesh

Returns
exportstr

Mesh represented as an ASCII STL file

trimesh.exchange.stl.load_stl(file_obj, file_type=None, **kwargs)

Load an STL file from a file object.

Parameters
file_obj: open file- like object
file_type: not used
Returns
loaded: kwargs for a Trimesh constructor with keys:

vertices: (n,3) float, vertices faces: (m,3) int, indexes of vertices face_normals: (m,3) float, normal vector of each face

trimesh.exchange.stl.load_stl_ascii(file_obj)

Load an ASCII STL file from a file object.

Parameters
file_obj: open file- like object
Returns
loaded: kwargs for a Trimesh constructor with keys:

vertices: (n,3) float, vertices faces: (m,3) int, indexes of vertices face_normals: (m,3) float, normal vector of each face

trimesh.exchange.stl.load_stl_binary(file_obj)

Load a binary STL file from a file object.

Parameters
file_obj: open file- like object
Returns
loaded: kwargs for a Trimesh constructor with keys:

vertices: (n,3) float, vertices faces: (m,3) int, indexes of vertices face_normals: (m,3) float, normal vector of each face

trimesh.exchange.threemf module

trimesh.exchange.threemf.load_3MF(file_obj, postprocess=True, **kwargs)

Load a 3MF formatted file into a Trimesh scene.

Parameters
file_obj: file object
Returns
kwargs: dict, with keys ‘graph’, ‘geometry’, ‘base_frame’

trimesh.exchange.urdf module

trimesh.exchange.urdf.export_urdf(mesh, directory, scale=1.0, color=[0.75, 0.75, 0.75], **kwargs)

Convert a Trimesh object into a URDF package for physics simulation. This breaks the mesh into convex pieces and writes them to the same directory as the .urdf file.

mesh : Trimesh object directory : str

The directory path for the URDF package

Returns
meshTrimesh object

Multi-body mesh containing convex decomposition

trimesh.exchange.wavefront module

trimesh.exchange.wavefront.export_wavefront(mesh, include_normals=True, include_texture=True)

Export a mesh as a Wavefront OBJ file

Parameters
mesh: Trimesh object
Returns
export: str, string of OBJ format output
trimesh.exchange.wavefront.load_wavefront(file_obj, resolver=None, **kwargs)

Loads an ascii Wavefront OBJ file_obj into kwargs for the Trimesh constructor.

Vertices with the same position but different normals or uvs are split into multiple vertices.

Colors are discarded.

Parameters
file_objfile object

Containing a wavefront file

resolvertrimesh.visual.Resolver or None

For loading referenced files, like MTL or textures

kwargs**

Passed to trimesh.Trimesh.__init__

Returns
loadeddict

kwargs for Trimesh constructor

trimesh.exchange.wavefront.parse_mtl(mtl)

Parse a loaded MTL file.

Parameters
mtlstr or bytes

Data from an MTL file

Returns
mtllibslist of dict

Each dict has keys: newmtl, map_Kd, Kd

trimesh.exchange.xml_based module

trimesh.exchange.xml_based.load_3DXML(file_obj, *args, **kwargs)

Load a 3DXML scene into kwargs.

Parameters
file_objfile object

Open and containing 3DXML data

Returns
kwargsdict

Can be passed to trimesh.exchange.load.load_kwargs

trimesh.exchange.xml_based.load_XAML(file_obj, *args, **kwargs)

Load a 3D XAML file.

Parameters
file_objfile object

Open, containing XAML file

Returns
resultdict

kwargs for a trimesh constructor, including: vertices: (n,3) np.float64, points in space faces: (m,3) np.int64, indices of vertices face_colors: (m,4) np.uint8, RGBA colors vertex_normals: (n,3) np.float64, vertex normals

trimesh.exchange.xml_based.print_element(element)

Pretty- print an lxml.etree element.

Parameters
elementetree element

Module contents