Datasets:

Modalities:
3D
Languages:
English
Tags:
3d
DOI:
nbanerje commited on
Commit
f9407b3
·
verified ·
1 Parent(s): 29f8540

Upload merge_fragments.py

Browse files
Files changed (1) hide show
  1. merge_fragments.py +50 -16
merge_fragments.py CHANGED
@@ -1,6 +1,7 @@
1
  import os
2
  import glob
3
  import sys
 
4
  import trimesh
5
 
6
  # Helper script for optional merging
@@ -9,7 +10,7 @@ import trimesh
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
@@ -21,36 +22,69 @@ import trimesh
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 )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
  import glob
3
  import sys
4
+ import shutil
5
  import trimesh
6
 
7
  # Helper script for optional merging
 
10
  #
11
  # Call this as
12
  #
13
+ # python3 merge_fragments.py [outfilepath] / [dirpath]"
14
  #
15
  # where outfilepath is the full path to the mesh (model_x_[N].ply)
16
  # x is either b for the broken mesh or r for the repair mesh
 
22
  # i.e., all files of the form model_x_[N]_fragment_[M].ply, where M = {0,1,2...}
23
  # and merges them using the 'trimesh' package
24
  #
25
+ # Examples (assuming merge_fragments.py is in root folder containing all class folders)
26
+ #
27
+ # For a single file:
28
  # python3 merge_fragments.py ./03593526/e4c871d1d5e3c49844b2fa2cac0778f5/models/model_b_1.ply
29
+ # For all models in an object folder:
30
+ # python3 merge_fragments.py ./03593526/e4c871d1d5e3c49844b2fa2cac0778f5
31
+ # For all models in a class:
32
+ # python3 merge_fragments.py ./03593526
33
+ # For all models:
34
+ # python3 merge_fragments.py ./
35
+ #
36
  #
37
  # THE FOLLOWING PACKAGE MUST BE PRE-INSTALLED PRIOR TO USING THIS SCRIPT: trimesh
38
  #
39
  # Author: Natasha Kholgade Banerjee
40
 
41
+ def merge_fragments_runner( outfilepath, show_output=True ):
42
 
43
  # Search for all fragments
44
  fragmentfiles = glob.glob( outfilepath.replace( ".ply", "_fragment_*.ply" ) )
45
  if not fragmentfiles:
46
+ if show_output:
47
+ print( f"No fragments found for {outfilepath}" )
48
  else:
49
+ if show_output:
50
+ print( "Merging following fragment files found for {outfilepath}:" )
51
+ print( fragmentfiles )
52
 
53
+ if len( fragmentfiles ) == 1:
54
+ # If only one fragment file, simply copy it over
55
+ shutil.copy2( fragmentfiles[0], outfilepath )
56
+
57
+ else:
58
+ # Load in all the fragment files as trimesh meshes
59
+ meshes = [trimesh.load( fragmentfile ) for fragmentfile in fragmentfiles]
60
 
61
+ # Concatenate all meshes into a single mesh using the concatenate function in trimesh
62
+ mesh = trimesh.util.concatenate( meshes )
63
 
64
+ # Export the concatenated mesh to {outfilepath}
65
+ mesh.export( outfilepath )
66
 
67
  if __name__ == "__main__":
68
  if len(sys.argv) != 2:
69
+ print("Usage: python3 merge_fragments.py [outfilepath] / [dirpath]")
70
+ print( "File Example: python3 merge_fragments.py ./03593526/e4c871d1d5e3c49844b2fa2cac0778f5/models/model_b_1.ply")
71
+ print( "Directory Examples (LAST TWO ARE TIME-INTENSIVE):" )
72
+ print( "For all models in an object folder: python3 merge_fragments.py ./03593526/e4c871d1d5e3c49844b2fa2cac0778f5" )
73
+ print( "For all models in a class: python3 merge_fragments.py ./03593526" )
74
+ print( "For all models: python3 merge_fragments.py ./" )
75
  else:
76
+ file_or_dir_path = sys.argv[1]
77
+ if os.path.isfile( file_or_dir_path ):
78
+ print( "Provided single file" )
79
+ merge_fragments_runner( file_or_dir_path )
80
+ else:
81
+ print( "Provided directory" )
82
+ N_max = 10
83
+ for root, subdirs, files in os.walk( file_or_dir_path ):
84
+ for i in range( 0, N_max ):
85
+ for suffix in ["b", "r"]:
86
+ path = os.path.join( root, f"model_{suffix}_{i}.ply" )
87
+ path_fragment = os.path.join( root, f"model_{suffix}_{i}_fragment_0.ply" )
88
+ if os.path.isfile( path_fragment ):
89
+ merge_fragments_runner( path )
90
+