batch remove .'s used with RNA_def_struct_ui_text
[blender.git] / source / blender / editors / armature / poseobject.c
index 9814a056b703d9f0ff787276967a39425bc13bb6..68d0d34c1c1d0e2e47b3a415a1887b53dae6f506 100644 (file)
@@ -21,6 +21,7 @@
  * All rights reserved.
  *
  * Contributor(s): Ton Roosendaal, Blender Foundation '05, full recode.
+ *                              Joshua Leung
  *
  * ***** END GPL LICENSE BLOCK *****
  * support for animation modes - Reevan McKay
@@ -32,7 +33,7 @@
 
 #include "MEM_guardedalloc.h"
 
-#include "BLI_arithb.h"
+#include "BLI_math.h"
 #include "BLI_blenlib.h"
 #include "BLI_dynstr.h"
 
@@ -50,6 +51,8 @@
 #include "DNA_view3d_types.h"
 #include "DNA_userdef_types.h"
 
+#include "BKE_anim.h"
+#include "BKE_animsys.h"
 #include "BKE_action.h"
 #include "BKE_armature.h"
 #include "BKE_blender.h"
 #include "armature_intern.h"
 
 /* ************* XXX *************** */
-static int movetolayer_short_buts() {return 1;}
 static int pupmenu() {return 0;}
-static void waitcursor() {};
 static void error() {};
 static void BIF_undo_push() {}
-static void countall() {}
-static void autokeyframe_pose_cb_func() {}
 /* ************* XXX *************** */
 
 /* This function is used to indicate that a bone is selected and needs keyframes inserted */
@@ -125,9 +124,8 @@ void ED_armature_enter_posemode(bContext *C, Base *base)
        
        switch (ob->type){
                case OB_ARMATURE:
-                       
-                       ob->flag |= OB_POSEMODE;
-                       base->flag= ob->flag;
+                       ob->restore_mode = ob->mode;
+                       ob->mode |= OB_MODE_POSE;
                        
                        WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_MODE_POSE, NULL);
                        
@@ -135,7 +133,8 @@ void ED_armature_enter_posemode(bContext *C, Base *base)
                default:
                        return;
        }
-       ED_view3d_exit_paint_modes(C);
+
+       //ED_object_toggle_modes(C, ob->mode);
 }
 
 void ED_armature_exit_posemode(bContext *C, Base *base)
@@ -143,8 +142,8 @@ void ED_armature_exit_posemode(bContext *C, Base *base)
        if(base) {
                Object *ob= base->object;
                
-               ob->flag &= ~OB_POSEMODE;
-               base->flag= ob->flag;
+               ob->restore_mode = ob->mode;
+               ob->mode &= ~OB_MODE_POSE;
                
                WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_MODE_OBJECT, NULL);
        }       
@@ -158,12 +157,12 @@ static short pose_has_protected_selected(Object *ob, short only_selected, short
        if (ob->proxy) {
                bPoseChannel *pchan;
                bArmature *arm= ob->data;
-               
+
                for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
                        if (pchan->bone && (pchan->bone->layer & arm->layer)) {
                                if (pchan->bone->layer & arm->layer_protected) {
-                                       if (only_selected && (pchan->bone->flag & BONE_ACTIVE));
-                                       else if (pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) 
+                                       if (only_selected && (pchan->bone == arm->act_bone));
+                                       else if (pchan->bone->flag & BONE_SELECTED || pchan->bone == arm->act_bone)
                                           break;
                                }
                        }
@@ -200,288 +199,177 @@ int ED_pose_channel_in_IK_chain(Object *ob, bPoseChannel *pchan)
 }
 
 /* ********************************************** */
+/* Motion Paths */
 
-/* For the object with pose/action: create path curves for selected bones 
- * This recalculates the WHOLE path within the pchan->pathsf and pchan->pathef range
+/* For the object with pose/action: update paths for those that have got them
+ * This should selectively update paths that exist...
+ *
+ * To be called from various tools that do incremental updates 
  */
-void pose_calculate_path(bContext *C, Scene *scene, Object *ob)
+void ED_pose_recalculate_paths(bContext *C, Scene *scene, Object *ob)
 {
-       bArmature *arm;
-       bPoseChannel *pchan;
-       Base *base;
-       float *fp;
-       int cfra;
-       int sfra, efra;
-       
-       if (ob==NULL || ob->pose==NULL)
-               return;
-       arm= ob->data;
-       
-       /* version patch for older files here (do_versions patch too complicated) */
-       if ((arm->pathsf == 0) || (arm->pathef == 0)) {
-               arm->pathsf = SFRA;
-               arm->pathef = EFRA;
-       }
-       if (arm->pathsize == 0) {
-               arm->pathsize = 1;
-       }
+       ListBase targets = {NULL, NULL};
        
-       /* set frame values */
-       cfra= CFRA;
-       sfra = arm->pathsf;
-       efra = arm->pathef;
-       if (efra <= sfra) {
-               error("Can't calculate paths when pathlen <= 0");
-               return;
-       }
+       /* set flag to force recalc, then grab the relevant bones to target */
+       ob->pose->avs.recalc |= ANIMVIZ_RECALC_PATHS;
+       animviz_get_object_motionpaths(ob, &targets);
        
-       waitcursor(1);
+       /* recalculate paths, then free */
+       animviz_calc_motionpaths(scene, &targets);
+       BLI_freelistN(&targets);
+}
+
+/* For the object with pose/action: create path curves for selected bones 
+ * This recalculates the WHOLE path within the pchan->pathsf and pchan->pathef range
+ */
+static int pose_calculate_paths_exec (bContext *C, wmOperator *op)
+{
+       ScrArea *sa= CTX_wm_area(C);
+       Scene *scene= CTX_data_scene(C);
+       Object *ob;
        
-       /* hack: for unsaved files, set OB_RECALC so that paths can get calculated */
-       if ((ob->recalc & OB_RECALC)==0) {
-               ob->recalc |= OB_RECALC;
-               ED_anim_object_flush_update(C, ob);
-       }
+       /* since this call may also be used from the buttons window, we need to check for where to get the object */
+       if (sa->spacetype == SPACE_BUTS) 
+               ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
        else
-               ED_anim_object_flush_update(C, ob);
-       
+               ob= CTX_data_active_object(C);
+               
+       if (ELEM(NULL, ob, ob->pose))
+               return OPERATOR_CANCELLED;
        
-       /* malloc the path blocks */
-       for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
-               if ((pchan->bone) && (pchan->bone->flag & BONE_SELECTED)) {
-                       if (arm->layer & pchan->bone->layer) {
-                               pchan->pathlen= efra-sfra+1;
-                               pchan->pathsf= sfra;
-                               pchan->pathef= efra+1;
-                               if (pchan->path)
-                                       MEM_freeN(pchan->path);
-                               pchan->path= MEM_callocN(3*pchan->pathlen*sizeof(float), "pchan path");
-                       }
-               }
+       /* set up path data for bones being calculated */
+       CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pose_bones) 
+       {
+               /* verify makes sure that the selected bone has a bone with the appropriate settings */
+               animviz_verify_motionpaths(scene, ob, pchan);
        }
+       CTX_DATA_END;
        
-       for (CFRA=sfra; CFRA<=efra; CFRA++) {
-               /* do all updates */
-               for (base= FIRSTBASE; base; base= base->next) {
-                       if (base->object->recalc) {
-                               int temp= base->object->recalc;
-                               object_handle_update(scene, base->object);
-                               base->object->recalc= temp;
-                       }
-               }
-               
-               for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
-                       if ((pchan->bone) && (pchan->bone->flag & BONE_SELECTED)) {
-                               if (arm->layer & pchan->bone->layer) {
-                                       if (pchan->path) {
-                                               fp= pchan->path+3*(CFRA-sfra);
-                                               
-                                               if (arm->pathflag & ARM_PATH_HEADS) { 
-                                                       VECCOPY(fp, pchan->pose_head);
-                                               }
-                                               else {
-                                                       VECCOPY(fp, pchan->pose_tail);
-                                               }
-                                               
-                                               Mat4MulVecfl(ob->obmat, fp);
-                                       }
-                               }
-                       }
-               }
-       }
+       /* calculate the bones that now have motionpaths... */
+       // TODO: only make for the selected bones?
+       ED_pose_recalculate_paths(C, scene, ob);
        
-       waitcursor(0);
+       /* notifiers for updates */
+       WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
        
-       CFRA= cfra;
+       return OPERATOR_FINISHED; 
 }
 
