Blender V4.3
BKE_blendfile_link_append.hh File Reference
#include <list>
#include <string>
#include "BLI_bit_vector.hh"
#include "BLI_function_ref.hh"
#include "BLI_map.hh"
#include "BLO_readfile.hh"

Go to the source code of this file.

Classes

struct  BlendfileLinkAppendContextItem
 
struct  BlendfileLinkAppendContextLibrary
 
struct  BlendfileLinkAppendContext
 

Macros

#define BLENDFILE_LINK_APPEND_INVALID   -1
 

Enumerations

enum  {
  LINK_APPEND_ACT_UNSET = 0 , LINK_APPEND_ACT_KEEP_LINKED , LINK_APPEND_ACT_REUSE_LOCAL , LINK_APPEND_ACT_MAKE_LOCAL ,
  LINK_APPEND_ACT_COPY_LOCAL
}
 
enum  { LINK_APPEND_TAG_INDIRECT = 1 << 0 , LINK_APPEND_TAG_LIBOVERRIDE_DEPENDENCY = 1 << 1 , LINK_APPEND_TAG_LIBOVERRIDE_DEPENDENCY_ONLY = 1 << 2 }
 
enum  eBlendfileLinkAppendForeachItemFlag { BKE_BLENDFILE_LINK_APPEND_FOREACH_ITEM_FLAG_DO_DIRECT = 1 << 0 , BKE_BLENDFILE_LINK_APPEND_FOREACH_ITEM_FLAG_DO_INDIRECT = 1 << 1 }
 
enum  eBKELibLinkOverride { BKE_LIBLINK_OVERRIDE_INIT = 0 , BKE_LIBLINK_OVERRIDE_USE_EXISTING_LIBOVERRIDES = 1 << 0 , BKE_LIBLINK_OVERRIDE_CREATE_RUNTIME = 1 << 1 }
 

Functions

BlendfileLinkAppendContextBKE_blendfile_link_append_context_new (LibraryLink_Params *params)
 
void BKE_blendfile_link_append_context_free (BlendfileLinkAppendContext *lapp_context)
 
void BKE_blendfile_link_append_context_flag_set (BlendfileLinkAppendContext *lapp_context, int flag, bool do_set)
 
void BKE_blendfile_link_append_context_embedded_blendfile_set (BlendfileLinkAppendContext *lapp_context, const void *blendfile_mem, int blendfile_memsize)
 
void BKE_blendfile_link_append_context_embedded_blendfile_clear (BlendfileLinkAppendContext *lapp_context)
 
void BKE_blendfile_link_append_context_library_add (BlendfileLinkAppendContext *lapp_context, const char *libname, BlendHandle *blo_handle)
 
BlendfileLinkAppendContextItemBKE_blendfile_link_append_context_item_add (BlendfileLinkAppendContext *lapp_context, const char *idname, short idcode, void *userdata)
 
int BKE_blendfile_link_append_context_item_idtypes_from_library_add (BlendfileLinkAppendContext *lapp_context, ReportList *reports, uint64_t id_types_filter, int library_index)
 
void BKE_blendfile_link_append_context_item_library_index_enable (BlendfileLinkAppendContext *lapp_context, BlendfileLinkAppendContextItem *item, int library_index)
 
bool BKE_blendfile_link_append_context_is_empty (BlendfileLinkAppendContext *lapp_context)
 
void * BKE_blendfile_link_append_context_item_userdata_get (BlendfileLinkAppendContext *lapp_context, BlendfileLinkAppendContextItem *item)
 
IDBKE_blendfile_link_append_context_item_newid_get (BlendfileLinkAppendContext *lapp_context, BlendfileLinkAppendContextItem *item)
 
void BKE_blendfile_link_append_context_item_newid_set (BlendfileLinkAppendContext *lapp_context, BlendfileLinkAppendContextItem *item, ID *new_id)
 
IDBKE_blendfile_link_append_context_item_liboverrideid_get (BlendfileLinkAppendContext *lapp_context, BlendfileLinkAppendContextItem *item)
 
