forked from JacquesLucke/animation_nodes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
id_keys.py
457 lines (351 loc) · 14 KB
/
id_keys.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
import bpy
from mathutils import Vector
from bpy.props import *
from . utils.path import toIDPropertyPath as toPath
from . utils.enum_items import enumItemsGenerator
# ID Keys
##########################
forcedIDKeyTypes = {
"Initial Transforms" : "Transforms",
"Initial Text" : "String" }
@enumItemsGenerator
def getIDKeyItems(self, context):
itemData = []
for item in getIDKeys():
name, type, id = item.name, item.type, item.id
itemData.append({
"id" : id + "|" + type,
"name" : name,
"description" : type})
return itemData
def getIDKeyInfo(object, id, type = None):
if not type: type = getIDType(id)
typeClass = getIDTypeClass(type)
return typeClass.read(object, id), typeClass.exists(object, id)
def hasIDKeyData(object, id, type = None):
if not type: type = getIDType(id)
typeClass = getIDTypeClass(type)
return typeClass.exists(object, id)
def getIDKeyData(object, id, type = None):
if not type: type = getIDType(id)
typeClass = getIDTypeClass(type)
return typeClass.read(object, id)
def setIDKeyData(object, id, type, data):
typeClass = getIDTypeClass(type)
typeClass.write(object, id, data)
def getIDType(id):
return getItem(id).type
def getItem(id):
for item in getIDKeys():
if id == item.id: return item
def getIDTypeClass(type):
return idTypes[type]
def getIDKeys():
return getIDKeySettings().keys
def getIDKeySettings():
return bpy.context.scene.mn_settings.idKeys
def createIDKey(id, type):
if not isCreatable(id, type): return
idKeys = getIDKeys()
item = idKeys.add()
item.name = id
item.id = id
item.type = type
def isCreatable(id, type):
if idKeyExists(id): return False
if not isValidCombination(id, type): return False
return True
def idKeyExists(id):
for item in getIDKeys():
if item.id == id: return True
return False
def isValidCombination(id, type):
if "|" in id: return False
if "|" in type: return False
if id == "": return False
if type not in idTypes.keys(): return False
if id in forcedIDKeyTypes:
if forcedIDKeyTypes[id] != type: return False
return True
def hasProp(object, name):
return hasattr(object, toPath(name))
def getProp(object, name, default):
return getattr(object, toPath(name), default)
def setProp(object, name, data):
object[name] = data
def removeProp(object, name):
if hasProp(object, name):
del object[name]
# ID Types
############################
# used for all custom id properties
prefix = "AN "
class TransformsIDType:
@classmethod
def create(cls, object, id):
cls.write(object, id, ((0.0, 0.0, 0.0), (0.0, 0.0, 0.0), (1.0, 1.0, 1.0)))
@staticmethod
def remove(object, id):
removeProp(object, prefix + id + " Location")
removeProp(object, prefix + id + " Rotation")
removeProp(object, prefix + id + " Scale")
@staticmethod
def exists(object, id):
return hasProp(object, prefix + id + " Location") and \
hasProp(object, prefix + id + " Rotation") and \
hasProp(object, prefix + id + " Scale")
@staticmethod
def read(object, id):
location = getProp(object, prefix + id + " Location", (0, 0, 0))
rotation = getProp(object, prefix + id + " Rotation", (0, 0, 0))
scale = getProp(object, prefix + id + " Scale", (1, 1, 1))
return Vector(location), Vector(rotation), Vector(scale)
@staticmethod
def write(object, id, data):
setProp(object, prefix + id + " Location", data[0])
setProp(object, prefix + id + " Rotation", data[1])
setProp(object, prefix + id + " Scale", data[2])
@staticmethod
def draw(layout, object, id, advanced = False):
row = layout.row()
col = row.column(align = True)
col.label("Location")
col.prop(object, toPath(prefix + id + " Location"), text = "")
col = row.column(align = True)
col.label("Rotation")
col.prop(object, toPath(prefix + id + " Rotation"), text = "")
col = row.column(align = True)
col.label("Scale")
col.prop(object, toPath(prefix + id + " Scale"), text = "")
@staticmethod
def drawOperators(layout, object, id):
row = layout.row(align = True)
props = row.operator("mn.set_current_transforms", text = "Use Current Transforms")
props.id = id
props.allSelectedObjects = False
props = row.operator("mn.set_current_transforms", icon = "WORLD", text = "")
props.id = id
props.allSelectedObjects = True
class SimpleIDType:
default = ""
@classmethod
def create(cls, object, id):
cls.write(object, id, cls.default)
@staticmethod
def remove(object, id):
removeProp(object, prefix + id)
@staticmethod
def exists(object, id):
return hasProp(object, prefix + id)
@classmethod
def read(cls, object, id):
value = getProp(object, prefix + id, cls.default)
return value
@staticmethod
def write(object, id, data):
setProp(object, prefix + id, data)
@classmethod
def draw(cls, layout, object, id, advanced = False):
item = getItem(id)
text = "" if advanced else item.name
layout.prop(object, toPath(prefix + id), text = text)
@staticmethod
def drawOperators(layout, object, id):
pass
class FloatIDType(SimpleIDType):
default = 0.0
class IntegerIDType(SimpleIDType):
default = 0
class StringIDType(SimpleIDType):
default = ""
@staticmethod
def drawOperators(layout, object, id):
row = layout.row(align = True)
props = row.operator("mn.set_current_texts", text = "Use Current Texts")
props.id = id
props.allSelectedObjects = False
props = row.operator("mn.set_current_texts", icon = "WORLD", text = "")
props.id = id
props.allSelectedObjects = True
idTypes = { "Transforms" : TransformsIDType,
"Float" : FloatIDType,
"String" : StringIDType,
"Integer" : IntegerIDType }
idTypeItems = [
("Transforms", "Transforms", "Contains 3 vectors for location, rotation and scale"),
("Float", "Float", "A single real number"),
("String", "String", "A text field"),
("Integer", "Integer", "Number without decimals") ]
# Panels
##############################
class IDKeysManagerPanel(bpy.types.Panel):
bl_idname = "mn.id_keys_manager"
bl_label = "ID Keys Manager"
bl_space_type = "VIEW_3D"
bl_region_type = "TOOLS"
bl_category = "AN"
def draw(self, context):
layout = self.layout
self.drawExistingKeysBox(layout)
self.drawNewKeyRow(layout)
def drawExistingKeysBox(self, layout):
box = layout.box()
idKeys = getIDKeys()
if len(idKeys) == 0:
box.label("There is no ID Key")
return
col = box.column(align = True)
for item in idKeys:
row = col.row()
row.prop(item, "name", text = "")
row.label(item.type)
hideIcon = "RESTRICT_VIEW_ON" if item.hide else "RESTRICT_VIEW_OFF"
row.prop(item, "hide", icon = hideIcon, emboss = False, icon_only = True)
props = row.operator("mn.remove_id_key", icon = "X", emboss = False, text = "")
props.id = item.id
def drawNewKeyRow(self, layout):
idKeySettings = bpy.context.scene.mn_settings.idKeys
row = layout.row(align = True)
row.prop(idKeySettings, "newKeyName", text = "")
row.prop(idKeySettings, "newKeyType", text = "")
row.operator("mn.new_id_key", icon = "PLUS", text = "")
class IDKeyPanel(bpy.types.Panel):
bl_idname = "mn.id_keys"
bl_label = "ID Keys for Active Object"
bl_space_type = "VIEW_3D"
bl_region_type = "TOOLS"
bl_category = "AN"
@classmethod
def poll(cls, context):
return context.active_object
def draw(self, context):
layout = self.layout
object = context.active_object
settings = getIDKeySettings()
layout.prop(settings, "showAdvanced")
layout.separator()
if settings.showAdvanced: self.drawKeysForObjectAdvanced(layout, object)
else: self.drawKeysForObjectSimple(layout, object)
def drawKeysForObjectAdvanced(self, layout, object):
for item in getIDKeys():
if item.hide: continue
box = layout.box()
keyName, keyType, keyID = item.name, item.type, item.id
typeClass = getIDTypeClass(keyType)
keyExists = typeClass.exists(object, keyID)
self.drawHeader(box, object, keyName, keyType, keyID, keyExists)
if keyExists:
typeClass.draw(box, object, keyID, advanced = True)
typeClass.drawOperators(box, object, keyID)
def drawKeysForObjectSimple(self, layout, object):
for item in getIDKeys():
if item.hide: continue
keyName, keyType, keyID = item.name, item.type, item.id
typeClass = getIDTypeClass(keyType)
keyExists = typeClass.exists(object, keyID)
if keyExists:
typeClass.draw(layout, object, keyID, advanced = False)
else:
props = layout.operator("mn.create_key_on_object", icon = "NEW", text = keyName)
props.id = keyID
props.type = keyType
props.objectName = object.name
def drawHeader(self, box, object, keyName, keyType, keyID, keyExists):
row = box.row()
subRow = row.row()
subRow.alignment = "LEFT"
subRow.label(keyName)
subRow = row.row()
subRow.alignment = "RIGHT"
subRow.label(keyType)
if keyExists:
props = row.operator("mn.remove_key_from_object", icon = "X", emboss = False, text = "")
else:
props = row.operator("mn.create_key_on_object", icon = "NEW", emboss = False, text = "")
props.id = keyID
props.type = keyType
props.objectName = object.name
# Operators
##############################
class NewIdKey(bpy.types.Operator):
bl_idname = "mn.new_id_key"
bl_label = "New ID Key"
bl_description = "New Key"
@classmethod
def poll(cls, context):
id, type = cls.getNewKeyData()
return isCreatable(id, type)
def execute(self, context):
id, type = self.getNewKeyData()
createIDKey(id, type)
getIDKeySettings().newKeyName = ""
context.area.tag_redraw()
return {'FINISHED'}
@classmethod
def getNewKeyData(cls):
idKeySettings = getIDKeySettings()
return idKeySettings.newKeyName, idKeySettings.newKeyType
class RemoveIDKey(bpy.types.Operator):
bl_idname = "mn.remove_id_key"
bl_label = "Remove ID Key"
bl_description = "Remove this key"
id = StringProperty()
def invoke(self, context, event):
return context.window_manager.invoke_confirm(self, event)
def execute(self, context):
idKeys = context.scene.mn_settings.idKeys
for i, item in enumerate(idKeys.keys):
if item.id == self.id:
idKeys.keys.remove(i)
context.area.tag_redraw()
return {'FINISHED'}
class CreateKeyOnObject(bpy.types.Operator):
bl_idname = "mn.create_key_on_object"
bl_label = "Create Key on Object"
bl_description = "Create the key on this object"
id = StringProperty()
type = StringProperty()
objectName = StringProperty()
def execute(self, context):
typeClass = getIDTypeClass(self.type)
typeClass.create(bpy.data.objects.get(self.objectName), self.id)
context.area.tag_redraw()
return {'FINISHED'}
class RemoveKeyFromObject(bpy.types.Operator):
bl_idname = "mn.remove_key_from_object"
bl_label = "Remove Key from Object"
bl_description = "Remove the key from this object"
id = StringProperty()
type = StringProperty()
objectName = StringProperty()
def invoke(self, context, event):
return context.window_manager.invoke_confirm(self, event)
def execute(self, context):
typeClass = getIDTypeClass(self.type)
typeClass.remove(bpy.data.objects.get(self.objectName), self.id)
context.area.tag_redraw()
return {'FINISHED'}
class SetCurrentTransforms(bpy.types.Operator):
bl_idname = "mn.set_current_transforms"
bl_label = "Set Current Transforms"
bl_description = "Set current transforms (World icon means to do this for all selected objects)"
id = StringProperty()
allSelectedObjects = BoolProperty()
def execute(self, context):
if self.allSelectedObjects: objects = context.selected_objects
else: objects = [context.active_object]
for object in objects:
TransformsIDType.write(object, self.id, (object.location, object.rotation_euler, object.scale))
return {'FINISHED'}
class SetCurrentTexts(bpy.types.Operator):
bl_idname = "mn.set_current_texts"
bl_label = "Set Current Texts"
bl_description = "Set current texts (World icon means to do this for all selected objects)"
id = StringProperty()
allSelectedObjects = BoolProperty()
def execute(self, context):
if self.allSelectedObjects: objects = context.selected_objects
else: objects = [context.active_object]
for object in objects:
StringIDType.write(object, self.id, getattr(object.data, "body", ""))
return {'FINISHED'}