From 3f08488244c024ffe8167d6910eccc28da6c9cee Mon Sep 17 00:00:00 2001 From: Bastien Montagne Date: Tue, 30 Nov 2021 17:11:25 +0100 Subject: Rewrite ID Paste code to use new BKE_blendfile_link_append module. No behavioral changes are expected here. Part of T91414: Unify link/append between WM operators and BPY context manager API, and cleanup usages of `BKE_library_make_local`. --- .../blender/blenkernel/intern/blender_copybuffer.c | 120 ++++++++++----------- 1 file changed, 57 insertions(+), 63 deletions(-) (limited to 'source') diff --git a/source/blender/blenkernel/intern/blender_copybuffer.c b/source/blender/blenkernel/intern/blender_copybuffer.c index f8b943d3479..f21c09d0adf 100644 --- a/source/blender/blenkernel/intern/blender_copybuffer.c +++ b/source/blender/blenkernel/intern/blender_copybuffer.c @@ -38,6 +38,7 @@ #include "BKE_blender_copybuffer.h" /* own include */ #include "BKE_blendfile.h" +#include "BKE_blendfile_link_append.h" #include "BKE_context.h" #include "BKE_global.h" #include "BKE_layer.h" @@ -88,6 +89,31 @@ bool BKE_copybuffer_copy_end(Main *bmain_src, const char *filename, ReportList * return retval; } +/* Common helper for paste functions. */ +static void copybuffer_append(BlendfileLinkAppendContext *lapp_context, + Main *bmain, + ReportList *reports) +{ + /* Tag existing IDs in given `bmain_dst` as already existing. */ + BKE_main_id_tag_all(bmain, LIB_TAG_PRE_EXISTING, true); + + BKE_blendfile_link(lapp_context, reports); + + /* Mark all library linked objects to be updated. */ + BKE_main_lib_objects_recalc_all(bmain); + IMB_colormanagement_check_file_config(bmain); + + /* Append, rather than linking */ + BKE_blendfile_append(lapp_context, reports); + + /* This must be unset, otherwise these object won't link into other scenes from this blend + * file. */ + BKE_main_id_tag_all(bmain, LIB_TAG_PRE_EXISTING, false); + + /* Recreate dependency graph to include new objects. */ + DEG_relations_tag_update(bmain); +} + /** * Paste datablocks from the given .blend file 'buffer' (i.e. append them). * @@ -103,31 +129,27 @@ bool BKE_copybuffer_read(Main *bmain_dst, ReportList *reports, const uint64_t id_types_mask) { - BlendFileReadReport bf_reports = {.reports = reports}; - BlendHandle *bh = BLO_blendhandle_from_file(libname, &bf_reports); - if (bh == NULL) { - /* Error reports will have been made by BLO_blendhandle_from_file(). */ - return false; - } - /* Here appending/linking starts. */ + /* Note: No recursive append here (no `BLO_LIBLINK_APPEND_RECURSIVE`), external linked data + * should remain linked. */ const int flag = 0; const int id_tag_extra = 0; struct LibraryLink_Params liblink_params; BLO_library_link_params_init(&liblink_params, bmain_dst, flag, id_tag_extra); - Main *mainl = BLO_library_link_begin(&bh, libname, &liblink_params); - BLO_library_link_copypaste(mainl, bh, id_types_mask); - BLO_library_link_end(mainl, &bh, &liblink_params); - /* Mark all library linked objects to be updated. */ - BKE_main_lib_objects_recalc_all(bmain_dst); - IMB_colormanagement_check_file_config(bmain_dst); - /* Append, rather than linking. */ - Library *lib = BLI_findstring(&bmain_dst->libraries, libname, offsetof(Library, filepath_abs)); - BKE_library_make_local(bmain_dst, lib, NULL, true, false); - /* Important we unset, otherwise these object won't - * link into other scenes from this blend file. - */ - BKE_main_id_tag_all(bmain_dst, LIB_TAG_PRE_EXISTING, false); - BLO_blendhandle_close(bh); + + BlendfileLinkAppendContext *lapp_context = BKE_blendfile_link_append_context_new( + &liblink_params); + BKE_blendfile_link_append_context_library_add(lapp_context, libname, NULL); + + const int num_pasted = BKE_blendfile_link_append_context_item_idtypes_from_library_add( + lapp_context, reports, id_types_mask, 0); + if (num_pasted == BLENDFILE_LINK_APPEND_INVALID) { + BKE_blendfile_link_append_context_free(lapp_context); + return false; + } + + copybuffer_append(lapp_context, bmain_dst, reports); + + BKE_blendfile_link_append_context_free(lapp_context); return true; } @@ -155,59 +177,31 @@ int BKE_copybuffer_paste(bContext *C, Scene *scene = CTX_data_scene(C); ViewLayer *view_layer = CTX_data_view_layer(C); View3D *v3d = CTX_wm_view3d(C); /* may be NULL. */ - Main *mainl = NULL; - Library *lib; - BlendHandle *bh; const int id_tag_extra = 0; - BlendFileReadReport bf_reports = {.reports = reports}; - bh = BLO_blendhandle_from_file(libname, &bf_reports); - - if (bh == NULL) { - /* error reports will have been made by BLO_blendhandle_from_file() */ - return 0; - } - - BKE_view_layer_base_deselect_all(view_layer); + /* Note: No recursive append here, external linked data should remain linked. */ + BLI_assert((flag & BLO_LIBLINK_APPEND_RECURSIVE) == 0); - /* tag everything, all untagged data can be made local - * its also generally useful to know what is new - * - * take extra care BKE_main_id_flag_all(bmain, LIB_TAG_PRE_EXISTING, false) is called after! */ - BKE_main_id_tag_all(bmain, LIB_TAG_PRE_EXISTING, true); - - /* here appending/linking starts */ struct LibraryLink_Params liblink_params; BLO_library_link_params_init_with_context( &liblink_params, bmain, flag, id_tag_extra, scene, view_layer, v3d); - mainl = BLO_library_link_begin(&bh, libname, &liblink_params); - - const int num_pasted = BLO_library_link_copypaste(mainl, bh, id_types_mask); - BLO_library_link_end(mainl, &bh, &liblink_params); + BlendfileLinkAppendContext *lapp_context = BKE_blendfile_link_append_context_new( + &liblink_params); + BKE_blendfile_link_append_context_library_add(lapp_context, libname, NULL); - /* mark all library linked objects to be updated */ - BKE_main_lib_objects_recalc_all(bmain); - IMB_colormanagement_check_file_config(bmain); - - /* append, rather than linking */ - lib = BLI_findstring(&bmain->libraries, libname, offsetof(Library, filepath_abs)); - BKE_library_make_local(bmain, lib, NULL, true, false); - - /* important we unset, otherwise these object won't - * link into other scenes from this blend file */ - BKE_main_id_tag_all(bmain, LIB_TAG_PRE_EXISTING, false); - - /* recreate dependency graph to include new objects */ - DEG_relations_tag_update(bmain); + const int num_pasted = BKE_blendfile_link_append_context_item_idtypes_from_library_add( + lapp_context, reports, id_types_mask, 0); + if (num_pasted == BLENDFILE_LINK_APPEND_INVALID) { + BKE_blendfile_link_append_context_free(lapp_context); + return 0; + } - /* Tag update the scene to flush base collection settings, since the new object is added to a - * new (active) collection, not its original collection, thus need recalculation. */ - DEG_id_tag_update(&scene->id, 0); + BKE_view_layer_base_deselect_all(view_layer); - BLO_blendhandle_close(bh); - /* remove library... */ + copybuffer_append(lapp_context, bmain, reports); + BKE_blendfile_link_append_context_free(lapp_context); return num_pasted; } -- cgit v1.2.3