Merge branch 'master' into blender2.8
[blender.git] / source / blender / blenkernel / intern / library_query.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2014 by Blender Foundation.
19  * All rights reserved.
20  *
21  * Contributor(s): Sergey Sharybin.
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/blenkernel/intern/library_query.c
27  *  \ingroup bke
28  */
29
30 #include <stdlib.h>
31
32 #include "MEM_guardedalloc.h"
33
34 #include "DNA_actuator_types.h"
35 #include "DNA_anim_types.h"
36 #include "DNA_brush_types.h"
37 #include "DNA_camera_types.h"
38 #include "DNA_constraint_types.h"
39 #include "DNA_controller_types.h"
40 #include "DNA_group_types.h"
41 #include "DNA_gpencil_types.h"
42 #include "DNA_key_types.h"
43 #include "DNA_lamp_types.h"
44 #include "DNA_lattice_types.h"
45 #include "DNA_linestyle_types.h"
46 #include "DNA_material_types.h"
47 #include "DNA_mesh_types.h"
48 #include "DNA_meta_types.h"
49 #include "DNA_movieclip_types.h"
50 #include "DNA_mask_types.h"
51 #include "DNA_node_types.h"
52 #include "DNA_object_force.h"
53 #include "DNA_object_types.h"
54 #include "DNA_rigidbody_types.h"
55 #include "DNA_scene_types.h"
56 #include "DNA_sensor_types.h"
57 #include "DNA_sequence_types.h"
58 #include "DNA_screen_types.h"
59 #include "DNA_speaker_types.h"
60 #include "DNA_sound_types.h"
61 #include "DNA_text_types.h"
62 #include "DNA_vfont_types.h"
63 #include "DNA_world_types.h"
64
65 #include "BLI_utildefines.h"
66 #include "BLI_ghash.h"
67 #include "BLI_linklist_stack.h"
68
69 #include "BKE_animsys.h"
70 #include "BKE_constraint.h"
71 #include "BKE_fcurve.h"
72 #include "BKE_library.h"
73 #include "BKE_library_query.h"
74 #include "BKE_modifier.h"
75 #include "BKE_rigidbody.h"
76 #include "BKE_sca.h"
77 #include "BKE_sequencer.h"
78 #include "BKE_tracking.h"
79
80
81 #define FOREACH_FINALIZE _finalize
82 #define FOREACH_FINALIZE_VOID FOREACH_FINALIZE: (void)0
83
84 #define FOREACH_CALLBACK_INVOKE_ID_PP(_data, id_pp, cb_flag) \
85         if (!((_data)->status & IDWALK_STOP)) { \
86                 const int _flag = (_data)->flag; \
87                 ID *old_id = *(id_pp); \
88                 const int callback_return = (_data)->callback((_data)->user_data, (_data)->self_id, id_pp, cb_flag); \
89                 if (_flag & IDWALK_READONLY) { \
90                         BLI_assert(*(id_pp) == old_id); \
91                 } \
92                 if (old_id && (_flag & IDWALK_RECURSE)) { \
93                         if (!BLI_gset_haskey((_data)->ids_handled, old_id)) { \
94                                 BLI_gset_add((_data)->ids_handled, old_id); \
95                                 if (!(callback_return & IDWALK_RET_STOP_RECURSION)) { \
96                                         BLI_LINKSTACK_PUSH((_data)->ids_todo, old_id); \
97                                 } \
98                         } \
99                 } \
100                 if (callback_return & IDWALK_RET_STOP_ITER) { \
101                         (_data)->status |= IDWALK_STOP; \
102                         goto FOREACH_FINALIZE; \
103                 } \
104         } \
105         else { \
106                 goto FOREACH_FINALIZE; \
107         } ((void)0)
108
109 #define FOREACH_CALLBACK_INVOKE_ID(_data, id, cb_flag) \
110         { \
111                 CHECK_TYPE_ANY(id, ID *, void *); \
112                 FOREACH_CALLBACK_INVOKE_ID_PP(_data, (ID **)&(id), cb_flag); \
113         } ((void)0)
114
115 #define FOREACH_CALLBACK_INVOKE(_data, id_super, cb_flag) \
116         { \
117                 CHECK_TYPE(&((id_super)->id), ID *); \
118                 FOREACH_CALLBACK_INVOKE_ID_PP(_data, (ID **)&(id_super), cb_flag); \
119         } ((void)0)
120
121 /* status */
122 enum {
123         IDWALK_STOP     = 1 << 0,
124 };
125
126 typedef struct LibraryForeachIDData {
127         ID *self_id;
128         int flag;
129         LibraryIDLinkCallback callback;
130         void *user_data;
131         int status;
132
133         /* To handle recursion. */
134         GSet *ids_handled;  /* All IDs that are either already done, or still in ids_todo stack. */
135         BLI_LINKSTACK_DECLARE(ids_todo, ID *);
136 } LibraryForeachIDData;
137
138 static void library_foreach_rigidbodyworldSceneLooper(
139         struct RigidBodyWorld *UNUSED(rbw), ID **id_pointer, void *user_data, int cd_flag)
140 {
141         LibraryForeachIDData *data = (LibraryForeachIDData *) user_data;
142         FOREACH_CALLBACK_INVOKE_ID_PP(data, id_pointer, cd_flag);
143
144         FOREACH_FINALIZE_VOID;
145 }
146
147 static void library_foreach_modifiersForeachIDLink(
148         void *user_data, Object *UNUSED(object), ID **id_pointer, int cd_flag)
149 {
150         LibraryForeachIDData *data = (LibraryForeachIDData *) user_data;
151         FOREACH_CALLBACK_INVOKE_ID_PP(data, id_pointer, cd_flag);
152
153         FOREACH_FINALIZE_VOID;
154 }
155
156 static void library_foreach_constraintObjectLooper(bConstraint *UNUSED(con), ID **id_pointer,
157                                                    bool is_reference, void *user_data)
158 {
159         LibraryForeachIDData *data = (LibraryForeachIDData *) user_data;
160         const int cd_flag = is_reference ? IDWALK_USER : IDWALK_NOP;
161         FOREACH_CALLBACK_INVOKE_ID_PP(data, id_pointer, cd_flag);
162
163         FOREACH_FINALIZE_VOID;
164 }
165
166 static void library_foreach_sensorsObjectLooper(
167         bSensor *UNUSED(sensor), ID **id_pointer, void *user_data, int cd_flag)
168 {
169         LibraryForeachIDData *data = (LibraryForeachIDData *) user_data;
170         FOREACH_CALLBACK_INVOKE_ID_PP(data, id_pointer, cd_flag);
171
172         FOREACH_FINALIZE_VOID;
173 }
174
175 static void library_foreach_controllersObjectLooper(
176         bController *UNUSED(controller), ID **id_pointer, void *user_data, int cd_flag)
177 {
178         LibraryForeachIDData *data = (LibraryForeachIDData *) user_data;
179         FOREACH_CALLBACK_INVOKE_ID_PP(data, id_pointer, cd_flag);
180
181         FOREACH_FINALIZE_VOID;
182 }
183
184 static void library_foreach_actuatorsObjectLooper(
185         bActuator *UNUSED(actuator), ID **id_pointer, void *user_data, int cd_flag)
186 {
187         LibraryForeachIDData *data = (LibraryForeachIDData *) user_data;
188         FOREACH_CALLBACK_INVOKE_ID_PP(data, id_pointer, cd_flag);
189
190         FOREACH_FINALIZE_VOID;
191 }
192
193 static void library_foreach_nla_strip(LibraryForeachIDData *data, NlaStrip *strip)
194 {
195         NlaStrip *substrip;
196
197         FOREACH_CALLBACK_INVOKE(data, strip->act, IDWALK_USER);
198
199         for (substrip = strip->strips.first; substrip; substrip = substrip->next) {
200                 library_foreach_nla_strip(data, substrip);
201         }
202
203         FOREACH_FINALIZE_VOID;
204 }
205
206 static void library_foreach_animationData(LibraryForeachIDData *data, AnimData *adt)
207 {
208         FCurve *fcu;
209         NlaTrack *nla_track;
210         NlaStrip *nla_strip;
211
212         for (fcu = adt->drivers.first; fcu; fcu = fcu->next) {
213                 ChannelDriver *driver = fcu->driver;
214                 DriverVar *dvar;
215
216                 for (dvar = driver->variables.first; dvar; dvar = dvar->next) {
217                         /* only used targets */
218                         DRIVER_TARGETS_USED_LOOPER(dvar)
219                         {
220                                 FOREACH_CALLBACK_INVOKE_ID(data, dtar->id, IDWALK_NOP);
221                         }
222                         DRIVER_TARGETS_LOOPER_END
223                 }
224         }
225
226         FOREACH_CALLBACK_INVOKE(data, adt->action, IDWALK_USER);
227         FOREACH_CALLBACK_INVOKE(data, adt->tmpact, IDWALK_USER);
228
229         for (nla_track = adt->nla_tracks.first; nla_track; nla_track = nla_track->next) {
230                 for (nla_strip = nla_track->strips.first; nla_strip; nla_strip = nla_strip->next) {
231                         library_foreach_nla_strip(data, nla_strip);
232                 }
233         }
234
235         FOREACH_FINALIZE_VOID;
236 }
237
238 static void library_foreach_mtex(LibraryForeachIDData *data, MTex *mtex)
239 {
240         FOREACH_CALLBACK_INVOKE(data, mtex->object, IDWALK_NOP);
241         FOREACH_CALLBACK_INVOKE(data, mtex->tex, IDWALK_USER);
242
243         FOREACH_FINALIZE_VOID;
244 }
245
246 static void library_foreach_paint(LibraryForeachIDData *data, Paint *paint)
247 {
248         FOREACH_CALLBACK_INVOKE(data, paint->brush, IDWALK_USER);
249         FOREACH_CALLBACK_INVOKE(data, paint->palette, IDWALK_USER);
250
251         FOREACH_FINALIZE_VOID;
252 }
253
254
255 /**
256  * Loop over all of the ID's this datablock links to.
257  *
258  * \note: May be extended to be recursive in the future.
259  */
260 void BKE_library_foreach_ID_link(ID *id, LibraryIDLinkCallback callback, void *user_data, int flag)
261 {
262         LibraryForeachIDData data;
263         int i;
264
265         if (flag & IDWALK_RECURSE) {
266                 /* For now, recusion implies read-only. */
267                 flag |= IDWALK_READONLY;
268
269                 data.ids_handled = BLI_gset_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, __func__);
270                 BLI_LINKSTACK_INIT(data.ids_todo);
271         }
272         else {
273                 data.ids_handled = NULL;
274         }
275         data.flag = flag;
276         data.status = 0;
277         data.callback = callback;
278         data.user_data = user_data;
279
280 #define CALLBACK_INVOKE_ID(check_id, cb_flag) \
281         FOREACH_CALLBACK_INVOKE_ID(&data, check_id, cb_flag)
282
283 #define CALLBACK_INVOKE(check_id_super, cb_flag) \
284         FOREACH_CALLBACK_INVOKE(&data, check_id_super, cb_flag)
285
286         do {
287                 data.self_id = id;
288
289                 AnimData *adt = BKE_animdata_from_id(id);
290                 if (adt) {
291                         library_foreach_animationData(&data, adt);
292                 }
293
294                 switch (GS(id->name)) {
295                         case ID_LI:
296                         {
297                                 Library *lib = (Library *) id;
298                                 CALLBACK_INVOKE(lib->parent, IDWALK_NOP);
299                                 break;
300                         }
301                         case ID_SCE:
302                         {
303                                 Scene *scene = (Scene *) id;
304                                 ToolSettings *toolsett = scene->toolsettings;
305                                 SceneRenderLayer *srl;
306                                 Base *base;
307
308                                 CALLBACK_INVOKE(scene->camera, IDWALK_NOP);
309                                 CALLBACK_INVOKE(scene->world, IDWALK_USER);
310                                 CALLBACK_INVOKE(scene->set, IDWALK_NOP);
311                                 CALLBACK_INVOKE(scene->clip, IDWALK_USER);
312                                 if (scene->nodetree) {
313                                         /* nodetree **are owned by IDs**, treat them as mere sub-data and not real ID! */
314                                         BKE_library_foreach_ID_link((ID *)scene->nodetree, callback, user_data, flag);
315                                 }
316                                 /* DO NOT handle scene->basact here, it's doubling with the loop over whole scene->base later,
317                                  * since basact is just a pointer to one of those items. */
318                                 CALLBACK_INVOKE(scene->obedit, IDWALK_NOP);
319
320                                 for (srl = scene->r.layers.first; srl; srl = srl->next) {
321                                         FreestyleModuleConfig *fmc;
322                                         FreestyleLineSet *fls;
323
324                                         if (srl->mat_override) {
325                                                 CALLBACK_INVOKE(srl->mat_override, IDWALK_USER);
326                                         }
327                                         if (srl->light_override) {
328                                                 CALLBACK_INVOKE(srl->light_override, IDWALK_USER);
329                                         }
330                                         for (fmc = srl->freestyleConfig.modules.first; fmc; fmc = fmc->next) {
331                                                 if (fmc->script) {
332                                                         CALLBACK_INVOKE(fmc->script, IDWALK_NOP);
333                                                 }
334                                         }
335                                         for (fls = srl->freestyleConfig.linesets.first; fls; fls = fls->next) {
336                                                 if (fls->group) {
337                                                         CALLBACK_INVOKE(fls->group, IDWALK_USER);
338                                                 }
339                                                 if (fls->linestyle) {
340                                                         CALLBACK_INVOKE(fls->linestyle, IDWALK_USER);
341                                                 }
342                                         }
343                                 }
344
345                                 if (scene->ed) {
346                                         Sequence *seq;
347                                         SEQP_BEGIN(scene->ed, seq)
348                                         {
349                                                 CALLBACK_INVOKE(seq->scene, IDWALK_NOP);
350                                                 CALLBACK_INVOKE(seq->scene_camera, IDWALK_NOP);
351                                                 CALLBACK_INVOKE(seq->clip, IDWALK_USER);
352                                                 CALLBACK_INVOKE(seq->mask, IDWALK_USER);
353                                                 CALLBACK_INVOKE(seq->sound, IDWALK_USER);
354                                                 for (SequenceModifierData *smd = seq->modifiers.first; smd; smd = smd->next) {
355                                                         CALLBACK_INVOKE(smd->mask_id, IDWALK_USER);
356                                                 }
357                                         }
358                                         SEQ_END
359                                 }
360
361                                 CALLBACK_INVOKE(scene->gpd, IDWALK_USER);
362
363                                 for (base = scene->base.first; base; base = base->next) {
364                                         CALLBACK_INVOKE(base->object, IDWALK_USER);
365                                 }
366
367                                 if (toolsett) {
368                                         CALLBACK_INVOKE(toolsett->skgen_template, IDWALK_NOP);
369
370                                         library_foreach_paint(&data, &toolsett->imapaint.paint);
371                                         CALLBACK_INVOKE(toolsett->imapaint.stencil, IDWALK_USER);
372                                         CALLBACK_INVOKE(toolsett->imapaint.clone, IDWALK_USER);
373                                         CALLBACK_INVOKE(toolsett->imapaint.canvas, IDWALK_USER);
374
375                                         if (toolsett->vpaint) {
376                                                 library_foreach_paint(&data, &toolsett->vpaint->paint);
377                                         }
378                                         if (toolsett->wpaint) {
379                                                 library_foreach_paint(&data, &toolsett->wpaint->paint);
380                                         }
381                                         if (toolsett->sculpt) {
382                                                 library_foreach_paint(&data, &toolsett->sculpt->paint);
383                                                 CALLBACK_INVOKE(toolsett->sculpt->gravity_object, IDWALK_NOP);
384                                         }
385                                         if (toolsett->uvsculpt) {
386                                                 library_foreach_paint(&data, &toolsett->uvsculpt->paint);
387                                         }
388                                 }
389
390                                 if (scene->rigidbody_world) {
391                                         BKE_rigidbody_world_id_loop(scene->rigidbody_world, library_foreach_rigidbodyworldSceneLooper, &data);
392                                 }
393
394                                 CALLBACK_INVOKE(scene->gm.dome.warptext, IDWALK_NOP);
395
396                                 break;
397                         }
398
399                         case ID_OB:
400                         {
401                                 Object *object = (Object *) id;
402
403                                 /* object data special case */
404                                 if (object->type == OB_EMPTY) {
405                                         /* empty can have NULL or Image */
406                                         CALLBACK_INVOKE_ID(object->data, IDWALK_USER);
407                                 }
408                                 else {
409                                         /* when set, this can't be NULL */
410                                         if (object->data) {
411                                                 CALLBACK_INVOKE_ID(object->data, IDWALK_USER | IDWALK_NEVER_NULL);
412                                         }
413                                 }
414
415                                 CALLBACK_INVOKE(object->parent, IDWALK_NOP);
416                                 CALLBACK_INVOKE(object->track, IDWALK_NOP);
417                                 /* object->proxy is refcounted, but not object->proxy_group... *sigh* */
418                                 CALLBACK_INVOKE(object->proxy, IDWALK_USER);
419                                 CALLBACK_INVOKE(object->proxy_group, IDWALK_NOP);
420                                 CALLBACK_INVOKE(object->proxy_from, IDWALK_NOP);
421                                 CALLBACK_INVOKE(object->poselib, IDWALK_USER);
422                                 for (i = 0; i < object->totcol; i++) {
423                                         CALLBACK_INVOKE(object->mat[i], IDWALK_USER);
424                                 }
425                                 CALLBACK_INVOKE(object->gpd, IDWALK_USER);
426                                 CALLBACK_INVOKE(object->dup_group, IDWALK_USER);
427
428                                 if (object->pd) {
429                                         CALLBACK_INVOKE(object->pd->tex, IDWALK_USER);
430                                         CALLBACK_INVOKE(object->pd->f_source, IDWALK_NOP);
431                                 }
432                                 /* Note that ob->effect is deprecated, so no need to handle it here. */
433
434                                 if (object->pose) {
435                                         bPoseChannel *pchan;
436                                         for (pchan = object->pose->chanbase.first; pchan; pchan = pchan->next) {
437                                                 CALLBACK_INVOKE(pchan->custom, IDWALK_USER);
438                                                 BKE_constraints_id_loop(&pchan->constraints, library_foreach_constraintObjectLooper, &data);
439                                         }
440                                 }
441
442                                 if (object->rigidbody_constraint) {
443                                         CALLBACK_INVOKE(object->rigidbody_constraint->ob1, IDWALK_NOP);
444                                         CALLBACK_INVOKE(object->rigidbody_constraint->ob2, IDWALK_NOP);
445                                 }
446
447                                 if (object->lodlevels.first) {
448                                         LodLevel *level;
449                                         for (level = object->lodlevels.first; level; level = level->next) {
450                                                 CALLBACK_INVOKE(level->source, IDWALK_NOP);
451                                         }
452                                 }
453
454                                 modifiers_foreachIDLink(object, library_foreach_modifiersForeachIDLink, &data);
455                                 BKE_constraints_id_loop(&object->constraints, library_foreach_constraintObjectLooper, &data);
456
457                                 BKE_sca_sensors_id_loop(&object->sensors, library_foreach_sensorsObjectLooper, &data);
458                                 BKE_sca_controllers_id_loop(&object->controllers, library_foreach_controllersObjectLooper, &data);
459                                 BKE_sca_actuators_id_loop(&object->actuators, library_foreach_actuatorsObjectLooper, &data);
460                                 break;
461                         }
462
463                         case ID_ME:
464                         {
465                                 Mesh *mesh = (Mesh *) id;
466                                 CALLBACK_INVOKE(mesh->texcomesh, IDWALK_USER);
467                                 CALLBACK_INVOKE(mesh->key, IDWALK_USER);
468                                 for (i = 0; i < mesh->totcol; i++) {
469                                         CALLBACK_INVOKE(mesh->mat[i], IDWALK_USER);
470                                 }
471                                 break;
472                         }
473
474                         case ID_CU:
475                         {
476                                 Curve *curve = (Curve *) id;
477                                 CALLBACK_INVOKE(curve->bevobj, IDWALK_NOP);
478                                 CALLBACK_INVOKE(curve->taperobj, IDWALK_NOP);
479                                 CALLBACK_INVOKE(curve->textoncurve, IDWALK_NOP);
480                                 CALLBACK_INVOKE(curve->key, IDWALK_USER);
481                                 for (i = 0; i < curve->totcol; i++) {
482                                         CALLBACK_INVOKE(curve->mat[i], IDWALK_USER);
483                                 }
484                                 CALLBACK_INVOKE(curve->vfont, IDWALK_USER);
485                                 CALLBACK_INVOKE(curve->vfontb, IDWALK_USER);
486                                 CALLBACK_INVOKE(curve->vfonti, IDWALK_USER);
487                                 CALLBACK_INVOKE(curve->vfontbi, IDWALK_USER);
488                                 break;
489                         }
490
491                         case ID_MB:
492                         {
493                                 MetaBall *metaball = (MetaBall *) id;
494                                 for (i = 0; i < metaball->totcol; i++) {
495                                         CALLBACK_INVOKE(metaball->mat[i], IDWALK_USER);
496                                 }
497                                 break;
498                         }
499
500                         case ID_MA:
501                         {
502                                 Material *material = (Material *) id;
503                                 for (i = 0; i < MAX_MTEX; i++) {
504                                         if (material->mtex[i]) {
505                                                 library_foreach_mtex(&data, material->mtex[i]);
506                                         }
507                                 }
508                                 if (material->nodetree) {
509                                         /* nodetree **are owned by IDs**, treat them as mere sub-data and not real ID! */
510                                         BKE_library_foreach_ID_link((ID *)material->nodetree, callback, user_data, flag);
511                                 }
512                                 CALLBACK_INVOKE(material->group, IDWALK_USER);
513                                 break;
514                         }
515
516                         case ID_TE:
517                         {
518                                 Tex *texture = (Tex *) id;
519                                 if (texture->nodetree) {
520                                         /* nodetree **are owned by IDs**, treat them as mere sub-data and not real ID! */
521                                         BKE_library_foreach_ID_link((ID *)texture->nodetree, callback, user_data, flag);
522                                 }
523                                 CALLBACK_INVOKE(texture->ima, IDWALK_USER);
524                                 if (texture->env) {
525                                         CALLBACK_INVOKE(texture->env->object, IDWALK_NOP);
526                                         CALLBACK_INVOKE(texture->env->ima, IDWALK_USER);
527                                 }
528                                 if (texture->pd)
529                                         CALLBACK_INVOKE(texture->pd->object, IDWALK_NOP);
530                                 if (texture->vd)
531                                         CALLBACK_INVOKE(texture->vd->object, IDWALK_NOP);
532                                 if (texture->ot)
533                                         CALLBACK_INVOKE(texture->ot->object, IDWALK_NOP);
534                                 break;
535                         }
536
537                         case ID_LT:
538                         {
539                                 Lattice *lattice = (Lattice *) id;
540                                 CALLBACK_INVOKE(lattice->key, IDWALK_USER);
541                                 break;
542                         }
543
544                         case ID_LA:
545                         {
546                                 Lamp *lamp = (Lamp *) id;
547                                 for (i = 0; i < MAX_MTEX; i++) {
548                                         if (lamp->mtex[i]) {
549                                                 library_foreach_mtex(&data, lamp->mtex[i]);
550                                         }
551                                 }
552                                 if (lamp->nodetree) {
553                                         /* nodetree **are owned by IDs**, treat them as mere sub-data and not real ID! */
554                                         BKE_library_foreach_ID_link((ID *)lamp->nodetree, callback, user_data, flag);
555                                 }
556                                 break;
557                         }
558
559                         case ID_CA:
560                         {
561                                 Camera *camera = (Camera *) id;
562                                 CALLBACK_INVOKE(camera->dof_ob, IDWALK_NOP);
563                                 break;
564                         }
565
566                         case ID_KE:
567                         {
568                                 Key *key = (Key *) id;
569                                 CALLBACK_INVOKE_ID(key->from, IDWALK_NOP);
570                                 break;
571                         }
572
573                         case ID_SCR:
574                         {
575                                 bScreen *screen = (bScreen *) id;
576                                 CALLBACK_INVOKE(screen->scene, IDWALK_USER_ONE);
577                                 break;
578                         }
579
580                         case ID_WO:
581                         {
582                                 World *world = (World *) id;
583                                 for (i = 0; i < MAX_MTEX; i++) {
584                                         if (world->mtex[i]) {
585                                                 library_foreach_mtex(&data, world->mtex[i]);
586                                         }
587                                 }
588                                 if (world->nodetree) {
589                                         /* nodetree **are owned by IDs**, treat them as mere sub-data and not real ID! */
590                                         BKE_library_foreach_ID_link((ID *)world->nodetree, callback, user_data, flag);
591                                 }
592                                 break;
593                         }
594
595                         case ID_SPK:
596                         {
597                                 Speaker *speaker = (Speaker *) id;
598                                 CALLBACK_INVOKE(speaker->sound, IDWALK_USER);
599                                 break;
600                         }
601
602                         case ID_GR:
603                         {
604                                 Group *group = (Group *) id;
605                                 GroupObject *gob;
606                                 for (gob = group->gobject.first; gob; gob = gob->next) {
607                                         CALLBACK_INVOKE(gob->ob, IDWALK_USER_ONE);
608                                 }
609                                 break;
610                         }
611
612                         case ID_NT:
613                         {
614                                 bNodeTree *ntree = (bNodeTree *) id;
615                                 bNode *node;
616                                 CALLBACK_INVOKE(ntree->gpd, IDWALK_USER);
617                                 for (node = ntree->nodes.first; node; node = node->next) {
618                                         CALLBACK_INVOKE_ID(node->id, IDWALK_USER);
619                                 }
620                                 break;
621                         }
622
623                         case ID_BR:
624                         {
625                                 Brush *brush = (Brush *) id;
626                                 CALLBACK_INVOKE(brush->toggle_brush, IDWALK_NOP);
627                                 CALLBACK_INVOKE(brush->clone.image, IDWALK_NOP);
628                                 CALLBACK_INVOKE(brush->paint_curve, IDWALK_USER);
629                                 library_foreach_mtex(&data, &brush->mtex);
630                                 library_foreach_mtex(&data, &brush->mask_mtex);
631                                 break;
632                         }
633
634                         case ID_MC:
635                         {
636                                 MovieClip *clip = (MovieClip *) id;
637                                 MovieTracking *tracking = &clip->tracking;
638                                 MovieTrackingObject *object;
639                                 MovieTrackingTrack *track;
640                                 MovieTrackingPlaneTrack *plane_track;
641
642                                 CALLBACK_INVOKE(clip->gpd, IDWALK_USER);
643
644                                 for (track = tracking->tracks.first; track; track = track->next) {
645                                         CALLBACK_INVOKE(track->gpd, IDWALK_USER);
646                                 }
647                                 for (object = tracking->objects.first; object; object = object->next) {
648                                         for (track = object->tracks.first; track; track = track->next) {
649                                                 CALLBACK_INVOKE(track->gpd, IDWALK_USER);
650                                         }
651                                 }
652
653                                 for (plane_track = tracking->plane_tracks.first; plane_track; plane_track = plane_track->next) {
654                                         CALLBACK_INVOKE(plane_track->image, IDWALK_USER);
655                                 }
656                                 break;
657                         }
658
659                         case ID_MSK:
660                         {
661                                 Mask *mask = (Mask *) id;
662                                 MaskLayer *mask_layer;
663                                 for (mask_layer = mask->masklayers.first; mask_layer; mask_layer = mask_layer->next) {
664                                         MaskSpline *mask_spline;
665
666                                         for (mask_spline = mask_layer->splines.first; mask_spline; mask_spline = mask_spline->next) {
667                                                 for (i = 0; i < mask_spline->tot_point; i++) {
668                                                         MaskSplinePoint *point = &mask_spline->points[i];
669                                                         CALLBACK_INVOKE_ID(point->parent.id, IDWALK_USER);
670                                                 }
671                                         }
672                                 }
673                                 break;
674                         }
675
676                         case ID_LS:
677                         {
678                                 FreestyleLineStyle *linestyle = (FreestyleLineStyle *) id;
679                                 LineStyleModifier *lsm;
680                                 for (i = 0; i < MAX_MTEX; i++) {
681                                         if (linestyle->mtex[i]) {
682                                                 library_foreach_mtex(&data, linestyle->mtex[i]);
683                                         }
684                                 }
685                                 if (linestyle->nodetree) {
686                                         /* nodetree **are owned by IDs**, treat them as mere sub-data and not real ID! */
687                                         BKE_library_foreach_ID_link((ID *)linestyle->nodetree, callback, user_data, flag);
688                                 }
689
690                                 for (lsm = linestyle->color_modifiers.first; lsm; lsm = lsm->next) {
691                                         if (lsm->type == LS_MODIFIER_DISTANCE_FROM_OBJECT) {
692                                                 LineStyleColorModifier_DistanceFromObject *p = (LineStyleColorModifier_DistanceFromObject *)lsm;
693                                                 if (p->target) {
694                                                         CALLBACK_INVOKE(p->target, IDWALK_NOP);
695                                                 }
696                                         }
697                                 }
698                                 for (lsm = linestyle->alpha_modifiers.first; lsm; lsm = lsm->next) {
699                                         if (lsm->type == LS_MODIFIER_DISTANCE_FROM_OBJECT) {
700                                                 LineStyleAlphaModifier_DistanceFromObject *p = (LineStyleAlphaModifier_DistanceFromObject *)lsm;
701                                                 if (p->target) {
702                                                         CALLBACK_INVOKE(p->target, IDWALK_NOP);
703                                                 }
704                                         }
705                                 }
706                                 for (lsm = linestyle->thickness_modifiers.first; lsm; lsm = lsm->next) {
707                                         if (lsm->type == LS_MODIFIER_DISTANCE_FROM_OBJECT) {
708                                                 LineStyleThicknessModifier_DistanceFromObject *p = (LineStyleThicknessModifier_DistanceFromObject *)lsm;
709                                                 if (p->target) {
710                                                         CALLBACK_INVOKE(p->target, IDWALK_NOP);
711                                                 }
712                                         }
713                                 }
714                                 break;
715                         }
716                 }
717         } while ((id = (flag & IDWALK_RECURSE) ? BLI_LINKSTACK_POP(data.ids_todo) : NULL));
718
719 FOREACH_FINALIZE:
720         if (data.ids_handled) {
721                 BLI_gset_free(data.ids_handled, NULL);
722                 BLI_LINKSTACK_FREE(data.ids_todo);
723         }
724
725 #undef CALLBACK_INVOKE_ID
726 #undef CALLBACK_INVOKE
727 }
728
729 #undef FOREACH_CALLBACK_INVOKE_ID
730 #undef FOREACH_CALLBACK_INVOKE
731
732 /**
733  * re-usable function, use when replacing ID's
734  */
735 void BKE_library_update_ID_link_user(ID *id_dst, ID *id_src, const int cd_flag)
736 {
737         if (cd_flag & IDWALK_USER) {
738                 id_us_min(id_src);
739                 id_us_plus(id_dst);
740         }
741         else if (cd_flag & IDWALK_USER_ONE) {
742                 id_us_ensure_real(id_dst);
743         }
744 }
745
746 /* ***** ID users iterator. ***** */
747 typedef struct IDUsersIter {
748         ID *id;
749
750         ListBase *lb_array[MAX_LIBARRAY];
751         int lb_idx;
752
753         ID *curr_id;
754         int count;  /* Set by callback. */
755 } IDUsersIter;
756
757 static int foreach_libblock_id_users_callback(void *user_data, ID *UNUSED(self_id), ID **id_p, int cb_flag)
758 {
759         IDUsersIter *iter = user_data;
760
761         if (*id_p && (*id_p == iter->id)) {
762 #if 0
763                 printf("%s uses %s (refcounted: %d, userone: %d, used_one: %d, used_one_active: %d)\n",
764                        iter->curr_id->name, iter->id->name, (cb_flag & IDWALK_USER) ? 1 : 0, (cb_flag & IDWALK_USER_ONE) ? 1 : 0,
765                        (iter->id->tag & LIB_TAG_EXTRAUSER) ? 1 : 0, (iter->id->tag & LIB_TAG_EXTRAUSER_SET) ? 1 : 0);
766 #else
767                 UNUSED_VARS(cb_flag);
768 #endif
769                 iter->count++;
770         }
771
772         return IDWALK_RET_NOP;
773 }
774
775 /**
776  * Return the number of times given \a id_user uses/references \a id_used.
777  *
778  * \note This only checks for pointer references of an ID, shallow usages (like e.g. by RNA paths, as done
779  *       for FCurves) are not detected at all.
780  *
781  * \param id_user the ID which is supposed to use (reference) \a id_used.
782  * \param id_used the ID which is supposed to be used (referenced) by \a id_user.
783  * \return the number of direct usages/references of \a id_used by \a id_user.
784  */
785 int BKE_library_ID_use_ID(ID *id_user, ID *id_used)
786 {
787         IDUsersIter iter;
788
789         /* We do not care about iter.lb_array/lb_idx here... */
790         iter.id = id_used;
791         iter.curr_id = id_user;
792         iter.count = 0;
793
794         BKE_library_foreach_ID_link(iter.curr_id, foreach_libblock_id_users_callback, (void *)&iter, IDWALK_NOP);
795
796         return iter.count;
797 }