short BKE_blendfile_link_append_context_item_idcode_get (BlendfileLinkAppendContext *lapp_context, BlendfileLinkAppendContextItem *item)
 
void BKE_blendfile_link_append_context_item_foreach (BlendfileLinkAppendContext *lapp_context, blender::FunctionRef< bool(BlendfileLinkAppendContext *lapp_context, BlendfileLinkAppendContextItem *item)> callback_function, eBlendfileLinkAppendForeachItemFlag flag)
 
void BKE_blendfile_link_append_context_init_done (BlendfileLinkAppendContext *lapp_context)
 
void BKE_blendfile_link (BlendfileLinkAppendContext *lapp_context, ReportList *reports)
 
void BKE_blendfile_append (BlendfileLinkAppendContext *lapp_context, ReportList *reports)
 
void BKE_blendfile_link_append_instantiate_loose (BlendfileLinkAppendContext *lapp_context, ReportList *reports)
 
void BKE_blendfile_link_append_context_finalize (BlendfileLinkAppendContext *lapp_context)
 
void BKE_blendfile_override (BlendfileLinkAppendContext *lapp_context, const eBKELibLinkOverride flags, ReportList *reports)
 
void BKE_blendfile_library_relocate (BlendfileLinkAppendContext *lapp_context, ReportList *reports, Library *library, bool do_reload)
 

Macro Definition Documentation

◆ BLENDFILE_LINK_APPEND_INVALID

Enumeration Type Documentation

◆ anonymous enum

anonymous enum

Actions to apply to an item (i.e. linked ID).

Enumerator
LINK_APPEND_ACT_UNSET 
LINK_APPEND_ACT_KEEP_LINKED 
LINK_APPEND_ACT_REUSE_LOCAL 
LINK_APPEND_ACT_MAKE_LOCAL 
LINK_APPEND_ACT_COPY_LOCAL 

Definition at line 31 of file BKE_blendfile_link_append.hh.

◆ anonymous enum

anonymous enum

Various status info about an item (i.e. linked ID).

Enumerator
LINK_APPEND_TAG_INDIRECT 

An indirectly linked ID.

LINK_APPEND_TAG_LIBOVERRIDE_DEPENDENCY 

An ID also used as liboverride dependency (either directly, as a liboverride reference, or indirectly, as data used by a liboverride reference). It should never be directly made local.

Mutually exclusive with LINK_APPEND_TAG_LIBOVERRIDE_DEPENDENCY_ONLY.

LINK_APPEND_TAG_LIBOVERRIDE_DEPENDENCY_ONLY 

An ID only used as liboverride dependency (either directly or indirectly, see LINK_APPEND_TAG_LIBOVERRIDE_DEPENDENCY for precisions). It should not be considered during the 'make local' process, and remain purely linked data.

Mutually exclusive with LINK_APPEND_TAG_LIBOVERRIDE_DEPENDENCY.

Definition at line 40 of file BKE_blendfile_link_append.hh.

◆ eBKELibLinkOverride

Options controlling the behavior of liboverrides creation.

Enumerator
BKE_LIBLINK_OVERRIDE_INIT 
BKE_LIBLINK_OVERRIDE_USE_EXISTING_LIBOVERRIDES 

Try to find a matching existing liboverride first, instead of always creating a new one.

Note
Takes into account the #BKE_LIBLINK_CREATE_RUNTIME flag too (i.e. only checks for runtime liboverrides if that flag is set, and vice-versa).
BKE_LIBLINK_OVERRIDE_CREATE_RUNTIME 

Create (or return an existing) runtime liboverride, instead of a regular saved-in-blend-files one. See also the ID_TAG_RUNTIME tag of IDs in DNA_ID.h.

Note
Typically, usage of this flag implies that no linked IDs are instantiated, such that their usages remain indirect.

Definition at line 351 of file BKE_blendfile_link_append.hh.

◆ eBlendfileLinkAppendForeachItemFlag

Enumerator
BKE_BLENDFILE_LINK_APPEND_FOREACH_ITEM_FLAG_DO_DIRECT 

Loop over directly linked items (i.e. those explicitly defined by user code).

