diff options
author | Campbell Barton <ideasman42@gmail.com> | 2021-03-08 17:01:31 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2021-03-08 17:01:31 +0300 |
commit | 9e0921497912cbfe9846358d1cb1220f88315f80 (patch) | |
tree | 093e2de27514b297de63ce6ea1597401507622ab /source/blender/python/intern/bpy_library_load.c | |
parent | cfd11af9819433c5b83359b72f002fcd59fdc1ab (diff) |
PyAPI: add bpy.types.BlendFile.temp_data for temporary library loading
This adds support for creating a `BlendFile` (internally called `Main`),
which is limited to a context.
Temporary data can now be created which can then use
`.libraries.load()` the same as with `bpy.data`.
To prevent errors caused by mixing the temporary ID's with data in
`bpy.data` they are tagged as temporary so they can't be assigned
to properties, however they can be passed as arguments to functions.
Reviewed By: mont29, sybren
Ref D10612
Diffstat (limited to 'source/blender/python/intern/bpy_library_load.c')
-rw-r--r-- | source/blender/python/intern/bpy_library_load.c | 16 |
1 files changed, 14 insertions, 2 deletions
diff --git a/source/blender/python/intern/bpy_library_load.c b/source/blender/python/intern/bpy_library_load.c index 5d9adb08f3d..1ee14df24cf 100644 --- a/source/blender/python/intern/bpy_library_load.c +++ b/source/blender/python/intern/bpy_library_load.c @@ -34,6 +34,7 @@ #include "BLI_string.h" #include "BLI_utildefines.h" +#include "BKE_context.h" #include "BKE_idtype.h" #include "BKE_lib_id.h" #include "BKE_main.h" @@ -67,8 +68,10 @@ typedef struct { BlendHandle *blo_handle; int flag; PyObject *dict; - /* Borrowed reference to the `bmain`, taken from the RNA instance of #RNA_BlendDataLibraries. */ + /* Borrowed reference to the `bmain`, taken from the RNA instance of #RNA_BlendDataLibraries. + * Defaults to #G.main, Otherwise use a temporary #Main when `bmain_is_temp` is true. */ Main *bmain; + bool bmain_is_temp; } BPy_Library; static PyObject *bpy_lib_load(BPy_PropertyRNA *self, PyObject *args, PyObject *kwds); @@ -185,6 +188,7 @@ PyDoc_STRVAR( " :type assets_only: bool\n"); static PyObject *bpy_lib_load(BPy_PropertyRNA *self, PyObject *args, PyObject *kw) { + Main *bmain_base = CTX_data_main(BPY_context_get()); Main *bmain = self->ptr.data; /* Typically #G_MAIN */ BPy_Library *ret; const char *filename = NULL; @@ -212,6 +216,7 @@ static PyObject *bpy_lib_load(BPy_PropertyRNA *self, PyObject *args, PyObject *k BLI_path_abs(ret->abspath, BKE_main_blendfile_path(bmain)); ret->bmain = bmain; + ret->bmain_is_temp = (bmain != bmain_base); ret->blo_handle = NULL; ret->flag = ((is_link ? FILE_LINK : 0) | (is_rel ? FILE_RELPATH : 0) | @@ -344,8 +349,9 @@ static PyObject *bpy_lib_exit(BPy_Library *self, PyObject *UNUSED(args)) BKE_main_id_tag_all(bmain, LIB_TAG_PRE_EXISTING, true); /* here appending/linking starts */ + const int id_tag_extra = self->bmain_is_temp ? LIB_TAG_TEMP_MAIN : 0; struct LibraryLink_Params liblink_params; - BLO_library_link_params_init(&liblink_params, bmain, self->flag, 0); + BLO_library_link_params_init(&liblink_params, bmain, self->flag, id_tag_extra); mainl = BLO_library_link_begin(&(self->blo_handle), self->relpath, &liblink_params); @@ -372,6 +378,12 @@ static PyObject *bpy_lib_exit(BPy_Library *self, PyObject *UNUSED(args)) ID *id = BLO_library_link_named_part( mainl, &(self->blo_handle), idcode, item_idname, &liblink_params); if (id) { + + if (self->bmain_is_temp) { + /* If this fails, #LibraryLink_Params.id_tag_extra is not being applied. */ + BLI_assert(id->tag & LIB_TAG_TEMP_MAIN); + } + #ifdef USE_RNA_DATABLOCKS /* swap name for pointer to the id */ item_dst = PyCapsule_New((void *)id, NULL, NULL); |