Fix T57816: Crash when deleting recently orphaned collection
[blender.git] / source / blender / blenkernel / intern / library_query.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  * The Original Code is Copyright (C) 2014 by Blender Foundation.
19  * All rights reserved.
20  *
21  * Contributor(s): Sergey Sharybin.
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/blenkernel/intern/library_query.c
27  *  \ingroup bke
28  */
29
30 #include <stdlib.h>
31
32 #include "MEM_guardedalloc.h"
33
34 #include "DNA_anim_types.h"
35 #include "DNA_armature_types.h"
36 #include "DNA_brush_types.h"
37 #include "DNA_camera_types.h"
38 #include "DNA_collection_types.h"
39 #include "DNA_constraint_types.h"
40 #include "DNA_gpencil_types.h"
41 #include "DNA_key_types.h"
42 #include "DNA_lamp_types.h"
43 #include "DNA_lattice_types.h"
44 #include "DNA_linestyle_types.h"
45 #include "DNA_material_types.h"
46 #include "DNA_mesh_types.h"
47 #include "DNA_meshdata_types.h"
48 #include "DNA_meta_types.h"
49 #include "DNA_movieclip_types.h"
50 #include "DNA_mask_types.h"
51 #include "DNA_node_types.h"
52 #include "DNA_object_force_types.h"
53 #include "DNA_lightprobe_types.h"
54 #include "DNA_rigidbody_types.h"
55 #include "DNA_scene_types.h"
56 #include "DNA_sequence_types.h"
57 #include "DNA_screen_types.h"
58 #include "DNA_speaker_types.h"
59 #include "DNA_sound_types.h"
60 #include "DNA_text_types.h"
61 #include "DNA_vfont_types.h"
62 #include "DNA_windowmanager_types.h"
63 #include "DNA_workspace_types.h"
64 #include "DNA_world_types.h"
65
66 #include "BLI_utildefines.h"
67 #include "BLI_listbase.h"
68 #include "BLI_ghash.h"
69 #include "BLI_linklist_stack.h"
70
71 #include "BKE_animsys.h"
72 #include "BKE_collection.h"
73 #include "BKE_constraint.h"
74 #include "BKE_fcurve.h"
75 #include "BKE_gpencil_modifier.h"
76 #include "BKE_idprop.h"
77 #include "BKE_library.h"
78 #include "BKE_library_query.h"
79 #include "BKE_main.h"
80 #include "BKE_modifier.h"
81 #include "BKE_node.h"
82 #include "BKE_particle.h"
83 #include "BKE_rigidbody.h"
84 #include "BKE_sequencer.h"
85 #include "BKE_tracking.h"
86 #include "BKE_workspace.h"
87
88
89 #define FOREACH_FINALIZE _finalize
90 #define FOREACH_FINALIZE_VOID \
91         if (0) { goto FOREACH_FINALIZE; } \
92         FOREACH_FINALIZE: ((void)0)
93
94 #define FOREACH_CALLBACK_INVOKE_ID_PP(_data, id_pp, _cb_flag) \
95         CHECK_TYPE(id_pp, ID **); \
96         if (!((_data)->status & IDWALK_STOP)) { \
97                 const int _flag = (_data)->flag; \
98                 ID *old_id = *(id_pp); \
99                 const int callback_return = (_data)->callback((_data)->user_data, (_data)->self_id, id_pp, _cb_flag | (_data)->cb_flag); \
100                 if (_flag & IDWALK_READONLY) { \
101                         BLI_assert(*(id_pp) == old_id); \
102                 } \
103                 if (old_id && (_flag & IDWALK_RECURSE)) { \
104                         if (BLI_gset_add((_data)->ids_handled, old_id)) { \
105                                 if (!(callback_return & IDWALK_RET_STOP_RECURSION)) { \
106                                         BLI_LINKSTACK_PUSH((_data)->ids_todo, old_id); \
107                                 } \
108                         } \
109                 } \
110                 if (callback_return & IDWALK_RET_STOP_ITER) { \
111                         (_data)->status |= IDWALK_STOP; \
112                         goto FOREACH_FINALIZE; \
113                 } \
114         } \
115         else { \
116                 goto FOREACH_FINALIZE; \
117         } ((void)0)
118
119 #define FOREACH_CALLBACK_INVOKE_ID(_data, id, cb_flag) \
120         { \
121                 CHECK_TYPE_ANY(id, ID *, void *); \
122                 FOREACH_CALLBACK_INVOKE_ID_PP(_data, (ID **)&(id), cb_flag); \
123         } ((void)0)
124
125 #define FOREACH_CALLBACK_INVOKE(_data, id_super, cb_flag) \
126         { \
127                 CHECK_TYPE(&((id_super)->id), ID *); \
128                 FOREACH_CALLBACK_INVOKE_ID_PP(_data, (ID **)&(id_super), cb_flag); \
129         } ((void)0)
130
131 /* status */
132 enum {
133         IDWALK_STOP     = 1 << 0,
134 };
135
136 typedef struct LibraryForeachIDData {
137         ID *self_id;
138         int flag;
139         int cb_flag;
140         LibraryIDLinkCallback callback;
141         void *user_data;
142         int status;
143
144         /* To handle recursion. */
145         GSet *ids_handled;  /* All IDs that are either already done, or still in ids_todo stack. */
146         BLI_LINKSTACK_DECLARE(ids_todo, ID *);
147 } LibraryForeachIDData;
148
149 static void library_foreach_idproperty_ID_link(LibraryForeachIDData *data, IDProperty *prop, int flag)
150 {
151         if (!prop)
152                 return;
153
154         switch (prop->type) {
155                 case IDP_GROUP:
156                 {
157                         for (IDProperty *loop = prop->data.group.first; loop; loop = loop->next) {
158                                 library_foreach_idproperty_ID_link(data, loop, flag);
159                         }
160                         break;
161                 }
162                 case IDP_IDPARRAY:
163                 {
164                         IDProperty *loop = IDP_Array(prop);
165                         for (int i = 0; i < prop->len; i++) {
166                                 library_foreach_idproperty_ID_link(data, &loop[i], flag);
167                         }
168                         break;
169                 }
170                 case IDP_ID:
171                         FOREACH_CALLBACK_INVOKE_ID(data, prop->data.pointer, flag);
172                         break;
173                 default:
174                         break;  /* Nothing to do here with other types of IDProperties... */
175         }
176
177         FOREACH_FINALIZE_VOID;
178 }
179
180 static void library_foreach_rigidbodyworldSceneLooper(
181         struct RigidBodyWorld *UNUSED(rbw), ID **id_pointer, void *user_data, int cb_flag)
182 {
183         LibraryForeachIDData *data = (LibraryForeachIDData *) user_data;
184         FOREACH_CALLBACK_INVOKE_ID_PP(data, id_pointer, cb_flag);
185
186         FOREACH_FINALIZE_VOID;
187 }
188
189 static void library_foreach_modifiersForeachIDLink(
190         void *user_data, Object *UNUSED(object), ID **id_pointer, int cb_flag)
191 {
192         LibraryForeachIDData *data = (LibraryForeachIDData *) user_data;
193         FOREACH_CALLBACK_INVOKE_ID_PP(data, id_pointer, cb_flag);
194
195         FOREACH_FINALIZE_VOID;
196 }
197
198 static void library_foreach_gpencil_modifiersForeachIDLink(
199         void *user_data, Object *UNUSED(object), ID **id_pointer, int cb_flag)
200 {
201         LibraryForeachIDData *data = (LibraryForeachIDData *) user_data;
202         FOREACH_CALLBACK_INVOKE_ID_PP(data, id_pointer, cb_flag);
203
204         FOREACH_FINALIZE_VOID;
205 }
206
207 static void library_foreach_constraintObjectLooper(bConstraint *UNUSED(con), ID **id_pointer,
208                                                    bool is_reference, void *user_data)
209 {
210         LibraryForeachIDData *data = (LibraryForeachIDData *) user_data;
211         const int cb_flag = is_reference ? IDWALK_CB_USER : IDWALK_CB_NOP;
212         FOREACH_CALLBACK_INVOKE_ID_PP(data, id_pointer, cb_flag);
213
214         FOREACH_FINALIZE_VOID;
215 }
216
217 static void library_foreach_particlesystemsObjectLooper(
218         ParticleSystem *UNUSED(psys), ID **id_pointer, void *user_data, int cb_flag)
219 {
220         LibraryForeachIDData *data = (LibraryForeachIDData *) user_data;
221         FOREACH_CALLBACK_INVOKE_ID_PP(data, id_pointer, cb_flag);
222
223         FOREACH_FINALIZE_VOID;
224 }
225
226 static void library_foreach_nla_strip(LibraryForeachIDData *data, NlaStrip *strip)
227 {
228         NlaStrip *substrip;
229
230         FOREACH_CALLBACK_INVOKE(data, strip->act, IDWALK_CB_USER);
231
232         for (substrip = strip->strips.first; substrip; substrip = substrip->next) {
233                 library_foreach_nla_strip(data, substrip);
234         }
235
236         FOREACH_FINALIZE_VOID;
237 }
238
239 static void library_foreach_animationData(LibraryForeachIDData *data, AnimData *adt)
240 {
241         FCurve *fcu;
242         NlaTrack *nla_track;
243         NlaStrip *nla_strip;
244
245         for (fcu = adt->drivers.first; fcu; fcu = fcu->next) {
246                 ChannelDriver *driver = fcu->driver;
247                 DriverVar *dvar;
248
249                 for (dvar = driver->variables.first; dvar; dvar = dvar->next) {
250                         /* only used targets */
251                         DRIVER_TARGETS_USED_LOOPER_BEGIN(dvar)
252                         {
253                                 FOREACH_CALLBACK_INVOKE_ID(data, dtar->id, IDWALK_CB_NOP);
254                         }
255                         DRIVER_TARGETS_LOOPER_END;
256                 }
257         }
258
259         FOREACH_CALLBACK_INVOKE(data, adt->action, IDWALK_CB_USER);
260         FOREACH_CALLBACK_INVOKE(data, adt->tmpact, IDWALK_CB_USER);
261
262         for (nla_track = adt->nla_tracks.first; nla_track; nla_track = nla_track->next) {
263                 for (nla_strip = nla_track->strips.first; nla_strip; nla_strip = nla_strip->next) {
264                         library_foreach_nla_strip(data, nla_strip);
265                 }
266         }
267
268         FOREACH_FINALIZE_VOID;
269 }
270
271 static void library_foreach_mtex(LibraryForeachIDData *data, MTex *mtex)
272 {
273         FOREACH_CALLBACK_INVOKE(data, mtex->object, IDWALK_CB_NOP);
274         FOREACH_CALLBACK_INVOKE(data, mtex->tex, IDWALK_CB_USER);
275
276         FOREACH_FINALIZE_VOID;
277 }
278
279 static void library_foreach_paint(LibraryForeachIDData *data, Paint *paint)
280 {
281         FOREACH_CALLBACK_INVOKE(data, paint->brush, IDWALK_CB_USER);
282         for (int i = 0; i < paint->tool_slots_len; i++) {
283                 FOREACH_CALLBACK_INVOKE(data, paint->tool_slots[i].brush, IDWALK_CB_USER);
284         }
285         FOREACH_CALLBACK_INVOKE(data, paint->palette, IDWALK_CB_USER);
286
287         FOREACH_FINALIZE_VOID;
288 }
289
290 static void library_foreach_bone(LibraryForeachIDData *data, Bone *bone)
291 {
292         library_foreach_idproperty_ID_link(data, bone->prop, IDWALK_CB_USER);
293
294         for (Bone *curbone = bone->childbase.first; curbone; curbone = curbone->next) {
295                 library_foreach_bone(data, curbone);
296         }
297
298         FOREACH_FINALIZE_VOID;
299 }
300
301 static void library_foreach_layer_collection(LibraryForeachIDData *data, ListBase *lb)
302 {
303         for (LayerCollection *lc = lb->first; lc; lc = lc->next) {
304                 FOREACH_CALLBACK_INVOKE(data, lc->collection, IDWALK_CB_NOP);
305                 library_foreach_layer_collection(data, &lc->layer_collections);
306         }
307
308         FOREACH_FINALIZE_VOID;
309 }
310
311 static void library_foreach_ID_as_subdata_link(
312         ID **id_pp, LibraryIDLinkCallback callback, void *user_data, int flag, LibraryForeachIDData *data)
313 {
314         /* Needed e.g. for callbacks handling relationships... This call shall be absolutely readonly. */
315         ID *id = *id_pp;
316         FOREACH_CALLBACK_INVOKE_ID_PP(data, id_pp, IDWALK_CB_PRIVATE);
317         BLI_assert(id == *id_pp);
318
319         if (flag & IDWALK_RECURSE) {
320                 /* Defer handling into main loop, recursively calling BKE_library_foreach_ID_link in IDWALK_RECURSE case is
321                  * troublesome, see T49553. */
322                 if (BLI_gset_add(data->ids_handled, id)) {
323                         BLI_LINKSTACK_PUSH(data->ids_todo, id);
324                 }
325         }
326         else {
327                 BKE_library_foreach_ID_link(NULL, id, callback, user_data, flag);
328         }
329
330         FOREACH_FINALIZE_VOID;
331 }
332
333 /**
334  * Loop over all of the ID's this datablock links to.
335  *
336  * \note: May be extended to be recursive in the future.
337  */
338 void BKE_library_foreach_ID_link(Main *bmain, ID *id, LibraryIDLinkCallback callback, void *user_data, int flag)
339 {
340         LibraryForeachIDData data;
341         int i;
342
343         if (flag & IDWALK_RECURSE) {
344                 /* For now, recusion implies read-only. */
345                 flag |= IDWALK_READONLY;
346
347                 data.ids_handled = BLI_gset_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, __func__);
348                 BLI_LINKSTACK_INIT(data.ids_todo);
349
350                 BLI_gset_add(data.ids_handled, id);
351         }
352         else {
353                 data.ids_handled = NULL;
354         }
355         data.flag = flag;
356         data.status = 0;
357         data.callback = callback;
358         data.user_data = user_data;
359
360 #define CALLBACK_INVOKE_ID(check_id, cb_flag) \
361         FOREACH_CALLBACK_INVOKE_ID(&data, check_id, cb_flag)
362
363 #define CALLBACK_INVOKE(check_id_super, cb_flag) \
364         FOREACH_CALLBACK_INVOKE(&data, check_id_super, cb_flag)
365
366         for (; id != NULL; id = (flag & IDWALK_RECURSE) ? BLI_LINKSTACK_POP(data.ids_todo) : NULL) {
367                 data.self_id = id;
368                 data.cb_flag = ID_IS_LINKED(id) ? IDWALK_CB_INDIRECT_USAGE : 0;
369
370                 if (bmain != NULL && bmain->relations != NULL && (flag & IDWALK_READONLY)) {
371                         /* Note that this is minor optimization, even in worst cases (like id being an object with lots of
372                          * drivers and constraints and modifiers, or material etc. with huge node tree),
373                          * but we might as well use it (Main->relations is always assumed valid, it's responsibility of code
374                          * creating it to free it, especially if/when it starts modifying Main database). */
375                         MainIDRelationsEntry *entry = BLI_ghash_lookup(bmain->relations->id_user_to_used, id);
376                         for (; entry != NULL; entry = entry->next) {
377                                 FOREACH_CALLBACK_INVOKE_ID_PP(&data, entry->id_pointer, entry->usage_flag);
378                         }
379                         continue;
380                 }
381
382                 if (id->override_static != NULL) {
383                         CALLBACK_INVOKE_ID(id->override_static->reference, IDWALK_CB_USER | IDWALK_CB_STATIC_OVERRIDE_REFERENCE);
384                         CALLBACK_INVOKE_ID(id->override_static->storage, IDWALK_CB_USER | IDWALK_CB_STATIC_OVERRIDE_REFERENCE);
385                 }
386
387                 library_foreach_idproperty_ID_link(&data, id->properties, IDWALK_CB_USER);
388
389                 AnimData *adt = BKE_animdata_from_id(id);
390                 if (adt) {
391                         library_foreach_animationData(&data, adt);
392                 }
393
394                 switch ((ID_Type)GS(id->name)) {
395                         case ID_LI:
396                         {
397                                 Library *lib = (Library *) id;
398                                 CALLBACK_INVOKE(lib->parent, IDWALK_CB_NOP);
399                                 break;
400                         }
401                         case ID_SCE:
402                         {
403                                 Scene *scene = (Scene *) id;
404                                 ToolSettings *toolsett = scene->toolsettings;
405
406                                 CALLBACK_INVOKE(scene->camera, IDWALK_CB_NOP);
407                                 CALLBACK_INVOKE(scene->world, IDWALK_CB_USER);
408                                 CALLBACK_INVOKE(scene->set, IDWALK_CB_NOP);
409                                 CALLBACK_INVOKE(scene->clip, IDWALK_CB_USER);
410                                 CALLBACK_INVOKE(scene->r.bake.cage_object, IDWALK_CB_NOP);
411                                 if (scene->nodetree) {
412                                         /* nodetree **are owned by IDs**, treat them as mere sub-data and not real ID! */
413                                         library_foreach_ID_as_subdata_link((ID **)&scene->nodetree, callback, user_data, flag, &data);
414                                 }
415                                 if (scene->ed) {
416                                         Sequence *seq;
417                                         SEQP_BEGIN(scene->ed, seq)
418                                         {
419                                                 CALLBACK_INVOKE(seq->scene, IDWALK_CB_NOP);
420                                                 CALLBACK_INVOKE(seq->scene_camera, IDWALK_CB_NOP);
421                                                 CALLBACK_INVOKE(seq->clip, IDWALK_CB_USER);
422                                                 CALLBACK_INVOKE(seq->mask, IDWALK_CB_USER);
423                                                 CALLBACK_INVOKE(seq->sound, IDWALK_CB_USER);
424                                                 library_foreach_idproperty_ID_link(&data, seq->prop, IDWALK_CB_USER);
425                                                 for (SequenceModifierData *smd = seq->modifiers.first; smd; smd = smd->next) {
426                                                         CALLBACK_INVOKE(smd->mask_id, IDWALK_CB_USER);
427                                                 }
428                                         } SEQ_END;
429                                 }
430
431
432                                 for (CollectionObject *cob = scene->master_collection->gobject.first; cob; cob = cob->next) {
433                                         CALLBACK_INVOKE(cob->ob, IDWALK_CB_USER);
434                                 }
435                                 for (CollectionChild *child = scene->master_collection->children.first; child; child = child->next) {
436                                         CALLBACK_INVOKE(child->collection, IDWALK_CB_USER);
437                                 }
438
439                                 ViewLayer *view_layer;
440                                 for (view_layer = scene->view_layers.first; view_layer; view_layer = view_layer->next) {
441                                         for (Base *base = view_layer->object_bases.first; base; base = base->next) {
442                                                 CALLBACK_INVOKE(base->object, IDWALK_CB_NOP);
443                                         }
444
445                                         library_foreach_layer_collection(&data, &view_layer->layer_collections);
446
447                                         for (FreestyleModuleConfig  *fmc = view_layer->freestyle_config.modules.first; fmc; fmc = fmc->next) {
448                                                 if (fmc->script) {
449                                                         CALLBACK_INVOKE(fmc->script, IDWALK_CB_NOP);
450                                                 }
451                                         }
452
453                                         for (FreestyleLineSet *fls = view_layer->freestyle_config.linesets.first; fls; fls = fls->next) {
454                                                 if (fls->group) {
455                                                         CALLBACK_INVOKE(fls->group, IDWALK_CB_USER);
456                                                 }
457
458                                                 if (fls->linestyle) {
459                                                         CALLBACK_INVOKE(fls->linestyle, IDWALK_CB_USER);
460                                                 }
461                                         }
462                                 }
463
464                                 for (TimeMarker *marker = scene->markers.first; marker; marker = marker->next) {
465                                         CALLBACK_INVOKE(marker->camera, IDWALK_CB_NOP);
466                                 }
467
468                                 if (toolsett) {
469                                         CALLBACK_INVOKE(toolsett->particle.scene, IDWALK_CB_NOP);
470                                         CALLBACK_INVOKE(toolsett->particle.object, IDWALK_CB_NOP);
471                                         CALLBACK_INVOKE(toolsett->particle.shape_object, IDWALK_CB_NOP);
472
473                                         library_foreach_paint(&data, &toolsett->imapaint.paint);
474                                         CALLBACK_INVOKE(toolsett->imapaint.stencil, IDWALK_CB_USER);
475                                         CALLBACK_INVOKE(toolsett->imapaint.clone, IDWALK_CB_USER);
476                                         CALLBACK_INVOKE(toolsett->imapaint.canvas, IDWALK_CB_USER);
477
478                                         if (toolsett->vpaint) {
479                                                 library_foreach_paint(&data, &toolsett->vpaint->paint);
480                                         }
481                                         if (toolsett->wpaint) {
482                                                 library_foreach_paint(&data, &toolsett->wpaint->paint);
483                                         }
484                                         if (toolsett->sculpt) {
485                                                 library_foreach_paint(&data, &toolsett->sculpt->paint);
486                                                 CALLBACK_INVOKE(toolsett->sculpt->gravity_object, IDWALK_CB_NOP);
487                                         }
488                                         if (toolsett->uvsculpt) {
489                                                 library_foreach_paint(&data, &toolsett->uvsculpt->paint);
490                                         }
491                                         if (toolsett->gp_paint) {
492                                                 library_foreach_paint(&data, &toolsett->gp_paint->paint);
493                                         }
494                                 }
495
496                                 if (scene->rigidbody_world) {
497                                         BKE_rigidbody_world_id_loop(scene->rigidbody_world, library_foreach_rigidbodyworldSceneLooper, &data);
498                                 }
499
500                                 break;
501                         }
502
503                         case ID_OB:
504                         {
505                                 Object *object = (Object *) id;
506                                 ParticleSystem *psys;
507
508                                 /* Object is special, proxies make things hard... */
509                                 const int data_cb_flag = data.cb_flag;
510                                 const int proxy_cb_flag = ((data.flag & IDWALK_NO_INDIRECT_PROXY_DATA_USAGE) == 0 && (object->proxy || object->proxy_group)) ?
511                                                               IDWALK_CB_INDIRECT_USAGE : 0;
512
513                                 /* object data special case */
514                                 data.cb_flag |= proxy_cb_flag;
515                                 if (object->type == OB_EMPTY) {
516                                         /* empty can have NULL or Image */
517                                         CALLBACK_INVOKE_ID(object->data, IDWALK_CB_USER);
518                                 }
519                                 else {
520                                         /* when set, this can't be NULL */
521                                         if (object->data) {
522                                                 CALLBACK_INVOKE_ID(object->data, IDWALK_CB_USER | IDWALK_CB_NEVER_NULL);
523                                         }
524                                 }
525                                 data.cb_flag = data_cb_flag;
526
527                                 CALLBACK_INVOKE(object->parent, IDWALK_CB_NOP);
528                                 CALLBACK_INVOKE(object->track, IDWALK_CB_NOP);
529                                 /* object->proxy is refcounted, but not object->proxy_group... *sigh* */
530                                 CALLBACK_INVOKE(object->proxy, IDWALK_CB_USER);
531                                 CALLBACK_INVOKE(object->proxy_group, IDWALK_CB_NOP);
532
533                                 /* Special case!
534                                  * Since this field is set/owned by 'user' of this ID (and not ID itself), it is only indirect usage
535                                  * if proxy object is linked... Twisted. */
536                                 if (object->proxy_from) {
537                                         data.cb_flag = ID_IS_LINKED(object->proxy_from) ? IDWALK_CB_INDIRECT_USAGE : 0;
538                                 }
539                                 CALLBACK_INVOKE(object->proxy_from, IDWALK_CB_LOOPBACK);
540                                 data.cb_flag = data_cb_flag;
541
542                                 CALLBACK_INVOKE(object->poselib, IDWALK_CB_USER);
543
544                                 data.cb_flag |= proxy_cb_flag;
545                                 for (i = 0; i < object->totcol; i++) {
546                                         CALLBACK_INVOKE(object->mat[i], IDWALK_CB_USER);
547                                 }
548                                 data.cb_flag = data_cb_flag;
549
550                                 CALLBACK_INVOKE(object->gpd, IDWALK_CB_USER);
551                                 CALLBACK_INVOKE(object->dup_group, IDWALK_CB_USER);
552
553                                 if (object->pd) {
554                                         CALLBACK_INVOKE(object->pd->tex, IDWALK_CB_USER);
555                                         CALLBACK_INVOKE(object->pd->f_source, IDWALK_CB_NOP);
556                                 }
557                                 /* Note that ob->effect is deprecated, so no need to handle it here. */
558
559                                 if (object->pose) {
560                                         bPoseChannel *pchan;
561
562                                         data.cb_flag |= proxy_cb_flag;
563                                         for (pchan = object->pose->chanbase.first; pchan; pchan = pchan->next) {
564                                                 library_foreach_idproperty_ID_link(&data, pchan->prop, IDWALK_CB_USER);
565                                                 CALLBACK_INVOKE(pchan->custom, IDWALK_CB_USER);
566                                                 BKE_constraints_id_loop(&pchan->constraints, library_foreach_constraintObjectLooper, &data);
567                                         }
568                                         data.cb_flag = data_cb_flag;
569                                 }
570
571                                 if (object->rigidbody_constraint) {
572                                         CALLBACK_INVOKE(object->rigidbody_constraint->ob1, IDWALK_CB_NOP);
573                                         CALLBACK_INVOKE(object->rigidbody_constraint->ob2, IDWALK_CB_NOP);
574                                 }
575
576                                 if (object->lodlevels.first) {
577                                         LodLevel *level;
578                                         for (level = object->lodlevels.first; level; level = level->next) {
579                                                 CALLBACK_INVOKE(level->source, IDWALK_CB_NOP);
580                                         }
581                                 }
582
583                                 modifiers_foreachIDLink(object, library_foreach_modifiersForeachIDLink, &data);
584                                 BKE_gpencil_modifiers_foreachIDLink(object, library_foreach_gpencil_modifiersForeachIDLink, &data);
585                                 BKE_constraints_id_loop(&object->constraints, library_foreach_constraintObjectLooper, &data);
586
587                                 for (psys = object->particlesystem.first; psys; psys = psys->next) {
588                                         BKE_particlesystem_id_loop(psys, library_foreach_particlesystemsObjectLooper, &data);
589                                 }
590
591                                 if (object->soft) {
592                                         CALLBACK_INVOKE(object->soft->collision_group, IDWALK_CB_NOP);
593
594                                         if (object->soft->effector_weights) {
595                                                 CALLBACK_INVOKE(object->soft->effector_weights->group, IDWALK_CB_NOP);
596                                         }
597                                 }
598                                 break;
599                         }
600
601                         case ID_AR:
602                         {
603                                 bArmature *arm = (bArmature *)id;
604
605                                 for (Bone *bone = arm->bonebase.first; bone; bone = bone->next) {
606                                         library_foreach_bone(&data, bone);
607                                 }
608                                 break;
609                         }
610
611                         case ID_ME:
612                         {
613                                 Mesh *mesh = (Mesh *) id;
614                                 CALLBACK_INVOKE(mesh->texcomesh, IDWALK_CB_USER);
615                                 CALLBACK_INVOKE(mesh->key, IDWALK_CB_USER);
616                                 for (i = 0; i < mesh->totcol; i++) {
617                                         CALLBACK_INVOKE(mesh->mat[i], IDWALK_CB_USER);
618                                 }
619                                 break;
620                         }
621
622                         case ID_CU:
623                         {
624                                 Curve *curve = (Curve *) id;
625                                 CALLBACK_INVOKE(curve->bevobj, IDWALK_CB_NOP);
626                                 CALLBACK_INVOKE(curve->taperobj, IDWALK_CB_NOP);
627                                 CALLBACK_INVOKE(curve->textoncurve, IDWALK_CB_NOP);
628                                 CALLBACK_INVOKE(curve->key, IDWALK_CB_USER);
629                                 for (i = 0; i < curve->totcol; i++) {
630                                         CALLBACK_INVOKE(curve->mat[i], IDWALK_CB_USER);
631                                 }
632                                 CALLBACK_INVOKE(curve->vfont, IDWALK_CB_USER);
633                                 CALLBACK_INVOKE(curve->vfontb, IDWALK_CB_USER);
634                                 CALLBACK_INVOKE(curve->vfonti, IDWALK_CB_USER);
635                                 CALLBACK_INVOKE(curve->vfontbi, IDWALK_CB_USER);
636                                 break;
637                         }
638
639                         case ID_MB:
640                         {
641                                 MetaBall *metaball = (MetaBall *) id;
642                                 for (i = 0; i < metaball->totcol; i++) {
643                                         CALLBACK_INVOKE(metaball->mat[i], IDWALK_CB_USER);
644                                 }
645                                 break;
646                         }
647
648                         case ID_MA:
649                         {
650                                 Material *material = (Material *) id;
651                                 if (material->nodetree) {
652                                         /* nodetree **are owned by IDs**, treat them as mere sub-data and not real ID! */
653                                         library_foreach_ID_as_subdata_link((ID **)&material->nodetree, callback, user_data, flag, &data);
654                                 }
655                                 if (material->texpaintslot != NULL) {
656                                         CALLBACK_INVOKE(material->texpaintslot->ima, IDWALK_CB_NOP);
657                                 }
658                                 if (material->gp_style != NULL) {
659                                         CALLBACK_INVOKE(material->gp_style->sima, IDWALK_CB_USER);
660                                         CALLBACK_INVOKE(material->gp_style->ima, IDWALK_CB_USER);
661                                 }
662                                 break;
663                         }
664
665                         case ID_TE:
666                         {
667                                 Tex *texture = (Tex *) id;
668                                 if (texture->nodetree) {
669                                         /* nodetree **are owned by IDs**, treat them as mere sub-data and not real ID! */
670                                         library_foreach_ID_as_subdata_link((ID **)&texture->nodetree, callback, user_data, flag, &data);
671                                 }
672                                 CALLBACK_INVOKE(texture->ima, IDWALK_CB_USER);
673                                 break;
674                         }
675
676                         case ID_LT:
677                         {
678                                 Lattice *lattice = (Lattice *) id;
679                                 CALLBACK_INVOKE(lattice->key, IDWALK_CB_USER);
680                                 break;
681                         }
682
683                         case ID_LA:
684                         {
685                                 Lamp *lamp = (Lamp *) id;
686                                 if (lamp->nodetree) {
687                                         /* nodetree **are owned by IDs**, treat them as mere sub-data and not real ID! */
688                                         library_foreach_ID_as_subdata_link((ID **)&lamp->nodetree, callback, user_data, flag, &data);
689                                 }
690                                 break;
691                         }
692
693                         case ID_CA:
694                         {
695                                 Camera *camera = (Camera *) id;
696                                 CALLBACK_INVOKE(camera->dof_ob, IDWALK_CB_NOP);
697                                 for (CameraBGImage *bgpic = camera->bg_images.first; bgpic; bgpic = bgpic->next) {
698                                         if (bgpic->source == CAM_BGIMG_SOURCE_IMAGE) {
699                                                 CALLBACK_INVOKE(bgpic->ima, IDWALK_CB_USER);
700                                         }
701                                         else if (bgpic->source == CAM_BGIMG_SOURCE_MOVIE) {
702                                                 CALLBACK_INVOKE(bgpic->clip, IDWALK_CB_USER);
703                                         }
704                                 }
705
706                                 break;
707                         }
708
709                         case ID_KE:
710                         {
711                                 Key *key = (Key *) id;
712                                 CALLBACK_INVOKE_ID(key->from, IDWALK_CB_LOOPBACK);
713                                 break;
714                         }
715
716                         case ID_WO:
717                         {
718                                 World *world = (World *) id;
719                                 if (world->nodetree) {
720                                         /* nodetree **are owned by IDs**, treat them as mere sub-data and not real ID! */
721                                         library_foreach_ID_as_subdata_link((ID **)&world->nodetree, callback, user_data, flag, &data);
722                                 }
723                                 break;
724                         }
725
726                         case ID_SPK:
727                         {
728                                 Speaker *speaker = (Speaker *) id;
729                                 CALLBACK_INVOKE(speaker->sound, IDWALK_CB_USER);
730                                 break;
731                         }
732
733                         case ID_LP:
734                         {
735                                 LightProbe *probe = (LightProbe *) id;
736                                 CALLBACK_INVOKE(probe->image, IDWALK_CB_USER);
737                                 CALLBACK_INVOKE(probe->visibility_grp, IDWALK_CB_NOP);
738                                 break;
739                         }
740
741                         case ID_GR:
742                         {
743                                 Collection *collection = (Collection *) id;
744                                 for (CollectionObject *cob = collection->gobject.first; cob; cob = cob->next) {
745                                         CALLBACK_INVOKE(cob->ob, IDWALK_CB_USER);
746                                 }
747                                 for (CollectionChild *child = collection->children.first; child; child = child->next) {
748                                         CALLBACK_INVOKE(child->collection, IDWALK_CB_USER);
749                                 }
750                                 for (CollectionParent *parent = collection->parents.first; parent; parent = parent->next) {
751                                         CALLBACK_INVOKE(parent->collection, IDWALK_CB_NOP);
752                                 }
753                                 break;
754                         }
755
756                         case ID_NT:
757                         {
758                                 bNodeTree *ntree = (bNodeTree *) id;
759                                 bNode *node;
760                                 bNodeSocket *sock;
761
762                                 CALLBACK_INVOKE(ntree->gpd, IDWALK_CB_USER);
763
764                                 for (node = ntree->nodes.first; node; node = node->next) {
765                                         CALLBACK_INVOKE_ID(node->id, IDWALK_CB_USER);
766
767                                         library_foreach_idproperty_ID_link(&data, node->prop, IDWALK_CB_USER);
768                                         for (sock = node->inputs.first; sock; sock = sock->next) {
769                                                 library_foreach_idproperty_ID_link(&data, sock->prop, IDWALK_CB_USER);
770                                         }
771                                         for (sock = node->outputs.first; sock; sock = sock->next) {
772                                                 library_foreach_idproperty_ID_link(&data, sock->prop, IDWALK_CB_USER);
773                                         }
774                                 }
775
776                                 for (sock = ntree->inputs.first; sock; sock = sock->next) {
777                                         library_foreach_idproperty_ID_link(&data, sock->prop, IDWALK_CB_USER);
778                                 }
779                                 for (sock = ntree->outputs.first; sock; sock = sock->next) {
780                                         library_foreach_idproperty_ID_link(&data, sock->prop, IDWALK_CB_USER);
781                                 }
782                                 break;
783                         }
784
785                         case ID_BR:
786                         {
787                                 Brush *brush = (Brush *) id;
788                                 CALLBACK_INVOKE(brush->toggle_brush, IDWALK_CB_NOP);
789                                 CALLBACK_INVOKE(brush->clone.image, IDWALK_CB_NOP);
790                                 CALLBACK_INVOKE(brush->paint_curve, IDWALK_CB_USER);
791                                 if (brush->gpencil_settings) {
792                                         CALLBACK_INVOKE(brush->gpencil_settings->material, IDWALK_CB_USER);
793                                 }
794                                 library_foreach_mtex(&data, &brush->mtex);
795                                 library_foreach_mtex(&data, &brush->mask_mtex);
796                                 break;
797                         }
798
799                         case ID_PA:
800                         {
801                                 ParticleSettings *psett = (ParticleSettings *) id;
802                                 CALLBACK_INVOKE(psett->dup_group, IDWALK_CB_NOP);
803                                 CALLBACK_INVOKE(psett->dup_ob, IDWALK_CB_NOP);
804                                 CALLBACK_INVOKE(psett->bb_ob, IDWALK_CB_NOP);
805                                 CALLBACK_INVOKE(psett->collision_group, IDWALK_CB_NOP);
806
807                                 for (i = 0; i < MAX_MTEX; i++) {
808                                         if (psett->mtex[i]) {
809                                                 library_foreach_mtex(&data, psett->mtex[i]);
810                                         }
811                                 }
812
813                                 if (psett->effector_weights) {
814                                         CALLBACK_INVOKE(psett->effector_weights->group, IDWALK_CB_NOP);
815                                 }
816
817                                 if (psett->pd) {
818                                         CALLBACK_INVOKE(psett->pd->tex, IDWALK_CB_USER);
819                                         CALLBACK_INVOKE(psett->pd->f_source, IDWALK_CB_NOP);
820                                 }
821                                 if (psett->pd2) {
822                                         CALLBACK_INVOKE(psett->pd2->tex, IDWALK_CB_USER);
823                                         CALLBACK_INVOKE(psett->pd2->f_source, IDWALK_CB_NOP);
824                                 }
825
826                                 if (psett->boids) {
827                                         BoidState *state;
828                                         BoidRule *rule;
829
830                                         for (state = psett->boids->states.first; state; state = state->next) {
831                                                 for (rule = state->rules.first; rule; rule = rule->next) {
832                                                         if (rule->type == eBoidRuleType_Avoid) {
833                                                                 BoidRuleGoalAvoid *gabr = (BoidRuleGoalAvoid *)rule;
834                                                                 CALLBACK_INVOKE(gabr->ob, IDWALK_CB_NOP);
835                                                         }
836                                                         else if (rule->type == eBoidRuleType_FollowLeader) {
837                                                                 BoidRuleFollowLeader *flbr = (BoidRuleFollowLeader *)rule;
838                                                                 CALLBACK_INVOKE(flbr->ob, IDWALK_CB_NOP);
839                                                         }
840                                                 }
841                                         }
842                                 }
843
844                                 for (ParticleDupliWeight *dw = psett->dupliweights.first; dw; dw = dw->next) {
845                                         CALLBACK_INVOKE(dw->ob, IDWALK_CB_NOP);
846                                 }
847                                 break;
848                         }
849
850                         case ID_MC:
851                         {
852                                 MovieClip *clip = (MovieClip *) id;
853                                 MovieTracking *tracking = &clip->tracking;
854                                 MovieTrackingObject *object;
855                                 MovieTrackingTrack *track;
856                                 MovieTrackingPlaneTrack *plane_track;
857
858                                 CALLBACK_INVOKE(clip->gpd, IDWALK_CB_USER);
859
860                                 for (track = tracking->tracks.first; track; track = track->next) {
861                                         CALLBACK_INVOKE(track->gpd, IDWALK_CB_USER);
862                                 }
863                                 for (object = tracking->objects.first; object; object = object->next) {
864                                         for (track = object->tracks.first; track; track = track->next) {
865                                                 CALLBACK_INVOKE(track->gpd, IDWALK_CB_USER);
866                                         }
867                                 }
868
869                                 for (plane_track = tracking->plane_tracks.first; plane_track; plane_track = plane_track->next) {
870                                         CALLBACK_INVOKE(plane_track->image, IDWALK_CB_USER);
871                                 }
872                                 break;
873                         }
874
875                         case ID_MSK:
876                         {
877                                 Mask *mask = (Mask *) id;
878                                 MaskLayer *mask_layer;
879                                 for (mask_layer = mask->masklayers.first; mask_layer; mask_layer = mask_layer->next) {
880                                         MaskSpline *mask_spline;
881
882                                         for (mask_spline = mask_layer->splines.first; mask_spline; mask_spline = mask_spline->next) {
883                                                 for (i = 0; i < mask_spline->tot_point; i++) {
884                                                         MaskSplinePoint *point = &mask_spline->points[i];
885                                                         CALLBACK_INVOKE_ID(point->parent.id, IDWALK_CB_USER);
886                                                 }
887                                         }
888                                 }
889                                 break;
890                         }
891
892                         case ID_LS:
893                         {
894                                 FreestyleLineStyle *linestyle = (FreestyleLineStyle *) id;
895                                 LineStyleModifier *lsm;
896                                 for (i = 0; i < MAX_MTEX; i++) {
897                                         if (linestyle->mtex[i]) {
898                                                 library_foreach_mtex(&data, linestyle->mtex[i]);
899                                         }
900                                 }
901                                 if (linestyle->nodetree) {
902                                         /* nodetree **are owned by IDs**, treat them as mere sub-data and not real ID! */
903                                         library_foreach_ID_as_subdata_link((ID **)&linestyle->nodetree, callback, user_data, flag, &data);
904                                 }
905
906                                 for (lsm = linestyle->color_modifiers.first; lsm; lsm = lsm->next) {
907                                         if (lsm->type == LS_MODIFIER_DISTANCE_FROM_OBJECT) {
908                                                 LineStyleColorModifier_DistanceFromObject *p = (LineStyleColorModifier_DistanceFromObject *)lsm;
909                                                 if (p->target) {
910                                                         CALLBACK_INVOKE(p->target, IDWALK_CB_NOP);
911                                                 }
912                                         }
913                                 }
914                                 for (lsm = linestyle->alpha_modifiers.first; lsm; lsm = lsm->next) {
915                                         if (lsm->type == LS_MODIFIER_DISTANCE_FROM_OBJECT) {
916                                                 LineStyleAlphaModifier_DistanceFromObject *p = (LineStyleAlphaModifier_DistanceFromObject *)lsm;
917                                                 if (p->target) {
918                                                         CALLBACK_INVOKE(p->target, IDWALK_CB_NOP);
919                                                 }
920                                         }
921                                 }
922                                 for (lsm = linestyle->thickness_modifiers.first; lsm; lsm = lsm->next) {
923                                         if (lsm->type == LS_MODIFIER_DISTANCE_FROM_OBJECT) {
924                                                 LineStyleThicknessModifier_DistanceFromObject *p = (LineStyleThicknessModifier_DistanceFromObject *)lsm;
925                                                 if (p->target) {
926                                                         CALLBACK_INVOKE(p->target, IDWALK_CB_NOP);
927                                                 }
928                                         }
929                                 }
930                                 break;
931                         }
932                         case ID_AC:
933                         {
934                                 bAction *act = (bAction *) id;
935
936                                 for (TimeMarker *marker = act->markers.first; marker; marker = marker->next) {
937                                         CALLBACK_INVOKE(marker->camera, IDWALK_CB_NOP);
938                                 }
939                                 break;
940                         }
941
942                         case ID_WM:
943                         {
944                                 wmWindowManager *wm = (wmWindowManager *)id;
945
946                                 for (wmWindow *win = wm->windows.first; win; win = win->next) {
947                                         ID *workspace = (ID *)BKE_workspace_active_get(win->workspace_hook);
948
949                                         CALLBACK_INVOKE(win->scene, IDWALK_CB_USER_ONE);
950
951                                         CALLBACK_INVOKE_ID(workspace, IDWALK_CB_NOP);
952                                         /* allow callback to set a different workspace */
953                                         BKE_workspace_active_set(win->workspace_hook, (WorkSpace *)workspace);
954                                 }
955                                 break;
956                         }
957
958                         case ID_WS:
959                         {
960                                 WorkSpace *workspace = (WorkSpace *)id;
961                                 ListBase *layouts = BKE_workspace_layouts_get(workspace);
962
963                                 for (WorkSpaceLayout *layout = layouts->first; layout; layout = layout->next) {
964                                         bScreen *screen = BKE_workspace_layout_screen_get(layout);
965
966                                         /* CALLBACK_INVOKE expects an actual pointer, not a variable holding the pointer.
967                                          * However we can't access layout->screen here since we are outside the workspace project. */
968                                         CALLBACK_INVOKE(screen, IDWALK_CB_USER);
969                                         /* allow callback to set a different screen */
970                                         BKE_workspace_layout_screen_set(layout, screen);
971                                 }
972                                 break;
973                         }
974                         case ID_GD:
975                         {
976                                 bGPdata *gpencil = (bGPdata *) id;
977                                 /* materials */
978                                 for (i = 0; i < gpencil->totcol; i++) {
979                                         CALLBACK_INVOKE(gpencil->mat[i], IDWALK_CB_USER);
980                                 }
981
982                                 for (bGPDlayer *gplayer = gpencil->layers.first; gplayer != NULL; gplayer = gplayer->next) {
983                                         CALLBACK_INVOKE(gplayer->parent, IDWALK_CB_NOP);
984                                 }
985
986                                 break;
987                         }
988
989                         /* Nothing needed for those... */
990                         case ID_SCR:
991                         case ID_IM:
992                         case ID_VF:
993                         case ID_TXT:
994                         case ID_SO:
995                         case ID_PAL:
996                         case ID_PC:
997                         case ID_CF:
998                                 break;
999
1000                         /* Deprecated. */
1001                         case ID_IP:
1002                                 break;
1003
1004                 }
1005         }
1006
1007 FOREACH_FINALIZE:
1008         if (data.ids_handled) {
1009                 BLI_gset_free(data.ids_handled, NULL);
1010                 BLI_LINKSTACK_FREE(data.ids_todo);
1011         }
1012
1013 #undef CALLBACK_INVOKE_ID
1014 #undef CALLBACK_INVOKE
1015 }
1016
1017 #undef FOREACH_CALLBACK_INVOKE_ID
1018 #undef FOREACH_CALLBACK_INVOKE
1019
1020 /**
1021  * re-usable function, use when replacing ID's
1022  */
1023 void BKE_library_update_ID_link_user(ID *id_dst, ID *id_src, const int cb_flag)
1024 {
1025         if (cb_flag & IDWALK_CB_USER) {
1026                 id_us_min(id_src);
1027                 id_us_plus(id_dst);
1028         }
1029         else if (cb_flag & IDWALK_CB_USER_ONE) {
1030                 id_us_ensure_real(id_dst);
1031         }
1032 }
1033
1034 /**
1035  * Say whether given \a id_type_owner can use (in any way) a datablock of \a id_type_used.
1036  *
1037  * This is a 'simplified' abstract version of #BKE_library_foreach_ID_link() above, quite useful to reduce
1038  * useless iterations in some cases.
1039  */
1040 /* XXX This has to be fully rethink, basing check on ID type is not really working anymore (and even worth once
1041  *     IDProps will support ID pointers), we'll have to do some quick checks on IDs themselves... */
1042 bool BKE_library_id_can_use_idtype(ID *id_owner, const short id_type_used)
1043 {
1044         /* any type of ID can be used in custom props. */
1045         if (id_owner->properties) {
1046                 return true;
1047         }
1048
1049         const short id_type_owner = GS(id_owner->name);
1050
1051         /* IDProps of armature bones and nodes, and bNode->id can use virtually any type of ID. */
1052         if (ELEM(id_type_owner, ID_NT, ID_AR)) {
1053                 return true;
1054         }
1055
1056         if (ntreeFromID(id_owner)) {
1057                 return true;
1058         }
1059
1060         if (BKE_animdata_from_id(id_owner)) {
1061                 return true;  /* AnimationData can use virtually any kind of datablocks, through drivers especially. */
1062         }
1063
1064         switch ((ID_Type)id_type_owner) {
1065                 case ID_LI:
1066                         return ELEM(id_type_used, ID_LI);
1067                 case ID_SCE:
1068                         return (ELEM(id_type_used, ID_OB, ID_WO, ID_SCE, ID_MC, ID_MA, ID_GR, ID_TXT,
1069                                                    ID_LS, ID_MSK, ID_SO, ID_GD, ID_BR, ID_PAL, ID_IM, ID_NT));
1070                 case ID_OB:
1071                         /* Could be more specific, but simpler to just always say 'yes' here. */
1072                         return true;
1073                 case ID_ME:
1074                         return ELEM(id_type_used, ID_ME, ID_KE, ID_MA, ID_IM);
1075                 case ID_CU:
1076                         return ELEM(id_type_used, ID_OB, ID_KE, ID_MA, ID_VF);
1077                 case ID_MB:
1078                         return ELEM(id_type_used, ID_MA);
1079                 case ID_MA:
1080                         return (ELEM(id_type_used, ID_TE, ID_GR));
1081                 case ID_TE:
1082                         return (ELEM(id_type_used, ID_IM, ID_OB));
1083                 case ID_LT:
1084                         return ELEM(id_type_used, ID_KE);
1085                 case ID_LA:
1086                         return (ELEM(id_type_used, ID_TE));
1087                 case ID_CA:
1088                         return ELEM(id_type_used, ID_OB);
1089                 case ID_KE:
1090                         return ELEM(id_type_used, ID_ME, ID_CU, ID_LT);  /* Warning! key->from, could be more types in future? */
1091                 case ID_SCR:
1092                         return ELEM(id_type_used, ID_SCE);
1093                 case ID_WO:
1094                         return (ELEM(id_type_used, ID_TE));
1095                 case ID_SPK:
1096                         return ELEM(id_type_used, ID_SO);
1097                 case ID_GR:
1098                         return ELEM(id_type_used, ID_OB, ID_GR);
1099                 case ID_NT:
1100                         /* Could be more specific, but node.id has no type restriction... */
1101                         return true;
1102                 case ID_BR:
1103                         return ELEM(id_type_used, ID_BR, ID_IM, ID_PC, ID_TE, ID_MA);
1104                 case ID_PA:
1105                         return ELEM(id_type_used, ID_OB, ID_GR, ID_TE);
1106                 case ID_MC:
1107                         return ELEM(id_type_used, ID_GD, ID_IM);
1108                 case ID_MSK:
1109                         return ELEM(id_type_used, ID_MC);  /* WARNING! mask->parent.id, not typed. */
1110                 case ID_LS:
1111                         return (ELEM(id_type_used, ID_TE, ID_OB));
1112                 case ID_LP:
1113                         return ELEM(id_type_used, ID_IM);
1114                 case ID_GD:
1115                         return ELEM(id_type_used, ID_MA);
1116                 case ID_WS:
1117                         return ELEM(id_type_used, ID_SCR, ID_SCE);
1118                 case ID_IM:
1119                 case ID_VF:
1120                 case ID_TXT:
1121                 case ID_SO:
1122                 case ID_AR:
1123                 case ID_AC:
1124                 case ID_WM:
1125                 case ID_PAL:
1126                 case ID_PC:
1127                 case ID_CF:
1128                         /* Those types never use/reference other IDs... */
1129                         return false;
1130                 case ID_IP:
1131                         /* Deprecated... */
1132                         return false;
1133         }
1134         return false;
1135 }
1136
1137
1138 /* ***** ID users iterator. ***** */
1139 typedef struct IDUsersIter {
1140         ID *id;
1141
1142         ListBase *lb_array[MAX_LIBARRAY];
1143         int lb_idx;
1144
1145         ID *curr_id;
1146         int count_direct, count_indirect;  /* Set by callback. */
1147 } IDUsersIter;
1148
1149 static int foreach_libblock_id_users_callback(void *user_data, ID *UNUSED(self_id), ID **id_p, int cb_flag)
1150 {
1151         IDUsersIter *iter = user_data;
1152
1153         if (*id_p) {
1154                 /* 'Loopback' ID pointers (the ugly 'from' ones, Object->proxy_from and Key->from).
1155                  * Those are not actually ID usage, we can ignore them here.
1156                  */
1157                 if (cb_flag & IDWALK_CB_LOOPBACK) {
1158                         return IDWALK_RET_NOP;
1159                 }
1160
1161                 if (*id_p == iter->id) {
1162 #if 0
1163                         printf("%s uses %s (refcounted: %d, userone: %d, used_one: %d, used_one_active: %d, indirect_usage: %d)\n",
1164                                    iter->curr_id->name, iter->id->name, (cb_flag & IDWALK_USER) ? 1 : 0, (cb_flag & IDWALK_USER_ONE) ? 1 : 0,
1165                                    (iter->id->tag & LIB_TAG_EXTRAUSER) ? 1 : 0, (iter->id->tag & LIB_TAG_EXTRAUSER_SET) ? 1 : 0,
1166                                    (cb_flag & IDWALK_INDIRECT_USAGE) ? 1 : 0);
1167 #endif
1168                         if (cb_flag & IDWALK_CB_INDIRECT_USAGE) {
1169                                 iter->count_indirect++;
1170                         }
1171                         else {
1172                                 iter->count_direct++;
1173                         }
1174                 }
1175         }
1176
1177         return IDWALK_RET_NOP;
1178 }
1179
1180 /**
1181  * Return the number of times given \a id_user uses/references \a id_used.
1182  *
1183  * \note This only checks for pointer references of an ID, shallow usages (like e.g. by RNA paths, as done
1184  *       for FCurves) are not detected at all.
1185  *
1186  * \param id_user: the ID which is supposed to use (reference) \a id_used.
1187  * \param id_used: the ID which is supposed to be used (referenced) by \a id_user.
1188  * \return the number of direct usages/references of \a id_used by \a id_user.
1189  */
1190 int BKE_library_ID_use_ID(ID *id_user, ID *id_used)
1191 {
1192         IDUsersIter iter;
1193
1194         /* We do not care about iter.lb_array/lb_idx here... */
1195         iter.id = id_used;
1196         iter.curr_id = id_user;
1197         iter.count_direct = iter.count_indirect = 0;
1198
1199         BKE_library_foreach_ID_link(NULL, iter.curr_id, foreach_libblock_id_users_callback, (void *)&iter, IDWALK_READONLY);
1200
1201         return iter.count_direct + iter.count_indirect;
1202 }
1203
1204 static bool library_ID_is_used(Main *bmain, void *idv, const bool check_linked)
1205 {
1206         IDUsersIter iter;
1207         ListBase *lb_array[MAX_LIBARRAY];
1208         ID *id = idv;
1209         int i = set_listbasepointers(bmain, lb_array);
1210         bool is_defined = false;
1211
1212         iter.id = id;
1213         iter.count_direct = iter.count_indirect = 0;
1214         while (i-- && !is_defined) {
1215                 ID *id_curr = lb_array[i]->first;
1216
1217                 if (!id_curr || !BKE_library_id_can_use_idtype(id_curr, GS(id->name))) {
1218                         continue;
1219                 }
1220
1221                 for (; id_curr && !is_defined; id_curr = id_curr->next) {
1222                         if (id_curr == id) {
1223                                 /* We are not interested in self-usages (mostly from drivers or bone constraints...). */
1224                                 continue;
1225                         }
1226                         iter.curr_id = id_curr;
1227                         BKE_library_foreach_ID_link(
1228                                     bmain, id_curr, foreach_libblock_id_users_callback, &iter, IDWALK_READONLY);
1229
1230                         is_defined = ((check_linked ? iter.count_indirect : iter.count_direct) != 0);
1231                 }
1232         }
1233
1234         return is_defined;
1235 }
1236
1237 /**
1238  * Check whether given ID is used locally (i.e. by another non-linked ID).
1239  */
1240 bool BKE_library_ID_is_locally_used(Main *bmain, void *idv)
1241 {
1242         return library_ID_is_used(bmain, idv, false);
1243 }
1244
1245 /**
1246  * Check whether given ID is used indirectly (i.e. by another linked ID).
1247  */
1248 bool BKE_library_ID_is_indirectly_used(Main *bmain, void *idv)
1249 {
1250         return library_ID_is_used(bmain, idv, true);
1251 }
1252
1253 /**
1254  * Combine \a BKE_library_ID_is_locally_used() and \a BKE_library_ID_is_indirectly_used() in a single call.
1255  */
1256 void BKE_library_ID_test_usages(Main *bmain, void *idv, bool *is_used_local, bool *is_used_linked)
1257 {
1258         IDUsersIter iter;
1259         ListBase *lb_array[MAX_LIBARRAY];
1260         ID *id = idv;
1261         int i = set_listbasepointers(bmain, lb_array);
1262         bool is_defined = false;
1263
1264         iter.id = id;
1265         iter.count_direct = iter.count_indirect = 0;
1266         while (i-- && !is_defined) {
1267                 ID *id_curr = lb_array[i]->first;
1268
1269                 if (!id_curr || !BKE_library_id_can_use_idtype(id_curr, GS(id->name))) {
1270                         continue;
1271                 }
1272
1273                 for (; id_curr && !is_defined; id_curr = id_curr->next) {
1274                         if (id_curr == id) {
1275                                 /* We are not interested in self-usages (mostly from drivers or bone constraints...). */
1276                                 continue;
1277                         }
1278                         iter.curr_id = id_curr;
1279                         BKE_library_foreach_ID_link(bmain, id_curr, foreach_libblock_id_users_callback, &iter, IDWALK_READONLY);
1280
1281                         is_defined = (iter.count_direct != 0 && iter.count_indirect != 0);
1282                 }
1283         }
1284
1285         *is_used_local = (iter.count_direct != 0);
1286         *is_used_linked = (iter.count_indirect != 0);
1287 }
1288
1289 /* ***** IDs usages.checking/tagging. ***** */
1290 static int foreach_libblock_used_linked_data_tag_clear_cb(
1291         void *user_data, ID *self_id, ID **id_p, int UNUSED(cb_flag))
1292 {
1293         bool *is_changed = user_data;
1294
1295         if (*id_p) {
1296                 /* XXX This is actually some kind of hack...
1297                  * Issue is, shapekeys' 'from' ID pointer is not actually ID usage.
1298                  * Maybe we should even nuke it from BKE_library_foreach_ID_link, not 100% sure yet...
1299                  */
1300                 if ((GS(self_id->name) == ID_KE) && (((Key *)self_id)->from == *id_p)) {
1301                         return IDWALK_RET_NOP;
1302                 }
1303                 /* XXX another hack, for similar reasons as above one. */
1304                 if ((GS(self_id->name) == ID_OB) && (((Object *)self_id)->proxy_from == (Object *)*id_p)) {
1305                         return IDWALK_RET_NOP;
1306                 }
1307
1308                 /* If checked id is used by an assumed used ID, then it is also used and not part of any linked archipelago. */
1309                 if (!(self_id->tag & LIB_TAG_DOIT) && ((*id_p)->tag & LIB_TAG_DOIT)) {
1310                         (*id_p)->tag &= ~LIB_TAG_DOIT;
1311                         *is_changed = true;
1312                 }
1313         }
1314
1315         return IDWALK_RET_NOP;
1316 }
1317
1318 /**
1319  * Detect orphaned linked data blocks (i.e. linked data not used (directly or indirectly) in any way by any local data),
1320  * including complex cases like 'linked archipelagoes', i.e. linked datablocks that use each other in loops,
1321  * which prevents their deletion by 'basic' usage checks...
1322  *
1323  * \param do_init_tag: if \a true, all linked data are checked, if \a false, only linked datablocks already tagged with
1324  *                    LIB_TAG_DOIT are checked.
1325  */
1326 void BKE_library_unused_linked_data_set_tag(Main *bmain, const bool do_init_tag)
1327 {
1328         ListBase *lb_array[MAX_LIBARRAY];
1329
1330         if (do_init_tag) {
1331                 int i = set_listbasepointers(bmain, lb_array);
1332
1333                 while (i--) {
1334                         for (ID *id = lb_array[i]->first; id; id = id->next) {
1335                                 if (id->lib && (id->tag & LIB_TAG_INDIRECT) != 0) {
1336                                         id->tag |= LIB_TAG_DOIT;
1337                                 }
1338                                 else {
1339                                         id->tag &= ~LIB_TAG_DOIT;
1340                                 }
1341                         }
1342                 }
1343         }
1344
1345         bool do_loop = true;
1346         while (do_loop) {
1347                 int i = set_listbasepointers(bmain, lb_array);
1348                 do_loop = false;
1349
1350                 while (i--) {
1351                         for (ID *id = lb_array[i]->first; id; id = id->next) {
1352                                 if (id->tag & LIB_TAG_DOIT) {
1353                                         /* Unused ID (so far), no need to check it further. */
1354                                         continue;
1355                                 }
1356                                 BKE_library_foreach_ID_link(
1357                                             bmain, id, foreach_libblock_used_linked_data_tag_clear_cb, &do_loop, IDWALK_READONLY);
1358                         }
1359                 }
1360         }
1361 }
1362
1363 /**
1364  * Untag linked data blocks used by other untagged linked datablocks.
1365  * Used to detect datablocks that we can forcefully make local (instead of copying them to later get rid of original):
1366  * All datablocks we want to make local are tagged by caller, after this function has ran caller knows datablocks still
1367  * tagged can directly be made local, since they are only used by other datablocks that will also be made fully local.
1368  */
1369 void BKE_library_indirectly_used_data_tag_clear(Main *bmain)
1370 {
1371         ListBase *lb_array[MAX_LIBARRAY];
1372
1373         bool do_loop = true;
1374         while (do_loop) {
1375                 int i = set_listbasepointers(bmain, lb_array);
1376                 do_loop = false;
1377
1378                 while (i--) {
1379                         for (ID *id = lb_array[i]->first; id; id = id->next) {
1380                                 if (id->lib == NULL || id->tag & LIB_TAG_DOIT) {
1381                                         /* Local or non-indirectly-used ID (so far), no need to check it further. */
1382                                         continue;
1383                                 }
1384                                 BKE_library_foreach_ID_link(
1385                                             bmain, id, foreach_libblock_used_linked_data_tag_clear_cb, &do_loop, IDWALK_READONLY);
1386                         }
1387                 }
1388         }
1389 }