1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
17 /** \file
18  * \ingroup bke
19  *
20  * Contains management of ID's for remapping.
21  */
22 
23 #include "CLG_log.h"
24 
25 #include "BLI_utildefines.h"
26 
27 #include "DNA_object_types.h"
28 
29 #include "BKE_armature.h"
30 #include "BKE_collection.h"
31 #include "BKE_curve.h"
32 #include "BKE_layer.h"
33 #include "BKE_lib_id.h"
34 #include "BKE_lib_query.h"
35 #include "BKE_lib_remap.h"
36 #include "BKE_main.h"
37 #include "BKE_material.h"
38 #include "BKE_mball.h"
39 #include "BKE_modifier.h"
40 #include "BKE_multires.h"
41 #include "BKE_node.h"
42 #include "BKE_object.h"
43 
44 #include "DEG_depsgraph.h"
45 #include "DEG_depsgraph_build.h"
46 
47 #include "lib_intern.h" /* own include */
48 
49 static CLG_LogRef LOG = {.identifier = "bke.lib_remap"};
50 
51 BKE_library_free_notifier_reference_cb free_notifier_reference_cb = NULL;
52 
BKE_library_callback_free_notifier_reference_set(BKE_library_free_notifier_reference_cb func)53 void BKE_library_callback_free_notifier_reference_set(BKE_library_free_notifier_reference_cb func)
54 {
55   free_notifier_reference_cb = func;
56 }
57 
58 BKE_library_remap_editor_id_reference_cb remap_editor_id_reference_cb = NULL;
59 
BKE_library_callback_remap_editor_id_reference_set(BKE_library_remap_editor_id_reference_cb func)60 void BKE_library_callback_remap_editor_id_reference_set(
61     BKE_library_remap_editor_id_reference_cb func)
62 {
63   remap_editor_id_reference_cb = func;
64 }
65 
66 typedef struct IDRemap {
67   Main *bmain; /* Only used to trigger depsgraph updates in the right bmain. */
68   ID *old_id;
69   ID *new_id;
70   /** The ID in which we are replacing old_id by new_id usages. */
71   ID *id_owner;
72   short flag;
73 
74   /* 'Output' data. */
75   short status;
76   /** Number of direct use cases that could not be remapped (e.g.: obdata when in edit mode). */
77   int skipped_direct;
78   /** Number of indirect use cases that could not be remapped. */
79   int skipped_indirect;
80   /** Number of skipped use cases that refcount the data-block. */
81   int skipped_refcounted;
82 } IDRemap;
83 
84 /* IDRemap->flag enums defined in BKE_lib.h */
85 
86 /* IDRemap->status */
87 enum {
88   /* *** Set by callback. *** */
89   ID_REMAP_IS_LINKED_DIRECT = 1 << 0,    /* new_id is directly linked in current .blend. */
90   ID_REMAP_IS_USER_ONE_SKIPPED = 1 << 1, /* There was some skipped 'user_one' usages of old_id. */
91 };
92 
foreach_libblock_remap_callback(LibraryIDLinkCallbackData * cb_data)93 static int foreach_libblock_remap_callback(LibraryIDLinkCallbackData *cb_data)
94 {
95   const int cb_flag = cb_data->cb_flag;
96 
97   if (cb_flag & IDWALK_CB_EMBEDDED) {
98     return IDWALK_RET_NOP;
99   }
100 
101   ID *id_owner = cb_data->id_owner;
102   ID *id_self = cb_data->id_self;
103   ID **id_p = cb_data->id_pointer;
104   IDRemap *id_remap_data = cb_data->user_data;
105   ID *old_id = id_remap_data->old_id;
106   ID *new_id = id_remap_data->new_id;
107 
108   /* Those asserts ensure the general sanity of ID tags regarding 'embedded' ID data (root
109    * nodetrees and co). */
110   BLI_assert(id_owner == id_remap_data->id_owner);
111   BLI_assert(id_self == id_owner || (id_self->flag & LIB_EMBEDDED_DATA) != 0);
112 
113   if (!old_id) { /* Used to cleanup all IDs used by a specific one. */
114     BLI_assert(!new_id);
115     old_id = *id_p;
116   }
117 
118   if (*id_p && (*id_p == old_id)) {
119     /* Better remap to NULL than not remapping at all,
120      * then we can handle it as a regular remap-to-NULL case. */
121     if ((cb_flag & IDWALK_CB_NEVER_SELF) && (new_id == id_self)) {
122       new_id = NULL;
123     }
124 
125     const bool is_reference = (cb_flag & IDWALK_CB_OVERRIDE_LIBRARY_REFERENCE) != 0;
126     const bool is_indirect = (cb_flag & IDWALK_CB_INDIRECT_USAGE) != 0;
127     const bool skip_indirect = (id_remap_data->flag & ID_REMAP_SKIP_INDIRECT_USAGE) != 0;
128     /* Note: proxy usage implies LIB_TAG_EXTERN, so on this aspect it is direct,
129      * on the other hand since they get reset to lib data on file open/reload it is indirect too.
130      * Edit Mode is also a 'skip direct' case. */
131     const bool is_obj = (GS(id_owner->name) == ID_OB);
132     const bool is_obj_proxy = (is_obj &&
133                                (((Object *)id_owner)->proxy || ((Object *)id_owner)->proxy_group));
134     const bool is_obj_editmode = (is_obj && BKE_object_is_in_editmode((Object *)id_owner));
135     const bool is_never_null = ((cb_flag & IDWALK_CB_NEVER_NULL) && (new_id == NULL) &&
136                                 (id_remap_data->flag & ID_REMAP_FORCE_NEVER_NULL_USAGE) == 0);
137     const bool skip_reference = (id_remap_data->flag & ID_REMAP_SKIP_OVERRIDE_LIBRARY) != 0;
138     const bool skip_never_null = (id_remap_data->flag & ID_REMAP_SKIP_NEVER_NULL_USAGE) != 0;
139 
140 #ifdef DEBUG_PRINT
141     printf(
142         "In %s (lib %p): Remapping %s (%p) to %s (%p) "
143         "(is_indirect: %d, skip_indirect: %d, is_reference: %d, skip_reference: %d)\n",
144         id->name,
145         id->lib,
146         old_id->name,
147         old_id,
148         new_id ? new_id->name : "<NONE>",
149         new_id,
150         is_indirect,
151         skip_indirect,
152         is_reference,
153         skip_reference);
154 #endif
155 
156     if ((id_remap_data->flag & ID_REMAP_FLAG_NEVER_NULL_USAGE) &&
157         (cb_flag & IDWALK_CB_NEVER_NULL)) {
158       id_owner->tag |= LIB_TAG_DOIT;
159     }
160 
161     /* Special hack in case it's Object->data and we are in edit mode, and new_id is not NULL
162      * (otherwise, we follow common NEVER_NULL flags).
163      * (skipped_indirect too). */
164     if ((is_never_null && skip_never_null) ||
165         (is_obj_editmode && (((Object *)id_owner)->data == *id_p) && new_id != NULL) ||
166         (skip_indirect && is_indirect) || (is_reference && skip_reference)) {
167       if (is_indirect) {
168         id_remap_data->skipped_indirect++;
169         if (is_obj) {
170           Object *ob = (Object *)id_owner;
171           if (ob->data == *id_p && ob->proxy != NULL) {
172             /* And another 'Proudly brought to you by Proxy Hell' hack!
173              * This will allow us to avoid clearing 'LIB_EXTERN' flag of obdata of proxies... */
174             id_remap_data->skipped_direct++;
175           }
176         }
177       }
178       else if (is_never_null || is_obj_editmode || is_reference) {
179         id_remap_data->skipped_direct++;
180       }
181       else {
182         BLI_assert(0);
183       }
184       if (cb_flag & IDWALK_CB_USER) {
185         id_remap_data->skipped_refcounted++;
186       }
187       else if (cb_flag & IDWALK_CB_USER_ONE) {
188         /* No need to count number of times this happens, just a flag is enough. */
189         id_remap_data->status |= ID_REMAP_IS_USER_ONE_SKIPPED;
190       }
191     }
192     else {
193       if (!is_never_null) {
194         *id_p = new_id;
195         DEG_id_tag_update_ex(id_remap_data->bmain,
196                              id_self,
197                              ID_RECALC_COPY_ON_WRITE | ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY);
198         if (id_self != id_owner) {
199           DEG_id_tag_update_ex(id_remap_data->bmain,
200                                id_owner,
201                                ID_RECALC_COPY_ON_WRITE | ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY);
202         }
203       }
204       if (cb_flag & IDWALK_CB_USER) {
205         /* NOTE: We don't user-count IDs which are not in the main database.
206          * This is because in certain conditions we can have data-blocks in
207          * the main which are referencing data-blocks outside of it.
208          * For example, BKE_mesh_new_from_object() called on an evaluated
209          * object will cause such situation.
210          */
211         if ((old_id->tag & LIB_TAG_NO_MAIN) == 0) {
212           id_us_min(old_id);
213         }
214         if (new_id != NULL && (new_id->tag & LIB_TAG_NO_MAIN) == 0) {
215           /* We do not want to handle LIB_TAG_INDIRECT/LIB_TAG_EXTERN here. */
216           new_id->us++;
217         }
218       }
219       else if (cb_flag & IDWALK_CB_USER_ONE) {
220         id_us_ensure_real(new_id);
221         /* We cannot affect old_id->us directly, LIB_TAG_EXTRAUSER(_SET)
222          * are assumed to be set as needed, that extra user is processed in final handling. */
223       }
224       if (!is_indirect || is_obj_proxy) {
225         id_remap_data->status |= ID_REMAP_IS_LINKED_DIRECT;
226       }
227       /* We need to remap proxy_from pointer of remapped proxy... sigh. */
228       if (is_obj_proxy && new_id != NULL) {
229         Object *ob = (Object *)id_owner;
230         if (ob->proxy == (Object *)new_id) {
231           ob->proxy->proxy_from = ob;
232         }
233       }
234     }
235   }
236 
237   return IDWALK_RET_NOP;
238 }
239 
libblock_remap_data_preprocess(IDRemap * r_id_remap_data)240 static void libblock_remap_data_preprocess(IDRemap *r_id_remap_data)
241 {
242   switch (GS(r_id_remap_data->id_owner->name)) {
243     case ID_OB: {
244       ID *old_id = r_id_remap_data->old_id;
245       if (!old_id || GS(old_id->name) == ID_AR) {
246         Object *ob = (Object *)r_id_remap_data->id_owner;
247         /* Object's pose holds reference to armature bones. sic */
248         /* Note that in theory, we should have to bother about linked/non-linked/never-null/etc.
249          * flags/states.
250          * Fortunately, this is just a tag, so we can accept to 'over-tag' a bit for pose recalc,
251          * and avoid another complex and risky condition nightmare like the one we have in
252          * foreach_libblock_remap_callback(). */
253         if (ob->pose && (!old_id || ob->data == old_id)) {
254           BLI_assert(ob->type == OB_ARMATURE);
255           ob->pose->flag |= POSE_RECALC;
256           /* We need to clear pose bone pointers immediately, some code may access those before
257            * pose is actually recomputed, which can lead to segfault. */
258           BKE_pose_clear_pointers(ob->pose);
259         }
260       }
261       break;
262     }
263     default:
264       break;
265   }
266 }
267 
268 /**
269  * Can be called with both old_ob and new_ob being NULL,
270  * this means we have to check whole Main database then.
271  */
libblock_remap_data_postprocess_object_update(Main * bmain,Object * old_ob,Object * new_ob)272 static void libblock_remap_data_postprocess_object_update(Main *bmain,
273                                                           Object *old_ob,
274                                                           Object *new_ob)
275 {
276   if (new_ob == NULL) {
277     /* In case we unlinked old_ob (new_ob is NULL), the object has already
278      * been removed from the scenes and their collections. We still have
279      * to remove the NULL children from collections not used in any scene. */
280     BKE_collections_object_remove_nulls(bmain);
281   }
282 
283   BKE_main_collection_sync_remap(bmain);
284 
285   if (old_ob == NULL) {
286     for (Object *ob = bmain->objects.first; ob != NULL; ob = ob->id.next) {
287       if (ob->type == OB_MBALL && BKE_mball_is_basis(ob)) {
288         DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY);
289       }
290     }
291   }
292   else {
293     for (Object *ob = bmain->objects.first; ob != NULL; ob = ob->id.next) {
294       if (ob->type == OB_MBALL && BKE_mball_is_basis_for(ob, old_ob)) {
295         DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY);
296         break; /* There is only one basis... */
297       }
298     }
299   }
300 }
301 
302 /* Can be called with both old_collection and new_collection being NULL,
303  * this means we have to check whole Main database then. */
libblock_remap_data_postprocess_collection_update(Main * bmain,Collection * UNUSED (old_collection),Collection * new_collection)304 static void libblock_remap_data_postprocess_collection_update(Main *bmain,
305                                                               Collection *UNUSED(old_collection),
306                                                               Collection *new_collection)
307 {
308   if (new_collection == NULL) {
309     /* XXX Complex cases can lead to NULL pointers in other collections than old_collection,
310      * and BKE_main_collection_sync_remap() does not tolerate any of those, so for now always check
311      * whole existing collections for NULL pointers.
312      * I'd consider optimizing that whole collection remapping process a TODO for later. */
313     BKE_collections_child_remove_nulls(bmain, NULL /*old_collection*/);
314   }
315   else {
316     /* Temp safe fix, but a "tad" brute force... We should probably be able to use parents from
317      * old_collection instead? */
318     BKE_main_collections_parent_relations_rebuild(bmain);
319   }
320 
321   BKE_main_collection_sync_remap(bmain);
322 }
323 
libblock_remap_data_postprocess_obdata_relink(Main * bmain,Object * ob,ID * new_id)324 static void libblock_remap_data_postprocess_obdata_relink(Main *bmain, Object *ob, ID *new_id)
325 {
326   if (ob->data == new_id) {
327     switch (GS(new_id->name)) {
328       case ID_ME:
329         multires_force_sculpt_rebuild(ob);
330         break;
331       case ID_CU:
332         BKE_curve_type_test(ob);
333         break;
334       default:
335         break;
336     }
337     BKE_modifiers_test_object(ob);
338     BKE_object_materials_test(bmain, ob, new_id);
339   }
340 }
341 
libblock_remap_data_postprocess_nodetree_update(Main * bmain,ID * new_id)342 static void libblock_remap_data_postprocess_nodetree_update(Main *bmain, ID *new_id)
343 {
344   /* Update all group nodes using a node group. */
345   ntreeUpdateAllUsers(bmain, new_id);
346 }
347 
348 /**
349  * Execute the 'data' part of the remapping (that is, all ID pointers from other ID data-blocks).
350  *
351  * Behavior differs depending on whether given \a id is NULL or not:
352  * - \a id NULL: \a old_id must be non-NULL, \a new_id may be NULL (unlinking \a old_id) or not
353  *   (remapping \a old_id to \a new_id).
354  *   The whole \a bmain database is checked, and all pointers to \a old_id
355  *   are remapped to \a new_id.
356  * - \a id is non-NULL:
357  *   + If \a old_id is NULL, \a new_id must also be NULL,
358  *     and all ID pointers from \a id are cleared
359  *     (i.e. \a id does not references any other data-block anymore).
360  *   + If \a old_id is non-NULL, behavior is as with a NULL \a id, but only within given \a id.
361  *
362  * \param bmain: the Main data storage to operate on (must never be NULL).
363  * \param id: the data-block to operate on
364  * (can be NULL, in which case we operate over all IDs from given bmain).
365  * \param old_id: the data-block to dereference (may be NULL if \a id is non-NULL).
366  * \param new_id: the new data-block to replace \a old_id references with (may be NULL).
367  * \param r_id_remap_data: if non-NULL, the IDRemap struct to use
368  * (uselful to retrieve info about remapping process).
369  */
370 ATTR_NONNULL(1)
libblock_remap_data(Main * bmain,ID * id,ID * old_id,ID * new_id,const short remap_flags,IDRemap * r_id_remap_data)371 static void libblock_remap_data(
372     Main *bmain, ID *id, ID *old_id, ID *new_id, const short remap_flags, IDRemap *r_id_remap_data)
373 {
374   IDRemap id_remap_data;
375   const int foreach_id_flags = (remap_flags & ID_REMAP_NO_INDIRECT_PROXY_DATA_USAGE) != 0 ?
376                                    IDWALK_NO_INDIRECT_PROXY_DATA_USAGE :
377                                    IDWALK_NOP;
378 
379   if (r_id_remap_data == NULL) {
380     r_id_remap_data = &id_remap_data;
381   }
382   r_id_remap_data->bmain = bmain;
383   r_id_remap_data->old_id = old_id;
384   r_id_remap_data->new_id = new_id;
385   r_id_remap_data->id_owner = NULL;
386   r_id_remap_data->flag = remap_flags;
387   r_id_remap_data->status = 0;
388   r_id_remap_data->skipped_direct = 0;
389   r_id_remap_data->skipped_indirect = 0;
390   r_id_remap_data->skipped_refcounted = 0;
391 
392   if (id) {
393 #ifdef DEBUG_PRINT
394     printf("\tchecking id %s (%p, %p)\n", id->name, id, id->lib);
395 #endif
396     r_id_remap_data->id_owner = id;
397     libblock_remap_data_preprocess(r_id_remap_data);
398     BKE_library_foreach_ID_link(
399         NULL, id, foreach_libblock_remap_callback, (void *)r_id_remap_data, foreach_id_flags);
400   }
401   else {
402     /* Note that this is a very 'brute force' approach,
403      * maybe we could use some depsgraph to only process objects actually using given old_id...
404      * sounds rather unlikely currently, though, so this will do for now. */
405     ID *id_curr;
406 
407     FOREACH_MAIN_ID_BEGIN (bmain, id_curr) {
408       if (BKE_library_id_can_use_idtype(id_curr, GS(old_id->name))) {
409         /* Note that we cannot skip indirect usages of old_id here (if requested),
410          * we still need to check it for the user count handling...
411          * XXX No more true (except for debug usage of those skipping counters). */
412         r_id_remap_data->id_owner = id_curr;
413         libblock_remap_data_preprocess(r_id_remap_data);
414         BKE_library_foreach_ID_link(NULL,
415                                     id_curr,
416                                     foreach_libblock_remap_callback,
417                                     (void *)r_id_remap_data,
418                                     foreach_id_flags);
419       }
420     }
421     FOREACH_MAIN_ID_END;
422   }
423 
424   /* XXX We may not want to always 'transfer' fake-user from old to new id...
425    *     Think for now it's desired behavior though,
426    *     we can always add an option (flag) to control this later if needed. */
427   if (old_id && (old_id->flag & LIB_FAKEUSER)) {
428     id_fake_user_clear(old_id);
429     id_fake_user_set(new_id);
430   }
431 
432   id_us_clear_real(old_id);
433 
434   if (new_id && (new_id->tag & LIB_TAG_INDIRECT) &&
435       (r_id_remap_data->status & ID_REMAP_IS_LINKED_DIRECT)) {
436     new_id->tag &= ~LIB_TAG_INDIRECT;
437     new_id->flag &= ~LIB_INDIRECT_WEAK_LINK;
438     new_id->tag |= LIB_TAG_EXTERN;
439   }
440 
441 #ifdef DEBUG_PRINT
442   printf("%s: %d occurrences skipped (%d direct and %d indirect ones)\n",
443          __func__,
444          r_id_remap_data->skipped_direct + r_id_remap_data->skipped_indirect,
445          r_id_remap_data->skipped_direct,
446          r_id_remap_data->skipped_indirect);
447 #endif
448 }
449 
450 /**
451  * Replace all references in given Main to \a old_id by \a new_id
452  * (if \a new_id is NULL, it unlinks \a old_id).
453  */
BKE_libblock_remap_locked(Main * bmain,void * old_idv,void * new_idv,const short remap_flags)454 void BKE_libblock_remap_locked(Main *bmain, void *old_idv, void *new_idv, const short remap_flags)
455 {
456   IDRemap id_remap_data;
457   ID *old_id = old_idv;
458   ID *new_id = new_idv;
459   int skipped_direct, skipped_refcounted;
460 
461   BLI_assert(old_id != NULL);
462   BLI_assert((new_id == NULL) || GS(old_id->name) == GS(new_id->name));
463   BLI_assert(old_id != new_id);
464 
465   libblock_remap_data(bmain, NULL, old_id, new_id, remap_flags, &id_remap_data);
466 
467   if (free_notifier_reference_cb) {
468     free_notifier_reference_cb(old_id);
469   }
470 
471   /* We assume editors do not hold references to their IDs... This is false in some cases
472    * (Image is especially tricky here),
473    * editors' code is to handle refcount (id->us) itself then. */
474   if (remap_editor_id_reference_cb) {
475     remap_editor_id_reference_cb(old_id, new_id);
476   }
477 
478   skipped_direct = id_remap_data.skipped_direct;
479   skipped_refcounted = id_remap_data.skipped_refcounted;
480 
481   /* If old_id was used by some ugly 'user_one' stuff (like Image or Clip editors...), and user
482    * count has actually been incremented for that, we have to decrease once more its user count...
483    * unless we had to skip some 'user_one' cases. */
484   if ((old_id->tag & LIB_TAG_EXTRAUSER_SET) &&
485       !(id_remap_data.status & ID_REMAP_IS_USER_ONE_SKIPPED)) {
486     id_us_clear_real(old_id);
487   }
488 
489   if (old_id->us - skipped_refcounted < 0) {
490     CLOG_ERROR(&LOG,
491                "Error in remapping process from '%s' (%p) to '%s' (%p): "
492                "wrong user count in old ID after process (summing up to %d)",
493                old_id->name,
494                old_id,
495                new_id ? new_id->name : "<NULL>",
496                new_id,
497                old_id->us - skipped_refcounted);
498     BLI_assert(0);
499   }
500 
501   if (skipped_direct == 0) {
502     /* old_id is assumed to not be used directly anymore... */
503     if (old_id->lib && (old_id->tag & LIB_TAG_EXTERN)) {
504       old_id->tag &= ~LIB_TAG_EXTERN;
505       old_id->tag |= LIB_TAG_INDIRECT;
506     }
507   }
508 
509   /* Some after-process updates.
510    * This is a bit ugly, but cannot see a way to avoid it.
511    * Maybe we should do a per-ID callback for this instead? */
512   switch (GS(old_id->name)) {
513     case ID_OB:
514       libblock_remap_data_postprocess_object_update(bmain, (Object *)old_id, (Object *)new_id);
515       break;
516     case ID_GR:
517       libblock_remap_data_postprocess_collection_update(
518           bmain, (Collection *)old_id, (Collection *)new_id);
519       break;
520     case ID_ME:
521     case ID_CU:
522     case ID_MB:
523     case ID_HA:
524     case ID_PT:
525     case ID_VO:
526       if (new_id) { /* Only affects us in case obdata was relinked (changed). */
527         for (Object *ob = bmain->objects.first; ob; ob = ob->id.next) {
528           libblock_remap_data_postprocess_obdata_relink(bmain, ob, new_id);
529         }
530       }
531       break;
532     default:
533       break;
534   }
535 
536   /* Node trees may virtually use any kind of data-block... */
537   /* XXX Yuck!!!! nodetree update can do pretty much any thing when talking about py nodes,
538    *     including creating new data-blocks (see T50385), so we need to unlock main here. :(
539    *     Why can't we have re-entrent locks? */
540   BKE_main_unlock(bmain);
541   libblock_remap_data_postprocess_nodetree_update(bmain, new_id);
542   BKE_main_lock(bmain);
543 
544   /* Full rebuild of DEG! */
545   DEG_relations_tag_update(bmain);
546 }
547 
BKE_libblock_remap(Main * bmain,void * old_idv,void * new_idv,const short remap_flags)548 void BKE_libblock_remap(Main *bmain, void *old_idv, void *new_idv, const short remap_flags)
549 {
550   BKE_main_lock(bmain);
551 
552   BKE_libblock_remap_locked(bmain, old_idv, new_idv, remap_flags);
553 
554   BKE_main_unlock(bmain);
555 }
556 
557 /**
558  * Unlink given \a id from given \a bmain
559  * (does not touch to indirect, i.e. library, usages of the ID).
560  *
561  * \param do_flag_never_null: If true, all IDs using \a idv in a 'non-NULL' way are flagged by
562  * #LIB_TAG_DOIT flag (quite obviously, 'non-NULL' usages can never be unlinked by this function).
563  */
BKE_libblock_unlink(Main * bmain,void * idv,const bool do_flag_never_null,const bool do_skip_indirect)564 void BKE_libblock_unlink(Main *bmain,
565                          void *idv,
566                          const bool do_flag_never_null,
567                          const bool do_skip_indirect)
568 {
569   const short remap_flags = (do_skip_indirect ? ID_REMAP_SKIP_INDIRECT_USAGE : 0) |
570                             (do_flag_never_null ? ID_REMAP_FLAG_NEVER_NULL_USAGE : 0);
571 
572   BKE_main_lock(bmain);
573 
574   BKE_libblock_remap_locked(bmain, idv, NULL, remap_flags);
575 
576   BKE_main_unlock(bmain);
577 }
578 
579 /**
580  * Similar to libblock_remap, but only affects IDs used by given \a idv ID.
581  *
582  * \param old_idv: Unlike BKE_libblock_remap, can be NULL,
583  * in which case all ID usages by given \a idv will be cleared.
584  * \param us_min_never_null: If \a true and new_id is NULL,
585  * 'NEVER_NULL' ID usages keep their old id, but this one still gets its user count decremented
586  * (needed when given \a idv is going to be deleted right after being unlinked).
587  */
588 /* Should be able to replace all _relink() funcs (constraints, rigidbody, etc.) ? */
589 /* XXX Arg! Naming... :(
590  *     _relink? avoids confusion with _remap, but is confusing with _unlink
591  *     _remap_used_ids?
592  *     _remap_datablocks?
593  *     BKE_id_remap maybe?
594  *     ... sigh
595  */
BKE_libblock_relink_ex(Main * bmain,void * idv,void * old_idv,void * new_idv,const short remap_flags)596 void BKE_libblock_relink_ex(
597     Main *bmain, void *idv, void *old_idv, void *new_idv, const short remap_flags)
598 {
599   ID *id = idv;
600   ID *old_id = old_idv;
601   ID *new_id = new_idv;
602 
603   /* No need to lock here, we are only affecting given ID, not bmain database. */
604 
605   BLI_assert(id);
606   if (old_id) {
607     BLI_assert((new_id == NULL) || GS(old_id->name) == GS(new_id->name));
608     BLI_assert(old_id != new_id);
609   }
610   else {
611     BLI_assert(new_id == NULL);
612   }
613 
614   libblock_remap_data(bmain, id, old_id, new_id, remap_flags, NULL);
615 
616   /* Some after-process updates.
617    * This is a bit ugly, but cannot see a way to avoid it.
618    * Maybe we should do a per-ID callback for this instead?
619    */
620   switch (GS(id->name)) {
621     case ID_SCE:
622     case ID_GR: {
623       if (old_id) {
624         switch (GS(old_id->name)) {
625           case ID_OB:
626             libblock_remap_data_postprocess_object_update(
627                 bmain, (Object *)old_id, (Object *)new_id);
628             break;
629           case ID_GR:
630             libblock_remap_data_postprocess_collection_update(
631                 bmain, (Collection *)old_id, (Collection *)new_id);
632             break;
633           default:
634             break;
635         }
636       }
637       else {
638         /* No choice but to check whole objects/collections. */
639         libblock_remap_data_postprocess_collection_update(bmain, NULL, NULL);
640         libblock_remap_data_postprocess_object_update(bmain, NULL, NULL);
641       }
642       break;
643     }
644     case ID_OB:
645       if (new_id) { /* Only affects us in case obdata was relinked (changed). */
646         libblock_remap_data_postprocess_obdata_relink(bmain, (Object *)id, new_id);
647       }
648       break;
649     default:
650       break;
651   }
652 
653   DEG_relations_tag_update(bmain);
654 }
655 
id_relink_to_newid_looper(LibraryIDLinkCallbackData * cb_data)656 static int id_relink_to_newid_looper(LibraryIDLinkCallbackData *cb_data)
657 {
658   const int cb_flag = cb_data->cb_flag;
659   if (cb_flag & IDWALK_CB_EMBEDDED) {
660     return IDWALK_RET_NOP;
661   }
662 
663   ID **id_pointer = cb_data->id_pointer;
664   ID *id = *id_pointer;
665   if (id) {
666     /* See: NEW_ID macro */
667     if (id->newid) {
668       BKE_library_update_ID_link_user(id->newid, id, cb_flag);
669       id = id->newid;
670       *id_pointer = id;
671     }
672     if (id->tag & LIB_TAG_NEW) {
673       id->tag &= ~LIB_TAG_NEW;
674       BKE_libblock_relink_to_newid(id);
675     }
676   }
677   return IDWALK_RET_NOP;
678 }
679 
680 /**
681  * Similar to #libblock_relink_ex,
682  * but is remapping IDs to their newid value if non-NULL, in given \a id.
683  *
684  * Very specific usage, not sure we'll keep it on the long run,
685  * currently only used in Object/Collection duplication code...
686  */
BKE_libblock_relink_to_newid(ID * id)687 void BKE_libblock_relink_to_newid(ID *id)
688 {
689   if (ID_IS_LINKED(id)) {
690     return;
691   }
692 
693   BKE_library_foreach_ID_link(NULL, id, id_relink_to_newid_looper, NULL, 0);
694 }
695