trimesh.exchange package

Submodules

trimesh.exchange.assimp module

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

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: file object, or str file_type: str, file extension (aka ‘stl’)

Returns:
meshes: (n,) list of dicts, which contain kwargs for Trimesh constructor
trimesh.exchange.assimp.load_pyassimp(file_obj, file_type=None, resolver=None)

Use the pyassimp library to load a mesh, from a file object and type, or filename (if file_obj is a string)

file_obj: file object, or str of file path file_type: str, file extension (aka ‘stl’)

Returns:
meshes: (n,) list of dicts, which contain kwargs for Trimesh constructor

trimesh.exchange.export module

trimesh.exchange.export.export_collada(mesh, digits=8)

Export a mesh as a COLLADA file.

Parameters:
mesh : Trimesh object

Mesh to be exported

digits : int

Number of ASCII digits to include for floating point variables

Returns:
dae : str

Mesh as a COLLADA file

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

Export a mesh to a dict

Parameters:
mesh : Trimesh object

Mesh to be exported

encoding : str, 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_obj: a filename string or a file-like object file_type: str representing file type (eg: ‘stl’) process: boolean flag, whether to process the mesh on load

Returns: mesh: a single Trimesh object, or a list of Trimesh objects,

depending on the file format.
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:
mesh : Trimesh object
digits : int

number of digits to include on floats

Returns:
export : str

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

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

Parameters:
scene: trimesh.Scene

Input geometry

Returns:
exported : bytes

Exported result in GLB 2.0

trimesh.exchange.gltf.export_gltf(scene)

Export a scene object as a GLTF directory.

This has the advantage of putting each mesh into a separate file (buffer) as opposed to one large file.

Parameters:
scene: trimesh.Scene object
Returns:
export: dict, {file name : file data}
trimesh.exchange.gltf.load_glb(file_obj, **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

Returns:
kwargs : dict

Kwargs to instantiate a trimesh.Scene

trimesh.exchange.load module

trimesh.exchange.load.available_formats()

Get a list of all available loaders

Returns:
loaders : list

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_obj : str, 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’)
resolver : trimesh.visual.Resolver
Object to load referenced assets like materials and textures
kwargs : **
Passed to geometry __init__
Returns:
geometry : Trimesh, 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_obj : open file-like object

Containing compressed data

file_type : str

Type of the archive file

mixed : bool

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

Returns:
scene : trimesh.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:
url : string

URL containing mesh file

**kwargs : passed to load
trimesh.exchange.load.mesh_formats()

Get a list of mesh formats

Returns:
loaders : list

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:
blob : bytes

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

Returns:
loaded : dict

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_obj : file object

Contains an OFF file

Returns:
loaded : dict

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_obj : open file- like object
Returns:
loaded : dict
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:
mesh : Trimesh object
Returns:
data : str or bytes

DRC file bytes

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

Export a mesh in the PLY format.

Parameters:
mesh : Trimesh object
encoding : [‘ascii’|’binary_little_endian’]
vertex_normal : include vertex normals
Returns:
export : bytes of result
trimesh.exchange.ply.load_draco(file_obj, **kwargs)

Load a mesh from Google’s Draco format.

Parameters:
file_obj : file- like object

Contains data

Returns:
kwargs : dict

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_obj : an open file- like object
Source data, ASCII or binary PLY
resolver : trimesh.visual.resolvers.Resolver
Object which can resolve assets
fix_texture : bool
If True, will re- index vertices and faces so vertices with different UV coordinates are disconnected.
Returns:
mesh_kwargs : dict

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_obj : open file object

Positioned at the start of the file

Returns:
elements : collections.OrderedDict

Fields and data types populated

is_ascii : bool

Whether the data is ASCII or binary

image_name : None 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:
export : str

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:
mesh : Trimesh 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_obj : file object

Containing a wavefront file

resolver : trimesh.visual.Resolver or None

For loading referenced files, like MTL or textures

kwargs : **

Passed to trimesh.Trimesh.__init__

Returns:
loaded : dict

kwargs for Trimesh constructor

trimesh.exchange.wavefront.parse_mtl(mtl)

Parse a loaded MTL file.

Parameters:
mtl : str or bytes

Data from an MTL file

Returns:
mtllibs : list 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_obj : file object

open, containing 3DXML

Returns:
geometries : list of dict

kwargs for Trimesh constructor

graph : list of dict

kwargs for Scene.graph.update

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

Load a 3D XAML file.

Parameters:
file_obj : file object

Open, containing XAML file

Returns:
result : dict

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:
element : etree element

Module contents