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