-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgenerate_material_validations.py
158 lines (127 loc) · 5.46 KB
/
generate_material_validations.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# -----------------------------------------------------------------------------------
# generate material validation scene for all uploaded assets
# -------------------------------------------- ---------------------------------------
import json
import os
import tempfile
import threading
import time
import pathlib
from blenderkit_server_utils.cloudflare_storage import CloudflareStorage
from blenderkit_server_utils import download, search, paths, upload, send_to_bg
results = []
page_size = 100
MAX_ASSETS = int(os.environ.get('MAX_ASSET_COUNT', '100'))
def render_material_validation_thread(asset_data, api_key):
'''
A thread that:
1.downloads file
2.starts an instance of Blender that renders the validation
3.uploads files that were prepared
4.patches asset data with a new parameter.
Parameters
----------
asset_data
Returns
-------
'''
destination_directory = tempfile.gettempdir()
upload_id = asset_data['files'][0]['downloadUrl'].split('/')[-2]
# Check if the asset has already been processed
# stop using author folder
# Check if the asset has already been processed
result_file_name = f"Render{upload_id}.webp"
# drive = google_drive.init_drive()
cloudflare_storage = CloudflareStorage(
access_key=os.getenv('CF_ACCESS_KEY'),
secret_key=os.getenv('CF_ACCESS_SECRET'),
endpoint_url=os.getenv('CF_ENDPOINT_URL')
)
# check if the directory exists on the drive
# we check file by file, since the comparison with folder contents is not reliable and would potentially
# compare with a very long list. main issue was what to set the page size for the search request...
f_exists = cloudflare_storage.folder_exists('validation-renders', upload_id)
if f_exists:
print('file exists, skipping')
#purge the folder
#cloudflare_storage.delete_folder_contents('validation-renders', upload_id)
return
# Download asset
file_path = download.download_asset(asset_data, api_key=api_key, directory=destination_directory)
# find template file
current_dir = pathlib.Path(__file__).parent.resolve()
template_file_path = os.path.join(current_dir, 'blend_files', 'material_validator_mix.blend')
# Send to background to generate resolutions
temp_folder = tempfile.mkdtemp()
# result folder where the stuff for upload to drive goes
result_folder = os.path.join(temp_folder, upload_id)
os.makedirs(result_folder, exist_ok=True)
# local file path of main rendered image
result_path = os.path.join(temp_folder,
result_folder,
result_file_name)
# send to background to render
send_to_bg.send_to_bg(asset_data,
asset_file_path=file_path,
template_file_path=template_file_path,
result_path=result_path,
result_folder=result_folder,
temp_folder=temp_folder,
script='material_validation_bg.py',
binary_type = 'NEWEST',
verbosity_level=2)
# send to background to render turnarounds
template_file_path = os.path.join(current_dir, 'blend_files', 'material_turnaround_validation.blend')
result_path = os.path.join(temp_folder,
result_folder,
upload_id + '_turnaround.mkv')
send_to_bg.send_to_bg(asset_data,
asset_file_path=file_path,
template_file_path=template_file_path,
result_path=result_path,
result_folder=result_folder,
temp_folder=temp_folder,
script='material_turnaround_bg.py',
binary_type = 'NEWEST',
verbosity_level=2)
# Upload result
cloudflare_storage = CloudflareStorage(
access_key=os.getenv('CF_ACCESS_KEY'),
secret_key=os.getenv('CF_ACCESS_SECRET'),
endpoint_url=os.getenv('CF_ENDPOINT_URL')
)
cloudflare_storage.upload_folder(result_folder, 'validation-renders', upload_id)
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 resolution 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')
params = {
'order': 'created',
'asset_type': 'material',
'verification_status': 'uploaded'
}
search.get_search_simple(params, filepath=filepath, page_size=min(MAX_ASSETS, 100), max_results=MAX_ASSETS,
api_key=paths.API_KEY)
assets = search.load_assets_list(filepath)
print('ASSETS TO BE PROCESSED')
for i, a in enumerate(assets):
print(a['name'], a['assetType'])
iterate_assets(filepath, process_count=1, api_key=paths.API_KEY, thread_function=render_material_validation_thread)
if __name__ == '__main__':
main()