Merge of itasc branch. Project files, scons and cmake should be working. Makefile...
[blender.git] / source / blender / blenkernel / intern / action.c
index a7427b1b306ecd2b8571e77e2f05a18a9ddb4bb3..4cfd35a494d9725115610352b70924679f10d2b1 100644 (file)
@@ -21,6 +21,7 @@
  * All rights reserved.
  *
  * Contributor(s): Full recode, Ton Roosendaal, Crete 2005
  * All rights reserved.
  *
  * Contributor(s): Full recode, Ton Roosendaal, Crete 2005
+ *                              Full recode, Joshua Leung, 2009
  *
  * ***** END GPL LICENSE BLOCK *****
  */
  *
  * ***** END GPL LICENSE BLOCK *****
  */
@@ -31,7 +32,8 @@
 
 #include <string.h>
 #include <math.h>
 
 #include <string.h>
 #include <math.h>
-#include <stdlib.h>    /* for NULL */
+#include <stdlib.h>
+#include <stddef.h>    
 
 #include "MEM_guardedalloc.h"
 
 
 #include "MEM_guardedalloc.h"
 
@@ -45,6 +47,7 @@
 #include "DNA_object_types.h"
 #include "DNA_scene_types.h"
 
 #include "DNA_object_types.h"
 #include "DNA_scene_types.h"
 
+#include "BKE_animsys.h"
 #include "BKE_action.h"
 #include "BKE_anim.h"
 #include "BKE_armature.h"
 #include "BKE_action.h"
 #include "BKE_anim.h"
 #include "BKE_armature.h"
 #include "BKE_main.h"
 #include "BKE_object.h"
 #include "BKE_utildefines.h"
 #include "BKE_main.h"
 #include "BKE_object.h"
 #include "BKE_utildefines.h"
+#include "BIK_api.h"
 
 #include "BLI_arithb.h"
 #include "BLI_blenlib.h"
 #include "BLI_ghash.h"
 
 
 #include "BLI_arithb.h"
 #include "BLI_blenlib.h"
 #include "BLI_ghash.h"
 
-//XXX #include "nla.h"
+#include "RNA_access.h"
+#include "RNA_types.h"
 
 /* *********************** NOTE ON POSE AND ACTION **********************
 
 
 /* *********************** NOTE ON POSE AND ACTION **********************
 
@@ -81,9 +86,9 @@
 
 /* ***************** Library data level operations on action ************** */
 
 
 /* ***************** Library data level operations on action ************** */
 
-nAction *add_empty_action(const char name[])
+bAction *add_empty_action(const char name[])
 {
 {
-       nAction *act;
+       bAction *act;
        
        act= alloc_libblock(&G.main->action, ID_AC, name);
        act->id.flag |= LIB_FAKEUSER; // XXX this is nasty for new users... maybe we don't want this anymore
        
        act= alloc_libblock(&G.main->action, ID_AC, name);
        act->id.flag |= LIB_FAKEUSER; // XXX this is nasty for new users... maybe we don't want this anymore
@@ -93,10 +98,10 @@ nAction *add_empty_action(const char name[])
 }      
 
 // does copy_fcurve...
 }      
 
 // does copy_fcurve...
-void make_local_action(nAction *act)
+void make_local_action(bAction *act)
 {
        // Object *ob;
 {
        // Object *ob;
-       nAction *actn;
+       bAction *actn;
        int local=0, lib=0;
        
        if (act->id.lib==0) return;
        int local=0, lib=0;
        
        if (act->id.lib==0) return;
@@ -147,19 +152,14 @@ void make_local_action(nAction *act)
 }
 
 
 }
 
 
-void free_action (nAction *act)
+void free_action (bAction *act)
 {
 {
-       FCurve *fcu;
-       
        /* sanity check */
        if (act == NULL)
                return;
        
        /* Free F-Curves */
        /* sanity check */
        if (act == NULL)
                return;
        
        /* Free F-Curves */
-       while ((fcu= act->curves.first)) {
-               BLI_remlink(&act->curves, fcu);
-               free_fcurve(fcu);
-       }
+       free_fcurves(&act->curves);
        
        /* Free groups */
        if (act->groups.first)
        
        /* Free groups */
        if (act->groups.first)
@@ -170,39 +170,42 @@ void free_action (nAction *act)
                BLI_freelistN(&act->markers);
 }
 
                BLI_freelistN(&act->markers);
 }
 
