-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
57 lines (46 loc) · 1.8 KB
/
__init__.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
bl_info = {
"name": "Cashew Nodes 腰果节点组",
"version": (1, 1, 1),
"blender": (3, 0, 0),
"category": "Assets",
}
import bpy
import os
# 定义资产库名称,资产库目录名需要与资产库名称相同
ASSET_LIBRARY_NAME = "Cashew Nodes"
def load_assets_library():
# 获取当前插件的路径
addon_dir = os.path.dirname(os.path.abspath(__file__))
assets_dir = os.path.join(addon_dir, ASSET_LIBRARY_NAME)
# 检查资产库是否已经存在,避免重复加载
if ASSET_LIBRARY_NAME in [lib.name for lib in bpy.context.preferences.filepaths.asset_libraries]:
print(f"Asset library '{ASSET_LIBRARY_NAME}' is already loaded")
return
if os.path.exists(assets_dir) and os.path.isdir(assets_dir):
# 添加资产库
bpy.ops.preferences.asset_library_add(directory=assets_dir)
print(f"Asset library '{ASSET_LIBRARY_NAME}' loaded from {assets_dir}")
else:
print(f"Assets folder not found in {assets_dir}")
def unload_assets_library():
# 尝试移除资产库
libraries = bpy.context.preferences.filepaths.asset_libraries
for library in libraries:
if library.name == ASSET_LIBRARY_NAME:
libraries.remove(library)
print(f"Asset library '{ASSET_LIBRARY_NAME}' removed")
return
print(f"No asset library named '{ASSET_LIBRARY_NAME}' found")
# 使用 bpy.app.timers 延迟执行加载资产库的操作
def load_assets_with_delay():
load_assets_library()
return None # 返回 None 表示定时器不再重复调用
# 注册函数
def register():
# 延迟加载资产库,确保上下文已准备好
bpy.app.timers.register(load_assets_with_delay)
# 注销函数
def unregister():
unload_assets_library()
if __name__ == "__main__":
register()