Blender V5.3
lib_id_delete.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include "MEM_guardedalloc.h"
12
13/* all types are needed here, in order to do memory operations */
14#include "DNA_ID.h"
15#include "DNA_key_types.h"
16
17#include "BLI_utildefines.hh"
18
19#include "BLI_listbase.hh"
20#include "BLI_set.hh"
21#include "BLI_vector.hh"
22
23#include "BKE_anim_data.hh"
24#include "BKE_asset.hh"
25#include "BKE_idprop.hh"
26#include "BKE_idtype.hh"
27#include "BKE_key.hh"
28#include "BKE_layer.hh"
29#include "BKE_lib_id.hh"
30#include "BKE_lib_override.hh"
31#include "BKE_lib_remap.hh"
32#include "BKE_library.hh"
33#include "BKE_main.hh"
35#include "BKE_main_namemap.hh"
36
37#include "BLO_readfile.hh"
38
39#include "lib_intern.hh"
40
41#include "DEG_depsgraph.hh"
42
43#ifdef WITH_PYTHON
44# include "BPY_extern.hh"
45#endif
46
47namespace blender {
48
49using namespace bke::id;
50
51void BKE_libblock_free_data(ID *id, const bool do_id_user)
52{
53 if (id->properties) {
54 IDP_FreeProperty_ex(id->properties, do_id_user);
55 id->properties = nullptr;
56 }
57 if (id->system_properties) {
58 IDP_FreeProperty_ex(id->system_properties, do_id_user);
59 id->system_properties = nullptr;
60 }
61
62 if (id->override_library) {
63 BKE_lib_override_library_free(&id->override_library, do_id_user);
64 id->override_library = nullptr;
65 }
66
67 if (id->asset_data) {
68 BKE_asset_metadata_free(&id->asset_data);
69 }
70
71 if (id->library_weak_reference != nullptr) {
72 MEM_delete(id->library_weak_reference);
73 }
74
75 BKE_animdata_free(id, do_id_user);
76
78}
79
80void BKE_libblock_free_datablock(ID *id, const int /*flag*/)
81{
82 const IDTypeInfo *idtype_info = BKE_idtype_get_info_from_id(id);
83
84 if (idtype_info != nullptr) {
85 if (idtype_info->free_data != nullptr) {
86 idtype_info->free_data(id);
87 }
88 return;
89 }
90
91 BLI_assert_msg(0, "IDType Missing IDTypeInfo");
92}
93
95{
96 if (id->runtime) {
97 /* During "normal" file loading this data is released when versioning ends. Some versioning
98 * code also deletes IDs, though. For example, in the startup blend file, brushes that were
99 * replaced by assets are deleted. This means that the regular "delete this ID" flow (aka this
100 * code here) also needs to free this data. */
102
103 MEM_SAFE_DELETE(id->runtime);
104 }
105}
106
107static int id_free(Main *bmain, void *idv, int flag, const bool use_flag_from_idtag)
108{
109 ID *id = static_cast<ID *>(idv);
110
111 if (use_flag_from_idtag) {
112 if ((id->tag & ID_TAG_NO_MAIN) != 0) {
114 }
115 else {
116 flag &= ~LIB_ID_FREE_NO_MAIN;
117 }
118
119 if ((id->tag & ID_TAG_NO_USER_REFCOUNT) != 0) {
121 }
122 else {
124 }
125
126 if ((id->tag & ID_TAG_NOT_ALLOCATED) != 0) {
128 }
129 else {
131 }
132 }
133
134 BLI_assert((flag & LIB_ID_FREE_NO_MAIN) != 0 || bmain != nullptr);
135 BLI_assert((flag & LIB_ID_FREE_NO_MAIN) != 0 || (flag & LIB_ID_FREE_NOT_ALLOCATED) == 0);
136 BLI_assert((flag & LIB_ID_FREE_NO_MAIN) != 0 || (flag & LIB_ID_FREE_NO_USER_REFCOUNT) == 0);
137
138 const short type = id->id_type();
139
140 if (bmain && (flag & LIB_ID_FREE_NO_DEG_TAG) == 0) {
141 BLI_assert(bmain->is_locked_for_linking == false);
142
143 DEG_id_type_tag(bmain, type);
144 }
145
147
148 Key *key = ((flag & LIB_ID_FREE_NO_MAIN) == 0) ? BKE_key_from_id(id) : nullptr;
149
150 if ((flag & LIB_ID_FREE_NO_USER_REFCOUNT) == 0) {
151 BKE_libblock_relink_ex(bmain, id, nullptr, nullptr, ID_REMAP_SKIP_USER_CLEAR);
152 }
153
154 if ((flag & LIB_ID_FREE_NO_MAIN) == 0 && key != nullptr) {
155 id_free(bmain, &key->id, flag, use_flag_from_idtag);
156 }
157
159
160 /* avoid notifying on removed data */
161 if ((flag & LIB_ID_FREE_NO_MAIN) == 0) {
162 BKE_main_lock(bmain);
163 }
164
165 if ((flag & LIB_ID_FREE_NO_UI_USER) == 0) {
168 }
169
171 IDRemapper remapper;
172 remapper.add(id, nullptr);
174 }
175 }
176
177 if ((flag & LIB_ID_FREE_NO_MAIN) == 0) {
178 ListBaseT<ID> *lb = which_libbase(bmain, type);
179 BLI_remlink(lb, id);
180 if ((flag & LIB_ID_FREE_NO_NAMEMAP_REMOVE) == 0) {
181 BKE_main_namemap_remove_id(*bmain, *id);
182 }
183 }
184
186
187 if ((flag & LIB_ID_FREE_NO_MAIN) == 0) {
188 BKE_main_unlock(bmain);
189 }
190
191 if ((flag & LIB_ID_FREE_NOT_ALLOCATED) == 0) {
192 MEM_delete(id);
193 }
194
195 return flag;
196}
197
198void BKE_id_free_ex(Main *bmain, void *idv, const int flag_orig, const bool use_flag_from_idtag)
199{
200 /* ViewLayer resync needs to be delayed during Scene freeing, since internal relationships
201 * between the Scene's master collection and its view_layers become invalid
202 * (due to remapping). */
203 if (bmain && (flag_orig & LIB_ID_FREE_NO_MAIN) == 0) {
205 }
206
207 const ID_Type id_type = static_cast<ID *>(idv)->id_type();
208
209 int flag_final = id_free(bmain, idv, flag_orig, use_flag_from_idtag);
210
211 if (bmain) {
212 if ((flag_orig & LIB_ID_FREE_NO_MAIN) == 0) {
214 }
215
216 if ((flag_final & LIB_ID_FREE_NO_MAIN) == 0) {
217 if (ELEM(id_type, ID_SCE, ID_GR, ID_OB)) {
219 }
220 }
221 }
222}
223
224void BKE_id_free(Main *bmain, void *idv)
225{
226 BKE_id_free_ex(bmain, idv, 0, true);
227}
228
229void BKE_id_free_us(Main *bmain, void *idv) /* test users */
230{
231 ID *id = static_cast<ID *>(idv);
232
233 id_us_min(id);
234
235 /* XXX This is a temp (2.77) hack so that we keep same behavior as in 2.76 regarding collections
236 * when deleting an object. Since only 'user_one' usage of objects is collections,
237 * and only 'real user' usage of objects is scenes, removing that 'user_one' tag when there
238 * is no more real (scene) users of an object ensures it gets fully unlinked.
239 * But only for local objects, not linked ones!
240 * Otherwise, there is no real way to get rid of an object anymore -
241 * better handling of this is TODO.
242 */
243 if ((id->id_type() == ID_OB) && (id->us == 1) && !ID_IS_LINKED(id)) {
245 }
246
247 if (id->us == 0) {
248 const bool is_lib = id->id_type() == ID_LI;
249
250 BKE_libblock_unlink(bmain, id, false);
251
252 BKE_id_free(bmain, id);
253
254 if (is_lib) {
256 }
257 }
258}
259
260static size_t id_delete(Main *bmain, Set<ID *> &ids_to_delete, const BKEIDDeleteOptions &options)
261{
262 const int extra_remapping_flags = options.extra_remapping_flags;
263 bool has_deleted_library = false;
264 bool has_deleted_linked_or_liboverride_id = false;
265
266 /* Used by batch tagged deletion, when we call BKE_id_free then, id is no more in Main database,
267 * and has already properly unlinked its other IDs usages.
268 * UI users are always cleared in BKE_libblock_remap_locked() call, so we can always skip it. */
269 const int free_flag = LIB_ID_FREE_NO_UI_USER | LIB_ID_FREE_NO_MAIN |
272 ID_REMAP_FORCE_INTERNAL_RUNTIME_POINTERS | extra_remapping_flags);
273
274 MainListsArray lbarray = BKE_main_lists_get(*bmain);
275 const int base_count = lbarray.size();
276
277 BKE_main_lock(bmain);
279 IDRemapper id_remapper;
280
281 auto process_id_to_delete =
282 [&bmain, &ids_to_delete, &id_remapper, &has_deleted_linked_or_liboverride_id](
283 ListBaseT<ID> *lb, ID *id_iter) -> void {
284 /* Do not tag as no_main now, we want to unlink it first (lower-level ID management
285 * code has some specific handling of 'no main' IDs that would be a problem in that
286 * case). */
287 BLI_remlink(lb, id_iter);
288 BKE_main_namemap_remove_id(*bmain, *id_iter);
289 ids_to_delete.add(id_iter);
290 id_remapper.add(id_iter, nullptr);
291
292 if (!has_deleted_linked_or_liboverride_id &&
293 (ID_IS_LINKED(id_iter) || ID_IS_OVERRIDE_LIBRARY(id_iter)))
294 {
295 has_deleted_linked_or_liboverride_id = true;
296 }
297 };
298
299 /* Main idea of batch deletion is to remove all IDs to be deleted from Main database.
300 * This means that we won't have to loop over all deleted IDs to remove usages
301 * of other deleted IDs.
302 * This gives tremendous speed-up when deleting a large amount of IDs from a Main
303 * containing thousands of these.
304 * This also means that we have to be very careful here, as we bypass many 'common'
305 * processing, hence risking to 'corrupt' at least user counts, if not IDs themselves. */
306 bool keep_looping = true;
307 while (keep_looping) {
308 keep_looping = false;
309
310 /* First tag and remove from Main all datablocks directly from target lib.
311 * Note that we go forward here, since we want to check dependencies before users
312 * (e.g. meshes before objects). Reduces the chances to have to loop many times in the
313 * `while (keep_looking)` outer loop. */
314 for (int i = 0; i < base_count; i++) {
315 ListBaseT<ID> *lb = lbarray[i];
316 ID *id_iter;
317
318 FOREACH_MAIN_LISTBASE_ID_BEGIN (lb, id_iter) {
319 /* NOTE: in case we delete a library, we also delete all its datablocks! */
320 if (ids_to_delete.contains(id_iter) ||
321 (ID_IS_LINKED(id_iter) && ids_to_delete.contains(&id_iter->lib->id)))
322 {
323 process_id_to_delete(lb, id_iter);
324
325 /* Forcefully also delete shapekeys of the deleted ID if any, 'orphaned' shapekeys are
326 * not allowed in Blender and will cause lots of problem in modern code (liboverrides,
327 * warning on write & read, etc.). */
328 Key *shape_key = BKE_key_from_id(id_iter);
329 if (shape_key && !ids_to_delete.contains(&shape_key->id)) {
330 process_id_to_delete(&bmain->shapekeys.cast<ID>(), &shape_key->id);
331 }
332
333 keep_looping = true;
334 }
335 }
337 }
338
339 /* Will tag 'never nullptr' users of this ID too.
340 *
341 * NOTE: #BKE_libblock_unlink() cannot be used here, since it would ignore indirect
342 * links, this can lead to nasty crashing here in second, actual deleting loop.
343 * Also, this will also flag users of deleted data that cannot be unlinked
344 * (object using deleted obdata, etc.), so that they also get deleted. */
345 BKE_libblock_remap_multiple_locked(bmain, id_remapper, remapping_flags);
346 for (ID *id_never_null_iter : id_remapper.never_null_users()) {
347 ids_to_delete.add(id_never_null_iter);
348 keep_looping = true;
349 }
350 id_remapper.clear();
351 }
352
353 /* Remapping above may have left some Library::runtime::archived_libraries items to nullptr,
354 * clean this up and shrink the vector accordingly. */
356
357 /* Since we removed IDs from Main, their own other IDs usages need to be removed 'manually'. */
358 Vector<ID *> cleanup_ids{ids_to_delete.begin(), ids_to_delete.end()};
360 bmain,
361 cleanup_ids,
363 id_remapper,
365 cleanup_ids.clear();
366
367 /* Now we can safely mark that ID as not being in Main database anymore. */
368 /* NOTE: This needs to be done in a separate loop than above, otherwise some user-counts of
369 * deleted IDs may not be properly decreased by the remappings (since `NO_MAIN` ID user-counts
370 * is never affected). */
371 for (ID *id : ids_to_delete) {
372 id->tag |= ID_TAG_NO_MAIN;
373 /* User-count needs to be reset artificially, since some usages may not be cleared in batch
374 * deletion (typically, if one deleted ID uses another deleted ID, this may not be cleared by
375 * remapping code, depending on order in which these are handled). */
376 id->us = ID_FAKE_USERS(id);
377
378 if (!has_deleted_library && id->id_type() == ID_LI) {
379 has_deleted_library = true;
380 }
381
382 id_free(bmain, id, free_flag, false);
383 }
384
385 BKE_main_unlock(bmain);
388 if (!options.prevent_invariants_update) {
390 }
391
392 if (has_deleted_library) {
394 }
395
396 /* Deleting a liboverride or linked ID may have had some impact on validity of liboverrides
397 * hierarchy roots, these need to be re-validated/re-generated. */
398 if (has_deleted_linked_or_liboverride_id && !options.prevent_liboverride_hierarchy_root_ensure) {
400 }
401
402 bmain->is_memfile_undo_written = false;
403 return size_t(ids_to_delete.size());
404}
405
406void BKE_id_delete(Main *bmain, void *idv, const BKEIDDeleteOptions &options)
407{
408 ID *id = static_cast<ID *>(idv);
409 BLI_assert_msg((id->tag & ID_TAG_NO_MAIN) == 0, "Cannot be used with IDs outside of Main");
410
411 Set<ID *> ids_to_delete = {id};
412 id_delete(bmain, ids_to_delete, options);
413}
414
416{
417 Set<ID *> ids_to_delete;
418 ID *id_iter;
419 FOREACH_MAIN_ID_BEGIN (bmain, id_iter) {
420 if (id_iter->tag & ID_TAG_DOIT) {
421 ids_to_delete.add(id_iter);
422 }
423 }
425 return id_delete(bmain, ids_to_delete, options);
426}
427
429 Set<ID *> &ids_to_delete,
431{
432 return id_delete(bmain, ids_to_delete, options);
433}
434
435/* -------------------------------------------------------------------- */
438
440{
441#ifdef WITH_PYTHON
442# ifdef WITH_PYTHON_SAFETY
443 BPY_id_release(id);
444# endif
445 if (id->py_instance) {
446 BPY_DECREF_RNA_INVALIDATE(id->py_instance);
447 }
448#else
449 UNUSED_VARS(id);
450#endif
451}
452
454
455} // namespace blender
#define FOREACH_MAIN_ID_END
Definition BKE_main.hh:735
#define FOREACH_MAIN_LISTBASE_ID_END
Definition BKE_main.hh:703
#define FOREACH_MAIN_LISTBASE_ID_BEGIN(_lb, _id)
Definition BKE_main.hh:697
#define FOREACH_MAIN_ID_BEGIN(_bmain, _id)
Definition BKE_main.hh:729
external readfile function prototypes.
ID and Library types, which are fundamental for SDNA.
#define ID_FAKE_USERS(id)
Definition DNA_ID.h:658
#define ID_IS_LINKED(_id)
Definition DNA_ID.h:697
#define ID_IS_OVERRIDE_LIBRARY(_id)
Definition DNA_ID.h:736
Read Blender memory allocation and freeing.
#define UNUSED_VARS(...)
#define ELEM(...)
#define BLI_assert_msg(a, msg)
Iterator begin() const
Definition BLI_set.hh:499
int64_t size() const
Definition BLI_set.hh:606
Iterator end() const
Definition BLI_set.hh:509
bool contains(const Key &key) const
Definition BLI_set.hh:310
bool add(const Key &key)
Definition BLI_set.hh:248
void add(ID *old_id, ID *new_id)
const Set< ID * > & never_null_users() const
CCL_NAMESPACE_BEGIN struct Options options
void main_cleanup_parent_archives(Main &bmain)
Definition library.cc:833
BMesh Iterators.
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
void BPY_DECREF_RNA_INVALIDATE(void *pyob_ptr)
BKE_library_remap_editor_id_reference_cb remap_editor_id_reference_cb
Definition lib_remap.cc:53
void IDP_FreeProperty_ex(IDProperty *prop, bool do_id_user)
Definition idprop.cc:1398
void BKE_id_delete(Main *bmain, void *idv, const BKEIDDeleteOptions &options={}) ATTR_NONNULL()
void BKE_libblock_relink_ex(Main *bmain, void *idv, void *old_idv, void *new_idv, int remap_flags) ATTR_NONNULL(2)
Definition lib_remap.cc:855
@ ID_REMAP_TYPE_CLEANUP
void BKE_id_free_us(Main *bmain, void *idv) ATTR_NONNULL()
void BPY_id_release(ID *id)
Definition bpy_rna.cc:453
void BKE_asset_metadata_free(AssetMetaData **asset_data)
Definition asset.cc:36
void BKE_id_free_ex(Main *bmain, void *idv, int flag_orig, bool use_flag_from_idtag)
@ ID_REMAP_FORCE_INTERNAL_RUNTIME_POINTERS
@ ID_REMAP_FORCE_NEVER_NULL_USAGE
@ ID_REMAP_SKIP_USER_CLEAR
@ ID_REMAP_STORE_NEVER_NULL_USAGE
MainListsArray BKE_main_lists_get(Main &bmain)
Definition main.cc:1158
@ LIB_ID_FREE_NO_USER_REFCOUNT
@ LIB_ID_FREE_NO_NAMEMAP_REMOVE
@ LIB_ID_FREE_NO_DEG_TAG
@ LIB_ID_FREE_NO_MAIN
@ LIB_ID_FREE_NO_UI_USER
@ LIB_ID_FREE_NOT_ALLOCATED
void id_us_clear_real(ID *id)
Definition lib_id.cc:406
void BKE_main_namemap_remove_id(Main &bmain, ID &id)
void BKE_layer_collection_resync_forbid(Main &bmain)
void BKE_libblock_free_data(ID *id, bool do_id_user) ATTR_NONNULL()
bool BKE_main_collection_sync_remap(const Main *bmain)
void BKE_library_main_rebuild_hierarchy(Main *bmain)
Definition library.cc:375
Key * BKE_key_from_id(ID *id)
Definition key.cc:1291
const IDTypeInfo * BKE_idtype_get_info_from_id(const ID *id)
Definition idtype.cc:232
void BKE_layer_collection_resync_allow(Main &bmain)
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
void BKE_lib_override_library_main_hierarchy_root_ensure(Main *bmain, LibOverride_HierarchyRoot_ValidateOptions options={}, ReportList *reports=nullptr)
void BKE_animdata_free(ID *id, bool do_id_user)
Definition anim_data.cc:174
void DEG_id_type_tag(Main *bmain, short id_type)
void BKE_main_ensure_invariants(Main &bmain, std::optional< Span< ID * > > modified_ids=std::nullopt)
size_t BKE_id_multi_tagged_delete(Main *bmain, const BKEIDDeleteOptions &options={}) ATTR_NONNULL()
static int id_free(Main *bmain, void *idv, int flag, const bool use_flag_from_idtag)
void BKE_id_free(Main *bmain, void *idv)
void BKE_libblock_free_runtime_data(ID *id)
void BKE_lib_override_library_free(IDOverrideLibrary **liboverride, bool do_id_user)
void BLO_readfile_id_runtime_data_free(ID &id)
Definition readfile.cc:2313
void BKE_main_unlock(Main *bmain)
Definition main.cc:666
@ ID_TAG_NO_MAIN
Definition DNA_ID.h:981
@ ID_TAG_NO_USER_REFCOUNT
Definition DNA_ID.h:1011
@ ID_TAG_NOT_ALLOCATED
Definition DNA_ID.h:1018
@ ID_TAG_DOIT
Definition DNA_ID.h:1029
void BKE_libblock_remap_multiple_locked(Main *bmain, bke::id::IDRemapper &mappings, const int remap_flags)
Definition lib_remap.cc:654
void BKE_main_lock(Main *bmain)
Definition main.cc:661
void BKE_libblock_free_datablock(ID *id, int flag) ATTR_NONNULL()
void void void BKE_libblock_unlink(Main *bmain, void *idv, bool do_skip_indirect) ATTR_NONNULL()
Definition lib_remap.cc:719
size_t BKE_id_multi_delete(Main *bmain, Set< ID * > &ids_to_delete, const BKEIDDeleteOptions &options={})
BKE_library_free_notifier_reference_cb free_notifier_reference_cb
Definition lib_remap.cc:46
void BKE_libblock_relink_multiple(Main *bmain, const Span< ID * > ids, eIDRemapType remap_type, bke::id::IDRemapper &id_remapper, int remap_flags)
Definition lib_remap.cc:793
void id_us_min(ID *id)
Definition lib_id.cc:441
static size_t id_delete(Main *bmain, Set< ID * > &ids_to_delete, const BKEIDDeleteOptions &options)
ListBaseT< ID > * which_libbase(Main *bmain, short type)
Definition main.cc:1073
void BKE_libblock_free_data_py(ID *id)
IDTypeFreeDataFunction free_data
struct Library * lib
Definition DNA_ID.h:421
ListBaseT< Key > shapekeys
Definition BKE_main.hh:398
bool is_locked_for_linking
Definition BKE_main.hh:313
bool is_memfile_undo_written
Definition BKE_main.hh:297