2023-08-16 00:20:26 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2023 Blender Authors
|
2023-05-31 16:19:06 +02:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2016-04-24 22:42:41 +10:00
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
|
* \ingroup bke
|
2016-04-24 22:42:41 +10:00
|
|
|
*
|
|
|
|
|
* Used for copy/paste operator, (using a temporary file).
|
|
|
|
|
*/
|
|
|
|
|
|
2023-07-22 11:27:25 +10:00
|
|
|
#include <cstdlib>
|
2016-04-24 22:42:41 +10:00
|
|
|
|
|
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
2024-02-19 15:54:48 +01:00
|
|
|
#include "DNA_scene_types.h"
|
|
|
|
|
#include "DNA_screen_types.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "DNA_userdef_types.h"
|
2024-02-19 15:54:48 +01:00
|
|
|
#include "DNA_view3d_types.h"
|
|
|
|
|
#include "DNA_windowmanager_types.h"
|
2016-04-24 22:42:41 +10:00
|
|
|
|
2024-02-19 15:54:48 +01:00
|
|
|
#include "BLI_blenlib.h"
|
2016-04-24 22:42:41 +10:00
|
|
|
#include "BLI_utildefines.h"
|
|
|
|
|
|
2024-02-19 15:54:48 +01:00
|
|
|
#include "IMB_imbuf.hh"
|
|
|
|
|
#include "IMB_moviecache.hh"
|
|
|
|
|
|
2024-01-21 19:34:20 +01:00
|
|
|
#include "BKE_blender_copybuffer.hh" /* own include */
|
2023-12-13 12:36:45 +01:00
|
|
|
#include "BKE_blendfile.hh"
|
|
|
|
|
#include "BKE_blendfile_link_append.hh"
|
2023-11-16 11:41:55 +01:00
|
|
|
#include "BKE_context.hh"
|
2024-02-19 15:54:48 +01:00
|
|
|
#include "BKE_global.h"
|
2024-01-23 15:18:09 -05:00
|
|
|
#include "BKE_layer.hh"
|
2024-01-15 12:44:04 -05:00
|
|
|
#include "BKE_lib_id.hh"
|
2024-02-19 15:54:48 +01:00
|
|
|
#include "BKE_main.hh"
|
|
|
|
|
#include "BKE_scene.h"
|
2016-04-24 22:42:41 +10:00
|
|
|
|
2024-02-19 15:54:48 +01:00
|
|
|
#include "DEG_depsgraph.hh"
|
2023-09-22 03:18:17 +02:00
|
|
|
#include "DEG_depsgraph_build.hh"
|
2017-04-06 16:11:50 +02:00
|
|
|
|
2024-02-19 15:54:48 +01:00
|
|
|
#include "BLO_readfile.h"
|
2023-08-28 15:01:05 +02:00
|
|
|
#include "BLO_writefile.hh"
|
2016-04-24 22:42:41 +10:00
|
|
|
|
2024-01-18 22:50:23 +02:00
|
|
|
#include "IMB_colormanagement.hh"
|
2016-04-24 22:42:41 +10:00
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Copy/Paste `.blend`, partial saves.
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2021-11-12 10:17:15 +01:00
|
|
|
void BKE_copybuffer_copy_begin(Main *bmain_src)
|
2016-04-24 22:42:41 +10:00
|
|
|
{
|
|
|
|
|
BKE_blendfile_write_partial_begin(bmain_src);
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-12 10:17:15 +01:00
|
|
|
void BKE_copybuffer_copy_tag_ID(ID *id)
|
2016-04-24 22:42:41 +10:00
|
|
|
{
|
|
|
|
|
BKE_blendfile_write_partial_tag_ID(id, true);
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-12 10:17:15 +01:00
|
|
|
bool BKE_copybuffer_copy_end(Main *bmain_src, const char *filename, ReportList *reports)
|
2016-04-24 22:42:41 +10:00
|
|
|
{
|
2020-06-18 15:25:22 +10:00
|
|
|
const int write_flags = 0;
|
|
|
|
|
const eBLO_WritePathRemap remap_mode = BLO_WRITE_PATH_REMAP_RELATIVE;
|
2016-04-24 22:42:41 +10:00
|
|
|
|
2020-06-18 15:25:22 +10:00
|
|
|
bool retval = BKE_blendfile_write_partial(bmain_src, filename, write_flags, remap_mode, reports);
|
2016-04-24 22:42:41 +10:00
|
|
|
|
|
|
|
|
BKE_blendfile_write_partial_end(bmain_src);
|
|
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-30 17:11:25 +01:00
|
|
|
/* 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);
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-25 09:55:36 +01:00
|
|
|
bool BKE_copybuffer_read(Main *bmain_dst,
|
|
|
|
|
const char *libname,
|
|
|
|
|
ReportList *reports,
|
2020-03-03 17:21:28 +01:00
|
|
|
const uint64_t id_types_mask)
|
2016-09-14 17:50:11 +02:00
|
|
|
{
|
2022-05-13 09:24:28 +10:00
|
|
|
/* NOTE: No recursive append here (no `BLO_LIBLINK_APPEND_RECURSIVE`), external linked data
|
2021-11-30 17:11:25 +01:00
|
|
|
* should remain linked. */
|
2020-09-08 15:32:43 +10:00
|
|
|
const int flag = 0;
|
2021-03-09 00:53:13 +11:00
|
|
|
const int id_tag_extra = 0;
|
2023-06-03 08:36:28 +10:00
|
|
|
LibraryLink_Params liblink_params;
|
2021-03-09 00:53:13 +11:00
|
|
|
BLO_library_link_params_init(&liblink_params, bmain_dst, flag, id_tag_extra);
|
2021-11-30 17:11:25 +01:00
|
|
|
|
|
|
|
|
BlendfileLinkAppendContext *lapp_context = BKE_blendfile_link_append_context_new(
|
|
|
|
|
&liblink_params);
|
2023-07-17 10:46:26 +02:00
|
|
|
BKE_blendfile_link_append_context_library_add(lapp_context, libname, nullptr);
|
2021-11-30 17:11:25 +01:00
|
|
|
|
|
|
|
|
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);
|
2016-09-14 17:50:11 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-25 09:55:36 +01:00
|
|
|
int BKE_copybuffer_paste(bContext *C,
|
|
|
|
|
const char *libname,
|
2021-11-12 10:17:15 +01:00
|
|
|
const int flag,
|
2019-03-25 09:55:36 +01:00
|
|
|
ReportList *reports,
|
2020-03-03 17:21:28 +01:00
|
|
|
const uint64_t id_types_mask)
|
2016-04-24 22:42:41 +10:00
|
|
|
{
|
|
|
|
|
Main *bmain = CTX_data_main(C);
|
|
|
|
|
Scene *scene = CTX_data_scene(C);
|
2017-11-22 10:52:39 -02:00
|
|
|
ViewLayer *view_layer = CTX_data_view_layer(C);
|
2023-07-17 10:46:26 +02:00
|
|
|
View3D *v3d = CTX_wm_view3d(C); /* may be nullptr. */
|
2021-03-09 00:53:13 +11:00
|
|
|
const int id_tag_extra = 0;
|
2016-04-24 22:42:41 +10:00
|
|
|
|
2022-05-13 09:24:28 +10:00
|
|
|
/* NOTE: No recursive append here, external linked data should remain linked. */
|
2021-11-30 17:11:25 +01:00
|
|
|
BLI_assert((flag & BLO_LIBLINK_APPEND_RECURSIVE) == 0);
|
2016-04-24 22:42:41 +10:00
|
|
|
|
2023-06-03 08:36:28 +10:00
|
|
|
LibraryLink_Params liblink_params;
|
2021-03-09 00:53:13 +11:00
|
|
|
BLO_library_link_params_init_with_context(
|
|
|
|
|
&liblink_params, bmain, flag, id_tag_extra, scene, view_layer, v3d);
|
2016-04-24 22:42:41 +10:00
|
|
|
|
2021-11-30 17:11:25 +01:00
|
|
|
BlendfileLinkAppendContext *lapp_context = BKE_blendfile_link_append_context_new(
|
|
|
|
|
&liblink_params);
|
2023-07-17 10:46:26 +02:00
|
|
|
BKE_blendfile_link_append_context_library_add(lapp_context, libname, nullptr);
|
2016-04-24 22:42:41 +10:00
|
|
|
|
2021-11-30 17:11:25 +01:00
|
|
|
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;
|
|
|
|
|
}
|
2016-04-24 22:42:41 +10:00
|
|
|
|
2022-09-14 21:30:20 +02:00
|
|
|
BKE_view_layer_base_deselect_all(scene, view_layer);
|
2017-08-16 12:14:31 +02:00
|
|
|
|
2021-11-30 17:11:25 +01:00
|
|
|
copybuffer_append(lapp_context, bmain, reports);
|
2016-04-24 22:42:41 +10:00
|
|
|
|
2021-11-30 17:11:25 +01:00
|
|
|
BKE_blendfile_link_append_context_free(lapp_context);
|
2019-03-25 09:55:36 +01:00
|
|
|
return num_pasted;
|
2016-04-24 22:42:41 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** \} */
|