-
Notifications
You must be signed in to change notification settings - Fork 2
/
generate_gltf.py
122 lines (95 loc) · 3.55 KB
/
generate_gltf.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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import json
import os
import tempfile
import time
import threading
from datetime import datetime
from blenderkit_server_utils import download, search, paths, upload, send_to_bg
results = []
page_size = 100
def generate_gltf_thread(asset_data, api_key):
'''
A thread that:
1.downloads file
2.starts an instance of Blender that generates the GLTF file
3.uploads GLTF file
4.patches asset data with a new parameter.
Parameters
----------
asset_data
Returns
-------
'''
destination_directory = tempfile.gettempdir()
# Download asset
asset_file_path = download.download_asset(asset_data, api_key=api_key, directory=destination_directory, resolution='2k')
# Unpack asset
send_to_bg.send_to_bg(asset_data, asset_file_path=asset_file_path, script='unpack_asset_bg.py')
if not asset_file_path:
# fail message?
return;
# Send to background to generate GLTF
temp_folder = tempfile.mkdtemp()
result_path = os.path.join(temp_folder, asset_data['assetBaseId'] + '_resdata.json')
send_to_bg.send_to_bg(asset_data, asset_file_path=asset_file_path,
result_path=result_path,
script='gltf_bg_blender.py')
files = None
try:
with open(result_path, 'r', encoding='utf-8') as f:
files = json.load(f)
except Exception as e:
print(e)
if files == None:
# this means error
result_state = 'error'
elif len(files) > 0:
# there are no actual resolutions
upload.upload_resolutions(files, asset_data, api_key=api_key)
result_state = 'success'
else:
# zero files, consider skipped
result_state = 'skipped'
print('changing asset variable')
today = datetime.today().strftime('%Y-%m-%d')
# patch the parameter to know when this was generated
upload.patch_individual_parameter(asset_data['id'], param_name='gltfGeneratedDate', param_value=today, api_key=api_key)
upload.get_individual_parameter(asset_data['id'], param_name='gltfGeneratedDate', api_key=api_key)
# upload.patch_asset_empty(asset_data['id'], api_key)
return
def iterate_assets(filepath, thread_function = None, process_count=12, api_key=''):
''' iterate through all assigned assets, check for those which need generation and send them to res gen'''
assets = search.load_assets_list(filepath)
threads = []
for asset_data in assets:
if asset_data is not None:
print('downloading and generating GLTF files for %s' % asset_data['name'])
thread = threading.Thread(target=thread_function, args=(asset_data, api_key))
thread.start()
threads.append(thread)
while len(threads) > process_count - 1:
for t in threads:
if not t.is_alive():
threads.remove(t)
break;
time.sleep(0.1) # wait for a bit to finish all threads
def main():
dpath = tempfile.gettempdir()
filepath = os.path.join(dpath, 'assets_for_resolutions.json')
# search for assets if assets were not provided with these parameters
params = {
'asset_type': 'model',
'order': '-created',
'verification_status': 'validated',
'gltfGeneratedDate_isnull': True
}
MAX_ASSETS = int(os.environ.get('MAX_ASSET_COUNT', '100'))
assets = search.get_search_simple(params, filepath, page_size=min(MAX_ASSETS, 100), max_results=MAX_ASSETS,
api_key=paths.API_KEY)
print('ASSETS TO BE PROCESSED')
for i, a in enumerate(assets):
print(a['name'], a['assetType'])
# return
iterate_assets(filepath, process_count=1, api_key=paths.API_KEY, thread_function=generate_gltf_thread)
if __name__ == '__main__':
main()