forked from Mwni/blender-animation-retargeting
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathik.py
More file actions
131 lines (98 loc) · 3.62 KB
/
Copy pathik.py
File metadata and controls
131 lines (98 loc) · 3.62 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
import bpy
from mathutils import Matrix, Vector
from .utilfuncs import *
def clear():
s = state()
for limb in s.ik_limbs:
target_bone = s.get_pose_bone('target', limb.target_bone)
if target_bone:
for con in target_bone.constraints:
if con.name == 'Retarget IK':
target_bone.constraints.remove(con)
break
if limb.target_empty_child != None:
bpy.data.objects.remove(limb.target_empty_child, do_unlink=True)
limb.target_empty_child = None
if limb.target_empty != None:
bpy.data.objects.remove(limb.target_empty, do_unlink=True)
limb.target_empty = None
#if limb.pole_empty != None:
# bpy.data.objects.remove(limb.pole_empty, do_unlink=True)
# limb.pole_empty = None
if limb.control_cube != None:
limb.control_transform = matrix4x4_to_data(limb.control_cube.matrix_local)
bpy.data.objects.remove(limb.control_cube, do_unlink=True)
limb.control_cube = None
if limb.control_holder != None:
bpy.data.objects.remove(limb.control_holder, do_unlink=True)
limb.control_holder = None
def build():
s = state()
aux_collection = next((c for c in bpy.data.collections if c.name == 'Retarget Auxiliary'), None)
ctl_collection = next((c for c in bpy.data.collections if c.name == 'Retarget Control'), None)
if aux_collection == None:
aux_collection = bpy.data.collections.new('Retarget Auxiliary')
#aux_collection.hide_viewport = True
bpy.context.scene.collection.children.link(aux_collection)
if ctl_collection == None:
ctl_collection = bpy.data.collections.new('Retarget Control')
bpy.context.scene.collection.children.link(ctl_collection)
clear()
h = s.target.dimensions.z
for limb in s.ik_limbs:
if not limb.enabled:
continue
target_arma_bone, target_bone = s.get_pose_and_arma_bone('target', limb.target_bone)
mapping = s.get_mapping_for_target(limb.target_bone)
te = bpy.data.objects.new(limb.target_bone + '-target', None)
tec = bpy.data.objects.new(limb.target_bone + '-target-child', None)
te.empty_display_size = h * 0.1
te.empty_display_type = 'PLAIN_AXES'
tec.empty_display_size = 0
tec.empty_display_type = 'PLAIN_AXES'
aux_collection.objects.link(te)
aux_collection.objects.link(tec)
te.parent = s.target
tec.parent = te
head = Vector(target_arma_bone.head_local)
tail = Vector(target_arma_bone.tail_local)
offset = head - tail
tec.location.x = 0
tec.location.y = target_arma_bone.length
tec.location.z = 0
#pe = bpy.data.objects.new(limb.target_bone + '-pole', None)
#pe.empty_display_size = h * 0.1
#pe.empty_display_type = 'PLAIN_AXES'
#aux_collection.objects.link(pe)
#pe.parent = s.target
ch = bpy.data.objects.new(limb.target_bone + '-transform-holder', None)
ch.empty_display_size = 0
ctl_collection.objects.link(ch)
ch.parent = s.target
ch.matrix_local = loc_mat(target_arma_bone.matrix_local)
cc = bpy.data.objects.new(limb.target_bone + '-transform', None)
cc.empty_display_size = h * 0.1
cc.empty_display_type = 'CUBE'
ctl_collection.objects.link(cc)
cc.parent = ch
cc.matrix_local = data_to_matrix4x4(limb.control_transform)
#ce.location.x, ce.location.y, ce.location.z = target_arma_bone.matrix_local.translation
con = target_bone.constraints.new('IK')
con.name = 'Retarget IK'
con.target = tec
#con.pole_target = pe
con.use_rotation = True
tb = target_bone.parent
tbn = 0
while tb != None:
tbn += 1
tb.lock_ik_x, tb.lock_ik_y, tb.lock_ik_z = tb.lock_rotation
if tb.name == limb.origin_bone:
break
tb = tb.parent
con.chain_count = tbn + 1
limb.target_empty = te
limb.target_empty_child = tec
#limb.pole_empty = pe
limb.control_holder = ch
limb.control_cube = cc