-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconvert_obj_to_glb.py
62 lines (50 loc) · 2.65 KB
/
convert_obj_to_glb.py
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
import sys
import argparse
import subprocess
import time
from pathlib import Path
def convert_files_in_directory(directory, extension):
directory = Path(directory)
glbs = []
for file in directory.rglob(f'*.{extension}'):
# check if a glb file already exists in the same directory as file
if list(file.parent.rglob('*.glb')): # empty list is False, non-empty list is True
print(f"Skipping file: {file} because a .glb file already exists in the same directory.")
continue
print(f"Converting file: {file}")
# Call the Blender conversion script for each file
basename = file.parent.name # uuid/object.obj -> uuid
subprocess.run(["blender", "-b", "-P", "2gltf2/2gltf2.py", "--", file.resolve(), basename])
return glbs # glb Paths
def convert_file(path):
basename = path.parent.parent.name # split/uuid/shape/object.obj -> uuid
print(f"Converting file: {path}")
# Call the Blender conversion script for each file
subprocess.run(["blender", "-b", "-P", "2gltf2/2gltf2.py", "--", path.resolve(), basename])
return str(path / f'{basename}.glb')
def convert_obj_to_glb(dir, extension, write_data_list):
# Run the conversion on the specified directory
start_time = time.time()
glbs = convert_files_in_directory(dir, extension)
print(f'Converted {len(glbs)} files in {dir}.')
# save glb paths to data_list.txt
threeD_dataset_name = 'Zeroverse'
# don't write the data_list by default
# when this script is parallelized, each on a single subdir. The main process should write the data_list
if write_data_list:
data_list_fpath = Path(dir) / f'{threeD_dataset_name}_data_list.txt'
with open(data_list_fpath, 'w') as f:
for glb in glbs:
f.write(f'{glb.resolve()}\n')
print(f'Saved absolute glb paths to {data_list_fpath}')
print(f'Total time: {time.time() - start_time:.2f} seconds. ')
print(f'Average: {(time.time() - start_time) / len(glbs):.2f} seconds per shape. ')
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Batch convert files to glTF 2.0 format using Blender.')
parser.add_argument('--dir', type=str, required=True,
help='The directory containing Shape__0/, Shape__1/, etc., which contain the .obj files.')
parser.add_argument('--extension', type=str, default='obj',
help='The file extension of the files to convert. Default: obj')
parser.add_argument('--write_data_list', default=False, action='store_true')
args = parser.parse_args()
convert_obj_to_glb(args.dir, args.extension, args.write_data_list)