Blender V5.3
lib_query.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2014 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cstdlib>
10
11#include "CLG_log.h"
12
13#include "DNA_anim_types.h"
14
15#include "BLI_function_ref.hh"
16#include "BLI_linklist_stack.hh"
17#include "BLI_listbase.hh"
18#include "BLI_map.hh"
19#include "BLI_set.hh"
20#include "BLI_stack.hh"
21
22#include "BKE_anim_data.hh"
23#include "BKE_idprop.hh"
24#include "BKE_idtype.hh"
25#include "BKE_lib_id.hh"
26#include "BKE_lib_query.hh"
27#include "BKE_main.hh"
28#include "BKE_node.hh"
29
30namespace blender {
31
32static CLG_LogRef LOG = {"lib.query"};
33
34/* status */
35enum {
36 IDWALK_STOP = 1 << 0,
37};
38
76
78{
79 return (data->status & IDWALK_STOP) != 0;
80}
81
83 ID **id_pp,
85{
87 return;
88 }
89
90 const LibraryForeachIDFlag flag = data->flag;
91 ID *old_id = *id_pp;
92
93 /* Update the callback flags with the ones defined (or forbidden) in `data` by the generic
94 * caller code. */
95 cb_flag = LibraryForeachIDCallbackFlag((cb_flag | data->cb_flag) & ~data->cb_flag_clear);
96
97 /* Update the callback flags with some extra information regarding overrides: all "loop-back",
98 * "internal", "embedded" etc. ID pointers are never overridable. */
101 }
102
103 LibraryIDLinkCallbackData callback_data{};
104 callback_data.user_data = data->user_data;
105 callback_data.bmain = data->bmain;
106 callback_data.owner_id = data->owner_id;
107 callback_data.self_id = data->self_id;
108 callback_data.id_pointer = id_pp;
109 callback_data.cb_flag = cb_flag;
110 const int callback_return = data->callback(&callback_data);
111
112 if (flag & IDWALK_READONLY) {
113 BLI_assert(*(id_pp) == old_id);
114 }
115 else {
117 "Iteration over ID usages should not be interrupted by the callback in "
118 "non-readonly cases");
119 }
120
121 if (old_id && (flag & IDWALK_RECURSE)) {
122 if (data->ids_handled->add(old_id)) {
123 if (!(callback_return & IDWALK_RET_STOP_RECURSION)) {
124 BLI_LINKSTACK_PUSH(data->ids_todo, old_id);
125 }
126 }
127 }
128 if (callback_return & IDWALK_RET_STOP_ITER) {
129 data->status |= IDWALK_STOP;
130 }
131}
132
137
142
144 LibraryForeachIDData *data, const LibraryForeachIDCallbackFlag cb_flag, const bool do_replace)
145{
146 const LibraryForeachIDCallbackFlag cb_flag_backup = data->cb_flag;
147 if (do_replace) {
148 data->cb_flag = cb_flag;
149 }
150 else {
151 data->cb_flag |= cb_flag;
152 }
153 return cb_flag_backup;
154}
155
156static bool library_foreach_ID_link(Main *bmain,
157 ID *owner_id,
158 ID *id,
159 FunctionRef<LibraryIDLinkCallback> callback,
160 void *user_data,
162 LibraryForeachIDData *inherit_data);
163
175
177{
178 /* Needed e.g. for callbacks handling relationships. This call should be absolutely read-only. */
179 ID *id = *id_pp;
180 const LibraryForeachIDFlag flag = data->flag;
181
184 return;
185 }
186 BLI_assert(id == *id_pp);
187
188 if (id == nullptr) {
189 return;
190 }
191
192 if (flag & IDWALK_IGNORE_EMBEDDED_ID) {
193 /* Do Nothing. */
194 }
195 else if (flag & IDWALK_RECURSE) {
196 /* Defer handling into main loop, recursively calling BKE_library_foreach_ID_link in
197 * IDWALK_RECURSE case is troublesome, see #49553. */
198 if (data->ids_handled->add(id)) {
199 BLI_LINKSTACK_PUSH(data->ids_todo, id);
200 }
201 }
202 else {
204 data->bmain, data->owner_id, id, data->callback, data->user_data, data->flag, data))
205 {
206 data->status |= IDWALK_STOP;
207 return;
208 }
209 }
210}
211
213{
214 if (data->ids_handled != nullptr) {
215 MEM_delete(data->ids_handled);
216 BLI_LINKSTACK_FREE(data->ids_todo);
217 }
218}
219
221static bool library_foreach_ID_link(Main *bmain,
222 ID *owner_id,
223 ID *id,
225 void *user_data,
227 LibraryForeachIDData *inherit_data)
228{
230 data.bmain = bmain;
231
232 BLI_assert(inherit_data == nullptr || data.bmain == inherit_data->bmain);
233 /* `IDWALK_NO_ORIG_POINTERS_ACCESS` is mutually exclusive with `IDWALK_RECURSE`. */
236
239 }
240
241 if (flag & IDWALK_RECURSE) {
242 /* For now, recursion implies read-only, and no internal pointers. */
243 flag |= IDWALK_READONLY;
245
246 /* NOTE: This function itself should never be called recursively when IDWALK_RECURSE is set,
247 * see also comments in #BKE_library_foreach_ID_embedded.
248 * This is why we can always create this data here, and do not need to try and re-use it from
249 * `inherit_data`. */
250 data.ids_handled = MEM_new<Set<ID *>>(__func__);
251 BLI_LINKSTACK_INIT(data.ids_todo);
252
253 data.ids_handled->add(id);
254 }
255 else {
256 data.ids_handled = nullptr;
257 }
258 data.flag = flag;
259 data.status = 0;
260 data.callback = callback;
261 data.user_data = user_data;
262
263#define CALLBACK_INVOKE_ID(check_id, cb_flag) \
264 { \
265 CHECK_TYPE_ANY((check_id), ID *, void *); \
266 BKE_lib_query_foreachid_process(&data, (ID **)&(check_id), (cb_flag)); \
267 if (BKE_lib_query_foreachid_iter_stop(&data)) { \
268 library_foreach_ID_data_cleanup(&data); \
269 return false; \
270 } \
271 } \
272 ((void)0)
273
274#define CALLBACK_INVOKE(check_id_super, cb_flag) \
275 { \
276 CHECK_TYPE(&((check_id_super)->id), ID *); \
277 BKE_lib_query_foreachid_process(&data, (ID **)&(check_id_super), (cb_flag)); \
278 if (BKE_lib_query_foreachid_iter_stop(&data)) { \
279 library_foreach_ID_data_cleanup(&data); \
280 return false; \
281 } \
282 } \
283 ((void)0)
284
285 for (; id != nullptr; id = (flag & IDWALK_RECURSE) ? BLI_LINKSTACK_POP(data.ids_todo) : nullptr,
286 owner_id = nullptr)
287 {
288 data.self_id = id;
289 /* owner ID is same as self ID, except for embedded ID case. */
290 if (id->flag & ID_FLAG_EMBEDDED_DATA) {
292 data.owner_id = owner_id ? owner_id : id;
293 }
294 else {
295 /* NOTE: Unfortunately it is not possible to ensure validity of the set owner_id pointer
296 * here. `foreach_id` is used a lot by code remapping pointers, and in such cases the
297 * current owner ID of the processed embedded ID is indeed invalid - and the given one is
298 * to be assumed valid for the purpose of the current process.
299 *
300 * In other words, it is the responsibility of the code calling this `foreach_id` process
301 * to ensure that the given owner ID is valid for its own purpose, or that it is not used.
302 */
303 // BLI_assert(owner_id == nullptr || BKE_id_owner_get(id) == owner_id);
304 if (!owner_id) {
305 owner_id = BKE_id_owner_get(id, false);
306 }
307 data.owner_id = owner_id;
308 }
309 }
310 else {
311 BLI_assert(ELEM(owner_id, nullptr, id));
312 data.owner_id = id;
313 }
314
315 /* inherit_data is non-nullptr when this function is called for some sub-data ID
316 * (like root node-tree of a material).
317 * In that case, we do not want to generate those 'generic flags' from our current sub-data ID
318 * (the node tree), but re-use those generated for the 'owner' ID (the material). */
319 if (inherit_data == nullptr) {
321 /* When an ID is defined as not reference-counting its ID usages, it should never do it. */
322 data.cb_flag_clear = (id->tag & ID_TAG_NO_USER_REFCOUNT) ?
325 }
326 else {
327 data.cb_flag = inherit_data->cb_flag;
328 data.cb_flag_clear = inherit_data->cb_flag_clear;
329 }
330
331 bool use_bmain_relations = bmain != nullptr && bmain->relations != nullptr &&
332 (flag & IDWALK_READONLY);
333 /* Including UI-related ID pointers should match with the relevant setting in Main relations
334 * cache. */
335 if (use_bmain_relations && (((bmain->relations->flag & MAINIDRELATIONS_INCLUDE_UI) == 0) !=
336 ((data.flag & IDWALK_INCLUDE_UI) == 0)))
337 {
338 use_bmain_relations = false;
339 }
340 /* No special 'internal' handling of ID pointers is covered by Main relations cache. */
341 if (use_bmain_relations &&
344 {
345 use_bmain_relations = false;
346 }
347 if (use_bmain_relations) {
348 /* Note that this is minor optimization, even in worst cases (like id being an object with
349 * lots of drivers and constraints and modifiers, or material etc. with huge node tree),
350 * but we might as well use it (Main->relations is always assumed valid,
351 * it's responsibility of code creating it to free it,
352 * especially if/when it starts modifying Main database). */
353 MainIDRelationsEntry *entry = bmain->relations->relations_from_pointers->lookup(id);
354 for (MainIDRelationsEntryItem *to_id_entry = entry->to_ids; to_id_entry != nullptr;
355 to_id_entry = to_id_entry->next)
356 {
357 /* NOTE: Since `use_bmain_relations` can only be true if `IDWALK_READONLY` is set, checking
358 * that the pointer is not modified is redundant (#BKE_lib_query_foreachid_process would
359 * already have asserted on it).
360 *
361 * Does not hurt to have that double-check here though, as it makes that expectation more
362 * obvious. */
363 ID *to_id_tmp = to_id_entry->id_pointer.to;
364 if (to_id_entry->usage_flag & IDWALK_CB_EMBEDDED) {
367 }
368 else {
369 BKE_lib_query_foreachid_process(&data, &to_id_tmp, to_id_entry->usage_flag);
370 }
371 BLI_assert(to_id_tmp == to_id_entry->id_pointer.to);
372
375 return false;
376 }
377 }
378 continue;
379 }
380
381 if (flag & IDWALK_DO_LIBRARY_POINTER) {
383 }
384
388 }
389
390 if (id->override_library != nullptr) {
391 CALLBACK_INVOKE_ID(id->override_library->reference,
393
394 CALLBACK_INVOKE_ID(id->override_library->hierarchy_root,
396 for (IDOverrideLibraryProperty &op : id->override_library->properties) {
402 }
403 }
404 }
405
406 IDP_foreach_property(id->properties, IDP_TYPE_FILTER_ID, [&](IDProperty *prop) {
407 BKE_lib_query_idpropertiesForeachIDLink_callback(prop, &data);
408 });
411 return false;
412 }
413
414 IDP_foreach_property(id->system_properties, IDP_TYPE_FILTER_ID, [&](IDProperty *prop) {
415 BKE_lib_query_idpropertiesForeachIDLink_callback(prop, &data);
416 });
419 return false;
420 }
421
423 if (adt) {
427 return false;
428 }
429 }
430
431 const IDTypeInfo *id_type = BKE_idtype_get_info_from_id(id);
432 if (id_type->foreach_id != nullptr) {
433 id_type->foreach_id(id, &data);
434
437 return false;
438 }
439 }
440 }
441
443 return true;
444
445#undef CALLBACK_INVOKE_ID
446#undef CALLBACK_INVOKE
447}
448
450 ID *id,
452 void *user_data,
453 const LibraryForeachIDFlag flag)
454{
455 library_foreach_ID_link(bmain, nullptr, id, callback, user_data, flag, nullptr);
456}
457
458void BKE_library_update_ID_link_user(ID *id_dst, ID *id_src, const int cb_flag)
459{
460 if (cb_flag & IDWALK_CB_USER) {
461 id_us_min(id_src);
462 id_us_plus(id_dst);
463 }
464 else if (cb_flag & IDWALK_CB_USER_ONE) {
465 id_us_ensure_real(id_dst);
466 }
467}
468
470 Main *bmain,
471 ID *owner_id,
472 ID *self_id,
473 FunctionRef<void(LibraryForeachIDData *data)> subdata_foreach_id,
475 void *user_data,
476 const LibraryForeachIDFlag flag)
477{
480
482 data.bmain = bmain;
483 data.owner_id = owner_id;
484 data.self_id = self_id;
485 data.ids_handled = nullptr;
486 data.flag = flag;
487 data.status = 0;
488 data.callback = callback;
489 data.user_data = user_data;
490
491 subdata_foreach_id(&data);
492}
493
495 const bool include_ui,
496 const IDTypeInfo *owner_id_type)
497{
498 /* any type of ID can be used in custom props. */
499 if (owner_id->properties) {
500 return FILTER_ID_ALL;
501 }
502 if (owner_id->system_properties) {
503 return FILTER_ID_ALL;
504 }
505 /* When including UI data (i.e. editors), Screen UI IDs can also link to virtually any ID
506 * (through e.g. the Outliner). */
507 if (include_ui && owner_id->id_type() == ID_SCR) {
508 return FILTER_ID_ALL;
509 }
510
511 /* Casting to non const.
512 * TODO(jbakker): We should introduce a ntree_id_has_tree function as we are actually not
513 * interested in the result. */
514 if (bke::node_tree_from_id(const_cast<ID *>(owner_id))) {
515 return FILTER_ID_ALL;
516 }
517
518 if (BKE_animdata_from_id(owner_id)) {
519 /* AnimationData can use virtually any kind of data-blocks, through drivers especially. */
520 return FILTER_ID_ALL;
521 }
522
523 if (ID_IS_OVERRIDE_LIBRARY_REAL(owner_id)) {
524 /* LibOverride data 'hierarchy root' can virtually point back to any type of ID. */
525 return FILTER_ID_ALL;
526 }
527
528 if (!owner_id_type) {
529 owner_id_type = BKE_idtype_get_info_from_id(owner_id);
530 }
531 if (owner_id_type) {
532 return owner_id_type->dependencies_id_types;
533 }
535 return 0;
536}
537
538bool BKE_library_id_can_use_idtype(ID *owner_id, const short id_type_used)
539{
540 const IDTypeInfo *owner_id_type = BKE_idtype_get_info_from_id(owner_id);
541 const uint64_t filter_id_type_used = BKE_idtype_idcode_to_idfilter(id_type_used);
542 const uint64_t can_be_used = BKE_library_id_can_use_filter_id(owner_id, false, owner_id_type);
543 return (can_be_used & filter_id_type_used) != 0;
544}
545
546/* ***** ID users iterator. ***** */
549
550 // ListBaseT<ID> *lb_array[INDEX_ID_MAX]; /* UNUSED. */
551 // int lb_idx; /* UNUSED. */
552
554 int count_direct, count_indirect; /* Set by callback. */
555};
556
558{
559 ID **id_p = cb_data->id_pointer;
560 const LibraryForeachIDCallbackFlag cb_flag = cb_data->cb_flag;
561 IDUsersIter *iter = static_cast<IDUsersIter *>(cb_data->user_data);
562
563 if (*id_p) {
564 /* "Loop-back" ID pointers (the ugly *from* ones, like `Key->from`).
565 * Those are not actually ID usage, we can ignore them here.
566 */
567 if (cb_flag & IDWALK_CB_LOOPBACK) {
568 return IDWALK_RET_NOP;
569 }
570
571 if (*id_p == iter->id) {
572#if 0
573 printf(
574 "%s uses %s (refcounted: %d, userone: %d, used_one: %d, used_one_active: %d, "
575 "indirect_usage: %d)\n",
576 iter->curr_id->name,
577 iter->id->name,
578 (cb_flag & IDWALK_USER) ? 1 : 0,
579 (cb_flag & IDWALK_USER_ONE) ? 1 : 0,
580 (iter->id->tag & ID_TAG_EXTRAUSER) ? 1 : 0,
581 (iter->id->tag & ID_TAG_EXTRAUSER_SET) ? 1 : 0,
582 (cb_flag & IDWALK_INDIRECT_USAGE) ? 1 : 0);
583#endif
584 if (cb_flag & IDWALK_CB_INDIRECT_USAGE) {
585 iter->count_indirect++;
586 }
587 else {
588 iter->count_direct++;
589 }
590 }
591 }
592
593 return IDWALK_RET_NOP;
594}
595
596int BKE_library_ID_use_ID(ID *id_user, ID *id_used)
597{
598 IDUsersIter iter;
599
600 /* We do not care about iter.lb_array/lb_idx here... */
601 iter.id = id_used;
602 iter.curr_id = id_user;
603 iter.count_direct = iter.count_indirect = 0;
604
606 iter.curr_id,
608 static_cast<void *>(&iter),
610
611 return iter.count_direct + iter.count_indirect;
612}
613
614static bool library_ID_is_used(Main *bmain, void *idv, const bool check_linked)
615{
616 IDUsersIter iter;
617 MainListsArray lb_array = BKE_main_lists_get(*bmain);
618 int i = lb_array.size();
619 ID *id = static_cast<ID *>(idv);
620 bool is_defined = false;
621
622 iter.id = id;
623 iter.count_direct = iter.count_indirect = 0;
624 while (i-- && !is_defined) {
625 ID *id_curr = lb_array[i]->first();
626
627 if (!id_curr || !BKE_library_id_can_use_idtype(id_curr, id->id_type())) {
628 continue;
629 }
630
631 for (; id_curr && !is_defined; id_curr = static_cast<ID *>(id_curr->next)) {
632 if (id_curr == id) {
633 /* We are not interested in self-usages (mostly from drivers or bone constraints...). */
634 continue;
635 }
636 iter.curr_id = id_curr;
639
640 is_defined = ((check_linked ? iter.count_indirect : iter.count_direct) != 0);
641 }
642 }
643
644 return is_defined;
645}
646
648{
649 return library_ID_is_used(bmain, idv, false);
650}
651
653{
654 return library_ID_is_used(bmain, idv, true);
655}
656
658 void *idv,
659 bool *r_is_used_local,
660 bool *r_is_used_linked)
661{
662 IDUsersIter iter;
663 MainListsArray lb_array = BKE_main_lists_get(*bmain);
664 int i = lb_array.size();
665 ID *id = static_cast<ID *>(idv);
666 bool is_defined = false;
667
668 iter.id = id;
669 iter.count_direct = iter.count_indirect = 0;
670 while (i-- && !is_defined) {
671 ID *id_curr = lb_array[i]->first();
672
673 if (!id_curr || !BKE_library_id_can_use_idtype(id_curr, id->id_type())) {
674 continue;
675 }
676
677 for (; id_curr && !is_defined; id_curr = static_cast<ID *>(id_curr->next)) {
678 if (id_curr == id) {
679 /* We are not interested in self-usages (mostly from drivers or bone constraints...). */
680 continue;
681 }
682 iter.curr_id = id_curr;
685
686 is_defined = (iter.count_direct != 0 && iter.count_indirect != 0);
687 }
688 }
689
690 *r_is_used_local = (iter.count_direct != 0);
691 *r_is_used_linked = (iter.count_indirect != 0);
692}
693
694/* ***** IDs usages.checking/tagging. ***** */
695
706 public:
708
709 const int id_tag;
710
714
716
717 std::array<int, INDEX_ID_MAX> *num_total;
718 std::array<int, INDEX_ID_MAX> *num_local;
719 std::array<int, INDEX_ID_MAX> *num_linked;
720
722 enum class Status : int8_t { Unknown, Used, Unused };
723
724 private:
725 /* Statuses of IDs.
726 *
727 * NOTE: In current implementation, 'Unknown' status IDs are not explicitly stored in this
728 * mapping. */
729 Map<ID *, Status> ids_status_;
730
731 public:
733 : bmain(bmain),
734 id_tag(id_tag),
735 do_local_ids(parameters.do_local_ids),
736 do_linked_ids(parameters.do_linked_ids),
737 do_recursive(parameters.do_recursive),
738 filter_fn(parameters.filter_fn),
739 num_total(&parameters.num_total),
740 num_local(&parameters.num_local),
741 num_linked(&parameters.num_linked)
742 {
743 }
744
746 void reset(const bool do_local_ids,
747 const bool do_linked_ids,
748 const bool do_recursive,
749 std::array<int, INDEX_ID_MAX> &num_total,
750 std::array<int, INDEX_ID_MAX> &num_local,
751 std::array<int, INDEX_ID_MAX> &num_linked)
752 {
753 ids_status_.clear();
754 this->do_local_ids = do_local_ids;
755 this->do_linked_ids = do_linked_ids;
756 this->do_recursive = do_recursive;
757 this->num_total = &num_total;
758 this->num_local = &num_local;
759 this->num_linked = &num_linked;
760 }
761
763 void set_id_status(ID &id, Status status)
764 {
765 if (id.flag & ID_FLAG_EMBEDDED_DATA) {
766 /* Nothing to do for embedded IDs, these may have to be processed in dependency chains, but
767 * are never actually considered for used/unused status, as they are private data of their
768 * owner ID. */
769 return;
770 }
771 if (status == ids_status_.lookup_default(&id, Status::Unknown)) {
772 return;
773 }
774
775 /* If the generic code has decided that this ID was unused, but the special filter callback
776 * says otherwise, then the final status of this ID must be forced to 'used'. */
777 if (status == Status::Unused && this->filter_fn && !this->filter_fn(&id)) {
778 status = Status::Used;
779 }
780
781 ids_status_.add_overwrite(&id, status);
782 }
783
786 {
787 return ids_status_.lookup_default(&id, Status::Unknown);
788 }
789
795 void tag_ids() const
796 {
797 ID *id;
798 FOREACH_MAIN_ID_BEGIN (this->bmain, id) {
799 const Status status = ids_status_.lookup_default(id, Status::Unknown);
800 const int id_type_index = BKE_idtype_idcode_to_index(id->id_type());
801 if (status == Status::Unused) {
802 id->tag |= this->id_tag;
803 (*this->num_total)[INDEX_ID_NULL]++;
804 (*this->num_total)[id_type_index]++;
805 if (ID_IS_LINKED(id)) {
806 (*this->num_linked)[INDEX_ID_NULL]++;
807 (*this->num_linked)[id_type_index]++;
808 }
809 else {
810 (*this->num_local)[INDEX_ID_NULL]++;
811 (*this->num_local)[id_type_index]++;
812 }
813 }
814 else {
815 id->tag &= ~this->id_tag;
816 }
817 }
819 }
820};
821
824{
825 if ((!data.do_linked_ids && ID_IS_LINKED(&id)) || (!data.do_local_ids && !ID_IS_LINKED(&id))) {
826 return true;
827 }
828
829 if (ID_FAKE_USERS(&id) > 0) {
830 return true;
831 }
832
833 switch (id.id_type()) {
834 case ID_IM: {
835 /* Images which have a 'viewer' source (e.g. render results) should not be considered as
836 * orphaned/unused data. */
837 const Image &image = reinterpret_cast<Image &>(id);
838 if (image.source == IMA_SRC_VIEWER) {
839 return true;
840 }
841 break;
842 }
843 default:
844 break;
845 }
846
847 const IDTypeInfo *id_type = BKE_idtype_get_info_from_id(&id);
848 if ((id_type->flags & IDTYPE_FLAGS_NEVER_UNUSED) != 0) {
849 return true;
850 }
851
852 return false;
853}
854
862{
863 switch (id.id_type()) {
864 case ID_OB: {
865 /* FIXME: This is a workaround until Object usages are handled more soundly.
866 *
867 * Historically, only reference-counting Object usages were the Collection ones. All other
868 * references (e.g. as Constraints or Modifiers targets) did not increase their user-count.
869 *
870 * This is not entirely true anymore (e.g. some type-agnostic ID usages like IDPointer custom
871 * properties do refcount Object ones too), but there are still many Object usages that
872 * should refcount them and don't do it.
873 *
874 * This becomes a problem with linked data, as in that case instancing of linked Objects in
875 * the scene is not enforced (to avoid cluttering the scene), which leaves some actually used
876 * linked objects with a `0` user-count.
877 *
878 * So this is a special check to consider linked objects as used also in case some other
879 * used ID uses them.
880 */
881 if (ID_IS_LINKED(&id)) {
882 return true;
883 }
884 break;
885 }
886 default:
887 break;
888 }
889 return false;
890}
891
900 const int required_usages)
901{
902 BLI_assert(data.get_id_status(id) == UnusedIDsData::Status::Used ||
903 (id.flag & ID_FLAG_EMBEDDED_DATA) != 0);
904
905 Stack<ID *> todo_ids = {&id};
906
907 auto foreach_id_callback =
908 [&data, &todo_ids, &required_usages](LibraryIDLinkCallbackData *foreachid_cb_data) -> int {
909 /* These ID usages are never valid.
910 *
911 * Note that embedded ID pointers are ignored here, because their are handled as 'private ID
912 * data' here, and the 'owner ID' is always used as reference. */
913 constexpr int ignored_usages = (IDWALK_CB_LOOPBACK | IDWALK_CB_EMBEDDED |
915
916 if ((foreachid_cb_data->cb_flag & ignored_usages) != 0) {
917 return IDWALK_RET_NOP;
918 }
919
920 ID &owner_id = *foreachid_cb_data->owner_id;
921 ID *id_to = *foreachid_cb_data->id_pointer;
922 if (!id_to || id_to == &owner_id || data.get_id_status(*id_to) == UnusedIDsData::Status::Used)
923 {
924 /* Already known to be used, potentially a dependency cycle, no need to go deeper in this
925 * branch in any case. */
926 return IDWALK_RET_NOP;
927 }
928
929 /* Note: First condition is always `true` if `required_usages == IDWALK_CB_NOP`. */
930 if ((foreachid_cb_data->cb_flag & required_usages) == 0 &&
932 {
933 /* The required usage conditions are not met, and it's not an 'enforced usage exception'.
934 */
935 return IDWALK_RET_NOP;
936 }
937
938 data.set_id_status(*id_to, UnusedIDsData::Status::Used);
939 todo_ids.push(id_to);
940 return IDWALK_RET_NOP;
941 };
942
943 while (!todo_ids.is_empty()) {
944 ID *id_iter = todo_ids.pop();
945
947 data.bmain, id_iter, foreach_id_callback, nullptr, IDWALK_READONLY);
948 }
949}
950
952{
953 BLI_assert(data.bmain->relations != nullptr);
954 BLI_assert(data.do_recursive);
955
956 /* First loop, basic check for directly unused IDs (those with 0 user count). */
957 ID *id;
958 FOREACH_MAIN_ID_BEGIN (data.bmain, id) {
959 const IDTypeInfo *id_type = BKE_idtype_get_info_from_id(id);
960 UNUSED_VARS_NDEBUG(id_type);
961 if (id_is_enforced_used(*id, data)) {
962 data.set_id_status(*id, UnusedIDsData::Status::Used);
963 }
964 else if (id->us == 0) {
965 BLI_assert((id_type->flags & IDTYPE_FLAGS_NEVER_UNUSED) == 0);
966 data.set_id_status(*id, UnusedIDsData::Status::Unused);
967 }
968 else {
969 data.set_id_status(*id, UnusedIDsData::Status::Unknown);
970 }
971 }
973
974 /* Pre-mark all IDs not yet known to be used as unused. */
975 FOREACH_MAIN_ID_BEGIN (data.bmain, id) {
976 if (data.get_id_status(*id) == UnusedIDsData::Status::Used) {
977 continue;
978 }
979 data.set_id_status(*id, UnusedIDsData::Status::Unused);
980 }
982
983 /* Follow all valid dependencies of known used IDs and mark them as used as well. */
984 FOREACH_MAIN_ID_BEGIN (data.bmain, id) {
985 if (data.get_id_status(*id) != UnusedIDsData::Status::Used) {
986 continue;
987 }
989 }
991
992 data.tag_ids();
993}
994
996{
997 BLI_assert(data.bmain->relations != nullptr);
998 BLI_assert(!data.do_recursive);
999
1000 /* First loop, basic check for immediately unused IDs (those with 0 user count). */
1001 ID *id;
1002 FOREACH_MAIN_ID_BEGIN (data.bmain, id) {
1003 const IDTypeInfo *id_type = BKE_idtype_get_info_from_id(id);
1004 UNUSED_VARS_NDEBUG(id_type);
1005 if (id_is_enforced_used(*id, data)) {
1006 data.set_id_status(*id, UnusedIDsData::Status::Used);
1007 }
1008 else if (id->us == 0) {
1009 BLI_assert((id_type->flags & IDTYPE_FLAGS_NEVER_UNUSED) == 0);
1010 data.set_id_status(*id, UnusedIDsData::Status::Unused);
1011 }
1012 else {
1013 /* In direct case, if an ID is not explicitly unused, it is always considered as used. */
1014 data.set_id_status(*id, UnusedIDsData::Status::Used);
1015 }
1016 }
1018
1019 /* Special post-process to handle 'exception cases', i.e. IDs that are effectively not used, but
1020 * should be considered as used based on their non-refcounting relationships to other IDs.
1021 *
1022 * Currently, this covers linked but not instantiated Objects (see also
1023 * #id_is_used_dependency_exception). */
1024 FOREACH_MAIN_ID_BEGIN (data.bmain, id) {
1025 if (data.get_id_status(*id) != UnusedIDsData::Status::Used) {
1026 continue;
1027 }
1029 }
1031
1032 data.tag_ids();
1033}
1034
1036{
1037 if (data.do_recursive) {
1039 }
1040 else {
1042 }
1043}
1044
1046{
1047 std::array<int, INDEX_ID_MAX> num_dummy{0};
1048 BKE_main_relations_create(bmain, 0);
1049
1050 parameters.num_total.fill(0);
1051 parameters.num_local.fill(0);
1052 parameters.num_linked.fill(0);
1053
1054 /* The complex fiddling with the two calls, which data they each get, based on the `do_local_ids`
1055 * and `do_linked_ids`, is here to reduce as much as possible the extra processing:
1056 *
1057 * If both local and linked options are enabled, a single call with all given parameters gives
1058 * all required data about unused IDs.
1059 *
1060 * If both local and linked options are disabled, total amount is left at zero, and each local
1061 * and linked amounts are computed separately.
1062 *
1063 * If local is disabled and linked is enabled, the first call will compute the amount of local
1064 * IDs that would be unused if the local option was enabled. Therefore, only the local amount can
1065 * be kept from this call. The second call will compute valid values for both linked, and total
1066 * data.
1067 *
1068 * If local is enabled and linked is disabled, the first call will compute valid values for both
1069 * local, and total data. The second call will compute the amount of linked IDs that would be
1070 * unused if the linked option was enabled. Therefore, only the linked amount can be kept from
1071 * this call.
1072 */
1073
1074 UnusedIDsData data(bmain, 0, parameters);
1075 data.do_local_ids = true;
1076 if (!parameters.do_local_ids) {
1077 data.num_total = &num_dummy;
1078 }
1079 if (!(parameters.do_local_ids && parameters.do_linked_ids)) {
1080 data.num_linked = &num_dummy;
1081 }
1083
1084 if (!(parameters.do_local_ids && parameters.do_linked_ids)) {
1085 /* In case a second run is required, clear runtime data and update settings for linked data. */
1086 data.reset(parameters.do_local_ids,
1087 true,
1088 parameters.do_recursive,
1089 (!parameters.do_local_ids && parameters.do_linked_ids) ? parameters.num_total :
1090 num_dummy,
1091 num_dummy,
1092 parameters.num_linked);
1094 }
1095
1097}
1098
1099void BKE_lib_query_unused_ids_tag(Main *bmain, const int tag, LibQueryUnusedIDsData &parameters)
1100{
1101 BLI_assert(tag != 0);
1102
1103 parameters.num_total.fill(0);
1104 parameters.num_local.fill(0);
1105 parameters.num_linked.fill(0);
1106
1107 UnusedIDsData data(bmain, tag, parameters);
1108
1109 BKE_main_relations_create(bmain, 0);
1112}
1113
1115{
1116 ID *self_id = cb_data->self_id;
1117 ID **id_p = cb_data->id_pointer;
1118 const LibraryForeachIDCallbackFlag cb_flag = cb_data->cb_flag;
1119 bool *is_changed = static_cast<bool *>(cb_data->user_data);
1120
1121 if (*id_p) {
1122 /* The infamous 'from' pointers (Key.from, ...).
1123 * those are not actually ID usage, so we ignore them here. */
1124 if (cb_flag & IDWALK_CB_LOOPBACK) {
1125 return IDWALK_RET_NOP;
1126 }
1127
1128 /* If checked id is used by an assumed used ID,
1129 * then it is also used and not part of any linked archipelago. */
1130 if (!(self_id->tag & ID_TAG_DOIT) && ((*id_p)->tag & ID_TAG_DOIT)) {
1131 (*id_p)->tag &= ~ID_TAG_DOIT;
1132 *is_changed = true;
1133 }
1134 }
1135
1136 return IDWALK_RET_NOP;
1137}
1138
1139void BKE_library_unused_linked_data_set_tag(Main *bmain, const bool do_init_tag)
1140{
1141 ID *id;
1142
1143 if (do_init_tag) {
1144 FOREACH_MAIN_ID_BEGIN (bmain, id) {
1145 if (id->lib && (id->tag & ID_TAG_INDIRECT) != 0) {
1146 id->tag |= ID_TAG_DOIT;
1147 }
1148 else {
1149 id->tag &= ~ID_TAG_DOIT;
1150 }
1151 }
1153 }
1154
1155 for (bool do_loop = true; do_loop;) {
1156 do_loop = false;
1157 FOREACH_MAIN_ID_BEGIN (bmain, id) {
1158 /* We only want to check that ID if it is currently known as used... */
1159 if ((id->tag & ID_TAG_DOIT) == 0) {
1162 }
1163 }
1165 }
1166}
1167
1169{
1170 bool do_loop = true;
1171 while (do_loop) {
1172 MainListsArray lb_array = BKE_main_lists_get(*bmain);
1173 int i = lb_array.size();
1174 do_loop = false;
1175
1176 while (i--) {
1177 for (ID &id : *lb_array[i]) {
1178 if (!ID_IS_LINKED(&id) || id.tag & ID_TAG_DOIT) {
1179 /* Local or non-indirectly-used ID (so far), no need to check it further. */
1180 continue;
1181 }
1184 }
1185 }
1186 }
1187}
1188
1189} // namespace blender
#define BKE_LIB_FOREACHID_PROCESS_ID(data_, id_, cb_flag_)
#define FOREACH_MAIN_ID_END
Definition BKE_main.hh:735
#define FOREACH_MAIN_ID_BEGIN(_bmain, _id)
Definition BKE_main.hh:729
#define ID_FAKE_USERS(id)
Definition DNA_ID.h:658
#define FILTER_ID_ALL
Definition DNA_ID.h:1241
#define ID_IS_OVERRIDE_LIBRARY_REAL(_id)
Definition DNA_ID.h:729
#define ID_IS_LINKED(_id)
Definition DNA_ID.h:697
#define BLI_assert_unreachable()
#define UNUSED_VARS_NDEBUG(...)
#define ELEM(...)
#define BLI_assert_msg(a, msg)
bool is_empty() const
Definition BLI_stack.hh:310
void push(const T &value)
Definition BLI_stack.hh:215
FunctionRef< bool(ID *id)> filter_fn
Definition lib_query.cc:715
void set_id_status(ID &id, Status status)
Definition lib_query.cc:763
void reset(const bool do_local_ids, const bool do_linked_ids, const bool do_recursive, std::array< int, INDEX_ID_MAX > &num_total, std::array< int, INDEX_ID_MAX > &num_local, std::array< int, INDEX_ID_MAX > &num_linked)
Definition lib_query.cc:746
Status get_id_status(ID &id) const
Definition lib_query.cc:785
std::array< int, INDEX_ID_MAX > * num_total
Definition lib_query.cc:717
std::array< int, INDEX_ID_MAX > * num_local
Definition lib_query.cc:718
std::array< int, INDEX_ID_MAX > * num_linked
Definition lib_query.cc:719
UnusedIDsData(Main *bmain, const int id_tag, LibQueryUnusedIDsData &parameters)
Definition lib_query.cc:732
#define CALLBACK_INVOKE(check_id_super, cb_flag)
#define CALLBACK_INVOKE_ID(check_id, cb_flag)
bNodeTree * node_tree_from_id(ID *id)
Definition node.cc:5337
BMesh Iterators.
void BKE_main_relations_free(Main *bmain)
Definition main.cc:760
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
std::array< ListBaseT< ID > *, INDEX_ID_MAX - 1 > MainListsArray
Definition BKE_main.hh:797
uint64_t BKE_idtype_idcode_to_idfilter(short idcode)
Definition idtype.cc:473
unsigned __int64 uint64_t
Definition BKE_text.h:91
Main * BKE_lib_query_foreachid_process_main_get(const LibraryForeachIDData *data)
Definition lib_query.cc:138
void BKE_lib_query_unused_ids_tag(Main *bmain, int tag, LibQueryUnusedIDsData &parameters)
bool BKE_library_id_can_use_idtype(ID *owner_id, short id_type_used)
Definition lib_query.cc:538
static int foreach_libblock_used_linked_data_tag_clear_cb(LibraryIDLinkCallbackData *cb_data)
void id_us_plus(ID *id)
Definition lib_id.cc:433
static void library_foreach_ID_data_cleanup(LibraryForeachIDData *data)
Definition lib_query.cc:212
@ IDWALK_NO_ORIG_POINTERS_ACCESS
@ IDWALK_IGNORE_EMBEDDED_ID
@ IDWALK_DO_INTERNAL_RUNTIME_POINTERS
@ IDWALK_DO_LIBRARY_POINTER
@ IDWALK_DO_DEPRECATED_POINTERS
@ IDWALK_IGNORE_MISSING_OWNER_ID
void IDP_foreach_property(IDProperty *id_property_root, int type_filter, FunctionRef< void(IDProperty *id_property)> callback)
MainListsArray BKE_main_lists_get(Main &bmain)
Definition main.cc:1158
static void lib_query_unused_ids_direct_tag(UnusedIDsData &data)
Definition lib_query.cc:995
LibraryForeachIDFlag BKE_lib_query_foreachid_process_flags_get(const LibraryForeachIDData *data)
Definition lib_query.cc:133
static void lib_query_unused_ids_find_used_dependencies(ID &id, UnusedIDsData &data, const int required_usages)
Definition lib_query.cc:898
@ MAINIDRELATIONS_INCLUDE_UI
Definition BKE_main.hh:222
void BKE_lib_query_foreachid_process(LibraryForeachIDData *data, ID **id_pp, LibraryForeachIDCallbackFlag cb_flag)
Definition lib_query.cc:82
void BKE_library_ID_test_usages(Main *bmain, void *idv, bool *r_is_used_local, bool *r_is_used_linked)
Definition lib_query.cc:657
AnimData * BKE_animdata_from_id(const ID *id)
Definition anim_data.cc:93
static bool library_ID_is_used(Main *bmain, void *idv, const bool check_linked)
Definition lib_query.cc:614
static bool id_is_used_dependency_exception(ID &id)
Definition lib_query.cc:861
const IDTypeInfo * BKE_idtype_get_info_from_id(const ID *id)
Definition idtype.cc:232
void BKE_lib_query_unused_ids_amounts(Main *bmain, LibQueryUnusedIDsData &parameters)
void BKE_library_foreach_subdata_id(Main *bmain, ID *owner_id, ID *self_id, FunctionRef< void(LibraryForeachIDData *data)> subdata_foreach_id, FunctionRef< LibraryIDLinkCallback > callback, void *user_data, const LibraryForeachIDFlag flag)
Definition lib_query.cc:469
int BKE_lib_query_foreachid_process_callback_flag_override(LibraryForeachIDData *data, LibraryForeachIDCallbackFlag cb_flag, bool do_replace)
Definition lib_query.cc:143
@ IDP_FLAG_OVERRIDABLE_LIBRARY
bool BKE_library_ID_is_locally_used(Main *bmain, void *idv)
Definition lib_query.cc:647
static bool id_is_enforced_used(ID &id, UnusedIDsData &data)
Definition lib_query.cc:823
BMesh const char void * data
void BKE_library_foreach_ID_embedded(LibraryForeachIDData *data, ID **id_pp)
Definition lib_query.cc:176
int BKE_idtype_idcode_to_index(short idcode)
Definition idtype.cc:338
void BKE_animdata_foreach_id(AnimData *adt, LibraryForeachIDData *data)
Definition anim_data.cc:245
void BKE_library_unused_linked_data_set_tag(Main *bmain, bool do_init_tag)
void BKE_main_relations_create(Main *bmain, short flag)
Definition main.cc:723
static bool library_foreach_ID_link(Main *bmain, ID *owner_id, ID *id, FunctionRef< LibraryIDLinkCallback > callback, void *user_data, LibraryForeachIDFlag flag, LibraryForeachIDData *inherit_data)
Definition lib_query.cc:221
bool BKE_lib_query_foreachid_iter_stop(const LibraryForeachIDData *data)
Definition lib_query.cc:77
void id_us_ensure_real(ID *id)
Definition lib_id.cc:388
@ IDWALK_RET_STOP_ITER
@ IDWALK_RET_STOP_RECURSION
@ INDEX_ID_NULL
Definition DNA_ID.h:1359
uint64_t BKE_library_id_can_use_filter_id(const ID *owner_id, const bool include_ui, const IDTypeInfo *owner_id_type=nullptr)
Definition lib_query.cc:494
void BKE_library_update_ID_link_user(ID *id_dst, ID *id_src, int cb_flag)
Definition lib_query.cc:458
@ ID_TAG_INDIRECT
Definition DNA_ID.h:851
@ ID_TAG_EXTRAUSER_SET
Definition DNA_ID.h:887
@ ID_TAG_NO_USER_REFCOUNT
Definition DNA_ID.h:1011
@ ID_TAG_EXTRAUSER
Definition DNA_ID.h:881
@ ID_TAG_DOIT
Definition DNA_ID.h:1029
static CLG_LogRef LOG
Definition logging.cpp:12
ID * BKE_id_owner_get(ID *id, const bool debug_relationship_assert=true)
Definition lib_id.cc:2770
@ ID_FLAG_EMBEDDED_DATA
Definition DNA_ID.h:775
static int foreach_libblock_id_users_callback(LibraryIDLinkCallbackData *cb_data)
Definition lib_query.cc:557
bool BKE_library_ID_is_indirectly_used(Main *bmain, void *idv)
Definition lib_query.cc:652
LibraryForeachIDCallbackFlag
@ IDWALK_CB_LOOPBACK
@ IDWALK_CB_DIRECT_WEAK_LINK
@ IDWALK_CB_EMBEDDED_NOT_OWNING
@ IDWALK_CB_OVERRIDE_LIBRARY_NOT_OVERRIDABLE
@ IDWALK_CB_NEVER_SELF
@ IDWALK_CB_INTERNAL
@ IDWALK_CB_OVERRIDE_LIBRARY_HIERARCHY_ROOT
@ IDWALK_CB_OVERRIDE_LIBRARY_REFERENCE
@ IDWALK_CB_EMBEDDED
@ IDWALK_CB_INDIRECT_USAGE
static void lib_query_unused_ids_recursive_tag(UnusedIDsData &data)
Definition lib_query.cc:951
int BKE_library_ID_use_ID(ID *id_user, ID *id_used)
Definition lib_query.cc:596
static void lib_query_unused_ids_tag(UnusedIDsData &data)
void BKE_lib_query_idpropertiesForeachIDLink_callback(IDProperty *id_prop, void *user_data)
Definition lib_query.cc:164
void id_us_min(ID *id)
Definition lib_id.cc:441
void BKE_library_foreach_ID_link(Main *bmain, ID *id, FunctionRef< LibraryIDLinkCallback > callback, void *user_data, LibraryForeachIDFlag flag)
Definition lib_query.cc:449
@ IDP_TYPE_FILTER_ID
signed char int8_t
Definition BKE_text.h:76
@ IDWALK_STOP
Definition lib_query.cc:36
void BKE_library_indirectly_used_data_tag_clear(Main *bmain)
@ IDTYPE_FLAGS_NEVER_UNUSED
Definition BKE_idtype.hh:73
ListBaseT< IDOverrideLibraryPropertyOperation > operations
Definition DNA_ID.h:326
eIDPropertyFlag flag
Definition DNA_ID.h:151
eIDPropertyType type
Definition DNA_ID.h:145
IDPropertyData data
Definition DNA_ID.h:157
IDTypeForeachIDFunction foreach_id
uint64_t dependencies_id_types
char name[258]
Definition DNA_ID.h:433
IDProperty * system_properties
Definition DNA_ID.h:490
short flag
Definition DNA_ID.h:439
void * next
Definition DNA_ID.h:418
IDProperty * properties
Definition DNA_ID.h:481
std::array< int, INDEX_ID_MAX > num_linked
std::array< int, INDEX_ID_MAX > num_total
std::array< int, INDEX_ID_MAX > num_local
LibraryForeachIDFlag flag
Definition lib_query.cc:53
LibraryForeachIDCallbackFlag cb_flag
Definition lib_query.cc:55
FunctionRef< LibraryIDLinkCallback > callback
Definition lib_query.cc:63
LibraryForeachIDCallbackFlag cb_flag_clear
Definition lib_query.cc:57
BLI_LINKSTACK_DECLARE(ids_todo, ID *)
LibraryForeachIDCallbackFlag cb_flag
MainIDRelationsEntryItem * next
Definition BKE_main.hh:139
MainIDRelationsEntryItem * to_ids
Definition BKE_main.hh:158
Map< const ID *, MainIDRelationsEntry * > * relations_from_pointers
Definition BKE_main.hh:211
MainIDRelations * relations
Definition BKE_main.hh:437