I/O for many mesh formats
npx @tessl/cli install tessl/pypi-meshio@5.3.0A comprehensive Python library for input/output of mesh files, supporting over 30 different mesh formats used in scientific computing and finite element analysis. meshio enables seamless conversion between formats and provides both command-line tools and a Python API for programmatic mesh manipulation.
pip install meshio or pip install meshio[all] (includes optional dependencies)import meshioFor working with specific formats:
import meshio.vtk
import meshio.gmsh
import meshio.xdmfimport meshio
# Read a mesh from any supported format
mesh = meshio.read("input.msh")
# Write mesh to a different format
meshio.write("output.vtk", mesh)
# Access mesh properties
print(f"Number of points: {len(mesh.points)}")
print(f"Cell types: {[cell.type for cell in mesh.cells]}")
# Create mesh from scratch
import numpy as np
points = np.array([
[0.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
])
cells = [
("triangle", [[0, 1, 2]])
]
mesh = meshio.Mesh(points, cells)
meshio.write("triangle.vtk", mesh)meshio uses a unified mesh representation with format-specific readers and writers:
Primary functions for reading and writing mesh files across all supported formats, with automatic format detection and comprehensive error handling.
def read(filename, file_format=None):
"""
Read mesh data from file.
Parameters:
- filename: file path or buffer to read from
- file_format: optional explicit format specification
Returns:
Mesh object containing points, cells, and data
"""
def write(filename, mesh, file_format=None, **kwargs):
"""
Write mesh data to file.
Parameters:
- filename: file path or buffer to write to
- mesh: Mesh object to write
- file_format: optional explicit format specification
- **kwargs: format-specific options
"""
def write_points_cells(filename, points, cells, point_data=None, cell_data=None, field_data=None, point_sets=None, cell_sets=None, file_format=None, **kwargs):
"""
Write mesh data without creating Mesh object.
Parameters:
- filename: output file path
- points: array-like of mesh points
- cells: cell connectivity data
- point_data: optional point-associated data
- cell_data: optional cell-associated data
- field_data: optional field data
- point_sets: optional point sets
- cell_sets: optional cell sets
- file_format: optional format specification
"""Core classes for representing mesh geometry, topology, and associated data with methods for manipulation and data conversion.
class Mesh:
"""
Central mesh representation with points, cells, and data.
Attributes:
- points: array of vertex coordinates
- cells: list of CellBlock objects
- point_data: dict of point-associated data arrays
- cell_data: dict of cell-associated data arrays
- field_data: dict of field data
- point_sets: dict of point sets
- cell_sets: dict of cell sets
"""
class CellBlock:
"""
Container for cells of the same type.
Attributes:
- type: cell type string
- data: connectivity array
- dim: topological dimension
- tags: list of tags
"""Specialized readers and writers for individual mesh formats, providing format-specific functionality and advanced features like time series support for XDMF.
# Standard format modules (each provides read/write functions)
import meshio.vtk # VTK legacy format
import meshio.vtu # VTK XML format
import meshio.gmsh # GMSH format with type conversion utilities
import meshio.stl # STL format
import meshio.ply # PLY format
import meshio.obj # Wavefront OBJ format
import meshio.xdmf # XDMF format with time series support
# ... and 25+ more formats
# Time series support for XDMF
from meshio.xdmf import TimeSeriesWriter, TimeSeriesReaderSystem for registering custom file formats and managing format detection based on file extensions.
def register_format(format_name, extensions, reader, writer_map):
"""
Register new file format with readers and writers.
Parameters:
- format_name: name identifier for format
- extensions: list of file extensions
- reader: reader function for format
- writer_map: dict mapping format to writer function
"""
def deregister_format(format_name):
"""
Remove previously registered format.
Parameters:
- format_name: name of format to remove
"""Command-line tools for mesh format conversion, inspection, and manipulation without requiring Python programming.
def main(argv=None):
"""
Entry point for meshio command-line interface.
Commands:
- convert: convert between formats
- info: display mesh information
- compress/decompress: handle compressed formats
- binary/ascii: convert between binary and ASCII
"""Exception classes for handling mesh reading and writing errors with descriptive error messages.
class ReadError(Exception):
"""Exception raised when mesh reading fails."""
class WriteError(Exception):
"""Exception raised when mesh writing fails."""Constants and utility functions for working with cell types and format detection.
topological_dimension: dict
# Mapping from cell type names to topological dimensions
# e.g., {"triangle": 2, "tetra": 3, "line": 1, ...}
extension_to_filetypes: dict
# Mapping from file extensions to supported format namesfrom typing import Optional, Dict, List, Union, Any
from numpy.typing import ArrayLike
# Core types used throughout the API
Points = ArrayLike # Array of point coordinates
CellData = List[int] # Cell connectivity data
PointData = Dict[str, ArrayLike] # Point-associated data
CellDataDict = Dict[str, List[ArrayLike]] # Cell-associated data
FieldData = Dict[str, Any] # Field data
PointSets = Dict[str, ArrayLike] # Named point sets
CellSets = Dict[str, List[ArrayLike]] # Named cell sets