-nAction *copy_action (nAction *src)
+bAction *copy_action (bAction *src)
 {
 {
-       nAction *dst = NULL;
-       //bActionChannel *dchan, *schan;
-       // bActionGroup *dgrp, *sgrp;
-       
-       if (!src) return NULL;
+       bAction *dst = NULL;
+       bActionGroup *dgrp, *sgrp;
+       FCurve *dfcu, *sfcu;
        
        
+       if (src == NULL) 
+               return NULL;
        dst= copy_libblock(src);
        
        dst= copy_libblock(src);
        
-       BLI_duplicatelist(&(dst->chanbase), &(src->chanbase));
-       BLI_duplicatelist(&(dst->groups), &(src->groups));
-       BLI_duplicatelist(&(dst->markers), &(src->markers));
+       /* duplicate the lists of groups and markers */
+       BLI_duplicatelist(&dst->groups, &src->groups);
+       BLI_duplicatelist(&dst->markers, &src->markers);
        
        
-#if 0  // XXX old animation system
-       for (dchan=dst->chanbase.first, schan=src->chanbase.first; dchan; dchan=dchan->next, schan=schan->next) {
+       /* copy F-Curves, fixing up the links as we go */
+       dst->curves.first= dst->curves.last= NULL;
+       
+       for (sfcu= src->curves.first; sfcu; sfcu= sfcu->next) {
+               /* duplicate F-Curve */
+               dfcu= copy_fcurve(sfcu);
+               BLI_addtail(&dst->curves, dfcu);
+               
+               /* fix group links (kindof bad list-in-list search, but this is the most reliable way) */
                for (dgrp=dst->groups.first, sgrp=src->groups.first; dgrp && sgrp; dgrp=dgrp->next, sgrp=sgrp->next) {
                for (dgrp=dst->groups.first, sgrp=src->groups.first; dgrp && sgrp; dgrp=dgrp->next, sgrp=sgrp->next) {
-                       if (dchan->grp == sgrp) {
-                               dchan->grp= dgrp;
+                       if (sfcu->grp == sgrp) {
+                               dfcu->grp= dgrp;
                                
                                
-                               if (dgrp->channels.first == schan)
-                                       dgrp->channels.first= dchan;
-                               if (dgrp->channels.last == schan)
-                                       dgrp->channels.last= dchan;
+                               if (dgrp->channels.first == sfcu)
+                                       dgrp->channels.first= dfcu;
+                               if (dgrp->channels.last == sfcu)
+                                       dgrp->channels.last= dfcu;
                                        
                                break;
                        }
                }
                                        
                                break;
                        }
                }
-               
-               dchan->ipo = copy_ipo(dchan->ipo);
-               copy_constraint_channels(&dchan->constraintChannels, &schan->constraintChannels);
        }
        }
-#endif // XXX old animation system
        
        dst->id.flag |= LIB_FAKEUSER; // XXX this is nasty for new users... maybe we don't want this anymore
        dst->id.us++;
        
        dst->id.flag |= LIB_FAKEUSER; // XXX this is nasty for new users... maybe we don't want this anymore
        dst->id.us++;
@@ -210,21 +213,214 @@ nAction *copy_action (nAction *src)
        return dst;
 }
 
        return dst;
 }
 