-/* For the object with pose/action: update paths for those that have got them
- * This should selectively update paths that exist...
- */
-void pose_recalculate_paths(bContext *C, Scene *scene, Object *ob)
+void POSE_OT_paths_calculate (wmOperatorType *ot)
 {
-       bArmature *arm;
-       bPoseChannel *pchan;
-       Base *base;
-       float *fp;
-       int cfra;
-       int sfra, efra;
-       
-       if (ob==NULL || ob->pose==NULL)
-               return;
-       arm= ob->data;
-       
-       /* set frame values */
-       cfra = CFRA;
-       sfra = efra = cfra; 
-       for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
-               if ((pchan->bone) && (arm->layer & pchan->bone->layer)) {
-                       if (pchan->path) {
-                               /* if the pathsf and pathef aren't initialised, abort! */
-                               if (ELEM(0, pchan->pathsf, pchan->pathef))      
-                                       return;
-                               
-                               /* try to increase area to do (only as much as needed) */
-                               sfra= MIN2(sfra, pchan->pathsf);
-                               efra= MAX2(efra, pchan->pathef);
-                       }
-               }
-       }
-       if (efra <= sfra) return;
-       
-       waitcursor(1);
-       
-       /* hack: for unsaved files, set OB_RECALC so that paths can get calculated */
-       if ((ob->recalc & OB_RECALC)==0) {
-               ob->recalc |= OB_RECALC;
-               ED_anim_object_flush_update(C, ob);
-       }
-       else
-               ED_anim_object_flush_update(C, ob);
-       
-       for (CFRA=sfra; CFRA<=efra; CFRA++) {
-               /* do all updates */
-               for (base= FIRSTBASE; base; base= base->next) {
-                       if (base->object->recalc) {
-                               int temp= base->object->recalc;
-                               object_handle_update(scene, base->object);
-                               base->object->recalc= temp;
-                       }
-               }
-               
-               for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
-                       if ((pchan->bone) && (arm->layer & pchan->bone->layer)) {
-                               if (pchan->path) {
-                                       /* only update if:
-                                        *      - in range of this pchan's existing path
-                                        *      - ... insert evil filtering/optimising conditions here...
-                                        */
-                                       if (IN_RANGE(CFRA, pchan->pathsf, pchan->pathef)) {
-                                               fp= pchan->path+3*(CFRA-sfra);
-                                               
-                                               if (arm->pathflag & ARM_PATH_HEADS) { 
-                                                       VECCOPY(fp, pchan->pose_head);
-                                               }
-                                               else {
-                                                       VECCOPY(fp, pchan->pose_tail);
-                                               }
-                                               
-                                               Mat4MulVecfl(ob->obmat, fp);
-                                       }
-                               }
-                       }
-               }
-       }
+       /* identifiers */
+       ot->name= "Calculate Bone Paths";
+       ot->idname= "POSE_OT_paths_calculate";
+       ot->description= "Calculate paths for the selected bones";
        
-       waitcursor(0);
+       /* api callbacks */
+       ot->exec= pose_calculate_paths_exec;
+       ot->poll= ED_operator_posemode;
        
-       CFRA= cfra;
-       ob->pose->flag &= ~POSE_RECALCPATHS;
+       /* flags */
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 }
 
+/* --------- */
+
 /* for the object with pose/action: clear path curves for selected bones only */
-void pose_clear_paths(Object *ob)
+void ED_pose_clear_paths(Object *ob)
 {
        bPoseChannel *pchan;
        
-       if (ob==NULL || ob->pose==NULL)
+       if ELEM(NULL, ob, ob->pose)
                return;
        
-       /* free the path blocks */
+       /* free the motionpath blocks */
        for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
                if ((pchan->bone) && (pchan->bone->flag & BONE_SELECTED)) {
-                       if (pchan->path) {
-                               MEM_freeN(pchan->path);
-                               pchan->path= NULL;
+                       if (pchan->mpath) {
+                               animviz_free_motionpath(pchan->mpath);
+                               pchan->mpath= NULL;
                        }
                }
        }
-       
 }
 
-
-// XXX this function is to be removed when the other stuff is recoded
-void pose_select_constraint_target(Scene *scene)
+/* operator callback for this */
+static int pose_clear_paths_exec (bContext *C, wmOperator *op)
 {
-       Object *obedit= scene->obedit; // XXX context
-       Object *ob= OBACT;
-       bArmature *arm= ob->data;
-       bPoseChannel *pchan;
-       bConstraint *con;
+       ScrArea *sa= CTX_wm_area(C);
+       Object *ob;
        
-       /* paranoia checks */
-       if (!ob && !ob->pose) return;
-       if (ob==obedit || (ob->flag & OB_POSEMODE)==0) return;
-       
-       for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
-               if (arm->layer & pchan->bone->layer) {
-                       if (pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
-                               for (con= pchan->constraints.first; con; con= con->next) {
-                                       bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
-                                       ListBase targets = {NULL, NULL};
-                                       bConstraintTarget *ct;
-                                       
-                                       if (cti && cti->get_constraint_targets) {
-                                               cti->get_constraint_targets(con, &targets);
-                                               
-                                               for (ct= targets.first; ct; ct= ct->next) {
-                                                       if ((ct->tar == ob) && (ct->subtarget[0])) {
-                                                               bPoseChannel *pchanc= get_pose_channel(ob->pose, ct->subtarget);
-                                                               if(pchanc)
-                                                                       pchanc->bone->flag |= BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL;
-                                                       }
-                                               }
-                                               
-                                               if (cti->flush_constraint_targets)
-                                                       cti->flush_constraint_targets(con, &targets, 1);
-                                       }
-                               }
-                       }
-               }
-       }
+       /* since this call may also be used from the buttons window, we need to check for where to get the object */
+       if (sa->spacetype == SPACE_BUTS) 
+               ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
+       else
+               ob= CTX_data_active_object(C);
+               
+       /* only continue if there's an object */
+       if ELEM(NULL, ob, ob->pose)
+               return OPERATOR_CANCELLED;
+       
+       /* use the backend function for this */
+       ED_pose_clear_paths(ob);
+       
+       /* notifiers for updates */
+       WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
        
-       BIF_undo_push("Select constraint target");
+       return OPERATOR_FINISHED; 
+}
 
+void POSE_OT_paths_clear (wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name= "Clear Bone Paths";
+       ot->idname= "POSE_OT_paths_clear";
+       ot->description= "Clear path caches for selected bones";
+       
+       /* api callbacks */
+       ot->exec= pose_clear_paths_exec;
+       ot->poll= ED_operator_posemode;
+       
+       /* flags */
+       ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
 }
 
