Blender V5.3
library.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
10
11#include <optional>
12
13#include "CLG_log.h"
14
15/* all types are needed here, in order to do memory operations */
16#include "DNA_ID.h"
18#include "DNA_scene_types.h"
19
20#include "BLI_utildefines.hh"
21
22#include "BLI_ghash.hh"
23#include "BLI_listbase.hh"
24#include "BLI_path_utils.hh"
25#include "BLI_set.hh"
26#include "BLI_string.hh"
27#include "BLI_vector_set.hh"
28
29#include "BLT_translation.hh"
30
31#include "BLO_read_write.hh"
32
33#include "BKE_bpath.hh"
34#include "BKE_id_hash.hh"
35#include "BKE_idtype.hh"
36#include "BKE_key.hh"
37#include "BKE_lib_id.hh"
38#include "BKE_lib_query.hh"
39#include "BKE_lib_remap.hh"
40#include "BKE_library.hh"
41#include "BKE_main.hh"
43#include "BKE_main_namemap.hh"
44#include "BKE_node.hh"
45#include "BKE_packedFile.hh"
46#include "BKE_report.hh"
47
48namespace blender {
49
50struct BlendDataReader;
51
52static CLG_LogRef LOG = {"lib.library"};
53
54using namespace blender::bke;
55using namespace blender::bke::library;
56
61
62static void library_init_data(ID *id)
63{
64 Library *library = reinterpret_cast<Library *>(id);
65 library->runtime = MEM_new<LibraryRuntime>(__func__);
66}
67
68static void library_free_data(ID *id)
69{
70 Library *library = id_cast<Library *>(id);
72 MEM_delete(library->runtime);
73 if (library->packedfile) {
74 BKE_packedfile_free(library->packedfile);
75 }
76}
77
78static void library_copy_data(Main *bmain,
79 std::optional<Library *> owner_library,
80 ID *id_dst,
81 const ID *id_src,
82 int /*flag*/)
83{
84 /* Libraries are always local IDs. */
85 BLI_assert(!owner_library || *owner_library == nullptr);
86 UNUSED_VARS_NDEBUG(bmain, owner_library);
87
88 const Library *library_src = reinterpret_cast<const Library *>(id_src);
89
90 /* Libraries are copyable now, but there should still be only one library ID for each linked
91 * blendfile (based on absolute filepath). */
92 BLI_assert(!bmain ||
93 !search_filepath_abs(&bmain->libraries, library_src->runtime->filepath_abs));
94
95 Library *library_dst = reinterpret_cast<Library *>(id_dst);
96 if (library_src->packedfile) {
97 library_dst->packedfile = BKE_packedfile_duplicate(library_src->packedfile);
98 }
99
100 /* Only explicitly copy a sub-set of the runtime data. */
101 library_dst->runtime = MEM_new<LibraryRuntime>(__func__);
102 BLI_strncpy(library_dst->runtime->filepath_abs,
103 library_src->runtime->filepath_abs,
104 sizeof(library_dst->runtime->filepath_abs));
105 library_dst->runtime->parent = library_src->runtime->parent;
106 library_dst->runtime->tag = library_src->runtime->tag;
107 library_dst->runtime->versionfile = library_src->runtime->versionfile;
108 library_dst->runtime->subversionfile = library_src->runtime->subversionfile;
109 library_dst->runtime->colorspace = library_src->runtime->colorspace;
110}
111
113{
114 Library *lib = id_cast<Library *>(id);
117 data,
118 lib->runtime->parent,
119 /* The Library::runtime->parent pointer is strictly runtime informative data currently. */
121
122 if (lib->flag & LIBRARY_FLAG_IS_ARCHIVE) {
123 /* Archive library must have a parent, this can't be nullptr. */
124 if (lib->archive_parent_library) {
127 }
128
129 /* Archive libraries should never 'own' other archives. */
130 BLI_assert(lib->runtime->archived_libraries.is_empty());
131 if (foreach_flag & IDWALK_DO_INTERNAL_RUNTIME_POINTERS) {
132 for (Library *&lib_p : lib->runtime->archived_libraries) {
135 }
136 }
137 }
138 else {
139 /* Regular libraries should never have an archive parent. */
142
143 if (foreach_flag & IDWALK_DO_INTERNAL_RUNTIME_POINTERS) {
144 for (Library *&lib_p : lib->runtime->archived_libraries) {
147 }
148 }
149 }
150}
151
152static void library_foreach_path(ID *id, BPathForeachPathData *bpath_data)
153{
154 Library *lib = id_cast<Library *>(id);
155
156 /* FIXME: Find if we should respect #BKE_BPATH_FOREACH_PATH_SKIP_PACKED here, and if not, explain
157 * why. */
158 if (lib->packedfile !=
159 nullptr /* `&& (bpath_data->flag & BKE_BPATH_FOREACH_PATH_SKIP_PACKED) != 0` */)
160 {
161 return;
162 }
163
164 if (BKE_bpath_foreach_path_fixed_process(bpath_data, lib->filepath, sizeof(lib->filepath))) {
165 BKE_library_filepath_set(bpath_data->bmain, lib, lib->filepath);
166 }
167}
168
169static void library_blend_write_data(BlendWriter *writer, ID *id, const void *id_address)
170{
171 Library *library = reinterpret_cast<Library *>(id);
172 const bool is_undo = writer->is_undo();
173
174 /* Runtime tags need to be preserved across undo steps. */
175 if (is_undo) {
176 library->undo_runtime_tag = library->runtime->tag;
177 }
178 else {
179 library->undo_runtime_tag = 0;
180 }
181
182 /* Clear runtime data. */
183 library->runtime = nullptr;
184
185 writer->write_id_struct(id_address, library);
186 BKE_id_blend_write(writer, id);
187
188 /* Write packed file if necessary. */
189 if (library->packedfile) {
190 BKE_packedfile_blend_write(writer, library->packedfile);
191 if (!is_undo) {
192 CLOG_DEBUG(&LOG, "Write packed .blend: %s", library->filepath);
193 }
194 }
195}
196
198{
199 Library *lib = reinterpret_cast<Library *>(id);
200 const bool is_undo = BLO_read_data_is_undo(reader);
201
202 lib->runtime = MEM_new<LibraryRuntime>(__func__);
203 if (is_undo) {
204 lib->runtime->tag = lib->undo_runtime_tag;
205 lib->undo_runtime_tag = 0;
206 }
207}
208
210{
211 Library *lib = reinterpret_cast<Library *>(id);
214 }
215}
216
218 .id_code = Library::id_type,
219 .id_filter = FILTER_ID_LI,
220 .dependencies_id_types = FILTER_ID_LI,
221 .main_listbase_index = INDEX_ID_LI,
222 .struct_size = sizeof(Library),
223 .name = "Library",
224 .name_plural = N_("libraries"),
225 .translation_context = BLT_I18NCONTEXT_ID_LIBRARY,
227 .asset_type_info = nullptr,
228
229 .init_data = library_init_data,
230 .copy_data = library_copy_data,
231 .free_data = library_free_data,
232 .make_local = nullptr,
233 .foreach_id = library_foreach_id,
234 .foreach_cache = nullptr,
235 .foreach_path = library_foreach_path,
236 .foreach_working_space_color = nullptr,
237 .foreach_asset_weak_reference = nullptr,
238 .owner_pointer_get = nullptr,
239
240 .blend_write = library_blend_write_data,
241 .blend_read_data = library_blend_read_data,
242 .blend_read_after_liblink = library_blend_read_after_liblink,
243
244 .blend_read_undo_preserve = nullptr,
245
246 .lib_override_apply_post = nullptr,
247};
248
249void BKE_library_filepath_set(Main *bmain, Library *lib, const char *filepath)
250{
251 /* in some cases this is used to update the absolute path from the
252 * relative */
253 if (lib->filepath != filepath) {
254 STRNCPY(lib->filepath, filepath);
255 }
256
257 STRNCPY(lib->runtime->filepath_abs, filepath);
258
259 /* Not essential but set `filepath_abs` is an absolute copy of value which
260 * is more useful if its kept in sync. */
262 /* NOTE(@ideasman42): the file may be unsaved, in this case, setting the
263 * `filepath_abs` on an indirectly linked path is not allowed from the
264 * outliner, and its not really supported but allow from here for now
265 * since making local could cause this to be directly linked.
266 */
267 /* Never make paths relative to parent lib - reading code (blenloader) always set *all*
268 * `lib->filepath` relative to current main, not to their parent for indirectly linked ones. */
269 const char *blendfile_path = BKE_main_blendfile_path(bmain);
270 BLI_path_abs(lib->runtime->filepath_abs, blendfile_path);
271 }
272}
273
275 Set<Library *> &directly_used_libs,
276 Set<Library *> &libs_in_hierarchy,
277 Library *lib)
278{
279 BLI_assert(!directly_used_libs.contains(lib));
280
281 /* Avoid infinite recursion in dependency loops between libraries (note: might be 'fake
282 * positives' caused only by the library `parent` pointers themselves). */
283 if (libs_in_hierarchy.contains(lib)) {
284 return;
285 }
286 libs_in_hierarchy.add(lib);
287
288 Library *best_parent_lib = nullptr;
289 bool do_break = false;
290 ListBaseT<ID> *lb;
291 ID *id_iter;
292 FOREACH_MAIN_LISTBASE_BEGIN (bmain, lb) {
293 FOREACH_MAIN_LISTBASE_ID_BEGIN (lb, id_iter) {
294 if (!ID_IS_LINKED(id_iter) || id_iter->lib != lib) {
295 continue;
296 }
297 if (ID_IS_PACKED(id_iter)) {
298 /* Packed data and their archive libraries do not participate to linked data hierarchy. */
300 continue;
301 }
302 MainIDRelationsEntry *entry = bmain->relations->relations_from_pointers->lookup(id_iter);
303 for (MainIDRelationsEntryItem *item = entry->from_ids; item; item = item->next) {
304 ID *from_id = item->id_pointer.from;
305 if (!ID_IS_LINKED(from_id)) {
307 continue;
308 }
309 if (ID_IS_PACKED(from_id)) {
310 continue;
311 }
312 Library *from_id_lib = from_id->lib;
313 BLI_assert((from_id_lib->flag & LIBRARY_FLAG_IS_ARCHIVE) == 0);
314 if (from_id_lib == lib) {
315 continue;
316 }
317 if (directly_used_libs.contains(from_id_lib)) {
318 /* Found the first best possible candidate, no need to search further. */
319 BLI_assert(best_parent_lib == nullptr || best_parent_lib->runtime->temp_index > 0);
320 best_parent_lib = from_id_lib;
321 do_break = true;
322 break;
323 }
324 if (!from_id_lib->runtime->parent) {
326 bmain, directly_used_libs, libs_in_hierarchy, from_id_lib);
327 }
328 if (!best_parent_lib ||
329 best_parent_lib->runtime->temp_index > from_id_lib->runtime->temp_index)
330 {
331 best_parent_lib = from_id_lib;
332 if (best_parent_lib->runtime->temp_index == 0) {
333 /* Found the first best possible candidate, no need to search further. */
334 if (!directly_used_libs.contains(best_parent_lib)) {
335 /* This can happen when deleting a library: some of its dependencies may not have any
336 * local usages anymore, creating an archipelago of unused indirectly linked
337 * libraries and their linked IDs. */
338 CLOG_INFO(&LOG,
339 "Library '%s' seems to be part of an archipelago of unused libraries now.",
340 best_parent_lib->runtime->filepath_abs);
341 best_parent_lib->runtime->temp_index = 1;
342 }
343 do_break = true;
344 break;
345 }
346 }
347 }
348 if (do_break) {
349 break;
350 }
351 }
353 if (do_break) {
354 break;
355 }
356 }
358
359 /* NOTE: It may happen that no parent library is found, e.g. if after deleting a directly used
360 * library, its indirect dependency is still around, but none of its linked IDs are used by local
361 * data. */
362 if (best_parent_lib) {
363 lib->runtime->parent = best_parent_lib;
364 lib->runtime->temp_index = best_parent_lib->runtime->temp_index + 1;
365 }
366 else {
367 lib->runtime->parent = nullptr;
368 lib->runtime->temp_index = 0;
369 directly_used_libs.add(lib);
370 }
371
372 libs_in_hierarchy.remove(lib);
373}
374
376{
378
379 /* Reset all values, they may have been set to irrelevant values by other processes (like the
380 * liboverride handling e.g., see #lib_override_libraries_index_define). */
381 for (Library &lib_iter : bmain->libraries) {
382 /* By definition, archive libraries are always contained/owned by a regular library, so they
383 * are never 'root' libraries in the hierarchy. */
384 if (lib_iter.flag & LIBRARY_FLAG_IS_ARCHIVE) {
385 lib_iter.runtime->parent = lib_iter.archive_parent_library;
386 lib_iter.runtime->temp_index = 1;
387 }
388 else {
389 lib_iter.runtime->temp_index = 0;
390 }
391 }
392
393 /* Find all libraries with directly linked IDs (i.e. IDs used by local data). */
394 Set<Library *> directly_used_libs;
395 ID *id_iter;
396 FOREACH_MAIN_ID_BEGIN (bmain, id_iter) {
397 if (!ID_IS_LINKED(id_iter)) {
398 continue;
399 }
400 if (ID_IS_PACKED(id_iter)) {
401 /* Packed data and their archive libraries do not participate to linked data hierarchy. */
402 continue;
403 }
404 if (directly_used_libs.contains(id_iter->lib)) {
405 continue;
406 }
407 MainIDRelationsEntry *entry = bmain->relations->relations_from_pointers->lookup(id_iter);
408 for (MainIDRelationsEntryItem *item = entry->from_ids; item; item = item->next) {
409 if (!ID_IS_LINKED(item->id_pointer.from)) {
410 directly_used_libs.add(id_iter->lib);
411 id_iter->lib->runtime->parent = nullptr;
412 break;
413 }
414 }
415 }
417
418 for (Library &lib_iter : bmain->libraries) {
419 /* A directly used library. */
420 if (directly_used_libs.contains(&lib_iter)) {
421 BLI_assert(lib_iter.runtime->temp_index == 0);
422 continue;
423 }
424
425 /* Assume existing parent is still valid, since it was not cleared in previous loop above.
426 * Just compute 'hierarchy value' in temp index, if needed. */
427 if (lib_iter.runtime->parent) {
428 if (lib_iter.runtime->temp_index > 0) {
429 continue;
430 }
431 if (lib_iter.flag & LIBRARY_FLAG_IS_ARCHIVE) {
432 /* Archive library parent is always their owner regular library, has already been
433 * reset/ensured at the start of this function, so this should never be reached. */
435 continue;
436 }
437 VectorSet<Library *> parent_libraries;
438 for (Library *parent_lib_iter = &lib_iter;
439 parent_lib_iter && parent_lib_iter->runtime->temp_index == 0;
440 parent_lib_iter = parent_lib_iter->runtime->parent)
441 {
442 if (parent_libraries.contains(parent_lib_iter)) {
443 /* Dependency loop between libraries (note: might be 'fake positives' caused only by the
444 * library `parent` pointers themselves). */
445 break;
446 }
447 parent_libraries.add(parent_lib_iter);
448 }
449 Library *most_remote_ancestor_library = parent_libraries.as_span().last();
450 int parent_temp_index =
451 (most_remote_ancestor_library->runtime->parent ?
452 most_remote_ancestor_library->runtime->parent->runtime->temp_index :
453 0) +
454 int(parent_libraries.size()) - 1;
455 for (Library *parent_lib_iter : parent_libraries) {
456 parent_lib_iter->runtime->temp_index = parent_temp_index--;
457 }
458 continue;
459 }
460
461 /* Otherwise, it's an indirectly used library with no known parent, another loop is needed to
462 * ensure all known hierarchy has valid indices when trying to find the best valid parent
463 * library. */
464 }
465
466 /* For all libraries known to be indirect, but without a known parent, find a best valid parent
467 * (i.e. a 'most directly used' library). */
468 for (Library &lib_iter : bmain->libraries) {
469 /* A directly used library. */
470 if (directly_used_libs.contains(&lib_iter)) {
471 BLI_assert(lib_iter.runtime->temp_index == 0);
472 continue;
473 }
474
475 if (lib_iter.runtime->parent) {
476 BLI_assert(lib_iter.runtime->temp_index > 0);
477 }
478 else {
480 "Archived libraries are always direct parent of their owner regular library, "
481 "this should have already been ensured at the start of this function.");
482 BLI_assert(lib_iter.runtime->temp_index == 0);
483 Set<Library *> libs_in_hierarchy;
484 rebuild_hierarchy_best_parent_find(bmain, directly_used_libs, libs_in_hierarchy, &lib_iter);
485 BLI_assert(libs_in_hierarchy.is_empty());
486 }
487 }
488
490}
491
492Library *bke::library::search_filepath_abs(ListBaseT<Library> *libraries, StringRef filepath_abs)
493{
494 for (Library &lib_iter : *libraries) {
495 if (lib_iter.flag & LIBRARY_FLAG_IS_ARCHIVE) {
496 /* Skip archive libraries because there may be multiple of those for the same path and there
497 * should also be a non-archive one. */
498 continue;
499 }
500 if (filepath_abs == lib_iter.runtime->filepath_abs) {
501 return &lib_iter;
502 }
503 }
504 return nullptr;
505}
506
510static Library *add_archive_library(Main &bmain, Library &reference_library)
511{
512 BLI_assert((reference_library.flag & LIBRARY_FLAG_IS_ARCHIVE) == 0);
513 /* Cannot copy libraries using generic ID copying functions, so create the copy manually. */
514 Library *archive_library = static_cast<Library *>(
515 BKE_id_new(&bmain, ID_LI, BKE_id_name(reference_library.id)));
516
517 /* Like in #direct_link_library. */
518 id_us_ensure_real(&archive_library->id);
519
520 archive_library->archive_parent_library = &reference_library;
521 constexpr uint16_t copy_flag = ~LIBRARY_FLAG_IS_ARCHIVE;
522 archive_library->flag = (reference_library.flag & copy_flag) | LIBRARY_FLAG_IS_ARCHIVE;
523 BKE_library_filepath_set(&bmain, archive_library, reference_library.filepath);
524
525 archive_library->runtime->parent = reference_library.runtime->parent;
526 /* Only copy a subset of the reference library tags. E.g. an archive library should never be
527 * considered as writable, so never copy #LIBRARY_ASSET_FILE_WRITABLE. This may need further
528 * tweaking still. */
531 archive_library->runtime->tag = reference_library.runtime->tag & copy_tag;
532 /* By definition, the file version of an archive library containing only packed linked data is
533 * the same as the one of its Main container. */
534 archive_library->runtime->versionfile = bmain.versionfile;
535 archive_library->runtime->subversionfile = bmain.subversionfile;
536
537 reference_library.runtime->archived_libraries.append(archive_library);
538
539 return archive_library;
540}
541
543 Main &bmain, ID &id, Library &reference_library, const IDHash &id_deep_hash, bool &is_new)
544{
546 BLI_assert((reference_library.flag & LIBRARY_FLAG_IS_ARCHIVE) == 0);
547
548 Library *archive_library = nullptr;
549 for (Library *lib_iter : reference_library.runtime->archived_libraries) {
550 BLI_assert((lib_iter->flag & LIBRARY_FLAG_IS_ARCHIVE) != 0);
551 BLI_assert(lib_iter->archive_parent_library != nullptr);
552 BLI_assert(lib_iter->archive_parent_library == &reference_library);
553 /* Check if current archive library already contains an ID of same type and name. */
554 if (BKE_main_namemap_contain_name(bmain, lib_iter, id.id_type(), BKE_id_name(id))) {
555#ifndef NDEBUG
557 &bmain, id.id_type(), BKE_id_name(id), BKE_id_name(lib_iter->id));
559 packed_id && packed_id->deep_hash != id_deep_hash,
560 "An already packed ID with same deep hash as the one to be packed, should have already "
561 "be found and used (deduplication) before reaching this code-path");
562#endif
563 UNUSED_VARS_NDEBUG(id_deep_hash);
564 continue;
565 }
566 archive_library = lib_iter;
567 break;
568 }
569 if (!archive_library) {
570 archive_library = add_archive_library(bmain, reference_library);
571 is_new = true;
572 }
573 else {
574 is_new = false;
575 }
576 BLI_assert(reference_library.runtime->archived_libraries.contains(archive_library));
577 return archive_library;
578}
579
580static void pack_linked_id(Main &bmain,
581 ID *linked_id,
582 const id_hash::ValidDeepHashes &deep_hashes,
583 Map<IDHash, ID *> &already_packed_ids,
584 VectorSet<ID *> &ids_to_remap,
585 bke::id::IDRemapper &id_remapper)
586{
587 const IDHash linked_id_deep_hash = deep_hashes.hashes.lookup(linked_id);
588 ID *packed_id = already_packed_ids.lookup_default(linked_id_deep_hash, nullptr);
589
590 /* It is possible that the `newid` was already set by a previous call to #pack_linked_ids, and
591 * not yet cleared (e.g. when linking & packing several 'root' IDs at the same time, which end up
592 * using the same dependencies).
593 *
594 * Handling it here again ensures consistency in deep hash values etc., and allows to properly
595 * handle its linked-to-packed versions remapping for all of its usages.
596 *
597 * See also #150786. */
598 BLI_assert(ELEM(linked_id->newid, nullptr, packed_id));
599
600 if (packed_id) {
601 /* Exact same ID (and all of its dependencies) have already been linked and packed before,
602 * re-use these packed data. */
603
604 auto existing_id_process = [&deep_hashes, &id_remapper](ID *linked_id, ID *packed_id) {
605 BLI_assert(packed_id);
606 BLI_assert(ID_IS_PACKED(packed_id));
607 /* Note: linked_id and packed_id may have the same deep hash while still coming from
608 * different original libraries. This easily happens copying an asset file such that each
609 * asset exists twice. */
610 BLI_assert(packed_id->deep_hash == deep_hashes.hashes.lookup(linked_id));
611 UNUSED_VARS_NDEBUG(deep_hashes);
612
613 id_remapper.add(linked_id, packed_id);
614 linked_id->newid = packed_id;
615 /* No need to remap this packed ID - otherwise there would be something very wrong in
616 * packed IDs state. */
617 };
618
619 existing_id_process(linked_id, packed_id);
620
621 /* Handle 'fake-embedded' ShapeKeys IDs. */
622 Key *linked_key = BKE_key_from_id(linked_id);
623 if (linked_key) {
624 Key *packed_key = BKE_key_from_id(packed_id);
625 BLI_assert(packed_key);
626 existing_id_process(&linked_key->id, &packed_key->id);
627 }
628 }
629 else {
630 /* This exact version of the ID and its dependencies have not been packed before, creates a
631 * new copy of it and pack it. */
632
633 /* Find an existing archive Library not containing a 'version' of this ID yet (to prevent names
634 * collisions). */
635 bool is_new;
636 Library *archive_lib = ensure_archive_library(
637 bmain, *linked_id, *linked_id->lib, linked_id_deep_hash, is_new);
638
639 auto copied_id_process =
640 [&archive_lib, &deep_hashes, &ids_to_remap, &id_remapper, &already_packed_ids](
641 ID *linked_id, ID *packed_id) {
642 BLI_assert(packed_id);
643 BLI_assert(ID_IS_PACKED(packed_id));
644 BLI_assert(packed_id->lib == archive_lib);
645 UNUSED_VARS_NDEBUG(archive_lib);
646
647 if (packed_id->id_type() == ID_SCE) {
648 /* Like in #scene_blend_read_data. */
649 id_us_ensure_real(packed_id);
650 }
651
652 packed_id->deep_hash = deep_hashes.hashes.lookup(linked_id);
653 id_remapper.add(linked_id, packed_id);
654 ids_to_remap.add(packed_id);
655 already_packed_ids.add(packed_id->deep_hash, packed_id);
656 };
657
658 packed_id = BKE_id_copy_in_lib(&bmain,
659 archive_lib,
660 linked_id,
661 std::nullopt,
662 nullptr,
665 id_us_min(packed_id);
666 copied_id_process(linked_id, packed_id);
667
668 /* Handle 'fake-embedded' ShapeKeys IDs. */
669 Key *linked_key = BKE_key_from_id(linked_id);
670 if (linked_key) {
671 Key *embedded_key = BKE_key_from_id(packed_id);
672 BLI_assert(embedded_key);
673 copied_id_process(&linked_key->id, &embedded_key->id);
674 }
675 }
676}
677
683static void pack_linked_ids(Main &bmain, const Set<ID *> &ids_to_pack)
684{
685 VectorSet<ID *> final_ids_to_pack;
686 VectorSet<ID *> ids_to_remap;
687 bke::id::IDRemapper id_remapper;
688
689 for (ID *id : ids_to_pack) {
691 if (ID_IS_PACKED(id)) {
692 /* Should not happen, but also not critical issue. */
694 "Trying to pack an already packed ID '%s' (from '%s')",
695 id->name,
696 id->lib->runtime->filepath_abs);
697 /* Already packed. */
698 continue;
699 }
700 final_ids_to_pack.add(id);
701 }
702
704 bmain, final_ids_to_pack.as_span());
705 if (const auto *errors = std::get_if<id_hash::DeepHashErrors>(&hash_result)) {
706 for (StringRefNull err_msg : errors->missing_files) {
708 &LOG, "Trying to pack IDs that depend on missing linked library: %s", err_msg.c_str());
709 }
710 for (StringRefNull err_msg : errors->missing_from_files) {
712 &LOG, "Trying to pack IDs that are missing from linked library: %s", err_msg.c_str());
713 }
714 for (StringRefNull err_msg : errors->updated_files) {
715 CLOG_ERROR(&LOG, "Trying to pack IDs that have been modified on disk: %s", err_msg.c_str());
716 }
717 return;
718 }
719 const auto &deep_hashes = std::get<id_hash::ValidDeepHashes>(hash_result);
720
721 Map<IDHash, ID *> already_packed_ids;
722 {
723 ID *id;
724 FOREACH_MAIN_ID_BEGIN (&bmain, id) {
725 if (ID_IS_PACKED(id)) {
726 already_packed_ids.add(id->deep_hash, id);
727 }
728 }
730 }
731
732 for (ID *linked_id : final_ids_to_pack) {
733 pack_linked_id(bmain, linked_id, deep_hashes, already_packed_ids, ids_to_remap, id_remapper);
734 }
735
737 &bmain, ids_to_remap.as_span(), ID_REMAP_TYPE_REMAP, id_remapper, 0);
739}
740
742{
743 BLI_assert(ID_IS_LINKED(&root_id));
744 BLI_assert(!ID_IS_PACKED(&root_id));
745
746 Set<ID *> ids_to_pack;
747 ids_to_pack.add(&root_id);
749 &bmain,
750 &root_id,
751 [&ids_to_pack](LibraryIDLinkCallbackData *cb_data) -> int {
752 if (cb_data->cb_flag & IDWALK_CB_LOOPBACK) {
753 return IDWALK_RET_NOP;
754 }
756 return IDWALK_RET_NOP;
757 }
758
759 ID *self_id = cb_data->self_id;
760 ID *referenced_id = *cb_data->id_pointer;
761 if (!referenced_id) {
762 return IDWALK_RET_NOP;
763 }
764 if (!ID_IS_LINKED(referenced_id)) {
765 CLOG_ERROR(&LOG, "Linked data-block references non-linked data-block");
766 return IDWALK_RET_NOP;
767 }
768 if (ID_IS_PACKED(referenced_id)) {
769 /* A linked ID can use another packed linked ID, as long as it is not from the same
770 * library. */
771 BLI_assert(referenced_id->lib && referenced_id->lib->archive_parent_library);
772 if (referenced_id->lib->archive_parent_library == self_id->lib) {
774 "Non-packed data-block references packed data-block from the same library, "
775 "which is not allowed");
776 }
777 return IDWALK_RET_NOP;
778 }
779 if (referenced_id->id_type() == ID_KE) {
780 /* Shape keys cannot be directly linked, from linking code PoV they behave as embedded
781 * data (i.e. their owning data is responsible to handle them). */
782 return IDWALK_RET_NOP;
783 }
784
785 ids_to_pack.add(referenced_id);
786 return IDWALK_RET_NOP;
787 },
788 nullptr,
790
791 pack_linked_ids(bmain, ids_to_pack);
792 return ids_to_pack;
793}
794
795static Library *add_external_archive_library(Main &bmain, Library &reference_library)
796{
797 BLI_assert((reference_library.flag & LIBRARY_FLAG_IS_EXTERNAL) != 0);
798 /* Cannot copy libraries using generic ID copying functions, so create the copy manually. */
799 Library *external_library = BKE_id_new<Library>(&bmain, BKE_id_name(reference_library.id));
800
801 /* Like in #direct_link_library. */
802 id_us_ensure_real(&external_library->id);
803
804 external_library->archive_parent_library = &reference_library;
806 external_library->flag = (reference_library.flag & copy_flag) |
808 BKE_library_filepath_set(&bmain, external_library, reference_library.filepath);
809
810 external_library->runtime->parent = reference_library.runtime->parent;
811 /* Only copy a subset of the reference library tags. E.g. an archive library should never be
812 * considered as writable, so never copy #LIBRARY_ASSET_FILE_WRITABLE. This may need further
813 * tweaking still. */
816 external_library->runtime->tag = reference_library.runtime->tag & copy_tag;
817
818 reference_library.runtime->archived_libraries.append(external_library);
819
820 return external_library;
821}
822
824{
825 BLI_assert(external_library.flag & LIBRARY_FLAG_IS_EXTERNAL);
826
827 Library *archive_library = add_external_archive_library(bmain, external_library);
828
829 BLI_assert(external_library.runtime->archived_libraries.contains(archive_library));
830 return archive_library;
831}
832
834{
835 for (Library &lib : bmain.libraries) {
836 if (lib.flag & LIBRARY_FLAG_IS_ARCHIVE) {
837 BLI_assert(!lib.runtime || lib.runtime->archived_libraries.is_empty());
838 }
839 else {
840 int i_read_curr = 0;
841 int i_insert_curr = 0;
842 for (; i_read_curr < lib.runtime->archived_libraries.size(); i_read_curr++) {
843 if (!lib.runtime->archived_libraries[i_read_curr]) {
844 continue;
845 }
846 if (i_insert_curr < i_read_curr) {
847 lib.runtime->archived_libraries[i_insert_curr] =
848 lib.runtime->archived_libraries[i_read_curr];
849 }
850 i_insert_curr++;
851 }
852 BLI_assert(i_insert_curr <= i_read_curr);
853 if (i_insert_curr < i_read_curr) {
854 lib.runtime->archived_libraries.resize(i_insert_curr);
855 }
856 }
857 }
858}
859
860} // namespace blender
#define BKE_LIB_FOREACHID_PROCESS_IDSUPER(data_, id_super_, cb_flag_)
#define BKE_LIB_FOREACHID_PROCESS_ID(data_, id_, cb_flag_)
#define FOREACH_MAIN_ID_END
Definition BKE_main.hh:735
#define FOREACH_MAIN_LISTBASE_ID_END
Definition BKE_main.hh:703
#define FOREACH_MAIN_LISTBASE_ID_BEGIN(_lb, _id)
Definition BKE_main.hh:697
#define FOREACH_MAIN_LISTBASE_END
Definition BKE_main.hh:715
#define FOREACH_MAIN_LISTBASE_BEGIN(_bmain, _lb)
Definition BKE_main.hh:708
#define FOREACH_MAIN_ID_BEGIN(_bmain, _id)
Definition BKE_main.hh:729
#define BLT_I18NCONTEXT_ID_LIBRARY
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
#define CLOG_DEBUG(clg_ref,...)
Definition CLG_log.h:191
#define CLOG_INFO(clg_ref,...)
Definition CLG_log.h:190
ID and Library types, which are fundamental for SDNA.
#define ID_IS_PACKED(_id)
Definition DNA_ID.h:703
#define ID_IS_LINKED(_id)
Definition DNA_ID.h:697
#define FILTER_ID_LI
Definition DNA_ID.h:1237
Object groups, one object can be in many groups at once.
#define BLI_assert_unreachable()
#define UNUSED_VARS_NDEBUG(...)
#define ELEM(...)
#define BLI_assert_msg(a, msg)
bool add(const Key &key, const Value &value)
Definition BLI_map.hh:295
Value lookup_default(const Key &key, const Value &default_value) const
Definition BLI_map.hh:570
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
bool contains(const Key &key) const
Span< Key > as_span() const
void add(ID *old_id, ID *new_id)
std::variant< ValidDeepHashes, DeepHashErrors > IDHashResult
IDHashResult compute_linked_id_deep_hashes(const Main &bmain, Span< const ID * > root_ids)
Definition id_hash.cc:241
Library * ensure_archive_library(Main &bmain, ID &id, Library &reference_library, const IDHash &id_deep_hash, bool &is_new)
Definition library.cc:542
void main_cleanup_parent_archives(Main &bmain)
Definition library.cc:833
Set< ID * > pack_linked_id_hierarchy(Main &bmain, ID &root_id)
Definition library.cc:741
Library * search_filepath_abs(ListBaseT< Library > *libraries, StringRef filepath_abs)
Definition library.cc:492
Library * create_external_archive_library(Main &bmain, Library &external_library)
Definition library.cc:823
BMesh Iterators.
void BKE_main_relations_free(Main *bmain)
Definition main.cc:760
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void BKE_packedfile_blend_write(BlendWriter *writer, const PackedFile *pf)
char * STRNCPY(char(&dst)[N], const char *src)
@ LIBRARY_TAG_RESYNC_REQUIRED
@ LIBRARY_IS_ASSET_EDIT_FILE
@ LIBRARY_ASSET_EDITABLE
static void library_init_data(ID *id)
Definition library.cc:62
PackedFile * BKE_packedfile_duplicate(const PackedFile *pf_src)
@ ID_REMAP_TYPE_REMAP
bool BLI_path_abs(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1
@ IDWALK_DO_INTERNAL_RUNTIME_POINTERS
void BKE_main_namemap_destroy(UniqueName_Map **r_name_map) ATTR_NONNULL()
static void library_blend_write_data(BlendWriter *writer, ID *id, const void *id_address)
Definition library.cc:169
static void library_foreach_id(ID *id, LibraryForeachIDData *data)
Definition library.cc:112
void BKE_packedfile_free(PackedFile *pf)
ID * BKE_libblock_find_name_and_library(Main *bmain, short type, const char *name, const char *lib_name)
Definition lib_id.cc:1850
static void library_copy_data(Main *bmain, std::optional< Library * > owner_library, ID *id_dst, const ID *id_src, int)
Definition library.cc:78
static void library_blend_read_data(BlendDataReader *reader, ID *id)
Definition library.cc:197
bool BKE_main_namemap_contain_name(Main &bmain, Library *lib, short id_type, StringRef name)
const char * BKE_id_name(const ID &id)
LibraryForeachIDFlag BKE_lib_query_foreachid_process_flags_get(const LibraryForeachIDData *data)
Definition lib_query.cc:133
void BKE_id_blend_write(BlendWriter *writer, ID *id)
Definition lib_id.cc:2890
@ LIB_ID_COPY_DEFAULT
@ LIB_ID_COPY_ASSET_METADATA
@ LIB_ID_COPY_ID_NEW_SET
static void library_runtime_reset(Library *lib)
Definition library.cc:57
const char * BKE_main_blendfile_path(const Main *bmain) ATTR_NONNULL()
Definition main.cc:1058
unsigned short uint16_t
Definition BKE_text.h:80
bool BLO_read_data_is_undo(BlendDataReader *reader)
Definition readfile.cc:6819
void BKE_library_main_rebuild_hierarchy(Main *bmain)
Definition library.cc:375
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
static void pack_linked_id(Main &bmain, ID *linked_id, const id_hash::ValidDeepHashes &deep_hashes, Map< IDHash, ID * > &already_packed_ids, VectorSet< ID * > &ids_to_remap, bke::id::IDRemapper &id_remapper)
Definition library.cc:580
static void library_blend_read_after_liblink(BlendLibReader *, ID *id)
Definition library.cc:209
static Library * add_archive_library(Main &bmain, Library &reference_library)
Definition library.cc:510
BMesh const char void * data
void BKE_main_ensure_invariants(Main &bmain, std::optional< Span< ID * > > modified_ids=std::nullopt)
static void library_free_data(ID *id)
Definition library.cc:68
static void pack_linked_ids(Main &bmain, const Set< ID * > &ids_to_pack)
Definition library.cc:683
static void rebuild_hierarchy_best_parent_find(Main *bmain, Set< Library * > &directly_used_libs, Set< Library * > &libs_in_hierarchy, Library *lib)
Definition library.cc:274
@ LIBRARY_FLAG_IS_ARCHIVE
@ LIBRARY_FLAG_IS_EXTERNAL
static void library_foreach_path(ID *id, BPathForeachPathData *bpath_data)
Definition library.cc:152
void BKE_main_relations_create(Main *bmain, short flag)
Definition main.cc:723
IDTypeInfo IDType_ID_LI
Definition library.cc:217
void BKE_library_filepath_set(Main *bmain, Library *lib, const char *filepath)
Definition library.cc:249
void id_us_ensure_real(ID *id)
Definition lib_id.cc:388
bool BKE_bpath_foreach_path_fixed_process(BPathForeachPathData *bpath_data, char *path, size_t path_maxncpy)
Definition bpath.cc:130
@ INDEX_ID_LI
Definition DNA_ID.h:1283
bool BLI_path_is_rel(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
static CLG_LogRef LOG
Definition logging.cpp:12
static Library * add_external_archive_library(Main &bmain, Library &reference_library)
Definition library.cc:795
@ IDWALK_CB_LOOPBACK
@ IDWALK_CB_WRITEFILE_IGNORE
@ IDWALK_CB_EMBEDDED_NOT_OWNING
@ IDWALK_CB_NEVER_NULL
@ IDWALK_CB_NEVER_SELF
@ IDWALK_CB_INTERNAL
@ IDWALK_CB_READFILE_IGNORE
@ IDWALK_CB_EMBEDDED
void BKE_libblock_relink_multiple(Main *bmain, const Span< ID * > ids, eIDRemapType remap_type, bke::id::IDRemapper &id_remapper, int remap_flags)
Definition lib_remap.cc:793
void id_us_min(ID *id)
Definition lib_id.cc:441
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
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
@ 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
void write_id_struct(const void *id_address, const T *id, BlendStructWriterTypedFn< T > fn=nullptr)
bool is_undo() const
struct ID * newid
Definition DNA_ID.h:419
struct Library * lib
Definition DNA_ID.h:421
IDHash deep_hash
Definition DNA_ID.h:475
LibraryForeachIDCallbackFlag cb_flag
uint16_t undo_runtime_tag
Definition DNA_ID.h:567
struct Library * archive_parent_library
Definition DNA_ID.h:575
struct PackedFile * packedfile
Definition DNA_ID.h:584
char filepath[1024]
Definition DNA_ID.h:562
bke::library::LibraryRuntime * runtime
Definition DNA_ID.h:591
uint16_t flag
Definition DNA_ID.h:565
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
MainIDRelations * relations
Definition BKE_main.hh:437
ListBaseT< Library > libraries
Definition BKE_main.hh:387
short versionfile
Definition BKE_main.hh:269
short subversionfile
Definition BKE_main.hh:270
Map< const ID *, IDHash > hashes
#define N_(msgid)