Upload merge_fragments.py
Browse filesScript to conduct optional merging of fragments into original post-subtraction broken/repair mesh model
- merge_fragments.py +56 -0
merge_fragments.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import glob
|
3 |
+
import sys
|
4 |
+
import trimesh
|
5 |
+
|
6 |
+
# Helper script for optional merging
|
7 |
+
# of fragments into the original
|
8 |
+
# broken/repair meshes
|
9 |
+
#
|
10 |
+
# Call this as
|
11 |
+
#
|
12 |
+
# python3 merge_fragments.py [outfilepath]"
|
13 |
+
#
|
14 |
+
# where outfilepath is the full path to the mesh (model_x_[N].ply)
|
15 |
+
# x is either b for the broken mesh or r for the repair mesh
|
16 |
+
# N is a number from 0 to N_max, corresponding to the number of
|
17 |
+
# subtractions performed (N_max is 3 for jars and bottles, 10 for mugs,
|
18 |
+
# and 1 for other classes
|
19 |
+
#
|
20 |
+
# The script searches for all fragments corresponding to model_x_[N].ply,
|
21 |
+
# i.e., all files of the form model_x_[N]_fragment_[M].ply, where M = {0,1,2...}
|
22 |
+
# and merges them using the 'trimesh' package
|
23 |
+
#
|
24 |
+
# Example:
|
25 |
+
# python3 merge_fragments.py ./03593526/e4c871d1d5e3c49844b2fa2cac0778f5/models/model_b_1.ply
|
26 |
+
#
|
27 |
+
# THE FOLLOWING PACKAGE MUST BE PRE-INSTALLED PRIOR TO USING THIS SCRIPT: trimesh
|
28 |
+
#
|
29 |
+
# Author: Natasha Kholgade Banerjee
|
30 |
+
|
31 |
+
def merge_fragments_runner( outfilepath ):
|
32 |
+
|
33 |
+
# Search for all fragments
|
34 |
+
fragmentfiles = glob.glob( outfilepath.replace( ".ply", "_fragment_*.ply" ) )
|
35 |
+
if not fragmentfiles:
|
36 |
+
print( f"No fragments found for {outfilepath}" )
|
37 |
+
else:
|
38 |
+
print( "Merging following fragment files found for {outfilepath}:" )
|
39 |
+
print( fragmentfiles )
|
40 |
+
|
41 |
+
# Load in all the fragment files as trimesh meshes
|
42 |
+
meshes = [trimesh.load( fragmentfile ) for fragmentfile in fragmentfiles]
|
43 |
+
|
44 |
+
# Concatenate all meshes into a single mesh using the concatenate function in trimesh
|
45 |
+
mesh = trimesh.util.concatenate( meshes )
|
46 |
+
|
47 |
+
# Export the concatenated mesh to {outfilepath}
|
48 |
+
mesh.export( outfilepath )
|
49 |
+
|
50 |
+
if __name__ == "__main__":
|
51 |
+
if len(sys.argv) != 2:
|
52 |
+
print("Usage: python3 merge_fragments.py [outfilepath]")
|
53 |
+
print( "Example: python3 merge_fragments.py ./03593526/e4c871d1d5e3c49844b2fa2cac0778f5/models/model_b_1.ply")
|
54 |
+
else:
|
55 |
+
outfilepath = sys.argv[1]
|
56 |
+
merge_fragments_runner( outfilepath )
|