Reading and Writing Meshes

pymesh.meshio.load_mesh(filename, drop_zero_dim=False)

Load mesh from a file.

Parameters:
  • filename – Input filename. File format is auto detected based on extension.
  • drop_zero_dim (bool) – If true, convert flat 3D mesh into 2D mesh.
Returns:

A Mesh object representing the loaded mesh.

pymesh.meshio.save_mesh(filename, mesh, *attributes, **setting)

Save mesh to file.

Parameters:
  • filename (str) – Output file. File format is auto detected from extension.
  • mesh (Mesh) – Mesh object.
  • *attributes (list) – (optional) Attribute names to be saved. This field would be ignored if the output format does not support attributes (e.g. .obj and .stl files)
  • **setting (dict) –

    (optional) The following keys are recognized.

    • ascii: whether to use ascii encoding, default is false.
    • use_float: store scalars as float instead of double, default is false.
    • anonymous: whether to indicate the file is generated by PyMesh.
Raises:

KeyError – Attributes cannot be found in mesh.

Example

>>> box = pymesh.generate_box_mesh();
>>> pymesh.save_mesh("tmp.stl", box, ascii=True);
pymesh.meshio.save_mesh_raw(filename, vertices, faces, voxels=None, **setting)

Save raw mesh to file.

Parameters:
  • filename (str) – Output file. File format is auto detected from extension.
  • vertices (numpy.ndarray) – Array of floats with size (num_vertices, dim).
  • faces (numpy.ndarray) – Arrayof ints with size (num_faces, vertex_per_face).
  • voxels (numpy.ndarray) – (optional) ndarray of ints with size (num_voxels, vertex_per_voxel). Use None for forming surface meshes.
  • **setting (dict) –

    (optional) The following keys are recognized.

    • ascii: whether to use ascii encoding, default is false.
    • use_float: store scalars as float instead of double, default is false.
    • anonymous: whether to indicate the file is generated by PyMesh.

Example

>>> mesh = pymesh.generate_regular_tetrahedron();
>>> pymesh.save_mesh_raw("out.msh",
...         mesh.vertices, mesh.faces, mesh.voxels);
pymesh.meshio.form_mesh(vertices, faces, voxels=None)

Convert raw mesh data into a Mesh object.

Parameters:
  • vertices (numpy.ndarray) – ndarray of floats with size (num_vertices, dim).
  • faces – ndarray of ints with size (num_faces, vertex_per_face).
  • voxels – optional ndarray of ints with size (num_voxels, vertex_per_voxel). Use None for forming surface meshes.
Returns:

A Mesh object formed by the inputs.

Example

>>> vertices = np.array([
...     [0.0, 0.0],
...     [1.0, 0.0],
...     [1.0, 1.0],
...     [0.0, 1.0],
...     ]);
>>> faces = np.array([
...     [0, 1, 2],
...     [0, 2, 3],
...     ]);
>>> mesh = pymesh.form_mesh(vertices, faces);