Blender  V2.93
blender_copybuffer.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
23 #include <stdlib.h>
24 
25 #include "MEM_guardedalloc.h"
26 
27 #include "DNA_scene_types.h"
28 #include "DNA_screen_types.h"
29 #include "DNA_userdef_types.h"
30 #include "DNA_view3d_types.h"
32 
33 #include "BLI_blenlib.h"
34 #include "BLI_utildefines.h"
35 
36 #include "IMB_imbuf.h"
37 #include "IMB_moviecache.h"
38 
39 #include "BKE_blender_copybuffer.h" /* own include */
40 #include "BKE_blendfile.h"
41 #include "BKE_context.h"
42 #include "BKE_global.h"
43 #include "BKE_layer.h"
44 #include "BKE_lib_id.h"
45 #include "BKE_main.h"
46 #include "BKE_scene.h"
47 
48 #include "DEG_depsgraph.h"
49 #include "DEG_depsgraph_build.h"
50 
51 #include "BLO_readfile.h"
52 #include "BLO_writefile.h"
53 
54 #include "IMB_colormanagement.h"
55 
56 /* -------------------------------------------------------------------- */
60 void BKE_copybuffer_begin(Main *bmain_src)
61 {
63 }
64 
66 {
68 }
69 
73 bool BKE_copybuffer_save(Main *bmain_src, const char *filename, ReportList *reports)
74 {
75  const int write_flags = 0;
77 
78  bool retval = BKE_blendfile_write_partial(bmain_src, filename, write_flags, remap_mode, reports);
79 
81 
82  return retval;
83 }
84 
85 bool BKE_copybuffer_read(Main *bmain_dst,
86  const char *libname,
87  ReportList *reports,
88  const uint64_t id_types_mask)
89 {
90  BlendHandle *bh = BLO_blendhandle_from_file(libname, reports);
91  if (bh == NULL) {
92  /* Error reports will have been made by BLO_blendhandle_from_file(). */
93  return false;
94  }
95  /* Here appending/linking starts. */
96  const int flag = 0;
97  const int id_tag_extra = 0;
98  struct LibraryLink_Params liblink_params;
99  BLO_library_link_params_init(&liblink_params, bmain_dst, flag, id_tag_extra);
100  Main *mainl = BLO_library_link_begin(&bh, libname, &liblink_params);
101  BLO_library_link_copypaste(mainl, bh, id_types_mask);
102  BLO_library_link_end(mainl, &bh, &liblink_params);
103  /* Mark all library linked objects to be updated. */
106  /* Append, rather than linking. */
107  Library *lib = BLI_findstring(&bmain_dst->libraries, libname, offsetof(Library, filepath_abs));
108  BKE_library_make_local(bmain_dst, lib, NULL, true, false);
109  /* Important we unset, otherwise these object wont
110  * link into other scenes from this blend file.
111  */
112  BKE_main_id_tag_all(bmain_dst, LIB_TAG_PRE_EXISTING, false);
114  return true;
115 }
116 
122  const char *libname,
123  const short flag,
124  ReportList *reports,
125  const uint64_t id_types_mask)
126 {
130  View3D *v3d = CTX_wm_view3d(C); /* may be NULL. */
131  Main *mainl = NULL;
132  Library *lib;
133  BlendHandle *bh;
134  const int id_tag_extra = 0;
135 
136  bh = BLO_blendhandle_from_file(libname, reports);
137 
138  if (bh == NULL) {
139  /* error reports will have been made by BLO_blendhandle_from_file() */
140  return 0;
141  }
142 
144 
145  /* tag everything, all untagged data can be made local
146  * its also generally useful to know what is new
147  *
148  * take extra care BKE_main_id_flag_all(bmain, LIB_TAG_PRE_EXISTING, false) is called after! */
150 
151  /* here appending/linking starts */
152  struct LibraryLink_Params liblink_params;
154  &liblink_params, bmain, flag, id_tag_extra, scene, view_layer, v3d);
155  mainl = BLO_library_link_begin(&bh, libname, &liblink_params);
156 
157  const int num_pasted = BLO_library_link_copypaste(mainl, bh, id_types_mask);
158 
159  BLO_library_link_end(mainl, &bh, &liblink_params);
160 
161  /* mark all library linked objects to be updated */
164 
165  /* append, rather than linking */
166  lib = BLI_findstring(&bmain->libraries, libname, offsetof(Library, filepath_abs));
167  BKE_library_make_local(bmain, lib, NULL, true, false);
168 
169  /* important we unset, otherwise these object wont
170  * link into other scenes from this blend file */
172 
173  /* recreate dependency graph to include new objects */
175 
176  /* Tag update the scene to flush base collection settings, since the new object is added to a
177  * new (active) collection, not its original collection, thus need recalculation. */
178  DEG_id_tag_update(&scene->id, 0);
179 
181  /* remove library... */
182 
183  return num_pasted;
184 }
185 
void BKE_blendfile_write_partial_begin(struct Main *bmain_src)
Definition: blendfile.c:837
void BKE_blendfile_write_partial_end(struct Main *bmain_src)
Definition: blendfile.c:952
bool BKE_blendfile_write_partial(struct Main *bmain_src, const char *filepath, const int write_flags, const int remap_mode, struct ReportList *reports)
Definition: blendfile.c:871
void BKE_blendfile_write_partial_tag_ID(struct ID *id, bool set)
Definition: blendfile.c:842
struct Scene * CTX_data_scene(const bContext *C)
Definition: context.c:1034
struct ViewLayer * CTX_data_view_layer(const bContext *C)
Definition: context.c:1044
struct View3D * CTX_wm_view3d(const bContext *C)
Definition: context.c:760
struct Main * CTX_data_main(const bContext *C)
Definition: context.c:1018
void BKE_view_layer_base_deselect_all(struct ViewLayer *view_layer)
Definition: layer.c:403
void BKE_main_id_tag_all(struct Main *mainvar, const int tag, const bool value)
Definition: lib_id.c:923
void BKE_main_lib_objects_recalc_all(struct Main *bmain)
Definition: lib_id.c:997
void BKE_library_make_local(struct Main *bmain, const struct Library *lib, struct GHash *old_to_new_ids, const bool untagged_only, const bool set_fake)
void * BLI_findstring(const struct ListBase *listbase, const char *id, const int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
external readfile function prototypes.
int BLO_library_link_copypaste(struct Main *mainl, BlendHandle *bh, const uint64_t id_types_mask)
Definition: readfile.c:4941
void BLO_library_link_params_init(struct LibraryLink_Params *params, struct Main *bmain, const int flag, const int id_tag_extra)
Definition: readfile.c:5069
void BLO_library_link_params_init_with_context(struct LibraryLink_Params *params, struct Main *bmain, const int flag, const int id_tag_extra, struct Scene *scene, struct ViewLayer *view_layer, const struct View3D *v3d)
Definition: readfile.c:5080
struct BlendHandle BlendHandle
Definition: BLO_readfile.h:49
struct Main * BLO_library_link_begin(BlendHandle **bh, const char *filepath, const struct LibraryLink_Params *params)
Definition: readfile.c:5109
void BLO_library_link_end(struct Main *mainl, BlendHandle **bh, const struct LibraryLink_Params *params)
Definition: readfile.c:5258
BlendHandle * BLO_blendhandle_from_file(const char *filepath, struct ReportList *reports)
Definition: readblenentry.c:71
void BLO_blendhandle_close(BlendHandle *bh)
external writefile function prototypes.
eBLO_WritePathRemap
Definition: BLO_writefile.h:41
@ BLO_WRITE_PATH_REMAP_RELATIVE
Definition: BLO_writefile.h:45
void DEG_id_tag_update(struct ID *id, int flag)
void DEG_relations_tag_update(struct Main *bmain)
@ LIB_TAG_PRE_EXISTING
Definition: DNA_ID.h:556
void IMB_colormanagement_check_file_config(struct Main *bmain)
Read Guarded memory(de)allocation.
#define C
Definition: RandGen.cpp:39
void BKE_copybuffer_begin(Main *bmain_src)
bool BKE_copybuffer_save(Main *bmain_src, const char *filename, ReportList *reports)
bool BKE_copybuffer_read(Main *bmain_dst, const char *libname, ReportList *reports, const uint64_t id_types_mask)
void BKE_copybuffer_tag_ID(ID *id)
int BKE_copybuffer_paste(bContext *C, const char *libname, const short flag, ReportList *reports, const uint64_t id_types_mask)
Scene scene
DRWShaderLibrary * lib
unsigned __int64 uint64_t
Definition: stdint.h:93
Definition: DNA_ID.h:273
Definition: BKE_main.h:116
ListBase libraries
Definition: BKE_main.h:147