BKE_BLENDFILE_LINK_APPEND_FOREACH_ITEM_FLAG_DO_INDIRECT 

Loop over indirectly linked items (i.e. those defined by internal code, as dependencies of direct ones).

IMPORTANT: Those 'indirect' items currently may not cover all indirectly linked data. See comments in #foreach_libblock_link_append_callback.

Definition at line 284 of file BKE_blendfile_link_append.hh.

Function Documentation

◆ BKE_blendfile_append()

void BKE_blendfile_append ( BlendfileLinkAppendContext * lapp_context,
ReportList * reports )

Perform append operation, using modern ID usage looper to detect which ID should be kept linked, made local, duplicated as local, re-used from local etc.

The IDs processed by this functions are the one that have been linked by a previous call to BKE_blendfile_link on the same lapp_context.

Definition at line 1276 of file blendfile_link_append.cc.

References BlendfileLinkAppendContext::Appending, BKE_id_multi_tagged_delete(), BKE_idtype_idcode_append_is_reusable(), BKE_lib_id_make_local(), BKE_libblock_relink_to_newid(), BKE_main_id_newptr_and_tag_clear(), BKE_main_id_tag_all(), BKE_main_library_weak_reference_add_item(), BKE_main_library_weak_reference_create(), BKE_main_library_weak_reference_destroy(), blendfile_append_define_actions(), BLI_assert, BLI_assert_unreachable, BLO_LIBLINK_APPEND_ASSET_DATA_CLEAR, BLO_LIBLINK_APPEND_RECURSIVE, BLO_LIBLINK_APPEND_SET_FAKEUSER, BLO_read_do_version_after_setup(), LibraryLink_Params::bmain, CLOG_ERROR, ELEM, FILE_LINK, FILE_MAX, LibraryLink_Params::flag, GS, id_fake_user_set(), ID_GR, ID_IS_LINKED, ID_NEW_SET, ID_OB, ID_TAG_DOIT, ID_TAG_PRE_EXISTING, BlendfileLinkAppendContext::items, LIB_ID_MAKELOCAL_ASSET_DATA_CLEAR, LIB_ID_MAKELOCAL_FORCE_COPY, LIB_ID_MAKELOCAL_FORCE_LOCAL, LIB_ID_MAKELOCAL_FULL_LIBRARY, LIB_ID_MAKELOCAL_LIBOVERRIDE_CLEAR, BlendfileLinkAppendContext::library_weak_reference_mapping, LINK_APPEND_ACT_COPY_LOCAL, LINK_APPEND_ACT_KEEP_LINKED, LINK_APPEND_ACT_MAKE_LOCAL, LINK_APPEND_ACT_REUSE_LOCAL, LINK_APPEND_ACT_UNSET, LINK_APPEND_TAG_LIBOVERRIDE_DEPENDENCY, LINK_APPEND_TAG_LIBOVERRIDE_DEPENDENCY_ONLY, BlendfileLinkAppendContext::Linking, LOG, MAX_ID_NAME, ID::name, new_id_to_item_mapping_create(), ID::newid, BlendfileLinkAppendContext::params, BlendfileLinkAppendContext::process_stage, BlendFileReadReport::reports, and STRNCPY.

Referenced by bpy_lib_exit(), copybuffer_append(), wm_file_link_append_datablock_ex(), and wm_link_append_exec().

◆ BKE_blendfile_library_relocate()

void BKE_blendfile_library_relocate ( BlendfileLinkAppendContext * lapp_context,
ReportList * reports,
Library * library,
bool do_reload )

Try to relocate all linked IDs added to lapp_context, belonging to the given library.

This function searches for matching IDs (type and name) in all libraries added to the given lapp_context.

Typical usages include:

  • Relocating a library:
    • Add the new target library path to lapp_context.
    • Add all IDs from the library to relocate to lapp_context
    • Mark the new target library to be considered for each ID.
    • Call this function.
  • Searching for (e.g.missing) linked IDs in a set or sub-set of libraries:
    • Add all potential library sources paths to lapp_context.
    • Add all IDs to search for to lapp_context.
    • Mark which libraries should be considered for each ID.
    • Call this function.

