-
Notifications
You must be signed in to change notification settings - Fork 40
/
copy_visual_transform.py
230 lines (180 loc) · 7.2 KB
/
copy_visual_transform.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
#====================== 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 ========================
"""
Disable constraints without moving.
Really simple add-on for disabling constraints without moving the constrained object.
"""
bl_info = {
'name': 'Copy Visual Transform',
'author': 'Sybren A. Stüvel',
'version': (1, 1),
'blender': (2, 81, 0),
'location': 'N-panel in the 3D Viewport',
'category': 'Animation',
'support': 'COMMUNITY',
}
from typing import Iterable, Optional, Set, Union
import bpy
class AutoKeying:
"""Auto-keying support.
Based on Rigify code by Alexander Gavrilov.
"""
@classmethod
def keying_options(cls, context) -> Set[str]:
"""Retrieve the general keyframing options from user preferences."""
prefs = context.preferences
ts = context.scene.tool_settings
options = set()
if prefs.edit.use_visual_keying:
options.add('INSERTKEY_VISUAL')
if prefs.edit.use_keyframe_insert_needed:
options.add('INSERTKEY_NEEDED')
if prefs.edit.use_insertkey_xyz_to_rgb:
options.add('INSERTKEY_XYZ_TO_RGB')
if ts.use_keyframe_cycle_aware:
options.add('INSERTKEY_CYCLE_AWARE')
return options
@classmethod
def autokeying_options(cls, context) -> Optional[Set[str]]:
"""Retrieve the Auto Keyframe options, or None if disabled."""
ts = context.scene.tool_settings
if not ts.use_keyframe_insert_auto:
return None
if ts.use_keyframe_insert_keyingset:
# No support for keying sets (yet).
return None
prefs = context.preferences
options = cls.keying_options(context)
if prefs.edit.use_keyframe_insert_available:
options.add('INSERTKEY_AVAILABLE')
if ts.auto_keying_mode == 'REPLACE_KEYS':
options.add('INSERTKEY_REPLACE')
return options
@staticmethod
def get_4d_rotlock(bone: bpy.types.PoseBone) -> Iterable[bool]:
"Retrieve the lock status for 4D rotation."
if bone.lock_rotations_4d:
return [bone.lock_rotation_w, *bone.lock_rotation]
else:
return [all(bone.lock_rotation)] * 4
@staticmethod
def keyframe_channels(
target: Union[bpy.types.Object, bpy.types.PoseBone],
options: Set[str],
data_path: str,
group: str,
locks: Iterable[bool],
) -> None:
if all(locks):
return
if not any(locks):
target.keyframe_insert(data_path, group=group, options=options)
return
for index, lock in enumerate(locks):
if lock:
continue
target.keyframe_insert(data_path, index=index, group=group, options=options)
@classmethod
def key_transformation(
cls,
target: Union[bpy.types.Object, bpy.types.PoseBone],
options: Set[str],
) -> None:
"""Keyframe transformation properties, avoiding keying locked channels."""
is_bone = isinstance(target, bpy.types.PoseBone)
if is_bone:
group = target.name
else:
group = 'Object Transforms'
def keyframe(data_path, locks):
cls.keyframe_channels(target, options, data_path, group, locks)
if not (is_bone and target.bone.use_connect):
keyframe('location', target.lock_location)
if target.rotation_mode == 'QUATERNION':
keyframe('rotation_quaternion', cls.get_4d_rotlock(target))
elif target.rotation_mode == 'AXIS_ANGLE':
keyframe('rotation_axis_angle', cls.get_4d_rotlock(target))
else:
keyframe('rotation_euler', target.lock_rotation)
keyframe('scale', target.lock_scale)
@classmethod
def autokey_transformation(cls, context, target: Union[bpy.types.Object, bpy.types.PoseBone]) -> None:
"""Auto-key transformation properties."""
options = cls.autokeying_options(context)
if options is None:
return
cls.key_transformation(target, options)
def get_matrix(context):
bone = context.active_pose_bone
if bone:
# Convert matrix to world space
arm = context.active_object
mat = arm.matrix_world @ bone.matrix
else:
mat = context.active_object.matrix_world
return mat
def set_matrix(context, mat):
bone = context.active_pose_bone
if bone:
# Convert matrix to local space
arm = context.active_object
bone.matrix = arm.matrix_world.inverted() @ mat
AutoKeying.autokey_transformation(context, bone)
else:
context.active_object.matrix_world = mat
AutoKeying.autokey_transformation(context, context.active_object)
class OBJECT_OT_copy_matrix(bpy.types.Operator):
bl_idname = 'object.copy_matrix'
bl_label = 'Copy matrix'
bl_description = 'Copies the matrix of the currently active object or pose bone ' \
'to the clipboard. Uses world-space matrices'
@classmethod
def poll(cls, context) -> bool:
return bool(context.active_pose_bone) or bool(context.active_object)
def execute(self, context) -> Set[str]:
context.window_manager.clipboard = repr(get_matrix(context))
return {'FINISHED'}
class OBJECT_OT_paste_matrix(bpy.types.Operator):
bl_idname = 'object.paste_matrix'
bl_label = 'Paste matrix'
bl_description = 'Pastes the matrix of the clipboard to the currently active pose bone ' \
'or object. Uses world-space matrices'
@classmethod
def poll(cls, context) -> bool:
return bool(context.active_pose_bone) or bool(context.active_object)
def execute(self, context) -> Set[str]:
from mathutils import Matrix
mat = eval(context.window_manager.clipboard, {}, {'Matrix': Matrix})
set_matrix(context, mat)
return {'FINISHED'}
class VIEW3D_PT_copy_matrix(bpy.types.Panel):
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = "View"
bl_label = "Copy Matrix"
def draw(self, context) -> None:
layout = self.layout
col = layout.column(align=True)
col.operator('object.copy_matrix', text="Copy Transform")
col.operator('object.paste_matrix', text="Paste Transform")
classes = (
OBJECT_OT_copy_matrix,
OBJECT_OT_paste_matrix,
VIEW3D_PT_copy_matrix,
)
register, unregister = bpy.utils.register_classes_factory(classes)