lagrange.io

Module Contents

Classes

FileEncoding

File encoding type

LoadOptions

Options used when loading a mesh or a scene. Note that not all options are supported for all backends or filetypes

SaveOptions

Options used when saving a mesh or a scene. Note that not all options are supported for all backends or filetypes

Functions

load_mesh(filename[, triangulate, load_normals, ...])

Load mesh from a file.

load_scene(…)

load_simple_scene(filename[, triangulate, search_path])

Load a simple scene from file.

mesh_to_string(mesh[, format, binary, exact_match, ...])

Convert a mesh to a binary string based on specified format.

save_mesh(filename, mesh[, binary, exact_match, ...])

Save mesh to file.

save_scene(…)

Save a scene. Supports gltf, glb, obj.

save_simple_scene(filename, scene[, binary])

Save a simple scene to file. Supports gltf, glb, obj.

scene_to_string(scene, format[, binary, exact_match, ...])

Convert a scene to a binary string based on specified format.

string_to_mesh(data[, triangulate])

Convert a binary string to a mesh.

string_to_scene(data[, triangulate])

Convert a binary string to a scene.

class lagrange.io.FileEncoding(*args, **kwds)

Bases: enum.Enum

File encoding type

Ascii = 1

ASCII text encoding

Binary = 0

Binary encoding

class lagrange.io.LoadOptions

Options used when loading a mesh or a scene. Note that not all options are supported for all backends or filetypes

property load_materials: bool

Load material ids as facet attribute

Return type:

bool

property load_normals: bool

Load vertex normals

Return type:

bool

property load_object_ids: bool

Load object ids as facet attribute

Return type:

bool

property load_tangents: bool

Load tangents and bitangents

Return type:

bool

property load_uvs: bool

Load texture coordinates

Return type:

bool

property load_vertex_colors: bool

Load vertex colors as vertex attribute

Return type:

bool

property load_weights: bool

Load skinning weights attributes (joints id and weight)

Return type:

bool

property search_path: pathlib.Path

Search path for related files, such as .mtl, .bin, or image textures. By default, searches the same folder as the provided filename

Return type:

pathlib.Path

property triangulate: bool

Triangulate any polygonal facet with > 3 vertices

Return type:

bool

class lagrange.io.SaveOptions

Options used when saving a mesh or a scene. Note that not all options are supported for all backends or filetypes

class AttributeConversionPolicy(*args, **kwds)

Bases: enum.Enum

Attribute conversion policy. Provides options to handle non-supported attributes when saving them

ConvertAsNeeded = 1

Convert attribute to supported attribute type when possible

ExactMatchOnly = 0

Ignore mismatched attributes and print a warning

class OutputAttributes(*args, **kwds)

Bases: enum.Enum

Which attributes to save with the mesh

All = 0

All attributes (default)

SelectedOnly = 1

Only attributes listed in selected_attributes

property attribute_conversion_policy: SaveOptions

The attribute conversion policy to use. While Lagrange SurfaceMesh supports vertex, facet, corner, edge and indexed attributes, many filetypes only support a subset of these attribute types

Return type:

SaveOptions

property embed_images: bool

Whether to embed images in the file (if supported by the filetype)

Return type:

bool

property encoding: FileEncoding

Whether to encode the file as plain text or binary. Some filetypes only support Ascii and will ignore this parameter

Return type:

FileEncoding

property export_materials: bool

Whether to export materials and textures.

Return type:

bool

property output_attributes: SaveOptions

Which attributes to save with the mesh

Return type:

SaveOptions

property selected_attributes: list[int]

Attributes to output, usage depends on output_attributes setting

Return type:

list[int]

lagrange.io.load_mesh(filename, triangulate=False, load_normals=True, load_tangents=True, load_uvs=True, load_weights=True, load_materials=True, load_vertex_colors=True, load_object_ids=True, load_images=True, stitch_vertices=False, search_path=...)

Load mesh from a file.

Parameters:
  • filename (str | os.PathLike) – The input file name.

  • triangulate (bool) – Whether to triangulate the mesh if it is not already triangulated. Defaults to False.

  • load_normals (bool) – Whether to load vertex normals from mesh if available. Defaults to True.

  • load_tangents (bool) – Whether to load tangents and bitangents from mesh if available. Defaults to True.

  • load_uvs (bool) – Whether to load texture coordinates from mesh if available. Defaults to True.

  • load_weights (bool) – Whether to load skinning weights attributes from mesh if available. Defaults to True.

  • load_materials (bool) – Whether to load material ids from mesh if available. Defaults to True.

  • load_vertex_colors (bool) – Whether to load vertex colors from mesh if available. Defaults to True.

  • load_object_id – Whether to load object ids from mesh if available. Defaults to True.

  • load_images (bool) – Whether to load external images if available. Defaults to True.

  • stitch_vertices (bool) – Whether to stitch boundary vertices based on position. Defaults to False.

  • search_path (str | os.PathLike) – Optional search path for external references (e.g. .mtl, .bin, etc.). Defaults to None.

  • load_object_ids (bool)

Return SurfaceMesh:

The mesh object extracted from the input string.

Return type:

lagrange.core.SurfaceMesh

lagrange.io.load_scene(filename: str | os.PathLike, options: LoadOptions = ...) lagrange.scene.Scene
lagrange.io.load_scene(filename: str | os.PathLike, triangulate: bool = False, load_normals: bool = True, load_tangents: bool = True, load_uvs: bool = True, load_weights: bool = True, load_materials: bool = True, load_vertex_colors: bool = True, load_object_ids: bool = True, load_images: bool = True, stitch_vertices: bool = False, search_path: str | os.PathLike = ...) lagrange.scene.Scene
lagrange.io.load_simple_scene(filename, triangulate=False, search_path=None)