NOTE: content of lapp_context after execution of that function should not be assumed valid anymore, and should immediately be freed.

Definition at line 1768 of file blendfile_link_append.cc.

References BKE_blendfile_link(), BKE_blendfile_link_append_context_init_done(), BKE_blendfile_link_append_context_item_add(), BKE_id_delete(), BKE_id_free(), BKE_id_multi_tagged_delete(), BKE_id_name(), BKE_idtype_idcode_is_linkable(), BKE_key_from_id(), BKE_key_from_id_p(), BKE_layer_collection_resync_allow(), BKE_layer_collection_resync_forbid(), BKE_lib_override_library_main_operations_create(), BKE_lib_override_library_main_resync(), BKE_lib_override_library_update(), BKE_library_main_rebuild_hierarchy(), BKE_main_collection_sync(), BKE_main_collection_sync_remap(), BKE_main_id_refcount_recompute(), BKE_main_id_tag_all(), BKE_main_id_tag_idcode(), BKE_main_lock(), BKE_main_unlock(), BKE_view_layer_find(), blendfile_library_relocate_remap(), BLI_addtail(), BLI_assert, BLI_remlink(), LibraryLink_Params::bmain, CLOG_INFO, LibraryLink_Params::context, blender::bits::BitVector< InlineBufferCapacity, Allocator >::fill(), ListBase::first, FOREACH_MAIN_ID_BEGIN, FOREACH_MAIN_ID_END, GS, Key::id, Library::id, Scene::id, ID_IS_LINKED, ID_IS_OVERRIDE_LIBRARY_REAL, ID_KE, ID_LI, ID_REMAP_SKIP_INDIRECT_USAGE, ID_REMAP_SKIP_NEVER_NULL_USAGE, ID_SCE, ID_TAG_DOIT, ID_TAG_MISSING, ID_TAG_PRE_EXISTING, id_us_clear_real(), id_us_min(), id_us_plus_no_lib(), INDEX_ID_MAX, BlendfileLinkAppendContext::items, ID::lib, lib, BlendfileLinkAppendContextItem::libraries, LOG, ID::name, ViewLayer::name, BlendfileLinkAppendContext::params, BlendFileReadReport::reports, LibraryLink_Params::scene, set_listbasepointers(), ID::tag, ID::us, USER_EXPERIMENTAL_TEST, LibraryLink_Params::view_layer, Scene::view_layers, and which_libbase().

Referenced by WM_lib_reload(), and wm_lib_relocate_exec_do().

◆ BKE_blendfile_link()

◆ BKE_blendfile_link_append_context_embedded_blendfile_clear()

void BKE_blendfile_link_append_context_embedded_blendfile_clear ( BlendfileLinkAppendContext * lapp_context)

Clear reference to Blender's embedded startup file into the context.

Definition at line 171 of file blendfile_link_append.cc.

References BlendfileLinkAppendContext::blendfile_mem, and BlendfileLinkAppendContext::blendfile_memsize.

◆ BKE_blendfile_link_append_context_embedded_blendfile_set()

void BKE_blendfile_link_append_context_embedded_blendfile_set ( BlendfileLinkAppendContext * lapp_context,
const void * blendfile_mem,
int blendfile_memsize )

Store reference to a Blender's embedded memfile into the context.

Note
This is required since embedded startup blender file is handled in ED module, which cannot be linked in BKE code.

Definition at line 161 of file blendfile_link_append.cc.

References BlendfileLinkAppendContext::blendfile_mem, BlendfileLinkAppendContext::blendfile_memsize, and BLI_assert_msg.

Referenced by wm_file_link_append_datablock_ex(), and wm_link_append_exec().

◆ BKE_blendfile_link_append_context_finalize()

◆ BKE_blendfile_link_append_context_flag_set()

void BKE_blendfile_link_append_context_flag_set ( BlendfileLinkAppendContext * lapp_context,
int flag,
bool do_set )

Set or clear flags in given lapp_context.

Parameters
flagA combination of:
do_setSet the given flag if true, clear it otherwise.

Definition at line 149 of file blendfile_link_append.cc.

