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