From 605cdc4346e5f82c031c4a5d6ecd91bf8268f7ff Mon Sep 17 00:00:00 2001 From: Bastien Montagne Date: Thu, 11 Nov 2021 14:40:11 +0100 Subject: BKE LibLink/Append: Add mechanism for external code to loop over link/append context items. Will be required for python's `bpy.data.libraries.load()` refactor. --- .../blender/blenkernel/BKE_blendfile_link_append.h | 28 +++++++++++++++ .../blenkernel/intern/blendfile_link_append.c | 41 ++++++++++++++++++++-- 2 files changed, 66 insertions(+), 3 deletions(-) (limited to 'source/blender') diff --git a/source/blender/blenkernel/BKE_blendfile_link_append.h b/source/blender/blenkernel/BKE_blendfile_link_append.h index 2035f69315c..9e4a498f5e7 100644 --- a/source/blender/blenkernel/BKE_blendfile_link_append.h +++ b/source/blender/blenkernel/BKE_blendfile_link_append.h @@ -68,6 +68,34 @@ void *BKE_blendfile_link_append_context_item_userdata_get( struct BlendfileLinkAppendContext *lapp_context, struct BlendfileLinkAppendContextItem *item); struct ID *BKE_blendfile_link_append_context_item_newid_get( struct BlendfileLinkAppendContext *lapp_context, struct BlendfileLinkAppendContextItem *item); +short BKE_blendfile_link_append_context_item_idcode_get( + struct BlendfileLinkAppendContext *lapp_context, struct BlendfileLinkAppendContextItem *item); + +typedef enum eBlendfileLinkAppendForeachItemFlag { + /** Loop over directly linked items (i.e. those explicitely defined by user code). */ + BKE_BLENDFILE_LINK_APPEND_FOREACH_ITEM_FLAG_DO_DIRECT = 1 << 0, + /** 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** indrectly linked data. See + * comments in #foreach_libblock_link_append_callback. */ + BKE_BLENDFILE_LINK_APPEND_FOREACH_ITEM_FLAG_DO_INDIRECT = 1 << 0, +} eBlendfileLinkAppendForeachItemFlag; +/** Callback called by #BKE_blendfile_link_append_context_item_foreach over each (or a subset of + * each) of the items in given #BlendfileLinkAppendContext. + * + * \param userdata: An opaque void pointer passed to the `callback_function`. + * + * \return `true` if iteration should continue, `false` otherwise. */ +typedef bool (*BKE_BlendfileLinkAppendContexteItemFunction)( + struct BlendfileLinkAppendContext *lapp_context, + struct BlendfileLinkAppendContextItem *item, + void *userdata); +void BKE_blendfile_link_append_context_item_foreach( + struct BlendfileLinkAppendContext *lapp_context, + BKE_BlendfileLinkAppendContexteItemFunction callback_function, + const eBlendfileLinkAppendForeachItemFlag flag, + void *userdata); void BKE_blendfile_append(struct BlendfileLinkAppendContext *lapp_context, struct ReportList *reports); diff --git a/source/blender/blenkernel/intern/blendfile_link_append.c b/source/blender/blenkernel/intern/blendfile_link_append.c index 107e89e0e1e..36f03990953 100644 --- a/source/blender/blenkernel/intern/blendfile_link_append.c +++ b/source/blender/blenkernel/intern/blendfile_link_append.c @@ -277,9 +277,6 @@ void BKE_blendfile_link_append_context_library_add(BlendfileLinkAppendContext *l BlendfileLinkAppendContextLibrary *lib_context = BLI_memarena_calloc(lapp_context->memarena, sizeof(*lib_context)); - BlendfileLinkAppendContextLibrary *ctx_lib = BLI_memarena_alloc(lapp_context->memarena, - sizeof(*ctx_lib)); - size_t len = strlen(libname) + 1; char *libpath = BLI_memarena_alloc(lapp_context->memarena, len); BLI_strncpy(libpath, libname, len); @@ -349,6 +346,44 @@ ID *BKE_blendfile_link_append_context_item_newid_get( return item->new_id; } +short BKE_blendfile_link_append_context_item_idcode_get( + struct BlendfileLinkAppendContext *UNUSED(lapp_context), + struct BlendfileLinkAppendContextItem *item) +{ + return item->idcode; +} + +/** Iterate over all (or a subset) of the items listed in given #BlendfileLinkAppendContext, and + * call the `callback_function` on them. + * + * \param flag: Control which type of items to process (see + * #eBlendfileLinkAppendForeachItemFlag enum flags). + * \param userdata: An opaque void pointer passed to the `callback_function`. + */ +void BKE_blendfile_link_append_context_item_foreach( + struct BlendfileLinkAppendContext *lapp_context, + BKE_BlendfileLinkAppendContexteItemFunction callback_function, + const eBlendfileLinkAppendForeachItemFlag flag, + void *userdata) +{ + for (LinkNode *itemlink = lapp_context->items.list; itemlink; itemlink = itemlink->next) { + BlendfileLinkAppendContextItem *item = itemlink->link; + + if ((flag & BKE_BLENDFILE_LINK_APPEND_FOREACH_ITEM_FLAG_DO_DIRECT) == 0 && + (item->tag & LINK_APPEND_TAG_INDIRECT) == 0) { + continue; + } + if ((flag & BKE_BLENDFILE_LINK_APPEND_FOREACH_ITEM_FLAG_DO_INDIRECT) == 0 && + (item->tag & LINK_APPEND_TAG_INDIRECT) != 0) { + continue; + } + + if (!callback_function(lapp_context, item, userdata)) { + break; + } + } +} + /** \} */ /* -------------------------------------------------------------------- */ -- cgit v1.2.3