Blender V5.0
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 = {"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) {
144 new_id->runtime->remap.status |= ID_REMAP_IS_LINKED_DIRECT;
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 const bool is_self_embedded = (id_self->flag & ID_FLAG_EMBEDDED_DATA) != 0;
190
191 /* Those asserts ensure the general sanity of ID tags regarding 'embedded' ID data (root
192 * node-trees and co). */
193 BLI_assert(id_owner == id_remap_data->id_owner);
194 BLI_assert(id_self == id_owner || is_self_embedded);
195
196 /* Early exit when id pointer isn't set. */
197 if (*id_p == nullptr) {
198 return IDWALK_RET_NOP;
199 }
200
201 /* Similar to above early-out on `IDWALK_CB_EMBEDDED` calls on ID pointers to embedded data, the
202 * 'loopback' pointers of embedded IDs towards their owner ID should never be remapped here.
203 *
204 * This relation between owner ID and its embedded ID is not the responsibility of ID management,
205 * and should never be affected by ID remapping.
206 */
207 if (is_self_embedded && (cb_flag & IDWALK_CB_LOOPBACK) != 0 && *id_p == id_owner) {
208 return IDWALK_RET_NOP;
209 }
210
211 IDRemapper &id_remapper = id_remap_data->id_remapper;
213
214 /* Used to cleanup all IDs used by a specific one. */
215 if (id_remap_data->type == ID_REMAP_TYPE_CLEANUP) {
216 /* Clearing existing instance to reduce potential lookup times for IDs referencing many other
217 * IDs. This makes sure that there will only be a single rule in the id_remapper. */
218 id_remapper.clear();
219 id_remapper.add(*id_p, nullptr);
220 }
221
222 /* Better remap to nullptr than not remapping at all,
223 * then we can handle it as a regular remap-to-nullptr case. */
224 if (cb_flag & IDWALK_CB_NEVER_SELF) {
225 id_remapper_options |= ID_REMAP_APPLY_UNMAP_WHEN_REMAPPING_TO_SELF;
226 }
227
228 const IDRemapperApplyResult expected_mapping_result = id_remapper.get_mapping_result(
229 *id_p, id_remapper_options, id_self);
230 /* Exit when no modifications will be done, ensuring id->runtime counters won't changed. */
231 if (ELEM(expected_mapping_result,
234 {
235 BLI_assert_msg(id_remap_data->type == ID_REMAP_TYPE_REMAP,
236 "Cleanup should always do unassign.");
237 return IDWALK_RET_NOP;
238 }
239
240 const bool is_reference = (cb_flag & IDWALK_CB_OVERRIDE_LIBRARY_REFERENCE) != 0;
241 const bool is_indirect = (cb_flag & IDWALK_CB_INDIRECT_USAGE) != 0;
242 const bool skip_indirect = (id_remap_data->flag & ID_REMAP_SKIP_INDIRECT_USAGE) != 0;
243 const bool is_obj = (GS(id_owner->name) == ID_OB);
244 /* NOTE: Edit Mode is a 'skip direct' case, unless specifically requested, obdata should not be
245 * remapped in this situation. */
246 const bool is_obj_editmode = (is_obj && BKE_object_is_in_editmode((Object *)id_owner) &&
247 (id_remap_data->flag & ID_REMAP_FORCE_OBDATA_IN_EDITMODE) == 0);
248 const bool violates_never_null = ((cb_flag & IDWALK_CB_NEVER_NULL) &&
249 (expected_mapping_result ==
251 (id_remap_data->flag & ID_REMAP_FORCE_NEVER_NULL_USAGE) == 0);
252 const bool skip_reference = (id_remap_data->flag & ID_REMAP_SKIP_OVERRIDE_LIBRARY) != 0;
253 const bool skip_never_null = (id_remap_data->flag & ID_REMAP_SKIP_NEVER_NULL_USAGE) != 0;
254
255#ifdef DEBUG_PRINT
256 printf(
257 "In %s (lib %p): Remapping %s (%p) remap operation: %s "
258 "(is_indirect: %d, skip_indirect: %d, is_reference: %d, skip_reference: %d)\n",
259 id_owner->name,
260 id_owner->lib,
261 (*id_p)->name,
262 *id_p,
263 id_remapper.result_to_string(expected_mapping_result).c_str(),
264 is_indirect,
265 skip_indirect,
266 is_reference,
267 skip_reference);
268#endif
269
270 if ((id_remap_data->flag & ID_REMAP_STORE_NEVER_NULL_USAGE) &&
271 (cb_flag & IDWALK_CB_NEVER_NULL) &&
272 (expected_mapping_result == ID_REMAP_RESULT_SOURCE_UNASSIGNED))
273 {
274 id_remapper.never_null_users_add(id_owner);
275 }
276
277 /* Special hack in case it's Object->data and we are in edit mode, and new_id is not nullptr
278 * (otherwise, we follow common NEVER_NULL flags).
279 * (skipped_indirect too). */
280 if ((violates_never_null && skip_never_null) ||
281 (is_obj_editmode && (((Object *)id_owner)->data == *id_p) &&
282 (expected_mapping_result == ID_REMAP_RESULT_SOURCE_REMAPPED)) ||
283 (skip_indirect && is_indirect) || (is_reference && skip_reference))
284 {
286 id_p,
287 cb_flag,
288 is_indirect,
289 is_reference,
290 violates_never_null,
291 is_obj,
292 is_obj_editmode);
293 }
294 else {
296 id_self,
297 id_p,
298 id_remap_data,
299 id_remapper,
300 id_remapper_options,
301 cb_flag,
302 is_indirect,
303 violates_never_null);
304 }
305
306 return IDWALK_RET_NOP;
307}
308
310 eIDRemapType remap_type,
311 const IDRemapper &id_remapper)
312{
313 if (ob->type != OB_ARMATURE) {
314 return;
315 }
316 if (ob->pose == nullptr) {
317 return;
318 }
319
320 const bool is_cleanup_type = remap_type == ID_REMAP_TYPE_CLEANUP;
321 /* Early exit when mapping, but no armature mappings present. */
322 if (!is_cleanup_type && !id_remapper.contains_mappings_for_any(FILTER_ID_AR)) {
323 return;
324 }
325
326 /* Object's pose holds reference to armature bones. sic */
327 /* Note that in theory, we should have to bother about linked/non-linked/never-null/etc.
328 * flags/states.
329 * Fortunately, this is just a tag, so we can accept to 'over-tag' a bit for pose recalc,
330 * and avoid another complex and risky condition nightmare like the one we have in
331 * foreach_libblock_remap_callback(). */
332 const IDRemapperApplyResult expected_mapping_result = id_remapper.get_mapping_result(
333 static_cast<ID *>(ob->data), ID_REMAP_APPLY_DEFAULT, nullptr);
334 if (is_cleanup_type || expected_mapping_result == ID_REMAP_RESULT_SOURCE_REMAPPED) {
335 ob->pose->flag |= POSE_RECALC;
336 /* We need to clear pose bone pointers immediately, some code may access those before
337 * pose is actually recomputed, which can lead to segfault. */
339 }
340}
341
342static void libblock_remap_data_preprocess(ID *id_owner,
343 eIDRemapType remap_type,
344 const IDRemapper &id_remapper)
345{
346 switch (GS(id_owner->name)) {
347 case ID_OB: {
348 Object *ob = (Object *)id_owner;
349 libblock_remap_data_preprocess_ob(ob, remap_type, id_remapper);
350 break;
351 }
352 default:
353 break;
354 }
355}
356
362 Object *old_ob,
363 Object * /*new_ob*/,
364 const bool do_sync_collection)
365{
366 /* Will only effectively process collections that have been tagged with
367 * #COLLECTION_TAG_COLLECTION_OBJECT_DIRTY. See #collection_foreach_id callback. */
369
370 if (do_sync_collection) {
372 }
373
374 if (old_ob == nullptr) {
375 for (Object *ob = static_cast<Object *>(bmain->objects.first); ob != nullptr;
376 ob = static_cast<Object *>(ob->id.next))
377 {
378 if (ob->type == OB_MBALL && BKE_mball_is_basis(ob)) {
380 }
381 }
382 }
383 else {
384 for (Object *ob = static_cast<Object *>(bmain->objects.first); ob != nullptr;
385 ob = static_cast<Object *>(ob->id.next))
386 {
387 if (ob->type == OB_MBALL && BKE_mball_is_basis_for(ob, old_ob)) {
389 break; /* There is only one basis... */
390 }
391 }
392 }
393}
394
395/* Can be called with both old_collection and new_collection being nullptr,
396 * this means we have to check whole Main database then. */
398 Collection *owner_collection,
399 Collection * /*old_collection*/,
400 Collection *new_collection)
401{
402 if (new_collection == nullptr) {
403 /* XXX Complex cases can lead to nullptr pointers in other collections than old_collection,
404 * and BKE_main_collection_sync_remap() does not tolerate any of those, so for now always check
405 * whole existing collections for nullptr pointers.
406 * I'd consider optimizing that whole collection remapping process a TODO: for later. */
407 BKE_collections_child_remove_nulls(bmain, owner_collection, nullptr /*old_collection*/);
408 }
409 else {
410 /* Temp safe fix, but a "tad" brute force... We should probably be able to use parents from
411 * old_collection instead? */
412 /* NOTE: Also takes care of duplicated child collections that remapping may have created. */
414 }
415
417}
418
420{
421 if (ob->data == new_id) {
422 switch (GS(new_id->name)) {
423 case ID_ME:
425 break;
426 case ID_CU_LEGACY:
427 BKE_curve_type_test(ob, true);
428 break;
429 default:
430 break;
431 }
433 BKE_object_materials_sync_length(bmain, ob, new_id);
434 }
435}
436
438{
439 /* Update all group nodes using a node group. */
441}
442
443static void libblock_remap_data_update_tags(ID *old_id, ID *new_id, IDRemap *id_remap_data)
444{
445 const int remap_flags = id_remap_data->flag;
446 if ((remap_flags & ID_REMAP_SKIP_USER_CLEAR) == 0) {
447 /* XXX We may not want to always 'transfer' fake-user from old to new id...
448 * Think for now it's desired behavior though,
449 * we can always add an option (flag) to control this later if needed. */
450 if (old_id != nullptr && (old_id->flag & ID_FLAG_FAKEUSER) && new_id != nullptr) {
451 id_fake_user_clear(old_id);
452 id_fake_user_set(new_id);
453 }
454
455 id_us_clear_real(old_id);
456 }
457
458 if (new_id != nullptr && (new_id->tag & ID_TAG_INDIRECT) &&
459 (new_id->runtime->remap.status & ID_REMAP_IS_LINKED_DIRECT))
460 {
461 new_id->tag &= ~ID_TAG_INDIRECT;
463 new_id->tag |= ID_TAG_EXTERN;
464 }
465}
466
467static void libblock_remap_reset_remapping_status_fn(ID *old_id, ID *new_id)
468{
470 if (new_id != nullptr) {
472 }
473}
474
499 Main *bmain, ID *id, eIDRemapType remap_type, IDRemapper &id_remapper, const int remap_flags)
500{
501 IDRemap id_remap_data = {
502 /*type*/ remap_type,
503 /*bmain*/ bmain,
504 /*id_remapper*/ id_remapper,
505 /*id_owner*/ nullptr,
506 /*flag*/ remap_flags,
507 };
508
509 const bool include_ui = (remap_flags & ID_REMAP_FORCE_UI_POINTERS) != 0;
510 const LibraryForeachIDFlag foreach_id_flags =
511 (((remap_flags & ID_REMAP_FORCE_INTERNAL_RUNTIME_POINTERS) != 0 ?
513 IDWALK_NOP) |
514 (include_ui ? IDWALK_INCLUDE_UI : IDWALK_NOP) |
515
517 IDWALK_NOP) |
519 IDWALK_NOP));
520
522
523 if (id) {
524#ifdef DEBUG_PRINT
525 printf("\tchecking id %s (%p, %p)\n", id->name, id, id->lib);
526#endif
527 id_remap_data.id_owner = (id->flag & ID_FLAG_EMBEDDED_DATA) ? BKE_id_owner_get(id) : id;
528 libblock_remap_data_preprocess(id_remap_data.id_owner, remap_type, id_remapper);
530 bmain, id, foreach_libblock_remap_callback, &id_remap_data, foreach_id_flags);
531 }
532 else {
533 /* Note that this is a very 'brute force' approach,
534 * maybe we could use some depsgraph to only process objects actually using given old_id...
535 * sounds rather unlikely currently, though, so this will do for now. */
536 ID *id_curr;
537
538 FOREACH_MAIN_ID_BEGIN (bmain, id_curr) {
539 const uint64_t can_use_filter_id = BKE_library_id_can_use_filter_id(id_curr, include_ui);
540 const bool has_mapping = id_remapper.contains_mappings_for_any(can_use_filter_id);
541
542 /* Continue when id_remapper doesn't have any mappings that can be used by id_curr. */
543 if (!has_mapping) {
544 continue;
545 }
546
547 /* Note that we cannot skip indirect usages of old_id
548 * here (if requested), we still need to check it for the
549 * user count handling...
550 * XXX No more true (except for debug usage of those
551 * skipping counters). */
552 id_remap_data.id_owner = id_curr;
553 libblock_remap_data_preprocess(id_remap_data.id_owner, remap_type, id_remapper);
555 bmain, id_curr, foreach_libblock_remap_callback, &id_remap_data, foreach_id_flags);
556 }
558 }
559
560 id_remapper.iter([&](ID *old_id, ID *new_id) {
561 libblock_remap_data_update_tags(old_id, new_id, &id_remap_data);
562 });
563}
564
565static void libblock_remap_foreach_idpair(ID *old_id, ID *new_id, Main *bmain, int remap_flags)
566{
567 if (old_id == new_id) {
568 return;
569 }
570
571 BLI_assert(old_id != nullptr);
572 BLI_assert((new_id == nullptr) || remap_flags & ID_REMAP_ALLOW_IDTYPE_MISMATCH ||
573 GS(old_id->name) == GS(new_id->name));
574
577 }
578
579 if ((remap_flags & ID_REMAP_SKIP_USER_CLEAR) == 0) {
580 /* If old_id was used by some ugly 'user_one' stuff (like Image or Clip editors...), and user
581 * count has actually been incremented for that, we have to decrease once more its user
582 * count... unless we had to skip some 'user_one' cases. */
583 if ((old_id->tag & ID_TAG_EXTRAUSER_SET) &&
584 !(old_id->runtime->remap.status & ID_REMAP_IS_USER_ONE_SKIPPED))
585 {
586 id_us_clear_real(old_id);
587 }
588 }
589
590 const int skipped_refcounted = old_id->runtime->remap.skipped_refcounted;
591 if (old_id->us - skipped_refcounted < 0) {
593 "Error in remapping process from '%s' (%p) to '%s' (%p): "
594 "wrong user count in old ID after process (summing up to %d)",
595 old_id->name,
596 old_id,
597 new_id ? new_id->name : "<nullptr>",
598 new_id,
599 old_id->us - skipped_refcounted);
600 }
601
602 const int skipped_direct = old_id->runtime->remap.skipped_direct;
603 if (skipped_direct == 0) {
604 /* old_id is assumed to not be used directly anymore... */
605 if (old_id->lib && (old_id->tag & ID_TAG_EXTERN)) {
606 old_id->tag &= ~ID_TAG_EXTERN;
607 old_id->tag |= ID_TAG_INDIRECT;
608 }
609 }
610
611 /* Some after-process updates.
612 * This is a bit ugly, but cannot see a way to avoid it.
613 * Maybe we should do a per-ID callback for this instead? */
614 switch (GS(old_id->name)) {
615 case ID_OB:
617 bmain, (Object *)old_id, (Object *)new_id, true);
618 break;
619 case ID_GR:
621 bmain, nullptr, (Collection *)old_id, (Collection *)new_id);
622 break;
623 case ID_ME:
624 case ID_CU_LEGACY:
625 case ID_MB:
626 case ID_CV:
627 case ID_PT:
628 case ID_VO:
629 if (new_id) { /* Only affects us in case obdata was relinked (changed). */
630 for (Object *ob = static_cast<Object *>(bmain->objects.first); ob;
631 ob = static_cast<Object *>(ob->id.next))
632 {
634 }
635 }
636 break;
637 default:
638 break;
639 }
640
641 /* Node trees may virtually use any kind of data-block... */
642 /* XXX Yuck!!!! nodetree update can do pretty much any thing when talking about py nodes,
643 * including creating new data-blocks (see #50385), so we need to unlock main here. :(
644 * Why can't we have re-entrent locks? */
645 BKE_main_unlock(bmain);
647 BKE_main_lock(bmain);
648
649 /* Full rebuild of DEG! */
651
653}
654
655void BKE_libblock_remap_multiple_locked(Main *bmain, IDRemapper &mappings, const int remap_flags)
656{
657 if (mappings.is_empty()) {
658 /* Early exit nothing to do. */
659 return;
660 }
661
662 libblock_remap_data(bmain, nullptr, ID_REMAP_TYPE_REMAP, mappings, remap_flags);
663
664 mappings.iter([&](ID *old_id, ID *new_id) {
665 libblock_remap_foreach_idpair(old_id, new_id, bmain, remap_flags);
666 });
667
668 /* We assume editors do not hold references to their IDs... This is false in some cases
669 * (Image is especially tricky here),
670 * editors' code is to handle refcount (id->us) itself then. */
673 }
674
675 /* Full rebuild of DEG! */
677}
678
679void BKE_libblock_remap_multiple_raw(Main *bmain, IDRemapper &mappings, const int remap_flags)
680{
681 if (mappings.is_empty()) {
682 /* Early exit nothing to do. */
683 return;
684 }
685
687 nullptr,
689 mappings,
691}
692
693void BKE_libblock_remap_locked(Main *bmain, void *old_idv, void *new_idv, const int remap_flags)
694{
695 IDRemapper remapper;
696 ID *old_id = static_cast<ID *>(old_idv);
697 ID *new_id = static_cast<ID *>(new_idv);
698 remapper.add(old_id, new_id);
699 BKE_libblock_remap_multiple_locked(bmain, remapper, remap_flags);
700}
701
702void BKE_libblock_remap(Main *bmain, void *old_idv, void *new_idv, const int remap_flags)
703{
704 BKE_main_lock(bmain);
705
706 BKE_libblock_remap_locked(bmain, old_idv, new_idv, remap_flags);
707
708 BKE_main_unlock(bmain);
709}
710
711void BKE_libblock_remap_multiple(Main *bmain, IDRemapper &mappings, const int remap_flags)
712{
713 BKE_main_lock(bmain);
714
715 BKE_libblock_remap_multiple_locked(bmain, mappings, remap_flags);
716
717 BKE_main_unlock(bmain);
718}
719
720void BKE_libblock_unlink(Main *bmain, void *idv, const bool do_skip_indirect)
721{
722 const int remap_flags = (do_skip_indirect ? ID_REMAP_SKIP_INDIRECT_USAGE : 0);
723
724 BKE_main_lock(bmain);
725
726 BKE_libblock_remap_locked(bmain, idv, nullptr, remap_flags);
727
728 BKE_main_unlock(bmain);
729}
730
731/* XXX Arg! Naming... :(
732 * _relink? avoids confusion with _remap, but is confusing with _unlink
733 * _remap_used_ids?
734 * _remap_datablocks?
735 * BKE_id_remap maybe?
736 * ... sigh
737 */
738
740 ID *new_id,
741 Main *bmain,
742 const blender::Span<ID *> ids)
743{
744 BLI_assert(old_id != nullptr);
745 BLI_assert((new_id == nullptr) || GS(old_id->name) == GS(new_id->name));
746 BLI_assert(old_id != new_id);
747
748 bool is_object_update_processed = false;
749 for (ID *id_iter : ids) {
750 /* Some after-process updates.
751 * This is a bit ugly, but cannot see a way to avoid it.
752 * Maybe we should do a per-ID callback for this instead?
753 */
754 switch (GS(id_iter->name)) {
755 case ID_SCE:
756 case ID_GR: {
757 /* NOTE: here we know which collection we have affected, so at lest for nullptr children
758 * detection we can only process that one.
759 * This is also a required fix in case `id` would not be in Main anymore, which can happen
760 * e.g. when called from `id_delete`. */
761 Collection *owner_collection = (GS(id_iter->name) == ID_GR) ?
762 (Collection *)id_iter :
763 ((Scene *)id_iter)->master_collection;
764 switch (GS(old_id->name)) {
765 case ID_OB:
766 if (!is_object_update_processed) {
768 bmain, (Object *)old_id, (Object *)new_id, true);
769 is_object_update_processed = true;
770 }
771 break;
772 case ID_GR:
774 bmain, owner_collection, (Collection *)old_id, (Collection *)new_id);
775 break;
776 default:
777 break;
778 }
779 break;
780 }
781 case ID_OB:
782 if (new_id != nullptr) { /* Only affects us in case obdata was relinked (changed). */
783 libblock_remap_data_postprocess_obdata_relink(bmain, (Object *)id_iter, new_id);
784 }
785 break;
786 default:
787 break;
788 }
789 }
790}
791
793 const blender::Span<ID *> ids,
794 const eIDRemapType remap_type,
795 IDRemapper &id_remapper,
796 const int remap_flags)
797{
798 BLI_assert(remap_type == ID_REMAP_TYPE_REMAP || id_remapper.is_empty());
799
800 for (ID *id_iter : ids) {
801 libblock_remap_data(bmain, id_iter, remap_type, id_remapper, remap_flags);
802 }
803
804 if (bmain == nullptr) {
805 return;
806 }
807
808 switch (remap_type) {
809 case ID_REMAP_TYPE_REMAP: {
810 id_remapper.iter([&](ID *old_id, ID *new_id) {
811 libblock_relink_foreach_idpair(old_id, new_id, bmain, ids);
812 });
813 break;
814 }
816 bool is_object_update_processed = false;
817 for (ID *id_iter : ids) {
818 switch (GS(id_iter->name)) {
819 case ID_SCE:
820 case ID_GR: {
821 /* NOTE: here we know which collection we have affected, so at lest for nullptr
822 * children detection we can only process that one. This is also a required fix in case
823 * `id` would not be in Main anymore, which can happen e.g. when called from
824 * `id_delete`. */
825 Collection *owner_collection = (GS(id_iter->name) == ID_GR) ?
826 (Collection *)id_iter :
827 ((Scene *)id_iter)->master_collection;
828 /* No choice but to check whole objects once, and all children collections. */
829 if (!is_object_update_processed) {
830 /* We only want to affect Object pointers here, not Collection ones, LayerCollections
831 * will be resynced as part of the call to
832 * `libblock_remap_data_postprocess_collection_update` below. */
833 libblock_remap_data_postprocess_object_update(bmain, nullptr, nullptr, false);
834 is_object_update_processed = true;
835 }
837 bmain, owner_collection, nullptr, nullptr);
838 break;
839 }
840 default:
841 break;
842 }
843 }
844
845 break;
846 }
847 default:
849 }
850
852}
853
855 Main *bmain, void *idv, void *old_idv, void *new_idv, const int remap_flags)
856{
857
858 /* Should be able to replace all _relink() functions (constraints, rigidbody, etc.) ? */
859
860 ID *id = static_cast<ID *>(idv);
861 ID *old_id = static_cast<ID *>(old_idv);
862 ID *new_id = static_cast<ID *>(new_idv);
863 blender::Array<ID *> ids = {id};
864
865 /* No need to lock here, we are only affecting given ID, not bmain database. */
866 IDRemapper id_remapper;
868
869 BLI_assert(id != nullptr);
871 if (old_id != nullptr) {
872 BLI_assert((new_id == nullptr) || GS(old_id->name) == GS(new_id->name));
873 BLI_assert(old_id != new_id);
874 id_remapper.add(old_id, new_id);
875 }
876 else {
877 BLI_assert(new_id == nullptr);
878 remap_type = ID_REMAP_TYPE_CLEANUP;
879 }
880
881 BKE_libblock_relink_multiple(bmain, ids, remap_type, id_remapper, remap_flags);
882}
883
888
890 ID *id,
891 RelinkToNewIDData *relink_data);
893{
894 const LibraryForeachIDCallbackFlag cb_flag = cb_data->cb_flag;
895 /* NOTE: For now, support remapping `IDWALK_CB_EMBEDDED_NON_OWNING` pointers. */
897 return IDWALK_RET_NOP;
898 }
899
900 Main *bmain = cb_data->bmain;
901 ID **id_pointer = cb_data->id_pointer;
902 ID *id = *id_pointer;
903 RelinkToNewIDData *relink_data = static_cast<RelinkToNewIDData *>(cb_data->user_data);
904
905 if (id) {
906 /* See: #ID_NEW_SET macro. */
907 if (id->newid != nullptr) {
908 relink_data->id_remapper.add(id, id->newid);
909 id = id->newid;
910 }
911 if (id->tag & ID_TAG_NEW) {
912 libblock_relink_to_newid_prepare_data(bmain, id, relink_data);
913 }
914 }
915 return IDWALK_RET_NOP;
916}
917
919 ID *id,
920 RelinkToNewIDData *relink_data)
921{
922 if (ID_IS_LINKED(id)) {
923 return;
924 }
925
926 id->tag &= ~ID_TAG_NEW;
927 relink_data->ids.append(id);
929}
930
931void BKE_libblock_relink_to_newid(Main *bmain, ID *id, const int remap_flag)
932{
933 if (ID_IS_LINKED(id)) {
934 return;
935 }
936 /* We do not want to have those cached relationship data here. */
937 BLI_assert(bmain->relations == nullptr);
938
939 RelinkToNewIDData relink_data{};
940
941 libblock_relink_to_newid_prepare_data(bmain, id, &relink_data);
942
943 const int remap_flag_final = remap_flag | ID_REMAP_SKIP_INDIRECT_USAGE |
946 bmain, relink_data.ids, ID_REMAP_TYPE_REMAP, relink_data.id_remapper, remap_flag_final);
947}
void BKE_pose_clear_pointers(bPose *pose)
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:454
void BKE_main_collection_sync_remap(const Main *bmain)
void id_fake_user_set(ID *id)
Definition lib_id.cc:396
ID * BKE_id_owner_get(ID *id, const bool debug_relationship_assert=true)
Definition lib_id.cc:2511
void id_us_ensure_real(ID *id)
Definition lib_id.cc:313
void id_fake_user_clear(ID *id)
Definition lib_id.cc:404
void id_us_clear_real(ID *id)
Definition lib_id.cc:331
void id_us_plus_no_lib(ID *id)
Definition lib_id.cc:342
void BKE_libblock_runtime_reset_remapping_status(ID *id) ATTR_NONNULL(1)
Definition lib_id.cc:1466
void id_us_min(ID *id)
Definition lib_id.cc:366
@ IDWALK_RET_NOP
LibraryForeachIDCallbackFlag
@ IDWALK_CB_LOOPBACK
@ 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
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
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 blender::bke::id::IDRemapper &mappings) BKE_library_remap_editor_id_reference_cb
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:583
void BKE_main_lock(Main *bmain)
Definition main.cc:486
#define FOREACH_MAIN_ID_BEGIN(_bmain, _id)
Definition BKE_main.hh:577
void BKE_main_unlock(Main *bmain)
Definition main.cc:491
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:255
bool BKE_mball_is_basis_for(const Object *ob1, const Object *ob2)
Definition mball.cc:294
void BKE_modifiers_test_object(Object *ob)
void multires_force_sculpt_rebuild(Object *object)
Definition multires.cc:327
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:188
void DEG_id_tag_update(ID *id, unsigned int flags)
void DEG_relations_tag_update(Main *bmain)
@ ID_TAG_NEW
Definition DNA_ID.h:919
@ ID_TAG_INDIRECT
Definition DNA_ID.h:848
@ ID_TAG_EXTERN
Definition DNA_ID.h:842
@ ID_TAG_EXTRAUSER_SET
Definition DNA_ID.h:884
@ ID_TAG_NO_MAIN
Definition DNA_ID.h:978
@ 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
#define ID_IS_LINKED(_id)
Definition DNA_ID.h:694
#define FILTER_ID_AR
Definition DNA_ID.h:1198
@ ID_REMAP_IS_LINKED_DIRECT
Definition DNA_ID.h:378
@ ID_REMAP_IS_USER_ONE_SKIPPED
Definition DNA_ID.h:380
@ ID_FLAG_INDIRECT_WEAK_LINK
Definition DNA_ID.h:780
@ ID_FLAG_FAKEUSER
Definition DNA_ID.h:769
@ ID_FLAG_EMBEDDED_DATA
Definition DNA_ID.h:774
@ 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 GS(x)
#define printf(...)
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:309
void BKE_libblock_relink_to_newid(Main *bmain, ID *id, const int remap_flag)
Definition lib_remap.cc:931
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:892
static void libblock_remap_data_postprocess_collection_update(Main *bmain, Collection *owner_collection, Collection *, Collection *new_collection)
Definition lib_remap.cc:397
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:498
void BKE_libblock_remap_multiple(Main *bmain, IDRemapper &mappings, const int remap_flags)
Definition lib_remap.cc:711
static void libblock_remap_data_preprocess(ID *id_owner, eIDRemapType remap_type, const IDRemapper &id_remapper)
Definition lib_remap.cc:342
static void libblock_relink_foreach_idpair(ID *old_id, ID *new_id, Main *bmain, const blender::Span< ID * > ids)
Definition lib_remap.cc:739
static void libblock_relink_to_newid_prepare_data(Main *bmain, ID *id, RelinkToNewIDData *relink_data)
Definition lib_remap.cc:918
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:854
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:702
void BKE_libblock_unlink(Main *bmain, void *idv, const bool do_skip_indirect)
Definition lib_remap.cc:720
static void libblock_remap_data_update_tags(ID *old_id, ID *new_id, IDRemap *id_remap_data)
Definition lib_remap.cc:443
void BKE_libblock_remap_multiple_raw(Main *bmain, IDRemapper &mappings, const int remap_flags)
Definition lib_remap.cc:679
static void libblock_remap_data_postprocess_obdata_relink(Main *bmain, Object *ob, ID *new_id)
Definition lib_remap.cc:419
static void libblock_remap_data_postprocess_object_update(Main *bmain, Object *old_ob, Object *, const bool do_sync_collection)
Definition lib_remap.cc:361
void BKE_libblock_remap_locked(Main *bmain, void *old_idv, void *new_idv, const int remap_flags)
Definition lib_remap.cc:693
void BKE_libblock_remap_multiple_locked(Main *bmain, IDRemapper &mappings, const int remap_flags)
Definition lib_remap.cc:655
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:792
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:467
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:437
static void libblock_remap_foreach_idpair(ID *old_id, ID *new_id, Main *bmain, int remap_flags)
Definition lib_remap.cc:565
#define LOG(level)
Definition log.h:97
void node_tree_update_all_users(Main *main, ID *id)
Definition node.cc:4945
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
Definition DNA_ID.h:414
int tag
Definition DNA_ID.h:442
ID_RuntimeHandle * runtime
Definition DNA_ID.h:538
struct Library * lib
Definition DNA_ID.h:420
char name[258]
Definition DNA_ID.h:432
int us
Definition DNA_ID.h:443
short flag
Definition DNA_ID.h:438
LibraryForeachIDCallbackFlag cb_flag
void * first
MainIDRelations * relations
Definition BKE_main.hh:329
ListBase objects
Definition BKE_main.hh:280
struct bPose * pose
IDRemapper id_remapper
Definition lib_remap.cc:886
blender::Vector< ID * > ids
Definition lib_remap.cc:885