+/* ******************* Select Constraint Target Operator ************* */
+
 static int pose_select_constraint_target_exec(bContext *C, wmOperator *op)
 {
        Object *ob= CTX_data_active_object(C);
        bArmature *arm= ob->data;
-       bPoseChannel *pchan;
        bConstraint *con;
        int found= 0;
        
-       for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
-               if (arm->layer & pchan->bone->layer) {
-                       if (pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
-                               for (con= pchan->constraints.first; con; con= con->next) {
-                                       bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
-                                       ListBase targets = {NULL, NULL};
-                                       bConstraintTarget *ct;
+       CTX_DATA_BEGIN(C, bPoseChannel *, pchan, visible_pose_bones) 
+       {
+               if ((pchan->bone->flag & BONE_SELECTED) || (pchan->bone == arm->act_bone)) {
+                       for (con= pchan->constraints.first; con; con= con->next) {
+                               bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
+                               ListBase targets = {NULL, NULL};
+                               bConstraintTarget *ct;
+                               
+                               if (cti && cti->get_constraint_targets) {
+                                       cti->get_constraint_targets(con, &targets);
                                        
-                                       if (cti && cti->get_constraint_targets) {
-                                               cti->get_constraint_targets(con, &targets);
-                                               
-                                               for (ct= targets.first; ct; ct= ct->next) {
-                                                       if ((ct->tar == ob) && (ct->subtarget[0])) {
-                                                               bPoseChannel *pchanc= get_pose_channel(ob->pose, ct->subtarget);
-                                                               if(pchanc) {
-                                                                       pchanc->bone->flag |= BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL;
-                                                                       found= 1;
-                                                               }
+                                       for (ct= targets.first; ct; ct= ct->next) {
+                                               if ((ct->tar == ob) && (ct->subtarget[0])) {
+                                                       bPoseChannel *pchanc= get_pose_channel(ob->pose, ct->subtarget);
+                                                       if((pchanc) && !(pchanc->bone->flag & BONE_UNSELECTABLE)) {
+                                                               pchanc->bone->flag |= BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL;
+                                                               found= 1;
                                                        }
                                                }
-                                               
-                                               if (cti->flush_constraint_targets)
-                                                       cti->flush_constraint_targets(con, &targets, 1);
                                        }
+                                       
+                                       if (cti->flush_constraint_targets)
+                                               cti->flush_constraint_targets(con, &targets, 1);
                                }
                        }
                }
        }
+       CTX_DATA_END;
 
-       if(!found)
+       if (!found)
                return OPERATOR_CANCELLED;
 
        WM_event_add_notifier(C, NC_OBJECT|ND_BONE_SELECT, ob);
@@ -509,42 +397,39 @@ static int pose_select_hierarchy_exec(bContext *C, wmOperator *op)
 {
        Object *ob= CTX_data_active_object(C);
        bArmature *arm= ob->data;
-       bPoseChannel *pchan;
        Bone *curbone, *pabone, *chbone;
        int direction = RNA_enum_get(op->ptr, "direction");
        int add_to_sel = RNA_boolean_get(op->ptr, "extend");
        int found= 0;
        
-       for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
+       CTX_DATA_BEGIN(C, bPoseChannel *, pchan, visible_pose_bones) 
+       {
                curbone= pchan->bone;
                
-               if (arm->layer & curbone->layer) {
-                       if (curbone->flag & (BONE_ACTIVE)) {
+               if ((curbone->flag & BONE_UNSELECTABLE)==0) {
+                       if (curbone == arm->act_bone) {
                                if (direction == BONE_SELECT_PARENT) {
-                               
                                        if (pchan->parent == NULL) continue;
                                        else pabone= pchan->parent->bone;
                                        
                                        if ((arm->layer & pabone->layer) && !(pabone->flag & BONE_HIDDEN_P)) {
-                                               
                                                if (!add_to_sel) curbone->flag &= ~BONE_SELECTED;
-                                               curbone->flag &= ~BONE_ACTIVE;
-                                               pabone->flag |= (BONE_ACTIVE|BONE_SELECTED);
-
+                                               pabone->flag |= BONE_SELECTED;
+                                               arm->act_bone= pabone;
+                                               
                                                found= 1;
                                                break;
                                        }
-                               } else { // BONE_SELECT_CHILD
-                               
+                               } 
+                               else { /* direction == BONE_SELECT_CHILD */
                                        if (pchan->child == NULL) continue;
                                        else chbone = pchan->child->bone;
                                        
                                        if ((arm->layer & chbone->layer) && !(chbone->flag & BONE_HIDDEN_P)) {
-                                       
                                                if (!add_to_sel) curbone->flag &= ~BONE_SELECTED;
-                                               curbone->flag &= ~BONE_ACTIVE;
-                                               chbone->flag |= (BONE_ACTIVE|BONE_SELECTED);
-
+                                               chbone->flag |= BONE_SELECTED;
+                                               arm->act_bone= chbone;
+                                               
                                                found= 1;
                                                break;
                                        }
@@ -552,8 +437,9 @@ static int pose_select_hierarchy_exec(bContext *C, wmOperator *op)
                        }
                }
        }
+       CTX_DATA_END;
 
-       if(!found)
+       if (found == 0)
                return OPERATOR_CANCELLED;
 
        WM_event_add_notifier(C, NC_OBJECT|ND_BONE_SELECT, ob);
@@ -581,12 +467,171 @@ void POSE_OT_select_hierarchy(wmOperatorType *ot)
        ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
        
        /* props */
-       RNA_def_enum(ot->srna, "direction", direction_items,
-                                BONE_SELECT_PARENT, "Direction", "");
+       ot->prop= RNA_def_enum(ot->srna, "direction", direction_items, BONE_SELECT_PARENT, "Direction", "");
        RNA_def_boolean(ot->srna, "extend", 0, "Add to Selection", "");
        
 }
 
+/* ******************* select grouped operator ************* */
+
+static short pose_select_same_group (bContext *C, Object *ob, short extend)
+{
+       bArmature *arm= (ob)? ob->data : NULL;
+       bPose *pose= (ob)? ob->pose : NULL;
+       char *group_flags;
+       int numGroups = 0;
+       short changed=0, tagged=0;
+       
+       /* sanity checks */
+       if (ELEM3(NULL, ob, pose, arm))
+               return 0;
+               
+       /* count the number of groups */
+       numGroups= BLI_countlist(&pose->agroups);
+       if (numGroups == 0)
+               return 0;
+               
+       /* alloc a small array to keep track of the groups to use 
+        *      - each cell stores on/off state for whether group should be used
+        *      - size is numGroups + 1, since index=0 is used for no-group
+        */
+       group_flags= MEM_callocN(numGroups+1, "pose_select_same_group");
+       
+       CTX_DATA_BEGIN(C, bPoseChannel *, pchan, visible_pose_bones) 
+       {
+               /* keep track of group as group to use later? */
+               if ((pchan->bone->flag & BONE_SELECTED) || (pchan->bone == arm->act_bone)) {
+                       group_flags[pchan->agrp_index] = 1;
+                       tagged= 1;
+               }
+               
+               /* deselect all bones before selecting new ones? */
+               if ((extend == 0) && (pchan->bone->flag & BONE_UNSELECTABLE)==0)
+                       pchan->bone->flag &= ~BONE_SELECTED;
+       }
+       CTX_DATA_END;
+       
+       /* small optimisation: only loop through bones a second time if there are any groups tagged */
+       if (tagged) {
+               /* only if group matches (and is not selected or current bone) */
+               CTX_DATA_BEGIN(C, bPoseChannel *, pchan, visible_pose_bones) 
+               {
+                       if ((pchan->bone->flag & BONE_UNSELECTABLE)==0) {
+                               /* check if the group used by this bone is counted */
+                               if (group_flags[pchan->agrp_index]) {
+                                       pchan->bone->flag |= BONE_SELECTED;
+                                       changed= 1;
+                               }
+                       }
+               }
+               CTX_DATA_END;
+       }
+       
+       /* free temp info */
+       MEM_freeN(group_flags);
+       
+       return changed;
+}
+
+static short pose_select_same_layer (bContext *C, Object *ob, short extend)
+{
+       bPose *pose= (ob)? ob->pose : NULL;
+       bArmature *arm= (ob)? ob->data : NULL;
+       short changed= 0;
+       int layers= 0;
+       
+       if (ELEM3(NULL, ob, pose, arm))
+               return 0;
+       
+       /* figure out what bones are selected */
+       CTX_DATA_BEGIN(C, bPoseChannel *, pchan, visible_pose_bones) 
+       {
+               /* keep track of layers to use later? */
+               if ((pchan->bone->flag & BONE_SELECTED) || (pchan->bone == arm->act_bone))
+                       layers |= pchan->bone->layer;
+                       
+               /* deselect all bones before selecting new ones? */
+               if ((extend == 0) && (pchan->bone->flag & BONE_UNSELECTABLE)==0)
+                       pchan->bone->flag &= ~BONE_SELECTED;
+       }
+       CTX_DATA_END;
+       if (layers == 0) 
+               return 0;
+               
+       /* select bones that are on same layers as layers flag */
+       CTX_DATA_BEGIN(C, bPoseChannel *, pchan, visible_pose_bones) 
+       {
+               /* if bone is on a suitable layer, and the bone can have its selection changed, select it */
+               if ((layers & pchan->bone->layer) && (pchan->bone->flag & BONE_UNSELECTABLE)==0) {
+                       pchan->bone->flag |= BONE_SELECTED;
+                       changed= 1;
+               }
+       }
+       CTX_DATA_END;
+       
+       return changed;
+}
+
+
+static int pose_select_grouped_exec (bContext *C, wmOperator *op)
+{
+       Object *ob= CTX_data_active_object(C);
+       short extend= RNA_boolean_get(op->ptr, "extend");
+       short changed = 0;
+       
+       /* sanity check */
+       if (ELEM(NULL, ob, ob->pose))
+               return OPERATOR_CANCELLED;
+               
+       /* selection types 
+        * NOTE: for the order of these, see the enum in POSE_OT_select_grouped()
+        */
+       switch (RNA_enum_get(op->ptr, "type")) {
+               case 1: /* group */
+                       changed= pose_select_same_group(C, ob, extend);
+                       break;
+               default: /* layer */
+                       changed= pose_select_same_layer(C, ob, extend);
+                       break;
+       }
+       
+       /* notifiers for updates */
+       WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
+       
+       /* report done status */
+       if (changed)
+               return OPERATOR_FINISHED;
+       else
+               return OPERATOR_CANCELLED;
+}
+
+void POSE_OT_select_grouped (wmOperatorType *ot)
+{
+       static EnumPropertyItem prop_select_grouped_types[] = {
+               {0, "LAYER", 0, "Layer", "Shared layers"},
+               {1, "GROUP", 0, "Group", "Shared group"},
+               {0, NULL, 0, NULL, NULL}
+       };
+
+       /* identifiers */
+       ot->name= "Select Grouped";
+       ot->description = "Select all visible bones grouped by various properties";
+       ot->idname= "POSE_OT_select_grouped";
+       
+       /* api callbacks */
+       ot->invoke= WM_menu_invoke;
+       ot->exec= pose_select_grouped_exec;
+       ot->poll= ED_operator_posemode;
+       
+       /* flags */
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+       
+       /* properties */
+       RNA_def_boolean(ot->srna, "extend", FALSE, "Extend", "Extend selection instead of deselecting everything first.");
+       ot->prop= RNA_def_enum(ot->srna, "type", prop_select_grouped_types, 0, "Type", "");
+}
+
+/* ********************************************** */
 
 void pose_copy_menu(Scene *scene)
 {
@@ -599,13 +644,9 @@ void pose_copy_menu(Scene *scene)
        
        /* paranoia checks */
        if (ELEM(NULL, ob, ob->pose)) return;
-       if ((ob==obedit) || (ob->flag & OB_POSEMODE)==0) return;
+       if ((ob==obedit) || (ob->mode & OB_MODE_POSE)==0) return;
        
-       /* find active */
-       for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
-               if (pchan->bone->flag & BONE_ACTIVE) 
-                       break;
-       }
+       pchan= get_active_posechannel(ob);
        
        if (pchan==NULL) return;
        pchanact= pchan;
@@ -643,6 +684,7 @@ void pose_copy_menu(Scene *scene)
                                                break;
                                        case 2: /* Local Rotation */
                                                QUATCOPY(pchan->quat, pchanact->quat);
+                                               VECCOPY(pchan->eul, pchanact->eul);
                                                break;
                                        case 3: /* Local Size */
                                                VECCOPY(pchan->size, pchanact->size);
@@ -681,6 +723,8 @@ void pose_copy_menu(Scene *scene)
                                                VECCOPY(pchan->limitmax, pchanact->limitmax);
                                                VECCOPY(pchan->stiffness, pchanact->stiffness);
                                                pchan->ikstretch= pchanact->ikstretch;
+                                               pchan->ikrotweight= pchanact->ikrotweight;
+                                               pchan->iklinweight= pchanact->iklinweight;
                                        }
                                                break;
                                        case 8: /* Custom Bone Shape */
@@ -691,11 +735,21 @@ void pose_copy_menu(Scene *scene)
                                                break;
                                        case 10: /* Visual Rotation */
                                        {
-                                               float delta_mat[4][4], quat[4];
+                                               float delta_mat[4][4];
                                                
                                                armature_mat_pose_to_bone(pchan, pchanact->pose_mat, delta_mat);
-                                               Mat4ToQuat(delta_mat, quat);
-                                               QUATCOPY(pchan->quat, quat);
+                                               
+                                               if (pchan->rotmode == ROT_MODE_AXISANGLE) {
+                                                       float tmp_quat[4];
+                                                       
+                                                       /* need to convert to quat first (in temp var)... */
+                                                       mat4_to_quat( tmp_quat,delta_mat);
+                                                       quat_to_axis_angle( pchan->rotAxis, &pchan->rotAngle,tmp_quat);
+                                               }
+                                               else if (pchan->rotmode == ROT_MODE_QUAT)
+                                                       mat4_to_quat( pchan->quat,delta_mat);
+                                               else
+                                                       mat4_to_eulO( pchan->eul, pchan->rotmode,delta_mat);
                                        }
                                                break;
                                        case 11: /* Visual Size */
@@ -703,7 +757,7 @@ void pose_copy_menu(Scene *scene)
                                                float delta_mat[4][4], size[4];
                                                
                                                armature_mat_pose_to_bone(pchan, pchanact->pose_mat, delta_mat);
-                                               Mat4ToSize(delta_mat, size);
+                                               mat4_to_size( size,delta_mat);
                                                VECCOPY(pchan->size, size);
                                        }
                                }
@@ -772,7 +826,7 @@ void pose_copy_menu(Scene *scene)
                        ob->pose->flag |= POSE_RECALC;
        }
        
-       DAG_object_flush_update(scene, ob, OB_RECALC_DATA);     // and all its relations
+       DAG_id_flush_update(&ob->id, OB_RECALC_DATA);   // and all its relations
        
        BIF_undo_push("Copy Pose Attributes");
        
@@ -822,7 +876,7 @@ void POSE_OT_copy (wmOperatorType *ot)
        /* identifiers */
        ot->name= "Copy Pose";
        ot->idname= "POSE_OT_copy";
-       ot->description= "Copies the current pose of the selected bones to copy/paste buffer.";
+       ot->description= "Copies the current pose of the selected bones to copy/paste buffer";
        
        /* api callbacks */
        ot->exec= pose_copy_exec;
@@ -834,6 +888,11 @@ void POSE_OT_copy (wmOperatorType *ot)
 
 /* ---- */
 
+/* Pointers to the builtin KeyingSets that we want to use */
+static KeyingSet *posePaste_ks_locrotscale = NULL;             /* the only keyingset we'll need */
+
+/* ---- */
+
 static int pose_paste_exec (bContext *C, wmOperator *op)
 {
        Scene *scene= CTX_data_scene(C);
@@ -842,6 +901,13 @@ static int pose_paste_exec (bContext *C, wmOperator *op)
        char name[32];
        int flip= RNA_boolean_get(op->ptr, "flipped");
        
+       bCommonKeySrc cks;
+       ListBase dsources = {&cks, &cks};
+       
+       /* init common-key-source for use by KeyingSets */
+       memset(&cks, 0, sizeof(bCommonKeySrc));
+       cks.id= &ob->id;
+       
        /* sanity checks */
        if ELEM(NULL, ob, ob->pose)
                return OPERATOR_CANCELLED;
@@ -873,20 +939,33 @@ static int pose_paste_exec (bContext *C, wmOperator *op)
                                /* check if rotation modes are compatible (i.e. do they need any conversions) */
                                if (pchan->rotmode == chan->rotmode) {
                                        /* copy the type of rotation in use */
-                                       if (pchan->rotmode) {
+                                       if (pchan->rotmode > 0) {
                                                VECCOPY(pchan->eul, chan->eul);
                                        }
                                        else {
                                                QUATCOPY(pchan->quat, chan->quat);
                                        }
                                }
-                               else if (pchan->rotmode) {
-                                       /* quat to euler */
-                                       QuatToEul(chan->quat, pchan->eul);
+                               else if (pchan->rotmode > 0) {
+                                       /* quat/axis-angle to euler */
+                                       if (chan->rotmode == ROT_MODE_AXISANGLE)
+                                               axis_angle_to_eulO( pchan->eul, pchan->rotmode,chan->rotAxis, chan->rotAngle);
+                                       else
+                                               quat_to_eulO( pchan->eul, pchan->rotmode,chan->quat);
+                               }
+                               else if (pchan->rotmode == ROT_MODE_AXISANGLE) {
+                                       /* quat/euler to axis angle */
+                                       if (chan->rotmode > 0)
+                                               eulO_to_axis_angle( pchan->rotAxis, &pchan->rotAngle,chan->eul, chan->rotmode);
+                                       else    
+                                               quat_to_axis_angle( pchan->rotAxis, &pchan->rotAngle,chan->quat);
                                }
                                else {
-                                       /* euler to quat */
-                                       EulToQuat(chan->eul, pchan->quat);
+                                       /* euler/axis-angle to quat */
+                                       if (chan->rotmode > 0)
+                                               eulO_to_quat( pchan->quat,chan->eul, chan->rotmode);
+                                       else
+                                               axis_angle_to_quat(pchan->quat, chan->rotAxis, pchan->rotAngle);
                                }
                                
                                /* paste flipped pose? */
@@ -894,42 +973,48 @@ static int pose_paste_exec (bContext *C, wmOperator *op)
                                        pchan->loc[0]*= -1;
                                        
                                        /* has to be done as eulers... */
-                                       if (pchan->rotmode) {
+                                       if (pchan->rotmode > 0) {
                                                pchan->eul[1] *= -1;
                                                pchan->eul[2] *= -1;
                                        }
+                                       else if (pchan->rotmode == ROT_MODE_AXISANGLE) {
+                                               float eul[3];
+                                               
+                                               axis_angle_to_eulO( eul, EULER_ORDER_DEFAULT,pchan->rotAxis, pchan->rotAngle);
+                                               eul[1]*= -1;
+                                               eul[2]*= -1;
+                                               eulO_to_axis_angle( pchan->rotAxis, &pchan->rotAngle,eul, EULER_ORDER_DEFAULT);
+                                               
+                                               // experimental method (uncomment to test):
+#if 0
+                                               /* experimental method: just flip the orientation of the axis on x/y axes */
+                                               pchan->quat[1] *= -1;
+                                               pchan->quat[2] *= -1;
+#endif
+                                       }
                                        else {
                                                float eul[3];
                                                
-                                               QuatToEul(pchan->quat, eul);
+                                               quat_to_eul( eul,pchan->quat);
                                                eul[1]*= -1;
                                                eul[2]*= -1;
-                                               EulToQuat(eul, pchan->quat);
+                                               eul_to_quat( pchan->quat,eul);
                                        }
                                }
                                
-#if 0 // XXX old animation system
-                               if (autokeyframe_cfra_can_key(ob)) {
-                                       ID *id= &ob->id;
+                               if (autokeyframe_cfra_can_key(scene, &ob->id)) {
+                                       /* Set keys on pose
+                                        *      - KeyingSet to use depends on rotation mode 
+                                        *      (but that's handled by the templates code)  
+                                        */
+                                       // TODO: for getting the KeyingSet used, we should really check which channels were affected
+                                       if (posePaste_ks_locrotscale == NULL)
+                                               posePaste_ks_locrotscale= ANIM_builtin_keyingset_get_named(NULL, "LocRotScale");
                                        
-                                       /* Set keys on pose */
-                                       // TODO: make these use keyingsets....
-                                       if (chan->flag & POSE_ROT) {
-                                               insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_X, 0);
-                                               insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_Y, 0);
-                                               insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_Z, 0);
-                                               insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_W, 0);
-                                       }
-                                       if (chan->flag & POSE_SIZE) {
-                                               insertkey(id, ID_PO, pchan->name, NULL, AC_SIZE_X, 0);
-                                               insertkey(id, ID_PO, pchan->name, NULL, AC_SIZE_Y, 0);
-                                               insertkey(id, ID_PO, pchan->name, NULL, AC_SIZE_Z, 0);
-                                       }
-                                       if (chan->flag & POSE_LOC) {
-                                               insertkey(id, ID_PO, pchan->name, NULL, AC_LOC_X, 0);
-                                               insertkey(id, ID_PO, pchan->name, NULL, AC_LOC_Y, 0);
-                                               insertkey(id, ID_PO, pchan->name, NULL, AC_LOC_Z, 0);
-                                       }
+                                       /* init cks for this PoseChannel, then use the relative KeyingSets to keyframe it */
+                                       cks.pchan= pchan;
+                                       
+                                       modify_keyframes(scene, &dsources, NULL, posePaste_ks_locrotscale, MODIFYKEY_MODE_INSERT, (float)CFRA);
                                        
                                        /* clear any unkeyed tags */
                                        if (chan->bone)
@@ -940,13 +1025,12 @@ static int pose_paste_exec (bContext *C, wmOperator *op)
                                        if (chan->bone)
                                                chan->bone->flag |= BONE_UNKEYED;
                                }
-#endif // XXX old animation system
                        }
                }
        }
 
        /* Update event for pose and deformation children */
-       DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
+       DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
        
        if (IS_AUTOKEY_ON(scene)) {
 // XXX         remake_action_ipos(ob->action);
@@ -958,7 +1042,8 @@ static int pose_paste_exec (bContext *C, wmOperator *op)
        }
        
        /* notifiers for updates */
-       WM_event_add_notifier(C, NC_OBJECT|ND_POSE|ND_TRANSFORM, ob);
+       WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
+       WM_event_add_notifier(C, NC_ANIMATION|ND_KEYFRAME_EDIT, NULL); // XXX not really needed, but here for completeness...
 
        return OPERATOR_FINISHED;
 }
@@ -968,7 +1053,7 @@ void POSE_OT_paste (wmOperatorType *ot)
        /* identifiers */
        ot->name= "Paste Pose";
        ot->idname= "POSE_OT_paste";
-       ot->description= "Pastes the stored pose on to the current pose.";
+       ot->description= "Pastes the stored pose on to the current pose";
        
        /* api callbacks */
        ot->exec= pose_paste_exec;
@@ -983,31 +1068,6 @@ void POSE_OT_paste (wmOperatorType *ot)
 
 /* ********************************************** */
 
-/* context weightpaint and deformer in posemode */
-void pose_adds_vgroups(Scene *scene, Object *meshobj, int heatweights)
-{
-// XXX extern VPaint Gwp;         /* from vpaint */
-       Object *poseobj= modifiers_isDeformedByArmature(meshobj);
-
-       if(poseobj==NULL || (poseobj->flag & OB_POSEMODE)==0) {
-               error("The active object must have a deforming armature in pose mode");
-               return;
-       }
-
-// XXX add_verts_to_dgroups(meshobj, poseobj, heatweights, (Gwp.flag & VP_MIRROR_X));
-
-       if(heatweights)
-               BIF_undo_push("Apply Bone Heat Weights to Vertex Groups");
-       else
-               BIF_undo_push("Apply Bone Envelopes to Vertex Groups");
-
-       
-       // and all its relations
-       DAG_object_flush_update(scene, meshobj, OB_RECALC_DATA);
-}
-
-/* ********************************************** */
-
 
 static int pose_group_add_exec (bContext *C, wmOperator *op)
 {
@@ -1038,7 +1098,7 @@ void POSE_OT_group_add (wmOperatorType *ot)
        /* identifiers */
        ot->name= "Add Bone Group";
        ot->idname= "POSE_OT_group_add";
-       ot->description= "Add a new bone group.";
+       ot->description= "Add a new bone group";
        
        /* api callbacks */
        ot->exec= pose_group_add_exec;
@@ -1078,7 +1138,7 @@ void POSE_OT_group_remove (wmOperatorType *ot)
        /* identifiers */
        ot->name= "Remove Bone Group";
        ot->idname= "POSE_OT_group_remove";
-       ot->description= "Removes the active bone group.";
+       ot->description= "Removes the active bone group";
        
        /* api callbacks */
        ot->exec= pose_group_remove_exec;
@@ -1150,7 +1210,6 @@ static int pose_group_assign_exec (bContext *C, wmOperator *op)
        Object *ob;
        bArmature *arm;
        bPose *pose;
-       bPoseChannel *pchan;
        short done= 0;
        
        /* since this call may also be used from the buttons window, we need to check for where to get the object */
@@ -1174,18 +1233,12 @@ static int pose_group_assign_exec (bContext *C, wmOperator *op)
        
        /* add selected bones to group then */
        // NOTE: unfortunately, we cannot use the context-iterators here, since they might not be defined...
-       // CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pchans) 
-       for (pchan= pose->chanbase.first; pchan; pchan= pchan->next) {
-               /* ensure that PoseChannel is on visible layer and is not hidden in PoseMode */
-               // NOTE: sync this view3d_context() in space_view3d.c
-               if ((pchan->bone) && (arm->layer & pchan->bone->layer) && !(pchan->bone->flag & BONE_HIDDEN_P)) {
-                       if (pchan->bone->flag & (BONE_SELECTED|BONE_ACTIVE)) {
-                               pchan->agrp_index= pose->active_group;
-                               done= 1;
-                       }
-               }
+       CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pose_bones) {
+               pchan->agrp_index= pose->active_group;
+               done= 1;
        }
-       
+       CTX_DATA_END;
+
        /* notifiers for updates */
        WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
        
@@ -1201,7 +1254,7 @@ void POSE_OT_group_assign (wmOperatorType *ot)
        /* identifiers */
        ot->name= "Add Selected to Bone Group";
        ot->idname= "POSE_OT_group_assign";
-       ot->description= "Add selected bones to the chosen bone group.";
+       ot->description= "Add selected bones to the chosen bone group";
        
        /* api callbacks */
        ot->invoke= pose_groups_menu_invoke;
@@ -1237,14 +1290,14 @@ static int pose_group_unassign_exec (bContext *C, wmOperator *op)
        pose= ob->pose;
        arm= ob->data;
        
-       /* add selected bones to ungroup then */
+       /* find selected bones to remove from all bone groups */
        // NOTE: unfortunately, we cannot use the context-iterators here, since they might not be defined...
-       // CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pchans) 
+       // CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pose_bones) 
        for (pchan= pose->chanbase.first; pchan; pchan= pchan->next) {
                /* ensure that PoseChannel is on visible layer and is not hidden in PoseMode */
                // NOTE: sync this view3d_context() in space_view3d.c
                if ((pchan->bone) && (arm->layer & pchan->bone->layer) && !(pchan->bone->flag & BONE_HIDDEN_P)) {
-                       if (pchan->bone->flag & (BONE_SELECTED|BONE_ACTIVE)) {
+                       if ((pchan->bone->flag & BONE_SELECTED) || (pchan->bone == arm->act_bone)) {
                                if (pchan->agrp_index) {
                                        pchan->agrp_index= 0;
                                        done= 1;
@@ -1268,7 +1321,7 @@ void POSE_OT_group_unassign (wmOperatorType *ot)
        /* identifiers */
        ot->name= "Remove Selected from Bone Groups";
        ot->idname= "POSE_OT_group_unassign";
-       ot->description= "Add selected bones from all bone groups";
+       ot->description= "Remove selected bones from all bone groups";
        
        /* api callbacks */
        ot->exec= pose_group_unassign_exec;
@@ -1278,202 +1331,110 @@ void POSE_OT_group_unassign (wmOperatorType *ot)
        ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
 }
 
-/* ----------------- */
+/* ********************************************** */
 
-static int pose_groupOps_menu_invoke (bContext *C, wmOperator *op, wmEvent *evt)
+static int pose_flip_names_exec (bContext *C, wmOperator *op)
 {
        Object *ob= CTX_data_active_object(C);
-       uiPopupMenu *pup= uiPupMenuBegin(C, op->type->name, 0);
-       uiLayout *layout= uiPupMenuLayout(pup);
+       bArmature *arm;
+       char newname[32];
        
-       /* sanity check - must have object with pose */
-       if ELEM(NULL, ob, ob->pose)
+       /* paranoia checks */
+       if (ELEM(NULL, ob, ob->pose)) 
                return OPERATOR_CANCELLED;
+       arm= ob->data;
        
-       /* get mode of action */
-       if (CTX_DATA_COUNT(C, selected_pchans)) {
-               /* if selected bone(s), include options to add/remove to active group */
-               uiItemO(layout, "Add Selected to Active Group", 0, "POSE_OT_group_assign");
-               
-               uiItemS(layout);
-               
-               uiItemO(layout, "Remove Selected from All Groups", 0, "POSE_OT_group_unassign");
-               uiItemO(layout, "Remove Active Group", 0, "POSE_OT_group_remove");
-       }
-       else {
-               /* no selected bones - so just options for groups management */
-               uiItemO(layout, "Add New Group", 0, "POSE_OT_group_add");
-               uiItemO(layout, "Remove Active Group", 0, "POSE_OT_group_remove");
+       /* loop through selected bones, auto-naming them */
+       CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pose_bones)
+       {
+               BLI_strncpy(newname, pchan->name, sizeof(newname));
+               bone_flip_name(newname, 1);     // 1 = do strip off number extensions
+               ED_armature_bone_rename(arm, pchan->name, newname);
        }
-               
-       return OPERATOR_CANCELLED;
+       CTX_DATA_END;
+       
+       /* since we renamed stuff... */
+       DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
+
+       /* note, notifier might evolve */
+       WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
+       
+       return OPERATOR_FINISHED;
 }
 
-void POSE_OT_groups_menu (wmOperatorType *ot)
+void POSE_OT_flip_names (wmOperatorType *ot)
 {
        /* identifiers */
-       ot->name= "Bone Group Tools";
-       ot->idname= "POSE_OT_groups_menu";
-       ot->description= "Menu displaying available tools for Bone Groups.";
+       ot->name= "Flip Names";
+       ot->idname= "POSE_OT_flip_names";
+       ot->description= "Flips (and corrects) the names of selected bones";
        
-       /* api callbacks (only invoke needed) */
-       ot->invoke= pose_groupOps_menu_invoke;
+       /* api callbacks */
+       ot->exec= pose_flip_names_exec;
        ot->poll= ED_operator_posemode;
        
        /* flags */
-       ot->flag= OPTYPE_REGISTER;
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 }
 
-/* ********************************************** */
+/* ------------------ */
 
-static short pose_select_same_group (Object *ob)
+static int pose_autoside_names_exec (bContext *C, wmOperator *op)
 {
-       bPose *pose= (ob)? ob->pose : NULL;
-       bArmature *arm= (ob)? ob->data : NULL;
-       bPoseChannel *pchan, *chan;
-       short changed= 0;
-       
-       if (ELEM3(NULL, ob, pose, arm))
-               return 0;
-       
-       /* loop in loop... bad and slow! */
-       for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
-               if (arm->layer & pchan->bone->layer) {
-                       if (pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
-                               
-                               /* only if group matches (and is not selected or current bone) */
-                               for (chan= ob->pose->chanbase.first; chan; chan= chan->next) {
-                                       if (arm->layer & chan->bone->layer) {
-                                               if (pchan->agrp_index == chan->agrp_index) {
-                                                       chan->bone->flag |= BONE_SELECTED;
-                                                       changed= 1;
-                                               }
-                                       }
-                               }
-                               
-                       }
-               }
-       }
-       
-       return changed;
-}
-
-static short pose_select_same_layer (Object *ob)
-{
-       bPose *pose= (ob)? ob->pose : NULL;
-       bArmature *arm= (ob)? ob->data : NULL;
-       bPoseChannel *pchan;
-       short layers= 0, changed= 0;
+       Object *ob= CTX_data_active_object(C);
+       bArmature *arm;
+       char newname[32];
+       short axis= RNA_enum_get(op->ptr, "axis");
        
-       if (ELEM3(NULL, ob, pose, arm))
-               return 0;
+       /* paranoia checks */
+       if (ELEM(NULL, ob, ob->pose)) 
+               return OPERATOR_CANCELLED;
+       arm= ob->data;
        
-       /* figure out what bones are selected */
-       for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
-               if (arm->layer & pchan->bone->layer) {
-                       if (pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
-                               layers |= pchan->bone->layer;
-                       }
-               }
-       }
-       if (layers == 0) 
-               return 0;
-               
-       /* select bones that are on same layers as layers flag */
-       for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
-               if (arm->layer & pchan->bone->layer) {
-                       if (layers & pchan->bone->layer) {
-                               pchan->bone->flag |= BONE_SELECTED;
-                               changed= 1;
-                       }
-               }
+       /* loop through selected bones, auto-naming them */
+       CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pose_bones)
+       {
+               BLI_strncpy(newname, pchan->name, sizeof(newname));
+               bone_autoside_name(newname, 1, axis, pchan->bone->head[axis], pchan->bone->tail[axis]);
+               ED_armature_bone_rename(arm, pchan->name, newname);
        }
+       CTX_DATA_END;
        
-       return changed;
-}
-
-void pose_select_grouped (Scene *scene, short nr)
-{
-       short changed = 0;
-       
-       if (nr == 1)            changed= pose_select_same_group(OBACT);
-       else if (nr == 2)       changed= pose_select_same_layer(OBACT);
-       
-       if (changed) {
-               countall();
-               BIF_undo_push("Select Grouped");
-       }
-}
+       /* since we renamed stuff... */
+       DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
 
-/* Shift-G in 3D-View while in PoseMode */
-void pose_select_grouped_menu (Scene *scene)
-{
-       short nr;
+       /* note, notifier might evolve */
+       WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
        
-       /* here we go */
-       nr= pupmenu("Select Grouped%t|In Same Group%x1|In Same Layer%x2");
-       pose_select_grouped(scene, nr);
+       return OPERATOR_FINISHED;
 }
 
-/* ********************************************** */
-
-/* context active object */
-void pose_flip_names(Scene *scene)
+void POSE_OT_autoside_names (wmOperatorType *ot)
 {
-       Object *obedit= scene->obedit; // XXX context
-       Object *ob= OBACT;
-       bArmature *arm= ob->data;
-       bPoseChannel *pchan;
-       char newname[32];
+       static EnumPropertyItem axis_items[]= {
+               {0, "XAXIS", 0, "X-Axis", "Left/Right"},
+               {1, "YAXIS", 0, "Y-Axis", "Front/Back"},
+               {2, "ZAXIS", 0, "Z-Axis", "Top/Bottom"},
+               {0, NULL, 0, NULL, NULL}};
        
-       /* paranoia checks */
-       if(!ob && !ob->pose) return;
-       if(ob==obedit || (ob->flag & OB_POSEMODE)==0) return;
+       /* identifiers */
+       ot->name= "AutoName by Axis";
+       ot->idname= "POSE_OT_autoside_names";
+       ot->description= "Automatically renames the selected bones according to which side of the target axis they fall on";
        
-       if(pose_has_protected_selected(ob, 0, 1))
-               return;
+       /* api callbacks */
+       ot->invoke= WM_menu_invoke;
+       ot->exec= pose_autoside_names_exec;
+       ot->poll= ED_operator_posemode;
        
-       for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
-               if(arm->layer & pchan->bone->layer) {
-                       if(pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
-                               BLI_strncpy(newname, pchan->name, sizeof(newname));
-                               bone_flip_name(newname, 1);     // 1 = do strip off number extensions
-                               ED_armature_bone_rename(arm, pchan->name, newname);
-                       }
-               }
-       }
+       /* flags */
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
        
-       BIF_undo_push("Flip names");
+       /* settings */
+       ot->prop= RNA_def_enum(ot->srna, "axis", axis_items, 0, "Axis", "Axis tag names with.");
 }
 
-/* context active object */
-void pose_autoside_names(Scene *scene, short axis)
-{
-       Object *obedit= scene->obedit; // XXX context
-       Object *ob= OBACT;
-       bArmature *arm= ob->data;
-       bPoseChannel *pchan;
-       char newname[32];
-       
-       /* paranoia checks */
-       if (ELEM(NULL, ob, ob->pose)) return;
-       if (ob==obedit || (ob->flag & OB_POSEMODE)==0) return;
-       
-       if (pose_has_protected_selected(ob, 0, 1))
-               return;
-       
-       for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
-               if(arm->layer & pchan->bone->layer) {
-                       if(pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
-                               BLI_strncpy(newname, pchan->name, sizeof(newname));
-                               bone_autoside_name(newname, 1, axis, pchan->bone->head[axis], pchan->bone->tail[axis]);
-                               ED_armature_bone_rename(arm, pchan->name, newname);
-                       }
-               }
-       }
-       
-       BIF_undo_push("Flip names");
-}
+/* ********************************************** */
 
 /* context active object, or weightpainted object with armature in posemode */
 void pose_activate_flipped_bone(Scene *scene)
@@ -1483,33 +1444,29 @@ void pose_activate_flipped_bone(Scene *scene)
        
        if(ob==NULL) return;
 
-       if(G.f & G_WEIGHTPAINT) {
+       if(ob->mode && OB_MODE_WEIGHT_PAINT) {
                ob= modifiers_isDeformedByArmature(ob);
        }
-       if(ob && (ob->flag & OB_POSEMODE)) {
-               bPoseChannel *pchan, *pchanf;
+       if(ob && (ob->mode & OB_MODE_POSE)) {
+               bPoseChannel *pchanf;
                
-               for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
-                       if(arm->layer & pchan->bone->layer) {
-                               if(pchan->bone->flag & BONE_ACTIVE)
-                                       break;
-                       }
-               }
-               if(pchan) {
+               if(arm->act_bone) {
                        char name[32];
                        
-                       BLI_strncpy(name, pchan->name, 32);
+                       BLI_strncpy(name, arm->act_bone->name, 32);
                        bone_flip_name(name, 1);        // 0 = do not strip off number extensions
                        
                        pchanf= get_pose_channel(ob->pose, name);
-                       if(pchanf && pchanf!=pchan) {
-                               pchan->bone->flag &= ~(BONE_SELECTED|BONE_ACTIVE);
-                               pchanf->bone->flag |= (BONE_SELECTED|BONE_ACTIVE);
-                       
+                       if(pchanf && pchanf->bone != arm->act_bone) {
+                               arm->act_bone->flag &= ~BONE_SELECTED;
+                               pchanf->bone->flag |= BONE_SELECTED;
+
+                               arm->act_bone= pchanf->bone;
+
                                /* in weightpaint we select the associated vertex group too */
-                               if(G.f & G_WEIGHTPAINT) {
-                                       vertexgroup_select_by_name(OBACT, name);
-                                       DAG_object_flush_update(scene, OBACT, OB_RECALC_DATA);
+                               if(ob->mode & OB_MODE_WEIGHT_PAINT) {
+                                       ED_vgroup_select_by_name(OBACT, name);
+                                       DAG_id_flush_update(&OBACT->id, OB_RECALC_DATA);
                                }
                                
                                // XXX notifiers need to be sent to other editors to update
@@ -1519,284 +1476,315 @@ void pose_activate_flipped_bone(Scene *scene)
        }
 }
 
-/* This function pops up the move-to-layer popup widgets when the user
- * presses either SHIFT-MKEY or MKEY in PoseMode OR EditMode (for Armatures)
- */
-void pose_movetolayer(Scene *scene)
+
+/* ********************************************** */
+
+/* Present a popup to get the layers that should be used */
+static int pose_armature_layers_invoke (bContext *C, wmOperator *op, wmEvent *evt)
 {
-       Object *obedit= scene->obedit; // XXX context
-       Object *ob= OBACT;
-       bArmature *arm;
-       short lay= 0;
-       short shift= 0; // XXX
-       
-       if (ob==NULL) return;
-       arm= ob->data;
+       Object *ob= CTX_data_active_object(C);
+       bArmature *arm= (ob)? ob->data : NULL;
+       PointerRNA ptr;
+       int layers[32]; /* hardcoded for now - we can only have 32 armature layers, so this should be fine... */
        
-       if (shift) {
-               /* armature layers */
-               lay= arm->layer;
-               if ( movetolayer_short_buts(&lay, "Armature Layers")==0 ) return;
-               if (lay==0) return;
-               arm->layer= lay;
-               if(ob->pose)
-                       ob->pose->proxy_layer= lay;
-               
-       }
-       else if (obedit) {
-               /* the check for editbone layer moving needs to occur before posemode one to work */
-               EditBone *ebo;
-               EditBone *flipBone;
-               
-               for (ebo= arm->edbo->first; ebo; ebo= ebo->next) {
-                       if (arm->layer & ebo->layer) {
-                               if (ebo->flag & BONE_SELECTED)
-                                       lay |= ebo->layer;
-                       }
-               }
-               if (lay==0) return;
-               
-               if ( movetolayer_short_buts(&lay, "Bone Layers")==0 ) return;
-               if (lay==0) return;
-               
-               for (ebo= arm->edbo->first; ebo; ebo= ebo->next) {
-                       if (arm->layer & ebo->layer) {
-                               if (ebo->flag & BONE_SELECTED) {
-                                       ebo->layer= lay;
-                                       if (arm->flag & ARM_MIRROR_EDIT) {
-                                               flipBone = ED_armature_bone_get_mirrored(arm->edbo, ebo);
-                                               if (flipBone)
-                                                       flipBone->layer = lay;
-                                       }
-                               }
-                       }
-               }
-               
-               BIF_undo_push("Move Bone Layer");
-       }
-       else if (ob->flag & OB_POSEMODE) {
-               /* pose-channel layers */
-               bPoseChannel *pchan;
-               
-               if (pose_has_protected_selected(ob, 0, 1))
-                       return;
-               
-               for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
-                       if (arm->layer & pchan->bone->layer) {
-                               if (pchan->bone->flag & BONE_SELECTED)
-                                       lay |= pchan->bone->layer;
-                       }
-               }
-               if (lay==0) return;
-               
-               if ( movetolayer_short_buts(&lay, "Bone Layers")==0 ) return;
-               if (lay==0) return;
-               
-               for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
-                       if (arm->layer & pchan->bone->layer) {
-                               if (pchan->bone->flag & BONE_SELECTED)
-                                       pchan->bone->layer= lay;
-                       }
-               }
+       /* sanity checking */
+       if (arm == NULL)
+               return OPERATOR_CANCELLED;
                
-               BIF_undo_push("Move Bone Layer");
-       }
+       /* get RNA pointer to armature data to use that to retrieve the layers as ints to init the operator */
+       RNA_id_pointer_create((ID *)arm, &ptr);
+       RNA_boolean_get_array(&ptr, "layer", layers);
+       RNA_boolean_set_array(op->ptr, "layers", layers);
+       
+       /* part to sync with other similar operators... */
+       return WM_operator_props_popup(C, op, evt);
 }
 
-#if 0
-// XXX old sys
-/* for use with pose_relax only */
-static int pose_relax_icu(struct IpoCurve *icu, float framef, float *val, float *frame_prev, float *frame_next)
+/* Set the visible layers for the active armature (edit and pose modes) */
+static int pose_armature_layers_exec (bContext *C, wmOperator *op)
 {
-       if (!icu) {
-               return 0;
-       } 
-       else {
-               BezTriple *bezt = icu->bezt;
-               
-               BezTriple *bezt_prev=NULL, *bezt_next=NULL;
-               float w1, w2, wtot;
-               int i;
-               
-               for (i=0; i < icu->totvert; i++, bezt++) {
-                       if (bezt->vec[1][0] < framef - 0.5) {
-                               bezt_prev = bezt;
-                       } else {
-                               break;
-                       }
-               }
-               
-               if (bezt_prev==NULL) return 0;
-               
-               /* advance to the next, dont need to advance i */
-               bezt = bezt_prev+1;
-               
-               for (; i < icu->totvert; i++, bezt++) {
-                       if (bezt->vec[1][0] > framef + 0.5) {
-                               bezt_next = bezt;
-                                               break;
-                       }
-               }
+       Object *ob= CTX_data_active_object(C);
+       bArmature *arm= (ob)? ob->data : NULL;
+       PointerRNA ptr;
+       int layers[32]; /* hardcoded for now - we can only have 32 armature layers, so this should be fine... */
+       
+       /* get the values set in the operator properties */
+       RNA_boolean_get_array(op->ptr, "layers", layers);
+       
+       /* get pointer for armature, and write data there... */
+       RNA_id_pointer_create((ID *)arm, &ptr);
+       RNA_boolean_set_array(&ptr, "layer", layers);
+       
+       /* note, notifier might evolve */
+       WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
+       
+       return OPERATOR_FINISHED;
+}
+
+
+void POSE_OT_armature_layers (wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name= "Change Armature Layers";
+       ot->idname= "POSE_OT_armature_layers";
+       ot->description= "Change the visible armature layers";
+       
+       /* callbacks */
+       ot->invoke= pose_armature_layers_invoke;
+       ot->exec= pose_armature_layers_exec;
+       ot->poll= ED_operator_posemode;
+       
+       /* flags */
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+       
+       /* properties */
+       RNA_def_boolean_layer_member(ot->srna, "layers", 32, NULL, "Layer", "Armature layers to make visible");
+}
+
+void ARMATURE_OT_armature_layers (wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name= "Change Armature Layers";
+       ot->idname= "ARMATURE_OT_armature_layers";
+       ot->description= "Change the visible armature layers";
+       
+       /* callbacks */
+       ot->invoke= pose_armature_layers_invoke;
+       ot->exec= pose_armature_layers_exec;
+       ot->poll= ED_operator_editarmature;
+       
+       /* flags */
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+       
+       /* properties */
+       RNA_def_boolean_layer_member(ot->srna, "layers", 32, NULL, "Layer", "Armature layers to make visible");
+}
+
+/* ------------------- */
+
+/* Present a popup to get the layers that should be used */
+static int pose_bone_layers_invoke (bContext *C, wmOperator *op, wmEvent *evt)
+{
+       int layers[32]; /* hardcoded for now - we can only have 32 armature layers, so this should be fine... */
+       
+       /* get layers that are active already */
+       memset(&layers, 0, sizeof(layers)); /* set all layers to be off by default */
+       
+       CTX_DATA_BEGIN(C, bPoseChannel *, pchan, selected_pose_bones) 
+       {
+               short bit;
                
-               if (bezt_next==NULL) return 0;
-       
-               if (val) {
-                       w1 = framef - bezt_prev->vec[1][0];
-                       w2 = bezt_next->vec[1][0] - framef;
-                       wtot = w1 + w2;
-                       w1=w1/wtot;
-                       w2=w2/wtot;
-#if 0
-                       val = (bezt_prev->vec[1][1] * w2) + (bezt_next->vec[1][1] * w1);
-#else
-                       /* apply the value with a hard coded 6th */
-                       *val = (((bezt_prev->vec[1][1] * w2) + (bezt_next->vec[1][1] * w1)) + (*val * 5.0f)) / 6.0f;
-#endif
+               /* loop over the bits for this pchan's layers, adding layers where they're needed */
+               for (bit= 0; bit < 32; bit++) {
+                       if (pchan->bone->layer & (1<<bit))
+                               layers[bit]= 1;
                }
-               
-               if (frame_prev) *frame_prev = bezt_prev->vec[1][0];
-               if (frame_next) *frame_next = bezt_next->vec[1][0];
-               
-               return 1;
        }
+       CTX_DATA_END;
+       
+       /* copy layers to operator */
+       RNA_boolean_set_array(op->ptr, "layers", layers);
+       
+               /* part to sync with other similar operators... */
+       return WM_operator_props_popup(C, op, evt);
 }
-#endif
 
-void pose_relax(Scene *scene)
+/* Set the visible layers for the active armature (edit and pose modes) */
+static int pose_bone_layers_exec (bContext *C, wmOperator *op)
 {
-       Object *ob = OBACT;
-       bPose *pose;
-       bAction *act;
-       bArmature *arm;
+       Object *ob= CTX_data_active_object(C);
+       bArmature *arm= (ob)? ob->data : NULL;
+       PointerRNA ptr;
+       int layers[32]; /* hardcoded for now - we can only have 32 armature layers, so this should be fine... */
+       
+       /* get the values set in the operator properties */
+       RNA_boolean_get_array(op->ptr, "layers", layers);
+       
+       /* set layers of pchans based on the values set in the operator props */
+       CTX_DATA_BEGIN(C, bPoseChannel *, pchan, selected_pose_bones) 
+       {
+               /* get pointer for pchan, and write flags this way */
+               RNA_pointer_create((ID *)arm, &RNA_Bone, pchan->bone, &ptr);
+               RNA_boolean_set_array(&ptr, "layer", layers);
+       }
+       CTX_DATA_END;
        
-//     IpoCurve *icu_w, *icu_x, *icu_y, *icu_z;
+       /* note, notifier might evolve */
+       WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
        
-       bPoseChannel *pchan;
-//     bActionChannel *achan;
-//     float framef = F_CFRA;
-//     float frame_prev, frame_next;
-//     float quat_prev[4], quat_next[4], quat_interp[4], quat_orig[4];
+       return OPERATOR_FINISHED;
+}
+
+void POSE_OT_bone_layers (wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name= "Change Bone Layers";
+       ot->idname= "POSE_OT_bone_layers";
+       ot->description= "Change the layers that the selected bones belong to";
        
-       int do_scale = 0;
-       int do_loc = 0;
-       int do_quat = 0;
-       int flag = 0;
-//     int do_x, do_y, do_z;
+       /* callbacks */
+       ot->invoke= pose_bone_layers_invoke;
+       ot->exec= pose_bone_layers_exec;
+       ot->poll= ED_operator_posemode;
        
-       if (!ob) return;
+       /* flags */
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
        
-       pose = ob->pose;
-       act = ob->action;
-       arm = (bArmature *)ob->data;
+       /* properties */
+       RNA_def_boolean_layer_member(ot->srna, "layers", 32, NULL, "Layer", "Armature layers that bone belongs to");
+}
+
+/* ------------------- */
+
+/* Present a popup to get the layers that should be used */
+static int armature_bone_layers_invoke (bContext *C, wmOperator *op, wmEvent *evt)
+{
+       int layers[32]; /* hardcoded for now - we can only have 32 armature layers, so this should be fine... */
        
-       if (!pose || !act || !arm) return;
+       /* get layers that are active already */
+       memset(&layers, 0, sizeof(layers)); /* set all layers to be off by default */
        
-       for (pchan=pose->chanbase.first; pchan; pchan= pchan->next) {
-               
-               pchan->bone->flag &= ~BONE_TRANSFORM;
+       CTX_DATA_BEGIN(C, EditBone *, ebone, selected_editable_bones) 
+       {
+               short bit;
                
-               if (pchan->bone->layer & arm->layer) {
-                       if (pchan->bone->flag & BONE_SELECTED) {
-                               /* do we have an ipo curve? */
-#if 0 // XXX old animation system
-                               achan= get_action_channel(act, pchan->name);
-                               
-                               if (achan && achan->ipo) {
-                                       /*calc_ipo(achan->ipo, ctime);*/
-                                       
-                                       do_x = pose_relax_icu(find_ipocurve(achan->ipo, AC_LOC_X), framef, &pchan->loc[0], NULL, NULL);
-                                       do_y = pose_relax_icu(find_ipocurve(achan->ipo, AC_LOC_Y), framef, &pchan->loc[1], NULL, NULL);
-                                       do_z = pose_relax_icu(find_ipocurve(achan->ipo, AC_LOC_Z), framef, &pchan->loc[2], NULL, NULL);
-                                       do_loc += do_x + do_y + do_z;
-                                       
-                                       do_x = pose_relax_icu(find_ipocurve(achan->ipo, AC_SIZE_X), framef, &pchan->size[0], NULL, NULL);
-                                       do_y = pose_relax_icu(find_ipocurve(achan->ipo, AC_SIZE_Y), framef, &pchan->size[1], NULL, NULL);
-                                       do_z = pose_relax_icu(find_ipocurve(achan->ipo, AC_SIZE_Z), framef, &pchan->size[2], NULL, NULL);
-                                       do_scale += do_x + do_y + do_z;
-                                               
-                                       if(     ((icu_w = find_ipocurve(achan->ipo, AC_QUAT_W))) &&
-                                               ((icu_x = find_ipocurve(achan->ipo, AC_QUAT_X))) &&
-                                               ((icu_y = find_ipocurve(achan->ipo, AC_QUAT_Y))) &&
-                                               ((icu_z = find_ipocurve(achan->ipo, AC_QUAT_Z))) )
-                                       {
-                                               /* use the quatw keyframe as a basis for others */
-                                               if (pose_relax_icu(icu_w, framef, NULL, &frame_prev, &frame_next)) {
-                                                       /* get 2 quats */
-                                                       quat_prev[0] = eval_icu(icu_w, frame_prev);
-                                                       quat_prev[1] = eval_icu(icu_x, frame_prev);
-                                                       quat_prev[2] = eval_icu(icu_y, frame_prev);
-                                                       quat_prev[3] = eval_icu(icu_z, frame_prev);
-                                                       
-                                                       quat_next[0] = eval_icu(icu_w, frame_next);
-                                                       quat_next[1] = eval_icu(icu_x, frame_next);
-                                                       quat_next[2] = eval_icu(icu_y, frame_next);
-                                                       quat_next[3] = eval_icu(icu_z, frame_next);
-                                                       
-#if 0
-                                                       /* apply the setting, completely smooth */
-                                                       QuatInterpol(pchan->quat, quat_prev, quat_next, (framef-frame_prev) / (frame_next-frame_prev) );
-#else
-                                                       /* tricky interpolation */
-                                                       QuatInterpol(quat_interp, quat_prev, quat_next, (framef-frame_prev) / (frame_next-frame_prev) );
-                                                       QUATCOPY(quat_orig, pchan->quat);
-                                                       QuatInterpol(pchan->quat, quat_orig, quat_interp, 1.0f/6.0f);
-                                                       /* done */
-#endif
-                                                       do_quat++;
-                                               }
-                                       }
-                                       
-                                       /* apply BONE_TRANSFORM tag so that autokeying will pick it up */
-                                       pchan->bone->flag |= BONE_TRANSFORM;
-                               }
-                               
-#endif // XXX old animation system
-                       }
+               /* loop over the bits for this pchan's layers, adding layers where they're needed */
+               for (bit= 0; bit < 32; bit++) {
+                       if (ebone->layer & (1<<bit))
+                               layers[bit]= 1;
                }
        }
+       CTX_DATA_END;
+       
+       /* copy layers to operator */
+       RNA_boolean_set_array(op->ptr, "layers", layers);
        
-       ob->pose->flag |= (POSE_LOCKED|POSE_DO_UNLOCK);
-       
-       /* do auto-keying */
-       if (do_loc)             flag |= TFM_TRANSLATION;
-       if (do_scale)   flag |= TFM_RESIZE;
-       if (do_quat)    flag |= TFM_ROTATION;
-       autokeyframe_pose_cb_func(ob, flag, 0);
-        
-       /* clear BONE_TRANSFORM flags */
-       for (pchan=pose->chanbase.first; pchan; pchan= pchan->next)
-               pchan->bone->flag &= ~ BONE_TRANSFORM;
-       
-       /* do depsgraph flush */
-       DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
-       BIF_undo_push("Relax Pose");
+               /* part to sync with other similar operators... */
+       return WM_operator_props_popup(C, op, evt);
 }
 
-/* for use in insertkey, ensure rotation goes other way around */
-void pose_flipquats(Scene *scene)
+/* Set the visible layers for the active armature (edit and pose modes) */
+static int armature_bone_layers_exec (bContext *C, wmOperator *op)
 {
-       Object *ob = OBACT;
-       bArmature *arm= ob->data;
-       bPoseChannel *pchan;
+       Object *ob= CTX_data_edit_object(C);
+       bArmature *arm= (ob)? ob->data : NULL;
+       PointerRNA ptr;
+       int layers[32]; /* hardcoded for now - we can only have 32 armature layers, so this should be fine... */
+       
+       /* get the values set in the operator properties */
+       RNA_boolean_get_array(op->ptr, "layers", layers);
+       
+       /* set layers of pchans based on the values set in the operator props */
+       CTX_DATA_BEGIN(C, EditBone *, ebone, selected_editable_bones) 
+       {
+               /* get pointer for pchan, and write flags this way */
+               RNA_pointer_create((ID *)arm, &RNA_EditBone, ebone, &ptr);
+               RNA_boolean_set_array(&ptr, "layer", layers);
+       }
+       CTX_DATA_END;
        
-       if(ob->pose==NULL)
-               return;
+       /* note, notifier might evolve */
+       WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
        
-       /* find sel bones */
-       for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
-               if(pchan->bone && (pchan->bone->flag & BONE_SELECTED) && (pchan->bone->layer & arm->layer)) {
+       return OPERATOR_FINISHED;
+}
+
+void ARMATURE_OT_bone_layers (wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name= "Change Bone Layers";
+       ot->idname= "ARMATURE_OT_bone_layers";
+       ot->description= "Change the layers that the selected bones belong to";
+       
+       /* callbacks */
+       ot->invoke= armature_bone_layers_invoke;
+       ot->exec= armature_bone_layers_exec;
+       ot->poll= ED_operator_editarmature;
+       
+       /* flags */
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+       
+       /* properties */
+       RNA_def_boolean_layer_member(ot->srna, "layers", 32, NULL, "Layer", "Armature layers that bone belongs to");
+}
+
+/* ********************************************** */
+
+static int pose_flip_quats_exec (bContext *C, wmOperator *op)
+{
+       Scene *scene= CTX_data_scene(C);
+       Object *ob= CTX_data_active_object(C);
+       
+       bCommonKeySrc cks;
+       ListBase dsources = {&cks, &cks};
+       
+       /* init common-key-source for use by KeyingSets */
+       memset(&cks, 0, sizeof(bCommonKeySrc));
+       cks.id= &ob->id;
+       
+       /* loop through all selected pchans, flipping and keying (as needed) */
+       CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pose_bones)
+       {
+               /* only if bone is using quaternion rotation */
+               if (pchan->rotmode == ROT_MODE_QUAT) {
                        /* quaternions have 720 degree range */
                        pchan->quat[0]= -pchan->quat[0];
                        pchan->quat[1]= -pchan->quat[1];
                        pchan->quat[2]= -pchan->quat[2];
                        pchan->quat[3]= -pchan->quat[3];
+                       
+                       /* perform auto-keying 
+                        * NOTE: paths don't need recalculation here, since the orientations shouldn't have changed
+                        */
+                       if (autokeyframe_cfra_can_key(scene, &ob->id)) {
+                               /* Set keys on pose
+                                *      - KeyingSet to use depends on rotation mode 
+                                *      (but that's handled by the templates code)  
+                                */
+                               KeyingSet *ks= ANIM_builtin_keyingset_get_named(NULL, "Rotation");
+                               
+                               /* init cks for this PoseChannel, then use the relative KeyingSets to keyframe it */
+                               cks.pchan= pchan;
+                               
+                               modify_keyframes(scene, &dsources, NULL, ks, MODIFYKEY_MODE_INSERT, (float)CFRA);
+                               
+                               /* clear any unkeyed tags */
+                               if (pchan->bone)
+                                       pchan->bone->flag &= ~BONE_UNKEYED;
+                       }
+                       else {
+                               /* add unkeyed tags */
+                               if (pchan->bone)
+                                       pchan->bone->flag |= BONE_UNKEYED;
+                       }
                }
        }
+       CTX_DATA_END;
+       
+       /* notifiers and updates */
+       DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
+       WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, ob);
+       
+       return OPERATOR_FINISHED;
+}
+
+void POSE_OT_quaternions_flip (wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name = "Flip Quats";
+       ot->idname= "POSE_OT_quaternions_flip";
+       ot->description= "Flip quaternion values to achieve desired rotations, while maintaining the same orientations";
+       
+       /* callbacks */
+       ot->exec= pose_flip_quats_exec;
+       ot->poll= ED_operator_posemode;
        
-       /* do autokey */
-       autokeyframe_pose_cb_func(ob, TFM_ROTATION, 0);
+       /* flags */
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 }
 
+/* ********************************************** */
+
 /* context: active channel */
 void pose_special_editmenu(Scene *scene)
 {
@@ -1807,7 +1795,7 @@ void pose_special_editmenu(Scene *scene)
        
        /* paranoia checks */
        if(!ob && !ob->pose) return;
-       if(ob==obedit || (ob->flag & OB_POSEMODE)==0) return;
+       if(ob==obedit || (ob->mode & OB_MODE_POSE)==0) return;
        
        nr= pupmenu("Specials%t|Select Constraint Target%x1|Flip Left-Right Names%x2|Calculate Paths%x3|Clear Paths%x4|Clear User Transform %x5|Relax Pose %x6|%l|AutoName Left-Right%x7|AutoName Front-Back%x8|AutoName Top-Bottom%x9");
        if(nr==1) {
@@ -1863,7 +1851,7 @@ void pose_clear_user_transforms(Scene *scene, Object *ob)
                rest_pose(ob->pose);
        }
        
-       DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
+       DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
        BIF_undo_push("Clear User Transform");
 }