blender/tests/python/bl_pose_assets.py

182 lines
7.1 KiB
Python
Raw Permalink Normal View History

# SPDX-FileCopyrightText: 2025 Blender Authors
#
# SPDX-License-Identifier: GPL-2.0-or-later
"""
blender -b --factory-startup --python tests/python/bl_pose_assets.py -- --testdir /path/to/tests/files/animation
"""
__all__ = (
"main",
)
Anim: create pose assets to different libraries Similar to how brush assets are created and managed this PR allows to export pose assets into a different library. Because of this there is a limitation to this where each asset is stored in a separate blend file. This may be lifted in the future as there are planned changes in the design phase: #122061 ### Create Asset Now available in the 3D viewport in the "Pose" menu: "Create Pose Asset". The button in the Dope Sheet will now call this new operator as well. Clicking either of those will open a popup in which you can: * Choose the name of the asset, which library and catalog it goes into. * Clicking "Create" will create a pose asset on disk in the given library. It is possible to create files into an outside library or add it in the current file. The latter option does a lot less since it basically just creates the action and tags it as an asset. If no Asset Shelf **AND** no Asset Browser is visible anywhere in Blender, the Asset Shelf will be shown on the 3D viewport from which the operator was called. ### Adjust Pose Asset Right clicking a pose asset that has been created in the way described before will have options to overwrite it. Only the active object will be considered for updating a pose asset Available Options (the latter 3 under the "Modify Pose Asset" submenu): * Adjust Pose Asset: From the selected bones, update ONLY channels that are also present in the asset. This is the default. * Replace: Will completely replace the data in the Pose Asset from the current selection * Add: Adds the current selection to the Pose Asset. Any already existing channels have their values updated * Remove: Remove selected bones from the pose asset Currently this refreshes the thumbnail. In the case of custom thumbnails it might not be something want ### Deleting an existing Pose Asset Right click on a Pose Asset and hit "Delete Pose Asset". Works in the shelf and in the asset library. Doing so will pop up a confirmation dialog, if confirming, the asset is gone forever. Deleting a local asset is basically the same as clearing the asset. This is a bit confusing because you get two options that basically do the same thing sometimes, but "Delete" works in other cases as well. I currently don't see a way around that. Part of design #131840 Pull Request: https://projects.blender.org/blender/blender/pulls/132747
2025-02-04 11:29:05 +01:00
import unittest
import bpy
import pathlib
import sys
import tempfile
import os
_BONE_NAME_1 = "bone"
_BONE_NAME_2 = "bone_2"
_LIB_NAME = "unit_test"
_BBONE_VALUES = {
f'pose.bones["{_BONE_NAME_1}"].bbone_curveinx': (0, ),
f'pose.bones["{_BONE_NAME_1}"].bbone_curveoutx': (0, ),
f'pose.bones["{_BONE_NAME_1}"].bbone_curveinz': (0, ),
f'pose.bones["{_BONE_NAME_1}"].bbone_curveoutz': (0, ),
f'pose.bones["{_BONE_NAME_1}"].bbone_rollin': (0, ),
f'pose.bones["{_BONE_NAME_1}"].bbone_rollout': (0, ),
f'pose.bones["{_BONE_NAME_1}"].bbone_scalein': (1, 1, 1),
f'pose.bones["{_BONE_NAME_1}"].bbone_scaleout': (1, 1, 1),
f'pose.bones["{_BONE_NAME_1}"].bbone_easein': (0, ),
f'pose.bones["{_BONE_NAME_1}"].bbone_easeout': (0, ),
}
def _create_armature():
armature = bpy.data.armatures.new("anim_armature")
armature_obj = bpy.data.objects.new("anim_object", armature)
bpy.context.scene.collection.objects.link(armature_obj)
bpy.context.view_layer.objects.active = armature_obj
armature_obj.select_set(True)
bpy.ops.object.mode_set(mode='EDIT')
edit_bone = armature.edit_bones.new(_BONE_NAME_1)
edit_bone.head = (1, 0, 0)
edit_bone = armature.edit_bones.new(_BONE_NAME_2)
edit_bone.head = (1, 0, 0)
return armature_obj
class CreateAssetTest(unittest.TestCase):
_library_folder = None
_library = None
_armature_object = None
def setUp(self):
super().setUp()
bpy.ops.wm.read_homefile(use_factory_startup=True)
self._armature_object = _create_armature()
self._library_folder = tempfile.TemporaryDirectory("pose_asset_test")
self._library = bpy.types.AssetLibraryCollection.new(name=_LIB_NAME, directory=self._library_folder.name)
bpy.context.view_layer.objects.active = self._armature_object
bpy.ops.object.mode_set(mode='POSE')
self._armature_object.pose.bones[_BONE_NAME_1]["bool_test"] = True
self._armature_object.pose.bones[_BONE_NAME_1]["float_test"] = 3.14
self._armature_object.pose.bones[_BONE_NAME_1]["string_test"] = "foobar"
def tearDown(self):
super().tearDown()
bpy.types.AssetLibraryCollection.remove(self._library)
self._library = None
self._library_folder.cleanup()
def test_create_local_asset(self):
self._armature_object.pose.bones[_BONE_NAME_1].location = (1, 1, 2)
self._armature_object.pose.bones[_BONE_NAME_2].location = (-1, 0, 0)
self._armature_object.pose.bones[_BONE_NAME_1].bone.select = True
self._armature_object.pose.bones[_BONE_NAME_2].bone.select = False
self.assertEqual(len(bpy.data.actions), 0)
bpy.ops.poselib.create_pose_asset(
pose_name="local_asset",
asset_library_reference='LOCAL',
catalog_path="unit_test")
self.assertEqual(len(bpy.data.actions), 1, "Local poses should be stored as actions")
pose_action = bpy.data.actions[0]
self.assertTrue(pose_action.asset_data is not None, "The created action should be marked as an asset")
expected_pose_values = {
f'pose.bones["{_BONE_NAME_1}"].location': (1, 1, 2),
f'pose.bones["{_BONE_NAME_1}"].rotation_quaternion': (1, 0, 0, 0),
f'pose.bones["{_BONE_NAME_1}"].scale': (1, 1, 1),
f'pose.bones["{_BONE_NAME_1}"]["bool_test"]': (True, ),
f'pose.bones["{_BONE_NAME_1}"]["float_test"]': (3.14, ),
# string_test is not here because it should not be keyed.
}
expected_pose_values.update(_BBONE_VALUES)
self.assertEqual(len(pose_action.fcurves), 26)
for fcurve in pose_action.fcurves:
self.assertTrue(
fcurve.data_path in expected_pose_values,
"Only the selected bone should be in the pose asset")
self.assertEqual(len(fcurve.keyframe_points), 1, "Only one key should have been created")
self.assertEqual(fcurve.keyframe_points[0].co.x, 1, "Poses should be on the first frame")
self.assertAlmostEqual(fcurve.keyframe_points[0].co.y,
expected_pose_values[fcurve.data_path][fcurve.array_index], 4)
def test_create_outside_asset(self):
self._armature_object.pose.bones[_BONE_NAME_1].location = (1, 1, 2)
self._armature_object.pose.bones[_BONE_NAME_2].location = (-1, 0, 0)
self._armature_object.pose.bones[_BONE_NAME_1].bone.select = True
self._armature_object.pose.bones[_BONE_NAME_2].bone.select = False
self.assertEqual(len(bpy.data.actions), 0)
bpy.ops.poselib.create_pose_asset(
pose_name="local_asset",
asset_library_reference=_LIB_NAME,
catalog_path="unit_test")
self.assertEqual(len(bpy.data.actions), 0, "The asset should not have been created in this file")
actions_folder = os.path.join(self._library.path, "Saved", "Actions")
asset_files = os.listdir(actions_folder)
self.assertEqual(len(asset_files),
1, "The pose asset file should have been created")
with bpy.data.libraries.load(os.path.join(actions_folder, asset_files[0])) as (data_from, data_to):
self.assertEqual(data_from.actions, ["local_asset"])
data_to.actions = data_from.actions
pose_action = data_to.actions[0]
self.assertTrue(pose_action.asset_data is not None, "The created action should be marked as an asset")
expected_pose_values = {
f'pose.bones["{_BONE_NAME_1}"].location': (1, 1, 2),
f'pose.bones["{_BONE_NAME_1}"].rotation_quaternion': (1, 0, 0, 0),
f'pose.bones["{_BONE_NAME_1}"].scale': (1, 1, 1),
f'pose.bones["{_BONE_NAME_1}"]["bool_test"]': (True, ),
f'pose.bones["{_BONE_NAME_1}"]["float_test"]': (3.14, ),
# string_test is not here because it should not be keyed.
}
expected_pose_values.update(_BBONE_VALUES)
self.assertEqual(len(pose_action.fcurves), 26)
for fcurve in pose_action.fcurves:
self.assertTrue(
fcurve.data_path in expected_pose_values,
"Only the selected bone should be in the pose asset")
self.assertEqual(len(fcurve.keyframe_points), 1, "Only one key should have been created")
self.assertEqual(fcurve.keyframe_points[0].co.x, 1, "Poses should be on the first frame")
self.assertAlmostEqual(fcurve.keyframe_points[0].co.y,
expected_pose_values[fcurve.data_path][fcurve.array_index], 4)
def main():
global args
import argparse
if '--' in sys.argv:
argv = [sys.argv[0]] + sys.argv[sys.argv.index('--') + 1:]
else:
argv = sys.argv
parser = argparse.ArgumentParser()
parser.add_argument('--testdir', required=True, type=pathlib.Path)
args, remaining = parser.parse_known_args(argv)
unittest.main(argv=remaining)
if __name__ == "__main__":
main()