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