Page MenuHome

BLO: Functions for temporarily loading a single datablock
ClosedPublic

Authored by Sybren A. Stüvel (sybren) on Mar 16 2021, 4:06 PM.

Details

Summary

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

Diff Detail

Repository
rB Blender

Event Timeline

Sybren A. Stüvel (sybren) requested review of this revision.Mar 16 2021, 4:06 PM
Sybren A. Stüvel (sybren) created this revision.

So this is essentially a thin wrapper around the lower-level BLO library API? LGTM, but think @Campbell Barton (campbellbarton) should also have a look on this.

This revision is now accepted and ready to land.Mar 17 2021, 3:13 PM
Ankit Meel (ankitm) added inline comments.
source/blender/blenloader/intern/tempload.c
16–17 ↗(On Diff #35254)

Is this copyright notice needed in new files?

Campbell Barton (campbellbarton) added inline comments.
source/blender/blenloader/CMakeLists.txt
53

Would prefer readfile_temp.c ,readfile_tempload.c or blend_tempload.c, if we're to split out logic in these files further, it could end up with overly generic names.

source/blender/blenloader/intern/tempload.c
41–42 ↗(On Diff #35254)

No need for the with_context version, BLO_library_link_params_init can be used here.

Sybren A. Stüvel (sybren) marked 2 inline comments as done.
Sybren A. Stüvel (sybren) edited the summary of this revision. (Show Details)
  • Less dated GPL block
  • Use BLO_library_link_params_init()
  • Rename tempload.c to readfile_tempload.c
source/blender/blenloader/intern/tempload.c
16–17 ↗(On Diff #35254)

Hah, good catch. That's what you get copy/pasting the license notice from an old file ;-)

Sybren A. Stüvel (sybren) marked an inline comment as done.Mar 18 2021, 11:08 AM