Blender V4.3
readfile_tempload.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8#include "BLO_readfile.hh"
9
10#include "MEM_guardedalloc.h"
11
12#include "BLI_string.h"
13
14#include "BKE_main.hh"
15
16#include "DNA_ID.h"
17
19 const char *blend_file_path,
20 const short idcode,
21 const char *idname,
22 ReportList *reports)
23{
24 TempLibraryContext *temp_lib_ctx = static_cast<TempLibraryContext *>(
25 MEM_callocN(sizeof(*temp_lib_ctx), __func__));
26 temp_lib_ctx->bmain_base = BKE_main_new();
27 temp_lib_ctx->bf_reports.reports = reports;
28
29 /* Copy the file path so any path remapping is performed properly. */
30 STRNCPY(temp_lib_ctx->bmain_base->filepath, real_main->filepath);
31
32 temp_lib_ctx->blendhandle = BLO_blendhandle_from_file(blend_file_path,
33 &temp_lib_ctx->bf_reports);
34
36 &temp_lib_ctx->liblink_params, temp_lib_ctx->bmain_base, 0, ID_TAG_TEMP_MAIN);
37
38 temp_lib_ctx->bmain_lib = BLO_library_link_begin(
39 &temp_lib_ctx->blendhandle, blend_file_path, &temp_lib_ctx->liblink_params);
40
41 temp_lib_ctx->temp_id = BLO_library_link_named_part(temp_lib_ctx->bmain_lib,
42 &temp_lib_ctx->blendhandle,
43 idcode,
44 idname,
45 &temp_lib_ctx->liblink_params);
46
47 return temp_lib_ctx;
48}
49
51{
52 /* This moves the temporary ID and any indirectly loaded data into `bmain_base`
53 * only to free `bmain_base`, while redundant this is the typical code-path for library linking,
54 * it's more convenient to follow this convention rather than create a new code-path for this
55 * one-off use case. */
57 temp_lib_ctx->bmain_lib, &temp_lib_ctx->blendhandle, &temp_lib_ctx->liblink_params);
59 BKE_main_free(temp_lib_ctx->bmain_base);
60 MEM_freeN(temp_lib_ctx);
61}
Main * BKE_main_new(void)
Definition main.cc:45
void BKE_main_free(Main *bmain)
Definition main.cc:175
#define STRNCPY(dst, src)
Definition BLI_string.h:593
external readfile function prototypes.
void BLO_library_link_params_init(LibraryLink_Params *params, Main *bmain, int flag, int id_tag_extra)
Definition readfile.cc:4304
BlendHandle * BLO_blendhandle_from_file(const char *filepath, BlendFileReadReport *reports)
void BLO_blendhandle_close(BlendHandle *bh) ATTR_NONNULL(1)
ID * BLO_library_link_named_part(Main *mainl, BlendHandle **bh, short idcode, const char *name, const LibraryLink_Params *params)
Definition readfile.cc:4248
Main * BLO_library_link_begin(BlendHandle **bh, const char *filepath, const LibraryLink_Params *params)
Definition readfile.cc:4332
void BLO_library_link_end(Main *mainl, BlendHandle **bh, const LibraryLink_Params *params)
Definition readfile.cc:4484
ID and Library types, which are fundamental for SDNA.
@ ID_TAG_TEMP_MAIN
Definition DNA_ID.h:938
Read Guarded memory(de)allocation.
void MEM_freeN(void *vmemh)
Definition mallocn.cc:105
void *(* MEM_callocN)(size_t len, const char *str)
Definition mallocn.cc:42
void BLO_library_temp_free(TempLibraryContext *temp_lib_ctx)
TempLibraryContext * BLO_library_temp_load_id(Main *real_main, const char *blend_file_path, const short idcode, const char *idname, ReportList *reports)
char filepath[1024]
Definition BKE_main.hh:136
BlendHandle * blendhandle
BlendFileReadReport bf_reports
LibraryLink_Params liblink_params