| 
									
										
										
										
											2016-04-24 22:42:41 +10:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * This program is free software; you can redistribute it and/or | 
					
						
							|  |  |  |  * modify it under the terms of the GNU General Public License | 
					
						
							|  |  |  |  * as published by the Free Software Foundation; either version 2 | 
					
						
							|  |  |  |  * of the License, or (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  |  * along with this program; if not, write to the Free Software Foundation, | 
					
						
							|  |  |  |  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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). | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <stdlib.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "MEM_guardedalloc.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "DNA_userdef_types.h"
 | 
					
						
							|  |  |  | #include "DNA_scene_types.h"
 | 
					
						
							|  |  |  | #include "DNA_screen_types.h"
 | 
					
						
							|  |  |  | #include "DNA_view3d_types.h"
 | 
					
						
							|  |  |  | #include "DNA_windowmanager_types.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "BLI_blenlib.h"
 | 
					
						
							|  |  |  | #include "BLI_utildefines.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "IMB_imbuf.h"
 | 
					
						
							|  |  |  | #include "IMB_moviecache.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "BKE_blender_copybuffer.h"  /* own include */
 | 
					
						
							|  |  |  | #include "BKE_blendfile.h"
 | 
					
						
							|  |  |  | #include "BKE_context.h"
 | 
					
						
							|  |  |  | #include "BKE_global.h"
 | 
					
						
							| 
									
										
										
										
											2017-11-08 12:16:49 -02:00
										 |  |  | #include "BKE_layer.h"
 | 
					
						
							| 
									
										
										
										
											2016-04-24 22:42:41 +10:00
										 |  |  | #include "BKE_library.h"
 | 
					
						
							|  |  |  | #include "BKE_main.h"
 | 
					
						
							|  |  |  | #include "BKE_scene.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-16 12:14:31 +02:00
										 |  |  | #include "DEG_depsgraph.h"
 | 
					
						
							| 
									
										
										
										
											2017-04-06 16:11:50 +02:00
										 |  |  | #include "DEG_depsgraph_build.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-24 22:42:41 +10:00
										 |  |  | #include "BLO_readfile.h"
 | 
					
						
							|  |  |  | #include "BLO_writefile.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "IMB_colormanagement.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* -------------------------------------------------------------------- */ | 
					
						
							|  |  |  | /** \name Copy/Paste `.blend`, partial saves.
 | 
					
						
							|  |  |  |  * \{ */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void BKE_copybuffer_begin(Main *bmain_src) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	BKE_blendfile_write_partial_begin(bmain_src); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void BKE_copybuffer_tag_ID(ID *id) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	BKE_blendfile_write_partial_tag_ID(id, true); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * \return Success. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | bool BKE_copybuffer_save(Main *bmain_src, const char *filename, ReportList *reports) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	const int write_flags = G_FILE_RELATIVE_REMAP; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bool retval = BKE_blendfile_write_partial(bmain_src, filename, write_flags, reports); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	BKE_blendfile_write_partial_end(bmain_src); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return retval; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-25 09:55:36 +01:00
										 |  |  | bool BKE_copybuffer_read(Main *bmain_dst, const char *libname, ReportList *reports, const unsigned int id_types_mask) | 
					
						
							| 
									
										
										
										
											2016-09-14 17:50:11 +02:00
										 |  |  | { | 
					
						
							|  |  |  | 	BlendHandle *bh = BLO_blendhandle_from_file(libname, reports); | 
					
						
							|  |  |  | 	if (bh == NULL) { | 
					
						
							|  |  |  | 		/* Error reports will have been made by BLO_blendhandle_from_file(). */ | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	/* Here appending/linking starts. */ | 
					
						
							|  |  |  | 	Main *mainl = BLO_library_link_begin(bmain_dst, &bh, libname); | 
					
						
							| 
									
										
										
										
											2019-03-25 09:55:36 +01:00
										 |  |  | 	BLO_library_link_copypaste(mainl, bh, id_types_mask); | 
					
						
							| 
									
										
										
										
											2018-12-18 16:23:38 +11:00
										 |  |  | 	BLO_library_link_end(mainl, &bh, 0, NULL, NULL, NULL, NULL); | 
					
						
							| 
									
										
										
										
											2016-09-14 17:50:11 +02:00
										 |  |  | 	/* 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. */ | 
					
						
							| 
									
										
										
										
											2019-03-08 09:29:17 +11:00
										 |  |  | 	Library *lib = BLI_findstring(&bmain_dst->libraries, libname, offsetof(Library, filepath)); | 
					
						
							| 
									
										
										
										
											2016-11-01 13:39:31 +01:00
										 |  |  | 	BKE_library_make_local(bmain_dst, lib, NULL, true, false); | 
					
						
							| 
									
										
										
										
											2016-09-14 17:50:11 +02:00
										 |  |  | 	/* Important we unset, otherwise these object wont
 | 
					
						
							|  |  |  | 	 * link into other scenes from this blend file. | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	BKE_main_id_tag_all(bmain_dst, LIB_TAG_PRE_EXISTING, false); | 
					
						
							|  |  |  | 	BLO_blendhandle_close(bh); | 
					
						
							|  |  |  | 	return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-24 22:42:41 +10:00
										 |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2019-03-25 09:55:36 +01:00
										 |  |  |  * \return Number of IDs directly pasted from the buffer (does not includes indirectly pulled out ones). | 
					
						
							| 
									
										
										
										
											2016-04-24 22:42:41 +10:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2019-03-25 09:55:36 +01:00
										 |  |  | int BKE_copybuffer_paste( | 
					
						
							|  |  |  |         bContext *C, const char *libname, const short flag, ReportList *reports, const unsigned int 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); | 
					
						
							| 
									
										
										
										
											2018-12-18 16:23:38 +11:00
										 |  |  | 	View3D *v3d = CTX_wm_view3d(C);  /* may be NULL. */ | 
					
						
							| 
									
										
										
										
											2016-04-24 22:42:41 +10:00
										 |  |  | 	Main *mainl = NULL; | 
					
						
							|  |  |  | 	Library *lib; | 
					
						
							|  |  |  | 	BlendHandle *bh; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bh = BLO_blendhandle_from_file(libname, reports); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (bh == NULL) { | 
					
						
							|  |  |  | 		/* error reports will have been made by BLO_blendhandle_from_file() */ | 
					
						
							| 
									
										
										
										
											2019-03-25 09:55:36 +01:00
										 |  |  | 		return 0; | 
					
						
							| 
									
										
										
										
											2016-04-24 22:42:41 +10:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-22 10:52:39 -02:00
										 |  |  | 	BKE_view_layer_base_deselect_all(view_layer); | 
					
						
							| 
									
										
										
										
											2016-04-24 22:42:41 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* 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 */ | 
					
						
							|  |  |  | 	mainl = BLO_library_link_begin(bmain, &bh, libname); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-25 09:55:36 +01:00
										 |  |  | 	const int num_pasted = BLO_library_link_copypaste(mainl, bh, id_types_mask); | 
					
						
							| 
									
										
										
										
											2016-04-24 22:42:41 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-18 16:23:38 +11:00
										 |  |  | 	BLO_library_link_end(mainl, &bh, flag, bmain, scene, view_layer, v3d); | 
					
						
							| 
									
										
										
										
											2016-04-24 22:42:41 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* 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 */ | 
					
						
							| 
									
										
										
										
											2019-03-08 09:29:17 +11:00
										 |  |  | 	lib = BLI_findstring(&bmain->libraries, libname, offsetof(Library, filepath)); | 
					
						
							| 
									
										
										
										
											2016-11-01 13:39:31 +01:00
										 |  |  | 	BKE_library_make_local(bmain, lib, NULL, true, false); | 
					
						
							| 
									
										
										
										
											2016-04-24 22:42:41 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* important we unset, otherwise these object wont
 | 
					
						
							|  |  |  | 	 * 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 */ | 
					
						
							| 
									
										
										
										
											2017-04-06 16:11:50 +02:00
										 |  |  | 	DEG_relations_tag_update(bmain); | 
					
						
							| 
									
										
										
										
											2016-04-24 22:42:41 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-16 12:14:31 +02:00
										 |  |  | 	/* 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); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-24 22:42:41 +10:00
										 |  |  | 	BLO_blendhandle_close(bh); | 
					
						
							|  |  |  | 	/* remove library... */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-25 09:55:36 +01:00
										 |  |  | 	return num_pasted; | 
					
						
							| 
									
										
										
										
											2016-04-24 22:42:41 +10:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** \} */ |