[Bf-blender-cvs] [4137ef1] gooseberry: Gooseberry animation request: Paste flipped pose in action and graph editor.

Antony Riakiotakis noreply at git.blender.org
Wed Jan 7 22:28:31 CET 2015


Commit: 4137ef10da3bcf7036841117c293a60bf51ad1ab
Author: Antony Riakiotakis
Date:   Wed Jan 7 22:25:33 2015 +0100
Branches: gooseberry
https://developer.blender.org/rB4137ef10da3bcf7036841117c293a60bf51ad1ab

Gooseberry animation request: Paste flipped pose in action
 and graph editor.

This was a tricky commit that was not so straightforward to make work.
The information for bones is not easy to come by in the animation curves,
however we do have some string manipulation tricks to make it happen.
For now committing to gooseberry branch for testing, it will be ported
to master after most usual use cases have been confirmed as working.

(Current test with all rotation modes and translation seems to work, but
Not sure how well this will hold up with drivers etc)

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

M	release/scripts/startup/bl_ui/space_dopesheet.py
M	release/scripts/startup/bl_ui/space_graph.py
M	source/blender/blenlib/BLI_string.h
M	source/blender/blenlib/intern/string.c
M	source/blender/editors/animation/keyframes_general.c
M	source/blender/editors/include/ED_keyframes_edit.h
M	source/blender/editors/space_action/action_edit.c
M	source/blender/editors/space_graph/graph_edit.c

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

diff --git a/release/scripts/startup/bl_ui/space_dopesheet.py b/release/scripts/startup/bl_ui/space_dopesheet.py
index 0458ffe..f901d9d 100644
--- a/release/scripts/startup/bl_ui/space_dopesheet.py
+++ b/release/scripts/startup/bl_ui/space_dopesheet.py
@@ -131,6 +131,8 @@ class DOPESHEET_HT_header(Header):
         row = layout.row(align=True)
         row.operator("action.copy", text="", icon='COPYDOWN')
         row.operator("action.paste", text="", icon='PASTEDOWN')
+        row.operator("action.paste", text="", icon='PASTEFLIPDOWN').flipped = True
+
 
 
 class DOPESHEET_MT_editor_menus(Menu):
diff --git a/release/scripts/startup/bl_ui/space_graph.py b/release/scripts/startup/bl_ui/space_graph.py
index 5861bc0..6b78403 100644
--- a/release/scripts/startup/bl_ui/space_graph.py
+++ b/release/scripts/startup/bl_ui/space_graph.py
@@ -52,6 +52,7 @@ class GRAPH_HT_header(Header):
         row = layout.row(align=True)
         row.operator("graph.copy", text="", icon='COPYDOWN')
         row.operator("graph.paste", text="", icon='PASTEDOWN')
+        row.operator("graph.paste", text="", icon='PASTEFLIPDOWN').flipped = True
 
         row = layout.row(align=True)
         if st.has_ghost_curves:
diff --git a/source/blender/blenlib/BLI_string.h b/source/blender/blenlib/BLI_string.h
index d6e7f3d..4d2007f 100644
--- a/source/blender/blenlib/BLI_string.h
+++ b/source/blender/blenlib/BLI_string.h
@@ -85,6 +85,9 @@ int BLI_str_rstrip_float_zero(char *str, const char pad) ATTR_NONNULL();
 int BLI_str_index_in_array_n(const char *__restrict str, const char **__restrict str_array, const int str_array_len) ATTR_NONNULL();
 int BLI_str_index_in_array(const char *__restrict str, const char **__restrict str_array) ATTR_NONNULL();
 
+bool BLI_str_ends_with(const char *str,const char *end) ATTR_NONNULL();
+bool BLI_strn_ends_with(const char *str,const char *end, int length) ATTR_NONNULL();
+
 size_t BLI_str_partition(const char *str, const char delim[], char **sep, char **suf) ATTR_NONNULL();
 size_t BLI_str_rpartition(const char *str, const char delim[], char **sep, char **suf) ATTR_NONNULL();
 size_t BLI_str_partition_ex(const char *str, const char delim[], char **sep, char **suf, const bool from_right) ATTR_NONNULL();
diff --git a/source/blender/blenlib/intern/string.c b/source/blender/blenlib/intern/string.c
index 765e2ea..365e555 100644
--- a/source/blender/blenlib/intern/string.c
+++ b/source/blender/blenlib/intern/string.c
@@ -773,6 +773,35 @@ int BLI_str_index_in_array(const char *__restrict str, const char **__restrict s
 	return -1;
 }
 