+/* *************** Action Groups *************** */
+
+/* Get the active action-group for an Action */
+bActionGroup *get_active_actiongroup (bAction *act)
+{
+       bActionGroup *agrp= NULL;
+       
+       if (act && act->groups.first) { 
+               for (agrp= act->groups.first; agrp; agrp= agrp->next) {
+                       if (agrp->flag & AGRP_ACTIVE)
+                               break;
+               }
+       }
+       
+       return agrp;
+}
+
+/* Make the given Action-Group the active one */
+void set_active_action_group (bAction *act, bActionGroup *agrp, short select)
+{
+       bActionGroup *grp;
+       
+       /* sanity checks */
+       if (act == NULL)
+               return;
+       
+       /* Deactive all others */
+       for (grp= act->groups.first; grp; grp= grp->next) {
+               if ((grp==agrp) && (select))
+                       grp->flag |= AGRP_ACTIVE;
+               else    
+                       grp->flag &= ~AGRP_ACTIVE;
+       }
+}
+
+/* Add given channel into (active) group 
+ *     - assumes that channel is not linked to anything anymore
+ *     - always adds at the end of the group 
+ */
+void action_groups_add_channel (bAction *act, bActionGroup *agrp, FCurve *fcurve)
+{
+       FCurve *fcu;
+       short done=0;
+       
+       /* sanity checks */
+       if (ELEM3(NULL, act, agrp, fcurve))
+               return;
+       
+       /* if no channels, just add to two lists at the same time */
+       if (act->curves.first == NULL) {
+               fcurve->next = fcurve->prev = NULL;
+               
+               agrp->channels.first = agrp->channels.last = fcurve;
+               act->curves.first = act->curves.last = fcurve;
+               
+               fcurve->grp= agrp;
+               return;
+       }
+       
+       /* try to find a channel to slot this in before/after */
+       for (fcu= act->curves.first; fcu; fcu= fcu->next) {
+               /* if channel has no group, then we have ungrouped channels, which should always occur after groups */
+               if (fcu->grp == NULL) {
+                       BLI_insertlinkbefore(&act->curves, fcu, fcurve);
+                       
+                       if (agrp->channels.first == NULL)
+                               agrp->channels.first= fcurve;
+                       agrp->channels.last= fcurve;
+                       
+                       done= 1;
+                       break;
+               }
+               
+               /* if channel has group after current, we can now insert (otherwise we have gone too far) */
+               else if (fcu->grp == agrp->next) {
+                       BLI_insertlinkbefore(&act->curves, fcu, fcurve);
+                       
+                       if (agrp->channels.first == NULL)
+                               agrp->channels.first= fcurve;
+                       agrp->channels.last= fcurve;
+                       
+                       done= 1;
+                       break;
+               }
+               
+               /* if channel has group we're targeting, check whether it is the last one of these */
+               else if (fcu->grp == agrp) {
+                       if ((fcu->next) && (fcu->next->grp != agrp)) {
+                               BLI_insertlinkafter(&act->curves, fcu, fcurve);
+                               agrp->channels.last= fcurve;
+                               done= 1;
+                               break;
+                       }
+                       else if (fcu->next == NULL) {
+                               BLI_addtail(&act->curves, fcurve);
+                               agrp->channels.last= fcurve;
+                               done= 1;
+                               break;
+                       }
+               }
+               
+               /* if channel has group before target, check whether the next one is something after target */
+               else if (fcu->grp == agrp->prev) {
+                       if (fcu->next) {
+                               if ((fcu->next->grp != fcu->grp) && (fcu->next->grp != agrp)) {
+                                       BLI_insertlinkafter(&act->curves, fcu, fcurve);
+                                       
+                                       agrp->channels.first= fcurve;
+                                       agrp->channels.last= fcurve;
+                                       
+                                       done= 1;
+                                       break;
+                               }
+                       }
+                       else {
+                               BLI_insertlinkafter(&act->curves, fcu, fcurve);
+                               
+                               agrp->channels.first= fcurve;
+                               agrp->channels.last= fcurve;
+                               
+                               done= 1;
+                               break;
+                       }
+               }
+       }
+       
+       /* only if added, set channel as belonging to this group */
+       if (done) {
+               //printf("FCurve added to group \n");
+               fcurve->grp= agrp;
+       }
+       else {
+               printf("Error: FCurve '%s' couldn't be added to Group '%s' \n", fcurve->rna_path, agrp->name);
+               BLI_addtail(&act->curves, fcurve);
+       }
+}      
+
+/* Remove the given channel from all groups */
+void action_groups_remove_channel (bAction *act, FCurve *fcu)
+{
+       /* sanity checks */
+       if (ELEM(NULL, act, fcu))       
+               return;
+       
+       /* check if any group used this directly */
+       if (fcu->grp) {
+               bActionGroup *agrp= fcu->grp;
+               
+               if (agrp->channels.first == agrp->channels.last) {
+                       if (agrp->channels.first == fcu) {
+                               agrp->channels.first= NULL;
+                               agrp->channels.last= NULL;
+                       }
+               }
+               else if (agrp->channels.first == fcu) {
+                       if ((fcu->next) && (fcu->next->grp==agrp))
+                               agrp->channels.first= fcu->next;
+                       else
+                               agrp->channels.first= NULL;
+               }
+               else if (agrp->channels.last == fcu) {
+                       if ((fcu->prev) && (fcu->prev->grp==agrp))
+                               agrp->channels.last= fcu->prev;
+                       else
+                               agrp->channels.last= NULL;
+               }
+               
+               fcu->grp= NULL;
+       }
+       
+       /* now just remove from list */
+       BLI_remlink(&act->curves, fcu);
+}
 
 
+/* Find a group with the given name */
+bActionGroup *action_groups_find_named (bAction *act, const char name[])
+{
+       bActionGroup *grp;
+       
+       /* sanity checks */
+       if (ELEM3(NULL, act, act->groups.first, name) || (name[0] == 0))
+               return NULL;
+               
+       /* do string comparisons */
+       for (grp= act->groups.first; grp; grp= grp->next) {
+               if (strcmp(grp->name, name) == 0)
+                       return grp;
+       }
+       
+       /* not found */
+       return NULL;
+}
 
 
-/* ************************ Pose channels *************** */
+/* *************** Pose channels *************** */
 
 /* usually used within a loop, so we got a N^2 slowdown */
 bPoseChannel *get_pose_channel(const bPose *pose, const char *name)
 {
        bPoseChannel *chan;
 
 /* usually used within a loop, so we got a N^2 slowdown */
 bPoseChannel *get_pose_channel(const bPose *pose, const char *name)
 {
        bPoseChannel *chan;
-
-       if(pose==NULL) return NULL;
+       
+       if (ELEM(NULL, pose, name) || (name[0] == 0))
+               return NULL;
        
        for (chan=pose->chanbase.first; chan; chan=chan->next) {
        
        for (chan=pose->chanbase.first; chan; chan=chan->next) {
-               if(chan->name[0] == name[0])
+               if (chan->name[0] == name[0]) {
                        if (!strcmp (chan->name, name))
                                return chan;
                        if (!strcmp (chan->name, name))
                                return chan;
+               }
        }
 
        return NULL;
        }
 
        return NULL;
@@ -236,11 +432,10 @@ bPoseChannel *verify_pose_channel(bPose* pose, const char* name)
 {
        bPoseChannel *chan;
        
 {
        bPoseChannel *chan;
        
-       if (!pose) {
+       if (pose == NULL)
                return NULL;
                return NULL;
-       }
        
        
-       /*      See if this channel exists */
+       /* See if this channel exists */
        for (chan=pose->chanbase.first; chan; chan=chan->next) {
                if (!strcmp (name, chan->name))
                        return chan;
        for (chan=pose->chanbase.first; chan; chan=chan->next) {
                if (!strcmp (name, chan->name))
                        return chan;
@@ -249,15 +444,15 @@ bPoseChannel *verify_pose_channel(bPose* pose, const char* name)
        /* If not, create it and add it */
        chan = MEM_callocN(sizeof(bPoseChannel), "verifyPoseChannel");
        
        /* If not, create it and add it */
        chan = MEM_callocN(sizeof(bPoseChannel), "verifyPoseChannel");
        
-       strncpy (chan->name, name, 31);
+       strncpy(chan->name, name, 31);
        /* init vars to prevent math errors */
        /* init vars to prevent math errors */
-       chan->quat[0] = 1.0F;
-       chan->size[0] = chan->size[1] = chan->size[2] = 1.0F;
+       chan->quat[0] = 1.0f;
+       chan->size[0] = chan->size[1] = chan->size[2] = 1.0f;
        
        chan->limitmin[0]= chan->limitmin[1]= chan->limitmin[2]= -180.0f;
        chan->limitmax[0]= chan->limitmax[1]= chan->limitmax[2]= 180.0f;
        chan->stiffness[0]= chan->stiffness[1]= chan->stiffness[2]= 0.0f;
        
        chan->limitmin[0]= chan->limitmin[1]= chan->limitmin[2]= -180.0f;
        chan->limitmax[0]= chan->limitmax[1]= chan->limitmax[2]= 180.0f;
        chan->stiffness[0]= chan->stiffness[1]= chan->stiffness[2]= 0.0f;
-       
+       chan->ikrotweight = chan->iklinweight = 0.0f;
        Mat4One(chan->constinv);
        
        BLI_addtail(&pose->chanbase, chan);
        Mat4One(chan->constinv);
        
        BLI_addtail(&pose->chanbase, chan);
@@ -283,9 +478,20 @@ bPoseChannel *get_active_posechannel (Object *ob)
        return NULL;
 }
 
        return NULL;
 }
 
-
+const char *get_ikparam_name(bPose *pose)
+{
+       if (pose) {
+               switch (pose->iksolver) {
+               case IKSOLVER_LEGACY:
+                       return NULL;
+               case IKSOLVER_ITASC:
+                       return "bItasc";
+               }
+       }
+       return NULL;
+}
 /* dst should be freed already, makes entire duplicate */
 /* dst should be freed already, makes entire duplicate */
-void copy_pose(bPose **dst, bPose *src, int copycon)
+void copy_pose (bPose **dst, bPose *src, int copycon)
 {
        bPose *outPose;
        bPoseChannel    *pchan;
 {
        bPose *outPose;
        bPoseChannel    *pchan;
@@ -305,7 +511,10 @@ void copy_pose(bPose **dst, bPose *src, int copycon)
        outPose= MEM_callocN(sizeof(bPose), "pose");
        
        BLI_duplicatelist(&outPose->chanbase, &src->chanbase);
        outPose= MEM_callocN(sizeof(bPose), "pose");
        
        BLI_duplicatelist(&outPose->chanbase, &src->chanbase);
-       
+       outPose->iksolver = src->iksolver;
+       outPose->ikdata = NULL;
+       outPose->ikparam = MEM_dupallocN(src->ikparam);
+
        if (copycon) {
                for (pchan=outPose->chanbase.first; pchan; pchan=pchan->next) {
                        copy_constraints(&listb, &pchan->constraints);  // copy_constraints NULLs listb
        if (copycon) {
                for (pchan=outPose->chanbase.first; pchan; pchan=pchan->next) {
                        copy_constraints(&listb, &pchan->constraints);  // copy_constraints NULLs listb
@@ -317,6 +526,39 @@ void copy_pose(bPose **dst, bPose *src, int copycon)
        *dst=outPose;
 }
 
        *dst=outPose;
 }
 
+void init_pose_itasc(bItasc *itasc)
+{
+       if (itasc) {
+               itasc->iksolver = IKSOLVER_ITASC;
+               itasc->minstep = 0.01f;
+               itasc->maxstep = 0.06f;
+               itasc->numiter = 100;
+               itasc->numstep = 4;
+               itasc->precision = 0.005f;
+               itasc->flag = ITASC_AUTO_STEP|ITASC_INITIAL_REITERATION|ITASC_SIMULATION;
+               itasc->feedback = 20.f;
+               itasc->maxvel = 50.f;
+               itasc->solver = ITASC_SOLVER_SDLS;
+               itasc->dampmax = 0.5;
+               itasc->dampeps = 0.15;
+       }
+}
+void init_pose_ikparam(bPose *pose)
+{
+       bItasc *itasc;
+       switch (pose->iksolver) {
+       case IKSOLVER_ITASC:
+               itasc = MEM_callocN(sizeof(bItasc), "itasc");
+               init_pose_itasc(itasc);
+               pose->ikparam = itasc;
+               break;
+       case IKSOLVER_LEGACY:
+       default:
+               pose->ikparam = NULL;
+               break;
+       }
+}
+
 void free_pose_channels(bPose *pose) 
 {
        bPoseChannel *pchan;
 void free_pose_channels(bPose *pose) 
 {
        bPoseChannel *pchan;
@@ -340,62 +582,15 @@ void free_pose(bPose *pose)
                /* free pose-groups */
                if (pose->agroups.first)
                        BLI_freelistN(&pose->agroups);
                /* free pose-groups */
                if (pose->agroups.first)
                        BLI_freelistN(&pose->agroups);
-               
-               /* free pose */
-               MEM_freeN(pose);
-       }
-}
-
-void game_copy_pose(bPose **dst, bPose *src)
-{
-       bPose *out;
-       bPoseChannel *pchan, *outpchan;
-       GHash *ghash;
-       
-       /* the game engine copies the current armature pose and then swaps
-        * the object pose pointer. this makes it possible to change poses
-        * without affecting the original blender data. */
 
 
-       if (!src) {
-               *dst=NULL;
-               return;
-       }
-       else if (*dst==src) {
-               printf("copy_pose source and target are the same\n");
-               *dst=NULL;
-               return;
-       }
-       
-       out= MEM_dupallocN(src);
-       out->agroups.first= out->agroups.last= NULL;
-       BLI_duplicatelist(&out->chanbase, &src->chanbase);
-
-       /* remap pointers */
-       ghash= BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp);
+               /* free IK solver state */
+               BIK_clear_data(pose);
 
 
-       pchan= src->chanbase.first;
-       outpchan= out->chanbase.first;
-       for (; pchan; pchan=pchan->next, outpchan=outpchan->next)
-               BLI_ghash_insert(ghash, pchan, outpchan);
-
-       for (pchan=out->chanbase.first; pchan; pchan=pchan->next) {
-               pchan->parent= BLI_ghash_lookup(ghash, pchan->parent);
-               pchan->child= BLI_ghash_lookup(ghash, pchan->child);
-               pchan->path= NULL;
-       }
+               /* free IK solver param */
+               if (pose->ikparam)
+                       MEM_freeN(pose->ikparam);
 
 
-       BLI_ghash_free(ghash, NULL, NULL);
-       
-       *dst=out;
-}
-
-void game_free_pose(bPose *pose)
-{
-       if (pose) {
-               /* we don't free constraints, those are owned by the original pose */
-               if(pose->chanbase.first)
-                       BLI_freelistN(&pose->chanbase);
-               
+               /* free pose */
                MEM_freeN(pose);
        }
 }
                MEM_freeN(pose);
        }
 }
@@ -502,110 +697,146 @@ void framechange_poses_clear_unkeyed(void)
        }
 }
 
        }
 }
 
-/* ************************ END Pose channels *************** */
-
-/* ************** time ****************** */
+/* ************************** Bone Groups ************************** */
 
 
-static bActionStrip *get_active_strip(Object *ob)
+/* Adds a new bone-group */
+void pose_add_group (Object *ob)
 {
 {
-#if 0  // XXX old animation system
-       bActionStrip *strip;
+       bPose *pose= (ob) ? ob->pose : NULL;
+       bActionGroup *grp;
        
        
-       if(ob->action==NULL)
-               return NULL;
-               
-       for (strip=ob->nlastrips.first; strip; strip=strip->next)
-               if(strip->flag & ACTSTRIP_ACTIVE)
-                       break;
+       if (ELEM(NULL, ob, ob->pose))
+               return;
        
        
-       if(strip && strip->act==ob->action)
-               return strip;
-#endif // XXX old animation system
-               
-       return NULL;
+       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);
+       
+       pose->active_group= BLI_countlist(&pose->agroups);
 }
 
 }
 
-/* non clipped mapping of strip */
-static float get_actionstrip_frame(bActionStrip *strip, float cframe, int invert)
+/* Remove the active bone-group */
+void pose_remove_group (Object *ob)
 {
 {
-       float length, actlength, repeat, scale;
-       
-       if (strip->repeat == 0.0f) strip->repeat = 1.0f;
-       repeat = (strip->flag & ACTSTRIP_USESTRIDE) ? (1.0f) : (strip->repeat);
-       
-       if (strip->scale == 0.0f) strip->scale= 1.0f;
-       scale = fabs(strip->scale); /* scale must be positive (for now) */
+       bPose *pose= (ob) ? ob->pose : NULL;
+       bActionGroup *grp = NULL;
+       bPoseChannel *pchan;
        
        
-       actlength = strip->actend-strip->actstart;
-       if (actlength == 0.0f) actlength = 1.0f;
-       length = repeat * scale * actlength;
+       /* sanity checks */
+       if (ELEM(NULL, ob, pose))
+               return;
+       if (pose->active_group <= 0)
+               return;
        
        
-       /* invert = convert action-strip time to global time */
-       if (invert)
-               return length*(cframe - strip->actstart)/(repeat*actlength) + strip->start;
-       else
-               return repeat*actlength*(cframe - strip->start)/length + strip->actstart;
+       /* 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;
+       }
 }
 
 }
 
-/* if the conditions match, it converts current time to strip time */
-float get_action_frame(Object *ob, float cframe)
-{
-       bActionStrip *strip= get_active_strip(ob);
-       
-       if(strip)
-               return get_actionstrip_frame(strip, cframe, 0);
-       return cframe;
-}
+/* ************** F-Curve Utilities for Actions ****************** */
 
 
-/* inverted, strip time to current time */
-float get_action_frame_inv(Object *ob, float cframe)
+/* Check if the given action has any keyframes */
+short action_has_motion(const bAction *act)
 {
 {
-       bActionStrip *strip= get_active_strip(ob);
+       FCurve *fcu;
        
        
-       if(strip)
-               return get_actionstrip_frame(strip, cframe, 1);
-       return cframe;
+       /* return on the first F-Curve that has some keyframes/samples defined */
+       if (act) {
+               for (fcu= act->curves.first; fcu; fcu= fcu->next) {
+                       if (fcu->totvert)
+                               return 1;
+               }
+       }
+       
+       /* nothing found */
+       return 0;
 }
 
 }
 
-
-
-
 /* Calculate the extents of given action */
 /* Calculate the extents of given action */
-void calc_action_range(const bAction *act, float *start, float *end, int incl_hidden)
+void calc_action_range(const bAction *act, float *start, float *end, short incl_modifiers)
 {
 {
-       // FCurve *fcu;
+       FCurve *fcu;
        float min=999999999.0f, max=-999999999.0f;
        float min=999999999.0f, max=-999999999.0f;
-       int     foundvert=0;
+       short foundvert=0, foundmod=0;
 
        if (act) {
 
        if (act) {
-#if 0 // XXX old animation system
-               for (chan=act->chanbase.first; chan; chan=chan->next) {
-                       if ((incl_hidden) || (chan->flag & ACHAN_HIDDEN)==0) {
-                               if (chan->ipo) {
-                                       for (icu=chan->ipo->curve.first; icu; icu=icu->next) {
-                                               if (icu->totvert) {
-                                                       min= MIN2(min, icu->bezt[0].vec[1][0]);
-                                                       max= MAX2(max, icu->bezt[icu->totvert-1].vec[1][0]);
-                                                       foundvert=1;
+               for (fcu= act->curves.first; fcu; fcu= fcu->next) {
+                       /* if curve has keyframes, consider them first */
+                       if (fcu->totvert) {
+                               float nmin, nmax;
+                               
+                               /* get extents for this curve */
+                               calc_fcurve_range(fcu, &nmin, &nmax);
+                               
+                               /* compare to the running tally */
+                               min= MIN2(min, nmin);
+                               max= MAX2(max, nmax);
+                               
+                               foundvert= 1;
+                       }
+                       
+                       /* if incl_modifiers is enabled, need to consider modifiers too
+                        *      - only really care about the last modifier
+                        */
+                       if ((incl_modifiers) && (fcu->modifiers.last)) {
+                               FModifier *fcm= fcu->modifiers.last;
+                               
+                               /* only use the maximum sensible limits of the modifiers if they are more extreme */
+                               switch (fcm->type) {
+                                       case FMODIFIER_TYPE_LIMITS: /* Limits F-Modifier */
+                                       {
+                                               FMod_Limits *fmd= (FMod_Limits *)fcm->data;
+                                               
+                                               if (fmd->flag & FCM_LIMIT_XMIN) {
+                                                       min= MIN2(min, fmd->rect.xmin);
                                                }
                                                }
-                                       }
-                               }
-                               for (conchan=chan->constraintChannels.first; conchan; conchan=conchan->next) {
-                                       if (conchan->ipo) {
-                                               for (icu=conchan->ipo->curve.first; icu; icu=icu->next) {
-                                                       if (icu->totvert) {
-                                                               min= MIN2(min, icu->bezt[0].vec[1][0]);
-                                                               max= MAX2(max, icu->bezt[icu->totvert-1].vec[1][0]);
-                                                               foundvert=1;
-                                                       }
+                                               if (fmd->flag & FCM_LIMIT_XMAX) {
+                                                       max= MAX2(max, fmd->rect.xmax);
                                                }
                                        }
                                                }
                                        }
+                                               break;
+                                               
+                                       case FMODIFIER_TYPE_CYCLES: /* Cycles F-Modifier */
+                                       {
+                                               FMod_Cycles *fmd= (FMod_Cycles *)fcm->data;
+                                               
+                                               if (fmd->before_mode != FCM_EXTRAPOLATE_NONE)
+                                                       min= MINAFRAMEF;
+                                               if (fmd->after_mode != FCM_EXTRAPOLATE_NONE)
+                                                       max= MAXFRAMEF;
+                                       }
+                                               break;
+                                               
+                                       // TODO: function modifier may need some special limits
+                                               
+                                       default: /* all other standard modifiers are on the infinite range... */
+                                               min= MINAFRAMEF;
+                                               max= MAXFRAMEF;
+                                               break;
                                }
                                }
+                               
+                               foundmod= 1;
                        }
                }
                        }
                }
-#endif // XXX old animation system
        }       
        }       
-       if (foundvert) {
+       
+       if (foundvert || foundmod) {
                if(min==max) max+= 1.0f;
                *start= min;
                *end= max;
                if(min==max) max+= 1.0f;
                *start= min;
                *end= max;
@@ -616,9 +847,102 @@ void calc_action_range(const bAction *act, float *start, float *end, int incl_hi
        }
 }
 
        }
 }
 
+/* Return flags indicating which transforms the given object/posechannel has 
+ *     - if 'curves' is provided, a list of links to these curves are also returned
+ */
+short action_get_item_transforms (bAction *act, Object *ob, bPoseChannel *pchan, ListBase *curves)
+{
+       PointerRNA ptr;
+       FCurve *fcu;
+       char *basePath=NULL;
+       short flags=0;
+       
+       /* build PointerRNA from provided data to obtain the paths to use */
+       if (pchan)
+               RNA_pointer_create((ID *)ob, &RNA_PoseChannel, pchan, &ptr);
+       else if (ob)
+               RNA_id_pointer_create((ID *)ob, &ptr);
+       else    
+               return 0;
+               
+       /* get the basic path to the properties of interest */
+       basePath= RNA_path_from_ID_to_struct(&ptr);
+       if (basePath == NULL)
+               return 0;
+               
+       /* search F-Curves for the given properties 
+        *      - we cannot use the groups, since they may not be grouped in that way...
+        */
+       for (fcu= act->curves.first; fcu; fcu= fcu->next) {
+               char *bPtr=NULL, *pPtr=NULL;
+               
+               /* if enough flags have been found, we can stop checking unless we're also getting the curves */
+               if ((flags == ACT_TRANS_ALL) && (curves == NULL))
+                       break;
+                       
+               /* just in case... */
+               if (fcu->rna_path == NULL)
+                       continue;
+               
+               /* step 1: check for matching base path */
+               bPtr= strstr(fcu->rna_path, basePath);
+               
+               if (bPtr) {
+                       /* step 2: check for some property with transforms 
+                        *      - to speed things up, only check for the ones not yet found 
+                        *        unless we're getting the curves too
+                        *      - if we're getting the curves, the BLI_genericNodeN() creates a LinkData
+                        *        node wrapping the F-Curve, which then gets added to the list
+                        *      - once a match has been found, the curve cannot possibly be any other one
+                        */
+                       if ((curves) || (flags & ACT_TRANS_LOC) == 0) {
+                               pPtr= strstr(fcu->rna_path, "location");
+                               if ((pPtr) && (pPtr >= bPtr)) {
+                                       flags |= ACT_TRANS_LOC;
+                                       
+                                       if (curves) 
+                                               BLI_addtail(curves, BLI_genericNodeN(fcu));
+                                       continue;
+                               }
+                       }
+                       
+                       if ((curves) || (flags & ACT_TRANS_SCALE) == 0) {
+                               pPtr= strstr(fcu->rna_path, "scale");
+                               if ((pPtr) && (pPtr >= bPtr)) {
+                                       flags |= ACT_TRANS_SCALE;
+                                       
+                                       if (curves) 
+                                               BLI_addtail(curves, BLI_genericNodeN(fcu));
+                                       continue;
+                               }
+                       }
+                       
+                       if ((curves) || (flags & ACT_TRANS_ROT) == 0) {
+                               pPtr= strstr(fcu->rna_path, "rotation");
+                               if ((pPtr) && (pPtr >= bPtr)) {
+                                       flags |= ACT_TRANS_ROT;
+                                       
+                                       if (curves) 
+                                               BLI_addtail(curves, BLI_genericNodeN(fcu));
+                                       continue;
+                               }
+                       }
+               }
+       }
+       
+       /* free basePath */
+       MEM_freeN(basePath);
+       
+       /* return flags found */
+       return flags;
+}
+
+/* ************** Pose Management Tools ****************** */
+
 /* Copy the data from the action-pose (src) into the pose */
 /* both args are assumed to be valid */
 /* exported to game engine */
 /* Copy the data from the action-pose (src) into the pose */
 /* both args are assumed to be valid */
 /* exported to game engine */
+/* Note! this assumes both poses are aligned, this isnt always true when dealing with user poses */
 void extract_pose_from_pose(bPose *pose, const bPose *src)
 {
        const bPoseChannel *schan;
 void extract_pose_from_pose(bPose *pose, const bPose *src)
 {
        const bPoseChannel *schan;
@@ -629,51 +953,11 @@ void extract_pose_from_pose(bPose *pose, const bPose *src)
                return;
        }
 
                return;
        }
 
-       for (schan=src->chanbase.first; schan; schan=schan->next, pchan= pchan->next) {
+       for (schan=src->chanbase.first; (schan && pchan); schan=schan->next, pchan= pchan->next) {
                copy_pose_channel_data(pchan, schan);
        }
 }
 
                copy_pose_channel_data(pchan, schan);
        }
 }
 
-/* Pose should exist, can have any number of channels too (used for constraint) */
-void extract_pose_from_action(bPose *pose, bAction *act, float ctime) 
-{
-#if 0  // XXX old animation system
-       bActionChannel *achan;
-       bPoseChannel    *pchan;
-       Ipo                             *ipo;
-
-       if (!act)
-               return;
-       if (!pose)
-               return;
-       
-       /* Copy the data from the action into the pose */
-       for (pchan= pose->chanbase.first; pchan; pchan=pchan->next) {
-               /* skip this pose channel if it has been tagged as having unkeyed poses */
-               if ((pchan->bone) && (pchan->bone->flag & BONE_UNKEYED)) 
-                       continue;
-               
-               /* get action channel and clear pchan-transform flags */
-               achan= get_action_channel(act, pchan->name);
-               pchan->flag &= ~(POSE_LOC|POSE_ROT|POSE_SIZE);
-               
-               if (achan) {
-                       ipo = achan->ipo;
-                       if (ipo) {
-                               /* Evaluates and sets the internal ipo value */
-                               calc_ipo(ipo, ctime);
-                               /* This call also sets the pchan flags */
-                               execute_action_ipo(achan, pchan);
-                       }
-                       /* 0 = do all ipos, not only drivers */
-                       do_constraint_channels(&pchan->constraints, &achan->constraintChannels, ctime, 0);
-               }
-       }
-#endif // XXX old animation system
-       
-       pose->ctime= ctime;     /* used for cyclic offset matching */
-}
-
 /* for do_all_pose_actions, clears the pose. Now also exported for proxy and tools */
 void rest_pose(bPose *pose)
 {
 /* for do_all_pose_actions, clears the pose. Now also exported for proxy and tools */
 void rest_pose(bPose *pose)
 {
@@ -720,9 +1004,11 @@ void copy_pose_result(bPose *to, bPose *from)
                if(pchanto) {
                        Mat4CpyMat4(pchanto->pose_mat, pchanfrom->pose_mat);
                        Mat4CpyMat4(pchanto->chan_mat, pchanfrom->chan_mat);
                if(pchanto) {
                        Mat4CpyMat4(pchanto->pose_mat, pchanfrom->pose_mat);
                        Mat4CpyMat4(pchanto->chan_mat, pchanfrom->chan_mat);
+                       
                        /* used for local constraints */
                        VECCOPY(pchanto->loc, pchanfrom->loc);
                        QUATCOPY(pchanto->quat, pchanfrom->quat);
                        /* used for local constraints */
                        VECCOPY(pchanto->loc, pchanfrom->loc);
                        QUATCOPY(pchanto->quat, pchanfrom->quat);
+                       VECCOPY(pchanto->eul, pchanfrom->eul);
                        VECCOPY(pchanto->size, pchanfrom->size);
                        
                        VECCOPY(pchanto->pose_head, pchanfrom->pose_head);
                        VECCOPY(pchanto->size, pchanfrom->size);
                        
                        VECCOPY(pchanto->pose_head, pchanfrom->pose_head);
@@ -732,6 +1018,65 @@ void copy_pose_result(bPose *to, bPose *from)
        }
 }
 
        }
 }
 
+/* For the calculation of the effects of an Action at the given frame on an object 
+ * This is currently only used for the Action Constraint 
+ */
+void what_does_obaction (Scene *scene, Object *ob, Object *workob, bPose *pose, bAction *act, char groupname[], float cframe)
+{
+       bActionGroup *agrp= action_groups_find_named(act, groupname);
+       
+       /* clear workob */
+       clear_workob(workob);
+       
+       /* init workob */
+       Mat4CpyMat4(workob->obmat, ob->obmat);
+       Mat4CpyMat4(workob->parentinv, ob->parentinv);
+       Mat4CpyMat4(workob->constinv, ob->constinv);
+       workob->parent= ob->parent;
+       workob->track= ob->track;
+
+       workob->trackflag= ob->trackflag;
+       workob->upflag= ob->upflag;
+       
+       workob->partype= ob->partype;
+       workob->par1= ob->par1;
+       workob->par2= ob->par2;
+       workob->par3= ob->par3;
+
+       workob->constraints.first = ob->constraints.first;
+       workob->constraints.last = ob->constraints.last;
+       
+       workob->pose= pose;     /* need to set pose too, since this is used for both types of Action Constraint */
+
+       strcpy(workob->parsubstr, ob->parsubstr);
+       strcpy(workob->id.name, "OB<ConstrWorkOb>"); /* we don't use real object name, otherwise RNA screws with the real thing */
+       
+       /* if we're given a group to use, it's likely to be more efficient (though a bit more dangerous) */
+       if (agrp) {
+               /* specifically evaluate this group only */
+               PointerRNA id_ptr;
+               
+               /* get RNA-pointer for the workob's ID */
+               RNA_id_pointer_create(&workob->id, &id_ptr);
+               
+               /* execute action for this group only */
+               animsys_evaluate_action_group(&id_ptr, act, agrp, NULL, cframe);
+       }
+       else {
+               AnimData adt;
+               
+               /* init animdata, and attach to workob */
+               memset(&adt, 0, sizeof(AnimData));
+               workob->adt= &adt;
+               
+               adt.recalc= ADT_RECALC_ANIM;
+               adt.action= act;
+               
+               /* execute effects of Action on to workob (or it's PoseChannels) */
+               BKE_animsys_evaluate_animdata(&workob->id, &adt, cframe, ADT_RECALC_ANIM);
+       }
+}
+
 /* ********** NLA with non-poses works with ipo channels ********** */
 
 #if 0 // XXX OLD ANIMATION SYSTEM (TO BE REMOVED)
 /* ********** NLA with non-poses works with ipo channels ********** */
 
 #if 0 // XXX OLD ANIMATION SYSTEM (TO BE REMOVED)
@@ -836,75 +1181,6 @@ static void blend_pose_offset_bone(bActionStrip *strip, bPose *dst, bPose *src,
        VecAddf(dst->cyclic_offset, dst->cyclic_offset, src->cyclic_offset);
 }
 
        VecAddf(dst->cyclic_offset, dst->cyclic_offset, src->cyclic_offset);
 }
 