References flag, LibraryLink_Params::flag, and BlendfileLinkAppendContext::params.

Referenced by blender::bke::asset_link_id(), and wm_lib_relocate_exec_do().

◆ BKE_blendfile_link_append_context_free()

◆ BKE_blendfile_link_append_context_init_done()

void BKE_blendfile_link_append_context_init_done ( BlendfileLinkAppendContext * lapp_context)

◆ BKE_blendfile_link_append_context_is_empty()

bool BKE_blendfile_link_append_context_is_empty ( BlendfileLinkAppendContext * lapp_context)

Check if given link/append context is empty (has no items to process) or not.

Definition at line 281 of file blendfile_link_append.cc.

References BlendfileLinkAppendContext::items.

Referenced by wm_link_append_exec().

◆ BKE_blendfile_link_append_context_item_add()

◆ BKE_blendfile_link_append_context_item_foreach()

void BKE_blendfile_link_append_context_item_foreach ( BlendfileLinkAppendContext * lapp_context,
blender::FunctionRef< bool(BlendfileLinkAppendContext *lapp_context, BlendfileLinkAppendContextItem *item)> callback_function,
eBlendfileLinkAppendForeachItemFlag flag )

Iterate over all (or a subset) of the items listed in given BlendfileLinkAppendContext, and call the callback_function on them.

Parameters
flagControl which type of items to process (see eBlendfileLinkAppendForeachItemFlag enum flags).
userdataAn opaque void pointer passed to the callback_function.
Parameters
callback_functionCalled over each (or a subset of each) of the items in given BlendfileLinkAppendContext.
Returns
true if iteration should continue, false otherwise.

Definition at line 329 of file blendfile_link_append.cc.

References BKE_BLENDFILE_LINK_APPEND_FOREACH_ITEM_FLAG_DO_DIRECT, BKE_BLENDFILE_LINK_APPEND_FOREACH_ITEM_FLAG_DO_INDIRECT, flag, BlendfileLinkAppendContext::items, LINK_APPEND_TAG_INDIRECT, and BlendfileLinkAppendContextItem::tag.

Referenced by bpy_lib_exit(), and blender::bke::greasepencil::convert::legacy_main().

◆ BKE_blendfile_link_append_context_item_idcode_get()

short BKE_blendfile_link_append_context_item_idcode_get ( BlendfileLinkAppendContext * lapp_context,
BlendfileLinkAppendContextItem * item )

◆ BKE_blendfile_link_append_context_item_idtypes_from_library_add()

int BKE_blendfile_link_append_context_item_idtypes_from_library_add ( BlendfileLinkAppendContext * lapp_context,
ReportList * reports,
uint64_t id_types_filter,
int library_index )

Search for all ID matching given id_types_filter in given library_index, and add them to the list of items to process.

Note
BKE_blendfile_link_append_context_library_add should never be called on the same lapp_context after this function.
Parameters
id_types_filterA set of FILTER_ID bitflags, the types of IDs to add to the items list.
library_indexThe index of the library to look into, in given lapp_context.
Returns
The number of items found and added to the list, or BLENDFILE_LINK_APPEND_INVALID if it could not open the .blend file.

Definition at line 219 of file blendfile_link_append.cc.

References BKE_blendfile_link_append_context_item_add(), BKE_blendfile_link_append_context_item_library_index_enable(), BKE_idtype_idcode_is_linkable(), BKE_idtype_idcode_iter_step(), BKE_idtype_idcode_to_idfilter(), BLENDFILE_LINK_APPEND_INVALID, BLI_assert, BLO_blendhandle_get_datablock_names(), FILE_ASSETS_ONLY, LibraryLink_Params::flag, id_name(), BlendfileLinkAppendContext::Init, BlendfileLinkAppendContext::libraries, LinkNode::link, link_append_context_library_blohandle_ensure(), MEM_freeN(), LinkNode::next, BlendfileLinkAppendContext::params, and BlendfileLinkAppendContext::process_stage.

Referenced by BKE_copybuffer_paste(), and BKE_copybuffer_read().

