[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [34655] trunk/blender/source/blender: Fix bones moving when changing between editmode and posemode.

Brecht Van Lommel brechtvanlommel at pandora.be
Sat Feb 5 14:19:15 CET 2011


Revision: 34655
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=34655
Author:   blendix
Date:     2011-02-05 13:19:14 +0000 (Sat, 05 Feb 2011)
Log Message:
-----------
Fix bones moving when changing between editmode and posemode.
Patch #25901 by Tobias Oelgarte.

Bone transformations would be converted back and forth between different
representations when changing modes, which due to numerical errors could
lead to bone transformations slowly changing as you edit the armature.

Now the editmode head, tail and roll values are stored in bones and used
directly when entering edit mode. Head and tail were already there but
now we ensure they are the exact same value, roll was not yet there, so
we have a version patch for it.

The sub version was incremented to 1 for the version patch.

Modified Paths:
--------------
    trunk/blender/source/blender/blenkernel/BKE_blender.h
    trunk/blender/source/blender/blenkernel/intern/armature.c
    trunk/blender/source/blender/blenloader/intern/readfile.c
    trunk/blender/source/blender/editors/armature/editarmature.c
    trunk/blender/source/blender/makesdna/DNA_armature_types.h

Modified: trunk/blender/source/blender/blenkernel/BKE_blender.h
===================================================================
--- trunk/blender/source/blender/blenkernel/BKE_blender.h	2011-02-05 13:17:34 UTC (rev 34654)
+++ trunk/blender/source/blender/blenkernel/BKE_blender.h	2011-02-05 13:19:14 UTC (rev 34655)
@@ -45,7 +45,7 @@
 struct Main;
 
 #define BLENDER_VERSION			256
-#define BLENDER_SUBVERSION		0
+#define BLENDER_SUBVERSION		1
 
 #define BLENDER_MINVERSION		250
 #define BLENDER_MINSUBVERSION	0

Modified: trunk/blender/source/blender/blenkernel/intern/armature.c
===================================================================
--- trunk/blender/source/blender/blenkernel/intern/armature.c	2011-02-05 13:17:34 UTC (rev 34654)
+++ trunk/blender/source/blender/blenkernel/intern/armature.c	2011-02-05 13:19:14 UTC (rev 34655)
@@ -1401,13 +1401,6 @@
 		VECCOPY(bone->arm_mat[3], bone->head);
 	}
 	
-	/* head */
-	VECCOPY(bone->arm_head, bone->arm_mat[3]);
-	/* tail is in current local coord system */
-	VECCOPY(vec, bone->arm_mat[1]);
-	mul_v3_fl(vec, bone->length);
-	add_v3_v3v3(bone->arm_tail, bone->arm_head, vec);
-	
 	/* and the kiddies */
 	prevbone= bone;
 	for(bone= bone->childbase.first; bone; bone= bone->next) {

Modified: trunk/blender/source/blender/blenloader/intern/readfile.c
===================================================================
--- trunk/blender/source/blender/blenloader/intern/readfile.c	2011-02-05 13:17:34 UTC (rev 34654)
+++ trunk/blender/source/blender/blenloader/intern/readfile.c	2011-02-05 13:19:14 UTC (rev 34655)
@@ -5746,6 +5746,23 @@
 	}
 }
 
+static void do_version_bone_head_tail_237(Bone *bone)
+{
+	Bone *child;
+	float vec[3];
+
+	/* head */
+	copy_v3_v3(bone->arm_head, bone->arm_mat[3]);
+
+	/* tail is in current local coord system */
+	copy_v3_v3(vec, bone->arm_mat[1]);
+	mul_v3_fl(vec, bone->length);
+	add_v3_v3v3(bone->arm_tail, bone->arm_head, vec);
+
+	for(child= bone->childbase.first; child; child= child->next)
+		do_version_bone_head_tail_237(child);
+}
+
 static void bone_version_238(ListBase *lb)
 {
 	Bone *bone;
@@ -6655,6 +6672,19 @@
 	}
 }
 