-
-/* Only allowed for Poses with identical channels */
-void blend_poses(bPose *dst, bPose *src, float srcweight, short mode)
-{
-       bPoseChannel *dchan;
-       const bPoseChannel *schan;
-       bConstraint *dcon, *scon;
-       float dstweight;
-       int i;
-       
-       switch (mode){
-       case ACTSTRIPMODE_BLEND:
-               dstweight = 1.0F - srcweight;
-               break;
-       case ACTSTRIPMODE_ADD:
-               dstweight = 1.0F;
-               break;
-       default :
-               dstweight = 1.0F;
-       }
-       
-       schan= src->chanbase.first;
-       for (dchan = dst->chanbase.first; dchan; dchan=dchan->next, schan= schan->next){
-               if (schan->flag & (POSE_ROT|POSE_LOC|POSE_SIZE)) {
-                       /* replaced quat->matrix->quat conversion with decent quaternion interpol (ton) */
-                       
-                       /* Do the transformation blend */
-                       if (schan->flag & POSE_ROT) {
-                               /* quat interpolation done separate */
-                               if (schan->rotmode == PCHAN_ROT_QUAT) {
-                                       float dquat[4], squat[4];
-                                       
-                                       QUATCOPY(dquat, dchan->quat);
-                                       QUATCOPY(squat, schan->quat);
-                                       if (mode==ACTSTRIPMODE_BLEND)
-                                               QuatInterpol(dchan->quat, dquat, squat, srcweight);
-                                       else {
-                                               QuatMulFac(squat, srcweight);
-                                               QuatMul(dchan->quat, dquat, squat);
-                                       }
-                                       
-                                       NormalQuat(dchan->quat);
-                               }
-                       }
-
-                       for (i=0; i<3; i++) {
-                               /* blending for loc and scale are pretty self-explanatory... */
-                               if (schan->flag & POSE_LOC)
-                                       dchan->loc[i] = (dchan->loc[i]*dstweight) + (schan->loc[i]*srcweight);
-                               if (schan->flag & POSE_SIZE)
-                                       dchan->size[i] = 1.0f + ((dchan->size[i]-1.0f)*dstweight) + ((schan->size[i]-1.0f)*srcweight);
-                               
-                               /* euler-rotation interpolation done here instead... */
-                               // FIXME: are these results decent?
-                               if ((schan->flag & POSE_ROT) && (schan->rotmode))
-                                       dchan->eul[i] = (dchan->eul[i]*dstweight) + (schan->eul[i]*srcweight);
-                       }
-                       dchan->flag |= schan->flag;
-               }
-               for(dcon= dchan->constraints.first, scon= schan->constraints.first; dcon && scon; dcon= dcon->next, scon= scon->next) {
-                       /* no 'add' option for constraint blending */
-                       dcon->enforce= dcon->enforce*(1.0f-srcweight) + scon->enforce*srcweight;
-               }
-       }
-       
-       /* this pose is now in src time */
-       dst->ctime= src->ctime;
-}
-
 typedef struct NlaIpoChannel {
        struct NlaIpoChannel *next, *prev;
        float val;
 typedef struct NlaIpoChannel {
        struct NlaIpoChannel *next, *prev;
        float val;
@@ -912,7 +1188,7 @@ typedef struct NlaIpoChannel {
        int type;
 } NlaIpoChannel;
 
        int type;
 } NlaIpoChannel;
 
-void extract_ipochannels_from_action(ListBase *lb, ID *id, bAction *act, const char *name, float ctime)
+static void extract_ipochannels_from_action(ListBase *lb, ID *id, bAction *act, const char *name, float ctime)
 {
        bActionChannel *achan= get_action_channel(act, name);
        IpoCurve *icu;
 {
        bActionChannel *achan= get_action_channel(act, name);
        IpoCurve *icu;
@@ -1005,7 +1281,7 @@ static void blend_ipochannels(ListBase *dst, ListBase *src, float srcweight, int
        }
 }
 
        }
 }
 