◆ BKE_blendfile_link_append_context_item_liboverrideid_get()

◆ BKE_blendfile_link_append_context_item_library_index_enable()

void BKE_blendfile_link_append_context_item_library_index_enable ( BlendfileLinkAppendContext * lapp_context,
BlendfileLinkAppendContextItem * item,
int library_index )

◆ BKE_blendfile_link_append_context_item_newid_get()

◆ BKE_blendfile_link_append_context_item_newid_set()

◆ BKE_blendfile_link_append_context_item_userdata_get()

void * BKE_blendfile_link_append_context_item_userdata_get ( BlendfileLinkAppendContext * lapp_context,
BlendfileLinkAppendContextItem * item )

◆ BKE_blendfile_link_append_context_library_add()

void BKE_blendfile_link_append_context_library_add ( BlendfileLinkAppendContext * lapp_context,
const char * libname,
BlendHandle * blo_handle )

Add a new source library to search for items to be linked to the given link/append context.

Parameters
libnamethe absolute path to the library blend file.
blo_handlethe blend file handle of the library, nullptr if not available. Note that the ownership of this handle is always stolen, because readfile code may forcefully clear this handle after reading in some cases (endianness conversion, see usages of the FD_FLAGS_SWITCH_ENDIAN flag).
Note
Never call BKE_blendfile_link_append_context_library_add() after having added some items.

Definition at line 178 of file blendfile_link_append.cc.

References blender::Vector< T, InlineBufferCapacity, Allocator >::append(), BLI_assert, BlendfileLinkAppendContextLibrary::blo_handle, BlendfileLinkAppendContextLibrary::blo_handle_is_owned, BlendfileLinkAppendContext::Init, BlendfileLinkAppendContext::items, BlendfileLinkAppendContext::libraries, BlendfileLinkAppendContextLibrary::path, and BlendfileLinkAppendContext::process_stage.

Referenced by blender::bke::asset_link_id(), BKE_copybuffer_paste(), BKE_copybuffer_read(), bpy_lib_exit(), wm_file_link_append_datablock_ex(), WM_lib_reload(), wm_lib_relocate_exec_do(), and wm_link_append_exec().

◆ BKE_blendfile_link_append_context_new()

◆ BKE_blendfile_link_append_instantiate_loose()

◆ BKE_blendfile_override()

void BKE_blendfile_override ( BlendfileLinkAppendContext * lapp_context,
const eBKELibLinkOverride flags,
ReportList * reports )

Create (or find existing) liboverrides from linked data.

The IDs processed by this functions are the one that have been linked by a previous call to BKE_blendfile_link on the same lapp_context.

Control over how liboverrides are created is done through the extra eBKELibLinkOverride flags.

Warning
Currently this function only performs very (very!) basic liboverrides, with no handling of dependencies or hierarchies. It is not expected to be directly exposed to users in its current state, but rather as a helper for specific use-cases like 'presets assets' handling.

Definition at line 1604 of file blendfile_link_append.cc.

References blender::Map< Key, Value, InlineBufferCapacity, ProbingStrategy, Hash, IsEqual, Slot, Allocator >::add(), BKE_lib_override_library_create_from_id(), BKE_LIBLINK_OVERRIDE_CREATE_RUNTIME, BKE_LIBLINK_OVERRIDE_USE_EXISTING_LIBOVERRIDES, BKE_main_namemap_clear(), BLI_assert, LibraryLink_Params::bmain, FILE_LINK, LibraryLink_Params::flag, FOREACH_MAIN_ID_BEGIN, FOREACH_MAIN_ID_END, ID_IS_LINKED, ID_IS_OVERRIDE_LIBRARY_REAL, ID_TAG_INDIRECT, ID_TAG_PRE_EXISTING, ID_TAG_RUNTIME, BlendfileLinkAppendContext::items, blender::Map< Key, Value, InlineBufferCapacity, ProbingStrategy, Hash, IsEqual, Slot, Allocator >::lookup_default(), ID::override_library, BlendfileLinkAppendContext::params, IDOverrideLibrary::reference, and ID::tag.

Referenced by bpy_lib_exit().