Blender  V2.93
blendfile_loading_base_test.cc
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2019 by Blender Foundation.
17  */
19 
20 #include "MEM_guardedalloc.h"
21 
22 #include "BKE_appdir.h"
23 #include "BKE_blender.h"
24 #include "BKE_context.h"
25 #include "BKE_global.h"
26 #include "BKE_idtype.h"
27 #include "BKE_image.h"
28 #include "BKE_main.h"
29 #include "BKE_modifier.h"
30 #include "BKE_node.h"
31 #include "BKE_scene.h"
32 
33 #include "BLI_path_util.h"
34 #include "BLI_threads.h"
35 
36 #include "BLO_readfile.h"
37 
38 #include "DEG_depsgraph.h"
39 #include "DEG_depsgraph_build.h"
40 
41 #include "DNA_genfile.h" /* for DNA_sdna_current_init() */
43 
44 #include "IMB_imbuf.h"
45 
46 #include "RNA_define.h"
47 
48 #include "WM_api.h"
49 #include "wm.h"
50 
51 #include "CLG_log.h"
52 
54 {
55  testing::Test::SetUpTestCase();
56 
57  /* Minimal code to make loading a blendfile and constructing a depsgraph not crash, copied from
58  * main() in creator.c. */
59  CLG_init();
61 
64 
67  IMB_init();
71  RNA_init();
73 
74  G.background = true;
75  G.factory_startup = true;
76 
77  /* Allocate a dummy window manager. The real window manager will try and load Python scripts from
78  * the release directory, which it won't be able to find. */
79  ASSERT_EQ(G.main->wm.first, nullptr);
80  G.main->wm.first = MEM_callocN(sizeof(wmWindowManager), __func__);
81 }
82 
84 {
85  if (G.main->wm.first != nullptr) {
86  MEM_freeN(G.main->wm.first);
87  G.main->wm.first = nullptr;
88  }
89 
90  /* Copied from WM_exit_ex() in wm_init_exit.c, and cherry-picked those lines that match the
91  * allocation/initialization done in SetUpTestCase(). */
93  RNA_exit();
94 
98 
100 
102  BKE_appdir_exit();
103  CLG_exit();
104 
105  testing::Test::TearDownTestCase();
106 }
107 
109 {
110  depsgraph_free();
111  blendfile_free();
112 
113  testing::Test::TearDown();
114 }
115 
117 {
118  const std::string &test_assets_dir = blender::tests::flags_test_asset_dir();
119  if (test_assets_dir.empty()) {
120  return false;
121  }
122 
123  char abspath[FILENAME_MAX];
124  BLI_path_join(abspath, sizeof(abspath), test_assets_dir.c_str(), filepath, NULL);
125 
126  bfile = BLO_read_from_file(abspath, BLO_READ_SKIP_NONE, nullptr /* reports */);
127  if (bfile == nullptr) {
128  ADD_FAILURE() << "Unable to load file '" << filepath << "' from test assets dir '"
129  << test_assets_dir << "'";
130  return false;
131  }
132  return true;
133 }
134 
136 {
137  if (bfile == nullptr) {
138  return;
139  }
140 
141  wmWindowManager *wm = static_cast<wmWindowManager *>(bfile->main->wm.first);
142  if (wm != nullptr) {
143  wm_close_and_free(nullptr, wm);
144  }
146  bfile = nullptr;
147 }
148 
150 {
152  bfile->main, bfile->curscene, bfile->cur_view_layer, depsgraph_evaluation_mode);
155 }
156 
158 {
159  if (depsgraph == nullptr) {
160  return;
161  }
163  depsgraph = nullptr;
164 }
void BKE_appdir_init(void)
Definition: appdir.c:111
void BKE_appdir_exit(void)
Definition: appdir.c:119
void BKE_tempdir_session_purge(void)
Definition: appdir.c:1190
Blender util stuff.
void BKE_blender_atexit(void)
Definition: blender.c:425
void BKE_blender_globals_init(void)
Definition: blender.c:159
void BKE_blender_free(void)
Definition: blender.c:75
void BKE_idtype_init(void)
Definition: idtype.c:125
void BKE_images_init(void)
Definition: image.c:431
void BKE_modifier_init(void)
void BKE_node_system_init(void)
Definition: node.cc:5003
void BKE_scene_graph_update_tagged(struct Depsgraph *depsgraph, struct Main *bmain)
Definition: scene.c:2713
size_t BLI_path_join(char *__restrict dst, const size_t dst_len, const char *path_first,...) ATTR_NONNULL(1
void BLI_threadapi_init(void)
Definition: threads.cc:143
void BLI_threadapi_exit(void)
Definition: threads.cc:151
external readfile function prototypes.
@ BLO_READ_SKIP_NONE
Definition: BLO_readfile.h:94
BlendFileData * BLO_read_from_file(const char *filepath, eBLOReadSkip skip_flags, struct ReportList *reports)
void BLO_blendfiledata_free(BlendFileData *bfd)
void CLG_exit(void)
Definition: clog.c:715
void CLG_init(void)
Definition: clog.c:708
Depsgraph * DEG_graph_new(struct Main *bmain, struct Scene *scene, struct ViewLayer *view_layer, eEvaluationMode mode)
Definition: depsgraph.cc:281
eEvaluationMode
Definition: DEG_depsgraph.h:60
void DEG_free_node_types(void)
void DEG_graph_free(Depsgraph *graph)
Definition: depsgraph.cc:314
void DEG_register_node_types(void)
void DEG_graph_build_from_view_layer(struct Depsgraph *graph)
blenloader genfile private function prototypes
void DNA_sdna_current_free(void)
Definition: dna_genfile.c:603
void DNA_sdna_current_init(void)
Definition: dna_genfile.c:592
void IMB_init(void)
Definition: module.c:30
Read Guarded memory(de)allocation.
virtual void depsgraph_create(eEvaluationMode depsgraph_evaluation_mode)
bool blendfile_load(const char *filepath)
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
void RNA_init(void)
Definition: rna_access.c:75
void RNA_exit(void)
Definition: rna_access.c:99
struct Scene * curscene
Definition: BLO_readfile.h:78
struct ViewLayer * cur_view_layer
Definition: BLO_readfile.h:79
struct Main * main
Definition: BLO_readfile.h:70
void * first
Definition: DNA_listBase.h:47
ListBase wm
Definition: BKE_main.h:175
#define G(x, y, z)
void wm_close_and_free(bContext *C, wmWindowManager *wm)
Definition: wm.c:571