The `pre` handler is called after blender internal code is done populating the link/append context with data to be processed, and before this data starts being linked from library files. The `post` handler is called after blender is done linking, and potentailly appending and/or instantiating, the requested data and all of their dependencies. Both handlers are called with a single argument, the link/append context. An new RNA sets of wrappers have been added to expose relevant info from these internal C++ structures. NOTE: !113658 is very similar (but tied to asset drag & drop), whereas this PR is more general (these could probably live hand-in-hand / side- by-side). Implements #122357 Pull Request: https://projects.blender.org/blender/blender/pulls/128279 ----------------- Some quick py example code: ```python import bpy def my_handler_pre(lapp_context): print("About to {}:\n\t".format("link" if "LINK" in lapp_context.options else "append"), "\n\t".join("{} '{}', from libs ['{}']".format(item.id_type, item.name, "', '".join([l.filepath for l in item.source_libraries])) for item in lapp_context.import_items)) def my_handler_post(lapp_context): print("{}:\n\t".format("Linked" if "LINK" in lapp_context.options else "Appended"), "\n\t".join("{} '{}', from lib '{}'".format(item.id.id_type, item.id.name, item.source_library.filepath) for item in lapp_context.import_items)) bpy.app.handlers.link_append_pre.append(my_handler_pre) bpy.app.handlers.link_append_post.append(my_handler_post) ```
136 lines
4.3 KiB
C++
136 lines
4.3 KiB
C++
/* SPDX-FileCopyrightText: 2023 Blender Authors
|
|
*
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
|
|
|
/** \file
|
|
* \ingroup bke
|
|
*
|
|
* Used for copy/paste operator, (using a temporary file).
|
|
*/
|
|
|
|
#include <cstdlib>
|
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
#include "DNA_userdef_types.h"
|
|
|
|
#include "BLI_utildefines.h"
|
|
|
|
#include "BKE_blender_copybuffer.hh" /* own include */
|
|
#include "BKE_blendfile_link_append.hh"
|
|
#include "BKE_context.hh"
|
|
#include "BKE_layer.hh"
|
|
#include "BKE_lib_id.hh"
|
|
|
|
#include "DEG_depsgraph_build.hh"
|
|
|
|
#include "BLO_readfile.hh"
|
|
#include "BLO_writefile.hh"
|
|
|
|
#include "IMB_colormanagement.hh"
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
/** \name Paste API based on 'partial' blend-files.
|
|
* \{ */
|
|
|
|
/* 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, ID_TAG_PRE_EXISTING, true);
|
|
|
|
BKE_blendfile_link_append_context_init_done(lapp_context);
|
|
|
|
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);
|
|
|
|
/* Instantiate loose data in the scene (e.g. add object to the active collection). */
|
|
BKE_blendfile_link_append_instantiate_loose(lapp_context, reports);
|
|
|
|
BKE_blendfile_link_append_context_finalize(lapp_context);
|
|
|
|
/* This must be unset, otherwise these object won't link into other scenes from this blend
|
|
* file. */
|
|
BKE_main_id_tag_all(bmain, ID_TAG_PRE_EXISTING, false);
|
|
|
|
/* Recreate dependency graph to include new objects. */
|
|
DEG_relations_tag_update(bmain);
|
|
}
|
|
|
|
bool BKE_copybuffer_read(Main *bmain_dst,
|
|
const char *libname,
|
|
ReportList *reports,
|
|
const uint64_t id_types_mask)
|
|
{
|
|
/* 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;
|
|
LibraryLink_Params liblink_params;
|
|
BLO_library_link_params_init(&liblink_params, bmain_dst, flag, id_tag_extra);
|
|
|
|
BlendfileLinkAppendContext *lapp_context = BKE_blendfile_link_append_context_new(
|
|
&liblink_params);
|
|
BKE_blendfile_link_append_context_library_add(lapp_context, libname, nullptr);
|
|
|
|
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;
|
|
}
|
|
|
|
int BKE_copybuffer_paste(bContext *C,
|
|
const char *libname,
|
|
const int flag,
|
|
ReportList *reports,
|
|
const uint64_t id_types_mask)
|
|
{
|
|
Main *bmain = CTX_data_main(C);
|
|
Scene *scene = CTX_data_scene(C);
|
|
ViewLayer *view_layer = CTX_data_view_layer(C);
|
|
View3D *v3d = CTX_wm_view3d(C); /* may be nullptr. */
|
|
const int id_tag_extra = 0;
|
|
|
|
/* NOTE: No recursive append here, external linked data should remain linked. */
|
|
BLI_assert((flag & BLO_LIBLINK_APPEND_RECURSIVE) == 0);
|
|
|
|
LibraryLink_Params liblink_params;
|
|
BLO_library_link_params_init_with_context(
|
|
&liblink_params, bmain, flag, id_tag_extra, scene, view_layer, v3d);
|
|
|
|
BlendfileLinkAppendContext *lapp_context = BKE_blendfile_link_append_context_new(
|
|
&liblink_params);
|
|
BKE_blendfile_link_append_context_library_add(lapp_context, libname, nullptr);
|
|
|
|
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;
|
|
}
|
|
|
|
BKE_view_layer_base_deselect_all(scene, view_layer);
|
|
|
|
copybuffer_append(lapp_context, bmain, reports);
|
|
|
|
BKE_blendfile_link_append_context_free(lapp_context);
|
|
return num_pasted;
|
|
}
|
|
|
|
/** \} */
|