Blender V4.5
lib_remap.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include "CLG_log.h"
12
13#include "BLI_array.hh"
14#include "BLI_utildefines.h"
15
17#include "DNA_object_types.h"
18
19#include "BKE_armature.hh"
20#include "BKE_collection.hh"
21#include "BKE_curve.hh"
22#include "BKE_layer.hh"
23#include "BKE_lib_id.hh"
24#include "BKE_lib_query.hh"
25#include "BKE_lib_remap.hh"
26#include "BKE_main.hh"
27#include "BKE_material.hh"
28#include "BKE_mball.hh"
29#include "BKE_modifier.hh"
30#include "BKE_multires.hh"
31#include "BKE_node.hh"
33#include "BKE_object.hh"
34
35#include "DEG_depsgraph.hh"
37
38#include "lib_intern.hh" /* own include */
39
40using namespace blender::bke::id;
41
42static CLG_LogRef LOG = {"bke.lib_remap"};
43
45
50
52
58
59struct IDRemap {
61 Main *bmain; /* Only used to trigger depsgraph updates in the right bmain. */
62
64
67 int flag;
68};
69
70/* IDRemap->flag enums defined in BKE_lib.h */
71
72static void foreach_libblock_remap_callback_skip(const ID * /*id_owner*/,
73 ID **id_ptr,
74 const int cb_flag,
75 const bool is_indirect,
76 const bool is_reference,
77 const bool violates_never_null,
78 const bool /*is_obj*/,
79 const bool is_obj_editmode)
80{
81 ID *id = *id_ptr;
82 BLI_assert(id != nullptr);
83
84 if (is_indirect) {
85 id->runtime.remap.skipped_indirect++;
86 }
87 else if (violates_never_null || is_obj_editmode || is_reference) {
88 id->runtime.remap.skipped_direct++;
89 }
90 else {
92 }
93
94 if (cb_flag & IDWALK_CB_USER) {
95 id->runtime.remap.skipped_refcounted++;
96 }
97 else if (cb_flag & IDWALK_CB_USER_ONE) {
98 /* No need to count number of times this happens, just a flag is enough. */
99 id->runtime.remap.status |= ID_REMAP_IS_USER_ONE_SKIPPED;
100 }
101}
102
104 ID *id_self,
105 ID **id_ptr,
106 IDRemap *id_remap_data,
107 const IDRemapper &mappings,
108 const IDRemapperApplyOptions id_remapper_options,
109 const int cb_flag,
110 const bool is_indirect,
111 const bool violates_never_null)
112{
113 const bool skip_update_tagging = (id_remap_data->flag & ID_REMAP_SKIP_UPDATE_TAGGING) != 0;
114 const bool skip_user_refcount = (id_remap_data->flag & ID_REMAP_SKIP_USER_REFCOUNT) != 0;
115 const bool force_user_refcount = (id_remap_data->flag & ID_REMAP_FORCE_USER_REFCOUNT) != 0;
116 BLI_assert(!skip_user_refcount || !force_user_refcount);
117
118 ID *old_id = *id_ptr;
119 if (!violates_never_null) {
120 mappings.apply(id_ptr, id_remapper_options, id_self);
121 if (!skip_update_tagging) {
122 if (id_remap_data->bmain != nullptr) {
123 DEG_id_tag_update_ex(id_remap_data->bmain,
124 id_self,
126 if (id_self != id_owner) {
127 DEG_id_tag_update_ex(id_remap_data->bmain,
128 id_owner,
130 }
131 }
132 if (GS(id_self->name) == ID_NT) {
133 /* Make sure that the node tree is updated after a property in it changed. Ideally, we
134 * would know which nodes property was changed so that only this node is tagged. */
136 }
137 }
138 }
139 /* Get the new_id pointer. When the mapping is violating never null we should use a nullptr
140 * pointer otherwise the incorrect users are decreased and increased on the same instance. */
141 ID *new_id = violates_never_null ? nullptr : *id_ptr;
142
143 if (!is_indirect && new_id) {
145 }
146
147 if (skip_user_refcount) {
148 return;
149 }
150
151 if (cb_flag & IDWALK_CB_USER) {
152 /* NOTE: by default we don't user-count IDs which are not in the main database.
153 * This is because in certain conditions we can have data-blocks in
154 * the main which are referencing data-blocks outside of it.
155 * For example, BKE_mesh_new_from_object() called on an evaluated
156 * object will cause such situation.
157 */
158 if (force_user_refcount || (old_id->tag & ID_TAG_NO_MAIN) == 0) {
159 id_us_min(old_id);
160 }
161 if (new_id != nullptr && (force_user_refcount || (new_id->tag & ID_TAG_NO_MAIN) == 0)) {
162 /* Do not handle ID_TAG_INDIRECT/ID_TAG_EXTERN here. */
163 id_us_plus_no_lib(new_id);
164 }
165 }
166 else if (cb_flag & IDWALK_CB_USER_ONE) {
167 id_us_ensure_real(new_id);
168 /* We cannot affect old_id->us directly, ID_TAG_EXTRAUSER(_SET)
169 * are assumed to be set as needed, that extra user is processed in final handling. */
170 }
171}
172
174{
175 const LibraryForeachIDCallbackFlag cb_flag = cb_data->cb_flag;
176
177 /* NOTE: Support remapping of `IDWALK_CB_EMBEDDED_NON_OWNING` pointers, this is necessary in some
178 * complex low-level ID manipulation cases (e.g. in ID swapping, see #BKE_lib_id_swap & co).
179 */
180 if (cb_flag & IDWALK_CB_EMBEDDED) {
181 return IDWALK_RET_NOP;
182 }
183
184 ID *id_owner = cb_data->owner_id;
185 ID *id_self = cb_data->self_id;
186 ID **id_p = cb_data->id_pointer;
187 IDRemap *id_remap_data = static_cast<IDRemap *>(cb_data->user_data);
188
189 /* Those asserts ensure the general sanity of ID tags regarding 'embedded' ID data (root
190 * node-trees and co). */
191 BLI_assert(id_owner == id_remap_data->id_owner);
192 BLI_assert(id_self == id_owner || (id_self->flag & ID_FLAG_EMBEDDED_DATA) != 0);
193
194 /* Early exit when id pointer isn't set. */
195 if (*id_p == nullptr) {
196 return IDWALK_RET_NOP;
197 }
198
199 IDRemapper &id_remapper = id_remap_data->id_remapper;
201
202 /* Used to cleanup all IDs used by a specific one. */
203 if (id_remap_data->type == ID_REMAP_TYPE_CLEANUP) {
204 /* Clearing existing instance to reduce potential lookup times for IDs referencing many other
205 * IDs. This makes sure that there will only be a single rule in the id_remapper. */
206 id_remapper.clear();
207 id_remapper.add(*id_p, nullptr);
208 }
209
210 /* Better remap to nullptr than not remapping at all,
211 * then we can handle it as a regular remap-to-nullptr case. */
212 if (cb_flag & IDWALK_CB_NEVER_SELF) {
213 id_remapper_options |= ID_REMAP_APPLY_UNMAP_WHEN_REMAPPING_TO_SELF;
214 }
215
216 const IDRemapperApplyResult expected_mapping_result = id_remapper.get_mapping_result(
217 *id_p, id_remapper_options, id_self);
218 /* Exit when no modifications will be done, ensuring id->runtime counters won't changed. */
219 if (ELEM(expected_mapping_result,
222 {
223 BLI_assert_msg(id_remap_data->type == ID_REMAP_TYPE_REMAP,
224 "Cleanup should always do unassign.");
225 return IDWALK_RET_NOP;
226 }
227
228 const bool is_reference = (cb_flag & IDWALK_CB_OVERRIDE_LIBRARY_REFERENCE) != 0;
229 const bool is_indirect = (cb_flag & IDWALK_CB_INDIRECT_USAGE) != 0;
230 const bool skip_indirect = (id_remap_data->flag & ID_REMAP_SKIP_INDIRECT_USAGE) != 0;
231 const bool is_obj = (GS(id_owner->name) == ID_OB);
232 /* NOTE: Edit Mode is a 'skip direct' case, unless specifically requested, obdata should not be
233 * remapped in this situation. */
234 const bool is_obj_editmode = (is_obj && BKE_object_is_in_editmode((Object *)id_owner) &&
235 (id_remap_data->flag & ID_REMAP_FORCE_OBDATA_IN_EDITMODE) == 0);
236 const bool violates_never_null = ((cb_flag & IDWALK_CB_NEVER_NULL) &&
237 (expected_mapping_result ==
239 (id_remap_data->flag & ID_REMAP_FORCE_NEVER_NULL_USAGE) == 0);
240 const bool skip_reference = (id_remap_data->flag & ID_REMAP_SKIP_OVERRIDE_LIBRARY) != 0;
241 const bool skip_never_null = (id_remap_data->flag & ID_REMAP_SKIP_NEVER_NULL_USAGE) != 0;
242
243#ifdef DEBUG_PRINT
244 printf(
245 "In %s (lib %p): Remapping %s (%p) remap operation: %s "
246 "(is_indirect: %d, skip_indirect: %d, is_reference: %d, skip_reference: %d)\n",
247 id_owner->name,
248 id_owner->lib,
249 (*id_p)->name,
250 *id_p,
251 id_remapper.result_to_string(expected_mapping_result).c_str(),
252 is_indirect,
253 skip_indirect,
254 is_reference,
255 skip_reference);
256#endif
257
258 if ((id_remap_data->flag & ID_REMAP_STORE_NEVER_NULL_USAGE) &&
259 (cb_flag & IDWALK_CB_NEVER_NULL) &&
260 (expected_mapping_result == ID_REMAP_RESULT_SOURCE_UNASSIGNED))
261 {
262 id_remapper.never_null_users_add(id_owner);
263 }
264
265 /* Special hack in case it's Object->data and we are in edit mode, and new_id is not nullptr
266 * (otherwise, we follow common NEVER_NULL flags).
267 * (skipped_indirect too). */
268 if ((violates_never_null && skip_never_null) ||
269 (is_obj_editmode && (((Object *)id_owner)->data == *id_p) &&
270 (expected_mapping_result == ID_REMAP_RESULT_SOURCE_REMAPPED)) ||
271 (skip_indirect && is_indirect) || (is_reference && skip_reference))
272 {
274 id_p,
275 cb_flag,
276 is_indirect,
277 is_reference,
278 violates_never_null,
279 is_obj,
280 is_obj_editmode);
281 }
282 else {
284 id_self,
285 id_p,
286 id_remap_data,
287 id_remapper,
288 id_remapper_options,
289 cb_flag,
290 is_indirect,
291 violates_never_null);
292 }
293
294 return IDWALK_RET_NOP;
295}
296
298 eIDRemapType remap_type,
299 const IDRemapper &id_remapper)
300{
301 if (ob->type != OB_ARMATURE) {
302 return;
303 }
304 if (ob->pose == nullptr) {
305 return;
306 }
307
308 const bool is_cleanup_type = remap_type == ID_REMAP_TYPE_CLEANUP;
309 /* Early exit when mapping, but no armature mappings present. */
310 if (!is_cleanup_type && !id_remapper.contains_mappings_for_any(FILTER_ID_AR)) {
311 return;
312 }
313
314 /* Object's pose holds reference to armature bones. sic */
315 /* Note that in theory, we should have to bother about linked/non-linked/never-null/etc.
316 * flags/states.
317 * Fortunately, this is just a tag, so we can accept to 'over-tag' a bit for pose recalc,
318 * and avoid another complex and risky condition nightmare like the one we have in
319 * foreach_libblock_remap_callback(). */
320 const IDRemapperApplyResult expected_mapping_result = id_remapper.get_mapping_result(
321 static_cast<ID *>(ob->data), ID_REMAP_APPLY_DEFAULT, nullptr);
322 if (is_cleanup_type || expected_mapping_result == ID_REMAP_RESULT_SOURCE_REMAPPED) {
323 ob->pose->flag |= POSE_RECALC;
324 /* We need to clear pose bone pointers immediately, some code may access those before
325 * pose is actually recomputed, which can lead to segfault. */
327 }
328}
329
330static void libblock_remap_data_preprocess(ID *id_owner,
331 eIDRemapType remap_type,
332 const IDRemapper &id_remapper)
333{
334 switch (GS(id_owner->name)) {
335 case ID_OB: {
336 Object *ob = (Object *)id_owner;
337 libblock_remap_data_preprocess_ob(ob, remap_type, id_remapper);
338 break;
339 }
340 default:
341 break;
342 }
343}
344
350 Object *old_ob,
351 Object * /*new_ob*/,
352 const bool do_sync_collection)
353{
354 /* Will only effectively process collections that have been tagged with
355 * #COLLECTION_TAG_COLLECTION_OBJECT_DIRTY. See #collection_foreach_id callback. */
357
358 if (do_sync_collection) {
360 }
361
362 if (old_ob == nullptr) {
363 for (Object *ob = static_cast<Object *>(bmain->objects.first); ob != nullptr;
364 ob = static_cast<Object *>(ob->id.next))
365 {
366 if (ob->type == OB_MBALL && BKE_mball_is_basis(ob)) {
368 }
369 }
370 }
371 else {
372 for (Object *ob = static_cast<Object *>(bmain->objects.first); ob != nullptr;
373 ob = static_cast<Object *>(ob->id.next))
374 {
375 if (ob->type == OB_MBALL && BKE_mball_is_basis_for(ob, old_ob)) {
377 break; /* There is only one basis... */
378 }
379 }
380 }
381}
382
383/* Can be called with both old_collection and new_collection being nullptr,
384 * this means we have to check whole Main database then. */
386 Collection *owner_collection,
387 Collection * /*old_collection*/,
388 Collection *new_collection)
389{
390 if (new_collection == nullptr) {
391 /* XXX Complex cases can lead to nullptr pointers in other collections than old_collection,
392 * and BKE_main_collection_sync_remap() does not tolerate any of those, so for now always check
393 * whole existing collections for nullptr pointers.
394 * I'd consider optimizing that whole collection remapping process a TODO: for later. */
395 BKE_collections_child_remove_nulls(bmain, owner_collection, nullptr /*old_collection*/);
396 }
397 else {
398 /* Temp safe fix, but a "tad" brute force... We should probably be able to use parents from
399 * old_collection instead? */
400 /* NOTE: Also takes care of duplicated child collections that remapping may have created. */
402 }
403
405}
406
408{
409 if (ob->data == new_id) {
410 switch (GS(new_id->name)) {
411 case ID_ME:
413 break;
414 case ID_CU_LEGACY:
415 BKE_curve_type_test(ob, true);
416 break;
417 default:
418 break;
419 }
421 BKE_object_materials_sync_length(bmain, ob, new_id);
422 }
423}
424
426{
427 /* Update all group nodes using a node group. */
429}
430
431static void libblock_remap_data_update_tags(ID *old_id, ID *new_id, IDRemap *id_remap_data)
432{
433 const int remap_flags = id_remap_data->flag;
434 if ((remap_flags & ID_REMAP_SKIP_USER_CLEAR) == 0) {
435 /* XXX We may not want to always 'transfer' fake-user from old to new id...
436 * Think for now it's desired behavior though,
437 * we can always add an option (flag) to control this later if needed. */
438 if (old_id != nullptr && (old_id->flag & ID_FLAG_FAKEUSER) && new_id != nullptr) {
439 id_fake_user_clear(old_id);
440 id_fake_user_set(new_id);
441 }
442
443 id_us_clear_real(old_id);
444 }
445
446 if (new_id != nullptr && (new_id->tag & ID_TAG_INDIRECT) &&
448 {
449 new_id->tag &= ~ID_TAG_INDIRECT;
451 new_id->tag |= ID_TAG_EXTERN;
452 }
453}
454
455static void libblock_remap_reset_remapping_status_fn(ID *old_id, ID *new_id)
456{
458 if (new_id != nullptr) {
460 }
461}
462
487 Main *bmain, ID *id, eIDRemapType remap_type, IDRemapper &id_remapper, const int remap_flags)
488{
489 IDRemap id_remap_data = {
490 /*type*/ remap_type,
491 /*bmain*/ bmain,
492 /*id_remapper*/ id_remapper,
493 /*id_owner*/ nullptr,
494 /*flag*/ remap_flags,
495 };
496
497 const bool include_ui = (remap_flags & ID_REMAP_FORCE_UI_POINTERS) != 0;
498 const LibraryForeachIDFlag foreach_id_flags =
499 (((remap_flags & ID_REMAP_FORCE_INTERNAL_RUNTIME_POINTERS) != 0 ?
501 IDWALK_NOP) |
502 (include_ui ? IDWALK_INCLUDE_UI : IDWALK_NOP) |
503
505 IDWALK_NOP) |
507 IDWALK_NOP));
508
510
511 if (id) {
512#ifdef DEBUG_PRINT
513 printf("\tchecking id %s (%p, %p)\n", id->name, id, id->lib);
514#endif
515 id_remap_data.id_owner = (id->flag & ID_FLAG_EMBEDDED_DATA) ? BKE_id_owner_get(id) : id;
516 libblock_remap_data_preprocess(id_remap_data.id_owner, remap_type, id_remapper);
518 bmain, id, foreach_libblock_remap_callback, &id_remap_data, foreach_id_flags);
519 }
520 else {
521 /* Note that this is a very 'brute force' approach,
522 * maybe we could use some depsgraph to only process objects actually using given old_id...
523 * sounds rather unlikely currently, though, so this will do for now. */
524 ID *id_curr;
525
526 FOREACH_MAIN_ID_BEGIN (bmain, id_curr) {
527 const uint64_t can_use_filter_id = BKE_library_id_can_use_filter_id(id_curr, include_ui);
528 const bool has_mapping = id_remapper.contains_mappings_for_any(can_use_filter_id);
529
530 /* Continue when id_remapper doesn't have any mappings that can be used by id_curr. */
531 if (!has_mapping) {
532 continue;
533 }
534
535 /* Note that we cannot skip indirect usages of old_id
536 * here (if requested), we still need to check it for the
537 * user count handling...
538 * XXX No more true (except for debug usage of those
539 * skipping counters). */
540 id_remap_data.id_owner = id_curr;
541 libblock_remap_data_preprocess(id_remap_data.id_owner, remap_type, id_remapper);
543 bmain, id_curr, foreach_libblock_remap_callback, &id_remap_data, foreach_id_flags);
544 }
546 }
547
548 id_remapper.iter([&](ID *old_id, ID *new_id) {
549 libblock_remap_data_update_tags(old_id, new_id, &id_remap_data);
550 });
551}
552
553static void libblock_remap_foreach_idpair(ID *old_id, ID *new_id, Main *bmain, int remap_flags)
554{
555 if (old_id == new_id) {
556 return;
557 }
558
559 BLI_assert(old_id != nullptr);
560 BLI_assert((new_id == nullptr) || remap_flags & ID_REMAP_ALLOW_IDTYPE_MISMATCH ||
561 GS(old_id->name) == GS(new_id->name));
562
565 }
566
567 if ((remap_flags & ID_REMAP_SKIP_USER_CLEAR) == 0) {
568 /* If old_id was used by some ugly 'user_one' stuff (like Image or Clip editors...), and user
569 * count has actually been incremented for that, we have to decrease once more its user
570 * count... unless we had to skip some 'user_one' cases. */
571 if ((old_id->tag & ID_TAG_EXTRAUSER_SET) &&
573 {
574 id_us_clear_real(old_id);
575 }
576 }
577
578 const int skipped_refcounted = old_id->runtime.remap.skipped_refcounted;
579 if (old_id->us - skipped_refcounted < 0) {
581 "Error in remapping process from '%s' (%p) to '%s' (%p): "
582 "wrong user count in old ID after process (summing up to %d)",
583 old_id->name,
584 old_id,
585 new_id ? new_id->name : "<nullptr>",
586 new_id,
587 old_id->us - skipped_refcounted);
588 }
589
590 const int skipped_direct = old_id->runtime.remap.skipped_direct;
591 if (skipped_direct == 0) {
592 /* old_id is assumed to not be used directly anymore... */
593 if (old_id->lib && (old_id->tag & ID_TAG_EXTERN)) {
594 old_id->tag &= ~ID_TAG_EXTERN;
595 old_id->tag |= ID_TAG_INDIRECT;
596 }
597 }
598
599 /* Some after-process updates.
600 * This is a bit ugly, but cannot see a way to avoid it.
601 * Maybe we should do a per-ID callback for this instead? */
602 switch (GS(old_id->name)) {
603 case ID_OB:
605 bmain, (Object *)old_id, (Object *)new_id, true);
606 break;
607 case ID_GR:
609 bmain, nullptr, (Collection *)old_id, (Collection *)new_id);
610 break;
611 case ID_ME:
612 case ID_CU_LEGACY:
613 case ID_MB:
614 case ID_CV:
615 case ID_PT:
616 case ID_VO:
617 if (new_id) { /* Only affects us in case obdata was relinked (changed). */
618 for (Object *ob = static_cast<Object *>(bmain->objects.first); ob;
619 ob = static_cast<Object *>(ob->id.next))
620 {
622 }
623 }
624 break;
625 default:
626 break;
627 }
628
629 /* Node trees may virtually use any kind of data-block... */
630 /* XXX Yuck!!!! nodetree update can do pretty much any thing when talking about py nodes,
631 * including creating new data-blocks (see #50385), so we need to unlock main here. :(
632 * Why can't we have re-entrent locks? */
633 BKE_main_unlock(bmain);
635 BKE_main_lock(bmain);
636
637 /* Full rebuild of DEG! */
639
641}
642
643void BKE_libblock_remap_multiple_locked(Main *bmain, IDRemapper &mappings, const int remap_flags)
644{
645 if (mappings.is_empty()) {
646 /* Early exit nothing to do. */
647 return;
648 }
649
650 libblock_remap_data(bmain, nullptr, ID_REMAP_TYPE_REMAP, mappings, remap_flags);
651
652 mappings.iter([&](ID *old_id, ID *new_id) {
653 libblock_remap_foreach_idpair(old_id, new_id, bmain, remap_flags);
654 });
655
656 /* We assume editors do not hold references to their IDs... This is false in some cases
657 * (Image is especially tricky here),
658 * editors' code is to handle refcount (id->us) itself then. */
661 }
662
663 /* Full rebuild of DEG! */
665}
666
667void BKE_libblock_remap_multiple_raw(Main *bmain, IDRemapper &mappings, const int remap_flags)
668{
669 if (mappings.is_empty()) {
670 /* Early exit nothing to do. */
671 return;
672 }
673
675 nullptr,
677 mappings,
679}
680
681void BKE_libblock_remap_locked(Main *bmain, void *old_idv, void *new_idv, const int remap_flags)
682{
683 IDRemapper remapper;
684 ID *old_id = static_cast<ID *>(old_idv);
685 ID *new_id = static_cast<ID *>(new_idv);
686 remapper.add(old_id, new_id);
687 BKE_libblock_remap_multiple_locked(bmain, remapper, remap_flags);
688}
689
690void BKE_libblock_remap(Main *bmain, void *old_idv, void *new_idv, const int remap_flags)
691{
692 BKE_main_lock(bmain);
693
694 BKE_libblock_remap_locked(bmain, old_idv, new_idv, remap_flags);
695
696 BKE_main_unlock(bmain);
697}
698
699void BKE_libblock_remap_multiple(Main *bmain, IDRemapper &mappings, const int remap_flags)
700{
701 BKE_main_lock(bmain);
702
703 BKE_libblock_remap_multiple_locked(bmain, mappings, remap_flags);
704
705 BKE_main_unlock(bmain);
706}
707
708void BKE_libblock_unlink(Main *bmain, void *idv, const bool do_skip_indirect)
709{
710 const int remap_flags = (do_skip_indirect ? ID_REMAP_SKIP_INDIRECT_USAGE : 0);
711
712 BKE_main_lock(bmain);
713
714 BKE_libblock_remap_locked(bmain, idv, nullptr, remap_flags);
715
716 BKE_main_unlock(bmain);
717}
718
719/* XXX Arg! Naming... :(
720 * _relink? avoids confusion with _remap, but is confusing with _unlink
721 * _remap_used_ids?
722 * _remap_datablocks?
723 * BKE_id_remap maybe?
724 * ... sigh
725 */
726
728 ID *new_id,
729 Main *bmain,
730 const blender::Span<ID *> ids)
731{
732 BLI_assert(old_id != nullptr);
733 BLI_assert((new_id == nullptr) || GS(old_id->name) == GS(new_id->name));
734 BLI_assert(old_id != new_id);
735
736 bool is_object_update_processed = false;
737 for (ID *id_iter : ids) {
738 /* Some after-process updates.
739 * This is a bit ugly, but cannot see a way to avoid it.
740 * Maybe we should do a per-ID callback for this instead?
741 */
742 switch (GS(id_iter->name)) {
743 case ID_SCE:
744 case ID_GR: {
745 /* NOTE: here we know which collection we have affected, so at lest for nullptr children
746 * detection we can only process that one.
747 * This is also a required fix in case `id` would not be in Main anymore, which can happen
748 * e.g. when called from `id_delete`. */
749 Collection *owner_collection = (GS(id_iter->name) == ID_GR) ?
750 (Collection *)id_iter :
751 ((Scene *)id_iter)->master_collection;
752 switch (GS(old_id->name)) {
753 case ID_OB:
754 if (!is_object_update_processed) {
756 bmain, (Object *)old_id, (Object *)new_id, true);
757 is_object_update_processed = true;
758 }
759 break;
760 case ID_GR:
762 bmain, owner_collection, (Collection *)old_id, (Collection *)new_id);
763 break;
764 default:
765 break;
766 }
767 break;
768 }
769 case ID_OB:
770 if (new_id != nullptr) { /* Only affects us in case obdata was relinked (changed). */
771 libblock_remap_data_postprocess_obdata_relink(bmain, (Object *)id_iter, new_id);
772 }
773 break;
774 default:
775 break;
776 }
777 }
778}
779
781 const blender::Span<ID *> ids,
782 const eIDRemapType remap_type,
783 IDRemapper &id_remapper,
784 const int remap_flags)
785{
786 BLI_assert(remap_type == ID_REMAP_TYPE_REMAP || id_remapper.is_empty());
787
788 for (ID *id_iter : ids) {
789 libblock_remap_data(bmain, id_iter, remap_type, id_remapper, remap_flags);
790 }
791
792 if (bmain == nullptr) {
793 return;
794 }
795
796 switch (remap_type) {
797 case ID_REMAP_TYPE_REMAP: {
798 id_remapper.iter([&](ID *old_id, ID *new_id) {
799 libblock_relink_foreach_idpair(old_id, new_id, bmain, ids);
800 });
801 break;
802 }
804 bool is_object_update_processed = false;
805 for (ID *id_iter : ids) {
806 switch (GS(id_iter->name)) {
807 case ID_SCE:
808 case ID_GR: {
809 /* NOTE: here we know which collection we have affected, so at lest for nullptr
810 * children detection we can only process that one. This is also a required fix in case
811 * `id` would not be in Main anymore, which can happen e.g. when called from
812 * `id_delete`. */
813 Collection *owner_collection = (GS(id_iter->name) == ID_GR) ?
814 (Collection *)id_iter :
815 ((Scene *)id_iter)->master_collection;
816 /* No choice but to check whole objects once, and all children collections. */
817 if (!is_object_update_processed) {
818 /* We only want to affect Object pointers here, not Collection ones, LayerCollections
819 * will be resynced as part of the call to
820 * `libblock_remap_data_postprocess_collection_update` below. */
821 libblock_remap_data_postprocess_object_update(bmain, nullptr, nullptr, false);
822 is_object_update_processed = true;
823 }
825 bmain, owner_collection, nullptr, nullptr);
826 break;
827 }
828 default:
829 break;
830 }
831 }
832
833 break;
834 }
835 default:
837 }
838
840}
841
843 Main *bmain, void *idv, void *old_idv, void *new_idv, const int remap_flags)
844{
845
846 /* Should be able to replace all _relink() functions (constraints, rigidbody, etc.) ? */
847
848 ID *id = static_cast<ID *>(idv);
849 ID *old_id = static_cast<ID *>(old_idv);
850 ID *new_id = static_cast<ID *>(new_idv);
851 blender::Array<ID *> ids = {id};
852
853 /* No need to lock here, we are only affecting given ID, not bmain database. */
854 IDRemapper id_remapper;
856
857 BLI_assert(id != nullptr);
859 if (old_id != nullptr) {
860 BLI_assert((new_id == nullptr) || GS(old_id->name) == GS(new_id->name));
861 BLI_assert(old_id != new_id);
862 id_remapper.add(old_id, new_id);
863 }
864 else {
865 BLI_assert(new_id == nullptr);
866 remap_type = ID_REMAP_TYPE_CLEANUP;
867 }
868
869 BKE_libblock_relink_multiple(bmain, ids, remap_type, id_remapper, remap_flags);
870}
871
876
878 ID *id,
879 RelinkToNewIDData *relink_data);
881{
882 const LibraryForeachIDCallbackFlag cb_flag = cb_data->cb_flag;
883 /* NOTE: For now, support remapping `IDWALK_CB_EMBEDDED_NON_OWNING` pointers. */
885 return IDWALK_RET_NOP;
886 }
887
888 Main *bmain = cb_data->bmain;
889 ID **id_pointer = cb_data->id_pointer;
890 ID *id = *id_pointer;
891 RelinkToNewIDData *relink_data = static_cast<RelinkToNewIDData *>(cb_data->user_data);
892
893 if (id) {
894 /* See: #ID_NEW_SET macro. */
895 if (id->newid != nullptr) {
896 relink_data->id_remapper.add(id, id->newid);
897 id = id->newid;
898 }
899 if (id->tag & ID_TAG_NEW) {
900 libblock_relink_to_newid_prepare_data(bmain, id, relink_data);
901 }
902 }
903 return IDWALK_RET_NOP;
904}
905
907 ID *id,
908 RelinkToNewIDData *relink_data)
909{
910 if (ID_IS_LINKED(id)) {
911 return;
912 }
913
914 id->tag &= ~ID_TAG_NEW;
915 relink_data->ids.append(id);
917}
918
919void BKE_libblock_relink_to_newid(Main *bmain, ID *id, const int remap_flag)
920{
921 if (ID_IS_LINKED(id)) {
922 return;
923 }
924 /* We do not want to have those cached relationship data here. */
925 BLI_assert(bmain->relations == nullptr);
926
927 RelinkToNewIDData relink_data{};
928
929 libblock_relink_to_newid_prepare_data(bmain, id, &relink_data);
930
931 const int remap_flag_final = remap_flag | ID_REMAP_SKIP_INDIRECT_USAGE |
934 bmain, relink_data.ids, ID_REMAP_TYPE_REMAP, relink_data.id_remapper, remap_flag_final);
935}
void BKE_pose_clear_pointers(bPose *pose)
Definition armature.cc:2839
void BKE_collections_object_remove_invalids(Main *bmain)
void BKE_collections_child_remove_nulls(Main *bmain, Collection *parent_collection, Collection *child_collection)
void BKE_main_collections_parent_relations_rebuild(Main *bmain)
void BKE_curve_type_test(Object *ob, bool dimension_update)
Definition curve.cc:458
void BKE_main_collection_sync_remap(const Main *bmain)
void id_fake_user_set(ID *id)
Definition lib_id.cc:391
ID * BKE_id_owner_get(ID *id, const bool debug_relationship_assert=true)
Definition lib_id.cc:2491
void id_us_ensure_real(ID *id)
Definition lib_id.cc:308
void id_fake_user_clear(ID *id)
Definition lib_id.cc:399
void id_us_clear_real(ID *id)
Definition lib_id.cc:326
void id_us_plus_no_lib(ID *id)
Definition lib_id.cc:337
void BKE_libblock_runtime_reset_remapping_status(ID *id) ATTR_NONNULL(1)
Definition lib_id.cc:1447
void id_us_min(ID *id)
Definition lib_id.cc:361
LibraryForeachIDCallbackFlag
@ IDWALK_CB_NEVER_SELF
@ IDWALK_CB_USER_ONE
@ IDWALK_CB_USER
@ IDWALK_CB_EMBEDDED
@ IDWALK_CB_NEVER_NULL
@ IDWALK_CB_OVERRIDE_LIBRARY_REFERENCE
@ IDWALK_CB_INDIRECT_USAGE
void BKE_library_foreach_ID_link(Main *bmain, ID *id, blender::FunctionRef< LibraryIDLinkCallback > callback, void *user_data, LibraryForeachIDFlag flag)
Definition lib_query.cc:431
@ IDWALK_RET_NOP
uint64_t BKE_library_id_can_use_filter_id(const ID *owner_id, const bool include_ui, const IDTypeInfo *owner_id_type=nullptr)
Definition lib_query.cc:476
LibraryForeachIDFlag
@ IDWALK_INCLUDE_UI
@ IDWALK_DO_LIBRARY_POINTER
@ IDWALK_NOP
@ IDWALK_DO_INTERNAL_RUNTIME_POINTERS
@ IDWALK_NO_ORIG_POINTERS_ACCESS
void(*)(const blender::bke::id::IDRemapper &mappings) BKE_library_remap_editor_id_reference_cb
IDRemapperApplyResult
@ ID_REMAP_RESULT_SOURCE_REMAPPED
@ ID_REMAP_RESULT_SOURCE_UNASSIGNED
@ ID_REMAP_RESULT_SOURCE_NOT_MAPPABLE
@ ID_REMAP_RESULT_SOURCE_UNAVAILABLE
IDRemapperApplyOptions
@ ID_REMAP_APPLY_UNMAP_WHEN_REMAPPING_TO_SELF
@ ID_REMAP_APPLY_DEFAULT
eIDRemapType
@ ID_REMAP_TYPE_REMAP
@ ID_REMAP_TYPE_CLEANUP
void(*)(const void *) BKE_library_free_notifier_reference_cb
@ ID_REMAP_SKIP_USER_CLEAR
@ ID_REMAP_SKIP_USER_REFCOUNT
@ ID_REMAP_FORCE_INTERNAL_RUNTIME_POINTERS
@ ID_REMAP_SKIP_OVERRIDE_LIBRARY
@ ID_REMAP_FORCE_USER_REFCOUNT
@ ID_REMAP_SKIP_NEVER_NULL_USAGE
@ ID_REMAP_DO_LIBRARY_POINTERS
@ ID_REMAP_FORCE_OBDATA_IN_EDITMODE
@ ID_REMAP_FORCE_UI_POINTERS
@ ID_REMAP_SKIP_INDIRECT_USAGE
@ ID_REMAP_FORCE_NEVER_NULL_USAGE
@ ID_REMAP_STORE_NEVER_NULL_USAGE
@ ID_REMAP_ALLOW_IDTYPE_MISMATCH
@ ID_REMAP_SKIP_UPDATE_TAGGING
@ ID_REMAP_NO_ORIG_POINTERS_ACCESS
#define FOREACH_MAIN_ID_END
Definition BKE_main.hh:563
void BKE_main_lock(Main *bmain)
Definition main.cc:479
#define FOREACH_MAIN_ID_BEGIN(_bmain, _id)
Definition BKE_main.hh:557
void BKE_main_unlock(Main *bmain)
Definition main.cc:484
General operations, lookup, etc. for materials.
void BKE_object_materials_sync_length(Main *bmain, Object *ob, ID *id)
bool BKE_mball_is_basis(const Object *ob)
Definition mball.cc:260
bool BKE_mball_is_basis_for(const Object *ob1, const Object *ob2)
Definition mball.cc:299
void BKE_modifiers_test_object(Object *ob)
void multires_force_sculpt_rebuild(Object *object)
Definition multires.cc:445
void BKE_ntree_update_tag_all(bNodeTree *ntree)
General operations, lookup, etc. for blender objects.
bool BKE_object_is_in_editmode(const Object *ob)
#define BLI_assert_unreachable()
Definition BLI_assert.h:93
#define BLI_assert(a)
Definition BLI_assert.h:46
#define BLI_assert_msg(a, msg)
Definition BLI_assert.h:53
#define UNUSED_VARS_NDEBUG(...)
#define ELEM(...)
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:182
void DEG_id_tag_update(ID *id, unsigned int flags)
void DEG_relations_tag_update(Main *bmain)
@ ID_FLAG_INDIRECT_WEAK_LINK
Definition DNA_ID.h:693
@ ID_FLAG_FAKEUSER
Definition DNA_ID.h:682
@ ID_FLAG_EMBEDDED_DATA
Definition DNA_ID.h:687
@ ID_REMAP_IS_LINKED_DIRECT
Definition DNA_ID.h:368
@ ID_REMAP_IS_USER_ONE_SKIPPED
Definition DNA_ID.h:370
@ ID_RECALC_TRANSFORM
Definition DNA_ID.h:962
@ ID_RECALC_SYNC_TO_EVAL
Definition DNA_ID.h:1026
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:982
@ ID_TAG_NEW
Definition DNA_ID.h:827
@ ID_TAG_INDIRECT
Definition DNA_ID.h:756
@ ID_TAG_EXTERN
Definition DNA_ID.h:750
@ ID_TAG_EXTRAUSER_SET
Definition DNA_ID.h:792
@ ID_TAG_NO_MAIN
Definition DNA_ID.h:886
@ ID_VO
@ ID_NT
@ ID_SCE
@ ID_CV
@ ID_CU_LEGACY
@ ID_ME
@ ID_GR
@ ID_MB
@ ID_OB
@ ID_PT
@ POSE_RECALC
Object groups, one object can be in many groups at once.
Object is a sort of wrapper for general info.
@ OB_MBALL
@ OB_ARMATURE
BMesh const char void * data
unsigned long long int uint64_t
void append(const T &value)
constexpr const char * c_str() const
IDRemapperApplyResult get_mapping_result(ID *id, IDRemapperApplyOptions options, const ID *id_self) const
IDRemapperApplyResult apply(ID **r_id_ptr, IDRemapperApplyOptions options, ID *id_self=nullptr) const
static StringRefNull result_to_string(const IDRemapperApplyResult result)
void iter(FunctionRef< void(ID *old_id, ID *new_id)> func) const
void add(ID *old_id, ID *new_id)
bool contains_mappings_for_any(IDTypeFilter filter) const
#define printf(...)
#define ID_IS_LINKED(_id)
#define FILTER_ID_AR
#define GS(a)
DEG_id_tag_update_ex(cb_data->bmain, cb_data->owner_id, ID_RECALC_TAG_FOR_UNDO|ID_RECALC_SYNC_TO_EVAL)
BKE_library_remap_editor_id_reference_cb remap_editor_id_reference_cb
Definition lib_remap.cc:51
BKE_library_free_notifier_reference_cb free_notifier_reference_cb
Definition lib_remap.cc:44
static void libblock_remap_data_preprocess_ob(Object *ob, eIDRemapType remap_type, const IDRemapper &id_remapper)
Definition lib_remap.cc:297
void BKE_libblock_relink_to_newid(Main *bmain, ID *id, const int remap_flag)
Definition lib_remap.cc:919
BKE_library_remap_editor_id_reference_cb remap_editor_id_reference_cb
Definition lib_remap.cc:51
static int id_relink_to_newid_looper(LibraryIDLinkCallbackData *cb_data)
Definition lib_remap.cc:880
static void libblock_remap_data_postprocess_collection_update(Main *bmain, Collection *owner_collection, Collection *, Collection *new_collection)
Definition lib_remap.cc:385
void BKE_library_callback_remap_editor_id_reference_set(BKE_library_remap_editor_id_reference_cb func)
Definition lib_remap.cc:53
static void libblock_remap_data(Main *bmain, ID *id, eIDRemapType remap_type, IDRemapper &id_remapper, const int remap_flags)
Definition lib_remap.cc:486
void BKE_libblock_remap_multiple(Main *bmain, IDRemapper &mappings, const int remap_flags)
Definition lib_remap.cc:699
static void libblock_remap_data_preprocess(ID *id_owner, eIDRemapType remap_type, const IDRemapper &id_remapper)
Definition lib_remap.cc:330
static void libblock_relink_foreach_idpair(ID *old_id, ID *new_id, Main *bmain, const blender::Span< ID * > ids)
Definition lib_remap.cc:727
static void libblock_relink_to_newid_prepare_data(Main *bmain, ID *id, RelinkToNewIDData *relink_data)
Definition lib_remap.cc:906
static void foreach_libblock_remap_callback_skip(const ID *, ID **id_ptr, const int cb_flag, const bool is_indirect, const bool is_reference, const bool violates_never_null, const bool, const bool is_obj_editmode)
Definition lib_remap.cc:72
void BKE_library_callback_free_notifier_reference_set(BKE_library_free_notifier_reference_cb func)
Definition lib_remap.cc:46
void BKE_libblock_relink_ex(Main *bmain, void *idv, void *old_idv, void *new_idv, const int remap_flags)
Definition lib_remap.cc:842
static void foreach_libblock_remap_callback_apply(ID *id_owner, ID *id_self, ID **id_ptr, IDRemap *id_remap_data, const IDRemapper &mappings, const IDRemapperApplyOptions id_remapper_options, const int cb_flag, const bool is_indirect, const bool violates_never_null)
Definition lib_remap.cc:103
void BKE_libblock_remap(Main *bmain, void *old_idv, void *new_idv, const int remap_flags)
Definition lib_remap.cc:690
void BKE_libblock_unlink(Main *bmain, void *idv, const bool do_skip_indirect)
Definition lib_remap.cc:708
static void libblock_remap_data_update_tags(ID *old_id, ID *new_id, IDRemap *id_remap_data)
Definition lib_remap.cc:431
void BKE_libblock_remap_multiple_raw(Main *bmain, IDRemapper &mappings, const int remap_flags)
Definition lib_remap.cc:667
static void libblock_remap_data_postprocess_obdata_relink(Main *bmain, Object *ob, ID *new_id)
Definition lib_remap.cc:407
static void libblock_remap_data_postprocess_object_update(Main *bmain, Object *old_ob, Object *, const bool do_sync_collection)
Definition lib_remap.cc:349
void BKE_libblock_remap_locked(Main *bmain, void *old_idv, void *new_idv, const int remap_flags)
Definition lib_remap.cc:681
void BKE_libblock_remap_multiple_locked(Main *bmain, IDRemapper &mappings, const int remap_flags)
Definition lib_remap.cc:643
void BKE_libblock_relink_multiple(Main *bmain, const blender::Span< ID * > ids, const eIDRemapType remap_type, IDRemapper &id_remapper, const int remap_flags)
Definition lib_remap.cc:780
BKE_library_free_notifier_reference_cb free_notifier_reference_cb
Definition lib_remap.cc:44
static void libblock_remap_reset_remapping_status_fn(ID *old_id, ID *new_id)
Definition lib_remap.cc:455
static int foreach_libblock_remap_callback(LibraryIDLinkCallbackData *cb_data)
Definition lib_remap.cc:173
static void libblock_remap_data_postprocess_nodetree_update(Main *bmain, ID *new_id)
Definition lib_remap.cc:425
static void libblock_remap_foreach_idpair(ID *old_id, ID *new_id, Main *bmain, int remap_flags)
Definition lib_remap.cc:553
#define LOG(severity)
Definition log.h:32
void node_tree_update_all_users(Main *main, ID *id)
Definition node.cc:5217
eIDRemapType type
Definition lib_remap.cc:60
ID * id_owner
Definition lib_remap.cc:66
int flag
Definition lib_remap.cc:67
Main * bmain
Definition lib_remap.cc:61
IDRemapper & id_remapper
Definition lib_remap.cc:63
int skipped_refcounted
Definition DNA_ID.h:378
ID_Runtime_Remap remap
Definition DNA_ID.h:389
Definition DNA_ID.h:404
int tag
Definition DNA_ID.h:424
struct Library * lib
Definition DNA_ID.h:410
int us
Definition DNA_ID.h:425
short flag
Definition DNA_ID.h:420
char name[66]
Definition DNA_ID.h:415
struct ID_Runtime runtime
Definition DNA_ID.h:493
LibraryForeachIDCallbackFlag cb_flag
void * first
MainIDRelations * relations
Definition BKE_main.hh:298
ListBase objects
Definition BKE_main.hh:247
struct bPose * pose
IDRemapper id_remapper
Definition lib_remap.cc:874
blender::Vector< ID * > ids
Definition lib_remap.cc:873