-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpose_tools.py
1182 lines (918 loc) · 40.2 KB
/
pose_tools.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
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import bpy, bmesh
from bpy.props import *
from .common import *
from mathutils import *
import bpy_types
class YToggleRestPos(bpy.types.Operator):
bl_idname = "object.y_toggle_rest_pos"
bl_label = "Toggle Rig Rest Position"
bl_description = "Toggle active armature rest position"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
return context.object and context.object.type in {'MESH', 'ARMATURE'}
def execute(self, context):
obj = context.object
if obj.type == 'MESH':
# Get armature modifier
arm_obj = None
for mod in obj.modifiers:
if mod.type == 'ARMATURE':
arm_obj = mod.object
break
if not arm_obj:
self.report({'ERROR'}, "No armature object found!")
return {'CANCELLED'}
obj = arm_obj
pos = obj.data.pose_position
obj.data.pose_position = 'REST' if pos == 'POSE' else 'POSE'
return {'FINISHED'}
def get_def_parent_name(bone, ori_bone, def_bones, parent_name=''):
if bone.parent:
#print(bone.parent.name)
if bone.parent.name.startswith('ORG-') or bone.parent.name.startswith('MCH-'):
basename = bone.parent.name[4:]
else: basename = bone.parent.name
if def_bones.get(basename):
parent_name = basename
if parent_name == '':
def_name = 'DEF-' + basename
if def_name != ori_bone.name and def_bones.get(def_name):
parent_name = def_name
if parent_name == '':
parent_name = get_def_parent_name(bone.parent, ori_bone, def_bones, parent_name)
return parent_name
def get_all_objects_using_rig_with_datas(rig):
objs = []
armods = []
ori_mod_props = []
ori_mod_idx = []
child_objs = []
parent_bones = []
for o in bpy.context.view_layer.objects:
if o.type == 'MESH':
for i, mod in enumerate(o.modifiers):
if mod.type == 'ARMATURE' and mod.object == rig:
objs.append(o)
armods.append(mod.name)
ori_mod_idx.append(i)
# Get original modifier props
dicts = {}
for prop in dir(mod):
try: dicts[prop] = getattr(mod, prop)
except: pass
ori_mod_props.append(dicts)
break
if o.parent == rig and o.parent_type == 'BONE':
child_objs.append(o)
parent_bones.append(o.parent_bone)
return objs, armods, ori_mod_props, ori_mod_idx, child_objs, parent_bones
def apply_armatures(context, objs, child_objs, armature_modifier_names, apply_above=True):
#armature_ids = []
for i, o in enumerate(objs):
#print(o, armature_modifier_names[i])
# Apply shapekeys
#if o.data.shape_keys:
# for sk in o.data.shape_keys.key_blocks:
# o.shape_key_remove(sk)
# If object is multi user, make it single user
if o.data.users > 1:
o.data = o.data.copy()
# Set object to active
context.view_layer.objects.active = o
# Check for other modifiers
if apply_above:
to_be_applied = []
for mod in o.modifiers:
if mod.name == armature_modifier_names[i]:
break
if mod.type not in {'SUBSURF'}:
to_be_applied.append(mod.name)
apply_modifiers_with_shape_keys(o, to_be_applied)
#for mod in to_be_applied:
# try: bpy.ops.object.modifier_apply(modifier=mod)
# except Exception as e: pass
# Apply armature modifier
for i, o in enumerate(objs):
context.view_layer.objects.active = o
#armature_ids.append([j for j, m in enumerate(o.modifiers) if m.name == armature_modifier_names[i]][0])
#try: bpy.ops.object.modifier_apply(modifier=armature_modifier_names[i])
#except Exception as e: pass
apply_modifiers_with_shape_keys(o, [armature_modifier_names[i]], False)
# Apply child of bones
#bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='DESELECT')
for o in child_objs:
o.select_set(True)
bpy.ops.object.parent_clear(type='CLEAR_KEEP_TRANSFORM')
#return armature_ids
def set_armature_back(context, rig, objs, child_objs, ori_mod_props, parent_bones, ori_mod_ids=[], back_to_rigify=False):
# Set armature back
for i, o in enumerate(objs):
# Set active object
context.view_layer.objects.active = o
num_mods = len(o.modifiers)
if len(ori_mod_ids) > i:
ori_mod_idx = ori_mod_ids[i]
else: ori_mod_idx = 0
mod = o.modifiers.new('Armature', 'ARMATURE')
# Recover modifier props
for prop in dir(mod):
try: setattr(mod, prop, ori_mod_props[i][prop])
except: pass
mod.object = rig
# Move up new modifier
#if self.apply_above:
for j in range(num_mods-ori_mod_idx):
bpy.ops.object.modifier_move_up(modifier=mod.name)
#else:
# for j in range(num_mods-ori_mod_idx[i]):
# bpy.ops.object.modifier_move_up(modifier=mod.name)
bpy.ops.object.select_all(action='DESELECT')
# Set back bone parent
rig.select_set(True)
context.view_layer.objects.active = rig
bpy.ops.object.mode_set(mode='POSE')
bpy.ops.pose.select_all(action='DESELECT')
ori_visibilities = []
if back_to_rigify:
if is_greater_than_400():
# Make all collection visible
for i, col in enumerate(rig.data.collections):
ori_visibilities.append(col.is_visible)
col.is_visible = True
else: rig.data.layers[29] = True
for i, o in enumerate(child_objs):
o.select_set(True)
name = parent_bones[i]
pbone = rig.pose.bones.get(name)
if not pbone:
if not back_to_rigify:
pbone = rig.pose.bones.get('DEF-'+name)
if name == 'head':
pbone = rig.pose.bones.get('DEF-spine.006')
elif name == 'neck':
pbone = rig.pose.bones.get('DEF-spine.004')
else:
if name == 'DEF-spine.006':
pbone = rig.pose.bones.get('head')
elif name == 'DEF-spine.004':
pbone = rig.pose.bones.get('neck')
elif name.startswith('DEF-'):
pbone = rig.pose.bones.get(name[4:])
if pbone:
pbone.bone.select = True
rig.data.bones.active = pbone.bone
bpy.ops.object.parent_set(type='BONE')
o.select_set(False)
pbone.bone.select = False
if back_to_rigify:
if is_greater_than_400():
# Revert back collection visibility
for i, val in enumerate(ori_visibilities):
if rig.data.collections[i].is_visible != val:
rig.data.collections[i].is_visible = val
else: rig.data.layers[29] = False
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='DESELECT')
class YAppyRigifyToMetarig(bpy.types.Operator):
bl_idname = "object.y_apply_rigify_to_metarig"
bl_label = "Apply Rigify to Metarig"
bl_description = "Apply rigify transform to metarig and regerate rigify back"
bl_options = {'REGISTER', 'UNDO'}
apply_above : BoolProperty(
name = "Apply Above Modifiers",
description = 'Apply modifiers above armature',
default = False)
@classmethod
def poll(cls, context):
#return context.object and context.object.type in {'ARMATURE'}
return context.object and context.object.type == 'ARMATURE' #and context.object.data.get("rig_id") is None
def execute(self, context):
if not hasattr(bpy.ops.pose, 'rigify_generate'):
self.report({'ERROR'}, "Rigify addon need to be installed!")
return {'CANCELLED'}
# Get rigify
rigify = context.object
# Get metarig
metarig = None
for o in context.view_layer.objects:
if o.type == 'ARMATURE' and hasattr(o.data, 'rigify_target_rig') and o.data.rigify_target_rig == rigify:
metarig = o
break
if not metarig:
self.report({'ERROR'}, "Metarig is not found!")
return {'CANCELLED'}
# Unhide metarig
ori_metarig_hide = metarig.hide_viewport
metarig.hide_viewport = False
metarig.hide_set(False)
# Get metarig parents
metarig_layer_cols = get_object_parent_layer_collections([], bpy.context.view_layer.layer_collection, metarig)
ori_metarig_hide_lcs = []
ori_metarig_hide_lccs = []
for lc in metarig_layer_cols:
ori_metarig_hide_lcs.append(lc.hide_viewport)
lc.hide_viewport = False
ori_metarig_hide_lccs.append(lc.collection.hide_viewport)
lc.collection.hide_viewport = False
# Get objects using rig
objs, armods, ori_mod_props, ori_mod_idx, child_objs, parent_bones = get_all_objects_using_rig_with_datas(rigify)
# Apply armature and modifiers above
apply_armatures(context, objs, child_objs, armods, apply_above=self.apply_above)
context.view_layer.objects.active = metarig
# Get bones that has same head and tail
bpy.ops.object.mode_set(mode='EDIT')
eb_dict = {}
for eb in metarig.data.edit_bones:
if not eb.use_connect and eb.parent:
length = (eb.parent.tail - eb.head).length
if length < 0.0001:
eb_dict[eb.parent.name] = eb.name
# Copy deform bones to metarig then apply
bpy.ops.object.mode_set(mode='POSE')
for pb in metarig.pose.bones:
bone = metarig.data.bones[pb.name]
metarig.data.bones.active = bone
subtarget_name = 'DEF-' + pb.name
if subtarget_name in rigify.data.bones:
c = pb.constraints.new('COPY_TRANSFORMS')
c.target = rigify
c.subtarget = 'DEF-' + pb.name
bpy.ops.constraint.apply(constraint=c.name, owner='BONE')
# Apply the transformed pose bones
bpy.ops.pose.select_all(action='SELECT')
bpy.ops.pose.armature_apply(selected=False)
# Match some head and tail bones to make sure its correct metarig
bpy.ops.object.mode_set(mode='EDIT')
for pbname, bname in eb_dict.items():
pb = metarig.data.edit_bones.get(pbname)
b = metarig.data.edit_bones.get(bname)
pb.tail = b.head
bpy.ops.object.mode_set(mode='POSE')
# Set armature back to rigify
set_armature_back(context, rigify, objs, child_objs, ori_mod_props, parent_bones, ori_mod_ids=ori_mod_idx, back_to_rigify=True)
# Regerate rigify
context.view_layer.objects.active = metarig
bpy.ops.object.y_regenerate_rigify()
# Recover state
context.view_layer.objects.active = rigify
metarig.hide_viewport = ori_metarig_hide
#metarig.hide_set(ori_metarig_hide)
for i, lc in enumerate(metarig_layer_cols):
lc.hide_viewport = ori_metarig_hide_lcs[i]
lc.collection.hide_viewport = ori_metarig_hide_lccs[i]
return {'FINISHED'}
class YApplyRigifyDeform(bpy.types.Operator):
bl_idname = "object.y_apply_rigiy_deform"
bl_label = "Apply Rigify Deform"
bl_description = "Apply Rigify Deform"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
return context.object and context.object.type in {'ARMATURE'}
#return context.object and context.object.type == 'ARMATURE' and context.active_object.data.get("rig_id") is None
def execute(self, context):
if not hasattr(bpy.ops.pose, 'rigify_generate'):
self.report({'ERROR'}, "Rigify addon need to be installed!")
return {'CANCELLED'}
scene = context.scene
ori = context.object
# RECOVER RIG
if ori.name.startswith('APPLIED-'):
# Get original rig
rig = context.view_layer.objects.get(ori.name[8:])
if not rig: return {'CANCELLED'}
# Unhide original rig
rig.hide_set(False)
rig.hide_viewport = False
rig.data.pose_position = 'REST'
# Get objects using rig
objs, armods, ori_mod_props, ori_mod_idx, child_objs, parent_bones = get_all_objects_using_rig_with_datas(ori)
# Remove parents
context.view_layer.objects.active = ori
ori.select_set(True)
bpy.ops.object.mode_set(mode='EDIT')
for bone in ori.data.edit_bones:
bone.parent = None
# Add constraints
bpy.ops.object.mode_set(mode='POSE')
for bone in ori.pose.bones:
c = bone.constraints.new('COPY_TRANSFORMS')
c.target = rig
c.subtarget = bone.name
bpy.ops.object.mode_set(mode='OBJECT')
#return {'FINISHED'}
# Apply armature and modifiers above
apply_armatures(context, objs, child_objs, armods)
# Apply armature deform
context.view_layer.objects.active = ori
bpy.ops.object.mode_set(mode='POSE')
bpy.ops.pose.armature_apply(selected=False)
# Remove constraints
for bone in ori.pose.bones:
for bc in bone.constraints:
bone.constraints.remove(bc)
# Get rigify bone length
bpy.context.view_layer.objects.active = rig
bpy.ops.object.mode_set(mode='POSE')
lengths = {}
for b in ori.data.bones:
bone = rig.pose.bones.get(b.name)
lengths[b.name] = abs((bone.tail - bone.head).length)
bpy.ops.object.mode_set(mode='OBJECT')
# Scale bones according to proportions
bpy.context.view_layer.objects.active = ori
for bone in ori.pose.bones:
l = abs((bone.tail - bone.head).length)
proportion = lengths[bone.name] / l
bone.scale.y *= proportion
# Set armature back to ori
set_armature_back(context, ori, objs, child_objs, ori_mod_props, parent_bones, back_to_rigify=False)
objs, armods, ori_mod_props, ori_mod_idx, child_objs, parent_bones = get_all_objects_using_rig_with_datas(ori)
# Apply again
apply_armatures(context, objs, child_objs, armods)
#return {'FINISHED'}
# Scale some bones
# Delete applied rig
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='DESELECT')
context.view_layer.objects.active = ori
ori.select_set(True)
bpy.ops.object.delete()
# Set armature back again
set_armature_back(context, rig, objs, child_objs, ori_mod_props, parent_bones, back_to_rigify=True)
context.view_layer.objects.active = rig
rig.select_set(True)
rig.data.pose_position = 'POSE'
return {'FINISHED'}
# APPLY RIG
# Get objects using rig
objs, armods, ori_mod_props, ori_mod_idx, child_objs, parent_bones = get_all_objects_using_rig_with_datas(ori)
# Apply armature and modifiers above
apply_armatures(context, objs, child_objs, armods)
#return {'FINISHED'}
# Copy rig object
rig = ori.copy()
rig.data = rig.data.copy()
scene.collection.objects.link(rig)
context.view_layer.objects.active = rig
rig.select_set(True)
ori.select_set(False)
rig.name = 'APPLIED-' + ori.name
# Remove drivers
for d in reversed(rig.animation_data.drivers):
rig.animation_data.drivers.remove(d)
for d in reversed(rig.data.animation_data.drivers):
rig.data.animation_data.drivers.remove(d)
# Remove non deform bones
bpy.ops.object.mode_set(mode='EDIT')
for bone in rig.data.edit_bones:
if not bone.use_deform:
rig.data.edit_bones.remove(bone)
# Remove constraints
bpy.ops.object.mode_set(mode='POSE')
for bone in rig.pose.bones:
for bc in bone.constraints:
bone.constraints.remove(bc)
# Set layer
if is_greater_than_400():
for c in rig.data.collections:
c.is_visible = True
else:
for i in range(32):
rig.data.layers[i] = True
# Get missing parents
bpy.ops.object.mode_set(mode='EDIT')
parent_dict = {}
for bone in rig.data.edit_bones:
ori_bone = ori.data.bones[bone.name]
if not bone.parent:
#print()
parent_name = get_def_parent_name(ori_bone, ori_bone, rig.data.bones)
#print(parent_name)
bone.parent = rig.data.edit_bones.get(parent_name)
if bone.parent:
parent_dict[bone.name] = bone.parent.name
# Temporarily remove all parents for saving scale data
for bone in rig.data.edit_bones:
bone.parent = None
# Unlock transformation
bpy.ops.object.mode_set(mode='POSE')
for bone in rig.pose.bones:
bone.lock_location[0] = False
bone.lock_location[1] = False
bone.lock_location[2] = False
bone.lock_rotation_w = False
bone.lock_rotation[0] = False
bone.lock_rotation[1] = False
bone.lock_rotation[2] = False
bone.lock_scale[0] = False
bone.lock_scale[1] = False
bone.lock_scale[2] = False
# Add constraints
for bone in rig.pose.bones:
c = bone.constraints.new('COPY_TRANSFORMS')
c.target = ori
c.subtarget = bone.name
# Bake constraint
#bpy.ops.nla.bake(frame_start=1, frame_end=1, only_selected=False, visual_keying=True, clear_constraints=True, bake_types={'POSE'})
# Remove action
#action = rig.animation_data.action
#rig.animation_data.action = None
#bpy.data.actions.remove(action)
## Save scale data
#for bone in rig.pose.bones:
# b = rig.data.y_applied_bones.add()
# b.name = bone.name
# b.scale = (bone.scale[0], bone.scale[1], bone.scale[2])
#for b in rig.data.y_applied_bones:
# print(b.name, b.scale[0], b.scale[1], b.scale[2])
# Apply pose
bpy.ops.pose.armature_apply(selected=False)
#return {'FINISHED'}
# Remove constraints
bpy.ops.object.mode_set(mode='POSE')
for bone in rig.pose.bones:
for bc in bone.constraints:
bone.constraints.remove(bc)
# Reparent
bpy.ops.object.mode_set(mode='EDIT')
for key, val in parent_dict.items():
bone = rig.data.edit_bones.get(key)
parent = rig.data.edit_bones.get(val)
if bone and parent: bone.parent = parent
bpy.ops.object.mode_set(mode='OBJECT')
# Set armature back
set_armature_back(context, rig, objs, child_objs, ori_mod_props, parent_bones, back_to_rigify=False)
context.view_layer.objects.active = rig
rig.select_set(True)
# Hide original rig
ori.hide_set(True)
ori.hide_viewport = True
return {'FINISHED'}
class YRegenerateRigify(bpy.types.Operator):
bl_idname = "object.y_regenerate_rigify"
bl_label = "Regenerate Rigify"
bl_description = "Regenerate Rigify and make sure all the pose bones transform stays"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
#return context.object and context.object.type in {'ARMATURE'}
return context.object and context.object.type == 'ARMATURE' and context.object.data.get("rig_id") is None
def execute(self, context):
if not hasattr(bpy.ops.pose, 'rigify_generate'):
self.report({'ERROR'}, "Rigify addon need to be installed!")
return {'CANCELLED'}
metarig = context.object
rig = metarig.data.rigify_target_rig
rig_name = rig.name
if not rig:
self.report({'ERROR'}, "Target rig need to be set!")
return {'CANCELLED'}
# Remember stuffs
ori_mode = rig.mode
rig_data_dict = {}
props = dir(rig.data)
for prop in props:
if prop.startswith('__') or prop in ['bl_rna', 'rna_type']: continue
try: rig_data_dict[prop] = getattr(rig.data, prop)
except:
print('Cannot get prop', prop, 'from', rig.data.name)
continue
#ori_pose_position = rig.data.pose_position
# Make sure to unhide rig
rig.hide_set(False)
# Set rig object to active
context.view_layer.objects.active = rig
bpy.ops.object.mode_set(mode='POSE', toggle=False)
# Enable all layers
ori_layer_enables = []
if is_greater_than_400():
for c in rig.data.collections:
if c.is_visible: ori_layer_enables.append(c.name)
c.is_visible = True
else:
for i in range(32):
if rig.data.layers[i]: ori_layer_enables.append(i)
rig.data.layers[i] = True
# Copy all pose bones
bpy.ops.pose.select_all(action='SELECT')
bpy.ops.pose.copy()
# Check contraints that need to be kept
pbcos = {}
for pb in rig.pose.bones:
for co in pb.constraints:
if co.name.startswith('KEEP-'):
if pb.name not in pbcos:
pbcos[pb.name] = []
co_dict = {}
props = dir(co)
for prop in props:
if prop.startswith('__') or prop in ['bl_rna', 'rna_type']: continue
try: co_dict[prop] = getattr(co, prop)
except:
print('Cannot get prop', prop, 'from', co.name)
continue
pbcos[pb.name].append(co_dict)
# Check NLA data (still incomplete since this only keep basic action data)
acts = []
frame_starts = []
for track in rig.animation_data.nla_tracks:
if len(track.strips) == 0: continue
strip = track.strips[0]
if strip.action:
acts.append(strip.action)
frame_starts.append(strip.frame_start)
# Get action
action = rig.animation_data.action
# Select metarig
context.view_layer.objects.active = metarig
# Old rigify flag
is_old_rigify = False
# Update metarig for Blender 4.0 or above
if is_greater_than_400():
if 'Layer 1' in metarig.data.collections:
is_old_rigify = True
bpy.ops.armature.rigify_upgrade_layers()
# Regenerate rigify
bpy.ops.pose.rigify_generate()
# Paste to new rig
rig = bpy.data.objects.get(rig_name)
context.view_layer.objects.active = rig
bpy.ops.object.mode_set(mode='POSE', toggle=False)
bpy.ops.pose.paste()
# Recreate constraints
for pb_name, co_lists in pbcos.items():
pb = rig.pose.bones.get(pb_name)
if not pb:
print('No bone named', pb_name)
continue
for co_dict in co_lists:
co = pb.constraints.new(co_dict['type'])
for key, value in co_dict.items():
try: setattr(co, key, value)
except: pass
# Set action
rig.animation_data.action = action
# Recover NLA data
for i, act in enumerate(acts):
track = rig.animation_data.nla_tracks.new()
strip = track.strips.new(act.name, int(frame_starts[i]), act)
# Enable layers
if is_greater_than_400():
if not is_old_rigify:
for c in rig.data.collections:
c.is_visible = True if c.name in ori_layer_enables else False
else:
for i in range(32):
rig.data.layers[i] = True if i in ori_layer_enables else False
# Recover stuffs
bpy.ops.object.mode_set(mode=ori_mode, toggle=False)
#rig.data.pose_position = ori_pose_position
for key, value in rig_data_dict.items():
try: setattr(rig.data, key, value)
except: pass
return {'FINISHED'}
class YApplyArmature(bpy.types.Operator):
bl_idname = "object.y_apply_armature"
bl_label = "Advanced Apply Armature"
bl_description = "Apply Armature as Rest Pose and all meshes using it will also follows"
bl_options = {'REGISTER', 'UNDO'}
apply_above : BoolProperty(
name = "Apply Above Modifiers",
description = 'Apply modifiers above armature',
default = True)
@classmethod
def poll(cls, context):
return context.object and context.object.type in {'MESH', 'ARMATURE'}
def invoke(self, context, event):
return context.window_manager.invoke_props_dialog(self, width=420)
def draw(self, context):
self.layout.label(text='WARNING: Multi user objects will be single user if using the armature deform', icon='ERROR')
self.layout.label(text='(Object with bone parent will not affected though)', icon='ERROR')
self.layout.prop(self, 'apply_above', text='Apply Modifiers above armature')
def execute(self, context):
obj = context.object
rig_obj = None
if obj.type == 'MESH':
for mod in obj.modifiers:
if mod.type == 'ARMATURE':
rig_obj = mod.object
break
elif obj.type == 'ARMATURE':
rig_obj = obj
if not rig_obj:
self.report({'ERROR'}, "No rig object found!")
return {'CANCELLED'}
# Never forget
ori_selects = [o for o in context.view_layer.objects if o.select_get()]
ori_hide_viewports = [o for o in context.view_layer.objects if o.hide_viewport]
ori_hide_selects = [o for o in context.view_layer.objects if o.hide_select]
ori_active = obj
ori_mode = obj.mode
ori_col_hide_viewports = [col for col in context.view_layer.layer_collection.children if col.hide_viewport]
ori_col_hide_selects = [col for col in bpy.data.collections if col.hide_select]
# Unhide all
for o in context.view_layer.objects:
if o.hide_viewport:
o.hide_viewport = False
if o.hide_select:
o.hide_select = False
for col in context.view_layer.layer_collection.children:
if col.hide_viewport:
col.hide_viewport = False
for col in bpy.data.collections:
if col.hide_select:
col.hide_select = False
#return {'FINISHED'}
# Go to object mode
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='DESELECT')
# Get all objects using the rig
objs = []
armods = []
ori_mod_props = []
ori_mod_idx = []
child_objs = []
parent_bones = []
for o in context.view_layer.objects:
# Check if objects has shape keys
#if o.data and hasattr(o.data, 'shape_keys') and o.data.shape_keys:
# continue
if o.type == 'MESH':
for i, mod in enumerate(o.modifiers):
if mod.type == 'ARMATURE' and mod.object == rig_obj:
objs.append(o)
armods.append(mod.name)
ori_mod_idx.append(i)
# Get original modifier props
dicts = {}
for prop in dir(mod):
try: dicts[prop] = getattr(mod, prop)
except: pass
ori_mod_props.append(dicts)
break
if o.parent == rig_obj and o.parent_type == 'BONE':
child_objs.append(o)
parent_bones.append(o.parent_bone)
# Apply armature and modifiers above
for i, o in enumerate(objs):
# If object is multi user, make it single user
if o.data.users > 1:
o.data = o.data.copy()
# Set object to active
context.view_layer.objects.active = o
# Check for mirror modifiers
to_be_applied = []
if self.apply_above:
for mod in o.modifiers:
if mod.name == armods[i]:
break
if mod.type not in {'SUBSURF'}:
#bpy.ops.object.modifier_apply(modifier=mod.name)
to_be_applied.append(mod.name)
#for mod in to_be_applied:
# bpy.ops.object.modifier_apply(modifier=mod.name)
apply_modifiers_with_shape_keys(o, to_be_applied)
# Apply armature modifier
#bpy.ops.object.modifier_apply(modifier=armods[i])
apply_modifiers_with_shape_keys(o, [armods[i]], False)
# Apply child of bones
for o in child_objs:
o.select_set(True)
bpy.ops.object.parent_clear(type='CLEAR_KEEP_TRANSFORM')
# Apply armature
bpy.ops.object.select_all(action='DESELECT')
context.view_layer.objects.active = rig_obj
rig_obj.select_set(True)
bpy.ops.object.mode_set(mode='POSE')
bpy.ops.pose.armature_apply(selected=False)
# Back to object mode
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='DESELECT')
# Set armature back
for i, o in enumerate(objs):
# Set active object
context.view_layer.objects.active = o
num_mods = len(o.modifiers)
mod = o.modifiers.new('Armature', 'ARMATURE')
#mod.object = rig_obj
# Recover modifier props
for prop in dir(mod):
try: setattr(mod, prop, ori_mod_props[i][prop])
except: pass
# Move up new modifier
if self.apply_above:
for j in range(num_mods):
bpy.ops.object.modifier_move_up(modifier=mod.name)
else:
for j in range(num_mods-ori_mod_idx[i]):
bpy.ops.object.modifier_move_up(modifier=mod.name)
bpy.ops.object.select_all(action='DESELECT')
# Set back bone parent
rig_obj.select_set(True)
context.view_layer.objects.active = rig_obj
bpy.ops.object.mode_set(mode='POSE')
bpy.ops.pose.select_all(action='DESELECT')
for i, o in enumerate(child_objs):
o.select_set(True)
pbone = rig_obj.pose.bones.get(parent_bones[i])
pbone.bone.select = True
rig_obj.data.bones.active = pbone.bone
bpy.ops.object.parent_set(type='BONE')
o.select_set(False)
pbone.bone.select = False
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='DESELECT')
# Recover hide and select
for col in ori_col_hide_viewports:
col.hide_viewport = True
for col in ori_col_hide_selects:
col.hide_select = True
for o in ori_hide_selects:
o.hide_select = True
for o in ori_hide_viewports:
o.hide_viewport = True
for o in ori_selects:
o.select_set(True)
context.view_layer.objects.active = ori_active
bpy.ops.object.mode_set(mode=ori_mode)
return {'FINISHED'}
class YLoopKeyframes(bpy.types.Operator):
bl_idname = "pose.y_loop_keyframes"
bl_label = "Loop Keyframes"
bl_description = "Loop keyframes for the first 2 keyframes of selected pose bones"
bl_options = {'REGISTER', 'UNDO'}
active_bone_only : BoolProperty(
name = 'Active Bone Only',
description = 'Loop keyframes only on active bone only',
default=False)
@classmethod
def poll(cls, context):
return context.object #and context.object.mode == 'POSE'
def execute(self, context):
obj = context.object
scene = context.scene
active_bone = None
if self.active_bone_only:
if obj.mode == 'POSE':
active_bone = context.active_pose_bone
if not active_bone:
self.report({'ERROR'}, "Must select a pose bone!")
return {'CANCELLED'}
objects = [obj]
elif context.area.type == 'DOPESHEET_EDITOR' and context.space_data.ui_mode != 'DOPESHEET':
objects = [obj]
else:
objects = context.view_layer.objects
actions = []
parent_strings = []
parents = []