[Bf-blender-cvs] [c1f8b9753ac] master: Copy/Paste: refactor to be able to paste any kind of IDs, by type.

Bastien Montagne noreply at git.blender.org
Mon Mar 25 10:22:42 CET 2019


Commit: c1f8b9753acd6e61317dd7076eac5408fc48d7e6
Author: Bastien Montagne
Date:   Mon Mar 25 09:55:36 2019 +0100
Branches: master
https://developer.blender.org/rBc1f8b9753acd6e61317dd7076eac5408fc48d7e6

Copy/Paste: refactor to be able to paste any kind of IDs, by type.

This commit does not add anything new from user perspective, but make it
possible to paste any kind of IDs, not only objects/collections.

Will be used by new copy/paste in the outliner in next commit.

===================================================================

M	source/blender/blenkernel/BKE_blender_copybuffer.h
M	source/blender/blenkernel/intern/blender_copybuffer.c
M	source/blender/blenloader/BLO_readfile.h
M	source/blender/blenloader/intern/readfile.c
M	source/blender/editors/armature/pose_transform.c
M	source/blender/editors/space_view3d/view3d_ops.c

===================================================================

diff --git a/source/blender/blenkernel/BKE_blender_copybuffer.h b/source/blender/blenkernel/BKE_blender_copybuffer.h
index a98bb6a8c1c..99cd5109632 100644
--- a/source/blender/blenkernel/BKE_blender_copybuffer.h
+++ b/source/blender/blenkernel/BKE_blender_copybuffer.h
@@ -33,8 +33,10 @@ struct bContext;
 void BKE_copybuffer_begin(struct Main *bmain_src);
 void BKE_copybuffer_tag_ID(struct ID *id);
 bool BKE_copybuffer_save(struct Main *bmain_src, const char *filename, struct ReportList *reports);
-bool BKE_copybuffer_read(struct Main *bmain_dst, const char *libname, struct ReportList *reports);
-bool BKE_copybuffer_paste(struct bContext *C, const char *libname, const short flag, struct ReportList *reports);
+bool BKE_copybuffer_read(
+        struct Main *bmain_dst, const char *libname, struct ReportList *reports, const unsigned int id_types_mask);
+int BKE_copybuffer_paste(
+        struct bContext *C, const char *libname, const short flag, struct ReportList *reports, const unsigned int id_types_mask);
 
 #ifdef __cplusplus
 }
diff --git a/source/blender/blenkernel/intern/blender_copybuffer.c b/source/blender/blenkernel/intern/blender_copybuffer.c
index bc935f0b760..2ac013df620 100644
--- a/source/blender/blenkernel/intern/blender_copybuffer.c
+++ b/source/blender/blenkernel/intern/blender_copybuffer.c
@@ -82,7 +82,7 @@ bool BKE_copybuffer_save(Main *bmain_src, const char *filename, ReportList *repo
 	return retval;
 }
 
