Merge branch 'blender-v4.0-release'
[blender-addons.git] / pose_library / conversion.py
blob08c250e578a95b3319ce3484686f583cc538d36c
1 # SPDX-FileCopyrightText: 2021-2023 Blender Foundation
3 # SPDX-License-Identifier: GPL-2.0-or-later
5 """
6 Pose Library - Conversion of old pose libraries.
7 """
9 from typing import Optional
10 from collections.abc import Collection
12 if "pose_creation" not in locals():
13 from . import pose_creation
14 else:
15 import importlib
17 pose_creation = importlib.reload(pose_creation)
19 import bpy
20 from bpy.types import (
21 Action,
22 TimelineMarker,
26 def convert_old_poselib(old_poselib: Action) -> Collection[Action]:
27 """Convert an old-style pose library to a set of pose Actions.
29 Old pose libraries were one Action with multiple pose markers. Each pose
30 marker will be converted to an Action by itself and marked as asset.
31 """
33 pose_assets = [action for marker in old_poselib.pose_markers if (action := convert_old_pose(old_poselib, marker))]
35 # Mark all Actions as assets in one go. Ideally this would be done on an
36 # appropriate frame in the scene (to set up things like the background
37 # colour), but the old-style poselib doesn't contain such information. All
38 # we can do is just render on the current frame.
39 context_override = {'selected_ids': pose_assets}
40 with bpy.context.temp_override(**context_override):
41 bpy.ops.asset.mark()
43 return pose_assets
46 def convert_old_pose(old_poselib: Action, marker: TimelineMarker) -> Optional[Action]:
47 """Convert an old-style pose library pose to a pose action."""
49 frame: int = marker.frame
50 action: Optional[Action] = None
52 for fcurve in old_poselib.fcurves:
53 key = pose_creation.find_keyframe(fcurve, frame)
54 if not key:
55 continue
57 if action is None:
58 action = bpy.data.actions.new(marker.name)
60 pose_creation.create_single_key_fcurve(action, fcurve, key)
62 return action