2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version 2
5 * of the License, or (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software Foundation,
14 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * The Original Code is Copyright (C) 2014 by Blender Foundation.
17 * All rights reserved.
26 #include "MEM_guardedalloc.h"
28 #include "DNA_anim_types.h"
29 #include "DNA_armature_types.h"
30 #include "DNA_brush_types.h"
31 #include "DNA_camera_types.h"
32 #include "DNA_collection_types.h"
33 #include "DNA_constraint_types.h"
34 #include "DNA_gpencil_types.h"
35 #include "DNA_key_types.h"
36 #include "DNA_light_types.h"
37 #include "DNA_lattice_types.h"
38 #include "DNA_linestyle_types.h"
39 #include "DNA_material_types.h"
40 #include "DNA_mesh_types.h"
41 #include "DNA_meshdata_types.h"
42 #include "DNA_meta_types.h"
43 #include "DNA_movieclip_types.h"
44 #include "DNA_mask_types.h"
45 #include "DNA_node_types.h"
46 #include "DNA_object_force_types.h"
47 #include "DNA_lightprobe_types.h"
48 #include "DNA_rigidbody_types.h"
49 #include "DNA_scene_types.h"
50 #include "DNA_sequence_types.h"
51 #include "DNA_screen_types.h"
52 #include "DNA_speaker_types.h"
53 #include "DNA_sound_types.h"
54 #include "DNA_text_types.h"
55 #include "DNA_vfont_types.h"
56 #include "DNA_windowmanager_types.h"
57 #include "DNA_workspace_types.h"
58 #include "DNA_world_types.h"
60 #include "BLI_utildefines.h"
61 #include "BLI_ghash.h"
62 #include "BLI_linklist_stack.h"
64 #include "BKE_animsys.h"
65 #include "BKE_collection.h"
66 #include "BKE_constraint.h"
67 #include "BKE_fcurve.h"
68 #include "BKE_gpencil_modifier.h"
69 #include "BKE_idprop.h"
70 #include "BKE_lib_id.h"
71 #include "BKE_lib_query.h"
73 #include "BKE_modifier.h"
75 #include "BKE_particle.h"
76 #include "BKE_rigidbody.h"
77 #include "BKE_sequencer.h"
78 #include "BKE_shader_fx.h"
79 #include "BKE_tracking.h"
80 #include "BKE_workspace.h"
82 #define FOREACH_FINALIZE _finalize
83 #define FOREACH_FINALIZE_VOID \
85 goto FOREACH_FINALIZE; \
90 #define FOREACH_CALLBACK_INVOKE_ID_PP(_data, id_pp, _cb_flag) \
91 CHECK_TYPE(id_pp, ID **); \
92 if (!((_data)->status & IDWALK_STOP)) { \
93 const int _flag = (_data)->flag; \
94 ID *old_id = *(id_pp); \
95 const int callback_return = (_data)->callback((_data)->user_data, \
98 (_cb_flag | (_data)->cb_flag) & \
99 ~(_data)->cb_flag_clear); \
100 if (_flag & IDWALK_READONLY) { \
101 BLI_assert(*(id_pp) == old_id); \
103 if (old_id && (_flag & IDWALK_RECURSE)) { \
104 if (BLI_gset_add((_data)->ids_handled, old_id)) { \
105 if (!(callback_return & IDWALK_RET_STOP_RECURSION)) { \
106 BLI_LINKSTACK_PUSH((_data)->ids_todo, old_id); \
110 if (callback_return & IDWALK_RET_STOP_ITER) { \
111 (_data)->status |= IDWALK_STOP; \
112 goto FOREACH_FINALIZE; \
116 goto FOREACH_FINALIZE; \
120 #define FOREACH_CALLBACK_INVOKE_ID(_data, id, cb_flag) \
122 CHECK_TYPE_ANY(id, ID *, void *); \
123 FOREACH_CALLBACK_INVOKE_ID_PP(_data, (ID **)&(id), cb_flag); \
127 #define FOREACH_CALLBACK_INVOKE(_data, id_super, cb_flag) \
129 CHECK_TYPE(&((id_super)->id), ID *); \
130 FOREACH_CALLBACK_INVOKE_ID_PP(_data, (ID **)&(id_super), cb_flag); \
136 IDWALK_STOP = 1 << 0,
139 typedef struct LibraryForeachIDData {
144 LibraryIDLinkCallback callback;
148 /* To handle recursion. */
149 GSet *ids_handled; /* All IDs that are either already done, or still in ids_todo stack. */
150 BLI_LINKSTACK_DECLARE(ids_todo, ID *);
151 } LibraryForeachIDData;
153 static void library_foreach_ID_link(Main *bmain,
155 LibraryIDLinkCallback callback,
158 LibraryForeachIDData *inherit_data);
160 static void library_foreach_idproperty_ID_link(LibraryForeachIDData *data,
168 switch (prop->type) {
170 for (IDProperty *loop = prop->data.group.first; loop; loop = loop->next) {
171 library_foreach_idproperty_ID_link(data, loop, flag);
176 IDProperty *loop = IDP_Array(prop);
177 for (int i = 0; i < prop->len; i++) {
178 library_foreach_idproperty_ID_link(data, &loop[i], flag);
183 FOREACH_CALLBACK_INVOKE_ID(data, prop->data.pointer, flag);
186 break; /* Nothing to do here with other types of IDProperties... */
189 FOREACH_FINALIZE_VOID;
192 static void library_foreach_rigidbodyworldSceneLooper(struct RigidBodyWorld *UNUSED(rbw),
197 LibraryForeachIDData *data = (LibraryForeachIDData *)user_data;
198 FOREACH_CALLBACK_INVOKE_ID_PP(data, id_pointer, cb_flag);
200 FOREACH_FINALIZE_VOID;
203 static void library_foreach_modifiersForeachIDLink(void *user_data,
204 Object *UNUSED(object),
208 LibraryForeachIDData *data = (LibraryForeachIDData *)user_data;
209 FOREACH_CALLBACK_INVOKE_ID_PP(data, id_pointer, cb_flag);
211 FOREACH_FINALIZE_VOID;
214 static void library_foreach_gpencil_modifiersForeachIDLink(void *user_data,
215 Object *UNUSED(object),
219 LibraryForeachIDData *data = (LibraryForeachIDData *)user_data;
220 FOREACH_CALLBACK_INVOKE_ID_PP(data, id_pointer, cb_flag);
222 FOREACH_FINALIZE_VOID;
225 static void library_foreach_shaderfxForeachIDLink(void *user_data,
226 Object *UNUSED(object),
230 LibraryForeachIDData *data = (LibraryForeachIDData *)user_data;
231 FOREACH_CALLBACK_INVOKE_ID_PP(data, id_pointer, cb_flag);
233 FOREACH_FINALIZE_VOID;
236 static void library_foreach_constraintObjectLooper(bConstraint *UNUSED(con),
241 LibraryForeachIDData *data = (LibraryForeachIDData *)user_data;
242 const int cb_flag = is_reference ? IDWALK_CB_USER : IDWALK_CB_NOP;
243 FOREACH_CALLBACK_INVOKE_ID_PP(data, id_pointer, cb_flag);
245 FOREACH_FINALIZE_VOID;
248 static void library_foreach_particlesystemsObjectLooper(ParticleSystem *UNUSED(psys),
253 LibraryForeachIDData *data = (LibraryForeachIDData *)user_data;
254 FOREACH_CALLBACK_INVOKE_ID_PP(data, id_pointer, cb_flag);
256 FOREACH_FINALIZE_VOID;
259 static void library_foreach_nla_strip(LibraryForeachIDData *data, NlaStrip *strip)
263 FOREACH_CALLBACK_INVOKE(data, strip->act, IDWALK_CB_USER);
265 for (substrip = strip->strips.first; substrip; substrip = substrip->next) {
266 library_foreach_nla_strip(data, substrip);
269 FOREACH_FINALIZE_VOID;
272 static void library_foreach_animationData(LibraryForeachIDData *data, AnimData *adt)
278 for (fcu = adt->drivers.first; fcu; fcu = fcu->next) {
279 ChannelDriver *driver = fcu->driver;
282 for (dvar = driver->variables.first; dvar; dvar = dvar->next) {
283 /* only used targets */
284 DRIVER_TARGETS_USED_LOOPER_BEGIN (dvar) {
285 FOREACH_CALLBACK_INVOKE_ID(data, dtar->id, IDWALK_CB_NOP);
287 DRIVER_TARGETS_LOOPER_END;
291 FOREACH_CALLBACK_INVOKE(data, adt->action, IDWALK_CB_USER);
292 FOREACH_CALLBACK_INVOKE(data, adt->tmpact, IDWALK_CB_USER);
294 for (nla_track = adt->nla_tracks.first; nla_track; nla_track = nla_track->next) {
295 for (nla_strip = nla_track->strips.first; nla_strip; nla_strip = nla_strip->next) {
296 library_foreach_nla_strip(data, nla_strip);
300 FOREACH_FINALIZE_VOID;
303 static void library_foreach_mtex(LibraryForeachIDData *data, MTex *mtex)
305 FOREACH_CALLBACK_INVOKE(data, mtex->object, IDWALK_CB_NOP);
306 FOREACH_CALLBACK_INVOKE(data, mtex->tex, IDWALK_CB_USER);
308 FOREACH_FINALIZE_VOID;
311 static void library_foreach_paint(LibraryForeachIDData *data, Paint *paint)
313 FOREACH_CALLBACK_INVOKE(data, paint->brush, IDWALK_CB_USER);
314 for (int i = 0; i < paint->tool_slots_len; i++) {
315 FOREACH_CALLBACK_INVOKE(data, paint->tool_slots[i].brush, IDWALK_CB_USER);
317 FOREACH_CALLBACK_INVOKE(data, paint->palette, IDWALK_CB_USER);
319 FOREACH_FINALIZE_VOID;
322 static void library_foreach_bone(LibraryForeachIDData *data, Bone *bone)
324 library_foreach_idproperty_ID_link(data, bone->prop, IDWALK_CB_USER);
326 for (Bone *curbone = bone->childbase.first; curbone; curbone = curbone->next) {
327 library_foreach_bone(data, curbone);
330 FOREACH_FINALIZE_VOID;
333 static void library_foreach_layer_collection(LibraryForeachIDData *data, ListBase *lb)
335 for (LayerCollection *lc = lb->first; lc; lc = lc->next) {
336 FOREACH_CALLBACK_INVOKE(data, lc->collection, IDWALK_CB_NOP);
337 library_foreach_layer_collection(data, &lc->layer_collections);
340 FOREACH_FINALIZE_VOID;
343 /* Used by both real Collection data-blocks, and the fake horror of master collection from Scene.
345 static void library_foreach_collection(LibraryForeachIDData *data, Collection *collection)
347 for (CollectionObject *cob = collection->gobject.first; cob; cob = cob->next) {
348 FOREACH_CALLBACK_INVOKE(data, cob->ob, IDWALK_CB_USER);
350 for (CollectionChild *child = collection->children.first; child; child = child->next) {
351 FOREACH_CALLBACK_INVOKE(data, child->collection, IDWALK_CB_NEVER_SELF | IDWALK_CB_USER);
353 for (CollectionParent *parent = collection->parents.first; parent; parent = parent->next) {
354 FOREACH_CALLBACK_INVOKE(data, parent->collection, IDWALK_CB_NEVER_SELF | IDWALK_CB_LOOPBACK);
357 FOREACH_FINALIZE_VOID;
360 static void library_foreach_ID_as_subdata_link(ID **id_pp,
361 LibraryIDLinkCallback callback,
364 LibraryForeachIDData *data)
366 /* Needed e.g. for callbacks handling relationships... This call shall be absolutely readonly. */
368 FOREACH_CALLBACK_INVOKE_ID_PP(data, id_pp, IDWALK_CB_PRIVATE);
369 BLI_assert(id == *id_pp);
371 if (flag & IDWALK_RECURSE) {
372 /* Defer handling into main loop, recursively calling BKE_library_foreach_ID_link in
373 * IDWALK_RECURSE case is troublesome, see T49553. */
374 if (BLI_gset_add(data->ids_handled, id)) {
375 BLI_LINKSTACK_PUSH(data->ids_todo, id);
379 library_foreach_ID_link(NULL, id, callback, user_data, flag, data);
382 FOREACH_FINALIZE_VOID;
385 static void library_foreach_ID_link(Main *bmain,
387 LibraryIDLinkCallback callback,
390 LibraryForeachIDData *inherit_data)
392 LibraryForeachIDData data;
395 if (flag & IDWALK_RECURSE) {
396 /* For now, recursion implies read-only. */
397 flag |= IDWALK_READONLY;
399 data.ids_handled = BLI_gset_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, __func__);
400 BLI_LINKSTACK_INIT(data.ids_todo);
402 BLI_gset_add(data.ids_handled, id);
405 data.ids_handled = NULL;
409 data.callback = callback;
410 data.user_data = user_data;
412 #define CALLBACK_INVOKE_ID(check_id, cb_flag) FOREACH_CALLBACK_INVOKE_ID(&data, check_id, cb_flag)
414 #define CALLBACK_INVOKE(check_id_super, cb_flag) \
415 FOREACH_CALLBACK_INVOKE(&data, check_id_super, cb_flag)
417 for (; id != NULL; id = (flag & IDWALK_RECURSE) ? BLI_LINKSTACK_POP(data.ids_todo) : NULL) {
420 /* inherit_data is non-NULL when this function is called for some sub-data ID
421 * (like root nodetree of a material).
422 * In that case, we do not want to generate those 'generic flags' from our current sub-data ID
423 * (the node tree), but re-use those generated for the 'owner' ID (the material). */
424 if (inherit_data == NULL) {
425 data.cb_flag = ID_IS_LINKED(id) ? IDWALK_CB_INDIRECT_USAGE : 0;
426 /* When an ID is not in Main database, it should never refcount IDs it is using.
427 * Exceptions: NodeTrees (yeeahhh!) directly used by Materials. */
428 data.cb_flag_clear = (id->tag & LIB_TAG_NO_MAIN) ? IDWALK_CB_USER | IDWALK_CB_USER_ONE : 0;
431 data.cb_flag = inherit_data->cb_flag;
432 data.cb_flag_clear = inherit_data->cb_flag_clear;
435 if (bmain != NULL && bmain->relations != NULL && (flag & IDWALK_READONLY)) {
436 /* Note that this is minor optimization, even in worst cases (like id being an object with
437 * lots of drivers and constraints and modifiers, or material etc. with huge node tree),
438 * but we might as well use it (Main->relations is always assumed valid,
439 * it's responsibility of code creating it to free it,
440 * especially if/when it starts modifying Main database). */
441 MainIDRelationsEntry *entry = BLI_ghash_lookup(bmain->relations->id_user_to_used, id);
442 for (; entry != NULL; entry = entry->next) {
443 FOREACH_CALLBACK_INVOKE_ID_PP(&data, entry->id_pointer, entry->usage_flag);
448 if (id->override_library != NULL) {
449 CALLBACK_INVOKE_ID(id->override_library->reference,
450 IDWALK_CB_USER | IDWALK_CB_OVERRIDE_LIBRARY_REFERENCE);
451 CALLBACK_INVOKE_ID(id->override_library->storage,
452 IDWALK_CB_USER | IDWALK_CB_OVERRIDE_LIBRARY_REFERENCE);
455 library_foreach_idproperty_ID_link(&data, id->properties, IDWALK_CB_USER);
457 AnimData *adt = BKE_animdata_from_id(id);
459 library_foreach_animationData(&data, adt);
462 switch ((ID_Type)GS(id->name)) {
464 Library *lib = (Library *)id;
465 CALLBACK_INVOKE(lib->parent, IDWALK_CB_NEVER_SELF);
469 Scene *scene = (Scene *)id;
470 ToolSettings *toolsett = scene->toolsettings;
472 CALLBACK_INVOKE(scene->camera, IDWALK_CB_NOP);
473 CALLBACK_INVOKE(scene->world, IDWALK_CB_USER);
474 CALLBACK_INVOKE(scene->set, IDWALK_CB_NEVER_SELF);
475 CALLBACK_INVOKE(scene->clip, IDWALK_CB_USER);
476 CALLBACK_INVOKE(scene->gpd, IDWALK_CB_USER);
477 CALLBACK_INVOKE(scene->r.bake.cage_object, IDWALK_CB_NOP);
478 if (scene->nodetree) {
479 /* nodetree **are owned by IDs**, treat them as mere sub-data and not real ID! */
480 library_foreach_ID_as_subdata_link(
481 (ID **)&scene->nodetree, callback, user_data, flag, &data);
485 SEQP_BEGIN (scene->ed, seq) {
486 CALLBACK_INVOKE(seq->scene, IDWALK_CB_NEVER_SELF);
487 CALLBACK_INVOKE(seq->scene_camera, IDWALK_CB_NOP);
488 CALLBACK_INVOKE(seq->clip, IDWALK_CB_USER);
489 CALLBACK_INVOKE(seq->mask, IDWALK_CB_USER);
490 CALLBACK_INVOKE(seq->sound, IDWALK_CB_USER);
491 library_foreach_idproperty_ID_link(&data, seq->prop, IDWALK_CB_USER);
492 for (SequenceModifierData *smd = seq->modifiers.first; smd; smd = smd->next) {
493 CALLBACK_INVOKE(smd->mask_id, IDWALK_CB_USER);
496 if (seq->type == SEQ_TYPE_TEXT && seq->effectdata) {
497 TextVars *text_data = seq->effectdata;
498 CALLBACK_INVOKE(text_data->text_font, IDWALK_CB_USER);
504 library_foreach_collection(&data, scene->master_collection);
506 ViewLayer *view_layer;
507 for (view_layer = scene->view_layers.first; view_layer; view_layer = view_layer->next) {
508 CALLBACK_INVOKE(view_layer->mat_override, IDWALK_CB_USER);
510 for (Base *base = view_layer->object_bases.first; base; base = base->next) {
511 CALLBACK_INVOKE(base->object, IDWALK_CB_NOP);
514 library_foreach_layer_collection(&data, &view_layer->layer_collections);
516 for (FreestyleModuleConfig *fmc = view_layer->freestyle_config.modules.first; fmc;
519 CALLBACK_INVOKE(fmc->script, IDWALK_CB_NOP);
523 for (FreestyleLineSet *fls = view_layer->freestyle_config.linesets.first; fls;
526 CALLBACK_INVOKE(fls->group, IDWALK_CB_USER);
529 if (fls->linestyle) {
530 CALLBACK_INVOKE(fls->linestyle, IDWALK_CB_USER);
535 for (TimeMarker *marker = scene->markers.first; marker; marker = marker->next) {
536 CALLBACK_INVOKE(marker->camera, IDWALK_CB_NOP);
540 CALLBACK_INVOKE(toolsett->particle.scene, IDWALK_CB_NOP);
541 CALLBACK_INVOKE(toolsett->particle.object, IDWALK_CB_NOP);
542 CALLBACK_INVOKE(toolsett->particle.shape_object, IDWALK_CB_NOP);
544 library_foreach_paint(&data, &toolsett->imapaint.paint);
545 CALLBACK_INVOKE(toolsett->imapaint.stencil, IDWALK_CB_USER);
546 CALLBACK_INVOKE(toolsett->imapaint.clone, IDWALK_CB_USER);
547 CALLBACK_INVOKE(toolsett->imapaint.canvas, IDWALK_CB_USER);
549 if (toolsett->vpaint) {
550 library_foreach_paint(&data, &toolsett->vpaint->paint);
552 if (toolsett->wpaint) {
553 library_foreach_paint(&data, &toolsett->wpaint->paint);
555 if (toolsett->sculpt) {
556 library_foreach_paint(&data, &toolsett->sculpt->paint);
557 CALLBACK_INVOKE(toolsett->sculpt->gravity_object, IDWALK_CB_NOP);
559 if (toolsett->uvsculpt) {
560 library_foreach_paint(&data, &toolsett->uvsculpt->paint);
562 if (toolsett->gp_paint) {
563 library_foreach_paint(&data, &toolsett->gp_paint->paint);
566 CALLBACK_INVOKE(toolsett->gp_sculpt.guide.reference_object, IDWALK_CB_NOP);
569 if (scene->rigidbody_world) {
570 BKE_rigidbody_world_id_loop(
571 scene->rigidbody_world, library_foreach_rigidbodyworldSceneLooper, &data);
578 Object *object = (Object *)id;
579 ParticleSystem *psys;
581 /* Object is special, proxies make things hard... */
582 const int data_cb_flag = data.cb_flag;
583 const int proxy_cb_flag = ((data.flag & IDWALK_NO_INDIRECT_PROXY_DATA_USAGE) == 0 &&
584 (object->proxy || object->proxy_group)) ?
585 IDWALK_CB_INDIRECT_USAGE :
588 /* object data special case */
589 data.cb_flag |= proxy_cb_flag;
590 if (object->type == OB_EMPTY) {
591 /* empty can have NULL or Image */
592 CALLBACK_INVOKE_ID(object->data, IDWALK_CB_USER);
595 /* when set, this can't be NULL */
597 CALLBACK_INVOKE_ID(object->data, IDWALK_CB_USER | IDWALK_CB_NEVER_NULL);
600 data.cb_flag = data_cb_flag;
602 CALLBACK_INVOKE(object->parent, IDWALK_CB_NEVER_SELF);
603 CALLBACK_INVOKE(object->track, IDWALK_CB_NEVER_SELF);
604 /* object->proxy is refcounted, but not object->proxy_group... *sigh* */
605 CALLBACK_INVOKE(object->proxy, IDWALK_CB_USER | IDWALK_CB_NEVER_SELF);
606 CALLBACK_INVOKE(object->proxy_group, IDWALK_CB_NOP);
609 * Since this field is set/owned by 'user' of this ID (and not ID itself),
610 * it is only indirect usage if proxy object is linked... Twisted. */
611 if (object->proxy_from) {
612 data.cb_flag = ID_IS_LINKED(object->proxy_from) ? IDWALK_CB_INDIRECT_USAGE : 0;
614 CALLBACK_INVOKE(object->proxy_from, IDWALK_CB_LOOPBACK | IDWALK_CB_NEVER_SELF);
615 data.cb_flag = data_cb_flag;
617 CALLBACK_INVOKE(object->poselib, IDWALK_CB_USER);
619 data.cb_flag |= proxy_cb_flag;
620 for (i = 0; i < object->totcol; i++) {
621 CALLBACK_INVOKE(object->mat[i], IDWALK_CB_USER);
623 data.cb_flag = data_cb_flag;
625 /* Note that ob->gpd is deprecated, so no need to handle it here. */
626 CALLBACK_INVOKE(object->instance_collection, IDWALK_CB_USER);
629 CALLBACK_INVOKE(object->pd->tex, IDWALK_CB_USER);
630 CALLBACK_INVOKE(object->pd->f_source, IDWALK_CB_NOP);
632 /* Note that ob->effect is deprecated, so no need to handle it here. */
637 data.cb_flag |= proxy_cb_flag;
638 for (pchan = object->pose->chanbase.first; pchan; pchan = pchan->next) {
639 library_foreach_idproperty_ID_link(&data, pchan->prop, IDWALK_CB_USER);
640 CALLBACK_INVOKE(pchan->custom, IDWALK_CB_USER);
641 BKE_constraints_id_loop(
642 &pchan->constraints, library_foreach_constraintObjectLooper, &data);
644 data.cb_flag = data_cb_flag;
647 if (object->rigidbody_constraint) {
648 CALLBACK_INVOKE(object->rigidbody_constraint->ob1, IDWALK_CB_NEVER_SELF);
649 CALLBACK_INVOKE(object->rigidbody_constraint->ob2, IDWALK_CB_NEVER_SELF);
652 if (object->lodlevels.first) {
654 for (level = object->lodlevels.first; level; level = level->next) {
655 CALLBACK_INVOKE(level->source, IDWALK_CB_NEVER_SELF);
659 modifiers_foreachIDLink(object, library_foreach_modifiersForeachIDLink, &data);
660 BKE_gpencil_modifiers_foreachIDLink(
661 object, library_foreach_gpencil_modifiersForeachIDLink, &data);
662 BKE_constraints_id_loop(
663 &object->constraints, library_foreach_constraintObjectLooper, &data);
664 BKE_shaderfx_foreachIDLink(object, library_foreach_shaderfxForeachIDLink, &data);
666 for (psys = object->particlesystem.first; psys; psys = psys->next) {
667 BKE_particlesystem_id_loop(psys, library_foreach_particlesystemsObjectLooper, &data);
671 CALLBACK_INVOKE(object->soft->collision_group, IDWALK_CB_NOP);
673 if (object->soft->effector_weights) {
674 CALLBACK_INVOKE(object->soft->effector_weights->group, IDWALK_CB_NOP);
681 bArmature *arm = (bArmature *)id;
683 for (Bone *bone = arm->bonebase.first; bone; bone = bone->next) {
684 library_foreach_bone(&data, bone);
690 Mesh *mesh = (Mesh *)id;
691 CALLBACK_INVOKE(mesh->texcomesh, IDWALK_CB_NEVER_SELF);
692 CALLBACK_INVOKE(mesh->key, IDWALK_CB_USER);
693 for (i = 0; i < mesh->totcol; i++) {
694 CALLBACK_INVOKE(mesh->mat[i], IDWALK_CB_USER);
700 Curve *curve = (Curve *)id;
701 CALLBACK_INVOKE(curve->bevobj, IDWALK_CB_NOP);
702 CALLBACK_INVOKE(curve->taperobj, IDWALK_CB_NOP);
703 CALLBACK_INVOKE(curve->textoncurve, IDWALK_CB_NOP);
704 CALLBACK_INVOKE(curve->key, IDWALK_CB_USER);
705 for (i = 0; i < curve->totcol; i++) {
706 CALLBACK_INVOKE(curve->mat[i], IDWALK_CB_USER);
708 CALLBACK_INVOKE(curve->vfont, IDWALK_CB_USER);
709 CALLBACK_INVOKE(curve->vfontb, IDWALK_CB_USER);
710 CALLBACK_INVOKE(curve->vfonti, IDWALK_CB_USER);
711 CALLBACK_INVOKE(curve->vfontbi, IDWALK_CB_USER);
716 MetaBall *metaball = (MetaBall *)id;
717 for (i = 0; i < metaball->totcol; i++) {
718 CALLBACK_INVOKE(metaball->mat[i], IDWALK_CB_USER);
724 Material *material = (Material *)id;
725 if (material->nodetree) {
726 /* nodetree **are owned by IDs**, treat them as mere sub-data and not real ID! */
727 library_foreach_ID_as_subdata_link(
728 (ID **)&material->nodetree, callback, user_data, flag, &data);
730 if (material->texpaintslot != NULL) {
731 CALLBACK_INVOKE(material->texpaintslot->ima, IDWALK_CB_NOP);
733 if (material->gp_style != NULL) {
734 CALLBACK_INVOKE(material->gp_style->sima, IDWALK_CB_USER);
735 CALLBACK_INVOKE(material->gp_style->ima, IDWALK_CB_USER);
741 Tex *texture = (Tex *)id;
742 if (texture->nodetree) {
743 /* nodetree **are owned by IDs**, treat them as mere sub-data and not real ID! */
744 library_foreach_ID_as_subdata_link(
745 (ID **)&texture->nodetree, callback, user_data, flag, &data);
747 CALLBACK_INVOKE(texture->ima, IDWALK_CB_USER);
752 Lattice *lattice = (Lattice *)id;
753 CALLBACK_INVOKE(lattice->key, IDWALK_CB_USER);
758 Light *lamp = (Light *)id;
759 if (lamp->nodetree) {
760 /* nodetree **are owned by IDs**, treat them as mere sub-data and not real ID! */
761 library_foreach_ID_as_subdata_link(
762 (ID **)&lamp->nodetree, callback, user_data, flag, &data);
768 Camera *camera = (Camera *)id;
769 CALLBACK_INVOKE(camera->dof.focus_object, IDWALK_CB_NOP);
770 for (CameraBGImage *bgpic = camera->bg_images.first; bgpic; bgpic = bgpic->next) {
771 if (bgpic->source == CAM_BGIMG_SOURCE_IMAGE) {
772 CALLBACK_INVOKE(bgpic->ima, IDWALK_CB_USER);
774 else if (bgpic->source == CAM_BGIMG_SOURCE_MOVIE) {
775 CALLBACK_INVOKE(bgpic->clip, IDWALK_CB_USER);
783 Key *key = (Key *)id;
784 CALLBACK_INVOKE_ID(key->from, IDWALK_CB_LOOPBACK);
789 World *world = (World *)id;
790 if (world->nodetree) {
791 /* nodetree **are owned by IDs**, treat them as mere sub-data and not real ID! */
792 library_foreach_ID_as_subdata_link(
793 (ID **)&world->nodetree, callback, user_data, flag, &data);
799 Speaker *speaker = (Speaker *)id;
800 CALLBACK_INVOKE(speaker->sound, IDWALK_CB_USER);
805 LightProbe *probe = (LightProbe *)id;
806 CALLBACK_INVOKE(probe->image, IDWALK_CB_USER);
807 CALLBACK_INVOKE(probe->visibility_grp, IDWALK_CB_NOP);
812 Collection *collection = (Collection *)id;
813 library_foreach_collection(&data, collection);
818 bNodeTree *ntree = (bNodeTree *)id;
822 CALLBACK_INVOKE(ntree->gpd, IDWALK_CB_USER);
824 for (node = ntree->nodes.first; node; node = node->next) {
825 CALLBACK_INVOKE_ID(node->id, IDWALK_CB_USER);
827 library_foreach_idproperty_ID_link(&data, node->prop, IDWALK_CB_USER);
828 for (sock = node->inputs.first; sock; sock = sock->next) {
829 library_foreach_idproperty_ID_link(&data, sock->prop, IDWALK_CB_USER);
831 for (sock = node->outputs.first; sock; sock = sock->next) {
832 library_foreach_idproperty_ID_link(&data, sock->prop, IDWALK_CB_USER);
836 for (sock = ntree->inputs.first; sock; sock = sock->next) {
837 library_foreach_idproperty_ID_link(&data, sock->prop, IDWALK_CB_USER);
839 for (sock = ntree->outputs.first; sock; sock = sock->next) {
840 library_foreach_idproperty_ID_link(&data, sock->prop, IDWALK_CB_USER);
846 Brush *brush = (Brush *)id;
847 CALLBACK_INVOKE(brush->toggle_brush, IDWALK_CB_NOP);
848 CALLBACK_INVOKE(brush->clone.image, IDWALK_CB_NOP);
849 CALLBACK_INVOKE(brush->paint_curve, IDWALK_CB_USER);
850 if (brush->gpencil_settings) {
851 CALLBACK_INVOKE(brush->gpencil_settings->material, IDWALK_CB_USER);
853 library_foreach_mtex(&data, &brush->mtex);
854 library_foreach_mtex(&data, &brush->mask_mtex);
859 ParticleSettings *psett = (ParticleSettings *)id;
860 CALLBACK_INVOKE(psett->instance_collection, IDWALK_CB_USER);
861 CALLBACK_INVOKE(psett->instance_object, IDWALK_CB_NOP);
862 CALLBACK_INVOKE(psett->bb_ob, IDWALK_CB_NOP);
863 CALLBACK_INVOKE(psett->collision_group, IDWALK_CB_NOP);
865 for (i = 0; i < MAX_MTEX; i++) {
866 if (psett->mtex[i]) {
867 library_foreach_mtex(&data, psett->mtex[i]);
871 if (psett->effector_weights) {
872 CALLBACK_INVOKE(psett->effector_weights->group, IDWALK_CB_NOP);
876 CALLBACK_INVOKE(psett->pd->tex, IDWALK_CB_USER);
877 CALLBACK_INVOKE(psett->pd->f_source, IDWALK_CB_NOP);
880 CALLBACK_INVOKE(psett->pd2->tex, IDWALK_CB_USER);
881 CALLBACK_INVOKE(psett->pd2->f_source, IDWALK_CB_NOP);
888 for (state = psett->boids->states.first; state; state = state->next) {
889 for (rule = state->rules.first; rule; rule = rule->next) {
890 if (rule->type == eBoidRuleType_Avoid) {
891 BoidRuleGoalAvoid *gabr = (BoidRuleGoalAvoid *)rule;
892 CALLBACK_INVOKE(gabr->ob, IDWALK_CB_NOP);
894 else if (rule->type == eBoidRuleType_FollowLeader) {
895 BoidRuleFollowLeader *flbr = (BoidRuleFollowLeader *)rule;
896 CALLBACK_INVOKE(flbr->ob, IDWALK_CB_NOP);
902 for (ParticleDupliWeight *dw = psett->instance_weights.first; dw; dw = dw->next) {
903 CALLBACK_INVOKE(dw->ob, IDWALK_CB_NOP);
909 MovieClip *clip = (MovieClip *)id;
910 MovieTracking *tracking = &clip->tracking;
911 MovieTrackingObject *object;
912 MovieTrackingTrack *track;
913 MovieTrackingPlaneTrack *plane_track;
915 CALLBACK_INVOKE(clip->gpd, IDWALK_CB_USER);
917 for (track = tracking->tracks.first; track; track = track->next) {
918 CALLBACK_INVOKE(track->gpd, IDWALK_CB_USER);
920 for (object = tracking->objects.first; object; object = object->next) {
921 for (track = object->tracks.first; track; track = track->next) {
922 CALLBACK_INVOKE(track->gpd, IDWALK_CB_USER);
926 for (plane_track = tracking->plane_tracks.first; plane_track;
927 plane_track = plane_track->next) {
928 CALLBACK_INVOKE(plane_track->image, IDWALK_CB_USER);
934 Mask *mask = (Mask *)id;
935 MaskLayer *mask_layer;
936 for (mask_layer = mask->masklayers.first; mask_layer; mask_layer = mask_layer->next) {
937 MaskSpline *mask_spline;
939 for (mask_spline = mask_layer->splines.first; mask_spline;
940 mask_spline = mask_spline->next) {
941 for (i = 0; i < mask_spline->tot_point; i++) {
942 MaskSplinePoint *point = &mask_spline->points[i];
943 CALLBACK_INVOKE_ID(point->parent.id, IDWALK_CB_USER);
951 FreestyleLineStyle *linestyle = (FreestyleLineStyle *)id;
952 LineStyleModifier *lsm;
953 for (i = 0; i < MAX_MTEX; i++) {
954 if (linestyle->mtex[i]) {
955 library_foreach_mtex(&data, linestyle->mtex[i]);
958 if (linestyle->nodetree) {
959 /* nodetree **are owned by IDs**, treat them as mere sub-data and not real ID! */
960 library_foreach_ID_as_subdata_link(
961 (ID **)&linestyle->nodetree, callback, user_data, flag, &data);
964 for (lsm = linestyle->color_modifiers.first; lsm; lsm = lsm->next) {
965 if (lsm->type == LS_MODIFIER_DISTANCE_FROM_OBJECT) {
966 LineStyleColorModifier_DistanceFromObject *p =
967 (LineStyleColorModifier_DistanceFromObject *)lsm;
969 CALLBACK_INVOKE(p->target, IDWALK_CB_NOP);
973 for (lsm = linestyle->alpha_modifiers.first; lsm; lsm = lsm->next) {
974 if (lsm->type == LS_MODIFIER_DISTANCE_FROM_OBJECT) {
975 LineStyleAlphaModifier_DistanceFromObject *p =
976 (LineStyleAlphaModifier_DistanceFromObject *)lsm;
978 CALLBACK_INVOKE(p->target, IDWALK_CB_NOP);
982 for (lsm = linestyle->thickness_modifiers.first; lsm; lsm = lsm->next) {
983 if (lsm->type == LS_MODIFIER_DISTANCE_FROM_OBJECT) {
984 LineStyleThicknessModifier_DistanceFromObject *p =
985 (LineStyleThicknessModifier_DistanceFromObject *)lsm;
987 CALLBACK_INVOKE(p->target, IDWALK_CB_NOP);
994 bAction *act = (bAction *)id;
996 for (TimeMarker *marker = act->markers.first; marker; marker = marker->next) {
997 CALLBACK_INVOKE(marker->camera, IDWALK_CB_NOP);
1003 wmWindowManager *wm = (wmWindowManager *)id;
1005 for (wmWindow *win = wm->windows.first; win; win = win->next) {
1006 ID *workspace = (ID *)BKE_workspace_active_get(win->workspace_hook);
1008 CALLBACK_INVOKE(win->scene, IDWALK_CB_USER_ONE);
1010 CALLBACK_INVOKE_ID(workspace, IDWALK_CB_NOP);
1011 /* allow callback to set a different workspace */
1012 BKE_workspace_active_set(win->workspace_hook, (WorkSpace *)workspace);
1018 WorkSpace *workspace = (WorkSpace *)id;
1019 ListBase *layouts = BKE_workspace_layouts_get(workspace);
1021 for (WorkSpaceLayout *layout = layouts->first; layout; layout = layout->next) {
1022 bScreen *screen = BKE_workspace_layout_screen_get(layout);
1024 /* CALLBACK_INVOKE expects an actual pointer, not a variable holding the pointer.
1025 * However we can't access layout->screen here
1026 * since we are outside the workspace project. */
1027 CALLBACK_INVOKE(screen, IDWALK_CB_USER);
1028 /* allow callback to set a different screen */
1029 BKE_workspace_layout_screen_set(layout, screen);
1034 bGPdata *gpencil = (bGPdata *)id;
1036 for (i = 0; i < gpencil->totcol; i++) {
1037 CALLBACK_INVOKE(gpencil->mat[i], IDWALK_CB_USER);
1040 for (bGPDlayer *gplayer = gpencil->layers.first; gplayer != NULL;
1041 gplayer = gplayer->next) {
1042 CALLBACK_INVOKE(gplayer->parent, IDWALK_CB_NOP);
1048 /* Nothing needed for those... */
1066 if (data.ids_handled) {
1067 BLI_gset_free(data.ids_handled, NULL);
1068 BLI_LINKSTACK_FREE(data.ids_todo);
1071 #undef CALLBACK_INVOKE_ID
1072 #undef CALLBACK_INVOKE
1075 #undef FOREACH_CALLBACK_INVOKE_ID
1076 #undef FOREACH_CALLBACK_INVOKE
1079 * Loop over all of the ID's this data-block links to.
1081 void BKE_library_foreach_ID_link(
1082 Main *bmain, ID *id, LibraryIDLinkCallback callback, void *user_data, int flag)
1084 library_foreach_ID_link(bmain, id, callback, user_data, flag, NULL);
1088 * re-usable function, use when replacing ID's
1090 void BKE_library_update_ID_link_user(ID *id_dst, ID *id_src, const int cb_flag)
1092 if (cb_flag & IDWALK_CB_USER) {
1096 else if (cb_flag & IDWALK_CB_USER_ONE) {
1097 id_us_ensure_real(id_dst);
1102 * Say whether given \a id_type_owner can use (in any way) a data-block of \a id_type_used.
1104 * This is a 'simplified' abstract version of #BKE_library_foreach_ID_link() above,
1105 * quite useful to reduce* useless iterations in some cases.
1107 /* XXX This has to be fully rethink, basing check on ID type is not really working anymore
1108 * (and even worth once IDProps will support ID pointers),
1109 * we'll have to do some quick checks on IDs themselves... */
1110 bool BKE_library_id_can_use_idtype(ID *id_owner, const short id_type_used)
1112 /* any type of ID can be used in custom props. */
1113 if (id_owner->properties) {
1117 const short id_type_owner = GS(id_owner->name);
1119 /* IDProps of armature bones and nodes, and bNode->id can use virtually any type of ID. */
1120 if (ELEM(id_type_owner, ID_NT, ID_AR)) {
1124 if (ntreeFromID(id_owner)) {
1128 if (BKE_animdata_from_id(id_owner)) {
1129 /* AnimationData can use virtually any kind of data-blocks, through drivers especially. */
1133 switch ((ID_Type)id_type_owner) {
1135 return ELEM(id_type_used, ID_LI);
1137 return (ELEM(id_type_used,
1154 /* Could be more specific, but simpler to just always say 'yes' here. */
1157 return ELEM(id_type_used, ID_ME, ID_KE, ID_MA, ID_IM);
1159 return ELEM(id_type_used, ID_OB, ID_KE, ID_MA, ID_VF);
1161 return ELEM(id_type_used, ID_MA);
1163 return (ELEM(id_type_used, ID_TE, ID_GR));
1165 return (ELEM(id_type_used, ID_IM, ID_OB));
1167 return ELEM(id_type_used, ID_KE);
1169 return (ELEM(id_type_used, ID_TE));
1171 return ELEM(id_type_used, ID_OB);
1173 /* Warning! key->from, could be more types in future? */
1174 return ELEM(id_type_used, ID_ME, ID_CU, ID_LT);
1176 return ELEM(id_type_used, ID_SCE);
1178 return (ELEM(id_type_used, ID_TE));
1180 return ELEM(id_type_used, ID_SO);
1182 return ELEM(id_type_used, ID_OB, ID_GR);
1184 /* Could be more specific, but node.id has no type restriction... */
1187 return ELEM(id_type_used, ID_BR, ID_IM, ID_PC, ID_TE, ID_MA);
1189 return ELEM(id_type_used, ID_OB, ID_GR, ID_TE);
1191 return ELEM(id_type_used, ID_GD, ID_IM);
1193 /* WARNING! mask->parent.id, not typed. */
1194 return ELEM(id_type_used, ID_MC);
1196 return (ELEM(id_type_used, ID_TE, ID_OB));
1198 return ELEM(id_type_used, ID_IM);
1200 return ELEM(id_type_used, ID_MA);
1202 return ELEM(id_type_used, ID_SCR, ID_SCE);
1213 /* Those types never use/reference other IDs... */
1222 /* ***** ID users iterator. ***** */
1223 typedef struct IDUsersIter {
1226 ListBase *lb_array[MAX_LIBARRAY];
1230 int count_direct, count_indirect; /* Set by callback. */
1233 static int foreach_libblock_id_users_callback(void *user_data,
1234 ID *UNUSED(self_id),
1238 IDUsersIter *iter = user_data;
1241 /* 'Loopback' ID pointers (the ugly 'from' ones, Object->proxy_from and Key->from).
1242 * Those are not actually ID usage, we can ignore them here.
1244 if (cb_flag & IDWALK_CB_LOOPBACK) {
1245 return IDWALK_RET_NOP;
1248 if (*id_p == iter->id) {
1251 "%s uses %s (refcounted: %d, userone: %d, used_one: %d, used_one_active: %d, "
1252 "indirect_usage: %d)\n",
1253 iter->curr_id->name,
1255 (cb_flag & IDWALK_USER) ? 1 : 0,
1256 (cb_flag & IDWALK_USER_ONE) ? 1 : 0,
1257 (iter->id->tag & LIB_TAG_EXTRAUSER) ? 1 : 0,
1258 (iter->id->tag & LIB_TAG_EXTRAUSER_SET) ? 1 : 0,
1259 (cb_flag & IDWALK_INDIRECT_USAGE) ? 1 : 0);
1261 if (cb_flag & IDWALK_CB_INDIRECT_USAGE) {
1262 iter->count_indirect++;
1265 iter->count_direct++;
1270 return IDWALK_RET_NOP;
1274 * Return the number of times given \a id_user uses/references \a id_used.
1276 * \note This only checks for pointer references of an ID, shallow usages
1277 * (like e.g. by RNA paths, as done for FCurves) are not detected at all.
1279 * \param id_user: the ID which is supposed to use (reference) \a id_used.
1280 * \param id_used: the ID which is supposed to be used (referenced) by \a id_user.
1281 * \return the number of direct usages/references of \a id_used by \a id_user.
1283 int BKE_library_ID_use_ID(ID *id_user, ID *id_used)
1287 /* We do not care about iter.lb_array/lb_idx here... */
1289 iter.curr_id = id_user;
1290 iter.count_direct = iter.count_indirect = 0;
1292 BKE_library_foreach_ID_link(
1293 NULL, iter.curr_id, foreach_libblock_id_users_callback, (void *)&iter, IDWALK_READONLY);
1295 return iter.count_direct + iter.count_indirect;
1298 static bool library_ID_is_used(Main *bmain, void *idv, const bool check_linked)
1301 ListBase *lb_array[MAX_LIBARRAY];
1303 int i = set_listbasepointers(bmain, lb_array);
1304 bool is_defined = false;
1307 iter.count_direct = iter.count_indirect = 0;
1308 while (i-- && !is_defined) {
1309 ID *id_curr = lb_array[i]->first;
1311 if (!id_curr || !BKE_library_id_can_use_idtype(id_curr, GS(id->name))) {
1315 for (; id_curr && !is_defined; id_curr = id_curr->next) {
1316 if (id_curr == id) {
1317 /* We are not interested in self-usages (mostly from drivers or bone constraints...). */
1320 iter.curr_id = id_curr;
1321 BKE_library_foreach_ID_link(
1322 bmain, id_curr, foreach_libblock_id_users_callback, &iter, IDWALK_READONLY);
1324 is_defined = ((check_linked ? iter.count_indirect : iter.count_direct) != 0);
1332 * Check whether given ID is used locally (i.e. by another non-linked ID).
1334 bool BKE_library_ID_is_locally_used(Main *bmain, void *idv)
1336 return library_ID_is_used(bmain, idv, false);
1340 * Check whether given ID is used indirectly (i.e. by another linked ID).
1342 bool BKE_library_ID_is_indirectly_used(Main *bmain, void *idv)
1344 return library_ID_is_used(bmain, idv, true);
1348 * Combine #BKE_library_ID_is_locally_used() and #BKE_library_ID_is_indirectly_used()
1351 void BKE_library_ID_test_usages(Main *bmain, void *idv, bool *is_used_local, bool *is_used_linked)
1354 ListBase *lb_array[MAX_LIBARRAY];
1356 int i = set_listbasepointers(bmain, lb_array);
1357 bool is_defined = false;
1360 iter.count_direct = iter.count_indirect = 0;
1361 while (i-- && !is_defined) {
1362 ID *id_curr = lb_array[i]->first;
1364 if (!id_curr || !BKE_library_id_can_use_idtype(id_curr, GS(id->name))) {
1368 for (; id_curr && !is_defined; id_curr = id_curr->next) {
1369 if (id_curr == id) {
1370 /* We are not interested in self-usages (mostly from drivers or bone constraints...). */
1373 iter.curr_id = id_curr;
1374 BKE_library_foreach_ID_link(
1375 bmain, id_curr, foreach_libblock_id_users_callback, &iter, IDWALK_READONLY);
1377 is_defined = (iter.count_direct != 0 && iter.count_indirect != 0);
1381 *is_used_local = (iter.count_direct != 0);
1382 *is_used_linked = (iter.count_indirect != 0);
1385 /* ***** IDs usages.checking/tagging. ***** */
1386 static int foreach_libblock_used_linked_data_tag_clear_cb(void *user_data,
1391 bool *is_changed = user_data;
1394 /* The infamous 'from' pointers (Key.from, Object.proxy_from, ...).
1395 * those are not actually ID usage, so we ignore them here. */
1396 if (cb_flag & IDWALK_CB_LOOPBACK) {
1397 return IDWALK_RET_NOP;
1400 /* If checked id is used by an assumed used ID,
1401 * then it is also used and not part of any linked archipelago. */
1402 if (!(self_id->tag & LIB_TAG_DOIT) && ((*id_p)->tag & LIB_TAG_DOIT)) {
1403 (*id_p)->tag &= ~LIB_TAG_DOIT;
1408 return IDWALK_RET_NOP;
1412 * Detect orphaned linked data blocks (i.e. linked data not used (directly or indirectly)
1413 * in any way by any local data), including complex cases like 'linked archipelagoes', i.e.
1414 * linked data-blocks that use each other in loops,
1415 * which prevents their deletion by 'basic' usage checks.
1417 * \param do_init_tag: if \a true, all linked data are checked, if \a false,
1418 * only linked data-blocks already tagged with #LIB_TAG_DOIT are checked.
1420 void BKE_library_unused_linked_data_set_tag(Main *bmain, const bool do_init_tag)
1425 FOREACH_MAIN_ID_BEGIN (bmain, id) {
1426 if (id->lib && (id->tag & LIB_TAG_INDIRECT) != 0) {
1427 id->tag |= LIB_TAG_DOIT;
1430 id->tag &= ~LIB_TAG_DOIT;
1433 FOREACH_MAIN_ID_END;
1436 for (bool do_loop = true; do_loop;) {
1438 FOREACH_MAIN_ID_BEGIN (bmain, id) {
1439 /* We only want to check that ID if it is currently known as used... */
1440 if ((id->tag & LIB_TAG_DOIT) == 0) {
1441 BKE_library_foreach_ID_link(
1442 bmain, id, foreach_libblock_used_linked_data_tag_clear_cb, &do_loop, IDWALK_READONLY);
1445 FOREACH_MAIN_ID_END;
1450 * Untag linked data blocks used by other untagged linked data-blocks.
1451 * Used to detect data-blocks that we can forcefully make local
1452 * (instead of copying them to later get rid of original):
1453 * All data-blocks we want to make local are tagged by caller,
1454 * after this function has ran caller knows data-blocks still tagged can directly be made local,
1455 * since they are only used by other data-blocks that will also be made fully local.
1457 void BKE_library_indirectly_used_data_tag_clear(Main *bmain)
1459 ListBase *lb_array[MAX_LIBARRAY];
1461 bool do_loop = true;
1463 int i = set_listbasepointers(bmain, lb_array);
1467 for (ID *id = lb_array[i]->first; id; id = id->next) {
1468 if (id->lib == NULL || id->tag & LIB_TAG_DOIT) {
1469 /* Local or non-indirectly-used ID (so far), no need to check it further. */
1472 BKE_library_foreach_ID_link(
1473 bmain, id, foreach_libblock_used_linked_data_tag_clear_cb, &do_loop, IDWALK_READONLY);