Convert Figma logo to code with AI

nschloe logomeshio

:spider_web: input/output for many mesh formats

1,929
399
1,929
197

Top Related Projects

4,934

The public CGAL repository, see the README below

1,670

Lightweight, general, scalable C++ library for finite element methods

2,593

3D plotting and mesh analysis through a streamlined interface for the Visualization Toolkit (VTK)

2,653

Mirror of Visualization Toolkit repository

Quick Overview

meshio is a Python library for reading and writing various mesh formats. It provides a unified interface for handling different mesh file types, making it easier to work with and convert between various mesh representations used in scientific computing and numerical simulations.

Pros

  • Supports a wide range of mesh formats (over 20)
  • Easy-to-use API for reading, writing, and converting meshes
  • Actively maintained and regularly updated
  • Integrates well with other scientific Python libraries

Cons

  • Limited support for some less common mesh formats
  • Performance may be slower compared to specialized libraries for specific formats
  • Requires additional dependencies for certain file formats
  • May have occasional inconsistencies in handling complex mesh data

Code Examples

Reading a mesh file:

import meshio

mesh = meshio.read("mesh.vtk")
print(mesh.points)
print(mesh.cells)

Writing a mesh file:

import meshio
import numpy as np

points = np.array([[0, 0, 0], [1, 0, 0], [0, 1, 0], [1, 1, 0]])
cells = [("triangle", np.array([[0, 1, 2], [1, 2, 3]]))]
mesh = meshio.Mesh(points, cells)
meshio.write("mesh.obj", mesh)

Converting between mesh formats:

import meshio

mesh = meshio.read("input_mesh.stl")
meshio.write("output_mesh.vtk", mesh)

Getting Started

To get started with meshio, first install it using pip:

pip install meshio

Then, you can use it in your Python scripts:

import meshio

# Read a mesh file
mesh = meshio.read("input_mesh.vtk")

# Access mesh data
points = mesh.points
cells = mesh.cells

# Write to a different format
meshio.write("output_mesh.obj", mesh)

This basic example demonstrates how to read a mesh file, access its data, and write it to a different format. meshio supports many more operations and mesh formats, which you can explore in the library's documentation.

Competitor Comparisons

4,934

The public CGAL repository, see the README below

Pros of CGAL

  • Comprehensive library for computational geometry algorithms
  • Robust and efficient implementations of complex geometric operations
  • Extensive documentation and long-standing community support

Cons of CGAL

  • Steeper learning curve due to its complexity and size
  • Primarily C++ based, which may not be ideal for all projects
  • Heavier dependency compared to meshio's lightweight nature

Code Comparison

CGAL (C++):

#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Triangulation_3.h>
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef CGAL::Triangulation_3<K> Triangulation;
Triangulation T;

meshio (Python):

import meshio
mesh = meshio.read("input.msh")
meshio.write("output.vtk", mesh)

Summary

CGAL is a powerful, comprehensive library for computational geometry, offering robust algorithms and extensive functionality. It's ideal for complex geometric operations but comes with a steeper learning curve and heavier dependencies. meshio, on the other hand, is a lightweight Python library focused on mesh I/O operations, offering simplicity and ease of use for basic mesh handling tasks. The choice between them depends on the specific requirements of your project and the depth of geometric operations needed.

1,670

Lightweight, general, scalable C++ library for finite element methods

Pros of mfem

  • Comprehensive finite element library with advanced numerical methods
  • Supports parallel computing and high-performance simulations
  • Extensive documentation and examples for various applications

Cons of mfem

  • Steeper learning curve due to its complexity and breadth
  • Primarily focused on finite element methods, less versatile for general mesh I/O
  • Heavier dependency footprint

Code Comparison

mfem example:

#include "mfem.hpp"
using namespace mfem;

int main(int argc, char *argv[])
{
   Mesh mesh("example.mesh");
   H1_FECollection fec(1, mesh.Dimension());
   FiniteElementSpace fespace(&mesh, &fec);
}

meshio example:

import meshio

mesh = meshio.read("example.mesh")
points, cells = mesh.points, mesh.cells
meshio.write("output.vtk", meshio.Mesh(points, cells))

Key Differences

  • mfem is a C++ library focused on finite element methods, while meshio is a Python library for mesh I/O
  • mfem provides advanced numerical solvers and parallel computing capabilities, whereas meshio specializes in reading and writing various mesh formats
  • meshio offers simpler usage for basic mesh operations, while mfem is more suitable for complex numerical simulations
2,593

3D plotting and mesh analysis through a streamlined interface for the Visualization Toolkit (VTK)

Pros of PyVista

  • More comprehensive visualization capabilities, including 3D rendering and interactive plotting
  • Broader functionality beyond mesh I/O, including data analysis and filtering tools
  • Larger and more active community, with frequent updates and extensive documentation

Cons of PyVista

  • Steeper learning curve due to its broader feature set
  • Heavier dependency footprint, which may impact installation and portability
  • Potentially slower for simple mesh I/O operations compared to Meshio's focused approach

Code Comparison

PyVista example:

import pyvista as pv
mesh = pv.read("model.stl")
mesh.plot()

Meshio example:

import meshio
mesh = meshio.read("model.stl")
print(mesh.points, mesh.cells)

PyVista offers a more streamlined approach for visualization, while Meshio focuses on efficient mesh I/O operations. PyVista's code is more concise for plotting, but Meshio provides direct access to mesh data structures.

2,653

Mirror of Visualization Toolkit repository

Pros of VTK

  • Comprehensive visualization toolkit with extensive features
  • Strong community support and regular updates
  • Supports a wide range of file formats and data types

Cons of VTK

  • Steeper learning curve due to its complexity
  • Larger codebase and dependencies
  • Can be overkill for simple mesh I/O operations

