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.
This commit is contained in:
Bastien Montagne 2021-11-11 14:40:11 +01:00
parent 0452a04f1a
commit 605cdc4346
2 changed files with 66 additions and 3 deletions

View File

@ -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);

View File

@ -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;
}
}
}
/** \} */
/* -------------------------------------------------------------------- */