Skip to content

kmarchais/mmgpy

Repository files navigation

mmgpy

PyPI Python License Docs codecov

mmgpy brings the power of MMG mesh adaptation to Python. Generate, optimize, and refine 2D, 3D, and surface meshes with a clean API.

import mmgpy

mesh = mmgpy.read("input.vtk")
mesh.remesh(hmax=0.1)
mesh.save("output.vtk")

Mechanical piece remeshing

Try It

No installation needed — run directly with uvx:

# Remesh a mesh file
uvx mmgpy input.stl -o output.mesh -hmax 0.1

# Launch the interactive UI
uvx --from "mmgpy[ui]" mmgpy-ui

Installation

pip install mmgpy

# With UI support
pip install "mmgpy[ui]"

Using uv?

uv add mmgpy                 # add to project dependencies
uv pip install mmgpy         # install in current environment
uv pip install "mmgpy[ui]"   # install with UI support
uv tool install mmgpy        # install CLI tools globally
uv tool install "mmgpy[ui]"  # install CLI tools + UI globally

Features

  • Multi-dimensional — 2D triangular, 3D tetrahedral, and surface meshes
  • Local refinement — Control mesh density with spheres, boxes, cylinders
  • Anisotropic adaptation — Metric tensors for directional refinement
  • Level-set discretization — Extract isosurfaces from implicit functions
  • Lagrangian motion — Remesh while tracking displacement fields
  • PyVista integration — Visualize and convert meshes seamlessly
  • 40+ file formats — VTK, STL, OBJ, GMSH, and more

Usage

Basic Remeshing

import mmgpy

mesh = mmgpy.read("input.mesh")
result = mesh.remesh(hmax=0.1)

print(f"Quality: {result.quality_mean_before:.2f}{result.quality_mean_after:.2f}")
mesh.save("output.vtk")

Local Sizing

mesh = mmgpy.read("input.mesh")

# Fine mesh near a point
mesh.set_size_sphere(center=[0.5, 0.5, 0.5], radius=0.2, size=0.01)

# Fine mesh in a region
mesh.set_size_box(bounds=[[0, 0, 0], [0.3, 0.3, 0.3]], size=0.02)

mesh.remesh(hmax=0.1)

Typed Options

from mmgpy import Mmg3DOptions

opts = Mmg3DOptions(hmin=0.01, hmax=0.1, hausd=0.001)
mesh.remesh(opts)

# Or use presets
mesh.remesh(Mmg3DOptions.fine())

Visualization

mesh.plot()  # Quick plot with edges

# Or for custom plotting:
import pyvista as pv
plotter = pv.Plotter()
plotter.add_mesh(mesh.vtk, show_edges=True, color="lightblue")
plotter.show()

Command Line

MMG executables are included and available after installation:

# Auto-detect mesh type
mmg input.mesh -o output.mesh -hmax 0.1

# Or use specific commands
mmg3d input.mesh -o output.mesh -hmax 0.1
mmgs surface.stl -o refined.mesh -hausd 0.001
mmg2d domain.mesh -o refined.mesh -hmax 0.05

# Check versions
mmg --version

The _O3 suffix variants (mmg3d_O3, etc.) are also available for compatibility.

Gallery

Surface remeshing

Smooth surface optimization

3D quality improvement

Documentation

kmarchais.github.io/mmgpy

Contributing

Contributions are welcome! See CONTRIBUTING.md for development setup, coding standards, and the pull request process.

License

MIT

About

Pythonic remeshing library based on the MMG software.

Resources

License

Contributing

Stars

Watchers

Forks

Packages

 
 
 

Contributors