Object Mode: remove Scene.obedit
[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                                 if (scene->ed) {
425                                         Sequence *seq;
426                                         SEQP_BEGIN(scene->ed, seq)
427                                         {
428                                                 CALLBACK_INVOKE(seq->scene, IDWALK_CB_NOP);
429                                                 CALLBACK_INVOKE(seq->scene_camera, IDWALK_CB_NOP);
430                                                 CALLBACK_INVOKE(seq->clip, IDWALK_CB_USER);
431                                                 CALLBACK_INVOKE(seq->mask, IDWALK_CB_USER);
432                                                 CALLBACK_INVOKE(seq->sound, IDWALK_CB_USER);
433                                                 library_foreach_idproperty_ID_link(&data, seq->prop, IDWALK_CB_USER);
434                                                 for (SequenceModifierData *smd = seq->modifiers.first; smd; smd = smd->next) {
435                                                         CALLBACK_INVOKE(smd->mask_id, IDWALK_CB_USER);
436                                                 }
437                                         }
438                                         SEQ_END
439                                 }
440
441                                 CALLBACK_INVOKE(scene->gpd, IDWALK_CB_USER);
442
443                                 FOREACH_SCENE_COLLECTION(scene, sc)
444                                 {
445                                         for (LinkData *link = sc->objects.first; link; link = link->next) {
446                                                 CALLBACK_INVOKE_ID(link->data, IDWALK_CB_USER);
447                                         }
448                                 }
449                                 FOREACH_SCENE_COLLECTION_END
450
451                                 ViewLayer *view_layer;
452                                 for (view_layer = scene->view_layers.first; view_layer; view_layer = view_layer->next) {
453                                         for (Base *base = view_layer->object_bases.first; base; base = base->next) {
454                                                 CALLBACK_INVOKE(base->object, IDWALK_NOP);
455                                         }
456
457                                         for (FreestyleModuleConfig  *fmc = view_layer->freestyle_config.modules.first; fmc; fmc = fmc->next) {
458                                                 if (fmc->script) {
459                                                         CALLBACK_INVOKE(fmc->script, IDWALK_CB_NOP);
460                                                 }
461                                         }
462
463                                         for (FreestyleLineSet *fls = view_layer->freestyle_config.linesets.first; fls; fls = fls->next) {
464                                                 if (fls->group) {
465                                                         CALLBACK_INVOKE(fls->group, IDWALK_CB_USER);
466                                                 }
467
468                                                 if (fls->linestyle) {
469                                                         CALLBACK_INVOKE(fls->linestyle, IDWALK_CB_USER);
470                                                 }
471                                         }
472                                 }
473
474                                 for (TimeMarker *marker = scene->markers.first; marker; marker = marker->next) {
475                                         CALLBACK_INVOKE(marker->camera, IDWALK_CB_NOP);
476                                 }
477
478                                 if (toolsett) {
479                                         CALLBACK_INVOKE(toolsett->skgen_template, IDWALK_CB_NOP);
480
481                                         CALLBACK_INVOKE(toolsett->particle.scene, IDWALK_CB_NOP);
482                                         CALLBACK_INVOKE(toolsett->particle.object, IDWALK_CB_NOP);
483                                         CALLBACK_INVOKE(toolsett->particle.shape_object, IDWALK_CB_NOP);
484
485                                         library_foreach_paint(&data, &toolsett->imapaint.paint);
486                                         CALLBACK_INVOKE(toolsett->imapaint.stencil, IDWALK_CB_USER);
487                                         CALLBACK_INVOKE(toolsett->imapaint.clone, IDWALK_CB_USER);
488                                         CALLBACK_INVOKE(toolsett->imapaint.canvas, IDWALK_CB_USER);
489
490                                         if (toolsett->vpaint) {
491                                                 library_foreach_paint(&data, &toolsett->vpaint->paint);
492                                         }
493                                         if (toolsett->wpaint) {
494                                                 library_foreach_paint(&data, &toolsett->wpaint->paint);
495                                         }
496                                         if (toolsett->sculpt) {
497                                                 library_foreach_paint(&data, &toolsett->sculpt->paint);
498                                                 CALLBACK_INVOKE(toolsett->sculpt->gravity_object, IDWALK_CB_NOP);
499                                         }
500                                         if (toolsett->uvsculpt) {
501                                                 library_foreach_paint(&data, &toolsett->uvsculpt->paint);
502                                         }
503                                 }
504
505                                 if (scene->rigidbody_world) {
506                                         BKE_rigidbody_world_id_loop(scene->rigidbody_world, library_foreach_rigidbodyworldSceneLooper, &data);
507                                 }
508
509                                 CALLBACK_INVOKE(scene->gm.dome.warptext, IDWALK_CB_NOP);
510
511                                 break;
512                         }
513
514                         case ID_OB:
515                         {
516                                 Object *object = (Object *) id;
517                                 ParticleSystem *psys;
518
519                                 /* Object is special, proxies make things hard... */
520                                 const int data_cb_flag = data.cb_flag;
521                                 const int proxy_cb_flag = ((data.flag & IDWALK_NO_INDIRECT_PROXY_DATA_USAGE) == 0 && (object->proxy || object->proxy_group)) ?
522                                                               IDWALK_CB_INDIRECT_USAGE : 0;
523
524                                 /* object data special case */
525                                 data.cb_flag |= proxy_cb_flag;
526                                 if (object->type == OB_EMPTY) {
527                                         /* empty can have NULL or Image */
528                                         CALLBACK_INVOKE_ID(object->data, IDWALK_CB_USER);
529                                 }
530                                 else {
531                                         /* when set, this can't be NULL */
532                                         if (object->data) {
533                                                 CALLBACK_INVOKE_ID(object->data, IDWALK_CB_USER | IDWALK_CB_NEVER_NULL);
534                                         }
535                                 }
536                                 data.cb_flag = data_cb_flag;
537
538                                 CALLBACK_INVOKE(object->parent, IDWALK_CB_NOP);
539                                 CALLBACK_INVOKE(object->track, IDWALK_CB_NOP);
540                                 /* object->proxy is refcounted, but not object->proxy_group... *sigh* */
541                                 CALLBACK_INVOKE(object->proxy, IDWALK_CB_USER);
542                                 CALLBACK_INVOKE(object->proxy_group, IDWALK_CB_NOP);
543
544                                 /* Special case!
545                                  * Since this field is set/owned by 'user' of this ID (and not ID itself), it is only indirect usage
546                                  * if proxy object is linked... Twisted. */
547                                 if (object->proxy_from) {
548                                         data.cb_flag = ID_IS_LINKED(object->proxy_from) ? IDWALK_CB_INDIRECT_USAGE : 0;
549                                 }
550                                 CALLBACK_INVOKE(object->proxy_from, IDWALK_CB_LOOPBACK);
551                                 data.cb_flag = data_cb_flag;
552
553                                 CALLBACK_INVOKE(object->poselib, IDWALK_CB_USER);
554
555                                 data.cb_flag |= proxy_cb_flag;
556                                 for (i = 0; i < object->totcol; i++) {
557                                         CALLBACK_INVOKE(object->mat[i], IDWALK_CB_USER);
558                                 }
559                                 data.cb_flag = data_cb_flag;
560
561                                 CALLBACK_INVOKE(object->gpd, IDWALK_CB_USER);
562                                 CALLBACK_INVOKE(object->dup_group, IDWALK_CB_USER);
563
564                                 if (object->pd) {
565                                         CALLBACK_INVOKE(object->pd->tex, IDWALK_CB_USER);
566                                         CALLBACK_INVOKE(object->pd->f_source, IDWALK_CB_NOP);
567                                 }
568                                 /* Note that ob->effect is deprecated, so no need to handle it here. */
569
570                                 if (object->pose) {
571                                         bPoseChannel *pchan;
572
573                                         data.cb_flag |= proxy_cb_flag;
574                                         for (pchan = object->pose->chanbase.first; pchan; pchan = pchan->next) {
575                                                 library_foreach_idproperty_ID_link(&data, pchan->prop, IDWALK_CB_USER);
576                                                 CALLBACK_INVOKE(pchan->custom, IDWALK_CB_USER);
577                                                 BKE_constraints_id_loop(&pchan->constraints, library_foreach_constraintObjectLooper, &data);
578                                         }
579                                         data.cb_flag = data_cb_flag;
580                                 }
581
582                                 if (object->rigidbody_constraint) {
583                                         CALLBACK_INVOKE(object->rigidbody_constraint->ob1, IDWALK_CB_NOP);
584                                         CALLBACK_INVOKE(object->rigidbody_constraint->ob2, IDWALK_CB_NOP);
585                                 }
586
587                                 if (object->lodlevels.first) {
588                                         LodLevel *level;
589                                         for (level = object->lodlevels.first; level; level = level->next) {
590                                                 CALLBACK_INVOKE(level->source, IDWALK_CB_NOP);
591                                         }
592                                 }
593
594                                 modifiers_foreachIDLink(object, library_foreach_modifiersForeachIDLink, &data);
595                                 BKE_constraints_id_loop(&object->constraints, library_foreach_constraintObjectLooper, &data);
596
597                                 for (psys = object->particlesystem.first; psys; psys = psys->next) {
598                                         BKE_particlesystem_id_loop(psys, library_foreach_particlesystemsObjectLooper, &data);
599                                 }
600
601                                 if (object->soft) {
602                                         CALLBACK_INVOKE(object->soft->collision_group, IDWALK_CB_NOP);
603
604                                         if (object->soft->effector_weights) {
605                                                 CALLBACK_INVOKE(object->soft->effector_weights->group, IDWALK_CB_NOP);
606                                         }
607                                 }
608
609                                 BKE_sca_sensors_id_loop(&object->sensors, library_foreach_sensorsObjectLooper, &data);
610                                 BKE_sca_controllers_id_loop(&object->controllers, library_foreach_controllersObjectLooper, &data);
611                                 BKE_sca_actuators_id_loop(&object->actuators, library_foreach_actuatorsObjectLooper, &data);
612                                 break;
613                         }
614
615                         case ID_AR:
616                         {
617                                 bArmature *arm = (bArmature *)id;
618
619                                 for (Bone *bone = arm->bonebase.first; bone; bone = bone->next) {
620                                         library_foreach_bone(&data, bone);
621                                 }
622                                 break;
623                         }
624
625                         case ID_ME:
626                         {
627                                 Mesh *mesh = (Mesh *) id;
628                                 CALLBACK_INVOKE(mesh->texcomesh, IDWALK_CB_USER);
629                                 CALLBACK_INVOKE(mesh->key, IDWALK_CB_USER);
630                                 for (i = 0; i < mesh->totcol; i++) {
631                                         CALLBACK_INVOKE(mesh->mat[i], IDWALK_CB_USER);
632                                 }
633                                 break;
634                         }
635
636                         case ID_CU:
637                         {
638                                 Curve *curve = (Curve *) id;
639                                 CALLBACK_INVOKE(curve->bevobj, IDWALK_CB_NOP);
640                                 CALLBACK_INVOKE(curve->taperobj, IDWALK_CB_NOP);
641                                 CALLBACK_INVOKE(curve->textoncurve, IDWALK_CB_NOP);
642                                 CALLBACK_INVOKE(curve->key, IDWALK_CB_USER);
643                                 for (i = 0; i < curve->totcol; i++) {
644                                         CALLBACK_INVOKE(curve->mat[i], IDWALK_CB_USER);
645                                 }
646                                 CALLBACK_INVOKE(curve->vfont, IDWALK_CB_USER);
647                                 CALLBACK_INVOKE(curve->vfontb, IDWALK_CB_USER);
648                                 CALLBACK_INVOKE(curve->vfonti, IDWALK_CB_USER);
649                                 CALLBACK_INVOKE(curve->vfontbi, IDWALK_CB_USER);
650                                 break;
651                         }
652
653                         case ID_MB:
654                         {
655                                 MetaBall *metaball = (MetaBall *) id;
656                                 for (i = 0; i < metaball->totcol; i++) {
657                                         CALLBACK_INVOKE(metaball->mat[i], IDWALK_CB_USER);
658                                 }
659                                 break;
660                         }
661
662                         case ID_MA:
663                         {
664                                 Material *material = (Material *) id;
665                                 for (i = 0; i < MAX_MTEX; i++) {
666                                         if (material->mtex[i]) {
667                                                 library_foreach_mtex(&data, material->mtex[i]);
668                                         }
669                                 }
670                                 if (material->nodetree) {
671                                         /* nodetree **are owned by IDs**, treat them as mere sub-data and not real ID! */
672                                         library_foreach_ID_as_subdata_link((ID **)&material->nodetree, callback, user_data, flag, &data);
673                                 }
674                                 CALLBACK_INVOKE(material->group, IDWALK_CB_USER);
675                                 CALLBACK_INVOKE(material->edit_image, IDWALK_CB_USER);
676                                 break;
677                         }
678
679                         case ID_TE:
680                         {
681                                 Tex *texture = (Tex *) id;
682                                 if (texture->nodetree) {
683                                         /* nodetree **are owned by IDs**, treat them as mere sub-data and not real ID! */
684                                         library_foreach_ID_as_subdata_link((ID **)&texture->nodetree, callback, user_data, flag, &data);
685                                 }
686                                 CALLBACK_INVOKE(texture->ima, IDWALK_CB_USER);
687                                 if (texture->env) {
688                                         CALLBACK_INVOKE(texture->env->object, IDWALK_CB_NOP);
689                                         CALLBACK_INVOKE(texture->env->ima, IDWALK_CB_USER);
690                                 }
691                                 if (texture->pd)
692                                         CALLBACK_INVOKE(texture->pd->object, IDWALK_CB_NOP);
693                                 if (texture->vd)
694                                         CALLBACK_INVOKE(texture->vd->object, IDWALK_CB_NOP);
695                                 if (texture->ot)
696                                         CALLBACK_INVOKE(texture->ot->object, IDWALK_CB_NOP);
697                                 break;
698                         }
699
700                         case ID_LT:
701                         {
702                                 Lattice *lattice = (Lattice *) id;
703                                 CALLBACK_INVOKE(lattice->key, IDWALK_CB_USER);
704                                 break;
705                         }
706
707                         case ID_LA:
708                         {
709                                 Lamp *lamp = (Lamp *) id;
710                                 for (i = 0; i < MAX_MTEX; i++) {
711                                         if (lamp->mtex[i]) {
712                                                 library_foreach_mtex(&data, lamp->mtex[i]);
713                                         }
714                                 }
715                                 if (lamp->nodetree) {
716                                         /* nodetree **are owned by IDs**, treat them as mere sub-data and not real ID! */
717                                         library_foreach_ID_as_subdata_link((ID **)&lamp->nodetree, callback, user_data, flag, &data);
718                                 }
719                                 break;
720                         }
721
722                         case ID_CA:
723                         {
724                                 Camera *camera = (Camera *) id;
725                                 CALLBACK_INVOKE(camera->dof_ob, IDWALK_CB_NOP);
726                                 break;
727                         }
728
729                         case ID_KE:
730                         {
731                                 Key *key = (Key *) id;
732                                 CALLBACK_INVOKE_ID(key->from, IDWALK_CB_LOOPBACK);
733                                 break;
734                         }
735
736                         case ID_WO:
737                         {
738                                 World *world = (World *) id;
739                                 for (i = 0; i < MAX_MTEX; i++) {
740                                         if (world->mtex[i]) {
741                                                 library_foreach_mtex(&data, world->mtex[i]);
742                                         }
743                                 }
744                                 if (world->nodetree) {
745                                         /* nodetree **are owned by IDs**, treat them as mere sub-data and not real ID! */
746                                         library_foreach_ID_as_subdata_link((ID **)&world->nodetree, callback, user_data, flag, &data);
747                                 }
748                                 break;
749                         }
750
751                         case ID_SPK:
752                         {
753                                 Speaker *speaker = (Speaker *) id;
754                                 CALLBACK_INVOKE(speaker->sound, IDWALK_CB_USER);
755                                 break;
756                         }
757
758                         case ID_LP:
759                         {
760                                 LightProbe *probe = (LightProbe *) id;
761                                 CALLBACK_INVOKE(probe->image, IDWALK_CB_USER);
762                                 break;
763                         }
764
765                         case ID_GR:
766                         {
767                                 Group *group = (Group *) id;
768                                 FOREACH_GROUP_BASE(group, base)
769                                 {
770                                         CALLBACK_INVOKE(base->object, IDWALK_CB_USER_ONE);
771                                 }
772                                 FOREACH_GROUP_BASE_END
773                                 break;
774                         }
775
776                         case ID_NT:
777                         {
778                                 bNodeTree *ntree = (bNodeTree *) id;
779                                 bNode *node;
780                                 bNodeSocket *sock;
781
782                                 CALLBACK_INVOKE(ntree->gpd, IDWALK_CB_USER);
783
784                                 for (node = ntree->nodes.first; node; node = node->next) {
785                                         CALLBACK_INVOKE_ID(node->id, IDWALK_CB_USER);
786
787                                         library_foreach_idproperty_ID_link(&data, node->prop, IDWALK_CB_USER);
788                                         for (sock = node->inputs.first; sock; sock = sock->next) {
789                                                 library_foreach_idproperty_ID_link(&data, sock->prop, IDWALK_CB_USER);
790                                         }
791                                         for (sock = node->outputs.first; sock; sock = sock->next) {
792                                                 library_foreach_idproperty_ID_link(&data, sock->prop, IDWALK_CB_USER);
793                                         }
794                                 }
795
796                                 for (sock = ntree->inputs.first; sock; sock = sock->next) {
797                                         library_foreach_idproperty_ID_link(&data, sock->prop, IDWALK_CB_USER);
798                                 }
799                                 for (sock = ntree->outputs.first; sock; sock = sock->next) {
800                                         library_foreach_idproperty_ID_link(&data, sock->prop, IDWALK_CB_USER);
801                                 }
802                                 break;
803                         }
804
805                         case ID_BR:
806                         {
807                                 Brush *brush = (Brush *) id;
808                                 CALLBACK_INVOKE(brush->toggle_brush, IDWALK_CB_NOP);
809                                 CALLBACK_INVOKE(brush->clone.image, IDWALK_CB_NOP);
810                                 CALLBACK_INVOKE(brush->paint_curve, IDWALK_CB_USER);
811                                 library_foreach_mtex(&data, &brush->mtex);
812                                 library_foreach_mtex(&data, &brush->mask_mtex);
813                                 break;
814                         }
815
816                         case ID_PA:
817                         {
818                                 ParticleSettings *psett = (ParticleSettings *) id;
819                                 CALLBACK_INVOKE(psett->dup_group, IDWALK_CB_NOP);
820                                 CALLBACK_INVOKE(psett->dup_ob, IDWALK_CB_NOP);
821                                 CALLBACK_INVOKE(psett->bb_ob, IDWALK_CB_NOP);
822                                 CALLBACK_INVOKE(psett->collision_group, IDWALK_CB_NOP);
823
824                                 for (i = 0; i < MAX_MTEX; i++) {
825                                         if (psett->mtex[i]) {
826                                                 library_foreach_mtex(&data, psett->mtex[i]);
827                                         }
828                                 }
829
830                                 if (psett->effector_weights) {
831                                         CALLBACK_INVOKE(psett->effector_weights->group, IDWALK_CB_NOP);
832                                 }
833
834                                 if (psett->pd) {
835                                         CALLBACK_INVOKE(psett->pd->tex, IDWALK_CB_USER);
836                                         CALLBACK_INVOKE(psett->pd->f_source, IDWALK_CB_NOP);
837                                 }
838                                 if (psett->pd2) {
839                                         CALLBACK_INVOKE(psett->pd2->tex, IDWALK_CB_USER);
840                                         CALLBACK_INVOKE(psett->pd2->f_source, IDWALK_CB_NOP);
841                                 }
842
843                                 if (psett->boids) {
844                                         BoidState *state;
845                                         BoidRule *rule;
846
847                                         for (state = psett->boids->states.first; state; state = state->next) {
848                                                 for (rule = state->rules.first; rule; rule = rule->next) {
849                                                         if (rule->type == eBoidRuleType_Avoid) {
850                                                                 BoidRuleGoalAvoid *gabr = (BoidRuleGoalAvoid *)rule;
851                                                                 CALLBACK_INVOKE(gabr->ob, IDWALK_CB_NOP);
852                                                         }
853                                                         else if (rule->type == eBoidRuleType_FollowLeader) {
854                                                                 BoidRuleFollowLeader *flbr = (BoidRuleFollowLeader *)rule;
855                                                                 CALLBACK_INVOKE(flbr->ob, IDWALK_CB_NOP);
856                                                         }
857                                                 }
858                                         }
859                                 }
860                                 break;
861                         }
862
863                         case ID_MC:
864                         {
865                                 MovieClip *clip = (MovieClip *) id;
866                                 MovieTracking *tracking = &clip->tracking;
867                                 MovieTrackingObject *object;
868                                 MovieTrackingTrack *track;
869                                 MovieTrackingPlaneTrack *plane_track;
870
871                                 CALLBACK_INVOKE(clip->gpd, IDWALK_CB_USER);
872
873                                 for (track = tracking->tracks.first; track; track = track->next) {
874                                         CALLBACK_INVOKE(track->gpd, IDWALK_CB_USER);
875                                 }
876                                 for (object = tracking->objects.first; object; object = object->next) {
877                                         for (track = object->tracks.first; track; track = track->next) {
878                                                 CALLBACK_INVOKE(track->gpd, IDWALK_CB_USER);
879                                         }
880                                 }
881
882                                 for (plane_track = tracking->plane_tracks.first; plane_track; plane_track = plane_track->next) {
883                                         CALLBACK_INVOKE(plane_track->image, IDWALK_CB_USER);
884                                 }
885                                 break;
886                         }
887
888                         case ID_MSK:
889                         {
890                                 Mask *mask = (Mask *) id;
891                                 MaskLayer *mask_layer;
892                                 for (mask_layer = mask->masklayers.first; mask_layer; mask_layer = mask_layer->next) {
893                                         MaskSpline *mask_spline;
894
895                                         for (mask_spline = mask_layer->splines.first; mask_spline; mask_spline = mask_spline->next) {
896                                                 for (i = 0; i < mask_spline->tot_point; i++) {
897                                                         MaskSplinePoint *point = &mask_spline->points[i];
898                                                         CALLBACK_INVOKE_ID(point->parent.id, IDWALK_CB_USER);
899                                                 }
900                                         }
901                                 }
902                                 break;
903                         }
904
905                         case ID_LS:
906                         {
907                                 FreestyleLineStyle *linestyle = (FreestyleLineStyle *) id;
908                                 LineStyleModifier *lsm;
909                                 for (i = 0; i < MAX_MTEX; i++) {
910                                         if (linestyle->mtex[i]) {
911                                                 library_foreach_mtex(&data, linestyle->mtex[i]);
912                                         }
913                                 }
914                                 if (linestyle->nodetree) {
915                                         /* nodetree **are owned by IDs**, treat them as mere sub-data and not real ID! */
916                                         library_foreach_ID_as_subdata_link((ID **)&linestyle->nodetree, callback, user_data, flag, &data);
917                                 }
918
919                                 for (lsm = linestyle->color_modifiers.first; lsm; lsm = lsm->next) {
920                                         if (lsm->type == LS_MODIFIER_DISTANCE_FROM_OBJECT) {
921                                                 LineStyleColorModifier_DistanceFromObject *p = (LineStyleColorModifier_DistanceFromObject *)lsm;
922                                                 if (p->target) {
923                                                         CALLBACK_INVOKE(p->target, IDWALK_CB_NOP);
924                                                 }
925                                         }
926                                 }
927                                 for (lsm = linestyle->alpha_modifiers.first; lsm; lsm = lsm->next) {
928                                         if (lsm->type == LS_MODIFIER_DISTANCE_FROM_OBJECT) {
929                                                 LineStyleAlphaModifier_DistanceFromObject *p = (LineStyleAlphaModifier_DistanceFromObject *)lsm;
930                                                 if (p->target) {
931                                                         CALLBACK_INVOKE(p->target, IDWALK_CB_NOP);
932                                                 }
933                                         }
934                                 }
935                                 for (lsm = linestyle->thickness_modifiers.first; lsm; lsm = lsm->next) {
936                                         if (lsm->type == LS_MODIFIER_DISTANCE_FROM_OBJECT) {
937                                                 LineStyleThicknessModifier_DistanceFromObject *p = (LineStyleThicknessModifier_DistanceFromObject *)lsm;
938                                                 if (p->target) {
939                                                         CALLBACK_INVOKE(p->target, IDWALK_CB_NOP);
940                                                 }
941                                         }
942                                 }
943                                 break;
944                         }
945                         case ID_AC:
946                         {
947                                 bAction *act = (bAction *) id;
948
949                                 for (TimeMarker *marker = act->markers.first; marker; marker = marker->next) {
950                                         CALLBACK_INVOKE(marker->camera, IDWALK_CB_NOP);
951                                 }
952                                 break;
953                         }
954
955                         case ID_WM:
956                         {
957                                 wmWindowManager *wm = (wmWindowManager *)id;
958
959                                 for (wmWindow *win = wm->windows.first; win; win = win->next) {
960                                         ID *workspace = (ID *)BKE_workspace_active_get(win->workspace_hook);
961
962                                         CALLBACK_INVOKE(win->scene, IDWALK_CB_USER_ONE);
963
964                                         CALLBACK_INVOKE_ID(workspace, IDWALK_CB_NOP);
965                                         /* allow callback to set a different workspace */
966                                         BKE_workspace_active_set(win->workspace_hook, (WorkSpace *)workspace);
967                                 }
968                                 break;
969                         }
970
971                         case ID_WS:
972                         {
973                                 WorkSpace *workspace = (WorkSpace *)id;
974                                 ListBase *layouts = BKE_workspace_layouts_get(workspace);
975
976                                 for (WorkSpaceLayout *layout = layouts->first; layout; layout = layout->next) {
977                                         bScreen *screen = BKE_workspace_layout_screen_get(layout);
978
979                                         /* CALLBACK_INVOKE expects an actual pointer, not a variable holding the pointer.
980                                          * However we can't acess layout->screen here since we are outside the workspace project. */
981                                         CALLBACK_INVOKE(screen, IDWALK_CB_NOP);
982                                         /* allow callback to set a different screen */
983                                         BKE_workspace_layout_screen_set(layout, screen);
984                                 }
985
986                                 break;
987                         }
988                         case ID_GD:
989                         {
990                                 bGPdata *gpencil = (bGPdata *) id;
991
992                                 for (bGPDlayer *gp_layer = gpencil->layers.first; gp_layer; gp_layer = gp_layer->next) {
993                                         CALLBACK_INVOKE(gp_layer->parent, IDWALK_CB_NOP);
994                                 }
995                                 break;
996                         }
997
998                         /* Nothing needed for those... */
999                         case ID_SCR:
1000                         case ID_IM:
1001                         case ID_VF:
1002                         case ID_TXT:
1003                         case ID_SO:
1004                         case ID_PAL:
1005                         case ID_PC:
1006                         case ID_CF:
1007                                 break;
1008
1009                         /* Deprecated. */
1010                         case ID_IP:
1011                                 break;
1012
1013                 }
1014         }
1015
1016 FOREACH_FINALIZE:
1017         if (data.ids_handled) {
1018                 BLI_gset_free(data.ids_handled, NULL);
1019                 BLI_LINKSTACK_FREE(data.ids_todo);
1020         }
1021
1022 #undef CALLBACK_INVOKE_ID
1023 #undef CALLBACK_INVOKE
1024 }
1025
1026 #undef FOREACH_CALLBACK_INVOKE_ID
1027 #undef FOREACH_CALLBACK_INVOKE
1028
1029 /**
1030  * re-usable function, use when replacing ID's
1031  */
1032 void BKE_library_update_ID_link_user(ID *id_dst, ID *id_src, const int cb_flag)
1033 {
1034         if (cb_flag & IDWALK_CB_USER) {
1035                 id_us_min(id_src);
1036                 id_us_plus(id_dst);
1037         }
1038         else if (cb_flag & IDWALK_CB_USER_ONE) {
1039                 id_us_ensure_real(id_dst);
1040         }
1041 }
1042
1043 /**
1044  * Say whether given \a id_type_owner can use (in any way) a datablock of \a id_type_used.
1045  *
1046  * This is a 'simplified' abstract version of #BKE_library_foreach_ID_link() above, quite useful to reduce
1047  * useless iterations in some cases.
1048  */
1049 /* XXX This has to be fully rethink, basing check on ID type is not really working anymore (and even worth once
1050  *     IDProps will support ID pointers), we'll have to do some quick checks on IDs themselves... */
1051 bool BKE_library_id_can_use_idtype(ID *id_owner, const short id_type_used)
1052 {
1053         /* any type of ID can be used in custom props. */
1054         if (id_owner->properties) {
1055                 return true;
1056         }
1057
1058         const short id_type_owner = GS(id_owner->name);
1059
1060         /* IDProps of armature bones and nodes, and bNode->id can use virtually any type of ID. */
1061         if (ELEM(id_type_owner, ID_NT, ID_AR)) {
1062                 return true;
1063         }
1064
1065         if (ntreeFromID(id_owner)) {
1066                 return true;
1067         }
1068
1069         if (BKE_animdata_from_id(id_owner)) {
1070                 return true;  /* AnimationData can use virtually any kind of datablocks, through drivers especially. */
1071         }
1072
1073         switch ((ID_Type)id_type_owner) {
1074                 case ID_LI:
1075                         return ELEM(id_type_used, ID_LI);
1076                 case ID_SCE:
1077                         return (ELEM(id_type_used, ID_OB, ID_WO, ID_SCE, ID_MC, ID_MA, ID_GR, ID_TXT,
1078                                                    ID_LS, ID_MSK, ID_SO, ID_GD, ID_BR, ID_PAL, ID_IM, ID_NT));
1079                 case ID_OB:
1080                         /* Could be the following, but simpler to just always say 'yes' here. */
1081 #if 0
1082                         return ELEM(id_type_used, ID_ME, ID_CU, ID_MB, ID_LT, ID_SPK, ID_AR, ID_LA, ID_CA,  /* obdata */
1083                                                   ID_OB, ID_MA, ID_GD, ID_GR, ID_TE, ID_PA, ID_TXT, ID_SO, ID_MC, ID_IM, ID_AC
1084                                                   /* + constraints, modifiers and game logic ID types... */);
1085 #else
1086                         return true;
1087 #endif
1088                 case ID_ME:
1089                         return ELEM(id_type_used, ID_ME, ID_KE, ID_MA, ID_IM);
1090                 case ID_CU:
1091                         return ELEM(id_type_used, ID_OB, ID_KE, ID_MA, ID_VF);
1092                 case ID_MB:
1093                         return ELEM(id_type_used, ID_MA);
1094                 case ID_MA:
1095                         return (ELEM(id_type_used, ID_TE, ID_GR));
1096                 case ID_TE:
1097                         return (ELEM(id_type_used, ID_IM, ID_OB));
1098                 case ID_LT:
1099                         return ELEM(id_type_used, ID_KE);
1100                 case ID_LA:
1101                         return (ELEM(id_type_used, ID_TE));
1102                 case ID_CA:
1103                         return ELEM(id_type_used, ID_OB);
1104                 case ID_KE:
1105                         return ELEM(id_type_used, ID_ME, ID_CU, ID_LT);  /* Warning! key->from, could be more types in future? */
1106                 case ID_SCR:
1107                         return ELEM(id_type_used, ID_SCE);
1108                 case ID_WO:
1109                         return (ELEM(id_type_used, ID_TE));
1110                 case ID_SPK:
1111                         return ELEM(id_type_used, ID_SO);
1112                 case ID_GR:
1113                         return ELEM(id_type_used, ID_OB);
1114                 case ID_NT:
1115                         /* Could be the following, but node.id has no type restriction... */
1116 #if 0
1117                         return ELEM(id_type_used, ID_GD /* + node.id types... */);
1118 #else
1119                         return true;
1120 #endif
1121                 case ID_BR:
1122                         return ELEM(id_type_used, ID_BR, ID_IM, ID_PC, ID_TE);
1123                 case ID_PA:
1124                         return ELEM(id_type_used, ID_OB, ID_GR, ID_TE);
1125                 case ID_MC:
1126                         return ELEM(id_type_used, ID_GD, ID_IM);
1127                 case ID_MSK:
1128                         return ELEM(id_type_used, ID_MC);  /* WARNING! mask->parent.id, not typed. */
1129                 case ID_LS:
1130                         return (ELEM(id_type_used, ID_TE, ID_OB));
1131                 case ID_LP:
1132                         return ELEM(id_type_used, ID_IM);
1133                 case ID_WS:
1134                 case ID_IM:
1135                 case ID_VF:
1136                 case ID_TXT:
1137                 case ID_SO:
1138                 case ID_AR:
1139                 case ID_AC:
1140                 case ID_GD:
1141                 case ID_WM:
1142                 case ID_PAL:
1143                 case ID_PC:
1144                 case ID_CF:
1145                         /* Those types never use/reference other IDs... */
1146                         return false;
1147                 case ID_IP:
1148                         /* Deprecated... */
1149                         return false;
1150         }
1151         return false;
1152 }
1153
1154
1155 /* ***** ID users iterator. ***** */
1156 typedef struct IDUsersIter {
1157         ID *id;
1158
1159         ListBase *lb_array[MAX_LIBARRAY];
1160         int lb_idx;
1161
1162         ID *curr_id;
1163         int count_direct, count_indirect;  /* Set by callback. */
1164 } IDUsersIter;
1165
1166 static int foreach_libblock_id_users_callback(void *user_data, ID *UNUSED(self_id), ID **id_p, int cb_flag)
1167 {
1168         IDUsersIter *iter = user_data;
1169
1170         if (*id_p) {
1171                 /* 'Loopback' ID pointers (the ugly 'from' ones, Object->proxy_from and Key->from).
1172                  * Those are not actually ID usage, we can ignore them here.
1173                  */
1174                 if (cb_flag & IDWALK_CB_LOOPBACK) {
1175                         return IDWALK_RET_NOP;
1176                 }
1177
1178                 if (*id_p == iter->id) {
1179 #if 0
1180                         printf("%s uses %s (refcounted: %d, userone: %d, used_one: %d, used_one_active: %d, indirect_usage: %d)\n",
1181                                    iter->curr_id->name, iter->id->name, (cb_flag & IDWALK_USER) ? 1 : 0, (cb_flag & IDWALK_USER_ONE) ? 1 : 0,
1182                                    (iter->id->tag & LIB_TAG_EXTRAUSER) ? 1 : 0, (iter->id->tag & LIB_TAG_EXTRAUSER_SET) ? 1 : 0,
1183                                    (cb_flag & IDWALK_INDIRECT_USAGE) ? 1 : 0);
1184 #endif
1185                         if (cb_flag & IDWALK_CB_INDIRECT_USAGE) {
1186                                 iter->count_indirect++;
1187                         }
1188                         else {
1189                                 iter->count_direct++;
1190                         }
1191                 }
1192         }
1193
1194         return IDWALK_RET_NOP;
1195 }
1196
1197 /**
1198  * Return the number of times given \a id_user uses/references \a id_used.
1199  *
1200  * \note This only checks for pointer references of an ID, shallow usages (like e.g. by RNA paths, as done
1201  *       for FCurves) are not detected at all.
1202  *
1203  * \param id_user the ID which is supposed to use (reference) \a id_used.
1204  * \param id_used the ID which is supposed to be used (referenced) by \a id_user.
1205  * \return the number of direct usages/references of \a id_used by \a id_user.
1206  */
1207 int BKE_library_ID_use_ID(ID *id_user, ID *id_used)
1208 {
1209         IDUsersIter iter;
1210
1211         /* We do not care about iter.lb_array/lb_idx here... */
1212         iter.id = id_used;
1213         iter.curr_id = id_user;
1214         iter.count_direct = iter.count_indirect = 0;
1215
1216         BKE_library_foreach_ID_link(NULL, iter.curr_id, foreach_libblock_id_users_callback, (void *)&iter, IDWALK_READONLY);
1217
1218         return iter.count_direct + iter.count_indirect;
1219 }
1220
1221 static bool library_ID_is_used(Main *bmain, void *idv, const bool check_linked)
1222 {
1223         IDUsersIter iter;
1224         ListBase *lb_array[MAX_LIBARRAY];
1225         ID *id = idv;
1226         int i = set_listbasepointers(bmain, lb_array);
1227         bool is_defined = false;
1228
1229         iter.id = id;
1230         iter.count_direct = iter.count_indirect = 0;
1231         while (i-- && !is_defined) {
1232                 ID *id_curr = lb_array[i]->first;
1233
1234                 if (!id_curr || !BKE_library_id_can_use_idtype(id_curr, GS(id->name))) {
1235                         continue;
1236                 }
1237
1238                 for (; id_curr && !is_defined; id_curr = id_curr->next) {
1239                         if (id_curr == id) {
1240                                 /* We are not interested in self-usages (mostly from drivers or bone constraints...). */
1241                                 continue;
1242                         }
1243                         iter.curr_id = id_curr;
1244                         BKE_library_foreach_ID_link(
1245                                     bmain, id_curr, foreach_libblock_id_users_callback, &iter, IDWALK_READONLY);
1246
1247                         is_defined = ((check_linked ? iter.count_indirect : iter.count_direct) != 0);
1248                 }
1249         }
1250
1251         return is_defined;
1252 }
1253
1254 /**
1255  * Check whether given ID is used locally (i.e. by another non-linked ID).
1256  */
1257 bool BKE_library_ID_is_locally_used(Main *bmain, void *idv)
1258 {
1259         return library_ID_is_used(bmain, idv, false);
1260 }
1261
1262 /**
1263  * Check whether given ID is used indirectly (i.e. by another linked ID).
1264  */
1265 bool BKE_library_ID_is_indirectly_used(Main *bmain, void *idv)
1266 {
1267         return library_ID_is_used(bmain, idv, true);
1268 }
1269
1270 /**
1271  * Combine \a BKE_library_ID_is_locally_used() and \a BKE_library_ID_is_indirectly_used() in a single call.
1272  */
1273 void BKE_library_ID_test_usages(Main *bmain, void *idv, bool *is_used_local, bool *is_used_linked)
1274 {
1275         IDUsersIter iter;
1276         ListBase *lb_array[MAX_LIBARRAY];
1277         ID *id = idv;
1278         int i = set_listbasepointers(bmain, lb_array);
1279         bool is_defined = false;
1280
1281         iter.id = id;
1282         iter.count_direct = iter.count_indirect = 0;
1283         while (i-- && !is_defined) {
1284                 ID *id_curr = lb_array[i]->first;
1285
1286                 if (!id_curr || !BKE_library_id_can_use_idtype(id_curr, GS(id->name))) {
1287                         continue;
1288                 }
1289
1290                 for (; id_curr && !is_defined; id_curr = id_curr->next) {
1291                         if (id_curr == id) {
1292                                 /* We are not interested in self-usages (mostly from drivers or bone constraints...). */
1293                                 continue;
1294                         }
1295                         iter.curr_id = id_curr;
1296                         BKE_library_foreach_ID_link(bmain, id_curr, foreach_libblock_id_users_callback, &iter, IDWALK_READONLY);
1297
1298                         is_defined = (iter.count_direct != 0 && iter.count_indirect != 0);
1299                 }
1300         }
1301
1302         *is_used_local = (iter.count_direct != 0);
1303         *is_used_linked = (iter.count_indirect != 0);
1304 }
1305
1306 /* ***** IDs usages.checking/tagging. ***** */
1307 static int foreach_libblock_used_linked_data_tag_clear_cb(
1308         void *user_data, ID *self_id, ID **id_p, int UNUSED(cb_flag))
1309 {
1310         bool *is_changed = user_data;
1311
1312         if (*id_p) {
1313                 /* XXX This is actually some kind of hack...
1314                  * Issue is, shapekeys' 'from' ID pointer is not actually ID usage.
1315                  * Maybe we should even nuke it from BKE_library_foreach_ID_link, not 100% sure yet...
1316                  */
1317                 if ((GS(self_id->name) == ID_KE) && (((Key *)self_id)->from == *id_p)) {
1318                         return IDWALK_RET_NOP;
1319                 }
1320                 /* XXX another hack, for similar reasons as above one. */
1321                 if ((GS(self_id->name) == ID_OB) && (((Object *)self_id)->proxy_from == (Object *)*id_p)) {
1322                         return IDWALK_RET_NOP;
1323                 }
1324
1325                 /* If checked id is used by an assumed used ID, then it is also used and not part of any linked archipelago. */
1326                 if (!(self_id->tag & LIB_TAG_DOIT) && ((*id_p)->tag & LIB_TAG_DOIT)) {
1327                         (*id_p)->tag &= ~LIB_TAG_DOIT;
1328                         *is_changed = true;
1329                 }
1330         }
1331
1332         return IDWALK_RET_NOP;
1333 }
1334
1335 /**
1336  * Detect orphaned linked data blocks (i.e. linked data not used (directly or indirectly) in any way by any local data),
1337  * including complex cases like 'linked archipelagoes', i.e. linked datablocks that use each other in loops,
1338  * which prevents their deletion by 'basic' usage checks...
1339  *
1340  * \param do_init_tag if \a true, all linked data are checked, if \a false, only linked datablocks already tagged with
1341  *                    LIB_TAG_DOIT are checked.
1342  */
1343 void BKE_library_unused_linked_data_set_tag(Main *bmain, const bool do_init_tag)
1344 {
1345         ListBase *lb_array[MAX_LIBARRAY];
1346
1347         if (do_init_tag) {
1348                 int i = set_listbasepointers(bmain, lb_array);
1349
1350                 while (i--) {
1351                         for (ID *id = lb_array[i]->first; id; id = id->next) {
1352                                 if (id->lib && (id->tag & LIB_TAG_INDIRECT) != 0) {
1353                                         id->tag |= LIB_TAG_DOIT;
1354                                 }
1355                                 else {
1356                                         id->tag &= ~LIB_TAG_DOIT;
1357                                 }
1358                         }
1359                 }
1360         }
1361
1362         bool do_loop = true;
1363         while (do_loop) {
1364                 int i = set_listbasepointers(bmain, lb_array);
1365                 do_loop = false;
1366
1367                 while (i--) {
1368                         for (ID *id = lb_array[i]->first; id; id = id->next) {
1369                                 if (id->tag & LIB_TAG_DOIT) {
1370                                         /* Unused ID (so far), no need to check it further. */
1371                                         continue;
1372                                 }
1373                                 BKE_library_foreach_ID_link(
1374                                             bmain, id, foreach_libblock_used_linked_data_tag_clear_cb, &do_loop, IDWALK_READONLY);
1375                         }
1376                 }
1377         }
1378 }
1379
1380 /**
1381  * Untag linked data blocks used by other untagged linked datablocks.
1382  * Used to detect datablocks that we can forcefully make local (instead of copying them to later get rid of original):
1383  * All datablocks we want to make local are tagged by caller, after this function has ran caller knows datablocks still
1384  * tagged can directly be made local, since they are only used by other datablocks that will also be made fully local.
1385  */
1386 void BKE_library_indirectly_used_data_tag_clear(Main *bmain)
1387 {
1388         ListBase *lb_array[MAX_LIBARRAY];
1389
1390         bool do_loop = true;
1391         while (do_loop) {
1392                 int i = set_listbasepointers(bmain, lb_array);
1393                 do_loop = false;
1394
1395                 while (i--) {
1396                         for (ID *id = lb_array[i]->first; id; id = id->next) {
1397                                 if (id->lib == NULL || id->tag & LIB_TAG_DOIT) {
1398                                         /* Local or non-indirectly-used ID (so far), no need to check it further. */
1399                                         continue;
1400                                 }
1401                                 BKE_library_foreach_ID_link(
1402                                             bmain, id, foreach_libblock_used_linked_data_tag_clear_cb, &do_loop, IDWALK_READONLY);
1403                         }
1404                 }
1405         }
1406 }