+
+static void do_version_bone_roll_256(Bone *bone)
+{
+	Bone *child;
+	float submat[3][3];
+	
+	copy_m3_m4(submat, bone->arm_mat);
+	mat3_to_vec_roll(submat, 0, &bone->arm_roll);
+	
+	for(child = bone->childbase.first; child; child = child->next)
+		do_version_bone_roll_256(child);
+}
+
 static void do_versions(FileData *fd, Library *lib, Main *main)
 {
 	/* WATCH IT!!!: pointers from libdata have not been converted */
@@ -7964,10 +7994,14 @@
 		bArmature *arm;
 		bConstraint *con;
 		Object *ob;
+		Bone *bone;
 		
 		// armature recode checks 
 		for(arm= main->armature.first; arm; arm= arm->id.next) {
 			where_is_armature(arm);
+
+			for(bone= arm->bonebase.first; bone; bone= bone->next)
+				do_version_bone_head_tail_237(bone);
 		}
 		for(ob= main->object.first; ob; ob= ob->id.next) {
 			if(ob->parent) {
@@ -11285,6 +11319,16 @@
 		}
 	}
 	
+	if (main->versionfile < 256 || (main->versionfile == 256 && main->subversionfile < 1)) {
+		/* fix for bones that didn't have arm_roll before */
+		bArmature* arm;
+		Bone* bone;
+
+		for (arm = main->armature.first; arm; arm = arm->id.next)
+			for (bone = arm->bonebase.first; bone; bone = bone->next)
+				do_version_bone_roll_256(bone);
+	}
+				
 	/* put compatibility code here until next subversion bump */
 	
 	{

Modified: trunk/blender/source/blender/editors/armature/editarmature.c
===================================================================
--- trunk/blender/source/blender/editors/armature/editarmature.c	2011-02-05 13:17:34 UTC (rev 34654)
+++ trunk/blender/source/blender/editors/armature/editarmature.c	2011-02-05 13:19:14 UTC (rev 34655)
@@ -246,11 +246,6 @@
 	EditBone	*eBoneAct= NULL;
 	EditBone	*eBoneTest= NULL;
 	Bone		*curBone;
-	float delta[3];
-	float premat[3][3];
-	float postmat[3][3];
-	float imat[3][3];
-	float difmat[3][3];
 		
 	for (curBone=bones->first; curBone; curBone=curBone->next) {
 		eBone= MEM_callocN(sizeof(EditBone), "make_editbone");
@@ -291,21 +286,9 @@
 		}
 
 		copy_v3_v3(eBone->head, curBone->arm_head);
-		copy_v3_v3(eBone->tail, curBone->arm_tail);		
+		copy_v3_v3(eBone->tail, curBone->arm_tail);
+		eBone->roll = curBone->arm_roll;
 		
-		eBone->roll= 0.0f;
-		
-		/* roll fixing */
-		sub_v3_v3v3(delta, eBone->tail, eBone->head);
-		vec_roll_to_mat3(delta, 0.0f, postmat);
-		
-		copy_m3_m4(premat, curBone->arm_mat);
-		
-		invert_m3_m3(imat, postmat);
-		mul_m3_m3m3(difmat, imat, premat);
-		
-		eBone->roll = (float)atan2(difmat[2][0], difmat[2][2]);
-		
 		/* rest of stuff copy */
 		eBone->length= curBone->length;
 		eBone->dist= curBone->dist;
@@ -420,8 +403,10 @@
 		eBone->temp= newBone;	/* Associate the real Bones with the EditBones */
 		
 		BLI_strncpy(newBone->name, eBone->name, sizeof(newBone->name));
-		memcpy(newBone->head, eBone->head, sizeof(newBone->head));
-		memcpy(newBone->tail, eBone->tail, sizeof(newBone->tail));
+		copy_v3_v3(newBone->arm_head, eBone->head);
+		copy_v3_v3(newBone->arm_tail, eBone->tail);
+		newBone->arm_roll = eBone->roll;
+		
 		newBone->flag= eBone->flag;
 		
 		if (eBone == arm->act_edbone) {
@@ -456,7 +441,6 @@
 			BLI_addtail(&newBone->parent->childbase, newBone);
 			
 			{
-				float M_boneRest[3][3];
 				float M_parentRest[3][3];
 				float iM_parentRest[3][3];
 				float	delta[3];
@@ -465,10 +449,6 @@
 				sub_v3_v3v3(delta, eBone->parent->tail, eBone->parent->head);
 				vec_roll_to_mat3(delta, eBone->parent->roll, M_parentRest);
 				
-				/* Get this bone's  matrix (rotation only) */
-				sub_v3_v3v3(delta, eBone->tail, eBone->head);
-				vec_roll_to_mat3(delta, eBone->roll, M_boneRest);
-				
 				/* Invert the parent matrix */
 				invert_m3_m3(iM_parentRest, M_parentRest);
 				
@@ -481,8 +461,11 @@
 			}
 		}
 		/*	...otherwise add this bone to the armature's bonebase */
-		else
+		else {
+			copy_v3_v3(newBone->head, eBone->head);
+			copy_v3_v3(newBone->tail, eBone->tail);
 			BLI_addtail(&arm->bonebase, newBone);
+		}
 	}
 	
 	/* Make a pass through the new armature to fix rolling */

Modified: trunk/blender/source/blender/makesdna/DNA_armature_types.h
===================================================================
--- trunk/blender/source/blender/makesdna/DNA_armature_types.h	2011-02-05 13:17:34 UTC (rev 34654)
+++ trunk/blender/source/blender/makesdna/DNA_armature_types.h	2011-02-05 13:19:14 UTC (rev 34655)
@@ -57,8 +57,9 @@
 	int				flag;
 	
 	float			arm_head[3];		
-	float			arm_tail[3];	/*	head/tail and roll in Armature Space (rest pos) */
+	float			arm_tail[3];	/*	head/tail in Armature Space (rest pos) */
 	float			arm_mat[4][4];  /*  matrix: (bonemat(b)+head(b))*arm_mat(b-1), rest pos*/
+	float           arm_roll;        /* roll in Armature Space (rest pos) */
 	
 	float			dist, weight;			/*  dist, weight: for non-deformgroup deforms */
 	float			xwidth, length, zwidth;	/*  width: for block bones. keep in this order, transform! */
@@ -68,7 +69,7 @@
 	float			size[3];		/*  patch for upward compat, UNUSED! */
 	int				layer;			/* layers that bone appears on */
 	short			segments;		/*  for B-bones */
-	short 			pad[3];
+	short 			pad[1];
 } Bone;
 
 typedef struct bArmature {




More information about the Bf-blender-cvs mailing list