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