or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

cli.mdcore-io.mdformat-registration.mdformats.mdindex.mdmesh-data.md
tile.json

tessl/pypi-meshio

I/O for many mesh formats

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/meshio@5.3.x

To install, run

npx @tessl/cli install tessl/pypi-meshio@5.3.0

index.mddocs/

meshio

A 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.

Package Information

  • Package Name: meshio
  • Language: Python
  • Installation: pip install meshio or pip install meshio[all] (includes optional dependencies)

Core Imports

import meshio

For working with specific formats:

import meshio.vtk
import meshio.gmsh
import meshio.xdmf

Basic Usage

import 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)

Architecture

meshio uses a unified mesh representation with format-specific readers and writers:

  • Mesh: Central class representing mesh data with points, cells, and associated data
  • CellBlock: Container for cells of the same type with connectivity information
  • Format modules: Specialized readers/writers for each supported format (VTK, GMSH, STL, etc.)
  • Helper functions: High-level I/O functions that automatically detect format from file extension
  • CLI tools: Command-line interface for format conversion and mesh inspection

Capabilities

Core I/O Functions

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 I/O

Mesh Data Structures

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
    """

Mesh Data

Format-Specific I/O

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, TimeSeriesReader

Format Modules

Format Registration

System 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
    """

Format Registration

Command Line Interface

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
    """

CLI Tools

Error Handling

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 Utilities

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 names

Types

from 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