PyMeshFix

0.18.0 · active · verified Fri Apr 17

PyMeshFix is a Python library that provides bindings to the MeshFix C++ library, enabling robust repair of triangular meshes. It can fix common mesh issues such as holes, self-intersections, and non-manifold edges. The current version is 0.18.0, and it generally sees several releases per year, often aligning with updates to the broader PyVista ecosystem.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to import `MeshFix`, initialize it with NumPy arrays for vertices and faces, and perform a basic repair operation. Note that `pymeshfix` is typically used to fix complex meshes loaded from files, and this example uses a simple mesh to illustrate the API. The `repair` method can take various parameters to control the fixing process.

import numpy as np
from pymeshfix import MeshFix

# Create a simple mesh with a known issue (e.g., open boundary)
# A triangle pointing up, but with a missing top vertex to create a hole
v = np.array([
    [0.0, 0.0, 0.0], # Base left
    [1.0, 0.0, 0.0], # Base right
    [0.5, 1.0, 0.0]  # Top (would create a closed triangle)
], dtype=np.float64)
f = np.array([
    [0, 1, 2] # A single triangle
], dtype=np.int32)

# To demonstrate repair, let's create a small hole by removing a vertex
# For a true 'hole fix', typically you'd have an open boundary that isn't manifold
# In this minimal example, we'll just demonstrate the API with a valid mesh
# Real-world usage involves meshes with actual defects.

# Initialize MeshFix with vertices and faces
mfx = MeshFix(v, f)

# Repair the mesh (e.g., fill holes, remove self-intersections)
# Common options include verbose=False, joincomp=True, remove_self_intersections=True
mfx.repair(verbose=False, joincomp=True, remove_self_intersections=True)

# Get the repaired mesh
v_repaired = mfx.v
f_repaired = mfx.f

print(f"Original vertices shape: {v.shape}")
print(f"Original faces shape: {f.shape}")
print(f"Repaired vertices shape: {v_repaired.shape}")
print(f"Repaired faces shape: {f_repaired.shape}")
# Often, repair might add or remove vertices/faces depending on the issues found

# Example of creating a mesh with a hole more explicitly (requires more complex setup)
# v_hole = np.array([[0,0,0], [1,0,0], [0,1,0], [1,1,0], [0.5,0.5,1]])
# f_hole = np.array([[0,1,2], [1,3,2], [0,2,4], [1,3,4]]) # missing [0,1,4] & [2,3,4] faces to make it a pyramid with a hole
# mfx_hole = MeshFix(v_hole, f_hole)
# mfx_hole.repair()
# print("Hole repaired:", mfx_hole.v.shape, mfx_hole.f.shape)

view raw JSON →