-
Notifications
You must be signed in to change notification settings - Fork 7
/
properties.py
313 lines (246 loc) · 8.96 KB
/
properties.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import bpy
from bpy.types import PropertyGroup
from bpy.props import (
StringProperty, IntProperty, FloatProperty, BoolProperty, EnumProperty, CollectionProperty,
PointerProperty
)
from .functions import update_text
from . import functions
from contextlib import suppress
classes = []
class STB_property:
space: StringProperty()
linename: StringProperty()
line: IntProperty()
sort: StringProperty()
class STB_property_string(STB_property, PropertyGroup):
def update_prop(self, context):
txt = self.prop.replace('"', '\\"').replace("'", "\\'")
update_text(
self.line,
self.linename,
'"%s"' % txt,
eval("context.scene.%s" % self.path_from_id().split(".")[0])
)
prop: StringProperty(update=update_prop)
class STB_property_int(STB_property, PropertyGroup):
def update_prop(self, context):
update_text(
self.line,
self.linename,
self.prop,
eval("context.scene.%s" % self.path_from_id().split(".")[0])
)
prop: IntProperty(update=update_prop)
class STB_property_float(STB_property, PropertyGroup):
def update_prop(self, context):
update_text(
self.line,
self.linename,
self.prop,
eval("context.scene.%s" % self.path_from_id().split(".")[0])
)
prop: FloatProperty(update=update_prop)
class STB_property_bool(STB_property, PropertyGroup):
def update_prop(self, context):
update_text(
self.line,
self.linename,
self.prop,
eval("context.scene.%s" % self.path_from_id().split(".")[0])
)
prop: BoolProperty(update=update_prop)
class STB_enum_item(PropertyGroup):
item: StringProperty()
class STB_property_enum(STB_property, PropertyGroup):
def prop_items(self, context):
return functions.list_to_enum_items([item.item for item in self.items])
def update_prop(self, context):
update_text(
self.line,
self.linename,
[self.prop, [item.item for item in self.items]],
eval("context.scene.%s" % self.path_from_id().split(".")[0])
)
prop: EnumProperty(items=prop_items, update=update_prop)
items: CollectionProperty(type=STB_enum_item)
class STB_vector_property(STB_property, PropertyGroup):
address: StringProperty()
class STB_enum_property(PropertyGroup):
def prop_items(self, context):
return functions.list_to_enum_items([item.item for item in self.items])
def prop_update(self, context):
split = self.path_from_id().split(".")
if len(split) > 1:
prop = eval("context.scene.%s" % ".".join(split[:2]))
else:
prop = eval(self.address)
update_text(
prop.line,
prop.linename,
[functions.type_getter(ele, ele.ptype) for ele in prop.prop],
eval("context.scene.%s" % self.path_from_id().split(".")[0])
)
prop: EnumProperty(items=prop_items, update=prop_update)
items: CollectionProperty(type=STB_enum_item)
class STB_property_list_item(PropertyGroup):
def update_prop(self, context):
split = self.path_from_id().split(".")
if len(split) > 1:
prop = eval("bpy.context.scene." + ".".join(split[:2]))
else:
prop = eval(self.address)
update_text(
prop.line,
prop.linename,
[functions.type_getter(ele, ele.ptype) for ele in prop.prop],
eval("context.scene.%s" % self.path_from_id().split(".")[0])
)
str_prop: StringProperty(update=update_prop)
int_prop: IntProperty(update=update_prop)
float_prop: FloatProperty(update=update_prop)
bool_prop: BoolProperty(update=update_prop)
enum_prop: PointerProperty(type=STB_enum_property)
intvector_prop: StringProperty()
floatvector_prop: StringProperty()
boolvector_prop: StringProperty()
ptype: StringProperty()
class STB_property_list(STB_property, PropertyGroup):
def update_prop(self, context):
if len(self.prop) >= 1:
self.prop[0].update_prop(context)
prop: CollectionProperty(type=STB_property_list_item)
class STB_property_object(STB_property, PropertyGroup):
def update_prop(self, context):
update_text(
self.line,
self.linename,
"bpy.data.objects['%s']" % self.prop if self.prop != '' else "''",
eval("bpy.context.scene." + self.path_from_id().split(".")[0])
)
prop: StringProperty(update=update_prop)
class STB_button_area(PropertyGroup):
area: StringProperty()
class STB_button_properties(PropertyGroup):
def get_selected(self) -> bool:
"""
default Blender property getter
Returns:
bool: selection state of the button
"""
return self.get("selected", False)
def set_selected(self, value: bool):
"""
set the button as active, False will not change anything
Args:
value (bool): state of button
"""
scene = bpy.context.scene
selected_name = scene.get("stb_button.selected_name", "")
# implementation similar to a UIList (only one selection of all can be active)
if value:
scene["stb_button.selected_name"] = self.name
self['selected'] = value
button = scene.stb.get(selected_name, None)
if button:
button.selected = False
elif selected_name != self.name:
self['selected'] = value
selected: BoolProperty(
name='Select',
description='Select this Button',
get=get_selected,
set=set_selected
)
StringProps: CollectionProperty(type=STB_property_string)
IntProps: CollectionProperty(type=STB_property_int)
FloatProps: CollectionProperty(type=STB_property_float)
BoolProps: CollectionProperty(type=STB_property_bool)
EnumProps: CollectionProperty(type=STB_property_enum)
IntVectorProps: CollectionProperty(type=STB_vector_property)
FloatVectorProps: CollectionProperty(type=STB_vector_property)
BoolVectorProps: CollectionProperty(type=STB_vector_property)
ListProps: CollectionProperty(type=STB_property_list)
ObjectProps: CollectionProperty(type=STB_property_object)
areas: CollectionProperty(type=STB_button_area)
panel: StringProperty(default="Button")
class STB_text_property(PropertyGroup):
name: StringProperty()
select: BoolProperty(default=False)
class STB_export_button(PropertyGroup):
def get_use(self) -> bool:
"""
get state whether the button will be used to export
with extra check if export_all is active
Returns:
bool: button export state
"""
return self.get("use", True) or self.get('export_all', False)
def set_use(self, value: bool) -> None:
"""
set state whether the button will be used to export
Args:
value (bool): button export state
"""
if not self.get('export_all', False):
self['use'] = value
use: BoolProperty(
default=True,
name="Import Button",
description="Decide whether to export the button",
get=get_use,
set=set_use
)
class STB_add_property_item(PropertyGroup):
position: IntProperty()
line: StringProperty()
value: StringProperty()
type: StringProperty()
class STB_edit_property_item(PropertyGroup):
name: StringProperty()
line: IntProperty()
linename: StringProperty()
use_delete: BoolProperty(default=False)
class STB_edit_area_item(PropertyGroup):
name: StringProperty()
label: StringProperty()
icon: IntProperty()
delete: BoolProperty(default=False)
classes = [
STB_property_string,
STB_property_int,
STB_property_float,
STB_property_bool,
STB_enum_item,
STB_property_enum,
STB_vector_property,
STB_enum_property,
STB_property_list_item,
STB_property_list,
STB_property_object,
STB_button_area,
STB_button_properties,
STB_text_property,
STB_export_button,
STB_add_property_item,
STB_edit_property_item,
STB_edit_area_item
]
def register():
for cls in classes:
bpy.utils.register_class(cls)
bpy.types.Scene.stb = CollectionProperty(type=STB_button_properties)
def unregister():
for ele in bpy.context.scene.stb:
for intvec in ele.IntVectorProps:
with suppress(AttributeError):
exec("del bpy.types.Scene.%s" % intvec.address.split(".")[-1])
for floatvec in ele.FloatVectorProps:
with suppress(AttributeError):
exec("del bpy.types.Scene.%s" % floatvec.address.split(".")[-1])
for boolvec in ele.BoolVectorProps:
with suppress(AttributeError):
exec("del bpy.types.Scene.%s" % boolvec.address.split(".")[-1])
for cls in classes:
bpy.utils.unregister_class(cls)
del bpy.types.Scene.stb