Anim: Move "Copy Global Transform" extension to internal scripts
Move the Copy Global Transform core add-on into Blender's code.
- The entire extension was one Python file. This PR basically splits
it into two, one for operators (in `bl_operators`) and the other for
UI panels. Those panels are registered in the 3D viewport's sidebar,
which were registered in `space_view3d`, but I made the decision
here to create a new file `space_view3d_sidebar`, because the main
file is getting too large and difficult to navigate. This PR puts
the global transform panel in this file. After this is merged, I
will do refactors to move the rest of the sidebar panels here as
well.
- `AutoKeying` class was moved into `bpy_extras/anim_utils.py` so that
it's reusable and also accessible from API, since it's generally
very useful. There were discussions about putting this somewhere,
but for now, I chose against it because creating a new file would
also mean PR would have to affect documentation generation, and
would complicate things. If we want to, we can probably create a new
module in the future.
- Little tweaks to labels and descriptions. Now that they exist
outside of the add-on context, and exist without the user explicitly
enabling them, they need to be more descriptive and tell users what
they actually do. They also need to conform to Blender's GUI
guidelines. Also tried organizing files a little by grouping
objects.
- Add-on properties (which included word `addon` in the name) have
been registered in C++, on `scene.tool_settings` with `anim_`
prefix.
Pull Request: https://projects.blender.org/blender/blender/pulls/145414
2025-10-03 17:42:04 +02:00
|
|
|
# SPDX-FileCopyrightText: 2021-2025 Blender Authors
|
2024-05-15 23:39:31 +10:00
|
|
|
#
|
|
|
|
|
# SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
Copy Global Transform
|
|
|
|
|
|
Anim: Move "Copy Global Transform" extension to internal scripts
Move the Copy Global Transform core add-on into Blender's code.
- The entire extension was one Python file. This PR basically splits
it into two, one for operators (in `bl_operators`) and the other for
UI panels. Those panels are registered in the 3D viewport's sidebar,
which were registered in `space_view3d`, but I made the decision
here to create a new file `space_view3d_sidebar`, because the main
file is getting too large and difficult to navigate. This PR puts
the global transform panel in this file. After this is merged, I
will do refactors to move the rest of the sidebar panels here as
well.
- `AutoKeying` class was moved into `bpy_extras/anim_utils.py` so that
it's reusable and also accessible from API, since it's generally
very useful. There were discussions about putting this somewhere,
but for now, I chose against it because creating a new file would
also mean PR would have to affect documentation generation, and
would complicate things. If we want to, we can probably create a new
module in the future.
- Little tweaks to labels and descriptions. Now that they exist
outside of the add-on context, and exist without the user explicitly
enabling them, they need to be more descriptive and tell users what
they actually do. They also need to conform to Blender's GUI
guidelines. Also tried organizing files a little by grouping
objects.
- Add-on properties (which included word `addon` in the name) have
been registered in C++, on `scene.tool_settings` with `anim_`
prefix.
Pull Request: https://projects.blender.org/blender/blender/pulls/145414
2025-10-03 17:42:04 +02:00
|
|
|
Simple operators for copying world-space transforms.
|
2024-05-15 23:39:31 +10:00
|
|
|
|
|
|
|
|
It's called "global" to avoid confusion with the Blender World data-block.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import abc
|
Anim: Move "Copy Global Transform" extension to internal scripts
Move the Copy Global Transform core add-on into Blender's code.
- The entire extension was one Python file. This PR basically splits
it into two, one for operators (in `bl_operators`) and the other for
UI panels. Those panels are registered in the 3D viewport's sidebar,
which were registered in `space_view3d`, but I made the decision
here to create a new file `space_view3d_sidebar`, because the main
file is getting too large and difficult to navigate. This PR puts
the global transform panel in this file. After this is merged, I
will do refactors to move the rest of the sidebar panels here as
well.
- `AutoKeying` class was moved into `bpy_extras/anim_utils.py` so that
it's reusable and also accessible from API, since it's generally
very useful. There were discussions about putting this somewhere,
but for now, I chose against it because creating a new file would
also mean PR would have to affect documentation generation, and
would complicate things. If we want to, we can probably create a new
module in the future.
- Little tweaks to labels and descriptions. Now that they exist
outside of the add-on context, and exist without the user explicitly
enabling them, they need to be more descriptive and tell users what
they actually do. They also need to conform to Blender's GUI
guidelines. Also tried organizing files a little by grouping
objects.
- Add-on properties (which included word `addon` in the name) have
been registered in C++, on `scene.tool_settings` with `anim_`
prefix.
Pull Request: https://projects.blender.org/blender/blender/pulls/145414
2025-10-03 17:42:04 +02:00
|
|
|
from typing import Iterable, Optional, Any, TypeAlias
|
2024-05-15 23:39:31 +10:00
|
|
|
|
|
|
|
|
import bpy
|
2025-03-20 16:08:10 +01:00
|
|
|
from bpy.types import (
|
Anim: Move "Copy Global Transform" extension to internal scripts
Move the Copy Global Transform core add-on into Blender's code.
- The entire extension was one Python file. This PR basically splits
it into two, one for operators (in `bl_operators`) and the other for
UI panels. Those panels are registered in the 3D viewport's sidebar,
which were registered in `space_view3d`, but I made the decision
here to create a new file `space_view3d_sidebar`, because the main
file is getting too large and difficult to navigate. This PR puts
the global transform panel in this file. After this is merged, I
will do refactors to move the rest of the sidebar panels here as
well.
- `AutoKeying` class was moved into `bpy_extras/anim_utils.py` so that
it's reusable and also accessible from API, since it's generally
very useful. There were discussions about putting this somewhere,
but for now, I chose against it because creating a new file would
also mean PR would have to affect documentation generation, and
would complicate things. If we want to, we can probably create a new
module in the future.
- Little tweaks to labels and descriptions. Now that they exist
outside of the add-on context, and exist without the user explicitly
enabling them, they need to be more descriptive and tell users what
they actually do. They also need to conform to Blender's GUI
guidelines. Also tried organizing files a little by grouping
objects.
- Add-on properties (which included word `addon` in the name) have
been registered in C++, on `scene.tool_settings` with `anim_`
prefix.
Pull Request: https://projects.blender.org/blender/blender/pulls/145414
2025-10-03 17:42:04 +02:00
|
|
|
Context, Object, Operator, PoseBone,
|
2025-10-07 09:15:20 +11:00
|
|
|
Camera, ID, ActionChannelbag,
|
2025-03-20 16:08:10 +01:00
|
|
|
)
|
2024-05-15 23:39:31 +10:00
|
|
|
from mathutils import Matrix
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_axis_enum_items = [
|
|
|
|
|
("x", "X", "", 1),
|
|
|
|
|
("y", "Y", "", 2),
|
|
|
|
|
("z", "Z", "", 3),
|
|
|
|
|
]
|
|
|
|
|
|
Anim: Move "Copy Global Transform" extension to internal scripts
Move the Copy Global Transform core add-on into Blender's code.
- The entire extension was one Python file. This PR basically splits
it into two, one for operators (in `bl_operators`) and the other for
UI panels. Those panels are registered in the 3D viewport's sidebar,
which were registered in `space_view3d`, but I made the decision
here to create a new file `space_view3d_sidebar`, because the main
file is getting too large and difficult to navigate. This PR puts
the global transform panel in this file. After this is merged, I
will do refactors to move the rest of the sidebar panels here as
well.
- `AutoKeying` class was moved into `bpy_extras/anim_utils.py` so that
it's reusable and also accessible from API, since it's generally
very useful. There were discussions about putting this somewhere,
but for now, I chose against it because creating a new file would
also mean PR would have to affect documentation generation, and
would complicate things. If we want to, we can probably create a new
module in the future.
- Little tweaks to labels and descriptions. Now that they exist
outside of the add-on context, and exist without the user explicitly
enabling them, they need to be more descriptive and tell users what
they actually do. They also need to conform to Blender's GUI
guidelines. Also tried organizing files a little by grouping
objects.
- Add-on properties (which included word `addon` in the name) have
been registered in C++, on `scene.tool_settings` with `anim_`
prefix.
Pull Request: https://projects.blender.org/blender/blender/pulls/145414
2025-10-03 17:42:04 +02:00
|
|
|
# Mapping from frame number to the dominant (in terms of genetics) key type.
|
|
|
|
|
# GENERATED is the only recessive key type, others are dominant.
|
|
|
|
|
KeyInfo: TypeAlias = dict[float, str]
|
2024-05-15 23:39:31 +10:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_matrix(context: Context) -> Matrix:
|
|
|
|
|
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: Context, mat: Matrix) -> None:
|
2025-10-07 09:01:19 +11:00
|
|
|
from bpy_extras.anim_utils import AutoKeying
|
2024-05-15 23:39:31 +10:00
|
|
|
bone = context.active_pose_bone
|
|
|
|
|
if bone:
|
|
|
|
|
# Convert matrix to local space
|
|
|
|
|
arm_eval = context.active_object.evaluated_get(context.view_layer.depsgraph)
|
|
|
|
|
bone.matrix = arm_eval.matrix_world.inverted() @ mat
|
|
|
|
|
AutoKeying.autokey_transformation(context, bone)
|
|
|
|
|
else:
|
|
|
|
|
context.active_object.matrix_world = mat
|
|
|
|
|
AutoKeying.autokey_transformation(context, context.active_object)
|
|
|
|
|
|
|
|
|
|
|
2024-12-09 12:31:26 +01:00
|
|
|
def _channelbag_for_id(animated_id: ID) -> ActionChannelbag | None:
|
|
|
|
|
# This is on purpose limited to the first layer and strip. To support more
|
|
|
|
|
# than 1 layer, a rewrite of the caller is needed.
|
|
|
|
|
|
|
|
|
|
adt = animated_id.animation_data
|
|
|
|
|
action = adt and adt.action
|
|
|
|
|
if action is None:
|
|
|
|
|
return None
|
|
|
|
|
|
2025-02-03 20:19:00 +01:00
|
|
|
slot = adt.action_slot
|
2024-12-09 12:31:26 +01:00
|
|
|
|
|
|
|
|
for layer in action.layers:
|
|
|
|
|
for strip in layer.strips:
|
|
|
|
|
assert strip.type == 'KEYFRAME'
|
2025-02-03 20:19:00 +01:00
|
|
|
channelbag = strip.channelbag(slot)
|
2024-12-09 12:31:26 +01:00
|
|
|
return channelbag
|
|
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
2024-05-15 23:39:31 +10:00
|
|
|
def _selected_keyframes(context: Context) -> list[float]:
|
|
|
|
|
"""Return the list of frame numbers that have a selected key.
|
|
|
|
|
|
|
|
|
|
Only keys on the active bone/object are considered.
|
|
|
|
|
"""
|
Anim: Move "Copy Global Transform" extension to internal scripts
Move the Copy Global Transform core add-on into Blender's code.
- The entire extension was one Python file. This PR basically splits
it into two, one for operators (in `bl_operators`) and the other for
UI panels. Those panels are registered in the 3D viewport's sidebar,
which were registered in `space_view3d`, but I made the decision
here to create a new file `space_view3d_sidebar`, because the main
file is getting too large and difficult to navigate. This PR puts
the global transform panel in this file. After this is merged, I
will do refactors to move the rest of the sidebar panels here as
well.
- `AutoKeying` class was moved into `bpy_extras/anim_utils.py` so that
it's reusable and also accessible from API, since it's generally
very useful. There were discussions about putting this somewhere,
but for now, I chose against it because creating a new file would
also mean PR would have to affect documentation generation, and
would complicate things. If we want to, we can probably create a new
module in the future.
- Little tweaks to labels and descriptions. Now that they exist
outside of the add-on context, and exist without the user explicitly
enabling them, they need to be more descriptive and tell users what
they actually do. They also need to conform to Blender's GUI
guidelines. Also tried organizing files a little by grouping
objects.
- Add-on properties (which included word `addon` in the name) have
been registered in C++, on `scene.tool_settings` with `anim_`
prefix.
Pull Request: https://projects.blender.org/blender/blender/pulls/145414
2025-10-03 17:42:04 +02:00
|
|
|
|
2024-05-15 23:39:31 +10:00
|
|
|
bone = context.active_pose_bone
|
|
|
|
|
if bone:
|
|
|
|
|
return _selected_keyframes_for_bone(context.active_object, bone)
|
|
|
|
|
return _selected_keyframes_for_object(context.active_object)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _selected_keyframes_for_bone(object: Object, bone: PoseBone) -> list[float]:
|
|
|
|
|
"""Return the list of frame numbers that have a selected key.
|
|
|
|
|
|
|
|
|
|
Only keys on the given pose bone are considered.
|
|
|
|
|
"""
|
|
|
|
|
name = bpy.utils.escape_identifier(bone.name)
|
2024-12-09 12:31:26 +01:00
|
|
|
return _selected_keyframes_for_action_slot(object, "pose.bones[\"{:s}\"].".format(name))
|
2024-05-15 23:39:31 +10:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def _selected_keyframes_for_object(object: Object) -> list[float]:
|
|
|
|
|
"""Return the list of frame numbers that have a selected key.
|
|
|
|
|
|
|
|
|
|
Only keys on the given object are considered.
|
|
|
|
|
"""
|
2024-12-09 12:31:26 +01:00
|
|
|
return _selected_keyframes_for_action_slot(object, "")
|
2024-05-15 23:39:31 +10:00
|
|
|
|
|
|
|
|
|
2024-12-09 12:31:26 +01:00
|
|
|
def _selected_keyframes_for_action_slot(object: Object, rna_path_prefix: str) -> list[float]:
|
2024-05-15 23:39:31 +10:00
|
|
|
"""Return the list of frame numbers that have a selected key.
|
|
|
|
|
|
2024-12-09 12:31:26 +01:00
|
|
|
Only keys on the given object's Action Slot on FCurves starting with rna_path_prefix are considered.
|
2024-05-15 23:39:31 +10:00
|
|
|
"""
|
|
|
|
|
|
2024-12-09 12:31:26 +01:00
|
|
|
cbag = _channelbag_for_id(object)
|
|
|
|
|
if not cbag:
|
2024-05-15 23:39:31 +10:00
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
keyframes = set()
|
2024-12-09 12:31:26 +01:00
|
|
|
for fcurve in cbag.fcurves:
|
2024-05-15 23:39:31 +10:00
|
|
|
if not fcurve.data_path.startswith(rna_path_prefix):
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
for kp in fcurve.keyframe_points:
|
|
|
|
|
if not kp.select_control_point:
|
|
|
|
|
continue
|
|
|
|
|
keyframes.add(kp.co.x)
|
|
|
|
|
return sorted(keyframes)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _copy_matrix_to_clipboard(window_manager: bpy.types.WindowManager, matrix: Matrix) -> None:
|
2025-10-07 09:54:38 +11:00
|
|
|
rows = [" {!r},".format(tuple(row)) for row in matrix]
|
2024-05-15 23:39:31 +10:00
|
|
|
as_string = "\n".join(rows)
|
2025-10-07 09:54:38 +11:00
|
|
|
window_manager.clipboard = "Matrix((\n{:s}\n))".format(as_string)
|
2024-05-15 23:39:31 +10:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class OBJECT_OT_copy_global_transform(Operator):
|
|
|
|
|
bl_idname = "object.copy_global_transform"
|
|
|
|
|
bl_label = "Copy Global Transform"
|
|
|
|
|
bl_description = (
|
|
|
|
|
"Copies the matrix of the currently active object or pose bone to the clipboard. Uses world-space matrices"
|
|
|
|
|
)
|
|
|
|
|
# This operator cannot be un-done because it manipulates data outside Blender.
|
|
|
|
|
bl_options = {'REGISTER'}
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def poll(cls, context: Context) -> bool:
|
|
|
|
|
return bool(context.active_pose_bone) or bool(context.active_object)
|
|
|
|
|
|
|
|
|
|
def execute(self, context: Context) -> set[str]:
|
|
|
|
|
mat = get_matrix(context)
|
|
|
|
|
_copy_matrix_to_clipboard(context.window_manager, mat)
|
|
|
|
|
return {'FINISHED'}
|
|
|
|
|
|
|
|
|
|
|
Anim: Move "Copy Global Transform" extension to internal scripts
Move the Copy Global Transform core add-on into Blender's code.
- The entire extension was one Python file. This PR basically splits
it into two, one for operators (in `bl_operators`) and the other for
UI panels. Those panels are registered in the 3D viewport's sidebar,
which were registered in `space_view3d`, but I made the decision
here to create a new file `space_view3d_sidebar`, because the main
file is getting too large and difficult to navigate. This PR puts
the global transform panel in this file. After this is merged, I
will do refactors to move the rest of the sidebar panels here as
well.
- `AutoKeying` class was moved into `bpy_extras/anim_utils.py` so that
it's reusable and also accessible from API, since it's generally
very useful. There were discussions about putting this somewhere,
but for now, I chose against it because creating a new file would
also mean PR would have to affect documentation generation, and
would complicate things. If we want to, we can probably create a new
module in the future.
- Little tweaks to labels and descriptions. Now that they exist
outside of the add-on context, and exist without the user explicitly
enabling them, they need to be more descriptive and tell users what
they actually do. They also need to conform to Blender's GUI
guidelines. Also tried organizing files a little by grouping
objects.
- Add-on properties (which included word `addon` in the name) have
been registered in C++, on `scene.tool_settings` with `anim_`
prefix.
Pull Request: https://projects.blender.org/blender/blender/pulls/145414
2025-10-03 17:42:04 +02:00
|
|
|
def get_relative_ob(context: Context) -> Optional[Object]:
|
2024-05-15 23:39:31 +10:00
|
|
|
"""Get the 'relative' object.
|
|
|
|
|
|
|
|
|
|
This is the object that's configured, or if that's empty, the active scene camera.
|
|
|
|
|
"""
|
Anim: Move "Copy Global Transform" extension to internal scripts
Move the Copy Global Transform core add-on into Blender's code.
- The entire extension was one Python file. This PR basically splits
it into two, one for operators (in `bl_operators`) and the other for
UI panels. Those panels are registered in the 3D viewport's sidebar,
which were registered in `space_view3d`, but I made the decision
here to create a new file `space_view3d_sidebar`, because the main
file is getting too large and difficult to navigate. This PR puts
the global transform panel in this file. After this is merged, I
will do refactors to move the rest of the sidebar panels here as
well.
- `AutoKeying` class was moved into `bpy_extras/anim_utils.py` so that
it's reusable and also accessible from API, since it's generally
very useful. There were discussions about putting this somewhere,
but for now, I chose against it because creating a new file would
also mean PR would have to affect documentation generation, and
would complicate things. If we want to, we can probably create a new
module in the future.
- Little tweaks to labels and descriptions. Now that they exist
outside of the add-on context, and exist without the user explicitly
enabling them, they need to be more descriptive and tell users what
they actually do. They also need to conform to Blender's GUI
guidelines. Also tried organizing files a little by grouping
objects.
- Add-on properties (which included word `addon` in the name) have
been registered in C++, on `scene.tool_settings` with `anim_`
prefix.
Pull Request: https://projects.blender.org/blender/blender/pulls/145414
2025-10-03 17:42:04 +02:00
|
|
|
rel_ob = context.scene.tool_settings.anim_relative_object
|
2024-05-15 23:39:31 +10:00
|
|
|
return rel_ob or context.scene.camera
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class OBJECT_OT_copy_relative_transform(Operator):
|
|
|
|
|
bl_idname = "object.copy_relative_transform"
|
|
|
|
|
bl_label = "Copy Relative Transform"
|
|
|
|
|
bl_description = "Copies the matrix of the currently active object or pose bone to the clipboard. " \
|
|
|
|
|
"Uses matrices relative to a specific object or the active scene camera"
|
|
|
|
|
# This operator cannot be un-done because it manipulates data outside Blender.
|
|
|
|
|
bl_options = {'REGISTER'}
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def poll(cls, context: Context) -> bool:
|
Anim: Move "Copy Global Transform" extension to internal scripts
Move the Copy Global Transform core add-on into Blender's code.
- The entire extension was one Python file. This PR basically splits
it into two, one for operators (in `bl_operators`) and the other for
UI panels. Those panels are registered in the 3D viewport's sidebar,
which were registered in `space_view3d`, but I made the decision
here to create a new file `space_view3d_sidebar`, because the main
file is getting too large and difficult to navigate. This PR puts
the global transform panel in this file. After this is merged, I
will do refactors to move the rest of the sidebar panels here as
well.
- `AutoKeying` class was moved into `bpy_extras/anim_utils.py` so that
it's reusable and also accessible from API, since it's generally
very useful. There were discussions about putting this somewhere,
but for now, I chose against it because creating a new file would
also mean PR would have to affect documentation generation, and
would complicate things. If we want to, we can probably create a new
module in the future.
- Little tweaks to labels and descriptions. Now that they exist
outside of the add-on context, and exist without the user explicitly
enabling them, they need to be more descriptive and tell users what
they actually do. They also need to conform to Blender's GUI
guidelines. Also tried organizing files a little by grouping
objects.
- Add-on properties (which included word `addon` in the name) have
been registered in C++, on `scene.tool_settings` with `anim_`
prefix.
Pull Request: https://projects.blender.org/blender/blender/pulls/145414
2025-10-03 17:42:04 +02:00
|
|
|
rel_ob = get_relative_ob(context)
|
2024-05-15 23:39:31 +10:00
|
|
|
if not rel_ob:
|
|
|
|
|
return False
|
|
|
|
|
return bool(context.active_pose_bone) or bool(context.active_object)
|
|
|
|
|
|
|
|
|
|
def execute(self, context: Context) -> set[str]:
|
Anim: Move "Copy Global Transform" extension to internal scripts
Move the Copy Global Transform core add-on into Blender's code.
- The entire extension was one Python file. This PR basically splits
it into two, one for operators (in `bl_operators`) and the other for
UI panels. Those panels are registered in the 3D viewport's sidebar,
which were registered in `space_view3d`, but I made the decision
here to create a new file `space_view3d_sidebar`, because the main
file is getting too large and difficult to navigate. This PR puts
the global transform panel in this file. After this is merged, I
will do refactors to move the rest of the sidebar panels here as
well.
- `AutoKeying` class was moved into `bpy_extras/anim_utils.py` so that
it's reusable and also accessible from API, since it's generally
very useful. There were discussions about putting this somewhere,
but for now, I chose against it because creating a new file would
also mean PR would have to affect documentation generation, and
would complicate things. If we want to, we can probably create a new
module in the future.
- Little tweaks to labels and descriptions. Now that they exist
outside of the add-on context, and exist without the user explicitly
enabling them, they need to be more descriptive and tell users what
they actually do. They also need to conform to Blender's GUI
guidelines. Also tried organizing files a little by grouping
objects.
- Add-on properties (which included word `addon` in the name) have
been registered in C++, on `scene.tool_settings` with `anim_`
prefix.
Pull Request: https://projects.blender.org/blender/blender/pulls/145414
2025-10-03 17:42:04 +02:00
|
|
|
rel_ob = get_relative_ob(context)
|
2025-01-20 10:44:38 +01:00
|
|
|
if not rel_ob:
|
|
|
|
|
self.report(
|
|
|
|
|
{'ERROR'},
|
|
|
|
|
"No 'Relative To' object found, set one explicitly or make sure there is an active object")
|
|
|
|
|
return {'CANCELLED'}
|
2024-05-15 23:39:31 +10:00
|
|
|
mat = rel_ob.matrix_world.inverted() @ get_matrix(context)
|
|
|
|
|
_copy_matrix_to_clipboard(context.window_manager, mat)
|
|
|
|
|
return {'FINISHED'}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class UnableToMirrorError(Exception):
|
|
|
|
|
"""Raised when mirroring is enabled but no mirror object/bone is set."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class OBJECT_OT_paste_transform(Operator):
|
|
|
|
|
bl_idname = "object.paste_transform"
|
|
|
|
|
bl_label = "Paste Global Transform"
|
|
|
|
|
bl_description = (
|
|
|
|
|
"Pastes the matrix from the clipboard to the currently active pose bone or object. Uses world-space matrices"
|
|
|
|
|
)
|
|
|
|
|
bl_options = {'REGISTER', 'UNDO'}
|
|
|
|
|
|
|
|
|
|
_method_items = [
|
|
|
|
|
(
|
|
|
|
|
'CURRENT',
|
|
|
|
|
"Current Transform",
|
|
|
|
|
"Paste onto the current values only, only manipulating the animation data if auto-keying is enabled",
|
|
|
|
|
),
|
|
|
|
|
(
|
|
|
|
|
'EXISTING_KEYS',
|
|
|
|
|
"Selected Keys",
|
|
|
|
|
"Paste onto frames that have a selected key, potentially creating new keys on those frames",
|
|
|
|
|
),
|
|
|
|
|
(
|
|
|
|
|
'BAKE',
|
|
|
|
|
"Bake on Key Range",
|
|
|
|
|
"Paste onto all frames between the first and last selected key, creating new keyframes if necessary",
|
|
|
|
|
),
|
|
|
|
|
]
|
|
|
|
|
method: bpy.props.EnumProperty( # type: ignore
|
|
|
|
|
items=_method_items,
|
|
|
|
|
name="Paste Method",
|
|
|
|
|
description="Update the current transform, selected keyframes, or even create new keys",
|
2025-09-01 15:21:09 +02:00
|
|
|
options={'SKIP_SAVE'},
|
2024-05-15 23:39:31 +10:00
|
|
|
)
|
|
|
|
|
bake_step: bpy.props.IntProperty( # type: ignore
|
|
|
|
|
name="Frame Step",
|
|
|
|
|
description="Only used for baking. Step=1 creates a key on every frame, step=2 bakes on 2s, etc",
|
|
|
|
|
min=1,
|
|
|
|
|
soft_min=1,
|
|
|
|
|
soft_max=5,
|
2025-09-01 15:21:09 +02:00
|
|
|
options={'SKIP_SAVE'},
|
2024-05-15 23:39:31 +10:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
use_mirror: bpy.props.BoolProperty( # type: ignore
|
|
|
|
|
name="Mirror Transform",
|
|
|
|
|
description="When pasting, mirror the transform relative to a specific object or bone",
|
|
|
|
|
default=False,
|
2025-09-01 15:21:09 +02:00
|
|
|
options={'SKIP_SAVE'},
|
2024-05-15 23:39:31 +10:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
mirror_axis_loc: bpy.props.EnumProperty( # type: ignore
|
|
|
|
|
items=_axis_enum_items,
|
|
|
|
|
name="Location Axis",
|
|
|
|
|
description="Coordinate axis used to mirror the location part of the transform",
|
|
|
|
|
default='x',
|
2025-09-01 15:21:09 +02:00
|
|
|
options={'SKIP_SAVE'},
|
2024-05-15 23:39:31 +10:00
|
|
|
)
|
|
|
|
|
mirror_axis_rot: bpy.props.EnumProperty( # type: ignore
|
|
|
|
|
items=_axis_enum_items,
|
|
|
|
|
name="Rotation Axis",
|
|
|
|
|
description="Coordinate axis used to mirror the rotation part of the transform",
|
|
|
|
|
default='z',
|
2025-09-01 15:21:09 +02:00
|
|
|
options={'SKIP_SAVE'},
|
2024-05-15 23:39:31 +10:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
use_relative: bpy.props.BoolProperty( # type: ignore
|
|
|
|
|
name="Use Relative Paste",
|
|
|
|
|
description="When pasting, assume the pasted matrix is relative to another object (set in the user interface)",
|
|
|
|
|
default=False,
|
2025-09-01 15:21:09 +02:00
|
|
|
options={'SKIP_SAVE'},
|
2024-05-15 23:39:31 +10:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def poll(cls, context: Context) -> bool:
|
|
|
|
|
if not context.active_pose_bone and not context.active_object:
|
|
|
|
|
cls.poll_message_set("Select an object or pose bone")
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
clipboard = context.window_manager.clipboard.strip()
|
|
|
|
|
if not (clipboard.startswith("Matrix(") or clipboard.startswith("<Matrix 4x4")):
|
|
|
|
|
cls.poll_message_set("Clipboard does not contain a valid matrix")
|
|
|
|
|
return False
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def parse_print_m4(value: str) -> Optional[Matrix]:
|
|
|
|
|
"""Parse output from Blender's print_m4() function.
|
|
|
|
|
|
|
|
|
|
Expects four lines of space-separated floats.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
lines = value.strip().splitlines()
|
|
|
|
|
if len(lines) != 4:
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
floats = tuple(tuple(float(item) for item in line.split()) for line in lines)
|
|
|
|
|
return Matrix(floats)
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def parse_repr_m4(value: str) -> Optional[Matrix]:
|
|
|
|
|
"""Four lines of (a, b, c, d) floats."""
|
|
|
|
|
|
|
|
|
|
lines = value.strip().splitlines()
|
|
|
|
|
if len(lines) != 4:
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
floats = tuple(tuple(float(item.strip()) for item in line.strip()[1:-1].split(',')) for line in lines)
|
|
|
|
|
return Matrix(floats)
|
|
|
|
|
|
|
|
|
|
def execute(self, context: Context) -> set[str]:
|
2025-10-07 09:01:19 +11:00
|
|
|
import ast
|
|
|
|
|
|
2024-05-15 23:39:31 +10:00
|
|
|
clipboard = context.window_manager.clipboard.strip()
|
|
|
|
|
if clipboard.startswith("Matrix"):
|
|
|
|
|
mat = Matrix(ast.literal_eval(clipboard[6:]))
|
|
|
|
|
elif clipboard.startswith("<Matrix 4x4"):
|
|
|
|
|
mat = self.parse_repr_m4(clipboard[12:-1])
|
|
|
|
|
else:
|
|
|
|
|
mat = self.parse_print_m4(clipboard)
|
|
|
|
|
|
|
|
|
|
if mat is None:
|
|
|
|
|
self.report({'ERROR'}, "Clipboard does not contain a valid matrix")
|
|
|
|
|
return {'CANCELLED'}
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
mat = self._preprocess_matrix(context, mat)
|
|
|
|
|
except UnableToMirrorError:
|
|
|
|
|
self.report({'ERROR'}, "Unable to mirror, no mirror object/bone configured")
|
|
|
|
|
return {'CANCELLED'}
|
|
|
|
|
|
|
|
|
|
applicator = {
|
|
|
|
|
'CURRENT': self._paste_current,
|
|
|
|
|
'EXISTING_KEYS': self._paste_existing_keys,
|
|
|
|
|
'BAKE': self._paste_bake,
|
|
|
|
|
}[self.method]
|
|
|
|
|
return applicator(context, mat)
|
|
|
|
|
|
|
|
|
|
def _preprocess_matrix(self, context: Context, matrix: Matrix) -> Matrix:
|
2024-12-09 12:31:26 +01:00
|
|
|
if self.use_relative:
|
|
|
|
|
matrix = self._relative_to_world(context, matrix)
|
2024-05-15 23:39:31 +10:00
|
|
|
|
|
|
|
|
if self.use_mirror:
|
|
|
|
|
matrix = self._mirror_matrix(context, matrix)
|
|
|
|
|
return matrix
|
|
|
|
|
|
|
|
|
|
def _relative_to_world(self, context: Context, matrix: Matrix) -> Matrix:
|
Anim: Move "Copy Global Transform" extension to internal scripts
Move the Copy Global Transform core add-on into Blender's code.
- The entire extension was one Python file. This PR basically splits
it into two, one for operators (in `bl_operators`) and the other for
UI panels. Those panels are registered in the 3D viewport's sidebar,
which were registered in `space_view3d`, but I made the decision
here to create a new file `space_view3d_sidebar`, because the main
file is getting too large and difficult to navigate. This PR puts
the global transform panel in this file. After this is merged, I
will do refactors to move the rest of the sidebar panels here as
well.
- `AutoKeying` class was moved into `bpy_extras/anim_utils.py` so that
it's reusable and also accessible from API, since it's generally
very useful. There were discussions about putting this somewhere,
but for now, I chose against it because creating a new file would
also mean PR would have to affect documentation generation, and
would complicate things. If we want to, we can probably create a new
module in the future.
- Little tweaks to labels and descriptions. Now that they exist
outside of the add-on context, and exist without the user explicitly
enabling them, they need to be more descriptive and tell users what
they actually do. They also need to conform to Blender's GUI
guidelines. Also tried organizing files a little by grouping
objects.
- Add-on properties (which included word `addon` in the name) have
been registered in C++, on `scene.tool_settings` with `anim_`
prefix.
Pull Request: https://projects.blender.org/blender/blender/pulls/145414
2025-10-03 17:42:04 +02:00
|
|
|
rel_ob = get_relative_ob(context)
|
2024-05-15 23:39:31 +10:00
|
|
|
if not rel_ob:
|
|
|
|
|
return matrix
|
|
|
|
|
|
|
|
|
|
rel_ob_eval = rel_ob.evaluated_get(context.view_layer.depsgraph)
|
|
|
|
|
return rel_ob_eval.matrix_world @ matrix
|
|
|
|
|
|
|
|
|
|
def _mirror_matrix(self, context: Context, matrix: Matrix) -> Matrix:
|
Anim: Move "Copy Global Transform" extension to internal scripts
Move the Copy Global Transform core add-on into Blender's code.
- The entire extension was one Python file. This PR basically splits
it into two, one for operators (in `bl_operators`) and the other for
UI panels. Those panels are registered in the 3D viewport's sidebar,
which were registered in `space_view3d`, but I made the decision
here to create a new file `space_view3d_sidebar`, because the main
file is getting too large and difficult to navigate. This PR puts
the global transform panel in this file. After this is merged, I
will do refactors to move the rest of the sidebar panels here as
well.
- `AutoKeying` class was moved into `bpy_extras/anim_utils.py` so that
it's reusable and also accessible from API, since it's generally
very useful. There were discussions about putting this somewhere,
but for now, I chose against it because creating a new file would
also mean PR would have to affect documentation generation, and
would complicate things. If we want to, we can probably create a new
module in the future.
- Little tweaks to labels and descriptions. Now that they exist
outside of the add-on context, and exist without the user explicitly
enabling them, they need to be more descriptive and tell users what
they actually do. They also need to conform to Blender's GUI
guidelines. Also tried organizing files a little by grouping
objects.
- Add-on properties (which included word `addon` in the name) have
been registered in C++, on `scene.tool_settings` with `anim_`
prefix.
Pull Request: https://projects.blender.org/blender/blender/pulls/145414
2025-10-03 17:42:04 +02:00
|
|
|
mirror_ob = context.scene.tool_settings.anim_mirror_object
|
|
|
|
|
mirror_bone = context.scene.tool_settings.anim_mirror_bone
|
2024-05-15 23:39:31 +10:00
|
|
|
|
|
|
|
|
# No mirror object means "current armature object".
|
|
|
|
|
ctx_ob = context.object
|
|
|
|
|
if not mirror_ob and mirror_bone and ctx_ob and ctx_ob.type == 'ARMATURE':
|
|
|
|
|
mirror_ob = ctx_ob
|
|
|
|
|
|
|
|
|
|
if not mirror_ob:
|
|
|
|
|
raise UnableToMirrorError()
|
|
|
|
|
|
|
|
|
|
if mirror_ob.type == 'ARMATURE' and mirror_bone:
|
|
|
|
|
return self._mirror_over_bone(matrix, mirror_ob, mirror_bone)
|
|
|
|
|
return self._mirror_over_ob(matrix, mirror_ob)
|
|
|
|
|
|
|
|
|
|
def _mirror_over_ob(self, matrix: Matrix, mirror_ob: bpy.types.Object) -> Matrix:
|
|
|
|
|
mirror_matrix = mirror_ob.matrix_world
|
|
|
|
|
return self._mirror_over_matrix(matrix, mirror_matrix)
|
|
|
|
|
|
|
|
|
|
def _mirror_over_bone(self, matrix: Matrix, mirror_ob: bpy.types.Object, mirror_bone_name: str) -> Matrix:
|
|
|
|
|
bone = mirror_ob.pose.bones[mirror_bone_name]
|
|
|
|
|
mirror_matrix = mirror_ob.matrix_world @ bone.matrix
|
|
|
|
|
return self._mirror_over_matrix(matrix, mirror_matrix)
|
|
|
|
|
|
|
|
|
|
def _mirror_over_matrix(self, matrix: Matrix, mirror_matrix: Matrix) -> Matrix:
|
|
|
|
|
# Compute the matrix in the space of the mirror matrix:
|
|
|
|
|
mat_local = mirror_matrix.inverted() @ matrix
|
|
|
|
|
|
|
|
|
|
# Decompose the matrix, as we don't want to touch the scale. This
|
|
|
|
|
# operator should only mirror the translation and rotation components.
|
|
|
|
|
trans, rot_q, scale = mat_local.decompose()
|
|
|
|
|
|
|
|
|
|
# Mirror the translation component:
|
|
|
|
|
axis_index = ord(self.mirror_axis_loc) - ord('x')
|
|
|
|
|
trans[axis_index] *= -1
|
|
|
|
|
|
|
|
|
|
# Flip the rotation, and use a rotation order that applies the to-be-flipped axes first.
|
|
|
|
|
match self.mirror_axis_rot:
|
|
|
|
|
case 'x':
|
|
|
|
|
rot_e = rot_q.to_euler('XYZ')
|
|
|
|
|
rot_e.x *= -1 # Flip the requested rotation axis.
|
|
|
|
|
rot_e.y *= -1 # Also flip the bone roll.
|
|
|
|
|
case 'y':
|
|
|
|
|
rot_e = rot_q.to_euler('YZX')
|
|
|
|
|
rot_e.y *= -1 # Flip the requested rotation axis.
|
|
|
|
|
rot_e.z *= -1 # Also flip another axis? Not sure how to handle this one.
|
|
|
|
|
case 'z':
|
|
|
|
|
rot_e = rot_q.to_euler('ZYX')
|
|
|
|
|
rot_e.z *= -1 # Flip the requested rotation axis.
|
|
|
|
|
rot_e.y *= -1 # Also flip the bone roll.
|
|
|
|
|
|
|
|
|
|
# Recompose the local matrix:
|
|
|
|
|
mat_local = Matrix.LocRotScale(trans, rot_e, scale)
|
|
|
|
|
|
|
|
|
|
# Go back to world space:
|
|
|
|
|
mirrored_world = mirror_matrix @ mat_local
|
|
|
|
|
return mirrored_world
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def _paste_current(context: Context, matrix: Matrix) -> set[str]:
|
|
|
|
|
set_matrix(context, matrix)
|
|
|
|
|
return {'FINISHED'}
|
|
|
|
|
|
|
|
|
|
def _paste_existing_keys(self, context: Context, matrix: Matrix) -> set[str]:
|
|
|
|
|
if not context.scene.tool_settings.use_keyframe_insert_auto:
|
|
|
|
|
self.report({'ERROR'}, "This mode requires auto-keying to work properly")
|
|
|
|
|
return {'CANCELLED'}
|
|
|
|
|
|
|
|
|
|
frame_numbers = _selected_keyframes(context)
|
|
|
|
|
if not frame_numbers:
|
|
|
|
|
self.report({'WARNING'}, "No selected frames found")
|
|
|
|
|
return {'CANCELLED'}
|
|
|
|
|
|
|
|
|
|
self._paste_on_frames(context, frame_numbers, matrix)
|
|
|
|
|
return {'FINISHED'}
|
|
|
|
|
|
|
|
|
|
def _paste_bake(self, context: Context, matrix: Matrix) -> set[str]:
|
|
|
|
|
if not context.scene.tool_settings.use_keyframe_insert_auto:
|
|
|
|
|
self.report({'ERROR'}, "This mode requires auto-keying to work properly")
|
|
|
|
|
return {'CANCELLED'}
|
|
|
|
|
|
|
|
|
|
bake_step = max(1, self.bake_step)
|
|
|
|
|
# Put the clamped bake step back into RNA for the redo panel.
|
|
|
|
|
self.bake_step = bake_step
|
|
|
|
|
|
|
|
|
|
frame_start, frame_end = self._determine_bake_range(context)
|
|
|
|
|
frame_range = range(round(frame_start), round(frame_end) + bake_step, bake_step)
|
|
|
|
|
self._paste_on_frames(context, frame_range, matrix)
|
|
|
|
|
return {'FINISHED'}
|
|
|
|
|
|
|
|
|
|
def _determine_bake_range(self, context: Context) -> tuple[float, float]:
|
|
|
|
|
frame_numbers = _selected_keyframes(context)
|
|
|
|
|
if frame_numbers:
|
|
|
|
|
# Note that these could be the same frame, if len(frame_numbers) == 1:
|
|
|
|
|
return frame_numbers[0], frame_numbers[-1]
|
|
|
|
|
|
|
|
|
|
if context.scene.use_preview_range:
|
|
|
|
|
self.report({'INFO'}, "No selected keys, pasting over preview range")
|
|
|
|
|
return context.scene.frame_preview_start, context.scene.frame_preview_end
|
|
|
|
|
|
|
|
|
|
self.report({'INFO'}, "No selected keys, pasting over scene range")
|
|
|
|
|
return context.scene.frame_start, context.scene.frame_end
|
|
|
|
|
|
|
|
|
|
def _paste_on_frames(self, context: Context, frame_numbers: Iterable[float], matrix: Matrix) -> None:
|
|
|
|
|
current_frame = context.scene.frame_current_final
|
|
|
|
|
try:
|
|
|
|
|
for frame in frame_numbers:
|
|
|
|
|
context.scene.frame_set(int(frame), subframe=frame % 1.0)
|
|
|
|
|
set_matrix(context, matrix)
|
|
|
|
|
finally:
|
|
|
|
|
context.scene.frame_set(int(current_frame), subframe=current_frame % 1.0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Transformable(metaclass=abc.ABCMeta):
|
|
|
|
|
"""Interface for a bone or an object."""
|
|
|
|
|
|
|
|
|
|
def __init__(self) -> None:
|
|
|
|
|
self._key_info_cache: Optional[KeyInfo] = None
|
|
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
|
def matrix_world(self) -> Matrix:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def set_matrix_world(self, context: Context, matrix: Matrix) -> None:
|
2025-01-20 12:08:12 +01:00
|
|
|
"""Set the world matrix, without autokeying."""
|
|
|
|
|
self._set_matrix_world(context, matrix)
|
|
|
|
|
|
|
|
|
|
def set_matrix_world_autokey(self, context: Context, matrix: Matrix) -> None:
|
|
|
|
|
"""Set the world matrix, and autokey the resulting transform."""
|
|
|
|
|
self._set_matrix_world(context, matrix)
|
|
|
|
|
self._autokey_matrix_world(context)
|
|
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
|
def _set_matrix_world(self, context: Context, matrix: Matrix) -> None:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
|
def _autokey_matrix_world(self, context: Context) -> None:
|
2024-05-15 23:39:31 +10:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
|
def _my_fcurves(self) -> Iterable[bpy.types.FCurve]:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def key_info(self) -> KeyInfo:
|
|
|
|
|
if self._key_info_cache is not None:
|
|
|
|
|
return self._key_info_cache
|
|
|
|
|
|
|
|
|
|
keyinfo: KeyInfo = {}
|
|
|
|
|
for fcurve in self._my_fcurves():
|
|
|
|
|
for kp in fcurve.keyframe_points:
|
|
|
|
|
frame = kp.co.x
|
|
|
|
|
if kp.type == 'GENERATED' and frame in keyinfo:
|
|
|
|
|
# Don't bother overwriting other key types.
|
|
|
|
|
continue
|
|
|
|
|
keyinfo[frame] = kp.type
|
|
|
|
|
|
|
|
|
|
self._key_info_cache = keyinfo
|
|
|
|
|
return keyinfo
|
|
|
|
|
|
2025-01-20 10:44:38 +01:00
|
|
|
def remove_keys_of_type(
|
|
|
|
|
self,
|
|
|
|
|
key_type: str,
|
|
|
|
|
*,
|
|
|
|
|
frame_start: float | int = float("-inf"),
|
|
|
|
|
frame_end: float | int = float("inf")) -> None:
|
2024-05-15 23:39:31 +10:00
|
|
|
self._key_info_cache = None
|
|
|
|
|
|
|
|
|
|
for fcurve in self._my_fcurves():
|
|
|
|
|
to_remove = [
|
|
|
|
|
kp for kp in fcurve.keyframe_points if kp.type == key_type and (frame_start <= kp.co.x <= frame_end)
|
|
|
|
|
]
|
|
|
|
|
for kp in reversed(to_remove):
|
|
|
|
|
fcurve.keyframe_points.remove(kp, fast=True)
|
|
|
|
|
fcurve.keyframe_points.handles_recalc()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TransformableObject(Transformable):
|
|
|
|
|
object: Object
|
|
|
|
|
|
|
|
|
|
def __init__(self, object: Object) -> None:
|
|
|
|
|
super().__init__()
|
|
|
|
|
self.object = object
|
|
|
|
|
|
2025-01-20 12:08:12 +01:00
|
|
|
def __str__(self) -> str:
|
2025-10-07 09:54:38 +11:00
|
|
|
return "TransformableObject({:s})".format(self.object.name)
|
2025-01-20 12:08:12 +01:00
|
|
|
|
2024-05-15 23:39:31 +10:00
|
|
|
def matrix_world(self) -> Matrix:
|
|
|
|
|
return self.object.matrix_world
|
|
|
|
|
|
2025-01-20 12:08:12 +01:00
|
|
|
def _set_matrix_world(self, _context: Context, matrix: Matrix) -> None:
|
2024-05-15 23:39:31 +10:00
|
|
|
self.object.matrix_world = matrix
|
2025-01-20 12:08:12 +01:00
|
|
|
|
|
|
|
|
def _autokey_matrix_world(self, context: Context) -> None:
|
2025-10-07 09:01:19 +11:00
|
|
|
from bpy_extras.anim_utils import AutoKeying
|
2024-05-15 23:39:31 +10:00
|
|
|
AutoKeying.autokey_transformation(context, self.object)
|
|
|
|
|
|
|
|
|
|
def __hash__(self) -> int:
|
|
|
|
|
return hash(self.object.as_pointer())
|
|
|
|
|
|
|
|
|
|
def _my_fcurves(self) -> Iterable[bpy.types.FCurve]:
|
2024-12-09 12:31:26 +01:00
|
|
|
cbag = _channelbag_for_id(self.object)
|
|
|
|
|
if not cbag:
|
2024-05-15 23:39:31 +10:00
|
|
|
return
|
2024-12-09 12:31:26 +01:00
|
|
|
yield from cbag.fcurves
|
2024-05-15 23:39:31 +10:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class TransformableBone(Transformable):
|
|
|
|
|
arm_object: Object
|
|
|
|
|
pose_bone: PoseBone
|
|
|
|
|
|
|
|
|
|
def __init__(self, pose_bone: PoseBone) -> None:
|
|
|
|
|
super().__init__()
|
|
|
|
|
self.arm_object = pose_bone.id_data
|
|
|
|
|
self.pose_bone = pose_bone
|
|
|
|
|
|
2025-01-20 12:08:12 +01:00
|
|
|
def __str__(self) -> str:
|
2025-10-07 09:54:38 +11:00
|
|
|
return "TransformableBone({:s}, bone={:s})".format(self.arm_object.name, self.pose_bone.name)
|
2025-01-20 12:08:12 +01:00
|
|
|
|
2024-05-15 23:39:31 +10:00
|
|
|
def matrix_world(self) -> Matrix:
|
|
|
|
|
mat = self.arm_object.matrix_world @ self.pose_bone.matrix
|
|
|
|
|
return mat
|
|
|
|
|
|
2025-01-20 12:08:12 +01:00
|
|
|
def _set_matrix_world(self, context: Context, matrix: Matrix) -> None:
|
2024-05-15 23:39:31 +10:00
|
|
|
# Convert matrix to armature-local space
|
|
|
|
|
arm_eval = self.arm_object.evaluated_get(context.view_layer.depsgraph)
|
|
|
|
|
self.pose_bone.matrix = arm_eval.matrix_world.inverted() @ matrix
|
2025-01-20 12:08:12 +01:00
|
|
|
|
|
|
|
|
def _autokey_matrix_world(self, context: Context) -> None:
|
2025-10-07 09:01:19 +11:00
|
|
|
from bpy_extras.anim_utils import AutoKeying
|
2024-05-15 23:39:31 +10:00
|
|
|
AutoKeying.autokey_transformation(context, self.pose_bone)
|
|
|
|
|
|
|
|
|
|
def __hash__(self) -> int:
|
|
|
|
|
return hash(self.pose_bone.as_pointer())
|
|
|
|
|
|
|
|
|
|
def _my_fcurves(self) -> Iterable[bpy.types.FCurve]:
|
2024-12-09 12:31:26 +01:00
|
|
|
cbag = _channelbag_for_id(self.arm_object)
|
|
|
|
|
if not cbag:
|
2024-05-15 23:39:31 +10:00
|
|
|
return
|
|
|
|
|
|
2024-12-09 12:31:26 +01:00
|
|
|
rna_prefix = self.pose_bone.path_from_id() + "."
|
|
|
|
|
for fcurve in cbag.fcurves:
|
2024-05-15 23:39:31 +10:00
|
|
|
if fcurve.data_path.startswith(rna_prefix):
|
|
|
|
|
yield fcurve
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class FixToCameraCommon:
|
|
|
|
|
"""Common functionality for the Fix To Scene Camera operator + its 'delete' button."""
|
|
|
|
|
|
|
|
|
|
keytype = 'GENERATED'
|
|
|
|
|
|
|
|
|
|
# Operator method stubs to avoid PyLance/MyPy errors:
|
|
|
|
|
@classmethod
|
|
|
|
|
def poll_message_set(cls, message: str) -> None:
|
2025-01-28 14:13:53 +01:00
|
|
|
super().poll_message_set(message)
|
2024-05-15 23:39:31 +10:00
|
|
|
|
|
|
|
|
def report(self, level: set[str], message: str) -> None:
|
2025-01-28 14:13:53 +01:00
|
|
|
super().report(level, message)
|
2024-05-15 23:39:31 +10:00
|
|
|
|
|
|
|
|
# Implement in subclass:
|
|
|
|
|
def _execute(self, context: Context, transformables: list[Transformable]) -> None:
|
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def poll(cls, context: Context) -> bool:
|
|
|
|
|
if not context.active_pose_bone and not context.active_object:
|
|
|
|
|
cls.poll_message_set("Select an object or pose bone")
|
|
|
|
|
return False
|
|
|
|
|
if context.mode not in {'POSE', 'OBJECT'}:
|
|
|
|
|
cls.poll_message_set("Switch to Pose or Object mode")
|
|
|
|
|
return False
|
|
|
|
|
if not context.scene.camera:
|
|
|
|
|
cls.poll_message_set("The Scene needs a camera")
|
|
|
|
|
return False
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
def execute(self, context: Context) -> set[str]:
|
|
|
|
|
match context.mode:
|
|
|
|
|
case 'OBJECT':
|
|
|
|
|
transformables = self._transformable_objects(context)
|
|
|
|
|
case 'POSE':
|
|
|
|
|
transformables = self._transformable_pbones(context)
|
|
|
|
|
case mode:
|
|
|
|
|
self.report({'ERROR'}, 'Unsupported mode: %r' % mode)
|
|
|
|
|
return {'CANCELLED'}
|
|
|
|
|
|
|
|
|
|
restore_frame = context.scene.frame_current
|
2025-01-20 12:08:12 +01:00
|
|
|
restore_matrices = [(transformable, transformable.matrix_world().copy()) for transformable in transformables]
|
|
|
|
|
|
2024-05-15 23:39:31 +10:00
|
|
|
try:
|
|
|
|
|
self._execute(context, transformables)
|
|
|
|
|
finally:
|
2025-01-20 12:08:12 +01:00
|
|
|
# Restore the state of the scene & the transformables. This is necessary
|
|
|
|
|
# as not all properties may have been auto-keyed (for example 'only
|
|
|
|
|
# available' enabled, and rotation is not actually keyed yet), so we can't
|
|
|
|
|
# assume that going to the original frame restores the entire matrix.
|
2024-05-15 23:39:31 +10:00
|
|
|
context.scene.frame_set(restore_frame)
|
2025-01-20 12:08:12 +01:00
|
|
|
for transformable, matrix in restore_matrices:
|
|
|
|
|
transformable.set_matrix_world(context, matrix)
|
|
|
|
|
|
2024-05-15 23:39:31 +10:00
|
|
|
return {'FINISHED'}
|
|
|
|
|
|
|
|
|
|
def _transformable_objects(self, context: Context) -> list[Transformable]:
|
|
|
|
|
return [TransformableObject(object=ob) for ob in context.selected_editable_objects]
|
|
|
|
|
|
|
|
|
|
def _transformable_pbones(self, context: Context) -> list[Transformable]:
|
|
|
|
|
return [TransformableBone(pose_bone=bone) for bone in context.selected_pose_bones]
|
|
|
|
|
|
|
|
|
|
|
2025-01-20 12:08:12 +01:00
|
|
|
class OBJECT_OT_fix_to_camera(FixToCameraCommon, Operator):
|
2024-05-15 23:39:31 +10:00
|
|
|
bl_idname = "object.fix_to_camera"
|
|
|
|
|
bl_label = "Fix to Scene Camera"
|
|
|
|
|
bl_description = "Generate new keys to fix the selected object/bone to the camera on unkeyed frames"
|
|
|
|
|
bl_options = {'REGISTER', 'UNDO'}
|
|
|
|
|
|
Anim: Move "Copy Global Transform" extension to internal scripts
Move the Copy Global Transform core add-on into Blender's code.
- The entire extension was one Python file. This PR basically splits
it into two, one for operators (in `bl_operators`) and the other for
UI panels. Those panels are registered in the 3D viewport's sidebar,
which were registered in `space_view3d`, but I made the decision
here to create a new file `space_view3d_sidebar`, because the main
file is getting too large and difficult to navigate. This PR puts
the global transform panel in this file. After this is merged, I
will do refactors to move the rest of the sidebar panels here as
well.
- `AutoKeying` class was moved into `bpy_extras/anim_utils.py` so that
it's reusable and also accessible from API, since it's generally
very useful. There were discussions about putting this somewhere,
but for now, I chose against it because creating a new file would
also mean PR would have to affect documentation generation, and
would complicate things. If we want to, we can probably create a new
module in the future.
- Little tweaks to labels and descriptions. Now that they exist
outside of the add-on context, and exist without the user explicitly
enabling them, they need to be more descriptive and tell users what
they actually do. They also need to conform to Blender's GUI
guidelines. Also tried organizing files a little by grouping
objects.
- Add-on properties (which included word `addon` in the name) have
been registered in C++, on `scene.tool_settings` with `anim_`
prefix.
Pull Request: https://projects.blender.org/blender/blender/pulls/145414
2025-10-03 17:42:04 +02:00
|
|
|
use_location: bpy.props.BoolProperty( # type: ignore
|
2024-05-15 23:39:31 +10:00
|
|
|
name="Location",
|
|
|
|
|
description="Create Location keys when fixing to the scene camera",
|
|
|
|
|
default=True,
|
|
|
|
|
)
|
Anim: Move "Copy Global Transform" extension to internal scripts
Move the Copy Global Transform core add-on into Blender's code.
- The entire extension was one Python file. This PR basically splits
it into two, one for operators (in `bl_operators`) and the other for
UI panels. Those panels are registered in the 3D viewport's sidebar,
which were registered in `space_view3d`, but I made the decision
here to create a new file `space_view3d_sidebar`, because the main
file is getting too large and difficult to navigate. This PR puts
the global transform panel in this file. After this is merged, I
will do refactors to move the rest of the sidebar panels here as
well.
- `AutoKeying` class was moved into `bpy_extras/anim_utils.py` so that
it's reusable and also accessible from API, since it's generally
very useful. There were discussions about putting this somewhere,
but for now, I chose against it because creating a new file would
also mean PR would have to affect documentation generation, and
would complicate things. If we want to, we can probably create a new
module in the future.
- Little tweaks to labels and descriptions. Now that they exist
outside of the add-on context, and exist without the user explicitly
enabling them, they need to be more descriptive and tell users what
they actually do. They also need to conform to Blender's GUI
guidelines. Also tried organizing files a little by grouping
objects.
- Add-on properties (which included word `addon` in the name) have
been registered in C++, on `scene.tool_settings` with `anim_`
prefix.
Pull Request: https://projects.blender.org/blender/blender/pulls/145414
2025-10-03 17:42:04 +02:00
|
|
|
use_rotation: bpy.props.BoolProperty( # type: ignore
|
2024-05-15 23:39:31 +10:00
|
|
|
name="Rotation",
|
|
|
|
|
description="Create Rotation keys when fixing to the scene camera",
|
|
|
|
|
default=True,
|
|
|
|
|
)
|
|
|
|
|
use_scale: bpy.props.BoolProperty( # type: ignore
|
|
|
|
|
name="Scale",
|
|
|
|
|
description="Create Scale keys when fixing to the scene camera",
|
|
|
|
|
default=True,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def _get_matrices(self, camera: Camera, transformables: list[Transformable]) -> dict[Transformable, Matrix]:
|
|
|
|
|
camera_mat_inv = camera.matrix_world.inverted()
|
|
|
|
|
return {t: camera_mat_inv @ t.matrix_world() for t in transformables}
|
|
|
|
|
|
|
|
|
|
def _execute(self, context: Context, transformables: list[Transformable]) -> None:
|
2025-10-07 09:01:19 +11:00
|
|
|
from bpy_extras.anim_utils import AutoKeying
|
2024-05-15 23:39:31 +10:00
|
|
|
depsgraph = context.view_layer.depsgraph
|
|
|
|
|
scene = context.scene
|
|
|
|
|
|
|
|
|
|
scene.frame_set(scene.frame_start)
|
|
|
|
|
camera_eval = scene.camera.evaluated_get(depsgraph)
|
|
|
|
|
last_camera_name = scene.camera.name
|
|
|
|
|
matrices = self._get_matrices(camera_eval, transformables)
|
|
|
|
|
|
|
|
|
|
if scene.use_preview_range:
|
|
|
|
|
frame_start = scene.frame_preview_start
|
|
|
|
|
frame_end = scene.frame_preview_end
|
|
|
|
|
else:
|
|
|
|
|
frame_start = scene.frame_start
|
|
|
|
|
frame_end = scene.frame_end
|
|
|
|
|
|
|
|
|
|
with AutoKeying.options(
|
|
|
|
|
keytype=self.keytype,
|
Anim: Move "Copy Global Transform" extension to internal scripts
Move the Copy Global Transform core add-on into Blender's code.
- The entire extension was one Python file. This PR basically splits
it into two, one for operators (in `bl_operators`) and the other for
UI panels. Those panels are registered in the 3D viewport's sidebar,
which were registered in `space_view3d`, but I made the decision
here to create a new file `space_view3d_sidebar`, because the main
file is getting too large and difficult to navigate. This PR puts
the global transform panel in this file. After this is merged, I
will do refactors to move the rest of the sidebar panels here as
well.
- `AutoKeying` class was moved into `bpy_extras/anim_utils.py` so that
it's reusable and also accessible from API, since it's generally
very useful. There were discussions about putting this somewhere,
but for now, I chose against it because creating a new file would
also mean PR would have to affect documentation generation, and
would complicate things. If we want to, we can probably create a new
module in the future.
- Little tweaks to labels and descriptions. Now that they exist
outside of the add-on context, and exist without the user explicitly
enabling them, they need to be more descriptive and tell users what
they actually do. They also need to conform to Blender's GUI
guidelines. Also tried organizing files a little by grouping
objects.
- Add-on properties (which included word `addon` in the name) have
been registered in C++, on `scene.tool_settings` with `anim_`
prefix.
Pull Request: https://projects.blender.org/blender/blender/pulls/145414
2025-10-03 17:42:04 +02:00
|
|
|
use_loc=self.use_location,
|
|
|
|
|
use_rot=self.use_rotation,
|
2024-05-15 23:39:31 +10:00
|
|
|
use_scale=self.use_scale,
|
|
|
|
|
force_autokey=True,
|
|
|
|
|
):
|
|
|
|
|
for frame in range(frame_start, frame_end + scene.frame_step, scene.frame_step):
|
|
|
|
|
scene.frame_set(frame)
|
|
|
|
|
|
|
|
|
|
camera_eval = scene.camera.evaluated_get(depsgraph)
|
|
|
|
|
cam_matrix_world = camera_eval.matrix_world
|
|
|
|
|
camera_mat_inv = cam_matrix_world.inverted()
|
|
|
|
|
|
|
|
|
|
if scene.camera.name != last_camera_name:
|
|
|
|
|
# The scene camera changed, so the previous
|
|
|
|
|
# relative-to-camera matrices can no longer be used.
|
|
|
|
|
matrices = self._get_matrices(camera_eval, transformables)
|
|
|
|
|
last_camera_name = scene.camera.name
|
|
|
|
|
|
|
|
|
|
for t, camera_rel_matrix in matrices.items():
|
|
|
|
|
key_info = t.key_info()
|
|
|
|
|
key_type = key_info.get(frame, "")
|
|
|
|
|
if key_type not in {self.keytype, ""}:
|
|
|
|
|
# Manually set key, remember the current camera-relative matrix.
|
|
|
|
|
matrices[t] = camera_mat_inv @ t.matrix_world()
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
# No key, or a generated one. Overwrite it with a new transform.
|
2025-01-20 12:08:12 +01:00
|
|
|
t.set_matrix_world_autokey(context, cam_matrix_world @ camera_rel_matrix)
|
2024-05-15 23:39:31 +10:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class OBJECT_OT_delete_fix_to_camera_keys(Operator, FixToCameraCommon):
|
|
|
|
|
bl_idname = "object.delete_fix_to_camera_keys"
|
|
|
|
|
bl_label = "Delete Generated Keys"
|
|
|
|
|
bl_description = "Delete all keys that were generated by the 'Fix to Scene Camera' operator"
|
|
|
|
|
bl_options = {'REGISTER', 'UNDO'}
|
|
|
|
|
|
|
|
|
|
def _execute(self, context: Context, transformables: list[Transformable]) -> None:
|
|
|
|
|
scene = context.scene
|
|
|
|
|
if scene.use_preview_range:
|
|
|
|
|
frame_start = scene.frame_preview_start
|
|
|
|
|
frame_end = scene.frame_preview_end
|
|
|
|
|
else:
|
|
|
|
|
frame_start = scene.frame_start
|
|
|
|
|
frame_end = scene.frame_end
|
|
|
|
|
|
|
|
|
|
for t in transformables:
|
|
|
|
|
t.remove_keys_of_type(self.keytype, frame_start=frame_start, frame_end=frame_end)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Messagebus subscription to monitor changes & refresh panels.
|
|
|
|
|
_msgbus_owner = object()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _refresh_3d_panels():
|
|
|
|
|
refresh_area_types = {'VIEW_3D'}
|
|
|
|
|
for win in bpy.context.window_manager.windows:
|
|
|
|
|
for area in win.screen.areas:
|
|
|
|
|
if area.type not in refresh_area_types:
|
|
|
|
|
continue
|
|
|
|
|
area.tag_redraw()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
classes = (
|
|
|
|
|
OBJECT_OT_copy_global_transform,
|
|
|
|
|
OBJECT_OT_copy_relative_transform,
|
|
|
|
|
OBJECT_OT_paste_transform,
|
|
|
|
|
OBJECT_OT_fix_to_camera,
|
|
|
|
|
OBJECT_OT_delete_fix_to_camera_keys,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _register_message_bus() -> None:
|
|
|
|
|
bpy.msgbus.subscribe_rna(
|
|
|
|
|
key=(bpy.types.ToolSettings, "use_keyframe_insert_auto"),
|
|
|
|
|
owner=_msgbus_owner,
|
|
|
|
|
args=(),
|
|
|
|
|
notify=_refresh_3d_panels,
|
|
|
|
|
options={'PERSISTENT'},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _unregister_message_bus() -> None:
|
|
|
|
|
bpy.msgbus.clear_by_owner(_msgbus_owner)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@bpy.app.handlers.persistent # type: ignore
|
2025-10-07 09:15:20 +11:00
|
|
|
def _on_blendfile_load_post(_none: Any, _other_none: Any) -> None:
|
2024-05-15 23:39:31 +10:00
|
|
|
# The parameters are required, but both are None.
|
|
|
|
|
_register_message_bus()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def register():
|
|
|
|
|
bpy.app.handlers.load_post.append(_on_blendfile_load_post)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def unregister():
|
|
|
|
|
_unregister_message_bus()
|
|
|
|
|
bpy.app.handlers.load_post.remove(_on_blendfile_load_post)
|