This introduces two functions to the blenloader module, here shown as calls for brevity:
- temp_lib_ctx = BLO_library_temp_load_id(real_main, blend_file_path, idcode, idname, reports);
- BLO_library_temp_free(temp_lib_ctx);
The first loads a datablock from a blend file, and returns it as part of a struct TempLibraryContext (suggestions for name improvements welcome). This struct contains the temp-loaded ID, as well as enough information to correctly free everything again.
Later, if necessary, we could add a function that takes a struct TempLibraryContext and another idcode and idname, and load another ID datablock from the same temporary library.
Some example code from the asset-browser-poselib branch:
typedef struct PoseBlendData {
/* For temp-loading the Action from the pose library, if necessary. */
TempLibraryContext *temp_lib_context;
/* Other elements removed. */
} PoseBlendData;
static bAction *poselib_tempload_enter(bContext *C, wmOperator *op)
{
PoseBlendData *pbd = op->customdata;
// Get "/path/to/file.blend/Actions/PoseName" string from the context.
const char *libname = poselib_asset_file_path(C);
char blend_file_path[FILE_MAX_LIBEXTRA];
char *group = NULL;
char *asset_name = NULL;
BLO_library_path_explode(libname, blend_file_path, &group, &asset_name);
pbd->temp_lib_context = BLO_library_temp_load_id(
CTX_data_main(C), blend_file_path, ID_AC, asset_name, op->reports);
if (pbd->temp_lib_context == NULL || pbd->temp_lib_context->temp_id == NULL) {
BKE_reportf(op->reports, RPT_ERROR, "Unable to load %s from %s", asset_name, blend_file_path);
return NULL;
}
return (bAction *)pbd->temp_lib_context->temp_id;
}
static void poselib_tempload_exit(PoseBlendData *pbd)
{
BLO_library_temp_free(pbd->temp_lib_context);
}