Repository navigation
Expand file tree
/
Copy pathcopyrig.py
More file actions
3675 lines (2803 loc) · 139 KB
/
Copy pathcopyrig.py
File metadata and controls
3675 lines (2803 loc) · 139 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
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
### Copyright 2021 The Machinimatrix Team
###
### This file is part of Tamagoyaki
###
### The module has been created based on this document:
### A Beginners Guide to Dual-Quaternions:
### http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.407.9047
###
### 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 #####
import bpy
import bmesh
import sys
import logging
import os
import mathutils
from bpy.props import *
from bpy.types import Menu, Operator
from bl_operators.presets import AddPresetBase
from bpy_extras.io_utils import ImportHelper
from bpy.app.handlers import persistent
from . import armature_util, context_util, const, propgroups, bind, create, data, mesh, messages, rig, shape, util
from .const import *
from .context_util import *
from .util import PVector, mulmat
from .data import Skeleton
CONST_NAMES_MAP = {}
CONST_NAMES_MAP['TrackTo']='Track To'
BONE_DATA_NAME = 0 # "name"
BONE_DATA_HEAD = 1 # "data head"
BONE_DATA_TAIL = 2 # "data tail"
BONE_DATA_SELECT = 3 # "select"
BONE_DATA_DEFORM = 4 # "deform"
BONE_DATA_ROLL = 5 # "roll"
BONE_DATA_MATRIX = 6 # "matrix"
BONE_DATA_PARENT = 7 # Parent name, or None if no parent
BONE_POSE_GROUP = 8 # Name of associated bone group, or None if no group assigned to Bone
BONE_POSE_COLORSET = 9 # Name of associated colorset, or None if no color set assigned to Bone group
BONE_DATA_CONNECT = 10 # Bone Connect state
BONE_DATA_CONSTRAINT = 11 # Bone Constraint data [type,influence,mute]
BONE_DATA_IKLIMITS = 12 # Bone IK Limits: bone.use_ik_limit_x/y/z
BONE_POSE_ROTATION_MODE = 13
BONE_DATA_HIDE = 14
BONE_DATA_COUNT = 15
log=logging.getLogger("tamagoyaki.copyrig")
registerlog = logging.getLogger("tamagoyaki.register")
def is_animated_mesh(obj, src_armature):
if obj.type != 'MESH': return False
has_modifier=False
for mod in [mod for mod in obj.modifiers if mod.type=='ARMATURE']:
if mod.object == src_armature:
return True
return False
def get_joint_data(armobj):
joint_data = [armobj.get('sl_joints'),
armobj.data.JointOffsetList
]
return joint_data
def copy_collection(toCollection, fromCollection):
toCollection.clear()
for elem in fromCollection:
item = toCollection.add()
for k, v in elem.items():
item[k] = v
def set_joint_data(armobj, joint_data):
sl_joints = joint_data[0]
JointOffsetList = joint_data[1]
if sl_joints:
armobj['sl_joints'] = sl_joints
if JointOffsetList:
copy_collection(armobj.data.JointOffsetList, JointOffsetList)
def copy_tamagoyaki(self,
context,
tgt_rig_type,
active_obj,
src_armature,
bone_repair,
mesh_repair
):
log.info("+=========================================================================")
log.info("| copy: Starting a %s Tamagoyaki copy from \"%s\"" % (tgt_rig_type, src_armature.name))
log.info("+=========================================================================")
no_mesh = not mesh_repair
jointType = src_armature.RigProp.JointType
skeletonType = src_armature.RigProp.SkeletonType
src_scale = src_armature.scale.copy()
src_rig_type = self.srcRigType
active_is_arm = active_obj == src_armature
selected_object_names = util.get_selected_object_names(context)
actsel = util.object_select_get(active_obj)
scene = context.scene
util.set_active_object(context, src_armature)
util.ensure_mode_is('OBJECT')
action = src_armature.animation_data.action
shape_data = shape.copy_to_scene(scene, src_armature).copy()
shape.reset_to_restpose(context, src_armature)
joint_data = get_joint_data(src_armature)
if self.applyRotation:
log.info("| Apply Rot&Scale on [%s]" % util.get_active_object(context).name)
log.info("| copy: Apply Visual transforms, Rotation and Scale to %s" % src_armature.name)
old_state = util.select_hierarchy(src_armature) # All of them!
bpy.ops.object.visual_transform_apply() #To get around some oddities with the apply tool (possible blender bug?)
util.restore_hierarchy(context, old_state)
util.update_view_layer(context)
util.ensure_mode_is("POSE")
use_restpose = src_armature.data.pose_position
src_armature.data.pose_position = 'POSE'
bpy.ops.pose.select_all(action="SELECT")
log.info("| copy pose from %s to pose buffer" % (src_armature.name) )
bpy.ops.pose.copy()
src_armature.data.pose_position = 'REST'
util.ensure_mode_is('OBJECT')
for arm in self.targets:
util.set_active_object(context, arm)
shape.reset_to_restpose(context, arm)
util.set_active_object(context, src_armature)
log.info("+=========================================================================")
self.copy_skeletons(context, src_armature, self.targets, src_rig_type, tgt_rig_type, skeletonType, transfer_joints=True, sync=False)
log.info("+=========================================================================")
shape.fromDictionary(src_armature, shape_data)
if active_is_arm:
active_obj = self.targets[0]
util.restore_object_select_states(context, selected_object_names)
oselector = util.set_disable_update_slider_selector(True)
tgt_armatures=[]
for arm in self.targets:
log.info("| Post actions on armature [%s]" % (arm.name) )
util.set_active_object(context, arm)
if arm.RigProp.rig_use_bind_pose != src_armature.RigProp.rig_use_bind_pose:
log.info("| Set use bind pose to %s" % src_armature.RigProp.rig_use_bind_pose)
arm.RigProp.rig_use_bind_pose = src_armature.RigProp.rig_use_bind_pose
shape.fromDictionary(arm, shape_data)
set_joint_data(arm, joint_data)
arm.scale = src_scale
if self.transferMeshes:
log.info("| Copy Meshes from [%s] to [%s]" % (src_armature.name, arm.name) )
self.move_objects_to_target(context, self.sources, arm, self.srcRigType) # copy from Tamagoyaki to Tamagoyaki
if self.appearance_enabled:
log.info("| Attach Sliders to Armature [%s]" % (arm.name) )
arm.ObjectProp.slider_selector = 'SL'
util.set_active_object(context, arm)
util.enforce_armature_update(context,arm)
tgt_armatures.append(arm)
util.set_disable_update_slider_selector(oselector)
util.set_active_object(context, active_obj)
util.object_select_set(active_obj, actsel)
if action:
active_obj.animation_data.action = action
log.info("| Assigned Action %s to %s" % (action.name, active_obj.name) )
log.info("+=========================================================================")
util.ensure_mode_is(self.active_mode)
return tgt_armatures
def convert_to_tamagoyaki(self,
context,
tgt_rig_type,
active_obj,
src_armature,
inplace_transfer,
bone_repair,
mesh_repair,
transfer_joints=True
):
log.info("+=========================================================================")
log.info("| convert: Starting a %s Tamagoyaki conversion from \"%s\"" % (tgt_rig_type, src_armature.name))
log.info("+=========================================================================")
no_mesh = not mesh_repair
jointType = src_armature.RigProp.JointType
skeletonType = src_armature.RigProp.SkeletonType
src_rig_type = self.srcRigType
active_is_arm = active_obj == src_armature
selected_object_names = util.get_selected_object_names(context)
actsel = util.object_select_get(active_obj)
scene = context.scene
util.set_active_object(context, src_armature)
util.ensure_mode_is('OBJECT')
action = src_armature.animation_data.action
if inplace_transfer:
if self.applyRotation:
log.warning("convert_to_tamagoyaki: Apply Visual transforms, Rotation and Scale to %s" % src_armature.name)
old_state = util.select_hierarchy(src_armature) # All of them!
bpy.ops.object.visual_transform_apply() #To get around some oddities with the apply tool (possible blender bug?)
try:
bpy.ops.object.transform_apply(rotation=True, scale=True)
except Exception as e:
log.error("Can not apply Object transformations to linked objects (Ignoring)")
util.restore_hierarchy(context, old_state)
util.update_view_layer(bpy.context)
util.ensure_mode_is("POSE")
use_restpose = src_armature.data.pose_position
src_armature.data.pose_position = 'POSE'
bpy.ops.pose.select_all(action="SELECT")
bpy.ops.pose.copy()
log.warning("copyrig-convert-tamagoyaki: copy pose from %s to pose buffer" % (src_armature.name) )
src_armature.data.pose_position = 'REST'
util.ensure_mode_is('OBJECT')
tgt_armature = create.createAvatar(context, quads=True, use_restpose=use_restpose, rigType=tgt_rig_type, jointType=jointType, skeletonType=skeletonType, no_mesh=no_mesh)
propgroups.gender_update(tgt_armature, self.use_male_shape)
self.targets.append(tgt_armature)
self.copy_skeletons(context, src_armature, self.targets, src_rig_type, tgt_rig_type, skeletonType, transfer_joints, sync=False)
if active_is_arm:
active_obj = self.targets[0]
util.restore_object_select_states(context, selected_object_names)
is_fitted=False
oselector = util.set_disable_update_slider_selector(True)
for arm in self.targets:
log.debug("3:target_armature has %d bones" % len(arm.data.bones))
util.set_active_object(context, arm)
if self.sl_bone_rolls:
rig.restore_source_bone_rolls(arm)
arm.RigProp.rig_use_bind_pose = util.use_sliders(context) and src_armature.RigProp.rig_use_bind_pose
self.move_objects_to_target(context, self.sources, arm, self.srcRigType) # Convert to tamagoyaki
if self.appearance_enabled:
arm.ObjectProp.slider_selector = 'SL'
for child in util.get_animated_meshes(context, arm, with_tamagoyaki=False):
if self.appearance_enabled:
util.set_active_object(context, child)
child.ObjectProp.slider_selector = 'SL'
if not is_fitted:
for key in child.vertex_groups.keys():
if key in data.get_volume_bones():
is_fitted = True
util.object_select_set(arm, True)
util.set_active_object(context, arm)
bpy.ops.tamagoyaki.armature_deform_enable(set='VOL')
break
if not inplace_transfer:
util.set_active_object(context, arm)
bpy.ops.tamagoyaki.apply_shape_sliders()
util.enforce_armature_update(context,arm)
util.set_disable_update_slider_selector(oselector)
util.set_active_object(context, active_obj)
util.object_select_set(active_obj, actsel)
if action:
active_obj.animation_data.action = action
log.warning("copyrig-convert-tamagoyaki: Assigned Action %s to %s" % (action.name, active_obj.name) )
util.ensure_mode_is('POSE')
bpy.ops.pose.select_all(action="SELECT")
src_armature.data.pose_position = 'POSE'
bpy.ops.pose.paste()
log.warning("copyrig-convert-tamagoyaki: paste pose from pose buffer to %s" % (active_obj.name) )
util.ensure_mode_is(self.active_mode)
return active_obj
def update_tamagoyaki(self,
context,
tgt_rig_type,
active_obj,
src_armature,
structure_repair,
mesh_repair,
transfer_joints=True
):
def adjust_shape_keys(child, src_armature, tgt_armature):
skeys = child.data.shape_keys
if not skeys:
return
animdata = skeys.animation_data
if not animdata:
return
for driver in animdata.drivers:
dr = driver.driver
vars = None
try:
vars = dr.variables
except:
pass
if not vars:
continue
for var in vars:
log.info("adjusting var %s : %s" % (child.name, var.name) )
for target in var.targets:
if target.id_type == 'OBJECT' and target.id == src_armature:
target.id = tgt_armature
def check_container_exists(armobj, msg=""):
for child in armobj.children:
if '_meshes' in child.name and child.type =='Empty':
return True
log.warning("%s: Avatar_meshes is gone from %s" % (msg, armobj.name) )
return False
def apply_scale_and_rotation(context, src_armature, sources):
scene = context.scene
oactive = util.get_active_object(context)
omode = util.ensure_mode_is('OBJECT')
apply_set = []
for obj in sources:
if not util.is_identity(obj.matrix_local):
apply_set.append(obj)
if not apply_set:
log.info("| update tamagoyaki: Apply Rot&Scale not necessary (All children of %s are clean)" % oactive)
return
log.info("| update tamagoyaki: Apply Visual transforms, Rotation and Scale to %s" % oactive.name)
select_states = util.get_select_and_hide(context.scene.objects, False, False, False)
util.set_select(apply_set, reset=False)
log.info("| update tamagoyaki: Apply Rot&Scale to %d children of [%s]" % (len(apply_set), oactive.name))
bpy.ops.object.visual_transform_apply() #To get around some oddities with the apply tool (possible blender bug?)
try:
bpy.ops.object.transform_apply(location=False, rotation=True, scale=True)
except Exception as e:
log.error("| update tamagoyaki: Can not apply Object transformations to linked objects (Ignoring)")
util.set_active_object(context, oactive)
util.object_select_set(oactive, True)
util.ensure_mode_is(omode)
util.set_select_and_hide(context, select_states)
util.update_view_layer(context)
src_rig_type = self.srcRigType
selected_object_names = util.get_selected_object_names(context)
armature_name = src_armature.name
jointType = self.JointType
skeletonType = self.SkeletonType
log.warning("+========================================================================")
log.warning("| update : %s Rig Update for Armature \"%s\"" % (tgt_rig_type, armature_name))
log.warning("| joint type : %s" % (jointType) )
log.warning("| Skeleton type : %s" % (skeletonType) )
if self.must_rebind():
log.warning("| rebinding : %d rebind candidates" % len(self.sources))
log.warning("+========================================================================")
no_mesh = not mesh_repair
active_is_arm = active_obj == src_armature
actsel = util.object_select_get(active_obj)
scene = context.scene
util.set_active_object(context, src_armature)
util.ensure_mode_is('EDIT')
use_bind_pose = util.use_sliders(context) and src_armature.RigProp.rig_use_bind_pose
if rig.has_modified_joint_info(src_armature):
bpy.ops.tamagoyaki.armature_jointpos_store(store_as_bind_pose=False)
util.ensure_mode_is('OBJECT')
if not src_armature.animation_data:
src_armature.animation_data_create()
action = src_armature.animation_data.action
if self.must_rebind():
objects_to_rebind = [o for o in src_armature.children]
log.warning("Unparent sources: %s" % [ob.name for ob in self.sources] )
util.unparent_selection(context, objects_to_rebind, type='CLEAR_KEEP_TRANSFORM', clear_armature=True)
else:
objects_to_rebind = []
if self.applyRotation:
apply_scale_and_rotation(context, src_armature, self.sources)
shape_data = shape.asDictionary(src_armature, full=True)
shape.reset_to_restpose(context, src_armature)
util.ensure_mode_is("POSE")
bone_store = initialise_bone_setup(context, src_armature)
use_restpose = src_armature.data.pose_position
src_armature.data.pose_position = 'POSE'
bpy.ops.pose.select_all(action="SELECT")
bpy.ops.pose.copy()
log.info("update: copy pose from %s to pose buffer" % (src_armature.name) )
src_armature.data.pose_position = 'REST'
util.ensure_mode_is('OBJECT')
if mesh_repair:
self.sources = [s for s in self.sources if not 'tamagoyaki-mesh' in s]
container = find_avatar_mesh_container(context, src_armature, msg="")
if container:
util.remove_object(context, container, recursive=True)
else:
shape.manage_tamagoyaki_shapes(context, src_armature, [])
log.warning("| Creating the new Tamagoyaki Rig")
tgt_armature = create.createAvatar(context, quads=True, use_restpose=True, rigType=tgt_rig_type, jointType=jointType, skeletonType=skeletonType, no_mesh=no_mesh)
util.copy_collection_visibility(context, tgt_armature, src_armature)
if tgt_armature.ShapeDrivers.male_80 != self.use_male_shape:
ofreeze = tgt_armature.ShapeDrivers.Freeze
tgt_armature.ShapeDrivers.Freeze = True
propgroups.gender_update(tgt_armature, self.use_male_shape)
tgt_armature.ShapeDrivers.Freeze = ofreeze
self.targets.append(tgt_armature)
log.debug("| Copy the Rig info")
self.copy_skeletons(context, src_armature, [tgt_armature], src_rig_type, tgt_rig_type, skeletonType, transfer_joints, sync=False, bone_store=bone_store)
Skeleton.get_toe_hover(tgt_armature, reset=True, bind=True)
tgt_armature.scale = src_armature.scale.copy()
tgt_armature.RigProp.rig_use_bind_pose = use_bind_pose
if shape_data:
shape.fromDictionary(src_armature, shape_data, update=True, init=True)
util.ensure_mode_is('OBJECT')
log.debug("| Restore Object Select states")
util.restore_object_select_states(context, selected_object_names)
util.set_active_object(context, tgt_armature)
util.ensure_mode_is('OBJECT')
src_armature.name = 'del_' + armature_name
tgt_armature.name = armature_name
is_fitted=False
oselector = util.set_disable_update_slider_selector(True)
log.debug("update: target_armature has %d bones" % len(tgt_armature.data.bones))
arm = tgt_armature
if True:
if self.sl_bone_rolls:
rig.restore_source_bone_rolls(arm)
if arm.RigProp.rig_use_bind_pose != use_bind_pose:
arm.RigProp.rig_use_bind_pose = use_bind_pose
util.object_show_in_front(arm, util.object_show_in_front(src_armature))
arm.data.show_bone_custom_shapes = src_armature.data.show_bone_custom_shapes
log.debug("| move objects to target")
all_children = util.getChildren(src_armature)
self.move_objects_to_target(context, all_children, arm, self.srcRigType, src_armature) # update tamagoyaki
if self.appearance_enabled:
arm.ObjectProp.slider_selector = 'SL'
shape.destroy_shape_info(context, arm)
log.debug("| update: Checking Custom meshes of %s" % arm.name)
log.debug("| adjust shape keys...")
for child in util.get_animated_meshes(context, arm, with_tamagoyaki=False):
adjust_shape_keys(child, src_armature, arm)
log.info("update: Checking Mesh %s" % child.name)
if not is_fitted:
for key in child.vertex_groups.keys():
if key in data.get_volume_bones():
is_fitted = True
util.object_select_set(arm, True)
util.set_active_object(context, arm)
bpy.ops.tamagoyaki.armature_deform_enable(set='VOL')
break
log.debug("| adjust shape keys done.")
util.set_disable_update_slider_selector(oselector)
util.set_active_object(context, tgt_armature)
util.object_select_set(tgt_armature, actsel)
if action:
tgt_armature.animation_data.action = action
log.info("update: Assigned Action %s to %s" % (action.name, tgt_armature.name) )
log.debug("| adjust support rig")
util.ensure_mode_is('EDIT')
shape.adjustSupportRig(context)
util.ensure_mode_is('OBJECT')
util.restore_object_select_states(context, selected_object_names)
util.ensure_mode_is('POSE')
bpy.ops.pose.select_all(action="SELECT")
tgt_armature.data.pose_position = 'POSE'
bpy.ops.pose.paste()
log.info("update: paste pose from pose buffer to %s" % (tgt_armature.name) )
if not active_is_arm:
util.set_active_object(context, active_obj)
util.object_select_set(active_obj, actsel)
util.ensure_mode_is(self.active_mode)
tgt_armature.ObjectProp.filter_deform_bones = src_armature.ObjectProp.filter_deform_bones
tgt_armature.ObjectProp.rig_display_type = src_armature.ObjectProp.rig_display_type
if shape_data:
shape.fromDictionary(tgt_armature, shape_data, update=True, init=True)
return tgt_armature
def convert_sl(self,
context,
tgt_rig_type,
jointType,
skeletonType,
active_obj,
src_armature,
inplace_transfer,
bone_repair,
mesh_repair,
transfer_joints=True,
shapefile=None
):
def get_child_set(context, arm, objects_to_rebind):
if objects_to_rebind:
child_set = objects_to_rebind
else:
child_set = util.get_animated_meshes(context, arm, with_tamagoyaki=False)
return child_set
log.warning("+========================================================================")
if inplace_transfer:
log.warning("| Start an %s Inplace Rig Migration of armature:'%s'" % (tgt_rig_type, src_armature.name))
else:
log.warning("| Start a %s Rig Transfer Copy from armature:'%s'" % (tgt_rig_type, src_armature.name))
log.warning("| joint type : %s" % (jointType) )
if self.must_rebind():
log.warning("| rebinding : %d rebind candidates" % len(self.sources))
log.warning("+========================================================================")
src_rig_type = self.srcRigType
active_is_arm = active_obj == src_armature
selected_object_names = util.get_selected_object_names(context)
updateRigProp = self #context.scene.UpdateRigProp
scene = context.scene
util.set_active_object(context, src_armature)
ob = util.get_active_object(context)
no_mesh = self.srcRigType == MANUELMAP
actsel = util.object_select_get(active_obj)
omode = util.ensure_mode_is("OBJECT", object=src_armature)
objects_to_rebind = []
if inplace_transfer:
if self.must_rebind():
objects_to_rebind = [o for o in src_armature.children]
log.warning("convert sl:| Unparent sources: %s" % [ob.name for ob in self.sources] )
util.unparent_selection(context, objects_to_rebind, type='CLEAR_KEEP_TRANSFORM')
if self.applyRotation:
util.set_active_object(context, src_armature)
old_state = util.select_hierarchy(src_armature) # All of them!
log.warning("convert sl:| Apply Visual transforms, Rotation and Scale to armature:%s" % src_armature.name)
bpy.ops.object.visual_transform_apply() #To get around some oddities with the apply tool (possible blender bug?)
bpy.ops.object.transform_apply(rotation=True, scale=True)
util.restore_hierarchy(context, old_state)
util.update_view_layer(context)
use_restpose = self.appearance_enabled
tgt_armature = create.createAvatar(context,
quads=True,
use_restpose=use_restpose,
rigType=tgt_rig_type,
jointType=jointType,
skeletonType=skeletonType,
no_mesh=no_mesh,
mesh_ids = context.scene.get('mesh_ids')
)
log.warning("convert sl:| created target armature '%s'" % tgt_armature.name)
util.set_active_object(context, tgt_armature)
if tgt_armature.ShapeDrivers.male_80 != self.use_male_shape:
propgroups.gender_update(tgt_armature, self.use_male_shape) # implicit call to refreshTamagoyakiShape
else:
shape.refreshTamagoyakiShape(context)
if shapefile:
shape.loadProps(context, tgt_armature, shapefile)
util.enforce_armature_update(context, tgt_armature)
self.targets.append(tgt_armature)
log.warning("convert sl:| Added new Armature name=[%s]" % tgt_armature.name)
if self.srcRigType == MANUELMAP:
util.ensure_mode_is('OBJECT')
for obj in src_armature.children:
util.set_active_object(context, obj)
splitcount = rig.tamagoyaki_split_manuel(context, obj, 100)
log.warning("Split Manuellab into %d children" % splitcount)
self.sources = [c for c in src_armature.children if c.type=='MESH']
else:
for arm in self.targets:
util.set_active_object(context, arm)
bpy.ops.tamagoyaki.store_bind_data()
log.warning("convert sl:| 1 self.targets[0] is '%s'" % self.targets[0].name)
self.copy_skeletons(context, src_armature, self.targets, src_rig_type, tgt_rig_type, skeletonType, transfer_joints, sync=True)
log.warning("convert sl:| 2 self.targets[0] is '%s'" % self.targets[0].name)
if active_is_arm:
active_obj = self.targets[0]
log.warning("convert sl:| changed active object to '%s'" % active_obj.name)
util.ensure_mode_is('OBJECT')
bpy.ops.object.select_all(action='DESELECT')
util.restore_object_select_states(context, selected_object_names)
is_fitted=False
oselector = util.set_disable_update_slider_selector(True)
for arm in self.targets:
util.set_active_object(context, arm)
if self.sl_bone_rolls:
rig.restore_source_bone_rolls(arm)
arm.RigProp.rig_use_bind_pose = util.use_sliders(context) and src_armature.RigProp.rig_use_bind_pose
child_set = get_child_set(context, arm, objects_to_rebind)
self.move_objects_to_target(context, child_set, arm, self.srcRigType) # convert SL
log.warning("convert sl:| Update slider settings for armature [%s]" % (arm.name) )
if self.appearance_enabled:
log.warning("convert sl:| Set Armature %s Slider Selector to 'SL'" % arm.name)
arm.ObjectProp.slider_selector = 'SL'
log.warning("convert sl:| Checking Custom meshes of armature [%s]" % arm.name)
for child in child_set:
log.warning("convert sl:| Checking Mesh %s" % child.name)
if self.appearance_enabled:
log.warning("convert sl:| Set child %s Slider Selector to 'SL'" % child.name)
util.set_active_object(context, child)
child.ObjectProp.slider_selector = 'SL'
if not is_fitted:
for key in child.vertex_groups.keys():
if key in data.get_volume_bones():
is_fitted = True
util.object_select_set(arm, True)
util.set_active_object(context, arm)
bpy.ops.tamagoyaki.armature_deform_enable(set='VOL')
break
if not inplace_transfer:
util.set_active_object(context, arm)
bpy.ops.tamagoyaki.apply_shape_sliders()
util.enforce_armature_update(context,arm)
bpy.ops.tamagoyaki.alter_to_restpose()
util.set_disable_update_slider_selector(oselector)
util.set_active_object(context, active_obj)
util.object_select_set(active_obj, actsel)
util.ensure_mode_is(self.active_mode)
if inplace_transfer:
log.warning("convert sl:| Finish %s Inplace Rig Migration with new rig '%s'" % (tgt_rig_type, active_obj.name))
else:
log.warning("convert sl:| Start a %s Rig Transfer Copy from '%s' to '%s'" % (tgt_rig_type, src_armature.name, active_obj.name))
return active_obj
def print_on_bone_tail_change(bone, msg=''):
if '_bt' in bone:
bt = Vector(bone['_bt'])
t = Vector(bone.tail)
has_changed = t != bt
else:
has_changed = True
if has_changed:
print("%s: %s head is now %s" % (msg, bone.name, bone.head) )
print("%s: %s tail is now %s" % (msg, bone.name, bone.tail) )
bone['_bt'] = bone.tail
def find_avatar_mesh_container(context, armobj, msg=""):
if armobj:
for c in armobj.children:
if c.type == 'EMPTY' and '_meshes' in c.name:
log.warning("%sFound mesh container %s" % (msg, c.name) )
return c
return None
def get_avatar_mesh_container(context, armobj, msg="", ct=None):
c = find_avatar_mesh_container(context, armobj, msg)
if c:
log.warning("%sReuse Target Mesh container %s for %s" % (msg, c.name, armobj.name) )
return c
if ct:
log.warning("%sReuse Source Mesh container %s for %s" % (msg, ct.name, armobj.name) )
c = ct
else:
log.warning("%sGenerate missing Mesh container for %s" % (msg, armobj.name) )
c = create.add_container(context, armobj, armobj.name + '_meshes')
return c
def import_collada(scene, filepath):
if not os.access(filepath, os.R_OK):
log.warning("Collada file not found or not readable:")
log.warning("filepath: %s" % filepath)
return None
before = [o for o in scene.objects]
bpy.ops.wm.collada_import(
filepath=filepath,
import_units=False,
fix_orientation=True,
find_chains=True,
auto_connect=False,
min_chain_length=0,
keep_bind_info=True)
after = [o for o in scene.objects]
importset = set(after) - set(before)
return importset
def import_blend(context, filepath):
scene = context.scene
def link_recursive(ob, indent = 0):
for ch in ob.children:
log.warning("%s%s" % (indent*" ", ch.name) )
util.link_object(context, ch)
link_recursive(ch, indent=indent+4)
if ob.type == 'EMPTY':
util.object_hide_set(ob, True)
before = [o for o in scene.objects]
with bpy.data.libraries.load(filepath) as (data_from, data_to):
data_to.objects = data_from.objects
for ob in data_to.objects:
if ob.type == 'ARMATURE':
log.warning("%s (linking armature)" % (ob.name) )
util.link_object(context, ob)
link_recursive(ob, 4)
for ob in data_to.objects:
if ob.users == 0:
log.warning("Remove orphan %s" % ob.name)
util.remove_object(context, ob)
after = [o for o in scene.objects]
importset = set(after) - set(before)
return importset
def import_rig_from_file(context, prop):
scene = context.scene
filepath = prop.devkit_filepath.replace("TAMAGOYAKI_ADDON_ROOT_FOLDER", TAMAGOYAKI_DIR)
shapefile = os.path.splitext(filepath)[0]+".shape"
if not os.path.exists(shapefile):
shapefile = ""
ext = os.path.splitext(filepath)[1]
log.warning("Import Devkit from file %s with extension %s" % (filepath, ext) )
if ext == '.dae':
importset = import_collada(scene, filepath)
elif ext == '.blend':
importset = import_blend(context, filepath)
else:
importset = None
if (not importset):
return None
arms = [ob for ob in importset if ob.type == 'ARMATURE']
if len(arms) != 1:
return None
armobj = arms[0]
util.set_active_object(context, armobj)
scale = prop.devkit_scale
if scale != 1.0:
armobj.scale = scale*armobj.scale
if prop.srcRigType == 'TAMAGOYAKI':
if armobj.RigProp.JointType == prop.JointType:
return armobj
create.smart_bone_connector(armobj, prop.srcRigType, prop.JointType)
prop.handleTargetMeshSelection = 'HIDE'
armobj.RigProp.JointType = prop.JointType
armobj.RigProp.rig_use_bind_pose = prop.devkit_use_bind_pose
use_male_shape = scene.UpdateRigProp.use_male_shape
use_male_skeleton = scene.UpdateRigProp.use_male_skeleton
scene.UpdateRigProp.use_male_shape=False
scene['mesh_ids'] = ['eyeBallLeftMesh', 'eyeBallRightMesh', 'headMesh'] if prop.devkit_use_sl_head else []
bpy.ops.tamagoyaki.copy_rig(
use_male_shape = False,
use_male_skeleton = use_male_skeleton,
JointType = prop.tgtJointType,
SkeletonType = armobj.RigProp.SkeletonType,
srcRigType = prop.srcRigType,
tgtRigType = prop.tgtRigType,
sl_bone_ends = prop.sl_bone_ends,
sl_bone_rolls = prop.sl_bone_rolls,
handleTargetMeshSelection = prop.handleTargetMeshSelection,
adjust_origin = 'ORIGIN_TO_ROOT',
apply_pose = False,
mesh_repair = prop.devkit_use_sl_head,
show_offsets = False,
bone_repair = False,
devkit_shapepath = shapefile
)
scene['mesh_ids'] = None
armobj = context.object
util.set_armature_layers(armobj, B_LEGACY_POSE_LAYERS)
showids = ['headMesh', 'eyeBallRightMesh', 'eyeBallLeftMesh']
mesh_objs = [m for m in util.getChildren(armobj, type='MESH')]
heads = [m for m in mesh_objs if m.get('mesh_id') in showids ]
custom_objs = [ob for ob in mesh_objs if ob not in heads]
rig.reset_cache(armobj, full=True)
shape.prepare_reference_meshes(custom_objs, armobj)
propgroups.update_sliders(context)
for head in heads:
util.object_hide_set(head, not prop.devkit_use_sl_head)
if use_male_shape:
propgroups.gender_update(armobj, True)
scene.UpdateRigProp.use_male_shape = True
if heads:
mesh.freeze_selection(context, targets=heads)
armobj.data.display_type ='STICK'
armobj.data.show_bone_custom_shapes=True
util.set_active_object(context, armobj)
util.set_select([armobj], reset=True)
preferences = util.getAddonPreferences()
mode = preferences.initial_rig_mode
util.set_mode(context, mode)
return armobj
class ImportColladaDevkit(bpy.types.Operator, ImportHelper):
bl_idname = "tamagoyaki.import_collada_devkit"
bl_label = "Import Collada Devkit"
bl_description = '''
'''
filename_ext = '.dae'
filter_glob : StringProperty(
default="*.dae",
options={'HIDDEN'},
)
devkit_type : StringProperty(name='devkit', default='')
def execute(self, context):
scene = context.scene
prop = scene.UpdateRigProp
armobj = import_rig_from_file(context, prop)
return {'FINISHED'} if armobj else {'CANCELLED'}
def armature_needs_update(armobj):
tamagoyaki_version, rig_version, rig_id, rig_type = util.get_version_info(armobj)
if not (rig_id or rig_version):
return True
repair = (tamagoyaki_version == rig_version or TAMAGOYAKI_RIG_ID == rig_id)
return not repair
class RigTransferConfigurator (util.ExpandablePannelSection):
bl_idname = "tamagoyaki.rig_transfer_configurator"
bl_label = "Rig Transfer"
class ButtonCopyTamagoyaki(bpy.types.Operator):
'''
Convert/Update/Cleanup Rigs
'''
bl_idname = "tamagoyaki.copy_rig"
bl_label = "Copy Rig"
bl_description = messages.tamagoyaki_copy_rig_bl_description