PyMeshLab

2025.7.post1 · active · verified Thu Apr 16

PyMeshLab is a Python interface to MeshLab, providing a robust toolset for 3D mesh processing, filtering, and analysis. It allows users to programmatically access MeshLab's powerful algorithms without needing the GUI. The current version is 2025.7.post1, with releases typically occurring yearly, often followed by several post-releases for bug fixes and updated Python/dependency support.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize PyMeshLab's MeshSet, (optionally create a dummy mesh for demonstrative purposes, or ideally load an existing mesh), apply a common processing filter like decimation, and print some statistics. For actual use, replace the dummy mesh creation with `ms.load_new_mesh('/path/to/your/mesh.ply')` and `ms.save_current_mesh('/path/to/save/decimated_mesh.ply')`.

import pymeshlab as ml
import os

# Create a MeshSet object
ms = ml.MeshSet()

# Example: Load a mesh, apply decimation, and save it
# In a real scenario, replace with your actual mesh file path.
# For demonstration, we'll assume a 'teapot.ply' exists or skip loading.

# Path to a dummy input/output file for demonstration
input_path = os.path.join(os.getcwd(), "dummy_input.ply")
output_path = os.path.join(os.getcwd(), "dummy_output.ply")

# To make this runnable without an actual mesh file, we'll bypass actual file ops for new users
# In a real use-case, you'd load an existing mesh:
# ms.load_new_mesh(input_path)

# For this quickstart, let's create a simple mesh programmatically if no file
# This is not common for PyMeshLab, but makes the example runnable.
# In a real scenario, you would have loaded an actual mesh file.
# If you have a .ply file, uncomment the line below and point it to your file.
# ms.load_new_mesh("/path/to/your/mesh.ply")

# --- If you don't have a mesh file, this part makes the example executable ---
# Create a dummy mesh if no file is loaded (for quickstart execution only)
if ms.number_meshes() == 0:
    print("No mesh loaded, creating a dummy mesh for demonstration...")
    # This is a highly simplified dummy mesh creation, not typical PyMeshLab usage
    # It's just to ensure the filter application doesn't fail immediately.
    try:
        ms.add_color_sphere(radius=1.0, quality=2)
        print("Dummy sphere created.")
    except Exception as e:
        print(f"Could not create dummy sphere: {e}. PyMeshLab is best used with existing mesh files.")
        # If even dummy creation fails, then the library isn't working as expected
        # For this quickstart, we assume it works.
# --- End of dummy mesh creation ---

if ms.number_meshes() > 0:
    print(f"Number of meshes before decimation: {ms.current_mesh().vertex_number()}")
    # Apply a decimation filter (e.g., reduce mesh complexity by 50%)
    # The parameters are specific to MeshLab filters.
    ms.meshing_decimation_quadric_edge_collapse(TargetPerc=0.5)
    print(f"Number of meshes after decimation: {ms.current_mesh().vertex_number()}")

    # Save the modified mesh
    # In a real scenario, ensure the output path is valid and writable
    # ms.save_current_mesh(output_path)
    # print(f"Decimated mesh saved to: {output_path}")
else:
    print("No mesh available to process. Please load a valid mesh file for actual use.")

print("PyMeshLab quickstart complete.")

view raw JSON →