-
-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathassets_manager.py
More file actions
70 lines (51 loc) · 1.83 KB
/
assets_manager.py
File metadata and controls
70 lines (51 loc) · 1.83 KB
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
from pathlib import Path
import logging
import glob
import bpy
from .utilities.register import get_path
from .global_data import LIB_NAME
logger = logging.getLogger(__name__)
def _get_lib_by_path(path):
libraries = bpy.context.preferences.filepaths.asset_libraries
for lib in libraries:
if path == lib.path:
return lib
return None
def _add_library(name, path):
bpy.ops.preferences.asset_library_add(directory=path)
logger.info("Add asset library: " + path)
lib = _get_lib_by_path(path)
if not lib:
logger.error("Could not create asset library: " + path)
return
lib.name = name
return lib
def load():
asset_path = (Path(get_path()) / "resources").as_posix()
libraries = bpy.context.preferences.filepaths.asset_libraries
# Get library by name
lib = libraries.get(LIB_NAME)
# Add library
if not lib:
lib = _add_library(LIB_NAME, asset_path)
# Ensure the path is correct
if lib.path != asset_path:
lib.path = asset_path
def load_asset(library, asset_type, asset):
"""Loads an asset of given type from a specified library
Returns True if it is loaded or already present in file"""
# Check if the asset is already present in file
if asset in [a.name for a in getattr(bpy.data, asset_type)]:
return True
prefs = bpy.context.preferences
fp = prefs.filepaths.asset_libraries[library].path
for file in glob.glob(fp + "/*.blend"):
with bpy.data.libraries.load(file, assets_only=True) as (data_from, data_to):
coll = getattr(data_from, asset_type)
if not asset in coll:
continue
getattr(data_to, asset_type).append(asset)
group = getattr(bpy.data, "node_groups").get(asset)
group.use_fake_user = True
return True
return False