mirror of
https://github.com/blender/blender
synced 2026-11-22 03:45:13 +03:00
This adds code to convert animation from one rotation mode to another. There are multiple ways to do the conversion: * Using Ctrl+R in pose mode * In the 3D viewport, open the "Object" or "Pose" menu, under "Animation" there is a new entry "Convert Rotation Mode" * Using the python API `convert_rotation_mode()` either on bones or objects **Important Behavior** * The result of the operation is always a fully keyed rotation, even if previously only a single channel was keyed. This is the only way to ensure that the rotations line up. * Interpolation may not match. In fact it is expected not to. This is desired for animators for when they are running into gimbal lock and they just want to try if a different rotation mode works better. * In case interpolation *has to* match, there is an option to insert keys on every frame before the conversion. * key types are somewhat preserved. We assume that for any given frame, all rotation keys are the same type. * keys on all related actions are converted, that includes the NLA Even though the animation data conversion could be triggered automatically by changes to the rotation_mode RNA property, I chose not to do that for a reason. This may be set directly via python in an addon and we shouldn't include the costly (and maybe unexpected) side effect of converting animation. This adds a new operator `anim.rotation_mode_convert` instead of modifying the existing `pose.rotation_mode_set`. Since the operator now works with objects as well, it's probably better to move it out of the `pose` category. `pose.rotation_mode_set` and `pose.convert_rotation` from rigify are deprecated with a warning that pops up every time you execute them. **Implementation** The core of the changes are in `anim_convert.cc`. * `convert_to_rotation_mode` is a high level function that converts the rotation for a single transformable. * `build_rotation_fcurve_map` and `convert_rotation_keys` are two functions that work in concert. The first builds the data structure to quickly find FCurves by their RNA path and the second consumes that data to do the conversion. * `bake_rotation_fcurves` utility function to bake FCurves before converting their rotation. This uses the `AnimTransformable` class to make this functionality available to objects and bones at the same time. When dealing with multiple transformables, we would need to iterate the action for each to find the FCurves for it. To avoid that I added `ChannelbagFCurveMap` which is created by `build_rotation_fcurve_map`. With that we can easily iterate iterate the FCurves of each Channelbag sorted by RNA path. `KeyframeIterator` Is a new helper class to iterate all frames with keys of N FCurves. **Deprecates** the existing `POSE_OT_convert_rotation` operator and removes it from the pose menu. Resolves #149607 Resolves #159131 Design: #137281 UI improvements will be handled in a separate PR: !159981 Pull Request: https://projects.blender.org/blender/blender/pulls/154309
303 lines
9.7 KiB
Python
303 lines
9.7 KiB
Python
# SPDX-FileCopyrightText: 2017-2022 Blender Foundation
|
|
#
|
|
# SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
"""
|
|
Quaternion/Euler Rotation Mode Converter v0.1
|
|
|
|
This script/addon:
|
|
- Changes (pose) bone rotation mode
|
|
- Converts keyframes from one rotation mode to another
|
|
- Creates fcurves/keyframes in target rotation mode
|
|
- Deletes previous fcurves/keyframes.
|
|
- Converts multiple bones
|
|
- Converts multiple Actions
|
|
|
|
TO-DO:
|
|
- Properly support slotted Actions.
|
|
- To convert object's rotation mode (already done in Mutant Bob script,
|
|
but not done in this one.)
|
|
- To understand "EnumProperty" and write it well.
|
|
- Code clean
|
|
- ...
|
|
|
|
GitHub: https://github.com/MarioMey/rotation_mode_addon/
|
|
BlenderArtist thread: https://blenderartists.org/forum/showthread.php?388197-Quat-Euler-Rotation-Mode-Converter
|
|
|
|
Mutant Bob did the "hard code" of this script. Thanks him!
|
|
blender.stackexchange.com/questions/40711/how-to-convert-quaternions-keyframes-to-euler-ones-in-several-actions
|
|
|
|
|
|
"""
|
|
|
|
# bl_info = {
|
|
# "name": "Rotation Mode Converter",
|
|
# "author": "Mario Mey / Mutant Bob",
|
|
# "version": (0, 2),
|
|
# "blender": (2, 91, 0),
|
|
# 'location': 'Pose Mode -> Header -> Pose -> Convert Rotation Modes',
|
|
# "description": "Converts Animation between different rotation orders",
|
|
# "warning": "",
|
|
# "doc_url": "",
|
|
# "tracker_url": "https://github.com/MarioMey/rotation_mode_addon/",
|
|
# "category": "Animation",
|
|
# }
|
|
|
|
import bpy
|
|
from bpy.props import (
|
|
EnumProperty,
|
|
StringProperty,
|
|
)
|
|
from bpy.types import (
|
|
ActionChannelbag,
|
|
)
|
|
|
|
from bpy_extras import anim_utils
|
|
|
|
|
|
def add_keyframe_quat(
|
|
channelbag: ActionChannelbag,
|
|
quat: list[float],
|
|
frame: float,
|
|
bone_prefix: str,
|
|
group_name: str) -> None:
|
|
for i in range(len(quat)):
|
|
fc = channelbag.fcurves.ensure(bone_prefix + "rotation_quaternion", index=i, group_name=group_name)
|
|
pos = len(fc.keyframe_points)
|
|
fc.keyframe_points.add(1)
|
|
fc.keyframe_points[pos].co = [frame, quat[i]]
|
|
fc.update()
|
|
|
|
|
|
def add_keyframe_euler(
|
|
channelbag: ActionChannelbag,
|
|
euler: list[float],
|
|
frame: float,
|
|
bone_prefix: str,
|
|
group_name: str) -> None:
|
|
for i in range(len(euler)):
|
|
fc = channelbag.fcurves.ensure(bone_prefix + "rotation_euler", index=i, group_name=group_name)
|
|
pos = len(fc.keyframe_points)
|
|
fc.keyframe_points.add(1)
|
|
fc.keyframe_points[pos].co = [frame, euler[i]]
|
|
fc.update()
|
|
|
|
|
|
def frames_matching(channelbag, data_path):
|
|
frames = set()
|
|
for fc in channelbag.fcurves:
|
|
if fc.data_path == data_path:
|
|
fri = [kp.co[0] for kp in fc.keyframe_points]
|
|
frames.update(fri)
|
|
return frames
|
|
|
|
|
|
def group_qe(_obj, channelbag, bone, bone_prefix, order):
|
|
"""Converts only one group/bone in one channelbag - Quaternion to euler."""
|
|
# pose_bone = bone
|
|
data_path = bone_prefix + "rotation_quaternion"
|
|
frames = frames_matching(channelbag, data_path)
|
|
|
|
for fr in frames:
|
|
quat = bone.rotation_quaternion.copy()
|
|
for fc in channelbag.fcurves:
|
|
if fc.data_path == data_path:
|
|
quat[fc.array_index] = fc.evaluate(fr)
|
|
euler = quat.to_euler(order)
|
|
|
|
add_keyframe_euler(channelbag, euler, fr, bone_prefix, bone.name)
|
|
bone.rotation_mode = order
|
|
|
|
|
|
def group_eq(obj, channelbag, bone, bone_prefix, order):
|
|
"""Converts only one group/bone in one channelbag - Euler to Quaternion."""
|
|
# pose_bone = bone
|
|
data_path = bone_prefix + "rotation_euler"
|
|
frames = frames_matching(channelbag, data_path)
|
|
|
|
for fr in frames:
|
|
euler = bone.rotation_euler.copy()
|
|
for fc in channelbag.fcurves:
|
|
if fc.data_path == data_path:
|
|
euler[fc.array_index] = fc.evaluate(fr)
|
|
quat = euler.to_quaternion()
|
|
|
|
add_keyframe_quat(channelbag, quat, fr, bone_prefix, bone.name)
|
|
bone.rotation_mode = order
|
|
|
|
|
|
def convert_curves_of_bone(obj, channelbag, bone, order):
|
|
"""Convert given bone's curves in given channelbag to given rotation order."""
|
|
to_euler = False
|
|
bone_prefix = ''
|
|
|
|
for fcurve in channelbag.fcurves:
|
|
if fcurve.group and fcurve.group.name == bone.name:
|
|
|
|
# If To-Euler conversion
|
|
if order != 'QUATERNION':
|
|
if fcurve.data_path.endswith('rotation_quaternion'):
|
|
to_euler = True
|
|
bone_prefix = fcurve.data_path[:-len('rotation_quaternion')]
|
|
break
|
|
|
|
# If To-Quaternion conversion
|
|
else:
|
|
if fcurve.data_path.endswith('rotation_euler'):
|
|
to_euler = True
|
|
bone_prefix = fcurve.data_path[:-len('rotation_euler')]
|
|
break
|
|
|
|
fcurves_to_remove = []
|
|
|
|
# If To-Euler conversion
|
|
if to_euler and order != 'QUATERNION':
|
|
# Converts the group/bone from Quaternion to Euler
|
|
group_qe(obj, channelbag, bone, bone_prefix, order)
|
|
|
|
# Removes quaternion fcurves
|
|
for key in channelbag.fcurves:
|
|
if key.data_path == 'pose.bones["' + bone.name + '"].rotation_quaternion':
|
|
fcurves_to_remove.append(key)
|
|
|
|
# If To-Quaternion conversion
|
|
elif to_euler:
|
|
# Converts the group/bone from Euler to Quaternion
|
|
group_eq(obj, channelbag, bone, bone_prefix, order)
|
|
|
|
# Removes euler fcurves
|
|
for key in channelbag.fcurves:
|
|
if key.data_path == 'pose.bones["' + bone.name + '"].rotation_euler':
|
|
fcurves_to_remove.append(key)
|
|
|
|
for fcurve in fcurves_to_remove:
|
|
channelbag.fcurves.remove(fcurve)
|
|
|
|
# Changes rotation mode to new one
|
|
bone.rotation_mode = order
|
|
|
|
|
|
# noinspection PyPep8Naming
|
|
class POSE_OT_convert_rotation(bpy.types.Operator):
|
|
bl_label = 'Convert Rotation Modes'
|
|
bl_idname = 'pose.convert_rotation'
|
|
bl_description = 'Convert animation from any rotation mode to any other'
|
|
bl_options = {'REGISTER', 'UNDO'}
|
|
|
|
# Properties.
|
|
target_rotation_mode: EnumProperty(
|
|
items=[
|
|
('QUATERNION', 'Quaternion', 'Quaternion'),
|
|
('XYZ', 'XYZ', 'XYZ Euler'),
|
|
('XZY', 'XZY', 'XZY Euler'),
|
|
('YXZ', 'YXZ', 'YXZ Euler'),
|
|
('YZX', 'YZX', 'YZX Euler'),
|
|
('ZXY', 'ZXY', 'ZXY Euler'),
|
|
('ZYX', 'ZYX', 'ZYX Euler')
|
|
],
|
|
name='Convert To',
|
|
description="The target rotation mode",
|
|
default='QUATERNION',
|
|
)
|
|
affected_bones: EnumProperty(
|
|
name="Affected Bones",
|
|
items=[
|
|
('SELECT', 'Selected', 'Selected'),
|
|
('ALL', 'All', 'All'),
|
|
],
|
|
description="Which bones to affect",
|
|
default='SELECT',
|
|
)
|
|
affected_actions: EnumProperty(
|
|
name="Affected Actions",
|
|
items=[
|
|
('SINGLE', 'Single', 'Single'),
|
|
('ALL', 'All', 'All'),
|
|
],
|
|
description="Which Actions to affect",
|
|
default='SINGLE',
|
|
)
|
|
selected_action: StringProperty(name="Action")
|
|
|
|
def invoke(self, context, event):
|
|
ob = context.object
|
|
if ob and ob.type == 'ARMATURE' and ob.animation_data and ob.animation_data.action:
|
|
self.selected_action = context.object.animation_data.action.name
|
|
else:
|
|
self.affected_actions = 'ALL'
|
|
|
|
wm = context.window_manager
|
|
return wm.invoke_props_dialog(self)
|
|
|
|
def draw(self, context):
|
|
layout = self.layout
|
|
layout.use_property_split = True
|
|
layout.use_property_decorate = False
|
|
|
|
layout.row().prop(self, 'affected_bones', expand=True)
|
|
layout.row().prop(self, 'affected_actions', expand=True)
|
|
if self.affected_actions == 'SINGLE':
|
|
layout.prop_search(self, 'selected_action', bpy.data, 'actions')
|
|
layout.prop(self, 'target_rotation_mode')
|
|
|
|
def execute(self, context):
|
|
self.report({'WARNING'}, "This operator is deprecated. Use anim.rotation_mode_convert instead")
|
|
obj = context.active_object
|
|
|
|
assigned_action = obj.animation_data and obj.animation_data.action
|
|
assigned_slot = obj.animation_data and obj.animation_data.action_slot
|
|
|
|
if self.affected_bones == 'ALL':
|
|
pose_bones = obj.pose.bones
|
|
else:
|
|
pose_bones = context.selected_pose_bones
|
|
|
|
if self.affected_actions == 'ALL':
|
|
actions = bpy.data.actions
|
|
else:
|
|
actions = [bpy.data.actions.get(self.selected_action)]
|
|
|
|
for action in actions:
|
|
if action == assigned_action:
|
|
# On the assigned Action, use the assigned slot.
|
|
action_slot = assigned_slot
|
|
else:
|
|
# Otherwise find a suitable slot to process.
|
|
#
|
|
# NOTE: this may not pick the right slot if they are not
|
|
# consistently named. Also, if there are multiple suitable
|
|
# slots, only the first one is converted.
|
|
if assigned_slot.identifier in action.slots:
|
|
action_slot = action.slots[assigned_slot.identifier]
|
|
else:
|
|
action_slot = anim_utils.action_get_first_suitable_slot(action, 'OBJECT')
|
|
|
|
channelbag = anim_utils.action_get_channelbag_for_slot(action, action_slot)
|
|
if not channelbag:
|
|
continue
|
|
|
|
for pb in pose_bones:
|
|
convert_curves_of_bone(obj, channelbag, pb, self.target_rotation_mode)
|
|
|
|
return {'FINISHED'}
|
|
|
|
|
|
classes = [
|
|
POSE_OT_convert_rotation
|
|
]
|
|
|
|
|
|
def register():
|
|
from bpy.utils import register_class
|
|
|
|
# Classes.
|
|
for cls in classes:
|
|
register_class(cls)
|
|
|
|
|
|
def unregister():
|
|
from bpy.utils import unregister_class
|
|
|
|
# Classes.
|
|
for cls in classes:
|
|
unregister_class(cls)
|