Blender V5.3
lib_override.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2016 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <algorithm>
10#include <cstdlib>
11#include <cstring>
12#include <deque>
13#include <iostream>
14#include <map>
15#include <optional>
16
17#include <fmt/format.h>
18
19#include "CLG_log.h"
20
21#include "MEM_guardedalloc.h"
22
23#include "DNA_ID.h"
24#include "DNA_anim_types.h"
25#include "DNA_camera_types.h"
28#include "DNA_key_types.h"
29#include "DNA_modifier_types.h"
30#include "DNA_object_types.h"
31#include "DNA_scene_types.h"
32#include "DNA_shader_fx_types.h"
33#include "DNA_userdef_types.h"
34
35#include "DEG_depsgraph.hh"
37
38#include "BKE_anim_data.hh"
39#include "BKE_armature.hh"
40#include "BKE_blender.hh"
41#include "BKE_collection.hh"
42#include "BKE_fcurve.hh"
43#include "BKE_global.hh"
44#include "BKE_idtype.hh"
45#include "BKE_key.hh"
46#include "BKE_layer.hh"
47#include "BKE_lib_id.hh"
48#include "BKE_lib_override.hh"
49#include "BKE_lib_query.hh"
50#include "BKE_lib_remap.hh"
51#include "BKE_library.hh"
52#include "BKE_main.hh"
54#include "BKE_main_namemap.hh"
55#include "BKE_node.hh"
56#include "BKE_report.hh"
57#include "BKE_scene.hh"
58
59#include "BLO_readfile.hh"
60
61#include "BLI_linklist.hh"
62#include "BLI_listbase.hh"
63#include "BLI_map.hh"
64#include "BLI_memarena.hh"
65#include "BLI_set.hh"
66#include "BLI_string.hh"
67#include "BLI_task_c.hh"
68#include "BLI_time.hh"
69#include "BLI_utildefines.hh"
70#include "BLI_vector.hh"
71#include "BLI_vector_set.hh"
72
73#include "RNA_access.hh"
74#include "RNA_path.hh"
75#include "RNA_prototypes.hh"
76#include "RNA_types.hh"
77
78#include "atomic_ops.h"
79
80#include "lib_intern.hh"
81
82namespace blender {
83
84// #define DEBUG_OVERRIDE_TIMEIT
85
86#ifdef DEBUG_OVERRIDE_TIMEIT
88#endif
89
90using namespace blender::bke;
91
92static CLG_LogRef LOG = {"lib.override"};
93static CLG_LogRef LOG_RESYNC = {"lib.override.resync"};
94
96 std::optional<Map<StringRefNull, IDOverrideLibraryProperty *>> rna_path_to_override_properties =
97 std::nullopt;
99};
100
101namespace bke::liboverride {
102
104{
105 return !USER_DEVELOPER_TOOL_TEST(&U, no_override_auto_resync) &&
106 (G.fileflags & G_LIBOVERRIDE_NO_AUTO_RESYNC) == 0;
107}
108
109} // namespace bke::liboverride
110
115
119
122{
123 if (liboverride->runtime == nullptr) {
124 liboverride->runtime = MEM_new<IDOverrideLibraryRuntime>(__func__);
125 }
126 return liboverride->runtime;
127}
128
135{
136 if (id_src->id_type() == ID_OB && id_dst->id_type() == ID_OB) {
137 Object *ob_src = reinterpret_cast<Object *>(id_src);
138 Object *ob_dst = reinterpret_cast<Object *>(id_dst);
139 if (ob_src->type == OB_ARMATURE && (ob_src->mode & OB_MODE_POSE) != 0) {
140 ob_dst->restore_mode = ob_dst->mode;
141 ob_dst->mode |= OB_MODE_POSE;
142 }
143 }
144}
145
148 const ID *id,
149 const ID * /*owner_id_hint*/,
150 const ID **r_owner_id)
151{
153 const ID *owner_id = BKE_id_owner_get(const_cast<ID *>(id));
154 BLI_assert_msg(owner_id != nullptr, "Liboverride-embedded ID with no owner");
155 if (r_owner_id != nullptr) {
156 *r_owner_id = owner_id;
157 }
158 return owner_id->override_library;
159 }
160
161 if (r_owner_id != nullptr) {
162 *r_owner_id = id;
163 }
164 return id->override_library;
165}
166
168 ID *id,
169 ID *owner_id_hint,
170 ID **r_owner_id)
171{
172 /* Reuse the implementation of the const access function, which does not change the arguments.
173 * Add const explicitly to make it clear to the compiler to avoid just calling this function. */
174 return const_cast<IDOverrideLibrary *>(
175 BKE_lib_override_library_get(const_cast<const Main *>(bmain),
176 const_cast<const ID *>(id),
177 const_cast<const ID *>(owner_id_hint),
178 const_cast<const ID **>(r_owner_id)));
179}
180
182{
183 /* The `reference_id` *must* be linked data. */
184 BLI_assert(!reference_id || ID_IS_LINKED(reference_id));
185 BLI_assert(local_id->override_library == nullptr);
186
187 /* Else, generate new empty override. */
188 local_id->override_library = MEM_new<IDOverrideLibrary>(__func__);
189 local_id->override_library->reference = reference_id;
190 if (reference_id) {
192 }
193 local_id->tag &= ~ID_TAG_LIBOVERRIDE_REFOK;
194 /* By default initialized liboverrides are 'system overrides', higher-level code is responsible
195 * to unset this flag for specific IDs. */
197 /* TODO: do we want to add tag or flag to referee to mark it as such? */
198 return local_id->override_library;
199}
200
201void BKE_lib_override_library_copy(ID *dst_id, const ID *src_id, const bool do_full_copy)
202{
204
205 if (dst_id->override_library != nullptr) {
206 if (src_id->override_library == nullptr) {
208 return;
209 }
210
212 }
213 else if (src_id->override_library == nullptr) {
214 /* Virtual overrides of embedded data does not require any extra work. */
215 return;
216 }
217 else {
218 BKE_lib_override_library_init(dst_id, nullptr);
219 }
220
221 /* Reuse the source's reference for destination ID. */
224
226 dst_id->override_library->flag = src_id->override_library->flag;
227
228 if (do_full_copy) {
230 &src_id->override_library->properties);
231 for (IDOverrideLibraryProperty *op_dst = dst_id->override_library->properties.first(),
232 *op_src = src_id->override_library->properties.first();
233 op_dst;
234 op_dst = op_dst->next, op_src = op_src->next)
235 {
237 }
238 }
239
241}
242
244{
245 BLI_assert(liboverride != nullptr);
246
247 if (liboverride->runtime && liboverride->runtime->rna_path_to_override_properties) {
248 liboverride->runtime->rna_path_to_override_properties->clear();
249 }
250
251 for (IDOverrideLibraryProperty &op : liboverride->properties) {
253 }
254 liboverride->properties.free_no_destruct();
255
256 if (do_id_user) {
257 id_us_min(liboverride->reference);
258 /* override->storage should never be refcounted... */
259 }
260}
261
263{
264 BLI_assert(*liboverride != nullptr);
265
266 if ((*liboverride)->runtime != nullptr) {
267 MEM_SAFE_DELETE((*liboverride)->runtime);
268 }
269
271 MEM_delete(*liboverride);
272 *liboverride = nullptr;
273}
274
276 const IDOverrideLibraryTag tag,
277 const bool value)
278{
279 BLI_assert(liboverride.runtime);
280 if (value) {
281 liboverride.runtime->tag |= tag;
282 }
283 else {
284 liboverride.runtime->tag &= ~tag;
285 }
286}
287
289 Library *owner_library,
290 ID *reference_id,
291 const int lib_id_copy_flags)
292{
293 /* NOTE: do not copy possible override data from the reference here. */
294 ID *local_id = BKE_id_copy_in_lib(bmain,
295 owner_library,
296 reference_id,
297 std::nullopt,
298 nullptr,
301 lib_id_copy_flags));
302 if (local_id == nullptr) {
303 return nullptr;
304 }
305 BLI_assert(local_id->lib == owner_library);
306 id_us_min(local_id);
307
308 /* In case we could not get an override ID with the exact same name as its linked reference,
309 * ensure we at least get a uniquely named override ID over the whole current Main data, to
310 * reduce potential name collisions with other reference IDs.
311 *
312 * While in normal cases this would not be an issue, when files start to get heavily broken and
313 * not sound, such conflicts can become a source of problems. */
314 if (!STREQ(local_id->name + 2, reference_id->name + 2)) {
315 BKE_main_namemap_remove_id(*bmain, *local_id);
316 BLI_strncpy(local_id->name + 2, reference_id->name + 2, MAX_ID_NAME - 2);
317 BKE_main_global_namemap_get_unique_name(*bmain, *local_id, local_id->name + 2);
318 id_sort_by_name(which_libbase(bmain, local_id->id_type()), local_id, nullptr);
319 }
320
321 /* In `NO_MAIN` case, generic `BKE_id_copy` code won't call this.
322 * In liboverride resync case however, the currently not-in-Main new IDs will be added back to
323 * Main later, so ensure that their linked dependencies and paths are properly handled here.
324 *
325 * NOTE: This is likely not the best place to do this. Ideally, #BKE_libblock_management_main_add
326 * e.g. should take care of this. But for the time being, this works and has been battle-proofed.
327 */
328 if ((lib_id_copy_flags & LIB_ID_CREATE_NO_MAIN) != 0 && !ID_IS_LINKED(local_id)) {
329 lib_id_copy_ensure_local(bmain, reference_id, local_id, 0);
330 }
331
332 BKE_lib_override_library_init(local_id, reference_id);
333
334 /* NOTE: From liboverride perspective (and RNA one), shape keys are considered as local embedded
335 * data-blocks, just like root node trees or master collections. Therefore, we never need to
336 * create overrides for them. We need a way to mark them as overrides though. */
337 Key *reference_key = BKE_key_from_id(reference_id);
338 if (reference_key != nullptr) {
339 Key *local_key = BKE_key_from_id(local_id);
340 BLI_assert(local_key != nullptr);
342 }
343
344 return local_id;
345}
346
348{
349 /* TODO: This could be simplified by storing a flag in #IDOverrideLibrary
350 * during the diffing process? */
351
352 if (!ID_IS_OVERRIDE_LIBRARY(id)) {
353 return false;
354 }
355
356 /* A bit weird, but those embedded IDs are handled by their owner ID anyway, so we can just
357 * assume they are never user-edited, actual proper detection will happen from their owner check.
358 */
360 return false;
361 }
362
363 for (const IDOverrideLibraryProperty &op : id->override_library->properties) {
364 for (const IDOverrideLibraryPropertyOperation &opop : op.operations) {
366 continue;
367 }
368 if (opop.operation == LIBOVERRIDE_OP_NOOP) {
369 continue;
370 }
371 /* If an operation does not match the filters above, it is considered as a user-editing one,
372 * therefore this override is user-edited. */
373 return true;
374 }
375 }
376 return false;
377}
378
380{
381 if (ID_IS_OVERRIDE_LIBRARY(id)) {
382 const ID *override_owner_id;
383 BKE_lib_override_library_get(bmain, id, nullptr, &override_owner_id);
384 return (override_owner_id->override_library->flag & LIBOVERRIDE_FLAG_SYSTEM_DEFINED) != 0;
385 }
386 return false;
387}
388
390 const ID *id,
391 const IDOverrideLibraryProperty *liboverride_prop,
392 const PropertyRNA *override_rna_prop,
393 const int rnaprop_index)
394{
395 AnimData *anim_data = BKE_animdata_from_id(id);
396 if (anim_data != nullptr) {
397 FCurve *fcurve;
398 char *index_token_start = const_cast<char *>(
399 RNA_path_array_index_token_find(liboverride_prop->rna_path, override_rna_prop));
400 if (index_token_start != nullptr) {
401 const char index_token_start_backup = *index_token_start;
402 *index_token_start = '\0';
404 anim_data, liboverride_prop->rna_path, rnaprop_index, nullptr, nullptr);
405 *index_token_start = index_token_start_backup;
406 }
407 else {
409 anim_data, liboverride_prop->rna_path, 0, nullptr, nullptr);
410 }
411 if (fcurve != nullptr) {
412 return true;
413 }
414 }
415 return false;
416}
417
419{
420 ID *id_owner = cb_data->owner_id;
421 ID *id = *cb_data->id_pointer;
422 bool *is_leaf = static_cast<bool *>(cb_data->user_data);
423
424 if (cb_data->cb_flag & IDWALK_CB_LOOPBACK) {
425 return IDWALK_RET_NOP;
426 }
427
428 if (id != nullptr && ID_IS_OVERRIDE_LIBRARY_REAL(id) &&
429 id->override_library->hierarchy_root == id_owner->override_library->hierarchy_root)
430 {
431 *is_leaf = false;
433 }
434 return IDWALK_RET_NOP;
435}
436
438{
440 bool is_leaf = true;
443 return is_leaf;
444 }
445
446 return false;
447}
448
450{
451 /* Only local liboverrides need to be tagged for refresh, linked ones should not be editable. */
452 if (ID_IS_LINKED(id) || !ID_IS_OVERRIDE_LIBRARY(id)) {
453 return;
454 }
455 /* NOTE: Valid relationships between IDs here (especially the beloved ObData <-> ShapeKey special
456 * case) cannot be always expected when ID get tagged. So now, embedded IDs and similar also get
457 * tagged, and the 'liboverride refresh' code is responsible to properly propagate the update to
458 * the owner ID when needed (see #BKE_lib_override_library_main_operations_create). */
460}
461
463 ID *reference_id,
464 const bool do_tagged_remap)
465{
466 BLI_assert(reference_id != nullptr);
467 BLI_assert(ID_IS_LINKED(reference_id));
468
469 ID *local_id = lib_override_library_create_from(bmain, nullptr, reference_id, 0);
470 /* We cannot allow automatic hierarchy resync on this ID, it is highly likely to generate a giant
471 * mess in case there are a lot of hidden, non-instantiated, non-properly organized dependencies.
472 * Ref #94650. */
475 local_id->override_library->hierarchy_root = local_id;
476
477 if (do_tagged_remap) {
478 Key *reference_key = BKE_key_from_id(reference_id);
479 Key *local_key = nullptr;
480 if (reference_key != nullptr) {
481 local_key = BKE_key_from_id(local_id);
482 BLI_assert(local_key != nullptr);
483 }
484
485 ID *other_id;
486 FOREACH_MAIN_ID_BEGIN (bmain, other_id) {
487 if ((other_id->tag & ID_TAG_DOIT) != 0 && !ID_IS_LINKED(other_id)) {
488 /* Note that using ID_REMAP_SKIP_INDIRECT_USAGE below is superfluous, as we only remap
489 * local IDs usages anyway. */
491 other_id,
492 reference_id,
493 local_id,
495 if (reference_key != nullptr) {
497 other_id,
498 &reference_key->id,
499 &local_key->id,
501 }
502 }
503 }
505 }
506
507 /* Cleanup global namemap, to avoid extra processing with regular ID name management. Better to
508 * re-create the global namemap on demand. */
510
511 return local_id;
512}
513
514static void lib_override_prefill_newid_from_existing_overrides(Main *bmain, ID *id_hierarchy_root)
515{
516 ID *id_iter;
517 FOREACH_MAIN_ID_BEGIN (bmain, id_iter) {
518 ID *id = id_iter;
519 if (id_iter->id_type() == ID_KE) {
520 id = reinterpret_cast<Key *>(id_iter)->from;
521 BLI_assert(id != nullptr);
522 }
524 id->override_library->hierarchy_root == id_hierarchy_root)
525 {
526 id->override_library->reference->newid = id;
527 if (id_iter->id_type() == ID_KE) {
528 Key *reference_key = BKE_key_from_id(id->override_library->reference);
529 if (reference_key != nullptr) {
530 reference_key->id.newid = id_iter;
531 }
532 }
533 }
534 }
536}
537
539 ID *reference_id,
540 ID *local_id)
541{
542 id_remapper.add(reference_id, local_id);
543
544 Key *reference_key = BKE_key_from_id(reference_id);
545 Key *local_key = nullptr;
546 if (reference_key != nullptr) {
547 if (reference_id->newid != nullptr) {
548 local_key = BKE_key_from_id(reference_id->newid);
549 BLI_assert(local_key != nullptr);
550 }
551
552 id_remapper.add(&reference_key->id, &local_key->id);
553 }
554}
555
557 Library *owner_library,
558 const ID *id_root_reference,
559 ID *id_hierarchy_root,
560 const ID *id_hierarchy_root_reference,
561 const bool do_no_main,
562 const bool do_fully_editable)
563{
564 /* TODO: Make this static local function instead?
565 * API is becoming complex, and it's not used outside of this file anyway. */
566
567 BLI_assert(id_root_reference != nullptr && ID_IS_LINKED(id_root_reference));
568 /* If we do not have any hierarchy root given, then the root reference must be tagged for
569 * override. */
570 BLI_assert(id_hierarchy_root != nullptr || id_hierarchy_root_reference != nullptr ||
571 (id_root_reference->tag & ID_TAG_DOIT) != 0);
572 /* At least one of the hierarchy root pointers must be nullptr, passing both is useless and can
573 * create confusion. */
574 BLI_assert(ELEM(nullptr, id_hierarchy_root, id_hierarchy_root_reference));
575
576 if (id_hierarchy_root != nullptr) {
577 /* If the hierarchy root is given, it must be a valid existing override (used during partial
578 * resync process mainly). */
579 BLI_assert(ID_IS_OVERRIDE_LIBRARY_REAL(id_hierarchy_root) &&
580 id_hierarchy_root->override_library->reference->lib == id_root_reference->lib);
581
582 if (!do_no_main) {
583 /* When processing within Main, set existing overrides in given hierarchy as 'newid' of their
584 * linked reference. This allows to re-use existing overrides instead of creating new ones in
585 * partial override cases. */
587 }
588 }
589 if (!ELEM(id_hierarchy_root_reference, nullptr, id_root_reference)) {
590 /* If the reference hierarchy root is given, it must be from the same library as the reference
591 * root, and also tagged for override. */
592 BLI_assert(id_hierarchy_root_reference->lib == id_root_reference->lib &&
593 (id_hierarchy_root_reference->tag & ID_TAG_DOIT) != 0);
594 }
595
596 const Library *reference_library = id_root_reference->lib;
597
598 ID *reference_id;
599 bool success = true;
600
601 ListBaseT<LinkData> todo_ids = {nullptr};
602 LinkData *todo_id_iter;
603
604 /* Get all IDs we want to override. */
605 FOREACH_MAIN_ID_BEGIN (bmain, reference_id) {
606 if ((reference_id->tag & ID_TAG_DOIT) != 0 && reference_id->lib == reference_library &&
607 BKE_idtype_idcode_is_linkable(reference_id->id_type()))
608 {
609 todo_id_iter = MEM_new_zeroed<LinkData>(__func__);
610 todo_id_iter->data = reference_id;
611 BLI_addtail(&todo_ids, todo_id_iter);
612 }
613 }
615
616 /* Override the IDs. */
617 for (todo_id_iter = todo_ids.first(); todo_id_iter != nullptr; todo_id_iter = todo_id_iter->next)
618 {
619 reference_id = static_cast<ID *>(todo_id_iter->data);
620
621 /* If `newid` is already set, assume it has been handled by calling code.
622 * Only current use case: re-using proxy ID when converting to liboverride. */
623 if (reference_id->newid == nullptr) {
624 /* NOTE: `no main` case is used during resync procedure, to support recursive resync.
625 * This requires extra care further down the resync process,
626 * see: #BKE_lib_override_library_resync. */
628 bmain, owner_library, reference_id, do_no_main ? LIB_ID_CREATE_NO_MAIN : 0);
629 if (reference_id->newid == nullptr) {
630 success = false;
631 break;
632 }
633 if (do_fully_editable) {
635 }
636 }
637 /* We also tag the new IDs so that in next step we can remap their pointers too. */
638 reference_id->newid->tag |= ID_TAG_DOIT;
639
640 Key *reference_key = BKE_key_from_id(reference_id);
641 if (reference_key != nullptr) {
642 reference_key->id.tag |= ID_TAG_DOIT;
643
644 Key *local_key = BKE_key_from_id(reference_id->newid);
645 BLI_assert(local_key != nullptr);
646 reference_key->id.newid = &local_key->id;
647 /* We also tag the new IDs so that in next step we can remap their pointers too. */
648 local_key->id.tag |= ID_TAG_DOIT;
649 }
650 }
651
652 /* Only remap new local ID's pointers, we don't want to force our new overrides onto our whole
653 * existing linked IDs usages. */
654 if (success) {
655 /* If a valid liboverride hierarchy root was given, only remap non-liboverride data and
656 * liboverrides belonging to that hierarchy. Avoids having other liboverride hierarchies of
657 * the same reference data also remapped to the newly created liboverride. */
658 const bool do_remap_liboverride_hierarchy_only = (id_hierarchy_root != nullptr && !do_no_main);
659
660 if (id_hierarchy_root_reference != nullptr) {
661 id_hierarchy_root = id_hierarchy_root_reference->newid;
662 }
663 else if (id_root_reference->newid != nullptr &&
664 (id_hierarchy_root == nullptr ||
665 id_hierarchy_root->override_library->reference == id_root_reference))
666 {
667 id_hierarchy_root = id_root_reference->newid;
668 }
669 BLI_assert(id_hierarchy_root != nullptr);
670
671 Vector<ID *> relinked_ids;
672 id::IDRemapper id_remapper;
673 /* Still checking the whole Main, that way we can tag other local IDs as needing to be
674 * remapped to use newly created overriding IDs, if needed. */
675 ID *id;
676 FOREACH_MAIN_ID_BEGIN (bmain, id) {
677 ID *other_id;
678 /* In case we created new overrides as 'no main', they are not accessible directly in this
679 * loop, but we can get to them through their reference's `newid` pointer. */
680 if (do_no_main && id->lib == id_root_reference->lib && id->newid != nullptr) {
681 other_id = id->newid;
682 /* Otherwise we cannot properly distinguish between IDs that are actually from the
683 * linked library (and should not be remapped), and IDs that are overrides re-generated
684 * from the reference from the linked library, and must therefore be remapped.
685 *
686 * This is reset afterwards at the end of this loop. */
687 other_id->lib = nullptr;
688 }
689 else {
690 other_id = id;
691 }
692
693 /* If other ID is a linked one, but not from the same library as our reference, then we
694 * consider we should also relink it, as part of recursive resync. */
695 if ((other_id->tag & ID_TAG_DOIT) != 0 && other_id->lib != id_root_reference->lib) {
696 ID *owner_id;
697 BKE_lib_override_library_get(bmain, other_id, nullptr, &owner_id);
698
699 /* When the root of the current liboverride hierarchy is known, only remap liboverrides if
700 * they belong to that hierarchy. */
701 if (!do_remap_liboverride_hierarchy_only ||
702 (!ID_IS_OVERRIDE_LIBRARY_REAL(owner_id) ||
703 owner_id->override_library->hierarchy_root == id_hierarchy_root))
704 {
705 relinked_ids.append(other_id);
706 }
707
708 if (ID_IS_OVERRIDE_LIBRARY_REAL(other_id) &&
709 other_id->override_library->hierarchy_root == id_hierarchy_root)
710 {
711 reference_id = other_id->override_library->reference;
712 ID *local_id = reference_id->newid;
713 if (other_id == local_id) {
714 lib_override_remapper_overrides_add(id_remapper, reference_id, local_id);
715 }
716 }
717 }
718 if (other_id != id) {
719 other_id->lib = id_root_reference->lib;
720 }
721 }
723
724 for (todo_id_iter = todo_ids.first(); todo_id_iter != nullptr;
725 todo_id_iter = todo_id_iter->next)
726 {
727 reference_id = static_cast<ID *>(todo_id_iter->data);
728 ID *local_id = reference_id->newid;
729
730 if (local_id == nullptr) {
731 continue;
732 }
733
734 local_id->override_library->hierarchy_root = id_hierarchy_root;
735
736 lib_override_remapper_overrides_add(id_remapper, reference_id, local_id);
737 }
738
740 relinked_ids,
742 id_remapper,
744
745 relinked_ids.clear();
746 }
747 else {
748 /* We need to cleanup potentially already created data. */
749 for (todo_id_iter = todo_ids.first(); todo_id_iter != nullptr;
750 todo_id_iter = todo_id_iter->next)
751 {
752 reference_id = static_cast<ID *>(todo_id_iter->data);
753 BKE_id_delete(bmain, reference_id->newid);
754 reference_id->newid = nullptr;
755 }
756 }
757
758 todo_ids.free_no_destruct();
759
760 return success;
761}
762
766
770
774
782
787 void root_set(ID *id_root)
788 {
789 if (is_override) {
790 id_root_override = id_root;
791 }
792 else {
793 id_root_reference = id_root;
794 }
795 }
800 void hierarchy_root_set(ID *hierarchy_root_id)
801 {
802 if (is_override) {
803 hierarchy_root_id_override = hierarchy_root_id;
804 }
805 else {
806 hierarchy_root_id_reference = hierarchy_root_id;
807 }
808 }
809
812
816
826
833 bool id_tag_set(ID *id, const bool is_missing)
834 {
837 }
838 else if (is_missing) {
839 id->tag |= missing_tag;
840 }
841 else {
842 id->tag |= tag;
843 }
844 return is_missing;
845 }
846 bool id_tag_clear(ID *id, const bool is_missing)
847 {
850 }
851 else if (is_missing) {
852 id->tag &= ~missing_tag;
853 }
854 else {
855 id->tag &= ~tag;
856 }
857 return is_missing;
858 }
859
860 /* Mapping linked objects to all their instantiating collections (as a linked list).
861 * Avoids calling #BKE_collection_object_find over and over, this function is very expansive. */
864
865 void clear()
866 {
870
871 bmain = nullptr;
872 scene = nullptr;
873 id_root_reference = nullptr;
875 id_root_override = nullptr;
877 tag = 0;
878 missing_tag = 0;
879 is_override = false;
880 is_resync = false;
881 }
882};
883
886{
887 for (CollectionObject &collection_object : collection->gobject) {
888 Object *ob = collection_object.ob;
889 if (!ID_IS_LINKED(ob)) {
890 continue;
891 }
892
893 LinkNodePair *collections_linkedlist =
894 data->linked_object_to_instantiating_collections.lookup_or_add_cb_as(
895 ob, [data]() -> LinkNodePair * {
896 return static_cast<LinkNodePair *>(
897 BLI_memarena_calloc(data->mem_arena, sizeof(LinkNodePair)));
898 });
899 BLI_linklist_append_arena(collections_linkedlist, collection, data->mem_arena);
900 }
901}
902
903/* Initialize complex data, `data` is expected to be already initialized with basic pointers and
904 * other simple data.
905 *
906 * NOTE: Currently creates a mapping from linked object to all of their instantiating collections
907 * (as returned by #BKE_collection_object_find). */
909{
910 data->mem_arena = BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE, __func__);
911
912 if (data->scene != nullptr) {
914 data, data->scene->master_collection);
915 }
916 for (Collection &collection : data->bmain->collections) {
918 }
919}
920
921/* Checks that can decide to skip the ID based only on the matching #MainIDRelationsEntryItem data
922 * representing the relationship of that ID with its owner ID. */
924 MainIDRelationsEntryItem *relation_id_entry)
925{
926 /* Skip all relationships that should never be taken into account to define a liboverride
927 * hierarchy ('from', 'parents', 'owner' etc. pointers). */
928 if ((relation_id_entry->usage_flag & IDWALK_CB_OVERRIDE_LIBRARY_NOT_OVERRIDABLE) != 0) {
929 return true;
930 }
931 /* Loop-back pointers (`from` ones) should not be taken into account in liboverride hierarchies.
932 * - They generate an 'inverted' dependency that adds processing and...
933 * - They should always have a regular, 'forward' matching relation anyway. */
934 if ((relation_id_entry->usage_flag & IDWALK_CB_LOOPBACK) != 0) {
935 return true;
936 }
937 return false;
938}
939
940/* Checks that can decide to skip the ID based on some of its data and the one from its owner. */
942 ID *other_id,
943 const bool check_override)
944{
945 /* Skip relationships to null pointer, or to itself. */
946 if (ELEM(other_id, nullptr, owner_id)) {
947 return true;
948 }
949 /* Skip any relationships to data from another library. */
950 if (other_id->lib != owner_id->lib) {
951 return true;
952 }
953 /* Skip relationships to non-override data if requested. */
954 if (check_override) {
956 ID_IS_OVERRIDE_LIBRARY(owner_id),
957 "When processing liboverrides, the owner ID should always be a liboverride too here.");
958 if (!ID_IS_OVERRIDE_LIBRARY(other_id)) {
959 return true;
960 }
961 }
962 /* Skip relationships to IDs that should not be involved in liboverrides currently.
963 * NOTE: The Scene case is a bit specific:
964 * - While not officially supported, API allow to create liboverrides of whole Scene.
965 * - However, when creating liboverrides from other type of data (e.g. collections or
966 * objects), scenes should really not be considered as part of a hierarchy. If there
967 * are dependencies from other overridden IDs to a scene, this is considered as not
968 * supported (see also #121410). */
969#define HIERARCHY_BREAKING_ID_TYPES ID_SCE, ID_LI, ID_SCR, ID_WM, ID_WS
970 if (ELEM(other_id->id_type(), HIERARCHY_BREAKING_ID_TYPES) &&
971 !ELEM(owner_id->id_type(), HIERARCHY_BREAKING_ID_TYPES))
972 {
973 return true;
974 }
975#undef HIERARCHY_BREAKING_ID_TYPES
976
977 return false;
978}
979
981{
982 Main *bmain = data->bmain;
983 ID *id = data->root_get();
984 const bool is_override = data->is_override;
985
986 if ((*reinterpret_cast<uint *>(&id->tag) & data->tag) == 0) {
987 /* This ID is not tagged, no reason to proceed further to its parents. */
988 return;
989 }
990
991 MainIDRelationsEntry *entry = bmain->relations->relations_from_pointers->lookup(id);
992 BLI_assert(entry != nullptr);
993
995 /* This ID has already been processed. */
996 return;
997 }
998 /* This way we won't process again that ID, should we encounter it again through another
999 * relationship hierarchy. */
1001
1002 for (MainIDRelationsEntryItem *from_id_entry = entry->from_ids; from_id_entry != nullptr;
1003 from_id_entry = from_id_entry->next)
1004 {
1006 continue;
1007 }
1008 ID *from_id = from_id_entry->id_pointer.from;
1009 if (lib_override_hierarchy_dependencies_skip_check(id, from_id, is_override)) {
1010 continue;
1011 }
1012
1013 from_id->tag |= data->tag;
1014 data->root_set(from_id);
1016 }
1017 data->root_set(id);
1018}
1019
1020/* Tag all IDs in dependency relationships within an override hierarchy/group.
1021 *
1022 * Requires existing `Main.relations`.
1023 *
1024 * NOTE: This is typically called to complete #lib_override_linked_group_tag.
1025 */
1027{
1028 Main *bmain = data->bmain;
1029 ID *id = data->root_get();
1030 const bool is_override = data->is_override;
1031 const bool is_resync = data->is_resync;
1032
1033 MainIDRelationsEntry *entry = bmain->relations->relations_from_pointers->lookup(id);
1034 BLI_assert(entry != nullptr);
1035
1037 /* This ID has already been processed. */
1038 return (*reinterpret_cast<uint *>(&id->tag) & data->tag) != 0;
1039 }
1040 /* This way we won't process again that ID, should we encounter it again through another
1041 * relationship hierarchy. */
1043
1044 for (MainIDRelationsEntryItem *to_id_entry = entry->to_ids; to_id_entry != nullptr;
1045 to_id_entry = to_id_entry->next)
1046 {
1048 continue;
1049 }
1050 ID *to_id = to_id_entry->id_pointer.to;
1051 if (lib_override_hierarchy_dependencies_skip_check(id, to_id, is_override)) {
1052 continue;
1053 }
1054
1055 data->root_set(to_id);
1057 id->tag |= data->tag;
1058 }
1059 }
1060 data->root_set(id);
1061
1062 /* If the current ID is/has been tagged for override above, then check its reversed dependencies
1063 * (i.e. IDs that depend on the current one).
1064 *
1065 * This will cover e.g. the case where user override an armature, and would expect the mesh
1066 * object deformed by that armature to also be overridden. */
1067 if ((*reinterpret_cast<uint *>(&id->tag) & data->tag) != 0 && !is_resync) {
1069 }
1070
1071 return (*reinterpret_cast<uint *>(&id->tag) & data->tag) != 0;
1072}
1073
1075{
1076 Main *bmain = data->bmain;
1077 ID *id_owner = data->root_get();
1078 BLI_assert(ID_IS_LINKED(id_owner));
1079 BLI_assert(!data->is_override);
1080
1081 MainIDRelationsEntry *entry = bmain->relations->relations_from_pointers->lookup(id_owner);
1082 BLI_assert(entry != nullptr);
1083
1085 /* This ID has already been processed. */
1086 return;
1087 }
1088 /* This way we won't process again that ID, should we encounter it again through another
1089 * relationship hierarchy. */
1091
1092 for (MainIDRelationsEntryItem *to_id_entry = entry->to_ids; to_id_entry != nullptr;
1093 to_id_entry = to_id_entry->next)
1094 {
1096 continue;
1097 }
1098 ID *to_id = to_id_entry->id_pointer.to;
1099 BLI_assert(ID_IS_LINKED(to_id));
1100 if (lib_override_hierarchy_dependencies_skip_check(id_owner, to_id, false)) {
1101 continue;
1102 }
1103
1104 /* Only tag ID if their usages is tagged as requiring liboverride by default, and the owner is
1105 * already tagged for liboverride.
1106 *
1107 * NOTE: 'in-between' IDs are handled as a separate step, typically by calling
1108 * #lib_override_hierarchy_dependencies_recursive_tag.
1109 * NOTE: missing IDs (aka placeholders) are never overridden. */
1110 if ((to_id_entry->usage_flag & IDWALK_CB_OVERRIDE_LIBRARY_HIERARCHY_DEFAULT) != 0 ||
1111 data->linked_ids_hierarchy_default_override.contains(to_id))
1112 {
1113 if (!data->id_tag_set(to_id, bool(to_id->tag & ID_TAG_MISSING))) {
1114 /* Only recursively process the dependencies if the owner is tagged for liboverride. */
1115 data->root_set(to_id);
1117 }
1118 }
1119 }
1120 data->root_set(id_owner);
1121}
1122
1125{
1126 /* NOTE: Collection's object cache (using bases, as returned by #BKE_collection_object_cache_get)
1127 * is not usable here, as it may have become invalid from some previous operation and it should
1128 * not be updated here. So instead only use collections' reliable 'raw' data to check if some
1129 * object in the hierarchy of the given collection is still tagged for override. */
1130 for (CollectionObject *collection_object = collection->gobject.first();
1131 collection_object != nullptr;
1132 collection_object = collection_object->next)
1133 {
1134 Object *object = collection_object->ob;
1135 if (object == nullptr) {
1136 continue;
1137 }
1138 if ((object->id.tag & data->tag) != 0 ||
1139 data->linked_ids_hierarchy_default_override.contains(&object->id))
1140 {
1141 return true;
1142 }
1143 }
1144
1145 for (CollectionChild *collection_child = collection->children.first();
1146 collection_child != nullptr;
1147 collection_child = collection_child->next)
1148 {
1150 data, collection_child->collection))
1151 {
1152 return true;
1153 }
1154 }
1155
1156 return false;
1157}
1158
1160{
1161 Main *bmain = data->bmain;
1162 ID *id_root = data->root_get();
1163
1164 /* Remove (untag) bone shape objects, they shall never need to be to directly/explicitly
1165 * overridden. */
1166 for (Object &ob : bmain->objects) {
1167 if (ob.id.lib != id_root->lib) {
1168 continue;
1169 }
1170 if (ob.type == OB_ARMATURE && ob.pose != nullptr &&
1171 ((ob.id.tag & data->tag) || data->linked_ids_hierarchy_default_override.contains(&ob.id)))
1172 {
1173 for (bPoseChannel *pchan = ob.pose->chanbase.first(); pchan != nullptr; pchan = pchan->next)
1174 {
1175 if (pchan->custom != nullptr && &pchan->custom->id != id_root) {
1176 data->id_tag_clear(&pchan->custom->id, bool(pchan->custom->id.tag & ID_TAG_MISSING));
1177 }
1178 }
1179 }
1180 }
1181
1182 /* Remove (untag) collections if they do not own any tagged object (either themselves, or in
1183 * their children collections). */
1184 for (Collection &collection : bmain->collections) {
1185 if (!((collection.id.tag & data->tag) != 0 ||
1186 data->linked_ids_hierarchy_default_override.contains(&collection.id)) ||
1187 &collection.id == id_root || collection.id.lib != id_root->lib)
1188 {
1189 continue;
1190 }
1191
1193 {
1194 data->id_tag_clear(&collection.id, bool(collection.id.tag & ID_TAG_MISSING));
1195 }
1196 }
1197}
1198
1199/* This will tag at least all 'boundary' linked IDs for a potential override group.
1200 *
1201 * Requires existing `Main.relations`.
1202 *
1203 * Note that you will then need to call #lib_override_hierarchy_dependencies_recursive_tag to
1204 * complete tagging of all dependencies within the override group.
1205 *
1206 * We currently only consider IDs which usages are marked as to be overridden by default (i.e.
1207 * tagged with #IDWALK_CB_OVERRIDE_LIBRARY_HIERARCHY_DEFAULT) as valid boundary IDs to define an
1208 * override group.
1209 */
1211{
1212 Main *bmain = data->bmain;
1213 ID *id_root = data->root_get();
1214 ID *hierarchy_root_id = data->hierarchy_root_get();
1215 const bool is_resync = data->is_resync;
1216 BLI_assert(!data->is_override);
1217
1218 if (id_root->tag & ID_TAG_MISSING) {
1219 id_root->tag |= data->missing_tag;
1220 return;
1221 }
1222
1223 /* In case this code only process part of the whole hierarchy, it first needs to process the
1224 * whole linked hierarchy to know which IDs should be overridden anyway, even though in the more
1225 * limited sub-hierarchy scope they would not be. This is critical for partial resync to work
1226 * properly.
1227 *
1228 * NOTE: Regenerating that Set for every processed sub-hierarchy is not optimal. This is done
1229 * that way for now to limit the scope of these changes. Better handling is considered a TODO for
1230 * later (as part of a general refactoring/modernization of this whole code area). */
1231
1232 const bool use_linked_overrides_set = hierarchy_root_id &&
1233 hierarchy_root_id->lib == id_root->lib &&
1234 hierarchy_root_id != id_root;
1235
1236 BLI_assert(data->do_create_linked_overrides_set == false);
1237 if (use_linked_overrides_set) {
1238 BLI_assert(data->linked_ids_hierarchy_default_override.is_empty());
1239 data->linked_ids_hierarchy_default_override.add(id_root);
1240 data->linked_ids_hierarchy_default_override.add(hierarchy_root_id);
1241 data->do_create_linked_overrides_set = true;
1242
1243 /* Store recursively all IDs in the hierarchy which should be liboverridden by default. */
1244 data->root_set(hierarchy_root_id);
1246
1247 /* Do not override objects used as bone shapes, nor their collections if possible. */
1249
1251 data->root_set(id_root);
1252 data->do_create_linked_overrides_set = false;
1253 }
1254
1255 /* Tag recursively all IDs in the hierarchy which should be liboverridden by default. */
1256 id_root->tag |= data->tag;
1258
1259 /* Do not override objects used as bone shapes, nor their collections if possible. */
1261
1262 if (use_linked_overrides_set) {
1263 data->linked_ids_hierarchy_default_override.clear();
1264 }
1265
1266 /* For each object tagged for override, ensure we get at least one local or liboverride
1267 * collection to host it. Avoids getting a bunch of random object in the scene's master
1268 * collection when all objects' dependencies are not properly 'packed' into a single root
1269 * collection.
1270 *
1271 * NOTE: In resync case, we do not handle this at all, since:
1272 * - In normal, valid cases nothing would be needed anyway (resync process takes care
1273 * of tagging needed 'owner' collection then).
1274 * - Partial resync makes it extremely difficult to properly handle such extra
1275 * collection 'tagging for override' (since one would need to know if the new object
1276 * is actually going to replace an already existing override [most common case], or
1277 * if it is actually a real new 'orphan' one).
1278 * - While not ideal, having objects dangling around is less critical than both points
1279 * above.
1280 * So if users add new objects to their library override hierarchy in an invalid way, so
1281 * be it. Trying to find a collection to override and host this new object would most
1282 * likely make existing override very unclean anyway. */
1283 if (is_resync) {
1284 return;
1285 }
1286 for (Object &ob : bmain->objects) {
1287 if (ID_IS_LINKED(&ob) && (ob.id.tag & data->tag) != 0) {
1288 Collection *instantiating_collection = nullptr;
1289 Collection *instantiating_collection_override_candidate = nullptr;
1290 /* Loop over all collections instantiating the object, if we already have a 'locale' one we
1291 * have nothing to do, otherwise try to find a 'linked' one that we can override too. */
1292 LinkNodePair *instantiating_collection_linklist = static_cast<LinkNodePair *>(
1293 data->linked_object_to_instantiating_collections.lookup_default_as(&ob, nullptr));
1294 if (instantiating_collection_linklist != nullptr) {
1295 for (LinkNode *instantiating_collection_linknode = instantiating_collection_linklist->list;
1296 instantiating_collection_linknode != nullptr;
1297 instantiating_collection_linknode = instantiating_collection_linknode->next)
1298 {
1299 instantiating_collection = static_cast<Collection *>(
1300 instantiating_collection_linknode->link);
1301 if (!ID_IS_LINKED(instantiating_collection)) {
1302 /* There is a local collection instantiating the linked object to override, nothing
1303 * else to be done here. */
1304 break;
1305 }
1306 if (instantiating_collection->id.tag & data->tag ||
1307 data->linked_ids_hierarchy_default_override.contains(&instantiating_collection->id))
1308 {
1309 /* There is a linked collection instantiating the linked object to override,
1310 * already tagged to be overridden, nothing else to be done here. */
1311 break;
1312 }
1313 instantiating_collection_override_candidate = instantiating_collection;
1314 instantiating_collection = nullptr;
1315 }
1316 }
1317
1318 if (instantiating_collection == nullptr &&
1319 instantiating_collection_override_candidate != nullptr)
1320 {
1321 data->id_tag_set(
1322 &instantiating_collection_override_candidate->id,
1323 bool(instantiating_collection_override_candidate->id.tag & ID_TAG_MISSING));
1324 }
1325 }
1326 }
1327}
1328
1330{
1331 Main *bmain = data->bmain;
1332 ID *id_owner = data->root_get();
1334 BLI_assert(data->is_override);
1335 BLI_assert(data->do_create_linked_overrides_set == false);
1336
1337 ID *id_hierarchy_root = data->hierarchy_root_get();
1338
1339 if (ID_IS_OVERRIDE_LIBRARY_REAL(id_owner) &&
1341 {
1342 return;
1343 }
1344
1345 MainIDRelationsEntry *entry = bmain->relations->relations_from_pointers->lookup(id_owner);
1346 BLI_assert(entry != nullptr);
1347
1349 /* This ID has already been processed. */
1350 return;
1351 }
1352 /* This way we won't process again that ID, should we encounter it again through another
1353 * relationship hierarchy. */
1355
1356 for (MainIDRelationsEntryItem *to_id_entry = entry->to_ids; to_id_entry != nullptr;
1357 to_id_entry = to_id_entry->next)
1358 {
1360 continue;
1361 }
1362 ID *to_id = to_id_entry->id_pointer.to;
1363 if (lib_override_hierarchy_dependencies_skip_check(id_owner, to_id, true)) {
1364 continue;
1365 }
1366
1367 /* Different hierarchy roots are break points in override hierarchies. */
1368 if (ID_IS_OVERRIDE_LIBRARY_REAL(to_id) &&
1369 to_id->override_library->hierarchy_root != id_hierarchy_root)
1370 {
1371 continue;
1372 }
1373
1374 const Library *reference_lib =
1375 BKE_lib_override_library_get(bmain, id_owner, nullptr, nullptr)->reference->lib;
1376 const ID *to_id_reference =
1377 BKE_lib_override_library_get(bmain, to_id, nullptr, nullptr)->reference;
1378 if (to_id_reference->lib != reference_lib) {
1379 /* We do not override data-blocks from other libraries, nor do we process them. */
1380 continue;
1381 }
1382
1383 data->id_tag_set(to_id, bool(to_id_reference->tag & ID_TAG_MISSING));
1384
1385 /* Recursively process the dependencies. */
1386 data->root_set(to_id);
1388 }
1389 data->root_set(id_owner);
1390}
1391
1392/* This will tag all override IDs of an override group defined by the given `id_root`. */
1394{
1395 ID *id_root = data->root_get();
1397 BLI_assert(data->is_override);
1398 BLI_assert(data->do_create_linked_overrides_set == false);
1399
1400 ID *id_hierarchy_root = data->hierarchy_root_get();
1401 BLI_assert(id_hierarchy_root != nullptr);
1402 BLI_assert(ID_IS_OVERRIDE_LIBRARY_REAL(id_hierarchy_root));
1403 UNUSED_VARS_NDEBUG(id_hierarchy_root);
1404
1405 data->id_tag_set(id_root, bool(id_root->override_library->reference->tag & ID_TAG_MISSING));
1406
1407 /* Tag all local overrides in id_root's group. */
1409}
1410
1412 Scene *scene,
1413 Library *owner_library,
1414 ID *id_root_reference,
1415 ID *id_hierarchy_root_reference,
1416 const bool do_fully_editable)
1417{
1418 BKE_main_relations_create(bmain, 0);
1420 data.bmain = bmain;
1421 data.scene = scene;
1422 data.tag = ID_TAG_DOIT;
1423 data.missing_tag = ID_TAG_MISSING;
1424 data.is_override = false;
1425 data.is_resync = false;
1426
1427 data.root_set(id_root_reference);
1428 data.hierarchy_root_set(id_hierarchy_root_reference);
1429
1432
1435
1436 /* In case the operation is on an already partially overridden hierarchy, all existing overrides
1437 * in that hierarchy need to be tagged for remapping from linked reference ID usages to newly
1438 * created overrides ones. */
1439 if (id_hierarchy_root_reference->lib != id_root_reference->lib) {
1440 BLI_assert(ID_IS_OVERRIDE_LIBRARY_REAL(id_hierarchy_root_reference));
1441 BLI_assert(id_hierarchy_root_reference->override_library->reference->lib ==
1442 id_root_reference->lib);
1443
1445 data.is_override = true;
1446 data.root_set(id_hierarchy_root_reference);
1447 data.hierarchy_root_set(id_hierarchy_root_reference);
1449 }
1450
1452 data.clear();
1453
1454 bool success = false;
1455 if (id_hierarchy_root_reference->lib != id_root_reference->lib) {
1457 owner_library,
1458 id_root_reference,
1459 id_hierarchy_root_reference,
1460 nullptr,
1461 false,
1462 do_fully_editable);
1463 }
1464 else {
1466 owner_library,
1467 id_root_reference,
1468 nullptr,
1469 id_hierarchy_root_reference,
1470 false,
1471 do_fully_editable);
1472 }
1473
1474 /* Cleanup global namemap, to avoid extra processing with regular ID name management. Better to
1475 * re-create the global namemap on demand. */
1477
1478 return success;
1479}
1480
1482 Scene *scene,
1483 ViewLayer *view_layer,
1484 const Library *owner_library,
1485 ID *id_root,
1486 ID *id_instance_hint,
1487 Collection *residual_storage,
1488 const Object *old_active_object,
1489 const bool is_resync)
1490{
1491 /* If there is an old active object, there should also always be a given view layer. */
1492 BLI_assert(old_active_object == nullptr || view_layer != nullptr);
1493
1494 /* NOTE: We only care about local IDs here, if a linked object is not instantiated in any way we
1495 * do not do anything about it. */
1496
1497 /* We need to use the `_remap` version here as we prevented any LayerCollection resync during the
1498 * whole liboverride resyncing, which involves a lot of ID remapping.
1499 *
1500 * Otherwise, cached Base Map e.g. can contain invalid stale data. */
1502
1503 /* We create a set of all objects referenced into the scene by its hierarchy of collections.
1504 * NOTE: This is different that the list of bases, since objects in excluded collections etc.
1505 * won't have a base, but are still considered as instanced from our point of view. */
1506 Set<Object *> *all_objects_in_scene = BKE_scene_objects_as_set(scene, nullptr);
1507
1508 if (is_resync || id_root == nullptr || id_root->newid == nullptr) {
1509 /* Instantiating the root collection or object should never be needed in resync case, since the
1510 * old override would be remapped to the new one. */
1511 }
1512 else if (ID_IS_LINKED(id_root->newid) && id_root->newid->lib != owner_library) {
1513 /* No instantiation in case the root override is linked data, unless it is part of the given
1514 * owner library.
1515 *
1516 * NOTE: that last case should never happen actually in current code? Since non-null owner
1517 * library should only happen in case of recursive resync, which is already excluded by the
1518 * previous condition. */
1519 }
1520 else if ((id_root->newid->override_library->flag & LIBOVERRIDE_FLAG_NO_HIERARCHY) == 0 &&
1521 id_root->newid->override_library->hierarchy_root != id_root->newid)
1522 {
1523 /* No instantiation in case this is not a hierarchy root, as it can be assumed already handled
1524 * as part of hierarchy processing. */
1525 }
1526 else {
1527 switch (id_root->id_type()) {
1528 case ID_GR: {
1529 Object *ob_reference = id_instance_hint != nullptr &&
1530 id_instance_hint->id_type() == ID_OB ?
1531 reinterpret_cast<Object *>(id_instance_hint) :
1532 nullptr;
1533 Collection *collection_new = (reinterpret_cast<Collection *>(id_root->newid));
1534 if (is_resync && BKE_collection_is_in_scene(collection_new)) {
1535 break;
1536 }
1537 if (ob_reference != nullptr) {
1538 BKE_collection_add_from_object(bmain, scene, ob_reference, collection_new);
1539 }
1540 else if (id_instance_hint != nullptr) {
1541 BLI_assert(id_instance_hint->id_type() == ID_GR);
1543 bmain, scene, (reinterpret_cast<Collection *>(id_instance_hint)), collection_new);
1544 }
1545 else {
1547 bmain, scene, (reinterpret_cast<Collection *>(id_root)), collection_new);
1548 }
1549
1550 BLI_assert(BKE_collection_is_in_scene(collection_new));
1551
1552 all_objects_in_scene = BKE_scene_objects_as_set(scene, all_objects_in_scene);
1553 break;
1554 }
1555 case ID_OB: {
1556 Object *ob_new = reinterpret_cast<Object *>(id_root->newid);
1557 if (!all_objects_in_scene->contains(ob_new)) {
1559 bmain, scene, reinterpret_cast<Object *>(id_root), ob_new);
1560 all_objects_in_scene = BKE_scene_objects_as_set(scene, all_objects_in_scene);
1561 }
1562 break;
1563 }
1564 default:
1565 break;
1566 }
1567 }
1568
1569 if (view_layer != nullptr) {
1570 BKE_view_layer_synced_ensure(*bmain, scene, view_layer);
1571 }
1572 else {
1574 }
1575
1576 /* We need to ensure all new overrides of objects are properly instantiated. */
1577 Collection *default_instantiating_collection = residual_storage;
1578 for (Object &ob : bmain->objects) {
1579 Object *ob_new = reinterpret_cast<Object *>(ob.id.newid);
1580 if (ob_new == nullptr || (ID_IS_LINKED(ob_new) && ob_new->id.lib != owner_library)) {
1581 continue;
1582 }
1583
1584 BLI_assert(ob_new->id.override_library != nullptr &&
1585 ob_new->id.override_library->reference == &ob.id);
1586
1587 if (old_active_object == &ob) {
1588 BLI_assert(view_layer);
1589 /* May have been tagged as dirty again in a previous iteration of this loop, e.g. if adding a
1590 * liboverride object to a collection. */
1591 BKE_view_layer_synced_ensure(*bmain, scene, view_layer);
1592 Base *basact = BKE_view_layer_base_find(view_layer, ob_new);
1593 if (basact != nullptr) {
1594 view_layer->basact = basact;
1595 }
1597 }
1598
1599 if (!all_objects_in_scene->contains(ob_new)) {
1600 if (id_root != nullptr && default_instantiating_collection == nullptr) {
1601 ID *id_ref = id_root->newid != nullptr ? id_root->newid : id_root;
1602 switch (id_ref->id_type()) {
1603 case ID_GR: {
1604 /* Adding the object to a specific collection outside of the root overridden one is a
1605 * fairly bad idea (it breaks the override hierarchy concept). But there is no other
1606 * way to do this currently (we cannot add new collections to overridden root one,
1607 * this is not currently supported).
1608 * Since that will be fairly annoying and noisy, only do that in case the override
1609 * object is not part of any existing collection (i.e. its user count is 0). In
1610 * practice this should never happen I think. */
1611 if (ID_REAL_USERS(ob_new) != 0) {
1612 continue;
1613 }
1614 default_instantiating_collection = BKE_id_new<Collection>(bmain, "OVERRIDE_HIDDEN");
1615 id_us_min(&default_instantiating_collection->id);
1616 /* Hide the collection from viewport and render. */
1617 default_instantiating_collection->flag |= COLLECTION_HIDE_VIEWPORT |
1619 break;
1620 }
1621 case ID_OB: {
1622 /* Add the other objects to one of the collections instantiating the
1623 * root object, or scene's master collection if none found. */
1624 Object *ob_ref = reinterpret_cast<Object *>(id_ref);
1625 for (Collection &collection : bmain->collections) {
1626 if (BKE_collection_has_object(&collection, ob_ref) &&
1627 (view_layer != nullptr ?
1628 BKE_view_layer_has_collection(view_layer, &collection) :
1629 BKE_collection_has_collection(scene->master_collection, &collection)) &&
1630 !ID_IS_LINKED(&collection) && !ID_IS_OVERRIDE_LIBRARY(&collection))
1631 {
1632 default_instantiating_collection = &collection;
1633 }
1634 }
1635 break;
1636 }
1637 default:
1638 break;
1639 }
1640 }
1641 if (default_instantiating_collection == nullptr) {
1642 default_instantiating_collection = scene->master_collection;
1643 }
1644
1645 BKE_collection_object_add(bmain, default_instantiating_collection, ob_new);
1647 }
1648 }
1649
1650 if (id_root != nullptr &&
1651 !ELEM(default_instantiating_collection, nullptr, scene->master_collection))
1652 {
1653 ID *id_ref = id_root->newid != nullptr ? id_root->newid : id_root;
1654 switch (id_ref->id_type()) {
1655 case ID_GR:
1657 scene,
1658 reinterpret_cast<Collection *>(id_ref),
1659 default_instantiating_collection);
1660 break;
1661 default:
1662 /* Add to master collection. */
1664 bmain, scene, nullptr, default_instantiating_collection);
1665 break;
1666 }
1667 }
1668
1669 MEM_delete(all_objects_in_scene);
1670}
1671
1673 Scene *scene,
1674 ViewLayer *view_layer,
1675 Library *owner_library,
1676 ID *id_root_reference,
1677 ID *id_hierarchy_root_reference,
1678 ID *id_instance_hint,
1679 ID **r_id_root_override,
1680 const bool do_fully_editable)
1681{
1682 if (r_id_root_override != nullptr) {
1683 *r_id_root_override = nullptr;
1684 }
1685
1686 if (id_hierarchy_root_reference == nullptr) {
1687 id_hierarchy_root_reference = id_root_reference;
1688 }
1689
1690 /* While in theory it _should_ be enough to ensure sync of given view-layer (if any), or at least
1691 * of given scene, think for now it's better to get a fully synced Main at this point, this code
1692 * may do some very wide remapping/data access in some cases. */
1694 const Object *old_active_object = (view_layer != nullptr) ?
1696 nullptr;
1697
1698 const bool success = lib_override_library_create_do(bmain,
1699 scene,
1700 owner_library,
1701 id_root_reference,
1702 id_hierarchy_root_reference,
1703 do_fully_editable);
1704
1705 if (!success) {
1706 return success;
1707 }
1708
1709 if (r_id_root_override != nullptr) {
1710 *r_id_root_override = id_root_reference->newid;
1711 }
1712
1714 scene,
1715 view_layer,
1716 owner_library,
1717 id_root_reference,
1718 id_instance_hint,
1719 nullptr,
1720 old_active_object,
1721 false);
1722
1723 /* Cleanup. */
1725 BKE_main_id_tag_all(bmain, ID_TAG_DOIT, false);
1726
1727 /* We need to rebuild some of the deleted override rules (for UI feedback purpose). */
1729
1730 return success;
1731}
1732
1733static ID *lib_override_root_find(Main *bmain, ID *id, const int curr_level, int *r_best_level)
1734{
1735 if (curr_level > 1000) {
1736 CLOG_ERROR(&LOG,
1737 "Levels of dependency relationships between library overrides IDs is way too high, "
1738 "skipping further processing loops (involves at least '%s')",
1739 id->name);
1740 return nullptr;
1741 }
1742
1743 if (!ID_IS_OVERRIDE_LIBRARY(id)) {
1745 return nullptr;
1746 }
1747
1748 MainIDRelationsEntry *entry = bmain->relations->relations_from_pointers->lookup(id);
1749 BLI_assert(entry != nullptr);
1750
1753 /* This ID has already been processed. */
1754 *r_best_level = curr_level;
1755 return id->override_library->hierarchy_root;
1756 }
1757
1759 ID *id_owner;
1760 int best_level_placeholder = 0;
1761 BKE_lib_override_library_get(bmain, id, nullptr, &id_owner);
1762 return lib_override_root_find(bmain, id_owner, curr_level + 1, &best_level_placeholder);
1763 }
1764
1766 /* Re-processing an entry already being processed higher in the call-graph (re-entry caused by
1767 * a dependency loops). Just do nothing, there is no more useful info to provide here. */
1768 return nullptr;
1769 }
1770 /* Flag this entry to avoid re-processing it in case some dependency loop leads to it again
1771 * downwards in the call-stack. */
1773
1774 int best_level_candidate = curr_level;
1775 ID *best_root_id_candidate = id;
1776
1777 for (MainIDRelationsEntryItem *from_id_entry = entry->from_ids; from_id_entry != nullptr;
1778 from_id_entry = from_id_entry->next)
1779 {
1781 continue;
1782 }
1783 ID *from_id = from_id_entry->id_pointer.from;
1784 if (lib_override_hierarchy_dependencies_skip_check(id, from_id, true)) {
1785 continue;
1786 }
1787
1788 int level_candidate = curr_level + 1;
1789 /* Recursively process the parent. */
1790 ID *root_id_candidate = lib_override_root_find(
1791 bmain, from_id, curr_level + 1, &level_candidate);
1792 if (level_candidate > best_level_candidate && root_id_candidate != nullptr) {
1793 best_root_id_candidate = root_id_candidate;
1794 best_level_candidate = level_candidate;
1795 }
1796 }
1797
1798 if (!ID_IS_OVERRIDE_LIBRARY_REAL(best_root_id_candidate)) {
1800 ID *id_owner;
1801 int best_level_placeholder = 0;
1802 BKE_lib_override_library_get(bmain, best_root_id_candidate, nullptr, &id_owner);
1803 best_root_id_candidate = lib_override_root_find(
1804 bmain, id_owner, curr_level + 1, &best_level_placeholder);
1805 }
1806
1807 BLI_assert(best_root_id_candidate != nullptr);
1808 BLI_assert((best_root_id_candidate->flag & ID_FLAG_EMBEDDED_DATA_LIB_OVERRIDE) == 0);
1809
1810 /* This way this ID won't be processed again, should it be encountered again through another
1811 * relationship hierarchy. */
1814
1815 *r_best_level = best_level_candidate;
1816 return best_root_id_candidate;
1817}
1818
1823static bool lib_override_root_is_valid(Main *bmain, ID *id)
1824{
1825 if (!ID_IS_OVERRIDE_LIBRARY_REAL(id)) {
1827 return false;
1828 }
1829 ID *id_root = id->override_library->hierarchy_root;
1830 if (!id_root || !ID_IS_OVERRIDE_LIBRARY_REAL(id_root)) {
1832 return false;
1833 }
1834
1835 if (id_root == id) {
1836 return true;
1837 }
1838
1839 VectorSet<ID *> ancestors = {id};
1840 for (int64_t i = 0; i < ancestors.size(); i++) {
1841 ID *id_iter = ancestors[i];
1842
1843 MainIDRelationsEntry *entry = bmain->relations->relations_from_pointers->lookup(id_iter);
1844 BLI_assert(entry != nullptr);
1845
1846 for (MainIDRelationsEntryItem *from_id_entry = entry->from_ids; from_id_entry != nullptr;
1847 from_id_entry = from_id_entry->next)
1848 {
1850 continue;
1851 }
1852 ID *from_id = from_id_entry->id_pointer.from;
1853 if (lib_override_hierarchy_dependencies_skip_check(id_iter, from_id, true)) {
1854 continue;
1855 }
1856
1857 if (from_id == id_root) {
1858 /* The hierarchy root is a valid ancestor of the given id. */
1859 return true;
1860 }
1861 ancestors.add(from_id);
1862 }
1863 }
1864 return false;
1865}
1866
1868 Main *bmain, ID *id_root, ID *id, ID *id_from, Set<ID *> &processed_ids)
1869{
1870 if (processed_ids.contains(id)) {
1871 /* This ID has already been checked as having a valid hierarchy root, do not attempt to replace
1872 * it with another one just because it is also used by another liboverride hierarchy. */
1873 return;
1874 }
1876 if (id->override_library->hierarchy_root == id_root) {
1877 /* Already set, nothing else to do here, sub-hierarchy is also assumed to be properly set
1878 * then. */
1879 return;
1880 }
1881
1882 /* Hierarchy root already set, and not matching currently proposed one, try to find which is
1883 * best. */
1884 if (id->override_library->hierarchy_root != nullptr) {
1885 /* Check if given `id_from` matches with the hierarchy of the linked reference ID, in which
1886 * case we assume that the given hierarchy root is the 'real' one.
1887 *
1888 * NOTE: This can fail if user mixed dependencies between several overrides of a same
1889 * reference linked hierarchy. Not much to be done in that case, it's virtually impossible to
1890 * fix this automatically in a reliable way. */
1891 if (id_from == nullptr || !ID_IS_OVERRIDE_LIBRARY_REAL(id_from)) {
1892 /* Too complicated to deal with for now. */
1893 CLOG_WARN(&LOG,
1894 "Inconsistency in library override hierarchy of ID '%s'.\n"
1895 "\tNot enough data to verify validity of current proposed root '%s', assuming "
1896 "already set one '%s' is valid.",
1897 id->name,
1898 id_root->name,
1899 id->override_library->hierarchy_root->name);
1900 return;
1901 }
1902
1903 ID *id_from_ref = id_from->override_library->reference;
1905 id->override_library->reference);
1906 BLI_assert(entry != nullptr);
1907
1908 /* Enforce replacing hierarchy root if the current one is invalid. */
1909 bool do_replace_root = (!id->override_library->hierarchy_root ||
1910 !ID_IS_OVERRIDE_LIBRARY_REAL(id->override_library->hierarchy_root) ||
1911 id->override_library->hierarchy_root->lib != id->lib);
1912 for (MainIDRelationsEntryItem *from_id_entry = entry->from_ids; from_id_entry != nullptr;
1913 from_id_entry = from_id_entry->next)
1914 {
1916 /* Never consider non-overridable relationships as actual dependencies. */
1917 continue;
1918 }
1919
1920 if (id_from_ref == from_id_entry->id_pointer.from) {
1921 /* A matching parent was found in reference linked data, assume given hierarchy root is
1922 * the valid one. */
1923 do_replace_root = true;
1924 CLOG_WARN(
1925 &LOG,
1926 "Inconsistency in library override hierarchy of ID '%s'.\n"
1927 "\tCurrent proposed root '%s' detected as valid, will replace already set one '%s'.",
1928 id->name,
1929 id_root->name,
1930 id->override_library->hierarchy_root->name);
1931 break;
1932 }
1933 }
1934
1935 if (!do_replace_root) {
1936 CLOG_WARN(
1937 &LOG,
1938 "Inconsistency in library override hierarchy of ID '%s'.\n"
1939 "\tCurrent proposed root '%s' not detected as valid, keeping already set one '%s'.",
1940 id->name,
1941 id_root->name,
1942 id->override_library->hierarchy_root->name);
1943 return;
1944 }
1945 }
1946
1947 CLOG_DEBUG(&LOG,
1948 "Modifying library override hierarchy of ID '%s'.\n"
1949 "\tFrom old root '%s' to new root '%s'.",
1950 id->name,
1951 id->override_library->hierarchy_root ? id->override_library->hierarchy_root->name :
1952 "<NONE>",
1953 id_root->name);
1954
1955 id->override_library->hierarchy_root = id_root;
1956 }
1957
1958 MainIDRelationsEntry *entry = bmain->relations->relations_from_pointers->lookup(id);
1959 BLI_assert(entry != nullptr);
1960
1961 for (MainIDRelationsEntryItem *to_id_entry = entry->to_ids; to_id_entry != nullptr;
1962 to_id_entry = to_id_entry->next)
1963 {
1965 continue;
1966 }
1967 ID *to_id = to_id_entry->id_pointer.to;
1969 continue;
1970 }
1971
1972 /* Recursively process the sub-hierarchy. */
1973 lib_override_root_hierarchy_set(bmain, id_root, to_id, id, processed_ids);
1974 }
1975}
1976
1978 Main *bmain,
1979 ID *id,
1980 Set<ID *> &processed_ids,
1982 ReportList *reports)
1983{
1985
1986 if (id->override_library->flag & LIBOVERRIDE_FLAG_NO_HIERARCHY) {
1987 if (!ELEM(id->override_library->hierarchy_root, id, nullptr)) {
1988 std::string error_msg = fmt::format(
1989 "Existing isolated override '{}' has a non-null hierarchy root ('{}'), will be "
1990 "cleared",
1991 id->name,
1992 id->override_library->hierarchy_root->name);
1993 if (reports) {
1994 BKE_report(reports, RPT_ERROR, error_msg.c_str());
1995 }
1996 else {
1997 CLOG_ERROR(&LOG, "%s", error_msg.c_str());
1998 }
1999 id->override_library->hierarchy_root = nullptr;
2000 }
2001 return;
2002 }
2003
2004 bool null_hierarchy_root_is_expected = false;
2005
2006 if (id->override_library->hierarchy_root != nullptr) {
2007 if (!ID_IS_OVERRIDE_LIBRARY_REAL(id->override_library->hierarchy_root) ||
2008 id->override_library->hierarchy_root->lib != id->lib)
2009 {
2010 std::string error_msg = fmt::format(
2011 "Existing override hierarchy root ('{}') for ID '{}' is invalid, will try to find a new "
2012 "valid one",
2013 id->override_library->hierarchy_root != nullptr ?
2014 id->override_library->hierarchy_root->name :
2015 "<NONE>",
2016 id->name);
2017 if (reports) {
2018 BKE_report(reports, RPT_ERROR, error_msg.c_str());
2019 }
2020 else {
2021 CLOG_ERROR(&LOG, "%s", error_msg.c_str());
2022 }
2023 id->override_library->hierarchy_root = nullptr;
2024 null_hierarchy_root_is_expected = true;
2025 }
2026 else if (!lib_override_root_is_valid(bmain, id)) {
2027 /* Serious invalid cases (likely resulting from bugs or invalid operations) should have
2028 * been caught by the first check above. Invalid hierarchy roots detected here can happen
2029 * in normal situations, e.g. when breaking a hierarchy by making one of its components
2030 * local. See also #137412. */
2031 std::string error_msg = fmt::format(
2032 "Existing override hierarchy root ('{}') for ID '{}' is invalid, will try to find a new "
2033 "valid one",
2034 id->override_library->hierarchy_root != nullptr ?
2035 id->override_library->hierarchy_root->name :
2036 "<NONE>",
2037 id->name);
2038 CLOG_DEBUG(&LOG, "%s", error_msg.c_str());
2039 id->override_library->hierarchy_root = nullptr;
2040 null_hierarchy_root_is_expected = true;
2041 }
2042 else {
2043 /* This ID is considered as having a valid hierarchy root. */
2044 processed_ids.add(id);
2045 return;
2046 }
2047 }
2048
2051
2052 int best_level = 0;
2053 ID *id_root = lib_override_root_find(bmain, id, best_level, &best_level);
2054
2055 if (!ELEM(id->override_library->hierarchy_root, id_root, nullptr)) {
2056 /* In case the detected hierarchy root does not match with the currently defined one, this is
2057 * likely an issue and is worth a warning. */
2058 std::string error_msg = fmt::format(
2059 "Potential inconsistency in library override hierarchy of ID '{}' (current root "
2060 "{}), detected as part of the hierarchy of '{}' (current root '{}')",
2061 id->name,
2062 id->override_library->hierarchy_root != nullptr ?
2063 id->override_library->hierarchy_root->name :
2064 "<NONE>",
2065 id_root->name,
2066 id_root->override_library->hierarchy_root != nullptr ?
2068 "<NONE>");
2069 if (reports) {
2070 BKE_report(reports, RPT_WARNING, error_msg.c_str());
2071 }
2072 else {
2073 CLOG_WARN(&LOG, "%s", error_msg.c_str());
2074 }
2075 processed_ids.add(id);
2076 return;
2077 }
2078
2079 if (!id->override_library->hierarchy_root && !null_hierarchy_root_is_expected &&
2081 {
2082 std::string error_msg = fmt::format(
2083 "Missing library override hierarchy root data for ID '{}', will be changed to use '{}' as "
2084 "root.",
2085 id->name,
2086 id_root->name);
2087 if (reports) {
2088 BKE_report(reports, RPT_ERROR, error_msg.c_str());
2089 }
2090 else {
2091 CLOG_ERROR(&LOG, "%s", error_msg.c_str());
2092 }
2093 }
2094
2095 lib_override_root_hierarchy_set(bmain, id_root, id, nullptr, processed_ids);
2096
2097 BLI_assert(id->override_library->hierarchy_root != nullptr);
2098}
2099
2102{
2103 ID *id;
2104
2105 BKE_main_relations_create(bmain, 0);
2106 Set<ID *> processed_ids;
2107
2108 FOREACH_MAIN_ID_BEGIN (bmain, id) {
2109 if (!ID_IS_OVERRIDE_LIBRARY_REAL(id)) {
2110 processed_ids.add(id);
2111 continue;
2112 }
2113
2114 if ((options & ONLY_PROCESS_NULL_ROOT_POINTERS) != 0 && id->override_library->hierarchy_root) {
2115 processed_ids.add(id);
2116 continue;
2117 }
2118
2119 lib_override_library_main_hierarchy_id_root_ensure(bmain, id, processed_ids, options, reports);
2120 }
2122
2124}
2125
2127 const ID *id_root_reference,
2128 Vector<std::pair<ID *, ID *>> &references_and_new_overrides,
2129 Map<ID *, ID *> &linkedref_to_old_override)
2130{
2131 id::IDRemapper remapper_overrides_old_to_new;
2132 Vector<ID *> nomain_ids;
2133 Vector<ID *> new_overrides;
2134
2135 /* Used to ensure that newly created overrides have all of their 'linked id' pointers remapped to
2136 * the matching override if it exists. Necessary because in partial resync case, some existing
2137 * liboverride may be used by the resynced ones, yet they would not be part of the resynced
2138 * partial hierarchy, so #BKE_lib_override_library_create_from_tag cannot find them and handle
2139 * their remapping. */
2140 id::IDRemapper remapper_overrides_reference_to_old;
2141
2142 /* Add remapping from old to new overrides. */
2143 for (auto [id_reference, id_override_new] : references_and_new_overrides) {
2144 new_overrides.append(id_override_new);
2145 ID *id_override_old = linkedref_to_old_override.lookup_default_as(id_reference, nullptr);
2146 if (id_override_old == nullptr) {
2147 continue;
2148 }
2149 remapper_overrides_old_to_new.add(id_override_old, id_override_new);
2150 }
2151
2152 for (auto item_iter : linkedref_to_old_override.items()) {
2153 /* Remap no-main override IDs we just created too. */
2154 ID *id_override_old_iter = item_iter.value;
2155 if ((id_override_old_iter->tag & ID_TAG_NO_MAIN) != 0) {
2156 nomain_ids.append(id_override_old_iter);
2157 }
2158 /* And remap linked data to old (existing, unchanged) overrides, when no new one was created.
2159 */
2160 ID *id_reference_iter = item_iter.key;
2161
2162 /* NOTE: Usually `id_reference_iter->lib == id_root_reference->lib` should always be true.
2163 * However, there are some cases where it is not, e.g. if the linked reference of a liboverride
2164 * is relocated to another ID in another library. */
2165#if 0
2166 BLI_assert(id_reference_iter->lib == id_root_reference->lib);
2167 UNUSED_VARS_NDEBUG(id_root_reference);
2168#else
2169 UNUSED_VARS(id_root_reference);
2170#endif
2171 if (!id_reference_iter->newid) {
2172 remapper_overrides_reference_to_old.add(id_reference_iter, id_override_old_iter);
2173 }
2174 }
2175
2176 /* Remap all IDs to use the new override. */
2177 BKE_libblock_remap_multiple(bmain, remapper_overrides_old_to_new, 0);
2179 nomain_ids,
2181 remapper_overrides_old_to_new,
2183 /* In new overrides, remap linked ID to their matching already existing overrides. */
2185 new_overrides,
2187 remapper_overrides_reference_to_old,
2189}
2190
2210
2211using LibOverrideMissingIDsData_Key = const std::pair<std::string, Library *>;
2212using LibOverrideMissingIDsData_Value = std::deque<ID *>;
2214 std::map<LibOverrideMissingIDsData_Key, LibOverrideMissingIDsData_Value>;
2215
2216/* Return a key suitable for the missing IDs mapping, i.e. a pair of
2217 * `<full ID name (including first two ID type chars) without a potential numeric extension,
2218 * ID library>`.
2219 *
2220 * So e.g. returns `<"OBMyObject", lib>` for ID from `lib` with names like `"OBMyObject"`,
2221 * `"OBMyObject.002"`, `"OBMyObject.12345"`, and so on, but _not_ `"OBMyObject.12.002"`.
2222 */
2224{
2225 std::string id_name_key(id->name);
2226 const size_t last_key_index = id_name_key.find_last_not_of("0123456789");
2227
2228 BLI_assert(last_key_index != std::string::npos);
2229
2230 if (id_name_key[last_key_index] == '.') {
2231 id_name_key.resize(last_key_index);
2232 }
2233
2234 return LibOverrideMissingIDsData_Key(id_name_key, id->lib);
2235}
2236
2238 Main *bmain, const bool is_relocate)
2239{
2240 LibOverrideMissingIDsData missing_ids;
2241 ID *id_iter;
2242 FOREACH_MAIN_ID_BEGIN (bmain, id_iter) {
2243 if (is_relocate) {
2244 if (!ID_IS_OVERRIDE_LIBRARY(id_iter)) {
2245 continue;
2246 }
2247 const int required_tags = ID_TAG_LIBOVERRIDE_NEED_RESYNC;
2248 if ((id_iter->tag & required_tags) != required_tags) {
2249 continue;
2250 }
2251 }
2252 else { /* Handling of missing linked liboverrides. */
2253 if (!ID_IS_LINKED(id_iter)) {
2254 continue;
2255 }
2256 const int required_tags = (ID_TAG_MISSING | ID_TAG_LIBOVERRIDE_NEED_RESYNC);
2257 if ((id_iter->tag & required_tags) != required_tags) {
2258 continue;
2259 }
2260 }
2261
2263 std::pair<LibOverrideMissingIDsData::iterator, bool> value = missing_ids.try_emplace(
2265 value.first->second.push_back(id_iter);
2266 }
2268
2269 return missing_ids;
2270}
2271
2273 LibOverrideMissingIDsData &missing_ids,
2274 Map<ID *, ID *> &linkedref_to_old_override,
2275 ID *id_override)
2276{
2278 const LibOverrideMissingIDsData::iterator value = missing_ids.find(key);
2279 if (value == missing_ids.end()) {
2280 return nullptr;
2281 }
2282 while (!value->second.empty()) {
2283 ID *match_id = value->second.front();
2284 value->second.pop_front();
2285 /* Never match multiple new liboverrides and their linked reference IDs to a same old one. This
2286 * will break many things, including remapping, deletion of old liboverrides, etc.
2287 *
2288 * This ensures that if the current found 'missing ID match' was a liboverride previously, its
2289 * linked reference ID is not already associated to another old liboverride.
2290 *
2291 * Not a very common situation, but see e.g. #156601 for a reproducible case.
2292 */
2293 if (ID_IS_OVERRIDE_LIBRARY_REAL(match_id) &&
2294 linkedref_to_old_override.contains_as(match_id->override_library->reference))
2295 {
2296 continue;
2297 }
2298 return match_id;
2299 }
2300 return nullptr;
2301}
2302
2304 const Map<Library *, Library *> *new_to_old_libraries_map,
2305 Scene *scene,
2306 ViewLayer *view_layer,
2307 ID *id_root,
2308 LinkNode *id_resync_roots,
2309 ListBaseT<ID> *no_main_ids_list,
2310 Collection *override_resync_residual_storage,
2311 const bool do_hierarchy_enforce,
2312 const bool do_post_process,
2313 BlendFileReadReport *reports)
2314{
2316
2317 ID *id_root_reference = id_root->override_library->reference;
2318 ID *id;
2319
2320 const Object *old_active_object = nullptr;
2321 if (view_layer) {
2322 BKE_view_layer_synced_ensure(*bmain, scene, view_layer);
2323 old_active_object = BKE_view_layer_active_object_get(view_layer);
2324 }
2325 else if (scene) {
2327 }
2328 else {
2330 }
2331
2332 if (id_root_reference->tag & ID_TAG_MISSING) {
2333 BKE_reportf(reports != nullptr ? reports->reports : nullptr,
2334 RPT_ERROR,
2335 "Impossible to resync data-block %s and its dependencies, as its linked reference "
2336 "is missing",
2337 id_root->name + 2);
2338 return false;
2339 }
2340
2341 BKE_main_relations_create(bmain, 0);
2343 data.bmain = bmain;
2344 data.scene = scene;
2345 data.tag = ID_TAG_DOIT;
2346 data.missing_tag = ID_TAG_MISSING;
2347 data.is_override = true;
2348 data.is_resync = true;
2349
2350 data.root_set(id_root);
2351 data.hierarchy_root_set(id_root->override_library->hierarchy_root);
2352
2354
2355 /* Mapping 'linked reference IDs' -> 'Local override IDs' of existing overrides, populated from
2356 * each sub-tree that actually needs to be resynced. */
2357 Map<ID *, ID *> linkedref_to_old_override;
2358
2359 /* Only tag linked IDs from related linked reference hierarchy that are actually part of
2360 * the sub-trees of each detected sub-roots needing resync. */
2361 for (LinkNode *resync_root_link = id_resync_roots; resync_root_link != nullptr;
2362 resync_root_link = resync_root_link->next)
2363 {
2364 ID *id_resync_root = static_cast<ID *>(resync_root_link->link);
2365 BLI_assert(ID_IS_OVERRIDE_LIBRARY_REAL(id_resync_root));
2366
2367 if ((id_resync_root->tag & ID_TAG_NO_MAIN) != 0) {
2369 "While dealing with root '%s', resync root ID '%s' (%p) found to be alreaady "
2370 "resynced.\n",
2371 id_root->name,
2372 id_resync_root->name,
2373 id_resync_root);
2374 }
2375 // if (no_main_ids_list && BLI_findindex(no_main_ids_list, id_resync_root) != -1) {
2376 // CLOG_ERROR(
2377 // &LOG,
2378 // "While dealing with root '%s', resync root ID '%s' found to be alreaady
2379 // resynced.\n", id_root->name, id_resync_root->name);
2380 // }
2381
2382 ID *id_resync_root_reference = id_resync_root->override_library->reference;
2383
2384 if (id_resync_root_reference->tag & ID_TAG_MISSING) {
2386 reports != nullptr ? reports->reports : nullptr,
2387 RPT_ERROR,
2388 "Impossible to resync data-block %s and its dependencies, as its linked reference "
2389 "is missing",
2390 id_root->name + 2);
2392 data.clear();
2393 return false;
2394 }
2395
2396 /* Tag local overrides of the current resync sub-hierarchy. */
2398 data.is_override = true;
2399 data.root_set(id_resync_root);
2401
2402 /* Tag reference data matching the current resync sub-hierarchy. */
2404 data.is_override = false;
2405 data.root_set(id_resync_root->override_library->reference);
2406 data.hierarchy_root_set(
2409
2412
2413 FOREACH_MAIN_ID_BEGIN (bmain, id) {
2414 if ((id->lib != id_root->lib) || !ID_IS_OVERRIDE_LIBRARY(id)) {
2415 continue;
2416 }
2417
2418 /* IDs that get fully removed from linked data remain as local overrides (using place-holder
2419 * linked IDs as reference), but they are often not reachable from any current valid local
2420 * override hierarchy anymore. This will ensure they get properly deleted at the end of this
2421 * function. */
2422 if (!ID_IS_LINKED(id) && ID_IS_OVERRIDE_LIBRARY_REAL(id) &&
2423 (id->override_library->reference->tag & ID_TAG_MISSING) != 0 &&
2424 /* Unfortunately deleting obdata means deleting their objects too. Since there is no
2425 * guarantee that a valid override object using an obsolete override obdata gets properly
2426 * updated, we ignore those here for now. In practice this should not be a big issue. */
2427 !OB_DATA_SUPPORT_ID(id->id_type()))
2428 {
2429 id->tag |= ID_TAG_MISSING;
2430 }
2431
2432 /* While this should not happen in typical cases (and won't be properly supported here),
2433 * user is free to do all kind of very bad things, including having different local
2434 * overrides of a same linked ID in a same hierarchy. */
2435 IDOverrideLibrary *id_override_library = BKE_lib_override_library_get(
2436 bmain, id, nullptr, nullptr);
2437
2438 if (id_override_library->hierarchy_root != id_root->override_library->hierarchy_root) {
2439 continue;
2440 }
2441
2442 ID *reference_id = id_override_library->reference;
2443 if (reference_id->id_type() != id->id_type()) {
2444 switch (id->id_type()) {
2445 case ID_KE:
2446 reference_id = reinterpret_cast<ID *>(BKE_key_from_id(reference_id));
2447 break;
2448 case ID_GR:
2449 BLI_assert(reference_id->id_type() == ID_SCE);
2450 reference_id = reinterpret_cast<ID *>(
2451 reinterpret_cast<Scene *>(reference_id)->master_collection);
2452 break;
2453 case ID_NT:
2454 reference_id = reinterpret_cast<ID *>(bke::node_tree_from_id(id));
2455 break;
2456 default:
2457 break;
2458 }
2459 }
2460 if (reference_id == nullptr) {
2461 /* Can happen e.g. when there is a local override of a shape-key, but the matching linked
2462 * obdata (mesh etc.) does not have any shape-key anymore. */
2463 continue;
2464 }
2465 BLI_assert(reference_id->id_type() == id->id_type());
2466
2467 if (!linkedref_to_old_override.contains_as(reference_id)) {
2468 linkedref_to_old_override.add_as(reference_id, id);
2469 if (!ID_IS_OVERRIDE_LIBRARY_REAL(id) || (id->tag & ID_TAG_DOIT) == 0) {
2470 continue;
2471 }
2472 if ((id->override_library->reference->tag & ID_TAG_DOIT) == 0) {
2473 /* We have an override, but now it does not seem to be necessary to override that ID
2474 * anymore. Check if there are some actual overrides from the user, otherwise assume
2475 * that we can get rid of this local override. */
2477 id->override_library->reference->tag |= ID_TAG_DOIT;
2478 }
2479 }
2480 }
2481 }
2483
2484 /* Code above may have added some tags, we need to update this too. */
2487 }
2488
2489 /* Tag all local overrides of the current hierarchy. */
2491 data.is_override = true;
2492 data.root_set(id_root);
2493 data.hierarchy_root_set(id_root->override_library->hierarchy_root);
2495
2497 data.clear();
2498
2499 /* Make new override from linked data. */
2500 /* Note that this call also remaps all pointers of tagged IDs from old override IDs to new
2501 * override IDs (including within the old overrides themselves, since those are tagged too
2502 * above). */
2503 const bool success = BKE_lib_override_library_create_from_tag(
2504 bmain,
2505 nullptr,
2506 id_root_reference,
2508 nullptr,
2509 true,
2510 false);
2511
2512 if (!success) {
2513 return success;
2514 }
2515
2516 /* This used to be the library of the root reference. Should always be the same as the current
2517 * library on readfile case, but may differ when relocating linked data from a library to
2518 * another (See #BKE_blendfile_id_relocate and #BKE_blendfile_library_relocate). */
2519 Library *id_root_reference_lib_old = (new_to_old_libraries_map ?
2520 new_to_old_libraries_map->lookup_default(
2521 id_root_reference->lib, id_root_reference->lib) :
2522 id_root_reference->lib);
2523 const bool is_relocate = id_root_reference_lib_old != id_root_reference->lib;
2524
2525 /* Get a mapping of all missing linked IDs that were liboverrides, to search for 'old
2526 * liboverrides' for newly created ones that do not already have one, in next step. */
2528 bmain, is_relocate);
2529 /* Vector of pairs of reference IDs, and their new override IDs. */
2530 Vector<std::pair<ID *, ID *>> references_and_new_overrides;
2531
2532 ListBaseT<ID> *lb;
2533 FOREACH_MAIN_LISTBASE_BEGIN (bmain, lb) {
2534 ID *id_reference_iter;
2535 FOREACH_MAIN_LISTBASE_ID_BEGIN (lb, id_reference_iter) {
2536 if ((id_reference_iter->tag & ID_TAG_DOIT) == 0 || id_reference_iter->newid == nullptr ||
2537 !ELEM(id_reference_iter->lib, id_root_reference->lib, id_root_reference_lib_old))
2538 {
2539 continue;
2540 }
2541 ID *id_override_new = id_reference_iter->newid;
2542 references_and_new_overrides.append(std::make_pair(id_reference_iter, id_override_new));
2543
2544 ID *id_override_old = linkedref_to_old_override.lookup_default_as(id_reference_iter,
2545 nullptr);
2546
2547 BLI_assert((id_override_new->tag & ID_TAG_LIBOVERRIDE_NEED_RESYNC) == 0);
2548
2549 /* We need to 'move back' newly created override into its proper library (since it was
2550 * duplicated from the reference ID with 'no main' option, it should currently be the same
2551 * as the reference ID one). */
2552 BLI_assert(/* `!ID_IS_LINKED(id_override_new) ||` */ id_override_new->lib ==
2553 id_reference_iter->lib);
2554 BLI_assert(id_override_old == nullptr || id_override_old->lib == id_root->lib);
2555 id_override_new->lib = id_root->lib;
2556
2557 /* The old override may have been created as linked data and then referenced by local data
2558 * during a previous Blender session, in which case it became directly linked and a reference
2559 * to it was stored in the local .blend file. However, since that linked liboverride ID does
2560 * not actually exist in the original library file, on next file read it is lost and marked
2561 * as missing ID. */
2562 if (id_override_old == nullptr && (ID_IS_LINKED(id_override_new) || is_relocate)) {
2564 missing_ids_data, linkedref_to_old_override, id_override_new);
2565 BLI_assert(id_override_old == nullptr || id_override_old->lib == id_override_new->lib);
2566 if (id_override_old != nullptr) {
2567 linkedref_to_old_override.add_as(id_reference_iter, id_override_old);
2568
2569 Key *key_override_old = BKE_key_from_id(id_override_old);
2570 Key *key_reference_iter = BKE_key_from_id(id_reference_iter);
2571 if (key_reference_iter && key_override_old) {
2572 linkedref_to_old_override.add_as(&key_reference_iter->id, &key_override_old->id);
2573 }
2574
2576 "Found missing linked or versioning-generated old override best-match %s for "
2577 "new linked override %s",
2578 id_override_old->name,
2579 id_override_new->name);
2580 }
2581 }
2582
2583 /* Remap step below will tag directly linked ones properly as needed. */
2584 if (ID_IS_LINKED(id_override_new)) {
2585 id_override_new->tag |= ID_TAG_INDIRECT;
2586 }
2587
2588 if (id_override_old != nullptr) {
2589 /* Swap the names between old override ID and new one. */
2590 char id_name_buf[MAX_ID_NAME];
2591 memcpy(id_name_buf, id_override_old->name, sizeof(id_name_buf));
2592 memcpy(id_override_old->name, id_override_new->name, sizeof(id_override_old->name));
2593 memcpy(id_override_new->name, id_name_buf, sizeof(id_override_new->name));
2594
2595 BLI_insertlinkreplace(lb, id_override_old, id_override_new);
2596 id_override_old->tag |= ID_TAG_NO_MAIN;
2597 id_override_new->tag &= ~ID_TAG_NO_MAIN;
2598
2599 lib_override_object_posemode_transfer(id_override_new, id_override_old);
2600
2601 /* Missing old liboverrides cannot transfer their override rules to new liboverride.
2602 * This is fine though, since these are expected to only be 'virtual' linked overrides
2603 * generated by resync of linked overrides. So nothing is expected to be overridden here.
2604 */
2605 if (ID_IS_OVERRIDE_LIBRARY_REAL(id_override_new) &&
2606 (id_override_old->tag & ID_TAG_MISSING) == 0)
2607 {
2608 BLI_assert(ID_IS_OVERRIDE_LIBRARY_REAL(id_override_old));
2609
2610 id_override_new->override_library->flag = id_override_old->override_library->flag;
2611
2612 /* NOTE: Since `runtime->tag` is not copied from old to new liboverride, the potential
2613 * `IDOverrideLibraryTag::TAG_RESYNC_ISOLATED_FROM_ROOT` is kept on the old, to-be-freed
2614 * liboverride, and the new one is assumed to be properly part of its hierarchy again. */
2615
2616 /* Copy over overrides rules from old override ID to new one. */
2618 &id_override_old->override_library->properties);
2620 id_override_new->override_library->properties.first();
2622 id_override_old->override_library->properties.first();
2623 for (; op_new; op_new = op_new->next, op_old = op_old->next) {
2624 lib_override_library_property_copy(op_new, op_old);
2625 }
2626 }
2627
2628 BLI_addtail(no_main_ids_list, id_override_old);
2629 }
2630 else {
2631 /* Add to proper main list, ensure unique name for local ID, sort, and clear relevant
2632 * tags. */
2633 BKE_libblock_management_main_add(bmain, id_override_new);
2634 }
2635 }
2637 }
2639
2640 /* We remap old to new override usages in a separate step, after all new overrides have
2641 * been added to Main.
2642 *
2643 * This function also ensures that newly created overrides get all their linked ID pointers
2644 * remapped to a valid override one, whether new or already existing. In partial resync case,
2645 * #BKE_lib_override_library_create_from_tag cannot reliably discover _all_ valid existing
2646 * overrides used by the newly resynced ones, since the local resynced hierarchy may not contain
2647 * them. */
2649 bmain, id_root_reference, references_and_new_overrides, linkedref_to_old_override);
2650
2652
2653 Vector<ID *> id_override_old_vector;
2654
2655 /* We need to apply override rules in a separate loop, after all ID pointers have been properly
2656 * remapped, and all new local override IDs have gotten their proper original names, otherwise
2657 * override operations based on those ID names would fail. */
2658 FOREACH_MAIN_ID_BEGIN (bmain, id) {
2659 if ((id->tag & ID_TAG_DOIT) == 0 || id->newid == nullptr || id->lib != id_root_reference->lib)
2660 {
2661 continue;
2662 }
2663
2664 ID *id_override_new = id->newid;
2665 if (!ID_IS_OVERRIDE_LIBRARY_REAL(id_override_new)) {
2666 continue;
2667 }
2668
2669 ID *id_override_old = linkedref_to_old_override.lookup_default_as(id, nullptr);
2670 if (id_override_old == nullptr) {
2671 continue;
2672 }
2673
2674 if (ID_IS_OVERRIDE_LIBRARY_REAL(id_override_old)) {
2675 /* The remapping from old to new liboverrides above has a sad side effect on ShapeKeys. Since
2676 * old liboverrides are also remapped, it means that the old liboverride owner of the shape
2677 * key is also now pointing to the new liboverride shape key, not the old one. Since shape
2678 * keys do not own their liboverride data, the old liboverride shape key user has to be
2679 * restored to use the old liboverride shape-key, otherwise applying shape key override
2680 * operations will be useless (would apply using the new, from linked data, liboverride,
2681 * being effectively a no-op). */
2682 Key **key_override_old_p = BKE_key_from_id_p(id_override_old);
2683 if (key_override_old_p != nullptr && *key_override_old_p != nullptr) {
2684 Key *key_linked_reference = BKE_key_from_id(id_override_new->override_library->reference);
2685 BLI_assert(key_linked_reference != nullptr);
2686 BLI_assert(key_linked_reference->id.newid == &(*key_override_old_p)->id);
2687 Key *key_override_old = id_cast<Key *>(
2688 linkedref_to_old_override.lookup_default_as(&key_linked_reference->id, nullptr));
2689 BLI_assert(key_override_old != nullptr);
2690 *key_override_old_p = key_override_old;
2691 }
2692
2693 /* Apply rules on new override ID using old one as 'source' data. */
2694 /* Note that since we already remapped ID pointers in old override IDs to new ones, we
2695 * can also apply ID pointer override rules safely here. */
2696 PointerRNA rnaptr_src = RNA_id_pointer_create(id_override_old);
2697 PointerRNA rnaptr_dst = RNA_id_pointer_create(id_override_new);
2698
2699 /* In case the parent of the liboverride object matches hierarchy-wise the parent of its
2700 * linked reference, also enforce clearing any override of the other related parenting
2701 * settings.
2702 *
2703 * While this may break some rare use-cases, in almost all situations the best behavior here
2704 * is to follow the values from the reference data (especially when it comes to the invert
2705 * parent matrix). */
2706 bool do_clear_parenting_override = false;
2707 if (id_override_new->id_type() == ID_OB) {
2708 Object *ob_old = reinterpret_cast<Object *>(id_override_old);
2709 Object *ob_new = reinterpret_cast<Object *>(id_override_new);
2710 if (ob_new->parent && ob_new->parent != ob_old->parent &&
2711 /* Parent is not a liboverride. */
2712 (ob_new->parent ==
2713 reinterpret_cast<Object *>(ob_new->id.override_library->reference)->parent ||
2714 /* Parent is a hierarchy-matching liboverride. */
2716 reinterpret_cast<Object *>(ob_new->parent->id.override_library->reference) ==
2717 reinterpret_cast<Object *>(ob_new->id.override_library->reference)->parent)))
2718 {
2719 do_clear_parenting_override = true;
2720 }
2721 }
2722
2723 /* We remove any operation tagged with `LIBOVERRIDE_OP_FLAG_IDPOINTER_MATCH_REFERENCE`,
2724 * that way the potentially new pointer will be properly kept, when old one is still valid
2725 * too (typical case: assigning new ID to some usage, while old one remains used elsewhere
2726 * in the override hierarchy). */
2727 for (IDOverrideLibraryProperty &op :
2728 id_override_new->override_library->properties.items_mutable())
2729 {
2730 for (IDOverrideLibraryPropertyOperation &opop : op.operations.items_mutable()) {
2733 BLI_freelinkN(&op.operations, &opop);
2734 }
2735 }
2736 if (op.operations.is_empty()) {
2738 }
2739 else if (do_clear_parenting_override) {
2740 if (strstr(op.rna_path, "matrix_parent_inverse") || strstr(op.rna_path, "parent_type") ||
2741 strstr(op.rna_path, "parent_bone") || strstr(op.rna_path, "parent_vertices"))
2742 {
2744 "Deleting liboverride property '%s' from object %s, as its parent pointer "
2745 "matches the reference data hierarchy wise",
2746 id_override_new->name + 2,
2747 op.rna_path);
2749 }
2750 }
2751 }
2752
2754 &rnaptr_dst,
2755 &rnaptr_src,
2756 nullptr,
2757 id_override_new->override_library,
2758 do_hierarchy_enforce ? RNA_OVERRIDE_APPLY_FLAG_IGNORE_ID_POINTERS :
2760
2761 /* Clear the old shape key pointer again, otherwise it won't make ID management code happy
2762 * when freeing (at least from user count side of things). */
2763 if (key_override_old_p != nullptr) {
2764 *key_override_old_p = nullptr;
2765 }
2766 }
2767
2768 id_override_old_vector.append(id_override_old);
2769 }
2771
2772 /* Once overrides have been properly 'transferred' from old to new ID, we can clear ID usages
2773 * of the old one.
2774 * This is necessary in case said old ID is not in Main anymore. */
2775 id::IDRemapper id_remapper;
2777 id_override_old_vector,
2779 id_remapper,
2781 for (ID *id_override_old : id_override_old_vector) {
2782 id_override_old->tag |= ID_TAG_NO_USER_REFCOUNT;
2783 }
2784 id_override_old_vector.clear();
2785
2786 /* Delete old override IDs.
2787 * Note that we have to use tagged group deletion here, since ID deletion also uses
2788 * ID_TAG_DOIT. This improves performances anyway, so everything is fine. */
2789 int user_edited_overrides_deletion_count = 0;
2790 FOREACH_MAIN_ID_BEGIN (bmain, id) {
2791 if (id->tag & ID_TAG_DOIT) {
2792 /* Since this code can also be called on linked liboverride now (during recursive resync),
2793 * order of processing cannot guarantee anymore that the old liboverride won't be tagged for
2794 * deletion before being processed by this loop (which would then untag it again).
2795 *
2796 * So instead store old liboverrides in Main into a temp list again, and do the tagging
2797 * separately once this loop over all IDs in main is done. */
2798 if (id->newid != nullptr && id->lib == id_root_reference->lib) {
2799 ID *id_override_old = linkedref_to_old_override.lookup_default_as(id, nullptr);
2800
2801 if (id_override_old != nullptr) {
2802 id->newid->tag &= ~ID_TAG_DOIT;
2803 if (id_override_old->tag & ID_TAG_NO_MAIN) {
2804 id_override_old->tag |= ID_TAG_DOIT;
2805 BLI_assert(BLI_findindex(no_main_ids_list, id_override_old) != -1);
2806 }
2807 else {
2808 /* Defer tagging. */
2809 id_override_old_vector.append(id_override_old);
2810 }
2811 }
2812 }
2813 id->tag &= ~ID_TAG_DOIT;
2814 }
2815 /* Also deal with old overrides that went missing in new linked data - only for real local
2816 * overrides for now, not those who are linked. */
2817 else if (id->tag & ID_TAG_MISSING && !ID_IS_LINKED(id) && ID_IS_OVERRIDE_LIBRARY_REAL(id)) {
2818 bool do_delete;
2819 ID *hierarchy_root = id->override_library->hierarchy_root;
2820 if (id->override_library->reference->lib->id.tag & ID_TAG_MISSING) {
2821 /* Do not delete overrides which reference is missing because the library itself is missing
2822 * (ref. #100586). */
2823 do_delete = false;
2824 }
2825 else if (hierarchy_root != nullptr &&
2826 hierarchy_root->override_library->reference->tag & ID_TAG_MISSING)
2827 {
2828 /* Do not delete overrides which root hierarchy reference is missing. This would typically
2829 * cause more harm than good. */
2830 do_delete = false;
2831 }
2833 /* If user never edited them, we can delete them. */
2834 do_delete = true;
2835 CLOG_DEBUG(&LOG_RESYNC, "Old override %s is being deleted", id->name);
2836 }
2837#if 0
2838 else {
2839 /* Otherwise, keep them, user needs to decide whether what to do with them. */
2840 BLI_assert((id->tag & ID_TAG_DOIT) == 0);
2841 do_delete = false;
2842 id_fake_user_set(id);
2845 "Old override %s is being kept around as it was user-edited",
2846 id->name);
2847 }
2848#else
2849 else {
2850 /* Delete them nevertheless, with fat warning, user needs to decide whether they want to
2851 * save that version of the file (and accept the loss), or not. */
2852 do_delete = true;
2854 "Old override %s is being deleted even though it was user-edited",
2855 id->name);
2856 user_edited_overrides_deletion_count++;
2857 }
2858#endif
2859 if (do_delete) {
2860 id->tag |= ID_TAG_DOIT;
2861 id->tag &= ~ID_TAG_MISSING;
2862 }
2863 else if (id->override_library->runtime != nullptr) {
2864 /* Cleanup of this temporary tag, since that somewhat broken liboverride is explicitly
2865 * kept for now. */
2866 id->override_library->runtime->tag &= ~IDOverrideLibraryTag::TAG_RESYNC_ISOLATED_FROM_ROOT;
2867 }
2868 }
2869 }
2871
2872 /* Finalize tagging old liboverrides for deletion. */
2873 for (ID *id_override_old : id_override_old_vector) {
2874 id_override_old->tag |= ID_TAG_DOIT;
2875 }
2876 id_override_old_vector.clear();
2877
2878 /* Cleanup, many pointers in this Map are already invalid now. */
2879 linkedref_to_old_override.clear();
2880
2881 /* Prevent any expansive update process, as this is a in-progress cleanup,
2882 * #lib_override_cleanup_after_resync will ensure that this is called properly at the end of the
2883 * whole resync process. */
2885 bmain,
2886 {.prevent_liboverride_hierarchy_root_ensure = true, .prevent_invariants_update = true});
2887
2888 /* At this point, `id_root` may have been resynced, therefore deleted. In that case we need to
2889 * update it to its new version.
2890 */
2891 if (id_root_reference->newid != nullptr) {
2892 id_root = id_root_reference->newid;
2893 }
2894
2895 if (user_edited_overrides_deletion_count > 0) {
2896 BKE_reportf(reports != nullptr ? reports->reports : nullptr,
2898 "During resync of data-block %s, %d obsolete overrides were deleted, that had "
2899 "local changes defined by user",
2900 id_root->name + 2,
2901 user_edited_overrides_deletion_count);
2902 }
2903
2904 if (do_post_process) {
2905 /* Essentially ensures that potentially new overrides of new objects will be instantiated. */
2906 /* NOTE: Here 'reference' collection and 'newly added' collection are the same, which is fine
2907 * since we already relinked old root override collection to new resync'ed one above. So this
2908 * call is not expected to instantiate this new resync'ed collection anywhere, just to ensure
2909 * that we do not have any stray objects. */
2911 scene,
2912 view_layer,
2913 nullptr,
2914 id_root_reference,
2915 id_root,
2916 override_resync_residual_storage,
2917 old_active_object,
2918 true);
2919 }
2920
2921 /* Cleanup. */
2923 /* That one should not be needed in fact, as #BKE_id_multi_tagged_delete call above should have
2924 * deleted all tagged IDs. */
2925 BKE_main_id_tag_all(bmain, ID_TAG_DOIT, false);
2926
2927 return success;
2928}
2929
2932{
2933 /* Deletions during the resync itself skip the invariants update. Ensure it happened before
2934 * searching for unused IDs, since obsolete node sockets kept alive by an outdated node tree
2935 * still count as users of the IDs they reference. */
2937
2938 LibQueryUnusedIDsData parameters;
2939 parameters.do_local_ids = true;
2940 parameters.do_linked_ids = true;
2941 parameters.do_recursive = true;
2942 parameters.filter_fn = [](const ID *id) -> bool {
2943 if (ID_IS_LINKED(id) && (id->tag & ID_TAG_MISSING) != 0) {
2944 return true;
2945 }
2946 /* This is a fairly complex case.
2947 *
2948 * LibOverride resync process takes care of removing 'no more valid' liboverrides (see at the
2949 * end of #lib_override_library_main_resync_on_library_indirect_level). However, since it does
2950 * not resync data which linked reference is missing (see
2951 * #lib_override_library_main_resync_id_skip_check), these are kept 'as is'. Indeed,
2952 * liboverride resync code cannot know if a specific liboverride data is only part of its
2953 * hierarchy, or if it is also used by some other data (in which case it should be preserved if
2954 * the linked reference goes missing).
2955 *
2956 * So instead, we consider these cases as also valid candidates for deletion here, since the
2957 * whole recursive process in `BKE_lib_query_unused_ids_tag` will ensure that if there is still
2958 * any valid user of these, they won't get tagged for deletion.
2959 *
2960 * Also, do not delete 'orphaned' liboverrides if it's a hierarchy root, or if its hierarchy
2961 * root's reference is missing, since this is much more likely a case of actual missing data,
2962 * rather than changes in the liboverride's hierarchy in the linked data.
2963 */
2964 if (ID_IS_OVERRIDE_LIBRARY(id)) {
2965 const IDOverrideLibrary *override_library = BKE_lib_override_library_get(
2966 nullptr, id, nullptr, nullptr);
2967 /* NOTE: Since hierarchy root final validation is only done on 'expected-to-be-processed' IDs
2968 * after each level of recursive resync (and not the whole Main data-base, see @00375abc38),
2969 * `root` may now be null here. Likely, because deleting some root override sets that pointer
2970 * to null.
2971 *
2972 * If this turns out to be a wrong fix, the other likely solution would be to add a call to
2973 * #BKE_lib_override_library_main_hierarchy_root_ensure before calling
2974 * #lib_override_cleanup_after_resync.
2975 *
2976 * This was technically already possible before (e.g. for non-hierarchical isolated
2977 * liboverrides), but in practice this is very rare case. */
2978 const ID *root = override_library->hierarchy_root;
2979 if (root && (root == id || (root->override_library->reference->tag & ID_TAG_MISSING) != 0)) {
2980 return false;
2981 }
2982 return ((override_library->reference->tag & ID_TAG_MISSING) != 0);
2983 }
2984 return false;
2985 };
2986 BKE_lib_query_unused_ids_tag(bmain, ID_TAG_DOIT, parameters);
2987 if (parameters.num_total[INDEX_ID_NULL]) {
2989 "Deleting %d unused linked missing IDs and their unused liboverrides (including %d "
2990 "local ones)\n",
2991 parameters.num_total[INDEX_ID_NULL],
2992 parameters.num_local[INDEX_ID_NULL]);
2993 }
2994 /* Do ensure invariants after deletion here, as this is a final cleanup call. */
2995 BKE_id_multi_tagged_delete(bmain, {.prevent_liboverride_hierarchy_root_ensure = true});
2996}
2997
2999 Scene *scene,
3000 ViewLayer *view_layer,
3001 ID *id_root,
3002 Collection *override_resync_residual_storage,
3003 const bool do_hierarchy_enforce,
3004 BlendFileReadReport *reports)
3005{
3006 ListBaseT<ID> no_main_ids_list = {nullptr};
3007 LinkNode id_resync_roots{};
3008 id_resync_roots.link = id_root;
3009 id_resync_roots.next = nullptr;
3010
3011 const bool success = lib_override_library_resync(bmain,
3012 nullptr,
3013 scene,
3014 view_layer,
3015 id_root,
3016 &id_resync_roots,
3017 &no_main_ids_list,
3018 override_resync_residual_storage,
3019 do_hierarchy_enforce,
3020 true,
3021 reports);
3022
3023 for (ID &id_iter : no_main_ids_list.items_mutable()) {
3024 BKE_id_free(bmain, &id_iter);
3025 }
3026
3027 /* Cleanup global namemap, to avoid extra processing with regular ID name management. Better to
3028 * re-create the global namemap on demand. */
3030
3032
3033 return success;
3034}
3035
3037 const int library_indirect_level,
3038 const bool do_strict_equal)
3039{
3040 const int id_lib_level = (ID_IS_LINKED(id) ? id->lib->runtime->temp_index : 0);
3041 return do_strict_equal ? id_lib_level == library_indirect_level :
3042 id_lib_level <= library_indirect_level;
3043}
3044
3045/* Check ancestors overrides for resync, to ensure all IDs in-between two tagged-for-resync ones
3046 * are also properly tagged.
3047 *
3048 * WARNING: Expects `bmain` to have valid relation data.
3049 *
3050 * Returns `true` if it finds an ancestor within the current liboverride hierarchy also tagged as
3051 * needing resync, `false` otherwise.
3052 *
3053 * NOTE: If `check_only` is true, it only does the check and returns, without any modification to
3054 * the data.
3055 */
3057 ID *id_root,
3058 ID *id_from,
3059 const int library_indirect_level,
3060 bool is_in_partial_resync_hierarchy)
3061{
3063 BLI_assert(id_root->override_library->hierarchy_root != nullptr);
3064
3065 if (!lib_override_resync_id_lib_level_is_valid(id_root, library_indirect_level, false)) {
3066 CLOG_ERROR(
3067 &LOG,
3068 "While processing indirect level %d, ID %s from lib %s of indirect level %d detected "
3069 "as needing resync, skipping",
3070 library_indirect_level,
3071 id_root->name,
3072 id_root->lib ? id_root->lib->filepath : "<LOCAL>",
3073 id_root->lib ? id_root->lib->runtime->temp_index : 0);
3075 return;
3076 }
3077
3078 MainIDRelationsEntry *entry = bmain->relations->relations_from_pointers->lookup(id_root);
3079 BLI_assert(entry != nullptr);
3080
3081 bool is_reprocessing_current_entry = false;
3083 /* This ID is already being processed, this indicates a dependency loop. */
3085
3086 if (id_root->tag & ID_TAG_LIBOVERRIDE_NEED_RESYNC) {
3087 /* This ID is already tagged for resync, then the loop leading back to it is also fully
3088 * tagged for resync, nothing else to do. */
3089 BLI_assert(is_in_partial_resync_hierarchy);
3090 return;
3091 }
3092 if (!is_in_partial_resync_hierarchy) {
3093 /* This ID is not tagged for resync, and is part of a loop where none of the other IDs are
3094 * tagged for resync, nothing else to do. */
3095 return;
3096 }
3097 /* This ID is not yet tagged for resync, but is part of a loop which is (partially) tagged
3098 * for resync.
3099 * The whole loop needs to be processed a second time to ensure all of its members are properly
3100 * tagged for resync then. */
3101 is_reprocessing_current_entry = true;
3102
3103 CLOG_DEBUG(
3104 &LOG,
3105 "ID %s (%p) is detected as part of a hierarchy dependency loop requiring resync, it "
3106 "is now being re-processed to ensure proper tagging of the whole loop",
3107 id_root->name,
3108 id_root->lib);
3109 }
3110 else if (entry->tags & MAINIDRELATIONS_ENTRY_TAGS_PROCESSED) {
3111 /* This ID has already been processed. */
3113
3114 /* If it was already detected as needing resync, then its whole sub-tree should also be fully
3115 * processed. Only need to ensure that it is not tagged as potential partial resync root
3116 * anymore, if now processed as part of another partial resync hierarchy. */
3117 if (id_root->tag & ID_TAG_LIBOVERRIDE_NEED_RESYNC) {
3118 if (entry->tags & MAINIDRELATIONS_ENTRY_TAGS_DOIT && is_in_partial_resync_hierarchy) {
3119 CLOG_DEBUG(
3120 &LOG,
3121 "ID %s (%p) was marked as a potential root for partial resync, but it is used by "
3122 "%s (%p), which is also tagged for resync, so it is not a root after all",
3123 id_root->name,
3124 id_root->lib,
3125 id_from->name,
3126 id_from->lib);
3127
3129 }
3130 return;
3131 }
3132 /* Else, if it is not being processed as part of a resync hierarchy, nothing more to do either,
3133 * its current status and the one of its whole dependency tree is also assumed valid. */
3134 if (!is_in_partial_resync_hierarchy) {
3135 return;
3136 }
3137
3138 /* Else, this ID was processed before and not detected as needing resync, but it now needs
3139 * resync, so its whole sub-tree needs to be re-processed to be properly tagged as needing
3140 * resync. */
3142 }
3143
3144 if (is_in_partial_resync_hierarchy) {
3145 BLI_assert(id_from != nullptr);
3146
3147 if ((id_root->tag & ID_TAG_LIBOVERRIDE_NEED_RESYNC) == 0) {
3148 CLOG_DEBUG(&LOG,
3149 "ID %s (%p) now tagged as needing resync because they are used by %s (%p) "
3150 "that needs to be resynced",
3151 id_root->name,
3152 id_root->lib,
3153 id_from->name,
3154 id_from->lib);
3156 }
3157 }
3158 else if (id_root->tag & ID_TAG_LIBOVERRIDE_NEED_RESYNC) {
3159 /* Not yet within a partial resync hierarchy, and this ID is tagged for resync, it is a
3160 * potential partial resync root. */
3161 is_in_partial_resync_hierarchy = true;
3162 }
3163
3164 /* Temporary tag to help manage dependency loops. */
3165 if (!is_reprocessing_current_entry) {
3168
3169 /* Since this ID is reached from the hierarchy root, it is not isolated from it. */
3170 if (id_root->override_library->hierarchy_root != id_root &&
3171 id_root->override_library->runtime != nullptr)
3172 {
3173 id_root->override_library->runtime->tag &=
3174 ~IDOverrideLibraryTag::TAG_RESYNC_ISOLATED_FROM_ROOT;
3175 }
3176 }
3177
3178 /* Check the whole sub-tree hierarchy of this ID. */
3179 for (MainIDRelationsEntryItem *entry_item = entry->to_ids; entry_item != nullptr;
3180 entry_item = entry_item->next)
3181 {
3183 continue;
3184 }
3185 ID *id_to = entry_item->id_pointer.to;
3186 /* Ensure the 'real' override is processed, in case `id_to` is e.g. an embedded ID, get its
3187 * owner instead. */
3188 BKE_lib_override_library_get(bmain, id_to, nullptr, &id_to);
3189
3190 if (lib_override_hierarchy_dependencies_skip_check(id_root, id_to, true)) {
3191 continue;
3192 }
3194 "Check above ensured `id_to` is a liboverride, so it should be a real one (not "
3195 "an embedded one)");
3196
3197 /* Non-matching hierarchy root IDs mean this is not the same liboverride hierarchy anymore. */
3199 continue;
3200 }
3201
3203 bmain, id_to, id_root, library_indirect_level, is_in_partial_resync_hierarchy);
3204
3205 /* Call above may have changed that status in case of dependency loop, update it for the next
3206 * dependency processing. */
3207 is_in_partial_resync_hierarchy = (id_root->tag & ID_TAG_LIBOVERRIDE_NEED_RESYNC) != 0;
3208 }
3209
3210 if (!is_reprocessing_current_entry) {
3213
3216
3217 if (is_in_partial_resync_hierarchy &&
3218 (id_from == nullptr || (id_from->tag & ID_TAG_LIBOVERRIDE_NEED_RESYNC) == 0))
3219 {
3220 /* This ID (and its whole sub-tree of dependencies) is now considered as processed. If it is
3221 * tagged for resync, but its 'calling parent' is not, it is a potential partial resync root.
3222 */
3223 CLOG_DEBUG(
3224 &LOG_RESYNC, "Potential root for partial resync: %s (%p)", id_root->name, id_root->lib);
3226 }
3227 }
3228}
3229
3230/* Return true if the ID should be skipped for resync given current context. */
3232 const int library_indirect_level)
3233{
3234 if (!ID_IS_OVERRIDE_LIBRARY_REAL(id)) {
3235 return true;
3236 }
3237
3238 if (!lib_override_resync_id_lib_level_is_valid(id, library_indirect_level, true)) {
3239 return true;
3240 }
3241
3242 /* Do not attempt to resync from missing data. */
3243 if (((id->tag | id->override_library->reference->tag) & ID_TAG_MISSING) != 0) {
3244 return true;
3245 }
3246
3247 if (id->override_library->flag & LIBOVERRIDE_FLAG_NO_HIERARCHY) {
3248 /* This ID is not part of an override hierarchy. */
3250 return true;
3251 }
3252
3253 /* Do not attempt to resync when hierarchy root is missing, this would usually do more harm
3254 * than good. */
3255 ID *hierarchy_root = id->override_library->hierarchy_root;
3256 if (hierarchy_root == nullptr ||
3257 ((hierarchy_root->tag | hierarchy_root->override_library->reference->tag) &
3258 ID_TAG_MISSING) != 0)
3259 {
3260 return true;
3261 }
3262
3263 return false;
3264}
3265
3278 Main *bmain, ID *id, const int library_indirect_level)
3279{
3280 BLI_assert(!lib_override_library_main_resync_id_skip_check(id, library_indirect_level));
3281
3282 if (id->override_library->hierarchy_root == id ||
3283 (id->override_library->runtime->tag & IDOverrideLibraryTag::TAG_RESYNC_ISOLATED_FROM_ROOT) ==
3285 {
3286 BLI_assert(id->override_library->hierarchy_root != id ||
3287 id->override_library->runtime == nullptr ||
3288 (id->override_library->runtime->tag &
3290 return true;
3291 }
3292
3293 MainIDRelationsEntry *entry = bmain->relations->relations_from_pointers->lookup(id);
3294 BLI_assert(entry != nullptr);
3295
3297 /* This ID has already been processed, since 'true' conditions have already been checked above,
3298 * it is validated as an isolated liboverride. */
3299 return false;
3300 }
3301 /* This way we won't process again that ID, should we encounter it again through another
3302 * relationship hierarchy. */
3304
3305 for (MainIDRelationsEntryItem *to_id_entry = entry->to_ids; to_id_entry != nullptr;
3306 to_id_entry = to_id_entry->next)
3307 {
3309 continue;
3310 }
3311 ID *to_id = to_id_entry->id_pointer.to;
3312 if (lib_override_library_main_resync_id_skip_check(to_id, library_indirect_level)) {
3313 continue;
3314 }
3315
3317 bmain, to_id, library_indirect_level))
3318 {
3319 id->override_library->runtime->tag &= ~IDOverrideLibraryTag::TAG_RESYNC_ISOLATED_FROM_ROOT;
3320 return true;
3321 }
3322 }
3323
3324 return false;
3325}
3326
3327/* Once all IDs needing resync have been tagged, partial ID roots can be found by processing each
3328 * tagged-for-resync IDs' ancestors within their liboverride hierarchy. */
3330 Map<ID *, LinkNodePair *> &id_roots,
3331 const int library_indirect_level)
3332{
3333 ID *id_iter;
3334
3335 /* Tag all IDs to be processed, which are real liboverrides part of a hierarchy, and not the
3336 * root of their hierarchy, as potentially isolated from their hierarchy root. */
3337 FOREACH_MAIN_ID_BEGIN (bmain, id_iter) {
3338 if (lib_override_library_main_resync_id_skip_check(id_iter, library_indirect_level)) {
3339 continue;
3340 }
3341
3342 if (!ELEM(id_iter->override_library->hierarchy_root, id_iter, nullptr)) {
3345 }
3346 }
3348
3349 /* Finalize all IDs needing tagging for resync, and tag partial resync roots. Will also clear the
3350 * 'isolated' tag from all processed IDs. */
3351 FOREACH_MAIN_ID_BEGIN (bmain, id_iter) {
3352 if (lib_override_library_main_resync_id_skip_check(id_iter, library_indirect_level)) {
3353 continue;
3354 }
3355
3356 /* Only process hierarchy root IDs here. */
3357 if (id_iter->override_library->hierarchy_root != id_iter) {
3358 continue;
3359 }
3360
3362 bmain, id_iter, nullptr, library_indirect_level, false);
3363 }
3365
3366#ifndef NDEBUG
3367 /* Validation loop to ensure all entries have been processed as expected by
3368 * `lib_override_resync_tagging_finalize_recurse`, in above loop. */
3369 FOREACH_MAIN_ID_BEGIN (bmain, id_iter) {
3370 if (lib_override_library_main_resync_id_skip_check(id_iter, library_indirect_level)) {
3371 continue;
3372 }
3373 if ((id_iter->tag & ID_TAG_LIBOVERRIDE_NEED_RESYNC) == 0) {
3374 continue;
3375 }
3376
3377 MainIDRelationsEntry *entry = bmain->relations->relations_from_pointers->lookup(id_iter);
3378 BLI_assert(entry != nullptr);
3380
3381 if ((entry->tags & MAINIDRELATIONS_ENTRY_TAGS_DOIT) == 0) {
3382 continue;
3383 }
3384
3386 }
3388#endif
3389
3391
3392 /* Process above cleared all IDs actually still in relation with their root from the tag.
3393 *
3394 * The only exception being IDs only in relation with their root through a 'reversed' from
3395 * pointer (typical case: armature object is the hierarchy root, its child mesh object is only
3396 * related to it through its own 'parent' pointer, the armature one has no 'to' relationships to
3397 * its deformed mesh object).
3398 *
3399 * Remaining ones are in a limbo, typically they could have been removed or moved around in the
3400 * hierarchy (e.g. an object moved into another sub-collection). Tag them as needing resync,
3401 * actual resyncing code will handle them as needed. */
3402 FOREACH_MAIN_ID_BEGIN (bmain, id_iter) {
3403 if (lib_override_library_main_resync_id_skip_check(id_iter, library_indirect_level)) {
3404 continue;
3405 }
3406
3407 if (!ELEM(id_iter->override_library->hierarchy_root, id_iter, nullptr) &&
3408 (id_iter->override_library->runtime->tag &
3410 {
3411 /* Check and clear 'isolated' tags from cases like child objects of a hierarchy root object.
3412 * Sigh. */
3414 bmain, id_iter, library_indirect_level))
3415 {
3420 "ID %s (%p) detected as only related to its hierarchy root by 'reversed' "
3421 "relationship(s) (e.g. object parenting), tagging it as needing "
3422 "resync",
3423 id_iter->name,
3424 id_iter->lib);
3425 }
3426 else {
3427 CLOG_DEBUG(
3428 &LOG_RESYNC,
3429 "ID %s (%p) detected as 'isolated' from its hierarchy root, tagging it as needing "
3430 "resync",
3431 id_iter->name,
3432 id_iter->lib);
3433 }
3435 }
3436 }
3438
3440
3441 /* If no tagged-for-resync ancestor was found, but the iterated ID is tagged for resync, then it
3442 * is a root of a resync sub-tree. Find the root of the whole override hierarchy and add the
3443 * iterated ID as one of its resync sub-tree roots. */
3444 FOREACH_MAIN_ID_BEGIN (bmain, id_iter) {
3445 if (lib_override_library_main_resync_id_skip_check(id_iter, library_indirect_level)) {
3446 continue;
3447 }
3448 if ((id_iter->tag & ID_TAG_LIBOVERRIDE_NEED_RESYNC) == 0) {
3449 continue;
3450 }
3451
3452 MainIDRelationsEntry *entry = bmain->relations->relations_from_pointers->lookup(id_iter);
3453 BLI_assert(entry != nullptr);
3454
3455 if ((entry->tags & MAINIDRELATIONS_ENTRY_TAGS_DOIT) == 0) {
3456 continue;
3457 }
3458
3459 ID *hierarchy_root = id_iter->override_library->hierarchy_root;
3460 BLI_assert(hierarchy_root->lib == id_iter->lib);
3461
3462 if (id_iter != hierarchy_root) {
3464 "Found root ID '%s' for partial resync root ID '%s'",
3465 hierarchy_root->name,
3466 id_iter->name);
3467
3468 BLI_assert(hierarchy_root->override_library != nullptr);
3469
3472 }
3473
3474 LinkNodePair *id_resync_roots = id_roots.lookup_or_add_cb(hierarchy_root, []() {
3475 return MEM_new_zeroed<LinkNodePair>("lib_override_resync_tagging_finalize");
3476 });
3477 BLI_linklist_append(id_resync_roots, id_iter);
3478 }
3480
3482 bmain,
3486 false);
3487}
3488
3489/* Ensure resync of all overrides at one level of indirect usage.
3490 *
3491 * We need to handle each level independently, since an override at level n may be affected by
3492 * other overrides from level n + 1 etc. (i.e. from linked overrides it may use).
3493 */
3495 Main *bmain,
3496 const Map<Library *, Library *> *new_to_old_libraries_map,
3497 Scene *scene,
3498 ViewLayer *view_layer,
3499 Collection *override_resync_residual_storage,
3500 const int library_indirect_level,
3501 BlendFileReadReport *reports)
3502{
3503 const bool do_reports_recursive_resync_timing = (library_indirect_level != 0);
3504 const double init_time = do_reports_recursive_resync_timing ? BLI_time_now_seconds() : 0.0;
3505
3506 BKE_main_relations_create(bmain, 0);
3507 BKE_main_id_tag_all(bmain, ID_TAG_DOIT, false);
3508
3509 /* NOTE: in code below, the order in which `FOREACH_MAIN_ID_BEGIN` processes ID types ensures
3510 * that we always process 'higher-level' overrides first (i.e. scenes, then collections, then
3511 * objects, then other types). */
3512
3513 /* Detect all linked data that would need to be overridden if we had to create an override from
3514 * those used by current existing overrides. */
3516 data.bmain = bmain;
3517 data.scene = scene;
3518 data.tag = ID_TAG_DOIT;
3519 data.missing_tag = ID_TAG_MISSING;
3520 data.is_override = false;
3521 data.is_resync = true;
3523 ID *id;
3524 FOREACH_MAIN_ID_BEGIN (bmain, id) {
3525 if (lib_override_library_main_resync_id_skip_check(id, library_indirect_level)) {
3526 continue;
3527 }
3528
3529 if (id->tag & (ID_TAG_DOIT | ID_TAG_MISSING)) {
3530 /* We already processed that ID as part of another ID's hierarchy. */
3531 continue;
3532 }
3533
3534 data.root_set(id->override_library->reference);
3539 }
3541 data.clear();
3542
3544
3545 /* Now check existing overrides, those needing resync will be the one either already tagged as
3546 * such, or the one using linked data that is now tagged as needing override. */
3547 FOREACH_MAIN_ID_BEGIN (bmain, id) {
3548 if (lib_override_library_main_resync_id_skip_check(id, library_indirect_level)) {
3549 continue;
3550 }
3551
3553 CLOG_DEBUG(
3554 &LOG_RESYNC, "ID %s (%p) was already tagged as needing resync", id->name, id->lib);
3556 override_library_runtime_ensure(id->override_library)->tag |=
3558 }
3559 continue;
3560 }
3561
3562 MainIDRelationsEntry *entry = bmain->relations->relations_from_pointers->lookup(id);
3563 BLI_assert(entry != nullptr);
3564
3565 for (MainIDRelationsEntryItem *entry_item = entry->to_ids; entry_item != nullptr;
3566 entry_item = entry_item->next)
3567 {
3569 continue;
3570 }
3571 ID *id_to = entry_item->id_pointer.to;
3572
3573 /* Case where this ID pointer was to a linked ID, that now needs to be overridden. */
3574 if (ID_IS_LINKED(id_to) && (id_to->lib != id->lib) && (id_to->tag & ID_TAG_DOIT) != 0) {
3576 "ID %s (%p) now tagged as needing resync because they use linked %s (%p) that "
3577 "now needs to be overridden",
3578 id->name,
3579 id->lib,
3580 id_to->name,
3581 id_to->lib);
3583 break;
3584 }
3585 }
3586 }
3588
3589 /* Handling hierarchy relations for final tagging needs to happen after all IDs in a given
3590 * hierarchy have been tagged for resync in previous loop above. Otherwise, some resync roots may
3591 * be missing. */
3592 lib_override_resync_tagging_finalize(bmain, id_roots, library_indirect_level);
3593
3594#ifndef NDEBUG
3595 /* Check for validity/integrity of the computed set of root IDs, and their sub-branches defined
3596 * by their resync root IDs. */
3597 {
3599 for (const auto &item : id_roots.items()) {
3600 ID *id_root = item.key;
3601 LinkNodePair *id_resync_roots = item.value;
3603 "Checking validity of computed TODO data for root '%s'... \n",
3604 id_root->name);
3605
3606 if (id_root->tag & ID_TAG_LIBOVERRIDE_NEED_RESYNC) {
3607 LinkNode *id_resync_root_iter = id_resync_roots->list;
3608 ID *id_resync_root = static_cast<ID *>(id_resync_root_iter->link);
3609
3610 if (id_resync_roots->list != id_resync_roots->last_node || id_resync_root != id_root) {
3612 "Hierarchy root ID is tagged for resync, yet it is not the only partial "
3613 "resync roots, this should not happen."
3614 "\n\tRoot ID: %s"
3615 "\n\tFirst Resync root ID: %s"
3616 "\n\tLast Resync root ID: %s",
3617 id_root->name,
3618 static_cast<ID *>(id_resync_roots->list->link)->name,
3619 static_cast<ID *>(id_resync_roots->last_node->link)->name);
3620 }
3621 }
3622 for (LinkNode *id_resync_root_iter = id_resync_roots->list; id_resync_root_iter != nullptr;
3623 id_resync_root_iter = id_resync_root_iter->next)
3624 {
3625 ID *id_resync_root = static_cast<ID *>(id_resync_root_iter->link);
3626 BLI_assert(id_resync_root == id_root || !id_roots.contains(id_resync_root));
3627 if (id_resync_root == id_root) {
3628 if (id_resync_root_iter != id_resync_roots->list ||
3629 id_resync_root_iter != id_resync_roots->last_node)
3630 {
3632 "Resync root ID is same as root ID of the override hierarchy, yet other "
3633 "resync root IDs are also defined, this should not happen at this point."
3634 "\n\tRoot ID: %s"
3635 "\n\tFirst Resync root ID: %s"
3636 "\n\tLast Resync root ID: %s",
3637 id_root->name,
3638 static_cast<ID *>(id_resync_roots->list->link)->name,
3639 static_cast<ID *>(id_resync_roots->last_node->link)->name);
3640 }
3641 }
3642 }
3643 }
3644 }
3645#endif
3646
3648 BKE_main_id_tag_all(bmain, ID_TAG_DOIT, false);
3649
3650 ListBaseT<ID> no_main_ids_list = {nullptr};
3651
3652 for (const auto &item : id_roots.items()) {
3653 ID *id_root = item.key;
3654 Library *library = id_root->lib;
3655 LinkNodePair *id_resync_roots = item.value;
3656
3657 if (ID_IS_LINKED(id_root)) {
3659 }
3660
3662 "Resyncing all dependencies under root %s (%p), first one being '%s'...",
3663 id_root->name,
3664 reinterpret_cast<void *>(library),
3665 reinterpret_cast<ID *>(id_resync_roots->list->link)->name);
3666 const bool success = lib_override_library_resync(bmain,
3667 new_to_old_libraries_map,
3668 scene,
3669 view_layer,
3670 id_root,
3671 id_resync_roots->list,
3672 &no_main_ids_list,
3673 override_resync_residual_storage,
3674 false,
3675 false,
3676 reports);
3677 CLOG_DEBUG(&LOG_RESYNC, "\tSuccess: %d", success);
3678 if (success) {
3679 reports->count.resynced_lib_overrides++;
3680 if (library_indirect_level > 0 && reports->do_resynced_lib_overrides_libraries_list &&
3682 {
3685 }
3686 }
3687
3688 BLI_linklist_free(id_resync_roots->list, nullptr);
3689 }
3690
3691 for (ID &id_iter : no_main_ids_list.items_mutable()) {
3692 BKE_id_free(bmain, &id_iter);
3693 }
3694 no_main_ids_list.clear_no_delete();
3695
3696 /* Just in case, should not be needed in theory, since #lib_override_library_resync should have
3697 * already cleared them all. */
3698 BKE_main_id_tag_all(bmain, ID_TAG_DOIT, false);
3699
3700 /* Check there are no left-over IDs needing resync from the current (or higher) level of indirect
3701 * library level. */
3702 bool process_lib_level_again = false;
3703
3704 FOREACH_MAIN_ID_BEGIN (bmain, id) {
3705 if (lib_override_library_main_resync_id_skip_check(id, library_indirect_level)) {
3706 continue;
3707 }
3708
3709 const bool need_resync = (id->tag & ID_TAG_LIBOVERRIDE_NEED_RESYNC) != 0;
3710 const bool need_reseync_original = (id->override_library->runtime != nullptr &&
3711 (id->override_library->runtime->tag &
3714 const bool is_isolated_from_root = (id->override_library->runtime != nullptr &&
3715 (id->override_library->runtime->tag &
3718
3719 if (need_resync && is_isolated_from_root) {
3721 CLOG_WARN(
3722 &LOG_RESYNC,
3723 "Deleting unused ID override %s from library level %d, still found as needing "
3724 "resync, and being isolated from its hierarchy root. This can happen when its "
3725 "otherwise unchanged linked reference was moved around in the library file (e.g. if "
3726 "an object was moved into another sub-collection of the same hierarchy).",
3727 id->name,
3728 ID_IS_LINKED(id) ? id->lib->runtime->temp_index : 0);
3729 id->tag |= ID_TAG_DOIT;
3730 }
3731 else {
3732 CLOG_WARN(
3733 &LOG_RESYNC,
3734 "Keeping user-edited ID override %s from library level %d still found as "
3735 "needing resync, and being isolated from its hierarchy root. This can happen when its "
3736 "otherwise unchanged linked reference was moved around in the library file (e.g. if "
3737 "an object was moved into another sub-collection of the same hierarchy).",
3738 id->name,
3739 ID_IS_LINKED(id) ? id->lib->runtime->temp_index : 0);
3741 id->override_library->runtime->tag &= ~IDOverrideLibraryTag::TAG_RESYNC_ISOLATED_FROM_ROOT;
3742 }
3743 }
3744 else if (need_resync) {
3745 if (need_reseync_original) {
3747 "ID override %s from library level %d still found as needing resync after "
3748 "tackling library level %d. Since it was originally tagged as such by "
3749 "RNA/liboverride apply code, this whole level of library needs to be processed "
3750 "another time.",
3751 id->name,
3752 ID_IS_LINKED(id) ? id->lib->runtime->temp_index : 0,
3753 library_indirect_level);
3754 process_lib_level_again = true;
3755 /* Cleanup tag for now, will be re-set by next iteration of this function. */
3756 id->override_library->runtime->tag &= ~IDOverrideLibraryTag::TAG_NEED_RESYNC_ORIGINAL;
3757 }
3758 else {
3759 /* If it was only tagged for resync as part of resync process itself, it means it was
3760 * originally inside of a resync hierarchy, but not in the matching reference hierarchy
3761 * anymore. So it did not actually need to be resynced, simply clear the tag. */
3763 "ID override %s from library level %d still found as needing resync after "
3764 "tackling library level %d. However, it was not tagged as such by "
3765 "RNA/liboverride apply code, so ignoring it",
3766 id->name,
3767 ID_IS_LINKED(id) ? id->lib->runtime->temp_index : 0,
3768 library_indirect_level);
3770 }
3771 }
3772 else if (need_reseync_original) {
3773 /* Just cleanup of temporary tag, the ID has been resynced successfully. */
3774 id->override_library->runtime->tag &= ~IDOverrideLibraryTag::TAG_NEED_RESYNC_ORIGINAL;
3775 }
3776 else if (is_isolated_from_root) {
3777 CLOG_ERROR(
3778 &LOG_RESYNC,
3779 "ID override %s from library level %d still tagged as isolated from its hierarchy root, "
3780 "it should have been either properly resynced or removed at that point.",
3781 id->name,
3782 ID_IS_LINKED(id) ? id->lib->runtime->temp_index : 0);
3783 id->override_library->runtime->tag &= ~IDOverrideLibraryTag::TAG_RESYNC_ISOLATED_FROM_ROOT;
3784 }
3785 }
3787
3788 /* Delete 'isolated from root' remaining IDs tagged in above check loop.
3789 *
3790 * Prevent any expansive update process, as this is a in-progress cleanup,
3791 * #lib_override_cleanup_after_resync will ensure that this is called properly at the end of the
3792 * whole resync process. */
3794 bmain,
3795 {.prevent_liboverride_hierarchy_root_ensure = true, .prevent_invariants_update = true});
3796 BKE_main_id_tag_all(bmain, ID_TAG_DOIT, false);
3797
3798 for (LinkNodePair *pair : id_roots.values()) {
3799 MEM_delete(pair);
3800 }
3801
3802 /* In some fairly rare (and degenerate) cases, some root ID from other liboverrides may have been
3803 * freed, and therefore set to nullptr. Attempt to fix this as best as possible. */
3804 /* WARNING: Cannot use directly #BKE_lib_override_library_main_hierarchy_root_ensure here, as it
3805 * processes the whole Main content - only the IDs matching current resync scope should be
3806 * checked here. */
3807 {
3808 BKE_main_relations_create(bmain, 0);
3809 Set<ID *> processed_ids;
3810
3811 FOREACH_MAIN_ID_BEGIN (bmain, id) {
3812 if (lib_override_library_main_resync_id_skip_check(id, library_indirect_level)) {
3813 processed_ids.add(id);
3814 continue;
3815 }
3816
3817 lib_override_library_main_hierarchy_id_root_ensure(bmain, id, processed_ids, {}, nullptr);
3818 }
3820
3822 }
3823
3824 if (do_reports_recursive_resync_timing) {
3825 reports->duration.lib_overrides_recursive_resync += BLI_time_now_seconds() - init_time;
3826 }
3827
3828 return process_lib_level_again;
3829}
3830
3839
3841{
3842 if (cb_data->cb_flag & IDWALK_CB_LOOPBACK) {
3843 return IDWALK_RET_NOP;
3844 }
3846 cb_data->user_data);
3847 ID *id_owner = cb_data->owner_id;
3848 ID *id = *cb_data->id_pointer;
3849 if (!id) {
3850 return IDWALK_RET_NOP;
3851 }
3852 if (!ID_IS_LINKED(id)) {
3853 if (ID_IS_LINKED(id_owner)) {
3855 "Linked id '%s' from '%s' uses local ID '%s')",
3856 id_owner->name,
3857 id_owner->lib->filepath,
3858 id->name);
3859 }
3860 return IDWALK_RET_NOP;
3861 }
3862
3863 /* Archive libraries, used to store packed data, should not be processed here, as conceptually
3864 * they are the same thing as the source/real library when it comes to dependency. And they can
3865 * easily lead to fake cyclic dependencies, as packed IDs that depend on each other may end up in
3866 * different archived libraries.
3867 *
3868 * Bottom line being, only consider 'real' libraries for dependencies here, the archive ones only
3869 * add noise and artifacts, and do not need to be processed. */
3870 auto get_real_library = [](ID *id) -> Library * {
3871 if (!ID_IS_LINKED(id)) {
3872 return nullptr;
3873 }
3874 Library *id_lib_valid = id->lib;
3875 if (id_lib_valid->flag & LIBRARY_FLAG_IS_ARCHIVE) {
3877 BLI_assert(id_lib_valid->archive_parent_library);
3878 id_lib_valid = id_lib_valid->archive_parent_library;
3879 }
3880 return id_lib_valid;
3881 };
3882
3883 Library *id_lib = get_real_library(id);
3884 BLI_assert(id_lib);
3885 Library *id_owner_lib = get_real_library(id_owner);
3886 if (id_lib == id_owner_lib) {
3887 return IDWALK_RET_NOP;
3888 }
3889
3890 const int owner_library_indirect_level = id_owner_lib ? id_owner_lib->runtime->temp_index : 0;
3891 auto store_depth_dependencies_info =
3892 [&data, id_owner, id, owner_library_indirect_level]() -> void {
3893 if (owner_library_indirect_level >= id->lib->runtime->temp_index) {
3894 const int next_temp_index = owner_library_indirect_level + 1;
3895 Vector<std::pair<ID *, ID *>> &user_ids = data.dependency_trace_data.lookup_or_add_default(
3896 id->lib);
3897 if (user_ids.size() > next_temp_index) {
3898 return;
3899 }
3900 user_ids.resize(next_temp_index + 1, {nullptr, nullptr});
3901 user_ids[next_temp_index] = {id_owner, id};
3902 }
3903 };
3904 if (owner_library_indirect_level > LibOverrideSortLibrariesData::MAX_DEPENDENCY_DEPTH) {
3905 data.has_depth_overflows = true;
3906 store_depth_dependencies_info();
3907 if (id->lib->runtime->temp_index > 0) {
3908 return IDWALK_RET_NOP;
3909 }
3910 }
3911 /* Keep track of (some of) the last dependencies leading to such high depth values. Typically 5
3912 * should be more than enough to identify/have and idea of the cause. */
3913 else if (owner_library_indirect_level > (LibOverrideSortLibrariesData::MAX_DEPENDENCY_DEPTH - 5))
3914 {
3915 store_depth_dependencies_info();
3916 }
3917
3918 if (owner_library_indirect_level >= id->lib->runtime->temp_index) {
3919 id->lib->runtime->temp_index = owner_library_indirect_level + 1;
3920 data.do_continue = true;
3921 }
3922
3923 return IDWALK_RET_NOP;
3924}
3925
3933{
3934 for (Library &library : bmain->libraries) {
3935 /* index 0 is reserved for local data. */
3936 library.runtime->temp_index = 1;
3937 }
3938 LibOverrideSortLibrariesData sort_libs_data = {};
3939 sort_libs_data.do_continue = true;
3940 while (sort_libs_data.do_continue) {
3941 sort_libs_data.do_continue = false;
3942 ID *id;
3943 FOREACH_MAIN_ID_BEGIN (bmain, id) {
3944 /* NOTE: In theory all non-liboverride IDs could be skipped here. This does not gives any
3945 * performances boost though, so for now keep it as is (i.e. also consider non-liboverride
3946 * relationships to establish libraries hierarchy). */
3948 bmain, id, lib_override_sort_libraries_func, &sort_libs_data, IDWALK_READONLY);
3949 }
3951 }
3952
3953 int library_indirect_level_max = 0;
3954 for (Library &library : bmain->libraries) {
3955 library_indirect_level_max = std::max(library.runtime->temp_index, library_indirect_level_max);
3956 if (sort_libs_data.has_depth_overflows &&
3957 sort_libs_data.dependency_trace_data.contains(&library))
3958 {
3959 Vector<std::pair<ID *, ID *>> &lib_user_ids = sort_libs_data.dependency_trace_data.lookup(
3960 &library);
3962 std::string deps_chain;
3963 int index = -1;
3964 for (auto [id_owner, id] : lib_user_ids) {
3965 index++;
3966 if (!(id && id_owner)) {
3967 continue;
3968 }
3969 deps_chain += fmt::format(
3970 "\tDepth level {: >3}: {: >32} | {: <32} ---> {: >32} | {}\n",
3971 index,
3972 id_owner->name,
3973 id_owner->lib ? BKE_id_name(id_owner->lib->id) : "<Local>",
3974 id->name,
3975 id->lib ? BKE_id_name(id->lib->id) : "<Local>");
3976 }
3978 "Levels of indirect usages of library '%s' is way too high, there are most "
3979 "likely dependency loops, skipping further building loops\n%s",
3980 library.filepath,
3981 deps_chain.c_str());
3982 }
3983 }
3984 }
3985 return library_indirect_level_max;
3986}
3987
3989 Main *bmain,
3990 const Map<Library *, Library *> *new_to_old_libraries_map,
3991 Scene *scene,
3992 ViewLayer *view_layer,
3993 BlendFileReadReport *reports)
3994{
3995 /* Active scene may be a linked one. cannot be used to host the 'leftover' collection. */
3996 if (ID_IS_LINKED(&scene->id)) {
3997 Scene *new_scene = nullptr;
3998 for (Scene &sce : bmain->scenes) {
3999 if (ID_IS_LINKED(&sce.id)) {
4000 continue;
4001 }
4002 new_scene = &sce;
4003 break;
4004 }
4005 if (new_scene) {
4006 if (view_layer) {
4007 view_layer = BKE_view_layer_find(new_scene, view_layer->name);
4008 }
4009 if (!view_layer) {
4010 view_layer = new_scene->view_layers.first();
4011 }
4012 if (view_layer) {
4014 "Provided scene '%s' is not local, using instead local scene '%s', view-layer "
4015 "'%s' as container for the library override leftover collections and objects",
4016 BKE_id_name(scene->id),
4017 BKE_id_name(new_scene->id),
4018 view_layer->name);
4019 scene = new_scene;
4020 }
4021 }
4022 }
4023 if (!view_layer || !scene) {
4025 "Provided scene '%s' is not usable as container for the library override leftover "
4026 "collections and objects, these may not be instantiated at all",
4027 BKE_id_name(scene->id));
4028 scene = nullptr;
4029 }
4030 /* We use a specific collection to gather/store all 'orphaned' override collections and objects
4031 * generated by re-sync-process. This avoids putting them in scene's master collection. */
4032#define OVERRIDE_RESYNC_RESIDUAL_STORAGE_NAME "OVERRIDE_RESYNC_LEFTOVERS"
4033 Collection *override_resync_residual_storage = static_cast<Collection *>(BLI_findstring(
4035 if (override_resync_residual_storage != nullptr &&
4036 ID_IS_LINKED(override_resync_residual_storage))
4037 {
4038 override_resync_residual_storage = nullptr;
4039 }
4040 if (override_resync_residual_storage == nullptr) {
4041 override_resync_residual_storage = BKE_collection_add(
4042 bmain, scene ? scene->master_collection : nullptr, OVERRIDE_RESYNC_RESIDUAL_STORAGE_NAME);
4043 /* Hide the collection from viewport and render. */
4044 override_resync_residual_storage->flag |= COLLECTION_HIDE_VIEWPORT | COLLECTION_HIDE_RENDER;
4045 }
4046 /* BKE_collection_add above could have tagged the view_layer out of sync. */
4047 const Object *old_active_object = nullptr;
4048 if (scene && view_layer) {
4049 BKE_view_layer_synced_ensure(*bmain, scene, view_layer);
4050 old_active_object = BKE_view_layer_active_object_get(view_layer);
4051 }
4052
4053 /* Necessary to improve performances, and prevent layers matching override sub-collections to be
4054 * lost when re-syncing the parent override collection.
4055 * Ref. #73411. */
4057
4058 int library_indirect_level = lib_override_libraries_index_define(bmain);
4059 while (library_indirect_level >= 0) {
4060 int level_reprocess_count = 0;
4061 /* Update overrides from each indirect level separately.
4062 *
4063 * About the looping here: It may happen that some sub-hierarchies of liboverride are moved
4064 * around (the hierarchy in reference data does not match anymore the existing one in
4065 * liboverride data). In some cases, these sub-hierarchies won't be resynced then. If some IDs
4066 * in these sub-hierarchies actually do need resync, then the whole process needs to be applied
4067 * again, until all cases are fully processed.
4068 *
4069 * In practice, even in very complex and 'dirty'/outdated production files, typically less than
4070 * ten reprocesses are enough to cover all cases (in the vast majority of cases, no reprocess
4071 * is needed at all). */
4073 bmain,
4074 new_to_old_libraries_map,
4075 scene,
4076 view_layer,
4077 override_resync_residual_storage,
4078 library_indirect_level,
4079 reports))
4080 {
4081 level_reprocess_count++;
4082 if (level_reprocess_count > 100) {
4083 CLOG_WARN(
4084 &LOG_RESYNC,
4085 "Need to reprocess resync for library level %d more than %d times, aborting. This is "
4086 "either caused by extremely complex liboverride hierarchies, or a bug",
4087 library_indirect_level,
4088 level_reprocess_count);
4089 break;
4090 }
4092 "Applying reprocess %d for resyncing at library level %d",
4093 level_reprocess_count,
4094 library_indirect_level);
4095 }
4096 library_indirect_level--;
4097 }
4098
4100
4101 /* Essentially ensures that potentially new overrides of new objects will be instantiated. */
4102 if (scene && view_layer) {
4104 scene,
4105 view_layer,
4106 nullptr,
4107 nullptr,
4108 nullptr,
4109 override_resync_residual_storage,
4110 old_active_object,
4111 true);
4112 }
4113
4114 if (BKE_collection_is_empty(override_resync_residual_storage)) {
4115 BKE_collection_delete(bmain, override_resync_residual_storage, true);
4116 }
4117
4118 for (Library &library : bmain->libraries) {
4119 if (library.runtime->tag & LIBRARY_TAG_RESYNC_REQUIRED) {
4121 "library '%s' contains some linked overrides that required recursive resync, "
4122 "consider updating it",
4123 library.filepath);
4124 }
4125 }
4126
4127 /* Cleanup global namemap, to avoid extra processing with regular ID name management. Better to
4128 * re-create the global namemap on demand. */
4130
4132
4134}
4135
4137{
4139
4140 /* Tag all library overrides in the chains of dependencies from the given root one. */
4141 BKE_main_relations_create(bmain, 0);
4143 data.bmain = bmain;
4144 data.scene = nullptr;
4145 data.tag = ID_TAG_DOIT;
4146 data.missing_tag = ID_TAG_MISSING;
4147 data.is_override = true;
4148 data.is_resync = false;
4149
4150 data.root_set(id_root);
4151 data.hierarchy_root_set(id_root->override_library->hierarchy_root);
4152
4155
4157 data.clear();
4158
4159 ID *id;
4160 FOREACH_MAIN_ID_BEGIN (bmain, id) {
4161 if (id->tag & ID_TAG_DOIT) {
4163 ID *id_override_reference = id->override_library->reference;
4164
4165 /* Remap the whole local IDs to use the linked data. */
4166 BKE_libblock_remap(bmain, id, id_override_reference, ID_REMAP_SKIP_INDIRECT_USAGE);
4167 }
4168 }
4169 }
4171
4172 /* Delete the override IDs. */
4173 BKE_id_multi_tagged_delete(bmain, {.prevent_liboverride_hierarchy_root_ensure = true});
4174
4175 /* Should not actually be needed here. */
4176 BKE_main_id_tag_all(bmain, ID_TAG_DOIT, false);
4177}
4178
4180{
4181 BLI_assert(!ID_IS_LINKED(&id));
4182
4184 if (animdata != nullptr) {
4185 for (NlaTrack &track : animdata->nla_tracks) {
4187 }
4188 }
4189
4190 switch (id.id_type()) {
4191 case ID_OB: {
4192 Object &ob = id_cast<Object &>(id);
4193
4194 for (ModifierData &md : ob.modifiers) {
4196 }
4197 for (bConstraint &constraint : ob.constraints) {
4199 }
4200 for (ShaderFxData &fx : ob.shader_fx) {
4202 }
4203 if (ob.pose) {
4204 for (bPoseChannel &pose_bone : ob.pose->chanbase) {
4205 for (bConstraint &constraint : pose_bone.constraints) {
4207 }
4208 }
4209 }
4210 break;
4211 }
4212 case ID_AR: {
4213 bArmature &arm = id_cast<bArmature &>(id);
4214 for (BoneCollection *bcoll : arm.collections_span()) {
4216 }
4217 break;
4218 }
4219 case ID_CA: {
4220 Camera &camera = id_cast<Camera &>(id);
4221
4222 for (CameraBGImage &bgpic : camera.bg_images) {
4224 }
4225 break;
4226 }
4227 default:
4228 break;
4229 }
4230}
4231
4233{
4235 /* We should never directly 'make local' virtual overrides (aka shape keys). */
4238 return;
4239 }
4240 /* Cannot use `ID_IS_OVERRIDE_LIBRARY` here, as we may call this function on some already
4241 * partially processed liboverrides (e.g. from the #PartialWriteContext code), where the linked
4242 * reference pointer has already been set to null. */
4243 if (id->override_library) {
4244 BKE_lib_override_library_free(&id->override_library, true);
4245 }
4246
4247 Key *shape_key = BKE_key_from_id(id);
4248 if (shape_key != nullptr) {
4250 }
4251
4252 bNodeTree *node_tree = bke::node_tree_from_id(id);
4253 if (node_tree != nullptr) {
4255 }
4256
4257 if (id->id_type() == ID_SCE) {
4258 Collection *master_collection = reinterpret_cast<Scene *>(id)->master_collection;
4259 if (master_collection != nullptr) {
4260 master_collection->id.flag &= ~ID_FLAG_EMBEDDED_DATA_LIB_OVERRIDE;
4261 }
4262 }
4263
4264 /* Need to mark all types of sub-data that can be mixed (from linked data/local to the override)
4265 * as local now. */
4266 if (!ID_IS_LINKED(id)) {
4268 }
4269
4270 /* In case a liboverride hierarchy root is 'made local', i.e. is not a liboverride anymore, all
4271 * hierarchy roots of all liboverrides need to be validated/re-generated again.
4272 * Only in case `bmain` is given, otherwise caller is responsible to do this. */
4273 if (bmain) {
4275 }
4276}
4277
4278/* We only build override Map on request. */
4281{
4283 if (!liboverride_runtime->rna_path_to_override_properties) [[unlikely]] {
4284 liboverride_runtime->rna_path_to_override_properties =
4285 std::make_optional<Map<StringRefNull, IDOverrideLibraryProperty *>>();
4286 for (IDOverrideLibraryProperty *op = liboverride->properties.first(); op != nullptr;
4287 op = op->next)
4288 {
4289 liboverride_runtime->rna_path_to_override_properties->add(op->rna_path, op);
4290 }
4291 }
4292
4293 return *liboverride_runtime->rna_path_to_override_properties;
4294}
4295
4304
4306 const char *rna_path,
4307 bool *r_created)
4308{
4310
4311 if (op == nullptr) {
4312 op = MEM_new<IDOverrideLibraryProperty>(__func__);
4314 BLI_addtail(&liboverride->properties, op);
4315
4318 liboverride_runtime.add(op->rna_path, op);
4319
4320 if (r_created) {
4321 *r_created = true;
4322 }
4323 }
4324 else if (r_created) {
4325 *r_created = false;
4326 }
4327
4328 return op;
4329}
4330
4332 const IDOverrideLibraryProperty *library_prop,
4333 PointerRNA *r_override_poin,
4334 PropertyRNA **r_override_prop,
4335 int *r_index)
4336{
4337 BLI_assert(RNA_struct_is_ID(idpoin->type) &&
4338 ID_IS_OVERRIDE_LIBRARY(static_cast<ID *>(idpoin->data)));
4340 idpoin, library_prop->rna_path, r_override_poin, r_override_prop, r_index);
4341}
4342
4345{
4346 op_dst->rna_path = BLI_strdup(op_src->rna_path);
4347 BLI_duplicatelist(&op_dst->operations, &op_src->operations);
4348
4349 for (IDOverrideLibraryPropertyOperation *opop_dst = op_dst->operations.first(),
4350 *opop_src = op_src->operations.first();
4351 opop_dst;
4352 opop_dst = opop_dst->next, opop_src = opop_src->next)
4353 {
4355 }
4356}
4357
4359{
4360 BLI_assert(op->rna_path != nullptr);
4361
4362 MEM_delete(op->rna_path);
4363
4366 }
4367 op->operations.free_no_destruct();
4368}
4369
4371 const char *old_rna_path,
4372 const char *new_rna_path)
4373{
4374 /* Find the override property by its old RNA path. */
4377 IDOverrideLibraryProperty *liboverride_property = liboverride_runtime.pop_default(old_rna_path,
4378 nullptr);
4379
4380 if (liboverride_property == nullptr) {
4381 return false;
4382 }
4383
4384 /* Switch over the RNA path. */
4385 MEM_SAFE_DELETE(liboverride_property->rna_path);
4386 liboverride_property->rna_path = BLI_strdup(new_rna_path);
4387
4388 /* Put property back into the lookup mapping, using the new RNA path. */
4389 liboverride_runtime.add(liboverride_property->rna_path, liboverride_property);
4390
4391 return true;
4392}
4393
4395 IDOverrideLibraryProperty *liboverride_property,
4396 const bool do_runtime_updates)
4397{
4398 if (do_runtime_updates && liboverride->runtime &&
4399 liboverride->runtime->rna_path_to_override_properties)
4400 {
4401 liboverride->runtime->rna_path_to_override_properties->remove(liboverride_property->rna_path);
4402 }
4403 lib_override_library_property_clear(liboverride_property);
4404 BLI_freelinkN(&liboverride->properties, liboverride_property);
4405}
4406
4408 const char *rna_path)
4409{
4410 /* Find the override property by its old RNA path. */
4413 IDOverrideLibraryProperty *liboverride_property = liboverride_runtime.pop_default(rna_path,
4414 nullptr);
4415
4416 if (liboverride_property == nullptr) {
4417 return false;
4418 }
4419
4420 /* The key (rna_path) was already popped out of the runtime mapping above. */
4421 lib_override_library_property_delete(liboverride, liboverride_property, false);
4422 return true;
4423}
4424
4430
4432 ListBaseT<IDOverrideLibraryPropertyOperation> *liboverride_operations,
4433 const char *subitem_main_name,
4434 const char *subitem_other_name,
4435 const std::optional<const ID *> &subitem_main_id,
4436 const std::optional<const ID *> &subitem_other_id,
4437 const size_t offesetof_opop_main_name,
4438 const size_t offesetof_opop_other_name,
4439 const size_t offesetof_opop_main_id,
4440 const size_t offesetof_opop_other_id)
4441{
4442 const bool do_ids(subitem_main_id);
4444 for (opop = static_cast<IDOverrideLibraryPropertyOperation *>(BLI_findstring_ptr(
4445 liboverride_operations, subitem_main_name, int(offesetof_opop_main_name)));
4446 opop;
4448 reinterpret_cast<Link *>(opop), subitem_main_name, int(offesetof_opop_main_name))))
4449 {
4450 const char *opop_other_name = *reinterpret_cast<const char **>(reinterpret_cast<char *>(opop) +
4451 offesetof_opop_other_name);
4452 const bool opop_use_id = (opop->flag & LIBOVERRIDE_OP_FLAG_IDPOINTER_ITEM_USE_ID) != 0;
4453
4454 if (do_ids && opop_use_id) {
4455 /* Skip if ID pointers are expected valid and they do not exactly match. */
4456 const ID *opop_main_id = *reinterpret_cast<const ID **>(reinterpret_cast<char *>(opop) +
4457 offesetof_opop_main_id);
4458 if (*subitem_main_id != opop_main_id) {
4459 continue;
4460 }
4461 const ID *opop_other_id = *reinterpret_cast<const ID **>(reinterpret_cast<char *>(opop) +
4462 offesetof_opop_other_id);
4463 if (*subitem_other_id != opop_other_id) {
4464 continue;
4465 }
4466 }
4467
4468 /* Only check other name if ID handling is matching between given search parameters and
4469 * current liboverride operation (i.e. if both have valid ID pointers, or both have none). */
4470 if ((do_ids && opop_use_id) || (!do_ids && !opop_use_id)) {
4471 if (!subitem_other_name && !opop_other_name) {
4472 return opop;
4473 }
4474 if (subitem_other_name && opop_other_name && STREQ(subitem_other_name, opop_other_name)) {
4475 return opop;
4476 }
4477 }
4478
4479 /* No exact match found, keep checking the rest of the list of operations. */
4480 }
4481
4482 return nullptr;
4483}
4484
4486 IDOverrideLibraryProperty *liboverride_property,
4487 const char *subitem_refname,
4488 const char *subitem_locname,
4489 const std::optional<const ID *> &subitem_refid,
4490 const std::optional<const ID *> &subitem_locid,
4491 const int subitem_refindex,
4492 const int subitem_locindex,
4493 const bool strict,
4494 bool *r_strict)
4495{
4496 BLI_assert(!subitem_refid == !subitem_locid);
4497
4499 const int subitem_defindex = -1;
4500
4501 if (r_strict) {
4502 *r_strict = true;
4503 }
4504
4505 if (subitem_locname != nullptr) {
4507 &liboverride_property->operations,
4508 subitem_locname,
4509 subitem_refname,
4510 subitem_locid,
4511 subitem_refid,
4512 offsetof(IDOverrideLibraryPropertyOperation, subitem_local_name),
4513 offsetof(IDOverrideLibraryPropertyOperation, subitem_reference_name),
4515 offsetof(IDOverrideLibraryPropertyOperation, subitem_reference_id));
4516
4517 if (opop != nullptr) {
4518 return opop;
4519 }
4520 }
4521
4522 if (subitem_refname != nullptr) {
4524 &liboverride_property->operations,
4525 subitem_refname,
4526 subitem_locname,
4527 subitem_refid,
4528 subitem_locid,
4529 offsetof(IDOverrideLibraryPropertyOperation, subitem_reference_name),
4530 offsetof(IDOverrideLibraryPropertyOperation, subitem_local_name),
4531 offsetof(IDOverrideLibraryPropertyOperation, subitem_reference_id),
4532 offsetof(IDOverrideLibraryPropertyOperation, subitem_local_id));
4533
4534 if (opop != nullptr) {
4535 return opop;
4536 }
4537 }
4538
4539 opop = static_cast<IDOverrideLibraryPropertyOperation *>(
4540 BLI_listbase_bytes_find(&liboverride_property->operations,
4541 &subitem_locindex,
4542 sizeof(subitem_locindex),
4543 offsetof(IDOverrideLibraryPropertyOperation, subitem_local_index)));
4544 if (opop) {
4545 return ELEM(subitem_refindex, -1, opop->subitem_reference_index) ? opop : nullptr;
4546 }
4547
4549 &liboverride_property->operations,
4550 &subitem_refindex,
4551 sizeof(subitem_refindex),
4552 offsetof(IDOverrideLibraryPropertyOperation, subitem_reference_index)));
4553 if (opop) {
4554 return ELEM(subitem_locindex, -1, opop->subitem_local_index) ? opop : nullptr;
4555 }
4556
4557 /* `index == -1` means all indices, that is a valid fallback in case we requested specific index.
4558 */
4559 if (!strict && (subitem_locindex != subitem_defindex)) {
4561 &liboverride_property->operations,
4562 &subitem_defindex,
4563 sizeof(subitem_defindex),
4564 offsetof(IDOverrideLibraryPropertyOperation, subitem_local_index)));
4565 if (opop) {
4566 if (r_strict) {
4567 *r_strict = false;
4568 }
4569 return opop;
4570 }
4571 }
4572
4573 return nullptr;
4574}
4575
4577 IDOverrideLibraryProperty *liboverride_property,
4578 const eID_OverrideLib_Op operation,
4579 const char *subitem_refname,
4580 const char *subitem_locname,
4581 const std::optional<ID *> &subitem_refid,
4582 const std::optional<ID *> &subitem_locid,
4583 const int subitem_refindex,
4584 const int subitem_locindex,
4585 const bool strict,
4586 bool *r_strict,
4587 bool *r_created)
4588{
4589 BLI_assert(!subitem_refid == !subitem_locid);
4590
4592 liboverride_property,
4593 subitem_refname,
4594 subitem_locname,
4595 subitem_refid,
4596 subitem_locid,
4597 subitem_refindex,
4598 subitem_locindex,
4599 strict,
4600 r_strict);
4601
4602 if (opop == nullptr) {
4603 opop = MEM_new<IDOverrideLibraryPropertyOperation>(__func__);
4604 opop->operation = operation;
4605 if (subitem_locname) {
4606 opop->subitem_local_name = BLI_strdup(subitem_locname);
4607 }
4608 if (subitem_refname) {
4609 opop->subitem_reference_name = BLI_strdup(subitem_refname);
4610 }
4611 opop->subitem_local_index = subitem_locindex;
4612 opop->subitem_reference_index = subitem_refindex;
4613
4614 if (subitem_refid) {
4615 opop->subitem_reference_id = *subitem_refid;
4616 opop->subitem_local_id = *subitem_locid;
4618 }
4619
4620 BLI_addtail(&liboverride_property->operations, opop);
4621
4622 if (r_created) {
4623 *r_created = true;
4624 }
4625 }
4626 else if (r_created) {
4627 *r_created = false;
4628 }
4629
4630 return opop;
4631}
4632
4635{
4636 if (opop_src->subitem_reference_name) {
4638 }
4639 if (opop_src->subitem_local_name) {
4640 opop_dst->subitem_local_name = BLI_strdup(opop_src->subitem_local_name);
4641 }
4642 if (opop_src->label) {
4643 opop_dst->label = BLI_strdup(opop_src->label);
4644 }
4645 if (opop_src->tooltip) {
4646 opop_dst->tooltip = BLI_strdup(opop_src->tooltip);
4647 }
4648}
4649
4651{
4652 if (opop->subitem_reference_name) {
4653 MEM_delete(opop->subitem_reference_name);
4654 }
4655 if (opop->subitem_local_name) {
4656 MEM_delete(opop->subitem_local_name);
4657 }
4658 if (opop->label) {
4659 MEM_delete(opop->label);
4660 }
4661 if (opop->tooltip) {
4662 MEM_delete(opop->tooltip);
4663 }
4664}
4665
4667 IDOverrideLibraryProperty *liboverride_property,
4668 IDOverrideLibraryPropertyOperation *liboverride_property_operation)
4669{
4670 lib_override_library_property_operation_clear(liboverride_property_operation);
4671 BLI_freelinkN(&liboverride_property->operations, liboverride_property_operation);
4672}
4673
4675 IDOverrideLibraryPropertyOperation &liboverride_property_operation,
4676 StringRefNull label,
4677 StringRefNull tooltip)
4678{
4679 MEM_SAFE_DELETE(liboverride_property_operation.label);
4680 liboverride_property_operation.label = BLI_strdup(label.c_str());
4681 MEM_SAFE_DELETE(liboverride_property_operation.tooltip);
4682 liboverride_property_operation.tooltip = BLI_strdup(tooltip.c_str());
4683}
4684
4685bool IDOverrideLibraryPropertyOperation::operator==(
4687{
4688 return (
4689 (this->operation == b.operation) && (this->flag == b.flag) &&
4690 (this->subitem_reference_id == b.subitem_reference_id) &&
4691 (this->subitem_local_id == b.subitem_local_id) &&
4692 (this->subitem_reference_index == b.subitem_reference_index) &&
4693 (this->subitem_local_index == b.subitem_local_index) &&
4694 ((!this->subitem_reference_name && !b.subitem_reference_name) ||
4695 (this->subitem_reference_name && b.subitem_reference_name &&
4697 ((!this->subitem_local_name && !b.subitem_local_name) ||
4698 (this->subitem_local_name && b.subitem_local_name &&
4700}
4701
4703 IDOverrideLibraryPropertyOperation *liboverride_property_operation,
4704 PointerRNA *ptr_dst,
4705 PointerRNA *ptr_src,
4706 PointerRNA *ptr_storage,
4707 PropertyRNA *prop_dst,
4708 PropertyRNA *prop_src,
4709 PropertyRNA *prop_storage)
4710{
4711 switch (liboverride_property_operation->operation) {
4713 return true;
4715 /* No way to validate these here, custom RNA liboverride callbacks have to take care of
4716 * validation. */
4717 return true;
4718 case LIBOVERRIDE_OP_ADD:
4723 if (!ptr_storage || !*ptr_storage || !prop_storage) {
4724 BLI_assert_msg(0, "Missing data to apply differential override operation.");
4725 return false;
4726 }
4733 if ((!ptr_dst || !*ptr_dst || !prop_dst) || (!ptr_src || !*ptr_src || !prop_src)) {
4734 BLI_assert_msg(0, "Missing data to apply override operation.");
4735 return false;
4736 }
4737 }
4738
4739 return true;
4740}
4741
4742static bool override_library_is_valid(const ID &id,
4744 ReportList *reports)
4745{
4746 if (liboverride.reference == nullptr) {
4747 /* This (probably) used to be a template ID, could be linked or local, not an override. */
4748 BKE_reportf(reports,
4750 "Library override templates have been removed: removing all override data from "
4751 "the data-block '%s'",
4752 id.name);
4753 return false;
4754 }
4755 if (liboverride.reference == &id) {
4756 /* Very serious data corruption, cannot do much about it besides removing the liboverride data.
4757 */
4758 BKE_reportf(reports,
4759 RPT_ERROR,
4760 "Data corruption: data-block '%s' is using itself as library override reference, "
4761 "removing all override data",
4762 id.name);
4763 return false;
4764 }
4765 if (!ID_IS_LINKED(liboverride.reference)) {
4766 /* Very serious data corruption, cannot do much about it besides removing the liboverride data.
4767 */
4768 BKE_reportf(reports,
4769 RPT_ERROR,
4770 "Data corruption: data-block '%s' is using another local data-block ('%s') as "
4771 "library override reference, removing all override data",
4772 id.name,
4773 liboverride.reference->name);
4774 return false;
4775 }
4776 return true;
4777}
4778
4782 ReportList *reports)
4783{
4784 for (IDOverrideLibraryProperty &op : liboverride.properties.items_mutable()) {
4785 if (!op.rna_path) {
4787 reports,
4788 RPT_ERROR,
4789 "Data corruption: data-block `%s` has a Library Override property with no RNA path",
4790 id.name);
4791 /* Simpler to allocate a dummy string here, than fix all 'normal' clearing/deletion code that
4792 * does expect a non-null RNA path. */
4793 op.rna_path = BLI_strdup("");
4795 }
4796 }
4797}
4798
4800{
4801 /* Do NOT use `ID_IS_OVERRIDE_LIBRARY` here, since this code also needs to fix broken cases (like
4802 * null reference pointer), which would be skipped by that macro. */
4803 if (id->override_library == nullptr && !ID_IS_OVERRIDE_LIBRARY_VIRTUAL(id)) {
4804 return;
4805 }
4806
4807 ID *liboverride_id = id;
4808 IDOverrideLibrary *liboverride = id->override_library;
4810 liboverride = BKE_lib_override_library_get(bmain, id, nullptr, &liboverride_id);
4811 if (!liboverride || !override_library_is_valid(*liboverride_id, *liboverride, reports)) {
4812 /* Happens in case the given ID is a liboverride-embedded one (actual embedded ID like
4813 * NodeTree or master collection, or shape-keys), used by a totally not-liboverride owner ID.
4814 * Just clear the relevant ID flag.
4815 */
4817 return;
4818 }
4819 }
4821
4822 /* NOTE: In code deleting liboverride data below, #BKE_lib_override_library_make_local is used
4823 * instead of directly calling #BKE_lib_override_library_free, because the former also handles
4824 * properly 'liboverride embedded' IDs, like root node-trees, or shape-keys. */
4825 if (!override_library_is_valid(*liboverride_id, *liboverride, reports)) {
4826 BKE_lib_override_library_make_local(nullptr, liboverride_id);
4827 return;
4828 }
4829
4830 override_library_properties_validate(*liboverride_id, *liboverride, reports);
4831}
4832
4834{
4835 ID *id;
4836
4837 FOREACH_MAIN_ID_BEGIN (bmain, id) {
4838 BKE_lib_override_library_validate(bmain, id, reports);
4839 }
4841}
4842
4844{
4846
4847 ID *reference = local->override_library->reference;
4848
4849 BLI_assert(reference);
4850 BLI_assert(local->id_type() == reference->id_type());
4851
4852 if (local->id_type() == ID_OB) {
4853 /* Our beloved pose's bone cross-data pointers. Usually, depsgraph evaluation would
4854 * ensure this is valid, but in some situations (like hidden collections etc.) this won't
4855 * be the case, so we need to take care of this ourselves. */
4856 Object *ob_local = reinterpret_cast<Object *>(local);
4857 if (ob_local->type == OB_ARMATURE) {
4858 Object *ob_reference = reinterpret_cast<Object *>(local->override_library->reference);
4859 BLI_assert(ob_local->data != nullptr);
4860 BLI_assert(ob_reference->data != nullptr);
4861 BKE_pose_ensure(bmain, ob_local, id_cast<bArmature *>(ob_local->data), true);
4862 BKE_pose_ensure(bmain, ob_reference, id_cast<bArmature *>(ob_reference->data), true);
4863 }
4864 }
4865
4866 /* Note that reference is assumed always valid, caller has to ensure that itself. */
4867
4868 PointerRNA rnaptr_local = RNA_id_pointer_create(local);
4869 PointerRNA rnaptr_reference = RNA_id_pointer_create(reference);
4870
4872 bmain,
4873 &rnaptr_local,
4874 &rnaptr_reference,
4875 nullptr,
4876 0,
4877 local->override_library,
4879 nullptr))
4880 {
4882 return false;
4883 }
4884
4885 return true;
4886}
4887
4889{
4891
4892 ID *reference = local->override_library->reference;
4893
4894 BLI_assert(reference);
4895 BLI_assert(local->id_type() == reference->id_type());
4896
4897 if (reference->override_library && (reference->tag & ID_TAG_LIBOVERRIDE_REFOK) == 0) {
4898 if (!BKE_lib_override_library_status_check_reference(bmain, reference)) {
4899 /* If reference is also an override of another data-block, and its status is not OK,
4900 * then this override is not OK either.
4901 * Note that this should only happen when reloading libraries. */
4903 return false;
4904 }
4905 }
4906
4907 if (local->id_type() == ID_OB) {
4908 /* Our beloved pose's bone cross-data pointers. Usually, depsgraph evaluation would
4909 * ensure this is valid, but in some situations (like hidden collections etc.) this won't
4910 * be the case, so we need to take care of this ourselves. */
4911 Object *ob_local = reinterpret_cast<Object *>(local);
4912 if (ob_local->type == OB_ARMATURE) {
4913 Object *ob_reference = reinterpret_cast<Object *>(local->override_library->reference);
4914 BLI_assert(ob_local->data != nullptr);
4915 BLI_assert(ob_reference->data != nullptr);
4916 BKE_pose_ensure(bmain, ob_local, id_cast<bArmature *>(ob_local->data), true);
4917 BKE_pose_ensure(bmain, ob_reference, id_cast<bArmature *>(ob_reference->data), true);
4918 }
4919 }
4920
4921 PointerRNA rnaptr_local = RNA_id_pointer_create(local);
4922 PointerRNA rnaptr_reference = RNA_id_pointer_create(reference);
4923
4924 if (!RNA_struct_override_matches(bmain,
4925 &rnaptr_local,
4926 &rnaptr_reference,
4927 nullptr,
4928 0,
4929 local->override_library,
4931 nullptr))
4932 {
4934 return false;
4935 }
4936
4937 return true;
4938}
4939
4941 ID *local,
4942 const eRNAOverrideMatch liboverride_match_flags,
4943 eRNAOverrideMatchResult *r_report_flags)
4944{
4945 BLI_assert(!ID_IS_LINKED(local));
4947
4948 /* Do not attempt to generate overriding rules from an empty place-holder generated by link
4949 * code when it cannot find the actual library/ID. Much better to keep the local data-block as
4950 * is in the file in that case, until broken lib is fixed. */
4951 if (ID_MISSING(local->override_library->reference)) {
4952 return;
4953 }
4954
4955 if (local->id_type() == ID_OB) {
4956 /* Our beloved pose's bone cross-data pointers. Usually, depsgraph evaluation would
4957 * ensure this is valid, but in some situations (like hidden collections etc.) this won't
4958 * be the case, so we need to take care of this ourselves. */
4959 Object *ob_local = reinterpret_cast<Object *>(local);
4960 if (ob_local->type == OB_ARMATURE) {
4961 Object *ob_reference = reinterpret_cast<Object *>(local->override_library->reference);
4962 BLI_assert(ob_local->data != nullptr);
4963 BLI_assert(ob_reference->data != nullptr);
4964 BKE_pose_ensure(bmain, ob_local, id_cast<bArmature *>(ob_local->data), true);
4965 BKE_pose_ensure(bmain, ob_reference, id_cast<bArmature *>(ob_reference->data), true);
4966 }
4967 }
4968
4969 PointerRNA rnaptr_local = RNA_id_pointer_create(local);
4970 PointerRNA rnaptr_reference = RNA_id_pointer_create(local->override_library->reference);
4971
4974 &rnaptr_local,
4975 &rnaptr_reference,
4976 nullptr,
4977 0,
4978 local->override_library,
4979 liboverride_match_flags,
4980 &local_report_flags);
4981
4982 if (local_report_flags & RNA_OVERRIDE_MATCH_RESULT_RESTORED) {
4983 CLOG_DEBUG(&LOG, "We did restore some properties of %s from its reference", local->name);
4984 }
4985 if (local_report_flags & RNA_OVERRIDE_MATCH_RESULT_RESTORE_TAGGED) {
4986 CLOG_DEBUG(
4987 &LOG, "We did tag some properties of %s for restoration from its reference", local->name);
4988 }
4989 if (local_report_flags & RNA_OVERRIDE_MATCH_RESULT_CREATED) {
4990 CLOG_DEBUG(&LOG, "We did generate library override rules for %s", local->name);
4991 }
4992 else {
4993 CLOG_DEBUG(&LOG, "No new library override rules for %s", local->name);
4994 }
4995
4996 if (r_report_flags != nullptr) {
4997 *r_report_flags = (*r_report_flags | local_report_flags);
4998 }
4999}
5000void BKE_lib_override_library_operations_create(Main *bmain, ID *local, int *r_report_flags)
5001{
5003 bmain,
5004 local,
5006 reinterpret_cast<eRNAOverrideMatchResult *>(r_report_flags));
5007}
5008
5009void BKE_lib_override_library_operations_restore(Main *bmain, ID *local, int *r_report_flags)
5010{
5011 if (!ID_IS_OVERRIDE_LIBRARY_REAL(local) ||
5014 {
5015 return;
5016 }
5017
5018 PointerRNA rnaptr_src = RNA_id_pointer_create(local);
5021 bmain,
5022 &rnaptr_dst,
5023 &rnaptr_src,
5024 nullptr,
5025 local->override_library,
5028
5029 for (IDOverrideLibraryProperty &op : local->override_library->properties.items_mutable()) {
5030 if (op.tag & LIBOVERRIDE_PROP_TAG_NEEDS_RETORE) {
5031 for (IDOverrideLibraryPropertyOperation &opop : op.operations.items_mutable()) {
5032 if (opop.tag & LIBOVERRIDE_PROP_TAG_NEEDS_RETORE) {
5034 }
5035 }
5036 if (local->override_library->properties.is_empty()) {
5038 }
5039 else {
5041 }
5042 }
5043 }
5044 local->override_library->runtime->tag &= ~IDOverrideLibraryTag::TAG_NEEDS_RESTORE;
5045
5046 if (r_report_flags != nullptr) {
5047 *r_report_flags |= RNA_OVERRIDE_MATCH_RESULT_RESTORED;
5048 }
5049}
5050
5055
5056static void lib_override_library_operations_create_cb(TaskPool *__restrict pool, void *taskdata)
5057{
5058 LibOverrideOpCreateData *create_data = static_cast<LibOverrideOpCreateData *>(
5060 ID *id = static_cast<ID *>(taskdata);
5061
5064 create_data->bmain,
5065 id,
5067 &report_flags);
5068 atomic_fetch_and_or_uint32(reinterpret_cast<uint32_t *>(&create_data->report_flags),
5069 report_flags);
5070}
5071
5073 const bool force_auto,
5074 int *r_report_flags)
5075{
5076 ID *id;
5077
5078#ifdef DEBUG_OVERRIDE_TIMEIT
5080#endif
5081
5082 /* When force-auto is set, we also remove all unused existing override properties & operations.
5083 */
5084 if (force_auto) {
5086 }
5087
5088 /* Usual pose bones issue, need to be done outside of the threaded process or we may run into
5089 * concurrency issues here.
5090 * Note that calling #BKE_pose_ensure again in thread in
5091 * #BKE_lib_override_library_operations_create is not a problem then. */
5092 for (Object &ob : bmain->objects) {
5093 if (ob.type == OB_ARMATURE) {
5094 BLI_assert(ob.data != nullptr);
5095 BKE_pose_ensure(bmain, &ob, id_cast<bArmature *>(ob.data), true);
5096 }
5097 }
5098 /* Similar issue with view layers, some may not be up-to-date, and re-syncing them from a
5099 * multi-threaded process is utterly unsafe. Some RNA property access may cause this, see e.g.
5100 * #147565 and the `node_warnings` property of the Geometry Nodes. */
5101 const bool resync_success = BKE_main_view_layers_synced_ensure(bmain);
5102 /* Layer resync should never fail here.
5103 *
5104 * This call is fairly high-level and should never happen within a call-path which has already
5105 * forbidden resync (using #BKE_layer_collection_resync_forbid).
5106 *
5107 * Other unlikely reasons for failure (like very old blend-file data before versioning, where
5108 * scenes have no master collection yet) are also never expected to be met in this code.
5109 */
5110 BLI_assert_msg(resync_success,
5111 "Ensuring that all view-layers in Main are synced with their collections failed");
5112 UNUSED_VARS_NDEBUG(resync_success);
5114
5115 LibOverrideOpCreateData create_pool_data{};
5116 create_pool_data.bmain = bmain;
5118 TaskPool *task_pool = BLI_task_pool_create(&create_pool_data, TASK_PRIORITY_HIGH);
5119
5120 FOREACH_MAIN_ID_BEGIN (bmain, id) {
5121 if (ID_IS_LINKED(id) || !ID_IS_OVERRIDE_LIBRARY_REAL(id)) {
5122 continue;
5123 }
5124 /* Propagate potential embedded data tag to the owner ID (see also
5125 * #BKE_lib_override_id_tag_on_deg_tag_from_user). */
5126 if (Key *key = BKE_key_from_id(id)) {
5127 if (key->id.tag & ID_TAG_LIBOVERRIDE_AUTOREFRESH) {
5128 key->id.tag &= ~ID_TAG_LIBOVERRIDE_AUTOREFRESH;
5130 }
5131 }
5132 if (bNodeTree *ntree = bke::node_tree_from_id(id)) {
5133 if (ntree->id.tag & ID_TAG_LIBOVERRIDE_AUTOREFRESH) {
5134 ntree->id.tag &= ~ID_TAG_LIBOVERRIDE_AUTOREFRESH;
5136 }
5137 }
5138 if (id->id_type() == ID_SCE) {
5139 if (Collection *scene_collection = reinterpret_cast<Scene *>(id)->master_collection) {
5140 if (scene_collection->id.tag & ID_TAG_LIBOVERRIDE_AUTOREFRESH) {
5141 scene_collection->id.tag &= ~ID_TAG_LIBOVERRIDE_AUTOREFRESH;
5143 }
5144 }
5145 }
5146
5147 if (force_auto || (id->tag & ID_TAG_LIBOVERRIDE_AUTOREFRESH)) {
5148 /* Usual issue with pose, it's quiet rare but sometimes they may not be up to date when this
5149 * function is called. */
5150 if (id->id_type() == ID_OB) {
5151 Object *ob = reinterpret_cast<Object *>(id);
5152 if (ob->type == OB_ARMATURE) {
5153 BLI_assert(ob->data != nullptr);
5154 BKE_pose_ensure(bmain, ob, id_cast<bArmature *>(ob->data), true);
5155 }
5156 }
5157 /* Only check overrides if we do have the real reference data available, and not some empty
5158 * 'placeholder' for missing data (broken links). */
5159 if ((id->override_library->reference->tag & ID_TAG_MISSING) == 0) {
5161 task_pool, lib_override_library_operations_create_cb, id, false, nullptr);
5162 }
5163 else {
5165 id->override_library, LIBOVERRIDE_PROP_OP_TAG_UNUSED, false);
5166 }
5167 }
5168 else {
5169 /* Clear 'unused' tag for un-processed IDs, otherwise e.g. linked overrides will loose their
5170 * list of overridden properties. */
5172 id->override_library, LIBOVERRIDE_PROP_OP_TAG_UNUSED, false);
5173 }
5175 }
5177
5178 BLI_task_pool_work_and_wait(task_pool);
5179
5180 BLI_task_pool_free(task_pool);
5181
5183
5186 bmain, reinterpret_cast<int *>(&create_pool_data.report_flags));
5187 create_pool_data.report_flags = (create_pool_data.report_flags &
5189 }
5190
5191 if (r_report_flags != nullptr) {
5192 *r_report_flags |= create_pool_data.report_flags;
5193 }
5194
5195 if (force_auto) {
5197 }
5198
5199#ifdef DEBUG_OVERRIDE_TIMEIT
5201#endif
5202}
5203
5205{
5206 ID *id;
5207
5208 FOREACH_MAIN_ID_BEGIN (bmain, id) {
5209 if (!(!ID_IS_LINKED(id) && ID_IS_OVERRIDE_LIBRARY_REAL(id) && id->override_library->runtime &&
5210 (id->override_library->runtime->tag & IDOverrideLibraryTag::TAG_NEEDS_RESTORE) !=
5212 {
5213 continue;
5214 }
5215
5216 /* Only restore overrides if we do have the real reference data available, and not some empty
5217 * 'placeholder' for missing data (broken links). */
5218 if (id->override_library->reference->tag & ID_TAG_MISSING) {
5219 continue;
5220 }
5221
5222 BKE_lib_override_library_operations_restore(bmain, id, r_report_flags);
5223 }
5225}
5226
5228 ID *id_root,
5229 const bool do_reset_system_override)
5230{
5231 bool was_op_deleted = false;
5232
5233 if (do_reset_system_override) {
5235 }
5236
5237 for (IDOverrideLibraryProperty &op : id_root->override_library->properties.items_mutable()) {
5238 bool do_op_delete = true;
5239 const bool is_collection = op.rna_prop_type == PROP_COLLECTION;
5240 if (is_collection || op.rna_prop_type == PROP_POINTER) {
5241 PointerRNA ptr, ptr_lib;
5242 PropertyRNA *prop, *prop_lib;
5243
5244 PointerRNA ptr_root = RNA_pointer_create_discrete(id_root, RNA_ID, id_root);
5246 id_root->override_library->reference, RNA_ID, id_root->override_library->reference);
5247
5248 bool prop_exists = RNA_path_resolve_property(&ptr_root, op.rna_path, &ptr, &prop);
5249 if (prop_exists) {
5250 prop_exists = RNA_path_resolve_property(&ptr_root_lib, op.rna_path, &ptr_lib, &prop_lib);
5251
5252 if (prop_exists) {
5255 if (is_collection) {
5256 ptr.type = RNA_property_pointer_type(&ptr, prop);
5257 ptr_lib.type = RNA_property_pointer_type(&ptr_lib, prop_lib);
5258 }
5259 else {
5260 ptr = RNA_property_pointer_get(&ptr, prop);
5261 ptr_lib = RNA_property_pointer_get(&ptr_lib, prop_lib);
5262 }
5263 if (ptr.owner_id != nullptr && ptr_lib.owner_id != nullptr) {
5264 BLI_assert(ptr.type == ptr_lib.type);
5265 do_op_delete = !(RNA_struct_is_ID(ptr.type) &&
5266 ptr.owner_id->override_library != nullptr &&
5267 ptr.owner_id->override_library->reference == ptr_lib.owner_id);
5268 }
5269 }
5270 }
5271 }
5272
5273 if (do_op_delete) {
5275 was_op_deleted = true;
5276 }
5277 }
5278
5279 if (was_op_deleted) {
5282 id_root->override_library);
5283 liboverride_runtime->tag |= IDOverrideLibraryTag::TAG_NEEDS_RELOAD;
5284 }
5285
5286 return was_op_deleted;
5287}
5288
5290 ID *id_root,
5291 const bool do_reset_system_override)
5292{
5293 if (!ID_IS_OVERRIDE_LIBRARY_REAL(id_root)) {
5294 return;
5295 }
5296
5297 if (lib_override_library_id_reset_do(bmain, id_root, do_reset_system_override)) {
5298 if (id_root->override_library->runtime != nullptr &&
5301 {
5302 BKE_lib_override_library_update(bmain, id_root);
5303 id_root->override_library->runtime->tag &= ~IDOverrideLibraryTag::TAG_NEEDS_RELOAD;
5304 }
5305 }
5306}
5307
5309 ID *id_root,
5310 const bool do_reset_system_override)
5311{
5312 if (!ID_IS_OVERRIDE_LIBRARY_REAL(id_root)) {
5313 return;
5314 }
5315
5316 MainIDRelationsEntry **entry_vp = bmain->relations->relations_from_pointers->lookup_ptr(id_root);
5317 if (entry_vp == nullptr) {
5318 /* This ID is not used by nor using any other ID. */
5319 lib_override_library_id_reset_do(bmain, id_root, do_reset_system_override);
5320 return;
5321 }
5322
5323 MainIDRelationsEntry *entry = *entry_vp;
5325 /* This ID has already been processed. */
5326 return;
5327 }
5328
5329 lib_override_library_id_reset_do(bmain, id_root, do_reset_system_override);
5330
5331 /* This way we won't process again that ID, should we encounter it again through another
5332 * relationship hierarchy. */
5334
5335 for (MainIDRelationsEntryItem *to_id_entry = entry->to_ids; to_id_entry != nullptr;
5336 to_id_entry = to_id_entry->next)
5337 {
5339 continue;
5340 }
5341 /* We only consider IDs from the same library. */
5342 if (to_id_entry->id_pointer.to != nullptr) {
5343 ID *to_id = to_id_entry->id_pointer.to;
5344 if (to_id->override_library != nullptr) {
5345 lib_override_library_id_hierarchy_recursive_reset(bmain, to_id, do_reset_system_override);
5346 }
5347 }
5348 }
5349}
5350
5352 ID *id_root,
5353 const bool do_reset_system_override)
5354{
5355 BKE_main_relations_create(bmain, 0);
5356
5357 lib_override_library_id_hierarchy_recursive_reset(bmain, id_root, do_reset_system_override);
5358
5360
5361 ID *id;
5362 FOREACH_MAIN_ID_BEGIN (bmain, id) {
5363 if (!ID_IS_OVERRIDE_LIBRARY_REAL(id) || id->override_library->runtime == nullptr ||
5364 (id->override_library->runtime->tag & IDOverrideLibraryTag::TAG_NEEDS_RELOAD) ==
5366 {
5367 continue;
5368 }
5370 id->override_library->runtime->tag &= ~IDOverrideLibraryTag::TAG_NEEDS_RELOAD;
5371 }
5373}
5374
5376 const eID_OverrideLib_PropTag tag,
5377 const bool do_set)
5378{
5379 if (liboverride_property != nullptr) {
5380 if (do_set) {
5381 liboverride_property->tag |= tag;
5382 }
5383 else {
5384 liboverride_property->tag &= ~tag;
5385 }
5386
5387 for (IDOverrideLibraryPropertyOperation &opop : liboverride_property->operations) {
5388 if (do_set) {
5389 opop.tag |= tag;
5390 }
5391 else {
5392 opop.tag &= ~tag;
5393 }
5394 }
5395 }
5396}
5397
5399 const eID_OverrideLib_PropTag tag,
5400 const bool do_set)
5401{
5402 if (liboverride != nullptr) {
5403 for (IDOverrideLibraryProperty &op : liboverride->properties) {
5405 }
5406 }
5407}
5408
5410 const eID_OverrideLib_PropTag tag,
5411 const bool do_set)
5412{
5413 ID *id;
5414
5415 FOREACH_MAIN_ID_BEGIN (bmain, id) {
5416 if (ID_IS_OVERRIDE_LIBRARY(id)) {
5417 BKE_lib_override_library_properties_tag(id->override_library, tag, do_set);
5418 }
5419 }
5421}
5422
5424{
5425 if (ID_IS_OVERRIDE_LIBRARY_REAL(local)) {
5426 for (IDOverrideLibraryProperty &op : local->override_library->properties.items_mutable()) {
5427 if (op.tag & LIBOVERRIDE_PROP_OP_TAG_UNUSED) {
5429 }
5430 else {
5431 for (IDOverrideLibraryPropertyOperation &opop : op.operations.items_mutable()) {
5432 if (opop.tag & LIBOVERRIDE_PROP_OP_TAG_UNUSED) {
5434 }
5435 }
5436 if (op.operations.is_empty()) {
5438 }
5439 }
5440 }
5441 }
5442}
5443
5445{
5446 ID *id;
5447
5448 FOREACH_MAIN_ID_BEGIN (bmain, id) {
5449 if (ID_IS_OVERRIDE_LIBRARY(id)) {
5451 }
5452 }
5454}
5455
5456static void lib_override_id_swap(Main *bmain, ID *id_local, ID *id_temp)
5457{
5458 /* Ensure ViewLayers are in sync in case a Scene is being swapped, and prevent any further resync
5459 * during the swapping itself. */
5460 if (id_local->id_type() == ID_SCE) {
5461 BKE_scene_view_layers_synced_ensure(*bmain, reinterpret_cast<Scene *>(id_local));
5462 BKE_scene_view_layers_synced_ensure(*bmain, reinterpret_cast<Scene *>(id_temp));
5463 }
5465
5466 BKE_lib_id_swap(bmain, id_local, id_temp, true, 0);
5467 /* We need to keep these tags from temp ID into orig one.
5468 * ID swap does not swap most of ID data itself. */
5469 id_local->tag |= (id_temp->tag & ID_TAG_LIBOVERRIDE_NEED_RESYNC);
5470
5472}
5473
5475{
5476 if (!ID_IS_OVERRIDE_LIBRARY_REAL(local)) {
5477 return;
5478 }
5479
5480 /* Do not attempt to apply overriding rules over an empty place-holder generated by link code
5481 * when it cannot find the actual library/ID. Much better to keep the local data-block as loaded
5482 * from the file in that case, until broken lib is fixed. */
5483 if (ID_MISSING(local->override_library->reference)) {
5484 return;
5485 }
5486
5487 /* Recursively do 'ancestor' overrides first, if any. */
5490 {
5492 }
5493
5494 /* We want to avoid having to remap here, however creating up-to-date override is much simpler
5495 * if based on reference than on current override.
5496 * So we work on temp copy of reference, and 'swap' its content with local. */
5497
5498 /* XXX We need a way to get off-Main copies of IDs (similar to localized mats/texts/ etc.)!
5499 * However, this is whole bunch of code work in itself, so for now plain stupid ID copy
5500 * will do, as inefficient as it is. :/
5501 * Actually, maybe not! Since we are swapping with original ID's local content, we want to
5502 * keep user-count in correct state when freeing tmp_id
5503 * (and that user-counts of IDs used by 'new' local data also remain correct). */
5504 /* This would imply change in handling of user-count all over RNA
5505 * (and possibly all over Blender code).
5506 * Not impossible to do, but would rather see first if extra useless usual user handling
5507 * is actually a (performances) issue here. */
5508
5509 /* Note: do not add duplicated object to rigid body collections, as we swap it with its 'local'
5510 * liboverride orig version, which should already be in these RB collections if needed.
5511 * Otherwise, the temp id gets also added to these RB collections, which will then crash on
5512 * freeing it at the end of this function, since it is assumed that this temp id is not used by
5513 * anything. */
5514 ID *tmp_id = BKE_id_copy_ex(bmain,
5516 nullptr,
5519
5520 if (tmp_id == nullptr) {
5521 return;
5522 }
5523
5524 /* Remove the pair (idname, lib) of this temp id from the name map. */
5525 BKE_main_namemap_remove_id(*bmain, *tmp_id);
5526
5527 tmp_id->lib = local->lib;
5528
5529 /* This ID name is problematic, since it is an 'rna name property' it should not be editable or
5530 * different from reference linked ID. But local ID names need to be unique in a given type
5531 * list of Main, so we cannot always keep it identical, which is why we need this special
5532 * manual handling here. */
5533 STRNCPY(tmp_id->name, local->name);
5534
5535 /* Those ugly loop-back pointers again. Luckily we only need to deal with the shape keys here,
5536 * collections' parents are fully runtime and reconstructed later. */
5537 Key *local_key = BKE_key_from_id(local);
5538 Key *tmp_key = BKE_key_from_id(tmp_id);
5539 if (local_key != nullptr && tmp_key != nullptr) {
5540 tmp_key->id.flag |= (local_key->id.flag & ID_FLAG_EMBEDDED_DATA_LIB_OVERRIDE);
5541 BKE_main_namemap_remove_id(*bmain, tmp_key->id);
5542 tmp_key->id.lib = local_key->id.lib;
5543 STRNCPY(tmp_key->id.name, local_key->id.name);
5544 }
5545
5546 PointerRNA rnaptr_src = RNA_id_pointer_create(local);
5547 PointerRNA rnaptr_dst = RNA_id_pointer_create(tmp_id);
5548
5550 &rnaptr_dst,
5551 &rnaptr_src,
5552 nullptr,
5553 local->override_library,
5555
5557
5558 /* This also transfers all pointers (memory) owned by local to tmp_id, and vice-versa.
5559 * So when we'll free tmp_id, we'll actually free old, outdated data from local. */
5560 lib_override_id_swap(bmain, local, tmp_id);
5561
5562 if (local_key != nullptr && tmp_key != nullptr) {
5563 /* This is some kind of hard-coded 'always enforced override'. */
5564 lib_override_id_swap(bmain, &local_key->id, &tmp_key->id);
5565 tmp_key->id.flag |= (local_key->id.flag & ID_FLAG_EMBEDDED_DATA_LIB_OVERRIDE);
5566 /* The swap of local and tmp_id inverted those pointers, we need to redefine proper
5567 * relationships. */
5568 *BKE_key_from_id_p(local) = local_key;
5569 *BKE_key_from_id_p(tmp_id) = tmp_key;
5570 local_key->from = local;
5571 tmp_key->from = tmp_id;
5572 }
5573
5574 /* Again, horribly inefficient in our case, we need something off-Main
5575 * (aka more generic nolib copy/free stuff).
5576 * NOTE: Do not remove this tmp_id's name from the namemap here, since this name actually still
5577 * exists in `bmain`. */
5579
5580 if (local->id_type() == ID_AR) {
5581 /* Fun times again, thanks to bone pointers in pose data of objects. We keep same ID addresses,
5582 * but internal data has changed for sure, so we need to invalidate pose-bones caches. */
5583 for (Object &ob : bmain->objects) {
5584 if (ob.pose != nullptr && ob.data == local) {
5586 ob.pose->flag |= POSE_RECALC;
5587 /* We need to clear pose bone pointers immediately, some code may access those before pose
5588 * is actually recomputed, which can lead to segfault. */
5590 }
5591 }
5592 }
5593
5594 /* NLA Tweak Mode is, in a way, an "edit mode" for certain animation data. However, contrary to
5595 * mesh/armature edit modes, it doesn't use its own runtime data, but directly changes various
5596 * DNA pointers & flags. As these need to be all consistently set for the system to behave in a
5597 * well-defined manner, and the values can come from different files (library NLA tracks/strips
5598 * vs. override-added NLA tracks/strips), they need to be checked _after_ all overrides have been
5599 * applied. */
5601
5602 local->tag |= ID_TAG_LIBOVERRIDE_REFOK;
5603
5604 /* NOTE: Since we reload full content from linked ID here, potentially from edited local
5605 * override, we do not really have a way to know *what* is changed, so we need to rely on the
5606 * massive destruction weapon of `ID_RECALC_ALL` here. */
5607 DEG_id_tag_update_ex(bmain, local, ID_RECALC_ALL);
5608 /* For same reason as above, also assume that the relationships between IDs changed. */
5610}
5611
5613{
5614 ID *id;
5615
5616 /* This temporary swap of G_MAIN is rather ugly,
5617 * but necessary to avoid asserts checks in some RNA assignment functions,
5618 * since those always use G_MAIN when they need access to a Main database. */
5619 Main *orig_gmain = BKE_blender_globals_main_swap(bmain);
5620
5622
5623 FOREACH_MAIN_ID_BEGIN (bmain, id) {
5624 if (id->override_library != nullptr) {
5626 }
5627 }
5629
5631
5632 Main *tmp_gmain = BKE_blender_globals_main_swap(orig_gmain);
5633 BLI_assert(tmp_gmain == bmain);
5634 UNUSED_VARS_NDEBUG(tmp_gmain);
5635}
5636
5638{
5639 /* The only strong known case currently are objects used by override collections. */
5640 /* TODO: There are most likely other cases... This may need to be addressed in a better way at
5641 * some point. */
5642 if (id->id_type() != ID_OB) {
5643 return true;
5644 }
5645 Object *ob = reinterpret_cast<Object *>(id);
5646 for (Collection &collection : bmain->collections) {
5647 if (!ID_IS_OVERRIDE_LIBRARY(&collection)) {
5648 continue;
5649 }
5650 if (BKE_collection_has_object(&collection, ob)) {
5651 return false;
5652 }
5653 }
5654 return true;
5655}
5656
5658{
5659 switch (operation) {
5661 return "NoOp";
5663 return "Replace";
5664 case LIBOVERRIDE_OP_ADD:
5665 return "Add";
5667 return "Subtract";
5669 return "Multiply";
5671 return "Insert After";
5673 return "Insert Before";
5675 return "Custom";
5676 }
5678 return "Unknown";
5679}
5680
5682{
5683 const char *line_prefix = "";
5684 if (intro_txt != nullptr) {
5685 std::cout << intro_txt << "\n";
5686 line_prefix = "\t";
5687 }
5688
5689 for (IDOverrideLibraryProperty &op : liboverride->properties) {
5690 std::cout << line_prefix << op.rna_path << " [";
5692 std::cout << " UNUSED ";
5693 }
5694 std::cout << "]\n";
5695
5697 std::cout << line_prefix << line_prefix
5700 std::cout << " UNUSED ";
5701 }
5703 std::cout << " MATCH_REF ";
5704 }
5705 std::cout << "] ";
5706 if (opop.subitem_reference_name || opop.subitem_local_name) {
5707 std::cout << "(" << opop.subitem_reference_name << " <" << opop.subitem_reference_id
5708 << "> -> " << opop.subitem_local_name << " <" << opop.subitem_local_id << ">)";
5709 }
5710 else if (opop.subitem_reference_index >= 0 || opop.subitem_local_index >= 0) {
5711 std::cout << "(" << opop.subitem_reference_index << " -> " << opop.subitem_local_index
5712 << ")";
5713 }
5714 std::cout << "\n";
5715 }
5716 }
5717}
5718
5719} // namespace blender
Blender util stuff.
#define FOREACH_MAIN_ID_END
Definition BKE_main.hh:735
#define FOREACH_MAIN_LISTBASE_ID_END
Definition BKE_main.hh:703
#define FOREACH_MAIN_LISTBASE_ID_BEGIN(_lb, _id)
Definition BKE_main.hh:697
#define FOREACH_MAIN_LISTBASE_END
Definition BKE_main.hh:715
#define FOREACH_MAIN_LISTBASE_BEGIN(_bmain, _lb)
Definition BKE_main.hh:708
#define FOREACH_MAIN_ID_BEGIN(_bmain, _id)
Definition BKE_main.hh:729
#define ATTR_FALLTHROUGH
#define BLI_MEMARENA_STD_BUFSIZE
Platform independent time functions.
Utility defines for timing/benchmarks.
#define TIMEIT_START_AVERAGED(var)
#define TIMEIT_END_AVERAGED(var)
external readfile function prototypes.
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
#define CLOG_DEBUG(clg_ref,...)
Definition CLG_log.h:191
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
ID and Library types, which are fundamental for SDNA.
#define ID_IS_OVERRIDE_LIBRARY_VIRTUAL(_id)
Definition DNA_ID.h:733
#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_REAL_USERS(id)
Definition DNA_ID.h:679
#define ID_IS_OVERRIDE_LIBRARY(_id)
Definition DNA_ID.h:736
#define ID_MISSING(_id)
Definition DNA_ID.h:695
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)
#define USER_DEVELOPER_TOOL_TEST(userdef, member)
Read Blender memory allocation and freeing.
Provides wrapper around system-specific atomic primitives, and some extensions (faked-atomic operatio...
ATOMIC_INLINE uint32_t atomic_fetch_and_or_uint32(uint32_t *p, uint32_t x)
#define BLI_assert_unreachable()
#define UNUSED_VARS(...)
#define UNUSED_VARS_NDEBUG(...)
#define BLI_INLINE
#define ELEM(...)
#define BLI_assert_msg(a, msg)
#define STREQ(a, b)
void clear()
Definition BLI_map.hh:1038
ValueIterator values() const &
Definition BLI_map.hh:884
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_or_add_cb(const Key &key, const CreateValueF &create_value)
Definition BLI_map.hh:620
bool add_as(ForwardKey &&key, ForwardValue &&...value)
Definition BLI_map.hh:312
bool contains_as(const ForwardKey &key) const
Definition BLI_map.hh:357
bool contains(const Key &key) const
Definition BLI_map.hh:353
Value lookup_default_as(const ForwardKey &key, ForwardValue &&...default_value) const
Definition BLI_map.hh:575
Value pop_default(const Key &key, const Value &default_value)
Definition BLI_map.hh:439
ItemIterator items() const &
Definition BLI_map.hh:902
bool contains(const Key &key) const
Definition BLI_set.hh:310
bool add(const Key &key)
Definition BLI_set.hh:248
void clear()
Definition BLI_set.hh:570
bool remove(const Key &key)
Definition BLI_set.hh:404
constexpr const char * c_str() const
constexpr StringRefNull()
bool add(const Key &key)
int64_t size() const
int64_t size() const
void append(const T &value)
void resize(const int64_t new_size)
void add(ID *old_id, ID *new_id)
CCL_NAMESPACE_BEGIN struct Options options
#define offsetof(t, d)
void camera(KernelGlobals &kg, const ShadingData &sd, float3 &outview, float &outdepth, float &outdist)
#define OVERRIDE_RESYNC_RESIDUAL_STORAGE_NAME
#define HIERARCHY_BREAKING_ID_TYPES
bNodeTree * node_tree_from_id(ID *id)
Definition node.cc:5337
BMesh Iterators.
static bool lib_override_library_create_do(Main *bmain, Scene *scene, Library *owner_library, ID *id_root_reference, ID *id_hierarchy_root_reference, const bool do_fully_editable)
bool BKE_collection_delete(Main *bmain, Collection *collection, bool hierarchy)
eRNAOverrideMatchResult
@ RNA_OVERRIDE_MATCH_RESULT_RESTORED
@ RNA_OVERRIDE_MATCH_RESULT_CREATED
@ RNA_OVERRIDE_MATCH_RESULT_INIT
@ RNA_OVERRIDE_MATCH_RESULT_RESTORE_TAGGED
UserDef U
Definition blender.cc:58
static void lib_override_library_operations_create(Main *bmain, ID *local, const eRNAOverrideMatch liboverride_match_flags, eRNAOverrideMatchResult *r_report_flags)
void BKE_main_relations_free(Main *bmain)
Definition main.cc:760
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static void lib_override_library_property_copy(IDOverrideLibraryProperty *op_dst, IDOverrideLibraryProperty *op_src)
void BKE_lib_override_library_tag_set(IDOverrideLibrary &liboverride, IDOverrideLibraryTag tag, bool value)
PointerRNA RNA_property_pointer_get(PointerRNA *ptr, PropertyRNA *prop)
__int64 int64_t
Definition BKE_text.h:90
void BKE_lib_override_library_clear(IDOverrideLibrary *liboverride, bool do_id_user)
@ NLATRACK_OVERRIDELIBRARY_LOCAL
char * STRNCPY(char(&dst)[N], const char *src)
Object * BKE_view_layer_active_object_get(const ViewLayer *view_layer)
@ LIBRARY_TAG_RESYNC_REQUIRED
@ RPT_WARNING
Definition BKE_report.hh:40
static void lib_override_id_swap(Main *bmain, ID *id_local, ID *id_temp)
IDOverrideLibraryPropertyOperation * BKE_lib_override_library_property_operation_find(IDOverrideLibraryProperty *liboverride_property, const char *subitem_refname, const char *subitem_locname, const std::optional< const ID * > &subitem_refid, const std::optional< const ID * > &subitem_locid, int subitem_refindex, int subitem_locindex, bool strict, bool *r_strict)
void BKE_id_delete(Main *bmain, void *idv, const BKEIDDeleteOptions &options={}) ATTR_NONNULL()
void BKE_lib_query_unused_ids_tag(Main *bmain, int tag, LibQueryUnusedIDsData &parameters)
MemArena * BLI_memarena_new(size_t bufsize, const char *name) ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL ATTR_NONNULL(2) ATTR_MALLOC
bool BKE_lib_override_library_create(Main *bmain, Scene *scene, ViewLayer *view_layer, Library *owner_library, ID *id_root_reference, ID *id_hierarchy_root_reference, ID *id_instance_hint, ID **r_id_root_override, const bool do_fully_editable)
void BKE_libblock_relink_ex(Main *bmain, void *idv, void *old_idv, void *new_idv, int remap_flags) ATTR_NONNULL(2)
Definition lib_remap.cc:855
@ ID_REMAP_TYPE_CLEANUP
@ ID_REMAP_TYPE_REMAP
unsigned int uint
Definition BKE_text.h:55
static void lib_override_library_property_delete(IDOverrideLibrary *liboverride, IDOverrideLibraryProperty *liboverride_property, const bool do_runtime_updates)
bool BKE_lib_override_library_is_user_edited(const ID *id)
static int foreachid_is_hierarchy_leaf_fn(LibraryIDLinkCallbackData *cb_data)
static void lib_override_resync_tagging_finalize(Main *bmain, Map< ID *, LinkNodePair * > &id_roots, const int library_indirect_level)
bool BKE_lib_override_library_property_operation_operands_validate(IDOverrideLibraryPropertyOperation *liboverride_property_operation, PointerRNA *ptr_dst, PointerRNA *ptr_src, PointerRNA *ptr_storage, PropertyRNA *prop_dst, PropertyRNA *prop_src, PropertyRNA *prop_storage)
bool BKE_scene_view_layers_synced_ensure(const Main &bmain, const Scene *scene)
static void lib_override_library_property_operation_copy(IDOverrideLibraryPropertyOperation *opop_dst, IDOverrideLibraryPropertyOperation *opop_src)
static bool lib_override_resync_tagging_finalize_recursive_check_from(Main *bmain, ID *id, const int library_indirect_level)
StringRefNull BKE_lib_override_operation_as_string(const eID_OverrideLib_Op operation)
void id_us_plus(ID *id)
Definition lib_id.cc:433
void BLI_insertlinkreplace(ListBase *listbase, void *vreplacelink, void *vnewlink) ATTR_NONNULL(1
Definition listbase.cc:417
PointerRNA RNA_id_pointer_create(ID *id)
static void lib_override_library_id_hierarchy_recursive_reset(Main *bmain, ID *id_root, const bool do_reset_system_override)
static int lib_override_libraries_index_define(Main *bmain)
static void override_library_properties_validate(const ID &id, IDOverrideLibrary &liboverride, ReportList *reports)
void void void void void void BLI_duplicatelist(ListBase *dst, const ListBase *src) ATTR_NONNULL(1
Definition listbase.cc:834
void BKE_pose_ensure(Main *bmain, Object *ob, bArmature *arm, bool do_id_user)
void BKE_lib_override_library_property_operation_ui_info_set(IDOverrideLibraryPropertyOperation &liboverride_property_operation, StringRefNull label, StringRefNull tooltip)
@ TASK_PRIORITY_HIGH
Definition BLI_task_c.hh:61
bool BKE_idtype_idcode_is_linkable(short idcode)
Definition idtype.cc:307
eRNAOverrideApplyFlag
@ RNA_OVERRIDE_APPLY_FLAG_SKIP_RESYNC_CHECK
@ RNA_OVERRIDE_APPLY_FLAG_IGNORE_ID_POINTERS
@ RNA_OVERRIDE_APPLY_FLAG_RESTORE_ONLY
@ RNA_OVERRIDE_APPLY_FLAG_NOP
void void BKE_libblock_remap(Main *bmain, void *old_idv, void *new_idv, int remap_flags) ATTR_NONNULL(1
Definition lib_remap.cc:701
void BKE_lib_override_flag_subdata_local(ID &id)
eID_OverrideLib_PropTag
Definition DNA_ID.h:237
@ LIBOVERRIDE_PROP_TAG_NEEDS_RETORE
Definition DNA_ID.h:242
@ LIBOVERRIDE_PROP_OP_TAG_UNUSED
Definition DNA_ID.h:239
void BKE_main_namemap_destroy(UniqueName_Map **r_name_map) ATTR_NONNULL()
void BKE_id_free_ex(Main *bmain, void *idv, int flag_orig, bool use_flag_from_idtag)
void * BLI_findstring(const ListBase *listbase, const char *id, int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:615
void BKE_pose_clear_pointers(bPose *pose)
static bool lib_override_resync_id_lib_level_is_valid(ID *id, const int library_indirect_level, const bool do_strict_equal)
void lib_id_copy_ensure_local(Main *bmain, const ID *old_id, ID *new_id, const int flags)
Definition lib_id.cc:562
void BKE_lib_override_id_tag_on_deg_tag_from_user(ID *id)
static ID * lib_override_library_create_from(Main *bmain, Library *owner_library, ID *reference_id, const int lib_id_copy_flags)
static bool lib_override_linked_group_tag_collections_keep_tagged_check_recursive(LibOverrideGroupTagData *data, Collection *collection)
ID * BKE_lib_override_library_create_from_id(Main *bmain, ID *reference_id, bool do_tagged_remap)
@ ID_REMAP_SKIP_INDIRECT_USAGE
@ ID_REMAP_FORCE_USER_REFCOUNT
@ ID_REMAP_FORCE_NEVER_NULL_USAGE
@ ID_REMAP_SKIP_OVERRIDE_LIBRARY
void BKE_lib_override_library_operations_restore(Main *bmain, ID *local, int *r_report_flags)
static CLG_LogRef LOG_RESYNC
static ID * lib_override_library_resync_search_missing_ids_data(LibOverrideMissingIDsData &missing_ids, Map< ID *, ID * > &linkedref_to_old_override, ID *id_override)
@ RNA_OVERRIDE_COMPARE_IGNORE_NON_OVERRIDABLE
@ RNA_OVERRIDE_COMPARE_RESTORE
@ RNA_OVERRIDE_COMPARE_TAG_FOR_RESTORE
@ RNA_OVERRIDE_COMPARE_CREATE
@ RNA_OVERRIDE_COMPARE_IGNORE_OVERRIDDEN
@ LIB_ID_FREE_NO_NAMEMAP_REMOVE
@ LIB_ID_FREE_NO_UI_USER
bool BKE_lib_override_library_resync(Main *bmain, Scene *scene, ViewLayer *view_layer, ID *id_root, Collection *override_resync_residual_storage, bool do_hierarchy_enforce, BlendFileReadReport *reports)
static LibOverrideMissingIDsData_Key lib_override_library_resync_missing_id_key(ID *id)
const char * BKE_id_name(const ID &id)
bool BKE_lib_override_library_is_system_defined(const Main *bmain, const ID *id)
void id_fake_user_set(ID *id)
Definition lib_id.cc:471
eID_OverrideLib_Op
Definition DNA_ID.h:182
@ LIBOVERRIDE_OP_ADD
Definition DNA_ID.h:192
@ LIBOVERRIDE_OP_INSERT_AFTER
Definition DNA_ID.h:200
@ LIBOVERRIDE_OP_SUBTRACT
Definition DNA_ID.h:194
@ LIBOVERRIDE_OP_MULTIPLY
Definition DNA_ID.h:196
@ LIBOVERRIDE_OP_NOOP
Definition DNA_ID.h:185
@ LIBOVERRIDE_OP_CUSTOM
Definition DNA_ID.h:212
@ LIBOVERRIDE_OP_REPLACE
Definition DNA_ID.h:188
@ LIBOVERRIDE_OP_INSERT_BEFORE
Definition DNA_ID.h:202
bool BKE_lib_override_library_status_check_local(Main *bmain, ID *local)
void BKE_lib_override_library_main_tag(Main *bmain, eID_OverrideLib_PropTag tag, bool do_set)
@ LIBOVERRIDE_FLAG_NO_HIERARCHY
Definition DNA_ID.h:303
@ LIBOVERRIDE_FLAG_SYSTEM_DEFINED
Definition DNA_ID.h:308
@ LIB_ID_COPY_RIGID_BODY_NO_COLLECTION_HANDLING
@ LIB_ID_COPY_NO_LIB_OVERRIDE
@ LIB_ID_COPY_DEFAULT
@ LIB_ID_COPY_NO_LIB_OVERRIDE_LOCAL_DATA_FLAG
@ LIB_ID_CREATE_NO_MAIN
void RNA_struct_override_apply(Main *bmain, PointerRNA *id_ptr_dst, PointerRNA *id_ptr_src, PointerRNA *id_ptr_storage, IDOverrideLibrary *liboverride, const eRNAOverrideApplyFlag flag)
LibOverride_HierarchyRoot_ValidateOptions
@ ONLY_PROCESS_NULL_ROOT_POINTERS
@ REPORT_NULL_ROOT_POINTERS
void void BLI_linklist_prepend(LinkNode **listp, void *ptr) ATTR_NONNULL(1)
void BKE_main_namemap_remove_id(Main &bmain, ID &id)
bool BKE_collection_has_object(Collection *collection, const Object *ob)
void BLI_task_pool_work_and_wait(TaskPool *pool)
Definition task_pool.cc:455
eMainIDRelationsEntryTags
Definition BKE_main.hh:168
@ MAINIDRELATIONS_ENTRY_TAGS_PROCESSED_TO
Definition BKE_main.hh:176
@ MAINIDRELATIONS_ENTRY_TAGS_DOIT
Definition BKE_main.hh:170
@ MAINIDRELATIONS_ENTRY_TAGS_INPROGRESS
Definition BKE_main.hh:202
@ MAINIDRELATIONS_ENTRY_TAGS_PROCESSED_FROM
Definition BKE_main.hh:181
@ MAINIDRELATIONS_ENTRY_TAGS_PROCESSED
Definition BKE_main.hh:183
void BKE_layer_collection_resync_forbid(Main &bmain)
void * BLI_task_pool_user_data(TaskPool *pool)
Definition task_pool.cc:460
void BKE_lib_override_library_update(Main *bmain, ID *local)
Global G
Definition blender.cc:57
@ eShaderFxFlag_OverrideLibrary_Local
void id_sort_by_name(ListBaseT< ID > *lb, ID *id, ID *id_sorting_hint)
Definition lib_id.cc:1888
static void lib_override_remapper_overrides_add(id::IDRemapper &id_remapper, ID *reference_id, ID *local_id)
int BLI_linklist_index(const LinkNode *list, const void *ptr) ATTR_WARN_UNUSED_RESULT
bool BKE_main_collection_sync_remap(const Main *bmain)
PropertyType RNA_property_type(PropertyRNA *prop)
AnimData * BKE_animdata_from_id(const ID *id)
Definition anim_data.cc:93
static void lib_override_overrides_group_tag(LibOverrideGroupTagData *data)
void BKE_lib_override_library_validate(Main *bmain, ID *id, ReportList *reports)
static void lib_override_library_property_clear(IDOverrideLibraryProperty *op)
unsigned int uint32_t
Definition BKE_text.h:81
int BLI_findindex(const ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:593
Key * BKE_key_from_id(ID *id)
Definition key.cc:1291
@ G_LIBOVERRIDE_NO_AUTO_RESYNC
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
void BKE_lib_override_library_id_unused_cleanup(ID *local)
void DEG_id_tag_update_ex(Main *bmain, ID *id, unsigned int flags)
void * BLI_findstring_ptr(const ListBase *listbase, const char *id, int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:658
void BKE_lib_override_library_main_operations_restore(Main *bmain, int *r_report_flags)
static bool lib_override_hierarchy_dependencies_skip_check(ID *owner_id, ID *other_id, const bool check_override)
static Map< StringRefNull, IDOverrideLibraryProperty * > & override_library_rna_path_mapping_ensure(IDOverrideLibrary *liboverride)
StructRNA * RNA_property_pointer_type(PointerRNA *ptr, PropertyRNA *prop)
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
static void lib_override_group_tag_data_object_to_collection_init(LibOverrideGroupTagData *data)
void BKE_layer_collection_resync_allow(Main &bmain)
static void lib_override_cleanup_after_resync(Main *bmain)
@ PROP_POINTER
Definition RNA_types.hh:215
@ PROP_COLLECTION
Definition RNA_types.hh:216
void BKE_lib_override_library_delete(Main *bmain, ID *id_root)
static bool lib_override_library_resync(Main *bmain, const Map< Library *, Library * > *new_to_old_libraries_map, Scene *scene, ViewLayer *view_layer, ID *id_root, LinkNode *id_resync_roots, ListBaseT< ID > *no_main_ids_list, Collection *override_resync_residual_storage, const bool do_hierarchy_enforce, const bool do_post_process, BlendFileReadReport *reports)
@ CAM_BGIMG_FLAG_OVERRIDE_LIBRARY_LOCAL
void DEG_id_tag_update(ID *id, unsigned int flags)
void BKE_lib_override_library_main_hierarchy_root_ensure(Main *bmain, LibOverride_HierarchyRoot_ValidateOptions options={}, ReportList *reports=nullptr)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
BMesh const char void * data
Key ** BKE_key_from_id_p(ID *id)
Definition key.cc:1266
void BKE_lib_override_library_properties_tag(IDOverrideLibrary *liboverride, eID_OverrideLib_PropTag tag, bool do_set)
void BKE_collection_add_from_object(Main *bmain, Scene *scene, const Object *ob_src, Collection *collection_dst)
bool BKE_collection_is_in_scene(Collection *collection)
static bool lib_override_library_main_resync_id_skip_check(ID *id, const int library_indirect_level)
bool BKE_collection_is_empty(const Collection *collection)
void BKE_main_ensure_invariants(Main &bmain, std::optional< Span< ID * > > modified_ids=std::nullopt)
static void lib_override_library_create_post_process(Main *bmain, Scene *scene, ViewLayer *view_layer, const Library *owner_library, ID *id_root, ID *id_instance_hint, Collection *residual_storage, const Object *old_active_object, const bool is_resync)
BLI_INLINE IDOverrideLibraryRuntime * override_library_runtime_ensure(IDOverrideLibrary *liboverride)
void BKE_lib_override_library_operations_tag(IDOverrideLibraryProperty *liboverride_property, eID_OverrideLib_PropTag tag, bool do_set)
void * BLI_listbase_findafter_string_ptr(Link *link, const char *id, int offset)
Definition listbase.cc:693
bool BKE_main_view_layers_synced_ensure(const Main *bmain)
FCurve * BKE_animadata_fcurve_find_by_rna_path(AnimData *animdata, const char *rna_path, const int rna_index, bAction **r_action, bool *r_driven)
bool RNA_struct_override_matches(Main *bmain, PointerRNA *ptr_local, PointerRNA *ptr_reference, const char *root_path, const size_t root_path_len, IDOverrideLibrary *liboverride, const eRNAOverrideMatch flags, eRNAOverrideMatchResult *r_report_flags)
void BKE_lib_override_library_make_local(Main *bmain, ID *id)
void BLI_freelinkN(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:277
size_t BKE_id_multi_tagged_delete(Main *bmain, const BKEIDDeleteOptions &options={}) ATTR_NONNULL()
void BKE_collection_add_from_collection(Main *bmain, Scene *scene, Collection *collection_src, Collection *collection_dst)
static void lib_override_prefill_newid_from_existing_overrides(Main *bmain, ID *id_hierarchy_root)
static IDOverrideLibraryPropertyOperation * liboverride_opop_find_name_lib_iterative(ListBaseT< IDOverrideLibraryPropertyOperation > *liboverride_operations, const char *subitem_main_name, const char *subitem_other_name, const std::optional< const ID * > &subitem_main_id, const std::optional< const ID * > &subitem_other_id, const size_t offesetof_opop_main_name, const size_t offesetof_opop_other_name, const size_t offesetof_opop_main_id, const size_t offesetof_opop_other_id)
bool BKE_view_layer_synced_ensure(const Main &bmain, const Scene *scene, ViewLayer *view_layer)
void BKE_libblock_remap_multiple(Main *bmain, bke::id::IDRemapper &mappings, const int remap_flags)
Definition lib_remap.cc:710
void BKE_collection_object_add_from(Main *bmain, Scene *scene, Object *ob_src, Object *ob_dst)
void BKE_lib_override_library_copy(ID *dst_id, const ID *src_id, bool do_full_copy)
bool BKE_collection_object_add(Main *bmain, Collection *collection, Object *ob)
static void lib_override_library_remap(Main *bmain, const ID *id_root_reference, Vector< std::pair< ID *, ID * > > &references_and_new_overrides, Map< ID *, ID * > &linkedref_to_old_override)
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
void BKE_lib_override_library_property_operation_delete(IDOverrideLibraryProperty *liboverride_property, IDOverrideLibraryPropertyOperation *liboverride_property_operation)
bool BKE_lib_override_library_status_check_reference(Main *bmain, ID *local)
bool BKE_collection_has_collection(const Collection *parent, const Collection *collection)
void BKE_lib_override_library_id_reset(Main *bmain, ID *id_root, bool do_reset_system_override)
IDOverrideLibraryProperty * BKE_lib_override_library_property_find(IDOverrideLibrary *liboverride, const char *rna_path)
@ LIBRARY_FLAG_IS_ARCHIVE
static void lib_override_resync_tagging_finalize_recurse(Main *bmain, ID *id_root, ID *id_from, const int library_indirect_level, bool is_in_partial_resync_hierarchy)
static void lib_override_linked_group_tag(LibOverrideGroupTagData *data)
static ID * lib_override_root_find(Main *bmain, ID *id, const int curr_level, int *r_best_level)
static bool override_library_is_valid(const ID &id, const IDOverrideLibrary &liboverride, ReportList *reports)
void BKE_main_relations_create(Main *bmain, short flag)
Definition main.cc:723
static LibOverrideMissingIDsData lib_override_library_resync_build_missing_ids_data(Main *bmain, const bool is_relocate)
void BKE_id_free(Main *bmain, void *idv)
bool BKE_main_namemap_validate(Main &bmain)
bool BKE_lib_override_library_property_search_and_delete(IDOverrideLibrary *liboverride, const char *rna_path)
void BKE_main_id_tag_all(Main *mainvar, int tag, bool value)
Definition lib_id.cc:1314
IDOverrideLibrary * BKE_lib_override_library_init(ID *local_id, ID *reference_id)
void BKE_lib_override_library_main_operations_create(Main *bmain, bool force_auto, int *r_report_flags)
void BKE_main_relations_tag_set(Main *bmain, eMainIDRelationsEntryTags tag, bool value)
Definition main.cc:773
void BLI_task_pool_free(TaskPool *pool)
Definition task_pool.cc:441
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)
bool RNA_struct_is_ID(const StructRNA *type)
static bool lib_override_library_main_resync_on_library_indirect_level(Main *bmain, const Map< Library *, Library * > *new_to_old_libraries_map, Scene *scene, ViewLayer *view_layer, Collection *override_resync_residual_storage, const int library_indirect_level, BlendFileReadReport *reports)
TaskPool * BLI_task_pool_create(void *userdata, eTaskPriority priority)
Definition task_pool.cc:404
static int lib_override_sort_libraries_func(LibraryIDLinkCallbackData *cb_data)
void BKE_main_id_newptr_and_tag_clear(Main *bmain)
Definition lib_id.cc:2097
Set< Object * > * BKE_scene_objects_as_set(Scene *scene, Set< Object * > *objects_set)
ViewLayer * BKE_view_layer_find(const Scene *scene, const char *layer_name)
static void lib_override_library_property_operation_clear(IDOverrideLibraryPropertyOperation *opop)
void BKE_lib_id_swap(Main *bmain, ID *id_a, ID *id_b, const bool do_self_remap, const int self_remap_flags)
Definition lib_id.cc:1130
void BKE_lib_override_library_main_update(Main *bmain)
bool BKE_main_global_namemap_get_unique_name(Main &bmain, ID &id, char *r_name)
static void lib_override_library_main_hierarchy_id_root_ensure(Main *bmain, ID *id, Set< ID * > &processed_ids, LibOverride_HierarchyRoot_ValidateOptions options, ReportList *reports)
void BKE_lib_override_library_main_validate(Main *bmain, ReportList *reports)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
void void void void BLI_linklist_append(LinkNodePair *list_pair, void *ptr) ATTR_NONNULL(1)
const std::pair< std::string, Library * > LibOverrideMissingIDsData_Key
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
void BLI_linklist_free(LinkNode *list, LinkNodeFreeFP freefunc)
bool BKE_lib_override_library_is_hierarchy_leaf(Main *bmain, ID *id)
static bool lib_override_hierarchy_dependencies_recursive_tag(LibOverrideGroupTagData *data)
void BKE_lib_override_library_free(IDOverrideLibrary **liboverride, bool do_id_user)
@ IDWALK_RET_STOP_ITER
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC
static void lib_override_linked_group_tag_clear_boneshapes_objects(LibOverrideGroupTagData *data)
@ INDEX_ID_NULL
Definition DNA_ID.h:1359
void BLI_linklist_append_arena(LinkNodePair *list_pair, void *ptr, struct MemArena *ma) ATTR_NONNULL(1
void * BLI_listbase_bytes_find(const ListBase *listbase, const void *bytes, size_t bytes_size, int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
Definition listbase.cc:737
static bool lib_override_hierarchy_dependencies_relationship_skip_check(MainIDRelationsEntryItem *relation_id_entry)
bool BKE_lib_override_library_property_rna_path_change(IDOverrideLibrary *liboverride, const char *old_rna_path, const char *new_rna_path)
IDOverrideLibraryProperty * BKE_lib_override_library_property_get(IDOverrideLibrary *liboverride, const char *rna_path, bool *r_created)
void BKE_lib_override_debug_print(IDOverrideLibrary *liboverride, const char *intro_txt)
void BKE_lib_override_library_main_unused_cleanup(Main *bmain)
bool BKE_lib_override_rna_property_find(PointerRNA *idpoin, const IDOverrideLibraryProperty *library_prop, PointerRNA *r_override_poin, PropertyRNA **r_override_prop, int *r_index)
@ ID_RECALC_ALL
Definition DNA_ID.h:1190
@ ID_RECALC_SELECT
Definition DNA_ID.h:1101
@ ID_RECALC_TRANSFORM
Definition DNA_ID.h:1054
@ ID_RECALC_SYNC_TO_EVAL
Definition DNA_ID.h:1118
@ ID_RECALC_BASE_FLAGS
Definition DNA_ID.h:1104
static void lib_override_library_operations_create_cb(TaskPool *__restrict pool, void *taskdata)
static void lib_override_hierarchy_dependencies_recursive_tag_from(LibOverrideGroupTagData *data)
Base * BKE_view_layer_base_find(ViewLayer *view_layer, Object *ob)
@ ID_TAG_LIBOVERRIDE_NEED_RESYNC
Definition DNA_ID.h:909
@ ID_TAG_NO_MAIN
Definition DNA_ID.h:981
@ ID_TAG_MISSING
Definition DNA_ID.h:870
@ ID_TAG_INDIRECT
Definition DNA_ID.h:851
@ ID_TAG_NO_USER_REFCOUNT
Definition DNA_ID.h:1011
@ ID_TAG_LIBOVERRIDE_REFOK
Definition DNA_ID.h:894
@ ID_TAG_LIBOVERRIDE_AUTOREFRESH
Definition DNA_ID.h:900
@ ID_TAG_DOIT
Definition DNA_ID.h:1029
bool BKE_lib_override_library_create_from_tag(Main *bmain, Library *owner_library, const ID *id_root_reference, ID *id_hierarchy_root, const ID *id_hierarchy_root_reference, bool do_no_main, const bool do_fully_editable)
@ eModifierFlag_OverrideLibrary_Local
BLI_INLINE void lib_override_object_posemode_transfer(ID *id_dst, ID *id_src)
static void lib_override_linked_group_tag_recursive(LibOverrideGroupTagData *data)
static void lib_override_group_tag_data_object_to_collection_init_collection_process(LibOverrideGroupTagData *data, Collection *collection)
static bool lib_override_root_is_valid(Main *bmain, ID *id)
static CLG_LogRef LOG
Definition logging.cpp:12
const char * RNA_path_array_index_token_find(const char *rna_path, const PropertyRNA *array_prop)
Definition rna_path.cc:789
void BKE_libblock_management_main_add(Main *bmain, void *idv)
Definition lib_id.cc:1201
double BLI_time_now_seconds()
Definition time.cc:30
ID * BKE_id_owner_get(ID *id, const bool debug_relationship_assert=true)
Definition lib_id.cc:2770
void DEG_relations_tag_update(Main *bmain)
@ ID_FLAG_LIB_OVERRIDE_RESYNC_LEFTOVER
Definition DNA_ID.h:791
@ ID_FLAG_EMBEDDED_DATA_LIB_OVERRIDE
Definition DNA_ID.h:786
@ BONE_COLLECTION_OVERRIDE_LIBRARY_LOCAL
std::deque< ID * > LibOverrideMissingIDsData_Value
ID * BKE_id_copy_ex(Main *bmain, const ID *id, ID **new_id_p, int flag)
Definition lib_id.cc:851
@ IDWALK_CB_LOOPBACK
@ IDWALK_CB_OVERRIDE_LIBRARY_NOT_OVERRIDABLE
@ IDWALK_CB_OVERRIDE_LIBRARY_HIERARCHY_DEFAULT
void BKE_lib_override_library_operations_create(Main *bmain, ID *local, int *r_report_flags)
bool BKE_lib_override_library_property_is_animated(const ID *id, const IDOverrideLibraryProperty *liboverride_prop, const PropertyRNA *override_rna_prop, const int rnaprop_index)
static bool lib_override_library_id_reset_do(Main *bmain, ID *id_root, const bool do_reset_system_override)
bool BKE_main_collection_sync(const Main *bmain)
void BKE_lib_override_library_property_delete(IDOverrideLibrary *liboverride, IDOverrideLibraryProperty *liboverride_property)
std::map< LibOverrideMissingIDsData_Key, LibOverrideMissingIDsData_Value > LibOverrideMissingIDsData
static void lib_override_root_hierarchy_set(Main *bmain, ID *id_root, ID *id, ID *id_from, Set< ID * > &processed_ids)
void BKE_libblock_relink_multiple(Main *bmain, const Span< ID * > ids, eIDRemapType remap_type, bke::id::IDRemapper &id_remapper, int remap_flags)
Definition lib_remap.cc:793
static void lib_override_overrides_group_tag_recursive(LibOverrideGroupTagData *data)
bool RNA_path_resolve_property_full(const PointerRNA *ptr, const char *path, PointerRNA *r_ptr, PropertyRNA **r_prop, int *r_index)
Definition rna_path.cc:609
bool RNA_path_resolve_property(const PointerRNA *ptr, const char *path, PointerRNA *r_ptr, PropertyRNA **r_prop)
Definition rna_path.cc:578
void id_us_min(ID *id)
Definition lib_id.cc:441
void BLI_task_pool_push(TaskPool *pool, TaskRunFunction run, void *taskdata, bool free_taskdata, TaskFreeFunction freedata)
Definition task_pool.cc:446
void * BLI_memarena_calloc(MemArena *ma, size_t size) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC ATTR_ALLOC_SIZE(2)
void BKE_lib_override_library_id_hierarchy_reset(Main *bmain, ID *id_root, bool do_reset_system_override)
void BKE_library_foreach_ID_link(Main *bmain, ID *id, FunctionRef< LibraryIDLinkCallback > callback, void *user_data, LibraryForeachIDFlag flag)
Definition lib_query.cc:449
IDOverrideLibraryPropertyOperation * BKE_lib_override_library_property_operation_get(IDOverrideLibraryProperty *liboverride_property, eID_OverrideLib_Op operation, const char *subitem_refname, const char *subitem_locname, const std::optional< ID * > &subitem_refid, const std::optional< ID * > &subitem_locid, int subitem_refindex, int subitem_locindex, bool strict, bool *r_strict, bool *r_created)
@ LIBOVERRIDE_OP_FLAG_IDPOINTER_ITEM_USE_ID
Definition DNA_ID.h:232
@ LIBOVERRIDE_OP_FLAG_IDPOINTER_MATCH_REFERENCE
Definition DNA_ID.h:226
void * BKE_id_new(Main *bmain, short type, const char *name)
Definition lib_id.cc:1612
void BKE_animdata_liboverride_post_process(ID *id)
struct ID * BKE_id_copy_in_lib(Main *bmain, std::optional< Library * > owner_library, const ID *id, std::optional< const ID * > new_owner_id, ID **new_id_p, int flag)
Definition lib_id.cc:749
bool BKE_lib_override_library_id_is_user_deletable(Main *bmain, ID *id)
bool BKE_view_layer_has_collection(const ViewLayer *view_layer, const Collection *collection)
@ CONSTRAINT_OVERRIDE_LIBRARY_LOCAL
void BLI_memarena_free(MemArena *ma) ATTR_NONNULL(1)
IDOverrideLibrary * BKE_lib_override_library_get(Main *bmain, ID *id, ID *owner_id_hint, ID **r_owner_id)
Main * BKE_blender_globals_main_swap(Main *new_gmain)
Definition blender.cc:234
ListBaseT< ID > * which_libbase(Main *bmain, short type)
Definition main.cc:1073
Collection * BKE_collection_add(Main *bmain, Collection *collection_parent, const char *name_custom)
struct blender::BlendFileReadReport::@140003175311343306107257242277261221002217304000 count
LinkNode * resynced_lib_overrides_libraries
struct blender::BlendFileReadReport::@070156237370130334241363347365056360307125257043 duration
eCamera_BGImage_Flag flag
ListBaseT< CollectionChild > children
ListBaseT< CollectionObject > gobject
ListBaseT< IDOverrideLibraryPropertyOperation > operations
Definition DNA_ID.h:326
eID_OverrideLib_PropTag tag
Definition DNA_ID.h:331
struct IDOverrideLibraryProperty * next
Definition DNA_ID.h:314
std::optional< Map< StringRefNull, IDOverrideLibraryProperty * > > rna_path_to_override_properties
ListBaseT< IDOverrideLibraryProperty > properties
Definition DNA_ID.h:344
eID_OverrideLib_Flag flag
Definition DNA_ID.h:356
struct ID * hierarchy_root
Definition DNA_ID.h:352
IDOverrideLibraryRuntime * runtime
Definition DNA_ID.h:354
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
void hierarchy_root_set(ID *hierarchy_root_id)
Set< ID * > linked_ids_hierarchy_default_override
bool id_tag_set(ID *id, const bool is_missing)
Map< Object *, LinkNodePair * > linked_object_to_instantiating_collections
bool id_tag_clear(ID *id, const bool is_missing)
eRNAOverrideMatchResult report_flags
Map< Library *, Vector< std::pair< ID *, ID * > > > dependency_trace_data
FunctionRef< bool(ID *id)> filter_fn
std::array< int, INDEX_ID_MAX > num_total
std::array< int, INDEX_ID_MAX > num_local
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 LinkData * next
struct LinkNode * next
LibraryForeachIDCallbackFlag usage_flag
Definition BKE_main.hh:151
MainIDRelationsEntryItem * next
Definition BKE_main.hh:139
MainIDRelationsEntryItem * from_ids
Definition BKE_main.hh:156
MainIDRelationsEntryItem * to_ids
Definition BKE_main.hh:158
Map< const ID *, MainIDRelationsEntry * > * relations_from_pointers
Definition BKE_main.hh:211
ListBaseT< Collection > collections
Definition BKE_main.hh:406
MainIDRelations * relations
Definition BKE_main.hh:437
UniqueName_Map * name_map_global
Definition BKE_main.hh:449
ListBaseT< Library > libraries
Definition BKE_main.hh:387
ListBaseT< Object > objects
Definition BKE_main.hh:388
ListBaseT< Scene > scenes
Definition BKE_main.hh:386
eNlaTrack_Flag flag
eObjectMode restore_mode
struct Object * parent
ListBaseT< ModifierData > modifiers
ListBaseT< ShaderFxData > shader_fx
ListBaseT< bConstraint > constraints
struct bPose * pose
StructRNA * type
Definition RNA_types.hh:63
ListBaseT< ViewLayer > view_layers
struct Collection * master_collection
ListBaseT< struct bConstraint > constraints
ListBaseT< bPoseChannel > chanbase
bool is_leaf(uint i)