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