|
import os |
|
import glob |
|
import sys |
|
import shutil |
|
import trimesh |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def merge_fragments_runner( outfilepath, show_output=True ): |
|
|
|
|
|
fragmentfiles = glob.glob( outfilepath.replace( ".ply", "_fragment_*.ply" ) ) |
|
if not fragmentfiles: |
|
if show_output: |
|
print( f"No fragments found for {outfilepath}" ) |
|
else: |
|
if show_output: |
|
print( "Merging following fragment files found for {outfilepath}:" ) |
|
print( fragmentfiles ) |
|
|
|
if len( fragmentfiles ) == 1: |
|
|
|
shutil.copy2( fragmentfiles[0], outfilepath ) |
|
|
|
else: |
|
|
|
meshes = [trimesh.load( fragmentfile ) for fragmentfile in fragmentfiles] |
|
|
|
|
|
mesh = trimesh.util.concatenate( meshes ) |
|
|
|
|
|
mesh.export( outfilepath ) |
|
|
|
if __name__ == "__main__": |
|
if len(sys.argv) != 2: |
|
print("Usage: python3 merge_fragments.py [outfilepath] / [dirpath]") |
|
print( "File Example: python3 merge_fragments.py ./03593526/e4c871d1d5e3c49844b2fa2cac0778f5/models/model_b_1.ply") |
|
print( "Directory Examples (LAST TWO ARE TIME-INTENSIVE):" ) |
|
print( "For all models in an object folder: python3 merge_fragments.py ./03593526/e4c871d1d5e3c49844b2fa2cac0778f5" ) |
|
print( "For all models in a class: python3 merge_fragments.py ./03593526" ) |
|
print( "For all models: python3 merge_fragments.py ./" ) |
|
else: |
|
file_or_dir_path = sys.argv[1] |
|
if os.path.isfile( file_or_dir_path ): |
|
print( "Provided single file" ) |
|
merge_fragments_runner( file_or_dir_path ) |
|
else: |
|
print( "Provided directory" ) |
|
N_max = 10 |
|
for root, subdirs, files in os.walk( file_or_dir_path ): |
|
for i in range( 0, N_max ): |
|
for suffix in ["b", "r"]: |
|
path = os.path.join( root, f"model_{suffix}_{i}.ply" ) |
|
path_fragment = os.path.join( root, f"model_{suffix}_{i}_fragment_0.ply" ) |
|
if os.path.isfile( path_fragment ): |
|
merge_fragments_runner( path ) |
|
|
|
|