Blender V5.3
blender.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include <cstdio>
12#include <cstdlib>
13#include <cstring>
14
16
17#include "MEM_guardedalloc.h"
18
19#include "BLI_listbase.hh"
20#include "BLI_string.hh"
21#include "BLI_string_utf8.hh"
22#include "BLI_utildefines.hh"
23
24#include "IMB_cache.hh"
25#include "IMB_imbuf.hh"
26
27#include "MOV_util.hh"
28
29#include "BKE_addon.h"
30#include "BKE_appdir.hh"
31#include "BKE_asset.hh"
32#include "BKE_blender.hh" /* own include */
33#include "BKE_blender_user_menu.hh" /* own include */
34#include "BKE_blender_version.h" /* own include */
35#include "BKE_brush.hh"
36#include "BKE_callbacks.hh"
37#include "BKE_global.hh"
38#include "BKE_idprop.hh"
39#include "BKE_main.hh"
40#include "BKE_node.hh"
41#include "BKE_preferences.h"
42#include "BKE_screen.hh"
43#include "BKE_studiolight.h"
44
45#include "DEG_depsgraph.hh"
46
47#include "RE_texture.h"
48
49#include "BLF_api.hh"
50
51#include "SEQ_utils.hh"
52
53#include "CLG_log.h"
54
55namespace blender {
56
59
60/* -------------------------------------------------------------------- */
63
65{
66 /* samples are in a global list..., also sets G_MAIN->sound->sample nullptr */
67
68 /* Needs to run before main free as window-manager is still referenced for icons preview jobs. */
70
72
73 if (G.log.file != nullptr) {
74 fclose(static_cast<FILE *>(G.log.file));
75 }
76
77 BKE_spacetypes_free(); /* after free main, it uses space callbacks */
78
79 IMB_exit();
81
84
86
89 MOV_exit();
90
92}
93
95
96/* -------------------------------------------------------------------- */
99
100static char blender_version_string[48] = "";
101
102/* Only includes patch if non-zero. */
104
106{
107 const char *version_cycle = "";
108 const char *version_cycle_compact = "";
109 if (STREQ(STRINGIFY(BLENDER_VERSION_CYCLE), "alpha")) {
110 version_cycle = " Alpha";
111 version_cycle_compact = " a";
112 }
113 else if (STREQ(STRINGIFY(BLENDER_VERSION_CYCLE), "beta")) {
114 version_cycle = " Beta";
115 version_cycle_compact = " b";
116 }
117 else if (STREQ(STRINGIFY(BLENDER_VERSION_CYCLE), "rc")) {
118 version_cycle = " Release Candidate";
119 version_cycle_compact = " RC";
120 }
121 else if (STREQ(STRINGIFY(BLENDER_VERSION_CYCLE), "release")) {
122 version_cycle = "";
123 version_cycle_compact = "";
124 }
125 else {
126 BLI_assert_msg(0, "Invalid Blender version cycle");
127 }
128
129 const char *version_suffix = BKE_blender_version_is_lts() ? " LTS" : "";
130
132 "%d.%01d.%d%s%s",
133 BLENDER_VERSION / 100,
134 BLENDER_VERSION % 100,
136 version_suffix,
137 version_cycle);
138
140 "%d.%01d.%d%s",
141 BLENDER_VERSION / 100,
142 BLENDER_VERSION % 100,
144 version_cycle_compact);
145}
146
148{
150}
151
156
158 const size_t str_buff_maxncpy,
159 const short file_version,
160 const short file_subversion)
161{
162 const short file_version_major = file_version / 100;
163 const short file_version_minor = file_version % 100;
164 if (file_subversion >= 0) {
165 BLI_snprintf_utf8(str_buff,
166 str_buff_maxncpy,
167 "%d.%d (sub %d)",
168 file_version_major,
169 file_version_minor,
170 file_subversion);
171 }
172 else {
173 BLI_snprintf_utf8(str_buff, str_buff_maxncpy, "%d.%d", file_version_major, file_version_minor);
174 }
175}
176
178{
179 bool is_alpha = STREQ(STRINGIFY(BLENDER_VERSION_CYCLE), "alpha");
180 return is_alpha;
181}
182
187
189
190/* -------------------------------------------------------------------- */
193
195{
197
198 G = Global{};
199
200 U.savetime = 1;
201
203
204 STRNCPY(G.filepath_last_image, "//");
205
206#ifndef WITH_PYTHON_SECURITY /* default */
208#else
210#endif
211
212 G.log.level = CLG_LEVEL_WARN;
213}
214
216{
217 if (G_MAIN == nullptr) {
218 return;
219 }
220 BLI_assert(G_MAIN->is_global_main);
221 BKE_main_free(G_MAIN); /* free all lib data */
222
223 G_MAIN = nullptr;
224}
225
227{
228 BLI_assert(!bmain->is_global_main);
230 bmain->is_global_main = true;
231 G_MAIN = bmain;
232}
233
235{
236 Main *old_gmain = G_MAIN;
237 BLI_assert(old_gmain->is_global_main);
238 BLI_assert(!new_gmain->is_global_main);
239 new_gmain->is_global_main = true;
240 G_MAIN = new_gmain;
241 old_gmain->is_global_main = false;
242 return old_gmain;
243}
244
246{
247 /* Might be called after WM/Main exit, so needs to be careful about nullptr-checking before
248 * de-referencing. */
249
250 if (!(G_MAIN && G_MAIN->filepath[0])) {
251 BLI_path_join(filepath, FILE_MAX, BKE_tempdir_base(), "blender.crash.txt");
252 }
253 else {
255 BLI_path_extension_replace(filepath, FILE_MAX, ".crash.txt");
256 }
257}
258
260
261/* -------------------------------------------------------------------- */
264
266{
267 if (kmi->properties) {
269 }
270 if (kmi->ptr) {
271 MEM_delete(kmi->ptr);
272 }
273}
274
276{
277 dna::shallow_swap(*userdef_a, *userdef_b);
278}
279
281{
283 BKE_blender_userdef_data_free(userdef, true);
284}
285
287{
289 MEM_delete(userdef);
290}
291
292static void userdef_free_keymaps(UserDef *userdef)
293{
294 for (wmKeyMap *km = userdef->user_keymaps.first(), *km_next; km; km = km_next) {
295 km_next = km->next;
296 for (wmKeyMapDiffItem &kmdi : km->diff_items) {
297 if (kmdi.add_item) {
299 MEM_delete(kmdi.add_item);
300 }
301 if (kmdi.remove_item) {
303 MEM_delete(kmdi.remove_item);
304 }
305 }
306
307 for (wmKeyMapItem &kmi : km->items) {
308 keymap_item_free(&kmi);
309 }
310
311 km->diff_items.free_no_destruct();
312 km->items.free_no_destruct();
313
314 MEM_delete(km);
315 }
316 userdef->user_keymaps.clear_no_delete();
317}
318
320{
321 for (wmKeyConfigPref *kpt = userdef->user_keyconfig_prefs.first(), *kpt_next; kpt;
322 kpt = kpt_next)
323 {
324 kpt_next = kpt->next;
325 IDP_FreeProperty(kpt->prop);
326 MEM_delete(kpt);
327 }
328 userdef->user_keyconfig_prefs.clear_no_delete();
329}
330
331static void userdef_free_user_menus(UserDef *userdef)
332{
333 for (bUserMenu *um = userdef->user_menus.first(), *um_next; um; um = um_next) {
334 um_next = um->next;
336 MEM_delete(um);
337 }
338}
339
340static void userdef_free_addons(UserDef *userdef)
341{
342 for (bAddon *addon = userdef->addons.first(), *addon_next; addon; addon = addon_next) {
343 addon_next = addon->next;
344 BKE_addon_free(addon);
345 }
346 userdef->addons.clear_no_delete();
347}
348
349void BKE_blender_userdef_data_free(UserDef *userdef, bool clear_fonts)
350{
351#define U BLI_STATIC_ASSERT(false, "Global 'U' not allowed, only use arguments passed in!")
352#ifdef U
353 /* Quiet warning. */
354#endif
355
356 userdef_free_keymaps(userdef);
359 userdef_free_addons(userdef);
360
361 if (clear_fonts) {
362 for (uiFont &font : userdef->uifonts) {
363 BLF_unload_id(font.blf_id);
364 }
365 BLF_default_set(-1);
366 }
367
368 userdef->autoexec_paths.free_no_destruct();
369 userdef->script_directories.free_no_destruct();
370
371 for (bUserAssetLibrary &library_ref : userdef->asset_libraries.items_mutable()) {
372 BKE_preferences_asset_library_remove(userdef, &library_ref);
373 }
374
375 for (bUserExtensionRepo &repo_ref : userdef->extension_repos.items_mutable()) {
376 MEM_SAFE_DELETE(repo_ref.access_token);
377 MEM_delete(&repo_ref);
378 }
379 userdef->extension_repos.clear_no_delete();
380
381 for (bUserAssetShelfSettings &settings : userdef->asset_shelves_settings.items_mutable()) {
382 BKE_asset_catalog_path_list_free(settings.enabled_catalog_paths);
383 MEM_delete(&settings);
384 }
385 userdef->asset_shelves_settings.clear_no_delete();
386
387 userdef->uistyles.free_no_destruct();
388 userdef->uifonts.free_no_destruct();
389 userdef->themes.free_no_destruct();
390
391#undef U
392}
393
395
396/* -------------------------------------------------------------------- */
399
401{
402 /* TODO:
403 * - various minor settings (add as needed).
404 */
405
406#define VALUE_SWAP(id) \
407 { \
408 std::swap(userdef_a->id, userdef_b->id); \
409 }
410
411#define DATA_SWAP(id) \
412 { \
413 UserDef userdef_tmp; \
414 memcpy(&(userdef_tmp.id), &(userdef_a->id), sizeof(userdef_tmp.id)); \
415 memcpy(&(userdef_a->id), &(userdef_b->id), sizeof(userdef_tmp.id)); \
416 memcpy(&(userdef_b->id), &(userdef_tmp.id), sizeof(userdef_tmp.id)); \
417 } \
418 ((void)0)
419
420#define FLAG_SWAP(id, ty, flags) \
421 { \
422 CHECK_TYPE(&(userdef_a->id), ty *); \
423 const ty f = flags; \
424 const ty a = userdef_a->id; \
425 const ty b = userdef_b->id; \
426 userdef_a->id = (userdef_a->id & ~f) | (b & f); \
427 userdef_b->id = (userdef_b->id & ~f) | (a & f); \
428 } \
429 ((void)0)
430
431 VALUE_SWAP(uistyles);
432 VALUE_SWAP(uifonts);
433 VALUE_SWAP(themes);
434 VALUE_SWAP(addons);
435 VALUE_SWAP(user_keymaps);
436 VALUE_SWAP(user_keyconfig_prefs);
437
438 DATA_SWAP(font_path_ui);
439 DATA_SWAP(font_path_ui_mono);
440 DATA_SWAP(keyconfigstr);
441
442 DATA_SWAP(gizmo_flag);
443 DATA_SWAP(app_flag);
444
445 /* We could add others. */
446 FLAG_SWAP(uiflag,
449
450 DATA_SWAP(ui_scale);
451
452#undef VALUE_SWAP
453#undef DATA_SWAP
454#undef FLAG_SWAP
455}
456
462
468
470
471/* -------------------------------------------------------------------- */
476
477static struct AtExitData {
479
480 void (*func)(void *user_data);
482} *g_atexit = nullptr;
483
484void BKE_blender_atexit_register(void (*func)(void *user_data), void *user_data)
485{
486 AtExitData *ae = static_cast<AtExitData *>(malloc(sizeof(*ae)));
487 ae->next = g_atexit;
488 ae->func = func;
489 ae->user_data = user_data;
490 g_atexit = ae;
491}
492
493void BKE_blender_atexit_unregister(void (*func)(void *user_data), const void *user_data)
494{
495 AtExitData *ae = g_atexit;
496 AtExitData **ae_p = &g_atexit;
497
498 while (ae) {
499 if ((ae->func == func) && (ae->user_data == user_data)) {
500 *ae_p = ae->next;
501 free(ae);
502 return;
503 }
504 ae_p = &ae->next;
505 ae = ae->next;
506 }
507}
508
510{
511 AtExitData *ae = g_atexit, *ae_next;
512 while (ae) {
513 ae_next = ae->next;
514
515 ae->func(ae->user_data);
516
517 free(ae);
518 ae = ae_next;
519 }
520 g_atexit = nullptr;
521}
522
524
525} // namespace blender
Blender util stuff.
#define BLENDER_VERSION_PATCH
#define BLENDER_VERSION_CYCLE
#define BLENDER_VERSION
#define BLENDER_VERSION_SUFFIX
#define G_MAIN
#define FILE_MAX
#define BLI_path_join(...)
#define SNPRINTF_UTF8(dst, format,...)
@ CLG_LEVEL_WARN
Definition CLG_log.h:58
Read Blender memory allocation and freeing.
#define VALUE_SWAP(id)
#define DATA_SWAP(id)
#define FLAG_SWAP(id, ty, flags)
#define STRINGIFY(x)
#define BLI_assert_msg(a, msg)
#define STREQ(a, b)
void free(Subdiv *subdiv)
Definition subdiv.cc:192
void node_system_exit()
Definition node.cc:6183
void shallow_swap(T &a, T &b)
Definition DNA_defs.h:164
void fontmap_clear()
BMesh Iterators.
void BKE_blender_free()
Definition blender.cc:64
UserDef U
Definition blender.cc:58
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
size_t size_t size_t BLI_snprintf_utf8(char *__restrict dst, size_t dst_maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
void BKE_blender_userdef_data_set_and_free(UserDef *userdef)
Definition blender.cc:286
void DEG_free_node_types()
void BKE_asset_catalog_path_list_free(ListBaseT< AssetCatalogPathLink > &catalog_path_list)
char * STRNCPY(char(&dst)[N], const char *src)
void BKE_blender_globals_init()
Definition blender.cc:194
static char blender_version_string[48]
Definition blender.cc:100
void BKE_blender_userdef_data_free(UserDef *userdef, bool clear_fonts)
Definition blender.cc:349
void BLF_default_set(int fontid)
void BKE_addon_free(struct bAddon *addon)
Definition addon.cc:70
Main * BKE_main_new()
Definition main.cc:93
void BKE_blender_atexit_unregister(void(*func)(void *user_data), const void *user_data)
Definition blender.cc:493
void BKE_studiolight_free()
void BKE_preferences_asset_library_remove(struct UserDef *userdef, struct bUserAssetLibrary *library) ATTR_NONNULL()
bool BKE_blender_version_is_alpha()
Definition blender.cc:177
void BKE_blender_userdef_app_template_data_set_and_free(UserDef *userdef)
Definition blender.cc:463
static void userdef_free_keymaps(UserDef *userdef)
Definition blender.cc:292
void RE_texture_rng_exit()
void MOV_exit()
Global G
Definition blender.cc:57
void void void const char * BLI_path_basename(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
static void keymap_item_free(wmKeyMapItem *kmi)
Definition blender.cc:265
void BKE_blender_userdef_app_template_data_swap(UserDef *userdef_a, UserDef *userdef_b)
Definition blender.cc:400
void BKE_blender_atexit()
Definition blender.cc:509
const char * BKE_tempdir_base() ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL
Definition appdir.cc:1288
bool BLI_path_extension_replace(char *path, size_t path_maxncpy, const char *ext) ATTR_NONNULL(1
void BKE_blender_user_menu_item_free_list(ListBaseT< bUserMenuItem > *lb)
static char blender_version_string_compact[48]
Definition blender.cc:103
void BKE_main_free(Main *bmain)
Definition main.cc:202
static void userdef_free_user_menus(UserDef *userdef)
Definition blender.cc:331
void BKE_callback_global_finalize()
Definition callbacks.cc:120
static void userdef_free_addons(UserDef *userdef)
Definition blender.cc:340
void BKE_brush_system_exit()
Definition brush.cc:631
@ G_FLAG_SCRIPT_AUTOEXEC
void IDP_FreeProperty(IDProperty *prop)
Definition idprop.cc:1403
static struct blender::AtExitData * g_atexit
void BKE_blender_globals_clear()
Definition blender.cc:215
void IMB_cache_destruct()
Definition cache.cc:262
static void blender_version_init()
Definition blender.cc:105
const char * BKE_blender_version_string_compact()
Definition blender.cc:152
void BKE_blender_userdef_data_set(UserDef *userdef)
Definition blender.cc:280
void BKE_blender_userdef_data_swap(UserDef *userdef_a, UserDef *userdef_b)
Definition blender.cc:275
bool BKE_blender_version_is_lts()
Definition blender.cc:183
static void userdef_free_keyconfig_prefs(UserDef *userdef)
Definition blender.cc:319
void BKE_blender_userdef_app_template_data_set(UserDef *userdef)
Definition blender.cc:457
void BKE_spacetypes_free()
Definition screen.cc:349
void BKE_blender_version_blendfile_string_from_values(char *str_buff, const size_t str_buff_maxncpy, const short file_version, const short file_subversion)
Definition blender.cc:157
const char * BKE_blender_version_string()
Definition blender.cc:147
void BKE_blender_globals_main_replace(Main *bmain)
Definition blender.cc:226
bool BLF_unload_id(int fontid)
Definition blf.cc:282
Main * BKE_blender_globals_main_swap(Main *new_gmain)
Definition blender.cc:234
void BKE_blender_globals_crash_path_get(char *filepath)
void BKE_blender_atexit_register(void(*func)(void *user_data), void *user_data)
Definition blender.cc:484
void IMB_exit()
Definition module.cc:32
AtExitData * next
Definition blender.cc:478
void(*) func(void *user_data)
Definition blender.cc:480
bool is_global_main
Definition BKE_main.hh:328
ListBaseT< bUserExtensionRepo > extension_repos
ListBaseT< bTheme > themes
ListBaseT< struct wmKeyMap > user_keymaps
ListBaseT< struct wmKeyConfigPref > user_keyconfig_prefs
ListBaseT< bUserAssetLibrary > asset_libraries
ListBaseT< bAddon > addons
ListBaseT< bPathCompare > autoexec_paths
ListBaseT< bUserScriptDirectory > script_directories
ListBaseT< uiStyle > uistyles
ListBaseT< bUserAssetShelfSettings > asset_shelves_settings
ListBaseT< uiFont > uifonts
ListBaseT< bUserMenu > user_menus
short blf_id