File size: 3,315 Bytes
bd8d1ad f9407b3 bd8d1ad f9407b3 bd8d1ad f9407b3 bd8d1ad f9407b3 bd8d1ad f9407b3 bd8d1ad f9407b3 bd8d1ad f9407b3 bd8d1ad f9407b3 bd8d1ad f9407b3 bd8d1ad f9407b3 bd8d1ad f9407b3 bd8d1ad f9407b3 |
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
import os
import glob
import sys
import shutil
import trimesh
# Helper script for optional merging
# of fragments into the original
# broken/repair meshes
#
# Call this as
#
# python3 merge_fragments.py [outfilepath] / [dirpath]"
#
# where outfilepath is the full path to the mesh (model_x_[N].ply)
# x is either b for the broken mesh or r for the repair mesh
# N is a number from 0 to N_max, corresponding to the number of
# subtractions performed (N_max is 3 for jars and bottles, 10 for mugs,
# and 1 for other classes
#
# The script searches for all fragments corresponding to model_x_[N].ply,
# i.e., all files of the form model_x_[N]_fragment_[M].ply, where M = {0,1,2...}
# and merges them using the 'trimesh' package
#
# Examples (assuming merge_fragments.py is in root folder containing all class folders)
#
# For a single file:
# python3 merge_fragments.py ./03593526/e4c871d1d5e3c49844b2fa2cac0778f5/models/model_b_1.ply
# For all models in an object folder:
# python3 merge_fragments.py ./03593526/e4c871d1d5e3c49844b2fa2cac0778f5
# For all models in a class:
# python3 merge_fragments.py ./03593526
# For all models:
# python3 merge_fragments.py ./
#
#
# THE FOLLOWING PACKAGE MUST BE PRE-INSTALLED PRIOR TO USING THIS SCRIPT: trimesh
#
# Author: Natasha Kholgade Banerjee
def merge_fragments_runner( outfilepath, show_output=True ):
# Search for all fragments
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:
# If only one fragment file, simply copy it over
shutil.copy2( fragmentfiles[0], outfilepath )
else:
# Load in all the fragment files as trimesh meshes
meshes = [trimesh.load( fragmentfile ) for fragmentfile in fragmentfiles]
# Concatenate all meshes into a single mesh using the concatenate function in trimesh
mesh = trimesh.util.concatenate( meshes )
# Export the concatenated mesh to {outfilepath}
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 )
|