-bool BKE_copybuffer_read(Main *bmain_dst, const char *libname, ReportList *reports)
+bool BKE_copybuffer_read(Main *bmain_dst, const char *libname, ReportList *reports, const unsigned int id_types_mask)
 {
 	BlendHandle *bh = BLO_blendhandle_from_file(libname, reports);
 	if (bh == NULL) {
@@ -91,7 +91,7 @@ bool BKE_copybuffer_read(Main *bmain_dst, const char *libname, ReportList *repor
 	}
 	/* Here appending/linking starts. */
 	Main *mainl = BLO_library_link_begin(bmain_dst, &bh, libname);
-	BLO_library_link_copypaste(mainl, bh);
+	BLO_library_link_copypaste(mainl, bh, id_types_mask);
 	BLO_library_link_end(mainl, &bh, 0, NULL, NULL, NULL, NULL);
 	/* Mark all library linked objects to be updated. */
 	BKE_main_lib_objects_recalc_all(bmain_dst);
@@ -108,9 +108,10 @@ bool BKE_copybuffer_read(Main *bmain_dst, const char *libname, ReportList *repor
 }
 
 /**
- * \return Success.
+ * \return Number of IDs directly pasted from the buffer (does not includes indirectly pulled out ones).
  */
-bool BKE_copybuffer_paste(bContext *C, const char *libname, const short flag, ReportList *reports)
+int BKE_copybuffer_paste(
+        bContext *C, const char *libname, const short flag, ReportList *reports, const unsigned int id_types_mask)
 {
 	Main *bmain = CTX_data_main(C);
 	Scene *scene = CTX_data_scene(C);
@@ -124,7 +125,7 @@ bool BKE_copybuffer_paste(bContext *C, const char *libname, const short flag, Re
 
 	if (bh == NULL) {
 		/* error reports will have been made by BLO_blendhandle_from_file() */
-		return false;
+		return 0;
 	}
 
 	BKE_view_layer_base_deselect_all(view_layer);
@@ -138,7 +139,7 @@ bool BKE_copybuffer_paste(bContext *C, const char *libname, const short flag, Re
 	/* here appending/linking starts */
 	mainl = BLO_library_link_begin(bmain, &bh, libname);
 
-	BLO_library_link_copypaste(mainl, bh);
+	const int num_pasted = BLO_library_link_copypaste(mainl, bh, id_types_mask);
 
 	BLO_library_link_end(mainl, &bh, flag, bmain, scene, view_layer, v3d);
 
@@ -164,7 +165,7 @@ bool BKE_copybuffer_paste(bContext *C, const char *libname, const short flag, Re
 	BLO_blendhandle_close(bh);
 	/* remove library... */
 
-	return true;
+	return num_pasted;
 }
 
 /** \} */
diff --git a/source/blender/blenloader/BLO_readfile.h b/source/blender/blenloader/BLO_readfile.h
index c3a1c0e5185..bff35167792 100644
--- a/source/blender/blenloader/BLO_readfile.h
+++ b/source/blender/blenloader/BLO_readfile.h
@@ -138,7 +138,7 @@ void BLO_library_link_end(
         struct Main *mainl, BlendHandle **bh, int flag,
         struct Main *bmain, struct Scene *scene, struct ViewLayer *view_layer, const struct View3D *v3d);
 
-void BLO_library_link_copypaste(struct Main *mainl, BlendHandle *bh);
+int BLO_library_link_copypaste(struct Main *mainl, BlendHandle *bh, const unsigned int id_types_mask);
 
 void *BLO_library_read_struct(struct FileData *fd, struct BHead *bh, const char *blockname);
 
diff --git a/source/blender/blenloader/intern/readfile.c b/source/blender/blenloader/intern/readfile.c
index 6a0ec8cc2cb..3c5a0f136ba 100644
--- a/source/blender/blenloader/intern/readfile.c
+++ b/source/blender/blenloader/intern/readfile.c
@@ -11019,21 +11019,25 @@ static ID *link_named_part(
 /**
  * Simple reader for copy/paste buffers.
  */
-void BLO_library_link_copypaste(Main *mainl, BlendHandle *bh)
+int BLO_library_link_copypaste(Main *mainl, BlendHandle *bh, const unsigned int id_types_mask)
 {
 	FileData *fd = (FileData *)(bh);
 	BHead *bhead;
+	int num_directly_linked = 0;
 
 	for (bhead = blo_bhead_first(fd); bhead; bhead = blo_bhead_next(fd, bhead)) {
 		ID *id = NULL;
 
 		if (bhead->code == ENDB)
 			break;
-		if (ELEM(bhead->code, ID_OB, ID_GR)) {
+
+		if (BKE_idcode_is_valid(bhead->code) && BKE_idcode_is_linkable(bhead->code) &&
+		    (id_types_mask == 0 || (BKE_idcode_to_idfilter((short)bhead->code) & id_types_mask) != 0))
+		{
 			read_libblock(fd, mainl, bhead, LIB_TAG_NEED_EXPAND | LIB_TAG_INDIRECT, &id);
+			num_directly_linked++;
 		}
 
-
 		if (id) {
 			/* sort by name in list */
 			ListBase *lb = which_libbase(mainl, GS(id->name));
@@ -11049,6 +11053,8 @@ void BLO_library_link_copypaste(Main *mainl, BlendHandle *bh)
 			}
 		}
 	}
+
+	return num_directly_linked;
 }
 
 static ID *link_named_part_ex(
diff --git a/source/blender/editors/armature/pose_transform.c b/source/blender/editors/armature/pose_transform.c
index 3c21919a7ac..cbb6d63aefe 100644
--- a/source/blender/editors/armature/pose_transform.c
+++ b/source/blender/editors/armature/pose_transform.c
@@ -530,7 +530,7 @@ static int pose_paste_exec(bContext *C, wmOperator *op)
 	char str[FILE_MAX];
 	Main *tmp_bmain = BKE_main_new();
 	BLI_make_file_string("/", str, BKE_tempdir_base(), "copybuffer_pose.blend");
-	if (!BKE_copybuffer_read(tmp_bmain, str, op->reports)) {
+	if (!BKE_copybuffer_read(tmp_bmain, str, op->reports, FILTER_ID_OB)) {
 		BKE_report(op->reports, RPT_ERROR, "Copy buffer is empty");
 		BKE_main_free(tmp_bmain);
 		return OPERATOR_CANCELLED;
diff --git a/source/blender/editors/space_view3d/view3d_ops.c b/source/blender/editors/space_view3d/view3d_ops.c
index 31c9be02311..1ec75e06cc9 100644
--- a/source/blender/editors/space_view3d/view3d_ops.c
+++ b/source/blender/editors/space_view3d/view3d_ops.c
@@ -107,17 +107,18 @@ static int view3d_pastebuffer_exec(bContext *C, wmOperator *op)
 		flag |= FILE_ACTIVE_COLLECTION;
 
 	BLI_make_file_string("/", str, BKE_tempdir_base(), "copybuffer.blend");
-	if (BKE_copybuffer_paste(C, str, flag, op->reports)) {
-		WM_event_add_notifier(C, NC_WINDOW, NULL);
 
-		BKE_report(op->reports, RPT_INFO, "Objects pasted from buffer");
-
-		return OPERATOR_FINISHED;
+	const int num_pasted = BKE_copybuffer_paste(C, str, flag, op->reports, FILTER_ID_OB);
+	if (num_pasted == 0) {
+		BKE_report(op->reports, RPT_INFO, "No buffer to paste from");
+		return OPERATOR_CANCELLED;
 	}
 
-	BKE_report(op->reports, RPT_INFO, "No buffer to paste from");
+	WM_event_add_notifier(C, NC_WINDOW, NULL);
+
+	BKE_reportf(op->reports, RPT_INFO, "%d objects pasted from buffer", num_pasted);
 
-	return OPERATOR_CANCELLED;
+	return OPERATOR_FINISHED;
 }
 
 static void VIEW3D_OT_pastebuffer(wmOperatorType *ot)



More information about the Bf-blender-cvs mailing list