Cleanup: spelling, indentation
[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_group_types.h"
42 #include "DNA_gpencil_types.h"
43 #include "DNA_ipo_types.h"
44 #include "DNA_key_types.h"
45 #include "DNA_lamp_types.h"
46 #include "DNA_lattice_types.h"
47 #include "DNA_linestyle_types.h"
48 #include "DNA_material_types.h"
49 #include "DNA_mesh_types.h"
50 #include "DNA_meta_types.h"
51 #include "DNA_movieclip_types.h"
52 #include "DNA_mask_types.h"
53 #include "DNA_node_types.h"
54 #include "DNA_object_types.h"
55 #include "DNA_scene_types.h"
56 #include "DNA_screen_types.h"
57 #include "DNA_speaker_types.h"
58 #include "DNA_sound_types.h"
59 #include "DNA_text_types.h"
60 #include "DNA_vfont_types.h"
61 #include "DNA_windowmanager_types.h"
62 #include "DNA_world_types.h"
63
64 #include "BLI_blenlib.h"
65 #include "BLI_utildefines.h"
66
67 #include "BKE_action.h"
68 #include "BKE_animsys.h"
69 #include "BKE_armature.h"
70 #include "BKE_brush.h"
71 #include "BKE_camera.h"
72 #include "BKE_curve.h"
73 #include "BKE_depsgraph.h"
74 #include "BKE_fcurve.h"
75 #include "BKE_font.h"
76 #include "BKE_group.h"
77 #include "BKE_gpencil.h"
78 #include "BKE_idprop.h"
79 #include "BKE_image.h"
80 #include "BKE_ipo.h"
81 #include "BKE_key.h"
82 #include "BKE_lamp.h"
83 #include "BKE_lattice.h"
84 #include "BKE_library.h"
85 #include "BKE_library_query.h"
86 #include "BKE_library_remap.h"
87 #include "BKE_linestyle.h"
88 #include "BKE_mesh.h"
89 #include "BKE_material.h"
90 #include "BKE_main.h"
91 #include "BKE_mball.h"
92 #include "BKE_movieclip.h"
93 #include "BKE_mask.h"
94 #include "BKE_node.h"
95 #include "BKE_object.h"
96 #include "BKE_paint.h"
97 #include "BKE_particle.h"
98 #include "BKE_speaker.h"
99 #include "BKE_sound.h"
100 #include "BKE_screen.h"
101 #include "BKE_scene.h"
102 #include "BKE_text.h"
103 #include "BKE_texture.h"
104 #include "BKE_world.h"
105
106 #ifdef WITH_PYTHON
107 #include "BPY_extern.h"
108 #endif
109
110 static BKE_library_free_window_manager_cb free_windowmanager_cb = NULL;
111
112 void BKE_library_callback_free_window_manager_set(BKE_library_free_window_manager_cb func)
113 {
114         free_windowmanager_cb = func;
115 }
116
117 static BKE_library_free_notifier_reference_cb free_notifier_reference_cb = NULL;
118
119 void BKE_library_callback_free_notifier_reference_set(BKE_library_free_notifier_reference_cb func)
120 {
121         free_notifier_reference_cb = func;
122 }
123
124 static BKE_library_remap_editor_id_reference_cb remap_editor_id_reference_cb = NULL;
125
126 void BKE_library_callback_remap_editor_id_reference_set(BKE_library_remap_editor_id_reference_cb func)
127 {
128         remap_editor_id_reference_cb = func;
129 }
130
131 typedef struct IDRemap {
132         ID *old_id;
133         ID *new_id;
134         ID *id;  /* The ID in which we are replacing old_id by new_id usages. */
135         short flag;
136
137         /* 'Output' data. */
138         short status;
139         int skipped_direct;  /* Number of direct usecases that could not be remapped (e.g.: obdata when in edit mode). */
140         int skipped_indirect;  /* Number of indirect usecases that could not be remapped. */
141         int skipped_refcounted;  /* Number of skipped usecases that refcount the datablock. */
142 } IDRemap;
143
144 /* IDRemap->flag enums defined in BKE_library.h */
145
146 /* IDRemap->status */
147 enum {
148         /* *** Set by callback. *** */
149         ID_REMAP_IS_LINKED_DIRECT       = 1 << 0,  /* new_id is directly linked in current .blend. */
150         ID_REMAP_IS_USER_ONE_SKIPPED    = 1 << 1,  /* There was some skipped 'user_one' usages of old_id. */
151 };
152
153 static int foreach_libblock_remap_callback(void *user_data, ID *UNUSED(id_self), ID **id_p, int cb_flag)
154 {
155         IDRemap *id_remap_data = user_data;
156         ID *old_id = id_remap_data->old_id;
157         ID *new_id = id_remap_data->new_id;
158         ID *id = id_remap_data->id;
159
160         if (!old_id) {  /* Used to cleanup all IDs used by a specific one. */
161                 BLI_assert(!new_id);
162                 old_id = *id_p;
163         }
164
165         if (*id_p && (*id_p == old_id)) {
166                 const bool is_indirect = (id->lib != NULL);
167                 const bool skip_indirect = (id_remap_data->flag & ID_REMAP_SKIP_INDIRECT_USAGE) != 0;
168                 /* Note: proxy usage implies LIB_TAG_EXTERN, so on this aspect it is direct,
169                  *       on the other hand since they get reset to lib data on file open/reload it is indirect too...
170                  *       Edit Mode is also a 'skip direct' case. */
171                 const bool is_obj = (GS(id->name) == ID_OB);
172                 const bool is_proxy = (is_obj && (((Object *)id)->proxy || ((Object *)id)->proxy_group));
173                 const bool is_obj_editmode = (is_obj && BKE_object_is_in_editmode((Object *)id));
174                 const bool is_never_null = ((cb_flag & IDWALK_NEVER_NULL) && (new_id == NULL) &&
175                                             (id_remap_data->flag & ID_REMAP_FORCE_NEVER_NULL_USAGE) == 0);
176                 const bool skip_never_null = (id_remap_data->flag & ID_REMAP_SKIP_NEVER_NULL_USAGE) != 0;
177
178                 if ((id_remap_data->flag & ID_REMAP_FLAG_NEVER_NULL_USAGE) && (cb_flag & IDWALK_NEVER_NULL)) {
179                         id->tag |= LIB_TAG_DOIT;
180                 }
181
182                 /* Special hack in case it's Object->data and we are in edit mode (skipped_direct too). */
183                 if ((is_never_null && skip_never_null) ||
184                     (is_obj_editmode && (((Object *)id)->data == *id_p)) ||
185                     (skip_indirect && (is_proxy || is_indirect)))
186                 {
187                         if (!is_indirect && (is_never_null || is_proxy || is_obj_editmode)) {
188                                 id_remap_data->skipped_direct++;
189                         }
190                         else {
191                                 id_remap_data->skipped_indirect++;
192                         }
193                         if (cb_flag & IDWALK_USER) {
194                                 id_remap_data->skipped_refcounted++;
195                         }
196                         else if (cb_flag & IDWALK_USER_ONE) {
197                                 /* No need to count number of times this happens, just a flag is enough. */
198                                 id_remap_data->status |= ID_REMAP_IS_USER_ONE_SKIPPED;
199                         }
200                 }
201                 else {
202                         if (!is_never_null) {
203                                 *id_p = new_id;
204                         }
205                         if (cb_flag & IDWALK_USER) {
206                                 id_us_min(old_id);
207                                 /* We do not want to handle LIB_TAG_INDIRECT/LIB_TAG_EXTERN here. */
208                                 if (new_id)
209                                         new_id->us++;
210                         }
211                         else if (cb_flag & IDWALK_USER_ONE) {
212                                 id_us_ensure_real(new_id);
213                                 /* We cannot affect old_id->us directly, LIB_TAG_EXTRAUSER(_SET) are assumed to be set as needed,
214                                  * that extra user is processed in final handling... */
215                         }
216                         if (!is_indirect) {
217                                 id_remap_data->status |= ID_REMAP_IS_LINKED_DIRECT;
218                         }
219                 }
220         }
221
222         return IDWALK_RET_NOP;
223 }
224
225 /**
226  * Execute the 'data' part of the remapping (that is, all ID pointers from other ID datablocks).
227  *
228  * Behavior differs depending on whether given \a id is NULL or not:
229  * - \a id NULL: \a old_id must be non-NULL, \a new_id may be NULL (unlinking \a old_id) or not
230  *   (remapping \a old_id to \a new_id). The whole \a bmain database is checked, and all pointers to \a old_id
231  *   are remapped to \a new_id.
232  * - \a id is non-NULL:
233  *   + 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
234  *     does not references any other datablock anymore).
235  *   + If \a old_id is non-NULL, behavior is as with a NULL \a id, but only for given \a id.
236  *
237  * \param bmain: the Main data storage to operate on (can be NULL if \a id is non-NULL).
238  * \param id: the datablock to operate on (can be NULL if \a bmain is non-NULL).
239  * \param old_id: the datablock to dereference (may be NULL if \a id is non-NULL).
240  * \param new_id: the new datablock to replace \a old_id references with (may be NULL).
241  * \param r_id_remap_data: if non-NULL, the IDRemap struct to use (uselful to retrieve info about remapping process).
242  */
243 static void libblock_remap_data(
244         Main *bmain, ID *id, ID *old_id, ID *new_id, const short remap_flags, IDRemap *r_id_remap_data)
245 {
246         IDRemap id_remap_data;
247         ListBase *lb_array[MAX_LIBARRAY];
248         int i;
249
250         if (r_id_remap_data == NULL) {
251                 r_id_remap_data = &id_remap_data;
252         }
253         r_id_remap_data->old_id = old_id;
254         r_id_remap_data->new_id = new_id;
255         r_id_remap_data->id = NULL;
256         r_id_remap_data->flag = remap_flags;
257         r_id_remap_data->status = 0;
258         r_id_remap_data->skipped_direct = 0;
259         r_id_remap_data->skipped_indirect = 0;
260         r_id_remap_data->skipped_refcounted = 0;
261
262         if (id) {
263 #ifdef DEBUG_PRINT
264                 printf("\tchecking id %s (%p, %p)\n", id->name, id, id->lib);
265 #endif
266                 r_id_remap_data->id = id;
267                 BKE_library_foreach_ID_link(id, foreach_libblock_remap_callback, (void *)r_id_remap_data, IDWALK_NOP);
268         }
269         else {
270                 i = set_listbasepointers(bmain, lb_array);
271
272                 /* Note that this is a very 'bruteforce' approach, maybe we could use some depsgraph to only process
273                  * objects actually using given old_id... sounds rather unlikely currently, though, so this will do for now. */
274
275                 while (i--) {
276                         ID *id_curr = lb_array[i]->first;
277
278                         for (; id_curr; id_curr = id_curr->next) {
279                                 /* Note that we cannot skip indirect usages of old_id here (if requested), we still need to check it for
280                                  * the user count handling...
281                                  * XXX No more true (except for debug usage of those skipping counters). */
282                                 r_id_remap_data->id = id_curr;
283                                 BKE_library_foreach_ID_link(
284                                             id_curr, foreach_libblock_remap_callback, (void *)r_id_remap_data, IDWALK_NOP);
285                         }
286                 }
287         }
288
289         /* XXX We may not want to always 'transfer' fakeuser from old to new id... Think for now it's desired behavior
290          *     though, we can always add an option (flag) to control this later if needed. */
291         if (old_id && (old_id->flag & LIB_FAKEUSER)) {
292                 id_fake_user_clear(old_id);
293                 id_fake_user_set(new_id);
294         }
295
296         id_us_clear_real(old_id);
297
298         if (new_id && (new_id->tag & LIB_TAG_INDIRECT) && (r_id_remap_data->status & ID_REMAP_IS_LINKED_DIRECT)) {
299                 new_id->tag &= ~LIB_TAG_INDIRECT;
300                 new_id->tag |= LIB_TAG_EXTERN;
301         }
302
303 #ifdef DEBUG_PRINT
304         printf("%s: %d occurences skipped (%d direct and %d indirect ones)\n", __func__,
305                r_id_remap_data->skipped_direct + r_id_remap_data->skipped_indirect,
306                r_id_remap_data->skipped_direct, r_id_remap_data->skipped_indirect);
307 #endif
308 }
309
310 /**
311  * Replace all references in given Main to \a old_id by \a new_id
312  * (if \a new_id is NULL, it unlinks \a old_id).
313  */
314 void BKE_libblock_remap_locked(
315         Main *bmain, void *old_idv, void *new_idv,
316         const short remap_flags)
317 {
318         IDRemap id_remap_data;
319         ID *old_id = old_idv;
320         ID *new_id = new_idv;
321         int skipped_direct, skipped_refcounted;
322
323         BLI_assert(old_id != NULL);
324         BLI_assert((new_id == NULL) || GS(old_id->name) == GS(new_id->name));
325         BLI_assert(old_id != new_id);
326
327         /* Some pre-process updates.
328          * This is a bit ugly, but cannot see a way to avoid it. Maybe we should do a per-ID callback for this instead?
329          */
330         if (GS(old_id->name) == ID_OB) {
331                 Object *old_ob = (Object *)old_id;
332                 Object *new_ob = (Object *)new_id;
333
334                 if (new_ob == NULL) {
335                         Scene *sce;
336                         Base *base;
337
338                         for (sce = bmain->scene.first; sce; sce = sce->id.next) {
339                                 base = BKE_scene_base_find(sce, old_ob);
340
341                                 if (base) {
342                                         id_us_min((ID *)base->object);
343                                         BKE_scene_base_unlink(sce, base);
344                                         MEM_freeN(base);
345                                 }
346                         }
347                 }
348         }
349
350         libblock_remap_data(bmain, NULL, old_id, new_id, remap_flags, &id_remap_data);
351
352         if (free_notifier_reference_cb) {
353                 free_notifier_reference_cb(old_id);
354         }
355
356         /* We assume editors do not hold references to their IDs... This is false in some cases
357          * (Image is especially tricky here), editors' code is to handle refcount (id->us) itself then. */
358         if (remap_editor_id_reference_cb) {
359                 remap_editor_id_reference_cb(old_id, new_id);
360         }
361
362         skipped_direct = id_remap_data.skipped_direct;
363         skipped_refcounted = id_remap_data.skipped_refcounted;
364
365         /* If old_id was used by some ugly 'user_one' stuff (like Image or Clip editors...), and user count has actually
366          * been incremented for that, we have to decrease once more its user count... unless we had to skip
367          * some 'user_one' cases. */
368         if ((old_id->tag & LIB_TAG_EXTRAUSER_SET) && !(id_remap_data.status & ID_REMAP_IS_USER_ONE_SKIPPED)) {
369                 id_us_min(old_id);
370                 old_id->tag &= ~LIB_TAG_EXTRAUSER_SET;
371         }
372
373         BLI_assert(old_id->us - skipped_refcounted >= 0);
374         UNUSED_VARS_NDEBUG(skipped_refcounted);
375
376         if (skipped_direct == 0) {
377                 /* old_id is assumed to not be used directly anymore... */
378                 if (old_id->lib && (old_id->tag & LIB_TAG_EXTERN)) {
379                         old_id->tag &= ~LIB_TAG_EXTERN;
380                         old_id->tag |= LIB_TAG_INDIRECT;
381                 }
382         }
383
384         /* Some after-process updates.
385          * This is a bit ugly, but cannot see a way to avoid it. Maybe we should do a per-ID callback for this instead?
386          */
387         switch (GS(old_id->name)) {
388                 case ID_OB:
389                 {
390                         Object *old_ob = (Object *)old_id;
391                         Object *new_ob = (Object *)new_id;
392
393                         if (old_ob->flag & OB_FROMGROUP) {
394                                 /* Note that for Scene's BaseObject->flag, either we:
395                                  *     - unlinked old_ob (i.e. new_ob is NULL), in which case scenes' bases have been removed already.
396                                  *     - remapped old_ob by new_ob, in which case scenes' bases are still valid as is.
397                                  * So in any case, no need to update them here. */
398                                 if (BKE_group_object_find(NULL, old_ob) == NULL) {
399                                         old_ob->flag &= ~OB_FROMGROUP;
400                                 }
401                                 if (new_ob == NULL) {  /* We need to remove NULL-ified groupobjects... */
402                                         Group *group;
403                                         for (group = bmain->group.first; group; group = group->id.next) {
404                                                 BKE_group_object_unlink(group, NULL, NULL, NULL);
405                                         }
406                                 }
407                                 else {
408                                         new_ob->flag |= OB_FROMGROUP;
409                                 }
410                         }
411                         break;
412                 }
413                 case ID_GR:
414                         if (new_id == NULL) {  /* Only affects us in case group was unlinked. */
415                                 for (Scene *sce = bmain->scene.first; sce; sce = sce->id.next) {
416                                         /* Note that here we assume no object has no base (i.e. all objects are assumed instanced
417                                          * in one scene...). */
418                                         for (Base *base = sce->base.first; base; base = base->next) {
419                                                 if (base->flag & OB_FROMGROUP) {
420                                                         Object *ob = base->object;
421
422                                                         if (ob->flag & OB_FROMGROUP) {
423                                                                 Group *grp = BKE_group_object_find(NULL, ob);
424
425                                                                 /* Unlinked group (old_id) is still in bmain... */
426                                                                 if (grp && (&grp->id == old_id)) {
427                                                                         grp = BKE_group_object_find(grp, ob);
428                                                                 }
429                                                                 if (!grp) {
430                                                                         ob->flag &= ~OB_FROMGROUP;
431                                                                 }
432                                                         }
433                                                         if (!(ob->flag & OB_FROMGROUP)) {
434                                                                 base->flag &= ~OB_FROMGROUP;
435                                                         }
436                                                 }
437                                         }
438                                 }
439                         }
440                         break;
441                 default:
442                         break;
443         }
444
445         /* Full rebuild of DAG! */
446         DAG_relations_tag_update(bmain);
447 }
448
449 void BKE_libblock_remap(Main *bmain, void *old_idv, void *new_idv, const short remap_flags)
450 {
451         BKE_main_lock(bmain);
452
453         BKE_libblock_remap_locked(bmain, old_idv, new_idv, remap_flags);
454
455         BKE_main_unlock(bmain);
456 }
457
458 /**
459  * Unlink given \a id from given \a bmain (does not touch to indirect, i.e. library, usages of the ID).
460  *
461  * \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
462  * (quite obviously, 'non-NULL' usages can never be unlinked by this function...).
463  */
464 void BKE_libblock_unlink(Main *bmain, void *idv, const bool do_flag_never_null)
465 {
466         const short remap_flags = ID_REMAP_SKIP_INDIRECT_USAGE | (do_flag_never_null ? ID_REMAP_FLAG_NEVER_NULL_USAGE : 0);
467
468         BKE_main_lock(bmain);
469
470         BKE_libblock_remap_locked(bmain, idv, NULL, remap_flags);
471
472         BKE_main_unlock(bmain);
473 }
474
475 /** Similar to libblock_remap, but only affects IDs used by given \a idv ID.
476  *
477  * \param old_idv: Unlike BKE_libblock_remap, can be NULL,
478  * in which case all ID usages by given \a idv will be cleared.
479  * \param us_min_never_null: If \a true and new_id is NULL,
480  * 'NEVER_NULL' ID usages keep their old id, but this one still gets its user count decremented
481  * (needed when given \a idv is going to be deleted right after being unlinked).
482  */
483 /* Should be able to replace all _relink() funcs (constraints, rigidbody, etc.) ? */
484 /* XXX Arg! Naming... :(
485  *     _relink? avoids confusion with _remap, but is confusing with _unlink
486  *     _remap_used_ids?
487  *     _remap_datablocks?
488  *     BKE_id_remap maybe?
489  *     ... sigh
490  */
491 void BKE_libblock_relink_ex(
492         void *idv, void *old_idv, void *new_idv, const bool us_min_never_null)
493 {
494         ID *id = idv;
495         ID *old_id = old_idv;
496         ID *new_id = new_idv;
497         int remap_flags = us_min_never_null ? 0 : ID_REMAP_SKIP_NEVER_NULL_USAGE;
498
499         /* No need to lock here, we are only affecting given ID. */
500
501         BLI_assert(id);
502         if (old_id) {
503                 BLI_assert((new_id == NULL) || GS(old_id->name) == GS(new_id->name));
504                 BLI_assert(old_id != new_id);
505         }
506         else {
507                 BLI_assert(new_id == NULL);
508         }
509
510         libblock_remap_data(NULL, id, old_id, new_id, remap_flags, NULL);
511 }
512
513 static void animdata_dtar_clear_cb(ID *UNUSED(id), AnimData *adt, void *userdata)
514 {
515         ChannelDriver *driver;
516         FCurve *fcu;
517
518         /* find the driver this belongs to and update it */
519         for (fcu = adt->drivers.first; fcu; fcu = fcu->next) {
520                 driver = fcu->driver;
521                 
522                 if (driver) {
523                         DriverVar *dvar;
524                         for (dvar = driver->variables.first; dvar; dvar = dvar->next) {
525                                 DRIVER_TARGETS_USED_LOOPER(dvar) 
526                                 {
527                                         if (dtar->id == userdata)
528                                                 dtar->id = NULL;
529                                 }
530                                 DRIVER_TARGETS_LOOPER_END
531                         }
532                 }
533         }
534 }
535
536 void BKE_libblock_free_data(Main *bmain, ID *id)
537 {
538         if (id->properties) {
539                 IDP_FreeProperty(id->properties);
540                 MEM_freeN(id->properties);
541         }
542         
543         /* this ID may be a driver target! */
544         BKE_animdata_main_cb(bmain, animdata_dtar_clear_cb, (void *)id);
545 }
546
547 /**
548  * used in headerbuttons.c image.c mesh.c screen.c sound.c and library.c
549  *
550  * \param do_id_user: if \a true, try to release other ID's 'references' hold by \a idv.
551  */
552 void BKE_libblock_free_ex(Main *bmain, void *idv, const bool do_id_user)
553 {
554         ID *id = idv;
555         short type = GS(id->name);
556         ListBase *lb = which_libbase(bmain, type);
557
558         DAG_id_type_tag(bmain, type);
559
560 #ifdef WITH_PYTHON
561         BPY_id_release(id);
562 #endif
563
564         if (do_id_user) {
565                 BKE_libblock_relink_ex(id, NULL, NULL, true);
566         }
567
568         switch (type) {
569                 case ID_SCE:
570                         BKE_scene_free((Scene *)id);
571                         break;
572                 case ID_LI:
573                         BKE_library_free((Library *)id);
574                         break;
575                 case ID_OB:
576                         BKE_object_free((Object *)id);
577                         break;
578                 case ID_ME:
579                         BKE_mesh_free((Mesh *)id);
580                         break;
581                 case ID_CU:
582                         BKE_curve_free((Curve *)id);
583                         break;
584                 case ID_MB:
585                         BKE_mball_free((MetaBall *)id);
586                         break;
587                 case ID_MA:
588                         BKE_material_free((Material *)id);
589                         break;
590                 case ID_TE:
591                         BKE_texture_free((Tex *)id);
592                         break;
593                 case ID_IM:
594                         BKE_image_free((Image *)id);
595                         break;
596                 case ID_LT:
597                         BKE_lattice_free((Lattice *)id);
598                         break;
599                 case ID_LA:
600                         BKE_lamp_free((Lamp *)id);
601                         break;
602                 case ID_CA:
603                         BKE_camera_free((Camera *) id);
604                         break;
605                 case ID_IP:  /* Deprecated. */
606                         BKE_ipo_free((Ipo *)id);
607                         break;
608                 case ID_KE:
609                         BKE_key_free((Key *)id);
610                         break;
611                 case ID_WO:
612                         BKE_world_free((World *)id);
613                         break;
614                 case ID_SCR:
615                         BKE_screen_free((bScreen *)id);
616                         break;
617                 case ID_VF:
618                         BKE_vfont_free((VFont *)id);
619                         break;
620                 case ID_TXT:
621                         BKE_text_free((Text *)id);
622                         break;
623                 case ID_SPK:
624                         BKE_speaker_free((Speaker *)id);
625                         break;
626                 case ID_SO:
627                         BKE_sound_free((bSound *)id);
628                         break;
629                 case ID_GR:
630                         BKE_group_free((Group *)id);
631                         break;
632                 case ID_AR:
633                         BKE_armature_free((bArmature *)id);
634                         break;
635                 case ID_AC:
636                         BKE_action_free((bAction *)id);
637                         break;
638                 case ID_NT:
639                         ntreeFreeTree((bNodeTree *)id);
640                         break;
641                 case ID_BR:
642                         BKE_brush_free((Brush *)id);
643                         break;
644                 case ID_PA:
645                         BKE_particlesettings_free((ParticleSettings *)id);
646                         break;
647                 case ID_WM:
648                         if (free_windowmanager_cb)
649                                 free_windowmanager_cb(NULL, (wmWindowManager *)id);
650                         break;
651                 case ID_GD:
652                         BKE_gpencil_free((bGPdata *)id);
653                         break;
654                 case ID_MC:
655                         BKE_movieclip_free((MovieClip *)id);
656                         break;
657                 case ID_MSK:
658                         BKE_mask_free((Mask *)id);
659                         break;
660                 case ID_LS:
661                         BKE_linestyle_free((FreestyleLineStyle *)id);
662                         break;
663                 case ID_PAL:
664                         BKE_palette_free((Palette *)id);
665                         break;
666                 case ID_PC:
667                         BKE_paint_curve_free((PaintCurve *)id);
668                         break;
669         }
670
671         /* avoid notifying on removed data */
672         BKE_main_lock(bmain);
673
674         if (free_notifier_reference_cb) {
675                 free_notifier_reference_cb(id);
676         }
677
678         if (remap_editor_id_reference_cb) {
679                 remap_editor_id_reference_cb(id, NULL);
680         }
681
682         BLI_remlink(lb, id);
683
684         BKE_libblock_free_data(bmain, id);
685         BKE_main_unlock(bmain);
686
687         MEM_freeN(id);
688 }
689
690 void BKE_libblock_free(Main *bmain, void *idv)
691 {
692         BKE_libblock_free_ex(bmain, idv, true);
693 }
694
695 void BKE_libblock_free_us(Main *bmain, void *idv)      /* test users */
696 {
697         ID *id = idv;
698         
699         id_us_min(id);
700
701         /* XXX This is a temp (2.77) hack so that we keep same behavior as in 2.76 regarding groups when deleting an object.
702          *     Since only 'user_one' usage of objects is groups, and only 'real user' usage of objects is scenes,
703          *     removing that 'user_one' tag when there is no more real (scene) users of an object ensures it gets
704          *     fully unlinked.
705          *     Otherwise, there is no real way to get rid of an object anymore - better handling of this is TODO.
706          */
707         if ((GS(id->name) == ID_OB) && (id->us == 1)) {
708                 id_us_clear_real(id);
709         }
710
711         if (id->us == 0) {
712                 BKE_libblock_unlink(bmain, id, false);
713                 
714                 BKE_libblock_free(bmain, id);
715         }
716 }
717
718 void BKE_libblock_delete(Main *bmain, void *idv)
719 {
720         ListBase *lbarray[MAX_LIBARRAY];
721         int base_count, i;
722
723         base_count = set_listbasepointers(bmain, lbarray);
724         BKE_main_id_tag_all(bmain, LIB_TAG_DOIT, false);
725
726         /* First tag all datablocks directly from target lib.
727      * Note that we go forward here, since we want to check dependencies before users (e.g. meshes before objects).
728      * Avoids to have to loop twice. */
729         for (i = 0; i < base_count; i++) {
730                 ListBase *lb = lbarray[i];
731                 ID *id;
732
733                 for (id = lb->first; id; id = id->next) {
734                         /* Note: in case we delete a library, we also delete all its datablocks! */
735                         if ((id == (ID *)idv) || (id->lib == (Library *)idv) || (id->tag & LIB_TAG_DOIT)) {
736                                 id->tag |= LIB_TAG_DOIT;
737                                 /* Will tag 'never NULL' users of this ID too.
738                                  * Note that we cannot use BKE_libblock_unlink() here, since it would ignore indirect (and proxy!)
739                                  * links, this can lead to nasty crashing here in second, actual deleting loop.
740                                  * Also, this will also flag users of deleted data that cannot be unlinked
741                                  * (object using deleted obdata, etc.), so that they also get deleted. */
742                                 BKE_libblock_remap(bmain, id, NULL, ID_REMAP_FLAG_NEVER_NULL_USAGE | ID_REMAP_FORCE_NEVER_NULL_USAGE);
743                         }
744                 }
745         }
746
747         /* In usual reversed order, such that all usage of a given ID, even 'never NULL' ones, have been already cleared
748          * when we reach it (e.g. Objects being processed before meshes, they'll have already released their 'reference'
749          * over meshes when we come to freeing obdata). */
750         for (i = base_count; i--; ) {
751                 ListBase *lb = lbarray[i];
752                 ID *id, *id_next;
753
754                 for (id = lb->first; id; id = id_next) {
755                         id_next = id->next;
756                         if (id->tag & LIB_TAG_DOIT) {
757                                 if (id->us != 0) {
758 #ifdef DEBUG_PRINT
759                                         printf("%s: deleting %s (%d)\n", __func__, id->name, id->us);
760 #endif
761                                         BLI_assert(id->us == 0);
762                                 }
763                                 BKE_libblock_free(bmain, id);
764                         }
765                 }
766         }
767 }