-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgenerate_gltf_godot.py
133 lines (107 loc) · 4.36 KB
/
generate_gltf_godot.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
"""Generate GLTF file for Godot. This is an optimized version for Godot engine (no Draco compression enabled)."""
import json
import os
import tempfile
from datetime import datetime
from blenderkit_server_utils import download, search, upload, send_to_bg
results = []
page_size = 100
def generate_gltf(asset_data, api_key, binary_path: str) -> bool:
'''
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
-------
'''
error = ""
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', binary_path=binary_path)
if not asset_file_path:
print(f"Asset file not found on path {asset_file_path}")
# fail message?
return False
# 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',
binary_path=binary_path,
target_format="gltf_godot"
)
files = None
try:
with open(result_path, 'r', encoding='utf-8') as f:
files = json.load(f)
except Exception as e:
print(f"---> Error reading result JSON {result_path}: {e}")
error += f" {e}"
if files == None:
error += " Files are None"
elif len(files) == 0:
error += f" len(files)={len(files)}"
else:
# there are no actual resolutions
print("Files are:", files)
upload.upload_resolutions(files, asset_data, api_key=api_key)
today = datetime.today().strftime('%Y-%m-%d')
param = 'gltfGodotGeneratedDate'
upload.patch_individual_parameter(asset_data['id'], param_name=param, param_value=today, api_key=api_key)
upload.get_individual_parameter(asset_data['id'], param_name=param, api_key=api_key)
print(f"---> Asset parameter {param} successfully patched with value {today}")
# TODO: Remove gltfGodotGeneratedError if it was filled by previous runs
return True
print('---> GLTF generation failed')
param = "gltfGodotGeneratedError"
value = error.strip()
upload.patch_individual_parameter(asset_data['id'], param_name=param, param_value=value, api_key=api_key)
upload.get_individual_parameter(asset_data['id'], param_name=param, api_key=api_key)
print(f'--> Asset parameter {param} patched with value {value} to signal GLTF generation FAILURE')
return False
def iterate_assets(assets: list, api_key: str='', binary_path:str=''):
for i, asset_data in enumerate(assets):
print(f"\n\n=== {i+1} downloading and generating GLTF files for {asset_data['name']}")
if asset_data is None:
print("---> skipping, asset_data are None")
continue
ok = generate_gltf(asset_data, api_key, binary_path=binary_path)
if ok:
print("===> GLTF GODOT SUCCESS")
else:
print("===> GLTF GODOT FAILED")
def main():
BLENDER_PATH = os.environ.get('BLENDER_PATH','')
API_KEY = os.environ.get('BLENDERKIT_API_KEY', '')
ASSET_BASE_ID = os.environ.get('ASSET_BASE_ID')
MAX_ASSETS = int(os.environ.get('MAX_ASSET_COUNT', '100'))
if ASSET_BASE_ID is not None: # Single asset handling - for asset validation hook
params = {
'asset_base_id': ASSET_BASE_ID,
'asset_type': 'model',
}
else: # None asset specified - will run on 100 unprocessed assets - for nightly jobs
params = {
'asset_type': 'model',
'order': '-created',
'verification_status': 'validated',
'gltfGeneratedDate_isnull': True, # Assets which does not have generated GLTF
'gltfGeneratedError_isnull': True, # Assets which does not have error from previously failed GLTF generation
}
assets = search.get_search_without_bullshit(params, page_size=min(MAX_ASSETS, 100), max_results=MAX_ASSETS, api_key=API_KEY)
print(f"--- Found {len(assets)} for GLTF conversion: ---")
for i, asset in enumerate(assets):
print(f"{i+1} {asset['name']} ||| {asset['assetType']}")
iterate_assets(assets, api_key=API_KEY, binary_path=BLENDER_PATH)
if __name__ == '__main__':
main()