Skip to content

Instantly share code, notes, and snippets.

@Merwanski
Created March 4, 2022 23:19
Show Gist options
  • Save Merwanski/e1c645310b8857402acbd23ac6fb3272 to your computer and use it in GitHub Desktop.
Save Merwanski/e1c645310b8857402acbd23ac6fb3272 to your computer and use it in GitHub Desktop.
How to use PyMeshLab to reduce vertex number to a certain number
import pymeshlab as ml
ms = ml.MeshSet()
ms.load_new_mesh('input.ply')
m = ms.current_mesh()
print('input mesh has', m.vertex_number(), 'vertex and', m.face_number(), 'faces')
# Target number of vertex
TARGET=10000
# Estimate number of faces to have 100+10000 vertex using Euler
numFaces = 100 + 2*TARGET
# Simplify the mesh. Only first simplification will be agressive
while (ms.current_mesh().vertex_number() > TARGET):
ms.apply_filter('simplification_quadric_edge_collapse_decimation', targetfacenum=numFaces, preservenormal=True)
print("Decimated to", numFaces, "faces mesh has", ms.current_mesh().vertex_number(), "vertex")
#Refine our estimation to slowly converge to TARGET vertex number
numFaces = numFaces - (ms.current_mesh().vertex_number() - TARGET)
m = ms.current_mesh()
print('output mesh has', m.vertex_number(), 'vertex and', m.face_number(), 'faces')
ms.save_current_mesh('output.ply')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment