batch remove .'s used with RNA_def_struct_ui_text
[blender.git] / source / blender / editors / armature / poseobject.c
index 2913d1d13d9b344e2bdade489b577a97d1294b86..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"
@@ -63,6 +66,7 @@
 #include "BKE_modifier.h"
 #include "BKE_object.h"
 #include "BKE_utildefines.h"
+#include "BKE_report.h"
 
 #include "BIF_gl.h"
 
 #include "ED_transform.h" /* for autokey TFM_TRANSLATION, etc */
 #include "ED_view3d.h"
 
+#include "UI_interface.h"
+
 #include "armature_intern.h"
 
 /* ************* XXX *************** */
-static int movetolayer_short_buts() {return 1;}
-static int okee() {return 0;}
 static int pupmenu() {return 0;}
-static void waitcursor() {};
 static void error() {};
 static void BIF_undo_push() {}
-static void countall() {}
-static void add_constraint() {}
-static void autokeyframe_pose_cb_func() {}
 /* ************* XXX *************** */
 
 /* This function is used to indicate that a bone is selected and needs keyframes inserted */
@@ -124,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);
                        
@@ -134,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)
@@ -142,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);
        }       
@@ -157,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;
                                }
                        }
@@ -199,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;
                        }
                }
        }
-       
 }
 
-
-
-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;
        
-       BIF_undo_push("Select constraint target");
+       /* use the backend function for this */
+       ED_pose_clear_paths(ob);
+       
+       /* notifiers for updates */
+       WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
+       
+       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);
@@ -508,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;
                                        }
@@ -551,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);
@@ -580,98 +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 ************* */
 
-void pose_add_IK(Scene *scene)
+static short pose_select_same_group (bContext *C, Object *ob, short extend)
 {
-       Object *obedit= scene->obedit; // XXX context
-       Object *ob= OBACT;
+       bArmature *arm= (ob)? ob->data : NULL;
+       bPose *pose= (ob)? ob->pose : NULL;
+       char *group_flags;
+       int numGroups = 0;
+       short changed=0, tagged=0;
        
-       /* paranoia checks */
-       if(!ob && !ob->pose) return;
-       if(ob==obedit || (ob->flag & OB_POSEMODE)==0) return;
+       /* 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);
        
-       add_constraint(1);      /* 1 means only IK */
+       return changed;
 }
 
-/* context: all selected channels */
-void pose_clear_IK(Scene *scene)
+static short pose_select_same_layer (bContext *C, Object *ob, short extend)
 {
-       Object *obedit= scene->obedit; // XXX context
-       Object *ob= OBACT;
-       bArmature *arm= ob->data;
-       bPoseChannel *pchan;
-       bConstraint *con;
-       bConstraint *next;
-       
-       /* paranoia checks */
-       if(!ob && !ob->pose) return;
-       if(ob==obedit || (ob->flag & OB_POSEMODE)==0) return;
+       bPose *pose= (ob)? ob->pose : NULL;
+       bArmature *arm= (ob)? ob->data : NULL;
+       short changed= 0;
+       int layers= 0;
        
-       if(pose_has_protected_selected(ob, 0, 1))
-               return;
+       if (ELEM3(NULL, ob, pose, arm))
+               return 0;
        
-       if(okee("Remove IK constraint(s)")==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= next) {
-                                       next= con->next;
-                                       if(con->type==CONSTRAINT_TYPE_KINEMATIC) {
-                                               BLI_remlink(&pchan->constraints, con);
-                                               free_constraint_data(con);
-                                               MEM_freeN(con);
-                                       }
-                               }
-                               pchan->constflag &= ~(PCHAN_HAS_IK|PCHAN_HAS_TARGET);
-                       }
+       /* 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;
        
-       DAG_object_flush_update(scene, ob, OB_RECALC_DATA);     // and all its relations
-       
-       BIF_undo_push("Remove IK constraint(s)");
+       return changed;
 }
 
-void pose_clear_constraints(Scene *scene)
+
+static int pose_select_grouped_exec (bContext *C, wmOperator *op)
 {
-       Object *obedit= scene->obedit; // XXX context
-       Object *ob= OBACT;
-       bArmature *arm= ob->data;
-       bPoseChannel *pchan;
-       
-       /* paranoia checks */
-       if(!ob && !ob->pose) return;
-       if(ob==obedit || (ob->flag & OB_POSEMODE)==0) return;
+       Object *ob= CTX_data_active_object(C);
+       short extend= RNA_boolean_get(op->ptr, "extend");
+       short changed = 0;
        
-       if(pose_has_protected_selected(ob, 0, 1))
-               return;
+       /* 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;
+       }
        
-       if(okee("Remove Constraints")==0) return;
+       /* notifiers for updates */
+       WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
        
-       /* find active */
-       for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
-               if(arm->layer & pchan->bone->layer) {
-                       if(pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
-                               free_constraints(&pchan->constraints);
-                               pchan->constflag= 0;
-                       }
-               }
-       }
+       /* 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";
        
-       DAG_object_flush_update(scene, ob, OB_RECALC_DATA);     // and all its relations
+       /* api callbacks */
+       ot->invoke= WM_menu_invoke;
+       ot->exec= pose_select_grouped_exec;
+       ot->poll= ED_operator_posemode;
        
-       BIF_undo_push("Remove Constraint(s)");
+       /* 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)
 {
@@ -684,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;
@@ -728,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);
@@ -766,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 */
@@ -776,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 */
@@ -788,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);
                                        }
                                }
@@ -857,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");
        
@@ -865,104 +834,187 @@ void pose_copy_menu(Scene *scene)
 
 /* ******************** copy/paste pose ********************** */
 
-static bPose   *g_posebuf=NULL;
+/* Global copy/paste buffer for pose - cleared on start/end session + before every copy operation */
+static bPose *g_posebuf = NULL;
 
 void free_posebuf(void) 
 {
        if (g_posebuf) {
-               // was copied without constraints
-               BLI_freelistN (&g_posebuf->chanbase);
-               MEM_freeN (g_posebuf);
+               /* was copied without constraints */
+               BLI_freelistN(&g_posebuf->chanbase);
+               MEM_freeN(g_posebuf);
        }
+       
        g_posebuf=NULL;
 }
 
-void copy_posebuf (Scene *scene)
-{
-       Object *ob= OBACT;
+/* ---- */
 
-       if (!ob || !ob->pose){
-               error ("No Pose");
-               return;
+static int pose_copy_exec (bContext *C, wmOperator *op)
+{
+       Object *ob= CTX_data_active_object(C);
+       
+       /* sanity checking */
+       if ELEM(NULL, ob, ob->pose) {
+               BKE_report(op->reports, RPT_ERROR, "No Pose to Copy");
+               return OPERATOR_CANCELLED;
        }
 
+       /* free existing pose buffer */
        free_posebuf();
        
-       set_pose_keys(ob);  // sets chan->flag to POSE_KEY if bone selected
+       /* sets chan->flag to POSE_KEY if bone selected, then copy those bones to the buffer */
+       set_pose_keys(ob);  
        copy_pose(&g_posebuf, ob->pose, 0);
+       
+       
+       return OPERATOR_FINISHED;
+}
 
+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";
+       
+       /* api callbacks */
+       ot->exec= pose_copy_exec;
+       ot->poll= ED_operator_posemode;
+       
+       /* flag */
+       ot->flag= OPTYPE_REGISTER;
 }
 
-void paste_posebuf (Scene *scene, int flip)
+/* ---- */
+
+/* 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)
 {
-       Object *ob= OBACT;
+       Scene *scene= CTX_data_scene(C);
+       Object *ob= CTX_data_active_object(C);
        bPoseChannel *chan, *pchan;
-       float eul[4];
        char name[32];
+       int flip= RNA_boolean_get(op->ptr, "flipped");
        
-       if (!ob || !ob->pose)
-               return;
+       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;
 
-       if (!g_posebuf){
-               error ("Copy buffer is empty");
-               return;
+       if (g_posebuf == NULL) {
+               BKE_report(op->reports, RPT_ERROR, "Copy buffer is empty");
+               return OPERATOR_CANCELLED;
        }
        
-       /*
-       // disabled until protected bones in proxies follow the rules everywhere else!
-       if(pose_has_protected_selected(ob, 1, 1))
-               return;
-       */
-       
-       /* Safely merge all of the channels in this pose into
-       any existing pose */
-       for (chan=g_posebuf->chanbase.first; chan; chan=chan->next) {
+       /* Safely merge all of the channels in the buffer pose into any existing pose */
+       for (chan= g_posebuf->chanbase.first; chan; chan=chan->next) {
                if (chan->flag & POSE_KEY) {
+                       /* get the name - if flipping, we must flip this first */
                        BLI_strncpy(name, chan->name, sizeof(name));
                        if (flip)
-                               bone_flip_name (name, 0);               // 0 = don't strip off number extensions
+                               bone_flip_name(name, 0);                /* 0 = don't strip off number extensions */
                                
                        /* only copy when channel exists, poses are not meant to add random channels to anymore */
                        pchan= get_pose_channel(ob->pose, name);
                        
                        if (pchan) {
-                               /* only loc rot size */
-                               /* only copies transform info for the pose */
+                               /* only loc rot size 
+                                *      - only copies transform info for the pose 
+                                */
                                VECCOPY(pchan->loc, chan->loc);
                                VECCOPY(pchan->size, chan->size);
-                               QUATCOPY(pchan->quat, chan->quat);
                                pchan->flag= chan->flag;
                                
-                               if (flip) {
-                                       pchan->loc[0]*= -1;
-                                       
-                                       QuatToEul(pchan->quat, eul);
-                                       eul[1]*= -1;
-                                       eul[2]*= -1;
-                                       EulToQuat(eul, pchan->quat);
+                               /* 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 > 0) {
+                                               VECCOPY(pchan->eul, chan->eul);
+                                       }
+                                       else {
+                                               QUATCOPY(pchan->quat, chan->quat);
+                                       }
+                               }
+                               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/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);
                                }
                                
-#if 0 // XXX old animation system
-                               if (autokeyframe_cfra_can_key(ob)) {
-                                       ID *id= &ob->id;
+                               /* paste flipped pose? */
+                               if (flip) {
+                                       pchan->loc[0]*= -1;
                                        
-                                       /* Set keys on pose */
-                                       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);
+                                       /* has to be done as eulers... */
+                                       if (pchan->rotmode > 0) {
+                                               pchan->eul[1] *= -1;
+                                               pchan->eul[2] *= -1;
                                        }
-                                       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);
+                                       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
                                        }
-                                       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);
+                                       else {
+                                               float eul[3];
+                                               
+                                               quat_to_eul( eul,pchan->quat);
+                                               eul[1]*= -1;
+                                               eul[2]*= -1;
+                                               eul_to_quat( pchan->quat,eul);
                                        }
+                               }
+                               
+                               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");
+                                       
+                                       /* 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)
@@ -973,13 +1025,12 @@ void paste_posebuf (Scene *scene, int flip)
                                        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);
@@ -989,400 +1040,402 @@ void paste_posebuf (Scene *scene, int flip)
                where_is_pose(scene, ob);
                ob->recalc= 0;
        }
+       
+       /* notifiers for updates */
+       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...
 
-       BIF_undo_push("Paste Action Pose");
+       return OPERATOR_FINISHED;
 }
 
-/* ********************************************** */
-
-/* context weightpaint and deformer in posemode */
-void pose_adds_vgroups(Scene *scene, Object *meshobj, int heatweights)
+void POSE_OT_paste (wmOperatorType *ot)
 {
-// XXX extern VPaint Gwp;         /* from vpaint */
-       Object *poseobj= modifiers_isDeformedByArmature(meshobj);
+       /* identifiers */
+       ot->name= "Paste Pose";
+       ot->idname= "POSE_OT_paste";
+       ot->description= "Pastes the stored pose on to the current pose";
+       
+       /* api callbacks */
+       ot->exec= pose_paste_exec;
+       ot->poll= ED_operator_posemode;
+       
+       /* flag */
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+       
+       /* properties */
+       RNA_def_boolean(ot->srna, "flipped", 0, "Flipped on X-Axis", "");
+}
 
-       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");
+static int pose_group_add_exec (bContext *C, wmOperator *op)
+{
+       ScrArea *sa= CTX_wm_area(C);
+       Object *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
-               BIF_undo_push("Apply Bone Envelopes to Vertex Groups");
+               ob= CTX_data_active_object(C);
+               
+       /* only continue if there's an object */
+       if (ob == NULL)
+               return OPERATOR_CANCELLED;
+       
+       /* for now, just call the API function for this */
+       pose_add_group(ob);
+       
+       /* notifiers for updates */
+       WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
+       
+       return OPERATOR_FINISHED;
+}
 
+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";
        
-       // and all its relations
-       DAG_object_flush_update(scene, meshobj, OB_RECALC_DATA);
+       /* api callbacks */
+       ot->exec= pose_group_add_exec;
+       ot->poll= ED_operator_posemode;
+       
+       /* flags */
+       ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
 }
 
-/* ********************************************** */
 
-/* adds a new pose-group */
-void pose_add_posegroup (Scene *scene)
+static int pose_group_remove_exec (bContext *C, wmOperator *op)
 {
-       Object *ob= OBACT;
-       bPose *pose= (ob) ? ob->pose : NULL;
-       bActionGroup *grp;
+       ScrArea *sa= CTX_wm_area(C);
+       Object *ob;
        
-       if (ELEM(NULL, ob, ob->pose))
-               return;
+       /* 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);
        
-       grp= MEM_callocN(sizeof(bActionGroup), "PoseGroup");
-       strcpy(grp->name, "Group");
-       BLI_addtail(&pose->agroups, grp);
-       BLI_uniquename(&pose->agroups, grp, "Group", '.', offsetof(bActionGroup, name), 32);
+       /* only continue if there's an object */
+       if (ob == NULL)
+               return OPERATOR_CANCELLED;
        
-       pose->active_group= BLI_countlist(&pose->agroups);
+       /* for now, just call the API function for this */
+       pose_remove_group(ob);
        
-       BIF_undo_push("Add Bone Group");
+       /* notifiers for updates */
+       WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
        
+       return OPERATOR_FINISHED;
 }
 
-/* Remove the active bone-group */
-void pose_remove_posegroup (Scene *scene)
+void POSE_OT_group_remove (wmOperatorType *ot)
 {
-       Object *ob= OBACT;
-       bPose *pose= (ob) ? ob->pose : NULL;
-       bActionGroup *grp = NULL;
-       bPoseChannel *pchan;
-       
-       /* sanity checks */
-       if (ELEM(NULL, ob, pose))
-               return;
-       if (pose->active_group <= 0)
-               return;
+       /* identifiers */
+       ot->name= "Remove Bone Group";
+       ot->idname= "POSE_OT_group_remove";
+       ot->description= "Removes the active bone group";
        
-       /* get group to remove */
-       grp= BLI_findlink(&pose->agroups, pose->active_group-1);
-       if (grp) {
-               /* adjust group references (the trouble of using indices!):
-                *      - firstly, make sure nothing references it 
-                *      - also, make sure that those after this item get corrected
-                */
-               for (pchan= pose->chanbase.first; pchan; pchan= pchan->next) {
-                       if (pchan->agrp_index == pose->active_group)
-                               pchan->agrp_index= 0;
-                       else if (pchan->agrp_index > pose->active_group)
-                               pchan->agrp_index--;
-               }
-               
-               /* now, remove it from the pose */
-               BLI_freelinkN(&pose->agroups, grp);
-               pose->active_group= 0;
-               
-               BIF_undo_push("Remove Bone Group");
-       }
+       /* api callbacks */
+       ot->exec= pose_group_remove_exec;
+       ot->poll= ED_operator_posemode;
        
+       /* flags */
+       ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
 }
 
-char *build_posegroups_menustr (bPose *pose, short for_pupmenu)
+/* ------------ */
+
+/* invoke callback which presents a list of bone-groups for the user to choose from */
+static int pose_groups_menu_invoke (bContext *C, wmOperator *op, wmEvent *evt)
 {
-       DynStr *pupds= BLI_dynstr_new();
+       ScrArea *sa= CTX_wm_area(C);
+       Object *ob;
+       bPose *pose;
+       
+       uiPopupMenu *pup;
+       uiLayout *layout;
        bActionGroup *grp;
-       char *str;
-       char buf[16];
        int i;
        
-       /* add title first (and the "none" entry) */
-       BLI_dynstr_append(pupds, "Bone Group%t|");
-       if (for_pupmenu)
-               BLI_dynstr_append(pupds, "Add New%x0|");
+       /* 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
-               BLI_dynstr_append(pupds, "BG: [None]%x0|");
+               ob= CTX_data_active_object(C);
+       
+       /* only continue if there's an object, and a pose there too */
+       if (ELEM(NULL, ob, ob->pose)) 
+               return OPERATOR_CANCELLED;
+       pose= ob->pose;
        
-       /* loop through groups, adding them */
-       for (grp= pose->agroups.first, i=1; grp; grp=grp->next, i++) {
-               if (for_pupmenu == 0)
-                       BLI_dynstr_append(pupds, "BG: ");
-               BLI_dynstr_append(pupds, grp->name);
+       /* if there's no active group (or active is invalid), create a new menu to find it */
+       if (pose->active_group <= 0) {
+               /* create a new menu, and start populating it with group names */
+               pup= uiPupMenuBegin(C, op->type->name, 0);
+               layout= uiPupMenuLayout(pup);
+               
+               /* special entry - allow to create new group, then use that 
+                *      (not to be used for removing though)
+                */
+               if (strstr(op->idname, "assign")) {
+                       uiItemIntO(layout, "New Group", 0, op->idname, "type", 0);
+                       uiItemS(layout);
+               }
                
-               sprintf(buf, "%%x%d", i);
-               BLI_dynstr_append(pupds, buf);
+               /* add entries for each group */
+               for (grp= pose->agroups.first, i=1; grp; grp=grp->next, i++)
+                       uiItemIntO(layout, grp->name, 0, op->idname, "type", i);
+                       
+               /* finish building the menu, and process it (should result in calling self again) */
+               uiPupMenuEnd(C, pup);
                
-               if (grp->next)
-                       BLI_dynstr_append(pupds, "|");
+               return OPERATOR_CANCELLED;
+       }
+       else {
+               /* just use the active group index, and call the exec callback for the calling operator */
+               RNA_int_set(op->ptr, "type", pose->active_group);
+               return op->type->exec(C, op);
        }
-       
-       /* convert to normal MEM_malloc'd string */
-       str= BLI_dynstr_get_cstring(pupds);
-       BLI_dynstr_free(pupds);
-       
-       return str;
 }
 
 /* Assign selected pchans to the bone group that the user selects */
-void pose_assign_to_posegroup (Scene *scene, short active)
+static int pose_group_assign_exec (bContext *C, wmOperator *op)
 {
-       Object *ob= OBACT;
-       bArmature *arm= (ob) ? ob->data : NULL;
-       bPose *pose= (ob) ? ob->pose : NULL;
-       bPoseChannel *pchan;
-       char *menustr;
-       int nr;
+       ScrArea *sa= CTX_wm_area(C);
+       Object *ob;
+       bArmature *arm;
+       bPose *pose;
        short done= 0;
        
-       /* sanity checks */
-       if (ELEM3(NULL, ob, pose, arm))
-               return;
-
-       /* get group to affect */
-       if ((active==0) || (pose->active_group <= 0)) {
-               menustr= build_posegroups_menustr(pose, 1);
-               nr= 0; // XXX pupmenu_col(menustr, 20);
-               MEM_freeN(menustr);
-               
-               if (nr < 0) 
-                       return;
-               else if (nr == 0) {
-                       /* add new - note: this does an undo push and sets active group */
-                       pose_add_posegroup(scene);
-               }
-               else
-                       pose->active_group= nr;
-       }
+       /* 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, and a pose there too */
+       if (ELEM(NULL, ob, ob->pose))
+               return OPERATOR_CANCELLED;
+       arm= ob->data;
+       pose= ob->pose;
+       
+       /* set the active group number to the one from operator props 
+        *      - if 0 after this, make a new group...
+        */
+       pose->active_group= RNA_int_get(op->ptr, "type");
+       if (pose->active_group == 0)
+               pose_add_group(ob);
        
        /* add selected bones to group then */
-       for (pchan= pose->chanbase.first; pchan; pchan= pchan->next) {
-               if ((pchan->bone->flag & BONE_SELECTED) && (pchan->bone->layer & arm->layer)) {
-                       pchan->agrp_index= pose->active_group;
-                       done= 1;
-               }
+       // NOTE: unfortunately, we cannot use the context-iterators here, since they might not be defined...
+       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);
        
+       /* report done status */
        if (done)
-               BIF_undo_push("Add Bones To Group");
-               
+               return OPERATOR_FINISHED;
+       else
+               return OPERATOR_CANCELLED;
 }
 
-/* Remove selected pchans from their bone groups */
-void pose_remove_from_posegroups (Scene *scene)
+void POSE_OT_group_assign (wmOperatorType *ot)
 {
-       Object *ob= OBACT;
-       bArmature *arm= (ob) ? ob->data : NULL;
-       bPose *pose= (ob) ? ob->pose : NULL;
+       /* identifiers */
+       ot->name= "Add Selected to Bone Group";
+       ot->idname= "POSE_OT_group_assign";
+       ot->description= "Add selected bones to the chosen bone group";
+       
+       /* api callbacks */
+       ot->invoke= pose_groups_menu_invoke;
+       ot->exec= pose_group_assign_exec;
+       ot->poll= ED_operator_posemode;
+       
+       /* flags */
+       ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
+       
+       /* properties */
+       RNA_def_int(ot->srna, "type", 0, 0, 10, "Bone Group Index", "", 0, INT_MAX);
+}
+
+
+static int pose_group_unassign_exec (bContext *C, wmOperator *op)
+{
+       ScrArea *sa= CTX_wm_area(C);
+       Object *ob;
+       bArmature *arm;
+       bPose *pose;
        bPoseChannel *pchan;
        short done= 0;
        
-       /* sanity checks */
-       if (ELEM3(NULL, ob, pose, arm))
-               return;
+       /* 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, and a pose there too */
+       if (ELEM(NULL, ob, ob->pose))
+               return OPERATOR_CANCELLED;
+       pose= ob->pose;
+       arm= ob->data;
        
-       /* remove selected bones from their groups */
+       /* 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_pose_bones) 
        for (pchan= pose->chanbase.first; pchan; pchan= pchan->next) {
-               if ((pchan->bone->flag & BONE_SELECTED) && (pchan->bone->layer & arm->layer)) {
-                       if (pchan->agrp_index) {
-                               pchan->agrp_index= 0;
-                               done= 1;
+               /* 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) || (pchan->bone == arm->act_bone)) {
+                               if (pchan->agrp_index) {
+                                       pchan->agrp_index= 0;
+                                       done= 1;
+                               }
                        }
                }
        }
        
+       /* notifiers for updates */
+       WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
+       
+       /* report done status */
        if (done)
-               BIF_undo_push("Remove Bones From Groups");
-               
+               return OPERATOR_FINISHED;
+       else
+               return OPERATOR_CANCELLED;
 }
 
-/* Ctrl-G in 3D-View while in PoseMode */
-void pgroup_operation_with_menu (Scene *scene)
+void POSE_OT_group_unassign (wmOperatorType *ot)
 {
-       Object *ob= OBACT;
-       bArmature *arm= (ob) ? ob->data : NULL;
-       bPose *pose= (ob) ? ob->pose : NULL;
-       bPoseChannel *pchan= NULL;
-       int mode;
-       
-       /* sanity checks */
-       if (ELEM3(NULL, ob, pose, arm))
-               return;
+       /* identifiers */
+       ot->name= "Remove Selected from Bone Groups";
+       ot->idname= "POSE_OT_group_unassign";
+       ot->description= "Remove selected bones from all bone groups";
        
-       /* check that something is selected */
-       for (pchan= pose->chanbase.first; pchan; pchan= pchan->next) {
-               if ((pchan->bone->flag & BONE_SELECTED) && (pchan->bone->layer & arm->layer)) 
-                       break;
-       }
-       if (pchan == NULL)
-               return;
+       /* api callbacks */
+       ot->exec= pose_group_unassign_exec;
+       ot->poll= ED_operator_posemode;
        
-       /* get mode of action */
-       if (pchan)
-               mode= pupmenu("Bone Groups%t|Add Selected to Active Group%x1|Add Selected to Group%x2|%|Remove Selected From Groups%x3|Remove Active Group%x4");
-       else
-               mode= pupmenu("Bone Groups%t|Add New Group%x5|Remove Active Group%x4");
-               
-       /* handle mode */
-       switch (mode) {
-               case 1:
-                       pose_assign_to_posegroup(scene, 1);
-                       break;
-               case 2:
-                       pose_assign_to_posegroup(scene, 0);
-                       break;
-               case 5:
-                       pose_add_posegroup(scene);
-                       break;
-               case 3:
-                       pose_remove_from_posegroups(scene);
-                       break;
-               case 4:
-                       pose_remove_posegroup(scene);
-                       break;
-       }
+       /* flags */
+       ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
 }
 
 /* ********************************************** */
 
-static short pose_select_same_group (Object *ob)
+static int pose_flip_names_exec (bContext *C, wmOperator *op)
 {
-       bPose *pose= (ob)? ob->pose : NULL;
-       bArmature *arm= (ob)? ob->data : NULL;
-       bPoseChannel *pchan, *chan;
-       short changed= 0;
+       Object *ob= CTX_data_active_object(C);
+       bArmature *arm;
+       char newname[32];
        
-       if (ELEM3(NULL, ob, pose, arm))
-               return 0;
+       /* paranoia checks */
+       if (ELEM(NULL, ob, ob->pose)) 
+               return OPERATOR_CANCELLED;
+       arm= ob->data;
        
-       /* 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;
-                                               }
-                                       }
-                               }
-                               
-                       }
-               }
+       /* 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);
        }
+       CTX_DATA_END;
        
-       return changed;
-}
+       /* since we renamed stuff... */
+       DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
 
-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;
+       /* note, notifier might evolve */
+       WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
        
-       if (ELEM3(NULL, ob, pose, arm))
-               return 0;
-       
-       /* 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;
-                       }
-               }
-       }
-       
-       return changed;
+       return OPERATOR_FINISHED;
 }
 
-void pose_select_grouped (Scene *scene, short nr)
+void POSE_OT_flip_names (wmOperatorType *ot)
 {
-       short changed = 0;
-       
-       if (nr == 1)            changed= pose_select_same_group(OBACT);
-       else if (nr == 2)       changed= pose_select_same_layer(OBACT);
+       /* identifiers */
+       ot->name= "Flip Names";
+       ot->idname= "POSE_OT_flip_names";
+       ot->description= "Flips (and corrects) the names of selected bones";
        
-       if (changed) {
-               countall();
-               BIF_undo_push("Select Grouped");
-       }
-}
-
-/* Shift-G in 3D-View while in PoseMode */
-void pose_select_grouped_menu (Scene *scene)
-{
-       short nr;
+       /* api callbacks */
+       ot->exec= pose_flip_names_exec;
+       ot->poll= ED_operator_posemode;
        
-       /* here we go */
-       nr= pupmenu("Select Grouped%t|In Same Group%x1|In Same Layer%x2");
-       pose_select_grouped(scene, nr);
+       /* flags */
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 }
 
-/* ********************************************** */
+/* ------------------ */
 
-/* context active object */
-void pose_flip_names(Scene *scene)
+static int pose_autoside_names_exec (bContext *C, wmOperator *op)
 {
-       Object *obedit= scene->obedit; // XXX context
-       Object *ob= OBACT;
-       bArmature *arm= ob->data;
-       bPoseChannel *pchan;
+       Object *ob= CTX_data_active_object(C);
+       bArmature *arm;
        char newname[32];
+       short axis= RNA_enum_get(op->ptr, "axis");
        
        /* paranoia checks */
-       if(!ob && !ob->pose) return;
-       if(ob==obedit || (ob->flag & OB_POSEMODE)==0) return;
-       
-       if(pose_has_protected_selected(ob, 0, 1))
-               return;
+       if (ELEM(NULL, ob, ob->pose)) 
+               return OPERATOR_CANCELLED;
+       arm= ob->data;
        
-       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
-                               armature_bone_rename(ob, pchan->name, newname);
-                       }
-               }
+       /* 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;
        
-       BIF_undo_push("Flip names");
+       /* 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;
 }
 
-/* context active object */
-void pose_autoside_names(Scene *scene, short axis)
+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 (ELEM(NULL, 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_autoside_name(newname, 1, axis, pchan->bone->head[axis], pchan->bone->tail[axis]);
-                               armature_bone_rename(ob, 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, or weightpainted object with armature in posemode */
 void pose_activate_flipped_bone(Scene *scene)
 {
@@ -1391,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
@@ -1427,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;
        
-       /* do autokey */
-       autokeyframe_pose_cb_func(ob, TFM_ROTATION, 0);
+       /* 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;
+       
+       /* flags */
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+}
+
+/* ********************************************** */
+
 /* context: active channel */
 void pose_special_editmenu(Scene *scene)
 {
@@ -1715,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) {
@@ -1771,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");
 }