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