meshio
meshio is a Python library for reading and writing various unstructured mesh formats, facilitating smooth conversion between them. It supports a wide array of formats including Abaqus, ANSYS msh, Gmsh, STL, VTK, XDMF, and many others. Currently at version 5.3.5, meshio is actively maintained with regular updates and requires Python 3.8 or newer. It can be used both as a command-line tool for conversions and as a Python API for programmatic mesh manipulation.
Warnings
- breaking Breaking changes in meshio versions 4.0.0 and newer, particularly concerning the internal representation and handling of mesh.cells, can cause compatibility issues with code written for older versions, especially when interfacing with other finite element libraries like FEniCS.
- gotcha For full support of all advertised mesh formats (e.g., H5M, NetCDF, comprehensive XDMF features), `meshio` requires several optional dependencies. Without these, attempts to read or write specific formats will result in `ModuleNotFoundError` or similar errors.
- gotcha When constructing a `meshio.Mesh` object programmatically, the `cells` argument expects a list of tuples, where each tuple must contain a string identifying the cell type (e.g., "triangle", "quad", "tetra") and a NumPy array of node indices for that cell type. Misunderstanding or incorrectly formatting this can lead to errors, particularly with mixed cell types or incorrect node ordering.
- breaking Older versions of `meshio` (prior to 5.3.5) exhibited compatibility issues with NumPy 2.0, leading to runtime errors or unexpected behavior.
Install
-
pip install meshio -
pip install meshio[all]
Imports
- meshio
import meshio
Quickstart
import meshio
import numpy as np
# Define points (vertices) of the mesh
points = [
[0.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[1.0, 1.0, 0.0],
[2.0, 0.0, 0.0],
[2.0, 1.0, 0.0],
]
# Define cells (elements) of the mesh, grouped by type
# Here, two triangles and one quad
cells = [
("triangle", np.array([[0, 1, 2], [1, 3, 2]])),
("quad", np.array([[1, 4, 5, 3]])),
]
# Create a Mesh object
mesh = meshio.Mesh(
points,
cells,
# Optionally provide extra data on points, cells, etc.
point_data={"T": np.array([0.3, -1.2, 0.5, 0.7, 0.0, -3.0])},
# Each item in cell data must match the cells array structure
cell_data={"a": [np.array([0.1, 0.2]), np.array([0.4])]}
)
# Write the mesh to a file (format inferred from extension, or explicitly specified)
mesh.write(
"output.vtk",
# file_format="vtk", # optional, inferred from extension
binary=True # Write in binary format (e.g. for VTK)
)
# Read a mesh from a file
read_mesh = meshio.read("output.vtk")
print(f"Read mesh points:\n{read_mesh.points}")
print(f"Read mesh cells:\n{read_mesh.cells}")
print(f"Read mesh point data (T):\n{read_mesh.point_data['T']}")
print(f"Read mesh cell data (a):\n{read_mesh.cell_data['a']}")