forked from miykael/3dprintyourbrain
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpost_process_mesh.py
52 lines (39 loc) · 1.96 KB
/
post_process_mesh.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import pymeshlab
import argparse
import sys
import os
# Set up argument parser
parser = argparse.ArgumentParser(description="Process mesh with optional smoothing and decimation")
parser.add_argument('input_folder', type=str, help='Path to the input folder containing STL files')
parser.add_argument('--smooth', type=int, default=100, help='Number of smoothing steps, 0 or False to disable')
parser.add_argument('--decimate', type=float, nargs='?', const=1, default=290000, help='Target number of faces or decimation percentage. Use 1, 0, or False to disable decimation')
# Parse arguments
args = parser.parse_args()
ms = pymeshlab.MeshSet()
print(f'loading cerebellum')
ms.load_new_mesh(f'{args.input_folder}/subcortical.stl')
if args.smooth and args.smooth != 0:
print(f'smoothing cerebellum with {args.smooth} steps')
ms.apply_filter('scaledependent_laplacian_smooth', stepsmoothnum=args.smooth,
delta=pymeshlab.Percentage(0.1))
else:
print('no smoothing requested')
print(f'loading neocortex')
ms.load_new_mesh(f'{args.input_folder}/cortical.stl')
print('merge meshes')
ms.apply_filter('flatten_visible_layers', mergevisible=True)
print('closing holes')
ms.apply_filter('merge_close_vertices', )
if args.decimate not in [1, 0, False]:
if args.decimate < 1: # Treat as percentage
print(f'decimating mesh to {args.decimate} percentage of original')
ms.apply_filter('simplification_quadric_edge_collapse_decimation', targetperc=args.decimate, preserveboundary=True, boundaryweight=2)
else: # Treat as target face number
print(f'decimating mesh to {args.decimate} faces')
ms.apply_filter('simplification_quadric_edge_collapse_decimation', targetfacenum=int(args.decimate), preserveboundary=True, boundaryweight=2)
else:
print('no decimation requested')
# Save the combined mesh
# try to decimate the mesh until the file size is smaller than 5
print('saving final.stl')
ms.save_current_mesh(f'{args.input_folder}/final.stl')