Blender V5.3
animrig/intern/nla_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "ANIM_action.hh"
6#include "ANIM_nla.hh"
7
8#include "BKE_action.hh"
9#include "BKE_anim_data.hh"
10#include "BKE_gtest_base.hh"
11#include "BKE_idtype.hh"
12#include "BKE_lib_id.hh"
13#include "BKE_main.hh"
14#include "BKE_nla.hh"
15#include "BKE_object.hh"
16
17#include "DNA_anim_types.h"
18#include "DNA_object_types.h"
19
20#include "BLI_listbase.hh"
21
22#include "testing/testing.h"
23
25
27 public:
31
32 void SetUp() override
33 {
35 action = BKE_id_new<Action>(bmain, "ACÄnimåtië");
36 action->id.us = 0; /* Nothing references this yet. */
38 cube->id.us = 0; /* Nothing references this yet. */
39 }
40
41 void TearDown() override
42 {
44 }
45};
46
47TEST_F(NLASlottedActionTest, assign_slot_to_nla_strip)
48{
49 ASSERT_EQ(action->id.us, 0);
50
51 AnimData *adt = BKE_animdata_ensure_id(&cube->id);
52 NlaTrack *track = BKE_nlatrack_new_tail(&adt->nla_tracks, false);
53
54 /* Create a strip. This automatically assigns the Action, but for now with the old flow. */
55 NlaStrip *strip = BKE_nlastrip_new(action, cube->id);
56 BKE_nlatrack_add_strip(track, strip, false);
57
59 EXPECT_STREQ(strip->last_slot_identifier, "");
60
61 /* Unassign the Action that was automatically assigned via BKE_nlastrip_new(). */
62 nla::unassign_action(*strip, cube->id);
63 EXPECT_EQ(strip->act, nullptr);
64 EXPECT_EQ(action->id.us, 0);
65
66 /* Assign an Action with a never-assigned slot. This should be picked automatically. */
67 Slot &virgin_slot = action->slot_add();
68
69 /* Assign the Action. */
70 EXPECT_TRUE(nla::assign_action(*strip, *action, cube->id));
71 EXPECT_EQ(strip->action_slot_handle, virgin_slot.handle);
72 EXPECT_STREQ(strip->last_slot_identifier, virgin_slot.identifier);
73 EXPECT_EQ(action->id.us, 1);
74 EXPECT_EQ(strip->act, action);
75 EXPECT_EQ(virgin_slot.idtype, GS(cube->id.name));
76
77 /* Unassign the Action. */
78 nla::unassign_action(*strip, cube->id);
79 EXPECT_EQ(strip->act, nullptr);
80 EXPECT_EQ(action->id.us, 0);
81
82 /* Create a slot for this ID, and make the NLA strip forget what slot it was assigned to before.
83 * Assigning the Action should now auto-pick the slot with the ID name. */
84 Slot &slot = action->slot_add_for_id(cube->id);
85 strip->last_slot_identifier[0] = '\0';
86 EXPECT_TRUE(nla::assign_action(*strip, *action, cube->id));
88 EXPECT_STREQ(strip->last_slot_identifier, slot.identifier);
89 EXPECT_EQ(action->id.us, 1);
90 EXPECT_EQ(strip->act, action);
91 EXPECT_TRUE(slot.runtime_users().contains(&cube->id));
92
93 /* Unassign the slot, but keep the Action assigned. */
96 EXPECT_STREQ(strip->last_slot_identifier, slot.identifier);
97 EXPECT_EQ(action->id.us, 1);
98 EXPECT_EQ(strip->act, action);
99 EXPECT_FALSE(slot.runtime_users().contains(&cube->id));
100
101 /* Unassign the Action, then reassign it. It should pick the same slot again. */
102 nla::unassign_action(*strip, cube->id);
103 EXPECT_TRUE(nla::assign_action(*strip, *action, cube->id));
104 EXPECT_EQ(strip->action_slot_handle, slot.handle);
105 EXPECT_TRUE(slot.runtime_users().contains(&cube->id));
106}
107
108TEST_F(NLASlottedActionTest, assign_slot_to_multiple_strips)
109{
110 AnimData *adt = BKE_animdata_ensure_id(&cube->id);
111 NlaTrack *track = BKE_nlatrack_new_tail(&adt->nla_tracks, false);
112
113 /* Create two strips. This automatically assigns the Action, but for now with
114 * the old flow (so no slots). */
115 NlaStrip *strip1 = BKE_nlastrip_new(action, cube->id);
116 strip1->start = 1;
117 strip1->end = 4;
118 NlaStrip *strip2 = BKE_nlastrip_new(action, cube->id);
119 strip1->start = 47;
120 strip1->end = 327;
121 ASSERT_TRUE(BKE_nlatrack_add_strip(track, strip1, false));
122 ASSERT_TRUE(BKE_nlatrack_add_strip(track, strip2, false));
123 ASSERT_EQ(1, adt->nla_tracks.count());
124 ASSERT_EQ(2, track->strips.count());
125
126 nla::unassign_action(*strip1, cube->id);
127 nla::unassign_action(*strip2, cube->id);
128
129 /* Create a virgin slot, it should be auto-picked. */
130 Slot &slot = action->slot_add();
131 EXPECT_TRUE(nla::assign_action(*strip1, *action, cube->id));
132 EXPECT_EQ(strip1->action_slot_handle, slot.handle);
133 EXPECT_STREQ(strip1->last_slot_identifier, slot.identifier);
134 EXPECT_EQ(slot.idtype, ID_OB);
135
136 /* Assign another slot 'manually'. */
137 Slot &other_slot = action->slot_add();
138 EXPECT_EQ(nla::assign_action_slot(*strip1, &other_slot, cube->id),
140 EXPECT_EQ(strip1->action_slot_handle, other_slot.handle);
141
142 /* Assign the Action + slot to the second strip. */
143 EXPECT_TRUE(nla::assign_action(*strip2, *action, cube->id));
145
146 /* The cube should be registered as user of the slot. */
147 EXPECT_TRUE(slot.runtime_users().contains(&cube->id));
148
149 nla::unassign_action(*strip1, cube->id);
150
151 /* The cube should still be registered as user of the slot, as there is a 2nd
152 * strip that references it. */
153 EXPECT_TRUE(slot.runtime_users().contains(&cube->id));
154
155 /* Remove the last use of this slot. */
156 nla::unassign_action(*strip2, cube->id);
157 EXPECT_FALSE(slot.runtime_users().contains(&cube->id));
158}
159
160} // namespace blender::animrig::nla::tests
Functions and classes to work with Actions.
Blender kernel action and pose functionality.
General operations, lookup, etc. for blender objects.
Object is a sort of wrapper for general info.
static constexpr slot_handle_t unassigned
#define EXPECT_FALSE(result)
#define EXPECT_TRUE(result)
TEST_F(NLASlottedActionTest, assign_slot_to_nla_strip)
void unassign_action(NlaStrip &strip, ID &animated_id)
bool assign_action(NlaStrip &strip, Action &action, ID &animated_id)
ActionSlotAssignmentResult assign_action_slot(NlaStrip &strip, Slot *slot_to_assign, ID &animated_id)
bool BKE_nlatrack_add_strip(NlaTrack *nlt, NlaStrip *strip, bool is_liboverride)
Main * BKE_main_new()
Definition main.cc:93
AnimData * BKE_animdata_ensure_id(ID *id)
Definition anim_data.cc:107
NlaStrip * BKE_nlastrip_new(bAction *act, ID &animated_id)
NlaTrack * BKE_nlatrack_new_tail(ListBaseT< NlaTrack > *nla_tracks, const bool is_liboverride)
void BKE_main_free(Main *bmain)
Definition main.cc:202
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
ID_Type GS(const char *name)
Definition DNA_ID.h:410
Object * BKE_object_add_only_object(Main *bmain, ObjectType type, const char *name) ATTR_RETURNS_NONNULL
Definition object.cc:2265
void * BKE_id_new(Main *bmain, short type, const char *name)
Definition lib_id.cc:1612
ListBaseT< NlaTrack > nla_tracks
char last_slot_identifier[258]
ListBaseT< NlaStrip > strips