capytaine.meshes.io module

capytaine.meshes.io.load_mesh(mesh_to_be_loaded, file_format=None, *, backend=None) Mesh | ReflectionSymmetricMesh[source]

Load a mesh from a file or file-like object.

This function can load mesh data from many file formats. It supports file paths and file-like objects.

Parameters:
  • mesh_to_be_loaded (str, pathlib.Path, or file-like object) –

    Can be either:
    • a path to a mesh file

    • a file-like object with mesh file data

    • a mesh object from one of the compatible external libraries (meshio, trimesh)

  • file_format (str, optional) – Format hint used only when loading from file-like objects, since the filename extension is unavailable. Valid values include "stl", "obj", "hst", "pnl", etc. Can be automatically inferred from file extension when a path is provided as first argument.

Returns:

A Mesh object, or a ReflectionSymmetricMesh if symmetry information is present in the file format.

Return type:

Mesh or ReflectionSymmetricMesh

Raises:

ValueError – If file_format is not provided when loading from a file-like object, or if an unsupported format is encountered.

Examples

Load a mesh from a file path.

>>> load_mesh("model.stl")

Load from a gzip-compressed file.

>>> import gzip
>>> with gzip.open("model.stl.gz") as handler:
...     load_mesh(handler, file_format="stl")

Load from any file-like object.

>>> with open("model.obj", "rb") as handler:
...     load_mesh(handler, file_format="obj")