+bool BLI_strn_ends_with(const char *str,const char *end, int slength)
+{
+	size_t elength = strlen(end);
+	
+	if (elength < slength) {
+		const char *iter = str + slength - elength;
+		while (*iter) {
+			if (*iter++ != *end++) {
+				return false;
+			}
+		}
+		return true;
+	}
+	return false;
+}
+
+/**
+ * Find if a string ends with another string.
+ *
+ * \param str The string to search within.
+ * \param end The string we look for at the end.
+ * \return If str ends with end.
+ */
+bool BLI_str_ends_with(const char *str,const char *end)
+{
+	size_t slength =  strlen(str);
+	return BLI_strn_ends_with(str, end, slength);
+}
+
 /**
  * Find the first char matching one of the chars in \a delim, from left.
  *
diff --git a/source/blender/editors/animation/keyframes_general.c b/source/blender/editors/animation/keyframes_general.c
index d00a64a..adfaae0 100644
--- a/source/blender/editors/animation/keyframes_general.c
+++ b/source/blender/editors/animation/keyframes_general.c
@@ -43,10 +43,12 @@
 #include "DNA_scene_types.h"
 
 
+#include "BKE_action.h"
 #include "BKE_fcurve.h"
 #include "BKE_report.h"
 #include "BKE_library.h"
 #include "BKE_global.h"
+#include "BKE_deform.h"
 
 #include "RNA_access.h"
 
@@ -477,6 +479,7 @@ typedef struct tAnimCopybufItem {
 	BezTriple *bezt;    /* keyframes in buffer */
 
 	short id_type;      /* Result of GS(id->name)*/
+	bool  is_bone;      /* special flag for armature bones */
 } tAnimCopybufItem;
 
 
@@ -540,6 +543,34 @@ short copy_animedit_keys(bAnimContext *ac, ListBase *anim_data)
 		aci->grp = fcu->grp;
 		aci->rna_path = MEM_dupallocN(fcu->rna_path);
 		aci->array_index = fcu->array_index;
+		
+		/* detect if this is a bone. We do that here rather than during pasting because ID pointers will get invalidated if we undo.
+		 * storing the relavant information here helps avoiding crashes if we undo-repaste */
+		if ((aci->id_type == ID_OB) && (((Object *)aci->id)->type == OB_ARMATURE) && aci->rna_path) {
+			Object *ob = (Object *)aci->id;
+			char *str_start;
+			
+			if ((str_start = strstr(aci->rna_path, "pose.bones["))) {
+				/* ninja coding, try to change the name */
+				bPoseChannel *pchan;
+				char bname[MAX_VGROUP_NAME];
+				int length = 0;
+				char *str_end;
+				
+				str_start += 12;
+				str_end = strchr(str_start, '\"');
+				length = str_end - str_start;
+				str_start[length] = 0;
+				BKE_deform_flip_side_name(bname, str_start, false);
+				str_start[length] = '\"';
+				pchan = BKE_pose_channel_find_name(ob->pose, bname);
+		
+				if (pchan) {
+					aci->is_bone = true;
+				}
+			}
+		}
+		
 		BLI_addtail(&animcopybuf, aci);
 		
 		/* add selected keyframes to buffer */
@@ -587,19 +618,64 @@ short copy_animedit_keys(bAnimContext *ac, ListBase *anim_data)
 	return 0;
 }
 
+static void flip_names(tAnimCopybufItem *aci, char **name) {
+	if (aci->is_bone) {
+		char *str_start;
+		if ((str_start = strstr(aci->rna_path, "pose.bones["))) {
+			/* ninja coding, try to change the name */
+			char bname_new[MAX_VGROUP_NAME];
+			char *str_iter, *str_end;
+			int length, prefix_l, postfix_l;
+			
+			str_start += 12;
+			prefix_l = str_start - aci->rna_path;
+			
+			str_end = strchr(str_start, '\"');
+			
+			length = str_end - str_start;
+			postfix_l = strlen(str_end);
+			
+			/* more ninja stuff, temporary substitute with NULL terminator */
+			str_start[length] = 0;
+			BKE_deform_flip_side_name(bname_new, str_start, false);
+			str_start[length] = '\"';
+			
+			str_iter = *name = MEM_mallocN(sizeof(char) * (prefix_l + postfix_l + length + 1), "flipped_path");
+			
+			BLI_strncpy(str_iter, aci->rna_path, prefix_l + 1);
+			str_iter += prefix_l ;
+			BLI_strncpy(str_iter, bname_new, length + 1);
+			str_iter += length;
+			BLI_strncpy(str_iter, str_end, postfix_l + 1);
+			str_iter[postfix_l] = 0;
+		}
+	}
+}
+
 /* ------------------- */
 
 /* most strict method: exact matches only */