-int execute_ipochannels(ListBase *lb)
+static int execute_ipochannels(ListBase *lb)
 {
        NlaIpoChannel *nic;
        int count = 0;
 {
        NlaIpoChannel *nic;
        int count = 0;
@@ -1186,45 +1462,6 @@ static Object *get_parent_path(Object *ob)
 
 /* ************** do the action ************ */
 
 
 /* ************** do the action ************ */
 
-/* For the calculation of the effects of an action at the given frame on an object 
- * This is currently only used for the action constraint 
- */
-void what_does_obaction (Scene *scene, Object *ob, Object *workob, bAction *act, float cframe)
-{
-       ListBase tchanbase= {NULL, NULL};
-       
-       clear_workob(workob);
-       Mat4CpyMat4(workob->obmat, ob->obmat);
-       Mat4CpyMat4(workob->parentinv, ob->parentinv);
-       Mat4CpyMat4(workob->constinv, ob->constinv);
-       workob->parent= ob->parent;
-       workob->track= ob->track;
-
-       workob->trackflag= ob->trackflag;
-       workob->upflag= ob->upflag;
-       
-       workob->partype= ob->partype;
-       workob->par1= ob->par1;
-       workob->par2= ob->par2;
-       workob->par3= ob->par3;
-
-       workob->constraints.first = ob->constraints.first;
-       workob->constraints.last = ob->constraints.last;
-
-       strcpy(workob->parsubstr, ob->parsubstr);
-       strcpy(workob->id.name, ob->id.name);
-       
-       /* extract_ipochannels_from_action needs id's! */
-       workob->action= act;
-       
-       extract_ipochannels_from_action(&tchanbase, &workob->id, act, "Object", bsystem_time(scene, workob, cframe, 0.0));
-       
-       if (tchanbase.first) {
-               execute_ipochannels(&tchanbase);
-               BLI_freelistN(&tchanbase);
-       }
-}
-
 /* ----- nla, etc. --------- */
 
 static void do_nla(Scene *scene, Object *ob, int blocktype)
 /* ----- nla, etc. --------- */
 
 static void do_nla(Scene *scene, Object *ob, int blocktype)