Blender  V2.93
readfile_tempload.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
20 #include "BLO_readfile.h"
21 
22 #include "MEM_guardedalloc.h"
23 
24 #include "BLI_string.h"
25 
26 #include "BKE_main.h"
27 #include "BKE_report.h"
28 
29 #include "DNA_ID.h"
30 
32  const char *blend_file_path,
33  const short idcode,
34  const char *idname,
35  struct ReportList *reports)
36 {
37  TempLibraryContext *temp_lib_ctx = MEM_callocN(sizeof(*temp_lib_ctx), __func__);
38  temp_lib_ctx->bmain_base = BKE_main_new();
39 
40  /* Copy the file path so any path remapping is performed properly. */
41  STRNCPY(temp_lib_ctx->bmain_base->name, real_main->name);
42 
43  temp_lib_ctx->blendhandle = BLO_blendhandle_from_file(blend_file_path, reports);
44 
46  &temp_lib_ctx->liblink_params, temp_lib_ctx->bmain_base, 0, LIB_TAG_TEMP_MAIN);
47 
48  temp_lib_ctx->bmain_lib = BLO_library_link_begin(
49  &temp_lib_ctx->blendhandle, blend_file_path, &temp_lib_ctx->liblink_params);
50 
51  temp_lib_ctx->temp_id = BLO_library_link_named_part(temp_lib_ctx->bmain_lib,
52  &temp_lib_ctx->blendhandle,
53  idcode,
54  idname,
55  &temp_lib_ctx->liblink_params);
56 
57  return temp_lib_ctx;
58 }
59 
61 {
62  /* This moves the temporary ID and any indirectly loaded data into `bmain_base`
63  * only to free `bmain_base`, while redundant this is the typical code-path for library linking,
64  * it's more convenient to follow this convention rather than create a new code-path for this
65  * one-off use case. */
67  temp_lib_ctx->bmain_lib, &temp_lib_ctx->blendhandle, &temp_lib_ctx->liblink_params);
68  BLO_blendhandle_close(temp_lib_ctx->blendhandle);
69  BKE_main_free(temp_lib_ctx->bmain_base);
70  MEM_freeN(temp_lib_ctx);
71 }
struct Main * BKE_main_new(void)
Definition: main.c:45
void BKE_main_free(struct Main *mainvar)
Definition: main.c:53
#define STRNCPY(dst, src)
Definition: BLI_string.h:163
external readfile function prototypes.
void BLO_library_link_params_init(struct LibraryLink_Params *params, struct Main *bmain, const int flag, const int id_tag_extra)
Definition: readfile.c:5069
struct ID * BLO_library_link_named_part(struct Main *mainl, BlendHandle **bh, const short idcode, const char *name, const struct LibraryLink_Params *params)
Definition: readfile.c:4989
struct Main * BLO_library_link_begin(BlendHandle **bh, const char *filepath, const struct LibraryLink_Params *params)
Definition: readfile.c:5109
void BLO_library_link_end(struct Main *mainl, BlendHandle **bh, const struct LibraryLink_Params *params)
Definition: readfile.c:5258
BlendHandle * BLO_blendhandle_from_file(const char *filepath, struct ReportList *reports)
Definition: readblenentry.c:71
void BLO_blendhandle_close(BlendHandle *bh)
ID and Library types, which are fundamental for sdna.
@ LIB_TAG_TEMP_MAIN
Definition: DNA_ID.h:585
Read Guarded memory(de)allocation.
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
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)
Definition: BKE_main.h:116
char name[1024]
Definition: BKE_main.h:118
struct Main * bmain_lib
Definition: BLO_readfile.h:230
struct ID * temp_id
Definition: BLO_readfile.h:238
struct LibraryLink_Params liblink_params
Definition: BLO_readfile.h:234
struct Main * bmain_base
Definition: BLO_readfile.h:232
struct BlendHandle * blendhandle
Definition: BLO_readfile.h:233