-static tAnimCopybufItem *pastebuf_match_path_full(FCurve *fcu, const short from_single, const short to_simple)
+static tAnimCopybufItem *pastebuf_match_path_full(FCurve *fcu, const short from_single, const short to_simple, bool flip)
 {
 	tAnimCopybufItem *aci;
 
 	for (aci = animcopybuf.first; aci; aci = aci->next) {
-		/* check that paths exist */
 		if (to_simple || (aci->rna_path && fcu->rna_path)) {
-			if (to_simple || (strcmp(aci->rna_path, fcu->rna_path) == 0)) {
-				if ((from_single) || (aci->array_index == fcu->array_index))
+			if (!to_simple && flip && aci->is_bone && fcu->rna_path) {
+				if ((from_single) || (aci->array_index == fcu->array_index)) {
+					char *name = NULL;
+					flip_names(aci, &name);
+					if (strcmp(name, fcu->rna_path) == 0) {
+						MEM_freeN(name);
+						break;
+					}
+					MEM_freeN(name);
+				}
+			}
+			else if (to_simple || (strcmp(aci->rna_path, fcu->rna_path) == 0)) {
+				if ((from_single) || (aci->array_index == fcu->array_index)) {
 					break;
+				}
 			}
 		}
 	}
@@ -670,8 +746,29 @@ static tAnimCopybufItem *pastebuf_match_index_only(FCurve *fcu, const short from
 
 /* ................ */
 
+static void do_curve_mirror_flippping(tAnimCopybufItem *aci, BezTriple *bezt) {
+	if (aci->is_bone) {
+		int slength = strlen(aci->rna_path);
+		bool flip = false;
+		if (BLI_strn_ends_with(aci->rna_path, "location", slength) && aci->array_index == 0) 
+			flip = true;
+		else if (BLI_strn_ends_with(aci->rna_path, "rotation_quaternion", slength) && ELEM(aci->array_index, 2, 3))
+			flip = true;
+		else if (BLI_strn_ends_with(aci->rna_path, "rotation_euler", slength) && ELEM(aci->array_index, 1, 2))
+			flip = true;
+		else if (BLI_strn_ends_with(aci->rna_path, "rotation_axis_angle", slength) && ELEM(aci->array_index, 2, 3))
+			flip = true;
+		
+		if (flip) {
+			bezt->vec[0][1] = -bezt->vec[0][1];
+			bezt->vec[1][1] = -bezt->vec[1][1];
+			bezt->vec[2][1] = -bezt->vec[2][1];
+		}
+	}
+}
+
 /* helper for paste_animedit_keys() - performs the actual pasting */
-static void paste_animedit_keys_fcurve(FCurve *fcu, tAnimCopybufItem *aci, float offset, const eKeyMergeMode merge_mode)
+static void paste_animedit_keys_fcurve(FCurve *fcu, tAnimCopybufItem *aci, float offset, const eKeyMergeMode merge_mode, bool flip)
 {
 	BezTriple *bezt;
 	int i;
@@ -726,6 +823,9 @@ static void paste_animedit_keys_fcurve(FCurve *fcu, tAnimCopybufItem *aci, float
 	/* just start pasting, with the first keyframe on the current frame, and so on */
 	for (i = 0, bezt = aci->bezt; i < aci->totvert; i++, bezt++) {
 		/* temporarily apply offset to src beztriple while copying */
+		if (flip)
+			do_curve_mirror_flippping(aci, bezt);
+		
 		bezt->vec[0][0] += offset;
 		bezt->vec[1][0] += offset;
 		bezt->vec[2][0] += offset;
@@ -733,12 +833,16 @@ static void paste_animedit_keys_fcurve(FCurve *fcu, tAnimCopybufItem *aci, float
 		/* insert the keyframe
 		 * NOTE: we do not want to inherit handles from existing keyframes in this case!
 		 */
-		insert_bezt_fcurve(fcu, bezt, INSERTKEY_OVERWRITE_FULL);
 		
+		insert_bezt_fcurve(fcu, bezt, INSERTKEY_OVERWRITE_FULL);
+
 		/* un-apply offset from src beztriple after copying */
 		bezt->vec[0][0] -= offset;
 		bezt->vec[1][0] -= offset;
 		bezt->vec[2][0] -= offset;
+		
+		if (flip)
+			do_curve_mirror_flippping(aci, bezt);
 	}
 	
 	/* recalculate F-Curve's handles? */
@@ -768,7 +872,7 @@ EnumPropertyItem keyframe_paste_merge_items[] = {
  * \return Status code is whether the method FAILED to do anything
  */
 short paste_animedit_keys(bAnimContext *ac, ListBase *anim_data,
-                          const eKeyPasteOffset offset_mode, const eKeyMergeMode merge_mode)
+                          const eKeyPasteOffset offset_mode, 

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list