Fix (unreported) missing remapping of proxy_from pointer.
[blender.git] / source / blender / blenkernel / intern / library_remap.c
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 blender/blenkernel/intern/library_remap.c
18  *  \ingroup bke
19  *
20  * Contains management of ID's and libraries remap, unlink and free logic.
21  */
22
23 #include <stdio.h>
24 #include <ctype.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <stddef.h>
28 #include <assert.h>
29
30 #include "MEM_guardedalloc.h"
31
32 /* all types are needed here, in order to do memory operations */
33 #include "DNA_anim_types.h"
34 #include "DNA_armature_types.h"
35 #include "DNA_brush_types.h"
36 #include "DNA_camera_types.h"
37 #include "DNA_cachefile_types.h"
38 #include "DNA_group_types.h"
39 #include "DNA_gpencil_types.h"
40 #include "DNA_ipo_types.h"
41 #include "DNA_key_types.h"
42 #include "DNA_lamp_types.h"
43 #include "DNA_lattice_types.h"
44 #include "DNA_linestyle_types.h"
45 #include "DNA_material_types.h"
46 #include "DNA_mesh_types.h"
47 #include "DNA_meta_types.h"
48 #include "DNA_movieclip_types.h"
49 #include "DNA_mask_types.h"
50 #include "DNA_node_types.h"
51 #include "DNA_object_types.h"
52 #include "DNA_scene_types.h"
53 #include "DNA_screen_types.h"
54 #include "DNA_speaker_types.h"
55 #include "DNA_sound_types.h"
56 #include "DNA_text_types.h"
57 #include "DNA_vfont_types.h"
58 #include "DNA_windowmanager_types.h"
59 #include "DNA_world_types.h"
60
61 #include "BLI_blenlib.h"
62 #include "BLI_utildefines.h"
63
64 #include "BKE_action.h"
65 #include "BKE_animsys.h"
66 #include "BKE_armature.h"
67 #include "BKE_brush.h"
68 #include "BKE_camera.h"
69 #include "BKE_cachefile.h"
70 #include "BKE_curve.h"
71 #include "BKE_depsgraph.h"
72 #include "BKE_fcurve.h"
73 #include "BKE_font.h"
74 #include "BKE_group.h"
75 #include "BKE_gpencil.h"
76 #include "BKE_idprop.h"
77 #include "BKE_image.h"
78 #include "BKE_ipo.h"
79 #include "BKE_key.h"
80 #include "BKE_lamp.h"
81 #include "BKE_lattice.h"
82 #include "BKE_library.h"
83 #include "BKE_library_query.h"
84 #include "BKE_library_remap.h"
85 #include "BKE_linestyle.h"
86 #include "BKE_mesh.h"
87 #include "BKE_material.h"
88 #include "BKE_main.h"
89 #include "BKE_mask.h"
90 #include "BKE_mball.h"
91 #include "BKE_modifier.h"
92 #include "BKE_movieclip.h"
93 #include "BKE_multires.h"
94 #include "BKE_node.h"
95 #include "BKE_object.h"
96 #include "BKE_paint.h"
97 #include "BKE_particle.h"
98 #include "BKE_sca.h"
99 #include "BKE_speaker.h"
100 #include "BKE_sound.h"
101 #include "BKE_screen.h"
102 #include "BKE_scene.h"
103 #include "BKE_text.h"
104 #include "BKE_texture.h"
105 #include "BKE_world.h"
106
107 #ifdef WITH_PYTHON
108 #include "BPY_extern.h"
109 #endif
110
111 static BKE_library_free_window_manager_cb free_windowmanager_cb = NULL;
112
113 void BKE_library_callback_free_window_manager_set(BKE_library_free_window_manager_cb func)
114 {
115         free_windowmanager_cb = func;
116 }
117
118 static BKE_library_free_notifier_reference_cb free_notifier_reference_cb = NULL;
119
120 void BKE_library_callback_free_notifier_reference_set(BKE_library_free_notifier_reference_cb func)
121 {
122         free_notifier_reference_cb = func;
123 }
124
125 static BKE_library_remap_editor_id_reference_cb remap_editor_id_reference_cb = NULL;
126
127 void BKE_library_callback_remap_editor_id_reference_set(BKE_library_remap_editor_id_reference_cb func)
128 {
129         remap_editor_id_reference_cb = func;
130 }
131
132 typedef struct IDRemap {
133         Main *bmain;  /* Only used to trigger depsgraph updates in the right bmain. */
134         ID *old_id;
135         ID *new_id;
136         ID *id;  /* The ID in which we are replacing old_id by new_id usages. */
137         short flag;
138
139         /* 'Output' data. */
140         short status;
141         int skipped_direct;  /* Number of direct usecases that could not be remapped (e.g.: obdata when in edit mode). */
142         int skipped_indirect;  /* Number of indirect usecases that could not be remapped. */
143         int skipped_refcounted;  /* Number of skipped usecases that refcount the datablock. */
144 } IDRemap;
145
146 /* IDRemap->flag enums defined in BKE_library.h */
147
148 /* IDRemap->status */
149 enum {
150         /* *** Set by callback. *** */
151         ID_REMAP_IS_LINKED_DIRECT       = 1 << 0,  /* new_id is directly linked in current .blend. */
152         ID_REMAP_IS_USER_ONE_SKIPPED    = 1 << 1,  /* There was some skipped 'user_one' usages of old_id. */
153 };
154
155 static int foreach_libblock_remap_callback(void *user_data, ID *id_self, ID **id_p, int cb_flag)
156 {
157         if (cb_flag & IDWALK_CB_PRIVATE) {
158                 return IDWALK_RET_NOP;
159         }
160
161         IDRemap *id_remap_data = user_data;
162         ID *old_id = id_remap_data->old_id;
163         ID *new_id = id_remap_data->new_id;
164         ID *id = id_remap_data->id;
165
166         if (!old_id) {  /* Used to cleanup all IDs used by a specific one. */
167                 BLI_assert(!new_id);
168                 old_id = *id_p;
169         }
170
171         if (*id_p && (*id_p == old_id)) {
172                 const bool is_indirect = (cb_flag & IDWALK_CB_INDIRECT_USAGE) != 0;
173                 const bool skip_indirect = (id_remap_data->flag & ID_REMAP_SKIP_INDIRECT_USAGE) != 0;
174                 /* Note: proxy usage implies LIB_TAG_EXTERN, so on this aspect it is direct,
175                  *       on the other hand since they get reset to lib data on file open/reload it is indirect too...
176                  *       Edit Mode is also a 'skip direct' case. */
177                 const bool is_obj = (GS(id->name) == ID_OB);
178                 const bool is_obj_proxy = (is_obj && (((Object *)id)->proxy || ((Object *)id)->proxy_group));
179                 const bool is_obj_editmode = (is_obj && BKE_object_is_in_editmode((Object *)id));
180                 const bool is_never_null = ((cb_flag & IDWALK_CB_NEVER_NULL) && (new_id == NULL) &&
181                                             (id_remap_data->flag & ID_REMAP_FORCE_NEVER_NULL_USAGE) == 0);
182                 const bool skip_never_null = (id_remap_data->flag & ID_REMAP_SKIP_NEVER_NULL_USAGE) != 0;
183
184 #ifdef DEBUG_PRINT
185                 printf("In %s: Remapping %s (%p) to %s (%p) (is_indirect: %d, skip_indirect: %d)\n",
186                        id->name, old_id->name, old_id, new_id ? new_id->name : "<NONE>", new_id, is_indirect, skip_indirect);
187 #endif
188
189                 if ((id_remap_data->flag & ID_REMAP_FLAG_NEVER_NULL_USAGE) && (cb_flag & IDWALK_CB_NEVER_NULL)) {
190                         id->tag |= LIB_TAG_DOIT;
191                 }
192
193                 /* Special hack in case it's Object->data and we are in edit mode, and new_id is not NULL
194                  * (otherwise, we follow common NEVER_NULL flags).
195                  * (skipped_indirect too). */
196                 if ((is_never_null && skip_never_null) ||
197                     (is_obj_editmode && (((Object *)id)->data == *id_p) && new_id != NULL) ||
198                     (skip_indirect && is_indirect))
199                 {
200                         if (is_indirect) {
201                                 id_remap_data->skipped_indirect++;
202                                 if (is_obj) {
203                                         Object *ob = (Object *)id;
204                                         if (ob->data == *id_p && ob->proxy != NULL) {
205                                                 /* And another 'Proudly brought to you by Proxy Hell' hack!
206                                                  * This will allow us to avoid clearing 'LIB_EXTERN' flag of obdata of proxies... */
207                                                 id_remap_data->skipped_direct++;
208                                         }
209                                 }
210                         }
211                         else if (is_never_null || is_obj_editmode) {
212                                 id_remap_data->skipped_direct++;
213                         }
214                         else {
215                                 BLI_assert(0);
216                         }
217                         if (cb_flag & IDWALK_CB_USER) {
218                                 id_remap_data->skipped_refcounted++;
219                         }
220                         else if (cb_flag & IDWALK_CB_USER_ONE) {
221                                 /* No need to count number of times this happens, just a flag is enough. */
222                                 id_remap_data->status |= ID_REMAP_IS_USER_ONE_SKIPPED;
223                         }
224                 }
225                 else {
226                         if (!is_never_null) {
227                                 *id_p = new_id;
228                                 DAG_id_tag_update_ex(id_remap_data->bmain, id_self, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
229                         }
230                         if (cb_flag & IDWALK_CB_USER) {
231                                 id_us_min(old_id);
232                                 /* We do not want to handle LIB_TAG_INDIRECT/LIB_TAG_EXTERN here. */
233                                 if (new_id)
234                                         new_id->us++;
235                         }
236                         else if (cb_flag & IDWALK_CB_USER_ONE) {
237                                 id_us_ensure_real(new_id);
238                                 /* We cannot affect old_id->us directly, LIB_TAG_EXTRAUSER(_SET) are assumed to be set as needed,
239                                  * that extra user is processed in final handling... */
240                         }
241                         if (!is_indirect || is_obj_proxy) {
242                                 id_remap_data->status |= ID_REMAP_IS_LINKED_DIRECT;
243                         }
244                         /* We need to remap proxy_from pointer of remapped proxy... sigh. */
245                         if (is_obj_proxy && new_id != NULL) {
246                                 Object *ob = (Object *)id;
247                                 if (ob->proxy == (Object *)new_id) {
248                                         ob->proxy->proxy_from = ob;
249                                 }
250                         }
251                 }
252         }
253
254         return IDWALK_RET_NOP;
255 }
256
257 /* Some remapping unfortunately require extra and/or specific handling, tackle those here. */
258 static void libblock_remap_data_preprocess_scene_base_unlink(
259         IDRemap *r_id_remap_data, Scene *sce, Base *base, const bool skip_indirect, const bool is_indirect)
260 {
261         if (skip_indirect && is_indirect) {
262                 r_id_remap_data->skipped_indirect++;
263                 r_id_remap_data->skipped_refcounted++;
264         }
265         else {
266                 id_us_min((ID *)base->object);
267                 BKE_scene_base_unlink(sce, base);
268                 MEM_freeN(base);
269                 if (!is_indirect) {
270                         r_id_remap_data->status |= ID_REMAP_IS_LINKED_DIRECT;
271                 }
272         }
273 }
274
275 static void libblock_remap_data_preprocess(IDRemap *r_id_remap_data)
276 {
277         switch (GS(r_id_remap_data->id->name)) {
278                 case ID_SCE:
279                 {
280                         Scene *sce = (Scene *)r_id_remap_data->id;
281
282                         if (!r_id_remap_data->new_id) {
283                                 const bool is_indirect = (sce->id.lib != NULL);
284                                 const bool skip_indirect = (r_id_remap_data->flag & ID_REMAP_SKIP_INDIRECT_USAGE) != 0;
285
286                                 /* In case we are unlinking... */
287                                 if (!r_id_remap_data->old_id) {
288                                         /* ... everything from scene. */
289                                         Base *base, *base_next;
290                                         for (base = sce->base.first; base; base = base_next) {
291                                                 base_next = base->next;
292                                                 libblock_remap_data_preprocess_scene_base_unlink(
293                                                             r_id_remap_data, sce, base, skip_indirect, is_indirect);
294                                         }
295                                 }
296                                 else if (GS(r_id_remap_data->old_id->name) == ID_OB) {
297                                         /* ... a specific object from scene. */
298                                         Object *old_ob = (Object *)r_id_remap_data->old_id;
299                                         Base *base = BKE_scene_base_find(sce, old_ob);
300
301                                         if (base) {
302                                                 libblock_remap_data_preprocess_scene_base_unlink(
303                                                             r_id_remap_data, sce, base, skip_indirect, is_indirect);
304                                         }
305                                 }
306                         }
307                         break;
308                 }
309                 case ID_OB:
310                 {
311                         ID *old_id = r_id_remap_data->old_id;
312                         if (!old_id || GS(old_id->name) == ID_AR) {
313                                 Object *ob = (Object *)r_id_remap_data->id;
314                                 /* Object's pose holds reference to armature bones... sic */
315                                 /* Note that in theory, we should have to bother about linked/non-linked/never-null/etc. flags/states.
316                                  * Fortunately, this is just a tag, so we can accept to 'over-tag' a bit for pose recalc, and avoid
317                                  * another complex and risky condition nightmare like the one we have in
318                                  * foreach_libblock_remap_callback()... */
319                                 if (ob->pose && (!old_id || ob->data == old_id)) {
320                                         BLI_assert(ob->type == OB_ARMATURE);
321                                         ob->pose->flag |= POSE_RECALC;
322                                         /* We need to clear pose bone pointers immediately, things like undo writefile may be called
323                                          * before pose is actually recomputed, can lead to segfault... */
324                                         BKE_pose_clear_pointers(ob->pose);
325                                 }
326                         }
327                         break;
328                 }
329                 default:
330                         break;
331         }
332 }
333
334 static void libblock_remap_data_postprocess_object_update(Main *bmain, Object *old_ob, Object *new_ob)
335 {
336         if (old_ob->flag & OB_FROMGROUP) {
337                 /* Note that for Scene's BaseObject->flag, either we:
338                  *     - unlinked old_ob (i.e. new_ob is NULL), in which case scenes' bases have been removed already.
339                  *     - remapped old_ob by new_ob, in which case scenes' bases are still valid as is.
340                  * So in any case, no need to update them here. */
341                 if (BKE_group_object_find(bmain, NULL, old_ob) == NULL) {
342                         old_ob->flag &= ~OB_FROMGROUP;
343                 }
344                 if (new_ob == NULL) {  /* We need to remove NULL-ified groupobjects... */
345                         for (Group *group = bmain->group.first; group; group = group->id.next) {
346                                 BKE_group_object_unlink(bmain, group, NULL, NULL, NULL);
347                         }
348                 }
349                 else {
350                         new_ob->flag |= OB_FROMGROUP;
351                 }
352         }
353         if (old_ob->type == OB_MBALL) {
354                 for (Object *ob = bmain->object.first; ob; ob = ob->id.next) {
355                         if (ob->type == OB_MBALL && BKE_mball_is_basis_for(ob, old_ob)) {
356                                 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
357                         }
358                 }
359         }
360 }
361
362 static void libblock_remap_data_postprocess_group_scene_unlink(Main *bmain, Scene *sce, ID *old_id)
363 {
364         /* Note that here we assume no object has no base (i.e. all objects are assumed instanced
365          * in one scene...). */
366         for (Base *base = sce->base.first; base; base = base->next) {
367                 if (base->flag & OB_FROMGROUP) {
368                         Object *ob = base->object;
369
370                         if (ob->flag & OB_FROMGROUP) {
371                                 Group *grp = BKE_group_object_find(bmain, NULL, ob);
372
373                                 /* Unlinked group (old_id) is still in bmain... */
374                                 if (grp && (&grp->id == old_id || grp->id.us == 0)) {
375                                         grp = BKE_group_object_find(bmain, grp, ob);
376                                 }
377                                 if (!grp) {
378                                         ob->flag &= ~OB_FROMGROUP;
379                                 }
380                         }
381                         if (!(ob->flag & OB_FROMGROUP)) {
382                                 base->flag &= ~OB_FROMGROUP;
383                         }
384                 }
385         }
386 }
387
388 static void libblock_remap_data_postprocess_obdata_relink(Main *bmain, Object *ob, ID *new_id)
389 {
390         if (ob->data == new_id) {
391                 switch (GS(new_id->name)) {
392                         case ID_ME:
393                                 multires_force_update(ob);
394                                 break;
395                         case ID_CU:
396                                 BKE_curve_type_test(ob);
397                                 break;
398                         default:
399                                 break;
400                 }
401                 test_object_modifiers(ob);
402                 test_object_materials(bmain, ob, new_id);
403         }
404 }
405
406 static void libblock_remap_data_postprocess_nodetree_update(Main *bmain, ID *new_id)
407 {
408         /* Verify all nodetree user nodes. */
409         ntreeVerifyNodes(bmain, new_id);
410
411         /* Update node trees as necessary. */
412         FOREACH_NODETREE_BEGIN(bmain, ntree, id) {
413                 /* make an update call for the tree */
414                 ntreeUpdateTree(bmain, ntree);
415         } FOREACH_NODETREE_END;
416 }
417
418 /**
419  * Execute the 'data' part of the remapping (that is, all ID pointers from other ID datablocks).
420  *
421  * Behavior differs depending on whether given \a id is NULL or not:
422  * - \a id NULL: \a old_id must be non-NULL, \a new_id may be NULL (unlinking \a old_id) or not
423  *   (remapping \a old_id to \a new_id). The whole \a bmain database is checked, and all pointers to \a old_id
424  *   are remapped to \a new_id.
425  * - \a id is non-NULL:
426  *   + If \a old_id is NULL, \a new_id must also be NULL, and all ID pointers from \a id are cleared (i.e. \a id
427  *     does not references any other datablock anymore).
428  *   + If \a old_id is non-NULL, behavior is as with a NULL \a id, but only within given \a id.
429  *
430  * \param bmain: the Main data storage to operate on (must never be NULL).
431  * \param id: the datablock to operate on (can be NULL, in which case we operate over all IDs from given bmain).
432  * \param old_id: the datablock to dereference (may be NULL if \a id is non-NULL).
433  * \param new_id: the new datablock to replace \a old_id references with (may be NULL).
434  * \param r_id_remap_data: if non-NULL, the IDRemap struct to use (uselful to retrieve info about remapping process).
435  */
436 ATTR_NONNULL(1) static void libblock_remap_data(
437         Main *bmain, ID *id, ID *old_id, ID *new_id, const short remap_flags, IDRemap *r_id_remap_data)
438 {
439         IDRemap id_remap_data;
440         ListBase *lb_array[MAX_LIBARRAY];
441         int i;
442         const int foreach_id_flags = (remap_flags & ID_REMAP_NO_INDIRECT_PROXY_DATA_USAGE) != 0 ? IDWALK_NO_INDIRECT_PROXY_DATA_USAGE : IDWALK_NOP;
443
444         if (r_id_remap_data == NULL) {
445                 r_id_remap_data = &id_remap_data;
446         }
447         r_id_remap_data->bmain = bmain;
448         r_id_remap_data->old_id = old_id;
449         r_id_remap_data->new_id = new_id;
450         r_id_remap_data->id = NULL;
451         r_id_remap_data->flag = remap_flags;
452         r_id_remap_data->status = 0;
453         r_id_remap_data->skipped_direct = 0;
454         r_id_remap_data->skipped_indirect = 0;
455         r_id_remap_data->skipped_refcounted = 0;
456
457         if (id) {
458 #ifdef DEBUG_PRINT
459                 printf("\tchecking id %s (%p, %p)\n", id->name, id, id->lib);
460 #endif
461                 r_id_remap_data->id = id;
462                 libblock_remap_data_preprocess(r_id_remap_data);
463                 BKE_library_foreach_ID_link(NULL, id, foreach_libblock_remap_callback, (void *)r_id_remap_data, foreach_id_flags);
464         }
465         else {
466                 i = set_listbasepointers(bmain, lb_array);
467
468                 /* Note that this is a very 'bruteforce' approach, maybe we could use some depsgraph to only process
469                  * objects actually using given old_id... sounds rather unlikely currently, though, so this will do for now. */
470
471                 while (i--) {
472                         for (ID *id_curr = lb_array[i]->first; id_curr; id_curr = id_curr->next) {
473                                 if (BKE_library_id_can_use_idtype(id_curr, GS(old_id->name))) {
474                                         /* Note that we cannot skip indirect usages of old_id here (if requested), we still need to check it for
475                                          * the user count handling...
476                                          * XXX No more true (except for debug usage of those skipping counters). */
477                                         r_id_remap_data->id = id_curr;
478                                         libblock_remap_data_preprocess(r_id_remap_data);
479                                         BKE_library_foreach_ID_link(
480                                                     NULL, id_curr, foreach_libblock_remap_callback, (void *)r_id_remap_data, foreach_id_flags);
481                                 }
482                         }
483                 }
484         }
485
486         if (old_id && GS(old_id->name) == ID_OB) {
487                 BKE_sca_logic_links_remap(bmain, (Object *)old_id, (Object *)new_id);
488         }
489
490         /* XXX We may not want to always 'transfer' fakeuser from old to new id... Think for now it's desired behavior
491          *     though, we can always add an option (flag) to control this later if needed. */
492         if (old_id && (old_id->flag & LIB_FAKEUSER)) {
493                 id_fake_user_clear(old_id);
494                 id_fake_user_set(new_id);
495         }
496
497         id_us_clear_real(old_id);
498
499         if (new_id && (new_id->tag & LIB_TAG_INDIRECT) && (r_id_remap_data->status & ID_REMAP_IS_LINKED_DIRECT)) {
500                 new_id->tag &= ~LIB_TAG_INDIRECT;
501                 new_id->tag |= LIB_TAG_EXTERN;
502         }
503
504 #ifdef DEBUG_PRINT
505         printf("%s: %d occurrences skipped (%d direct and %d indirect ones)\n", __func__,
506                r_id_remap_data->skipped_direct + r_id_remap_data->skipped_indirect,
507                r_id_remap_data->skipped_direct, r_id_remap_data->skipped_indirect);
508 #endif
509 }
510
511 /**
512  * Replace all references in given Main to \a old_id by \a new_id
513  * (if \a new_id is NULL, it unlinks \a old_id).
514  */
515 void BKE_libblock_remap_locked(
516         Main *bmain, void *old_idv, void *new_idv,
517         const short remap_flags)
518 {
519         IDRemap id_remap_data;
520         ID *old_id = old_idv;
521         ID *new_id = new_idv;
522         int skipped_direct, skipped_refcounted;
523
524         BLI_assert(old_id != NULL);
525         BLI_assert((new_id == NULL) || GS(old_id->name) == GS(new_id->name));
526         BLI_assert(old_id != new_id);
527
528         libblock_remap_data(bmain, NULL, old_id, new_id, remap_flags, &id_remap_data);
529
530         if (free_notifier_reference_cb) {
531                 free_notifier_reference_cb(old_id);
532         }
533
534         /* We assume editors do not hold references to their IDs... This is false in some cases
535          * (Image is especially tricky here), editors' code is to handle refcount (id->us) itself then. */
536         if (remap_editor_id_reference_cb) {
537                 remap_editor_id_reference_cb(old_id, new_id);
538         }
539
540         skipped_direct = id_remap_data.skipped_direct;
541         skipped_refcounted = id_remap_data.skipped_refcounted;
542
543         /* If old_id was used by some ugly 'user_one' stuff (like Image or Clip editors...), and user count has actually
544          * been incremented for that, we have to decrease once more its user count... unless we had to skip
545          * some 'user_one' cases. */
546         if ((old_id->tag & LIB_TAG_EXTRAUSER_SET) && !(id_remap_data.status & ID_REMAP_IS_USER_ONE_SKIPPED)) {
547                 id_us_clear_real(old_id);
548         }
549
550         if (old_id->us - skipped_refcounted < 0) {
551                 printf("Error in remapping process from '%s' (%p) to '%s' (%p): "
552                        "wrong user count in old ID after process (summing up to %d)\n",
553                        old_id->name, old_id, new_id ? new_id->name : "<NULL>", new_id, old_id->us - skipped_refcounted);
554                 BLI_assert(0);
555         }
556
557         if (skipped_direct == 0) {
558                 /* old_id is assumed to not be used directly anymore... */
559                 if (old_id->lib && (old_id->tag & LIB_TAG_EXTERN)) {
560                         old_id->tag &= ~LIB_TAG_EXTERN;
561                         old_id->tag |= LIB_TAG_INDIRECT;
562                 }
563         }
564
565         /* Some after-process updates.
566          * This is a bit ugly, but cannot see a way to avoid it. Maybe we should do a per-ID callback for this instead?
567          */
568         switch (GS(old_id->name)) {
569                 case ID_OB:
570                         libblock_remap_data_postprocess_object_update(bmain, (Object *)old_id, (Object *)new_id);
571                         break;
572                 case ID_GR:
573                         if (!new_id) {  /* Only affects us in case group was unlinked. */
574                                 for (Scene *sce = bmain->scene.first; sce; sce = sce->id.next) {
575                                         libblock_remap_data_postprocess_group_scene_unlink(bmain, sce, old_id);
576                                 }
577                         }
578                         break;
579                 case ID_ME:
580                 case ID_CU:
581                 case ID_MB:
582                         if (new_id) {  /* Only affects us in case obdata was relinked (changed). */
583                                 for (Object *ob = bmain->object.first; ob; ob = ob->id.next) {
584                                         libblock_remap_data_postprocess_obdata_relink(bmain, ob, new_id);
585                                 }
586                         }
587                         break;
588                 default:
589                         break;
590         }
591
592         /* Node trees may virtually use any kind of data-block... */
593         /* XXX Yuck!!!! nodetree update can do pretty much any thing when talking about py nodes,
594          *     including creating new data-blocks (see T50385), so we need to unlock main here. :(
595          *     Why can't we have re-entrent locks? */
596         BKE_main_unlock(bmain);
597         libblock_remap_data_postprocess_nodetree_update(bmain, new_id);
598         BKE_main_lock(bmain);
599
600         /* Full rebuild of DAG! */
601         DAG_relations_tag_update(bmain);
602 }
603
604 void BKE_libblock_remap(Main *bmain, void *old_idv, void *new_idv, const short remap_flags)
605 {
606         BKE_main_lock(bmain);
607
608         BKE_libblock_remap_locked(bmain, old_idv, new_idv, remap_flags);
609
610         BKE_main_unlock(bmain);
611 }
612
613 /**
614  * Unlink given \a id from given \a bmain (does not touch to indirect, i.e. library, usages of the ID).
615  *
616  * \param do_flag_never_null: If true, all IDs using \a idv in a 'non-NULL' way are flagged by \a LIB_TAG_DOIT flag
617  * (quite obviously, 'non-NULL' usages can never be unlinked by this function...).
618  */
619 void BKE_libblock_unlink(Main *bmain, void *idv, const bool do_flag_never_null, const bool do_skip_indirect)
620 {
621         const short remap_flags = (do_skip_indirect ? ID_REMAP_SKIP_INDIRECT_USAGE : 0) |
622                                   (do_flag_never_null ? ID_REMAP_FLAG_NEVER_NULL_USAGE : 0);
623
624         BKE_main_lock(bmain);
625
626         BKE_libblock_remap_locked(bmain, idv, NULL, remap_flags);
627
628         BKE_main_unlock(bmain);
629 }
630
631 /** Similar to libblock_remap, but only affects IDs used by given \a idv ID.
632  *
633  * \param old_idv: Unlike BKE_libblock_remap, can be NULL,
634  * in which case all ID usages by given \a idv will be cleared.
635  * \param us_min_never_null: If \a true and new_id is NULL,
636  * 'NEVER_NULL' ID usages keep their old id, but this one still gets its user count decremented
637  * (needed when given \a idv is going to be deleted right after being unlinked).
638  */
639 /* Should be able to replace all _relink() funcs (constraints, rigidbody, etc.) ? */
640 /* XXX Arg! Naming... :(
641  *     _relink? avoids confusion with _remap, but is confusing with _unlink
642  *     _remap_used_ids?
643  *     _remap_datablocks?
644  *     BKE_id_remap maybe?
645  *     ... sigh
646  */
647 void BKE_libblock_relink_ex(
648         Main *bmain, void *idv, void *old_idv, void *new_idv, const bool us_min_never_null)
649 {
650         ID *id = idv;
651         ID *old_id = old_idv;
652         ID *new_id = new_idv;
653         int remap_flags = us_min_never_null ? 0 : ID_REMAP_SKIP_NEVER_NULL_USAGE;
654
655         /* No need to lock here, we are only affecting given ID, not bmain database. */
656
657         BLI_assert(id);
658         if (old_id) {
659                 BLI_assert((new_id == NULL) || GS(old_id->name) == GS(new_id->name));
660                 BLI_assert(old_id != new_id);
661         }
662         else {
663                 BLI_assert(new_id == NULL);
664         }
665
666         libblock_remap_data(bmain, id, old_id, new_id, remap_flags, NULL);
667
668         /* Some after-process updates.
669          * This is a bit ugly, but cannot see a way to avoid it. Maybe we should do a per-ID callback for this instead?
670          */
671         switch (GS(id->name)) {
672                 case ID_SCE:
673                 {
674                         Scene *sce = (Scene *)id;
675
676                         if (old_id) {
677                                 switch (GS(old_id->name)) {
678                                         case ID_OB:
679                                         {
680                                                 libblock_remap_data_postprocess_object_update(bmain, (Object *)old_id, (Object *)new_id);
681                                                 break;
682                                         }
683                                         case ID_GR:
684                                                 if (!new_id) {  /* Only affects us in case group was unlinked. */
685                                                         libblock_remap_data_postprocess_group_scene_unlink(bmain, sce, old_id);
686                                                 }
687                                                 break;
688                                         default:
689                                                 break;
690                                 }
691                         }
692                         else {
693                                 /* No choice but to check whole objects/groups. */
694                                 for (Object *ob = bmain->object.first; ob; ob = ob->id.next) {
695                                         libblock_remap_data_postprocess_object_update(bmain, ob, NULL);
696                                 }
697                                 for (Group *grp = bmain->group.first; grp; grp = grp->id.next) {
698                                         libblock_remap_data_postprocess_group_scene_unlink(bmain, sce, NULL);
699                                 }
700                         }
701                         break;
702                 }
703                 case ID_OB:
704                         if (new_id) {  /* Only affects us in case obdata was relinked (changed). */
705                                 libblock_remap_data_postprocess_obdata_relink(bmain, (Object *)id, new_id);
706                         }
707                         break;
708                 default:
709                         break;
710         }
711 }
712
713 static int id_relink_to_newid_looper(void *UNUSED(user_data), ID *UNUSED(self_id), ID **id_pointer, const int cb_flag)
714 {
715         if (cb_flag & IDWALK_CB_PRIVATE) {
716                 return IDWALK_RET_NOP;
717         }
718
719         ID *id = *id_pointer;
720         if (id) {
721                 /* See: NEW_ID macro */
722                 if (id->newid) {
723                         BKE_library_update_ID_link_user(id->newid, id, cb_flag);
724                         *id_pointer = id->newid;
725                 }
726                 else if (id->tag & LIB_TAG_NEW) {
727                         id->tag &= ~LIB_TAG_NEW;
728                         BKE_libblock_relink_to_newid(id);
729                 }
730         }
731         return IDWALK_RET_NOP;
732 }
733
734 /** Similar to libblock_relink_ex, but is remapping IDs to their newid value if non-NULL, in given \a id.
735  *
736  * Very specific usage, not sure we'll keep it on the long run, currently only used in Object duplication code...
737  */
738 void BKE_libblock_relink_to_newid(ID *id)
739 {
740         if (ID_IS_LINKED(id))
741                 return;
742
743         BKE_library_foreach_ID_link(NULL, id, id_relink_to_newid_looper, NULL, 0);
744 }
745
746 void BKE_libblock_free_data(ID *id, const bool do_id_user)
747 {
748         if (id->properties) {
749                 IDP_FreeProperty_ex(id->properties, do_id_user);
750                 MEM_freeN(id->properties);
751         }
752
753         /* XXX TODO remove animdata handling from each type's freeing func, and do it here, like for copy! */
754 }
755
756 void BKE_libblock_free_datablock(ID *id, const int UNUSED(flag))
757 {
758         const short type = GS(id->name);
759         switch (type) {
760                 case ID_SCE:
761                         BKE_scene_free((Scene *)id);
762                         break;
763                 case ID_LI:
764                         BKE_library_free((Library *)id);
765                         break;
766                 case ID_OB:
767                         BKE_object_free((Object *)id);
768                         break;
769                 case ID_ME:
770                         BKE_mesh_free((Mesh *)id);
771                         break;
772                 case ID_CU:
773                         BKE_curve_free((Curve *)id);
774                         break;
775                 case ID_MB:
776                         BKE_mball_free((MetaBall *)id);
777                         break;
778                 case ID_MA:
779                         BKE_material_free((Material *)id);
780                         break;
781                 case ID_TE:
782                         BKE_texture_free((Tex *)id);
783                         break;
784                 case ID_IM:
785                         BKE_image_free((Image *)id);
786                         break;
787                 case ID_LT:
788                         BKE_lattice_free((Lattice *)id);
789                         break;
790                 case ID_LA:
791                         BKE_lamp_free((Lamp *)id);
792                         break;
793                 case ID_CA:
794                         BKE_camera_free((Camera *) id);
795                         break;
796                 case ID_IP:  /* Deprecated. */
797                         BKE_ipo_free((Ipo *)id);
798                         break;
799                 case ID_KE:
800                         BKE_key_free((Key *)id);
801                         break;
802                 case ID_WO:
803                         BKE_world_free((World *)id);
804                         break;
805                 case ID_SCR:
806                         BKE_screen_free((bScreen *)id);
807                         break;
808                 case ID_VF:
809                         BKE_vfont_free((VFont *)id);
810                         break;
811                 case ID_TXT:
812                         BKE_text_free((Text *)id);
813                         break;
814                 case ID_SPK:
815                         BKE_speaker_free((Speaker *)id);
816                         break;
817                 case ID_SO:
818                         BKE_sound_free((bSound *)id);
819                         break;
820                 case ID_GR:
821                         BKE_group_free((Group *)id);
822                         break;
823                 case ID_AR:
824                         BKE_armature_free((bArmature *)id);
825                         break;
826                 case ID_AC:
827                         BKE_action_free((bAction *)id);
828                         break;
829                 case ID_NT:
830                         ntreeFreeTree((bNodeTree *)id);
831                         break;
832                 case ID_BR:
833                         BKE_brush_free((Brush *)id);
834                         break;
835                 case ID_PA:
836                         BKE_particlesettings_free((ParticleSettings *)id);
837                         break;
838                 case ID_WM:
839                         if (free_windowmanager_cb)
840                                 free_windowmanager_cb(NULL, (wmWindowManager *)id);
841                         break;
842                 case ID_GD:
843                         BKE_gpencil_free((bGPdata *)id, true);
844                         break;
845                 case ID_MC:
846                         BKE_movieclip_free((MovieClip *)id);
847                         break;
848                 case ID_MSK:
849                         BKE_mask_free((Mask *)id);
850                         break;
851                 case ID_LS:
852                         BKE_linestyle_free((FreestyleLineStyle *)id);
853                         break;
854                 case ID_PAL:
855                         BKE_palette_free((Palette *)id);
856                         break;
857                 case ID_PC:
858                         BKE_paint_curve_free((PaintCurve *)id);
859                         break;
860                 case ID_CF:
861                         BKE_cachefile_free((CacheFile *)id);
862                         break;
863         }
864 }
865
866
867 void BKE_id_free_ex(Main *bmain, void *idv, int flag, const bool use_flag_from_idtag)
868 {
869         ID *id = idv;
870
871         if (use_flag_from_idtag) {
872                 if ((id->tag & LIB_TAG_NO_MAIN) != 0) {
873                         flag |= LIB_ID_FREE_NO_MAIN;
874                 }
875                 else {
876                         flag &= ~LIB_ID_FREE_NO_MAIN;
877                 }
878
879                 if ((id->tag & LIB_TAG_NO_USER_REFCOUNT) != 0) {
880                         flag |= LIB_ID_FREE_NO_USER_REFCOUNT;
881                 }
882                 else {
883                         flag &= ~LIB_ID_FREE_NO_USER_REFCOUNT;
884                 }
885
886                 if ((id->tag & LIB_TAG_NOT_ALLOCATED) != 0) {
887                         flag |= LIB_ID_FREE_NOT_ALLOCATED;
888                 }
889                 else {
890                         flag &= ~LIB_ID_FREE_NOT_ALLOCATED;
891                 }
892         }
893
894         BLI_assert((flag & LIB_ID_FREE_NO_MAIN) != 0 || bmain != NULL);
895         BLI_assert((flag & LIB_ID_FREE_NO_MAIN) != 0 || (flag & LIB_ID_FREE_NOT_ALLOCATED) == 0);
896         BLI_assert((flag & LIB_ID_FREE_NO_MAIN) != 0 || (flag & LIB_ID_FREE_NO_USER_REFCOUNT) == 0);
897
898         const short type = GS(id->name);
899
900         if (bmain && (flag & LIB_ID_FREE_NO_DEG_TAG) == 0) {
901                 DAG_id_type_tag(bmain, type);
902         }
903
904 #ifdef WITH_PYTHON
905         BPY_id_release(id);
906 #endif
907
908         if ((flag & LIB_ID_FREE_NO_USER_REFCOUNT) == 0) {
909                 BKE_libblock_relink_ex(bmain, id, NULL, NULL, true);
910         }
911
912         BKE_libblock_free_datablock(id, flag);
913
914         /* avoid notifying on removed data */
915         if (bmain) {
916                 BKE_main_lock(bmain);
917         }
918
919         if ((flag & LIB_ID_FREE_NO_UI_USER) == 0) {
920                 if (free_notifier_reference_cb) {
921                         free_notifier_reference_cb(id);
922                 }
923
924                 if (remap_editor_id_reference_cb) {
925                         remap_editor_id_reference_cb(id, NULL);
926                 }
927         }
928
929         if ((flag & LIB_ID_FREE_NO_MAIN) == 0) {
930                 ListBase *lb = which_libbase(bmain, type);
931                 BLI_remlink(lb, id);
932         }
933
934         BKE_libblock_free_data(id, (flag & LIB_ID_FREE_NO_USER_REFCOUNT) == 0);
935
936         if (bmain) {
937                 BKE_main_unlock(bmain);
938         }
939
940         if ((flag & LIB_ID_FREE_NOT_ALLOCATED) == 0) {
941                 MEM_freeN(id);
942         }
943 }
944
945 void BKE_id_free(Main *bmain, void *idv)
946 {
947         BKE_id_free_ex(bmain, idv, 0, true);
948 }
949
950 /**
951  * used in headerbuttons.c image.c mesh.c screen.c sound.c and library.c
952  *
953  * \param do_id_user: if \a true, try to release other ID's 'references' hold by \a idv.
954  *                    (only applies to main database)
955  * \param do_ui_user: similar to do_id_user but makes sure UI does not hold references to
956  *                    \a id.
957  */
958 void BKE_libblock_free_ex(Main *bmain, void *idv, const bool do_id_user, const bool do_ui_user)
959 {
960         ID *id = idv;
961         short type = GS(id->name);
962         ListBase *lb = which_libbase(bmain, type);
963
964         DAG_id_type_tag(bmain, type);
965
966 #ifdef WITH_PYTHON
967 #ifdef WITH_PYTHON_SAFETY
968         BPY_id_release(id);
969 #endif
970         if (id->py_instance) {
971                 BPY_DECREF_RNA_INVALIDATE(id->py_instance);
972         }
973 #endif
974
975         if (do_id_user) {
976                 BKE_libblock_relink_ex(bmain, id, NULL, NULL, true);
977         }
978
979         BKE_libblock_free_datablock(id, 0);
980
981         /* avoid notifying on removed data */
982         BKE_main_lock(bmain);
983
984         if (do_ui_user) {
985                 if (free_notifier_reference_cb) {
986                         free_notifier_reference_cb(id);
987                 }
988
989                 if (remap_editor_id_reference_cb) {
990                         remap_editor_id_reference_cb(id, NULL);
991                 }
992         }
993
994         BLI_remlink(lb, id);
995
996         BKE_libblock_free_data(id, do_id_user);
997         BKE_main_unlock(bmain);
998
999         MEM_freeN(id);
1000 }
1001
1002 void BKE_libblock_free(Main *bmain, void *idv)
1003 {
1004         BKE_libblock_free_ex(bmain, idv, true, true);
1005 }
1006
1007 void BKE_libblock_free_us(Main *bmain, void *idv)      /* test users */
1008 {
1009         ID *id = idv;
1010
1011         id_us_min(id);
1012
1013         /* XXX This is a temp (2.77) hack so that we keep same behavior as in 2.76 regarding groups when deleting an object.
1014          *     Since only 'user_one' usage of objects is groups, and only 'real user' usage of objects is scenes,
1015          *     removing that 'user_one' tag when there is no more real (scene) users of an object ensures it gets
1016          *     fully unlinked.
1017          *     But only for local objects, not linked ones!
1018          *     Otherwise, there is no real way to get rid of an object anymore - better handling of this is TODO.
1019          */
1020         if ((GS(id->name) == ID_OB) && (id->us == 1) && (id->lib == NULL)) {
1021                 id_us_clear_real(id);
1022         }
1023
1024         if (id->us == 0) {
1025                 BKE_libblock_unlink(bmain, id, false, false);
1026
1027                 BKE_libblock_free(bmain, id);
1028         }
1029 }
1030
1031 void BKE_libblock_delete(Main *bmain, void *idv)
1032 {
1033         ListBase *lbarray[MAX_LIBARRAY];
1034         int base_count, i;
1035
1036         base_count = set_listbasepointers(bmain, lbarray);
1037         BKE_main_id_tag_all(bmain, LIB_TAG_DOIT, false);
1038
1039         /* First tag all datablocks directly from target lib.
1040          * Note that we go forward here, since we want to check dependencies before users (e.g. meshes before objects).
1041          * Avoids to have to loop twice. */
1042         for (i = 0; i < base_count; i++) {
1043                 ListBase *lb = lbarray[i];
1044                 ID *id;
1045
1046                 for (id = lb->first; id; id = id->next) {
1047                         /* Note: in case we delete a library, we also delete all its datablocks! */
1048                         if ((id == (ID *)idv) || (id->lib == (Library *)idv) || (id->tag & LIB_TAG_DOIT)) {
1049                                 id->tag |= LIB_TAG_DOIT;
1050                                 /* Will tag 'never NULL' users of this ID too.
1051                                  * Note that we cannot use BKE_libblock_unlink() here, since it would ignore indirect (and proxy!)
1052                                  * links, this can lead to nasty crashing here in second, actual deleting loop.
1053                                  * Also, this will also flag users of deleted data that cannot be unlinked
1054                                  * (object using deleted obdata, etc.), so that they also get deleted. */
1055                                 BKE_libblock_remap(bmain, id, NULL, ID_REMAP_FLAG_NEVER_NULL_USAGE | ID_REMAP_FORCE_NEVER_NULL_USAGE);
1056                         }
1057                 }
1058         }
1059
1060         /* In usual reversed order, such that all usage of a given ID, even 'never NULL' ones, have been already cleared
1061          * when we reach it (e.g. Objects being processed before meshes, they'll have already released their 'reference'
1062          * over meshes when we come to freeing obdata). */
1063         for (i = base_count; i--; ) {
1064                 ListBase *lb = lbarray[i];
1065                 ID *id, *id_next;
1066
1067                 for (id = lb->first; id; id = id_next) {
1068                         id_next = id->next;
1069                         if (id->tag & LIB_TAG_DOIT) {
1070                                 if (id->us != 0) {
1071 #ifdef DEBUG_PRINT
1072                                         printf("%s: deleting %s (%d)\n", __func__, id->name, id->us);
1073 #endif
1074                                         BLI_assert(id->us == 0);
1075                                 }
1076                                 BKE_libblock_free(bmain, id);
1077                         }
1078                 }
1079         }
1080 }