-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasset.py
286 lines (204 loc) · 8.92 KB
/
asset.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import bpy
import os
from . system import printd, save_json, load_json
from . registration import get_prefs
from .. items import asset_browser_bookmark_props
def get_registered_library_references(context):
base_libs = ['LOCAL']
if bpy.app.version >= (3, 5, 0):
base_libs.insert(0, 'ALL')
base_libs.append('ESSENTIALS')
librefs = base_libs + [lib.name for lib in context.preferences.filepaths.asset_libraries]
return librefs
def get_asset_library_reference(params):
if bpy.app.version >= (4, 0, 0):
return params.asset_library_reference
else:
return params.asset_library_ref
def set_asset_library_reference(params, name):
if name in get_registered_library_references(bpy.context):
if bpy.app.version >= (4, 0, 0):
params.asset_library_reference = name
else:
params.asset_library_ref = name
return True
return False
def get_asset_import_method(params):
if bpy.app.version >= (4, 0, 0):
return params.import_method
else:
return params.import_type
def set_asset_import_method(params, name):
if bpy.app.version >= (4, 0, 0):
params.import_method = name
else:
params.import_type = name
def get_asset_ids(context):
if bpy.app.version >= (4, 0, 0):
active = context.asset
else:
active = context.active_file
if active:
return active, active.id_type, active.local_id
return None, None, None
def get_catalogs_from_asset_libraries(context, debug=False):
asset_libraries = context.preferences.filepaths.asset_libraries
all_catalogs = []
for lib in asset_libraries:
libname = lib.name
libpath = lib.path
cat_path = os.path.join(libpath, 'blender_assets.cats.txt')
if os.path.exists(cat_path):
if debug:
print(libname, cat_path)
with open(cat_path) as f:
lines = f.readlines()
for line in lines:
if line != '\n' and not any([line.startswith(skip) for skip in ['#', 'VERSION']]) and len(line.split(':')) == 3:
all_catalogs.append(line[:-1].split(':') + [libname, libpath])
catalogs = {}
for uuid, catalog, simple_name, libname, libpath in all_catalogs:
if uuid not in catalogs:
catalogs[uuid] = {'catalog': catalog,
'simple_name': simple_name,
'libname': libname,
'libpath': libpath}
if debug:
printd(catalogs)
return catalogs
def update_asset_catalogs(self, context):
self.catalogs = get_catalogs_from_asset_libraries(context, debug=False)
catalog_names = []
items = [('NONE', 'None', '')]
for uuid, catalog_data in self.catalogs.items():
catalog = catalog_data['catalog']
if catalog not in catalog_names:
catalog_names.append(catalog)
items.append((catalog, catalog, ''))
default = get_prefs().preferred_default_catalog if get_prefs().preferred_default_catalog in catalog_names else 'NONE'
bpy.types.WindowManager.M4_asset_catalogs = bpy.props.EnumProperty(name="Asset Categories", items=items, default=default)
def get_asset_details_from_space(context, space, debug=False):
lib_reference = get_asset_library_reference(space.params)
catalog_id = space.params.catalog_id
libname = '' if lib_reference == 'ALL' else lib_reference
libpath = space.params.directory.decode('utf-8')
filename = space.params.filename
import_method = get_asset_import_method(space.params)
if debug:
print()
print("get_asset_details_from_space()")
print(" asset_library_reference:", lib_reference)
print(" catalog_id:", catalog_id)
print(" libname:", libname)
print(" libpath:", libpath)
print(" filename:", filename)
print(" import_method:", import_method)
print()
if libname == 'ESSENTIALS':
return None, None, '', None
elif libname == 'LOCAL':
if 'Object/' in filename:
return libname, libpath, filename, import_method
elif not '.blend' in filename:
if debug:
print(" WARNING: LOCAL library, but ALL or library is chosen (instead of current file)!")
if 'Object/' in filename:
return 'LOCAL', '', filename, import_method
elif not libname and not libpath:
if debug:
print(" WARNING: EXTERNAL library, but library ref is ALL and directory is not set!")
catalogs = get_catalogs_from_asset_libraries(context, debug=False)
for uuid, catdata in catalogs.items():
if catalog_id == uuid:
catalog = catdata['catalog']
libname = catdata['libname']
libpath = catdata['libpath']
if debug:
print(f" INFO: found catalog {catalog}'s libname and libpath via asset catalogs:", libname, "at", libpath)
break
if debug:
print()
if libpath:
return libname, libpath, filename, import_method
else:
return None, None, '', None
def get_libref_and_catalog(context, bookmark=None):
if context.area.type == 'FILE_BROWSER' and context.area.ui_type == 'ASSETS':
space = context.space_data
if bookmark:
libref = bookmark['libref']
catalog_id = bookmark['catalog_id']
else:
libref = get_asset_library_reference(space.params)
catalog_id = space.params.catalog_id
catalogs = get_catalogs_from_asset_libraries(context, debug=False)
catalog = catalogs.get(catalog_id, None)
return libref, catalog_id, catalog
return None, None, None
def validate_libref_and_catalog(context, libref, catalog_id):
base_libs = ['ALL', 'ESSENTIALS']
asset_libraries = base_libs + [lib.name for lib in context.preferences.filepaths.asset_libraries]
if libref in asset_libraries:
catalogs = get_catalogs_from_asset_libraries(context)
return catalog_id in catalogs
return False
bookmarks = None
def validate_assetbrowser_bookmarks(debug=False):
bookmarks_path = os.path.join(bpy.utils.user_resource('CONFIG'), 'assetbrowser_bookmarks.json')
bookmark_indices = [str(i + 1) for i in range(10)]
bookmark_props = asset_browser_bookmark_props
if os.path.exists(bookmarks_path):
bookmarks = load_json(bookmarks_path)
if bookmarks:
if all([idx in bookmarks and all(prop in bookmarks[idx] for prop in bookmark_props) for idx in bookmark_indices]):
context = bpy.context
catalogs = get_catalogs_from_asset_libraries(context)
new_bookmarks = {}
for idx, bookmark in bookmarks.items():
libref = bookmark['libref']
catalog_id = bookmark['catalog_id']
display_size = bookmark['display_size']
valid = validate_libref_and_catalog(context, libref, catalog_id)
if bpy.app.version >= (4, 0, 0):
if isinstance(display_size, str):
display_size = 128
else:
if isinstance(display_size, int):
display_size = 'NORMAL'
if not valid:
if catalog_id in catalogs:
catalog = catalogs[catalog_id]
libref = catalog['libname']
valid = True
new_bookmarks[idx] = {'libref': libref,
'catalog_id': catalog_id,
'display_size': display_size,
'valid': valid}
if new_bookmarks != bookmarks:
set_assetbrowser_bookmarks(new_bookmarks)
return True
print("WARNING: Corruption in assetbrowser_bookmarks.json detected")
else:
print("WARNING: No presets .json found")
print("INFO: Initializing Assetbrowser Bookmarks")
bookmarks = {}
for idx in bookmark_indices:
bookmarks[idx] = {key: None for key in bookmark_props}
save_json(bookmarks, bookmarks_path)
if debug:
printd(bookmarks, 'All Presets')
return bookmarks
def get_assetbrowser_bookmarks(force=False):
global bookmarks
if bookmarks and not force:
return bookmarks
bookmarks_path = os.path.join(bpy.utils.user_resource('CONFIG'), 'assetbrowser_bookmarks.json')
if os.path.exists(bookmarks_path):
bookmarks = load_json(bookmarks_path)
else:
bookmarks = validate_assetbrowser_bookmarks()
return bookmarks
def set_assetbrowser_bookmarks(bookmarks):
bookmarks_path = os.path.join(bpy.utils.user_resource('CONFIG'), 'assetbrowser_bookmarks.json')
save_json(bookmarks, bookmarks_path)
get_assetbrowser_bookmarks(force=True)