Code Comparison

VTK (C++):

#include <vtkXMLUnstructuredGridReader.h>
#include <vtkUnstructuredGrid.h>

vtkSmartPointer<vtkXMLUnstructuredGridReader> reader = vtkSmartPointer<vtkXMLUnstructuredGridReader>::New();
reader->SetFileName("mesh.vtu");
reader->Update();
vtkUnstructuredGrid* mesh = reader->GetOutput();

meshio (Python):

import meshio

mesh = meshio.read("mesh.vtu")

Key Differences

  • VTK offers a full-featured visualization toolkit, while meshio focuses on mesh I/O operations
  • meshio provides a simpler, more lightweight solution for basic mesh reading and writing
  • VTK has broader language support (C++, Python, Java), while meshio is primarily Python-based
  • meshio offers easier installation and integration for Python projects
  • VTK provides more advanced visualization and data processing capabilities

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

meshio

I/O for mesh files.

PyPi Version Anaconda Cloud Packaging status PyPI pyversions DOI GitHub stars Downloads

Discord

gh-actions codecov LGTM Code style: black

There are various mesh formats available for representing unstructured meshes. meshio can read and write all of the following and smoothly converts between them:

Abaqus (.inp), ANSYS msh (.msh), AVS-UCD (.avs), CGNS (.cgns), DOLFIN XML (.xml), Exodus (.e, .exo), FLAC3D (.f3grid), H5M (.h5m), Kratos/MDPA (.mdpa), Medit (.mesh, .meshb), MED/Salome (.med), Nastran (bulk data, .bdf, .fem, .nas), Netgen (.vol, .vol.gz), Neuroglancer precomputed format, Gmsh (format versions 2.2, 4.0, and 4.1, .msh), OBJ (.obj), OFF (.off), PERMAS (.post, .post.gz, .dato, .dato.gz), PLY (.ply), STL (.stl), Tecplot .dat, TetGen .node/.ele, SVG (2D output only) (.svg), SU2 (.su2), UGRID (.ugrid), VTK (.vtk), VTU (.vtu), WKT (TIN) (.wkt), XDMF (.xdmf, .xmf).

(Here's a little survey on which formats are actually used.)

Install with one of

pip install meshio[all]
conda install -c conda-forge meshio

([all] pulls in all optional dependencies. By default, meshio only uses numpy.) You can then use the command-line tool

meshio convert    input.msh output.vtk   # convert between two formats

meshio info       input.xdmf             # show some info about the mesh

meshio compress   input.vtu              # compress the mesh file
meshio decompress input.vtu              # decompress the mesh file

meshio binary     input.msh              # convert to binary format
meshio ascii      input.msh              # convert to ASCII format

with any of the supported formats.

In Python, simply do

import meshio

mesh = meshio.read(
    filename,  # string, os.PathLike, or a buffer/open file
    # file_format="stl",  # optional if filename is a path; inferred from extension
    # see meshio-convert -h for all possible formats
)
# mesh.points, mesh.cells, mesh.cells_dict, ...

# mesh.vtk.read() is also possible

to read a mesh. To write, do

import meshio

# two triangles and one quad
points = [
    [0.0, 0.0],
    [1.0, 0.0],
    [0.0, 1.0],
    [1.0, 1.0],
    [2.0, 0.0],
    [2.0, 1.0],
]
cells = [
    ("triangle", [[0, 1, 2], [1, 3, 2]]),
    ("quad", [[1, 4, 5, 3]]),
]

mesh = meshio.Mesh(
    points,
    cells,
    # Optionally provide extra data on points, cells, etc.
    point_data={"T": [0.3, -1.2, 0.5, 0.7, 0.0, -3.0]},
    # Each item in cell data must match the cells array
    cell_data={"a": [[0.1, 0.2], [0.4]]},
)
mesh.write(
    "foo.vtk",  # str, os.PathLike, or buffer/open file
    # file_format="vtk",  # optional if first argument is a path; inferred from extension
)

# Alternative with the same options
meshio.write_points_cells("foo.vtk", points, cells)

For both input and output, you can optionally specify the exact file_format (in case you would like to enforce ASCII over binary VTK, for example).

Time series

The XDMF format supports time series with a shared mesh. You can write times series data using meshio with

with meshio.xdmf.TimeSeriesWriter(filename) as writer:
    writer.write_points_cells(points, cells)
    for t in [0.0, 0.1, 0.21]:
        writer.write_data(t, point_data={"phi": data})

and read it with

with meshio.xdmf.TimeSeriesReader(filename) as reader:
    points, cells = reader.read_points_cells()
    for k in range(reader.num_steps):
        t, point_data, cell_data = reader.read_data(k)

ParaView plugin

gmsh paraview *A Gmsh file opened with ParaView.*

If you have downloaded a binary version of ParaView, you may proceed as follows.

  • Install meshio for the Python major version that ParaView uses (check pvpython --version)
  • Open ParaView
  • Find the file paraview-meshio-plugin.py of your meshio installation (on Linux: ~/.local/share/paraview-5.9/plugins/) and load it under Tools / Manage Plugins / Load New
  • Optional: Activate Auto Load

You can now open all meshio-supported files in ParaView.

Performance comparison

The comparisons here are for a triangular mesh with about 900k points and 1.8M triangles. The red lines mark the size of the mesh in memory.

File sizes

file size

I/O speed

performance

Maximum memory usage

memory usage

Installation

meshio is available from the Python Package Index, so simply run

pip install meshio

to install.

Additional dependencies (netcdf4, h5py) are required for some of the output formats and can be pulled in by

pip install meshio[all]

You can also install meshio from Anaconda:

conda install -c conda-forge meshio

Testing

To run the meshio unit tests, check out this repository and type

tox

License

meshio is published under the MIT license.