PyMeshLab icon indicating copy to clipboard operation
PyMeshLab copied to clipboard

MeshSet produces corrupt mesh on first run unless dummy mesh is created and saved (works on rerun)

Open JonasTriki opened this issue 3 months ago • 0 comments

When using pymeshlab.MeshSet to load and merge multiple OBJ meshes, the first run in a fresh Python process produces a corrupt output mesh (missing geometry, visuals, etc). If I rerun the code (with n_runs=2), the mesh is saved correctly—even without the dummy mesh workaround. Creating and saving a dummy mesh before the main processing also avoids the issue.

Minimal reproducible example:

import os
import pymeshlab

mesh_data_dir = "data/mesh_extracted" # Dir that contains .obj, .mtl and .jpg files per object. See attached `mesh_raw` ZIP and unextract to `data/mesh_extracted`
processed_mesh_path = "data/processed_mesh.obj"

# Get a list of all .obj files
mesh_filepaths = []
for root, dirs, files in os.walk(mesh_data_dir):
    for file in files:
        if file.lower().endswith((".obj")):
            mesh_filepaths.append(os.path.join(root, file))
print(f"Found {len(mesh_filepaths)} mesh files.")

n_runs = 1  # Change to 2 to 'fix' the issue, or uncomment the ms_dummy lines below

# Create and save a dummy mesh to initialize PyMeshLab properly
# ms_dummy = pymeshlab.MeshSet()
# ms_dummy.create_cube()
# ms_dummy.save_current_mesh("data/empty.obj")
# del ms_dummy

for run_idx in range(n_runs):
    print(f"Run {run_idx + 1}/{n_runs}")
    ms = pymeshlab.MeshSet(verbose=True)
    for mesh_filepath in mesh_filepaths:
        ms.load_new_mesh(mesh_filepath)
    ms.generate_by_merging_visible_meshes()
    ms.save_current_mesh(processed_mesh_path)

Symptoms:

  • On first run after kernel restart, the output mesh is corrupt (missing geometry, invalid file, etc).
  • On rerun (or with n_runs=2), the mesh is saved correctly.
  • Creating and saving a dummy mesh before main processing also avoids the issue.
  • Even if all input OBJ files load fine individually, the problem still occurs on first run.
  • Workaround: Uncommenting the dummy meshset code before the main loop makes the first run work as expected.

What I tried:

  • Using ms.clear() before loading meshes (does not help).
  • Re-instantiating MeshSet (does not help).
  • Running in a fresh kernel (problem persists on first run).

Environment:

  • PyMeshLab version: 2025.7 (also tested 2023.12.post3)
  • Python version: 3.12.5
  • OS: macOS 15.6.1 (24G90)
  • Installation method: pip

Expected behavior: MeshSet should work correctly on first run without needing a dummy meshset workaround or rerunning the code.

Actual behavior: MeshSet produces a corrupt mesh on first run unless a dummy mesh is created and saved first, or the code is rerun.

Additional info:

  • No similar issues found in the public issues/discussions.
  • I have uploaded raw data files, as well as example ZIPs with bugged and working output OBJ-files: https://we.tl/t-hxckXJIzSH. If the link does not work anymore, please comment and I will create a new link.

JonasTriki avatar Sep 08 '25 13:09 JonasTriki