Is there a way to reduce the number of triangles/polygons for a 3D model (.glb/.obj/.fbx) using python code. I have tried using pymeshlabs and open3D module, but for some reason none of the things seem to be working correctly.
this is a sample code for testing
import open3d as o3d
import os
import trimesh
import pymeshlab
def get_polygon_count(mesh):
return len(mesh.triangles)
def high_quality_simplify(input_file, output_file, target_faces):
mesh = trimesh.load(input_file)
temp_file = 'temp.obj'
mesh.export(temp_file)
ms = pymeshlab.MeshSet()
ms.load_new_mesh(temp_file)
ms.meshing_decimation_quadric_edge_collapse(
targetfacenum=target_faces,
preservenormal=True,
planarquadric=True,
autoclean=True
)
simplified_temp_file = 'simplified_temp.obj'
ms.save_current_mesh(simplified_temp_file, save_textures=False)
simplified_mesh = trimesh.load(simplified_temp_file)
simplified_mesh.export(output_file)
os.remove(temp_file)
os.remove(simplified_temp_file)
input_file = 'baseline_studio.glb' # or .fbx
output_file = 'baseline_studio_optimised.glb' # or .fbx
reduction_percentage = 10
num_triangles = get_polygon_count(o3d.io.read_triangle_mesh(input_file))
target_triangles = int(num_triangles * (1 - reduction_percentage / 100))
high_quality_simplify(input_file, output_file, target_triangles)```
#i have tried couple of different POC's using chatGPT like vtk but none of them seem to be working.