Blender V4.3
blendfile.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
12#include <cstdlib>
13#include <cstring>
14
15#include "CLG_log.h"
16
17#include "MEM_guardedalloc.h"
18
19#include "DNA_brush_types.h"
20#include "DNA_material_types.h"
21#include "DNA_scene_types.h"
22#include "DNA_screen_types.h"
23#include "DNA_space_types.h"
24#include "DNA_workspace_types.h"
25
26#include "BLI_fileops.h"
27#include "BLI_function_ref.hh"
28#include "BLI_listbase.h"
29#include "BLI_path_utils.hh"
30#include "BLI_string.h"
31#include "BLI_system.h"
32#include "BLI_time.h"
33#include "BLI_utildefines.h"
34#include "BLI_vector_set.hh"
35
36#include "BLT_translation.hh"
37
39
40#include "BKE_addon.h"
41#include "BKE_appdir.hh"
42#include "BKE_blender.hh"
43#include "BKE_blender_version.h"
44#include "BKE_blendfile.hh"
45#include "BKE_bpath.hh"
46#include "BKE_colorband.hh"
47#include "BKE_context.hh"
48#include "BKE_global.hh"
49#include "BKE_idtype.hh"
50#include "BKE_layer.hh"
51#include "BKE_lib_id.hh"
52#include "BKE_lib_override.hh"
53#include "BKE_lib_query.hh"
54#include "BKE_lib_remap.hh"
55#include "BKE_main.hh"
56#include "BKE_main_idmap.hh"
57#include "BKE_main_namemap.hh"
58#include "BKE_preferences.h"
59#include "BKE_report.hh"
60#include "BKE_scene.hh"
61#include "BKE_screen.hh"
62#include "BKE_studiolight.h"
63#include "BKE_undo_system.hh"
64#include "BKE_workspace.hh"
65
66#include "BLO_read_write.hh"
67#include "BLO_readfile.hh"
68#include "BLO_userdef_default.h"
69#include "BLO_writefile.hh"
70
71#include "RE_pipeline.h"
72
73#ifdef WITH_PYTHON
74# include "BPY_extern.hh"
75#endif
76
77using namespace blender::bke;
78
79/* -------------------------------------------------------------------- */
84{
85 const char *ext_test[4] = {".blend", ".ble", ".blend.gz", nullptr};
86 return BLI_path_extension_check_array(str, ext_test);
87}
88
90 char *r_dir,
91 char **r_group,
92 char **r_name)
93{
94 /* We might get some data names with slashes,
95 * so we have to go up in path until we find blend file itself,
96 * then we know next path item is group, and everything else is data name. */
97 char *slash = nullptr, *prev_slash = nullptr, c = '\0';
98
99 r_dir[0] = '\0';
100 if (r_group) {
101 *r_group = nullptr;
102 }
103 if (r_name) {
104 *r_name = nullptr;
105 }
106
107 /* if path leads to an existing directory, we can be sure we're not (in) a library */
108 if (BLI_is_dir(path)) {
109 return false;
110 }
111
112 BLI_strncpy(r_dir, path, FILE_MAX_LIBEXTRA);
113
114 while ((slash = (char *)BLI_path_slash_rfind(r_dir))) {
115 char tc = *slash;
116 *slash = '\0';
117 if (BKE_blendfile_extension_check(r_dir) && BLI_is_file(r_dir)) {
118 break;
119 }
120 if (STREQ(r_dir, BLO_EMBEDDED_STARTUP_BLEND)) {
121 break;
122 }
123
124 if (prev_slash) {
125 *prev_slash = c;
126 }
127 prev_slash = slash;
128 c = tc;
129 }
130
131 if (!slash) {
132 return false;
133 }
134
135 if (slash[1] != '\0') {
136 BLI_assert(strlen(slash + 1) < BLO_GROUP_MAX);
137 if (r_group) {
138 *r_group = slash + 1;
139 }
140 }
141
142 if (prev_slash && (prev_slash[1] != '\0')) {
143 BLI_assert(strlen(prev_slash + 1) < MAX_ID_NAME - 2);
144 if (r_name) {
145 *r_name = prev_slash + 1;
146 }
147 }
148
149 return true;
150}
151
152bool BKE_blendfile_is_readable(const char *path, ReportList *reports)
153{
154 BlendFileReadReport readfile_reports;
155 readfile_reports.reports = reports;
156 BlendHandle *bh = BLO_blendhandle_from_file(path, &readfile_reports);
157 if (bh != nullptr) {
159 return true;
160 }
161 return false;
162}
163
166/* -------------------------------------------------------------------- */
170static bool foreach_path_clean_cb(BPathForeachPathData * /*bpath_data*/,
171 char *path_dst,
172 size_t path_dst_maxncpy,
173 const char *path_src)
174{
175 BLI_strncpy(path_dst, path_src, path_dst_maxncpy);
176 BLI_path_slash_native(path_dst);
177 return !STREQ(path_dst, path_src);
178}
179
180/* make sure path names are correct for OS */
181static void clean_paths(Main *bmain)
182{
183 BPathForeachPathData foreach_path_data{};
184 foreach_path_data.bmain = bmain;
185 foreach_path_data.callback_function = foreach_path_clean_cb;
186 foreach_path_data.flag = BKE_BPATH_FOREACH_PATH_SKIP_MULTIFILE;
187 foreach_path_data.user_data = nullptr;
188
189 BKE_bpath_foreach_path_main(&foreach_path_data);
190
191 LISTBASE_FOREACH (Scene *, scene, &bmain->scenes) {
192 BLI_path_slash_native(scene->r.pic);
193 }
194}
195
197{
198 LISTBASE_FOREACH (wmWindow *, win, &wm->windows) {
199 if (win->scene == scene) {
200 return true;
201 }
202 }
203 return false;
204}
205
207{
208 if (bfd->user) {
209 /* only here free userdef themes... */
211 bfd->user = nullptr;
212
213 /* Security issue: any blend file could include a #BLO_CODE_USER block.
214 *
215 * Preferences are loaded from #BLENDER_STARTUP_FILE and later on load #BLENDER_USERPREF_FILE,
216 * to load the preferences defined in the users home directory.
217 *
218 * This means we will never accidentally (or maliciously)
219 * enable scripts auto-execution by loading a `.blend` file. */
221 }
222}
223
250
262{
263 if (reuse_data->is_libraries_remapped) {
264 return *reuse_data->remapper;
265 }
266
267 if (reuse_data->remapper == nullptr) {
268 reuse_data->remapper = MEM_new<id::IDRemapper>(__func__);
269 }
270
271 Main *new_bmain = reuse_data->new_bmain;
272 Main *old_bmain = reuse_data->old_bmain;
273 id::IDRemapper &remapper = *reuse_data->remapper;
274
275 LISTBASE_FOREACH (Library *, old_lib_iter, &old_bmain->libraries) {
276 /* In case newly opened `new_bmain` is a library of the `old_bmain`, remap it to null, since a
277 * file should never ever have linked data from itself. */
278 if (STREQ(old_lib_iter->runtime.filepath_abs, new_bmain->filepath)) {
279 remapper.add(&old_lib_iter->id, nullptr);
280 continue;
281 }
282
283 /* NOTE: Although this is quadratic complexity, it is not expected to be an issue in practice:
284 * - Files using more than a few tens of libraries are extremely rare.
285 * - This code is only executed once for every file reading (not on undos).
286 */
287 LISTBASE_FOREACH (Library *, new_lib_iter, &new_bmain->libraries) {
288 if (!STREQ(old_lib_iter->runtime.filepath_abs, new_lib_iter->runtime.filepath_abs)) {
289 continue;
290 }
291
292 remapper.add(&old_lib_iter->id, &new_lib_iter->id);
293 break;
294 }
295 }
296
297 reuse_data->is_libraries_remapped = true;
298 return *reuse_data->remapper;
299}
300
302{
305 /* ID is already remapped to its matching ID in the new main, or explicitly remapped to null,
306 * nothing else to do here. */
307 return true;
308 }
310 "There should never be a non-mappable (i.e. null) input here.");
312 return false;
313}
314
316 ID *id,
317 Library *lib,
318 const bool reuse_existing)
319{
320 id::IDRemapper &remapper = reuse_bmain_data_remapper_ensure(reuse_data);
321 Main *new_bmain = reuse_data->new_bmain;
322 Main *old_bmain = reuse_data->old_bmain;
323 ListBase *new_lb = which_libbase(new_bmain, GS(id->name));
324 ListBase *old_lb = which_libbase(old_bmain, GS(id->name));
325
326 if (reuse_existing) {
327 /* A 'new' version of the same data may already exist in new_bmain, in the rare case
328 * that the same asset blend file was linked explicitly into the blend file we are loading.
329 * Don't move the old linked ID, but remap its usages to the new one instead. */
330 LISTBASE_FOREACH_BACKWARD (ID *, id_iter, new_lb) {
331 if (!ELEM(id_iter->lib, id->lib, lib)) {
332 continue;
333 }
334 if (!STREQ(id_iter->name + 2, id->name + 2)) {
335 continue;
336 }
337
338 remapper.add(id, id_iter);
339 return false;
340 }
341 }
342
343 /* If ID is already in the new_bmain, this should not have been called. */
344 BLI_assert(BLI_findindex(new_lb, id) < 0);
345 BLI_assert(BLI_findindex(old_lb, id) >= 0);
346
347 /* Move from one list to another, and ensure name is valid. */
348 BLI_remlink_safe(old_lb, id);
349 BKE_main_namemap_remove_name(old_bmain, id, id->name + 2);
350
351 id->lib = lib;
352 BLI_addtail(new_lb, id);
354 *new_bmain, *new_lb, *id, nullptr, IDNewNameMode::RenameExistingNever, true);
356
357 /* Remap to itself, to avoid re-processing this ID again. */
358 remapper.add(id, id);
359 return true;
360}
361
363 Library *old_lib)
364{
365 const id::IDRemapper &remapper = reuse_bmain_data_remapper_ensure(reuse_data);
366 Library *new_lib = old_lib;
367 IDRemapperApplyResult result = remapper.apply(reinterpret_cast<ID **>(&new_lib),
369
370 switch (result) {
372 /* Move library to new bmain.
373 * There should be no filepath conflicts, as #reuse_bmain_data_remapper_ensure has
374 * already remapped existing libraries with matching filepath. */
375 reuse_bmain_move_id(reuse_data, &old_lib->id, nullptr, false);
376 return old_lib;
377 }
380 return nullptr;
381 }
383 /* Already in new bmain, only transfer flags. */
384 new_lib->runtime.tag |= old_lib->runtime.tag &
386 return new_lib;
387 }
389 /* Happens when the library is the newly opened blend file. */
390 return nullptr;
391 }
392 }
393
395 return nullptr;
396}
397
400{
401 ID *id = *cb_data->id_pointer;
402
403 if (id == nullptr) {
404 return IDWALK_RET_NOP;
405 }
406
407 if (GS(id->name) == ID_LI) {
408 /* Libraries are handled separately. */
410 }
411
412 ReuseOldBMainData *reuse_data = static_cast<ReuseOldBMainData *>(cb_data->user_data);
413
414 /* First check if it has already been remapped. */
415 id::IDRemapper &remapper = reuse_bmain_data_remapper_ensure(reuse_data);
418 }
419
420 if (id->lib == nullptr) {
421 /* There should be no links to local datablocks from linked editable data. */
422 remapper.add(id, nullptr);
425 }
426
427 /* Only preserve specific datablock types. */
428 if (!ID_TYPE_SUPPORTS_ASSET_EDITABLE(GS(id->name))) {
429 remapper.add(id, nullptr);
431 }
432
433 /* There may be a new library pointer in new_bmain, matching a library in old_bmain, even
434 * though pointer values are not the same. So we need to check new linked IDs in new_bmain
435 * against both potential library pointers. */
436 Library *old_id_new_lib = reuse_bmain_data_dependencies_new_library_get(reuse_data, id->lib);
437
438 /* Happens when the library is the newly opened blend file. */
439 if (old_id_new_lib == nullptr) {
440 remapper.add(id, nullptr);
442 }
443
444 /* Move to new main database. */
445 return reuse_bmain_move_id(reuse_data, id, old_id_new_lib, true) ? IDWALK_RET_STOP_RECURSION :
447}
448
450{
451 Main *old_bmain = reuse_data->old_bmain;
452 LISTBASE_FOREACH (Library *, lib, &old_bmain->libraries) {
453 if (lib->runtime.tag & LIBRARY_ASSET_EDITABLE) {
454 return true;
455 }
456 }
457 return false;
458}
459
474 const short idcode)
475{
476 Main *new_bmain = reuse_data->new_bmain;
477 Main *old_bmain = reuse_data->old_bmain;
478
479 id::IDRemapper &remapper = reuse_bmain_data_remapper_ensure(reuse_data);
480
481 ListBase *old_lb = which_libbase(old_bmain, idcode);
482 ID *old_id_iter;
483
484 FOREACH_MAIN_LISTBASE_ID_BEGIN (old_lb, old_id_iter) {
485 /* Keep any datablocks from libraries marked as LIBRARY_ASSET_EDITABLE. */
486 if (!(ID_IS_LINKED(old_id_iter) && old_id_iter->lib->runtime.tag & LIBRARY_ASSET_EDITABLE)) {
487 continue;
488 }
489
490 Library *old_id_new_lib = reuse_bmain_data_dependencies_new_library_get(reuse_data,
491 old_id_iter->lib);
492
493 /* Happens when the library is the newly opened blend file. */
494 if (old_id_new_lib == nullptr) {
495 remapper.add(old_id_iter, nullptr);
496 continue;
497 }
498
499 if (reuse_bmain_move_id(reuse_data, old_id_iter, old_id_new_lib, true)) {
500 /* Port over dependencies of re-used ID, unless matching already existing ones in
501 * new_bmain can be found.
502 *
503 * NOTE : No pointers are remapped here, this code only moves dependencies from old_bmain
504 * to new_bmain if needed, and add necessary remapping rules to the reuse_data.remapper. */
506 old_id_iter,
508 reuse_data,
510 }
511 }
513}
514
520{
521 ID *old_id_iter;
522 FOREACH_MAIN_LISTBASE_ID_BEGIN (&reuse_data->old_bmain->brushes, old_id_iter) {
523 const Brush *brush = reinterpret_cast<Brush *>(old_id_iter);
524 if (brush->gpencil_settings && brush->gpencil_settings->material &&
525 /* Don't unpin if this material is linked, then it can be preserved for the new file. */
527 {
528 /* Unpin material and clear pointer. */
529 brush->gpencil_settings->flag &= ~GP_BRUSH_MATERIAL_PINNED;
530 brush->gpencil_settings->material = nullptr;
531 }
532 }
534}
535
547static void swap_old_bmain_data_for_blendfile(ReuseOldBMainData *reuse_data, const short id_code)
548{
549 Main *new_bmain = reuse_data->new_bmain;
550 Main *old_bmain = reuse_data->old_bmain;
551
552 ListBase *new_lb = which_libbase(new_bmain, id_code);
553 ListBase *old_lb = which_libbase(old_bmain, id_code);
554
555 id::IDRemapper &remapper = reuse_bmain_data_remapper_ensure(reuse_data);
556
557 /* NOTE: Full swapping is only supported for ID types that are assumed to be only local
558 * data-blocks (like UI-like ones). Otherwise, the swapping could fail in many funny ways. */
561
562 std::swap(*new_lb, *old_lb);
563
564 /* TODO: Could add per-IDType control over name-maps clearing, if this becomes a performances
565 * concern. */
566 BKE_main_namemap_clear(old_bmain);
567 BKE_main_namemap_clear(new_bmain);
568
569 /* Original 'new' IDs have been moved into the old listbase and will be discarded (deleted).
570 * Original 'old' IDs have been moved into the new listbase and are being reused (kept).
571 * The discarded ones need to be remapped to a matching reused one, based on their names, if
572 * possible.
573 *
574 * Since both lists are ordered, and they are all local, we can do a smart parallel processing of
575 * both lists here instead of doing complete full list searches. */
576 ID *discarded_id_iter = static_cast<ID *>(old_lb->first);
577 ID *reused_id_iter = static_cast<ID *>(new_lb->first);
578 while (!ELEM(nullptr, discarded_id_iter, reused_id_iter)) {
579 const int strcmp_result = strcmp(discarded_id_iter->name + 2, reused_id_iter->name + 2);
580 if (strcmp_result == 0) {
581 /* Matching IDs, we can remap the discarded 'new' one to the re-used 'old' one. */
582 remapper.add(discarded_id_iter, reused_id_iter);
583
584 discarded_id_iter = static_cast<ID *>(discarded_id_iter->next);
585 reused_id_iter = static_cast<ID *>(reused_id_iter->next);
586 }
587 else if (strcmp_result < 0) {
588 /* No matching reused 'old' ID for this discarded 'new' one. */
589 remapper.add(discarded_id_iter, nullptr);
590
591 discarded_id_iter = static_cast<ID *>(discarded_id_iter->next);
592 }
593 else {
594 reused_id_iter = static_cast<ID *>(reused_id_iter->next);
595 }
596 }
597 /* Also remap all remaining non-compared discarded 'new' IDs to null. */
598 for (; discarded_id_iter != nullptr;
599 discarded_id_iter = static_cast<ID *>(discarded_id_iter->next))
600 {
601 remapper.add(discarded_id_iter, nullptr);
602 }
603
604 FOREACH_MAIN_LISTBASE_ID_BEGIN (new_lb, reused_id_iter) {
605 /* Necessary as all `session_uid` are renewed on blendfile loading. */
607
608 /* Ensure that the reused ID is remapped to itself, since it is known to be in the `new_bmain`.
609 */
610 remapper.add_overwrite(reused_id_iter, reused_id_iter);
611 }
613}
614
620static void swap_wm_data_for_blendfile(ReuseOldBMainData *reuse_data, const bool load_ui)
621{
622 Main *old_bmain = reuse_data->old_bmain;
623 Main *new_bmain = reuse_data->new_bmain;
624 ListBase *old_wm_list = &old_bmain->wm;
625 ListBase *new_wm_list = &new_bmain->wm;
626
627 /* Currently there should never be more than one WM in a main. */
628 BLI_assert(BLI_listbase_count_at_most(new_wm_list, 2) <= 1);
629 BLI_assert(BLI_listbase_count_at_most(old_wm_list, 2) <= 1);
630
631 wmWindowManager *old_wm = static_cast<wmWindowManager *>(old_wm_list->first);
632 wmWindowManager *new_wm = static_cast<wmWindowManager *>(new_wm_list->first);
633
634 if (old_wm == nullptr) {
635 /* No current (old) WM. Either (new) WM from file is used, or if none, WM code is responsible
636 * to add a new default WM. Nothing to do here. */
637 return;
638 }
639
640 /* Current (old) WM, and (new) WM in file, and loading UI: use WM from file, keep old WM around
641 * for further processing in WM code. */
642 if (load_ui && new_wm != nullptr) {
643 /* Support window-manager ID references being held between file load operations by keeping
644 * #Main.wm.first memory address in-place, while swapping all of its contents.
645 *
646 * This is needed so items such as key-maps can be held by an add-on,
647 * without it pointing to invalid memory, see: #86431. */
648 BLI_remlink(old_wm_list, old_wm);
649 BLI_remlink(new_wm_list, new_wm);
650 BKE_lib_id_swap_full(nullptr,
651 &old_wm->id,
652 &new_wm->id,
653 true,
656 /* Not strictly necessary, but helps for readability. */
657 std::swap<wmWindowManager *>(old_wm, new_wm);
658 BLI_addhead(new_wm_list, new_wm);
659 /* Do not add old WM back to `old_bmain`, so that it does not get freed when `old_bmain` is
660 * freed. Calling WM code will need this old WM to restore some windows etc. data into the
661 * new WM, and is responsible to free it properly. */
662 reuse_data->wm_setup_data->old_wm = old_wm;
663
664 id::IDRemapper &remapper = reuse_bmain_data_remapper_ensure(reuse_data);
665 remapper.add(&old_wm->id, &new_wm->id);
666 }
667 /* Current (old) WM, but no (new) one in file (should only happen when reading pre 2.5 files, no
668 * WM back then), or not loading UI: Keep current WM. */
669 else {
671 old_wm->init_flag &= ~WM_INIT_FLAG_WINDOW;
672 reuse_data->wm_setup_data->old_wm = old_wm;
673 }
674}
675
678{
679 ID *id = *cb_data->id_pointer;
680
681 if (id == nullptr) {
682 return IDWALK_RET_NOP;
683 }
684
685 ReuseOldBMainData *reuse_data = static_cast<ReuseOldBMainData *>(cb_data->user_data);
686
687 /* First check if it has already been remapped. */
688 id::IDRemapper &remapper = reuse_bmain_data_remapper_ensure(reuse_data);
690 return IDWALK_RET_NOP;
691 }
692
693 IDNameLib_Map *id_map = reuse_data->id_map;
694 BLI_assert(id_map != nullptr);
695
696 ID *id_new = BKE_main_idmap_lookup_id(id_map, id);
697 remapper.add(id, id_new);
698
699 return IDWALK_RET_NOP;
700}
701
703 const short id_code)
704{
705 Main *new_bmain = reuse_data->new_bmain;
706 ListBase *new_lb = which_libbase(new_bmain, id_code);
707
708 BLI_assert(reuse_data->id_map != nullptr);
709
710 ID *new_id_iter;
711 FOREACH_MAIN_LISTBASE_ID_BEGIN (new_lb, new_id_iter) {
712 /* Check all ID usages and find a matching new ID to remap them to in `new_bmain` if possible
713 * (matching by names and libraries).
714 *
715 * Note that this call does not do any effective remapping, it only adds required remapping
716 * operations to the remapper. */
718 new_id_iter,
720 reuse_data,
722 }
724}
725
727{
728 ID *id = *cb_data->id_pointer;
729
730 if (id == nullptr) {
731 return IDWALK_RET_NOP;
732 }
733
734 /* Embedded data cannot (yet) be fully trusted to have the same lib pointer as their owner ID, so
735 * for now ignore them. This code should never have anything to fix for them anyway, otherwise
736 * there is something extremely wrong going on. */
737 if ((cb_data->cb_flag & (IDWALK_CB_EMBEDDED | IDWALK_CB_EMBEDDED_NOT_OWNING)) != 0) {
738 return IDWALK_RET_NOP;
739 }
740
741 if (!ID_IS_LINKED(id)) {
742 ID *owner_id = cb_data->owner_id;
743
744 /* Do not allow linked data to use local data. */
745 if (ID_IS_LINKED(owner_id)) {
746 if (cb_data->cb_flag & IDWALK_CB_USER) {
747 id_us_min(id);
748 }
749 *cb_data->id_pointer = nullptr;
750 }
751 /* Do not allow local liboverride data to use local data as reference. */
752 else if (ID_IS_OVERRIDE_LIBRARY_REAL(owner_id) &&
753 &owner_id->override_library->reference == cb_data->id_pointer)
754 {
755 if (cb_data->cb_flag & IDWALK_CB_USER) {
756 id_us_min(id);
757 }
758 *cb_data->id_pointer = nullptr;
759 }
760 }
761
762 return IDWALK_RET_NOP;
763}
764
769{
770 Main *new_bmain = reuse_data->new_bmain;
771 ID *id_iter;
772 FOREACH_MAIN_ID_BEGIN (new_bmain, id_iter) {
773 if (!ID_IS_LINKED(id_iter) && !ID_IS_OVERRIDE_LIBRARY_REAL(id_iter)) {
774 continue;
775 }
776
777 ID *liboverride_reference = ID_IS_OVERRIDE_LIBRARY_REAL(id_iter) ?
778 id_iter->override_library->reference :
779 nullptr;
780
782 new_bmain, id_iter, reuse_bmain_data_invalid_local_usages_fix_cb, reuse_data, 0);
783
784 /* Liboverrides who lost their reference should not be liboverrides anymore, but regular IDs.
785 */
786 if (ID_IS_OVERRIDE_LIBRARY_REAL(id_iter) &&
787 id_iter->override_library->reference != liboverride_reference)
788 {
790 }
791 }
793}
794
795/* Post-remapping helpers to ensure validity of the UI data. */
796
797static void view3d_data_consistency_ensure(wmWindow *win, Scene *scene, ViewLayer *view_layer)
798{
800
801 LISTBASE_FOREACH (ScrArea *, area, &screen->areabase) {
802 LISTBASE_FOREACH (SpaceLink *, sl, &area->spacedata) {
803 if (sl->spacetype != SPACE_VIEW3D) {
804 continue;
805 }
806
807 View3D *v3d = reinterpret_cast<View3D *>(sl);
808 if (v3d->camera == nullptr || v3d->scenelock) {
809 v3d->camera = scene->camera;
810 }
811 if (v3d->localvd == nullptr) {
812 continue;
813 }
814
815 if (v3d->localvd->camera == nullptr || v3d->scenelock) {
816 v3d->localvd->camera = v3d->camera;
817 }
818 /* Local-view can become invalid during undo/redo steps, exit it when no valid object could
819 * be found. */
820 Base *base;
821 for (base = static_cast<Base *>(view_layer->object_bases.first); base; base = base->next) {
822 if (base->local_view_bits & v3d->local_view_uid) {
823 break;
824 }
825 }
826 if (base != nullptr) {
827 /* The local view3D still has a valid object, nothing else to do. */
828 continue;
829 }
830
831 /* No valid object found for the local view3D, it has to be cleared off. */
832 MEM_freeN(v3d->localvd);
833 v3d->localvd = nullptr;
834 v3d->local_view_uid = 0;
835
836 /* Region-base storage is different depending on whether the space is active or not. */
837 ListBase *regionbase = (sl == area->spacedata.first) ? &area->regionbase : &sl->regionbase;
838 LISTBASE_FOREACH (ARegion *, region, regionbase) {
839 if (region->regiontype != RGN_TYPE_WINDOW) {
840 continue;
841 }
842
843 RegionView3D *rv3d = static_cast<RegionView3D *>(region->regiondata);
844 MEM_SAFE_FREE(rv3d->localvd);
845 }
846 }
847 }
848}
849
851 Scene *cur_scene,
852 ViewLayer *cur_view_layer)
853{
854 /* There may not be any available WM (e.g. when reading `userpref.blend`). */
855 if (curwm == nullptr) {
856 return;
857 }
858
859 LISTBASE_FOREACH (wmWindow *, win, &curwm->windows) {
860 if (win->scene == nullptr) {
861 win->scene = cur_scene;
862 }
863 if (BKE_view_layer_find(win->scene, win->view_layer_name) == nullptr) {
864 STRNCPY(win->view_layer_name, cur_view_layer->name);
865 }
866
867 view3d_data_consistency_ensure(win, win->scene, cur_view_layer);
868 }
869}
870
884 BlendFileData *bfd,
886 BlendFileReadWMSetupData *wm_setup_data,
887 BlendFileReadReport *reports)
888{
889 Main *bmain = G_MAIN;
890 const bool recover = (G.fileflags & G_FILE_RECOVER_READ) != 0;
891 enum {
892 LOAD_UI = 1,
893 LOAD_UI_OFF,
894 LOAD_UNDO,
895 } mode;
896
897 if (params->undo_direction != STEP_INVALID) {
898 BLI_assert(bfd->curscene != nullptr);
899 mode = LOAD_UNDO;
900 }
901 else if (bfd->fileflags & G_FILE_ASSET_EDIT_FILE) {
902 BKE_report(reports->reports,
904 "This file is managed by the asset system, you cannot overwrite it (using \"Save "
905 "As\" is possible)");
906 /* From now on the file in memory is a normal file, further saving it will contain a
907 * window-manager, scene, ... and potentially user created data. Use #Main.is_asset_edit_file
908 * to detect if saving this file needs extra protections. */
909 bfd->fileflags &= ~G_FILE_ASSET_EDIT_FILE;
911 mode = LOAD_UI_OFF;
912 }
913 /* May happen with library files, loading undo-data should never have a null `curscene`
914 * (but may have a null `curscreen`). */
915 else if (ELEM(nullptr, bfd->curscreen, bfd->curscene)) {
916 BKE_report(reports->reports, RPT_WARNING, "Library file, loading empty scene");
917 mode = LOAD_UI_OFF;
918 }
919 else if (G.fileflags & G_FILE_NO_UI) {
920 mode = LOAD_UI_OFF;
921 }
922 else {
923 mode = LOAD_UI;
924 }
925
926 /* Free all render results, without this stale data gets displayed after loading files */
927 if (mode != LOAD_UNDO) {
929 }
930
931 /* Only make file-paths compatible when loading for real (not undo). */
932 if (mode != LOAD_UNDO) {
933 clean_paths(bfd->main);
934 }
935
937
938 /* Temporary data to handle swapping around IDs between old and new mains,
939 * and accumulate the required remapping accordingly. */
940 ReuseOldBMainData reuse_data = {nullptr};
941 reuse_data.new_bmain = bfd->main;
942 reuse_data.old_bmain = bmain;
943 reuse_data.wm_setup_data = wm_setup_data;
944
945 if (mode != LOAD_UNDO) {
946 const short ui_id_codes[]{ID_WS, ID_SCR};
947
948 /* WM needs special complex handling, regardless of whether UI is kept or loaded from file. */
949 swap_wm_data_for_blendfile(&reuse_data, mode == LOAD_UI);
950 if (mode != LOAD_UI) {
951 /* Re-use UI data from `old_bmain` if keeping existing UI. */
952 for (auto id_code : ui_id_codes) {
953 swap_old_bmain_data_for_blendfile(&reuse_data, id_code);
954 }
955 }
956
957 /* Needs to happen after all data from `old_bmain` has been moved into new one. */
958 BLI_assert(reuse_data.id_map == nullptr);
959 reuse_data.id_map = BKE_main_idmap_create(
960 reuse_data.new_bmain, true, reuse_data.old_bmain, MAIN_IDMAP_TYPE_NAME);
961
963 if (mode != LOAD_UI) {
964 for (auto id_code : ui_id_codes) {
965 swap_old_bmain_data_dependencies_process(&reuse_data, id_code);
966 }
967 }
968
969 BKE_main_idmap_destroy(reuse_data.id_map);
970
971 if (!params->is_factory_settings && reuse_editable_asset_needed(&reuse_data)) {
973 /* Keep linked brush asset data, similar to UI data. Only does a known
974 * subset know. Could do everything, but that risks dragging along more
975 * scene data than we want. */
976 for (short idtype_index = 0; idtype_index < INDEX_ID_MAX; idtype_index++) {
977 const IDTypeInfo *idtype_info = BKE_idtype_get_info_from_idtype_index(idtype_index);
978 if (ID_TYPE_SUPPORTS_ASSET_EDITABLE(idtype_info->id_code)) {
980 }
981 }
982 }
983
984 if (mode != LOAD_UI) {
985 LISTBASE_FOREACH (bScreen *, screen, &bfd->main->screens) {
987 }
988 }
989 }
990
991 /* Logic for 'track_undo_scene' is to keep using the scene which the active screen has, as long
992 * as the scene associated with the undo operation is visible in one of the open windows.
993 *
994 * - 'curscreen->scene': Scene the user is currently looking at.
995 * - 'bfd->curscene': Scene undo-step was created in.
996 *
997 * This means that users can have 2 or more windows open and undo in both without screens
998 * switching. But if they close one of the screens, undo will ensure that the scene being
999 * operated on will be activated (otherwise we'd be undoing on an off-screen scene which isn't
1000 * acceptable). See: #43424. */
1001 bool track_undo_scene = false;
1002
1003 /* Always use the Scene and ViewLayer pointers from new file, if possible. */
1004 ViewLayer *cur_view_layer = bfd->cur_view_layer;
1005 Scene *curscene = bfd->curscene;
1006
1007 wmWindow *win = nullptr;
1008 bScreen *curscreen = nullptr;
1009
1010 /* Ensure that there is a valid scene and view-layer. */
1011 if (curscene == nullptr) {
1012 curscene = static_cast<Scene *>(bfd->main->scenes.first);
1013 }
1014 /* Empty file, add a scene to make Blender work. */
1015 if (curscene == nullptr) {
1016 curscene = BKE_scene_add(bfd->main, "Empty");
1017 }
1018 if (cur_view_layer == nullptr) {
1019 /* Fallback to the active scene view layer. */
1020 cur_view_layer = BKE_view_layer_default_view(curscene);
1021 }
1022
1023 /* If UI is not loaded when opening actual `.blend` file,
1024 * and always in case of undo MEMFILE reading. */
1025 if (mode != LOAD_UI) {
1026 /* Re-use current window and screen. */
1027 win = CTX_wm_window(C);
1028 curscreen = CTX_wm_screen(C);
1029
1030 track_undo_scene = (mode == LOAD_UNDO && curscreen && curscene && bfd->main->wm.first);
1031
1032 if (track_undo_scene) {
1033 /* Keep the old (to-be-freed) scene, remapping below will ensure it's remapped to the
1034 * matching new scene if available, or null otherwise, in which case
1035 * #wm_data_consistency_ensure will define `curscene` as the active one. */
1036 }
1037 /* Enforce `curscene` to be in current screen. */
1038 else if (win) { /* The window may be null in background-mode. */
1039 win->scene = curscene;
1040 }
1041 }
1042
1044
1045 /* Apply remapping of ID pointers caused by re-using part of the data from the 'old' main into
1046 * the new one. */
1047 if (reuse_data.remapper != nullptr) {
1048 /* In undo case all "keeping old data" and remapping logic is now handled
1049 * in file reading code itself, so there should never be any remapping to do here. */
1050 BLI_assert(mode != LOAD_UNDO);
1051
1052 /* Handle all pending remapping from swapping old and new IDs around. */
1054 *reuse_data.remapper,
1057
1058 /* Fix potential invalid usages of now-locale-data created by remapping above. Should never
1059 * be needed in undo case, this is to address cases like:
1060 * "opening a blend-file that was a library of the previous opened blend-file". */
1062
1063 MEM_delete(reuse_data.remapper);
1064 reuse_data.remapper = nullptr;
1065
1066 wm_data_consistency_ensure(CTX_wm_manager(C), curscene, cur_view_layer);
1067 }
1068
1069 if (mode == LOAD_UNDO) {
1070 /* It's possible to undo into a time before the scene existed, in this case the window's scene
1071 * will be null. Since it doesn't make sense to remove the window, set it to the current scene.
1072 *
1073 * NOTE: Redo will restore the active scene to the window so a reasonably consistent state
1074 * is maintained. We could do better by keeping a window/scene map for each undo step.
1075 *
1076 * Another source of potential inconsistency is undoing into a step where the active camera
1077 * object does not exist (see e.g. #125636).
1078 */
1079 wm_data_consistency_ensure(CTX_wm_manager(C), curscene, cur_view_layer);
1080 }
1081
1083
1084 if (mode != LOAD_UI) {
1085 if (win) {
1086 curscene = win->scene;
1087 }
1088
1089 if (track_undo_scene) {
1090 wmWindowManager *wm = static_cast<wmWindowManager *>(bfd->main->wm.first);
1091 if (!wm_scene_is_visible(wm, bfd->curscene)) {
1092 curscene = bfd->curscene;
1093 if (win) {
1094 win->scene = curscene;
1095 }
1096 BKE_screen_view3d_scene_sync(curscreen, curscene);
1097 }
1098 }
1099
1100 /* We need to tag this here because events may be handled immediately after.
1101 * only the current screen is important because we won't have to handle
1102 * events from multiple screens at once. */
1103 if (curscreen) {
1105 }
1106 }
1107 CTX_data_scene_set(C, curscene);
1108
1110
1111 /* This frees the `old_bmain`. */
1113 bmain = G_MAIN;
1114 bfd->main = nullptr;
1115 CTX_data_main_set(C, bmain);
1116
1118
1119 /* These context data should remain valid if old UI is being re-used. */
1120 if (mode == LOAD_UI) {
1121 /* Setting a window-manger clears all other windowing members (window, screen, area, etc).
1122 * So only do it when effectively loading a new #wmWindowManager
1123 * otherwise just assert that the WM from context is still the same as in `new_bmain`. */
1124 CTX_wm_manager_set(C, static_cast<wmWindowManager *>(bmain->wm.first));
1126 CTX_wm_area_set(C, nullptr);
1127 CTX_wm_region_set(C, nullptr);
1128 CTX_wm_region_popup_set(C, nullptr);
1129 }
1130 BLI_assert(CTX_wm_manager(C) == static_cast<wmWindowManager *>(bmain->wm.first));
1131
1132 /* Keep state from preferences. */
1133 const int fileflags_keep = G_FILE_FLAG_ALL_RUNTIME;
1134 G.fileflags = (G.fileflags & fileflags_keep) | (bfd->fileflags & ~fileflags_keep);
1135
1136 /* Special cases, override any #G_FLAG_ALL_READFILE flags from the blend-file. */
1137 if (G.f != bfd->globalf) {
1138 const int flags_keep = G_FLAG_ALL_RUNTIME;
1140 bfd->globalf = (bfd->globalf & ~flags_keep) | (G.f & flags_keep);
1141 }
1142
1143 G.f = bfd->globalf;
1144
1145#ifdef WITH_PYTHON
1146 /* let python know about new main */
1147 if (CTX_py_init_get(C)) {
1149 }
1150#endif
1151
1152 if (mode != LOAD_UNDO) {
1153 /* Perform complex versioning that involves adding or removing IDs,
1154 * and/or needs to operate over the whole Main data-base
1155 * (versioning done in file reading code only operates on a per-library basis). */
1156 BLO_read_do_version_after_setup(bmain, nullptr, reports);
1157 }
1158
1159 bmain->recovered = false;
1160
1161 /* `startup.blend` or recovered startup. */
1162 if (params->is_startup) {
1163 bmain->filepath[0] = '\0';
1164 }
1165 else if (recover) {
1166 /* In case of auto-save or `quit.blend`, use original file-path instead
1167 * (see also #read_global in `readfile.cc`). */
1168 bmain->recovered = true;
1169 STRNCPY(bmain->filepath, bfd->filepath);
1170 }
1171
1172 /* Base-flags, groups, make depsgraph, etc. */
1173 /* first handle case if other windows have different scenes visible. */
1174 if (mode == LOAD_UI) {
1175 wmWindowManager *wm = static_cast<wmWindowManager *>(bmain->wm.first);
1176 if (wm) {
1177 LISTBASE_FOREACH (wmWindow *, win, &wm->windows) {
1178 if (win->scene && win->scene != curscene) {
1179 BKE_scene_set_background(bmain, win->scene);
1180 }
1181 }
1182 }
1183 }
1184
1185 /* Setting scene might require having a dependency graph, with copy-on-eval
1186 * we need to make sure we ensure scene has correct color management before
1187 * constructing dependency graph. */
1188 if (mode != LOAD_UNDO) {
1190 }
1191
1192 BKE_scene_set_background(bmain, curscene);
1193
1194 if (mode != LOAD_UNDO) {
1195 /* TODO(@sergey): Can this be also move above? */
1197 }
1198
1199 /* Both undo and regular file loading can perform some fairly complex ID manipulation, simpler
1200 * and safer to fully redo reference-counting. This is a relatively cheap process anyway. */
1201 BKE_main_id_refcount_recompute(bmain, false);
1202
1204
1205 if (mode != LOAD_UNDO && !USER_EXPERIMENTAL_TEST(&U, no_override_auto_resync)) {
1207
1209 bmain,
1210 curscene,
1212 reports);
1213
1216
1217 /* We need to rebuild some of the deleted override rules (for UI feedback purpose). */
1219 }
1220
1221 /* Now that liboverrides have been resynced and 'irrelevant' missing linked IDs has been removed,
1222 * report actual missing linked data. */
1223 if (mode != LOAD_UNDO) {
1224 ID *id_iter;
1225 int missing_linked_ids_num = 0;
1226 FOREACH_MAIN_ID_BEGIN (bmain, id_iter) {
1227 if (ID_IS_LINKED(id_iter) && (id_iter->tag & ID_TAG_MISSING)) {
1228 missing_linked_ids_num++;
1229 BLO_reportf_wrap(reports,
1230 RPT_INFO,
1231 RPT_("LIB: %s: '%s' missing from '%s', parent '%s'"),
1233 id_iter->name + 2,
1234 id_iter->lib->runtime.filepath_abs,
1235 id_iter->lib->runtime.parent ?
1237 "<direct>");
1238 }
1239 }
1241 reports->count.missing_linked_id = missing_linked_ids_num;
1242 }
1243}
1244
1246 BlendFileData *bfd,
1248 BlendFileReadWMSetupData *wm_setup_data,
1249 BlendFileReadReport *reports)
1250{
1251 if ((params->skip_flags & BLO_READ_SKIP_USERDEF) == 0) {
1252 setup_app_userdef(bfd);
1253 }
1254 if ((params->skip_flags & BLO_READ_SKIP_DATA) == 0) {
1255 setup_app_data(C, bfd, params, wm_setup_data, reports);
1256 }
1257}
1258
1260{
1261 if (main->versionfile > BLENDER_FILE_VERSION || (main->versionfile == BLENDER_FILE_VERSION &&
1262 main->subversionfile > BLENDER_FILE_SUBVERSION))
1263 {
1264 BKE_reportf(reports->reports,
1266 "File written by newer Blender binary (%d.%d), expect loss of data!",
1267 main->versionfile,
1268 main->subversionfile);
1269 }
1270}
1271
1273 BlendFileData *bfd,
1275 BlendFileReadWMSetupData *wm_setup_data,
1276 BlendFileReadReport *reports,
1277 /* Extra args. */
1278 const bool startup_update_defaults,
1279 const char *startup_app_template)
1280{
1281 if (bfd->main->is_read_invalid) {
1283 "File could not be read, critical data corruption detected");
1285 return;
1286 }
1287
1288 if (startup_update_defaults) {
1289 if ((params->skip_flags & BLO_READ_SKIP_DATA) == 0) {
1290 BLO_update_defaults_startup_blend(bfd->main, startup_app_template);
1291 }
1292 }
1293 setup_app_blend_file_data(C, bfd, params, wm_setup_data, reports);
1295}
1296
1298 BlendFileData *bfd,
1300 BlendFileReadReport *reports)
1301{
1302 BKE_blendfile_read_setup_readfile(C, bfd, params, nullptr, reports, false, nullptr);
1303}
1304
1305BlendFileData *BKE_blendfile_read(const char *filepath,
1307 BlendFileReadReport *reports)
1308{
1309 /* Don't print startup file loading. */
1310 if (params->is_startup == false) {
1311 if (!G.quiet) {
1312 printf("Read blend: \"%s\"\n", filepath);
1313 }
1314 }
1315
1316 BlendFileData *bfd = BLO_read_from_file(filepath, eBLOReadSkip(params->skip_flags), reports);
1317 if (bfd && bfd->main->is_read_invalid) {
1319 bfd = nullptr;
1320 }
1321 if (bfd) {
1322 handle_subversion_warning(bfd->main, reports);
1323 }
1324 else {
1325 BKE_reports_prependf(reports->reports, "Loading \"%s\" failed: ", filepath);
1326 }
1327 return bfd;
1328}
1329
1331 int file_buf_size,
1333 ReportList *reports)
1334{
1336 file_buf, file_buf_size, eBLOReadSkip(params->skip_flags), reports);
1337 if (bfd && bfd->main->is_read_invalid) {
1339 bfd = nullptr;
1340 }
1341 if (bfd) {
1342 /* Pass. */
1343 }
1344 else {
1345 BKE_reports_prepend(reports, "Loading failed: ");
1346 }
1347 return bfd;
1348}
1349
1351 MemFile *memfile,
1353 ReportList *reports)
1354{
1356 bmain, BKE_main_blendfile_path(bmain), memfile, params, reports);
1357 if (bfd && bfd->main->is_read_invalid) {
1359 bfd = nullptr;
1360 }
1361 if (bfd == nullptr) {
1362 BKE_reports_prepend(reports, "Loading failed: ");
1363 }
1364 return bfd;
1365}
1366
1368{
1369 Main *bmain = CTX_data_main(C);
1370 ListBase *lb;
1371 ID *id;
1372
1373 FOREACH_MAIN_LISTBASE_BEGIN (bmain, lb) {
1375 if (ELEM(GS(id->name), ID_SCE, ID_SCR, ID_WM, ID_WS)) {
1376 break;
1377 }
1378 BKE_id_delete(bmain, id);
1379 }
1381 }
1383}
1384
1387/* -------------------------------------------------------------------- */
1413UserDef *BKE_blendfile_userdef_read(const char *filepath, ReportList *reports)
1414{
1415 BlendFileData *bfd;
1416 UserDef *userdef = nullptr;
1417
1418 BlendFileReadReport blend_file_read_reports{};
1419 blend_file_read_reports.reports = reports;
1420
1421 bfd = BLO_read_from_file(
1422 filepath, BLO_READ_SKIP_ALL & ~BLO_READ_SKIP_USERDEF, &blend_file_read_reports);
1423 if (bfd) {
1424 if (bfd->user) {
1425 userdef = bfd->user;
1426 }
1427 BKE_main_free(bfd->main);
1428 MEM_freeN(bfd);
1429 }
1430
1431 return userdef;
1432}
1433
1435 int file_buf_size,
1436 ReportList *reports)
1437{
1438 BlendFileData *bfd;
1439 UserDef *userdef = nullptr;
1440
1442 file_buf, file_buf_size, BLO_READ_SKIP_ALL & ~BLO_READ_SKIP_USERDEF, reports);
1443 if (bfd) {
1444 if (bfd->user) {
1445 userdef = bfd->user;
1446 }
1447 BKE_main_free(bfd->main);
1448 MEM_freeN(bfd);
1449 }
1450 else {
1451 BKE_reports_prepend(reports, "Loading failed: ");
1452 }
1453
1454 return userdef;
1455}
1456
1458{
1459 UserDef *userdef = static_cast<UserDef *>(MEM_callocN(sizeof(UserDef), __func__));
1460 *userdef = blender::dna::shallow_copy(U_default);
1461
1462 /* Add-ons. */
1463 {
1464 const char *addons[] = {
1465 "io_anim_bvh",
1466 "io_curve_svg",
1467 "io_mesh_uv_layout",
1468 "io_scene_fbx",
1469 "io_scene_gltf2",
1470 "cycles",
1471 "pose_library",
1472 "bl_pkg",
1473 };
1474 for (int i = 0; i < ARRAY_SIZE(addons); i++) {
1475 bAddon *addon = BKE_addon_new();
1476 STRNCPY(addon->module, addons[i]);
1477 BLI_addtail(&userdef->addons, addon);
1478 }
1479 }
1480
1481 /* Theme. */
1482 {
1483 bTheme *btheme = static_cast<bTheme *>(MEM_mallocN(sizeof(*btheme), __func__));
1484 memcpy(btheme, &U_theme_default, sizeof(*btheme));
1485
1486 BLI_addtail(&userdef->themes, btheme);
1487 }
1488
1489#ifdef WITH_PYTHON_SECURITY
1490 /* use alternative setting for security nuts
1491 * otherwise we'd need to patch the binary blob - startup.blend.c */
1493#else
1494 userdef->flag &= ~USER_SCRIPT_AUTOEXEC_DISABLE;
1495#endif
1496
1497 /* System-specific fonts directory.
1498 * NOTE: when not found, leaves as-is (`//` for the blend-file directory). */
1499 if (BKE_appdir_font_folder_default(userdef->fontdir, sizeof(userdef->fontdir))) {
1500 /* Not actually needed, just a convention that directory selection
1501 * adds a trailing slash. */
1502 BLI_path_slash_ensure(userdef->fontdir, sizeof(userdef->fontdir));
1503 }
1504
1506 userdef->memcachelimit);
1507
1508 /* Init weight paint range. */
1509 BKE_colorband_init(&userdef->coba_weight, true);
1510
1511 /* Default studio light. */
1513
1514 /*
1515 * Enable translation by default. ALT Linux specific patch.
1516 * See ALT#31561
1517 */
1518
1519 userdef->language = ULANGUAGE_AUTO;
1520 userdef->transopts |= USER_TR_IFACE;
1521 userdef->transopts |= USER_TR_TOOLTIPS;
1522 userdef->transopts |= USER_TR_NEWDATANAME;
1523
1525
1527
1528 {
1530 userdef, "VIEW3D_AST_brush_sculpt", "Brushes/Mesh Sculpt/General");
1532 userdef, "VIEW3D_AST_brush_sculpt", "Brushes/Mesh Sculpt/Paint");
1534 userdef, "VIEW3D_AST_brush_sculpt", "Brushes/Mesh Sculpt/Simulation");
1535
1537 userdef, "VIEW3D_AST_brush_gpencil_paint", "Brushes/Grease Pencil Draw/Draw");
1539 userdef, "VIEW3D_AST_brush_gpencil_paint", "Brushes/Grease Pencil Draw/Erase");
1541 userdef, "VIEW3D_AST_brush_gpencil_paint", "Brushes/Grease Pencil Draw/Utilities");
1542
1544 userdef, "VIEW3D_AST_brush_gpencil_sculpt", "Brushes/Grease Pencil Sculpt/Contrast");
1546 userdef, "VIEW3D_AST_brush_gpencil_sculpt", "Brushes/Grease Pencil Sculpt/Transform");
1548 userdef, "VIEW3D_AST_brush_gpencil_sculpt", "Brushes/Grease Pencil Sculpt/Utilities");
1549 }
1550
1551 return userdef;
1552}
1553
1554bool BKE_blendfile_userdef_write(const char *filepath, ReportList *reports)
1555{
1556 Main *mainb = MEM_cnew<Main>("empty main");
1557 bool ok = false;
1558
1560 params.use_userdef = true;
1561
1562 if (BLO_write_file(mainb, filepath, 0, &params, reports)) {
1563 ok = true;
1564 }
1565
1566 MEM_freeN(mainb);
1567
1568 return ok;
1569}
1570
1571bool BKE_blendfile_userdef_write_app_template(const char *filepath, ReportList *reports)
1572{
1573 /* Checking that `filepath` exists is not essential, it just avoids printing a warning that
1574 * the file can't be found. In this case it's not an error - as the file is used if it exists,
1575 * falling back to the defaults.
1576 * If the preferences exists but file reading fails - the file can be assumed corrupt
1577 * so overwriting the file is OK. */
1578 UserDef *userdef_default = BLI_exists(filepath) ? BKE_blendfile_userdef_read(filepath, nullptr) :
1579 nullptr;
1580 if (userdef_default == nullptr) {
1581 userdef_default = BKE_blendfile_userdef_from_defaults();
1582 }
1583
1585 bool ok = BKE_blendfile_userdef_write(filepath, reports);
1587 BKE_blender_userdef_data_free(userdef_default, false);
1588 MEM_freeN(userdef_default);
1589 return ok;
1590}
1591
1593{
1594 char filepath[FILE_MAX];
1595 std::optional<std::string> cfgdir;
1596 bool ok = true;
1597 const bool use_template_userpref = BKE_appdir_app_template_has_userpref(U.app_template);
1598
1599 if ((cfgdir = BKE_appdir_folder_id_create(BLENDER_USER_CONFIG, nullptr))) {
1600 bool ok_write;
1601 BLI_path_join(filepath, sizeof(filepath), cfgdir->c_str(), BLENDER_USERPREF_FILE);
1602 if (!G.quiet) {
1603 printf("Writing userprefs: \"%s\" ", filepath);
1604 }
1605 if (use_template_userpref) {
1606 ok_write = BKE_blendfile_userdef_write_app_template(filepath, reports);
1607 }
1608 else {
1609 ok_write = BKE_blendfile_userdef_write(filepath, reports);
1610 }
1611
1612 if (ok_write) {
1613 if (!G.quiet) {
1614 printf("ok\n");
1615 }
1616 BKE_report(reports, RPT_INFO, "Preferences saved");
1617 }
1618 else {
1619 if (!G.quiet) {
1620 printf("fail\n");
1621 }
1622 ok = false;
1623 BKE_report(reports, RPT_ERROR, "Saving preferences failed");
1624 }
1625 }
1626 else {
1627 BKE_report(reports, RPT_ERROR, "Unable to create userpref path");
1628 }
1629
1630 if (use_template_userpref) {
1631 if ((cfgdir = BKE_appdir_folder_id_create(BLENDER_USER_CONFIG, U.app_template))) {
1632 /* Also save app-template preferences. */
1633 BLI_path_join(filepath, sizeof(filepath), cfgdir->c_str(), BLENDER_USERPREF_FILE);
1634
1635 if (!G.quiet) {
1636 printf("Writing userprefs app-template: \"%s\" ", filepath);
1637 }
1638 if (BKE_blendfile_userdef_write(filepath, reports) != 0) {
1639 if (!G.quiet) {
1640 printf("ok\n");
1641 }
1642 }
1643 else {
1644 if (!G.quiet) {
1645 printf("fail\n");
1646 }
1647 ok = false;
1648 }
1649 }
1650 else {
1651 BKE_report(reports, RPT_ERROR, "Unable to create app-template userpref path");
1652 ok = false;
1653 }
1654 }
1655
1656 if (ok) {
1657 U.runtime.is_dirty = false;
1658 }
1659 return ok;
1660}
1661
1664/* -------------------------------------------------------------------- */
1669 const void *file_buf,
1670 int file_buf_size,
1671 ReportList *reports)
1672{
1673 BlendFileData *bfd;
1674 WorkspaceConfigFileData *workspace_config = nullptr;
1675
1676 if (filepath) {
1677 BlendFileReadReport blend_file_read_reports{};
1678 blend_file_read_reports.reports = reports;
1679 bfd = BLO_read_from_file(filepath, BLO_READ_SKIP_USERDEF, &blend_file_read_reports);
1680 }
1681 else {
1682 bfd = BLO_read_from_memory(file_buf, file_buf_size, BLO_READ_SKIP_USERDEF, reports);
1683 }
1684
1685 if (bfd) {
1686 workspace_config = MEM_cnew<WorkspaceConfigFileData>(__func__);
1687 workspace_config->main = bfd->main;
1688
1689 /* Only 2.80+ files have actual workspaces, don't try to use screens
1690 * from older versions. */
1691 if (bfd->main->versionfile >= 280) {
1692 workspace_config->workspaces = bfd->main->workspaces;
1693 }
1694
1695 MEM_freeN(bfd);
1696 }
1697
1698 return workspace_config;
1699}
1700
1702{
1703 BKE_main_free(workspace_config->main);
1704 MEM_freeN(workspace_config);
1705}
1706
1709/* -------------------------------------------------------------------- */
1713static CLG_LogRef LOG_PARTIALWRITE = {"bke.blendfile.partial_write"};
1714
1715namespace blender::bke::blendfile {
1716
1718 : reference_root_filepath_(reference_root_filepath)
1719{
1720 BKE_main_init(this->bmain);
1721 if (!reference_root_filepath_.empty()) {
1722 STRNCPY(this->bmain.filepath, reference_root_filepath_.c_str());
1723 }
1724 /* Only for IDs matching existing data in current G_MAIN. */
1725 matching_uid_map_ = BKE_main_idmap_create(&this->bmain, false, nullptr, MAIN_IDMAP_TYPE_UID);
1726 /* For all IDs existing in the context. */
1728 &this->bmain, false, nullptr, MAIN_IDMAP_TYPE_UID | MAIN_IDMAP_TYPE_NAME);
1729};
1730
1732{
1733 BKE_main_idmap_destroy(matching_uid_map_);
1734
1735 BLI_assert(this->bmain.next == nullptr);
1736 BKE_main_destroy(this->bmain);
1737};
1738
1739void PartialWriteContext::preempt_session_uid(ID *ctx_id, uint session_uid)
1740{
1741 /* If there is already an existing ID in the 'matching' set with that UID, it should be the same
1742 * as the given ctx_id. */
1743 ID *matching_ctx_id = BKE_main_idmap_lookup_uid(matching_uid_map_, session_uid);
1744 if (matching_ctx_id == ctx_id) {
1745 /* That ID has already been added to the context, nothing to do. */
1746 BLI_assert(matching_ctx_id->session_uid == session_uid);
1747 return;
1748 }
1749 if (matching_ctx_id != nullptr) {
1750 /* Another ID in the context, who has a matching ID in current G_MAIN, is sharing the same
1751 * session UID. This marks a critical corruption somewhere! */
1752 CLOG_FATAL(
1754 "Different matching IDs sharing the same session UID in the partial write context.");
1755 return;
1756 }
1757 /* No ID with this session UID in the context, who's matching a current ID in G_MAIN. Check if a
1758 * non-matching context ID is already using that UID, if yes, regenerate a new one for it, such
1759 * that given `ctx_id` can use the desired UID. */
1760 /* NOTE: In theory, there should never be any session uid collision currently, since these are
1761 * generated session-wide, regardless of the type/source of the IDs. */
1762 matching_ctx_id = BKE_main_idmap_lookup_uid(this->bmain.id_map, session_uid);
1763 BLI_assert(matching_ctx_id != ctx_id);
1764 if (matching_ctx_id) {
1766 3,
1767 "Non-matching IDs sharing the same session UID in the partial write context.");
1768 BKE_main_idmap_remove_id(this->bmain.id_map, matching_ctx_id);
1769 /* FIXME: Allow #BKE_lib_libblock_session_uid_renew to work with temp IDs? */
1770 matching_ctx_id->tag &= ~ID_TAG_TEMP_MAIN;
1771 BKE_lib_libblock_session_uid_renew(matching_ctx_id);
1772 matching_ctx_id->tag |= ID_TAG_TEMP_MAIN;
1773 BKE_main_idmap_insert_id(this->bmain.id_map, matching_ctx_id);
1774 BLI_assert(BKE_main_idmap_lookup_uid(this->bmain.id_map, session_uid) == nullptr);
1775 }
1776 ctx_id->session_uid = session_uid;
1777}
1778
1779void PartialWriteContext::process_added_id(ID *ctx_id,
1780 const PartialWriteContext::IDAddOperations operations)
1781{
1782 const bool set_fake_user = (operations & SET_FAKE_USER) != 0;
1783 const bool set_clipboard_mark = (operations & SET_CLIPBOARD_MARK) != 0;
1784
1785 if (set_fake_user) {
1786 id_fake_user_set(ctx_id);
1787 }
1788 else {
1789 /* NOTE: Using this tag will ensure that this ID is written on disk in current state (current
1790 * context session). However, reloading the blendfile will clear this tag. */
1791 id_us_ensure_real(ctx_id);
1792 }
1793
1794 if (set_clipboard_mark) {
1795 ctx_id->flag |= ID_FLAG_CLIPBOARD_MARK;
1796 }
1797}
1798
1799ID *PartialWriteContext::id_add_copy(const ID *id, const bool regenerate_session_uid)
1800{
1801 ID *ctx_root_id = nullptr;
1802 BLI_assert(BKE_main_idmap_lookup_uid(matching_uid_map_, id->session_uid) == nullptr);
1803 const int copy_flags = (LIB_ID_CREATE_NO_MAIN | LIB_ID_CREATE_NO_USER_REFCOUNT |
1804 /* NOTE: Could make this an option if needed in the future */
1806 ctx_root_id = BKE_id_copy_in_lib(nullptr, id->lib, id, nullptr, nullptr, copy_flags);
1807 ctx_root_id->tag |= ID_TAG_TEMP_MAIN;
1808 if (regenerate_session_uid) {
1809 /* Calling #BKE_lib_libblock_session_uid_renew is not needed here, copying already generated a
1810 * new one. */
1811 BLI_assert(BKE_main_idmap_lookup_uid(matching_uid_map_, id->session_uid) == nullptr);
1812 }
1813 else {
1814 this->preempt_session_uid(ctx_root_id, id->session_uid);
1815 BKE_main_idmap_insert_id(matching_uid_map_, ctx_root_id);
1816 }
1817 BKE_main_idmap_insert_id(this->bmain.id_map, ctx_root_id);
1818 BKE_libblock_management_main_add(&this->bmain, ctx_root_id);
1819 /* Note: remapping of external file relative paths is done as part of the 'write' process. */
1820 return ctx_root_id;
1821}
1822
1823void PartialWriteContext::make_local(ID *ctx_id, const int make_local_flags)
1824{
1825 /* Making an ID local typically resets its session UID, here we want to keep the same value. */
1826 const uint ctx_id_session_uid = ctx_id->session_uid;
1827 BKE_main_idmap_remove_id(this->bmain.id_map, ctx_id);
1828 BKE_main_idmap_remove_id(matching_uid_map_, ctx_id);
1829
1830 BKE_lib_id_make_local(&this->bmain, ctx_id, make_local_flags);
1831
1832 this->preempt_session_uid(ctx_id, ctx_id_session_uid);
1833 BKE_main_idmap_insert_id(this->bmain.id_map, ctx_id);
1834 BKE_main_idmap_insert_id(matching_uid_map_, ctx_id);
1835}
1836
1837Library *PartialWriteContext::ensure_library(ID *ctx_id)
1838{
1839 if (!ID_IS_LINKED(ctx_id)) {
1840 return nullptr;
1841 }
1842 blender::StringRefNull lib_path = ctx_id->lib->runtime.filepath_abs;
1843 Library *ctx_lib = this->libraries_map_.lookup_default(lib_path, nullptr);
1844 if (!ctx_lib) {
1845 ctx_lib = reinterpret_cast<Library *>(id_add_copy(&ctx_id->lib->id, true));
1846 this->libraries_map_.add(lib_path, ctx_lib);
1847 }
1848 ctx_id->lib = ctx_lib;
1849 return ctx_lib;
1850}
1851Library *PartialWriteContext::ensure_library(blender::StringRefNull library_absolute_path)
1852{
1853 Library *ctx_lib = this->libraries_map_.lookup_default(library_absolute_path, nullptr);
1854 if (!ctx_lib) {
1855 const char *library_name = BLI_path_basename(library_absolute_path.c_str());
1856 ctx_lib = static_cast<Library *>(
1857 BKE_id_new_in_lib(&this->bmain, nullptr, ID_LI, library_name));
1858 ctx_lib->id.tag |= ID_TAG_TEMP_MAIN;
1859 id_us_min(&ctx_lib->id);
1860 this->libraries_map_.add(library_absolute_path, ctx_lib);
1861 }
1862 return ctx_lib;
1863}
1864
1866 const ID *id,
1870 dependencies_filter_cb)
1871{
1872 constexpr int make_local_flags = (LIB_ID_MAKELOCAL_INDIRECT | LIB_ID_MAKELOCAL_FORCE_LOCAL |
1874
1875 const bool add_dependencies = (options.operations & ADD_DEPENDENCIES) != 0;
1876 const bool clear_dependencies = (options.operations & CLEAR_DEPENDENCIES) != 0;
1877 const bool duplicate_dependencies = (options.operations & DUPLICATE_DEPENDENCIES) != 0;
1878 BLI_assert(clear_dependencies || add_dependencies || dependencies_filter_cb);
1879 BLI_assert(!clear_dependencies || !(add_dependencies || duplicate_dependencies));
1880 UNUSED_VARS_NDEBUG(add_dependencies, clear_dependencies, duplicate_dependencies);
1881
1882 /* Do not directly add an embedded ID. Add its owner instead. */
1883 if (id->flag & ID_FLAG_EMBEDDED_DATA) {
1884 id = BKE_id_owner_get(const_cast<ID *>(id), true);
1885 }
1886
1887 /* The given ID may have already been added (either explicitly or as a dependency) before. */
1888 ID *ctx_root_id = BKE_main_idmap_lookup_uid(matching_uid_map_, id->session_uid);
1889 if (ctx_root_id) {
1890 /* If the root orig ID is already in the context, assume all of its dependencies are as well.
1891 */
1892 BLI_assert(ctx_root_id->session_uid == id->session_uid);
1893 this->process_added_id(ctx_root_id, options.operations);
1894 return ctx_root_id;
1895 }
1896
1897 /* Local mapping, such that even in case dependencies are duplicated for this specific added ID,
1898 * once a dependency has been duplicated, it can be re-used for other ID usages within the
1899 * dependencies of the added ID. */
1900 blender::Map<const ID *, ID *> local_ctx_id_map;
1901 /* A list of IDs to post-process. Only contains IDs that were actually added to the context (not
1902 * the ones that were already there and were re-used). The #IDAddOperations item of the pair
1903 * stores the returned value from the given #dependencies_filter_cb (or given global #options
1904 * parameter otherwise). */
1906
1907 ctx_root_id = id_add_copy(id, false);
1908 BLI_assert(ctx_root_id->session_uid == id->session_uid);
1909 local_ctx_id_map.add(id, ctx_root_id);
1910 post_process_ids_todo.append({ctx_root_id, options.operations});
1911 this->process_added_id(ctx_root_id, options.operations);
1912
1913 blender::VectorSet<ID *> ids_to_process{ctx_root_id};
1914 auto dependencies_cb = [this,
1915 options,
1916 &local_ctx_id_map,
1917 &ids_to_process,
1918 &post_process_ids_todo,
1919 dependencies_filter_cb](LibraryIDLinkCallbackData *cb_data) -> int {
1920 ID **id_ptr = cb_data->id_pointer;
1921 const ID *orig_deps_id = *id_ptr;
1922
1924 return IDWALK_RET_NOP;
1925 }
1926 if (!orig_deps_id) {
1927 return IDWALK_RET_NOP;
1928 }
1929
1930 if (cb_data->cb_flag & IDWALK_CB_INTERNAL) {
1931 /* Cleanup internal ID pointers. */
1932 *id_ptr = nullptr;
1933 return IDWALK_RET_NOP;
1934 }
1935
1937 options.operations & MASK_INHERITED);
1938 if (dependencies_filter_cb) {
1939 const PartialWriteContext::IDAddOperations operations_per_id = dependencies_filter_cb(
1940 cb_data, options);
1941 operations_final = PartialWriteContext::IDAddOperations(
1942 (operations_per_id & MASK_PER_ID_USAGE) | (operations_final & ~MASK_PER_ID_USAGE));
1943 }
1944
1945 const bool add_dependencies = (operations_final & ADD_DEPENDENCIES) != 0;
1946 const bool clear_dependencies = (operations_final & CLEAR_DEPENDENCIES) != 0;
1947 const bool duplicate_dependencies = (operations_final & DUPLICATE_DEPENDENCIES) != 0;
1948 BLI_assert(clear_dependencies || add_dependencies);
1949 BLI_assert(!clear_dependencies || !(add_dependencies || duplicate_dependencies));
1950 UNUSED_VARS_NDEBUG(add_dependencies);
1951
1952 if (clear_dependencies) {
1953 if (cb_data->cb_flag & IDWALK_CB_NEVER_NULL) {
1955 "Clearing a 'never null' ID usage of '%s' by '%s', this is likely not a "
1956 "desired action",
1957 (*id_ptr)->name,
1958 cb_data->owner_id->name);
1959 }
1960 /* Owner ID should be a 'context-main' duplicate of a real Main ID, as such there should be
1961 * no need to decrease ID usages refcount here. */
1962 *id_ptr = nullptr;
1963 return IDWALK_RET_NOP;
1964 }
1965 /* else if (add_dependencies) */
1966 /* The given ID may have already been added (either explicitly or as a dependency) before. */
1967 ID *ctx_deps_id = nullptr;
1968 if (duplicate_dependencies) {
1969 ctx_deps_id = local_ctx_id_map.lookup(orig_deps_id);
1970 }
1971 else {
1972 ctx_deps_id = BKE_main_idmap_lookup_uid(matching_uid_map_, orig_deps_id->session_uid);
1973 }
1974 if (!ctx_deps_id) {
1975 if (cb_data->cb_flag & IDWALK_CB_LOOPBACK) {
1976 /* Do not follow 'loop back' pointers. */
1977 /* NOTE: Not sure whether this should be considered an error or not. Typically hitting such
1978 * a case is bad practice. On the other hand, some of these pointers are present in
1979 * 'normal' IDs, like e.g. the parent collections ones. This implies that currently, all
1980 * attempt to adding a collection to a partial write context should make usage of a custom
1981 * `dependencies_filter_cb` function to explicitly clear these pointers. */
1983 "First dependency to ID '%s' found through a 'loopback' usage from ID '%s', "
1984 "this should never happen",
1985 (*id_ptr)->name,
1986 cb_data->owner_id->name);
1987 *id_ptr = nullptr;
1988 return IDWALK_RET_NOP;
1989 }
1990 ctx_deps_id = this->id_add_copy(orig_deps_id, duplicate_dependencies);
1991 local_ctx_id_map.add(orig_deps_id, ctx_deps_id);
1992 ids_to_process.add(ctx_deps_id);
1993 post_process_ids_todo.append({ctx_deps_id, operations_final});
1994 }
1995 if (duplicate_dependencies) {
1996 BLI_assert(ctx_deps_id->session_uid != orig_deps_id->session_uid);
1997 }
1998 else {
1999 BLI_assert(ctx_deps_id->session_uid == orig_deps_id->session_uid);
2000 }
2001 this->process_added_id(ctx_deps_id, operations_final);
2002 /* In-place remapping. */
2003 *id_ptr = ctx_deps_id;
2004 return IDWALK_RET_NOP;
2005 };
2006 while (!ids_to_process.is_empty()) {
2007 ID *ctx_id = ids_to_process.pop();
2009 &this->bmain, ctx_id, dependencies_cb, &options, IDWALK_DO_INTERNAL_RUNTIME_POINTERS);
2010 }
2011
2012 /* Post process all newly added IDs in the context:
2013 * - Make them local or ensure that their library reference is also in the context.
2014 */
2015 for (auto [ctx_id, options_final] : post_process_ids_todo) {
2016 const bool do_make_local = (options_final & MAKE_LOCAL) != 0;
2017 if (do_make_local) {
2018 this->make_local(ctx_id, make_local_flags);
2019 }
2020 else {
2021 this->ensure_library(ctx_id);
2022 }
2023 }
2024
2025 return ctx_root_id;
2026}
2027
2030 Library *library,
2032{
2033 Library *ctx_library = nullptr;
2034 if (library) {
2035 ctx_library = this->ensure_library(library->runtime.filepath_abs);
2036 }
2037 ID *ctx_id = static_cast<ID *>(
2038 BKE_id_new_in_lib(&this->bmain, ctx_library, id_type, id_name.c_str()));
2039 ctx_id->tag |= ID_TAG_TEMP_MAIN;
2040 id_us_min(ctx_id);
2041 this->process_added_id(ctx_id, options.operations);
2042 /* See function doc about why handling of #matching_uid_map_ can be skipped here. */
2043 BKE_main_idmap_insert_id(this->bmain.id_map, ctx_id);
2044 return ctx_id;
2045}
2046
2048{
2049 if (ID *ctx_id = BKE_main_idmap_lookup_uid(matching_uid_map_, id->session_uid)) {
2050 BKE_main_idmap_remove_id(matching_uid_map_, ctx_id);
2051 BKE_id_delete(&this->bmain, ctx_id);
2052 }
2053}
2054
2055void PartialWriteContext::remove_unused(const bool clear_extra_user)
2056{
2058 parameters.do_local_ids = true;
2059 parameters.do_linked_ids = true;
2060 parameters.do_recursive = true;
2061
2062 if (clear_extra_user) {
2063 ID *id_iter;
2064 FOREACH_MAIN_ID_BEGIN (&this->bmain, id_iter) {
2065 id_us_clear_real(id_iter);
2066 }
2068 }
2069 BKE_lib_query_unused_ids_tag(&this->bmain, ID_TAG_DOIT, parameters);
2070
2072 3,
2073 "Removing %d unused IDs from current partial write context",
2074 parameters.num_total[INDEX_ID_NULL]);
2075 ID *id_iter;
2076 FOREACH_MAIN_ID_BEGIN (&this->bmain, id_iter) {
2077 if ((id_iter->tag & ID_TAG_DOIT) != 0) {
2078 BKE_main_idmap_remove_id(matching_uid_map_, id_iter);
2079 }
2080 }
2083}
2084
2086{
2087 BKE_main_idmap_clear(*matching_uid_map_);
2088 BKE_main_clear(this->bmain);
2089}
2090
2092{
2093 blender::Set<ID *> ids_in_context;
2094 blender::Set<uint> session_uids_in_context;
2095 bool is_valid = true;
2096
2097 ID *id_iter;
2098
2099 /* Fill `ids_in_context`, check uniqueness of session_uid's. */
2100 FOREACH_MAIN_ID_BEGIN (&this->bmain, id_iter) {
2101 ids_in_context.add(id_iter);
2102 if (session_uids_in_context.contains(id_iter->session_uid)) {
2103 CLOG_ERROR(&LOG_PARTIALWRITE, "ID %s does not have a unique session_uid", id_iter->name);
2104 is_valid = false;
2105 }
2106 else {
2107 session_uids_in_context.add(id_iter->session_uid);
2108 }
2109 }
2111
2112 /* Check that no ID uses IDs from outside this context. */
2113 auto id_validate_dependencies_cb = [&ids_in_context,
2114 &is_valid](LibraryIDLinkCallbackData *cb_data) -> int {
2115 ID **id_p = cb_data->id_pointer;
2116 ID *owner_id = cb_data->owner_id;
2117 ID *self_id = cb_data->self_id;
2118
2119 /* By definition, embedded IDs are not in Main, so they are not listed in this context either.
2120 */
2121 if (cb_data->cb_flag & (IDWALK_CB_EMBEDDED | IDWALK_CB_EMBEDDED_NOT_OWNING)) {
2122 return IDWALK_RET_NOP;
2123 }
2124
2125 if (*id_p && !ids_in_context.contains(*id_p)) {
2126 if (owner_id != self_id) {
2127 CLOG_ERROR(
2129 "ID %s (used by ID '%s', embedded ID '%s') is not in current partial write context",
2130 (*id_p)->name,
2131 owner_id->name,
2132 self_id->name);
2133 }
2134 else {
2136 "ID %s (used by ID '%s') is not in current partial write context",
2137 (*id_p)->name,
2138 owner_id->name);
2139 }
2140 is_valid = false;
2141 }
2142 return IDWALK_RET_NOP;
2143 };
2144 FOREACH_MAIN_ID_BEGIN (&this->bmain, id_iter) {
2146 &this->bmain, id_iter, id_validate_dependencies_cb, nullptr, IDWALK_READONLY);
2147 }
2149
2150 return is_valid;
2151}
2152
2153bool PartialWriteContext::write(const char *write_filepath,
2154 const int write_flags,
2155 const int remap_mode,
2156 ReportList &reports)
2157{
2158 BLI_assert_msg(write_filepath != reference_root_filepath_,
2159 "A library blendfile should not overwrite currently edited blendfile");
2160
2161 /* In case the write path is the same as one of the libraries used by this context, make this
2162 * library local, and delete it (and all of its potentially remaining linked data). */
2163 Library *make_local_lib = nullptr;
2164 LISTBASE_FOREACH (Library *, library, &this->bmain.libraries) {
2165 if (STREQ(write_filepath, library->runtime.filepath_abs)) {
2166 make_local_lib = library;
2167 }
2168 }
2169 if (make_local_lib) {
2170 BKE_library_make_local(&this->bmain, make_local_lib, nullptr, false, false, false);
2171 BKE_id_delete(&this->bmain, make_local_lib);
2172 make_local_lib = nullptr;
2173 }
2174
2175 BLI_assert(this->is_valid());
2176
2177 BlendFileWriteParams blend_file_write_params{};
2178 blend_file_write_params.remap_mode = eBLO_WritePathRemap(remap_mode);
2179 return BLO_write_file(
2180 &this->bmain, write_filepath, write_flags, &blend_file_write_params, &reports);
2181}
2182
2183bool PartialWriteContext::write(const char *write_filepath, ReportList &reports)
2184{
2185 return this->write(write_filepath, 0, BLO_WRITE_PATH_REMAP_RELATIVE, reports);
2186}
2187
2188} // namespace blender::bke::blendfile
2189
struct bAddon * BKE_addon_new(void)
Definition addon.cc:35
#define BLENDER_USERPREF_FILE
bool BKE_appdir_app_template_has_userpref(const char *app_template) ATTR_NONNULL(1)
Definition appdir.cc:1085
@ BLENDER_USER_CONFIG
bool BKE_appdir_font_folder_default(char *dir, size_t dir_maxncpy)
Definition appdir.cc:250
std::optional< std::string > BKE_appdir_folder_id_create(int folder_id, const char *subfolder) ATTR_WARN_UNUSED_RESULT
Definition appdir.cc:764
Blender util stuff.
void BKE_blender_globals_main_replace(Main *bmain)
Definition blender.cc:218
void BKE_blender_userdef_data_set_and_free(UserDef *userdef)
Definition blender.cc:264
void BKE_blender_userdef_app_template_data_swap(UserDef *userdef_a, UserDef *userdef_b)
Definition blender.cc:384
void BKE_blender_userdef_data_free(UserDef *userdef, bool clear_fonts)
Definition blender.cc:335
#define BLENDER_FILE_SUBVERSION
#define BLENDER_FILE_VERSION
@ BKE_BPATH_FOREACH_PATH_SKIP_MULTIFILE
Definition BKE_bpath.hh:55
void BKE_bpath_foreach_path_main(BPathForeachPathData *bpath_data)
Definition bpath.cc:114
void BKE_colorband_init(ColorBand *coba, bool rangetype)
Definition colorband.cc:22
void CTX_data_main_set(bContext *C, Main *bmain)
bScreen * CTX_wm_screen(const bContext *C)
bool CTX_py_init_get(bContext *C)
void CTX_wm_manager_set(bContext *C, wmWindowManager *wm)
wmWindow * CTX_wm_window(const bContext *C)
void CTX_wm_screen_set(bContext *C, bScreen *screen)
void CTX_data_scene_set(bContext *C, Scene *scene)
Main * CTX_data_main(const bContext *C)
void CTX_wm_area_set(bContext *C, ScrArea *area)
void CTX_wm_region_set(bContext *C, ARegion *region)
wmWindowManager * CTX_wm_manager(const bContext *C)
void CTX_wm_region_popup_set(bContext *C, ARegion *region_popup)
#define G_FLAG_ALL_READFILE
@ G_FILE_ASSET_EDIT_FILE
@ G_FILE_RECOVER_READ
@ G_FILE_NO_UI
#define G_MAIN
#define G_FILE_FLAG_ALL_RUNTIME
#define G_FLAG_ALL_RUNTIME
const IDTypeInfo * BKE_idtype_get_info_from_idtype_index(const int idtype_index)
Definition idtype.cc:133
const char * BKE_idtype_idcode_to_name(short idcode)
Definition idtype.cc:168
ViewLayer * BKE_view_layer_default_view(const Scene *scene)
ViewLayer * BKE_view_layer_find(const Scene *scene, const char *layer_name)
void BKE_id_delete(Main *bmain, void *idv) ATTR_NONNULL()
IDNewNameResult BKE_id_new_name_validate(Main &bmain, ListBase &lb, ID &id, const char *newname, IDNewNameMode mode, bool do_linked_data)
Definition lib_id.cc:1850
void size_t BKE_id_multi_tagged_delete(Main *bmain) ATTR_NONNULL()
@ LIB_ID_COPY_ASSET_METADATA
@ LIB_ID_CREATE_NO_USER_REFCOUNT
@ LIB_ID_CREATE_NO_MAIN
void * BKE_id_new_in_lib(Main *bmain, std::optional< Library * > owner_library, short type, const char *name)
Definition lib_id.cc:1464
void BKE_library_make_local(Main *bmain, const Library *lib, GHash *old_to_new_ids, bool untagged_only, bool set_fake, bool clear_asset_data)
Definition lib_id.cc:2075
void id_fake_user_set(ID *id)
Definition lib_id.cc:389
ID * BKE_id_owner_get(ID *id, const bool debug_relationship_assert=true)
Definition lib_id.cc:2444
struct ID * BKE_id_copy_in_lib(Main *bmain, std::optional< Library * > owner_library, const ID *id, const ID *new_owner_id, ID **new_id_p, int flag)
Definition lib_id.cc:656
bool BKE_lib_id_make_local(Main *bmain, ID *id, int flags)
Definition lib_id.cc:584
void id_us_ensure_real(ID *id)
Definition lib_id.cc:306
void id_us_clear_real(ID *id)
Definition lib_id.cc:324
void BKE_lib_id_swap_full(Main *bmain, ID *id_a, ID *id_b, const bool do_self_remap, const int self_remap_flags)
Definition lib_id.cc:1028
@ LIB_ID_MAKELOCAL_INDIRECT
@ LIB_ID_MAKELOCAL_FORCE_LOCAL
@ LIB_ID_MAKELOCAL_LIBOVERRIDE_CLEAR
void id_us_min(ID *id)
Definition lib_id.cc:359
void BKE_libblock_management_main_add(Main *bmain, void *idv)
Definition lib_id.cc:1097
void BKE_main_id_refcount_recompute(Main *bmain, bool do_linked_only)
Definition lib_id.cc:1981
void BKE_lib_libblock_session_uid_renew(ID *id)
Definition lib_id.cc:1458
void BKE_lib_override_library_free(IDOverrideLibrary **liboverride, bool do_id_user)
void BKE_lib_override_library_main_resync(Main *bmain, Scene *scene, ViewLayer *view_layer, BlendFileReadReport *reports)
void BKE_lib_override_library_main_operations_create(Main *bmain, bool force_auto, int *r_report_flags)
@ IDWALK_RET_STOP_RECURSION
@ IDWALK_RET_NOP
void BKE_lib_query_unused_ids_tag(Main *bmain, int tag, LibQueryUnusedIDsData &parameters)
Definition lib_query.cc:962
@ IDWALK_CB_LOOPBACK
@ IDWALK_CB_USER
@ IDWALK_CB_INTERNAL
@ IDWALK_CB_EMBEDDED_NOT_OWNING
@ IDWALK_CB_EMBEDDED
@ IDWALK_CB_NEVER_NULL
void BKE_library_foreach_ID_link(Main *bmain, ID *id, blender::FunctionRef< LibraryIDLinkCallback > callback, void *user_data, int flag)
Definition lib_query.cc:416
@ IDWALK_RECURSE
@ IDWALK_INCLUDE_UI
@ IDWALK_DO_LIBRARY_POINTER
@ IDWALK_READONLY
@ IDWALK_DO_INTERNAL_RUNTIME_POINTERS
void BKE_libblock_remap_multiple_raw(Main *bmain, blender::bke::id::IDRemapper &mappings, const int remap_flags)
Definition lib_remap.cc:669
@ ID_REMAP_SKIP_USER_CLEAR
@ ID_REMAP_SKIP_USER_REFCOUNT
@ ID_REMAP_SKIP_NEVER_NULL_USAGE
@ ID_REMAP_FORCE_UI_POINTERS
@ ID_REMAP_SKIP_UPDATE_TAGGING
IDRemapperApplyResult
@ ID_REMAP_RESULT_SOURCE_REMAPPED
@ ID_REMAP_RESULT_SOURCE_UNASSIGNED
@ ID_REMAP_RESULT_SOURCE_NOT_MAPPABLE
@ ID_REMAP_RESULT_SOURCE_UNAVAILABLE
@ ID_REMAP_APPLY_DEFAULT
#define FOREACH_MAIN_ID_END
Definition BKE_main.hh:500
ListBase * which_libbase(Main *bmain, short type)
Definition main.cc:842
void BKE_main_clear(Main &bmain)
Definition main.cc:64
#define FOREACH_MAIN_LISTBASE_ID_END
Definition BKE_main.hh:469
#define FOREACH_MAIN_LISTBASE_ID_BEGIN(_lb, _id)
Definition BKE_main.hh:463
#define FOREACH_MAIN_LISTBASE_END
Definition BKE_main.hh:481
void BKE_main_destroy(Main &bmain)
Definition main.cc:166
void BKE_main_init(Main &bmain)
Definition main.cc:52
#define FOREACH_MAIN_LISTBASE_BEGIN(_bmain, _lb)
Definition BKE_main.hh:474
void BKE_main_free(Main *bmain)
Definition main.cc:175
#define FOREACH_MAIN_ID_BEGIN(_bmain, _id)
Definition BKE_main.hh:494
const char * BKE_main_blendfile_path(const Main *bmain) ATTR_NONNULL()
Definition main.cc:832
ID ID ID * BKE_main_idmap_lookup_uid(IDNameLib_Map *id_map, uint session_uid) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
IDNameLib_Map * BKE_main_idmap_create(Main *bmain, bool create_valid_ids_set, Main *old_bmain, int idmap_types) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition main_idmap.cc:77
@ MAIN_IDMAP_TYPE_UID
@ MAIN_IDMAP_TYPE_NAME
void BKE_main_idmap_remove_id(IDNameLib_Map *id_map, const ID *id) ATTR_NONNULL()
void BKE_main_idmap_destroy(IDNameLib_Map *id_map) ATTR_NONNULL()
void BKE_main_idmap_insert_id(IDNameLib_Map *id_map, ID *id) ATTR_NONNULL()
void BKE_main_idmap_clear(IDNameLib_Map &id_map)
ID ID * BKE_main_idmap_lookup_id(IDNameLib_Map *id_map, const ID *id) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
void BKE_main_namemap_remove_name(Main *bmain, ID *id, const char *name) ATTR_NONNULL()
void BKE_main_namemap_clear(Main *bmain) ATTR_NONNULL()
bool BKE_main_namemap_validate(Main *bmain) ATTR_NONNULL()
void BKE_preferences_asset_library_default_add(struct UserDef *userdef) ATTR_NONNULL()
void BKE_preferences_extension_repo_add_defaults_all(UserDef *userdef)
bool BKE_preferences_asset_shelf_settings_ensure_catalog_path_enabled(UserDef *userdef, const char *shelf_idname, const char *catalog_path)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
void BKE_reports_prependf(ReportList *reports, const char *prepend_format,...) ATTR_PRINTF_FORMAT(2
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:125
void void BKE_reports_prepend(ReportList *reports, const char *prepend)
Definition report.cc:205
Scene * BKE_scene_add(Main *bmain, const char *name)
Definition scene.cc:1954
void BKE_scene_set_background(Main *bmain, Scene *sce)
Definition scene.cc:1987
ScrArea ScrArea void BKE_screen_gizmo_tag_refresh(bScreen *screen)
Definition screen.cc:469
void BKE_screen_view3d_scene_sync(bScreen *screen, Scene *scene)
Definition screen.cc:963
void BKE_screen_runtime_refresh_for_blendfile(bScreen *screen)
Definition screen.cc:484
void BKE_studiolight_default(SolidLight lights[4], float light_ambient[3])
@ STEP_INVALID
bScreen * BKE_workspace_active_screen_get(const WorkSpaceInstanceHook *hook) GETTER_ATTRS
Definition workspace.cc:613
#define BLI_assert_unreachable()
Definition BLI_assert.h:97
#define BLI_assert(a)
Definition BLI_assert.h:50
#define BLI_assert_msg(a, msg)
Definition BLI_assert.h:57
File and directory operations.
int BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:350
bool BLI_is_file(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:438
bool BLI_is_dir(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:433
BLI_INLINE bool BLI_listbase_is_empty(const struct ListBase *lb)
void BLI_addhead(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:90
#define LISTBASE_FOREACH(type, var, list)
#define LISTBASE_FOREACH_BACKWARD(type, var, list)
bool BLI_remlink_safe(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:153
int BLI_listbase_count_at_most(const struct ListBase *listbase, int count_max) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:110
void BLI_remlink(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:130
int BLI_findindex(const struct ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
MINLINE int min_ii(int a, int b)
void void void const char * BLI_path_basename(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
bool BLI_path_extension_check_array(const char *path, const char **ext_array) ATTR_NONNULL(1
#define FILE_MAX
void BLI_path_slash_native(char *path) ATTR_NONNULL(1)
#define BLI_path_join(...)
int BLI_path_slash_ensure(char *path, size_t path_maxncpy) ATTR_NONNULL(1)
const char * BLI_path_slash_rfind(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
#define STRNCPY(dst, src)
Definition BLI_string.h:593
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
unsigned int uint
int BLI_system_memory_max_in_megabytes_int(void)
Definition system.c:201
Platform independent time functions.
double BLI_time_now_seconds(void)
Definition time.c:65
#define ARRAY_SIZE(arr)
#define UNUSED_VARS_NDEBUG(...)
#define ELEM(...)
#define STREQ(a, b)
void BLO_reportf_wrap(BlendFileReadReport *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
external readfile function prototypes.
#define BLO_GROUP_MAX
eBLOReadSkip
@ BLO_READ_SKIP_DATA
@ BLO_READ_SKIP_USERDEF
#define BLO_EMBEDDED_STARTUP_BLEND
BlendHandle * BLO_blendhandle_from_file(const char *filepath, BlendFileReadReport *reports)
void BLO_blendhandle_close(BlendHandle *bh) ATTR_NONNULL(1)
BlendFileData * BLO_read_from_memfile(Main *oldmain, const char *filepath, MemFile *memfile, const BlendFileReadParams *params, ReportList *reports)
BlendFileData * BLO_read_from_memory(const void *mem, int memsize, eBLOReadSkip skip_flags, ReportList *reports)
#define BLO_READ_SKIP_ALL
void BLO_update_defaults_startup_blend(Main *bmain, const char *app_template)
BlendFileData * BLO_read_from_file(const char *filepath, eBLOReadSkip skip_flags, BlendFileReadReport *reports)
void BLO_read_do_version_after_setup(Main *new_bmain, BlendfileLinkAppendContext *lapp_context, BlendFileReadReport *reports)
void BLO_blendfiledata_free(BlendFileData *bfd)
const UserDef U_default
const bTheme U_theme_default
external writefile.cc function prototypes.
bool BLO_write_file(Main *mainvar, const char *filepath, int write_flags, const BlendFileWriteParams *params, ReportList *reports)
eBLO_WritePathRemap
@ BLO_WRITE_PATH_REMAP_RELATIVE
#define RPT_(msgid)
void BPY_context_update(bContext *C)
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:182
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:181
#define CLOG_INFO(clg_ref, level,...)
Definition CLG_log.h:179
#define CLOG_FATAL(clg_ref,...)
Definition CLG_log.h:183
#define ID_TYPE_SUPPORTS_ASSET_EDITABLE(id_type)
Definition DNA_ID.h:656
#define ID_IS_OVERRIDE_LIBRARY_REAL(_id)
Definition DNA_ID.h:676
#define ID_IS_LINKED(_id)
Definition DNA_ID.h:654
#define MAX_ID_NAME
Definition DNA_ID.h:377
@ ID_FLAG_CLIPBOARD_MARK
Definition DNA_ID.h:750
@ ID_FLAG_EMBEDDED_DATA
Definition DNA_ID.h:725
@ ID_TAG_TEMP_MAIN
Definition DNA_ID.h:938
@ ID_TAG_MISSING
Definition DNA_ID.h:813
@ ID_TAG_DOIT
Definition DNA_ID.h:1003
#define INDEX_ID_MAX
Definition DNA_ID.h:1328
@ INDEX_ID_NULL
Definition DNA_ID.h:1325
@ LIBRARY_ASSET_EDITABLE
Definition DNA_ID.h:549
@ LIBRARY_ASSET_FILE_WRITABLE
Definition DNA_ID.h:551
@ ID_WM
@ ID_LI
@ ID_WS
@ ID_SCE
@ ID_SCR
@ RGN_TYPE_WINDOW
@ SPACE_VIEW3D
#define FILE_MAX_LIBEXTRA
@ USER_TR_NEWDATANAME
@ USER_TR_TOOLTIPS
@ USER_TR_IFACE
@ USER_SCRIPT_AUTOEXEC_DISABLE
@ ULANGUAGE_AUTO
#define USER_EXPERIMENTAL_TEST(userdef, member)
void IMB_colormanagement_check_file_config(Main *bmain)
Read Guarded memory(de)allocation.
#define MEM_SAFE_FREE(v)
void BKE_blendfile_read_setup_undo(bContext *C, BlendFileData *bfd, const BlendFileReadParams *params, BlendFileReadReport *reports)
static void view3d_data_consistency_ensure(wmWindow *win, Scene *scene, ViewLayer *view_layer)
Definition blendfile.cc:797
void BKE_blendfile_read_setup_readfile(bContext *C, BlendFileData *bfd, const BlendFileReadParams *params, BlendFileReadWMSetupData *wm_setup_data, BlendFileReadReport *reports, const bool startup_update_defaults, const char *startup_app_template)
bool BKE_blendfile_userdef_write_all(ReportList *reports)
static void wm_data_consistency_ensure(wmWindowManager *curwm, Scene *cur_scene, ViewLayer *cur_view_layer)
Definition blendfile.cc:850
static int swap_old_bmain_data_for_blendfile_dependencies_process_cb(LibraryIDLinkCallbackData *cb_data)
Definition blendfile.cc:676
static void handle_subversion_warning(Main *main, BlendFileReadReport *reports)
WorkspaceConfigFileData * BKE_blendfile_workspace_config_read(const char *filepath, const void *file_buf, int file_buf_size, ReportList *reports)
bool BKE_blendfile_userdef_write(const char *filepath, ReportList *reports)
static void setup_app_data(bContext *C, BlendFileData *bfd, const BlendFileReadParams *params, BlendFileReadWMSetupData *wm_setup_data, BlendFileReadReport *reports)
Definition blendfile.cc:883
static bool reuse_bmain_data_remapper_is_id_remapped(id::IDRemapper &remapper, ID *id)
Definition blendfile.cc:301
static void reuse_bmain_data_invalid_local_usages_fix(ReuseOldBMainData *reuse_data)
Definition blendfile.cc:768
void BKE_blendfile_read_make_empty(bContext *C)
static void swap_wm_data_for_blendfile(ReuseOldBMainData *reuse_data, const bool load_ui)
Definition blendfile.cc:620
bool BKE_blendfile_library_path_explode(const char *path, char *r_dir, char **r_group, char **r_name)
Definition blendfile.cc:89
bool BKE_blendfile_extension_check(const char *str)
Definition blendfile.cc:83
static bool reuse_editable_asset_needed(ReuseOldBMainData *reuse_data)
Definition blendfile.cc:449
static void swap_old_bmain_data_dependencies_process(ReuseOldBMainData *reuse_data, const short id_code)
Definition blendfile.cc:702
void BKE_blendfile_workspace_config_data_free(WorkspaceConfigFileData *workspace_config)
UserDef * BKE_blendfile_userdef_from_defaults()
static Library * reuse_bmain_data_dependencies_new_library_get(ReuseOldBMainData *reuse_data, Library *old_lib)
Definition blendfile.cc:362
static id::IDRemapper & reuse_bmain_data_remapper_ensure(ReuseOldBMainData *reuse_data)
Definition blendfile.cc:261
UserDef * BKE_blendfile_userdef_read(const char *filepath, ReportList *reports)
static void clean_paths(Main *bmain)
Definition blendfile.cc:181
BlendFileData * BKE_blendfile_read_from_memfile(Main *bmain, MemFile *memfile, const BlendFileReadParams *params, ReportList *reports)
static int reuse_bmain_data_invalid_local_usages_fix_cb(LibraryIDLinkCallbackData *cb_data)
Definition blendfile.cc:726
static void reuse_editable_asset_bmain_data_for_blendfile(ReuseOldBMainData *reuse_data, const short idcode)
Definition blendfile.cc:473
static void swap_old_bmain_data_for_blendfile(ReuseOldBMainData *reuse_data, const short id_code)
Definition blendfile.cc:547
UserDef * BKE_blendfile_userdef_read_from_memory(const void *file_buf, int file_buf_size, ReportList *reports)
BlendFileData * BKE_blendfile_read(const char *filepath, const BlendFileReadParams *params, BlendFileReadReport *reports)
BlendFileData * BKE_blendfile_read_from_memory(const void *file_buf, int file_buf_size, const BlendFileReadParams *params, ReportList *reports)
static void setup_app_blend_file_data(bContext *C, BlendFileData *bfd, const BlendFileReadParams *params, BlendFileReadWMSetupData *wm_setup_data, BlendFileReadReport *reports)
static bool wm_scene_is_visible(wmWindowManager *wm, Scene *scene)
Definition blendfile.cc:196
static bool foreach_path_clean_cb(BPathForeachPathData *, char *path_dst, size_t path_dst_maxncpy, const char *path_src)
Definition blendfile.cc:170
static bool reuse_bmain_move_id(ReuseOldBMainData *reuse_data, ID *id, Library *lib, const bool reuse_existing)
Definition blendfile.cc:315
bool BKE_blendfile_userdef_write_app_template(const char *filepath, ReportList *reports)
static CLG_LogRef LOG_PARTIALWRITE
bool BKE_blendfile_is_readable(const char *path, ReportList *reports)
Definition blendfile.cc:152
static void setup_app_userdef(BlendFileData *bfd)
Definition blendfile.cc:206
static void unpin_file_local_grease_pencil_brush_materials(const ReuseOldBMainData *reuse_data)
Definition blendfile.cc:519
static int reuse_editable_asset_bmain_data_dependencies_process_cb(LibraryIDLinkCallbackData *cb_data)
Definition blendfile.cc:398
unsigned int U
Definition btGjkEpa3.h:78
bool add(const Key &key, const Value &value)
Definition BLI_map.hh:271
const Value & lookup(const Key &key) const
Definition BLI_map.hh:506
Value lookup_default(const Key &key, const Value &default_value) const
Definition BLI_map.hh:531
bool contains(const Key &key) const
Definition BLI_set.hh:291
bool add(const Key &key)
Definition BLI_set.hh:248
constexpr const char * c_str() const
void append(const T &value)
void remove_unused(bool clear_extra_user=false)
ID * id_add(const ID *id, IDAddOptions options, blender::FunctionRef< IDAddOperations(LibraryIDLinkCallbackData *cb_data, IDAddOptions options)> dependencies_filter_cb=nullptr)
bool write(const char *write_filepath, int write_flags, int remap_mode, ReportList &reports)
ID * id_create(short id_type, StringRefNull id_name, Library *library, IDAddOptions options)
IDRemapperApplyResult get_mapping_result(ID *id, IDRemapperApplyOptions options, const ID *id_self) const
IDRemapperApplyResult apply(ID **r_id_ptr, IDRemapperApplyOptions options, ID *id_self=nullptr) const
void add(ID *old_id, ID *new_id)
void add_overwrite(ID *old_id, ID *new_id)
std::string id_name(void *id)
#define printf
CCL_NAMESPACE_BEGIN struct Options options
#define str(s)
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
#define GS(x)
Definition iris.cc:202
double parameters[NUM_PARAMETERS]
void *(* MEM_mallocN)(size_t len, const char *str)
Definition mallocn.cc:44
void MEM_freeN(void *vmemh)
Definition mallocn.cc:105
void *(* MEM_callocN)(size_t len, const char *str)
Definition mallocn.cc:42
#define G(x, y, z)
int main()
struct Base * next
unsigned short local_view_bits
UserDef * user
ViewLayer * cur_view_layer
char filepath[1024]
bScreen * curscreen
struct BlendFileReadReport::@127 duration
struct BlendFileReadReport::@128 count
wmWindowManager * old_wm
eBLO_WritePathRemap remap_mode
struct Material * material
struct BrushGpencilSettings * gpencil_settings
struct ID * reference
Definition DNA_ID.h:333
short id_code
Definition DNA_ID.h:413
int tag
Definition DNA_ID.h:434
struct Library * lib
Definition DNA_ID.h:419
IDOverrideLibrary * override_library
Definition DNA_ID.h:459
short flag
Definition DNA_ID.h:430
void * next
Definition DNA_ID.h:416
char name[66]
Definition DNA_ID.h:425
unsigned int session_uid
Definition DNA_ID.h:454
struct Library * parent
Definition DNA_ID.h:512
char filepath_abs[1024]
Definition DNA_ID.h:509
struct Library_Runtime runtime
Definition DNA_ID.h:535
ID id
Definition DNA_ID.h:529
void * last
void * first
ListBase brushes
Definition BKE_main.hh:235
ListBase scenes
Definition BKE_main.hh:210
ListBase wm
Definition BKE_main.hh:239
bool is_asset_edit_file
Definition BKE_main.hh:151
char filepath[1024]
Definition BKE_main.hh:136
bool recovered
Definition BKE_main.hh:158
ListBase libraries
Definition BKE_main.hh:211
IDNameLib_Map * id_map
Definition BKE_main.hh:263
Main * next
Definition BKE_main.hh:123
ListBase screens
Definition BKE_main.hh:225
short versionfile
Definition BKE_main.hh:137
ListBase workspaces
Definition BKE_main.hh:246
bool is_read_invalid
Definition BKE_main.hh:183
struct RegionView3D * localvd
IDNameLib_Map * id_map
Definition blendfile.cc:248
BlendFileReadWMSetupData * wm_setup_data
Definition blendfile.cc:239
id::IDRemapper * remapper
Definition blendfile.cc:243
struct ListBase addons
float light_ambient[3]
struct ListBase themes
char fontdir[768]
struct SolidLight light_param[4]
struct ColorBand coba_weight
struct Object * camera
struct View3D * localvd
short scenelock
unsigned short local_view_uid
ListBase object_bases
char name[64]
char module[128]
struct Scene * scene
struct WorkSpaceInstanceHook * workspace_hook
static DynamicLibrary lib