MCPcopy Create free account
hub / github.com/apple/ml-pointersect / copy_obj_files

Function copy_obj_files

plib/preprocess_obj.py:62–130  ·  view source on GitHub ↗

Copy the obj and all related files into a new directory. Args: input_dir: input folder of the mesh (obj) output_dir: output folder of the mesh (obj) relative_filepath: filename / relative path of the mesh (obj) Returns: full path of the output file

(
        input_dir: str,
        output_dir: str,
        relative_filepath: str,
)

Source from the content-addressed store, hash-verified

60
61
62def copy_obj_files(
63 input_dir: str,
64 output_dir: str,
65 relative_filepath: str,
66) -> str:
67 """
68 Copy the obj and all related files into a new directory.
69
70 Args:
71 input_dir: input folder of the mesh (obj)
72 output_dir: output folder of the mesh (obj)
73 relative_filepath: filename / relative path of the mesh (obj)
74
75 Returns:
76 full path of the output file
77 """
78 input_filepath = os.path.join(input_dir, relative_filepath)
79 output_filepath = os.path.join(output_dir, relative_filepath)
80 input_full_dir, filename = os.path.split(input_filepath)
81 output_full_dir, _ = os.path.split(output_filepath)
82
83 # get mtl file
84 mtl_line = ''
85 with open(input_filepath, 'r') as f:
86 lines = f.readlines()
87 for line_id, line in enumerate(lines):
88 if line.lstrip().startswith('mtllib'):
89 mtl_line = line.lstrip()
90 mtl_line = mtl_line[6:].lstrip().replace('\n', '')
91 break
92
93 if not mtl_line: # empty
94 print('no mtl file found!')
95 return ''
96
97 input_mtl_filepath = os.path.join(input_full_dir, mtl_line)
98 # force the output name of material file as filename+'.mtl'
99 output_mtl_filepath = os.path.join(output_full_dir, filename + '.mtl')
100 # make the mtl file is in the same folder with obj file (it should)
101 lines[line_id] = f'mtllib ./{filename}.mtl'
102
103 # copy obj file (change the refered mtl file name)
104 if not os.path.exists(output_full_dir):
105 os.makedirs(output_full_dir)
106
107 with open(output_filepath, 'w') as f:
108 for line in lines:
109 f.write(line)
110
111 # copy mtl file
112 shutil.copy(input_mtl_filepath, output_mtl_filepath)
113
114 # copy all textures
115 with open(input_mtl_filepath, 'r') as f:
116 lines = f.readlines()
117 tex_lines = [line.lstrip() for line in lines if line.lstrip().startswith('map_Kd')]
118 tex_path_list = [tex_line[6:].lstrip().replace('\n', '') for tex_line in tex_lines] # extract texture file path
119 for tex_path in tex_path_list:

Callers 1

generated_cleaned_meshFunction · 0.85

Calls 1

splitMethod · 0.80

Tested by

no test coverage detected