Blender V5.3
scene_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BKE_anim_data.hh"
6#include "BKE_gtest_base.hh"
7#include "BKE_idprop.hh"
8#include "BKE_lib_id.hh"
9#include "BKE_main.hh"
10#include "BKE_scene.hh"
11
12#include "DNA_defs.h"
13
14#include "DNA_action_types.h"
15#include "DNA_anim_types.h"
16#include "DNA_scene_types.h"
17#include "DNA_userdef_types.h"
18
19#include "testing/testing.h"
20
21namespace blender::bke::tests {
22
23class SceneTest : public ::testing::Test {
24 public:
26
27 static void SetUpTestSuite()
28 {
30 /* #BKE_scene_duplicate() uses #U::dupflag. */
32 }
33
34 static void TearDownTestSuite()
35 {
37 }
38
39 void SetUp() override
40 {
42 }
43
44 void TearDown() override
45 {
47 }
48};
49
50TEST_F(SceneTest, frame_snap_by_seconds)
51{
52 Scene fake_scene = {};
53
54 /* Regular 24 FPS snapping. */
55 fake_scene.r.frs_sec = 24;
56 fake_scene.r.frs_sec_base = 1.0;
57 EXPECT_FLOAT_EQ(48.0, BKE_scene_frame_snap_by_seconds(&fake_scene, 1.0, 47));
58 EXPECT_FLOAT_EQ(48.0, BKE_scene_frame_snap_by_seconds(&fake_scene, 1.0, 49));
59 EXPECT_FLOAT_EQ(48.0, BKE_scene_frame_snap_by_seconds(&fake_scene, 1.0, 59));
60 EXPECT_FLOAT_EQ(72.0, BKE_scene_frame_snap_by_seconds(&fake_scene, 1.0, 60));
61 EXPECT_FLOAT_EQ(9984.0, BKE_scene_frame_snap_by_seconds(&fake_scene, 2.0, 10000.0));
62
63 /* 12 FPS snapping by incrementing the base. */
64 fake_scene.r.frs_sec = 24;
65 fake_scene.r.frs_sec_base = 2.0;
66 EXPECT_FLOAT_EQ(48.0, BKE_scene_frame_snap_by_seconds(&fake_scene, 1.0, 47));
67 EXPECT_FLOAT_EQ(48.0, BKE_scene_frame_snap_by_seconds(&fake_scene, 1.0, 49));
68 EXPECT_FLOAT_EQ(48.0, BKE_scene_frame_snap_by_seconds(&fake_scene, 1.0, 53));
69 EXPECT_FLOAT_EQ(60.0, BKE_scene_frame_snap_by_seconds(&fake_scene, 1.0, 54));
70 EXPECT_FLOAT_EQ(9996.0, BKE_scene_frame_snap_by_seconds(&fake_scene, 1.0, 10000.0));
71
72 /* 0.1 FPS snapping to 2-second intervals. */
73 fake_scene.r.frs_sec = 1;
74 fake_scene.r.frs_sec_base = 10.0;
75 EXPECT_FLOAT_EQ(48.0, BKE_scene_frame_snap_by_seconds(&fake_scene, 2.0, 48.0));
76 EXPECT_FLOAT_EQ(48.0, BKE_scene_frame_snap_by_seconds(&fake_scene, 2.0, 48.1));
77 EXPECT_FLOAT_EQ(48.2, BKE_scene_frame_snap_by_seconds(&fake_scene, 2.0, 48.2));
78 EXPECT_FLOAT_EQ(10000.0, BKE_scene_frame_snap_by_seconds(&fake_scene, 2.0, 10000.0));
79}
80
81TEST_F(SceneTest, linked_copy_id_remapping)
82{
83 Scene *scene_src = BKE_id_new<Scene>(bmain, "Scene_source");
84 bAction *action_src = BKE_id_new<bAction>(bmain, "Scene_source_action");
85 BKE_animdata_set_action(nullptr, &scene_src->id, action_src);
86 AnimData *animdata_src = BKE_animdata_from_id(&scene_src->id);
87 ASSERT_NE(animdata_src, nullptr);
88 EXPECT_EQ(animdata_src->action, action_src);
89
90 constexpr StringRef idp_scene2scene_name = "scene2scene";
91 constexpr StringRef idp_scene2action_name = "scene2action";
92 constexpr StringRef idp_action2scene_name = "action2scene";
93 constexpr StringRef idp_action2action_name = "action2action";
94
95 IDProperty *scene_idgroup_src = IDP_EnsureProperties(&scene_src->id);
96 IDP_AddToGroup(scene_idgroup_src,
97 bke::idprop::create(idp_scene2scene_name, &scene_src->id).release());
98 IDP_AddToGroup(scene_idgroup_src,
99 bke::idprop::create(idp_scene2action_name, &action_src->id).release());
100
101 IDProperty *action_idgroup_src = IDP_EnsureProperties(&action_src->id);
102 IDP_AddToGroup(action_idgroup_src,
103 bke::idprop::create(idp_action2scene_name, &scene_src->id).release());
104 IDP_AddToGroup(action_idgroup_src,
105 bke::idprop::create(idp_action2action_name, &action_src->id).release());
106
107 Scene *scene_copy = BKE_scene_duplicate(
108 bmain,
109 scene_src,
111 static_cast<eDupli_ID_Flags>(U.dupflag | USER_DUP_OBJECT),
113
114 /* Source data should remain unchanged. */
115
116 EXPECT_EQ(IDP_ID_get(IDP_GetPropertyFromGroup(scene_idgroup_src, idp_scene2scene_name)),
117 &scene_src->id);
118 EXPECT_EQ(IDP_ID_get(IDP_GetPropertyFromGroup(scene_idgroup_src, idp_scene2action_name)),
119 &action_src->id);
120
121 EXPECT_EQ(IDP_ID_get(IDP_GetPropertyFromGroup(action_idgroup_src, idp_action2scene_name)),
122 &scene_src->id);
123 EXPECT_EQ(IDP_ID_get(IDP_GetPropertyFromGroup(action_idgroup_src, idp_action2action_name)),
124 &action_src->id);
125
126 /* Copied data should have its ID usages remapped to new copies if possible. */
127
128 EXPECT_NE(scene_copy, scene_src);
129 AnimData *animdata_copy = BKE_animdata_from_id(&scene_copy->id);
130 ASSERT_NE(animdata_copy, nullptr);
131 EXPECT_NE(animdata_copy, animdata_src);
132 bAction *action_copy = animdata_copy->action;
133 ASSERT_NE(action_copy, nullptr);
134 EXPECT_NE(action_copy, action_src);
135
136 IDProperty *scene_idgroup_copy = IDP_GetProperties(&scene_copy->id);
137 ASSERT_NE(scene_idgroup_copy, nullptr);
138 EXPECT_EQ(IDP_ID_get(IDP_GetPropertyFromGroup(scene_idgroup_copy, idp_scene2scene_name)),
139 &scene_copy->id);
140 EXPECT_EQ(IDP_ID_get(IDP_GetPropertyFromGroup(scene_idgroup_copy, idp_scene2action_name)),
141 &action_copy->id);
142
143 IDProperty *action_idgroup_copy = IDP_GetProperties(&action_copy->id);
144 ASSERT_NE(action_idgroup_copy, nullptr);
145 EXPECT_EQ(IDP_ID_get(IDP_GetPropertyFromGroup(action_idgroup_copy, idp_action2scene_name)),
146 &scene_copy->id);
147 EXPECT_EQ(IDP_ID_get(IDP_GetPropertyFromGroup(action_idgroup_copy, idp_action2action_name)),
148 &action_copy->id);
149}
150
151} // namespace blender::bke::tests
#define IDP_ID_get(prop)
#define EXPECT_NE(result, expect)
std::unique_ptr< IDProperty, IDPropertyDeleter > create(StringRef prop_name, int32_t value, eIDPropertyFlag flags={})
Allocate a new IDProperty of type IDP_INT, set its name and value.
TEST_F(AnimDataTest, BKE_fcurves_id_cb_test)
void gtest_teardown()
void gtest_setup()
internal::ShallowDataConstRef< T > shallow_copy(const T &other)
Definition DNA_defs.h:147
UserDef U
Definition blender.cc:58
bool IDP_AddToGroup(IDProperty *group, IDProperty *prop) ATTR_NONNULL()
Definition idprop.cc:788
IDProperty * IDP_GetPropertyFromGroup(const IDProperty *prop, StringRef name) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition idprop.cc:818
Main * BKE_main_new()
Definition main.cc:93
@ SCE_COPY_LINK_COLLECTION
Definition BKE_scene.hh:38
@ LIB_ID_DUPLICATE_IS_ROOT_ID
bool BKE_animdata_set_action(ReportList *reports, ID *id, bAction *act)
Definition anim_data.cc:135
AnimData * BKE_animdata_from_id(const ID *id)
Definition anim_data.cc:93
IDProperty * IDP_GetProperties(ID *id) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition idprop.cc:1000
void BKE_main_free(Main *bmain)
Definition main.cc:202
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
float BKE_scene_frame_snap_by_seconds(const Scene *scene, double interval_in_seconds, float frame)
Definition scene.cc:2532
IDProperty * IDP_EnsureProperties(ID *id) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition idprop.cc:1005
void * BKE_id_new(Main *bmain, short type, const char *name)
Definition lib_id.cc:1612
Scene * BKE_scene_duplicate(Main *bmain, Scene *sce, eSceneCopyMethod type, eDupli_ID_Flags duplicate_flags, uint duplicate_options)
Definition scene.cc:1917
struct RenderData r