Blender  V3.3
readfile_tempload.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
6 #include "BLO_readfile.h"
7 
8 #include "MEM_guardedalloc.h"
9 
10 #include "BLI_string.h"
11 
12 #include "BKE_main.h"
13 #include "BKE_report.h"
14 
15 #include "DNA_ID.h"
16 
18  const char *blend_file_path,
19  const short idcode,
20  const char *idname,
21  struct ReportList *reports)
22 {
23  TempLibraryContext *temp_lib_ctx = MEM_callocN(sizeof(*temp_lib_ctx), __func__);
24  temp_lib_ctx->bmain_base = BKE_main_new();
25  temp_lib_ctx->bf_reports.reports = reports;
26 
27  /* Copy the file path so any path remapping is performed properly. */
28  STRNCPY(temp_lib_ctx->bmain_base->filepath, real_main->filepath);
29 
30  temp_lib_ctx->blendhandle = BLO_blendhandle_from_file(blend_file_path,
31  &temp_lib_ctx->bf_reports);
32 
34  &temp_lib_ctx->liblink_params, temp_lib_ctx->bmain_base, 0, LIB_TAG_TEMP_MAIN);
35 
36  temp_lib_ctx->bmain_lib = BLO_library_link_begin(
37  &temp_lib_ctx->blendhandle, blend_file_path, &temp_lib_ctx->liblink_params);
38 
39  temp_lib_ctx->temp_id = BLO_library_link_named_part(temp_lib_ctx->bmain_lib,
40  &temp_lib_ctx->blendhandle,
41  idcode,
42  idname,
43  &temp_lib_ctx->liblink_params);
44 
45  return temp_lib_ctx;
46 }
47 
49 {
50  /* This moves the temporary ID and any indirectly loaded data into `bmain_base`
51  * only to free `bmain_base`, while redundant this is the typical code-path for library linking,
52  * it's more convenient to follow this convention rather than create a new code-path for this
53  * one-off use case. */
55  temp_lib_ctx->bmain_lib, &temp_lib_ctx->blendhandle, &temp_lib_ctx->liblink_params);
56  BLO_blendhandle_close(temp_lib_ctx->blendhandle);
57  BKE_main_free(temp_lib_ctx->bmain_base);
58  MEM_freeN(temp_lib_ctx);
59 }
struct Main * BKE_main_new(void)
Definition: main.c:32
void BKE_main_free(struct Main *mainvar)
Definition: main.c:40
#define STRNCPY(dst, src)
Definition: BLI_string.h:483
external readfile function prototypes.
BlendHandle * BLO_blendhandle_from_file(const char *filepath, struct BlendFileReadReport *reports)
Definition: readblenentry.c:48
struct Main * BLO_library_link_begin(BlendHandle **bh, const char *filepath, const struct LibraryLink_Params *params)
Definition: readfile.c:4650
void BLO_library_link_end(struct Main *mainl, BlendHandle **bh, const struct LibraryLink_Params *params)
Definition: readfile.c:4787
void BLO_library_link_params_init(struct LibraryLink_Params *params, struct Main *bmain, int flag, int id_tag_extra)
Definition: readfile.c:4622
struct ID * BLO_library_link_named_part(struct Main *mainl, BlendHandle **bh, short idcode, const char *name, const struct LibraryLink_Params *params)
Definition: readfile.c:4569
void BLO_blendhandle_close(BlendHandle *bh)
ID and Library types, which are fundamental for sdna.
@ LIB_TAG_TEMP_MAIN
Definition: DNA_ID.h:757
Read Guarded memory(de)allocation.
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:27
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:31
void BLO_library_temp_free(TempLibraryContext *temp_lib_ctx)
TempLibraryContext * BLO_library_temp_load_id(struct Main *real_main, const char *blend_file_path, const short idcode, const char *idname, struct ReportList *reports)
struct ReportList * reports
Definition: BLO_readfile.h:80
Definition: BKE_main.h:121
char filepath[1024]
Definition: BKE_main.h:124
struct Main * bmain_lib
Definition: BLO_readfile.h:429
struct ID * temp_id
Definition: BLO_readfile.h:438
struct LibraryLink_Params liblink_params
Definition: BLO_readfile.h:434
struct Main * bmain_base
Definition: BLO_readfile.h:431
struct BlendHandle * blendhandle
Definition: BLO_readfile.h:432
struct BlendFileReadReport bf_reports
Definition: BLO_readfile.h:433