-
Notifications
You must be signed in to change notification settings - Fork 3
/
shape-key-extras.py
936 lines (756 loc) · 33.9 KB
/
shape-key-extras.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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program 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 2
# of the License, or (at your option) any later version.
#
# This program 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 this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
bl_info = {
"name": "Shape Key Extras",
"description": "Blender Add-on to manipulate Shape Keys",
"author": "Christian Brinkmann (p2or)",
"version": (0, 2, 0),
"blender": (2, 80, 0),
"location": "Properties > Object Data > Shape Keys",
"tracker_url": "https://github.com/p2or/blender-shapekeyextras/issues",
"category": "Mesh"
}
import bpy
import random
from bpy.props import (IntProperty,
BoolProperty,
FloatProperty,
StringProperty,
PointerProperty,
CollectionProperty,
EnumProperty
)
from bpy.types import (Operator,
Panel,
UIList,
PropertyGroup
)
# -------------------------------------------------------------------
# Helper
# -------------------------------------------------------------------
def search_chars(char_sequence, name):
if char_sequence:
if char_sequence.endswith(','):
char_sequence = char_sequence[:-1]
if char_sequence.startswith(','):
char_sequence = char_sequence[-1:]
char_list = [i.strip() for i in char_sequence.split(",")]
if name.startswith(tuple(char_list)) or name.endswith(tuple(char_list)):
return True
else:
return False
else:
return False
def shape_key_selection(op, context):
scn = context.scene
ske = scn.shape_key_extras
shape_key_names = []
if not ske.sk_only:
for shapekey in context.object.data.shape_keys.key_blocks:
exclude_char = search_chars(ske.sk_exclude, shapekey.name)
if exclude_char is not True:
if ske.sk_selection == 'ALL':
shape_key_names.append(shapekey.name)
if ske.sk_selection == 'ENABLED':
if shapekey.mute is False:
shape_key_names.append(shapekey.name)
if ske.sk_selection == 'DISABLED':
if shapekey.mute is True:
shape_key_names.append(shapekey.name)
else:
for shapekey in context.object.data.shape_keys.key_blocks:
only_char = search_chars(ske.sk_only, shapekey.name)
if only_char is True:
if ske.sk_selection == 'ALL':
shape_key_names.append(shapekey.name)
if ske.sk_selection == 'ENABLED':
if shapekey.mute is False:
shape_key_names.append(shapekey.name)
if ske.sk_selection == 'DISABLED':
if shapekey.mute is True:
shape_key_names.append(shapekey.name)
return shape_key_names
# -------------------------------------------------------------------
# Properties
# -------------------------------------------------------------------
class SKE_PG_sceneSettings(PropertyGroup):
sk_value: FloatProperty(
name = "Value",
description = "Set static value",
default = 0, #min = 0, #max =1
)
sk_random_min: FloatProperty(
name = "Min",
description = "Set minimum random value",
default = 0,
)
sk_random_max: FloatProperty(
name = "Max",
description = "Set maximum random value",
default = 1,
)
sk_exclude: StringProperty (
name = "Exclude",
description = "Exclude by first character",
default = "Basis, #, *"
)
sk_only: StringProperty (
name = "Only",
description = "Include by first character",
default = ""
)
sk_selection: EnumProperty(
name="Selection",
description="Shape Key Selection",
items = (('ALL', "All", ""),
('ENABLED', "Enabled", ""),
('DISABLED', "Disabled", ""),
),default='ALL'
)
sk_set_attributes: BoolProperty(default=False)
sk_advanced_selection: BoolProperty(default=False)
vg_uilist_index: IntProperty()
vg_merge_vgroups: BoolProperty(default=False)
class SKE_PT_indexShapeKeys(PropertyGroup):
collection_id: IntProperty()
# -------------------------------------------------------------------
# Shape Key Operators
# -------------------------------------------------------------------
class SKE_OT_enableShapeKeys(Operator):
bl_idname = "shapekeyextras.enable_all"
bl_label = "Enable All"
bl_description = "Enable all Shape Keys in Selection"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
scn = context.scene
ske = scn.shape_key_extras
if context.object.data.shape_keys:
shape_keys = (shape_key_selection(self, context))
for i in shape_keys:
shapekey = context.object.data.shape_keys.key_blocks[i]
shapekey.mute = False
self.report({'INFO'}, "All Shape Keys enabled")
else:
self.report({'WARNING'}, "No shape keys found.")
return {'FINISHED'}
class SKE_OT_disableShapeKeys(Operator):
bl_idname = "shapekeyextras.disable_all"
bl_label = "Disable All"
bl_description = "Mute all Shape Keys in Selection"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
scn = context.scene
ske = scn.shape_key_extras
if context.object.data.shape_keys:
shape_keys = shape_key_selection(self, context)
for i in shape_keys:
shapekey = context.object.data.shape_keys.key_blocks[i]
shapekey.mute = True
self.report({'INFO'}, "All Shape Keys disabled")
else:
self.report({'WARNING'}, "No shape keys found.")
return {'FINISHED'}
class SKE_OT_toggleShapeKeys(Operator):
bl_idname = "shapekeyextras.toggle_mute"
bl_label = "Toggle Visibility"
bl_description = "Toggle Mute State of all Shape Keys in Selection"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
scn = context.scene
ske = scn.shape_key_extras
if context.object.data.shape_keys:
shape_keys = shape_key_selection(self, context)
for i in shape_keys:
shapekey = context.object.data.shape_keys.key_blocks[i]
shapekey.mute = not shapekey.mute
self.report({'INFO'}, "Enabled Shape Keys disabled and Disabled Shape Keys enabled")
else:
self.report({'WARNING'}, "No shape keys found.")
return {'FINISHED'}
class SKE_OT_randomShapeKeyEnable(Operator):
bl_idname = "shapekeyextras.random_visibility"
bl_label = "Randomize Visibility"
bl_description = "Randomize Visibility/Mute State for all Shape Keys in Selection"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
scn = context.scene
ske = scn.shape_key_extras
if context.object.data.shape_keys:
shape_keys = shape_key_selection(self, context)
for i in shape_keys:
shapekey = context.object.data.shape_keys.key_blocks[i]
shapekey.mute = bool(random.getrandbits(1))
self.report({'INFO'}, "Ramdomized Shape Key Visibility")
else:
self.report({'WARNING'}, "No shape keys found.")
return {'FINISHED'}
class SKE_OT_randomShapeKeyValue(Operator):
bl_idname = "shapekeyextras.randomize"
bl_label = "Randomize Shape Key Value"
bl_description = "Randomize Shape Key Value for all Shape Keys in Selection"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
scn = context.scene
ske = scn.shape_key_extras
if context.object.data.shape_keys:
shape_keys = shape_key_selection(self, context)
for i in shape_keys:
if i != 'Basis':
shapekey = context.object.data.shape_keys.key_blocks[i]
shapekey.value = random.uniform(ske.sk_random_min, ske.sk_random_max)
self.report({'INFO'}, "Values for Shape Keys generated")
else:
self.report({'WARNING'}, "No shape keys found.")
return {'FINISHED'}
class SKE_OT_setShapeKeyRange(Operator):
bl_idname = "shapekeyextras.set_range"
bl_label = "Set Shape Key Range"
bl_description = "Set Range Values for Shape Keys in Selection"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
scn = context.scene
ske = scn.shape_key_extras
if context.object.data.shape_keys:
shape_keys = shape_key_selection(self, context)
for i in shape_keys:
if i != 'Basis':
shapekey = context.object.data.shape_keys.key_blocks[i]
shapekey.slider_min = ske.sk_random_min
shapekey.slider_max = ske.sk_random_max
self.report({'INFO'}, "Range Values adjusted")
else:
self.report({'WARNING'}, "No shape keys found.")
return {'FINISHED'}
class SKE_OT_applyShapeKeyValue(Operator):
bl_idname = "shapekeyextras.set_values"
bl_label = "Set Shape Key Values"
bl_description = "Assign static Values to all Shape Keys in Selection"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
scn = context.scene
ske = scn.shape_key_extras
if context.object.data.shape_keys:
shape_keys = shape_key_selection(self, context)
for i in shape_keys:
if i != 'Basis':
shapekey = context.object.data.shape_keys.key_blocks[i]
shapekey.value = ske.sk_value
self.report({'INFO'}, "Value assigned to Shape Keys")
else:
self.report({'WARNING'}, "No shape keys found.")
return {'FINISHED'}
class SKE_OT_removeShapeKeyDriver(Operator):
bl_idname = "shapekeyextras.remove_drivers"
bl_label = "Remove Drivers"
bl_description = "Remove Drivers from Shapekeys in Selection"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
if context.object.data.shape_keys:
shape_keys = shape_key_selection(self, context)
for i in shape_keys:
if i != 'Basis':
context.object.data.shape_keys.key_blocks[i].driver_remove("value")
self.report({'INFO'}, "Drivers Removed")
else:
self.report({'WARNING'}, "No shape keys found.")
return {'FINISHED'}
def invoke(self, context, event):
return context.window_manager.invoke_confirm(self, event)
class SKE_OT_addShapeKeyDriver(Operator):
bl_idname = "shapekeyextras.add_drivers"
bl_label = "Add Drivers"
bl_description = "Add Drivers to Shapekeys in Selection"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
if context.object.data.shape_keys:
shape_keys = shape_key_selection(self, context)
for i in shape_keys:
if i != 'Basis':
context.object.data.shape_keys.key_blocks[i].driver_add("value")
self.report({'INFO'}, "Drivers added")
else:
self.report({'WARNING'}, "No shape keys found.")
return {'FINISHED'}
# http://stackoverflow.com/questions/7977550/how-to-change-the-value-of-the-shape-key-in-blender-script
class SKE_OT_addShapeKeyKeyframe(Operator):
bl_idname = "shapekeyextras.insert_keyframe"
bl_label = "Insert Value Keyframe"
bl_description = "Insert Keyframe (Shape Key Value) for all Shape Keys in Selection"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
if context.object.data.shape_keys:
shape_keys = shape_key_selection(self, context)
for i in shape_keys:
if i != 'Basis':
context.object.data.shape_keys.key_blocks[i].keyframe_insert(data_path="value")
self.report({'INFO'}, "Keyframes inserted")
else:
self.report({'WARNING'}, "No shape keys found.")
return {'FINISHED'}
class SKE_OT_deleteShapeKeyKeyframe (Operator):
bl_idname = "shapekeyextras.delete_keyframe"
bl_label = "Remove current Keyframe"
bl_description = "Remove current Keyframe for all Shape Keys in Selection"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
if context.object.data.shape_keys:
shape_keys = shape_key_selection(self, context)
for i in shape_keys:
if i != 'Basis':
try:
context.object.data.shape_keys.key_blocks[i].keyframe_delete(data_path="value")
self.report({'INFO'}, "Keyframes deleted.")
except:
self.report({'WARNING'}, "No Keyframe to remove.")
else:
self.report({'WARNING'}, "No shape keys found.")
return {'FINISHED'}
def invoke(self, context, event):
return context.window_manager.invoke_confirm(self, event)
# http://blender.stackexchange.com/questions/5827/get-shape-key-from-action-or-fcurve-in-python
class SKE_OT_removeAllShapeKeyKeyframes(Operator):
bl_idname = "shapekeyextras.delete_all_keyframes"
bl_label = "Remove Animation"
bl_description = "Remove all Value Keyframes for all Shape Keys in Selection"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
sce = context.scene
if context.object.data.shape_keys:
shape_keys = shape_key_selection(self, context)
sk_data = context.object.data.shape_keys
for i in shape_keys:
if i != 'Basis' and sk_data.animation_data is not None:
for f in range(sce.frame_start, sce.frame_end+1):
if sk_data.animation_data.action:
sk_data.key_blocks[i].keyframe_delete(data_path="value", frame=f) #returns a bool
self.report({'INFO'}, "All Keyframes removed.")
else:
self.report({'WARNING'}, "No shape keys found.")
return {'FINISHED'}
def invoke(self, context, event):
return context.window_manager.invoke_confirm(self, event)
class SKE_OT_removeShapeKeysSelected(Operator):
bl_idname = "shapekeyextras.remove_selection"
bl_label = "Remove Shape Keys"
bl_description = "Remove all Shape Keys in Selection"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
scn = context.scene
ske = scn.shape_key_extras
if context.object.data.shape_keys:
shape_keys = shape_key_selection(self, context)
if len(shape_keys) > 0:
for i in shape_keys:
shapekey_index = context.object.data.shape_keys.key_blocks.keys().index(i)
context.object.active_shape_key_index = shapekey_index
bpy.ops.object.shape_key_remove()
self.report({'INFO'}, "Selected Shape Keys removed")
else:
self.report({'INFO'}, "Nothing to remove")
else:
self.report({'WARNING'}, "No shape keys found.")
return {'FINISHED'}
def invoke(self, context, event):
return context.window_manager.invoke_confirm(self, event)
class SKE_OT_printShapeKeySelection(Operator):
bl_idname = "shapekeyextras.print_shape_key_selection"
bl_label = "Print Selection to Console"
bl_description = "Print Shape Key Selection to the Console"
bl_options = {'INTERNAL'}
def execute(self, context):
if context.object.data.shape_keys:
shape_keys = shape_key_selection(self, context)
self.report({'INFO'}, ('Selection: %s' % (', '.join(shape_keys))))
print ("Selection:", ', '.join(shape_keys))
else:
self.report({'WARNING'}, "No shape keys found.")
return {'FINISHED'}
class SKE_OT_moveShapeKey(bpy.types.Operator):
bl_idname = "shapekeyextras.move_shapekey"
bl_label = "Move Shape Key"
bl_description = "Move Shape Key up or down by certain amount"
bl_options = {'REGISTER', 'UNDO'}
steps: IntProperty()
action: EnumProperty(
items=(
('DOWN','Down','', 'TRIA_DOWN', 1),
('UP','Up','', 'TRIA_UP', 2)
))
@classmethod
def poll(cls, context):
return context.object.active_shape_key
def invoke(self, context, event):
return context.window_manager.invoke_props_dialog(self)
def draw(self, context):
row = self.layout
row.prop(self, "steps", text="Steps")
row.prop(self, "action", expand=True)
row.separator()
def execute(self, context):
if context.object.active_shape_key:
old_id = context.object.active_shape_key_index
for i in range(self.steps):
if self.action == 'DOWN':
bpy.ops.object.shape_key_move(type="DOWN")
else:
bpy.ops.object.shape_key_move(type="UP")
new_id = context.object.active_shape_key_index
info = 'Shape Key moved from %s to %s' % (old_id + 1, new_id + 1)
self.report({'INFO'}, info)
return {'FINISHED'}
# -------------------------------------------------------------------
# Vertex Group Operators
# -------------------------------------------------------------------
class SKE_OT_mergeVertexGroups(Operator):
bl_idname = "shapekeyextras.merge_vg_ui_list"
bl_label = "Merge Groups"
bl_description = "Merge all Groups in List"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
return (context.active_object.type == 'MESH' and
context.mode == 'OBJECT' and
len(context.active_object.vertex_groups) > 1)
def execute(self, context):
group_input = {i.name for i in context.scene.shape_key_extras_collection if i.name}
ob = context.active_object
group_lookup = {g.index: g.name for g in ob.vertex_groups}
group_candidates = {n for n in group_lookup.values() if n in group_input}
if len(group_candidates) > 1:
# iterate through the vertices and sum the weights per group
vertex_weights = {}
for vert in ob.data.vertices:
if len(vert.groups):
for item in vert.groups:
vg = ob.vertex_groups[item.group]
if vg.name in group_candidates:
if vert.index in vertex_weights:
vertex_weights[vert.index] += vg.weight(vert.index)
else:
vertex_weights[vert.index] = vg.weight(vert.index)
# create new vertex group
vgroup = ob.vertex_groups.new(name="+".join(group_candidates))
# add the values to the group
for key, value in vertex_weights.items():
vgroup.add([key], value ,'REPLACE') #'ADD','SUBTRACT'
self.report({'INFO'}, ('Merged: %s' % (', '.join(group_candidates))))
return{'FINISHED'}
else:
self.report({'WARNING'}, "No Groups to merge.")
return{'CANCELLED'}
class SKE_OT_printVertexGroups(Operator):
bl_idname = "shapekeyextras.print_vg_ui_list"
bl_label = "Print Selection"
bl_description = "Print Vertex Group Selection to Console"
bl_options = {'INTERNAL'}
def execute(self, context):
selection = {i.name for i in context.scene.shape_key_extras_collection if i.name}
if selection:
self.report({'INFO'}, ", ".join(selection))
else:
self.report({'INFO'}, "Nothing in Selection")
return{'FINISHED'}
class SKE_OT_addVertexGroups(Operator):
bl_idname = "shapekeyextras.add_all_vg_ui_list"
bl_label = "Add All Groups"
bl_description = "Add all Vertex Groups to the List"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
scn = context.scene
ske = scn.shape_key_extras
colprop = scn.shape_key_extras_collection
idx = ske.vg_uilist_index
try:
item = colprop[idx]
except IndexError:
pass
for idx, item in enumerate(colprop):
if not item.name:
colprop.remove(idx)
items = 0
for g in context.active_object.vertex_groups:
if g.name not in scn.shape_key_extras_collection:
item = colprop.add()
item.collection_id = len(colprop)
item.name = g.name
ske.vg_uilist_index = (len(colprop)-1)
items += 1
if items: info = '%s Vertex Groups added to the list' % (items)
else: info = 'Nothing to add'
self.report({'INFO'}, info)
return{'FINISHED'}
class SKE_OT_clearVertexGroups(Operator):
bl_idname = "shapekeyextras.clear_vg_ui_list"
bl_label = "Clear List"
bl_description = "Clear all Items in Vertex Group list"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
scn = context.scene
ske = scn.shape_key_extras
coll = scn.shape_key_extras_collection
if len(coll) > 0:
# reverse range to remove last item first
for i in range(len(coll)-1,-1,-1):
coll.remove(i)
self.report({'INFO'}, "All items removed")
else:
self.report({'INFO'}, "Nothing to remove")
return{'FINISHED'}
# -------------------------------------------------------------------
# UI
# -------------------------------------------------------------------
def shapekey_panel_append(self, context):
if (context.object.data.shape_keys and
context.mode == 'OBJECT'):
scn = context.scene
ske = scn.shape_key_extras
layout = self.layout
layout.separator()
box_set_attributes = layout.box()
row = box_set_attributes.row()
row.prop(ske, "sk_set_attributes",
icon="TRIA_DOWN" if ske.sk_set_attributes else "TRIA_RIGHT",
icon_only=True, emboss=False)
row.label(text="Set Shape Key Properties")
if ske.sk_set_attributes:
row = box_set_attributes.row()
col = row.column(align=True)
rowsub = col.row(align=True)
rowsub.operator("shapekeyextras.enable_all", icon="RESTRICT_VIEW_OFF")
rowsub.operator("shapekeyextras.disable_all", icon="RESTRICT_VIEW_ON")
col.operator("shapekeyextras.toggle_mute", icon="FILE_REFRESH")
col.operator("shapekeyextras.random_visibility", icon="GROUP_VERTEX")
col.separator()
#row = box_set_attributes.row()
col = box_set_attributes.column(align=True)
rowsub = col.row(align=True)
rowsub.prop(ske, "sk_selection", expand=True)
box_selection = col.box()
row = box_selection.row()
row.prop(ske, "sk_advanced_selection",
icon="TRIA_DOWN" if ske.sk_advanced_selection else "TRIA_RIGHT",
icon_only=True, emboss=False)
row.label(text="Custom Selection")
if ske.sk_advanced_selection:
row = box_selection.row()
col = row.column(align=True)
rowsub = col.row(align=True)
rowsub = col.row(align=True)
rowsub.prop(ske, "sk_exclude")
rowsub = col.column(align=True)
rowsub.prop(ske, "sk_only")
row = box_selection.row()
col = box_selection.column(align=True)
rowsub = col.row(align=True)
rowsub.operator("shapekeyextras.print_shape_key_selection", icon="CONSOLE")
col.separator()
row = box_set_attributes.row()
col = row.column(align=True)
col.prop(ske, "sk_value")
col.operator("shapekeyextras.set_values", icon="KEY_HLT")
rowsub = col.row(align=True)
rowsub.prop(ske, "sk_random_min")
rowsub.prop(ske, "sk_random_max")
col.operator("shapekeyextras.randomize", icon="KEYINGSET")
col.operator("shapekeyextras.set_range", icon="SORTSIZE")
col.separator()
row = box_set_attributes.row()
col = row.column(align=True)
rowsub = col.row(align=True)
rowsub.operator("shapekeyextras.insert_keyframe", icon="ACTION")
rowsub = col.row(align=True)
rowsub.operator("shapekeyextras.delete_keyframe", icon="PANEL_CLOSE")
rowsub.operator("shapekeyextras.delete_all_keyframes", icon="PANEL_CLOSE")
col.separator()
row = box_set_attributes.row()
col = row.column(align=True)
rowsub = col.row(align=True)
rowsub.operator("shapekeyextras.add_drivers", icon="DRIVER")
rowsub.operator("shapekeyextras.remove_drivers", icon="PANEL_CLOSE")
rowsub = col.row(align=True)
rowsub.operator("shapekeyextras.remove_selection", icon="CANCEL")
layout.separator()
class SKE_OT_vertexGroupActions(Operator):
bl_idname = "shapekeyextras.action_vg_ui_list"
bl_label = "Vertex Group List Actions"
bl_options = {'REGISTER', 'UNDO'}
action: EnumProperty(
items=(
('UP', "Up", ""),
('DOWN', "Down", ""),
('REMOVE', "Remove", ""),
('ADD', "Add", ""),
)
)
def invoke(self, context, event):
scn = context.scene
ske = scn.shape_key_extras
colprop = scn.shape_key_extras_collection
idx = ske.vg_uilist_index
try:
item = colprop[idx]
except IndexError:
pass
else:
if self.action == 'DOWN' and idx < len(colprop) - 1:
item_next = colprop[idx+1].name
ske.vg_uilist_index += 1
info = 'Item %d selected' % (ske.vg_uilist_index + 1)
self.report({'INFO'}, info)
elif self.action == 'UP' and idx >= 1:
item_prev = colprop[idx-1].name
ske.vg_uilist_index -= 1
info = 'Item %d selected' % (ske.vg_uilist_index + 1)
self.report({'INFO'}, info)
elif self.action == 'REMOVE':
first_item = True if ske.vg_uilist_index == 0 else False
if colprop[ske.vg_uilist_index].name:
info = '%s removed from list' % (colprop[ske.vg_uilist_index].name)
else:
info = 'Item %s removed from list' % (ske.vg_uilist_index +1)
if first_item:
ske.vg_uilist_index = 0
else:
ske.vg_uilist_index -= 1
self.report({'INFO'}, info)
colprop.remove(idx)
if self.action == 'ADD':
item = colprop.add()
item.collection_id = len(colprop)
#item.collection_name = ""
ske.vg_uilist_index = (len(colprop)-1)
#info = '%s added to list' % ("Empty group item")
#self.report({'INFO'}, info)
return {"FINISHED"}
class SKE_UL_vertexGroups(UIList):
def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
split = layout.split(factor=0.1)
split.scale_y = 1.1
split.label(text=str(index+1))
split.prop_search(item, "name", context.active_object, "vertex_groups", text="")
def vertexgroup_panel_append(self, context):
if (context.active_object.type == 'MESH' and
len(context.active_object.vertex_groups) > 1 and
context.mode == 'OBJECT'):
scn = context.scene
ske = scn.shape_key_extras
layout = self.layout
box_merge_vgroups = layout.box()
row = box_merge_vgroups.row()
row.prop(ske, "vg_merge_vgroups",
icon="TRIA_DOWN" if ske.vg_merge_vgroups else "TRIA_RIGHT",
icon_only=True, emboss=False
)
row.label(text="Merge Vertex Groups")
if ske.vg_merge_vgroups:
rows = 4
row = box_merge_vgroups.row()
row.template_list("SKE_UL_vertexGroups", "", scn, "shape_key_extras_collection", ske, "vg_uilist_index", rows=rows)
col = row.column(align=True)
col.operator("shapekeyextras.action_vg_ui_list", icon='ZOOM_IN', text="").action = 'ADD'
col.operator("shapekeyextras.action_vg_ui_list", icon='ZOOM_OUT', text="").action = 'REMOVE'
col.separator()
col.operator("shapekeyextras.action_vg_ui_list", icon='TRIA_UP', text="").action = 'UP'
col.operator("shapekeyextras.action_vg_ui_list", icon='TRIA_DOWN', text="").action = 'DOWN'
row = box_merge_vgroups.row()
col = row.column(align=True)
rowsub = col.row(align=True)
#rowsub.operator("shapekeyextras.print_vg_ui_list", icon="WORDWRAP_ON")
rowsub.operator("shapekeyextras.add_all_vg_ui_list", icon="WORDWRAP_ON")
rowsub.operator("shapekeyextras.clear_vg_ui_list", icon="X")
col.operator("shapekeyextras.merge_vg_ui_list", icon="STICKY_UVS_LOC")
layout.separator()
def shapekey_specials_append(self, context):
layout = self.layout
row = layout.row(align=True)
row.operator("shapekeyextras.move_shapekey", icon="PHYSICS")
class DrawShapeKeyListItem:
def draw(self, context, layout, data, item, icon, active_data, active_propname, index):
obj = active_data
key_block = item
if self.layout_type in {'DEFAULT', 'COMPACT'}:
split = layout.split(factor=0.1)
split.label(text=str(index+1))
row = split.row(align=True)
row.prop(key_block, "name", text="", emboss=False, icon_value=icon)
row = split.row(align=True)
if key_block.mute or (obj.mode == 'EDIT' and not (obj.use_shape_key_edit_mode and obj.type == 'MESH')):
row.active = False
if not item.id_data.use_relative:
row.prop(key_block, "frame", text="", emboss=False)
elif index > 0:
row.prop(key_block, "value", text="", emboss=False)
else:
row.label(text="")
row.prop(key_block, "mute", text="", emboss=False)
elif self.layout_type == 'GRID':
layout.alignment = 'CENTER'
layout.label(text="", icon_value=icon)
# built-in method ../startup/bl_ui/properties_data_mesh.py
_draw = bpy.types.MESH_UL_shape_keys.draw_item
# -------------------------------------------------------------------
# Register
# -------------------------------------------------------------------
classes = (
SKE_PG_sceneSettings,
SKE_PT_indexShapeKeys,
SKE_OT_enableShapeKeys,
SKE_OT_disableShapeKeys,
SKE_OT_toggleShapeKeys,
SKE_OT_randomShapeKeyEnable,
SKE_OT_randomShapeKeyValue,
SKE_OT_setShapeKeyRange,
SKE_OT_applyShapeKeyValue,
SKE_OT_removeShapeKeyDriver,
SKE_OT_addShapeKeyDriver,
SKE_OT_addShapeKeyKeyframe,
SKE_OT_deleteShapeKeyKeyframe,
SKE_OT_removeAllShapeKeyKeyframes,
SKE_OT_removeShapeKeysSelected,
SKE_OT_printShapeKeySelection,
SKE_OT_moveShapeKey,
SKE_OT_mergeVertexGroups,
SKE_OT_printVertexGroups,
SKE_OT_addVertexGroups,
SKE_OT_clearVertexGroups,
SKE_OT_vertexGroupActions,
SKE_UL_vertexGroups,
# DrawShapeKeyListItem,
)
def register():
from bpy.utils import register_class
for cls in classes:
register_class(cls)
bpy.types.Scene.shape_key_extras = PointerProperty(type=SKE_PG_sceneSettings)
bpy.types.Scene.shape_key_extras_collection = CollectionProperty(type=SKE_PT_indexShapeKeys)
bpy.types.DATA_PT_shape_keys.append(shapekey_panel_append)
bpy.types.DATA_PT_vertex_groups.append(vertexgroup_panel_append)
bpy.types.MESH_MT_shape_key_context_menu.append(shapekey_specials_append)
bpy.types.MESH_UL_shape_keys.draw_item = DrawShapeKeyListItem.draw
def unregister():
bpy.types.DATA_PT_shape_keys.remove(shapekey_panel_append)
bpy.types.DATA_PT_vertex_groups.remove(vertexgroup_panel_append)
bpy.types.MESH_MT_shape_key_context_menu.remove(shapekey_specials_append)
bpy.types.MESH_UL_shape_keys.draw_item = DrawShapeKeyListItem._draw
from bpy.utils import unregister_class
for cls in reversed(classes):
unregister_class(cls)
del bpy.types.Scene.shape_key_extras_collection
del bpy.types.Scene.shape_key_extras
if __name__ == "__main__":
register()