Blender V5.3
action_iterators_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4#include "ANIM_action.hh"
6
7#include "BKE_gtest_base.hh"
8#include "BKE_idtype.hh"
9#include "BKE_lib_id.hh"
10#include "BKE_main.hh"
11#include "BKE_object.hh"
12
13#include "DNA_anim_types.h"
14#include "DNA_object_types.h"
15
16#include "RNA_access.hh"
17#include "RNA_prototypes.hh"
18
19#include "testing/testing.h"
20
23 public:
26
27 void SetUp() override
28 {
30 action = BKE_id_new<Action>(bmain, "ACLayeredAction");
31 }
32
33 void TearDown() override
34 {
36 }
37};
38
39TEST_F(ActionIteratorsTest, iterate_all_fcurves_of_slot)
40{
41 Slot &cube_slot = action->slot_add();
42 Slot &monkey_slot = action->slot_add();
43
44 /* Try iterating an empty action. */
45 Vector<const FCurve *> no_fcurves;
47 *action, cube_slot.handle, [&](const FCurve &fcurve) { no_fcurves.append(&fcurve); });
48
49 ASSERT_TRUE(no_fcurves.is_empty());
50
51 Layer &layer = action->layer_add("Layer One");
52 Strip &strip = layer.strip_add(*action, Strip::Type::Keyframe);
53 StripKeyframeData &strip_data = strip.data<StripKeyframeData>(*action);
54 const KeyframeSettings settings = get_keyframe_settings(false);
55
56 /* Insert 3 FCurves for each slot. */
57 for (int i = 0; i < 3; i++) {
58 SingleKeyingResult result_cube = strip_data.keyframe_insert(
59 bmain, cube_slot, {"location", i}, {1.0f, 0.0f}, settings);
60 ASSERT_EQ(SingleKeyingResult::SUCCESS, result_cube)
61 << "Expected keyframe insertion to be successful";
62
63 SingleKeyingResult result_monkey = strip_data.keyframe_insert(
64 bmain, monkey_slot, {"rotation", i}, {1.0f, 0.0f}, settings);
65 ASSERT_EQ(SingleKeyingResult::SUCCESS, result_monkey)
66 << "Expected keyframe insertion to be successful";
67 }
68
69 /* Get all FCurves. */
70 Vector<const FCurve *> cube_fcurves;
72 *action, cube_slot.handle, [&](const FCurve &fcurve) { cube_fcurves.append(&fcurve); });
73
74 ASSERT_EQ(cube_fcurves.size(), 3);
75 for (const FCurve *fcurve : cube_fcurves) {
76 ASSERT_EQ(fcurve->rna_path(), "location");
77 }
78
79 /* Get only FCurves with index 0 which should be 1. */
80 Vector<const FCurve *> monkey_fcurves;
81 foreach_fcurve_in_action_slot(*action, monkey_slot.handle, [&](const FCurve &fcurve) {
82 if (fcurve.array_index == 0) {
83 monkey_fcurves.append(&fcurve);
84 }
85 });
86
87 ASSERT_EQ(monkey_fcurves.size(), 1);
88 ASSERT_EQ(monkey_fcurves[0]->rna_path(), "rotation");
89
90 /* Slots handles are just numbers. Passing in a slot handle that doesn't exist should return
91 * nothing. */
92 Vector<const FCurve *> invalid_slot_fcurves;
94 *action, monkey_slot.handle + cube_slot.handle, [&](const FCurve &fcurve) {
95 invalid_slot_fcurves.append(&fcurve);
96 });
97 ASSERT_TRUE(invalid_slot_fcurves.is_empty());
98}
99
101{
102 /* Create a cube and assign the Action + a slot. */
103 Object *cube = BKE_id_new<Object>(bmain, "OBCube");
104 Slot *slot_cube = assign_action_ensure_slot_for_keying(*action, cube->id);
105 ASSERT_NE(slot_cube, nullptr);
106
107 /* Create another Action with slot to assign. */
108 Action &other_action = BKE_id_new<bAction>(bmain, "ACAnotherAction")->wrap();
109 Slot &another_slot = other_action.slot_add();
110
111 std::optional<ActionSlotAssignmentResult> slot_assignment_result;
112
113 bool all_assigns_ok = true;
114 const auto assign_other_action = [&](ID & /* animated_id */,
115 bAction *&action_ptr_ref,
116 slot_handle_t &slot_handle_ref,
117 char *last_slot_identifier) -> bool {
118 /* Assign the other Action. */
119 all_assigns_ok &= generic_assign_action(
120 cube->id, &other_action, action_ptr_ref, slot_handle_ref, last_slot_identifier);
121
122 /* Assign the slot of the other Action. */
123 slot_assignment_result = generic_assign_action_slot(
124 &another_slot, cube->id, action_ptr_ref, slot_handle_ref, last_slot_identifier);
125
126 return true;
127 };
128
129 foreach_action_slot_use_with_references(cube->id, assign_other_action);
130 ASSERT_TRUE(all_assigns_ok);
131
132 /* Check the result, the slot assignment should have been changed. */
133 ASSERT_TRUE(slot_assignment_result.has_value());
134 EXPECT_EQ(ActionSlotAssignmentResult::OK, slot_assignment_result.value());
135
136 std::optional<std::pair<Action *, Slot *>> action_and_slot = get_action_slot_pair(cube->id);
137
138 ASSERT_TRUE(action_and_slot.has_value());
139 EXPECT_EQ(&other_action, action_and_slot->first)
140 << "Expected Action " << other_action.id.name << " but found "
141 << action_and_slot->first->id.name;
142 EXPECT_EQ(&another_slot, action_and_slot->second)
143 << "Expected Slot " << another_slot.identifier << " but found "
144 << action_and_slot->second->identifier;
145}
146
148{
149 /* Create a cube and assign the Action + a slot. */
150 Object *cube = BKE_id_new<Object>(bmain, "OBCube");
151 Slot *slot_cube = assign_action_ensure_slot_for_keying(*action, cube->id);
152 ASSERT_NE(slot_cube, nullptr);
153 Slot &another_slot = action->slot_add();
154
155 const auto assign_other_slot = [&](ID & /* animated_id */,
156 bAction *action,
157 PointerRNA &action_slot_owner_ptr,
158 PropertyRNA &action_slot_prop,
159 char * /*last_slot_identifier*/) -> bool {
160 PointerRNA rna_slot = RNA_pointer_create_discrete(&action->id, RNA_ActionSlot, &another_slot);
161 RNA_property_pointer_set(&action_slot_owner_ptr, &action_slot_prop, rna_slot, nullptr);
162 return true;
163 };
164
165 foreach_action_slot_use_with_rna(cube->id, assign_other_slot);
166
167 /* Check the result, the slot assignment should have been changed. */
168 std::optional<std::pair<Action *, Slot *>> action_and_slot = get_action_slot_pair(cube->id);
169
170 ASSERT_TRUE(action_and_slot.has_value());
171 EXPECT_EQ(action, action_and_slot->first)
172 << "Expected Action " << action->id.name << " but found " << action_and_slot->first->id.name;
173 EXPECT_EQ(&another_slot, action_and_slot->second)
174 << "Expected Slot " << another_slot.identifier << " but found "
175 << action_and_slot->second->identifier;
176}
177
178} // namespace blender::animrig::tests
Functions and classes to work with Actions.
Functionality to iterate an Action in various ways.
General operations, lookup, etc. for blender objects.
Object is a sort of wrapper for general info.
int64_t size() const
bool is_empty() const
SingleKeyingResult keyframe_insert(Main *bmain, const Slot &slot, const FCurveDescriptor &fcurve_descriptor, float2 time_value, const KeyframeSettings &settings, eInsertKeyFlags insert_key_flags=INSERTKEY_NOFLAGS, std::optional< float2 > cycle_range=std::nullopt)
const T & data(const Action &owning_action) const
#define layer
TEST_F(ActionIteratorsTest, iterate_all_fcurves_of_slot)
void foreach_fcurve_in_action_slot(Action &action, slot_handle_t handle, FunctionRef< void(FCurve &fcurve)> callback)
decltype(blender::ActionSlot::handle) slot_handle_t
KeyframeSettings get_keyframe_settings(bool from_userprefs)
Slot * assign_action_ensure_slot_for_keying(Action &action, ID &animated_id)
bool generic_assign_action(ID &animated_id, bAction *action_to_assign, bAction *&action_ptr_ref, slot_handle_t &slot_handle_ref, char *slot_identifier)
ActionSlotAssignmentResult generic_assign_action_slot(Slot *slot_to_assign, ID &animated_id, bAction *&action_ptr_ref, slot_handle_t &slot_handle_ref, char *slot_identifier)
bool foreach_action_slot_use_with_rna(ID &animated_id, FunctionRef< bool(ID &animated_id, bAction *action, PointerRNA &action_slot_owner_ptr, PropertyRNA &action_slot_prop, char *last_slot_identifier)> callback)
std::optional< std::pair< Action *, Slot * > > get_action_slot_pair(ID &animated_id)
bool foreach_action_slot_use_with_references(ID &animated_id, FunctionRef< bool(ID &animated_id, bAction *&action_ptr_ref, slot_handle_t &slot_handle_ref, char *last_slot_identifier)> callback)
void RNA_property_pointer_set(PointerRNA *ptr, PropertyRNA *prop, PointerRNA ptr_value, ReportList *reports)
Main * BKE_main_new()
Definition main.cc:93
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
void BKE_main_free(Main *bmain)
Definition main.cc:202
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
void * BKE_id_new(Main *bmain, short type, const char *name)
Definition lib_id.cc:1612
char name[258]
Definition DNA_ID.h:433