forked from Mwni/blender-animation-retargeting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalignment.py
175 lines (131 loc) · 4.16 KB
/
alignment.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
import bpy
from mathutils import Matrix
from .utilfuncs import *
def draw_panel(layout):
s = state()
n = s.get_alignments_count()
if not s.editing_alignment:
if n == 0:
row = layout.row()
row.label(text='No Rest Alignment', icon='INFO')
row.operator('retarget_alignment.edit', text='Set Up', icon='POSE_HLT')
else:
row = layout.row()
row.label(text=str(n) + ' Bones with Alignment', icon='POSE_HLT')
row.operator('retarget_alignment.edit', text='Edit', icon='TOOL_SETTINGS')
row.operator('retarget_alignment.reset', text='', icon='X')
else:
layout.label(text='Editing Rest Pose Alignment', icon='POSE_HLT')
layout.label(text='Align the rest pose with the source', icon='INFO')
row = layout.row()
row.operator('retarget_alignment.cancel', text='Cancel', icon='X')
row.operator('retarget_alignment.apply', text='Apply', icon='CHECKMARK')
def enter_offset():
s = state()
s.unleash()
s.target_pose_backup.clear()
for bone in s.target.pose.bones:
bp = s.target_pose_backup.add()
bp.bone = bone.name
bp.matrix = matrix4x4_to_data(bone.matrix_basis)
for bone in s.target.pose.bones:
for m in s.mappings:
if m.target == bone.name:
bone.matrix_basis = data_to_matrix4x4(m.offset)
def restore_poses():
s = state()
for bone in s.target.pose.bones:
for bp in s.target_pose_backup:
if bp.bone == bone.name:
bone.matrix_basis = data_to_matrix4x4(bp.matrix)
break
def store_matrices():
s = state()
for bone in s.target.pose.bones:
for m in s.mappings:
if m.target == bone.name:
m.rest = matrix4x4_to_data(bone.matrix)
m.offset = matrix4x4_to_data(bone.matrix_basis)
def update_rest():
enter_offset()
store_matrices()
restore_poses()
state().update_drivers()
def leave_edit():
if handle_edit_change in bpy.app.handlers.depsgraph_update_post:
bpy.app.handlers.depsgraph_update_post.remove(handle_edit_change)
s = state()
s.editing_alignment = False
s.get_source_armature().pose_position = 'POSE'
bpy.ops.object.mode_set(mode='OBJECT')
s.update_drivers()
def handle_edit_change(self, context):
if bpy.context.object.mode != 'POSE':
leave_edit()
class Panel(bpy.types.Panel):
bl_idname = 'RT_PT_Alignment'
bl_label = 'Alignment'
bl_parent_id = 'RT_PT_Main'
bl_category = 'Retarget'
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
def draw(self, context):
if uist().edit_alignment:
box = self.layout.box()
box.label(text='Align the target\'s (%s) rest pose with source.' % cfg().source.name, icon='INFO')
row = self.layout.row()
row.operator('retarget_alignment.cancel', text='Cancel')
row.operator('retarget_alignment.apply', text='Apply')
else:
n = cfg().get_alignments_count()
if n == 0:
self.layout.operator('retarget_alignment.edit', text='Set Up')
else:
row = self.layout.row()
row.label(text='%i bone(s) with offset' % n, icon='BONE_DATA')
row.operator('retarget_alignment.edit', text='Edit Alignment')
pass
class EditOperator(bpy.types.Operator):
bl_idname = 'retarget_alignment.edit'
bl_label = 'Change Alignment'
def execute(self, context):
s = state()
s.editing_alignment = True
s.get_source_armature().pose_position = 'REST'
bpy.ops.object.mode_set(mode='POSE')
enter_offset()
bpy.app.handlers.depsgraph_update_post.append(handle_edit_change)
return {'FINISHED'}
class ApplyOperator(bpy.types.Operator):
bl_idname = 'retarget_alignment.apply'
bl_label = 'Apply Alignment'
def execute(self, context):
store_matrices()
restore_poses()
leave_edit()
return {'FINISHED'}
class CancelOperator(bpy.types.Operator):
bl_idname = 'retarget_alignment.cancel'
bl_label = 'Cancel Alignment Change'
def execute(self, context):
restore_poses()
leave_edit()
return {'FINISHED'}
class ResetOperator(bpy.types.Operator):
bl_idname = 'retarget_alignment.reset'
bl_label = 'Reset Rest Pose Alignment'
bl_options = {'REGISTER', 'INTERNAL'}
def invoke(self, context, event):
return context.window_manager.invoke_confirm(self, event)
def execute(self, context):
s = state()
for mapping in s.mappings:
mapping.reset_offset()
s.update_drivers()
return {'FINISHED'}
classes = (
EditOperator,
ApplyOperator,
CancelOperator,
ResetOperator
)