diff options
author | Sybren A. Stüvel <sybren@blender.org> | 2021-07-20 17:13:14 +0300 |
---|---|---|
committer | Sybren A. Stüvel <sybren@blender.org> | 2021-07-20 18:12:01 +0300 |
commit | 6754d7aef613ee2eb12baa85b7f99969045c91e2 (patch) | |
tree | fd47349c44d9209b32ed3de8bb1f5684a7907942 /source/blender/blenkernel/intern/armature_test.cc | |
parent | c4f71f319366eb34e732f05b5162d627a02752c7 (diff) |
Pose Library: remove assumption about Action group names
Remove the assumption of the pose library that Action groups are named
after the bones in the armature. Even though this assumption is correct
when the keys are created by Blender, action groups can be renamed. Keys
created by Python scripts can also use arbitrary group names.
Since there is more code in Blender making this assumption, and looping
over selected bones is also a common occurrence, this commit contains
some generic functionality to aid in this:
- `BKE_armature_find_selected_bones`: function that iterates over all
bones in an armature and calls a callback for each selected one. It
returns a struct with info about the selection states (all or no bones
selected).
- `BKE_armature_find_selected_bone_names(armature)` uses the above
function to return a set of selected bone names.
- `BKE_pose_find_fcurves_with_bones()` calls a callback for each FCurve
in an Action that targets a bone, also passing it the bone name.
Diffstat (limited to 'source/blender/blenkernel/intern/armature_test.cc')
-rw-r--r-- | source/blender/blenkernel/intern/armature_test.cc | 77 |
1 files changed, 76 insertions, 1 deletions
diff --git a/source/blender/blenkernel/intern/armature_test.cc b/source/blender/blenkernel/intern/armature_test.cc index 589337d9d01..0ad2b58f5d9 100644 --- a/source/blender/blenkernel/intern/armature_test.cc +++ b/source/blender/blenkernel/intern/armature_test.cc @@ -17,10 +17,13 @@ * All rights reserved. */ -#include "BKE_armature.h" +#include "BKE_armature.hh" +#include "BLI_listbase.h" #include "BLI_math.h" +#include "DNA_armature_types.h" + #include "testing/testing.h" namespace blender::bke::tests { @@ -157,4 +160,76 @@ TEST(vec_roll_to_mat3_normalized, Rotationmatrix) } } +class BKE_armature_find_selected_bones_test : public testing::Test { + protected: + bArmature arm; + Bone bone1, bone2, bone3; + + void SetUp() override + { + strcpy(bone1.name, "bone1"); + strcpy(bone2.name, "bone2"); + strcpy(bone3.name, "bone3"); + + arm.bonebase = {NULL, NULL}; + BLI_addtail(&arm.bonebase, &bone1); + BLI_addtail(&arm.bonebase, &bone2); + BLI_addtail(&arm.bonebase, &bone3); + + // Make sure the armature & its bones are visible, to make them selectable. + arm.layer = bone1.layer = bone2.layer = bone3.layer = 1; + } +}; + +TEST_F(BKE_armature_find_selected_bones_test, some_bones_selected) +{ + bone1.flag = BONE_SELECTED; + bone2.flag = 0; + bone3.flag = BONE_SELECTED; + + std::vector<Bone *> seen_bones; + auto callback = [&](Bone *bone) { seen_bones.push_back(bone); }; + + SelectedBonesResult result = BKE_armature_find_selected_bones(&arm, callback); + + ASSERT_EQ(seen_bones.size(), 2) << "Expected 2 selected bones, got " << seen_bones.size(); + EXPECT_EQ(seen_bones[0], &bone1); + EXPECT_EQ(seen_bones[1], &bone3); + + EXPECT_FALSE(result.all_bones_selected); // Bone 2 was not selected. + EXPECT_FALSE(result.no_bones_selected); // Bones 1 and 3 were selected. +} + +TEST_F(BKE_armature_find_selected_bones_test, no_bones_selected) +{ + bone1.flag = bone2.flag = bone3.flag = 0; + + std::vector<Bone *> seen_bones; + auto callback = [&](Bone *bone) { seen_bones.push_back(bone); }; + + SelectedBonesResult result = BKE_armature_find_selected_bones(&arm, callback); + + EXPECT_TRUE(seen_bones.empty()) << "Expected no selected bones, got " << seen_bones.size(); + EXPECT_FALSE(result.all_bones_selected); + EXPECT_TRUE(result.no_bones_selected); +} + +TEST_F(BKE_armature_find_selected_bones_test, all_bones_selected) +{ + bone1.flag = bone2.flag = bone3.flag = BONE_SELECTED; + + std::vector<Bone *> seen_bones; + auto callback = [&](Bone *bone) { seen_bones.push_back(bone); }; + + SelectedBonesResult result = BKE_armature_find_selected_bones(&arm, callback); + + ASSERT_EQ(seen_bones.size(), 3) << "Expected 3 selected bones, got " << seen_bones.size(); + EXPECT_EQ(seen_bones[0], &bone1); + EXPECT_EQ(seen_bones[1], &bone2); + EXPECT_EQ(seen_bones[2], &bone3); + + EXPECT_TRUE(result.all_bones_selected); + EXPECT_FALSE(result.no_bones_selected); +} + } // namespace blender::bke::tests |