-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathlib.py
More file actions
172 lines (144 loc) · 6.34 KB
/
lib.py
File metadata and controls
172 lines (144 loc) · 6.34 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
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
# Copyright (C) 2021 Victor Soupday
# This file is part of CC/iC Blender Tools <https://github.com/soupday/cc_blender_tools>
#
# CC/iC Blender Tools is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# CC/iC Blender Tools is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with CC/iC Blender Tools. If not, see <https://www.gnu.org/licenses/>.
import bpy, os
from . import utils, vars
def get_object(object_names,
lib_tag="RL_Library_Object",
allow_duplicates=True,
names=None):
single = False
if type(object_names) is str:
object_names = [ object_names ]
if names:
names = [ names ]
single = True
appended_objects = [None]*len(object_names)
found = 0
if not allow_duplicates:
for obj in bpy.data.objects:
for i, object_name in enumerate(object_names):
if ((obj.name.startswith(object_name) or
(obj.name.startswith(names[i]))) and
utils.get_prop(obj, lib_tag) and
is_version(obj)):
appended_objects[i] = obj
found += 1
files = [ {"name": object_name } for i, object_name in enumerate(object_names) if appended_objects[i] is None ]
if files:
path = os.path.dirname(os.path.realpath(__file__))
filename = "_LIB341.blend"
datablock = "Object"
file = os.path.join(path, filename)
if os.path.exists(file):
objects = utils.get_set(bpy.data.objects)
bpy.ops.wm.append(directory=os.path.join(path, filename, datablock),
files=files,
set_fake=True,
link=False)
new = utils.get_set_new(bpy.data.objects, objects)
for i, object_name in enumerate(object_names):
if appended_objects[i] is None:
for obj in new:
if utils.strip_name(obj.name) == object_name and lib_tag not in obj:
obj[lib_tag] = True
obj["RL_Addon_Version"] = vars.VERSION_STRING
if names:
obj.name = names[i]
try:
obj.data.name = names[i]
except: ...
utils.log_info(f"Appended Library Object: {path} / {object_name} > {obj.name}")
appended_objects[i] = obj
found += 1
if found < len(object_names):
raise ValueError(f"Unable to append all Library Objects: {object_names} from {path}")
if single:
return appended_objects[0]
else:
return appended_objects
def get_image(image_name, lib_tag="RL_Library_Image"):
for img in bpy.data.images:
if (img.name.startswith(image_name) and
utils.get_prop(img, lib_tag) and
is_version(img)):
if not img.packed_file:
img.pack()
return img
path = os.path.dirname(os.path.realpath(__file__))
filename = "_LIB341.blend"
datablock = "Image"
file = os.path.join(path, filename)
appended_image = None
if os.path.exists(file):
images = utils.get_set(bpy.data.images)
bpy.ops.wm.append(directory=os.path.join(path, filename, datablock),
filename=image_name,
set_fake=True,
link=False)
new = utils.get_set_new(bpy.data.images, images)
for img in new:
if utils.strip_name(img.name) == image_name and lib_tag not in img:
img[lib_tag] = True
img["RL_Addon_Version"] = vars.VERSION_STRING
utils.log_info(f"Appended Library Image: {path} / {image_name} > {img.name}")
appended_image = img
if not appended_image:
raise ValueError(f"Unable to append Library Image: {image_name} from {path}")
else:
if not appended_image.packed_file:
appended_image.pack()
return appended_image
def get_node_group(group_name, lib_tag="RL_Node_Group"):
for node_tree in bpy.data.node_groups:
if (node_tree.name.startswith(group_name) and
utils.get_prop(node_tree, lib_tag) and
is_version(node_tree)):
return node_tree
path = os.path.dirname(os.path.realpath(__file__))
filename = "_LIB341.blend"
datablock = "NodeTree"
file = os.path.join(path, filename)
appended_object = None
if os.path.exists(file):
node_groups = utils.get_set(bpy.data.node_groups)
bpy.ops.wm.append(directory=os.path.join(path, filename, datablock),
filename=group_name,
set_fake=True,
link=False)
new = utils.get_set_new(bpy.data.node_groups, node_groups)
for node_tree in new:
if utils.strip_name(node_tree.name) == group_name and lib_tag not in node_tree:
node_tree[lib_tag] = True
node_tree["RL_Addon_Version"] = vars.VERSION_STRING
utils.log_info(f"Appended Library Node Group: {path} / {group_name} > {node_tree.name}")
appended_object = node_tree
if not appended_object:
raise ValueError(f"Unable to append Library Image: {group_name} from {path}")
return appended_object
def check_node_groups():
for name in vars.NODE_GROUPS:
get_node_group(name)
def remove_all_groups():
for group in bpy.data.node_groups:
if vars.NODE_PREFIX in group.name or "RL_Node_Group" in group:
bpy.data.node_groups.remove(group)
def rebuild_node_groups():
remove_all_groups()
check_node_groups()
return
def is_version(obj):
return (vars.VERSION_STRING in obj.name or
utils.get_prop(obj, "RL_Addon_Version") == vars.VERSION_STRING)