-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathalignment.py
171 lines (123 loc) · 4.97 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
import bpy
from .drivers import clear_drivers, update_drivers
from .util import matrix_to_list, list_to_matrix
def draw_panel(ctx, layout):
n = ctx.get_bone_alignments_count()
if not ctx.ui_editing_alignment:
if n == 0 and not ctx.did_setup_empty_alignment:
row = layout.row()
row.label(text='No rest pose alignment', icon='INFO')
row.operator(AlignmentEditOperator.bl_idname, text='Set Up', icon='POSE_HLT')
else:
row = layout.row()
row.label(text='%i Bones with alignment' % n if n > 0 else 'Rest pose is alignment pose', icon='POSE_HLT')
row.operator(AlignmentEditOperator.bl_idname, text='Edit', icon='TOOL_SETTINGS')
row.operator(AlignmentResetOperator.bl_idname, text='', icon='X')
else:
layout.label(text='Editing Rest Pose Alignment', icon='POSE_HLT')
col = layout.column()
col.label(text='Align the target\'s pose with the source.', icon='INFO')
col.label(text=' The target should mimic the pose of the source as close as possible.')
row = layout.row()
row.operator(AlignmentCancelOperator.bl_idname, text='Cancel', icon='X')
row.operator(AlignmentApplyOperator.bl_idname, text='Apply', icon='CHECKMARK')
def enter_alignment_mode(ctx):
ctx.ui_editing_alignment = True
ctx.get_source_armature().pose_position = 'REST'
bpy.ops.object.mode_set(mode='POSE')
clear_drivers(ctx)
ctx.target_pose_backup.clear()
for bone in ctx.target.pose.bones:
bp = ctx.target_pose_backup.add()
bp.bone = bone.name
bp.matrix = matrix_to_list(bone.matrix_basis)
for bone in ctx.target.pose.bones:
for m in ctx.mappings:
if m.target == bone.name:
bone.matrix_basis = list_to_matrix(m.offset)
bpy.app.handlers.depsgraph_update_post.append(handle_edit_change)
def store_alignments(ctx):
for bone in ctx.target.pose.bones:
for m in ctx.mappings:
if m.target == bone.name:
m.rest = matrix_to_list(bone.matrix)
m.offset = matrix_to_list(bone.matrix_basis)
def leave_alignment_mode(ctx):
if handle_edit_change in bpy.app.handlers.depsgraph_update_post:
bpy.app.handlers.depsgraph_update_post.remove(handle_edit_change)
for bone in ctx.target.pose.bones:
for bp in ctx.target_pose_backup:
if bp.bone == bone.name:
bone.matrix_basis = list_to_matrix(bp.matrix)
break
ctx.ui_editing_alignment = False
ctx.get_source_armature().pose_position = 'POSE'
bpy.ops.object.mode_set(mode='OBJECT')
update_drivers(ctx)
def handle_edit_change(self, context):
if bpy.context.object.mode != 'POSE':
leave_alignment_mode(bpy.context.object.retargeting_context)
class AlignmentEditOperator(bpy.types.Operator):
bl_idname = 'alignment.edit'
bl_label = 'Change Alignment'
bl_description = 'Set the source to target armature rest pose alignment'
def execute(self, context):
enter_alignment_mode(context.object.retargeting_context)
return {'FINISHED'}
class AlignmentApplyOperator(bpy.types.Operator):
bl_idname = 'alignment.apply'
bl_label = 'Apply Alignment'
bl_description = 'Use the current pose as reference rest pose when retargeting'
def execute(self, context):
ctx = context.object.retargeting_context
store_alignments(ctx)
if ctx.get_bone_alignments_count() == 0 and not ctx.did_setup_empty_alignment:
context.window_manager.popup_menu(
title='Alignment pose is rest pose?',
icon='QUESTION',
draw_func=lambda self, _: (
self.layout.label(text='You have not adjusted the alignment pose.'),
self.layout.label(text='Does the current rest pose already fit the source?'),
self.layout.operator(AlignmentUseEmptyPoseOperator.bl_idname, text='Yes, use this pose', icon='CHECKMARK')
)
)
return {'CANCELLED'}
leave_alignment_mode(ctx)
return {'FINISHED'}
class AlignmentUseEmptyPoseOperator(bpy.types.Operator):
bl_idname = 'alignment.use_empty_pose'
bl_label = 'Use Empty Alignment Pose'
bl_description = 'Use the original rest pose as alignment pose'
def execute(self, context):
ctx = context.object.retargeting_context
ctx.did_setup_empty_alignment = True
leave_alignment_mode(ctx)
return {'FINISHED'}
class AlignmentCancelOperator(bpy.types.Operator):
bl_idname = 'alignment.cancel'
bl_label = 'Cancel Alignment Change'
bl_description = 'Discard the current pose and reset to pose before editing'
def execute(self, context):
leave_alignment_mode(context.object.retargeting_context)
return {'CANCELLED'}
class AlignmentResetOperator(bpy.types.Operator):
bl_idname = 'alignment.reset'
bl_label = 'Reset Rest Pose Alignment'
bl_description = 'Discard configured rest pose and start from scratch.'
bl_options = {'REGISTER', 'INTERNAL'}
def invoke(self, context, event):
return context.window_manager.invoke_confirm(self, event)
def execute(self, context):
ctx = context.object.retargeting_context
ctx.did_setup_empty_alignment = False
for mapping in ctx.mappings:
mapping.reset_offset()
update_drivers(ctx)
return {'FINISHED'}
classes = (
AlignmentEditOperator,
AlignmentApplyOperator,
AlignmentUseEmptyPoseOperator,
AlignmentCancelOperator,
AlignmentResetOperator
)