Blender V5.3
blendfile_link_append.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
13#include <algorithm>
14#include <cstdlib>
15#include <cstring>
16
18#include "CLG_log.h"
19
20#include "MEM_guardedalloc.h"
21
22#include "DNA_ID.h"
25#include "DNA_key_types.h"
26#include "DNA_object_types.h"
27#include "DNA_scene_types.h"
28#include "DNA_space_types.h"
29#include "DNA_userdef_types.h"
30
31#include "BLI_linklist.hh"
32#include "BLI_listbase.hh"
33#include "BLI_math_vector_c.hh"
34#include "BLI_set.hh"
35#include "BLI_stack.hh"
36#include "BLI_string.hh"
37#include "BLI_string_ref.hh"
38#include "BLI_utildefines.hh"
39#include "BLI_vector.hh"
40
41#include "BLT_translation.hh"
42
43#include "RNA_access.hh"
44#include "RNA_prototypes.hh"
45
46#include "BKE_callbacks.hh"
47#include "BKE_idtype.hh"
48#include "BKE_key.hh"
49#include "BKE_layer.hh"
50#include "BKE_lib_id.hh"
51#include "BKE_lib_override.hh"
52#include "BKE_lib_query.hh"
53#include "BKE_lib_remap.hh"
54#include "BKE_library.hh"
55#include "BKE_main.hh"
57#include "BKE_main_namemap.hh"
58#include "BKE_material.hh"
60#include "BKE_object.hh"
61#include "BKE_report.hh"
62#include "BKE_rigidbody.h"
63#include "BKE_scene.hh"
64
66
67#include "BLO_writefile.hh"
68
69namespace blender {
70
71static CLG_LogRef LOG = {"lib.link_append"};
72
73using namespace blender::bke;
74
75/* -------------------------------------------------------------------- */
78
101
103 BlendfileLinkAppendContext &lapp_context,
105 ReportList *reports)
106{
107 if (reports != nullptr) {
108 lib_context.bf_reports.reports = reports;
109 }
110
111 const StringRefNull libname = lib_context.path;
112 BlendHandle *blo_handle = lib_context.blo_handle;
113 if (blo_handle == nullptr) {
114 if (libname == BLO_EMBEDDED_STARTUP_BLEND) {
115 blo_handle = BLO_blendhandle_from_memory(lapp_context.blendfile_mem,
116 int(lapp_context.blendfile_memsize),
117 &lib_context.bf_reports);
118 }
119 else {
120 blo_handle = BLO_blendhandle_from_file(libname.c_str(), &lib_context.bf_reports);
121 }
122 lib_context.blo_handle = blo_handle;
123 lib_context.blo_handle_is_owned = true;
124 }
125
126 return blo_handle;
127}
128
130 BlendfileLinkAppendContext & /*lapp_context*/, BlendfileLinkAppendContextLibrary &lib_context)
131{
132 if (lib_context.blo_handle_is_owned && lib_context.blo_handle != nullptr) {
134 lib_context.blo_handle = nullptr;
135 }
136}
137
139{
140 BlendfileLinkAppendContext *lapp_context = MEM_new<BlendfileLinkAppendContext>(__func__);
141 lapp_context->params = params;
143 return lapp_context;
144}
145
147{
148 for (BlendfileLinkAppendContextLibrary &lib_context : lapp_context->libraries) {
149 link_append_context_library_blohandle_release(*lapp_context, lib_context);
150 }
151
152 BLI_assert(lapp_context->library_weak_reference_mapping == nullptr);
153
154 MEM_delete(lapp_context);
155}
156
158 const int flag,
159 const bool do_set)
160{
161 if (do_set) {
162 lapp_context->params->flag |= flag;
163 }
164 else {
165 lapp_context->params->flag &= ~flag;
166 }
167}
168
170 BlendfileLinkAppendContext *lapp_context, const void *blendfile_mem, int blendfile_memsize)
171{
172 BLI_assert_msg(lapp_context->blendfile_mem == nullptr,
173 "Please explicitly clear reference to an embedded blender memfile before "
174 "setting a new one");
175 lapp_context->blendfile_mem = blendfile_mem;
176 lapp_context->blendfile_memsize = size_t(blendfile_memsize);
177}
178
180 BlendfileLinkAppendContext *lapp_context)
181{
182 lapp_context->blendfile_mem = nullptr;
183 lapp_context->blendfile_memsize = 0;
184}
185
187 const char *libname,
188 BlendHandle *blo_handle)
189{
190 BLI_assert(lapp_context->items.empty());
192
193 BlendfileLinkAppendContextLibrary lib_context = {};
194
195 lib_context.path = libname;
196 lib_context.blo_handle = blo_handle;
197 /* Always steal the ownership on the blendfile handle, as it may be freed by readfile code in
198 * case of endianness conversion. */
199 lib_context.blo_handle_is_owned = true;
200
201 lapp_context->libraries.append(lib_context);
202}
203
205 BlendfileLinkAppendContext *lapp_context,
206 const char *idname,
207 const short idcode,
208 void *userdata)
209{
211
212 item.lapp_context = lapp_context;
213
214 item.name = idname;
215 item.idcode = idcode;
216 item.libraries = BitVector<>(lapp_context->libraries.size(), false);
217
218 item.new_id = nullptr;
220 item.userdata = userdata;
221
222 lapp_context->items.push_back(item);
223
224 return &lapp_context->items.back();
225}
226
228 BlendfileLinkAppendContext *lapp_context,
229 ReportList *reports,
230 const uint64_t id_types_filter,
231 const int library_index)
232{
234
235 int id_num = 0;
236 int id_code_iter = 0;
237 short id_code;
238
239 BlendfileLinkAppendContextLibrary &lib_context = lapp_context->libraries[library_index];
240 BlendHandle *blo_handle = link_append_context_library_blohandle_ensure(
241 *lapp_context, lib_context, reports);
242
243 if (blo_handle == nullptr) {
245 }
246
247 const bool use_assets_only = (lapp_context->params->flag & FILE_ASSETS_ONLY) != 0;
248
249 while ((id_code = BKE_idtype_idcode_iter_step(&id_code_iter))) {
250 if (!BKE_idtype_idcode_is_linkable(id_code) ||
251 (id_types_filter != 0 && (BKE_idtype_idcode_to_idfilter(id_code) & id_types_filter) == 0))
252 {
253 continue;
254 }
255
256 int id_names_num;
258 blo_handle, id_code, use_assets_only, &id_names_num);
259
260 for (LinkNode *link_next = nullptr; id_names_list != nullptr; id_names_list = link_next) {
261 link_next = id_names_list->next;
262
263 char *id_name = static_cast<char *>(id_names_list->link);
265 lapp_context, id_name, id_code, nullptr);
267 lapp_context, item, library_index);
268
269 MEM_delete(id_name);
270 MEM_delete(id_names_list);
271 }
272
273 id_num += id_names_num;
274 }
275
276 return id_num;
277}
278
280 BlendfileLinkAppendContext *lapp_context,
282 const int library_index)
283{
285 UNUSED_VARS_NDEBUG(lapp_context);
286 item->libraries[library_index].set();
287}
288
290{
291 return lapp_context->items.empty();
292}
293
299
307
310 ID *new_id)
311{
313 BLI_assert(item->new_id);
315 /* The only cases where library pointers should be different is when:
316 * - A linked ID is replaced by a matching local ID (append case).
317 * - A linked ID is replaced by a matching linked packed ID. */
318 BLI_assert(new_id->lib == item->new_id->lib ||
319 (ID_IS_LINKED(item->new_id) && !ID_IS_LINKED(new_id)) ||
320 (ID_IS_PACKED(new_id) && new_id->lib->archive_parent_library == item->new_id->lib));
321 BLI_assert(!lapp_context->new_id_to_item.contains(new_id));
322
323 lapp_context->new_id_to_item.remove(item->new_id);
324 item->new_id = new_id;
325 lapp_context->new_id_to_item.add(new_id, item);
326}
327
335
341
343 BlendfileLinkAppendContext *lapp_context,
344 FunctionRef<bool(BlendfileLinkAppendContext *lapp_context,
345 BlendfileLinkAppendContextItem *item)> callback_function,
347{
348 for (BlendfileLinkAppendContextItem &item : lapp_context->items) {
350 (item.tag & LINK_APPEND_TAG_INDIRECT) == 0)
351 {
352 continue;
353 }
355 (item.tag & LINK_APPEND_TAG_INDIRECT) != 0)
356 {
357 continue;
358 }
359
360 if (!callback_function(lapp_context, &item)) {
361 break;
362 }
363 }
364}
365
367{
369
370 PointerRNA ctx_ptr = RNA_pointer_create_discrete(nullptr, RNA_BlendImportContext, lapp_context);
371 PointerRNA *pointers[1] = {&ctx_ptr};
372 BKE_callback_exec(lapp_context->params->bmain, pointers, 1, BKE_CB_EVT_BLENDIMPORT_PRE);
373}
374
376{
377 BLI_assert(ELEM(lapp_context->process_stage,
382
383 /* Linking may have changed some data link status.
384 * Further more, some versioning code can leave some ID link status tags in invalid state, due to
385 * low-level manipulations of these instead of using API like `id_lib_extern`, which will be also
386 * fixed by this call. */
388
389 /* Tag node trees to update generated RNA with potentially updated session uid values from
390 * data-block defaults in interfaces. This is only necessary because RNA types were already
391 * generated before these data-blocks were local; theoretically that shouldn't be necessary. */
392 for (ID *id : lapp_context->new_id_to_item.keys()) {
393 if (id->id_type() == ID_NT) {
394 bNodeTree *ntree = id_cast<bNodeTree *>(id);
396 }
397 }
398
399 BKE_main_ensure_invariants(*lapp_context->params->bmain);
400
401 PointerRNA ctx_ptr = RNA_pointer_create_discrete(nullptr, RNA_BlendImportContext, lapp_context);
402 PointerRNA *pointers[1] = {&ctx_ptr};
403 BKE_callback_exec(lapp_context->params->bmain, pointers, 1, BKE_CB_EVT_BLENDIMPORT_POST);
404}
405
407
408/* -------------------------------------------------------------------- */
412
413/* Struct gathering all required data to handle instantiation of loose data-blocks. */
416
417 /* The collection in which to add loose collections/objects. */
419};
420
421static bool object_in_any_scene(Main *bmain, Object *ob)
422{
423 for (Scene &sce : bmain->scenes) {
424 /* #BKE_scene_has_object checks bases cache of the scenes' view-layer, not actual content of
425 * their collections. */
427 return true;
428 }
429 }
430
431 return false;
432}
433
442 LooseDataInstantiateContext *instantiate_context, Collection *collection)
443{
444 BlendfileLinkAppendContext *lapp_context = instantiate_context->lapp_context;
445 for (BlendfileLinkAppendContextItem &item : lapp_context->items) {
446 ID *item_id = item.new_id;
447 if (!item_id) {
448 continue;
449 }
450 if (item_id->id_type() == ID_OB) {
451 Object *item_ob = id_cast<Object *>(item_id);
452 if (item_ob->type == OB_EMPTY && item_ob->instance_collection == collection) {
453 return true;
454 }
455 }
456 else if (item_id->id_type() == ID_GR) {
457 Collection *item_col = id_cast<Collection *>(item_id);
458 if (BKE_collection_has_collection(item_col, collection)) {
459 return true;
460 }
461 }
462 }
463 return false;
464}
465
468{
469 BlendfileLinkAppendContext *lapp_context = instantiate_context->lapp_context;
470 /* In linking case, we always want to handle instantiation. */
471 if (lapp_context->params->flag & FILE_LINK) {
472 return item->new_id;
473 }
474
475 /* We consider that if we either kept it linked, or re-used already local data, instantiation
476 * status of those should not be modified. */
478 return nullptr;
479 }
480
481 ID *id = item->new_id;
482 if (id == nullptr) {
483 return nullptr;
484 }
485
487 return id;
488}
489
491 LooseDataInstantiateContext *instantiate_context)
492{
493
494 BlendfileLinkAppendContext *lapp_context = instantiate_context->lapp_context;
495 Main *bmain = instantiate_context->lapp_context->params->bmain;
496 Scene *scene = instantiate_context->lapp_context->params->context.scene;
497 ViewLayer *view_layer = instantiate_context->lapp_context->params->context.view_layer;
498
499 /* Find or add collection as needed. When `target_collection` is non-null, it is assumed to be
500 * editable. */
501 if (instantiate_context->target_collection == nullptr) {
502 auto add_instantiating_collection =
503 [&bmain, &lapp_context](Collection *parent_collection) -> Collection * {
504 if (lapp_context->params->flag & FILE_LINK) {
505 return BKE_collection_add(bmain, parent_collection, DATA_("Linked Data"));
506 }
507 return BKE_collection_add(bmain, parent_collection, DATA_("Appended Data"));
508 };
509
510 if (lapp_context->params->flag & FILE_ACTIVE_COLLECTION) {
513 view_layer, lc->collection);
514 /* In all 'sane' cases, `BKE_collection_parent_editable_find_recursive` should find a valid
515 * parent collection. This is only a minimal backup in case the link/append operation happens
516 * in a very weird, broken context. */
517 if (!instantiate_context->target_collection) {
518 instantiate_context->target_collection = add_instantiating_collection(nullptr);
519 }
520 }
521 else {
523 scene->master_collection) ?
524 scene->master_collection :
525 nullptr;
526 instantiate_context->target_collection = add_instantiating_collection(parent_collection);
527 }
528 }
529}
530
532 Collection *collection,
533 Object *ob,
534 const Scene *scene,
535 ViewLayer *view_layer,
536 const View3D *v3d,
537 const int flag,
538 bool set_active)
539{
540 /* Auto-select and appending. */
541 if ((flag & FILE_AUTOSELECT) && ((flag & FILE_LINK) == 0)) {
542 /* While in general the object should not be manipulated,
543 * when the user requests the object to be selected, ensure it's visible and selectable. */
545 }
546
547 BKE_collection_object_add(bmain, collection, ob);
548 BKE_view_layer_synced_ensure(*bmain, scene, view_layer);
549 Base *base = BKE_view_layer_base_find(view_layer, ob);
550
551 if (v3d != nullptr) {
552 base->local_view_bits |= v3d->local_view_uid;
553 }
554
555 if (flag & FILE_AUTOSELECT) {
556 /* All objects that use #FILE_AUTOSELECT must be selectable (unless linking data). */
557 BLI_assert((base->flag & BASE_SELECTABLE) || (flag & FILE_LINK));
558 if (base->flag & BASE_SELECTABLE) {
559 base->flag |= BASE_SELECTED;
560 }
561 }
562
563 if (set_active) {
564 view_layer->basact = base;
565 }
566
568}
569
570/* Tag obdata that actually need to be instantiated (those referenced by an object do not, since
571 * the object will be instantiated instead if needed). */
573 LooseDataInstantiateContext *instantiate_context)
574{
575 BlendfileLinkAppendContext *lapp_context = instantiate_context->lapp_context;
576
577 /* First pass on obdata to enable their instantiation by default, then do a second pass on
578 * objects to clear it for any obdata already in use. */
579 for (BlendfileLinkAppendContextItem &item : lapp_context->items) {
580 ID *id = loose_data_instantiate_process_check(instantiate_context, &item);
581 if (id == nullptr) {
582 continue;
583 }
584 const ID_Type idcode = id->id_type();
585 if (!OB_DATA_SUPPORT_ID(idcode)) {
586 continue;
587 }
588 if (idcode == ID_GD_LEGACY) {
589 const bGPdata *legacy_gpd = reinterpret_cast<bGPdata *>(id);
590 if ((legacy_gpd->flag & GP_DATA_ANNOTATIONS) != 0) {
591 continue;
592 }
593 }
594
595 id->tag |= ID_TAG_DOIT;
596 }
597 for (BlendfileLinkAppendContextItem &item : lapp_context->items) {
598 ID *id = item.new_id;
599 if (id == nullptr || id->id_type() != ID_OB) {
600 continue;
601 }
602
603 Object *ob = reinterpret_cast<Object *>(id);
604 Object *new_ob = reinterpret_cast<Object *>(id->newid);
605 if (ob->data != nullptr) {
606 ob->data->tag &= ~ID_TAG_DOIT;
607 }
608 if (new_ob != nullptr && new_ob->data != nullptr) {
609 new_ob->data->tag &= ~ID_TAG_DOIT;
610 }
611 }
612}
613
614/* Test whether some ancestor collection is also tagged for instantiation (return true) or not
615 * (return false). */
617{
618 for (CollectionParent *parent_collection = collection->runtime->parents.first();
619 parent_collection != nullptr;
620 parent_collection = parent_collection->next)
621 {
622 if ((parent_collection->collection->id.tag & ID_TAG_DOIT) != 0) {
623 return true;
624 }
625 if (loose_data_instantiate_collection_parents_check_recursive(parent_collection->collection)) {
626 return true;
627 }
628 }
629 return false;
630}
631
633 LooseDataInstantiateContext *instantiate_context)
634{
635 BlendfileLinkAppendContext *lapp_context = instantiate_context->lapp_context;
636 Main *bmain = lapp_context->params->bmain;
637 Scene *scene = lapp_context->params->context.scene;
638 ViewLayer *view_layer = lapp_context->params->context.view_layer;
639 const View3D *v3d = lapp_context->params->context.v3d;
640
641 const bool do_append = (lapp_context->params->flag & FILE_LINK) == 0;
642 const bool do_instantiate_as_empty = (lapp_context->params->flag &
644
645 /* NOTE: For collections we only view_layer-instantiate duplicated collections that have
646 * non-instantiated objects in them.
647 * NOTE: Also avoid view-layer-instantiating of collections children of other instantiated
648 * collections. This is why we need two passes here. */
649 for (BlendfileLinkAppendContextItem &item : lapp_context->items) {
650 ID *id = loose_data_instantiate_process_check(instantiate_context, &item);
651 if (id == nullptr || id->id_type() != ID_GR) {
652 continue;
653 }
654
655 /* Forced instantiation of indirectly appended collections is not wanted. Users can now
656 * easily instantiate collections (and their objects) as needed by themselves. See #67032. */
657 /* We need to check that objects in that collections are already instantiated in a scene.
658 * Otherwise, it's better to add the collection to the scene's active collection, than to
659 * instantiate its objects in active scene's collection directly. See #61141.
660 *
661 * NOTE: We only check object directly into that collection, not recursively into its
662 * children.
663 */
664 Collection *collection = id_cast<Collection *>(id);
665 /* The collection could be linked/appended together with an Empty object instantiating it,
666 * better not instantiate the collection in the view-layer in that case.
667 *
668 * Can easily happen when copy/pasting such instantiating empty, see #93839. */
669 const bool collection_is_instantiated = is_collection_instantiated_by_other_link_append_data(
670 instantiate_context, collection);
671 /* Always consider adding collections directly selected by the user. */
672 bool do_add_collection = (item.tag & LINK_APPEND_TAG_INDIRECT) == 0 &&
673 !collection_is_instantiated;
674 /* In linking case, do not enforce instantiating non-directly linked collections/objects.
675 * This avoids cluttering the view-layers, user can instantiate themselves specific collections
676 * or objects easily from the Outliner if needed. */
677 if (!do_add_collection && do_append && !collection_is_instantiated) {
678 for (CollectionObject &coll_ob : collection->gobject) {
679 Object *ob = coll_ob.ob;
680 if (!object_in_any_scene(bmain, ob)) {
681 do_add_collection = true;
682 break;
683 }
684 }
685 }
686 if (do_add_collection) {
687 collection->id.tag |= ID_TAG_DOIT;
688 }
689 }
690
691 /* Second loop to actually instantiate collections tagged as such in first loop, unless some of
692 * their ancestor is also instantiated in case this is not an empty-instantiation. */
693 for (BlendfileLinkAppendContextItem &item : lapp_context->items) {
694 ID *id = loose_data_instantiate_process_check(instantiate_context, &item);
695 if (id == nullptr || id->id_type() != ID_GR) {
696 continue;
697 }
698
699 Collection *collection = reinterpret_cast<Collection *>(id);
700 bool do_add_collection = (id->tag & ID_TAG_DOIT) != 0;
701
702 if (!do_add_collection) {
703 continue;
704 }
705 /* When instantiated into view-layer, do not add collections if one of their parents is also
706 * instantiated. */
707 if (!do_instantiate_as_empty &&
709 {
710 continue;
711 }
712 /* When instantiated as empty, do not add indirectly linked (i.e. non-user-selected)
713 * collections. */
714 if (do_instantiate_as_empty && (item.tag & LINK_APPEND_TAG_INDIRECT) != 0) {
715 continue;
716 }
717
719 Collection *target_collection = instantiate_context->target_collection;
720
721 if (do_instantiate_as_empty) {
722 /* BKE_object_add(...) messes with the selection. */
723 Object *ob = BKE_object_add_only_object(bmain, OB_EMPTY, collection->id.name + 2);
724 ob->type = OB_EMPTY;
725 ob->empty_drawsize = U.collection_instance_empty_size;
726
727 const bool set_selected = (lapp_context->params->flag & FILE_AUTOSELECT) != 0;
728 /* TODO: why is it OK to make this active here but not in other situations?
729 * See other callers of #object_base_instance_init */
730 const bool set_active = set_selected;
732 target_collection,
733 ob,
734 scene,
735 view_layer,
736 v3d,
737 lapp_context->params->flag,
738 set_active);
739
740 /* Assign the collection. */
741 ob->instance_collection = collection;
742 id_us_plus(&collection->id);
744 copy_v3_v3(ob->loc, scene->cursor.location);
745 }
746 else {
747 /* Add collection as child of target collection. */
748 BKE_collection_child_add(bmain, target_collection, collection);
749 BKE_view_layer_synced_ensure(*bmain, scene, view_layer);
750
751 if ((lapp_context->params->flag & FILE_AUTOSELECT) != 0) {
752 /* All objects contained in this collection need to be processed, including the ones
753 * belonging to children collections. */
755 Base *base = BKE_view_layer_base_find(view_layer, ob);
756 if (base) {
757 base->flag |= BASE_SELECTED;
759 }
760 }
762 }
763 }
764 }
765}
766
768 LooseDataInstantiateContext &instantiate_context,
769 const Scene &scene,
770 ViewLayer &view_layer,
771 Set<Object *> &r_instanciated_objects)
772{
774 *instantiate_context.lapp_context->params->bmain, &scene, &view_layer);
775
776 Stack<Collection *> instance_collections;
777 Set<Collection *> known_instance_collections;
778
780 instantiate_context.lapp_context->params->bmain, &scene, &view_layer, ob_iter)
781 {
782 r_instanciated_objects.add(ob_iter);
783 Collection *instance_collection = ob_iter->instance_collection;
784 if (instance_collection && !known_instance_collections.contains(instance_collection)) {
785 instance_collections.push_as(instance_collection);
786 known_instance_collections.add_new(instance_collection);
787 }
788 }
790
791 /* Instanced collections may instance other collections. So we need to accumulate and process all
792 * these instanced collections recursively. */
793 while (!instance_collections.is_empty()) {
794 Collection *instance_collection = instance_collections.pop();
795 BLI_assert(known_instance_collections.contains(instance_collection));
796 FOREACH_COLLECTION_OBJECT_RECURSIVE_BEGIN (instance_collection, ob_coll_iter) {
797 r_instanciated_objects.add(ob_coll_iter);
798 if (ob_coll_iter->instance_collection &&
799 !known_instance_collections.contains(ob_coll_iter->instance_collection))
800 {
801 instance_collections.push_as(ob_coll_iter->instance_collection);
802 known_instance_collections.add_new(ob_coll_iter->instance_collection);
803 }
804 }
806 }
807}
808
810 LooseDataInstantiateContext &instantiate_context, Set<Object *> &instanciated_objects)
811{
812 BlendfileLinkAppendContext *lapp_context = instantiate_context.lapp_context;
813 const Scene *scene = lapp_context->params->context.scene;
814 ViewLayer *view_layer = lapp_context->params->context.view_layer;
815
816 /* Linked/appended objects only need to be instantiated if they are not already in the current
817 * view layer, either:
818 * - Directly instantiated there (i.e. in one of the view layer instantiated collections).
819 * - Indirectly instanciated (i.e. being in a collection that is object-instanciated).
820 */
822 instantiate_context, *scene, *view_layer, instanciated_objects);
823
824 /* When linking or appending a whole Scene, typically its objects are already instantiated there,
825 * so no need to instantiate them in the active Scene.
826 *
827 * See also #153986. */
828 for (BlendfileLinkAppendContextItem &item : lapp_context->items) {
829 if (!item.new_id) {
830 continue;
831 }
832 if (item.new_id->id_type() != ID_SCE) {
833 continue;
834 }
835 Scene &scene_iter = *id_cast<Scene *>(item.new_id);
836 for (ViewLayer &view_layer_iter : scene_iter.view_layers) {
838 instantiate_context, scene_iter, view_layer_iter, instanciated_objects);
839 }
840 }
841}
842
844{
845 BlendfileLinkAppendContext *lapp_context = instantiate_context->lapp_context;
846 Main *bmain = lapp_context->params->bmain;
847 const Scene *scene = lapp_context->params->context.scene;
848 ViewLayer *view_layer = lapp_context->params->context.view_layer;
849 const View3D *v3d = lapp_context->params->context.v3d;
850
851 const bool do_object_active_done = (lapp_context->params->flag &
853
854 /* Do NOT make base active here! screws up GUI stuff,
855 * if you want it do it at the editor level (unless `do_object_active_done` is set). */
856 bool object_set_active = false;
857
858 const bool is_linking = (lapp_context->params->flag & FILE_LINK) != 0;
859
860 Set<Object *> instanciated_objects;
861 loose_data_gather_instanciated_objects(*instantiate_context, instanciated_objects);
862
863 /* NOTE: For objects we only view_layer-instantiate duplicated objects that are not yet used
864 * anywhere. */
865 for (BlendfileLinkAppendContextItem &item : lapp_context->items) {
866 ID *id = loose_data_instantiate_process_check(instantiate_context, &item);
867 if (id == nullptr || id->id_type() != ID_OB) {
868 continue;
869 }
870
871 /* In linking case, never instantiate stray objects that are not directly linked.
872 *
873 * While this is not ideal (in theory no object should remain un-owned), in case of indirectly
874 * linked objects, the other solution would be to add them to a local collection, which would
875 * make them directly linked. Think for now keeping them indirectly linked is more important.
876 * Ref. #93757.
877 */
878 if (is_linking && (item.tag & LINK_APPEND_TAG_INDIRECT) != 0) {
879 continue;
880 }
881
882 Object *ob = id_cast<Object *>(id);
883
884 if (instanciated_objects.contains(ob)) {
885 continue;
886 }
887
889 Collection *target_collection = instantiate_context->target_collection;
890
891 CLAMP_MIN(ob->id.us, 0);
892 ob->mode = OB_MODE_OBJECT;
893
894 object_set_active = do_object_active_done && (ob->flag & OB_FLAG_ACTIVE_CLIPBOARD);
895
897 target_collection,
898 ob,
899 scene,
900 view_layer,
901 v3d,
902 lapp_context->params->flag,
903 object_set_active);
904
905 /* Instancing an object may also instance implicitly others, so we need to update the set
906 * every time. */
907 loose_data_gather_instanciated_objects(*instantiate_context, instanciated_objects);
908 }
909}
910
912{
913 BlendfileLinkAppendContext *lapp_context = instantiate_context->lapp_context;
914 Main *bmain = lapp_context->params->bmain;
915 Scene *scene = lapp_context->params->context.scene;
916 ViewLayer *view_layer = lapp_context->params->context.view_layer;
917 const View3D *v3d = lapp_context->params->context.v3d;
918
919 /* Do NOT make base active here! screws up GUI stuff,
920 * if you want it do it at the editor level. */
921 const bool object_set_active = false;
922
923 for (BlendfileLinkAppendContextItem &item : lapp_context->items) {
924 ID *id = loose_data_instantiate_process_check(instantiate_context, &item);
925 if (id == nullptr) {
926 continue;
927 }
928 const ID_Type idcode = id->id_type();
929 if (!OB_DATA_SUPPORT_ID(idcode)) {
930 continue;
931 }
932 if ((id->tag & ID_TAG_DOIT) == 0) {
933 continue;
934 }
935
937 Collection *target_collection = instantiate_context->target_collection;
938
939 const int type = BKE_object_obdata_to_type(id);
940 BLI_assert(type != -1);
941 Object *ob = BKE_object_add_only_object(bmain, ObjectType(type), id->name + 2);
942 ob->data = id;
943 id_us_plus(id);
945
947 target_collection,
948 ob,
949 scene,
950 view_layer,
951 v3d,
952 lapp_context->params->flag,
953 object_set_active);
954
955 copy_v3_v3(ob->loc, scene->cursor.location);
956
957 id->tag &= ~ID_TAG_DOIT;
958 }
959}
960
962 LooseDataInstantiateContext *instantiate_context)
963{
964 BlendfileLinkAppendContext *lapp_context = instantiate_context->lapp_context;
965 Main *bmain = lapp_context->params->bmain;
966
967 /* Add rigid body objects and constraints to current RB world(s). */
968 for (BlendfileLinkAppendContextItem &item : lapp_context->items) {
969 ID *id = loose_data_instantiate_process_check(instantiate_context, &item);
970 if (id == nullptr || id->id_type() != ID_OB) {
971 continue;
972 }
973 BKE_rigidbody_ensure_local_object(bmain, reinterpret_cast<Object *>(id));
974 }
975}
976
978{
979 if (instantiate_context->lapp_context->params->context.scene == nullptr) {
980 /* In some cases, like the asset drag&drop e.g., the caller code manages instantiation itself.
981 */
982 return;
983 }
984
985 BlendfileLinkAppendContext *lapp_context = instantiate_context->lapp_context;
986 const bool do_obdata = (lapp_context->params->flag & BLO_LIBLINK_OBDATA_INSTANCE) != 0;
987
988 /* First pass on obdata to enable their instantiation by default, then do a second pass on
989 * objects to clear it for any obdata already in use. */
990 if (do_obdata) {
991 loose_data_instantiate_obdata_preprocess(instantiate_context);
992 }
993
994 /* First do collections, then objects, then obdata. */
996 loose_data_instantiate_object_process(instantiate_context);
997 if (do_obdata) {
998 loose_data_instantiate_obdata_process(instantiate_context);
999 }
1000
1002
1003 BKE_main_id_tag_all(lapp_context->params->bmain, ID_TAG_DOIT, false);
1004}
1005
1007 ID *id,
1009{
1010 lapp_context.new_id_to_item.add(id, &item);
1011}
1012
1013/* Generate a mapping between newly linked IDs and their items, and tag linked IDs used as
1014 * liboverride references as already existing. */
1016{
1017 for (BlendfileLinkAppendContextItem &item : lapp_context.items) {
1018 ID *id = item.new_id;
1019 if (id == nullptr) {
1020 continue;
1021 }
1022
1023 new_id_to_item_mapping_add(lapp_context, id, item);
1024 }
1025}
1026
1027/* All callbacks processing dependencies of an ID for link/append post-processing share a same
1028 * common logic to skip some cases. This is factorized in this helper function.
1029 *
1030 * Returns false if further processing should be skipped. */
1033{
1036 {
1037 return false;
1038 }
1039
1040 ID *id = *cb_data->id_pointer;
1041 if (id == nullptr) {
1042 return false;
1043 }
1044 if (!ID_IS_LINKED(id)) {
1045 CLOG_ERROR(
1046 &LOG,
1047 "Local ID '%s' found as part of the linked data hierarchy, this should never happen",
1048 id->name);
1049 return false;
1050 }
1051
1052 if (!BKE_idtype_idcode_is_linkable(id->id_type())) {
1053 /* While we do not want to add non-linkable ID (shape keys...) to the list of linked items,
1054 * unfortunately they can use fully linkable valid IDs too, like actions. Those need to be
1055 * processed, so we need to recursively deal with them here. */
1056 /* NOTE: Since we are bypassing checks in `BKE_library_foreach_ID_link` by manually calling it
1057 * recursively, we need to take care of potential recursion cases ourselves (e.g.anim-data of
1058 * shape-key referencing the shape-key itself). */
1059 /* NOTE: in case both IDs (owner and 'used' ones) are non-linkable, we can assume we can break
1060 * the dependency here. Indeed, either they are both linked in another way (through their own
1061 * meshes for shape keys e.g.), or this is an unsupported case (two shape-keys depending on
1062 * each-other need to be also 'linked' in by their respective meshes, independent shape-keys
1063 * are not allowed). ref #96048. */
1064 if (id != cb_data->self_id && BKE_idtype_idcode_is_linkable(cb_data->self_id->id_type())) {
1065 BKE_library_foreach_ID_link(cb_data->bmain, id, callback, cb_data->user_data, IDWALK_NOP);
1066 }
1067 return false;
1068 }
1069
1070 return true;
1071}
1072
1074
1075/* -------------------------------------------------------------------- */
1078
1080{
1081 Main *bmain = lapp_context->params->bmain;
1082
1083 new_id_to_item_mapping_create(*lapp_context);
1084
1085 /* Find all newly linked data-blocks, these will need to be deleted after they have been
1086 * successfully packed, to avoid keeping lots of unused linked IDs around.
1087 *
1088 * Also add them to the items list, such that they can be checked, and removed from the deletion
1089 * set in case packing fails. */
1090 Set<ID *> linked_ids_to_delete;
1091
1092 ID *id_iter;
1093 FOREACH_MAIN_ID_BEGIN (bmain, id_iter) {
1094 if (!ID_IS_LINKED(id_iter) || ID_IS_PACKED(id_iter) ||
1095 (id_iter->tag & ID_TAG_PRE_EXISTING) != 0)
1096 {
1097 continue;
1098 }
1099
1100 linked_ids_to_delete.add(id_iter);
1101
1102 BlendfileLinkAppendContextItem *item = lapp_context->new_id_to_item.lookup_default(id_iter,
1103 nullptr);
1104 if (item == nullptr) {
1106 lapp_context, BKE_id_name(*id_iter), id_iter->id_type(), nullptr);
1107 item->new_id = id_iter;
1108 item->source_library = id_iter->lib;
1109 /* Since we did not have an item for that ID yet, we know user did not select it
1110 * explicitly, it was rather linked indirectly. This info is important for
1111 * instantiation of collections.
1112 */
1115 new_id_to_item_mapping_add(*lapp_context, id_iter, *item);
1116 }
1117 }
1119
1120 for (BlendfileLinkAppendContextItem &item : lapp_context->items) {
1121 ID *id = item.new_id;
1122 if (id == nullptr) {
1123 continue;
1124 }
1126 /* Shape keys should always have been already packed by their owner ID.
1127 *
1128 * This might not always be true, in case the owner of the shapekey failed to be packed for
1129 * some reason, in that case do not attempt to pack the shapekey either. */
1130 if (id->id_type() != ID_KE && !(ID_IS_PACKED(id) || (id->newid && ID_IS_PACKED(id->newid)))) {
1131 /* No yet packed. */
1132 Set<ID *> ids_to_pack = bke::library::pack_linked_id_hierarchy(*bmain, *id);
1133
1134 /* If packing failed, do not delete the linked versions of the whole hierarchy. Packing is
1135 * all or nothing, but IDs shared with another hierarchy may still have been packed with
1136 * that one, and remain used by this linked hierarchy. */
1137 if (!id->newid || !ID_IS_PACKED(id->newid)) {
1138 for (ID *id_iter : ids_to_pack) {
1139 linked_ids_to_delete.remove(id_iter);
1140 if (Key *key = BKE_key_from_id(id_iter)) {
1141 linked_ids_to_delete.remove(&key->id);
1142 }
1143 }
1144 }
1145 }
1146 /* Calling code may want to access newly packed embedded IDs from the link/append context
1147 * items. */
1148 if (id->newid) {
1149 BKE_blendfile_link_append_context_item_newid_set(lapp_context, &item, id->newid);
1150 }
1151 }
1153
1154 BKE_id_multi_delete(bmain, linked_ids_to_delete);
1155}
1156
1158
1159/* -------------------------------------------------------------------- */
1162
1164{
1167 {
1168 return IDWALK_RET_NOP;
1169 }
1170 ID *id = *cb_data->id_pointer;
1172 static_cast<BlendfileLinkAppendContextCallBack *>(cb_data->user_data);
1173
1174 /* NOTE: In append case, all dependencies are needed in the items list, to cover potential
1175 * complex cases (e.g. linked data from another library referencing other IDs from the main
1176 * appended library). */
1177
1178 BlendfileLinkAppendContextItem *item = data->lapp_context->new_id_to_item.lookup_default(
1179 id, nullptr);
1180 if (item == nullptr) {
1182 data->lapp_context, BKE_id_name(*id), id->id_type(), nullptr);
1183 item->new_id = id;
1184 item->source_library = id->lib;
1185 /* Since we did not have an item for that ID yet, we know user did not select it explicitly,
1186 * it was rather linked indirectly. This info is important for instantiation of collections.
1187 */
1190 new_id_to_item_mapping_add(*data->lapp_context, id, *item);
1191
1192 if ((cb_data->cb_flag & IDWALK_CB_OVERRIDE_LIBRARY_REFERENCE) != 0 ||
1193 data->is_liboverride_dependency_only)
1194 {
1195 /* New item, (currently) detected as only used as a liboverride linked dependency. */
1197 }
1198 else if (data->is_liboverride_dependency) {
1199 /* New item, (currently) detected as used as a liboverride linked dependency, among
1200 * others. */
1202 }
1203 }
1204 else {
1205 if ((cb_data->cb_flag & IDWALK_CB_OVERRIDE_LIBRARY_REFERENCE) != 0 ||
1206 data->is_liboverride_dependency_only)
1207 {
1208 /* Existing item, here only used as a liboverride reference dependency. If it was not
1209 * tagged as such before, it is also used by non-liboverride reference data. */
1212 }
1213 }
1214 else if ((item->tag & LINK_APPEND_TAG_LIBOVERRIDE_DEPENDENCY_ONLY) != 0) {
1215 /* Existing item, here used in a non-liboverride dependency context. If it was
1216 * tagged as a liboverride dependency only, its tag and action need to be updated. */
1219 }
1220 }
1221
1222 return IDWALK_RET_NOP;
1223}
1224
1227{
1230 {
1231 return IDWALK_RET_NOP;
1232 }
1233 ID *id = *cb_data->id_pointer;
1235 static_cast<BlendfileLinkAppendContextCallBack *>(cb_data->user_data);
1236
1237 if (!data->item->reusable_local_id) {
1238 return IDWALK_RET_NOP;
1239 }
1240
1241 BlendfileLinkAppendContextItem *item = data->lapp_context->new_id_to_item.lookup(id);
1242 BLI_assert(item != nullptr);
1243
1244 /* If the currently processed owner ID is not defined as being kept linked, and is using a
1245 * dependency that cannot be reused form local data, then the owner ID should not reuse its
1246 * local data either. */
1247 if (item->action != LINK_APPEND_ACT_KEEP_LINKED && item->reusable_local_id == nullptr) {
1248 BKE_main_library_weak_reference_remove_item(data->lapp_context->library_weak_reference_mapping,
1249 cb_data->owner_id->lib->filepath,
1250 cb_data->owner_id->name,
1251 data->item->reusable_local_id);
1252 data->item->reusable_local_id = nullptr;
1253 }
1254
1255 return IDWALK_RET_NOP;
1256}
1257
1259{
1262 {
1263 return IDWALK_RET_NOP;
1264 }
1265 ID *id = *cb_data->id_pointer;
1267 cb_data->user_data);
1268
1269 BlendfileLinkAppendContextItem *item = data->lapp_context->new_id_to_item.lookup(id);
1270 BLI_assert(item != nullptr);
1272
1273 if (item->action == LINK_APPEND_ACT_MAKE_LOCAL) {
1274 CLOG_DEBUG(&LOG,
1275 "Appended ID '%s' was to be made directly local, but is also used by data that is "
1276 "kept linked, so duplicating it instead.",
1277 id->name);
1279 }
1280 return IDWALK_RET_NOP;
1281}
1282
1284 ReportList *reports)
1285{
1286 Main *bmain = lapp_context.params->bmain;
1287
1288 const bool do_recursive = (lapp_context.params->flag & BLO_LIBLINK_APPEND_RECURSIVE) != 0;
1289 const bool do_reuse_local_id = (lapp_context.params->flag & BLO_LIBLINK_APPEND_LOCAL_ID_REUSE) !=
1290 0;
1291
1292 /* In case of non-recursive appending, gather a set of all 'original' libraries (i.e. libraries
1293 * containing data that was explicitly selected by the user). */
1294 Set<Library *> direct_libraries;
1295 if (!do_recursive) {
1296 for (BlendfileLinkAppendContextItem &item : lapp_context.items) {
1297 ID *id = item.new_id;
1298 if (id == nullptr) {
1299 continue;
1300 }
1301 direct_libraries.add(id->lib);
1302 }
1303 }
1304
1305 /* Add items for all not yet known IDs (i.e. implicitly linked indirect dependencies) to the
1306 * list.
1307 * NOTE: Since items are appended, this list will grow and these IDs will be processed later,
1308 * leading to a flatten recursive processing of all the linked dependencies.
1309 */
1310 for (BlendfileLinkAppendContextItem &item : lapp_context.items) {
1311 ID *id = item.new_id;
1312 if (id == nullptr) {
1313 continue;
1314 }
1315 BLI_assert(item.reusable_local_id == nullptr);
1316
1317 /* NOTE: handling of reusable local ID info is needed, even if their usage is not requested
1318 * for that append operation:
1319 * - Newly appended data need to get their weak reference, such that it can be reused later
1320 * if requested.
1321 * - Existing appended data may need to get this 'reuse' weak reference cleared, e.g. if a
1322 * new version of it is made local. */
1325 lapp_context.library_weak_reference_mapping,
1326 id->lib->filepath,
1327 id->name) :
1328 nullptr;
1329
1331 cb_data.lapp_context = &lapp_context;
1332 cb_data.item = &item;
1333 cb_data.reports = reports;
1335 cb_data.is_liboverride_dependency_only = (item.tag &
1339 }
1340
1341 /* At this point, linked IDs that should remain linked can already be defined as such:
1342 * - In case of non-recursive appending, IDs from other libraries.
1343 * - IDs only used as liboverride references. */
1344 for (BlendfileLinkAppendContextItem &item : lapp_context.items) {
1345 /* These tags should have been set in above loop, here they can be check for validity (they
1346 * are mutually exclusive). */
1347 BLI_assert(
1348 (item.tag &
1351
1352 ID *id = item.new_id;
1353 if (id == nullptr) {
1354 continue;
1355 }
1356 /* IDs exclusively used as liboverride reference should not be made local at all. */
1358 CLOG_DEBUG(
1359 &LOG,
1360 "Appended ID '%s' is only used as a liboverride linked dependency, keeping it linked.",
1361 id->name);
1363 item.reusable_local_id = nullptr;
1364 }
1365 /* In non-recursive append case, only IDs from the same libraries as the directly appended
1366 * ones are made local. All dependencies from other libraries are kept linked. */
1367 if (!do_recursive && !direct_libraries.contains(id->lib)) {
1368 CLOG_DEBUG(&LOG,
1369 "Appended ID '%s' belongs to another library and recursive append is disabled, "
1370 "keeping it linked.",
1371 id->name);
1373 item.reusable_local_id = nullptr;
1374 }
1375 }
1376
1377 /* The reusable local IDs can cause severe issues in hierarchies of appended data. If an ID
1378 * user e.g. still has a local reusable ID found, but one of its dependencies does not (i.e.
1379 * either there were some changes in the library data, or the previously appended local
1380 * dependencies was modified in current file and therefore cannot be re-used anymore), then the
1381 * user ID should not be considered as usable either. */
1382 /* TODO: This process is currently fairly raw and inefficient. This is likely not a
1383 * (significant) issue currently anyway. But would be good to refactor this whole code to use
1384 * modern CPP containers (list of items could be an `std::deque` e.g., to be iterable in both
1385 * directions). Being able to loop backward here (i.e. typically process the dependencies
1386 * before the user IDs) could avoid a lot of iterations. */
1387 for (bool keep_looping = do_reuse_local_id; keep_looping;) {
1388 keep_looping = false;
1389 for (BlendfileLinkAppendContextItem &item : lapp_context.items) {
1390 ID *id = item.new_id;
1391 if (id == nullptr) {
1392 continue;
1393 }
1394 if (!item.reusable_local_id) {
1395 continue;
1396 }
1398 cb_data.lapp_context = &lapp_context;
1399 cb_data.item = &item;
1400 cb_data.reports = reports;
1402 cb_data.is_liboverride_dependency_only = (item.tag &
1405 id,
1407 &cb_data,
1408 IDWALK_NOP);
1409 if (!item.reusable_local_id) {
1410 /* If some reusable ID was cleared, another loop over all items is needed to potentially
1411 * propagate this change higher in the dependency hierarchy. */
1412 keep_looping = true;
1413 }
1414 }
1415 }
1416
1417 for (BlendfileLinkAppendContextItem &item : lapp_context.items) {
1418 ID *id = item.new_id;
1419 if (id == nullptr) {
1420 continue;
1421 }
1422
1423 if (item.action != LINK_APPEND_ACT_UNSET) {
1424 /* Already set, pass. */
1426 continue;
1427 }
1429
1430 if (do_reuse_local_id && item.reusable_local_id != nullptr) {
1431 CLOG_DEBUG(&LOG, "Appended ID '%s' as a matching local one, re-using it.", id->name);
1433 }
1434 else if (id->tag & ID_TAG_PRE_EXISTING) {
1435 CLOG_DEBUG(&LOG, "Appended ID '%s' was already linked, duplicating it.", id->name);
1437 }
1439 CLOG_DEBUG(
1440 &LOG,
1441 "Appended ID '%s' is also used as a liboverride linked dependency, duplicating it.",
1442 id->name);
1444 }
1445 else if (ID_IS_LINKED(id) && ID_IS_OVERRIDE_LIBRARY(id)) {
1446 /* While in theory liboverrides can be directly made local, this causes complex potential
1447 * problems, e.g. because hierarchy roots can become temporarily invalid when the root is
1448 * made local, etc.
1449 *
1450 * So for now, simpler to always duplicate linked liboverrides. */
1451 CLOG_DEBUG(&LOG, "Appended ID '%s' is a liboverride, duplicating it.", id->name);
1453 }
1454 else {
1455 /* That last action, making linked data directly local, can still be changed to
1456 * #LINK_APPEND_ACT_COPY_LOCAL in the last checks below. This can happen in rare cases with
1457 * complex relationships involving IDs that are kept linked and IDs that are made local,
1458 * both using some same dependencies. */
1459 CLOG_DEBUG(&LOG, "Appended ID '%s' will be made local.", id->name);
1461 }
1462 }
1463
1464 /* Some linked IDs marked to be made directly local may also be used by other items
1465 * marked to be kept linked. in such case, they need to be copied for the local data, such that
1466 * a linked version of these remains available as dependency for other linked data. */
1467 for (BlendfileLinkAppendContextItem &item : lapp_context.items) {
1468 ID *id = item.new_id;
1469 if (id == nullptr) {
1470 continue;
1471 }
1472
1473 /* Only IDs kept as linked need to be checked here. */
1474 if (item.action == LINK_APPEND_ACT_KEEP_LINKED) {
1476 cb_data.lapp_context = &lapp_context;
1477 cb_data.item = &item;
1478 cb_data.reports = reports;
1480 cb_data.is_liboverride_dependency_only = (item.tag &
1484 }
1485
1486 /* If we found a matching existing local id but are not re-using it, we need to properly
1487 * clear its weak reference to linked data. */
1488 if (item.reusable_local_id != nullptr &&
1490 {
1491 BLI_assert_msg(!do_reuse_local_id,
1492 "This code should only be reached when the current append operation does not "
1493 "try to reuse local data.");
1495 id->lib->filepath,
1496 id->name,
1497 item.reusable_local_id);
1498 item.reusable_local_id = nullptr;
1499 }
1500 }
1501}
1502
1504{
1507
1508 if (lapp_context->items.empty()) {
1509 /* Nothing to append. */
1510 return;
1511 }
1512
1513 Main *bmain = lapp_context->params->bmain;
1514
1515 BLI_assert((lapp_context->params->flag & FILE_LINK) == 0);
1516
1517 const bool set_fakeuser = (lapp_context->params->flag & BLO_LIBLINK_APPEND_SET_FAKEUSER) != 0;
1518
1519 const int make_local_common_flags =
1521 ((lapp_context->params->flag & BLO_LIBLINK_APPEND_ASSET_DATA_CLEAR) != 0 ?
1523 0) |
1524 /* In recursive case (i.e. everything becomes local), clear liboverrides. Otherwise (i.e.
1525 * only data from immediately linked libraries is made local), preserve liboverrides. */
1526 ((lapp_context->params->flag & BLO_LIBLINK_APPEND_RECURSIVE) != 0 ?
1528 0);
1529
1530 new_id_to_item_mapping_create(*lapp_context);
1532
1533 /* Add missing items (the indirectly linked ones), and carefully define which action should be
1534 * applied to each of them. */
1535 blendfile_append_define_actions(*lapp_context, reports);
1536
1537 /* Effectively perform required operation on every linked ID. */
1538 for (BlendfileLinkAppendContextItem &item : lapp_context->items) {
1539 ID *id = item.new_id;
1540 if (id == nullptr) {
1541 continue;
1542 }
1543
1544 ID *local_appended_new_id = nullptr;
1545 char lib_filepath[FILE_MAX];
1546 STRNCPY(lib_filepath, id->lib->filepath);
1547 char lib_id_name[MAX_ID_NAME];
1548 STRNCPY(lib_id_name, id->name);
1549
1550 switch (item.action) {
1552 BKE_lib_id_make_local(bmain, id, make_local_common_flags | LIB_ID_MAKELOCAL_FORCE_COPY);
1553 local_appended_new_id = id->newid;
1554 break;
1556 BKE_lib_id_make_local(bmain, id, make_local_common_flags | LIB_ID_MAKELOCAL_FORCE_LOCAL);
1557 BLI_assert(id->newid == nullptr);
1558 local_appended_new_id = id;
1559 break;
1561 /* Nothing to do here. */
1562 break;
1564 BLI_assert(item.reusable_local_id != nullptr);
1565 /* We only need to set `newid` to ID found in previous loop, for proper remapping. */
1566 ID_NEW_SET(id, item.reusable_local_id);
1567 /* This is not a 'new' local appended id, do not set `local_appended_new_id` here. */
1568 break;
1570 CLOG_ERROR(
1571 &LOG, "Unexpected unset append action for '%s' ID, assuming 'keep link'", id->name);
1572 break;
1573 default:
1575 }
1576
1577 if (local_appended_new_id != nullptr) {
1578 if (BKE_idtype_idcode_append_is_reusable(local_appended_new_id->id_type())) {
1580 lib_filepath,
1581 lib_id_name,
1582 local_appended_new_id);
1583 }
1584
1585 if (set_fakeuser) {
1586 if (!ELEM(local_appended_new_id->id_type(), ID_OB, ID_GR)) {
1587 /* Do not set fake user on objects nor collections (instancing). */
1588 id_fake_user_set(local_appended_new_id);
1589 }
1590 }
1591 }
1592 }
1593
1595 lapp_context->library_weak_reference_mapping = nullptr;
1596
1597 /* Remap IDs as needed. */
1598 for (BlendfileLinkAppendContextItem &item : lapp_context->items) {
1599 if (item.action == LINK_APPEND_ACT_KEEP_LINKED) {
1600 continue;
1601 }
1602
1603 ID *id = item.new_id;
1604 if (id == nullptr) {
1605 continue;
1606 }
1609 id = id->newid;
1610 if (id == nullptr) {
1611 continue;
1612 }
1613 }
1614
1616
1617 BKE_libblock_relink_to_newid(bmain, id, 0);
1618 }
1619
1620 /* Remove linked IDs when a local existing data has been reused instead. */
1621 BKE_main_id_tag_all(bmain, ID_TAG_DOIT, false);
1622 for (BlendfileLinkAppendContextItem &item : lapp_context->items) {
1624 continue;
1625 }
1626
1627 ID *id = item.new_id;
1628 if (id == nullptr) {
1629 continue;
1630 }
1632 BLI_assert(id->newid != nullptr);
1633
1634 /* Calling code may want to access newly appended IDs from the link/append context items. */
1635 BKE_blendfile_link_append_context_item_newid_set(lapp_context, &item, id->newid);
1636
1637 /* Only the 'reuse local' action should leave unused newly linked data behind. */
1638 if (item.action != LINK_APPEND_ACT_REUSE_LOCAL) {
1639 continue;
1640 }
1641 /* Do NOT delete a linked data that was already linked before this append. */
1642 if (id->tag & ID_TAG_PRE_EXISTING) {
1643 continue;
1644 }
1645 /* Do NOT delete a linked data that is (also) used a liboverride dependency. */
1648 continue;
1649 }
1650
1651 id->tag |= ID_TAG_DOIT;
1652 }
1654
1656
1657 BlendFileReadReport bf_reports{};
1658 bf_reports.reports = reports;
1659 BLO_read_do_version_after_setup(bmain, lapp_context, &bf_reports);
1660
1662}
1663
1665
1666/* -------------------------------------------------------------------- */
1669
1671{
1673 {
1674 return IDWALK_RET_NOP;
1675 }
1676 ID *id = *cb_data->id_pointer;
1678 static_cast<BlendfileLinkAppendContextCallBack *>(cb_data->user_data);
1679
1680 if ((id->tag & ID_TAG_PRE_EXISTING) != 0) {
1681 /* About to re-use a linked data that was already there, and that will stay linked. This case
1682 * does not need any further processing of the child hierarchy (existing linked data
1683 * instantiation status should not be modified here). */
1684 return IDWALK_RET_NOP;
1685 }
1686
1687 /* In linking case, all linked IDs are considered for instantiation, including from other
1688 * libraries. So all linked IDs that were not skipped so far need to be added to the items
1689 * list.
1690 */
1691 BlendfileLinkAppendContextItem *item = data->lapp_context->new_id_to_item.lookup_default(
1692 id, nullptr);
1693 /* NOTE: liboverride info (tags like #LINK_APPEND_TAG_LIBOVERRIDE_DEPENDENCY) can be
1694 * ignored/skipped here, since all data are kept linked anyway, they are not useful currently.
1695 */
1696 if (item == nullptr) {
1698 data->lapp_context, BKE_id_name(*id), id->id_type(), nullptr);
1699 item->new_id = id;
1700 item->source_library = id->lib;
1701 /* Since there is no item for that ID yet, the user did not select it explicitly, it was
1702 * rather linked indirectly. This info is important for instantiation of collections. */
1704 /* In linking case we already know what we want to do with these items. */
1706 new_id_to_item_mapping_add(*data->lapp_context, id, *item);
1707 }
1708 return IDWALK_RET_NOP;
1709}
1710
1712 ReportList *reports)
1713{
1714 BLI_assert(ELEM(lapp_context->process_stage,
1718
1719 if (!lapp_context->params->context.scene) {
1720 return;
1721 }
1722 if (lapp_context->params->flag & FILE_LINK) {
1723 new_id_to_item_mapping_create(*lapp_context);
1724 /* Add items for all not yet known IDs (i.e. implicitly linked indirect dependencies) to the
1725 * list.
1726 * NOTE: Since items are appended to the list, this list will grow and these IDs will be
1727 * processed later, leading to a flatten recursive processing of all the linked dependencies.
1728 */
1729 for (BlendfileLinkAppendContextItem &item : lapp_context->items) {
1730 ID *id = item.new_id;
1731 if (id == nullptr) {
1732 continue;
1733 }
1734 BLI_assert(item.userdata == nullptr);
1735
1737 cb_data.lapp_context = lapp_context;
1738 cb_data.item = &item;
1739 cb_data.reports = reports;
1741 id,
1743 &cb_data,
1744 IDWALK_NOP);
1745 }
1746 }
1747
1748 LooseDataInstantiateContext instantiate_context{};
1749 instantiate_context.lapp_context = lapp_context;
1750 instantiate_context.target_collection = lapp_context->target_collection;
1751 loose_data_instantiate(&instantiate_context);
1752}
1753
1755 Scene *scene,
1756 ViewLayer *view_layer,
1757 Collection *target_collection,
1758 ReportList *reports)
1759{
1760 if (!scene) {
1761 return;
1762 }
1763
1764 /* Build a minimal link/append context populated with only the new IDs from bmain,
1765 * so the standard loose-data instantiation functions can be reused directly.
1766 */
1767 LibraryLink_Params lapp_params{};
1768 lapp_params.bmain = bmain;
1769 lapp_params.flag = FILE_LINK;
1770 lapp_params.id_tag_extra = 0;
1771 lapp_params.context.scene = scene;
1772 lapp_params.context.view_layer = view_layer;
1773 lapp_params.context.v3d = nullptr;
1774
1775 BlendfileLinkAppendContext lapp_context{};
1776 lapp_context.params = &lapp_params;
1778 lapp_context.target_collection = target_collection;
1779
1780 for (ID &id : MainAllIDsIterator(*bmain)) {
1781 if (id.tag & ID_TAG_PRE_EXISTING) {
1782 continue;
1783 }
1784
1786 &lapp_context, BKE_id_name(id), id.id_type(), nullptr);
1787
1788 item->new_id = &id;
1789 item->tag = 0;
1791 }
1792
1793 BKE_blendfile_link_append_instantiate_loose(&lapp_context, reports);
1794
1795 BKE_main_id_tag_all(bmain, ID_TAG_DOIT, false);
1796}
1797
1799{
1802
1803 if (lapp_context->items.empty()) {
1804 /* Nothing to be linked. */
1805 return;
1806 }
1807
1808 BLI_assert(!lapp_context->libraries.is_empty());
1809
1810 Main *mainl;
1811 Library *lib;
1812
1813 for (const int lib_idx : lapp_context->libraries.index_range()) {
1814 BlendfileLinkAppendContextLibrary &lib_context = lapp_context->libraries[lib_idx];
1815 const char *libname = lib_context.path.c_str();
1816
1817 if (!link_append_context_library_blohandle_ensure(*lapp_context, lib_context, reports)) {
1818 /* Unlikely since we just browsed it, but possible
1819 * Error reports will have been made by BLO_blendhandle_from_file() */
1820 continue;
1821 }
1822
1823 /* here appending/linking starts */
1824
1825 mainl = BLO_library_link_begin(&lib_context.blo_handle, libname, lapp_context->params);
1826 lib = mainl->curlib;
1827 BLI_assert(lib != nullptr);
1828 /* In case lib was already existing but not found originally, see #99820. */
1829 lib->id.tag &= ~ID_TAG_MISSING;
1830
1831 if (mainl->versionfile < 250) {
1832 BKE_reportf(reports,
1834 "Linking or appending from a very old .blend file format (%d.%d), no animation "
1835 "conversion will "
1836 "be done! You may want to re-save your lib file with current Blender",
1837 mainl->versionfile,
1838 mainl->subversionfile);
1839 }
1840
1841 /* For each lib file, we try to link all items belonging to that lib,
1842 * and tag those successful to not try to load them again with the other libraries. */
1843 for (BlendfileLinkAppendContextItem &item : lapp_context->items) {
1844 ID *new_id;
1845
1846 if (!item.libraries[lib_idx]) {
1847 continue;
1848 }
1849
1851 mainl, &lib_context.blo_handle, item.idcode, item.name.c_str(), lapp_context->params);
1852
1853 if (new_id) {
1854 /* If the link is successful, clear item's libraries 'todo' flags.
1855 * This avoids trying to link same item with other libraries to come. */
1856 item.libraries.fill(false);
1857 item.new_id = new_id;
1858 item.source_library = new_id->lib;
1859 }
1860 }
1861
1862 BLO_library_link_end(mainl, &lib_context.blo_handle, lapp_context->params, reports);
1863 link_append_context_library_blohandle_release(*lapp_context, lib_context);
1864 }
1865
1866 /* In linking case finalizing process (ensuring all data is valid, instantiating loose
1867 * collections or objects, etc.) can be done here directly.
1868 *
1869 * In append case, the finalizing process is much more complex and requires and additional call
1870 * to #BKE_blendfile_append for caller code. */
1871 if (lapp_context->params->flag & FILE_LINK) {
1872 BlendFileReadReport bf_reports{};
1873 bf_reports.reports = reports;
1874 BLO_read_do_version_after_setup(lapp_context->params->bmain, lapp_context, &bf_reports);
1875
1877 }
1878}
1879
1881 const eBKELibLinkOverride flags,
1882 ReportList * /*reports*/)
1883{
1884 if (lapp_context->items.empty()) {
1885 /* Nothing to override. */
1886 return;
1887 }
1888
1889 Main *bmain = lapp_context->params->bmain;
1890
1891 /* Liboverride only makes sense if data was linked, not appended. */
1892 BLI_assert((lapp_context->params->flag & FILE_LINK) != 0);
1893
1894 const bool set_runtime = (flags & BKE_LIBLINK_OVERRIDE_CREATE_RUNTIME) != 0;
1895 const bool do_use_exisiting_liboverrides = (flags &
1897
1898 Map<ID *, ID *> linked_ids_to_local_liboverrides;
1899 if (do_use_exisiting_liboverrides) {
1900 ID *id_iter;
1901 FOREACH_MAIN_ID_BEGIN (bmain, id_iter) {
1902 if (ID_IS_LINKED(id_iter)) {
1903 continue;
1904 }
1905 if (!ID_IS_OVERRIDE_LIBRARY_REAL(id_iter)) {
1906 continue;
1907 }
1908 /* Do not consider regular liboverrides if runtime ones are requested, and vice-versa. */
1909 if ((set_runtime && (id_iter->tag & ID_TAG_RUNTIME) == 0) ||
1910 (!set_runtime && (id_iter->tag & ID_TAG_RUNTIME) != 0))
1911 {
1912 continue;
1913 }
1914
1915 /* In case several liboverrides exist of the same data, only consider the first found one, so
1916 * don't use `add_overwrite`. */
1917 linked_ids_to_local_liboverrides.add(id_iter->override_library->reference, id_iter);
1918 }
1920 }
1921
1922 for (BlendfileLinkAppendContextItem &item : lapp_context->items) {
1923 ID *id = item.new_id;
1924 if (id == nullptr) {
1925 continue;
1926 }
1927 BLI_assert(item.userdata == nullptr);
1928
1929 if (do_use_exisiting_liboverrides) {
1930 item.liboverride_id = linked_ids_to_local_liboverrides.lookup_default(id, nullptr);
1931 }
1932 if (item.liboverride_id == nullptr) {
1934 if (set_runtime) {
1936 if ((id->tag & ID_TAG_PRE_EXISTING) == 0) {
1937 /* If the linked ID is newly linked, in case its override is runtime-only, assume its
1938 * reference to be indirectly linked.
1939 *
1940 * This is more of an heuristic for 'as best as possible' user feedback in the UI
1941 * (Outliner), which is expected to be valid in almost all practical use-cases. Direct or
1942 * indirect linked status is properly checked before saving .blend file. */
1943 id->tag &= ~ID_TAG_EXTERN;
1944 id->tag |= ID_TAG_INDIRECT;
1945 }
1946 }
1947 }
1948 }
1949
1950 BKE_main_namemap_clear(*bmain);
1951}
1952
1954
1955/* -------------------------------------------------------------------- */
1958
1960 Map<ID *, ID *> &old_owner_id_to_shapekey,
1961 ID *old_id,
1962 ID *new_id,
1963 const bool do_reload)
1964{
1965 BLI_assert(old_id);
1966 if (do_reload) {
1967 /* Since we asked for placeholders in case of missing IDs,
1968 * we expect to always get a valid one. */
1969 BLI_assert(new_id);
1970 }
1971 if (new_id) {
1972 CLOG_DEBUG(&LOG,
1973 "Before remap of %s, old_id users: %d, new_id users: %d",
1974 old_id->name,
1975 old_id->us,
1976 new_id->us);
1977 remapper.add(old_id, new_id);
1978
1979 /* Usual special code for ShapeKeys snowflakes...
1980 *
1981 * NOTE: Unfortunately, actual reasons for why the old shapekeys needs to be removed from their
1982 * old owner ID was not documented in the initial commit. Suspect it's related to the fact that
1983 * the old ID should not end up using the new shapekeys? */
1984 Key **old_key_p = BKE_key_from_id_p(old_id);
1985 if (old_key_p == nullptr) {
1986 return;
1987 }
1988 Key *old_key = *old_key_p;
1989 Key *new_key = BKE_key_from_id(new_id);
1990 if (old_key != nullptr) {
1991 old_owner_id_to_shapekey.add(old_id, &old_key->id);
1992 *old_key_p = nullptr;
1993 id_us_min(&old_key->id);
1994 remapper.add(&old_key->id, &new_key->id);
1995 }
1996 }
1997}
1998
2000 Map<ID *, ID *> &old_owner_id_to_shapekey,
2001 ID *old_id,
2002 ID *new_id,
2003 ReportList *reports,
2004 const bool do_reload)
2005{
2006 /* Restore old shapekey pointer in old id (see also
2007 * #blendfile_library_relocate_id_remap_prepare above). */
2008 Key **old_key_p = BKE_key_from_id_p(old_id);
2009 if (old_key_p) {
2010 Key *old_key = reinterpret_cast<Key *>(
2011 old_owner_id_to_shapekey.lookup_default_as(old_id, nullptr));
2012 if (old_key) {
2013 BLI_assert(GS(old_key->id.name) == ID_KE);
2014 *old_key_p = old_key;
2015 id_us_plus_no_lib(&old_key->id);
2016 }
2017 }
2018
2019 if (old_id->flag & ID_FLAG_FAKEUSER) {
2020 id_fake_user_clear(old_id);
2021 id_fake_user_set(new_id);
2022 }
2023
2024 CLOG_DEBUG(&LOG,
2025 "After remap of %s, old_id users: %d, new_id users: %d",
2026 old_id->name,
2027 old_id->us,
2028 new_id->us);
2029
2030 /* In some cases, new_id might become direct link, remove parent of library in this case. */
2031 if (new_id->lib->runtime->parent && (new_id->tag & ID_TAG_INDIRECT) == 0) {
2032 if (do_reload) {
2033 BLI_assert_unreachable(); /* Should not happen in 'pure' reload case... */
2034 }
2035 new_id->lib->runtime->parent = nullptr;
2036 }
2037
2038 if (old_id->us > 0 && old_id->lib == new_id->lib) {
2039 /* Note that this *should* not happen - but better be safe than sorry in this area,
2040 * at least until we are 100% sure this cannot ever happen.
2041 * Also, we can safely assume names were unique so far,
2042 * so just replacing '.' by '~' should work,
2043 * but this does not totally rules out the possibility of name collision. */
2044 size_t len = strlen(old_id->name);
2045 size_t dot_pos;
2046 bool has_num = false;
2047
2048 for (dot_pos = len; dot_pos--;) {
2049 char c = old_id->name[dot_pos];
2050 if (c == '.') {
2051 break;
2052 }
2053 if (c < '0' || c > '9') {
2054 has_num = false;
2055 break;
2056 }
2057 has_num = true;
2058 }
2059
2060 if (has_num) {
2061 old_id->name[dot_pos] = '~';
2062 }
2063 else {
2064 len = std::min<size_t>(len, MAX_ID_NAME - 7);
2065 BLI_strncpy(&old_id->name[len], "~000", 7);
2066 }
2067
2068 id_sort_by_name(which_libbase(bmain, old_id->id_type()), old_id, nullptr);
2069
2071 reports,
2073 "Lib Reload: Replacing all references to old data-block '%s' by reloaded one failed, "
2074 "old one (%d remaining users) had to be kept and was renamed to '%s'",
2075 new_id->name,
2076 old_id->us,
2077 old_id->name);
2078 }
2079}
2080
2082 ReportList *reports,
2083 const bool do_reload,
2084 const int remap_flags)
2085{
2086 Main *bmain = lapp_context.params->bmain;
2087
2088 id::IDRemapper remapper;
2089 Map<ID *, ID *> old_owner_id_to_shapekey;
2090
2091 for (BlendfileLinkAppendContextItem &item : lapp_context.items) {
2092 ID *old_id = static_cast<ID *>(item.userdata);
2093 if (!old_id) {
2094 continue;
2095 }
2096 ID *new_id = item.new_id;
2098 remapper, old_owner_id_to_shapekey, old_id, new_id, do_reload);
2099 }
2100
2101 BKE_libblock_remap_multiple_locked(bmain, remapper, remap_flags);
2102
2103 for (BlendfileLinkAppendContextItem &item : lapp_context.items) {
2104 ID *old_id = static_cast<ID *>(item.userdata);
2105 if (!old_id) {
2106 continue;
2107 }
2108 ID *new_id = item.new_id;
2109 if (!new_id) {
2110 if (do_reload) {
2111 /* Since we asked for placeholders in case of missing IDs, we expect to always get a valid
2112 * one. */
2113 BLI_assert_msg(false,
2114 "On library reload, placeholders should be generated when a linked ID is "
2115 "missing, so there should never be a nullptr 'new_id' here");
2116 }
2117 /* If finding a valid matching ID for `old_id` in the searched library(-ies) failed, do not
2118 * clear references to the current 'old_id' placeholder. */
2119 continue;
2120 }
2122 bmain, old_owner_id_to_shapekey, old_id, new_id, reports, do_reload);
2123 }
2124}
2125
2128{
2129 Main &bmain = *lapp_context.params->bmain;
2130
2131 Set<ID *> ids_to_delete = {};
2132 ID *id_iter;
2133
2134 /* Delete all no more used old IDs. */
2135 /* NOTE: While this looping over until we are sure we deleted everything is very far from
2136 * efficient, doing otherwise would require a much more complex handling of indirectly linked IDs
2137 * in steps above. Currently, in case of relocation, those are skipped in remapping phase, though
2138 * in some cases (essentially internal links between IDs from the same library) remapping should
2139 * happen. But getting this to work reliably would be very difficult, so since this is not a
2140 * performance-critical code, better to go with the (relatively) simpler, brute-force approach
2141 * here in 'removal of old IDs' step. */
2142 bool keep_looping = true;
2143 while (keep_looping) {
2144 keep_looping = false;
2145
2146 for (BlendfileLinkAppendContextItem &item : lapp_context.items) {
2147 ID *old_id = static_cast<ID *>(item.userdata);
2148
2149 if (old_id == nullptr) {
2150 continue;
2151 }
2152
2153 if (old_id->id_type() == ID_KE) {
2154 /* Shape Keys are handled as part of their owning obdata (see below). This implies that
2155 * there is no way to know when the old pointer gets invalid, so just clear it immediately.
2156 */
2157 item.userdata = nullptr;
2158 continue;
2159 }
2160
2161 /* In case the active scene was reloaded, the context pointers in
2162 * `lapp_context->params->context` need to be updated before the old Scene ID is freed. */
2163 if (old_id == &lapp_context.params->context.scene->id) {
2164 BLI_assert(old_id->id_type() == ID_SCE);
2165 Scene *new_scene = reinterpret_cast<Scene *>(item.new_id);
2166 BLI_assert(new_scene != nullptr);
2167 lapp_context.params->context.scene = new_scene;
2168 if (lapp_context.params->context.view_layer != nullptr) {
2169 ViewLayer *new_view_layer = BKE_view_layer_find(
2170 new_scene, lapp_context.params->context.view_layer->name);
2171 lapp_context.params->context.view_layer = static_cast<ViewLayer *>(
2172 (new_view_layer != nullptr) ? new_view_layer : new_scene->view_layers.first());
2173 }
2174 /* lapp_context->params->context.v3d should never be made invalid by newly linked data
2175 * here, as it is UI data, ultimately owned by a #bScreen ID, which is not linkable. */
2176 }
2177
2178 if (old_id->us == 0) {
2179 ids_to_delete.add(old_id);
2180 item.userdata = nullptr;
2181 keep_looping = true;
2182 Key *old_key = BKE_key_from_id(old_id);
2183 if (old_key != nullptr) {
2184 ids_to_delete.add(&old_key->id);
2185 }
2186 }
2187 }
2188 BKE_id_multi_delete(&bmain, ids_to_delete);
2189 ids_to_delete.clear();
2190 }
2191
2192 /* Some datablocks can get reloaded/replaced 'silently' because they are not linkable
2193 * (shape keys e.g.), so we need another loop here to clear old ones if possible. */
2194 FOREACH_MAIN_ID_BEGIN (&bmain, id_iter) {
2195 /* XXX That check may be a bit to generic/permissive? */
2196 if (id_iter->lib && (id_iter->flag & ID_TAG_PRE_EXISTING) && id_iter->us == 0) {
2197 ids_to_delete.add(id_iter);
2198 }
2199 }
2201 BKE_id_multi_delete(&bmain, ids_to_delete);
2202 ids_to_delete.clear();
2203
2204 /* Get rid of no more used libraries... */
2205 ListBaseT<ID> *libraries = which_libbase(&bmain, ID_LI);
2206 for (ID &id_iter : *libraries) {
2207 ids_to_delete.add(&id_iter);
2208 }
2209 FOREACH_MAIN_ID_BEGIN (&bmain, id_iter) {
2210 if (id_iter->lib) {
2211 ids_to_delete.remove(&id_iter->lib->id);
2212 /* If the used library is an archive one, its owner 'normal' library is also used. */
2213 if (id_iter->lib->archive_parent_library) {
2215 ids_to_delete.remove(&id_iter->lib->archive_parent_library->id);
2216 }
2217 }
2218 }
2220 BKE_id_multi_delete(&bmain, ids_to_delete);
2221}
2222
2225 BlendfileLinkAppendContext &lapp_context,
2226 const Map<Library *, Library *> &new_to_old_libraries_map,
2227 ReportList *reports)
2228{
2229 Main &bmain = *lapp_context.params->bmain;
2230
2231 ID *id_iter;
2232 FOREACH_MAIN_ID_BEGIN (&bmain, id_iter) {
2233 if (ID_IS_LINKED(id_iter) || !ID_IS_OVERRIDE_LIBRARY_REAL(id_iter) ||
2234 (id_iter->tag & ID_TAG_PRE_EXISTING) == 0)
2235 {
2236 continue;
2237 }
2238 if ((id_iter->override_library->reference->tag & ID_TAG_MISSING) == 0) {
2239 id_iter->tag &= ~ID_TAG_MISSING;
2240 }
2241 if ((id_iter->override_library->reference->tag & ID_TAG_PRE_EXISTING) == 0) {
2242 BKE_lib_override_library_update(&bmain, id_iter);
2243 }
2244 }
2246
2248
2249 /* Resync overrides if needed. */
2250 if (liboverride::is_auto_resync_enabled() && lapp_context.params->context.scene != nullptr) {
2251 BlendFileReadReport report{};
2252 report.reports = reports;
2254 &new_to_old_libraries_map,
2255 lapp_context.params->context.scene,
2256 lapp_context.params->context.view_layer,
2257 &report);
2258 /* We need to rebuild some of the deleted override rules (for UI feedback purpose). */
2260 }
2261}
2262
2264 ReportList *reports,
2266 const bool do_reload)
2267{
2268 Main *bmain = lapp_context->params->bmain;
2269
2270 /* All override rules need to be up to date, since there will be no do_version here, otherwise
2271 * older, now-invalid rules might be applied and likely fail, or some changes might be missing,
2272 * etc. See #93353. */
2274
2275 /* Remove all IDs to be reloaded from Main. */
2276 MainListsArray lbarray = BKE_main_lists_get(*bmain);
2277 int lba_idx = lbarray.size();
2278 while (lba_idx--) {
2279 ID *id = lbarray[lba_idx]->first();
2280 const short idcode = id ? id->id_type() : 0;
2281
2282 if (!id || !BKE_idtype_idcode_is_linkable(idcode)) {
2283 /* No need to reload non-linkable data-types,
2284 * those will get relinked with their 'users ID'. */
2285 continue;
2286 }
2287
2288 for (; id; id = static_cast<ID *>(id->next)) {
2289 if (id->lib == library) {
2291
2292 /* We remove it from current Main, and add it to items to link... */
2293 /* Note that non-linkable IDs (like e.g. shape-keys) are also explicitly linked here... */
2294 BLI_remlink(lbarray[lba_idx], id);
2295 /* Usual special code for ShapeKeys snowflakes... */
2296 Key *old_key = BKE_key_from_id(id);
2297 if (old_key != nullptr) {
2298 BLI_remlink(which_libbase(bmain, old_key->id.id_type()), &old_key->id);
2299 }
2300
2302 lapp_context, BKE_id_name(*id), idcode, id);
2303 item->libraries.fill(true);
2304
2305 CLOG_DEBUG(&LOG, "Data-block to seek for: %s", id->name);
2306 }
2307 }
2308 }
2309
2310 if (lapp_context->items.empty()) {
2311 /* Early out in case there is nothing to do. */
2312 return;
2313 }
2314
2316
2317 /* Since some IDs have been removed from Main, trying to rebuild collections hierarchy should not
2318 * happen. It has to be done manually below once removed IDs have been added back to Main. Also
2319 * see #136432. */
2322
2324
2325 /* We do not want any instantiation here! */
2326 BKE_blendfile_link(lapp_context, reports);
2327
2328 BKE_main_lock(bmain);
2329
2330 /* We add back old id to bmain.
2331 * We need to do this in a first, separated loop, otherwise some of those may not be handled by
2332 * ID remapping, which means they would still reference old data to be deleted... */
2333 for (BlendfileLinkAppendContextItem &item : lapp_context->items) {
2334 ID *old_id = static_cast<ID *>(item.userdata);
2335
2336 BLI_assert(old_id);
2337 BLI_addtail(which_libbase(bmain, old_id->id_type()), old_id);
2338
2339 /* Usual special code for ShapeKeys snowflakes... */
2340 Key *old_key = BKE_key_from_id(old_id);
2341 if (old_key != nullptr) {
2342 BLI_addtail(which_libbase(bmain, old_key->id.id_type()), &old_key->id);
2343 }
2344 }
2345
2346 /* FIXME Temporary 'fix' to a problem in how temp ID are copied in
2347 * `BKE_lib_override_library_main_update`, see #103062.
2348 * Proper fix involves first addressing #90610. */
2350
2351 /* Since our (old) reloaded IDs were removed from main, the user count done for them in linking
2352 * code is wrong, we need to redo it here after adding them back to main. */
2353 BKE_main_id_refcount_recompute(bmain, false);
2354
2355 /* Mapping from old to new libraries, needed to allow liboverride resync to map properly old and
2356 * new data. */
2357 Map<Library *, Library *> new_to_old_libraries_map;
2358
2359 for (BlendfileLinkAppendContextItem &item : lapp_context->items) {
2360 ID *old_id = static_cast<ID *>(item.userdata);
2361 ID *new_id = item.new_id;
2362 if (new_id) {
2363 new_to_old_libraries_map.add(new_id->lib, old_id->lib);
2364 }
2365 }
2366
2368
2369 /* Note that in reload case, we also want to replace indirect usages. */
2370 const int remap_flags = ID_REMAP_SKIP_NEVER_NULL_USAGE |
2371 (do_reload ? 0 : ID_REMAP_SKIP_INDIRECT_USAGE);
2372 blendfile_library_relocate_id_remap(*lapp_context, reports, do_reload, remap_flags);
2373
2376
2377 BKE_main_unlock(bmain);
2378
2379 /* Delete all no more used old IDs. */
2381
2382 /* Update and resync liboverrides of reloaded linked data-blocks. */
2383 blendfile_relocate_postprocess_liboverrides(*lapp_context, new_to_old_libraries_map, reports);
2384
2386}
2387
2389{
2390 if (lapp_context.items.empty()) {
2391 /* Nothing to relocate. */
2392 return;
2393 }
2394 /* Only support relocating one ID at a time currently. */
2395 BLI_assert(lapp_context.items.size() == 1);
2396
2397 Main *bmain = lapp_context.params->bmain;
2398
2399 /* Relocate only works on linked data currently. */
2400 BLI_assert((lapp_context.params->flag & FILE_LINK) != 0);
2401
2402 /* Tag everything, its generally useful to know what is new.
2403 *
2404 * Take extra care `BKE_main_id_flag_all(bmain, ID_TAG_PRE_EXISTING, false)` is called after! */
2406
2407 /* XXX We'd need re-entrant locking on Main for this to work... */
2408 // BKE_main_lock(bmain);
2409
2410 BKE_blendfile_link(&lapp_context, reports);
2411
2412 // BKE_main_unlock(bmain);
2413
2414 /* Finalize relocation (remap ID usages, rebuild LibOverrides if needed, etc.). */
2415
2416 /* Mapping from old to new libraries, needed to allow liboverride resync to map properly old and
2417 * new data. */
2418 Map<Library *, Library *> new_to_old_libraries_map{};
2419
2420 /* The first item should be the root of the relocation, and the only one containing a non-null
2421 * `userdata`. */
2422 BlendfileLinkAppendContextItem &root_item = lapp_context.items.front();
2423 BLI_assert(root_item.userdata);
2424 ID *old_id = static_cast<ID *>(root_item.userdata);
2425 ID *new_id = root_item.new_id;
2426 new_to_old_libraries_map.add(new_id->lib, old_id->lib);
2427 BLI_assert(old_id->id_type() == GS(new_id->name));
2428#ifndef NDEBUG
2429 for (BlendfileLinkAppendContextItem &item : lapp_context.items) {
2430 BLI_assert(&item == &root_item || item.userdata == nullptr);
2431 }
2432#endif
2433
2434 BKE_main_lock(bmain);
2436
2437 /* Do not affect indirect usages. */
2439 blendfile_library_relocate_id_remap(lapp_context, reports, false, remap_flags);
2440
2443 BKE_main_unlock(bmain);
2444
2445 /* Delete all no more used old IDs. */
2447
2448 /* Update and resync liboverrides of reloaded linked data-blocks. */
2449 blendfile_relocate_postprocess_liboverrides(lapp_context, new_to_old_libraries_map, reports);
2450
2452
2453 /* Important we unset, otherwise these object won't
2454 * link into other scenes from this blend file. */
2456}
2457
2459
2460} // namespace blender
#define FOREACH_COLLECTION_OBJECT_RECURSIVE_END
#define FOREACH_COLLECTION_OBJECT_RECURSIVE_BEGIN(_collection, _object)
#define FOREACH_OBJECT_END
Definition BKE_layer.hh:533
#define FOREACH_OBJECT_BEGIN(_bmain, _scene, _view_layer, _instance)
Definition BKE_layer.hh:523
#define FOREACH_MAIN_ID_END
Definition BKE_main.hh:735
#define FOREACH_MAIN_ID_BEGIN(_bmain, _id)
Definition BKE_main.hh:729
General operations, lookup, etc. for materials.
General operations, lookup, etc. for blender objects.
API for Blender-side Rigid Body stuff.
#define FILE_MAX
#define BLO_EMBEDDED_STARTUP_BLEND
external writefile.cc function prototypes.
#define DATA_(msgid)
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
#define CLOG_DEBUG(clg_ref,...)
Definition CLG_log.h:191
ID and Library types, which are fundamental for SDNA.
#define ID_IS_PACKED(_id)
Definition DNA_ID.h:703
#define ID_IS_OVERRIDE_LIBRARY_REAL(_id)
Definition DNA_ID.h:729
#define ID_IS_LINKED(_id)
Definition DNA_ID.h:697
#define MAX_ID_NAME
Definition DNA_ID.h:367
#define ID_IS_OVERRIDE_LIBRARY(_id)
Definition DNA_ID.h:736
#define ID_NEW_SET(_id, _idn)
Definition DNA_ID.h:757
Object groups, one object can be in many groups at once.
Object is a sort of wrapper for general info.
#define OB_DATA_SUPPORT_ID(_id_type)
Read Blender memory allocation and freeing.
#define BLI_assert_unreachable()
#define UNUSED_VARS_NDEBUG(...)
#define ELEM(...)
#define BLI_assert_msg(a, msg)
#define CLAMP_MIN(a, b)
BitVector(Allocator allocator={}) noexcept
bool add(const Key &key, const Value &value)
Definition BLI_map.hh:295
Value lookup_default(const Key &key, const Value &default_value) const
Definition BLI_map.hh:570
Value lookup_default_as(const ForwardKey &key, ForwardValue &&...default_value) const
Definition BLI_map.hh:575
bool contains(const Key &key) const
Definition BLI_set.hh:310
bool add(const Key &key)
Definition BLI_set.hh:248
void add_new(const Key &key)
Definition BLI_set.hh:233
void clear()
Definition BLI_set.hh:570
bool remove(const Key &key)
Definition BLI_set.hh:404
void push_as(ForwardT &&...value)
Definition BLI_stack.hh:224
bool is_empty() const
Definition BLI_stack.hh:310
constexpr const char * c_str() const
void fill(const bool value)
void add(ID *old_id, ID *new_id)
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
Set< ID * > pack_linked_id_hierarchy(Main &bmain, ID &root_id)
Definition library.cc:741
BMesh Iterators.
void BKE_blendfile_link_append_instantiate_loose(BlendfileLinkAppendContext *lapp_context, ReportList *reports)
UserDef U
Definition blender.cc:58
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
BlendHandle * BLO_blendhandle_from_file(const char *filepath, BlendFileReadReport *reports)
std::array< ListBaseT< ID > *, INDEX_ID_MAX - 1 > MainListsArray
Definition BKE_main.hh:797
uint64_t BKE_idtype_idcode_to_idfilter(short idcode)
Definition idtype.cc:473
unsigned __int64 uint64_t
Definition BKE_text.h:91
BlendfileLinkAppendContext * BKE_blendfile_link_append_context_new(LibraryLink_Params *params)
static void loose_data_gather_instanciated_objects(LooseDataInstantiateContext &instantiate_context, Set< Object * > &instanciated_objects)
static int foreach_libblock_append_add_dependencies_callback(LibraryIDLinkCallbackData *cb_data)
char * STRNCPY(char(&dst)[N], const char *src)
void BKE_main_library_weak_reference_add_item(MainLibraryWeakReferenceMap *library_weak_reference_mapping, const char *library_filepath, const char *library_id_name, ID *new_id) ATTR_NONNULL()
Definition main.cc:874
@ RPT_WARNING
Definition BKE_report.hh:40
ID * BKE_main_library_weak_reference_search_item(MainLibraryWeakReferenceMap *library_weak_reference_mapping, const char *library_filepath, const char *library_id_name) ATTR_NONNULL()
Definition main.cc:865
MINLINE void copy_v3_v3(float r[3], const float a[3])
@ BKE_CB_EVT_BLENDIMPORT_POST
@ BKE_CB_EVT_BLENDIMPORT_PRE
void BKE_libblock_relink_to_newid(Main *bmain, ID *id, int remap_flag) ATTR_NONNULL()
Definition lib_remap.cc:932
static bool is_collection_instantiated_by_other_link_append_data(LooseDataInstantiateContext *instantiate_context, Collection *collection)
static int foreach_libblock_append_finalize_action_callback(LibraryIDLinkCallbackData *cb_data)
void id_us_plus(ID *id)
Definition lib_id.cc:433
static bool object_in_any_scene(Main *bmain, Object *ob)
LayerCollection * BKE_layer_collection_get_active(ViewLayer *view_layer)
void BLO_blendhandle_close(BlendHandle *bh) ATTR_NONNULL(1)
void BKE_blendfile_link_append_context_free(BlendfileLinkAppendContext *lapp_context)
bool BKE_collection_child_add(Main *bmain, Collection *parent, Collection *child)
bool BKE_idtype_idcode_is_linkable(short idcode)
Definition idtype.cc:307
@ OB_FLAG_ACTIVE_CLIPBOARD
void BKE_main_collections_parent_relations_rebuild(Main *bmain)
BlendfileLinkAppendContextItem * BKE_blendfile_link_append_context_item_add(BlendfileLinkAppendContext *lapp_context, const char *idname, short idcode, void *userdata)
bool BKE_idtype_idcode_append_is_reusable(short idcode)
Definition idtype.cc:326
static void loose_data_instantiate_obdata_process(LooseDataInstantiateContext *instantiate_context)
static int foreach_libblock_append_ensure_reusable_local_id_callback(LibraryIDLinkCallbackData *cb_data)
void BKE_blendfile_link_append_context_item_library_index_enable(BlendfileLinkAppendContext *lapp_context, BlendfileLinkAppendContextItem *item, int library_index)
ID * BKE_lib_override_library_create_from_id(Main *bmain, ID *reference_id, bool do_tagged_remap)
@ ID_REMAP_SKIP_NEVER_NULL_USAGE
@ ID_REMAP_SKIP_INDIRECT_USAGE
MainListsArray BKE_main_lists_get(Main &bmain)
Definition main.cc:1158
void BKE_main_library_weak_reference_remove_item(MainLibraryWeakReferenceMap *library_weak_reference_mapping, const char *library_filepath, const char *library_id_name, ID *old_id) ATTR_NONNULL()
Definition main.cc:920
static bool foreach_libblock_link_append_common_processing(LibraryIDLinkCallbackData *cb_data, FunctionRef< LibraryIDLinkCallback > callback)
void BKE_blendfile_id_relocate(BlendfileLinkAppendContext &lapp_context, ReportList *reports)
void BLO_read_do_version_after_setup(Main *new_bmain, BlendfileLinkAppendContext *lapp_context, BlendFileReadReport *reports)
void BKE_blendfile_link_append_context_embedded_blendfile_clear(BlendfileLinkAppendContext *lapp_context)
const char * BKE_id_name(const ID &id)
ID * BLO_library_link_named_part(Main *mainl, BlendHandle **bh, short idcode, const char *name, const LibraryLink_Params *params)
Definition readfile.cc:5679
static BlendHandle * link_append_context_library_blohandle_ensure(BlendfileLinkAppendContext &lapp_context, BlendfileLinkAppendContextLibrary &lib_context, ReportList *reports)
static void loose_data_instantiate_collection_process(LooseDataInstantiateContext *instantiate_context)
void id_fake_user_set(ID *id)
Definition lib_id.cc:471
@ BLO_LIBLINK_APPEND_LOCAL_ID_REUSE
@ BLO_LIBLINK_APPEND_RECURSIVE
@ BLO_LIBLINK_APPEND_SET_FAKEUSER
@ BLO_LIBLINK_OBDATA_INSTANCE
@ BLO_LIBLINK_COLLECTION_NO_HIERARCHY_REBUILD
@ BLO_LIBLINK_APPEND_ASSET_DATA_CLEAR
@ BLO_LIBLINK_APPEND_SET_OB_ACTIVE_CLIPBOARD
@ BLO_LIBLINK_COLLECTION_INSTANCE
static void loose_data_instantiate(LooseDataInstantiateContext *instantiate_context)
ID * BKE_blendfile_link_append_context_item_newid_get(BlendfileLinkAppendContext *lapp_context, BlendfileLinkAppendContextItem *item)
void BKE_main_id_refcount_recompute(Main *bmain, bool do_linked_only)
Definition lib_id.cc:2131
void BKE_layer_collection_resync_forbid(Main &bmain)
void BKE_lib_override_library_update(Main *bmain, ID *local)
ID * BKE_blendfile_link_append_context_item_liboverrideid_get(BlendfileLinkAppendContext *lapp_context, BlendfileLinkAppendContextItem *item)
void id_sort_by_name(ListBaseT< ID > *lb, ID *id, ID *id_sorting_hint)
Definition lib_id.cc:1888
void BKE_main_library_weak_reference_destroy(MainLibraryWeakReferenceMap *library_weak_reference_mapping) ATTR_NONNULL()
Definition main.cc:859
short BKE_blendfile_link_append_context_item_idcode_get(BlendfileLinkAppendContext *lapp_context, BlendfileLinkAppendContextItem *item)
bool BKE_main_collection_sync_remap(const Main *bmain)
BlendHandle * BLO_blendhandle_from_memory(const void *mem, int memsize, BlendFileReadReport *reports)
void BKE_library_main_rebuild_hierarchy(Main *bmain)
Definition library.cc:375
Key * BKE_key_from_id(ID *id)
Definition key.cc:1291
static void blendfile_library_relocate_id_remap_prepare(id::IDRemapper &remapper, Map< ID *, ID * > &old_owner_id_to_shapekey, ID *old_id, ID *new_id, const bool do_reload)
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
static void link_append_context_library_blohandle_release(BlendfileLinkAppendContext &, BlendfileLinkAppendContextLibrary &lib_context)
void BKE_layer_collection_resync_allow(Main &bmain)
void BKE_rigidbody_ensure_local_object(struct Main *bmain, struct Object *ob)
void * BKE_blendfile_link_append_context_item_userdata_get(BlendfileLinkAppendContext *lapp_context, BlendfileLinkAppendContextItem *item)
void BKE_blendfile_link_append_context_item_newid_set(BlendfileLinkAppendContext *lapp_context, BlendfileLinkAppendContextItem *item, ID *new_id)
void BKE_blendfile_link_append_instantiate_loose_from_bmain(Main *bmain, Scene *scene, ViewLayer *view_layer, Collection *target_collection, ReportList *reports)
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
short BKE_idtype_idcode_iter_step(int *idtype_index)
Definition idtype.cc:483
void BKE_blendfile_link_append_context_embedded_blendfile_set(BlendfileLinkAppendContext *lapp_context, const void *blendfile_mem, int blendfile_memsize)
BMesh const char void * data
void BKE_object_materials_sync_length(Main *bmain, Object *ob, ID *id)
static void loose_data_instantiate_object_rigidbody_postprocess(LooseDataInstantiateContext *instantiate_context)
Key ** BKE_key_from_id_p(ID *id)
Definition key.cc:1266
bool BKE_blendfile_link_append_context_is_empty(BlendfileLinkAppendContext *lapp_context)
void BKE_main_ensure_invariants(Main &bmain, std::optional< Span< ID * > > modified_ids=std::nullopt)
static int foreach_libblock_link_finalize_cb(LibraryIDLinkCallbackData *cb_data)
void BKE_scene_object_base_flag_sync_from_base(Base *base)
Definition scene.cc:3111
void BLO_library_link_end(Main *mainl, BlendHandle **bh, const LibraryLink_Params *params, ReportList *reports)
Definition readfile.cc:5935
@ FILE_ACTIVE_COLLECTION
void BKE_blendfile_link_append_context_finalize(BlendfileLinkAppendContext *lapp_context)
size_t BKE_id_multi_tagged_delete(Main *bmain, const BKEIDDeleteOptions &options={}) ATTR_NONNULL()
@ BKE_LIBLINK_OVERRIDE_USE_EXISTING_LIBOVERRIDES
Collection * BKE_collection_parent_editable_find_recursive(const ViewLayer *view_layer, Collection *collection)
bool BKE_view_layer_synced_ensure(const Main &bmain, const Scene *scene, ViewLayer *view_layer)
static void loose_data_gather_instanciated_objects_for_viewlayer(LooseDataInstantiateContext &instantiate_context, const Scene &scene, ViewLayer &view_layer, Set< Object * > &r_instanciated_objects)
void BKE_blendfile_link_append_context_flag_set(BlendfileLinkAppendContext *lapp_context, int flag, bool do_set)
static void new_id_to_item_mapping_create(BlendfileLinkAppendContext &lapp_context)
static void loose_data_instantiate_obdata_preprocess(LooseDataInstantiateContext *instantiate_context)
static void loose_data_instantiate_object_process(LooseDataInstantiateContext *instantiate_context)
bool BKE_collection_object_add(Main *bmain, Collection *collection, Object *ob)
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
bool BKE_collection_has_collection(const Collection *parent, const Collection *collection)
static void loose_data_instantiate_object_base_instance_init(Main *bmain, Collection *collection, Object *ob, const Scene *scene, ViewLayer *view_layer, const View3D *v3d, const int flag, bool set_active)
void BKE_main_namemap_clear(Main &bmain)
@ LIBRARY_FLAG_IS_ARCHIVE
void BKE_blendfile_append(BlendfileLinkAppendContext *lapp_context, ReportList *reports)
void BKE_ntree_update_tag_all(bNodeTree *ntree)
void BKE_main_id_tag_all(Main *mainvar, int tag, bool value)
Definition lib_id.cc:1314
int BKE_object_obdata_to_type(const ID *id) ATTR_NONNULL(1)
Definition object.cc:2230
void BKE_lib_override_library_main_operations_create(Main *bmain, bool force_auto, int *r_report_flags)
static void blendfile_library_relocate_id_remap_finalize(Main *bmain, Map< ID *, ID * > &old_owner_id_to_shapekey, ID *old_id, ID *new_id, ReportList *reports, const bool do_reload)
void BKE_lib_override_library_main_resync(Main *bmain, const Map< Library *, Library * > *new_to_old_libraries_map, Scene *scene, ViewLayer *view_layer, BlendFileReadReport *reports)
Main * BLO_library_link_begin(BlendHandle **bh, const char *filepath, const LibraryLink_Params *params)
Definition readfile.cc:5775
static void loose_data_instantiate_ensure_target_collection(LooseDataInstantiateContext *instantiate_context)
void BKE_main_id_newptr_and_tag_clear(Main *bmain)
Definition lib_id.cc:2097
ViewLayer * BKE_view_layer_find(const Scene *scene, const char *layer_name)
static void blendfile_library_relocate_id_remap(BlendfileLinkAppendContext &lapp_context, ReportList *reports, const bool do_reload, const int remap_flags)
void BKE_blendfile_link_pack(BlendfileLinkAppendContext *lapp_context, ReportList *reports)
bool BKE_collection_is_content_editable(const Collection *collection, std::string *reason=nullptr)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
static bool loose_data_instantiate_collection_parents_check_recursive(Collection *collection)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
static void blendfile_relocate_postprocess_cleanup(BlendfileLinkAppendContext &lapp_context)
void BKE_blendfile_library_relocate(BlendfileLinkAppendContext *lapp_context, ReportList *reports, Library *library, bool do_reload)
void BKE_blendfile_link_append_context_library_add(BlendfileLinkAppendContext *lapp_context, const char *libname, BlendHandle *blo_handle)
static void new_id_to_item_mapping_add(BlendfileLinkAppendContext &lapp_context, ID *id, BlendfileLinkAppendContextItem &item)
void BKE_main_unlock(Main *bmain)
Definition main.cc:666
ID_Type GS(const char *name)
Definition DNA_ID.h:410
@ LINK_APPEND_TAG_LIBOVERRIDE_DEPENDENCY_ONLY
Base * BKE_view_layer_base_find(ViewLayer *view_layer, Object *ob)
@ ID_TAG_MISSING
Definition DNA_ID.h:870
@ ID_TAG_INDIRECT
Definition DNA_ID.h:851
@ ID_TAG_RUNTIME
Definition DNA_ID.h:862
@ ID_TAG_PRE_EXISTING
Definition DNA_ID.h:929
@ ID_TAG_EXTERN
Definition DNA_ID.h:845
@ ID_TAG_DOIT
Definition DNA_ID.h:1029
LinkNode * BLO_blendhandle_get_datablock_names(BlendHandle *bh, int ofblocktype, bool use_assets_only, int *r_tot_names)
void BKE_libblock_remap_multiple_locked(Main *bmain, bke::id::IDRemapper &mappings, const int remap_flags)
Definition lib_remap.cc:654
static CLG_LogRef LOG
Definition logging.cpp:12
void BKE_main_lock(Main *bmain)
Definition main.cc:661
@ BKE_BLENDFILE_LINK_APPEND_FOREACH_ITEM_FLAG_DO_INDIRECT
@ BKE_BLENDFILE_LINK_APPEND_FOREACH_ITEM_FLAG_DO_DIRECT
void BKE_blendfile_link(BlendfileLinkAppendContext *lapp_context, ReportList *reports)
@ ID_FLAG_FAKEUSER
Definition DNA_ID.h:770
@ LIB_ID_MAKELOCAL_LIBOVERRIDE_CLEAR
@ LIB_ID_MAKELOCAL_FULL_LIBRARY
@ LIB_ID_MAKELOCAL_FORCE_LOCAL
@ LIB_ID_MAKELOCAL_ASSET_DATA_CLEAR
@ LIB_ID_MAKELOCAL_FORCE_COPY
bool BKE_collection_has_object_recursive(Collection *collection, Object *ob)
@ IDWALK_CB_LOOPBACK
@ IDWALK_CB_EMBEDDED_NOT_OWNING
@ IDWALK_CB_INTERNAL
@ IDWALK_CB_OVERRIDE_LIBRARY_REFERENCE
@ IDWALK_CB_EMBEDDED
void BKE_main_id_indirect_linked_update(Main &bmain, std::optional< Span< ID * > > local_ids=std::nullopt)
Definition lib_id.cc:2211
Object * BKE_object_add_only_object(Main *bmain, ObjectType type, const char *name) ATTR_RETURNS_NONNULL
Definition object.cc:2265
void BKE_blendfile_link_append_context_item_foreach(BlendfileLinkAppendContext *lapp_context, FunctionRef< bool(BlendfileLinkAppendContext *lapp_context, BlendfileLinkAppendContextItem *item)> callback_function, eBlendfileLinkAppendForeachItemFlag flag)
bool BKE_lib_id_make_local(Main *bmain, ID *id, int flags)
Definition lib_id.cc:672
bool BKE_main_collection_sync(const Main *bmain)
int BKE_blendfile_link_append_context_item_idtypes_from_library_add(BlendfileLinkAppendContext *lapp_context, ReportList *reports, uint64_t id_types_filter, int library_index)
void BLO_readfile_id_runtime_data_free_all(Main &bmain)
Definition readfile.cc:2318
size_t BKE_id_multi_delete(Main *bmain, Set< ID * > &ids_to_delete, const BKEIDDeleteOptions &options={})
void id_us_plus_no_lib(ID *id)
Definition lib_id.cc:417
void id_us_min(ID *id)
Definition lib_id.cc:441
void id_fake_user_clear(ID *id)
Definition lib_id.cc:479
void BKE_library_foreach_ID_link(Main *bmain, ID *id, FunctionRef< LibraryIDLinkCallback > callback, void *user_data, LibraryForeachIDFlag flag)
Definition lib_query.cc:449
void BKE_blendfile_link_append_context_init_done(BlendfileLinkAppendContext *lapp_context)
static ID * loose_data_instantiate_process_check(LooseDataInstantiateContext *instantiate_context, BlendfileLinkAppendContextItem *item)
static void blendfile_relocate_postprocess_liboverrides(BlendfileLinkAppendContext &lapp_context, const Map< Library *, Library * > &new_to_old_libraries_map, ReportList *reports)
ListBaseT< ID > * which_libbase(Main *bmain, short type)
Definition main.cc:1073
MainLibraryWeakReferenceMap * BKE_main_library_weak_reference_create(Main *bmain) ATTR_NONNULL()
Definition main.cc:828
void BKE_blendfile_override(BlendfileLinkAppendContext *lapp_context, const eBKELibLinkOverride flags, ReportList *reports)
static void blendfile_append_define_actions(BlendfileLinkAppendContext &lapp_context, ReportList *reports)
Collection * BKE_collection_add(Main *bmain, Collection *collection_parent, const char *name_custom)
void BKE_callback_exec(Main *bmain, PointerRNA **pointers, int pointers_num, eCbEvent evt)
Definition callbacks.cc:29
unsigned short local_view_bits
Vector< BlendfileLinkAppendContextLibrary > libraries
Map< ID *, BlendfileLinkAppendContextItem * > new_id_to_item
std::list< BlendfileLinkAppendContextItem > items
MainLibraryWeakReferenceMap * library_weak_reference_mapping
bke::CollectionRuntime * runtime
ListBaseT< CollectionObject > gobject
char name[258]
Definition DNA_ID.h:433
struct ID * newid
Definition DNA_ID.h:419
struct Library * lib
Definition DNA_ID.h:421
short flag
Definition DNA_ID.h:439
IDOverrideLibrary * override_library
Definition DNA_ID.h:495
struct Collection * collection
LibraryForeachIDCallbackFlag cb_flag
struct Library * archive_parent_library
Definition DNA_ID.h:575
char filepath[1024]
Definition DNA_ID.h:562
bke::library::LibraryRuntime * runtime
Definition DNA_ID.h:591
uint16_t flag
Definition DNA_ID.h:565
struct LinkNode * next
BlendfileLinkAppendContext * lapp_context
Library * curlib
Definition BKE_main.hh:366
short versionfile
Definition BKE_main.hh:269
short subversionfile
Definition BKE_main.hh:270
ListBaseT< Scene > scenes
Definition BKE_main.hh:386
eObject_VisibilityFlag visibility_flag
eObject_TransFlag transflag
struct Collection * instance_collection
ListBaseT< ViewLayer > view_layers
struct Collection * master_collection
View3DCursor cursor
unsigned short local_view_uid
ListBaseT< CollectionParent > parents