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