Blender  V3.3
blendfile_link_append.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
11 #include <stdlib.h>
12 #include <string.h>
13 
14 #include "CLG_log.h"
15 
16 #include "MEM_guardedalloc.h"
17 
18 #include "DNA_ID.h"
19 #include "DNA_collection_types.h"
20 #include "DNA_key_types.h"
21 #include "DNA_object_types.h"
22 #include "DNA_scene_types.h"
23 #include "DNA_screen_types.h"
24 #include "DNA_space_types.h"
25 
26 #include "BLI_bitmap.h"
27 #include "BLI_blenlib.h"
28 #include "BLI_ghash.h"
29 #include "BLI_linklist.h"
30 #include "BLI_math.h"
31 #include "BLI_memarena.h"
32 #include "BLI_utildefines.h"
33 
34 #include "BLT_translation.h"
35 
36 #include "BKE_idtype.h"
37 #include "BKE_key.h"
38 #include "BKE_layer.h"
39 #include "BKE_lib_id.h"
40 #include "BKE_lib_override.h"
41 #include "BKE_lib_query.h"
42 #include "BKE_lib_remap.h"
43 #include "BKE_main.h"
44 #include "BKE_material.h"
45 #include "BKE_object.h"
46 #include "BKE_report.h"
47 #include "BKE_rigidbody.h"
48 #include "BKE_scene.h"
49 
51 
52 #include "BLO_readfile.h"
53 #include "BLO_writefile.h"
54 
55 static CLG_LogRef LOG = {"bke.blendfile_link_append"};
56 
57 /* -------------------------------------------------------------------- */
63  char *name;
67  short idcode;
68 
71  char action;
72  char tag;
73 
80  void *userdata;
82 
83 /* A blendfile library entry in the `libraries` list of #BlendfileLinkAppendContext. */
85  char *path; /* Absolute .blend file path. */
86  BlendHandle *blo_handle; /* Blend file handle, if any. */
87  bool blo_handle_is_owned; /* Whether the blend file handle is owned, or borrowed. */
88  /* The blendfile report associated with the `blo_handle`, if owned. */
91 
98  int num_items;
101 
104 
107 
109  const void *blendfile_mem;
111 
115 
120 
122 
123 /* Actions to apply to an item (i.e. linked ID). */
124 enum {
130 };
131 
132 /* Various status info about an item (i.e. linked ID). */
133 enum {
134  /* An indirectly linked ID. */
136 };
137 
139  BlendfileLinkAppendContext *lapp_context,
141  ReportList *reports)
142 {
143  if (reports != NULL) {
144  lib_context->bf_reports.reports = reports;
145  }
146 
147  char *libname = lib_context->path;
148  BlendHandle *blo_handle = lib_context->blo_handle;
149  if (blo_handle == NULL) {
150  if (STREQ(libname, BLO_EMBEDDED_STARTUP_BLEND)) {
151  blo_handle = BLO_blendhandle_from_memory(lapp_context->blendfile_mem,
152  (int)lapp_context->blendfile_memsize,
153  &lib_context->bf_reports);
154  }
155  else {
156  blo_handle = BLO_blendhandle_from_file(libname, &lib_context->bf_reports);
157  }
158  lib_context->blo_handle = blo_handle;
159  lib_context->blo_handle_is_owned = true;
160  }
161 
162  return blo_handle;
163 }
164 
166  BlendfileLinkAppendContext *UNUSED(lapp_context),
168 {
169  if (lib_context->blo_handle_is_owned && lib_context->blo_handle != NULL) {
170  BLO_blendhandle_close(lib_context->blo_handle);
171  lib_context->blo_handle = NULL;
172  }
173 }
174 
176 {
178  BlendfileLinkAppendContext *lapp_context = BLI_memarena_calloc(ma, sizeof(*lapp_context));
179 
180  lapp_context->params = params;
181  lapp_context->memarena = ma;
182 
183  return lapp_context;
184 }
185 
187 {
188  if (lapp_context->new_id_to_item != NULL) {
189  BLI_ghash_free(lapp_context->new_id_to_item, NULL, NULL);
190  }
191 
192  for (LinkNode *liblink = lapp_context->libraries.list; liblink != NULL;
193  liblink = liblink->next) {
194  BlendfileLinkAppendContextLibrary *lib_context = liblink->link;
195  link_append_context_library_blohandle_release(lapp_context, lib_context);
196  }
197 
199 
200  BLI_memarena_free(lapp_context->memarena);
201 }
202 
204  const int flag,
205  const bool do_set)
206 {
207  if (do_set) {
208  lapp_context->params->flag |= flag;
209  }
210  else {
211  lapp_context->params->flag &= ~flag;
212  }
213 }
214 
216  BlendfileLinkAppendContext *lapp_context, const void *blendfile_mem, int blendfile_memsize)
217 {
218  BLI_assert_msg(lapp_context->blendfile_mem == NULL,
219  "Please explicitly clear reference to an embedded blender memfile before "
220  "setting a new one");
221  lapp_context->blendfile_mem = blendfile_mem;
222  lapp_context->blendfile_memsize = (size_t)blendfile_memsize;
223 }
224 
226  BlendfileLinkAppendContext *lapp_context)
227 {
228  lapp_context->blendfile_mem = NULL;
229  lapp_context->blendfile_memsize = 0;
230 }
231 
233  const char *libname,
234  BlendHandle *blo_handle)
235 {
236  BLI_assert(lapp_context->items.list == NULL);
237 
239  sizeof(*lib_context));
240 
241  size_t len = strlen(libname) + 1;
242  char *libpath = BLI_memarena_alloc(lapp_context->memarena, len);
243  BLI_strncpy(libpath, libname, len);
244 
245  lib_context->path = libpath;
246  lib_context->blo_handle = blo_handle;
247  lib_context->blo_handle_is_owned = (blo_handle == NULL);
248 
249  BLI_linklist_append_arena(&lapp_context->libraries, lib_context, lapp_context->memarena);
250  lapp_context->num_libraries++;
251 }
252 
254  BlendfileLinkAppendContext *lapp_context,
255  const char *idname,
256  const short idcode,
257  void *userdata)
258 {
260  sizeof(*item));
261  size_t len = strlen(idname) + 1;
262 
263  item->name = BLI_memarena_alloc(lapp_context->memarena, len);
264  BLI_strncpy(item->name, idname, len);
265  item->idcode = idcode;
266  item->libraries = BLI_BITMAP_NEW_MEMARENA(lapp_context->memarena, lapp_context->num_libraries);
267 
268  item->new_id = NULL;
270  item->userdata = userdata;
271 
272  BLI_linklist_append_arena(&lapp_context->items, item, lapp_context->memarena);
273  lapp_context->num_items++;
274 
275  return item;
276 }
277 
279  BlendfileLinkAppendContext *lapp_context,
280  ReportList *reports,
281  const uint64_t id_types_filter,
282  const int library_index)
283 {
284  int id_num = 0;
285  int id_code_iter = 0;
286  short id_code;
287 
288  LinkNode *lib_context_link = BLI_linklist_find(lapp_context->libraries.list, library_index);
289  BlendfileLinkAppendContextLibrary *lib_context = lib_context_link->link;
291  lapp_context, lib_context, reports);
292 
293  if (blo_handle == NULL) {
295  }
296 
297  const bool use_assets_only = (lapp_context->params->flag & FILE_ASSETS_ONLY) != 0;
298 
299  while ((id_code = BKE_idtype_idcode_iter_step(&id_code_iter))) {
300  if (!BKE_idtype_idcode_is_linkable(id_code) ||
301  (id_types_filter != 0 &&
302  (BKE_idtype_idcode_to_idfilter(id_code) & id_types_filter) == 0)) {
303  continue;
304  }
305 
306  int id_names_num;
308  blo_handle, id_code, use_assets_only, &id_names_num);
309 
310  for (LinkNode *link_next = NULL; id_names_list != NULL; id_names_list = link_next) {
311  link_next = id_names_list->next;
312 
313  char *id_name = id_names_list->link;
315  lapp_context, id_name, id_code, NULL);
317  lapp_context, item, library_index);
318 
320  MEM_freeN(id_names_list);
321  }
322 
323  id_num += id_names_num;
324  }
325 
326  return id_num;
327 }
328 
330  BlendfileLinkAppendContext *UNUSED(lapp_context),
332  const int library_index)
333 {
334  BLI_BITMAP_ENABLE(item->libraries, library_index);
335 }
336 
338 {
339  return lapp_context->num_items == 0;
340 }
341 
344 {
345  return item->userdata;
346 }
347 
350 {
351  return item->new_id;
352 }
353 
355  struct BlendfileLinkAppendContext *UNUSED(lapp_context),
356  struct BlendfileLinkAppendContextItem *item)
357 {
358  return item->idcode;
359 }
360 
362  struct BlendfileLinkAppendContext *lapp_context,
365  void *userdata)
366 {
367  for (LinkNode *itemlink = lapp_context->items.list; itemlink; itemlink = itemlink->next) {
368  BlendfileLinkAppendContextItem *item = itemlink->link;
369 
371  (item->tag & LINK_APPEND_TAG_INDIRECT) == 0) {
372  continue;
373  }
375  (item->tag & LINK_APPEND_TAG_INDIRECT) != 0) {
376  continue;
377  }
378 
379  if (!callback_function(lapp_context, item, userdata)) {
380  break;
381  }
382  }
383 }
384 
387 /* -------------------------------------------------------------------- */
392 /* Struct gathering all required data to handle instantiation of loose data-blocks. */
395 
396  /* The collection in which to add loose collections/objects. */
399 
400 static bool object_in_any_scene(Main *bmain, Object *ob)
401 {
402  LISTBASE_FOREACH (Scene *, sce, &bmain->scenes) {
403  /* #BKE_scene_has_object checks bases cache of the scenes' view-layer, not actual content of
404  * their collections. */
405  if (BKE_collection_has_object_recursive(sce->master_collection, ob)) {
406  return true;
407  }
408  }
409 
410  return false;
411 }
412 
413 static bool object_in_any_collection(Main *bmain, Object *ob)
414 {
415  LISTBASE_FOREACH (Collection *, collection, &bmain->collections) {
416  if (BKE_collection_has_object(collection, ob)) {
417  return true;
418  }
419  }
420 
421  LISTBASE_FOREACH (Scene *, scene, &bmain->scenes) {
422  if (scene->master_collection != NULL &&
424  return true;
425  }
426  }
427 
428  return false;
429 }
430 
431 static bool collection_instantiated_by_any_object(Main *bmain, Collection *collection)
432 {
433  LISTBASE_FOREACH (Object *, ob, &bmain->objects) {
434  if (ob->type == OB_EMPTY && ob->instance_collection == collection) {
435  return true;
436  }
437  }
438  return false;
439 }
440 
443 {
444  BlendfileLinkAppendContext *lapp_context = instantiate_context->lapp_context;
445  /* In linking case, we always want to handle instantiation. */
446  if (lapp_context->params->flag & FILE_LINK) {
447  return item->new_id;
448  }
449 
450  /* We consider that if we either kept it linked, or re-used already local data, instantiation
451  * status of those should not be modified. */
453  return NULL;
454  }
455 
456  ID *id = item->new_id;
457  if (id == NULL) {
458  return NULL;
459  }
460 
461  BLI_assert(!ID_IS_LINKED(id));
462  return id;
463 }
464 
466  LooseDataInstantiateContext *instantiate_context)
467 {
468 
469  BlendfileLinkAppendContext *lapp_context = instantiate_context->lapp_context;
470  Main *bmain = instantiate_context->lapp_context->params->bmain;
471  Scene *scene = instantiate_context->lapp_context->params->context.scene;
472  ViewLayer *view_layer = instantiate_context->lapp_context->params->context.view_layer;
473 
474  /* Find or add collection as needed. When `active_collection` is non-null, it is assumed to be
475  * editable. */
476  if (instantiate_context->active_collection == NULL) {
477  if (lapp_context->params->flag & FILE_ACTIVE_COLLECTION) {
480  view_layer, lc->collection);
481  }
482  else {
483  if (lapp_context->params->flag & FILE_LINK) {
484  instantiate_context->active_collection = BKE_collection_add(
485  bmain, scene->master_collection, DATA_("Linked Data"));
486  }
487  else {
488  instantiate_context->active_collection = BKE_collection_add(
489  bmain, scene->master_collection, DATA_("Appended Data"));
490  }
491  }
492  }
493 }
494 
496  Collection *collection,
497  Object *ob,
498  ViewLayer *view_layer,
499  const View3D *v3d,
500  const int flag,
501  bool set_active)
502 {
503  /* Auto-select and appending. */
504  if ((flag & FILE_AUTOSELECT) && ((flag & FILE_LINK) == 0)) {
505  /* While in general the object should not be manipulated,
506  * when the user requests the object to be selected, ensure it's visible and selectable. */
508  }
509 
510  BKE_collection_object_add(bmain, collection, ob);
511 
512  Base *base = BKE_view_layer_base_find(view_layer, ob);
513 
514  if (v3d != NULL) {
515  base->local_view_bits |= v3d->local_view_uuid;
516  }
517 
518  if (flag & FILE_AUTOSELECT) {
519  /* All objects that use #FILE_AUTOSELECT must be selectable (unless linking data). */
520  BLI_assert((base->flag & BASE_SELECTABLE) || (flag & FILE_LINK));
521  if (base->flag & BASE_SELECTABLE) {
522  base->flag |= BASE_SELECTED;
523  }
524  }
525 
526  if (set_active) {
527  view_layer->basact = base;
528  }
529 
531 }
532 
533 /* Tag obdata that actually need to be instantiated (those referenced by an object do not, since
534  * the object will be instantiated instead if needed. */
536  LooseDataInstantiateContext *instantiate_context)
537 {
538  BlendfileLinkAppendContext *lapp_context = instantiate_context->lapp_context;
539  LinkNode *itemlink;
540 
541  /* First pass on obdata to enable their instantiation by default, then do a second pass on
542  * objects to clear it for any obdata already in use. */
543  for (itemlink = lapp_context->items.list; itemlink; itemlink = itemlink->next) {
544  BlendfileLinkAppendContextItem *item = itemlink->link;
545  ID *id = loose_data_instantiate_process_check(instantiate_context, item);
546  if (id == NULL) {
547  continue;
548  }
549  const ID_Type idcode = GS(id->name);
550  if (!OB_DATA_SUPPORT_ID(idcode)) {
551  continue;
552  }
553 
554  id->tag |= LIB_TAG_DOIT;
555  }
556  for (itemlink = lapp_context->items.list; itemlink; itemlink = itemlink->next) {
557  BlendfileLinkAppendContextItem *item = itemlink->link;
558  ID *id = item->new_id;
559  if (id == NULL || GS(id->name) != ID_OB) {
560  continue;
561  }
562 
563  Object *ob = (Object *)id;
564  Object *new_ob = (Object *)id->newid;
565  if (ob->data != NULL) {
566  ((ID *)(ob->data))->tag &= ~LIB_TAG_DOIT;
567  }
568  if (new_ob != NULL && new_ob->data != NULL) {
569  ((ID *)(new_ob->data))->tag &= ~LIB_TAG_DOIT;
570  }
571  }
572 }
573 
574 /* Test whether some ancestor collection is also tagged for instantiation (return true) or not
575  * (return false). */
577 {
578  for (CollectionParent *parent_collection = collection->parents.first; parent_collection != NULL;
579  parent_collection = parent_collection->next) {
580  if ((parent_collection->collection->id.tag & LIB_TAG_DOIT) != 0) {
581  return true;
582  }
583  if (loose_data_instantiate_collection_parents_check_recursive(parent_collection->collection)) {
584  return true;
585  }
586  }
587  return false;
588 }
589 
591  LooseDataInstantiateContext *instantiate_context)
592 {
593  BlendfileLinkAppendContext *lapp_context = instantiate_context->lapp_context;
594  Main *bmain = lapp_context->params->bmain;
595  Scene *scene = lapp_context->params->context.scene;
596  ViewLayer *view_layer = lapp_context->params->context.view_layer;
597  const View3D *v3d = lapp_context->params->context.v3d;
598 
599  const bool do_append = (lapp_context->params->flag & FILE_LINK) == 0;
600  const bool do_instantiate_as_empty = (lapp_context->params->flag &
602 
603  /* NOTE: For collections we only view_layer-instantiate duplicated collections that have
604  * non-instantiated objects in them.
605  * NOTE: Also avoid view-layer-instantiating of collections children of other instantiated
606  * collections. This is why we need two passes here. */
607  LinkNode *itemlink;
608  for (itemlink = lapp_context->items.list; itemlink; itemlink = itemlink->next) {
609  BlendfileLinkAppendContextItem *item = itemlink->link;
610  ID *id = loose_data_instantiate_process_check(instantiate_context, item);
611  if (id == NULL || GS(id->name) != ID_GR) {
612  continue;
613  }
614 
615  /* Forced instantiation of indirectly appended collections is not wanted. Users can now
616  * easily instantiate collections (and their objects) as needed by themselves. See T67032. */
617  /* We need to check that objects in that collections are already instantiated in a scene.
618  * Otherwise, it's better to add the collection to the scene's active collection, than to
619  * instantiate its objects in active scene's collection directly. See T61141.
620  *
621  * NOTE: We only check object directly into that collection, not recursively into its
622  * children.
623  */
624  Collection *collection = (Collection *)id;
625  /* The collection could be linked/appended together with an Empty object instantiating it,
626  * better not instantiate the collection in the view-layer in that case.
627  *
628  * Can easily happen when copy/pasting such instantiating empty, see T93839. */
629  const bool collection_is_instantiated = collection_instantiated_by_any_object(bmain,
630  collection);
631  /* Always consider adding collections directly selected by the user. */
632  bool do_add_collection = (item->tag & LINK_APPEND_TAG_INDIRECT) == 0 &&
633  !collection_is_instantiated;
634  /* In linking case, do not enforce instantiating non-directly linked collections/objects.
635  * This avoids cluttering the view-layers, user can instantiate themselves specific collections
636  * or objects easily from the Outliner if needed. */
637  if (!do_add_collection && do_append && !collection_is_instantiated) {
638  LISTBASE_FOREACH (CollectionObject *, coll_ob, &collection->gobject) {
639  Object *ob = coll_ob->ob;
640  if (!object_in_any_scene(bmain, ob)) {
641  do_add_collection = true;
642  break;
643  }
644  }
645  }
646  if (do_add_collection) {
647  collection->id.tag |= LIB_TAG_DOIT;
648  }
649  }
650 
651  /* Second loop to actually instantiate collections tagged as such in first loop, unless some of
652  * their ancestor is also instantiated in case this is not an empty-instantiation. */
653  for (itemlink = lapp_context->items.list; itemlink; itemlink = itemlink->next) {
654  BlendfileLinkAppendContextItem *item = itemlink->link;
655  ID *id = loose_data_instantiate_process_check(instantiate_context, item);
656  if (id == NULL || GS(id->name) != ID_GR) {
657  continue;
658  }
659 
660  Collection *collection = (Collection *)id;
661  bool do_add_collection = (id->tag & LIB_TAG_DOIT) != 0;
662 
663  if (!do_add_collection) {
664  continue;
665  }
666  /* When instantiated into view-layer, do not add collections if one of their parents is also
667  * instantiated. */
668  if (!do_instantiate_as_empty &&
670  continue;
671  }
672  /* When instantiated as empty, do not add indirectly linked (i.e. non-user-selected)
673  * collections. */
674  if (do_instantiate_as_empty && (item->tag & LINK_APPEND_TAG_INDIRECT) != 0) {
675  continue;
676  }
677 
679  Collection *active_collection = instantiate_context->active_collection;
680 
681  if (do_instantiate_as_empty) {
682  /* BKE_object_add(...) messes with the selection. */
683  Object *ob = BKE_object_add_only_object(bmain, OB_EMPTY, collection->id.name + 2);
684  ob->type = OB_EMPTY;
685  ob->empty_drawsize = U.collection_instance_empty_size;
686 
687  const bool set_selected = (lapp_context->params->flag & FILE_AUTOSELECT) != 0;
688  /* TODO: why is it OK to make this active here but not in other situations?
689  * See other callers of #object_base_instance_init */
690  const bool set_active = set_selected;
692  bmain, active_collection, ob, view_layer, v3d, lapp_context->params->flag, set_active);
693 
694  /* Assign the collection. */
695  ob->instance_collection = collection;
696  id_us_plus(&collection->id);
699  }
700  else {
701  /* Add collection as child of active collection. */
702  BKE_collection_child_add(bmain, active_collection, collection);
703 
704  if ((lapp_context->params->flag & FILE_AUTOSELECT) != 0) {
705  LISTBASE_FOREACH (CollectionObject *, coll_ob, &collection->gobject) {
706  Object *ob = coll_ob->ob;
707  Base *base = BKE_view_layer_base_find(view_layer, ob);
708  if (base) {
709  base->flag |= BASE_SELECTED;
711  }
712  }
713  }
714  }
715  }
716 }
717 
719 {
720  BlendfileLinkAppendContext *lapp_context = instantiate_context->lapp_context;
721  Main *bmain = lapp_context->params->bmain;
722  ViewLayer *view_layer = lapp_context->params->context.view_layer;
723  const View3D *v3d = lapp_context->params->context.v3d;
724 
725  /* Do NOT make base active here! screws up GUI stuff,
726  * if you want it do it at the editor level. */
727  const bool object_set_active = false;
728 
729  const bool is_linking = (lapp_context->params->flag & FILE_LINK) != 0;
730 
731  /* NOTE: For objects we only view_layer-instantiate duplicated objects that are not yet used
732  * anywhere. */
733  LinkNode *itemlink;
734  for (itemlink = lapp_context->items.list; itemlink; itemlink = itemlink->next) {
735  BlendfileLinkAppendContextItem *item = itemlink->link;
736  ID *id = loose_data_instantiate_process_check(instantiate_context, item);
737  if (id == NULL || GS(id->name) != ID_OB) {
738  continue;
739  }
740 
741  /* In linking case, never instantiate stray objects that are not directly linked.
742  *
743  * While this is not ideal (in theory no object should remain un-owned), in case of indirectly
744  * linked objects, the other solution would be to add them to a local collection, which would
745  * make them directly linked. Think for now keeping them indirectly linked is more important.
746  * Ref. T93757.
747  */
748  if (is_linking && (item->tag & LINK_APPEND_TAG_INDIRECT) != 0) {
749  continue;
750  }
751 
752  Object *ob = (Object *)id;
753 
754  if (object_in_any_collection(bmain, ob)) {
755  continue;
756  }
757 
759  Collection *active_collection = instantiate_context->active_collection;
760 
761  CLAMP_MIN(ob->id.us, 0);
762  ob->mode = OB_MODE_OBJECT;
763 
765  active_collection,
766  ob,
767  view_layer,
768  v3d,
769  lapp_context->params->flag,
770  object_set_active);
771  }
772 }
773 
775 {
776  BlendfileLinkAppendContext *lapp_context = instantiate_context->lapp_context;
777  Main *bmain = lapp_context->params->bmain;
778  Scene *scene = lapp_context->params->context.scene;
779  ViewLayer *view_layer = lapp_context->params->context.view_layer;
780  const View3D *v3d = lapp_context->params->context.v3d;
781 
782  /* Do NOT make base active here! screws up GUI stuff,
783  * if you want it do it at the editor level. */
784  const bool object_set_active = false;
785 
786  LinkNode *itemlink;
787  for (itemlink = lapp_context->items.list; itemlink; itemlink = itemlink->next) {
788  BlendfileLinkAppendContextItem *item = itemlink->link;
789  ID *id = loose_data_instantiate_process_check(instantiate_context, item);
790  if (id == NULL) {
791  continue;
792  }
793  const ID_Type idcode = GS(id->name);
794  if (!OB_DATA_SUPPORT_ID(idcode)) {
795  continue;
796  }
797  if ((id->tag & LIB_TAG_DOIT) == 0) {
798  continue;
799  }
800 
802  Collection *active_collection = instantiate_context->active_collection;
803 
804  const int type = BKE_object_obdata_to_type(id);
805  BLI_assert(type != -1);
806  Object *ob = BKE_object_add_only_object(bmain, type, id->name + 2);
807  ob->data = id;
808  id_us_plus(id);
809  BKE_object_materials_test(bmain, ob, ob->data);
810 
812  active_collection,
813  ob,
814  view_layer,
815  v3d,
816  lapp_context->params->flag,
817  object_set_active);
818 
820 
821  id->tag &= ~LIB_TAG_DOIT;
822  }
823 }
824 
826  LooseDataInstantiateContext *instantiate_context)
827 {
828  BlendfileLinkAppendContext *lapp_context = instantiate_context->lapp_context;
829  Main *bmain = lapp_context->params->bmain;
830 
831  LinkNode *itemlink;
832  /* Add rigid body objects and constraints to current RB world(s). */
833  for (itemlink = lapp_context->items.list; itemlink; itemlink = itemlink->next) {
834  BlendfileLinkAppendContextItem *item = itemlink->link;
835  ID *id = loose_data_instantiate_process_check(instantiate_context, item);
836  if (id == NULL || GS(id->name) != ID_OB) {
837  continue;
838  }
840  }
841 }
842 
843 static void loose_data_instantiate(LooseDataInstantiateContext *instantiate_context)
844 {
845  if (instantiate_context->lapp_context->params->context.scene == NULL) {
846  /* In some cases, like the asset drag&drop e.g., the caller code manages instantiation itself.
847  */
848  return;
849  }
850 
851  BlendfileLinkAppendContext *lapp_context = instantiate_context->lapp_context;
852  const bool do_obdata = (lapp_context->params->flag & BLO_LIBLINK_OBDATA_INSTANCE) != 0;
853 
854  /* First pass on obdata to enable their instantiation by default, then do a second pass on
855  * objects to clear it for any obdata already in use. */
856  if (do_obdata) {
857  loose_data_instantiate_obdata_preprocess(instantiate_context);
858  }
859 
860  /* First do collections, then objects, then obdata. */
861  loose_data_instantiate_collection_process(instantiate_context);
862  loose_data_instantiate_object_process(instantiate_context);
863  if (do_obdata) {
864  loose_data_instantiate_obdata_process(instantiate_context);
865  }
866 
868 }
869 
871  ID *id,
873 {
874  BLI_ghash_insert(lapp_context->new_id_to_item, id, item);
875 
876  /* This ensures that if a liboverride reference is also linked/used by some other appended
877  * data, it gets a local copy instead of being made directly local, so that the liboverride
878  * references remain valid (i.e. linked data). */
879  if (ID_IS_OVERRIDE_LIBRARY_REAL(id)) {
880  id->override_library->reference->tag |= LIB_TAG_PRE_EXISTING;
881  }
882 }
883 
884 /* Generate a mapping between newly linked IDs and their items, and tag linked IDs used as
885  * liboverride references as already existing. */
887 {
888  lapp_context->new_id_to_item = BLI_ghash_new(
890  for (LinkNode *itemlink = lapp_context->items.list; itemlink; itemlink = itemlink->next) {
891  BlendfileLinkAppendContextItem *item = itemlink->link;
892  ID *id = item->new_id;
893  if (id == NULL) {
894  continue;
895  }
896 
897  new_id_to_item_mapping_add(lapp_context, id, item);
898  }
899 }
900 
902 {
903  /* NOTE: It is important to also skip liboverride references here, as those should never be made
904  * local. */
907  return IDWALK_RET_NOP;
908  }
909 
911  ID *id = *cb_data->id_pointer;
912 
913  if (id == NULL) {
914  return IDWALK_RET_NOP;
915  }
916 
918  /* While we do not want to add non-linkable ID (shape keys...) to the list of linked items,
919  * unfortunately they can use fully linkable valid IDs too, like actions. Those need to be
920  * processed, so we need to recursively deal with them here. */
921  /* NOTE: Since we are by-passing checks in `BKE_library_foreach_ID_link` by manually calling it
922  * recursively, we need to take care of potential recursion cases ourselves (e.g.anim-data of
923  * shape-key referencing the shape-key itself).
924  * NOTE: in case both IDs (owner and 'used' ones) are non-linkable, we can assume we can break
925  * the dependency here. Indeed, either they are both linked in another way (through their own
926  * meshes for shape keys e.g.), or this is an unsupported case (two shape-keys depending on
927  * each-other need to be also 'linked' in by their respective meshes, independent shape-keys
928  * are not allowed). ref T96048. */
929  if (id != cb_data->id_self && BKE_idtype_idcode_is_linkable(GS(cb_data->id_self->name))) {
932  }
933  return IDWALK_RET_NOP;
934  }
935 
936  /* In linking case, we always consider all linked IDs, even indirectly ones, for instantiation,
937  * so we need to add them all to the items list.
938  *
939  * In appending case, when `do_recursive` is false, we only make local IDs from same
940  * library(-ies) as the initially directly linked ones.
941  *
942  * NOTE: Since in append case, linked IDs are also fully skipped during instantiation step (see
943  * #append_loose_data_instantiate_process_check), we can avoid adding them to the items list
944  * completely. */
945  const bool do_link = (data->lapp_context->params->flag & FILE_LINK) != 0;
946  const bool do_recursive = (data->lapp_context->params->flag & BLO_LIBLINK_APPEND_RECURSIVE) !=
947  0 ||
948  do_link;
949  if (!do_recursive && cb_data->id_owner->lib != id->lib) {
950  return IDWALK_RET_NOP;
951  }
952 
953  BlendfileLinkAppendContextItem *item = BLI_ghash_lookup(data->lapp_context->new_id_to_item, id);
954  if (item == NULL) {
956  data->lapp_context, id->name, GS(id->name), NULL);
957  item->new_id = id;
958  item->source_library = id->lib;
959  /* Since we did not have an item for that ID yet, we know user did not selected it explicitly,
960  * it was rather linked indirectly. This info is important for instantiation of collections. */
961  item->tag |= LINK_APPEND_TAG_INDIRECT;
962  /* In linking case we already know what we want to do with those items. */
963  if (do_link) {
965  }
966  new_id_to_item_mapping_add(data->lapp_context, id, item);
967  }
968 
969  /* NOTE: currently there is no need to do anything else here, but in the future this would be
970  * the place to add specific per-usage decisions on how to append an ID. */
971 
972  return IDWALK_RET_NOP;
973 }
974 
981 {
982  /* NOTE: Do not bother checking file versions here, if there are no proxies to convert this code
983  * is quite fast anyway. */
984 
985  BlendFileReadReport bf_reports = {.reports = reports};
987 
988  if (bf_reports.count.proxies_to_lib_overrides_success != 0 ||
989  bf_reports.count.proxies_to_lib_overrides_failures != 0) {
990  BKE_reportf(
991  bf_reports.reports,
992  RPT_WARNING,
993  "Proxies have been removed from Blender (%d proxies were automatically converted "
994  "to library overrides, %d proxies could not be converted and were cleared). "
995  "Please consider re-saving any library .blend file with the newest Blender version",
998  }
999 }
1000 
1002 {
1003  if (lapp_context->num_items == 0) {
1004  /* Nothing to append. */
1005  return;
1006  }
1007 
1008  Main *bmain = lapp_context->params->bmain;
1009 
1010  BLI_assert((lapp_context->params->flag & FILE_LINK) == 0);
1011 
1012  const bool set_fakeuser = (lapp_context->params->flag & BLO_LIBLINK_APPEND_SET_FAKEUSER) != 0;
1013  const bool do_reuse_local_id = (lapp_context->params->flag &
1015 
1016  const int make_local_common_flags = LIB_ID_MAKELOCAL_FULL_LIBRARY |
1017  ((lapp_context->params->flag &
1020  0);
1021 
1022  LinkNode *itemlink;
1023 
1024  new_id_to_item_mapping_create(lapp_context);
1026 
1027  /* NOTE: Since we append items for IDs not already listed (i.e. implicitly linked indirect
1028  * dependencies), this list will grow and we will process those IDs later, leading to a flatten
1029  * recursive processing of all the linked dependencies. */
1030  for (itemlink = lapp_context->items.list; itemlink; itemlink = itemlink->next) {
1031  BlendfileLinkAppendContextItem *item = itemlink->link;
1032  ID *id = item->new_id;
1033  if (id == NULL) {
1034  continue;
1035  }
1036  BLI_assert(item->userdata == NULL);
1037 
1038  ID *existing_local_id = BKE_idtype_idcode_append_is_reusable(GS(id->name)) ?
1040  lapp_context->library_weak_reference_mapping,
1041  id->lib->filepath,
1042  id->name) :
1043  NULL;
1044 
1045  if (item->action != LINK_APPEND_ACT_UNSET) {
1046  /* Already set, pass. */
1047  }
1048  else if (do_reuse_local_id && existing_local_id != NULL) {
1049  CLOG_INFO(&LOG, 3, "Appended ID '%s' as a matching local one, re-using it...", id->name);
1051  item->userdata = existing_local_id;
1052  }
1053  else if (id->tag & LIB_TAG_PRE_EXISTING) {
1054  CLOG_INFO(&LOG, 3, "Appended ID '%s' was already linked, need to copy it...", id->name);
1056  }
1057  else {
1058  CLOG_INFO(&LOG, 3, "Appended ID '%s' will be made local...", id->name);
1060  }
1061 
1062  /* Only check dependencies if we are not keeping linked data, nor re-using existing local data.
1063  */
1066  .lapp_context = lapp_context, .item = item, .reports = reports};
1068  bmain, id, foreach_libblock_link_append_callback, &cb_data, IDWALK_NOP);
1069  }
1070 
1071  /* If we found a matching existing local id but are not re-using it, we need to properly clear
1072  * its weak reference to linked data. */
1073  if (existing_local_id != NULL &&
1076  id->lib->filepath,
1077  id->name,
1078  existing_local_id);
1079  }
1080  }
1081 
1082  /* Effectively perform required operation on every linked ID. */
1083  for (itemlink = lapp_context->items.list; itemlink; itemlink = itemlink->next) {
1084  BlendfileLinkAppendContextItem *item = itemlink->link;
1085  ID *id = item->new_id;
1086  if (id == NULL) {
1087  continue;
1088  }
1089 
1090  ID *local_appended_new_id = NULL;
1091  char lib_filepath[FILE_MAX];
1092  BLI_strncpy(lib_filepath, id->lib->filepath, sizeof(lib_filepath));
1093  char lib_id_name[MAX_ID_NAME];
1094  BLI_strncpy(lib_id_name, id->name, sizeof(lib_id_name));
1095 
1096  switch (item->action) {
1098  BKE_lib_id_make_local(bmain, id, make_local_common_flags | LIB_ID_MAKELOCAL_FORCE_COPY);
1099  local_appended_new_id = id->newid;
1100  break;
1102  BKE_lib_id_make_local(bmain, id, make_local_common_flags | LIB_ID_MAKELOCAL_FORCE_LOCAL);
1103  BLI_assert(id->newid == NULL);
1104  local_appended_new_id = id;
1105  break;
1107  /* Nothing to do here. */
1108  break;
1110  /* We only need to set `newid` to ID found in previous loop, for proper remapping. */
1111  ID_NEW_SET(id, item->userdata);
1112  /* This is not a 'new' local appended id, do not set `local_appended_new_id` here. */
1113  break;
1114  case LINK_APPEND_ACT_UNSET:
1115  CLOG_ERROR(
1116  &LOG, "Unexpected unset append action for '%s' ID, assuming 'keep link'", id->name);
1117  break;
1118  default:
1120  }
1121 
1122  if (local_appended_new_id != NULL) {
1123  if (BKE_idtype_idcode_append_is_reusable(GS(local_appended_new_id->name))) {
1125  lib_filepath,
1126  lib_id_name,
1127  local_appended_new_id);
1128  }
1129 
1130  if (set_fakeuser) {
1131  if (!ELEM(GS(local_appended_new_id->name), ID_OB, ID_GR)) {
1132  /* Do not set fake user on objects nor collections (instancing). */
1133  id_fake_user_set(local_appended_new_id);
1134  }
1135  }
1136  }
1137  }
1138 
1140  lapp_context->library_weak_reference_mapping = NULL;
1141 
1142  /* Remap IDs as needed. */
1143  for (itemlink = lapp_context->items.list; itemlink; itemlink = itemlink->next) {
1144  BlendfileLinkAppendContextItem *item = itemlink->link;
1145 
1146  if (item->action == LINK_APPEND_ACT_KEEP_LINKED) {
1147  continue;
1148  }
1149 
1150  ID *id = item->new_id;
1151  if (id == NULL) {
1152  continue;
1153  }
1155  BLI_assert(ID_IS_LINKED(id));
1156  id = id->newid;
1157  if (id == NULL) {
1158  continue;
1159  }
1160  }
1161 
1162  BLI_assert(!ID_IS_LINKED(id));
1163 
1164  BKE_libblock_relink_to_newid(bmain, id, 0);
1165  }
1166 
1167  /* Remove linked IDs when a local existing data has been reused instead. */
1168  BKE_main_id_tag_all(bmain, LIB_TAG_DOIT, false);
1169  for (itemlink = lapp_context->items.list; itemlink; itemlink = itemlink->next) {
1170  BlendfileLinkAppendContextItem *item = itemlink->link;
1171 
1173  continue;
1174  }
1175 
1176  ID *id = item->new_id;
1177  if (id == NULL) {
1178  continue;
1179  }
1180  BLI_assert(ID_IS_LINKED(id));
1181  BLI_assert(id->newid != NULL);
1182 
1183  /* Calling code may want to access newly appended IDs from the link/append context items. */
1184  item->new_id = id->newid;
1185 
1186  /* Do NOT delete a linked data that was already linked before this append. */
1187  if (id->tag & LIB_TAG_PRE_EXISTING) {
1188  continue;
1189  }
1190 
1191  id->tag |= LIB_TAG_DOIT;
1192  }
1194 
1195  /* Instantiate newly created (duplicated) IDs as needed. */
1196  LooseDataInstantiateContext instantiate_context = {.lapp_context = lapp_context,
1197  .active_collection = NULL};
1198  loose_data_instantiate(&instantiate_context);
1199 
1201 
1202  blendfile_link_append_proxies_convert(bmain, reports);
1203 }
1204 
1206 {
1207  if (lapp_context->num_items == 0) {
1208  /* Nothing to be linked. */
1209  return;
1210  }
1211 
1212  BLI_assert(lapp_context->num_libraries != 0);
1213 
1214  Main *mainl;
1215  Library *lib;
1216 
1217  LinkNode *liblink, *itemlink;
1218  int lib_idx, item_idx;
1219 
1220  for (lib_idx = 0, liblink = lapp_context->libraries.list; liblink;
1221  lib_idx++, liblink = liblink->next) {
1222  BlendfileLinkAppendContextLibrary *lib_context = liblink->link;
1223  char *libname = lib_context->path;
1225  lapp_context, lib_context, reports);
1226 
1227  if (blo_handle == NULL) {
1228  /* Unlikely since we just browsed it, but possible
1229  * Error reports will have been made by BLO_blendhandle_from_file() */
1230  continue;
1231  }
1232 
1233  /* here appending/linking starts */
1234 
1235  mainl = BLO_library_link_begin(&blo_handle, libname, lapp_context->params);
1236  lib = mainl->curlib;
1237  BLI_assert(lib != NULL);
1238  /* In case lib was already existing but not found originally, see T99820. */
1239  lib->id.tag &= ~LIB_TAG_MISSING;
1240 
1241  if (mainl->versionfile < 250) {
1242  BKE_reportf(reports,
1243  RPT_WARNING,
1244  "Linking or appending from a very old .blend file format (%d.%d), no animation "
1245  "conversion will "
1246  "be done! You may want to re-save your lib file with current Blender",
1247  mainl->versionfile,
1248  mainl->subversionfile);
1249  }
1250 
1251  /* For each lib file, we try to link all items belonging to that lib,
1252  * and tag those successful to not try to load them again with the other libs. */
1253  for (item_idx = 0, itemlink = lapp_context->items.list; itemlink;
1254  item_idx++, itemlink = itemlink->next) {
1255  BlendfileLinkAppendContextItem *item = itemlink->link;
1256  ID *new_id;
1257 
1258  if (!BLI_BITMAP_TEST(item->libraries, lib_idx)) {
1259  continue;
1260  }
1261 
1262  new_id = BLO_library_link_named_part(
1263  mainl, &blo_handle, item->idcode, item->name, lapp_context->params);
1264 
1265  if (new_id) {
1266  /* If the link is successful, clear item's libs 'todo' flags.
1267  * This avoids trying to link same item with other libraries to come. */
1268  BLI_bitmap_set_all(item->libraries, false, lapp_context->num_libraries);
1269  item->new_id = new_id;
1270  item->source_library = new_id->lib;
1271  }
1272  }
1273 
1274  BLO_library_link_end(mainl, &blo_handle, lapp_context->params);
1275  link_append_context_library_blohandle_release(lapp_context, lib_context);
1276  }
1277 
1278  /* Instantiate newly linked IDs as needed, if no append is scheduled. */
1279  if ((lapp_context->params->flag & FILE_LINK) != 0 &&
1280  lapp_context->params->context.scene != NULL) {
1281  new_id_to_item_mapping_create(lapp_context);
1282  /* NOTE: Since we append items for IDs not already listed (i.e. implicitly linked indirect
1283  * dependencies), this list will grow and we will process those IDs later, leading to a flatten
1284  * recursive processing of all the linked dependencies. */
1285  for (itemlink = lapp_context->items.list; itemlink; itemlink = itemlink->next) {
1286  BlendfileLinkAppendContextItem *item = itemlink->link;
1287  ID *id = item->new_id;
1288  if (id == NULL) {
1289  continue;
1290  }
1291  BLI_assert(item->userdata == NULL);
1292 
1294  .lapp_context = lapp_context, .item = item, .reports = reports};
1295  BKE_library_foreach_ID_link(lapp_context->params->bmain,
1296  id,
1298  &cb_data,
1299  IDWALK_NOP);
1300  }
1301 
1302  LooseDataInstantiateContext instantiate_context = {.lapp_context = lapp_context,
1303  .active_collection = NULL};
1304  loose_data_instantiate(&instantiate_context);
1305  }
1306 
1307  if ((lapp_context->params->flag & FILE_LINK) != 0) {
1308  blendfile_link_append_proxies_convert(lapp_context->params->bmain, reports);
1309  }
1310 }
1311 
1318  ID *old_id,
1319  ID *new_id,
1320  ReportList *reports,
1321  const bool do_reload,
1322  const short remap_flags)
1323 {
1324  BLI_assert(old_id);
1325  if (do_reload) {
1326  /* Since we asked for placeholders in case of missing IDs,
1327  * we expect to always get a valid one. */
1328  BLI_assert(new_id);
1329  }
1330  if (new_id) {
1331  CLOG_INFO(&LOG,
1332  4,
1333  "Before remap of %s, old_id users: %d, new_id users: %d",
1334  old_id->name,
1335  old_id->us,
1336  new_id->us);
1337  BKE_libblock_remap_locked(bmain, old_id, new_id, remap_flags);
1338 
1339  if (old_id->flag & LIB_FAKEUSER) {
1340  id_fake_user_clear(old_id);
1341  id_fake_user_set(new_id);
1342  }
1343 
1344  CLOG_INFO(&LOG,
1345  4,
1346  "After remap of %s, old_id users: %d, new_id users: %d",
1347  old_id->name,
1348  old_id->us,
1349  new_id->us);
1350 
1351  /* In some cases, new_id might become direct link, remove parent of library in this case. */
1352  if (new_id->lib->parent && (new_id->tag & LIB_TAG_INDIRECT) == 0) {
1353  if (do_reload) {
1354  BLI_assert_unreachable(); /* Should not happen in 'pure' reload case... */
1355  }
1356  new_id->lib->parent = NULL;
1357  }
1358  }
1359 
1360  if (old_id->us > 0 && new_id && old_id->lib == new_id->lib) {
1361  /* Note that this *should* not happen - but better be safe than sorry in this area,
1362  * at least until we are 100% sure this cannot ever happen.
1363  * Also, we can safely assume names were unique so far,
1364  * so just replacing '.' by '~' should work,
1365  * but this does not totally rules out the possibility of name collision. */
1366  size_t len = strlen(old_id->name);
1367  size_t dot_pos;
1368  bool has_num = false;
1369 
1370  for (dot_pos = len; dot_pos--;) {
1371  char c = old_id->name[dot_pos];
1372  if (c == '.') {
1373  break;
1374  }
1375  if (c < '0' || c > '9') {
1376  has_num = false;
1377  break;
1378  }
1379  has_num = true;
1380  }
1381 
1382  if (has_num) {
1383  old_id->name[dot_pos] = '~';
1384  }
1385  else {
1386  len = MIN2(len, MAX_ID_NAME - 7);
1387  BLI_strncpy(&old_id->name[len], "~000", 7);
1388  }
1389 
1390  id_sort_by_name(which_libbase(bmain, GS(old_id->name)), old_id, NULL);
1391 
1392  BKE_reportf(
1393  reports,
1394  RPT_WARNING,
1395  "Lib Reload: Replacing all references to old data-block '%s' by reloaded one failed, "
1396  "old one (%d remaining users) had to be kept and was renamed to '%s'",
1397  new_id->name,
1398  old_id->us,
1399  old_id->name);
1400  }
1401 }
1402 
1404  ReportList *reports,
1405  Library *library,
1406  const bool do_reload)
1407 {
1408  ListBase *lbarray[INDEX_ID_MAX];
1409  int lba_idx;
1410 
1411  LinkNode *itemlink;
1412  int item_idx;
1413 
1414  Main *bmain = lapp_context->params->bmain;
1415 
1416  /* All override rules need to be up to date, since there will be no do_version here, otherwise
1417  * older, now-invalid rules might be applied and likely fail, or some changes might be missing,
1418  * etc. See T93353. */
1420 
1421  /* Remove all IDs to be reloaded from Main. */
1422  lba_idx = set_listbasepointers(bmain, lbarray);
1423  while (lba_idx--) {
1424  ID *id = lbarray[lba_idx]->first;
1425  const short idcode = id ? GS(id->name) : 0;
1426 
1427  if (!id || !BKE_idtype_idcode_is_linkable(idcode)) {
1428  /* No need to reload non-linkable datatypes,
1429  * those will get relinked with their 'users ID'. */
1430  continue;
1431  }
1432 
1433  for (; id; id = id->next) {
1434  if (id->lib == library) {
1436 
1437  /* We remove it from current Main, and add it to items to link... */
1438  /* Note that non-linkable IDs (like e.g. shape-keys) are also explicitly linked here... */
1439  BLI_remlink(lbarray[lba_idx], id);
1440  /* Usual special code for ShapeKeys snowflakes... */
1441  Key *old_key = BKE_key_from_id(id);
1442  if (old_key != NULL) {
1443  BLI_remlink(which_libbase(bmain, GS(old_key->id.name)), &old_key->id);
1444  }
1445 
1446  item = BKE_blendfile_link_append_context_item_add(lapp_context, id->name + 2, idcode, id);
1447  BLI_bitmap_set_all(item->libraries, true, (size_t)lapp_context->num_libraries);
1448 
1449  CLOG_INFO(&LOG, 4, "Datablock to seek for: %s", id->name);
1450  }
1451  }
1452  }
1453 
1454  if (lapp_context->num_items == 0) {
1455  /* Early out in case there is nothing to do. */
1456  return;
1457  }
1458 
1460 
1461  /* We do not want any instantiation here! */
1462  BKE_blendfile_link(lapp_context, reports);
1463 
1464  BKE_main_lock(bmain);
1465 
1466  /* We add back old id to bmain.
1467  * We need to do this in a first, separated loop, otherwise some of those may not be handled by
1468  * ID remapping, which means they would still reference old data to be deleted... */
1469  for (item_idx = 0, itemlink = lapp_context->items.list; itemlink;
1470  item_idx++, itemlink = itemlink->next) {
1471  BlendfileLinkAppendContextItem *item = itemlink->link;
1472  ID *old_id = item->userdata;
1473 
1474  BLI_assert(old_id);
1475  BLI_addtail(which_libbase(bmain, GS(old_id->name)), old_id);
1476 
1477  /* Usual special code for ShapeKeys snowflakes... */
1478  Key *old_key = BKE_key_from_id(old_id);
1479  if (old_key != NULL) {
1480  BLI_addtail(which_libbase(bmain, GS(old_key->id.name)), &old_key->id);
1481  }
1482  }
1483 
1484  /* Since our (old) reloaded IDs were removed from main, the user count done for them in linking
1485  * code is wrong, we need to redo it here after adding them back to main. */
1486  BKE_main_id_refcount_recompute(bmain, false);
1487 
1488  /* Note that in reload case, we also want to replace indirect usages. */
1489  const short remap_flags = ID_REMAP_SKIP_NEVER_NULL_USAGE |
1490  (do_reload ? 0 : ID_REMAP_SKIP_INDIRECT_USAGE);
1491  for (item_idx = 0, itemlink = lapp_context->items.list; itemlink;
1492  item_idx++, itemlink = itemlink->next) {
1493  BlendfileLinkAppendContextItem *item = itemlink->link;
1494  ID *old_id = item->userdata;
1495  ID *new_id = item->new_id;
1496 
1497  blendfile_library_relocate_remap(bmain, old_id, new_id, reports, do_reload, remap_flags);
1498  if (new_id == NULL) {
1499  continue;
1500  }
1501  /* Usual special code for ShapeKeys snowflakes... */
1502  Key **old_key_p = BKE_key_from_id_p(old_id);
1503  if (old_key_p == NULL) {
1504  continue;
1505  }
1506  Key *old_key = *old_key_p;
1507  Key *new_key = BKE_key_from_id(new_id);
1508  if (old_key != NULL) {
1509  *old_key_p = NULL;
1510  id_us_min(&old_key->id);
1512  bmain, &old_key->id, &new_key->id, reports, do_reload, remap_flags);
1513  *old_key_p = old_key;
1514  id_us_plus_no_lib(&old_key->id);
1515  }
1516  }
1517 
1518  BKE_main_unlock(bmain);
1519 
1520  /* Delete all no more used old IDs. */
1521  /* NOTE: While this looping over until we are sure we deleted everything is very far from
1522  * efficient, doing otherwise would require a much more complex handling of indirectly linked IDs
1523  * in steps above. Currently, in case of relocation, those are skipped in remapping phase, though
1524  * in some cases (essentially internal links between IDs from the same library) remapping should
1525  * happen. But getting this to work reliably would be very difficult, so since this is not a
1526  * performance-critical code, better to go with the (relatively) simpler, brute-force approach
1527  * here in 'removal of old IDs' step. */
1528  bool keep_looping = true;
1529  while (keep_looping) {
1530  keep_looping = false;
1531 
1532  BKE_main_id_tag_all(bmain, LIB_TAG_DOIT, false);
1533  for (item_idx = 0, itemlink = lapp_context->items.list; itemlink;
1534  item_idx++, itemlink = itemlink->next) {
1535  BlendfileLinkAppendContextItem *item = itemlink->link;
1536  ID *old_id = item->userdata;
1537 
1538  if (old_id == NULL) {
1539  continue;
1540  }
1541 
1542  if (GS(old_id->name) == ID_KE) {
1543  /* Shape Keys are handled as part of their owning obdata (see below). This implies that
1544  * there is no way to know when the old pointer gets invalid, so just clear it immediately.
1545  */
1546  item->userdata = NULL;
1547  continue;
1548  }
1549 
1550  /* In case the active scene was reloaded, the context pointers in
1551  * `lapp_context->params->context` need to be updated before the old Scene ID is freed. */
1552  if (old_id == &lapp_context->params->context.scene->id) {
1553  BLI_assert(GS(old_id->name) == ID_SCE);
1554  Scene *new_scene = (Scene *)item->new_id;
1555  BLI_assert(new_scene != NULL);
1556  lapp_context->params->context.scene = new_scene;
1557  if (lapp_context->params->context.view_layer != NULL) {
1558  ViewLayer *new_view_layer = BKE_view_layer_find(
1559  new_scene, lapp_context->params->context.view_layer->name);
1560  lapp_context->params->context.view_layer = (new_view_layer != NULL) ?
1561  new_view_layer :
1562  new_scene->view_layers.first;
1563  }
1564  /* lapp_context->params->context.v3d should never become invalid by newly linked data here.
1565  */
1566  }
1567 
1568  if (old_id->us == 0) {
1569  old_id->tag |= LIB_TAG_DOIT;
1570  item->userdata = NULL;
1571  keep_looping = true;
1572  Key *old_key = BKE_key_from_id(old_id);
1573  if (old_key != NULL) {
1574  old_key->id.tag |= LIB_TAG_DOIT;
1575  }
1576  }
1577  }
1579  /* Should not be needed, all tagged IDs should have been deleted above, just 'in case'. */
1580  BKE_main_id_tag_all(bmain, LIB_TAG_DOIT, false);
1581  }
1582 
1583  /* Some datablocks can get reloaded/replaced 'silently' because they are not linkable
1584  * (shape keys e.g.), so we need another loop here to clear old ones if possible. */
1585  lba_idx = set_listbasepointers(bmain, lbarray);
1586  while (lba_idx--) {
1587  ID *id, *id_next;
1588  for (id = lbarray[lba_idx]->first; id; id = id_next) {
1589  id_next = id->next;
1590  /* XXX That check may be a bit to generic/permissive? */
1591  if (id->lib && (id->flag & LIB_TAG_PRE_EXISTING) && id->us == 0) {
1592  BKE_id_free(bmain, id);
1593  }
1594  }
1595  }
1596 
1597  /* Get rid of no more used libraries... */
1598  BKE_main_id_tag_idcode(bmain, ID_LI, LIB_TAG_DOIT, true);
1599  lba_idx = set_listbasepointers(bmain, lbarray);
1600  while (lba_idx--) {
1601  ID *id;
1602  for (id = lbarray[lba_idx]->first; id; id = id->next) {
1603  if (id->lib) {
1604  id->lib->id.tag &= ~LIB_TAG_DOIT;
1605  }
1606  }
1607  }
1608  Library *lib, *lib_next;
1609  for (lib = which_libbase(bmain, ID_LI)->first; lib; lib = lib_next) {
1610  lib_next = lib->id.next;
1611  if (lib->id.tag & LIB_TAG_DOIT) {
1612  id_us_clear_real(&lib->id);
1613  if (lib->id.us == 0) {
1614  BKE_id_free(bmain, (ID *)lib);
1615  }
1616  }
1617  }
1618 
1619  /* Update overrides of reloaded linked data-blocks. */
1620  ID *id;
1621  FOREACH_MAIN_ID_BEGIN (bmain, id) {
1622  if (ID_IS_LINKED(id) || !ID_IS_OVERRIDE_LIBRARY_REAL(id) ||
1623  (id->tag & LIB_TAG_PRE_EXISTING) == 0) {
1624  continue;
1625  }
1626  if ((id->override_library->reference->tag & LIB_TAG_MISSING) == 0) {
1627  id->tag &= ~LIB_TAG_MISSING;
1628  }
1631  }
1632  }
1634 
1635  /* Resync overrides if needed. */
1636  if (!USER_EXPERIMENTAL_TEST(&U, no_override_auto_resync)) {
1638  lapp_context->params->context.scene,
1639  lapp_context->params->context.view_layer,
1640  &(struct BlendFileReadReport){
1641  .reports = reports,
1642  });
1643  /* We need to rebuild some of the deleted override rules (for UI feedback purpose). */
1645  }
1646 
1647  BKE_main_collection_sync(bmain);
1648 }
1649 
struct Collection * BKE_collection_add(struct Main *bmain, struct Collection *parent, const char *name)
Definition: collection.c:425
bool BKE_collection_child_add(struct Main *bmain, struct Collection *parent, struct Collection *child)
Definition: collection.c:1585
bool BKE_collection_object_add(struct Main *bmain, struct Collection *collection, struct Object *ob)
Definition: collection.c:1125
bool BKE_collection_has_object(struct Collection *collection, const struct Object *ob)
bool BKE_collection_has_object_recursive(struct Collection *collection, struct Object *ob)
Definition: collection.c:921
struct Collection * BKE_collection_parent_editable_find_recursive(const struct ViewLayer *view_layer, struct Collection *collection)
uint64_t BKE_idtype_idcode_to_idfilter(short idcode)
Definition: idtype.c:206
short BKE_idtype_idcode_iter_step(int *index)
Definition: idtype.c:442
bool BKE_idtype_idcode_is_linkable(short idcode)
Definition: idtype.c:175
bool BKE_idtype_idcode_append_is_reusable(short idcode)
Definition: idtype.c:194
struct Key * BKE_key_from_id(struct ID *id)
Definition: key.c:1783
struct Key ** BKE_key_from_id_p(struct ID *id)
Definition: key.c:1758
struct LayerCollection * BKE_layer_collection_get_active(struct ViewLayer *view_layer)
Definition: layer.c:636
void BKE_main_collection_sync(const struct Main *bmain)
struct Base * BKE_view_layer_base_find(struct ViewLayer *view_layer, struct Object *ob)
Definition: layer.c:379
struct ViewLayer * BKE_view_layer_find(const struct Scene *scene, const char *layer_name)
void BKE_main_id_newptr_and_tag_clear(struct Main *bmain)
Definition: lib_id.c:1465
void BKE_main_id_tag_idcode(struct Main *mainvar, short type, int tag, bool value)
Definition: lib_id.c:920
void BKE_main_id_tag_all(struct Main *mainvar, int tag, bool value)
Definition: lib_id.c:930
void id_us_min(struct ID *id)
Definition: lib_id.c:313
void id_fake_user_set(struct ID *id)
Definition: lib_id.c:343
void BKE_main_id_refcount_recompute(struct Main *bmain, bool do_linked_only)
Definition: lib_id.c:1499
@ LIB_ID_MAKELOCAL_FORCE_LOCAL
Definition: BKE_lib_id.h:363
@ LIB_ID_MAKELOCAL_ASSET_DATA_CLEAR
Definition: BKE_lib_id.h:369
@ LIB_ID_MAKELOCAL_FULL_LIBRARY
Definition: BKE_lib_id.h:360
@ LIB_ID_MAKELOCAL_FORCE_COPY
Definition: BKE_lib_id.h:365
void BKE_id_free(struct Main *bmain, void *idv)
void id_us_plus(struct ID *id)
Definition: lib_id.c:305
void id_us_clear_real(struct ID *id)
Definition: lib_id.c:278
void id_us_plus_no_lib(struct ID *id)
Definition: lib_id.c:289
bool BKE_lib_id_make_local(struct Main *bmain, struct ID *id, int flags)
Definition: lib_id.c:533
void id_fake_user_clear(struct ID *id)
Definition: lib_id.c:351
size_t BKE_id_multi_tagged_delete(struct Main *bmain) ATTR_NONNULL()
void id_sort_by_name(struct ListBase *lb, struct ID *id, struct ID *id_sorting_hint)
Definition: lib_id.c:1318
void BKE_lib_override_library_main_resync(struct Main *bmain, struct Scene *scene, struct ViewLayer *view_layer, struct BlendFileReadReport *reports)
void BKE_lib_override_library_main_proxy_convert(struct Main *bmain, struct BlendFileReadReport *reports)
bool BKE_lib_override_library_main_operations_create(struct Main *bmain, bool force_auto)
void BKE_lib_override_library_update(struct Main *bmain, struct ID *local)
void BKE_library_foreach_ID_link(struct Main *bmain, struct ID *id, LibraryIDLinkCallback callback, void *user_data, int flag)
Definition: lib_query.c:350
@ IDWALK_NOP
@ IDWALK_CB_LOOPBACK
Definition: BKE_lib_query.h:54
@ IDWALK_CB_INTERNAL
Definition: BKE_lib_query.h:67
@ IDWALK_CB_EMBEDDED
Definition: BKE_lib_query.h:48
@ IDWALK_CB_OVERRIDE_LIBRARY_REFERENCE
Definition: BKE_lib_query.h:57
@ IDWALK_RET_NOP
Definition: BKE_lib_query.h:83
@ ID_REMAP_SKIP_NEVER_NULL_USAGE
Definition: BKE_lib_remap.h:45
@ ID_REMAP_SKIP_INDIRECT_USAGE
Definition: BKE_lib_remap.h:36
void BKE_libblock_relink_to_newid(struct Main *bmain, struct ID *id, int remap_flag) ATTR_NONNULL()
Definition: lib_remap.c:894
void BKE_libblock_remap_locked(struct Main *bmain, void *old_idv, void *new_idv, short remap_flags) ATTR_NONNULL(1
void BKE_main_library_weak_reference_add_item(struct GHash *library_weak_reference_mapping, const char *library_filepath, const char *library_id_name, struct ID *new_id) ATTR_NONNULL()
Definition: main.c:440
#define FOREACH_MAIN_ID_END
Definition: BKE_main.h:367
int set_listbasepointers(struct Main *main, struct ListBase *lb[])
Definition: main.c:654
struct ID * BKE_main_library_weak_reference_search_item(struct GHash *library_weak_reference_mapping, const char *library_filepath, const char *library_id_name) ATTR_NONNULL()
Definition: main.c:431
struct GHash * BKE_main_library_weak_reference_create(struct Main *bmain) ATTR_NONNULL()
Definition: main.c:394
void BKE_main_library_weak_reference_destroy(struct GHash *library_weak_reference_mapping) ATTR_NONNULL()
Definition: main.c:426
void BKE_main_unlock(struct Main *bmain)
Definition: main.c:219
void BKE_main_library_weak_reference_remove_item(struct GHash *library_weak_reference_mapping, const char *library_filepath, const char *library_id_name, struct ID *old_id) ATTR_NONNULL()
Definition: main.c:491
void BKE_main_lock(struct Main *bmain)
Definition: main.c:214
#define FOREACH_MAIN_ID_BEGIN(_bmain, _id)
Definition: BKE_main.h:361
struct ListBase * which_libbase(struct Main *bmain, short type)
Definition: main.c:567
General operations, lookup, etc. for materials.
void BKE_object_materials_test(struct Main *bmain, struct Object *ob, struct ID *id)
Definition: material.c:864
General operations, lookup, etc. for blender objects.
int BKE_object_obdata_to_type(const struct ID *id) ATTR_NONNULL(1)
struct Object * BKE_object_add_only_object(struct Main *bmain, int type, const char *name) ATTR_RETURNS_NONNULL
Definition: object.cc:2241
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
API for Blender-side Rigid Body stuff.
void BKE_rigidbody_ensure_local_object(struct Main *bmain, struct Object *ob)
Definition: rigidbody.c:2329
void BKE_scene_object_base_flag_sync_from_base(struct Base *base)
Definition: scene.cc:2896
#define BLI_assert_unreachable()
Definition: BLI_assert.h:93
#define BLI_assert(a)
Definition: BLI_assert.h:46
#define BLI_assert_msg(a, msg)
Definition: BLI_assert.h:53
#define BLI_BITMAP_TEST(_bitmap, _index)
Definition: BLI_bitmap.h:64
#define BLI_BITMAP_ENABLE(_bitmap, _index)
Definition: BLI_bitmap.h:81
#define BLI_BITMAP_NEW_MEMARENA(_mem, _num)
Definition: BLI_bitmap.h:52
void BLI_bitmap_set_all(BLI_bitmap *bitmap, bool set, size_t bits)
Definition: bitmap.c:17
unsigned int BLI_bitmap
Definition: BLI_bitmap.h:16
unsigned int BLI_ghashutil_ptrhash(const void *key)
GHash * BLI_ghash_new(GHashHashFP hashfp, GHashCmpFP cmpfp, const char *info) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
Definition: BLI_ghash.c:689
void * BLI_ghash_lookup(const GHash *gh, const void *key) ATTR_WARN_UNUSED_RESULT
Definition: BLI_ghash.c:734
bool BLI_ghashutil_ptrcmp(const void *a, const void *b)
void BLI_ghash_insert(GHash *gh, void *key, void *val)
Definition: BLI_ghash.c:710
void BLI_ghash_free(GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp)
Definition: BLI_ghash.c:863
#define LISTBASE_FOREACH(type, var, list)
Definition: BLI_listbase.h:336
void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:80
void BLI_remlink(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:100
MINLINE void copy_v3_v3(float r[3], const float a[3])
void BLI_memarena_free(struct MemArena *ma) ATTR_NONNULL(1)
Definition: BLI_memarena.c:94
struct MemArena * BLI_memarena_new(size_t bufsize, const char *name) ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL ATTR_NONNULL(2) ATTR_MALLOC
Definition: BLI_memarena.c:64
#define BLI_MEMARENA_STD_BUFSIZE
Definition: BLI_memarena.h:20
void * BLI_memarena_alloc(struct MemArena *ma, size_t size) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC ATTR_ALLOC_SIZE(2)
Definition: BLI_memarena.c:116
void * BLI_memarena_calloc(struct MemArena *ma, size_t size) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC ATTR_ALLOC_SIZE(2)
Definition: BLI_memarena.c:153
#define FILE_MAX
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t maxncpy) ATTR_NONNULL()
Definition: string.c:64
#define UNUSED(x)
#define ELEM(...)
#define MIN2(a, b)
#define STREQ(a, b)
#define CLAMP_MIN(a, b)
external readfile function prototypes.
@ BLO_LIBLINK_APPEND_RECURSIVE
Definition: BLO_readfile.h:339
@ BLO_LIBLINK_APPEND_ASSET_DATA_CLEAR
Definition: BLO_readfile.h:343
@ BLO_LIBLINK_OBDATA_INSTANCE
Definition: BLO_readfile.h:345
@ BLO_LIBLINK_APPEND_SET_FAKEUSER
Definition: BLO_readfile.h:335
@ BLO_LIBLINK_APPEND_LOCAL_ID_REUSE
Definition: BLO_readfile.h:341
@ BLO_LIBLINK_COLLECTION_INSTANCE
Definition: BLO_readfile.h:347
BlendHandle * BLO_blendhandle_from_file(const char *filepath, struct BlendFileReadReport *reports)
Definition: readblenentry.c:48
struct LinkNode * BLO_blendhandle_get_datablock_names(BlendHandle *bh, int ofblocktype, bool use_assets_only, int *r_tot_names)
struct BlendHandle BlendHandle
Definition: BLO_readfile.h:35
#define BLO_EMBEDDED_STARTUP_BLEND
Definition: BLO_readfile.h:297
struct Main * BLO_library_link_begin(BlendHandle **bh, const char *filepath, const struct LibraryLink_Params *params)
Definition: readfile.c:4650
void BLO_library_link_end(struct Main *mainl, BlendHandle **bh, const struct LibraryLink_Params *params)
Definition: readfile.c:4787
struct ID * BLO_library_link_named_part(struct Main *mainl, BlendHandle **bh, short idcode, const char *name, const struct LibraryLink_Params *params)
Definition: readfile.c:4569
void BLO_blendhandle_close(BlendHandle *bh)
BlendHandle * BLO_blendhandle_from_memory(const void *mem, int memsize, struct BlendFileReadReport *reports)
Definition: readblenentry.c:57
external writefile.c function prototypes.
#define DATA_(msgid)
#define CLOG_ERROR(clg_ref,...)
Definition: CLG_log.h:190
#define CLOG_INFO(clg_ref, level,...)
Definition: CLG_log.h:187
ID and Library types, which are fundamental for sdna.
#define ID_IS_OVERRIDE_LIBRARY_REAL(_id)
Definition: DNA_ID.h:581
@ INDEX_ID_MAX
Definition: DNA_ID.h:1058
#define ID_IS_LINKED(_id)
Definition: DNA_ID.h:566
#define MAX_ID_NAME
Definition: DNA_ID.h:337
@ LIB_TAG_INDIRECT
Definition: DNA_ID.h:677
@ LIB_TAG_PRE_EXISTING
Definition: DNA_ID.h:709
@ LIB_TAG_DOIT
Definition: DNA_ID.h:707
@ LIB_TAG_MISSING
Definition: DNA_ID.h:690
@ LIB_FAKEUSER
Definition: DNA_ID.h:630
#define ID_NEW_SET(_id, _idn)
Definition: DNA_ID.h:617
ID_Type
Definition: DNA_ID_enums.h:44
@ ID_LI
Definition: DNA_ID_enums.h:46
@ ID_KE
Definition: DNA_ID_enums.h:58
@ ID_SCE
Definition: DNA_ID_enums.h:45
@ ID_GR
Definition: DNA_ID_enums.h:65
@ ID_OB
Definition: DNA_ID_enums.h:47
Object groups, one object can be in many groups at once.
@ BASE_SELECTABLE
@ BASE_SELECTED
@ OB_MODE_OBJECT
Object is a sort of wrapper for general info.
@ OB_HIDE_SELECT
@ OB_HIDE_VIEWPORT
#define OB_DATA_SUPPORT_ID(_id_type)
@ OB_EMPTY
@ OB_DUPLICOLLECTION
@ FILE_ACTIVE_COLLECTION
@ FILE_AUTOSELECT
@ FILE_LINK
@ FILE_ASSETS_ONLY
#define USER_EXPERIMENTAL_TEST(userdef, member)
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum type
Read Guarded memory(de)allocation.
unsigned int U
Definition: btGjkEpa3.h:78
std::string id_name(void *id)
Scene scene
int len
Definition: draw_manager.c:108
DRWShaderLibrary * lib
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
#define GS(x)
Definition: iris.c:225
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:27
static unsigned c
Definition: RandGen.cpp:83
unsigned __int64 uint64_t
Definition: stdint.h:90
short flag
unsigned short local_view_bits
struct ReportList * reports
Definition: BLO_readfile.h:80
int proxies_to_lib_overrides_failures
Definition: BLO_readfile.h:107
struct BlendFileReadReport::@134 count
int proxies_to_lib_overrides_success
Definition: BLO_readfile.h:105
BlendfileLinkAppendContext * lapp_context
BlendfileLinkAppendContextItem * item
struct ID * reference
Definition: DNA_ID.h:294
Definition: DNA_ID.h:368
int tag
Definition: DNA_ID.h:387
struct Library * lib
Definition: DNA_ID.h:372
int us
Definition: DNA_ID.h:388
struct ID * newid
Definition: DNA_ID.h:370
IDOverrideLibrary * override_library
Definition: DNA_ID.h:412
short flag
Definition: DNA_ID.h:383
void * next
Definition: DNA_ID.h:369
char name[66]
Definition: DNA_ID.h:378
ID id
Definition: DNA_key_types.h:63
struct Collection * collection
char filepath[1024]
Definition: DNA_ID.h:461
ID id
Definition: DNA_ID.h:458
struct Library * parent
Definition: DNA_ID.h:474
LinkNode * list
Definition: BLI_linklist.h:34
void * link
Definition: BLI_linklist.h:24
struct LinkNode * next
Definition: BLI_linklist.h:23
void * first
Definition: DNA_listBase.h:31
BlendfileLinkAppendContext * lapp_context
Definition: BKE_main.h:121
ListBase scenes
Definition: BKE_main.h:168
short subversionfile
Definition: BKE_main.h:125
short versionfile
Definition: BKE_main.h:125
ListBase collections
Definition: BKE_main.h:189
ListBase objects
Definition: BKE_main.h:170
struct Library * curlib
Definition: BKE_main.h:167
short transflag
struct Collection * instance_collection
float loc[3]
short visibility_flag
float empty_drawsize
void * data
struct Collection * master_collection
View3DCursor cursor
ListBase view_layers
unsigned short local_view_uuid
struct Base * basact
char name[64]
static FT_Library library