Load a simple scene from file.

Parameters:
  • filename (str | os.PathLike) – The input file name.

  • triangulate (bool) – Whether to triangulate the scene if it is not already triangulated. Defaults to False.

  • search_path (str | os.PathLike | None) – Optional search path for external references (e.g. .mtl, .bin, etc.). Defaults to None.

Return SimpleScene:

The scene object extracted from the input string.

Return type:

lagrange.scene.SimpleScene3D

lagrange.io.mesh_to_string(mesh, format='ply', binary=True, exact_match=True, selected_attributes=None)

Convert a mesh to a binary string based on specified format.

Parameters:
  • mesh (lagrange.core.SurfaceMesh) – The input mesh.

  • format (str) – Format to use. Supported formats are “obj”, “ply”, “gltf” and “msh”.

  • binary (bool) – Whether to save the mesh in binary format if supported. Defaults to True. Only msh, ply and glb support binary format.

  • exact_match (bool) – Whether to save attributes in their exact form. Some mesh formats may not support all the attribute types. If set to False, attributes will be converted to the closest supported attribute type. Defaults to True.

  • selected_attributes (collections.abc.Sequence[int] | None) – A list of attribute ids to save. If not specified, all attributes will be saved. Defaults to None.

Return str:

The string representing the input mesh.

Return type:

bytes

lagrange.io.save_mesh(filename, mesh, binary=True, exact_match=True, selected_attributes=None)

Save mesh to file.

Filename extension determines the file format. Supported formats are: obj, ply, msh, glb and gltf.

Parameters:
  • filename (str | os.PathLike) – The output file name.

  • mesh (lagrange.core.SurfaceMesh) – The input mesh.

  • binary (bool) – Whether to save the mesh in binary format if supported. Defaults to True. Only msh, ply and glb support binary format.

  • exact_match (bool) – Whether to save attributes in their exact form. Some mesh formats may not support all the attribute types. If set to False, attributes will be converted to the closest supported attribute type. Defaults to True.

  • selected_attributes (collections.abc.Sequence[int] | None) – A list of attribute ids to save. If not specified, all attributes will be saved. Defaults to None.

Return type:

None

lagrange.io.save_scene(filename: str | os.PathLike, scene: lagrange.scene.Scene, options: SaveOptions = ...) None
lagrange.io.save_scene(filename: str | os.PathLike, scene: lagrange.scene.Scene, binary: bool = True, exact_match: bool = True, embed_images: bool = False, selected_attributes: collections.abc.Sequence[int] | None = None) None

Save a scene. Supports gltf, glb, obj.

Parameters:
  • filename – The output file name.

  • scene – The scene to save.

  • binary – Whether to save the scene in binary format if supported. Defaults to True. Only glb supports binary format.

  • exact_match – Whether to save attributes in their exact form. Some mesh formats may not support all the attribute types. If set to False, attributes will be converted to the closest supported attribute type. Defaults to True.

  • selected_attributes – A list of attribute ids to save. If not specified, all attributes will be saved. Defaults to None.

Return str:

The string representing the input scene.

lagrange.io.save_simple_scene(filename, scene, binary=True)

Save a simple scene to file. Supports gltf, glb, obj.

Parameters:
  • filename (str | os.PathLike) – The output file name.

  • scene (lagrange.scene.SimpleScene3D) – The input scene.

  • binary (bool) – Whether to save the scene in binary format if supported. Defaults to True. Only glb supports binary format.

Return type:

None

lagrange.io.scene_to_string(scene, format, binary=True, exact_match=True, embed_images=False, selected_attributes=None)

Convert a scene to a binary string based on specified format.

Parameters:
  • scene (lagrange.scene.Scene) – The input scene.

  • format (str) – Format to use. Supported formats are “gltf” and “glb”.

  • binary (bool) – Whether to save the scene in binary format if supported. Defaults to True. Only glb supports binary format.

  • exact_match (bool) – Whether to save attributes in their exact form. Some mesh formats may not support all the attribute types. If set to False, attributes will be converted to the closest supported attribute type. Defaults to True.

  • selected_attributes (collections.abc.Sequence[int] | None) – A list of attribute ids to save. If not specified, all attributes will be saved. Defaults to None.

  • embed_images (bool)

Return str:

The string representing the input scene.

Return type:

bytes

lagrange.io.string_to_mesh(data, triangulate=False)

Convert a binary string to a mesh.

The binary string should use one of the supported formats. Supported formats include obj, ply, gltf, glb, fbx and msh. Format is automatically detected.

Parameters:
  • data (bytes) – A binary string representing the mesh data in a supported format.

  • triangulate (bool) – Whether to triangulate the mesh if it is not already triangulated. Defaults to False.

Return SurfaceMesh:

The mesh object extracted from the input string.

Return type:

lagrange.core.SurfaceMesh

lagrange.io.string_to_scene(data, triangulate=False)

Convert a binary string to a scene.

The binary string should use one of the supported formats (i.e. gltf, glb and fbx).

Parameters:
  • data (bytes) – A binary string representing the scene data in a supported format.

  • triangulate (bool) – Whether to triangulate the scene if it is not already triangulated. Defaults to False.

Return Scene:

The scene object extracted from the input string.

Return type:

lagrange.scene.Scene