bugfix [#24907] bone roll z up broken and python script showing correct method to...
[blender.git] / source / blender / editors / armature / editarmature.c
index 525ca9aae1cc5cb64bd143d4e743ba738d93a1ad..61d16f99f2fc0e8bd5116b699bf7e3524a9ef54e 100644 (file)
 
 #include <ctype.h>
 #include <stdlib.h>
+#include <stddef.h>
 #include <string.h>
 #include <math.h> 
 #include <float.h> 
+#include <assert.h> 
 
-#include "MEM_guardedalloc.h"
 
 #include "DNA_anim_types.h"
 #include "DNA_armature_types.h"
@@ -39,6 +40,8 @@
 #include "DNA_meshdata_types.h"
 #include "DNA_scene_types.h"
 
+#include "MEM_guardedalloc.h"
+
 #include "BLI_blenlib.h"
 #include "BLI_math.h"
 #include "BLI_editVert.h"
@@ -62,7 +65,6 @@
 #include "DNA_object_types.h"
 
 #include "BIF_gl.h"
-#include "BIF_generate.h"
 
 #include "RNA_access.h"
 #include "RNA_define.h"
 #include "reeb.h"
 #endif
 
-/* ************* XXX *************** */
-static void BIF_undo_push(const char *msg) {}
-/* ************* XXX *************** */
-
 /* **************** tools on Editmode Armature **************** */
 
 /* Sync selection to parent for connected children */
@@ -173,22 +171,41 @@ EditBone *make_boneList(ListBase *edbo, ListBase *bones, EditBone *parent, Bone
                
                /*      Copy relevant data from bone to eBone */
                eBone->parent= parent;
-               BLI_strncpy(eBone->name, curBone->name, 32);
+               BLI_strncpy(eBone->name, curBone->name, sizeof(eBone->name));
                eBone->flag = curBone->flag;
                
                /* fix selection flags */
+
                if (eBone->flag & BONE_SELECTED) {
+                       /* if the bone is selected the copy its root selection to the parents tip */
                        eBone->flag |= BONE_TIPSEL;
-                       if (eBone->parent && (eBone->flag & BONE_CONNECTED))
+                       if (eBone->parent && (eBone->flag & BONE_CONNECTED)) {
                                eBone->parent->flag |= BONE_TIPSEL;
-                       else 
+                               eBone->flag &= ~BONE_ROOTSEL; /* this is ignored when there is a connected parent, so unset it */
+                       }
+                       else {
                                eBone->flag |= BONE_ROOTSEL;
+                       }
                }
-               else 
-                       eBone->flag &= ~BONE_ROOTSEL;
-               
-               VECCOPY(eBone->head, curBone->arm_head);
-               VECCOPY(eBone->tail, curBone->arm_tail);                
+               else {
+                       /* if the bone is not selected, but connected to its parent
+                        *  copy the parents tip selection state */
+                       if(eBone->parent &&  (eBone->flag & BONE_CONNECTED)) {
+                               /* selecting with the mouse gives this behavior */
+                               if(eBone->parent->flag & BONE_TIPSEL) {
+                                       eBone->flag |= BONE_ROOTSEL;
+                               }
+                               else {
+                                       eBone->flag &= ~BONE_ROOTSEL;
+                               }
+
+                               /* probably not selected but just incase */
+                               eBone->flag &= ~BONE_TIPSEL;
+                       }
+               }
+
+               copy_v3_v3(eBone->head, curBone->arm_head);
+               copy_v3_v3(eBone->tail, curBone->arm_tail);             
                
                eBone->roll= 0.0f;
                
@@ -316,9 +333,9 @@ void ED_armature_from_edit(Object *obedit)
                newBone= MEM_callocN(sizeof(Bone), "bone");
                eBone->temp= newBone;   /* Associate the real Bones with the EditBones */
                
-               BLI_strncpy(newBone->name, eBone->name, 32);
-               memcpy(newBone->head, eBone->head, sizeof(float)*3);
-               memcpy(newBone->tail, eBone->tail, sizeof(float)*3);
+               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));
                newBone->flag= eBone->flag;
                
                if (eBone == arm->act_edbone) {
@@ -486,42 +503,24 @@ void docenter_armature (Scene *scene, Object *ob, float cursor[3], int centermod
 /* checks if an EditBone with a matching name already, returning the matching bone if it exists */
 static EditBone *editbone_name_exists (ListBase *edbo, const char *name)
 {
-       EditBone *eBone;
-       
-       for (eBone=edbo->first; eBone; eBone=eBone->next) {
-               if (!strcmp(name, eBone->name))
-                       return eBone;
-       }
-       return NULL;
+       return BLI_findstring(edbo, name, offsetof(EditBone, name));
 }
 
 /* note: there's a unique_bone_name() too! */
+static int editbone_unique_check(void *arg, const char *name)
+{
+       struct {ListBase *lb;void *bone;} *data= arg;
+       EditBone *dupli= editbone_name_exists(data->lb, name);
+       return dupli && dupli != data->bone;
+}
+
 void unique_editbone_name (ListBase *edbo, char *name, EditBone *bone)
 {
-       EditBone *dupli;
-       char    tempname[64];
-       int             number;
-       char    *dot;
+       struct {ListBase *lb; void *bone;} data;
+       data.lb= edbo;
+       data.bone= bone;
 
-       dupli = editbone_name_exists(edbo, name);
-       
-       if (dupli && bone != dupli) {
-               /*      Strip off the suffix, if it's a number */
-               number= strlen(name);
-               if (number && isdigit(name[number-1])) {
-                       dot= strrchr(name, '.');        // last occurrence
-                       if (dot)
-                               *dot=0;
-               }
-               
-               for (number = 1; number <= 999; number++) {
-                       sprintf(tempname, "%s.%03d", name, number);
-                       if (!editbone_name_exists(edbo, tempname)) {
-                               BLI_strncpy(name, tempname, 32);
-                               return;
-                       }
-               }
-       }
+       BLI_uniquename_cb(editbone_unique_check, &data, "Bone", '.', name, sizeof(bone->name));
 }
 
 /* helper for apply_armature_pose2bones - fixes parenting of objects that are bone-parented to armature */
@@ -536,7 +535,7 @@ static void applyarmature_fix_boneparents (Scene *scene, Object *armob)
                        /* apply current transform from parent (not yet destroyed), 
                         * then calculate new parent inverse matrix
                         */
-                       object_apply_mat4(ob, ob->obmat);
+                       object_apply_mat4(ob, ob->obmat, FALSE, FALSE);
                        
                        what_does_parent(scene, ob, &workob);
                        invert_m4_m4(ob->parentinv, workob.obmat);
@@ -548,7 +547,7 @@ static void applyarmature_fix_boneparents (Scene *scene, Object *armob)
 static int apply_armature_pose2bones_exec (bContext *C, wmOperator *op)
 {
        Scene *scene= CTX_data_scene(C);
-       Object *ob= CTX_data_active_object(C); // must be active object, not edit-object
+       Object *ob= ED_object_pose_armature(CTX_data_active_object(C)); // must be active object, not edit-object
        bArmature *arm= get_armature(ob);
        bPose *pose;
        bPoseChannel *pchan;
@@ -575,8 +574,8 @@ static int apply_armature_pose2bones_exec (bContext *C, wmOperator *op)
                curbone= editbone_name_exists(arm->edbo, pchan->name);
                
                /* simply copy the head/tail values from pchan over to curbone */
-               VECCOPY(curbone->head, pchan->pose_head);
-               VECCOPY(curbone->tail, pchan->pose_tail);
+               copy_v3_v3(curbone->head, pchan->pose_head);
+               copy_v3_v3(curbone->tail, pchan->pose_tail);
                
                /* fix roll:
                 *      1. find auto-calculated roll value for this bone now
@@ -645,10 +644,10 @@ void POSE_OT_armature_apply (wmOperatorType *ot)
 
 
 /* set the current pose as the restpose */
-static int pose_visual_transform_apply_exec (bContext *C, wmOperator *op)
+static int pose_visual_transform_apply_exec (bContext *C, wmOperator *UNUSED(op))
 {
        Scene *scene= CTX_data_scene(C);
-       Object *ob= CTX_data_active_object(C); // must be active object, not edit-object
+       Object *ob= ED_object_pose_armature(CTX_data_active_object(C)); // must be active object, not edit-object
 
        /* don't check if editmode (should be done by caller) */
        if (ob->type!=OB_ARMATURE)
@@ -671,7 +670,7 @@ static int pose_visual_transform_apply_exec (bContext *C, wmOperator *op)
                invert_m4_m4(imat, pchan->pose_mat);
                mul_m4_m4m4(delta_mat, mat, imat);
 
-               pchan_apply_mat4(pchan, delta_mat);
+               pchan_apply_mat4(pchan, delta_mat, TRUE);
 
                where_is_pose_bone(scene, ob, pchan, CFRA, 1);
        }
@@ -753,7 +752,7 @@ static void joined_armature_fix_links(Object *tarArm, Object *srcArm, bPoseChann
                                                        
                                                        for (achan= act->chanbase.first; achan; achan= achan->next) {
                                                                if (strcmp(achan->name, pchan->name)==0)
-                                                                       BLI_strncpy(achan->name, curbone->name, 32);
+                                                                       BLI_strncpy(achan->name, curbone->name, sizeof(achan->name));
                                                        }
                                                }
                                        }
@@ -797,7 +796,7 @@ static void joined_armature_fix_links(Object *tarArm, Object *srcArm, bPoseChann
                        if (ob->partype==PARBONE) {
                                /* bone name in object */
                                if (!strcmp(ob->parsubstr, pchan->name))
-                                       BLI_strncpy(ob->parsubstr, curbone->name, 32);
+                                       BLI_strncpy(ob->parsubstr, curbone->name, sizeof(ob->parsubstr));
                        }
                        
                        /* make tar armature be new parent */
@@ -807,7 +806,7 @@ static void joined_armature_fix_links(Object *tarArm, Object *srcArm, bPoseChann
 }
 
 /* join armature exec is exported for use in object->join objects operator... */
-int join_armature_exec(bContext *C, wmOperator *op)
+int join_armature_exec(bContext *C, wmOperator *UNUSED(op))
 {
        Main *bmain= CTX_data_main(C);
        Scene *scene= CTX_data_scene(C);
@@ -1065,7 +1064,7 @@ static void separated_armature_fix_links(Object *origArm, Object *newArm)
  *     sel: remove selected bones from the armature, otherwise the unselected bones are removed
  *  (ob is not in editmode)
  */
-static void separate_armature_bones (Scene *scene, Object *ob, short sel) 
+static void separate_armature_bones(Object *ob, short sel) 
 {
        bArmature *arm= (bArmature *)ob->data;
        bPoseChannel *pchan, *pchann;
@@ -1117,7 +1116,7 @@ static void separate_armature_bones (Scene *scene, Object *ob, short sel)
 }
 
 /* separate selected bones into their armature */
-static int separate_armature_exec (bContext *C, wmOperator *op)
+static int separate_armature_exec (bContext *C, wmOperator *UNUSED(op))
 {
        Main *bmain= CTX_data_main(C);
        Scene *scene= CTX_data_scene(C);
@@ -1166,8 +1165,8 @@ static int separate_armature_exec (bContext *C, wmOperator *op)
        
        
        /* 3) remove bones that shouldn't still be around on both armatures */
-       separate_armature_bones(scene, oldob, 1);
-       separate_armature_bones(scene, newob, 0);
+       separate_armature_bones(oldob, 1);
+       separate_armature_bones(newob, 0);
        
        
        /* 4) fix links before depsgraph flushes */ // err... or after?
@@ -1354,8 +1353,9 @@ static EditBone *editbone_get_child(bArmature *arm, EditBone *pabone, short use_
        for (curbone= arm->edbo->first; curbone; curbone= curbone->next) {
                if (curbone->parent == pabone) {
                        if (use_visibility) {
-                               if ((arm->layer & curbone->layer) && !(pabone->flag & BONE_HIDDEN_A))
+                               if ((arm->layer & curbone->layer) && !(pabone->flag & BONE_HIDDEN_A)) {
                                        chbone = curbone;
+                               }
                        }
                        else
                                chbone = curbone;
@@ -1379,7 +1379,7 @@ static int pose_setflag_exec (bContext *C, wmOperator *op)
        CTX_DATA_END;
        
        /* note, notifier might evolve */
-       WM_event_add_notifier(C, NC_OBJECT|ND_POSE, CTX_data_active_object(C));
+       WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ED_object_pose_armature(CTX_data_active_object(C)));
        
        return OPERATOR_FINISHED;
 }
@@ -1777,7 +1777,7 @@ EditBone *ED_armature_bone_get_mirrored(ListBase *edbo, EditBone *ebo)
 
 /* previously delete_armature */
 /* only editmode! */
-static int armature_delete_selected_exec(bContext *C, wmOperator *op)
+static int armature_delete_selected_exec(bContext *C, wmOperator *UNUSED(op))
 {
        bArmature *arm;
        EditBone        *curBone, *next;
@@ -1882,7 +1882,7 @@ void ARMATURE_OT_delete(wmOperatorType *ot)
  * toggle==2: only active tag
  * toggle==3: swap (no test)
  */
-void ED_armature_deselectall(Object *obedit, int toggle, int doundo)
+void ED_armature_deselect_all(Object *obedit, int toggle)
 {
        bArmature *arm= obedit->data;
        EditBone        *eBone;
@@ -1909,7 +1909,7 @@ void ED_armature_deselectall(Object *obedit, int toggle, int doundo)
                for (eBone=arm->edbo->first;eBone;eBone=eBone->next) {
                        if (sel==3) {
                                /* invert selection of bone */
-                               if ((arm->layer & eBone->layer) && (eBone->flag & BONE_HIDDEN_A)==0) {
+                               if(EBONE_VISIBLE(arm, eBone)) {
                                        eBone->flag ^= (BONE_SELECTED | BONE_TIPSEL | BONE_ROOTSEL);
                                        if(arm->act_edbone==eBone)
                                                arm->act_edbone= NULL;
@@ -1917,7 +1917,7 @@ void ED_armature_deselectall(Object *obedit, int toggle, int doundo)
                        }
                        else if (sel==1) {
                                /* select bone */
-                               if(arm->layer & eBone->layer && (eBone->flag & BONE_HIDDEN_A)==0) {
+                               if(EBONE_VISIBLE(arm, eBone)) {
                                        eBone->flag |= (BONE_SELECTED | BONE_TIPSEL | BONE_ROOTSEL);
                                        if(eBone->parent)
                                                eBone->parent->flag |= (BONE_TIPSEL);
@@ -1935,6 +1935,20 @@ void ED_armature_deselectall(Object *obedit, int toggle, int doundo)
        ED_armature_sync_selection(arm->edbo);
 }
 
+void ED_armature_deselect_all_visible(Object *obedit)
+{
+       bArmature *arm= obedit->data;
+       EditBone        *ebone;
+
+       for (ebone= arm->edbo->first; ebone; ebone= ebone->next) {
+               /* first and foremost, bone must be visible and selected */
+               if (EBONE_VISIBLE(arm, ebone) && (ebone->flag & BONE_UNSELECTABLE)==0) {
+                       ebone->flag &= ~(BONE_SELECTED | BONE_TIPSEL | BONE_ROOTSEL);
+               }
+       }
+
+       ED_armature_sync_selection(arm->edbo);
+}
 
 /* context: editmode armature in view3d */
 int mouse_armature(bContext *C, short mval[2], int extend)
@@ -1953,7 +1967,7 @@ int mouse_armature(bContext *C, short mval[2], int extend)
        if (nearBone) {
 
                if (!extend)
-                       ED_armature_deselectall(obedit, 0, 0);
+                       ED_armature_deselect_all(obedit, 0);
                
                /* by definition the non-root connected bones have no root point drawn,
                   so a root selection needs to be delivered to the parent tip */
@@ -2053,136 +2067,110 @@ void ED_armature_to_edit(Object *ob)
 /* adjust bone roll to align Z axis with vector
  * vec is in local space and is normalized
  */
-float ED_rollBoneToVector(EditBone *bone, float new_up_axis[3])
+
+float ED_rollBoneToVector(EditBone *bone, const float align_axis[3], const short axis_only)
 {
-       float mat[3][3], nor[3], up_axis[3], vec[3];
-       float roll;
+       float mat[3][3], nor[3];
 
        sub_v3_v3v3(nor, bone->tail, bone->head);
-       
-       vec_roll_to_mat3(nor, 0, mat);
-       VECCOPY(up_axis, mat[2]);
-       
-       roll = angle_normalized_v3v3(new_up_axis, up_axis);
-       
-       cross_v3_v3v3(vec, up_axis, new_up_axis);
-       
-       if (dot_v3v3(vec, nor) < 0)
-       {
-               roll = -roll;
-       }
-       
-       return roll;
-}
+       vec_roll_to_mat3(nor, 0.0f, mat);
 
+       /* check the bone isnt aligned with the axis */
+       if(!is_zero_v3(align_axis) && angle_v3v3(align_axis, mat[2]) > FLT_EPSILON) {
+               float vec[3], align_axis_proj[3], roll;
 
-/* Set roll value for given bone -> Z-Axis Point up (original method) */
-static void auto_align_ebone_zaxisup(Scene *scene, View3D *v3d, EditBone *ebone)
-{
-       float   delta[3], curmat[3][3];
-       float   xaxis[3]={1.0f, 0.0f, 0.0f}, yaxis[3], zaxis[3]={0.0f, 0.0f, 1.0f};
-       float   targetmat[3][3], imat[3][3], diffmat[3][3];
-       
-       /* Find the current bone matrix */
-       sub_v3_v3v3(delta, ebone->tail, ebone->head);
-       vec_roll_to_mat3(delta, 0.0f, curmat);
-       
-       /* Make new matrix based on y axis & z-up */
-       VECCOPY(yaxis, curmat[1]);
-       
-       unit_m3(targetmat);
-       VECCOPY(targetmat[0], xaxis);
-       VECCOPY(targetmat[1], yaxis);
-       VECCOPY(targetmat[2], zaxis);
-       normalize_m3(targetmat);
-       
-       /* Find the difference between the two matrices */
-       invert_m3_m3(imat, targetmat);
-       mul_m3_m3m3(diffmat, imat, curmat);
-       
-       // old-method... let's see if using mat3_to_vec_roll is more accurate
-       //ebone->roll = atan2(diffmat[2][0], diffmat[2][2]);  
-       mat3_to_vec_roll(diffmat, delta, &ebone->roll);
-}
-
-void auto_align_ebone_topoint(EditBone *ebone, float *cursor)
-{
-       float   delta[3], curmat[3][3];
-       float   mat[4][4], tmat[4][4], imat[4][4];
-       float   rmat[4][4], rot[3];
-       float   vec[3];
-
-       /* find the current bone matrix as a 4x4 matrix (in Armature Space) */
-       sub_v3_v3v3(delta, ebone->tail, ebone->head);
-       vec_roll_to_mat3(delta, ebone->roll, curmat);
-       copy_m4_m3(mat, curmat);
-       VECCOPY(mat[3], ebone->head);
-       
-       /* multiply bone-matrix by object matrix (so that bone-matrix is in WorldSpace) */
-       invert_m4_m4(imat, mat);
-       
-       /* find position of cursor relative to bone */
-       mul_v3_m4v3(vec, imat, cursor);
-       
-       /* check that cursor is in usable position */
-       if ((IS_EQ(vec[0], 0)==0) && (IS_EQ(vec[2], 0)==0)) {
-               /* Compute a rotation matrix around y */
-               rot[1] = (float)atan2(vec[0], vec[2]);
-               rot[0] = rot[2] = 0.0f;
-               eul_to_mat4( rmat,rot);
+               /* project the new_up_axis along the normal */
+               project_v3_v3v3(vec, align_axis, nor);
+               sub_v3_v3v3(align_axis_proj, align_axis, vec);
                
-               /* Multiply the bone matrix by rotation matrix. This should be new bone-matrix */
-               mul_m4_m4m4(tmat, rmat, mat);
-               copy_m3_m4(curmat, tmat);
+               if(axis_only) {
+                       if(angle_v3v3(align_axis_proj, mat[2]) > M_PI/2) {
+                               negate_v3(align_axis_proj);
+                       }
+               }
                
-               /* Now convert from new bone-matrix, back to a roll value (in radians) */
-               mat3_to_vec_roll(curmat, delta, &ebone->roll);
-       }
-}
+               roll = angle_v3v3(align_axis_proj, mat[2]);
+               
+               cross_v3_v3v3(vec, mat[2], align_axis_proj);
+               
+               if (dot_v3v3(vec, nor) < 0) {
+                       roll = -roll;
+               }
 
-static void auto_align_ebone_tocursor(Scene *scene, View3D *v3d, EditBone *ebone)
-{
-       float cursor_local[3];
-       float   *cursor= give_cursor(scene, v3d);
-       float imat[3][3];
+               return roll;
+       }
 
-       copy_m3_m4(imat, scene->obedit->obmat);
-       invert_m3(imat);
-       copy_v3_v3(cursor_local, cursor);
-       mul_m3_v3(imat, cursor_local);
-       auto_align_ebone_topoint(ebone, cursor_local);
+       return 0.0f;
 }
 
+
 static EnumPropertyItem prop_calc_roll_types[] = {
-       {0, "GLOBALUP", 0, "Z-Axis Up", ""},
-       {1, "CURSOR", 0, "Z-Axis to Cursor", ""},
+       {0, "X", 0, "X Axis", ""},
+       {1, "Y", 0, "Y Axis", ""},
+       {2, "Z", 0, "Z Axis", ""},
+       {6, "VIEW", 0, "View Axis", ""},
+       {7, "CURSOR", 0, "Cursor", ""},
        {0, NULL, 0, NULL, NULL}
 };
 
+
 static int armature_calc_roll_exec(bContext *C, wmOperator *op) 
 {
-       Scene *scene= CTX_data_scene(C);
-       View3D *v3d= CTX_wm_view3d(C);
        Object *ob= CTX_data_edit_object(C);
-       void (*roll_func)(Scene *, View3D *, EditBone *) = NULL;
+       const short type= RNA_enum_get(op->ptr, "type");
+       const short axis_only= RNA_boolean_get(op->ptr, "axis_only");
+       const short axis_flip= RNA_boolean_get(op->ptr, "axis_flip");
+
+       float imat[3][3];
+
+       copy_m3_m4(imat, ob->obmat);
+       invert_m3(imat);
+
+       if(type==7) { /* Cursor */
+               Scene *scene= CTX_data_scene(C);
+               View3D *v3d= CTX_wm_view3d(C); /* can be NULL */
+               float cursor_local[3];
+               float   *cursor= give_cursor(scene, v3d);
        
-       /* specific method used to calculate roll depends on mode */
-       switch (RNA_enum_get(op->ptr, "type")) {
-               case 1:  /* Z-Axis point towards cursor */
-                       roll_func= auto_align_ebone_tocursor;
-                       break;
-               default: /* Z-Axis Point Up */
-                       roll_func= auto_align_ebone_zaxisup;
-                       break;
+
+               copy_v3_v3(cursor_local, cursor);
+               mul_m3_v3(imat, cursor_local);
+
+               /* cursor */
+               CTX_DATA_BEGIN(C, EditBone *, ebone, selected_editable_bones) {
+                       float cursor_rel[3];
+                       sub_v3_v3v3(cursor_rel, cursor_local, ebone->head);
+                       if(axis_flip) negate_v3(cursor_rel);
+                       ebone->roll= ED_rollBoneToVector(ebone, cursor_rel, axis_only);
+               }
+               CTX_DATA_END;
        }
-       
-       /* recalculate roll on selected bones */
-       CTX_DATA_BEGIN(C, EditBone *, ebone, selected_editable_bones) {
-               /* roll func is a callback which assumes that all is well */
-               roll_func(scene, v3d, ebone);
+       else {
+               float vec[3]= {0.0f, 0.0f, 0.0f};
+               if(type==6) { /* View */
+                       RegionView3D *rv3d= CTX_wm_region_view3d(C);
+                       if(rv3d==NULL) {
+                               BKE_report(op->reports, RPT_ERROR, "No region view3d available");
+                               return OPERATOR_CANCELLED;
+                       }
+
+                       copy_v3_v3(vec, rv3d->viewinv[2]);
+               }
+               else { /* Axis */
+                       assert(type >= 0 && type <= 5);
+                       if(type<3)      vec[type]= 1.0f; 
+                       else            vec[type-2]= -1.0f; 
+               }
+
+               mul_m3_v3(imat, vec);
+               if(axis_flip) negate_v3(vec);
+
+               CTX_DATA_BEGIN(C, EditBone *, ebone, selected_editable_bones) {
+                       /* roll func is a callback which assumes that all is well */
+                       ebone->roll= ED_rollBoneToVector(ebone, vec, axis_only);
+               }
+               CTX_DATA_END;
        }
-       CTX_DATA_END;
-       
 
        /* note, notifier might evolve */
        WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
@@ -2203,9 +2191,11 @@ void ARMATURE_OT_calculate_roll(wmOperatorType *ot)
        
        /* flags */
        ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
-       
+
        /* properties */
        ot->prop= RNA_def_enum(ot->srna, "type", prop_calc_roll_types, 0, "Type", "");
+       RNA_def_boolean(ot->srna, "axis_flip", 0, "Flip Axis", "Negate the alignment axis.");
+       RNA_def_boolean(ot->srna, "axis_only", 0, "Shortest Rotation", "Ignore the axis direction, use the shortest rotation to align.");
 }
 
 /* **************** undo for armatures ************** */
@@ -2235,6 +2225,8 @@ static void undoBones_to_editBones(void *uarmv, void *armv)
                ebo= uarm->act_edbone;
                arm->act_edbone= ebo->temp;
        }
+       else
+               arm->act_edbone= NULL;
 
        /* set pointers */
        for(newebo= arm->edbo->first; newebo; newebo= newebo->next) {
@@ -2293,7 +2285,7 @@ static void *get_armature_edit(bContext *C)
 }
 
 /* and this is all the undo system needs to know */
-void undo_push_armature(bContext *C, char *name)
+void undo_push_armature(bContext *C, const char *name)
 {
        // XXX solve getdata()
        undo_editmode_push(C, name, get_armature_edit, free_undoBones, undoBones_to_editBones, editBones_to_undoBones, NULL);
@@ -2305,11 +2297,11 @@ void undo_push_armature(bContext *C, char *name)
 /* *************** Adding stuff in editmode *************** */
 
 /* default bone add, returns it selected, but without tail set */
-EditBone *ED_armature_edit_bone_add(bArmature *arm, char *name)
+EditBone *ED_armature_edit_bone_add(bArmature *arm, const char *name)
 {
        EditBone *bone= MEM_callocN(sizeof(EditBone), "eBone");
        
-       BLI_strncpy(bone->name, name, 32);
+       BLI_strncpy(bone->name, name, sizeof(bone->name));
        unique_editbone_name(arm->edbo, bone->name, NULL);
        
        BLI_addtail(arm->edbo, bone);
@@ -2348,7 +2340,7 @@ void add_primitive_bone(Scene *scene, View3D *v3d, RegionView3D *rv3d)
        mul_m3_m3m3(totmat, obmat, viewmat);
        invert_m3_m3(imat, totmat);
        
-       ED_armature_deselectall(obedit, 0, 0);
+       ED_armature_deselect_all(obedit, 0);
        
        /*      Create a bone   */
        bone= ED_armature_edit_bone_add(obedit->data, "Bone");
@@ -2365,7 +2357,7 @@ void add_primitive_bone(Scene *scene, View3D *v3d, RegionView3D *rv3d)
 
 /* previously addvert_armature */
 /* the ctrl-click method */
-static int armature_click_extrude_exec(bContext *C, wmOperator *op)
+static int armature_click_extrude_exec(bContext *C, wmOperator *UNUSED(op))
 {
        View3D *v3d;
        bArmature *arm;
@@ -2401,7 +2393,7 @@ static int armature_click_extrude_exec(bContext *C, wmOperator *op)
                to_root= 1;
        }
        
-       ED_armature_deselectall(obedit, 0, 0);
+       ED_armature_deselect_all(obedit, 0);
        
        /* we re-use code for mirror editing... */
        flipbone= NULL;
@@ -2421,19 +2413,19 @@ static int armature_click_extrude_exec(bContext *C, wmOperator *op)
                arm->act_edbone= newbone;
                
                if (to_root) {
-                       VECCOPY(newbone->head, ebone->head);
+                       copy_v3_v3(newbone->head, ebone->head);
                        newbone->rad_head= ebone->rad_tail;
                        newbone->parent= ebone->parent;
                }
                else {
-                       VECCOPY(newbone->head, ebone->tail);
+                       copy_v3_v3(newbone->head, ebone->tail);
                        newbone->rad_head= ebone->rad_tail;
                        newbone->parent= ebone;
                        newbone->flag |= BONE_CONNECTED;
                }
                
                curs= give_cursor(scene, v3d);
-               VECCOPY(newbone->tail, curs);
+               copy_v3_v3(newbone->tail, curs);
                sub_v3_v3v3(newbone->tail, newbone->tail, obedit->obmat[3]);
                
                if (a==1) 
@@ -2477,7 +2469,7 @@ static int armature_click_extrude_invoke(bContext *C, wmOperator *op, wmEvent *e
        
        fp= give_cursor(scene, v3d);
        
-       VECCOPY(oldcurs, fp);
+       copy_v3_v3(oldcurs, fp);
        
        mx= event->x - ar->winrct.xmin;
        my= event->y - ar->winrct.ymin;
@@ -2507,7 +2499,7 @@ static int armature_click_extrude_invoke(bContext *C, wmOperator *op, wmEvent *e
        retv= armature_click_extrude_exec(C, op);
 
        /* restore previous 3d cursor position */
-       VECCOPY(fp, oldcurs);
+       copy_v3_v3(fp, oldcurs);
 
        return retv;
 }
@@ -2536,8 +2528,8 @@ static EditBone *add_points_bone (Object *obedit, float head[], float tail[])
        
        ebo= ED_armature_edit_bone_add(obedit->data, "Bone");
        
-       VECCOPY(ebo->head, head);
-       VECCOPY(ebo->tail, tail);
+       copy_v3_v3(ebo->head, head);
+       copy_v3_v3(ebo->tail, tail);
        
        return ebo;
 }
@@ -2641,7 +2633,7 @@ EditBone *duplicateEditBoneObjects(EditBone *curBone, char *name, ListBase *edit
        
        if (name != NULL)
        {
-               BLI_strncpy(eBone->name, name, 32);
+               BLI_strncpy(eBone->name, name, sizeof(eBone->name));
        }
 
        unique_editbone_name(editbones, eBone->name, NULL);
@@ -2679,7 +2671,7 @@ EditBone *duplicateEditBone(EditBone *curBone, char *name, ListBase *editbones,
 }
 
 /* previously adduplicate_armature */
-static int armature_duplicate_selected_exec(bContext *C, wmOperator *op)
+static int armature_duplicate_selected_exec(bContext *C, wmOperator *UNUSED(op))
 {
        bArmature *arm;
        EditBone        *eBone = NULL;
@@ -2864,10 +2856,10 @@ static void fill_add_joint (EditBone *ebo, short eb_tail, ListBase *points)
        short found= 0;
        
        if (eb_tail) {
-               VECCOPY(vec, ebo->tail);
+               copy_v3_v3(vec, ebo->tail);
        }
        else {
-               VECCOPY(vec, ebo->head);
+               copy_v3_v3(vec, ebo->head);
        }
        
        for (ebp= points->first; ebp; ebp= ebp->next) {
@@ -2896,11 +2888,11 @@ static void fill_add_joint (EditBone *ebo, short eb_tail, ListBase *points)
                ebp= MEM_callocN(sizeof(EditBonePoint), "EditBonePoint");
                
                if (eb_tail) {
-                       VECCOPY(ebp->vec, ebo->tail);
+                       copy_v3_v3(ebp->vec, ebo->tail);
                        ebp->tail_owner= ebo;
                }
                else {
-                       VECCOPY(ebp->vec, ebo->head);
+                       copy_v3_v3(ebp->vec, ebo->head);
                        ebp->head_owner= ebo;
                }
                
@@ -3112,8 +3104,8 @@ static void bones_merge(Object *obedit, EditBone *start, EditBone *end, EditBone
        /* step 2a: parent children of in-between bones to newbone */
        for (chain= chains->first; chain; chain= chain->next) {
                /* ick: we need to check if parent of each bone in chain is one of the bones in the */
+               short found= 0;
                for (ebo= chain->data; ebo; ebo= ebo->parent) {
-                       short found= 0;
                        
                        /* try to find which bone from the list to be removed, is the parent */
                        for (ebone= end; ebone; ebone= ebone->parent) {
@@ -3129,6 +3121,9 @@ static void bones_merge(Object *obedit, EditBone *start, EditBone *end, EditBone
                                break;
                        }
                }
+               if (found) {
+                       break;
+               }
        }
        
        /* step 2b: parent child of end to newbone (child from this chain) */
@@ -3140,6 +3135,9 @@ static void bones_merge(Object *obedit, EditBone *start, EditBone *end, EditBone
                ebone= (ebo == start) ? (NULL) : (ebo->parent);
                bone_free(arm, ebo);
        }
+       
+       newbone->flag |= (BONE_ROOTSEL|BONE_TIPSEL|BONE_SELECTED);
+       ED_armature_sync_selection(arm->edbo);
 }
 
 
@@ -3244,6 +3242,81 @@ void ARMATURE_OT_merge (wmOperatorType *ot)
 /* ************** END Add/Remove stuff in editmode ************ */
 /* *************** Tools in editmode *********** */
 
+static int armature_hide_exec(bContext *C, wmOperator *UNUSED(op))
+{
+       Object *obedit= CTX_data_edit_object(C);
+       bArmature *arm= obedit->data;
+       EditBone *ebone;
+
+       /* cancel if nothing selected */
+       if (CTX_DATA_COUNT(C, selected_bones) == 0)
+               return OPERATOR_CANCELLED;
+
+       for (ebone = arm->edbo->first; ebone; ebone=ebone->next) {
+               if (EBONE_VISIBLE(arm, ebone)) {
+                       if (ebone->flag & BONE_SELECTED) {
+                               ebone->flag &= ~(BONE_TIPSEL|BONE_SELECTED|BONE_ROOTSEL);
+                               ebone->flag |= BONE_HIDDEN_A;
+                       }
+               }
+       }
+       ED_armature_validate_active(arm);
+       ED_armature_sync_selection(arm->edbo);
+
+       WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, obedit);
+
+       return OPERATOR_FINISHED;
+}
+
+void ARMATURE_OT_hide(wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name= "Hide Selected Bones";
+       ot->idname= "ARMATURE_OT_hide";
+       
+       /* api callbacks */
+       ot->exec= armature_hide_exec;
+       ot->poll= ED_operator_editarmature;
+       
+       /* flags */
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+}
+
+static int armature_reveal_exec(bContext *C, wmOperator *UNUSED(op))
+{
+       Object *obedit= CTX_data_edit_object(C);
+       bArmature *arm= obedit->data;
+       EditBone *ebone;
+       
+       for (ebone = arm->edbo->first; ebone; ebone=ebone->next) {
+               if(arm->layer & ebone->layer) {
+                       if (ebone->flag & BONE_HIDDEN_A) {
+                               ebone->flag |= (BONE_TIPSEL|BONE_SELECTED|BONE_ROOTSEL);
+                               ebone->flag &= ~BONE_HIDDEN_A;
+                       }
+               }
+       }
+       ED_armature_validate_active(arm);
+       ED_armature_sync_selection(arm->edbo);
+
+       WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, obedit);
+
+       return OPERATOR_FINISHED;
+}
+
+void ARMATURE_OT_reveal(wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name= "Reveal Bones";
+       ot->idname= "ARMATURE_OT_reveal";
+       
+       /* api callbacks */
+       ot->exec= armature_reveal_exec;
+       ot->poll= ED_operator_editarmature;
+       
+       /* flags */
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+}
 
 void hide_selected_armature_bones(Scene *scene)
 {
@@ -3261,7 +3334,6 @@ void hide_selected_armature_bones(Scene *scene)
        }
        ED_armature_validate_active(arm);
        ED_armature_sync_selection(arm->edbo);
-       BIF_undo_push("Hide Bones");
 }
 
 
@@ -3284,7 +3356,6 @@ static void hide_unselected_armature_bones(Scene *scene)
 
        ED_armature_validate_active(arm);
        ED_armature_sync_selection(arm->edbo);
-       BIF_undo_push("Hide Unselected Bones");
 }
 #endif
 
@@ -3305,7 +3376,6 @@ void show_all_armature_bones(Scene *scene)
        }
        ED_armature_validate_active(arm);
        ED_armature_sync_selection(arm->edbo);
-       BIF_undo_push("Reveal Bones");
 }
 #endif
 
@@ -3407,7 +3477,7 @@ static int armature_extrude_exec(bContext *C, wmOperator *op)
                                        newbone->segments= 1;
                                        newbone->layer= ebone->layer;
                                        
-                                       BLI_strncpy (newbone->name, ebone->name, 32);
+                                       BLI_strncpy (newbone->name, ebone->name, sizeof(newbone->name));
                                        
                                        if (flipbone && forked) {       // only set if mirror edit
                                                if (strlen(newbone->name)<30) {
@@ -3490,7 +3560,7 @@ static int armature_bone_primitive_add_exec(bContext *C, wmOperator *op)
        mul_m3_m3m3(totmat, obmat, viewmat);
        invert_m3_m3(imat, totmat);
        
-       ED_armature_deselectall(obedit, 0, 0);
+       ED_armature_deselect_all(obedit, 0);
        
        /*      Create a bone   */
        bone= ED_armature_edit_bone_add(obedit->data, name);
@@ -3542,10 +3612,7 @@ static int armature_subdivide_exec(bContext *C, wmOperator *op)
        int numcuts, i;
        
        /* there may not be a number_cuts property defined (for 'simple' subdivide) */
-       if (RNA_property_is_set(op->ptr, "number_cuts"))
-               numcuts= RNA_int_get(op->ptr, "number_cuts");
-       else
-               numcuts= 1;
+       numcuts= RNA_int_get(op->ptr, "number_cuts");
        
        /* loop over all editable bones */
        // XXX the old code did this in reverse order though!
@@ -3600,26 +3667,11 @@ static int armature_subdivide_exec(bContext *C, wmOperator *op)
        return OPERATOR_FINISHED;
 }
 
-
-void ARMATURE_OT_subdivide_simple(wmOperatorType *ot)
-{
-       /* identifiers */
-       ot->name= "Subdivide Simple";
-       ot->idname= "ARMATURE_OT_subdivide_simple";
-       
-       /* api callbacks */
-       ot->exec = armature_subdivide_exec;
-       ot->poll = ED_operator_editarmature;
-       
-       /* flags */
-       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
-}
-
-void ARMATURE_OT_subdivide_multi(wmOperatorType *ot)
+void ARMATURE_OT_subdivide(wmOperatorType *ot)
 {
        /* identifiers */
        ot->name= "Subdivide Multi";
-       ot->idname= "ARMATURE_OT_subdivide_multi";
+       ot->idname= "ARMATURE_OT_subdivide";
        
        /* api callbacks */
        ot->exec = armature_subdivide_exec;
@@ -3629,65 +3681,7 @@ void ARMATURE_OT_subdivide_multi(wmOperatorType *ot)
        ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
        
        /* Properties */
-       RNA_def_int(ot->srna, "number_cuts", 2, 1, INT_MAX, "Number of Cuts", "", 1, 10);
-}
-
-
-
-static int armature_subdivs_invoke(bContext *C, wmOperator *op, wmEvent *event)
-{
-       uiPopupMenu *pup;
-       uiLayout *layout;
-
-       pup= uiPupMenuBegin(C, "Subdivision Type", 0);
-       layout= uiPupMenuLayout(pup);
-       uiItemsEnumO(layout, "ARMATURE_OT_subdivs", "type");
-       uiPupMenuEnd(C, pup);
-       
-       return OPERATOR_CANCELLED;
-}
-
-static int armature_subdivs_exec(bContext *C, wmOperator *op)
-{      
-       switch (RNA_int_get(op->ptr, "type"))
-       {
-               case 0: /* simple */
-                       RNA_int_set(op->ptr, "number_cuts", 1);
-                       armature_subdivide_exec(C, op);
-                       break;
-               case 1: /* multi */
-                       armature_subdivide_exec(C, op);
-                       break;
-       }
-       
-       return OPERATOR_FINISHED;
-}
-
-void ARMATURE_OT_subdivs(wmOperatorType *ot)
-{
-       static EnumPropertyItem type_items[]= {
-                {0, "SIMPLE", 0, "Simple", ""},
-               {1, "MULTI", 0, "Multi", ""},
-               {0, NULL, 0, NULL, NULL}};
-
-       /* identifiers */
-       ot->name= "subdivs";
-       ot->idname= "ARMATURE_OT_subdivs";
-       
-       /* api callbacks */
-       ot->invoke= armature_subdivs_invoke;
-       ot->exec= armature_subdivs_exec;
-       
-       ot->poll= ED_operator_editarmature;
-       
-       /* flags */
-       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
-       
-       /* props */
-       RNA_def_enum(ot->srna, "type", type_items, 0, "Type", "");
-       
-       /* this is temp, the ops are different, but they are called from subdivs, so all the possible props should be here as well*/
-       RNA_def_int(ot->srna, "number_cuts", 2, 1, INT_MAX, "Number of Cuts", "", 1, 10); 
+       RNA_def_int(ot->srna, "number_cuts", 1, 1, INT_MAX, "Number of Cuts", "", 1, 10);
 }
 
 /* ----------- */
@@ -3698,7 +3692,7 @@ void ARMATURE_OT_subdivs(wmOperatorType *ot)
  * this to be done easily.
  */
 
-static int armature_switch_direction_exec(bContext *C, wmOperator *op
+static int armature_switch_direction_exec(bContext *C, wmOperator *UNUSED(op)
 {
        Object *ob= CTX_data_edit_object(C);
        bArmature *arm= (bArmature *)ob->data;
@@ -3919,7 +3913,7 @@ static int armature_parent_set_exec(bContext *C, wmOperator *op)
        return OPERATOR_FINISHED;
 }
 
-static int armature_parent_set_invoke(bContext *C, wmOperator *op, wmEvent *event)
+static int armature_parent_set_invoke(bContext *C, wmOperator *UNUSED(op), wmEvent *UNUSED(event))
 {
        EditBone *actbone = CTX_data_active_bone(C);
        uiPopupMenu *pup= uiPupMenuBegin(C, "Make Parent ", 0);
@@ -4016,7 +4010,7 @@ void ARMATURE_OT_parent_clear(wmOperatorType *ot)
 
 /* ****************  Selections  ******************/
 
-static int armature_select_inverse_exec(bContext *C, wmOperator *op)
+static int armature_select_inverse_exec(bContext *C, wmOperator *UNUSED(op))
 {
        /*      Set the flags */
        CTX_DATA_BEGIN(C, EditBone *, ebone, visible_bones) {
@@ -4373,7 +4367,7 @@ int ED_do_pose_selectbuffer(Scene *scene, Base *base, unsigned int *buffer, shor
                
                /* since we do unified select, we don't shift+select a bone if the armature object was not active yet */
                if (!(extend) || (base != scene->basact)) {
-                       ED_pose_deselectall(ob, 0, 0);
+                       ED_pose_deselectall(ob, 0);
                        nearBone->flag |= (BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL);
                        arm->act_bone= nearBone;
                        
@@ -4420,7 +4414,7 @@ int ED_do_pose_selectbuffer(Scene *scene, Base *base, unsigned int *buffer, shor
    test==2: only clear active tag
    test==3: swap select (no test / inverse selection status of all independently)
 */
-void ED_pose_deselectall (Object *ob, int test, int doundo)
+void ED_pose_deselectall (Object *ob, int test)
 {
        bArmature *arm= ob->data;
        bPoseChannel *pchan;
@@ -4432,7 +4426,7 @@ void ED_pose_deselectall (Object *ob, int test, int doundo)
        /*      Determine if we're selecting or deselecting     */
        if (test==1) {
                for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
-                       if ((pchan->bone->layer & arm->layer) && !(pchan->bone->flag & BONE_HIDDEN_P)) {
+                       if (PBONE_VISIBLE(arm, pchan->bone)) {
                                if (pchan->bone->flag & BONE_SELECTED)
                                        break;
                        }
@@ -4460,16 +4454,9 @@ void ED_pose_deselectall (Object *ob, int test, int doundo)
        
        if(arm->act_bone && (arm->act_bone->flag & BONE_SELECTED)==0)
                arm->act_bone= NULL;
-
-       //countall(); // XXX need an equivalent to this...
-       
-       if (doundo) {
-               if (selectmode==1) BIF_undo_push("Select All");
-               else BIF_undo_push("Deselect All");
-       }
 }
 
-static int bone_skinnable(Object *ob, Bone *bone, void *datap)
+static int bone_skinnable_cb(Object *ob, Bone *bone, void *datap)
 {
        /* Bones that are deforming
         * are regarded to be "skinnable" and are eligible for
@@ -4518,7 +4505,7 @@ static int bone_skinnable(Object *ob, Bone *bone, void *datap)
        return 0;
 }
 
-static int ED_vgroup_add_unique_bone(Object *ob, Bone *bone, void *data
+static int vgroup_add_unique_bone_cb(Object *ob, Bone *bone, void *UNUSED(ptr)
 {
        /* This group creates a vertex group to ob that has the
          * same name as bone (provided the bone is skinnable). 
@@ -4533,7 +4520,7 @@ static int ED_vgroup_add_unique_bone(Object *ob, Bone *bone, void *data)
        return 0;
 }
 
-static int dgroup_skinnable(Object *ob, Bone *bone, void *datap) 
+static int dgroup_skinnable_cb(Object *ob, Bone *bone, void *datap) 
 {
        /* Bones that are deforming
         * are regarded to be "skinnable" and are eligible for
@@ -4589,7 +4576,7 @@ static int dgroup_skinnable(Object *ob, Bone *bone, void *datap)
        return 0;
 }
 
-static void add_vgroups__mapFunc(void *userData, int index, float *co, float *no_f, short *no_s)
+static void add_vgroups__mapFunc(void *userData, int index, float *co, float *UNUSED(no_f), short *UNUSED(no_s))
 {
        /* DerivedMesh mapFunc for getting final coords in weight paint mode */
 
@@ -4640,7 +4627,7 @@ static void envelope_bone_weighting(Object *ob, Mesh *mesh, float (*verts)[3], i
        }
 }
 
-void add_verts_to_dgroups(Scene *scene, Object *ob, Object *par, int heat, int mirror)
+void add_verts_to_dgroups(ReportList *reports, Scene *scene, Object *ob, Object *par, int heat, int mirror)
 {
        /* This functions implements the automatic computation of vertex group
         * weights, either through envelopes or using a heat equilibrium.
@@ -4657,7 +4644,7 @@ void add_verts_to_dgroups(Scene *scene, Object *ob, Object *par, int heat, int m
        bArmature *arm= par->data;
        Bone **bonelist, *bone;
        bDeformGroup **dgrouplist, **dgroupflip;
-       bDeformGroup *dgroup, *curdg;
+       bDeformGroup *dgroup;
        bPoseChannel *pchan;
        Mesh *mesh;
        Mat4 *bbone = NULL;
@@ -4672,7 +4659,7 @@ void add_verts_to_dgroups(Scene *scene, Object *ob, Object *par, int heat, int m
        looper_data.list= NULL;
 
        /* count the number of skinnable bones */
-       numbones = bone_looper(ob, arm->bonebase.first, &looper_data, bone_skinnable);
+       numbones = bone_looper(ob, arm->bonebase.first, &looper_data, bone_skinnable_cb);
        
        if (numbones == 0)
                return;
@@ -4681,7 +4668,7 @@ void add_verts_to_dgroups(Scene *scene, Object *ob, Object *par, int heat, int m
         * and fill it with all of the skinnable bones */
        bonelist = MEM_callocN(numbones*sizeof(Bone *), "bonelist");
        looper_data.list= bonelist;
-       bone_looper(ob, arm->bonebase.first, &looper_data, bone_skinnable);
+       bone_looper(ob, arm->bonebase.first, &looper_data, bone_skinnable_cb);
 
        /* create an array of pointers to the deform groups that
         * coorespond to the skinnable bones (creating them
@@ -4690,7 +4677,7 @@ void add_verts_to_dgroups(Scene *scene, Object *ob, Object *par, int heat, int m
        dgroupflip = MEM_callocN(numbones*sizeof(bDeformGroup *), "dgroupflip");
 
        looper_data.list= dgrouplist;
-       bone_looper(ob, arm->bonebase.first, &looper_data, dgroup_skinnable);
+       bone_looper(ob, arm->bonebase.first, &looper_data, dgroup_skinnable_cb);
 
        /* create an array of root and tip positions transformed into
         * global coords */
@@ -4751,13 +4738,7 @@ void add_verts_to_dgroups(Scene *scene, Object *ob, Object *par, int heat, int m
 
                        // 0 = don't strip off number extensions
                        flip_side_name(name, dgroup->name, FALSE);
-
-                       for (curdg = ob->defbase.first; curdg; curdg=curdg->next) {
-                               if (!strcmp(curdg->name, name))
-                                       break;
-                       }
-                       
-                       dgroupflip[j] = curdg;
+                       dgroupflip[j] = defgroup_find_name(ob, name);
                }
        }
 
@@ -4793,14 +4774,22 @@ void add_verts_to_dgroups(Scene *scene, Object *ob, Object *par, int heat, int m
 
        /* compute the weights based on gathered vertices and bones */
        if (heat) {
+               const char *error= NULL;
                heat_bone_weighting(ob, mesh, verts, numbones, dgrouplist, dgroupflip,
-                       root, tip, selected);
+                       root, tip, selected, &error);
+               
+               if(error) {
+                       BKE_report(reports, RPT_WARNING, error);
+               }
        }
        else {
                envelope_bone_weighting(ob, mesh, verts, numbones, bonelist, dgrouplist,
                        dgroupflip, root, tip, selected, mat4_to_scale(par->obmat));
        }
-       
+
+       /* only generated in some cases but can call anyway */
+       mesh_octree_table(ob, NULL, NULL, 'e');
+
        /* free the memory allocated */
        MEM_freeN(bonelist);
        MEM_freeN(dgrouplist);
@@ -4811,7 +4800,7 @@ void add_verts_to_dgroups(Scene *scene, Object *ob, Object *par, int heat, int m
        MEM_freeN(verts);
 }
 
-void create_vgroups_from_armature(Scene *scene, Object *ob, Object *par, int mode, int mirror)
+void create_vgroups_from_armature(ReportList *reports, Scene *scene, Object *ob, Object *par, int mode, int mirror)
 {
        /* Lets try to create some vertex groups 
         * based on the bones of the parent armature.
@@ -4822,7 +4811,7 @@ void create_vgroups_from_armature(Scene *scene, Object *ob, Object *par, int mod
                /* Traverse the bone list, trying to create empty vertex 
                 * groups cooresponding to the bone.
                 */
-               bone_looper(ob, arm->bonebase.first, NULL, ED_vgroup_add_unique_bone);
+               bone_looper(ob, arm->bonebase.first, NULL, vgroup_add_unique_bone_cb);
 
                if (ob->type == OB_MESH)
                        ED_vgroup_data_create(ob->data);
@@ -4832,17 +4821,15 @@ void create_vgroups_from_armature(Scene *scene, Object *ob, Object *par, int mod
                 * that are populated with the vertices for which the
                 * bone is closest.
                 */
-               add_verts_to_dgroups(scene, ob, par, (mode == ARM_GROUPS_AUTO), mirror);
+               add_verts_to_dgroups(reports, scene, ob, par, (mode == ARM_GROUPS_AUTO), mirror);
        }
 } 
 /* ************* Clear Pose *****************************/
 
-static int pose_clear_scale_exec(bContext *C, wmOperator *op
+static int pose_clear_scale_exec(bContext *C, wmOperator *UNUSED(op)
 {
        Scene *scene= CTX_data_scene(C);
-       Object *ob= CTX_data_active_object(C);
-       
-       KeyingSet *ks= ANIM_builtin_keyingset_get_named(NULL, "Scaling");
+       Object *ob= ED_object_pose_armature(CTX_data_active_object(C));
        short autokey = 0;
        
        /* only clear those channels that are not locked */
@@ -4873,12 +4860,15 @@ static int pose_clear_scale_exec(bContext *C, wmOperator *op)
        
        /* perform autokeying on the bones if needed */
        if (autokey) {
+               /* get KeyingSet to use */
+               KeyingSet *ks = ANIM_get_keyingset_for_autokeying(scene, "Scaling");
+               
                /* insert keyframes */
                ANIM_apply_keyingset(C, NULL, NULL, ks, MODIFYKEY_MODE_INSERT, (float)CFRA);
                
                /* now recalculate paths */
                if ((ob->pose->avs.path_bakeflag & MOTIONPATH_BAKE_HAS_PATHS))
-                       ED_pose_recalculate_paths(C, scene, ob);
+                       ED_pose_recalculate_paths(scene, ob);
        }
        
        DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
@@ -4904,12 +4894,10 @@ void POSE_OT_scale_clear(wmOperatorType *ot)
        ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 }
 
-static int pose_clear_loc_exec(bContext *C, wmOperator *op
+static int pose_clear_loc_exec(bContext *C, wmOperator *UNUSED(op)
 {
        Scene *scene= CTX_data_scene(C);
-       Object *ob= CTX_data_active_object(C);
-       
-       KeyingSet *ks= ANIM_builtin_keyingset_get_named(NULL, "Location");
+       Object *ob= ED_object_pose_armature(CTX_data_active_object(C));
        short autokey = 0;
        
        /* only clear those channels that are not locked */
@@ -4941,12 +4929,15 @@ static int pose_clear_loc_exec(bContext *C, wmOperator *op)
        
        /* perform autokeying on the bones if needed */
        if (autokey) {
+               /* get KeyingSet to use */
+               KeyingSet *ks = ANIM_get_keyingset_for_autokeying(scene, "Location");
+               
                /* insert keyframes */
                ANIM_apply_keyingset(C, NULL, NULL, ks, MODIFYKEY_MODE_INSERT, (float)CFRA);
                
                /* now recalculate paths */
                if ((ob->pose->avs.path_bakeflag & MOTIONPATH_BAKE_HAS_PATHS))
-                       ED_pose_recalculate_paths(C, scene, ob);
+                       ED_pose_recalculate_paths(scene, ob);
        }
        
        DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
@@ -4972,12 +4963,10 @@ void POSE_OT_loc_clear(wmOperatorType *ot)
        ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 }
 
-static int pose_clear_rot_exec(bContext *C, wmOperator *op
+static int pose_clear_rot_exec(bContext *C, wmOperator *UNUSED(op)
 {
        Scene *scene= CTX_data_scene(C);
-       Object *ob= CTX_data_active_object(C);
-       
-       KeyingSet *ks= ANIM_builtin_keyingset_get_named(NULL, "Rotation");
+       Object *ob= ED_object_pose_armature(CTX_data_active_object(C));
        short autokey = 0;
        
        /* only clear those channels that are not locked */
@@ -5093,12 +5082,15 @@ static int pose_clear_rot_exec(bContext *C, wmOperator *op)
        
        /* perform autokeying on the bones if needed */
        if (autokey) {
+               /* get KeyingSet to use */
+               KeyingSet *ks = ANIM_get_keyingset_for_autokeying(scene, "Rotation");
+               
                /* insert keyframes */
                ANIM_apply_keyingset(C, NULL, NULL, ks, MODIFYKEY_MODE_INSERT, (float)CFRA);
                
                /* now recalculate paths */
                if ((ob->pose->avs.path_bakeflag & MOTIONPATH_BAKE_HAS_PATHS))
-                       ED_pose_recalculate_paths(C, scene, ob);
+                       ED_pose_recalculate_paths(scene, ob);
        }
        
        DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
@@ -5127,7 +5119,7 @@ void POSE_OT_rot_clear(wmOperatorType *ot)
 
 /* ***************** selections ********************** */
 
-static int pose_select_inverse_exec(bContext *C, wmOperator *op)
+static int pose_select_inverse_exec(bContext *C, wmOperator *UNUSED(op))
 {
        
        /*      Set the flags */
@@ -5224,9 +5216,9 @@ void POSE_OT_select_all(wmOperatorType *ot)
        WM_operator_properties_select_all(ot);
 }
 
-static int pose_select_parent_exec(bContext *C, wmOperator *op)
+static int pose_select_parent_exec(bContext *C, wmOperator *UNUSED(op))
 {
-       Object *ob= CTX_data_active_object(C);
+       Object *ob= ED_object_pose_armature(CTX_data_active_object(C));
        bPoseChannel *pchan,*parent;
 
        /*      Determine if there is an active bone */
@@ -5268,13 +5260,14 @@ void POSE_OT_select_parent(wmOperatorType *ot)
 
 /* ************* hide/unhide pose bones ******************* */
 
-static int hide_selected_pose_bone(Object *ob, Bone *bone, void *ptr
+static int hide_selected_pose_bone_cb(Object *ob, Bone *bone, void *UNUSED(ptr)
 {
        bArmature *arm= ob->data;
        
        if (arm->layer & bone->layer) {
                if (bone->flag & BONE_SELECTED) {
                        bone->flag |= BONE_HIDDEN_P;
+                       bone->flag &= ~BONE_SELECTED;
                        if(arm->act_bone==bone)
                                arm->act_bone= NULL;
                }
@@ -5282,7 +5275,7 @@ static int hide_selected_pose_bone(Object *ob, Bone *bone, void *ptr)
        return 0;
 }
 
-static int hide_unselected_pose_bone(Object *ob, Bone *bone, void *ptr
+static int hide_unselected_pose_bone_cb(Object *ob, Bone *bone, void *UNUSED(ptr)
 {
        bArmature *arm= ob->data;
        
@@ -5300,15 +5293,13 @@ static int hide_unselected_pose_bone(Object *ob, Bone *bone, void *ptr)
 /* active object is armature in posemode, poll checked */
 static int pose_hide_exec(bContext *C, wmOperator *op) 
 {
-       Object *ob= CTX_data_active_object(C);
+       Object *ob= ED_object_pose_armature(CTX_data_active_object(C));
        bArmature *arm= ob->data;
 
        if(RNA_boolean_get(op->ptr, "unselected"))
-          bone_looper(ob, arm->bonebase.first, NULL, 
-                               hide_unselected_pose_bone);
+          bone_looper(ob, arm->bonebase.first, NULL, hide_unselected_pose_bone_cb);
        else
-          bone_looper(ob, arm->bonebase.first, NULL, 
-                                  hide_selected_pose_bone);
+          bone_looper(ob, arm->bonebase.first, NULL, hide_selected_pose_bone_cb);
        
        /* note, notifier might evolve */
        WM_event_add_notifier(C, NC_OBJECT|ND_BONE_SELECT, ob);
@@ -5333,7 +5324,7 @@ void POSE_OT_hide(wmOperatorType *ot)
        RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "");
 }
 
-static int show_pose_bone(Object *ob, Bone *bone, void *ptr
+static int show_pose_bone_cb(Object *ob, Bone *bone, void *UNUSED(ptr)
 {
        bArmature *arm= ob->data;
        
@@ -5348,12 +5339,12 @@ static int show_pose_bone(Object *ob, Bone *bone, void *ptr)
 }
 
 /* active object is armature in posemode, poll checked */
-static int pose_reveal_exec(bContext *C, wmOperator *op
+static int pose_reveal_exec(bContext *C, wmOperator *UNUSED(op)
 {
-       Object *ob= CTX_data_active_object(C);
+       Object *ob= ED_object_pose_armature(CTX_data_active_object(C));
        bArmature *arm= ob->data;
        
-       bone_looper(ob, arm->bonebase.first, NULL, show_pose_bone);
+       bone_looper(ob, arm->bonebase.first, NULL, show_pose_bone_cb);
        
        /* note, notifier might evolve */
        WM_event_add_notifier(C, NC_OBJECT|ND_BONE_SELECT, ob);
@@ -5377,33 +5368,17 @@ void POSE_OT_reveal(wmOperatorType *ot)
 
 /* ************* RENAMING DISASTERS ************ */
 
-/* note: there's a unique_editbone_name() too! */
-void unique_bone_name (bArmature *arm, char *name)
+static int bone_unique_check(void *arg, const char *name)
 {
-       char            tempname[64];
-       int                     number;
-       char            *dot;
-       
-       if (get_named_bone(arm, name)) {
-               
-               /*      Strip off the suffix, if it's a number */
-               number= strlen(name);
-               if(number && isdigit(name[number-1])) {
-                       dot= strrchr(name, '.');        // last occurrence
-                       if (dot)
-                               *dot=0;
-               }
-               
-               for (number = 1; number <=999; number++) {
-                       sprintf (tempname, "%s.%03d", name, number);
-                       if (!get_named_bone(arm, tempname)) {
-                               BLI_strncpy (name, tempname, 32);
-                               return;
-                       }
-               }
-       }
+       return get_named_bone((bArmature *)arg, name) != NULL;
+}
+
+void unique_bone_name(bArmature *arm, char *name)
+{
+       BLI_uniquename_cb(bone_unique_check, (void *)arm, "Bone", '.', name, sizeof(((Bone *)NULL)->name));
 }
 
+
 #define MAXBONENAME 32
 /* helper call for armature_bone_rename */
 static void constraint_bone_name_fix(Object *ob, ListBase *conlist, char *oldname, char *newname)
@@ -5470,6 +5445,8 @@ void ED_armature_bone_rename(bArmature *arm, char *oldnamep, char *newnamep)
                
                /* do entire dbase - objects */
                for (ob= G.main->object.first; ob; ob= ob->id.next) {
+                       ModifierData *md;
+                       
                        /* we have the object using the armature */
                        if (arm==ob->data) {
                                Object *cob;
@@ -5513,11 +5490,22 @@ void ED_armature_bone_rename(bArmature *arm, char *oldnamep, char *newnamep)
                        }
                        
                        if (modifiers_usesArmature(ob, arm)) { 
-                               bDeformGroup *dg;
-                               /* bone name in defgroup */
-                               for (dg=ob->defbase.first; dg; dg=dg->next) {
-                                       if (!strcmp(dg->name, oldname))
-                                          BLI_strncpy(dg->name, newname, MAXBONENAME);
+                               bDeformGroup *dg= defgroup_find_name(ob, oldname);
+                               if(dg) {
+                                       BLI_strncpy(dg->name, newname, MAXBONENAME);
+                               }
+                       }
+                       
+                       /* fix modifiers that might be using this name */
+                       for (md= ob->modifiers.first; md; md= md->next) {
+                               if (md->type == eModifierType_Hook) {
+                                       HookModifierData *hmd = (HookModifierData *)md;
+                                       
+                                       /* uses armature, so may use the affected bone name */
+                                       if (hmd->object && (hmd->object->data == arm)) {
+                                               if (!strcmp(hmd->subtarget, oldname))
+                                                       BLI_strncpy(hmd->subtarget, newname, MAXBONENAME);
+                                       }
                                }
                        }
                        
@@ -5532,7 +5520,7 @@ void ED_armature_bone_rename(bArmature *arm, char *oldnamep, char *newnamep)
 }
 
 
-static int armature_flip_names_exec (bContext *C, wmOperator *op)
+static int armature_flip_names_exec (bContext *C, wmOperator *UNUSED(op))
 {
        Object *ob= CTX_data_edit_object(C);
        bArmature *arm;
@@ -5654,11 +5642,12 @@ void transform_armature_mirror_update(Object *obedit)
                                        eboflip->tail[1]= ebo->tail[1];
                                        eboflip->tail[2]= ebo->tail[2];
                                        eboflip->rad_tail= ebo->rad_tail;
+                                       eboflip->roll= -ebo->roll;
 
                                        /* Also move connected children, in case children's name aren't mirrored properly */
                                        for (children=arm->edbo->first; children; children=children->next) {
                                                if (children->parent == eboflip && children->flag & BONE_CONNECTED) {
-                                                       VECCOPY(children->head, eboflip->tail);
+                                                       copy_v3_v3(children->head, eboflip->tail);
                                                        children->rad_head = ebo->rad_tail;
                                                }
                                        }
@@ -5668,12 +5657,13 @@ void transform_armature_mirror_update(Object *obedit)
                                        eboflip->head[1]= ebo->head[1];
                                        eboflip->head[2]= ebo->head[2];
                                        eboflip->rad_head= ebo->rad_head;
+                                       eboflip->roll= -ebo->roll;
                                        
                                        /* Also move connected parent, in case parent's name isn't mirrored properly */
                                        if (eboflip->parent && eboflip->flag & BONE_CONNECTED)
                                        {
                                                EditBone *parent = eboflip->parent;
-                                               VECCOPY(parent->tail, eboflip->head);
+                                               copy_v3_v3(parent->tail, eboflip->head);
                                                parent->rad_tail = ebo->rad_head;
                                        }
                                }
@@ -5714,7 +5704,7 @@ EditBone * subdivideByAngle(Scene *scene, Object *obedit, ReebArc *arc, ReebNode
                
                parent = ED_armature_edit_bone_add(arm, "Bone");
                parent->flag |= BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL;
-               VECCOPY(parent->head, head->p);
+               copy_v3_v3(parent->head, head->p);
                
                root = parent;
                
@@ -5739,17 +5729,17 @@ EditBone * subdivideByAngle(Scene *scene, Object *obedit, ReebArc *arc, ReebNode
 
                        if (len1 > 0.0f && len2 > 0.0f && dot_v3v3(vec1, vec2) < angleLimit)
                        {
-                               VECCOPY(parent->tail, previous);
+                               copy_v3_v3(parent->tail, previous);
 
                                child = ED_armature_edit_bone_add(arm, "Bone");
-                               VECCOPY(child->head, parent->tail);
+                               copy_v3_v3(child->head, parent->tail);
                                child->parent = parent;
                                child->flag |= BONE_CONNECTED|BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL;
                                
                                parent = child; /* new child is next parent */
                        }
                }
-               VECCOPY(parent->tail, tail->p);
+               copy_v3_v3(parent->tail, tail->p);
                
                /* If the bone wasn't subdivided, delete it and return NULL
                 * to let subsequent subdivision methods do their thing. 
@@ -5868,7 +5858,7 @@ void generateSkeletonFromReebGraph(Scene *scene, ReebGraph *rg)
        obedit= scene->basact->object;
        
        /* Copy orientation from source */
-       VECCOPY(dst->loc, src->obmat[3]);
+       copy_v3_v3(dst->loc, src->obmat[3]);
        mat4_to_eul( dst->rot,src->obmat);
        mat4_to_size( dst->size,src->obmat);
        
@@ -5950,8 +5940,8 @@ void generateSkeletonFromReebGraph(Scene *scene, ReebGraph *rg)
                        bone = ED_armature_edit_bone_add(obedit->data, "Bone");
                        bone->flag |= BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL;
                        
-                       VECCOPY(bone->head, head->p);
-                       VECCOPY(bone->tail, tail->p);
+                       copy_v3_v3(bone->head, head->p);
+                       copy_v3_v3(bone->tail, tail->p);
                        
                        /* set first and last bone, since there's only one */
                        lastBone = bone;
@@ -6015,8 +6005,6 @@ void generateSkeletonFromReebGraph(Scene *scene, ReebGraph *rg)
        }
        
        BLI_ghash_free(arcBoneMap, NULL, NULL);
-       
-       BIF_undo_push("Generate Skeleton");
 }
 
 void generateSkeleton(Scene *scene)