Skip to content
Snippets Groups Projects
copy_global_transform.py 15.7 KiB
Newer Older
  • Learn to ignore specific revisions
  • 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 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
    # ====================== 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 ========================
    
    """
    Copy Global Transform
    
    Simple add-on for copying world-space transforms.
    
    It's called "global" to avoid confusion with the Blender World data-block.
    """
    
    bl_info = {
        "name": "Copy Global Transform",
        "author": "Sybren A. Stüvel",
        "version": (2, 0),
        "blender": (3, 1, 0),
        "location": "N-panel in the 3D Viewport",
        "category": "Animation",
        "support": 'OFFICIAL',
    }
    
    import ast
    from typing import Iterable, Optional, Union, Any
    
    import bpy
    from bpy.types import Context, Object, Operator, Panel, PoseBone
    from mathutils import Matrix
    
    
    class AutoKeying:
        """Auto-keying support.
    
        Based on Rigify code by Alexander Gavrilov.
        """
    
        @classmethod
        def keying_options(cls, context: 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: 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: 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[Object, 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[Object, PoseBone],
            options: set[str],
        ) -> None:
            """Keyframe transformation properties, avoiding keying locked channels."""
    
            is_bone = isinstance(target, PoseBone)
            if is_bone:
                group = target.name
            else:
                group = "Object Transforms"
    
            def keyframe(data_path: str, locks: Iterable[bool]) -> None:
                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: Context, target: Union[Object, 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: 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:
        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)
    
    
    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.
        """
        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)
        return _selected_keyframes_in_action(object, f'pose.bones["{name}"].')
    
    
    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.
        """
        return _selected_keyframes_in_action(object, "")
    
    
    def _selected_keyframes_in_action(object: Object, rna_path_prefix: str) -> list[float]:
        """Return the list of frame numbers that have a selected key.
    
        Only keys on the given object's Action on FCurves starting with rna_path_prefix are considered.
        """
    
        action = object.animation_data and object.animation_data.action
        if action is None:
            return []
    
        keyframes = set()
        for fcurve in action.fcurves:
            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)
    
    
    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)
            rows = [f"    {tuple(row)!r}," for row in mat]
            as_string = "\n".join(rows)
            context.window_manager.clipboard = f"Matrix((\n{as_string}\n))"
            return {'FINISHED'}
    
    
    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",
        )
        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,
        )
    
        @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 not context.window_manager.clipboard.startswith("Matrix("):
                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)
    
        def execute(self, context: Context) -> set[str]:
            clipboard = context.window_manager.clipboard
            if clipboard.startswith("Matrix"):
                mat = Matrix(ast.literal_eval(clipboard[6:]))
            else:
                mat = self.parse_print_m4(clipboard)
    
            if mat is None:
                self.report({'ERROR'}, "Clipboard does not contain a valid matrix.")
                return {'CANCELLED'}
    
            applicator = {
                'CURRENT': self._paste_current,
                'EXISTING_KEYS': self._paste_existing_keys,
                'BAKE': self._paste_bake,
            }[self.method]
            return applicator(context, mat)
    
        @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 VIEW3D_PT_copy_global_transform(Panel):
        bl_space_type = 'VIEW_3D'
        bl_region_type = 'UI'
        bl_category = "Animation"
        bl_label = "Global Transform"
    
        def draw(self, context: Context) -> None:
            layout = self.layout
    
            # No need to put "Global Transform" in the operator text, given that it's already in the panel title.
            layout.operator("object.copy_global_transform", text="Copy", icon='COPYDOWN')
    
            paste_col = layout.column(align=True)
            paste_col.operator("object.paste_transform", text="Paste", icon='PASTEDOWN').method = 'CURRENT'
            wants_autokey_col = paste_col.column(align=True)
            has_autokey = context.scene.tool_settings.use_keyframe_insert_auto
            wants_autokey_col.enabled = has_autokey
            if not has_autokey:
                wants_autokey_col.label(text="These require auto-key:")
    
            wants_autokey_col.operator(
                "object.paste_transform",
                text="Paste to Selected Keys",
                icon='PASTEDOWN',
            ).method = 'EXISTING_KEYS'
            wants_autokey_col.operator(
                "object.paste_transform",
                text="Paste and Bake",
                icon='PASTEDOWN',
            ).method = 'BAKE'
    
    
    ### 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_paste_transform,
        VIEW3D_PT_copy_global_transform,
    )
    _register, _unregister = bpy.utils.register_classes_factory(classes)
    
    
    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
    def _on_blendfile_load_post(none: Any, other_none: Any) -> None:
        # The parameters are required, but both are None.
        _register_message_bus()
    
    
    def register():
        _register()
        bpy.app.handlers.load_post.append(_on_blendfile_load_post)
    
    
    def unregister():
        _unregister()
        _unregister_message_bus()
        bpy.app.handlers.load_post.remove(_on_blendfile_load_post)