Blender V5.3
lib_id.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
11
12#include <cctype>
13#include <cstddef>
14#include <cstdio>
15#include <cstdlib>
16#include <cstring>
17#include <optional>
18
19#include "CLG_log.h"
20
21#include "DNA_defs.h"
22#include "MEM_guardedalloc.h"
23
24/* all types are needed here, in order to do memory operations */
25#include "DNA_ID.h"
26#include "DNA_anim_types.h"
28#include "DNA_key_types.h"
29#include "DNA_node_types.h"
30#include "DNA_workspace_types.h"
31
32#include "BLI_listbase.hh"
33#include "BLI_utildefines.hh"
34
35#include "BLI_ghash.hh"
36#include "BLI_linklist.hh"
37#include "BLI_memarena.hh"
38#include "BLI_path_utils.hh"
39#include "BLI_string.hh"
40#include "BLI_string_ref.hh"
41#include "BLI_string_utf8.hh"
42#include "BLI_string_utils.hh"
43
44#include "BLT_translation.hh"
45
46#include "BKE_anim_data.hh"
47#include "BKE_armature.hh"
48#include "BKE_asset.hh"
49#include "BKE_bpath.hh"
50#include "BKE_context.hh"
51#include "BKE_global.hh"
52#include "BKE_idprop.hh"
53#include "BKE_idtype.hh"
54#include "BKE_key.hh"
55#include "BKE_lib_id.hh"
56#include "BKE_lib_override.hh"
57#include "BKE_lib_query.hh"
58#include "BKE_lib_remap.hh"
59#include "BKE_library.hh"
60#include "BKE_main.hh"
61#include "BKE_main_namemap.hh"
62#include "BKE_node.hh"
63#include "BKE_rigidbody.h"
64
65#include "DEG_depsgraph.hh"
68
69#include "RNA_access.hh"
70
71#include "BLO_read_write.hh"
72
73#include "atomic_ops.h"
74
75#include "lib_intern.hh"
76
77// #define DEBUG_TIME
78
79#ifdef DEBUG_TIME
81#endif
82
83namespace blender {
84
85using namespace bke::id;
86
87static CLG_LogRef LOG = {"lib.id"};
88
90 .id_code = ID_LINK_PLACEHOLDER,
91 .id_filter = 0,
92 .dependencies_id_types = 0,
93 .main_listbase_index = INDEX_ID_NULL,
94 .struct_size = sizeof(ID),
95 .name = "LinkPlaceholder",
96 .name_plural = N_("link_placeholders"),
97 .translation_context = BLT_I18NCONTEXT_ID_ID,
99 .asset_type_info = nullptr,
100
101 .init_data = nullptr,
102 .copy_data = nullptr,
103 .free_data = nullptr,
104 .make_local = nullptr,
105 .foreach_id = nullptr,
106 .foreach_cache = nullptr,
107 .foreach_path = nullptr,
108 .foreach_working_space_color = nullptr,
109 .foreach_asset_weak_reference = nullptr,
110 .owner_pointer_get = nullptr,
111
112 .blend_write = nullptr,
113 .blend_read_data = nullptr,
114 .blend_read_after_liblink = nullptr,
115
116 .blend_read_undo_preserve = nullptr,
117
118 .lib_override_apply_post = nullptr,
119};
120
121/* GS reads the memory pointed at in a specific ordering.
122 * only use this definition, makes little and big endian systems
123 * work fine, in conjunction with MAKE_ID */
124
125/* ************* general ************************ */
126
132 char *path_dst,
133 size_t path_dst_maxncpy,
134 const char *path_src)
135{
136 const char **data = static_cast<const char **>(bpath_data->user_data);
137 /* be sure there is low chance of the path being too short */
138 char filepath[(FILE_MAXDIR * 2) + FILE_MAXFILE];
139 const char *base_new = data[0];
140 const char *base_old = data[1];
141
142 if (BLI_path_is_rel(base_old)) {
143 CLOG_ERROR(&LOG, "old base path '%s' is not absolute.", base_old);
144 return false;
145 }
146
147 /* Make referenced file absolute. This would be a side-effect of
148 * BLI_path_normalize, but we do it explicitly so we know if it changed. */
149 BLI_strncpy(filepath, path_src, FILE_MAX);
150 if (BLI_path_abs(filepath, base_old)) {
151 /* Path was relative and is now absolute. Remap.
152 * Important BLI_path_normalize runs before the path is made relative
153 * because it won't work for paths that start with `//../` */
154 BLI_path_normalize(filepath);
155 BLI_path_rel(filepath, base_new);
156 BLI_strncpy(path_dst, filepath, path_dst_maxncpy);
157 return true;
158 }
159
160 /* Path was not relative to begin with. */
161 return false;
162}
163
181static void lib_id_library_local_paths(Main *bmain, Library *lib_to, Library *lib_from, ID *id)
182{
183 BLI_assert(lib_to || lib_from);
184 const char *bpath_user_data[2] = {
185 lib_to ? lib_to->runtime->filepath_abs : BKE_main_blendfile_path(bmain),
186 lib_from ? lib_from->runtime->filepath_abs : BKE_main_blendfile_path(bmain)};
187
188 BPathForeachPathData path_data{};
189 path_data.bmain = bmain;
192 path_data.user_data = static_cast<void *>(bpath_user_data);
193 BKE_bpath_foreach_path_id(&path_data, id);
194}
195
197{
198 ID *id = static_cast<ID *>(cb_data->user_data);
199 if (*cb_data->id_pointer == id) {
200 /* Even though the ID itself remain the same after being made local, from depsgraph point of
201 * view this is a different ID. Hence we need to tag all of its users for a copy-on-eval
202 * update. */
206 }
207 return IDWALK_RET_NOP;
208}
209
210void BKE_lib_id_clear_library_data(Main *bmain, ID *id, const int flags)
211{
212 const bool id_in_mainlist = (id->tag & ID_TAG_NO_MAIN) == 0 &&
213 (id->flag & ID_FLAG_EMBEDDED_DATA) == 0;
214
215 if (id_in_mainlist) {
216 BKE_main_namemap_remove_id(*bmain, *id);
217 }
218
219 lib_id_library_local_paths(bmain, nullptr, id->lib, id);
220
222
223 MEM_SAFE_DELETE(id->library_weak_reference);
224
225 id->lib = nullptr;
226 id->tag &= ~(ID_TAG_INDIRECT | ID_TAG_EXTERN);
228 if (id_in_mainlist) {
230 *which_libbase(bmain, id->id_type()),
231 *id,
232 nullptr,
234 false);
235 if (!ELEM(result.action,
238 {
239 bmain->is_memfile_undo_written = false;
240 }
241 }
242
243 /* Conceptually, an ID made local is not the same as the linked one anymore. Reflect that by
244 * regenerating its session UID. */
245 if ((id->tag & ID_TAG_TEMP_MAIN) == 0) {
247 }
248
249 if (ID_IS_ASSET(id)) {
250 if ((flags & LIB_ID_MAKELOCAL_ASSET_DATA_CLEAR) != 0) {
251 const IDTypeInfo *idtype_info = BKE_idtype_get_info_from_id(id);
252 if (idtype_info && idtype_info->asset_type_info &&
254 {
255 idtype_info->asset_type_info->on_clear_asset_fn(id, id->asset_data);
256 }
257 BKE_asset_metadata_free(&id->asset_data);
258 }
259 else {
260 /* Assets should always have a fake user. Ensure this is the case after "Make Local". */
262 }
263 }
264
265 /* Ensure that the deep-hash is reset when making an ID local (in case it was previously a packed
266 * linked ID), as this is by definition not a valid deep-hash anymore (that ID is now a fully
267 * independent copy living in another blend-file). */
268 id->deep_hash = {};
269
270 /* We need to tag this IDs and all of its users, conceptually new local ID and original linked
271 * ones are two completely different data-blocks that were virtually remapped, even though in
272 * reality they remain the same data. For undo this info is critical now. */
274 ID *id_iter;
275 FOREACH_MAIN_ID_BEGIN (bmain, id_iter) {
278 }
280
281 /* Internal shape key blocks inside data-blocks also stores id->lib,
282 * make sure this stays in sync (note that we do not need any explicit handling for real EMBEDDED
283 * IDs here, this is down automatically in `lib_id_expand_local_cb()`). */
284 Key *key = BKE_key_from_id(id);
285 if (key != nullptr) {
286 BKE_lib_id_clear_library_data(bmain, &key->id, flags);
287 }
288
289 /* Even though the ID itself remain the same after being made local, from depsgraph point of view
290 * this is a different ID. Hence we rebuild depsgraph relationships. */
292}
293
294/* TODO:
295 * - Make this public API?
296 * - Add more status to keep in sync?
297 * - ID_FLAG_LINKED_AND_PACKED, ID_FLAG_FAKEUSER, ID_FLAG_CLIPBOARD_MARK ?
298 * - ID_TAG_RUNTIME, ID_TAG_MISSING, ID_TAG_NEW, ID_TAG_PRE_EXISTING, ID_TAG_TEMP_MAIN,
299 * ID_TAG_NO_MAIN, ID_TAG_NO_USER_REFCOUNT ?
300 */
302{
303 auto propagate_status_fn = [&id](ID &embedded_id) -> void {
304 constexpr eID_Flag FLAGS_TO_SYNC = ID_FLAG_INDIRECT_WEAK_LINK;
305 constexpr eID_Tag TAGS_TO_SYNC = (ID_TAG_EXTERN | ID_TAG_INDIRECT);
306
307 BLI_assert(embedded_id.flag & ID_FLAG_EMBEDDED_DATA);
308 BLI_assert(embedded_id.lib == id.lib);
309
310 embedded_id.flag &= ~FLAGS_TO_SYNC;
311 embedded_id.flag |= (id.flag & FLAGS_TO_SYNC);
312 embedded_id.tag &= ~TAGS_TO_SYNC;
313 embedded_id.tag |= (id.tag & TAGS_TO_SYNC);
314 };
315
316 bNodeTree *ntree = bke::node_tree_from_id(&id);
317 if (ntree) {
318 propagate_status_fn(ntree->id);
319 }
320 if (id.id_type() == ID_SCE) {
321 Collection *master_collection = id_cast<Scene &>(id).master_collection;
322 if (master_collection) {
323 propagate_status_fn(master_collection->id);
324 }
325 }
326}
327
328void id_lib_extern(ID *id, const bool enforce_fix)
329{
330 if (id && ID_IS_LINKED(id)) {
331#ifndef NDEBUG
332 if (!enforce_fix) {
333 const eID_Tag linked_tags = eID_Tag(id->tag & (ID_TAG_EXTERN | ID_TAG_INDIRECT));
335 linked_tags != (ID_TAG_EXTERN | ID_TAG_INDIRECT),
336 "A linked ID must never be both directly and indirectly linked at the same time.");
337 BLI_assert_msg(linked_tags != 0,
338 "A linked ID must always be either directly or indirectly linked.");
339 }
340#endif
341
342 /* Note: non-linkable IDs can be directly linked in case they are linked packed. */
344 if ((id->tag & ID_TAG_INDIRECT) != 0 || enforce_fix) {
345 id->tag &= ~ID_TAG_INDIRECT;
346 id->flag &= ~ID_FLAG_INDIRECT_WEAK_LINK;
347 id->tag |= ID_TAG_EXTERN;
348 id->lib->runtime->parent = nullptr;
350 }
351 }
352}
353
354void id_lib_indirect(ID *id, const bool enforce_fix)
355{
356 if (id && ID_IS_LINKED(id)) {
357#ifndef NDEBUG
358 if (!enforce_fix) {
359 const eID_Tag linked_tags = eID_Tag(id->tag & (ID_TAG_EXTERN | ID_TAG_INDIRECT));
361 linked_tags != (ID_TAG_EXTERN | ID_TAG_INDIRECT),
362 "A linked ID must never be both directly and indirectly linked at the same time.");
363 BLI_assert_msg(linked_tags != 0,
364 "A linked ID must always be either directly or indirectly linked.");
365 }
366#endif
367
368 if ((id->tag & ID_TAG_EXTERN) != 0 || enforce_fix) {
369 id->tag &= ~ID_TAG_EXTERN;
370 id->tag |= ID_TAG_INDIRECT;
371 /* Do not clear library parent info here, it might have already been set to correct value. */
373 }
374 }
375}
376
378{
379 if (id && ID_IS_LINKED(id)) {
381 if (id->tag & ID_TAG_INDIRECT) {
382 id->flag |= ID_FLAG_INDIRECT_WEAK_LINK;
384 }
385 }
386}
387
389{
390 if (id) {
391 const int limit = ID_FAKE_USERS(id);
392 id->tag |= ID_TAG_EXTRAUSER;
393 if (id->us <= limit) {
394 if (id->us < limit || ((id->us == limit) && (id->tag & ID_TAG_EXTRAUSER_SET))) {
396 "ID user count error: %s (from '%s')",
397 id->name,
398 id->lib ? id->lib->runtime->filepath_abs : "[Main]");
399 }
400 id->us = limit + 1;
401 id->tag |= ID_TAG_EXTRAUSER_SET;
402 }
403 }
404}
405
407{
408 if (id && (id->tag & ID_TAG_EXTRAUSER)) {
409 if (id->tag & ID_TAG_EXTRAUSER_SET) {
410 id->us--;
411 BLI_assert(id->us >= ID_FAKE_USERS(id));
412 }
414 }
415}
416
418{
419 if (id) {
420 if ((id->tag & ID_TAG_EXTRAUSER) && (id->tag & ID_TAG_EXTRAUSER_SET)) {
421 BLI_assert(id->us >= 1);
422 /* No need to increase count, just tag extra user as no more set.
423 * Avoids annoying & inconsistent +1 in user count. */
424 id->tag &= ~ID_TAG_EXTRAUSER_SET;
425 }
426 else {
427 BLI_assert(id->us >= 0);
428 id->us++;
429 }
430 }
431}
432
433void id_us_plus(ID *id)
434{
435 if (id) {
437 id_lib_extern(id);
438 }
439}
440
441void id_us_min(ID *id)
442{
443 if (id) {
444 const int limit = ID_FAKE_USERS(id);
445
446 if (id->us <= limit) {
447 if (!ID_TYPE_IS_DEPRECATED(id->id_type())) {
448 /* Do not assert on deprecated ID types, we cannot really ensure that their ID
449 * reference-counting is valid. */
451 "ID user decrement error: %s (from '%s'): %d <= %d",
452 id->name,
453 id->lib ? id->lib->runtime->filepath_abs : "[Main]",
454 id->us,
455 limit);
456 }
457 id->us = limit;
458 }
459 else {
460 id->us--;
461 }
462
463 if ((id->us == limit) && (id->tag & ID_TAG_EXTRAUSER)) {
464 /* We need an extra user here, but never actually incremented user count for it so far,
465 * do it now. */
467 }
468 }
469}
470
472{
473 if (id && !(id->flag & ID_FLAG_FAKEUSER)) {
474 id->flag |= ID_FLAG_FAKEUSER;
475 id_us_plus(id);
476 }
477}
478
480{
481 if (id && (id->flag & ID_FLAG_FAKEUSER)) {
482 id->flag &= ~ID_FLAG_FAKEUSER;
483 id_us_min(id);
484 }
485}
486
488{
489 /* We assume that if this ID has no new ID, its embedded data has not either. */
490 if (id->newid == nullptr) {
491 return;
492 }
493
494 id->newid->tag &= ~ID_TAG_NEW;
495 id->newid = nullptr;
496
497 /* Deal with embedded data too. */
498 /* NOTE: even though ShapeKeys are not technically embedded data currently, they behave as such
499 * in most cases, so for sake of consistency treat them as such here. Also mirrors the behavior
500 * in `BKE_lib_id_make_local`. */
501 Key *key = BKE_key_from_id(id);
502 if (key != nullptr) {
504 }
506 if (ntree != nullptr) {
508 }
509 if (id->id_type() == ID_SCE) {
510 Collection *master_collection = (id_cast<Scene *>(id))->master_collection;
511 if (master_collection != nullptr) {
512 BKE_id_newptr_and_tag_clear(&master_collection->id);
513 }
514 }
515}
516
518{
519 Main *bmain = cb_data->bmain;
520 ID *self_id = cb_data->self_id;
521 ID **id_pointer = cb_data->id_pointer;
522 int const cb_flag = cb_data->cb_flag;
523 const int flags = POINTER_AS_INT(cb_data->user_data);
524
525 if (cb_flag & IDWALK_CB_LOOPBACK) {
526 /* We should never have anything to do with loop-back pointers here. */
527 return IDWALK_RET_NOP;
528 }
529
531 /* Embedded data-blocks need to be made fully local as well.
532 * Note however that in some cases (when owner ID had to be duplicated instead of being made
533 * local directly), its embedded IDs should also have already been duplicated, and hence be
534 * fully local here already. */
535 if (*id_pointer != nullptr && ID_IS_LINKED(*id_pointer)) {
536 BLI_assert(*id_pointer != self_id);
537
538 BKE_lib_id_clear_library_data(bmain, *id_pointer, flags);
539 }
540 return IDWALK_RET_NOP;
541 }
542
543 /* Can happen that we get un-linkable ID here, e.g. with shape-key referring to itself
544 * (through drivers)...
545 * Just skip it, shape key can only be either indirectly linked, or fully local, period.
546 * And let's curse one more time that stupid useless shape-key ID type! */
547 if (*id_pointer && *id_pointer != self_id &&
548 BKE_idtype_idcode_is_linkable((*id_pointer)->id_type()))
549 {
550 id_lib_extern(*id_pointer);
551 }
552
553 return IDWALK_RET_NOP;
554}
555
556void BKE_lib_id_expand_local(Main *bmain, ID *id, const int flags)
557{
560}
561
562void lib_id_copy_ensure_local(Main *bmain, const ID *old_id, ID *new_id, const int flags)
563{
564 if (ID_IS_LINKED(old_id)) {
565 /* For packed linked data copied into local IDs in Main, assume that they are no more related
566 * to their original library source, and clear their deep-hash.
567 *
568 * NOTE: In case more control is needed over that behavior in the future, a new flag can be
569 * added instead. */
570 new_id->deep_hash = {};
571
572 BKE_lib_id_expand_local(bmain, new_id, flags);
573 lib_id_library_local_paths(bmain, nullptr, old_id->lib, new_id);
574 }
575}
576
578 Main *bmain, ID *id, int flags, bool *r_force_local, bool *r_force_copy)
579{
580 bool force_local = (flags & LIB_ID_MAKELOCAL_FORCE_LOCAL) != 0;
581 bool force_copy = (flags & LIB_ID_MAKELOCAL_FORCE_COPY) != 0;
582 BLI_assert(force_copy == false || force_copy != force_local);
583
584 if (force_local || force_copy) {
585 /* Already set by caller code, nothing to do here. */
586 *r_force_local = force_local;
587 *r_force_copy = force_copy;
588 return;
589 }
590
591 const bool lib_local = (flags & LIB_ID_MAKELOCAL_FULL_LIBRARY) != 0;
592 bool is_local = false, is_lib = false;
593
594 /* - no user (neither lib nor local): make local (happens e.g. with UI-used only data).
595 * - only lib users: do nothing (unless force_local is set)
596 * - only local users: make local
597 * - mixed: make copy
598 * In case we make a whole lib's content local,
599 * we always want to localize, and we skip remapping (done later).
600 */
601
602 BKE_library_ID_test_usages(bmain, id, &is_local, &is_lib);
603 if (!lib_local && !is_local && !is_lib) {
604 force_local = true;
605 }
606 else if (lib_local || is_local) {
607 if (!is_lib) {
608 force_local = true;
609 }
610 else {
611 force_copy = true;
612 }
613 }
614
615 *r_force_local = force_local;
616 *r_force_copy = force_copy;
617}
618
619void BKE_lib_id_make_local_generic(Main *bmain, ID *id, const int flags)
620{
621 if (!ID_IS_LINKED(id)) {
622 return;
623 }
624
625 bool force_local, force_copy;
626 BKE_lib_id_make_local_generic_action_define(bmain, id, flags, &force_local, &force_copy);
627
628 if (force_local) {
629 BKE_lib_id_clear_library_data(bmain, id, flags);
630 if ((flags & LIB_ID_MAKELOCAL_LIBOVERRIDE_CLEAR) != 0) {
632 }
633 BKE_lib_id_expand_local(bmain, id, flags);
634 }
635 else if (force_copy) {
636 const int copy_flags =
639 ID *id_new = BKE_id_copy_ex(bmain, id, nullptr, copy_flags);
640
641 /* Should not fail in expected use cases,
642 * but a few ID types cannot be copied (LIB, WM, SCR...). */
643 if (id_new != nullptr) {
644 id_new->us = 0;
645
646 /* setting newid is mandatory for complex make_lib_local logic... */
647 ID_NEW_SET(id, id_new);
648 Key *key = BKE_key_from_id(id), *key_new = BKE_key_from_id(id);
649 if (key && key_new) {
650 ID_NEW_SET(key, key_new);
651 }
652 bNodeTree *ntree = bke::node_tree_from_id(id), *ntree_new = bke::node_tree_from_id(id_new);
653 if (ntree && ntree_new) {
654 ID_NEW_SET(ntree, ntree_new);
655 }
656 if (id->id_type() == ID_SCE) {
657 Collection *master_collection = (id_cast<Scene *>(id))->master_collection,
658 *master_collection_new = (id_cast<Scene *>(id_new))->master_collection;
659 if (master_collection && master_collection_new) {
660 ID_NEW_SET(master_collection, master_collection_new);
661 }
662 }
663
664 const bool lib_local = (flags & LIB_ID_MAKELOCAL_FULL_LIBRARY) != 0;
665 if (!lib_local) {
667 }
668 }
669 }
670}
671
672bool BKE_lib_id_make_local(Main *bmain, ID *id, const int flags)
673{
674 const bool lib_local = (flags & LIB_ID_MAKELOCAL_FULL_LIBRARY) != 0;
675
676 /* Skip indirectly linked IDs, unless the whole library is made local, or handling them is
677 * explicitly requested. */
678 if (!(lib_local || (flags & LIB_ID_MAKELOCAL_INDIRECT) != 0) && (id->tag & ID_TAG_INDIRECT)) {
679 return false;
680 }
681
682 const IDTypeInfo *idtype_info = BKE_idtype_get_info_from_id(id);
683
684 if (idtype_info == nullptr) {
685 BLI_assert_msg(0, "IDType Missing IDTypeInfo");
686 return false;
687 }
688
689 BLI_assert((idtype_info->flags & IDTYPE_FLAGS_NO_LIBLINKING) == 0);
690
691 if (idtype_info->make_local != nullptr) {
692 idtype_info->make_local(bmain, id, flags);
693 }
694 else {
695 BKE_lib_id_make_local_generic(bmain, id, flags);
696 }
697
698 return true;
699}
700
706
709{
710 ID **id_pointer = cb_data->id_pointer;
711 ID *id = *id_pointer;
712 const LibraryForeachIDCallbackFlag cb_flag = cb_data->cb_flag;
714
715 /* Remap self-references to new copied ID. */
716 if (id == data->id_src) {
717 /* We cannot use self_id here, it is not *always* id_dst (thanks to confounded node-trees!). */
718 id = *id_pointer = data->id_dst;
719 }
720
721 /* Increase used IDs refcount if needed and required. */
722 if ((data->flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0 && (cb_flag & IDWALK_CB_USER)) {
723 if ((data->flag & LIB_ID_CREATE_NO_MAIN) != 0) {
726 }
727 else if (ID_IS_LINKED(cb_data->owner_id)) {
728 /* Do not mark copied ID as directly linked, if its current user is also linked data (which
729 * is now fairly common when using 'copy_in_lib' feature). */
731 }
732 else {
733 id_us_plus(id);
734 }
735 }
736
737 return IDWALK_RET_NOP;
738}
739
741{
742#define LIB_ID_TYPES_NOCOPY ID_LI, ID_SCR, ID_WM, ID_WS /* Not supported */
743
744 return !ID_TYPE_IS_DEPRECATED(id->id_type()) && !ELEM(id->id_type(), LIB_ID_TYPES_NOCOPY);
745
746#undef LIB_ID_TYPES_NOCOPY
747}
748
750 std::optional<Library *> owner_library,
751 const ID *id,
752 std::optional<const ID *> new_owner_id,
753 ID **new_id_p,
754 const int flag)
755{
756 ID *newid = (new_id_p != nullptr) ? *new_id_p : nullptr;
757 BLI_assert_msg(newid || (flag & LIB_ID_CREATE_NO_ALLOCATE) == 0,
758 "Copying with 'no allocate' behavior should always get a non-null new ID buffer");
759
760 /* Make sure destination pointer is all good. */
761 if ((flag & LIB_ID_CREATE_NO_ALLOCATE) == 0) {
762 newid = nullptr;
763 }
764 else {
765 if (!newid) {
766 /* Invalid case, already caught by the assert above. */
767 return nullptr;
768 }
769 }
770
771 /* Early output if source is nullptr. */
772 if (id == nullptr) {
773 return nullptr;
774 }
775
776 const IDTypeInfo *idtype_info = BKE_idtype_get_info_from_id(id);
777
778 if (idtype_info != nullptr) {
779 if ((idtype_info->flags & IDTYPE_FLAGS_NO_COPY) != 0) {
780 return nullptr;
781 }
782
783 BKE_libblock_copy_in_lib(bmain, owner_library, id, new_owner_id, &newid, flag);
784
785 if (idtype_info->copy_data != nullptr) {
786 idtype_info->copy_data(bmain, owner_library, newid, id, flag);
787 }
788 }
789 else {
790 BLI_assert_msg(0, "IDType Missing IDTypeInfo");
791 }
792
793 BLI_assert_msg(newid, "Could not get an allocated new ID to copy into");
794 if (!newid) {
795 return nullptr;
796 }
797
798 /* Update ID refcount, remap pointers to self in new ID. */
800 data.id_src = id;
801 data.id_dst = newid;
802 data.flag = flag;
803 /* When copying an embedded ID, typically at this point its owner ID pointer will still point to
804 * the owner of the source, this code has no access to its valid (i.e. destination) owner. This
805 * can be added at some point if needed, but currently the #id_copy_libmanagement_cb callback
806 * does need this information. */
809
810 /* FIXME: Check if this code can be moved in #BKE_libblock_copy_in_lib ? Would feel more fitted
811 * there, having library handling split between both functions does not look good. */
812 /* Do not make new copy local in case we are copying outside of main...
813 * XXX TODO: is this behavior OK, or should we need a separate flag to control that? */
814 if ((flag & LIB_ID_CREATE_NO_MAIN) == 0) {
815 BLI_assert(!owner_library || newid->lib == *owner_library);
816 /* If the ID was copied into a library, ensure paths are properly remapped, and that it has a
817 * 'linked' tag set. */
818 if (ID_IS_LINKED(newid)) {
819 if (newid->lib != id->lib) {
820 lib_id_library_local_paths(bmain, newid->lib, id->lib, newid);
821 }
822 if ((newid->tag & (ID_TAG_EXTERN | ID_TAG_INDIRECT)) == 0) {
823 newid->tag |= ID_TAG_EXTERN;
824 }
825 }
826 /* Expanding local linked ID usages should never be needed with embedded IDs - this will be
827 * handled together with their owner ID copying code. */
828 else if ((newid->flag & ID_FLAG_EMBEDDED_DATA) == 0) {
829 lib_id_copy_ensure_local(bmain, id, newid, 0);
830 }
831 }
832
833 else {
834 /* NOTE: Do not call `ensure_local` for IDs copied outside of Main, even if they do become
835 * local.
836 *
837 * Most of the time, this would not be the desired behavior currently.
838 *
839 * In the few cases where this is actually needed (e.g. from liboverride resync code, see
840 * #lib_override_library_create_from), calling code is responsible for this. */
841 newid->lib = owner_library ? *owner_library : id->lib;
842 }
843
844 if (new_id_p != nullptr) {
845 *new_id_p = newid;
846 }
847
848 return newid;
849}
850
851ID *BKE_id_copy_ex(Main *bmain, const ID *id, ID **new_id_p, const int flag)
852{
853 return BKE_id_copy_in_lib(bmain, std::nullopt, id, std::nullopt, new_id_p, flag);
854}
855
856ID *BKE_id_copy(Main *bmain, const ID *id)
857{
858 return BKE_id_copy_in_lib(bmain, std::nullopt, id, std::nullopt, nullptr, LIB_ID_COPY_DEFAULT);
859}
860
862 ID *id,
863 const eDupli_ID_Flags duplicate_flags,
864 const int copy_flags)
865{
866 if (id == nullptr) {
867 return id;
868 }
869 if (id->newid == nullptr) {
870 const bool do_linked_id = (duplicate_flags & USER_DUP_LINKED_ID) != 0;
871 if (!(do_linked_id || !ID_IS_LINKED(id))) {
872 return id;
873 }
874
875 ID *id_new = BKE_id_copy_ex(bmain, id, nullptr, copy_flags);
876 /* Copying add one user by default, need to get rid of that one. */
877 id_us_min(id_new);
878 ID_NEW_SET(id, id_new);
879
880 /* Shape keys are always copied with their owner ID, by default. */
881 ID *key_new = id_cast<ID *>(BKE_key_from_id(id_new));
882 ID *key = id_cast<ID *>(BKE_key_from_id(id));
883 if (key != nullptr) {
884 ID_NEW_SET(key, key_new);
885 }
886
887 /* NOTE: embedded data (root node-trees and master collections) should never be referenced by
888 * anything else, so we do not need to set their newid pointer and flag. */
889
890 BKE_animdata_duplicate_id_action(bmain, id_new, duplicate_flags);
891 if (key_new != nullptr) {
892 BKE_animdata_duplicate_id_action(bmain, key_new, duplicate_flags);
893 }
894 /* Note that actions of embedded data (root node-trees and master collections) are handled
895 * by #BKE_animdata_duplicate_id_action as well. */
896 }
897 return id->newid;
898}
899
901{
902 ID **id_p = cb_data->id_pointer;
903
904 if (*id_p) {
905 ID *id = *id_p;
906 *id_p = DEG_get_original(id);
907
908 /* If the ID changes increase the user count.
909 *
910 * This means that the reference to evaluated ID has been changed with a reference to the
911 * original ID which implies that the user count of the original ID is increased.
912 *
913 * The evaluated IDs do not maintain their user counter, so do not change it to avoid issues
914 * with the user counter going negative. */
915 if (*id_p != id) {
916 if ((cb_data->cb_flag & IDWALK_CB_USER) != 0) {
917 id_us_plus(*id_p);
918 }
919 }
920 }
921
922 return IDWALK_RET_NOP;
923}
924
926{
927 ID *newid = BKE_id_copy(bmain, id);
928
929 if (newid == nullptr) {
930 return newid;
931 }
932
933 /* Assign ID references directly used by the given ID to their original complementary parts.
934 *
935 * For example, when is called on an evaluated object will assign object->data to its original
936 * pointer, the evaluated object->data will be kept unchanged. */
938 nullptr, newid, foreach_assign_id_to_orig_callback, nullptr, IDWALK_NOP);
939
940 /* Shape keys reference on evaluated ID is preserved to keep driver paths available, but the key
941 * data is likely to be invalid now due to modifiers, so clear the shape key reference avoiding
942 * any possible shape corruption. */
943 if (DEG_is_evaluated(id)) {
944 Key **key_p = BKE_key_from_id_p(newid);
945 if (key_p) {
946 *key_p = nullptr;
947 }
948 }
949
950 return newid;
951}
952
953void BKE_id_move_to_same_lib(Main &bmain, ID &id, const ID &owner_id)
954{
955 if (owner_id.lib == id.lib) {
956 /* `id` is already in the target library, nothing to do. */
957 return;
958 }
959 if (ID_IS_LINKED(&id)) {
960 BLI_assert_msg(false, "Only local IDs can be moved into a library");
961 /* Protect release builds against errors in calling code, as continuing here can lead to
962 * critical Main data-base corruption. */
963 return;
964 }
965
967
968 id.lib = owner_id.lib;
969 id.tag |= ID_TAG_INDIRECT;
970
971 ListBaseT<ID> &lb = *which_libbase(&bmain, id.id_type());
973 bmain, lb, id, BKE_id_name(id), IDNewNameMode::RenameExistingNever, true);
974}
975
976static void id_embedded_swap(Main *bmain,
977 ID **embedded_id_a,
978 ID **embedded_id_b,
979 const bool do_full_id,
980 IDRemapper *remapper_id_a,
981 IDRemapper *remapper_id_b);
982
987static void id_swap(Main *bmain,
988 ID *id_a,
989 ID *id_b,
990 const bool do_full_id,
991 const bool do_self_remap,
992 IDRemapper *input_remapper_id_a,
993 IDRemapper *input_remapper_id_b,
994 const int self_remap_flags)
995{
996 BLI_assert(GS(id_a->name) == GS(id_b->name));
997
998 IDRemapper *remapper_id_a = input_remapper_id_a;
999 IDRemapper *remapper_id_b = input_remapper_id_b;
1000 if (do_self_remap) {
1001 if (remapper_id_a == nullptr) {
1002 remapper_id_a = MEM_new<IDRemapper>(__func__);
1003 }
1004 if (remapper_id_b == nullptr) {
1005 remapper_id_b = MEM_new<IDRemapper>(__func__);
1006 }
1007 }
1008
1009 const IDTypeInfo *id_type = BKE_idtype_get_info_from_id(id_a);
1010 BLI_assert(id_type != nullptr);
1011 const size_t id_struct_size = id_type->struct_size;
1012
1013 const ID id_a_back = *id_a;
1014 const ID id_b_back = *id_b;
1015
1016 char *id_swap_buff = static_cast<char *>(alloca(id_struct_size));
1017
1018 memcpy(id_swap_buff, id_a, id_struct_size);
1019 memcpy(id_a, id_b, id_struct_size);
1020 memcpy(id_b, id_swap_buff, id_struct_size);
1021
1022 if (!do_full_id) {
1023 /* Restore original ID's internal data. */
1024 *id_a = id_a_back;
1025 *id_b = id_b_back;
1026
1027 /* Exception: IDProperties. */
1028 id_a->properties = id_b_back.properties;
1029 id_b->properties = id_a_back.properties;
1030 id_a->system_properties = id_b_back.system_properties;
1031 id_b->system_properties = id_a_back.system_properties;
1032 /* Exception: recalc flags. */
1033 id_a->recalc = id_b_back.recalc;
1034 id_b->recalc = id_a_back.recalc;
1035 }
1036
1037 id_embedded_swap(bmain,
1038 reinterpret_cast<ID **>(bke::node_tree_ptr_from_id(id_a)),
1039 reinterpret_cast<ID **>(bke::node_tree_ptr_from_id(id_b)),
1040 do_full_id,
1041 remapper_id_a,
1042 remapper_id_b);
1043 if (id_a->id_type() == ID_SCE) {
1044 Scene *scene_a = id_cast<Scene *>(id_a);
1045 Scene *scene_b = id_cast<Scene *>(id_b);
1046 id_embedded_swap(bmain,
1047 reinterpret_cast<ID **>(&scene_a->master_collection),
1048 reinterpret_cast<ID **>(&scene_b->master_collection),
1049 do_full_id,
1050 remapper_id_a,
1051 remapper_id_b);
1052 }
1053
1054 if (remapper_id_a != nullptr) {
1055 remapper_id_a->add(id_b, id_a);
1056 }
1057 if (remapper_id_b != nullptr) {
1058 remapper_id_b->add(id_a, id_b);
1059 }
1060
1061 /* Finalize remapping of internal references to self broken by swapping, if requested. */
1062 if (do_self_remap) {
1064 bmain, {id_a}, ID_REMAP_TYPE_REMAP, *remapper_id_a, self_remap_flags);
1066 bmain, {id_b}, ID_REMAP_TYPE_REMAP, *remapper_id_b, self_remap_flags);
1067 }
1068
1069 if ((id_type->flags & IDTYPE_FLAGS_NO_ANIMDATA) == 0 && bmain) {
1070 /* Action Slots point to the IDs they animate, and thus now also needs swapping. Instead of
1071 * doing this here (and requiring knowledge of how that's supposed to be done), just mark these
1072 * pointers as dirty so that they're rebuilt at first use.
1073 *
1074 * There are a few calls to id_swap() where `bmain` is nil. None of these matter here, though:
1075 *
1076 * - At blendfile load (`read_libblock_undo_restore_at_old_address()`). Fine
1077 * because after loading the action slot user cache is rebuilt anyway.
1078 * - Swapping window managers (`swap_wm_data_for_blendfile()`). Fine because
1079 * WMs cannot be animated.
1080 * - Palette undo code (`palette_undo_preserve()`). Fine because palettes
1081 * cannot be animated. */
1083 }
1084
1085 if (input_remapper_id_a == nullptr && remapper_id_a != nullptr) {
1086 MEM_delete(remapper_id_a);
1087 }
1088 if (input_remapper_id_b == nullptr && remapper_id_b != nullptr) {
1089 MEM_delete(remapper_id_b);
1090 }
1091}
1092
1093/* Conceptually, embedded IDs are part of their owner's data. However, some parts of the code
1094 * (like e.g. the depsgraph) may treat them as independent IDs, so swapping them here and
1095 * switching their pointers in the owner IDs allows to help not break cached relationships and
1096 * such (by preserving the pointer values). */
1097static void id_embedded_swap(Main *bmain,
1098 ID **embedded_id_a,
1099 ID **embedded_id_b,
1100 const bool do_full_id,
1101 IDRemapper *remapper_id_a,
1102 IDRemapper *remapper_id_b)
1103{
1104 if (embedded_id_a != nullptr && *embedded_id_a != nullptr) {
1105 BLI_assert(embedded_id_b != nullptr);
1106
1107 if (*embedded_id_b == nullptr) {
1108 /* Cannot swap anything if one of the embedded IDs is nullptr. */
1109 return;
1110 }
1111
1112 /* Do not remap internal references to itself here, since embedded IDs pointers also need to be
1113 * potentially remapped in owner ID's data, which will also handle embedded IDs data. */
1114 id_swap(
1115 bmain, *embedded_id_a, *embedded_id_b, do_full_id, false, remapper_id_a, remapper_id_b, 0);
1116 /* Manual 'remap' of owning embedded pointer in owner ID. */
1117 std::swap(*embedded_id_a, *embedded_id_b);
1118
1119 /* Restore internal pointers to the swapped embedded IDs in their owners' data. This also
1120 * includes the potential self-references inside the embedded IDs themselves. */
1121 if (remapper_id_a != nullptr) {
1122 remapper_id_a->add(*embedded_id_b, *embedded_id_a);
1123 }
1124 if (remapper_id_b != nullptr) {
1125 remapper_id_b->add(*embedded_id_a, *embedded_id_b);
1126 }
1127 }
1128}
1129
1131 Main *bmain, ID *id_a, ID *id_b, const bool do_self_remap, const int self_remap_flags)
1132{
1133 id_swap(bmain, id_a, id_b, false, do_self_remap, nullptr, nullptr, self_remap_flags);
1134}
1135
1137 Main *bmain, ID *id_a, ID *id_b, const bool do_self_remap, const int self_remap_flags)
1138{
1139 id_swap(bmain, id_a, id_b, true, do_self_remap, nullptr, nullptr, self_remap_flags);
1140}
1141
1143{
1144 ID *newid = nullptr;
1145
1146 if (id && (ID_REAL_USERS(id) > 1)) {
1147 /* If property isn't editable,
1148 * we're going to have an extra block hanging around until we save. */
1149 if (RNA_property_editable(ptr, prop)) {
1150 Main *bmain = CTX_data_main(C);
1151 /* copy animation actions too */
1152 newid = BKE_id_copy_ex(bmain, id, nullptr, LIB_ID_COPY_DEFAULT | LIB_ID_COPY_ACTIONS);
1153 if (newid != nullptr) {
1154 /* us is 1 by convention with new IDs, but RNA_property_pointer_set
1155 * will also increment it if it's a user-reference-counting usage, decrement it here. */
1156 id_us_min(newid);
1157 /* 'Never unused' IDs types should always have an extra 'virtual' user ensured. */
1159 id_us_ensure_real(newid);
1160 }
1161
1162 /* assign copy */
1163 PointerRNA idptr = RNA_id_pointer_create(newid);
1164 RNA_property_pointer_set(ptr, prop, idptr, nullptr);
1165 RNA_property_update(C, ptr, prop);
1166
1167 return true;
1168 }
1169 }
1170 }
1171
1172 return false;
1173}
1174
1176{
1177 ID **id_pointer = cb_data->id_pointer;
1178 const LibraryForeachIDCallbackFlag cb_flag = cb_data->cb_flag;
1179 if (cb_flag & IDWALK_CB_USER) {
1180 id_us_plus(*id_pointer);
1181 }
1182 if (cb_flag & IDWALK_CB_USER_ONE) {
1183 id_us_ensure_real(*id_pointer);
1184 }
1185
1186 return IDWALK_RET_NOP;
1187}
1188
1190{
1191 ID **id_pointer = cb_data->id_pointer;
1192 const LibraryForeachIDCallbackFlag cb_flag = cb_data->cb_flag;
1193 if (cb_flag & IDWALK_CB_USER) {
1194 id_us_min(*id_pointer);
1195 }
1196 /* We can do nothing in IDWALK_CB_USER_ONE case! */
1197
1198 return IDWALK_RET_NOP;
1199}
1200
1202{
1203 ID *id = static_cast<ID *>(idv);
1204
1205 BLI_assert(bmain != nullptr);
1206 if ((id->tag & ID_TAG_NO_MAIN) == 0) {
1207 return;
1208 }
1209
1210 if ((id->tag & ID_TAG_NOT_ALLOCATED) != 0) {
1211 /* We cannot add non-allocated ID to Main! */
1212 return;
1213 }
1214
1215 /* We cannot allow non-userrefcounting IDs in Main database! */
1216 if ((id->tag & ID_TAG_NO_USER_REFCOUNT) != 0) {
1218 }
1219
1220 if (ID_IS_PACKED(id)) {
1222 if ((id->lib->flag & LIBRARY_FLAG_IS_ARCHIVE) == 0) {
1223 /* If the packed ID is currently using a regular library, find or create a suitable archive
1224 * one, and assign it to the id before adding it to the Main. */
1225 bool is_new_;
1227 *bmain, *id, *id->lib, id->deep_hash, is_new_);
1228 id->lib = archive_lib;
1229 }
1230 }
1231
1232 ListBaseT<ID> *lb = which_libbase(bmain, id->id_type());
1233 BKE_main_lock(bmain);
1234 BLI_addtail(lb, id);
1235 /* We need to allow adding extra datablocks into libraries too, e.g. to support generating new
1236 * overrides for recursive resync. */
1237 BKE_id_new_name_validate(*bmain, *lb, *id, nullptr, IDNewNameMode::RenameExistingNever, true);
1238 /* alphabetic insertion: is in new_id */
1240 bmain->is_memfile_undo_written = false;
1241 BKE_main_unlock(bmain);
1242
1244}
1245
1247{
1248 ID *id = static_cast<ID *>(idv);
1249
1250 BLI_assert(bmain != nullptr);
1251 if ((id->tag & ID_TAG_NO_MAIN) != 0) {
1252 return;
1253 }
1254
1255 /* For now, allow userrefcounting IDs to get out of Main - can be handy in some cases... */
1256
1257 ListBaseT<ID> *lb = which_libbase(bmain, id->id_type());
1258 BKE_main_lock(bmain);
1259 BLI_remlink(lb, id);
1260 BKE_main_namemap_remove_id(*bmain, *id);
1261 id->tag |= ID_TAG_NO_MAIN;
1262 bmain->is_memfile_undo_written = false;
1263 BKE_main_unlock(bmain);
1264}
1265
1267{
1268 ID *id = static_cast<ID *>(idv);
1269
1270 if ((id->tag & ID_TAG_NO_USER_REFCOUNT) == 0) {
1271 return;
1272 }
1273
1275 id->tag &= ~ID_TAG_NO_USER_REFCOUNT;
1276}
1277
1279{
1280 ID *id = static_cast<ID *>(idv);
1281
1282 /* We do not allow IDs in Main database to not be userrefcounting. */
1283 if ((id->tag & ID_TAG_NO_USER_REFCOUNT) != 0 || (id->tag & ID_TAG_NO_MAIN) != 0) {
1284 return;
1285 }
1286
1288 id->tag |= ID_TAG_NO_USER_REFCOUNT;
1289}
1290
1291void BKE_main_id_tag_listbase(ListBaseT<ID> *lb, const int tag, const bool value)
1292{
1293 ID *id;
1294 if (value) {
1295 for (id = lb->first(); id; id = static_cast<ID *>(id->next)) {
1296 id->tag |= tag;
1297 }
1298 }
1299 else {
1300 const int ntag = ~tag;
1301 for (id = lb->first(); id; id = static_cast<ID *>(id->next)) {
1302 id->tag &= ntag;
1303 }
1304 }
1305}
1306
1307void BKE_main_id_tag_idcode(Main *mainvar, const short type, const int tag, const bool value)
1308{
1309 ListBaseT<ID> *lb = which_libbase(mainvar, type);
1310
1311 BKE_main_id_tag_listbase(lb, tag, value);
1312}
1313
1314void BKE_main_id_tag_all(Main *mainvar, const int tag, const bool value)
1315{
1316 MainListsArray lbarray = BKE_main_lists_get(*mainvar);
1317 int a = lbarray.size();
1318 while (a--) {
1319 BKE_main_id_tag_listbase(lbarray[a], tag, value);
1320 }
1321}
1322
1323void BKE_main_id_flag_listbase(ListBaseT<ID> *lb, const int flag, const bool value)
1324{
1325 ID *id;
1326 if (value) {
1327 for (id = lb->first(); id; id = static_cast<ID *>(id->next)) {
1328 id->tag |= flag;
1329 }
1330 }
1331 else {
1332 const int nflag = ~flag;
1333 for (id = lb->first(); id; id = static_cast<ID *>(id->next)) {
1334 id->tag &= nflag;
1335 }
1336 }
1337}
1338
1339void BKE_main_id_flag_all(Main *bmain, const int flag, const bool value)
1340{
1341 MainListsArray lbarray = BKE_main_lists_get(*bmain);
1342 int a = lbarray.size();
1343 while (a--) {
1344 BKE_main_id_flag_listbase(lbarray[a], flag, value);
1345 }
1346}
1347
1349{
1350 int lb_len = 0;
1351 for (ID &id : *lb) {
1352 if (!ID_IS_LINKED(&id)) {
1353 lb_len += 1;
1354 }
1355 }
1356 if (lb_len <= 1) {
1357 return;
1358 }
1359
1360 /* Fill an array because renaming sorts. */
1361 ID **id_array = MEM_new_array_uninitialized<ID *>(size_t(lb_len), __func__);
1362 Set<StringRef> name_set;
1363 int i = 0;
1364 for (ID &id : *lb) {
1365 if (!ID_IS_LINKED(&id)) {
1366 id_array[i] = &id;
1367 i++;
1368 }
1369 }
1370 for (i = 0; i < lb_len; i++) {
1371 if (!name_set.add(BKE_id_name(*id_array[i]))) {
1373 *bmain, *lb, *id_array[i], nullptr, IDNewNameMode::RenameExistingNever, false);
1374 }
1375 }
1376 MEM_delete(id_array);
1377}
1378
1380{
1381 Object *ob;
1382
1383 /* flag for full recalc */
1384 for (ob = bmain->objects.first(); ob; ob = static_cast<Object *>(ob->id.next)) {
1385 if (ID_IS_LINKED(ob)) {
1387 }
1388 }
1389
1390 DEG_id_type_tag(bmain, ID_OB);
1391}
1392
1393/* *********** ALLOC AND FREE *****************
1394 *
1395 * BKE_libblock_free(ListBaseT<ID> *lb, ID *id )
1396 * provide a list-basis and data-block, but only ID is read
1397 *
1398 * void *BKE_libblock_alloc(ListBaseT<ID> *lb, type, name)
1399 * inserts in list and returns a new ID
1400 *
1401 * **************************** */
1402
1404{
1405 if (!id.runtime) {
1406 id.runtime = MEM_new<bke::id::ID_Runtime>(__func__);
1407 }
1408}
1409
1410size_t BKE_libblock_get_alloc_info(short type, const char **r_name)
1411{
1412 const IDTypeInfo *id_type = BKE_idtype_get_info_from_idcode(type);
1413
1414 if (id_type == nullptr) {
1415 if (r_name != nullptr) {
1416 *r_name = nullptr;
1417 }
1418 return 0;
1419 }
1420
1421 if (r_name != nullptr) {
1422 *r_name = id_type->name;
1423 }
1424 return id_type->struct_size;
1425}
1426
1428{
1429 const char *name;
1430 size_t size = BKE_libblock_get_alloc_info(type, &name);
1431 if (size != 0) {
1432 ID *id = static_cast<ID *>(MEM_new_zeroed(size, name));
1433 return id;
1434 }
1435 BLI_assert_msg(0, "Request to allocate unknown data type");
1436 return nullptr;
1437}
1438
1440 std::optional<Library *> owner_library,
1441 short type,
1442 const char *name,
1443 const int flag)
1444{
1446 BLI_assert((flag & LIB_ID_CREATE_NO_MAIN) != 0 || bmain != nullptr);
1447 BLI_assert((flag & LIB_ID_CREATE_NO_MAIN) != 0 || (flag & LIB_ID_CREATE_LOCAL) == 0);
1448
1449 ID *id = BKE_libblock_alloc_notest(type);
1451
1452 if (id) {
1453 if ((flag & LIB_ID_CREATE_NO_MAIN) != 0) {
1454 id->tag |= ID_TAG_NO_MAIN;
1455 }
1456 if ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) != 0) {
1457 id->tag |= ID_TAG_NO_USER_REFCOUNT;
1458 }
1459 if (flag & LIB_ID_CREATE_LOCAL) {
1460 id->tag |= ID_TAG_LOCALIZED;
1461 }
1462
1463 id->icon_id = 0;
1464 *(reinterpret_cast<short *>(id->name)) = type;
1465 if ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) {
1466 id->us = 1;
1467 }
1468 if ((flag & LIB_ID_CREATE_NO_MAIN) == 0) {
1469 /* Note that 2.8x versioning has tested not to cause conflicts. Node trees are
1470 * skipped in this check to allow adding a geometry node tree for versioning. */
1471 BLI_assert(bmain->is_locked_for_linking == false || ELEM(type, ID_WS, ID_GR, ID_NT));
1472 ListBaseT<ID> *lb = which_libbase(bmain, type);
1473
1474 /* This is important in "read-file do-version after lib-link" context mainly, but is a good
1475 * behavior for consistency in general: ID created for a Main should get that main's current
1476 * library pointer.
1477 *
1478 * NOTE: A bit convoluted.
1479 * - When Main has a defined `curlib`, it is assumed to be a split main containing only IDs
1480 * from that library. In that case, the library can be set later, and it avoids
1481 * synchronization issues in the namemap between the one of that temp 'library' Main and
1482 * the library ID runtime namemap itself. In a way, the ID can be assumed local to the
1483 * current Main, for its assignment to this Main.
1484 * - In all other cases, the Main is assumed 'complete', i.e. containing all local and
1485 * linked IDs, In that case, it is critical that the ID gets the correct library assigned
1486 * now, to ensure that the call to #BKE_id_new_name_validate gives a fully valid result
1487 * once it has been assigned to the current Main.
1488 */
1489 if (bmain->curlib) {
1490 id->lib = nullptr;
1491 }
1492 else {
1493 id->lib = owner_library ? *owner_library : nullptr;
1494 }
1495
1496 BKE_main_lock(bmain);
1497 BLI_addtail(lb, id);
1498 BKE_id_new_name_validate(*bmain, *lb, *id, name, IDNewNameMode::RenameExistingNever, true);
1499 bmain->is_memfile_undo_written = false;
1500 /* alphabetic insertion: is in new_id */
1501 BKE_main_unlock(bmain);
1502
1503 /* Split Main case, now the ID should get the Main's #curlib. */
1504 if (bmain->curlib) {
1505 BLI_assert(!owner_library || *owner_library == bmain->curlib);
1506 id->lib = bmain->curlib;
1507 }
1508
1509 /* This assert avoids having to keep name_map consistency when changing the library of an ID,
1510 * if this check is not true anymore it will have to be done here too. */
1511 BLI_assert(bmain->curlib == nullptr || bmain->curlib->runtime->name_map == nullptr);
1512
1513 /* TODO: to be removed from here! */
1514 if ((flag & LIB_ID_CREATE_NO_DEG_TAG) == 0) {
1515 DEG_id_type_tag(bmain, type);
1516 }
1517 }
1518 else {
1519 BLI_strncpy(id->name + 2, name, sizeof(id->name) - 2);
1520 id->lib = owner_library ? *owner_library : nullptr;
1521 }
1522
1523 /* When creating an ID in a library, ensure it is properly tagged as either directly or
1524 * indirectly linked.
1525 *
1526 * Note: Using the library parent here to decide if the type of linking is somewhat weak, but
1527 * there is no other info at this level. In any case, this status is reset/re-validated on file
1528 * save and read, and caller code is free to reset this value to its liking using
1529 * `id_lib_extern`/`id_lib_indirect`. */
1530 if (ID_IS_LINKED(id)) {
1531 id->tag |= (id->lib->runtime->parent) ? ID_TAG_INDIRECT : ID_TAG_EXTERN;
1532 }
1533
1534 /* We also need to ensure a valid `session_uid` for some non-main data (like embedded IDs).
1535 * IDs not allocated however should not need those (this would e.g. avoid generating session
1536 * UIDs for depsgraph evaluated IDs, if it was using this function). */
1537 if ((flag & LIB_ID_CREATE_NO_ALLOCATE) == 0) {
1539 }
1540 }
1541
1542 return id;
1543}
1544
1545void *BKE_libblock_alloc(Main *bmain, short type, const char *name, const int flag)
1546{
1547 return BKE_libblock_alloc_in_lib(bmain, std::nullopt, type, name, flag);
1548}
1549
1551{
1552 const IDTypeInfo *idtype_info = BKE_idtype_get_info_from_id(id);
1553
1554 if (idtype_info != nullptr) {
1555 if (idtype_info->init_data != nullptr) {
1556 idtype_info->init_data(id);
1557 }
1558 return;
1559 }
1560
1561 BLI_assert_msg(0, "IDType Missing IDTypeInfo");
1562}
1563
1565{
1566 id->runtime->remap.status = 0;
1567 id->runtime->remap.skipped_refcounted = 0;
1568 id->runtime->remap.skipped_direct = 0;
1569 id->runtime->remap.skipped_indirect = 0;
1570}
1571
1572/* ********** ID session-wise UID management. ********** */
1574
1576{
1577 if (id->session_uid == MAIN_ID_SESSION_UID_UNSET) {
1578 BLI_assert((id->tag & ID_TAG_TEMP_MAIN) == 0); /* Caller must ensure this. */
1579 id->session_uid = atomic_add_and_fetch_uint32(&global_session_uid, 1);
1580 /* In case overflow happens, still assign a valid ID. This way opening files many times works
1581 * correctly. */
1582 if (id->session_uid == MAIN_ID_SESSION_UID_UNSET) [[unlikely]] {
1583 id->session_uid = atomic_add_and_fetch_uint32(&global_session_uid, 1);
1584 }
1585 }
1586}
1587
1593
1595 std::optional<Library *> owner_library,
1596 const short type,
1597 const char *name)
1598
1599{
1600 BLI_assert(bmain != nullptr);
1601
1602 if (name == nullptr) {
1603 name = DATA_(BKE_idtype_idcode_to_name(type));
1604 }
1605
1606 ID *id = static_cast<ID *>(BKE_libblock_alloc_in_lib(bmain, owner_library, type, name, 0));
1608
1609 return id;
1610}
1611
1612void *BKE_id_new(Main *bmain, const short type, const char *name)
1613{
1614 return BKE_id_new_in_lib(bmain, std::nullopt, type, name);
1615}
1616
1617void *BKE_id_new_nomain(const short type, const char *name)
1618{
1619 if (name == nullptr) {
1620 name = DATA_(BKE_idtype_idcode_to_name(type));
1621 }
1622
1623 ID *id = static_cast<ID *>(BKE_libblock_alloc(
1624 nullptr,
1625 type,
1626 name,
1629
1630 return id;
1631}
1632
1634 std::optional<Library *> owner_library,
1635 const ID *id,
1636 std::optional<const ID *> new_owner_id,
1637 ID **new_id_p,
1638 const int orig_flag)
1639{
1640 ID *new_id = *new_id_p;
1641 int flag = orig_flag;
1642
1643 const bool is_embedded_id = (id->flag & ID_FLAG_EMBEDDED_DATA) != 0;
1644
1645 BLI_assert((flag & LIB_ID_CREATE_NO_MAIN) != 0 || bmain != nullptr);
1646 BLI_assert((flag & LIB_ID_CREATE_NO_MAIN) != 0 || (flag & LIB_ID_CREATE_NO_ALLOCATE) == 0);
1647 BLI_assert((flag & LIB_ID_CREATE_NO_MAIN) != 0 || (flag & LIB_ID_CREATE_LOCAL) == 0);
1648
1649 /* Embedded ID handling.
1650 *
1651 * NOTE: This makes copying code of embedded IDs non-reentrant (i.e. copying an embedded ID as
1652 * part of another embedded ID would not work properly). This is not an issue currently, but may
1653 * need to be addressed in the future. */
1654 if ((bmain != nullptr) && is_embedded_id) {
1655 flag |= LIB_ID_CREATE_NO_MAIN;
1656 }
1657
1658 /* The id->flag bits to copy over. */
1659 const int copy_idflag_mask = ID_FLAG_EMBEDDED_DATA;
1660 /* The id->tag bits to copy over. */
1661 const int copy_idtag_mask =
1662 /* Only copy potentially existing 'linked' tags if the new ID is being placed into a library.
1663 *
1664 * Further tag and paths remapping is handled in #BKE_id_copy_in_lib.
1665 */
1666 ((owner_library && *owner_library) ? (ID_TAG_EXTERN | ID_TAG_INDIRECT) : 0);
1667
1668 if ((flag & LIB_ID_CREATE_NO_ALLOCATE) != 0) {
1669 /* `new_id_p` already contains pointer to allocated memory.
1670 * Clear and initialize it similar to BKE_libblock_alloc_in_lib. */
1671 const size_t size = BKE_libblock_get_alloc_info(id->id_type(), nullptr);
1672 memset(static_cast<void *>(new_id), 0, size);
1674 STRNCPY(new_id->name, id->name);
1675 new_id->us = 0;
1677 new_id->lib = owner_library ? *owner_library : id->lib;
1678 /* TODO: Is this entirely consistent with BKE_libblock_alloc_in_lib, and can we
1679 * deduplicate the initialization code? */
1680 }
1681 else {
1682 new_id = static_cast<ID *>(
1683 BKE_libblock_alloc_in_lib(bmain, owner_library, id->id_type(), BKE_id_name(*id), flag));
1684 }
1685 BLI_assert(new_id != nullptr);
1686
1687 if ((flag & LIB_ID_COPY_SET_COPIED_ON_WRITE) != 0) {
1688 new_id->tag |= ID_TAG_COPIED_ON_EVAL;
1689 }
1690 else {
1691 new_id->tag &= ~ID_TAG_COPIED_ON_EVAL;
1692 }
1693
1694 const size_t id_len = BKE_libblock_get_alloc_info(new_id->id_type(), nullptr);
1695 const size_t id_offset = sizeof(ID);
1696 if (int(id_len) - int(id_offset) > 0) { /* signed to allow neg result */ /* XXX ????? */
1697 const char *cp = reinterpret_cast<const char *>(id);
1698 char *cpn = reinterpret_cast<char *>(new_id);
1699
1700 memcpy(cpn + id_offset, cp + id_offset, id_len - id_offset);
1701 }
1702
1703 new_id->flag = (new_id->flag & ~copy_idflag_mask) | (id->flag & copy_idflag_mask);
1704 new_id->tag = (new_id->tag & ~copy_idtag_mask) | (id->tag & copy_idtag_mask);
1705
1706 /* Embedded ID data handling. */
1707 if (is_embedded_id && (orig_flag & LIB_ID_CREATE_NO_MAIN) == 0) {
1708 new_id->tag &= ~ID_TAG_NO_MAIN;
1709 }
1710 /* NOTE: This also needs to run for ShapeKeys, which are not (yet) actual embedded IDs.
1711 * NOTE: for now, keep existing owner ID (i.e. owner of the source embedded ID) if no new one
1712 * is given. In some cases (e.g. depsgraph), this is important for later remapping to work
1713 * properly.
1714 */
1715 if (new_owner_id.has_value()) {
1716 const IDTypeInfo *idtype = BKE_idtype_get_info_from_id(new_id);
1717 BLI_assert(idtype->owner_pointer_get != nullptr);
1718 ID **owner_id_pointer = idtype->owner_pointer_get(new_id, false);
1719 if (owner_id_pointer) {
1720 *owner_id_pointer = const_cast<ID *>(*new_owner_id);
1721 if (*new_owner_id == nullptr) {
1722 /* If the new id does not have an owner, it's also not embedded. */
1723 new_id->flag &= ~ID_FLAG_EMBEDDED_DATA;
1724 }
1725 }
1726 }
1727
1728 /* We do not want any handling of user-count in code duplicating the data here, we do that all
1729 * at once in id_copy_libmanagement_cb() at the end. */
1730 const int copy_data_flag = orig_flag | LIB_ID_CREATE_NO_USER_REFCOUNT;
1731
1732 if (id->properties) {
1733 new_id->properties = IDP_CopyProperty_ex(id->properties, copy_data_flag);
1734 }
1735 if (id->system_properties) {
1736 new_id->system_properties = IDP_CopyProperty_ex(id->system_properties, copy_data_flag);
1737 }
1738
1739 /* This is never duplicated, only one existing ID should have a given weak ref to library/ID. */
1740 new_id->library_weak_reference = nullptr;
1741
1742 if ((orig_flag & LIB_ID_COPY_NO_LIB_OVERRIDE) == 0) {
1744 /* We do not want to copy existing override rules here, as they would break the proper
1745 * remapping between IDs. Proper overrides rules will be re-generated anyway. */
1746 BKE_lib_override_library_copy(new_id, id, false);
1747 }
1748 else if (ID_IS_OVERRIDE_LIBRARY_VIRTUAL(id)) {
1749 /* Just ensure virtual overrides do get properly tagged, there is not actual override data to
1750 * copy here. */
1752 }
1753 }
1754
1755 if (id_can_have_animdata(new_id)) {
1756 IdAdtTemplate *iat = reinterpret_cast<IdAdtTemplate *>(new_id);
1757
1758 /* the duplicate should get a copy of the animdata */
1759 if ((flag & LIB_ID_COPY_NO_ANIMDATA) == 0) {
1760 /* Note that even though horrors like root node-trees are not in bmain, the actions they use
1761 * in their anim data *are* in bmain... super-mega-hooray. */
1762 BLI_assert((copy_data_flag & LIB_ID_COPY_ACTIONS) == 0 ||
1763 (copy_data_flag & LIB_ID_CREATE_NO_MAIN) == 0);
1764 iat->adt = BKE_animdata_copy_in_lib(bmain, owner_library, iat->adt, copy_data_flag);
1765 }
1766 else {
1767 iat->adt = nullptr;
1768 }
1769 }
1770
1771 if (flag & LIB_ID_COPY_ASSET_METADATA) {
1772 if (id->asset_data) {
1773 new_id->asset_data = BKE_asset_metadata_copy(id->asset_data);
1774 }
1775 }
1776
1777 if ((flag & LIB_ID_CREATE_NO_DEG_TAG) == 0 && (flag & LIB_ID_CREATE_NO_MAIN) == 0) {
1778 DEG_id_type_tag(bmain, new_id->id_type());
1779 }
1780
1781 if (owner_library && *owner_library && ((*owner_library)->flag & LIBRARY_FLAG_IS_ARCHIVE) != 0) {
1783 }
1784
1785 if (flag & LIB_ID_COPY_ID_NEW_SET) {
1786 ID_NEW_SET(const_cast<ID *>(id), new_id);
1787 }
1788
1789 *new_id_p = new_id;
1790}
1791
1792void BKE_libblock_copy_ex(Main *bmain, const ID *id, ID **new_id_p, const int orig_flag)
1793{
1794 BKE_libblock_copy_in_lib(bmain, std::nullopt, id, std::nullopt, new_id_p, orig_flag);
1795}
1796
1797void *BKE_libblock_copy(Main *bmain, const ID *id)
1798{
1799 ID *idn = nullptr;
1800
1801 BKE_libblock_copy_in_lib(bmain, std::nullopt, id, std::nullopt, &idn, 0);
1802
1803 return idn;
1804}
1805
1806/* ***************** ID ************************ */
1807
1809 const short type,
1810 const char *name,
1811 const std::optional<Library *> lib)
1812{
1813 const ListBaseT<ID> *lb = which_libbase(bmain, type);
1814 BLI_assert(lb != nullptr);
1815
1816 ID *id = static_cast<ID *>(BLI_findstring(lb, name, offsetof(ID, name) + 2));
1817 if (lib) {
1818 while (id && id->lib != *lib) {
1819 id = static_cast<ID *>(BLI_listbase_findafter_string(
1820 reinterpret_cast<Link *>(id), name, offsetof(ID, name) + 2));
1821 }
1822 }
1823 return id;
1824}
1825
1826ID *BKE_libblock_find_session_uid(Main *bmain, const short type, const uint32_t session_uid)
1827{
1828 const ListBaseT<ID> *lb = which_libbase(bmain, type);
1829 BLI_assert(lb != nullptr);
1830 for (ID &id : *lb) {
1831 if (id.session_uid == session_uid) {
1832 return &id;
1833 }
1834 }
1835 return nullptr;
1836}
1837
1839{
1840 ID *id_iter;
1841 FOREACH_MAIN_ID_BEGIN (bmain, id_iter) {
1842 if (id_iter->session_uid == session_uid) {
1843 return id_iter;
1844 }
1845 }
1847 return nullptr;
1848}
1849
1851 const short type,
1852 const char *name,
1853 const char *lib_name)
1854{
1855 const bool is_linked = (lib_name && lib_name[0] != '\0');
1856 Library *library = is_linked ? reinterpret_cast<Library *>(
1857 BKE_libblock_find_name(bmain, ID_LI, lib_name, nullptr)) :
1858 nullptr;
1859 if (is_linked && !library) {
1860 return nullptr;
1861 }
1862 return BKE_libblock_find_name(bmain, type, name, library);
1863}
1864
1866 short type,
1867 const char *name,
1868 const char *lib_filepath_abs)
1869{
1870 const bool is_linked = (lib_filepath_abs && lib_filepath_abs[0] != '\0');
1871 Library *library = nullptr;
1872 if (is_linked) {
1873 const ListBaseT<ID> *lb = which_libbase(bmain, ID_LI);
1874 for (ID &id_iter : *lb) {
1875 Library *lib_iter = reinterpret_cast<Library *>(&id_iter);
1876 if (STREQ(lib_iter->runtime->filepath_abs, lib_filepath_abs)) {
1877 library = lib_iter;
1878 break;
1879 }
1880 }
1881 if (!library) {
1882 return nullptr;
1883 }
1884 }
1885 return BKE_libblock_find_name(bmain, type, name, library);
1886}
1887
1888void id_sort_by_name(ListBaseT<ID> *lb, ID *id, ID *id_sorting_hint)
1889{
1890#define ID_SORT_STEP_SIZE 512
1891
1892 ID *idtest;
1893
1894 /* insert alphabetically */
1895 if (lb->first() == lb->last()) {
1896 return;
1897 }
1898
1899 BLI_remlink(lb, id);
1900
1901 /* Check if we can actually insert id before or after id_sorting_hint, if given. */
1902 if (!ELEM(id_sorting_hint, nullptr, id) && id_sorting_hint->lib == id->lib) {
1903 BLI_assert(BLI_findindex(lb, id_sorting_hint) >= 0);
1904
1905 ID *id_sorting_hint_next = static_cast<ID *>(id_sorting_hint->next);
1906 if (BLI_strcasecmp(id_sorting_hint->name, id->name) < 0 &&
1907 (id_sorting_hint_next == nullptr || id_sorting_hint_next->lib != id->lib ||
1908 BLI_strcasecmp(id_sorting_hint_next->name, id->name) > 0))
1909 {
1910 BLI_insertlinkafter(lb, id_sorting_hint, id);
1911 return;
1912 }
1913
1914 ID *id_sorting_hint_prev = static_cast<ID *>(id_sorting_hint->prev);
1915 if (BLI_strcasecmp(id_sorting_hint->name, id->name) > 0 &&
1916 (id_sorting_hint_prev == nullptr || id_sorting_hint_prev->lib != id->lib ||
1917 BLI_strcasecmp(id_sorting_hint_prev->name, id->name) < 0))
1918 {
1919 BLI_insertlinkbefore(lb, id_sorting_hint, id);
1920 return;
1921 }
1922 }
1923
1924 void *item_array[ID_SORT_STEP_SIZE];
1925 int item_array_index;
1926
1927 /* Step one: We go backward over a whole chunk of items at once, until we find a limit item
1928 * that is lower than, or equal (should never happen!) to the one we want to insert. */
1929 /* NOTE: We start from the end, because in typical 'heavy' case (insertion of lots of IDs at
1930 * once using the same base name), newly inserted items will generally be towards the end
1931 * (higher extension numbers). */
1932 bool is_in_library = false;
1933 item_array_index = ID_SORT_STEP_SIZE - 1;
1934 for (idtest = lb->last(); idtest != nullptr; idtest = static_cast<ID *>(idtest->prev)) {
1935 if (is_in_library) {
1936 if (idtest->lib != id->lib) {
1937 /* We got out of expected library 'range' in the list, so we are done here and can move on
1938 * to the next step. */
1939 break;
1940 }
1941 }
1942 else if (idtest->lib == id->lib) {
1943 /* We are entering the expected library 'range' of IDs in the list. */
1944 is_in_library = true;
1945 }
1946
1947 if (!is_in_library) {
1948 continue;
1949 }
1950
1951 item_array[item_array_index] = idtest;
1952 if (item_array_index == 0) {
1953 if (BLI_strcasecmp(idtest->name, id->name) <= 0) {
1954 break;
1955 }
1956 item_array_index = ID_SORT_STEP_SIZE;
1957 }
1958 item_array_index--;
1959 }
1960
1961 /* Step two: we go forward in the selected chunk of items and check all of them, as we know
1962 * that our target is in there. */
1963
1964 /* If we reached start of the list, current item_array_index is off-by-one.
1965 * Otherwise, we already know that it points to an item lower-or-equal-than the one we want to
1966 * insert, no need to redo the check for that one.
1967 * So we can increment that index in any case. */
1968 for (item_array_index++; item_array_index < ID_SORT_STEP_SIZE; item_array_index++) {
1969 idtest = static_cast<ID *>(item_array[item_array_index]);
1970 if (BLI_strcasecmp(idtest->name, id->name) > 0) {
1971 BLI_insertlinkbefore(lb, idtest, id);
1972 break;
1973 }
1974 }
1975 if (item_array_index == ID_SORT_STEP_SIZE) {
1976 if (idtest == nullptr) {
1977 /* If idtest is nullptr here, it means that in the first loop, the last comparison was
1978 * performed exactly on the first item of the list, and that it also failed. And that the
1979 * second loop was not walked at all.
1980 *
1981 * In other words, if `id` is local, all the items in the list are greater than the inserted
1982 * one, so we can put it at the start of the list. Or, if `id` is linked, it is the first one
1983 * of its library, and we can put it at the very end of the list. */
1984 if (ID_IS_LINKED(id)) {
1985 BLI_addtail(lb, id);
1986 }
1987 else {
1988 BLI_addhead(lb, id);
1989 }
1990 }
1991 else {
1992 BLI_insertlinkafter(lb, idtest, id);
1993 }
1994 }
1995
1996#undef ID_SORT_STEP_SIZE
1997}
1998
2000 ListBaseT<ID> &lb,
2001 ID &id,
2002 const char *newname,
2003 IDNewNameMode mode,
2004 const bool do_linked_data)
2005{
2006 char name[MAX_ID_NAME - 2];
2007
2008 /* If library, don't rename (unless explicitly required), but do ensure proper sorting. */
2009 if (!do_linked_data && ID_IS_LINKED(&id)) {
2010 id_sort_by_name(&lb, &id, nullptr);
2011
2012 return {IDNewNameResult::Action::UNCHANGED, nullptr};
2013 }
2014
2015 /* If no name given, use name of current ID. */
2016 if (newname == nullptr) {
2017 newname = BKE_id_name(id);
2018 }
2019 /* Make a copy of given name (newname args can be const). */
2020 STRNCPY(name, newname);
2021
2022 if (name[0] == '\0') {
2023 /* Disallow empty names. */
2024 STRNCPY_UTF8(name, DATA_(BKE_idtype_idcode_to_name(id.id_type())));
2025 }
2026 else {
2027 /* Disallow non UTF8 chars,
2028 * the interface checks for this but new ID's based on file names don't. */
2029 BLI_str_utf8_invalid_strip(name, strlen(name));
2030 }
2031
2032 /* Store original requested new name, in modes that may solve name conflict by renaming the
2033 * existing conflicting ID. */
2034 char orig_name[MAX_ID_NAME - 2];
2036 STRNCPY(orig_name, name);
2037 }
2038
2039 const bool had_name_collision = BKE_main_namemap_get_unique_name(bmain, id, name);
2040
2041 if (had_name_collision &&
2043 {
2044 char prev_name[MAX_ID_NAME - 2];
2045 char prev_name_root[MAX_ID_NAME - 2];
2046 int prev_number = 0;
2047 char new_name_root[MAX_ID_NAME - 2];
2048 int new_number = 0;
2049 STRNCPY(prev_name, BKE_id_name(id));
2051 BLI_string_split_name_number(BKE_id_name(id), '.', prev_name_root, &prev_number);
2052 BLI_string_split_name_number(name, '.', new_name_root, &new_number);
2053 }
2054
2055 ID *id_other = BKE_libblock_find_name(&bmain, id.id_type(), orig_name, id.lib);
2056 BLI_assert(id_other);
2057
2058 /* In case of #RenameExistingSameRoot, the existing ID (`id_other`) is only renamed if it has
2059 * the same 'root' name as the current name of the renamed `id`. */
2061 (mode == IDNewNameMode::RenameExistingSameRoot && STREQ(prev_name_root, new_name_root)))
2062 {
2063 BLI_strncpy(id_other->name + 2, name, sizeof(id_other->name) - 2);
2064 id_sort_by_name(&lb, id_other, nullptr);
2065
2066 const bool is_idname_changed = !STREQ(BKE_id_name(id), orig_name);
2068 if (is_idname_changed) {
2069 BLI_strncpy(id.name + 2, orig_name, sizeof(id.name) - 2);
2071 }
2072 id_sort_by_name(&lb, &id, nullptr);
2073
2074 return result;
2075 }
2076 }
2077
2078 /* The requested new name may be available (not collide with any other existing ID name), but
2079 * still differ from the current name of the renamed ID.
2080 * Conversely, the requested new name may have been colliding with an existing one, and the
2081 * generated unique name may end up being the current ID's name. */
2082 const bool is_idname_changed = !STREQ(BKE_id_name(id), name);
2083
2085 if (is_idname_changed) {
2086 BLI_strncpy(id.name + 2, name, sizeof(id.name) - 2);
2087 result.action = had_name_collision ? IDNewNameResult::Action::RENAMED_COLLISION_ADJUSTED :
2089 }
2090 else if (had_name_collision) {
2092 }
2093 id_sort_by_name(&lb, &id, nullptr);
2094 return result;
2095}
2096
2098{
2099 ID *id;
2100
2101 FOREACH_MAIN_ID_BEGIN (bmain, id) {
2103 }
2105}
2106
2108{
2109 ID **id_pointer = cb_data->id_pointer;
2110 const LibraryForeachIDCallbackFlag cb_flag = cb_data->cb_flag;
2111 const bool do_linked_only = bool(POINTER_AS_INT(cb_data->user_data));
2112
2113 if (*id_pointer == nullptr) {
2114 return IDWALK_RET_NOP;
2115 }
2116 if (do_linked_only && !ID_IS_LINKED(*id_pointer)) {
2117 return IDWALK_RET_NOP;
2118 }
2119
2120 if (cb_flag & IDWALK_CB_USER) {
2121 /* Do not touch to direct/indirect linked status here... */
2122 id_us_plus_no_lib(*id_pointer);
2123 }
2124 if (cb_flag & IDWALK_CB_USER_ONE) {
2125 id_us_ensure_real(*id_pointer);
2126 }
2127
2128 return IDWALK_RET_NOP;
2129}
2130
2131void BKE_main_id_refcount_recompute(Main *bmain, const bool do_linked_only)
2132{
2133 ID *id;
2134
2135 FOREACH_MAIN_ID_BEGIN (bmain, id) {
2136 if (!ID_IS_LINKED(id) && do_linked_only) {
2137 continue;
2138 }
2139 id->us = ID_FAKE_USERS(id);
2140 /* Note that we keep EXTRAUSER tag here, since some UI users may define it too... */
2141 if (id->tag & ID_TAG_EXTRAUSER) {
2142 id->tag &= ~(ID_TAG_EXTRAUSER | ID_TAG_EXTRAUSER_SET);
2144 }
2145 if (ELEM(id->id_type(), ID_SCE, ID_WM, ID_WS)) {
2146 /* These IDs should always have a 'virtual' user. */
2148 }
2149 }
2151
2152 /* Go over whole Main database to re-generate proper user-counts. */
2153 FOREACH_MAIN_ID_BEGIN (bmain, id) {
2155 id,
2157 POINTER_FROM_INT(int(do_linked_only)),
2159 }
2161}
2162
2164{
2165 ID *self_id = cb_data->self_id;
2166 ID **id_pointer = cb_data->id_pointer;
2167 ID *id = *id_pointer;
2168 const LibraryForeachIDCallbackFlag cb_flag = cb_data->cb_flag;
2169 auto &extern_packed_ids = *static_cast<VectorSet<ID *> *>(cb_data->user_data);
2170
2171 if (!id) {
2172 return IDWALK_RET_NOP;
2173 }
2174 if (!ID_IS_LINKED(id)) {
2175 return IDWALK_RET_NOP;
2176 }
2177 BLI_assert((cb_flag & IDWALK_CB_INDIRECT_USAGE) == 0);
2178 if (self_id->tag & ID_TAG_RUNTIME) {
2179 return IDWALK_RET_NOP;
2180 }
2181 if (cb_flag & IDWALK_CB_WRITEFILE_IGNORE) {
2182 /* Do not consider these ID usages (typically, from the Outliner e.g.) as making the ID
2183 * directly linked. */
2184 return IDWALK_RET_NOP;
2185 }
2186
2187 if (ID_IS_PACKED(id)) {
2188 /* Linked packed IDs are always unconditionally considered extern if used by a local ID. See
2189 * also comment in caller #BKE_main_id_indirect_linked_update for more details. */
2190 id_lib_extern(id);
2191 extern_packed_ids.add(id);
2192 return IDWALK_RET_NOP;
2193 }
2194
2195 if (!BKE_idtype_idcode_is_linkable(id->id_type())) {
2196 /* Usages of unlinkable IDs (aka ShapeKeys and some UI IDs) should never cause them to
2197 * be considered as directly linked. This can often happen e.g. from UI data (the
2198 * Outliner will have links to most IDs).
2199 */
2200 return IDWALK_RET_NOP;
2201 }
2202 if (cb_flag & IDWALK_CB_DIRECT_WEAK_LINK) {
2204 }
2205 else {
2206 id_lib_extern(id);
2207 }
2208 return IDWALK_RET_NOP;
2209}
2210
2211void BKE_main_id_indirect_linked_update(Main &bmain, std::optional<Span<ID *>> local_ids)
2212{
2213 auto reset_linked_status_cb = [](ID &id) -> void {
2214 if (!ID_IS_LINKED(&id)) {
2215 return;
2216 }
2217 if (BKE_idtype_idcode_is_linkable(id.id_type())) {
2218 if (USER_DEVELOPER_TOOL_TEST(&U, use_all_linked_data_direct)) {
2219 /* Forces all linked data to be considered as directly linked.
2220 * FIXME: Workaround some BAT tool limitations for Heist production, should be removed
2221 * asap afterward. */
2222 id_lib_extern(&id, true);
2223 }
2224 else if (id.id_type() == ID_SCE) {
2225 /* For scenes, do not force them into 'indirectly linked' status.
2226 * The main reason is that scenes typically have no users, so most linked scene would be
2227 * systematically 'lost' on file save.
2228 *
2229 * While this change re-introduces the 'no-more-used data laying around in files for
2230 * ever' issue when it comes to scenes, this solution seems to be the most sensible one
2231 * for the time being, considering that:
2232 * - Scene are a top-level container.
2233 * - Linked scenes are typically explicitly linked by the user.
2234 * - Cases where scenes would be indirectly linked by other data (e.g. when linking a
2235 * collection or material) can be considered at the very least as not following sane
2236 * practice in data dependencies.
2237 * - There are typically not hundreds of scenes in a file, and they are always very
2238 * easily discoverable and browsable from the main UI. */
2239 /* Ensure that embedded IDs of a scene are in sync with their scene's directly/indirectly
2240 * linked status. */
2242 }
2243 else {
2244 id_lib_indirect(&id, true);
2245 }
2246 }
2247 else {
2248 /* By definition, non-linkable IDs are always indirectly linked (outside of special cases
2249 * like the packed linked data, which is covered later in this function, see
2250 * `id_indirect_linked_update_packed_ids_fn`). */
2251 id_lib_indirect(&id, true);
2252 }
2253 };
2254 for (ID &id : MainAllIDsIterator(bmain)) {
2255 reset_linked_status_cb(id);
2256 }
2257
2258 const LibraryForeachIDFlag foreach_id_flag = IDWALK_READONLY | IDWALK_INCLUDE_UI;
2259 VectorSet<ID *> extern_packed_ids;
2260 if (local_ids.has_value()) {
2261 for (ID *id : *local_ids) {
2264 &bmain, id, id_indirect_linked_update_fn, &extern_packed_ids, foreach_id_flag);
2265 }
2266 }
2267 else {
2268 for (ID &id : MainAllIDsIterator(bmain)) {
2269 if (ID_IS_LINKED(&id)) {
2270 continue;
2271 }
2273 &bmain, &id, id_indirect_linked_update_fn, &extern_packed_ids, foreach_id_flag);
2274 }
2275 }
2276
2277 /* Packed linked IDs have a different understanding of directly vs. indirectly linked.
2278 *
2279 * Essentially, all IDs which are a dependency of a given directly linked packed data are also
2280 * considered directly linked.
2281 *
2282 * This makes sense on the principle level (packed IDs _never_ use regular other linked
2283 * IDs, so they behave as some sort of coherent data unit). And it ensures e.g. that if one
2284 * packed ID is written in a blendfile, then all of its dependencies are also written with it.
2285 */
2286 auto id_indirect_linked_update_packed_ids_fn =
2287 [&extern_packed_ids](LibraryIDLinkCallbackData *cb_data) -> int {
2288 ID *self_id = cb_data->self_id;
2289 ID **id_pointer = cb_data->id_pointer;
2290 ID *id = *id_pointer;
2291
2292 if (!id) {
2293 return IDWALK_RET_NOP;
2294 }
2295 if (cb_data->cb_flag & IDWALK_CB_LOOPBACK) {
2296 /* Loop-back usages (e.g. from shape-key to their mesh etc.) can be ignored in ID dependency
2297 * processes. */
2298 return IDWALK_RET_NOP;
2299 }
2300 if (cb_data->cb_flag & (IDWALK_CB_EMBEDDED | IDWALK_CB_EMBEDDED_NOT_OWNING)) {
2301 /* Embedded IDs should always share the directly/indirectly linked status of their owner
2302 * regular ID (ensured as part of `id_lib_extern` calls).
2303 *
2304 * There's also no need to add them to `extern_packed_ids`, their owner is already there and
2305 * `BKE_library_foreach_ID_link` will take care of processing their embedded IDs own ID
2306 * usages. */
2308 BLI_assert(id->tag & ID_TAG_EXTERN);
2309 return IDWALK_RET_NOP;
2310 }
2311 if (cb_data->cb_flag & IDWALK_CB_WRITEFILE_IGNORE) {
2312 /* Do not consider these ID usages (typically, from the Outliner e.g.) as making the ID
2313 * directly linked. */
2314 return IDWALK_RET_NOP;
2315 }
2316
2317 BLI_assert(ID_IS_PACKED(self_id) && ID_IS_PACKED(id));
2318 BLI_assert(self_id->tag & ID_TAG_EXTERN);
2319 UNUSED_VARS_NDEBUG(self_id);
2320
2321 id_lib_extern(id);
2322 extern_packed_ids.add(id);
2323 return IDWALK_RET_NOP;
2324 };
2325 for (int i = 0; i < extern_packed_ids.size(); i++) {
2327 extern_packed_ids[i],
2328 id_indirect_linked_update_packed_ids_fn,
2329 nullptr,
2330 foreach_id_flag);
2331 }
2332}
2333
2335 Set<ID *> &loop_tags,
2336 MainIDRelations *id_relations,
2337 Set<ID *> &done_ids)
2338{
2339 if (done_ids.contains(id)) {
2340 return; /* Already checked, nothing else to do. */
2341 }
2342
2343 MainIDRelationsEntry *entry = id_relations->relations_from_pointers->lookup(id);
2344 loop_tags.add(id);
2345 for (MainIDRelationsEntryItem *from_id_entry = entry->from_ids; from_id_entry != nullptr;
2346 from_id_entry = from_id_entry->next)
2347 {
2348 /* Our oh-so-beloved 'from' pointers... Those should always be ignored here, since the actual
2349 * relation we want to check is in the other way around. */
2350 if (from_id_entry->usage_flag & IDWALK_CB_LOOPBACK) {
2351 continue;
2352 }
2353
2354 ID *from_id = from_id_entry->id_pointer.from;
2355
2356 /* Shape-keys are considered 'private' to their owner ID here, and never tagged
2357 * (since they cannot be linked), so we have to switch effective parent to their owner.
2358 */
2359 if (from_id->id_type() == ID_KE) {
2360 from_id = (id_cast<Key *>(from_id))->from;
2361 }
2362
2363 if (!ID_IS_LINKED(from_id)) {
2364 /* Local user, early out to avoid some gset querying... */
2365 continue;
2366 }
2367 if (!done_ids.contains(from_id)) {
2368 if (loop_tags.contains(from_id)) {
2369 /* We are in a 'dependency loop' of IDs, this does not say us anything, skip it.
2370 * Note that this is the situation that can lead to archipelagos of linked data-blocks
2371 * (since all of them have non-local users, they would all be duplicated,
2372 * leading to a loop of unused linked data-blocks that cannot be freed since they all use
2373 * each other...). */
2374 continue;
2375 }
2376 /* Else, recursively check that user ID. */
2377 library_make_local_copying_check(from_id, loop_tags, id_relations, done_ids);
2378 }
2379
2380 if (from_id->tag & ID_TAG_DOIT) {
2381 /* This user will be fully local in future, so far so good,
2382 * nothing to do here but check next user. */
2383 }
2384 else {
2385 /* This user won't be fully local in future, so current ID won't be either.
2386 * And we are done checking it. */
2387 id->tag &= ~ID_TAG_DOIT;
2388 break;
2389 }
2390 }
2391 done_ids.add(id);
2392 loop_tags.remove(id);
2393}
2394
2396 const Library *lib,
2397 GHash *old_to_new_ids,
2398 const bool untagged_only,
2399 const bool set_fake,
2400 const bool clear_asset_data)
2401{
2402 /* NOTE: Old (2.77) version was simply making (tagging) data-blocks as local,
2403 * without actually making any check whether they were also indirectly used or not...
2404 *
2405 * Current version uses regular id_make_local callback, with advanced pre-processing step to
2406 * detect all cases of IDs currently indirectly used, but which will be used by local data only
2407 * once this function is finished. This allows to avoid any unneeded duplication of IDs, and
2408 * hence all time lost afterwards to remove orphaned linked data-blocks. */
2409
2410 MainListsArray lbarray = BKE_main_lists_get(*bmain);
2411
2412 LinkNode *todo_ids = nullptr;
2413 LinkNode *copied_ids = nullptr;
2414 MemArena *linklist_mem = BLI_memarena_new(512 * sizeof(*todo_ids), __func__);
2415
2416 Set<ID *> done_ids;
2417
2418#ifdef DEBUG_TIME
2419 TIMEIT_START(make_local);
2420#endif
2421
2422 BKE_main_relations_create(bmain, 0);
2423
2424#ifdef DEBUG_TIME
2425 printf("Pre-compute current ID relations: Done.\n");
2426 TIMEIT_VALUE_PRINT(make_local);
2427#endif
2428
2429 /* Step 1: Detect data-blocks to make local. */
2430 for (int a = lbarray.size(); a--;) {
2431 ID *id = lbarray[a]->first();
2432
2433 /* Do not explicitly make local non-linkable IDs (shape-keys, in fact),
2434 * they are assumed to be handled by real data-blocks responsible of them. */
2435 const bool do_skip = (id && !BKE_idtype_idcode_is_linkable(id->id_type()));
2436
2437 for (; id; id = static_cast<ID *>(id->next)) {
2438 ID *ntree = id_cast<ID *>(bke::node_tree_from_id(id));
2439
2440 id->tag &= ~ID_TAG_DOIT;
2441 if (ntree != nullptr) {
2442 ntree->tag &= ~ID_TAG_DOIT;
2443 }
2444
2445 if (!ID_IS_LINKED(id)) {
2446 id->tag &= ~(ID_TAG_EXTERN | ID_TAG_INDIRECT | ID_TAG_NEW);
2447 id->flag &= ~ID_FLAG_INDIRECT_WEAK_LINK;
2449 ELEM(lib, nullptr, id->override_library->reference->lib) &&
2450 ((untagged_only == false) || !(id->tag & ID_TAG_PRE_EXISTING)))
2451 {
2452 /* Validating liboverride hierarchy root pointers will happen later in this function,
2453 * rather than doing it for each and every localized ID. */
2455 }
2456 }
2457 /* The check on the fourth line (ID_TAG_PRE_EXISTING) is done so it's possible to tag data
2458 * you don't want to be made local, used for appending data,
2459 * so any libdata already linked won't become local (very nasty
2460 * to discover all your links are lost after appending).
2461 * Also, never ever make proxified objects local, would not make any sense. */
2462 /* Some more notes:
2463 * - Shape-keys are never tagged here (since they are not linkable).
2464 * - Node-trees used in materials etc. have to be tagged manually,
2465 * since they do not exist in Main (!).
2466 * This is ok-ish on 'make local' side of things
2467 * (since those are handled by their 'owner' IDs),
2468 * but complicates slightly the pre-processing of relations between IDs at step 2... */
2469 else if (!do_skip && id->tag & (ID_TAG_EXTERN | ID_TAG_INDIRECT | ID_TAG_NEW) &&
2470 ELEM(lib, nullptr, id->lib) &&
2471 ((untagged_only == false) || !(id->tag & ID_TAG_PRE_EXISTING)))
2472 {
2473 BLI_linklist_prepend_arena(&todo_ids, id, linklist_mem);
2474 id->tag |= ID_TAG_DOIT;
2475
2476 /* Tag those nasty non-ID node-trees,
2477 * but do not add them to todo list, making them local is handled by 'owner' ID.
2478 * This is needed for library_make_local_copying_check() to work OK at step 2. */
2479 if (ntree != nullptr) {
2480 ntree->tag |= ID_TAG_DOIT;
2481 }
2482 }
2483 else {
2484 /* Linked ID that we won't be making local (needed info for step 2, see below). */
2485 done_ids.add(id);
2486 }
2487 }
2488 }
2489
2490#ifdef DEBUG_TIME
2491 printf("Step 1: Detect data-blocks to make local: Done.\n");
2492 TIMEIT_VALUE_PRINT(make_local);
2493#endif
2494
2495 /* Step 2: Check which data-blocks we can directly make local
2496 * (because they are only used by already, or future, local data),
2497 * others will need to be duplicated. */
2498 Set<ID *> loop_tags;
2499 for (LinkNode *it = todo_ids; it; it = it->next) {
2501 static_cast<ID *>(it->link), loop_tags, bmain->relations, done_ids);
2502 BLI_assert(loop_tags.is_empty());
2503 }
2504
2505 /* Next step will most likely add new IDs, better to get rid of this mapping now. */
2507
2508#ifdef DEBUG_TIME
2509 printf("Step 2: Check which data-blocks we can directly make local: Done.\n");
2510 TIMEIT_VALUE_PRINT(make_local);
2511#endif
2512
2513 const int make_local_flags = clear_asset_data ? LIB_ID_MAKELOCAL_ASSET_DATA_CLEAR : 0;
2514
2515 /* Step 3: Make IDs local, either directly (quick and simple), or using generic process,
2516 * which involves more complex checks and might instead
2517 * create a local copy of original linked ID. */
2518 for (LinkNode *it = todo_ids, *it_next; it; it = it_next) {
2519 it_next = it->next;
2520 ID *id = static_cast<ID *>(it->link);
2521
2522 if (id->tag & ID_TAG_DOIT) {
2523 /* We know all users of this object are local or will be made fully local, even if
2524 * currently there are some indirect usages. So instead of making a copy that we'll likely
2525 * get rid of later, directly make that data block local.
2526 * Saves a tremendous amount of time with complex scenes... */
2527 BKE_lib_id_clear_library_data(bmain, id, make_local_flags);
2528 BKE_lib_id_expand_local(bmain, id, 0);
2529 id->tag &= ~ID_TAG_DOIT;
2530
2531 if (id->id_type() == ID_OB) {
2532 BKE_rigidbody_ensure_local_object(bmain, id_cast<Object *>(id));
2533 }
2534 }
2535 else {
2536 /* In this specific case, we do want to make ID local even if it has no local usage yet... */
2537 BKE_lib_id_make_local(bmain, id, make_local_flags | LIB_ID_MAKELOCAL_FULL_LIBRARY);
2538
2539 if (id->newid) {
2540 if (id->newid->id_type() == ID_OB) {
2541 BKE_rigidbody_ensure_local_object(bmain, id_cast<Object *>(id->newid));
2542 }
2543
2544 /* Reuse already allocated LinkNode (transferring it from todo_ids to copied_ids). */
2545 BLI_linklist_prepend_nlink(&copied_ids, id, it);
2546 }
2547 }
2548
2549 if (set_fake) {
2550 if (!ELEM(id->id_type(), ID_OB, ID_GR)) {
2551 /* do not set fake user on objects, groups (instancing) */
2552 id_fake_user_set(id);
2553 }
2554 }
2555 }
2556
2557#ifdef DEBUG_TIME
2558 printf("Step 3: Make IDs local: Done.\n");
2559 TIMEIT_VALUE_PRINT(make_local);
2560#endif
2561
2562 /* At this point, we are done with directly made local IDs.
2563 * Now we have to handle duplicated ones, since their
2564 * remaining linked original counterpart may not be needed anymore... */
2565 todo_ids = nullptr;
2566
2567 /* Step 4: We have to remap local usages of old (linked) ID to new (local)
2568 * ID in a separated loop,
2569 * as lbarray ordering is not enough to ensure us we did catch all dependencies
2570 * (e.g. if making local a parent object before its child...). See #48907. */
2571 /* TODO: This is now the biggest step by far (in term of processing time).
2572 * We may be able to gain here by using again main->relations mapping, but...
2573 * this implies BKE_libblock_remap & co to be able to update main->relations on the fly.
2574 * Have to think about it a bit more, and see whether new code is OK first, anyway. */
2575 for (LinkNode *it = copied_ids; it; it = it->next) {
2576 ID *id = static_cast<ID *>(it->link);
2577
2578 BLI_assert(id->newid != nullptr);
2580
2582 if (old_to_new_ids) {
2583 BLI_ghash_insert(old_to_new_ids, id, id->newid);
2584 }
2585
2586 /* Special hack for groups... Thing is, since we can't instantiate them here, we need to
2587 * ensure they remain 'alive' (only instantiation is a real group 'user'... *sigh* See
2588 * #49722). */
2589 if (id->id_type() == ID_GR && (id->tag & ID_TAG_INDIRECT) != 0) {
2590 id_us_ensure_real(id->newid);
2591 }
2592 }
2593
2594 /* Making some liboverride local may have had some impact on validity of liboverrides hierarchy
2595 * roots, these need to be re-validated/re-generated. */
2597
2598#ifdef DEBUG_TIME
2599 printf("Step 4: Remap local usages of old (linked) ID to new (local) ID: Done.\n");
2600 TIMEIT_VALUE_PRINT(make_local);
2601#endif
2602
2603 /* This is probably more of a hack than something we should do here, but...
2604 * Issue is, the whole copying + remapping done in complex cases above may leave pose-channels
2605 * of armatures in complete invalid state (more precisely, the bone pointers of the
2606 * pose-channels - very crappy cross-data-blocks relationship), so we tag it to be fully
2607 * recomputed, but this does not seems to be enough in some cases, and evaluation code ends up
2608 * trying to evaluate a not-yet-updated armature object's deformations.
2609 * Try "make all local" in 04_01_H.lighting.blend from Agent327 without this, e.g. */
2610 for (Object *ob = bmain->objects.first(); ob; ob = static_cast<Object *>(ob->id.next)) {
2611 if (ob->data != nullptr && ob->type == OB_ARMATURE && ob->pose != nullptr &&
2612 ob->pose->flag & POSE_RECALC)
2613 {
2614 BKE_pose_rebuild(bmain, ob, id_cast<bArmature *>(ob->data), true);
2615 }
2616 }
2617
2618#ifdef DEBUG_TIME
2619 printf("Hack: Forcefully rebuild armature object poses: Done.\n");
2620 TIMEIT_VALUE_PRINT(make_local);
2621#endif
2622
2624 BLI_memarena_free(linklist_mem);
2625
2626#ifdef DEBUG_TIME
2627 printf("Cleanup and finish: Done.\n");
2628 TIMEIT_END(make_local);
2629#endif
2630}
2631
2633 ID &id,
2634 StringRefNull name,
2635 const IDNewNameMode mode)
2636{
2637 BLI_assert(BKE_id_is_in_main(&bmain, &id));
2638
2639 if (STREQ(BKE_id_name(id), name.c_str())) {
2640 return {IDNewNameResult::Action::UNCHANGED, nullptr};
2641 }
2642 BKE_main_namemap_remove_id(bmain, id);
2643 ListBaseT<ID> &lb = *which_libbase(&bmain, id.id_type());
2644 IDNewNameResult result = BKE_id_new_name_validate(bmain, lb, id, name.c_str(), mode, true);
2645 if (!ELEM(result.action,
2648 {
2649 bmain.is_memfile_undo_written = false;
2650 }
2651 return result;
2652}
2653
2655{
2656 const IDNewNameResult result = BKE_libblock_rename(bmain, id, name, mode);
2657
2658 auto deg_tag_id = [](ID &id) -> void {
2660 switch (id.id_type()) {
2661 case ID_OB: {
2662 Object &ob = reinterpret_cast<Object &>(id);
2663 if (ob.type == OB_MBALL) {
2665 }
2666 break;
2667 }
2668 default:
2669 break;
2670 }
2671 };
2672
2673 switch (result.action) {
2676 break;
2679 deg_tag_id(id);
2680 break;
2682 BLI_assert(result.other_id);
2683 deg_tag_id(*result.other_id);
2684 deg_tag_id(id);
2685 break;
2686 }
2687
2688 return result;
2689}
2690
2691void BKE_id_full_name_get(char name[MAX_ID_FULL_NAME], const ID *id, char separator_char)
2692{
2694
2695 if (ID_IS_LINKED(id)) {
2696 const size_t idname_len = strlen(BKE_id_name(*id));
2697 const size_t libname_len = strlen(BKE_id_name(id->lib->id));
2698
2699 name[idname_len] = separator_char ? separator_char : ' ';
2700 name[idname_len + 1] = '[';
2702 name + idname_len + 2, BKE_id_name(id->lib->id), MAX_ID_FULL_NAME - (idname_len + 2));
2703 name[idname_len + 2 + libname_len] = ']';
2704 name[idname_len + 2 + libname_len + 1] = '\0';
2705 }
2706}
2707
2709 const ID *id,
2710 const bool add_lib_hint,
2711 char separator_char,
2712 int *r_prefix_len)
2713{
2714 int i = 0;
2715
2716 if (add_lib_hint) {
2717 name[i++] = id->lib ? (ID_MISSING(id) ? 'M' : 'L') : ID_IS_OVERRIDE_LIBRARY(id) ? 'O' : ' ';
2718 }
2719 name[i++] = (id->flag & ID_FLAG_FAKEUSER) ? 'F' : ((id->us == 0) ? '0' : ' ');
2720 name[i++] = ' ';
2721
2722 BKE_id_full_name_get(name + i, id, separator_char);
2723
2724 if (r_prefix_len) {
2725 *r_prefix_len = i;
2726 }
2727}
2728
2730{
2731 if (!ID_IS_LINKED(id)) {
2732 return BLI_strdup(id->name);
2733 }
2734
2735 /* Prefix with an ASCII character in the range of 32..96 (visible)
2736 * this ensures we can't have a library ID pair that collide.
2737 * Where 'LIfooOBbarOBbaz' could be ('LIfoo, OBbarOBbaz') or ('LIfooOBbar', 'OBbaz'). */
2738 const char ascii_len = strlen(BKE_id_name(id->lib->id)) + 32;
2739 return BLI_sprintfN("%c%s%s", ascii_len, id->lib->id.name, id->name);
2740}
2741
2742void BKE_id_tag_set_atomic(ID *id, int tag)
2743{
2744 atomic_fetch_and_or_int32(&id->tag, tag);
2745}
2746
2747void BKE_id_tag_clear_atomic(ID *id, int tag)
2748{
2749 atomic_fetch_and_and_int32(&id->tag, ~tag);
2750}
2751
2752bool BKE_id_is_in_main(Main *bmain, ID *id)
2753{
2754 /* We do not want to fail when id is nullptr here, even though this is a bit strange behavior...
2755 */
2756 return (id == nullptr || BLI_findindex(which_libbase(bmain, id->id_type()), id) != -1);
2757}
2758
2760{
2761 return BKE_id_is_in_main(G_MAIN, id);
2762}
2763
2765{
2766 return ID_IS_EDITABLE(id) && !ID_IS_OVERRIDE_LIBRARY(id) &&
2768}
2769
2770ID *BKE_id_owner_get(ID *id, const bool debug_relationship_assert)
2771{
2772 const IDTypeInfo *idtype = BKE_idtype_get_info_from_id(id);
2773 if (idtype->owner_pointer_get != nullptr) {
2774 ID **owner_id_pointer = idtype->owner_pointer_get(id, debug_relationship_assert);
2775 if (owner_id_pointer != nullptr) {
2776 return *owner_id_pointer;
2777 }
2778 }
2779 return nullptr;
2780}
2781
2782bool BKE_id_is_editable(const Main *bmain, const ID *id)
2783{
2785}
2786
2787bool BKE_id_can_use_id(const ID &id_from, const ID &id_to)
2788{
2789 /* Can't point from linked to local. */
2790 if (id_from.lib && !id_to.lib) {
2791 return false;
2792 }
2793 /* Can't point from ID in main database to one outside of it. */
2794 if (!(id_from.tag & ID_TAG_NO_MAIN) && (id_to.tag & ID_TAG_NO_MAIN)) {
2795 return false;
2796 }
2797
2798 return true;
2799}
2800
2801/************************* Datablock order in UI **************************/
2802
2803static int *id_order_get(ID *id)
2804{
2805 /* Only for workspace tabs currently. */
2806 switch (id->id_type()) {
2807 case ID_WS:
2808 return &(id_cast<WorkSpace *>(id))->order;
2809 default:
2810 return nullptr;
2811 }
2812}
2813
2814static bool id_order_compare(ID *a, ID *b)
2815{
2816 int *order_a = id_order_get(a);
2817 int *order_b = id_order_get(b);
2818
2819 /* In practice either both or neither are set,
2820 * failing to do this would result in a logically invalid sort function, see #137712. */
2821 BLI_assert((order_a && order_b) || (!order_a && !order_b));
2822
2823 if (order_a && order_b) {
2824 if (*order_a < *order_b) {
2825 return true;
2826 }
2827 if (*order_a > *order_b) {
2828 return false;
2829 }
2830 }
2831
2832 return strcmp(a->name, b->name) < 0;
2833}
2834
2835Vector<ID *> BKE_id_ordered_list(const ListBaseT<ID> *lb)
2836{
2837 Vector<ID *> ordered;
2838
2839 for (ID &id : *lb) {
2840 ordered.append(&id);
2841 }
2842
2843 std::ranges::sort(ordered, id_order_compare);
2844
2845 for (const int i : ordered.index_range()) {
2846 if (int *order = id_order_get(ordered[i])) {
2847 *order = i;
2848 }
2849 }
2850
2851 return ordered;
2852}
2853
2854void BKE_id_reorder(const ListBaseT<ID> *lb, ID *id, ID *relative, bool after)
2855{
2856 int *id_order = id_order_get(id);
2857 int relative_order;
2858
2859 if (relative) {
2860 relative_order = *id_order_get(relative);
2861 }
2862 else {
2863 relative_order = (after) ? lb->count() : 0;
2864 }
2865
2866 if (after) {
2867 /* Insert after. */
2868 for (ID &other : *lb) {
2869 int *order = id_order_get(&other);
2870 if (*order > relative_order) {
2871 (*order)++;
2872 }
2873 }
2874
2875 *id_order = relative_order + 1;
2876 }
2877 else {
2878 /* Insert before. */
2879 for (ID &other : *lb) {
2880 int *order = id_order_get(&other);
2881 if (*order < relative_order) {
2882 (*order)--;
2883 }
2884 }
2885
2886 *id_order = relative_order - 1;
2887 }
2888}
2889
2891{
2892 if (id->asset_data) {
2893 BKE_asset_metadata_write(writer, id->asset_data);
2894 }
2895
2896 if (id->library_weak_reference != nullptr) {
2897 writer->write_struct(id->library_weak_reference);
2898 }
2899
2900 /* ID_WM's id->properties are considered runtime only, and never written in .blend file. */
2901 if (id->properties && !ELEM(id->id_type(), ID_WM)) {
2902 IDP_BlendWrite(writer, id->properties);
2903 }
2904 /* ID_WM's id->system_properties are considered runtime only, and never written in .blend file.
2905 */
2906 if (id->system_properties && !ELEM(id->id_type(), ID_WM)) {
2907 IDP_BlendWrite(writer, id->system_properties);
2908 }
2909
2910 BKE_animdata_blend_write(writer, id);
2911
2912 if (id->override_library) {
2913 writer->write_struct(id->override_library);
2914
2915 writer->write_struct_list(&id->override_library->properties);
2916 for (IDOverrideLibraryProperty &op : id->override_library->properties) {
2917 writer->write_string(op.rna_path);
2918
2919 writer->write_struct_list(&op.operations);
2921 if (opop.subitem_reference_name) {
2923 }
2924 if (opop.subitem_local_name) {
2925 writer->write_string(opop.subitem_local_name);
2926 }
2927 if (opop.label) {
2928 writer->write_string(opop.label);
2929 }
2930 if (opop.tooltip) {
2931 writer->write_string(opop.tooltip);
2932 }
2933 }
2934 }
2935 }
2936}
2937
2939 int id;
2940};
2941
2942static_assert(dna::is_ID_v<ID>);
2943static_assert(dna::is_ID_v<Object>);
2944static_assert(!dna::is_ID_v<int>);
2945static_assert(!dna::is_ID_v<ID *>);
2946static_assert(!dna::is_ID_v<const ID>);
2947static_assert(!dna::is_ID_v<ListBase>);
2948static_assert(!dna::is_ID_v<SomeTypeWithIDMember>);
2949
2950} // namespace blender
#define G_MAIN
#define MAIN_ID_SESSION_UID_UNSET
#define MAX_ID_FULL_NAME_UI
#define MAX_ID_FULL_NAME
#define FOREACH_MAIN_ID_END
Definition BKE_main.hh:735
#define FOREACH_MAIN_ID_BEGIN(_bmain, _id)
Definition BKE_main.hh:729
API for Blender-side Rigid Body stuff.
#define FILE_MAXFILE
#define FILE_MAX
#define FILE_MAXDIR
#define STRNCPY_UTF8(dst, src)
Utility defines for timing/benchmarks.
#define TIMEIT_START(var)
#define TIMEIT_VALUE_PRINT(var)
#define TIMEIT_END(var)
#define BLT_I18NCONTEXT_ID_ID
#define DATA_(msgid)
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
ID and Library types, which are fundamental for SDNA.
#define ID_IS_OVERRIDE_LIBRARY_VIRTUAL(_id)
Definition DNA_ID.h:733
#define ID_IS_PACKED(_id)
Definition DNA_ID.h:703
#define ID_FAKE_USERS(id)
Definition DNA_ID.h:658
#define ID_IS_OVERRIDE_LIBRARY_REAL(_id)
Definition DNA_ID.h:729
#define ID_IS_LINKED(_id)
Definition DNA_ID.h:697
#define MAX_ID_NAME
Definition DNA_ID.h:367
#define ID_IS_ASSET(_id)
Definition DNA_ID.h:743
#define ID_TYPE_IS_DEPRECATED(id_type)
Definition DNA_ID.h:755
#define ID_IS_EDITABLE(_id)
Definition DNA_ID.h:708
#define ID_REAL_USERS(id)
Definition DNA_ID.h:679
#define ID_IS_OVERRIDE_LIBRARY(_id)
Definition DNA_ID.h:736
#define ID_NEW_SET(_id, _idn)
Definition DNA_ID.h:757
#define ID_MISSING(_id)
Definition DNA_ID.h:695
#define ID_LINK_PLACEHOLDER
Object groups, one object can be in many groups at once.
#define USER_DEVELOPER_TOOL_TEST(userdef, member)
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
Provides wrapper around system-specific atomic primitives, and some extensions (faked-atomic operatio...
ATOMIC_INLINE int32_t atomic_fetch_and_or_int32(int32_t *p, int32_t x)
ATOMIC_INLINE uint32_t atomic_add_and_fetch_uint32(uint32_t *p, uint32_t x)
ATOMIC_INLINE int32_t atomic_fetch_and_and_int32(int32_t *p, int32_t x)
#define POINTER_FROM_INT(i)
#define UNUSED_VARS_NDEBUG(...)
#define POINTER_AS_INT(i)
#define ELEM(...)
#define BLI_assert_msg(a, msg)
#define STREQ(a, b)
bool contains(const Key &key) const
Definition BLI_set.hh:310
bool add(const Key &key)
Definition BLI_set.hh:248
bool is_empty() const
Definition BLI_set.hh:614
bool remove(const Key &key)
Definition BLI_set.hh:404
constexpr const char * c_str() const
bool add(const Key &key)
int64_t size() const
void append(const T &value)
IndexRange index_range() const
void add(ID *old_id, ID *new_id)
#define offsetof(t, d)
#define ID_SORT_STEP_SIZE
#define LIB_ID_TYPES_NOCOPY
void action_slots_user_cache_invalidate(Main &bmain)
Library * ensure_archive_library(Main &bmain, ID &id, Library &reference_library, const IDHash &id_deep_hash, bool &is_new)
Definition library.cc:542
bNodeTree ** node_tree_ptr_from_id(ID *id)
Definition node.cc:5312
bNodeTree * node_tree_from_id(ID *id)
Definition node.cc:5337
BMesh Iterators.
const char * BKE_idtype_idcode_to_name(short idcode)
Definition idtype.cc:261
void BKE_main_id_flag_listbase(ListBaseT< ID > *lb, int flag, bool value)
Definition lib_id.cc:1323
UserDef U
Definition blender.cc:58
void BKE_main_relations_free(Main *bmain)
Definition main.cc:760
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
int char char int BLI_strcasecmp(const char *s1, const char *s2) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
void BKE_libblock_runtime_reset_remapping_status(ID *id) ATTR_NONNULL(1)
Definition lib_id.cc:1564
std::array< ListBaseT< ID > *, INDEX_ID_MAX - 1 > MainListsArray
Definition BKE_main.hh:797
void id_lib_extern(ID *id, bool enforce_fix=false)
Definition lib_id.cc:328
static int foreach_assign_id_to_orig_callback(LibraryIDLinkCallbackData *cb_data)
Definition lib_id.cc:900
const IDTypeInfo * BKE_idtype_get_info_from_idcode(short id_code)
Definition idtype.cc:227
char * STRNCPY(char(&dst)[N], const char *src)
ID * BKE_libblock_find_name(Main *bmain, short type, const char *name, const std::optional< Library * > lib=std::nullopt) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition lib_id.cc:1808
void BKE_pose_rebuild(Main *bmain, Object *ob, bArmature *arm, bool do_id_user)
void BKE_asset_metadata_write(BlendWriter *writer, AssetMetaData *asset_data)
Definition asset.cc:201
void * BKE_libblock_alloc_in_lib(Main *bmain, std::optional< Library * > owner_library, short type, const char *name, int flag) ATTR_WARN_UNUSED_RESULT
Definition lib_id.cc:1439
MemArena * BLI_memarena_new(size_t bufsize, const char *name) ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL ATTR_NONNULL(2) ATTR_MALLOC
void RNA_property_update(bContext *C, PointerRNA *ptr, PropertyRNA *prop)
void RNA_property_pointer_set(PointerRNA *ptr, PropertyRNA *prop, PointerRNA ptr_value, ReportList *reports)
@ ID_REMAP_TYPE_REMAP
static int libblock_management_us_min(LibraryIDLinkCallbackData *cb_data)
Definition lib_id.cc:1189
unsigned int uint
Definition BKE_text.h:55
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
bool BLI_path_abs(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1
void BLI_addhead(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:98
AssetMetaData * BKE_asset_metadata_copy(const AssetMetaData *source)
Definition asset.cc:42
void id_us_plus(ID *id)
Definition lib_id.cc:433
IDTypeInfo IDType_ID_LINK_PLACEHOLDER
Definition lib_id.cc:89
PointerRNA RNA_id_pointer_create(ID *id)
ID * BKE_libblock_find_session_uid(Main *bmain, short type, uint32_t session_uid)
Definition lib_id.cc:1826
static void id_embedded_swap(Main *bmain, ID **embedded_id_a, ID **embedded_id_b, const bool do_full_id, IDRemapper *remapper_id_a, IDRemapper *remapper_id_b)
Definition lib_id.cc:1097
ID * BKE_libblock_find_name_and_library_filepath(Main *bmain, short type, const char *name, const char *lib_filepath_abs)
Definition lib_id.cc:1865
void BKE_main_id_flag_all(Main *bmain, int flag, bool value)
Definition lib_id.cc:1339
void BKE_id_tag_set_atomic(ID *id, int tag)
Definition lib_id.cc:2742
@ IDWALK_IGNORE_MISSING_OWNER_ID
bool BKE_idtype_idcode_is_linkable(short idcode)
Definition idtype.cc:307
static int * id_order_get(ID *id)
Definition lib_id.cc:2803
void BKE_lib_libblock_session_uid_ensure(ID *id)
Definition lib_id.cc:1575
void BKE_asset_metadata_free(AssetMetaData **asset_data)
Definition asset.cc:36
void IDP_BlendWrite(BlendWriter *writer, const IDProperty *prop)
Definition idprop.cc:1696
void void BKE_libblock_remap(Main *bmain, void *old_idv, void *new_idv, int remap_flags) ATTR_NONNULL(1
Definition lib_remap.cc:701
static void lib_id_library_local_paths(Main *bmain, Library *lib_to, Library *lib_from, ID *id)
Definition lib_id.cc:181
static int libblock_management_us_plus(LibraryIDLinkCallbackData *cb_data)
Definition lib_id.cc:1175
ID * BKE_id_copy_for_use_in_bmain(Main *bmain, const ID *id)
Definition lib_id.cc:925
char * BKE_id_to_unique_string_key(const ID *id)
Definition lib_id.cc:2729
void * BLI_listbase_findafter_string(Link *link, const char *id, int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:645
void BLI_linklist_prepend_arena(LinkNode **listp, void *ptr, struct MemArena *ma) ATTR_NONNULL(1
char * BLI_sprintfN(const char *__restrict format,...) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC ATTR_PRINTF_FORMAT(1
void BKE_libblock_management_main_remove(Main *bmain, void *idv)
Definition lib_id.cc:1246
void * BLI_findstring(const ListBase *listbase, const char *id, int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:615
ID * BKE_id_copy_for_duplicate(Main *bmain, ID *id, eDupli_ID_Flags duplicate_flags, int copy_flags)
Definition lib_id.cc:861
ID * BKE_libblock_alloc_notest(short type) ATTR_WARN_UNUSED_RESULT
Definition lib_id.cc:1427
bool BKE_main_namemap_get_unique_name(Main &bmain, ID &id, char *r_name)
ID * BKE_libblock_find_name_and_library(Main *bmain, short type, const char *name, const char *lib_name)
Definition lib_id.cc:1850
static int id_indirect_linked_update_fn(LibraryIDLinkCallbackData *cb_data)
Definition lib_id.cc:2163
void lib_id_copy_ensure_local(Main *bmain, const ID *old_id, ID *new_id, const int flags)
Definition lib_id.cc:562
AnimData * BKE_animdata_copy_in_lib(Main *bmain, std::optional< Library * > owner_library, AnimData *adt, int flag)
Definition anim_data.cc:263
@ ID_REMAP_SKIP_INDIRECT_USAGE
MainListsArray BKE_main_lists_get(Main &bmain)
Definition main.cc:1158
bool id_can_have_animdata(const ID *id)
Definition anim_data.cc:78
void BKE_main_id_repair_duplicate_names_listbase(Main *bmain, ListBaseT< ID > *lb)
Definition lib_id.cc:1348
const char * BKE_id_name(const ID &id)
bool BKE_lib_override_library_is_system_defined(const Main *bmain, const ID *id)
void BKE_animdata_duplicate_id_action(Main *bmain, ID *id, uint duplicate_flags)
Definition anim_data.cc:434
void id_fake_user_set(ID *id)
Definition lib_id.cc:471
void BKE_animdata_blend_write(BlendWriter *writer, ID *id)
void BKE_id_blend_write(BlendWriter *writer, ID *id)
Definition lib_id.cc:2890
void id_us_clear_real(ID *id)
Definition lib_id.cc:406
void BKE_libblock_copy_in_lib(Main *bmain, std::optional< Library * > owner_library, const ID *id, std::optional< const ID * > new_owner_id, ID **new_id_p, int orig_flag)
Definition lib_id.cc:1633
void BKE_main_id_tag_idcode(Main *mainvar, short type, int tag, bool value)
Definition lib_id.cc:1307
@ LIB_ID_CREATE_NO_USER_REFCOUNT
@ LIB_ID_COPY_NO_LIB_OVERRIDE
@ LIB_ID_COPY_NO_ANIMDATA
@ LIB_ID_CREATE_LOCAL
@ LIB_ID_COPY_SET_COPIED_ON_WRITE
@ LIB_ID_COPY_DEFAULT
@ LIB_ID_CREATE_NO_DEG_TAG
@ LIB_ID_COPY_ACTIONS
@ LIB_ID_CREATE_NO_ALLOCATE
@ LIB_ID_CREATE_NO_MAIN
@ LIB_ID_COPY_ASSET_METADATA
@ LIB_ID_COPY_ID_NEW_SET
void BLI_ghash_insert(GHash *gh, void *key, void *val)
Definition BLI_ghash.cc:711
void BKE_main_namemap_remove_id(Main &bmain, ID &id)
void BKE_main_id_refcount_recompute(Main *bmain, bool do_linked_only)
Definition lib_id.cc:2131
const char * BKE_main_blendfile_path(const Main *bmain) ATTR_NONNULL()
Definition main.cc:1058
void id_lib_indirect_weak_link(ID *id)
Definition lib_id.cc:377
void BKE_lib_id_make_local_generic_action_define(Main *bmain, ID *id, int flags, bool *r_force_local, bool *r_force_copy)
Definition lib_id.cc:577
bool BKE_id_can_use_id(const ID &id_from, const ID &id_to)
Definition lib_id.cc:2787
void id_sort_by_name(ListBaseT< ID > *lb, ID *id, ID *id_sorting_hint)
Definition lib_id.cc:1888
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
static void propagate_status_to_embedded_ids(ID &id)
Definition lib_id.cc:301
void BKE_libblock_copy_ex(Main *bmain, const ID *id, ID **new_id_p, int orig_flag)
Definition lib_id.cc:1792
unsigned int uint32_t
Definition BKE_text.h:81
int BLI_findindex(const ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:593
Key * BKE_key_from_id(ID *id)
Definition key.cc:1291
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
const IDTypeInfo * BKE_idtype_get_info_from_id(const ID *id)
Definition idtype.cc:232
void DEG_id_tag_update_ex(Main *bmain, ID *id, unsigned int flags)
void * BKE_libblock_copy(Main *bmain, const ID *id) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition lib_id.cc:1797
void BKE_id_full_name_ui_prefix_get(char name[MAX_ID_FULL_NAME_UI], const ID *id, bool add_lib_hint, char separator_char, int *r_prefix_len)
Definition lib_id.cc:2708
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
void BKE_libblock_runtime_ensure(ID &id)
Definition lib_id.cc:1403
void BKE_rigidbody_ensure_local_object(struct Main *bmain, struct Object *ob)
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
void DEG_id_tag_update(ID *id, unsigned int flags)
void BKE_lib_override_library_main_hierarchy_root_ensure(Main *bmain, LibOverride_HierarchyRoot_ValidateOptions options={}, ReportList *reports=nullptr)
BMesh const char void * data
void BLI_insertlinkafter(ListBase *listbase, void *vprevlink, void *vnewlink) ATTR_NONNULL(1)
Definition listbase.cc:339
Key ** BKE_key_from_id_p(ID *id)
Definition key.cc:1266
void BLI_linklist_prepend_nlink(LinkNode **listp, void *ptr, LinkNode *nlink) ATTR_NONNULL(1
void id_lib_indirect(ID *id, bool enforce_fix=false)
Definition lib_id.cc:354
void DEG_id_type_tag(Main *bmain, short id_type)
void BKE_lib_id_clear_library_data(Main *bmain, ID *id, int flags)
Definition lib_id.cc:210
void BKE_lib_id_make_local_generic(Main *bmain, ID *id, int flags)
Definition lib_id.cc:619
static void id_swap(Main *bmain, ID *id_a, ID *id_b, const bool do_full_id, const bool do_self_remap, IDRemapper *input_remapper_id_a, IDRemapper *input_remapper_id_b, const int self_remap_flags)
Definition lib_id.cc:987
void BKE_lib_override_library_make_local(Main *bmain, ID *id)
void BKE_id_newptr_and_tag_clear(ID *id)
Definition lib_id.cc:487
bool void BLI_path_rel(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1)
void BKE_lib_override_library_copy(ID *dst_id, const ID *src_id, bool do_full_copy)
void BKE_libblock_init_empty(ID *id) ATTR_NONNULL(1)
Definition lib_id.cc:1550
static bool lib_id_library_local_paths_callback(BPathForeachPathData *bpath_data, char *path_dst, size_t path_dst_maxncpy, const char *path_src)
Definition lib_id.cc:131
int BLI_path_normalize(char *path) ATTR_NONNULL(1)
void BKE_bpath_foreach_path_id(BPathForeachPathData *bpath_data, ID *id)
Definition bpath.cc:77
@ LIBRARY_FLAG_IS_ARCHIVE
Vector< ID * > BKE_id_ordered_list(const ListBaseT< ID > *lb)
Definition lib_id.cc:2835
bool BKE_id_is_in_main(Main *bmain, ID *id)
Definition lib_id.cc:2752
void BKE_main_relations_create(Main *bmain, short flag)
Definition main.cc:723
void BKE_main_id_tag_all(Main *mainvar, int tag, bool value)
Definition lib_id.cc:1314
void BLI_insertlinkbefore(ListBase *listbase, void *vnextlink, void *vnewlink) ATTR_NONNULL(1)
Definition listbase.cc:378
static int id_copy_libmanagement_cb(LibraryIDLinkCallbackData *cb_data)
Definition lib_id.cc:708
bool id_single_user(bContext *C, ID *id, PointerRNA *ptr, PropertyRNA *prop)
Definition lib_id.cc:1142
static bool id_order_compare(ID *a, ID *b)
Definition lib_id.cc:2814
void BKE_lib_id_expand_local(Main *bmain, ID *id, int flags)
Definition lib_id.cc:556
void * BKE_id_new_nomain(short type, const char *name)
Definition lib_id.cc:1617
void BKE_libblock_management_usercounts_set(Main *bmain, void *idv)
Definition lib_id.cc:1266
void BKE_main_id_newptr_and_tag_clear(Main *bmain)
Definition lib_id.cc:2097
void id_us_ensure_real(ID *id)
Definition lib_id.cc:388
@ BKE_BPATH_FOREACH_PATH_SKIP_MULTIFILE
Definition BKE_bpath.hh:76
void BKE_lib_id_swap(Main *bmain, ID *id_a, ID *id_b, const bool do_self_remap, const int self_remap_flags)
Definition lib_id.cc:1130
IDNewNameResult BKE_libblock_rename(Main &bmain, ID &id, StringRefNull name, const IDNewNameMode mode=IDNewNameMode::RenameExistingNever)
Definition lib_id.cc:2632
void BKE_id_reorder(const ListBaseT< ID > *lb, ID *id, ID *relative, bool after)
Definition lib_id.cc:2854
T * DEG_get_original(T *id)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
Main * CTX_data_main(const bContext *C)
@ IDWALK_RET_STOP_ITER
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC
@ INDEX_ID_NULL
Definition DNA_ID.h:1359
void BKE_lib_id_swap_full(Main *bmain, ID *id_a, ID *id_b, const bool do_self_remap, const int self_remap_flags)
Definition lib_id.cc:1136
static int lib_id_clear_library_data_users_update_cb(LibraryIDLinkCallbackData *cb_data)
Definition lib_id.cc:196
bool BLI_path_is_rel(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
static void library_make_local_copying_check(ID *id, Set< ID * > &loop_tags, MainIDRelations *id_relations, Set< ID * > &done_ids)
Definition lib_id.cc:2334
void BKE_main_unlock(Main *bmain)
Definition main.cc:666
void BKE_lib_libblock_session_uid_renew(ID *id)
Definition lib_id.cc:1588
@ ID_RECALC_ANIMATION
Definition DNA_ID.h:1077
@ ID_RECALC_TAG_FOR_UNDO
Definition DNA_ID.h:1152
@ ID_RECALC_TRANSFORM
Definition DNA_ID.h:1054
@ ID_RECALC_SYNC_TO_EVAL
Definition DNA_ID.h:1118
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
ID_Type GS(const char *name)
Definition DNA_ID.h:410
static int id_refcount_recompute_callback(LibraryIDLinkCallbackData *cb_data)
Definition lib_id.cc:2107
@ ID_TAG_NO_MAIN
Definition DNA_ID.h:981
@ 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_TEMP_MAIN
Definition DNA_ID.h:974
@ ID_TAG_RUNTIME
Definition DNA_ID.h:862
@ ID_TAG_COPIED_ON_EVAL
Definition DNA_ID.h:1000
@ ID_TAG_LOCALIZED
Definition DNA_ID.h:990
@ ID_TAG_PRE_EXISTING
Definition DNA_ID.h:929
@ ID_TAG_EXTRAUSER
Definition DNA_ID.h:881
@ ID_TAG_EXTERN
Definition DNA_ID.h:845
@ ID_TAG_NOT_ALLOCATED
Definition DNA_ID.h:1018
@ ID_TAG_DOIT
Definition DNA_ID.h:1029
@ ID_TAG_NEW
Definition DNA_ID.h:922
void BKE_id_tag_clear_atomic(ID *id, int tag)
Definition lib_id.cc:2747
void BKE_main_id_tag_listbase(ListBaseT< ID > *lb, int tag, bool value)
Definition lib_id.cc:1291
static uint global_session_uid
Definition lib_id.cc:1573
bool RNA_property_editable(const PointerRNA *ptr, PropertyRNA *prop)
static int lib_id_expand_local_cb(LibraryIDLinkCallbackData *cb_data)
Definition lib_id.cc:517
static CLG_LogRef LOG
Definition logging.cpp:12
void BKE_main_lock(Main *bmain)
Definition main.cc:661
void BKE_id_move_to_same_lib(Main &bmain, ID &id, const ID &owner_id)
Definition lib_id.cc:953
bool BKE_id_copy_is_allowed(const ID *id)
Definition lib_id.cc:740
void BKE_libblock_management_main_add(Main *bmain, void *idv)
Definition lib_id.cc:1201
void BKE_main_lib_objects_recalc_all(Main *bmain)
Definition lib_id.cc:1379
void BKE_id_full_name_get(char name[MAX_ID_FULL_NAME], const ID *id, char separator_char)
Definition lib_id.cc:2691
ID * BKE_id_owner_get(ID *id, const bool debug_relationship_assert=true)
Definition lib_id.cc:2770
void DEG_relations_tag_update(Main *bmain)
void * BKE_id_new_in_lib(Main *bmain, std::optional< Library * > owner_library, short type, const char *name)
Definition lib_id.cc:1594
IDNewNameResult BKE_id_rename(Main &bmain, ID &id, StringRefNull name, const IDNewNameMode mode=IDNewNameMode::RenameExistingNever)
Definition lib_id.cc:2654
@ ID_FLAG_INDIRECT_WEAK_LINK
Definition DNA_ID.h:781
@ ID_FLAG_FAKEUSER
Definition DNA_ID.h:770
@ ID_FLAG_EMBEDDED_DATA
Definition DNA_ID.h:775
@ ID_FLAG_LINKED_AND_PACKED
Definition DNA_ID.h:806
@ ID_FLAG_EMBEDDED_DATA_LIB_OVERRIDE
Definition DNA_ID.h:786
@ LIB_ID_MAKELOCAL_LIBOVERRIDE_CLEAR
@ LIB_ID_MAKELOCAL_FULL_LIBRARY
@ LIB_ID_MAKELOCAL_FORCE_LOCAL
@ LIB_ID_MAKELOCAL_ASSET_DATA_CLEAR
@ LIB_ID_MAKELOCAL_INDIRECT
@ LIB_ID_MAKELOCAL_FORCE_COPY
size_t BLI_string_split_name_number(const char *name, char delim, char *r_name_left, int *r_number) ATTR_NONNULL(1
ID * BKE_id_copy_ex(Main *bmain, const ID *id, ID **new_id_p, int flag)
Definition lib_id.cc:851
LibraryForeachIDCallbackFlag
@ IDWALK_CB_LOOPBACK
@ IDWALK_CB_WRITEFILE_IGNORE
@ IDWALK_CB_DIRECT_WEAK_LINK
@ IDWALK_CB_EMBEDDED_NOT_OWNING
@ IDWALK_CB_EMBEDDED
@ IDWALK_CB_INDIRECT_USAGE
void BKE_main_id_indirect_linked_update(Main &bmain, std::optional< Span< ID * > > local_ids=std::nullopt)
Definition lib_id.cc:2211
bool BKE_lib_id_make_local(Main *bmain, ID *id, int flags)
Definition lib_id.cc:672
IDProperty * IDP_CopyProperty_ex(const IDProperty *prop, int flag) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition idprop.cc:968
bool DEG_is_evaluated(const T *id)
bool BKE_id_is_editable(const Main *bmain, const ID *id)
Definition lib_id.cc:2782
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
ID * BKE_id_copy(Main *bmain, const ID *id)
Definition lib_id.cc:856
bool BKE_id_is_in_global_main(ID *id)
Definition lib_id.cc:2759
void id_us_plus_no_lib(ID *id)
Definition lib_id.cc:417
void id_us_min(ID *id)
Definition lib_id.cc:441
void id_fake_user_clear(ID *id)
Definition lib_id.cc:479
void BKE_library_foreach_ID_link(Main *bmain, ID *id, FunctionRef< LibraryIDLinkCallback > callback, void *user_data, LibraryForeachIDFlag flag)
Definition lib_query.cc:449
void * BKE_id_new(Main *bmain, short type, const char *name)
Definition lib_id.cc:1612
size_t BKE_libblock_get_alloc_info(short type, const char **r_name)
Definition lib_id.cc:1410
void * BKE_libblock_alloc(Main *bmain, short type, const char *name, int flag) ATTR_WARN_UNUSED_RESULT
Definition lib_id.cc:1545
struct ID * BKE_id_copy_in_lib(Main *bmain, std::optional< Library * > owner_library, const ID *id, std::optional< const ID * > new_owner_id, ID **new_id_p, int flag)
Definition lib_id.cc:749
bool BKE_id_can_be_asset(const ID *id)
Definition lib_id.cc:2764
int BLI_str_utf8_invalid_strip(char *str, size_t str_len) ATTR_NONNULL(1)
void BKE_library_make_local(Main *bmain, const Library *lib, GHash *old_to_new_ids, bool untagged_only, bool set_fake, bool clear_asset_data)
Definition lib_id.cc:2395
void BLI_memarena_free(MemArena *ma) ATTR_NONNULL(1)
IDNewNameResult BKE_id_new_name_validate(Main &bmain, ListBaseT< ID > &lb, ID &id, const char *newname, IDNewNameMode mode, bool do_linked_data)
Definition lib_id.cc:1999
ListBaseT< ID > * which_libbase(Main *bmain, short type)
Definition main.cc:1073
void BKE_libblock_management_usercounts_clear(Main *bmain, void *idv)
Definition lib_id.cc:1278
@ IDTYPE_FLAGS_NO_COPY
Definition BKE_idtype.hh:36
@ IDTYPE_FLAGS_NO_ANIMDATA
Definition BKE_idtype.hh:52
@ IDTYPE_FLAGS_NO_LIBLINKING
Definition BKE_idtype.hh:38
@ IDTYPE_FLAGS_NEVER_UNUSED
Definition BKE_idtype.hh:73
OnClearAssetDataFn on_clear_asset_fn
Definition BKE_asset.hh:43
BPathForeachPathFunctionCallback callback_function
Definition BKE_bpath.hh:124
void write_string(const char *data)
void write_struct_list(const ListBaseT< T > *list, BlendStructWriterTypedFn< T > fn=nullptr)
void write_struct(const T *data, BlendStructWriterTypedFn< T > fn=nullptr)
ListBaseT< IDOverrideLibraryPropertyOperation > operations
Definition DNA_ID.h:326
IDTypeEmbeddedOwnerPointerGetFunction owner_pointer_get
IDTypeMakeLocalFunction make_local
AssetTypeInfo * asset_type_info
IDTypeInitDataFunction init_data
IDTypeCopyDataFunction copy_data
const char * name
char name[258]
Definition DNA_ID.h:433
unsigned int session_uid
Definition DNA_ID.h:463
struct ID * newid
Definition DNA_ID.h:419
struct LibraryWeakReference * library_weak_reference
Definition DNA_ID.h:529
struct Library * lib
Definition DNA_ID.h:421
IDProperty * system_properties
Definition DNA_ID.h:490
void * prev
Definition DNA_ID.h:418
short flag
Definition DNA_ID.h:439
struct AssetMetaData * asset_data
Definition DNA_ID.h:424
void * next
Definition DNA_ID.h:418
IDProperty * properties
Definition DNA_ID.h:481
unsigned int recalc
Definition DNA_ID.h:446
IDHash deep_hash
Definition DNA_ID.h:475
LibraryForeachIDCallbackFlag cb_flag
bke::library::LibraryRuntime * runtime
Definition DNA_ID.h:591
struct LinkNode * next
MainIDRelationsEntryItem * next
Definition BKE_main.hh:139
MainIDRelationsEntryItem * from_ids
Definition BKE_main.hh:156
Map< const ID *, MainIDRelationsEntry * > * relations_from_pointers
Definition BKE_main.hh:211
Library * curlib
Definition BKE_main.hh:366
MainIDRelations * relations
Definition BKE_main.hh:437
bool is_locked_for_linking
Definition BKE_main.hh:313
ListBaseT< Object > objects
Definition BKE_main.hh:388
bool is_memfile_undo_written
Definition BKE_main.hh:297
struct Collection * master_collection
#define N_(msgid)