87868eff8124b7328b779fd0ee57faea88949514
[blender.git] / source / blender / blenkernel / intern / action.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * Contributor(s): Full recode, Ton Roosendaal, Crete 2005
24  *                               Full recode, Joshua Leung, 2009
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #ifdef HAVE_CONFIG_H
30 #include <config.h>
31 #endif
32
33 #include <string.h>
34 #include <math.h>
35 #include <stdlib.h>
36 #include <stddef.h>     
37
38 #include "MEM_guardedalloc.h"
39
40 #include "DNA_anim_types.h"
41 #include "DNA_action_types.h"
42 #include "DNA_armature_types.h"
43 #include "DNA_constraint_types.h"
44 #include "DNA_curve_types.h"
45 #include "DNA_key_types.h"
46 #include "DNA_nla_types.h"
47 #include "DNA_object_types.h"
48 #include "DNA_scene_types.h"
49
50 #include "BKE_animsys.h"
51 #include "BKE_action.h"
52 #include "BKE_anim.h"
53 #include "BKE_armature.h"
54 #include "BKE_blender.h"
55 #include "BKE_constraint.h"
56 #include "BKE_displist.h"
57 #include "BKE_global.h"
58 #include "BKE_fcurve.h"
59 #include "BKE_key.h"
60 #include "BKE_lattice.h"
61 #include "BKE_library.h"
62 #include "BKE_main.h"
63 #include "BKE_object.h"
64 #include "BKE_utildefines.h"
65 #include "BIK_api.h"
66
67 #include "BLI_arithb.h"
68 #include "BLI_blenlib.h"
69 #include "BLI_ghash.h"
70
71 #include "RNA_access.h"
72 #include "RNA_types.h"
73
74 /* *********************** NOTE ON POSE AND ACTION **********************
75
76   - Pose is the local (object level) component of armature. The current
77     object pose is saved in files, and (will be) is presorted for dependency
78   - Actions have fewer (or other) channels, and write data to a Pose
79   - Currently ob->pose data is controlled in where_is_pose only. The (recalc)
80     event system takes care of calling that
81   - The NLA system (here too) uses Poses as interpolation format for Actions
82   - Therefore we assume poses to be static, and duplicates of poses have channels in
83     same order, for quick interpolation reasons
84
85   ****************************** (ton) ************************************ */
86
87 /* ***************** Library data level operations on action ************** */
88
89 bAction *add_empty_action(const char name[])
90 {
91         bAction *act;
92         
93         act= alloc_libblock(&G.main->action, ID_AC, name);
94         act->id.flag |= LIB_FAKEUSER; // XXX this is nasty for new users... maybe we don't want this anymore
95         act->id.us++;
96         
97         return act;
98 }       
99
100 // does copy_fcurve...
101 void make_local_action(bAction *act)
102 {
103         // Object *ob;
104         bAction *actn;
105         int local=0, lib=0;
106         
107         if (act->id.lib==0) return;
108         if (act->id.us==1) {
109                 act->id.lib= 0;
110                 act->id.flag= LIB_LOCAL;
111                 //make_local_action_channels(act);
112                 new_id(0, (ID *)act, 0);
113                 return;
114         }
115         
116 #if 0   // XXX old animation system
117         ob= G.main->object.first;
118         while(ob) {
119                 if(ob->action==act) {
120                         if(ob->id.lib) lib= 1;
121                         else local= 1;
122                 }
123                 ob= ob->id.next;
124         }
125 #endif
126         
127         if(local && lib==0) {
128                 act->id.lib= 0;
129                 act->id.flag= LIB_LOCAL;
130                 //make_local_action_channels(act);
131                 new_id(0, (ID *)act, 0);
132         }
133         else if(local && lib) {
134                 actn= copy_action(act);
135                 actn->id.us= 0;
136                 
137 #if 0   // XXX old animation system
138                 ob= G.main->object.first;
139                 while(ob) {
140                         if(ob->action==act) {
141                                 
142                                 if(ob->id.lib==0) {
143                                         ob->action = actn;
144                                         actn->id.us++;
145                                         act->id.us--;
146                                 }
147                         }
148                         ob= ob->id.next;
149                 }
150 #endif  // XXX old animation system
151         }
152 }
153
154
155 void free_action (bAction *act)
156 {
157         /* sanity check */
158         if (act == NULL)
159                 return;
160         
161         /* Free F-Curves */
162         free_fcurves(&act->curves);
163         
164         /* Free groups */
165         if (act->groups.first)
166                 BLI_freelistN(&act->groups);
167                 
168         /* Free pose-references (aka local markers) */
169         if (act->markers.first)
170                 BLI_freelistN(&act->markers);
171 }
172
173 bAction *copy_action (bAction *src)
174 {
175         bAction *dst = NULL;
176         bActionGroup *dgrp, *sgrp;
177         FCurve *dfcu, *sfcu;
178         
179         if (src == NULL) 
180                 return NULL;
181         dst= copy_libblock(src);
182         
183         /* duplicate the lists of groups and markers */
184         BLI_duplicatelist(&dst->groups, &src->groups);
185         BLI_duplicatelist(&dst->markers, &src->markers);
186         
187         /* copy F-Curves, fixing up the links as we go */
188         dst->curves.first= dst->curves.last= NULL;
189         
190         for (sfcu= src->curves.first; sfcu; sfcu= sfcu->next) {
191                 /* duplicate F-Curve */
192                 dfcu= copy_fcurve(sfcu);
193                 BLI_addtail(&dst->curves, dfcu);
194                 
195                 /* fix group links (kindof bad list-in-list search, but this is the most reliable way) */
196                 for (dgrp=dst->groups.first, sgrp=src->groups.first; dgrp && sgrp; dgrp=dgrp->next, sgrp=sgrp->next) {
197                         if (sfcu->grp == sgrp) {
198                                 dfcu->grp= dgrp;
199                                 
200                                 if (dgrp->channels.first == sfcu)
201                                         dgrp->channels.first= dfcu;
202                                 if (dgrp->channels.last == sfcu)
203                                         dgrp->channels.last= dfcu;
204                                         
205                                 break;
206                         }
207                 }
208         }
209         
210         dst->id.flag |= LIB_FAKEUSER; // XXX this is nasty for new users... maybe we don't want this anymore
211         dst->id.us++;
212         
213         return dst;
214 }
215
216 /* *************** Action Groups *************** */
217
218 /* Get the active action-group for an Action */
219 bActionGroup *get_active_actiongroup (bAction *act)
220 {
221         bActionGroup *agrp= NULL;
222         
223         if (act && act->groups.first) { 
224                 for (agrp= act->groups.first; agrp; agrp= agrp->next) {
225                         if (agrp->flag & AGRP_ACTIVE)
226                                 break;
227                 }
228         }
229         
230         return agrp;
231 }
232
233 /* Make the given Action-Group the active one */
234 void set_active_action_group (bAction *act, bActionGroup *agrp, short select)
235 {
236         bActionGroup *grp;
237         
238         /* sanity checks */
239         if (act == NULL)
240                 return;
241         
242         /* Deactive all others */
243         for (grp= act->groups.first; grp; grp= grp->next) {
244                 if ((grp==agrp) && (select))
245                         grp->flag |= AGRP_ACTIVE;
246                 else    
247                         grp->flag &= ~AGRP_ACTIVE;
248         }
249 }
250
251 /* Add given channel into (active) group 
252  *      - assumes that channel is not linked to anything anymore
253  *      - always adds at the end of the group 
254  */
255 void action_groups_add_channel (bAction *act, bActionGroup *agrp, FCurve *fcurve)
256 {
257         FCurve *fcu;
258         short done=0;
259         
260         /* sanity checks */
261         if (ELEM3(NULL, act, agrp, fcurve))
262                 return;
263         
264         /* if no channels, just add to two lists at the same time */
265         if (act->curves.first == NULL) {
266                 fcurve->next = fcurve->prev = NULL;
267                 
268                 agrp->channels.first = agrp->channels.last = fcurve;
269                 act->curves.first = act->curves.last = fcurve;
270                 
271                 fcurve->grp= agrp;
272                 return;
273         }
274         
275         /* try to find a channel to slot this in before/after */
276         for (fcu= act->curves.first; fcu; fcu= fcu->next) {
277                 /* if channel has no group, then we have ungrouped channels, which should always occur after groups */
278                 if (fcu->grp == NULL) {
279                         BLI_insertlinkbefore(&act->curves, fcu, fcurve);
280                         
281                         if (agrp->channels.first == NULL)
282                                 agrp->channels.first= fcurve;
283                         agrp->channels.last= fcurve;
284                         
285                         done= 1;
286                         break;
287                 }
288                 
289                 /* if channel has group after current, we can now insert (otherwise we have gone too far) */
290                 else if (fcu->grp == agrp->next) {
291                         BLI_insertlinkbefore(&act->curves, fcu, fcurve);
292                         
293                         if (agrp->channels.first == NULL)
294                                 agrp->channels.first= fcurve;
295                         agrp->channels.last= fcurve;
296                         
297                         done= 1;
298                         break;
299                 }
300                 
301                 /* if channel has group we're targeting, check whether it is the last one of these */
302                 else if (fcu->grp == agrp) {
303                         if ((fcu->next) && (fcu->next->grp != agrp)) {
304                                 BLI_insertlinkafter(&act->curves, fcu, fcurve);
305                                 agrp->channels.last= fcurve;
306                                 done= 1;
307                                 break;
308                         }
309                         else if (fcu->next == NULL) {
310                                 BLI_addtail(&act->curves, fcurve);
311                                 agrp->channels.last= fcurve;
312                                 done= 1;
313                                 break;
314                         }
315                 }
316                 
317                 /* if channel has group before target, check whether the next one is something after target */
318                 else if (fcu->grp == agrp->prev) {
319                         if (fcu->next) {
320                                 if ((fcu->next->grp != fcu->grp) && (fcu->next->grp != agrp)) {
321                                         BLI_insertlinkafter(&act->curves, fcu, fcurve);
322                                         
323                                         agrp->channels.first= fcurve;
324                                         agrp->channels.last= fcurve;
325                                         
326                                         done= 1;
327                                         break;
328                                 }
329                         }
330                         else {
331                                 BLI_insertlinkafter(&act->curves, fcu, fcurve);
332                                 
333                                 agrp->channels.first= fcurve;
334                                 agrp->channels.last= fcurve;
335                                 
336                                 done= 1;
337                                 break;
338                         }
339                 }
340         }
341         
342         /* only if added, set channel as belonging to this group */
343         if (done) {
344                 //printf("FCurve added to group \n");
345                 fcurve->grp= agrp;
346         }
347         else {
348                 printf("Error: FCurve '%s' couldn't be added to Group '%s' \n", fcurve->rna_path, agrp->name);
349                 BLI_addtail(&act->curves, fcurve);
350         }
351 }       
352
353 /* Remove the given channel from all groups */
354 void action_groups_remove_channel (bAction *act, FCurve *fcu)
355 {
356         /* sanity checks */
357         if (ELEM(NULL, act, fcu))       
358                 return;
359         
360         /* check if any group used this directly */
361         if (fcu->grp) {
362                 bActionGroup *agrp= fcu->grp;
363                 
364                 if (agrp->channels.first == agrp->channels.last) {
365                         if (agrp->channels.first == fcu) {
366                                 agrp->channels.first= NULL;
367                                 agrp->channels.last= NULL;
368                         }
369                 }
370                 else if (agrp->channels.first == fcu) {
371                         if ((fcu->next) && (fcu->next->grp==agrp))
372                                 agrp->channels.first= fcu->next;
373                         else
374                                 agrp->channels.first= NULL;
375                 }
376                 else if (agrp->channels.last == fcu) {
377                         if ((fcu->prev) && (fcu->prev->grp==agrp))
378                                 agrp->channels.last= fcu->prev;
379                         else
380                                 agrp->channels.last= NULL;
381                 }
382                 
383                 fcu->grp= NULL;
384         }
385         
386         /* now just remove from list */
387         BLI_remlink(&act->curves, fcu);
388 }
389
390 /* Find a group with the given name */
391 bActionGroup *action_groups_find_named (bAction *act, const char name[])
392 {
393         bActionGroup *grp;
394         
395         /* sanity checks */
396         if (ELEM3(NULL, act, act->groups.first, name) || (name[0] == 0))
397                 return NULL;
398                 
399         /* do string comparisons */
400         for (grp= act->groups.first; grp; grp= grp->next) {
401                 if (strcmp(grp->name, name) == 0)
402                         return grp;
403         }
404         
405         /* not found */
406         return NULL;
407 }
408
409 /* *************** Pose channels *************** */
410
411 /* usually used within a loop, so we got a N^2 slowdown */
412 bPoseChannel *get_pose_channel(const bPose *pose, const char *name)
413 {
414         bPoseChannel *chan;
415         
416         if (ELEM(NULL, pose, name) || (name[0] == 0))
417                 return NULL;
418         
419         for (chan=pose->chanbase.first; chan; chan=chan->next) {
420                 if (chan->name[0] == name[0]) {
421                         if (!strcmp (chan->name, name))
422                                 return chan;
423                 }
424         }
425
426         return NULL;
427 }
428
429 /* Use with care, not on Armature poses but for temporal ones */
430 /* (currently used for action constraints and in rebuild_pose) */
431 bPoseChannel *verify_pose_channel(bPose* pose, const char* name)
432 {
433         bPoseChannel *chan;
434         
435         if (pose == NULL)
436                 return NULL;
437         
438         /* See if this channel exists */
439         for (chan=pose->chanbase.first; chan; chan=chan->next) {
440                 if (!strcmp (name, chan->name))
441                         return chan;
442         }
443         
444         /* If not, create it and add it */
445         chan = MEM_callocN(sizeof(bPoseChannel), "verifyPoseChannel");
446         
447         strncpy(chan->name, name, 31);
448         /* init vars to prevent math errors */
449         chan->quat[0] = 1.0f;
450         chan->size[0] = chan->size[1] = chan->size[2] = 1.0f;
451         
452         chan->limitmin[0]= chan->limitmin[1]= chan->limitmin[2]= -180.0f;
453         chan->limitmax[0]= chan->limitmax[1]= chan->limitmax[2]= 180.0f;
454         chan->stiffness[0]= chan->stiffness[1]= chan->stiffness[2]= 0.0f;
455         chan->ikrotweight = chan->iklinweight = 0.0f;
456         Mat4One(chan->constinv);
457         
458         BLI_addtail(&pose->chanbase, chan);
459         
460         return chan;
461 }
462
463 /* Find the active posechannel for an object (we can't just use pose, as layer info is in armature) */
464 bPoseChannel *get_active_posechannel (Object *ob)
465 {
466         bArmature *arm= (ob) ? ob->data : NULL;
467         bPoseChannel *pchan;
468         
469         if ELEM3(NULL, ob, ob->pose, arm)
470                 return NULL;
471         
472         /* find active */
473         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
474                 if ((pchan->bone) && (pchan->bone->flag & BONE_ACTIVE) && (pchan->bone->layer & arm->layer))
475                         return pchan;
476         }
477         
478         return NULL;
479 }
480
481 const char *get_ikparam_name(bPose *pose)
482 {
483         if (pose) {
484                 switch (pose->iksolver) {
485                 case IKSOLVER_LEGACY:
486                         return NULL;
487                 case IKSOLVER_ITASC:
488                         return "bItasc";
489                 }
490         }
491         return NULL;
492 }
493 /* dst should be freed already, makes entire duplicate */
494 void copy_pose (bPose **dst, bPose *src, int copycon)
495 {
496         bPose *outPose;
497         bPoseChannel    *pchan;
498         ListBase listb;
499         
500         if (!src){
501                 *dst=NULL;
502                 return;
503         }
504         
505         if (*dst==src) {
506                 printf("copy_pose source and target are the same\n");
507                 *dst=NULL;
508                 return;
509         }
510         
511         outPose= MEM_callocN(sizeof(bPose), "pose");
512         
513         BLI_duplicatelist(&outPose->chanbase, &src->chanbase);
514         outPose->iksolver = src->iksolver;
515         outPose->ikdata = NULL;
516         outPose->ikparam = MEM_dupallocN(src->ikparam);
517
518         if (copycon) {
519                 for (pchan=outPose->chanbase.first; pchan; pchan=pchan->next) {
520                         copy_constraints(&listb, &pchan->constraints);  // copy_constraints NULLs listb
521                         pchan->constraints= listb;
522                         pchan->path= NULL;
523                 }
524         }
525         
526         *dst=outPose;
527 }
528
529 void init_pose_itasc(bItasc *itasc)
530 {
531         if (itasc) {
532                 itasc->iksolver = IKSOLVER_ITASC;
533                 itasc->minstep = 0.01f;
534                 itasc->maxstep = 0.06f;
535                 itasc->numiter = 100;
536                 itasc->numstep = 4;
537                 itasc->precision = 0.005f;
538                 itasc->flag = ITASC_AUTO_STEP|ITASC_INITIAL_REITERATION;
539                 itasc->feedback = 20.f;
540                 itasc->maxvel = 50.f;
541                 itasc->solver = ITASC_SOLVER_SDLS;
542                 itasc->dampmax = 0.5;
543                 itasc->dampeps = 0.15;
544         }
545 }
546 void init_pose_ikparam(bPose *pose)
547 {
548         bItasc *itasc;
549         switch (pose->iksolver) {
550         case IKSOLVER_ITASC:
551                 itasc = MEM_callocN(sizeof(bItasc), "itasc");
552                 init_pose_itasc(itasc);
553                 pose->ikparam = itasc;
554                 break;
555         case IKSOLVER_LEGACY:
556         default:
557                 pose->ikparam = NULL;
558                 break;
559         }
560 }
561
562 void free_pose_channels(bPose *pose) 
563 {
564         bPoseChannel *pchan;
565         
566         if (pose->chanbase.first) {
567                 for (pchan = pose->chanbase.first; pchan; pchan=pchan->next){
568                         if (pchan->path)
569                                 MEM_freeN(pchan->path);
570                         free_constraints(&pchan->constraints);
571                 }
572                 BLI_freelistN(&pose->chanbase);
573         }
574 }
575
576 void free_pose(bPose *pose)
577 {
578         if (pose) {
579                 /* free pose-channels */
580                 free_pose_channels(pose);
581                 
582                 /* free pose-groups */
583                 if (pose->agroups.first)
584                         BLI_freelistN(&pose->agroups);
585
586                 /* free IK solver state */
587                 BIK_clear_data(pose);
588
589                 /* free IK solver param */
590                 if (pose->ikparam)
591                         MEM_freeN(pose->ikparam);
592
593                 /* free pose */
594                 MEM_freeN(pose);
595         }
596 }
597
598 static void copy_pose_channel_data(bPoseChannel *pchan, const bPoseChannel *chan)
599 {
600         bConstraint *pcon, *con;
601         
602         VECCOPY(pchan->loc, chan->loc);
603         VECCOPY(pchan->size, chan->size);
604         VECCOPY(pchan->eul, chan->eul);
605         QUATCOPY(pchan->quat, chan->quat);
606         pchan->rotmode= chan->rotmode;
607         Mat4CpyMat4(pchan->chan_mat, (float(*)[4])chan->chan_mat);
608         Mat4CpyMat4(pchan->pose_mat, (float(*)[4])chan->pose_mat);
609         pchan->flag= chan->flag;
610         
611         con= chan->constraints.first;
612         for(pcon= pchan->constraints.first; pcon; pcon= pcon->next, con= con->next) {
613                 pcon->enforce= con->enforce;
614                 pcon->headtail= con->headtail;
615         }
616 }
617
618 /* checks for IK constraint, and also for Follow-Path constraint.
619  * can do more constraints flags later 
620  */
621 /* pose should be entirely OK */
622 void update_pose_constraint_flags(bPose *pose)
623 {
624         bPoseChannel *pchan, *parchan;
625         bConstraint *con;
626         
627         /* clear */
628         for (pchan= pose->chanbase.first; pchan; pchan= pchan->next) {
629                 pchan->constflag= 0;
630         }
631         pose->flag &= ~POSE_CONSTRAINTS_TIMEDEPEND;
632         
633         /* detect */
634         for (pchan= pose->chanbase.first; pchan; pchan=pchan->next) {
635                 for (con= pchan->constraints.first; con; con= con->next) {
636                         if (con->type==CONSTRAINT_TYPE_KINEMATIC) {
637                                 bKinematicConstraint *data = (bKinematicConstraint*)con->data;
638                                 
639                                 pchan->constflag |= PCHAN_HAS_IK;
640                                 
641                                 if(data->tar==NULL || (data->tar->type==OB_ARMATURE && data->subtarget[0]==0))
642                                         pchan->constflag |= PCHAN_HAS_TARGET;
643                                 
644                                 /* negative rootbone = recalc rootbone index. used in do_versions */
645                                 if(data->rootbone<0) {
646                                         data->rootbone= 0;
647                                         
648                                         if(data->flag & CONSTRAINT_IK_TIP) parchan= pchan;
649                                         else parchan= pchan->parent;
650                                         
651                                         while(parchan) {
652                                                 data->rootbone++;
653                                                 if((parchan->bone->flag & BONE_CONNECTED)==0)
654                                                         break;
655                                                 parchan= parchan->parent;
656                                         }
657                                 }
658                         }
659                         else if (con->type == CONSTRAINT_TYPE_FOLLOWPATH) {
660                                 bFollowPathConstraint *data= (bFollowPathConstraint *)con->data;
661                                 
662                                 /* for drawing constraint colors when color set allows this */
663                                 pchan->constflag |= PCHAN_HAS_CONST;
664                                 
665                                 /* if we have a valid target, make sure that this will get updated on frame-change
666                                  * (needed for when there is no anim-data for this pose)
667                                  */
668                                 if ((data->tar) && (data->tar->type==OB_CURVE))
669                                         pose->flag |= POSE_CONSTRAINTS_TIMEDEPEND;
670                         }
671                         else 
672                                 pchan->constflag |= PCHAN_HAS_CONST;
673                 }
674         }
675 }
676
677 /* Clears all BONE_UNKEYED flags for every pose channel in every pose 
678  * This should only be called on frame changing, when it is acceptable to
679  * do this. Otherwise, these flags should not get cleared as poses may get lost.
680  */
681 void framechange_poses_clear_unkeyed(void)
682 {
683         Object *ob;
684         bPose *pose;
685         bPoseChannel *pchan;
686         
687         /* This needs to be done for each object that has a pose */
688         // TODO: proxies may/may not be correctly handled here... (this needs checking) 
689         for (ob= G.main->object.first; ob; ob= ob->id.next) {
690                 /* we only need to do this on objects with a pose */
691                 if ( (pose= ob->pose) ) {
692                         for (pchan= pose->chanbase.first; pchan; pchan= pchan->next) {
693                                 if (pchan->bone) 
694                                         pchan->bone->flag &= ~BONE_UNKEYED;
695                         }
696                 }
697         }
698 }
699
700 /* ************************** Bone Groups ************************** */
701
702 /* Adds a new bone-group */
703 void pose_add_group (Object *ob)
704 {
705         bPose *pose= (ob) ? ob->pose : NULL;
706         bActionGroup *grp;
707         
708         if (ELEM(NULL, ob, ob->pose))
709                 return;
710         
711         grp= MEM_callocN(sizeof(bActionGroup), "PoseGroup");
712         strcpy(grp->name, "Group");
713         BLI_addtail(&pose->agroups, grp);
714         BLI_uniquename(&pose->agroups, grp, "Group", '.', offsetof(bActionGroup, name), 32);
715         
716         pose->active_group= BLI_countlist(&pose->agroups);
717 }
718
719 /* Remove the active bone-group */
720 void pose_remove_group (Object *ob)
721 {
722         bPose *pose= (ob) ? ob->pose : NULL;
723         bActionGroup *grp = NULL;
724         bPoseChannel *pchan;
725         
726         /* sanity checks */
727         if (ELEM(NULL, ob, pose))
728                 return;
729         if (pose->active_group <= 0)
730                 return;
731         
732         /* get group to remove */
733         grp= BLI_findlink(&pose->agroups, pose->active_group-1);
734         if (grp) {
735                 /* adjust group references (the trouble of using indices!):
736                  *      - firstly, make sure nothing references it 
737                  *      - also, make sure that those after this item get corrected
738                  */
739                 for (pchan= pose->chanbase.first; pchan; pchan= pchan->next) {
740                         if (pchan->agrp_index == pose->active_group)
741                                 pchan->agrp_index= 0;
742                         else if (pchan->agrp_index > pose->active_group)
743                                 pchan->agrp_index--;
744                 }
745                 
746                 /* now, remove it from the pose */
747                 BLI_freelinkN(&pose->agroups, grp);
748                 pose->active_group= 0;
749         }
750 }
751
752 /* ************** F-Curve Utilities for Actions ****************** */
753
754 /* Check if the given action has any keyframes */
755 short action_has_motion(const bAction *act)
756 {
757         FCurve *fcu;
758         
759         /* return on the first F-Curve that has some keyframes/samples defined */
760         if (act) {
761                 for (fcu= act->curves.first; fcu; fcu= fcu->next) {
762                         if (fcu->totvert)
763                                 return 1;
764                 }
765         }
766         
767         /* nothing found */
768         return 0;
769 }
770
771 /* Calculate the extents of given action */
772 void calc_action_range(const bAction *act, float *start, float *end, short incl_modifiers)
773 {
774         FCurve *fcu;
775         float min=999999999.0f, max=-999999999.0f;
776         short foundvert=0, foundmod=0;
777
778         if (act) {
779                 for (fcu= act->curves.first; fcu; fcu= fcu->next) {
780                         /* if curve has keyframes, consider them first */
781                         if (fcu->totvert) {
782                                 float nmin, nmax;
783                                 
784                                 /* get extents for this curve */
785                                 calc_fcurve_range(fcu, &nmin, &nmax);
786                                 
787                                 /* compare to the running tally */
788                                 min= MIN2(min, nmin);
789                                 max= MAX2(max, nmax);
790                                 
791                                 foundvert= 1;
792                         }
793                         
794                         /* if incl_modifiers is enabled, need to consider modifiers too
795                          *      - only really care about the last modifier
796                          */
797                         if ((incl_modifiers) && (fcu->modifiers.last)) {
798                                 FModifier *fcm= fcu->modifiers.last;
799                                 
800                                 /* only use the maximum sensible limits of the modifiers if they are more extreme */
801                                 switch (fcm->type) {
802                                         case FMODIFIER_TYPE_LIMITS: /* Limits F-Modifier */
803                                         {
804                                                 FMod_Limits *fmd= (FMod_Limits *)fcm->data;
805                                                 
806                                                 if (fmd->flag & FCM_LIMIT_XMIN) {
807                                                         min= MIN2(min, fmd->rect.xmin);
808                                                 }
809                                                 if (fmd->flag & FCM_LIMIT_XMAX) {
810                                                         max= MAX2(max, fmd->rect.xmax);
811                                                 }
812                                         }
813                                                 break;
814                                                 
815                                         case FMODIFIER_TYPE_CYCLES: /* Cycles F-Modifier */
816                                         {
817                                                 FMod_Cycles *fmd= (FMod_Cycles *)fcm->data;
818                                                 
819                                                 if (fmd->before_mode != FCM_EXTRAPOLATE_NONE)
820                                                         min= MINAFRAMEF;
821                                                 if (fmd->after_mode != FCM_EXTRAPOLATE_NONE)
822                                                         max= MAXFRAMEF;
823                                         }
824                                                 break;
825                                                 
826                                         // TODO: function modifier may need some special limits
827                                                 
828                                         default: /* all other standard modifiers are on the infinite range... */
829                                                 min= MINAFRAMEF;
830                                                 max= MAXFRAMEF;
831                                                 break;
832                                 }
833                                 
834                                 foundmod= 1;
835                         }
836                 }
837         }       
838         
839         if (foundvert || foundmod) {
840                 if(min==max) max+= 1.0f;
841                 *start= min;
842                 *end= max;
843         }
844         else {
845                 *start= 0.0f;
846                 *end= 1.0f;
847         }
848 }
849
850 /* Return flags indicating which transforms the given object/posechannel has 
851  *      - if 'curves' is provided, a list of links to these curves are also returned
852  */
853 short action_get_item_transforms (bAction *act, Object *ob, bPoseChannel *pchan, ListBase *curves)
854 {
855         PointerRNA ptr;
856         FCurve *fcu;
857         char *basePath=NULL;
858         short flags=0;
859         
860         /* build PointerRNA from provided data to obtain the paths to use */
861         if (pchan)
862                 RNA_pointer_create((ID *)ob, &RNA_PoseChannel, pchan, &ptr);
863         else if (ob)
864                 RNA_id_pointer_create((ID *)ob, &ptr);
865         else    
866                 return 0;
867                 
868         /* get the basic path to the properties of interest */
869         basePath= RNA_path_from_ID_to_struct(&ptr);
870         if (basePath == NULL)
871                 return 0;
872                 
873         /* search F-Curves for the given properties 
874          *      - we cannot use the groups, since they may not be grouped in that way...
875          */
876         for (fcu= act->curves.first; fcu; fcu= fcu->next) {
877                 char *bPtr=NULL, *pPtr=NULL;
878                 
879                 /* if enough flags have been found, we can stop checking unless we're also getting the curves */
880                 if ((flags == ACT_TRANS_ALL) && (curves == NULL))
881                         break;
882                         
883                 /* just in case... */
884                 if (fcu->rna_path == NULL)
885                         continue;
886                 
887                 /* step 1: check for matching base path */
888                 bPtr= strstr(fcu->rna_path, basePath);
889                 
890                 if (bPtr) {
891                         /* step 2: check for some property with transforms 
892                          *      - to speed things up, only check for the ones not yet found 
893                          *        unless we're getting the curves too
894                          *      - if we're getting the curves, the BLI_genericNodeN() creates a LinkData
895                          *        node wrapping the F-Curve, which then gets added to the list
896                          *      - once a match has been found, the curve cannot possibly be any other one
897                          */
898                         if ((curves) || (flags & ACT_TRANS_LOC) == 0) {
899                                 pPtr= strstr(fcu->rna_path, "location");
900                                 if ((pPtr) && (pPtr >= bPtr)) {
901                                         flags |= ACT_TRANS_LOC;
902                                         
903                                         if (curves) 
904                                                 BLI_addtail(curves, BLI_genericNodeN(fcu));
905                                         continue;
906                                 }
907                         }
908                         
909                         if ((curves) || (flags & ACT_TRANS_SCALE) == 0) {
910                                 pPtr= strstr(fcu->rna_path, "scale");
911                                 if ((pPtr) && (pPtr >= bPtr)) {
912                                         flags |= ACT_TRANS_SCALE;
913                                         
914                                         if (curves) 
915                                                 BLI_addtail(curves, BLI_genericNodeN(fcu));
916                                         continue;
917                                 }
918                         }
919                         
920                         if ((curves) || (flags & ACT_TRANS_ROT) == 0) {
921                                 pPtr= strstr(fcu->rna_path, "rotation");
922                                 if ((pPtr) && (pPtr >= bPtr)) {
923                                         flags |= ACT_TRANS_ROT;
924                                         
925                                         if (curves) 
926                                                 BLI_addtail(curves, BLI_genericNodeN(fcu));
927                                         continue;
928                                 }
929                         }
930                 }
931         }
932         
933         /* free basePath */
934         MEM_freeN(basePath);
935         
936         /* return flags found */
937         return flags;
938 }
939
940 /* ************** Pose Management Tools ****************** */
941
942 /* Copy the data from the action-pose (src) into the pose */
943 /* both args are assumed to be valid */
944 /* exported to game engine */
945 /* Note! this assumes both poses are aligned, this isnt always true when dealing with user poses */
946 void extract_pose_from_pose(bPose *pose, const bPose *src)
947 {
948         const bPoseChannel *schan;
949         bPoseChannel *pchan= pose->chanbase.first;
950
951         if (pose==src) {
952                 printf("extract_pose_from_pose source and target are the same\n");
953                 return;
954         }
955
956         for (schan=src->chanbase.first; (schan && pchan); schan=schan->next, pchan= pchan->next) {
957                 copy_pose_channel_data(pchan, schan);
958         }
959 }
960
961 /* for do_all_pose_actions, clears the pose. Now also exported for proxy and tools */
962 void rest_pose(bPose *pose)
963 {
964         bPoseChannel *pchan;
965         int i;
966         
967         if (!pose)
968                 return;
969         
970         memset(pose->stride_offset, 0, sizeof(pose->stride_offset));
971         memset(pose->cyclic_offset, 0, sizeof(pose->cyclic_offset));
972         
973         for (pchan=pose->chanbase.first; pchan; pchan= pchan->next){
974                 for (i=0; i<3; i++) {
975                         pchan->loc[i]= 0.0f;
976                         pchan->quat[i+1]= 0.0f;
977                         pchan->eul[i]= 0.0f;
978                         pchan->size[i]= 1.0f;
979                 }
980                 pchan->quat[0]= 1.0f;
981                 
982                 pchan->flag &= ~(POSE_LOC|POSE_ROT|POSE_SIZE);
983         }
984 }
985
986 /* both poses should be in sync */
987 void copy_pose_result(bPose *to, bPose *from)
988 {
989         bPoseChannel *pchanto, *pchanfrom;
990         
991         if(to==NULL || from==NULL) {
992                 printf("pose result copy error\n"); // debug temp
993                 return;
994         }
995
996         if (to==from) {
997                 printf("copy_pose_result source and target are the same\n");
998                 return;
999         }
1000
1001
1002         for(pchanfrom= from->chanbase.first; pchanfrom; pchanfrom= pchanfrom->next) {
1003                 pchanto= get_pose_channel(to, pchanfrom->name);
1004                 if(pchanto) {
1005                         Mat4CpyMat4(pchanto->pose_mat, pchanfrom->pose_mat);
1006                         Mat4CpyMat4(pchanto->chan_mat, pchanfrom->chan_mat);
1007                         
1008                         /* used for local constraints */
1009                         VECCOPY(pchanto->loc, pchanfrom->loc);
1010                         QUATCOPY(pchanto->quat, pchanfrom->quat);
1011                         VECCOPY(pchanto->eul, pchanfrom->eul);
1012                         VECCOPY(pchanto->size, pchanfrom->size);
1013                         
1014                         VECCOPY(pchanto->pose_head, pchanfrom->pose_head);
1015                         VECCOPY(pchanto->pose_tail, pchanfrom->pose_tail);
1016                         pchanto->flag= pchanfrom->flag;
1017                 }
1018         }
1019 }
1020
1021 /* For the calculation of the effects of an Action at the given frame on an object 
1022  * This is currently only used for the Action Constraint 
1023  */
1024 void what_does_obaction (Scene *scene, Object *ob, Object *workob, bPose *pose, bAction *act, char groupname[], float cframe)
1025 {
1026         bActionGroup *agrp= action_groups_find_named(act, groupname);
1027         
1028         /* clear workob */
1029         clear_workob(workob);
1030         
1031         /* init workob */
1032         Mat4CpyMat4(workob->obmat, ob->obmat);
1033         Mat4CpyMat4(workob->parentinv, ob->parentinv);
1034         Mat4CpyMat4(workob->constinv, ob->constinv);
1035         workob->parent= ob->parent;
1036         workob->track= ob->track;
1037
1038         workob->trackflag= ob->trackflag;
1039         workob->upflag= ob->upflag;
1040         
1041         workob->partype= ob->partype;
1042         workob->par1= ob->par1;
1043         workob->par2= ob->par2;
1044         workob->par3= ob->par3;
1045
1046         workob->constraints.first = ob->constraints.first;
1047         workob->constraints.last = ob->constraints.last;
1048         
1049         workob->pose= pose;     /* need to set pose too, since this is used for both types of Action Constraint */
1050
1051         strcpy(workob->parsubstr, ob->parsubstr);
1052         strcpy(workob->id.name, "OB<ConstrWorkOb>"); /* we don't use real object name, otherwise RNA screws with the real thing */
1053         
1054         /* if we're given a group to use, it's likely to be more efficient (though a bit more dangerous) */
1055         if (agrp) {
1056                 /* specifically evaluate this group only */
1057                 PointerRNA id_ptr;
1058                 
1059                 /* get RNA-pointer for the workob's ID */
1060                 RNA_id_pointer_create(&workob->id, &id_ptr);
1061                 
1062                 /* execute action for this group only */
1063                 animsys_evaluate_action_group(&id_ptr, act, agrp, NULL, cframe);
1064         }
1065         else {
1066                 AnimData adt;
1067                 
1068                 /* init animdata, and attach to workob */
1069                 memset(&adt, 0, sizeof(AnimData));
1070                 workob->adt= &adt;
1071                 
1072                 adt.recalc= ADT_RECALC_ANIM;
1073                 adt.action= act;
1074                 
1075                 /* execute effects of Action on to workob (or it's PoseChannels) */
1076                 BKE_animsys_evaluate_animdata(&workob->id, &adt, cframe, ADT_RECALC_ANIM);
1077         }
1078 }
1079
1080 /* ********** NLA with non-poses works with ipo channels ********** */
1081
1082 #if 0 // XXX OLD ANIMATION SYSTEM (TO BE REMOVED)
1083
1084 /* ************************ Blending with NLA *************** */
1085
1086 static void blend_pose_strides(bPose *dst, bPose *src, float srcweight, short mode)
1087 {
1088         float dstweight;
1089         
1090         switch (mode){
1091                 case ACTSTRIPMODE_BLEND:
1092                         dstweight = 1.0F - srcweight;
1093                         break;
1094                 case ACTSTRIPMODE_ADD:
1095                         dstweight = 1.0F;
1096                         break;
1097                 default :
1098                         dstweight = 1.0F;
1099         }
1100         
1101         VecLerpf(dst->stride_offset, dst->stride_offset, src->stride_offset, srcweight);
1102 }
1103
1104
1105 /* 
1106
1107 bone matching diagram, strips A and B
1108
1109                  .------------------------.
1110                  |         A              |
1111                  '------------------------'
1112                                  .          .             b2
1113                  .          .-------------v----------.
1114                  .              |         B   .          |
1115                  .          '------------------------'
1116                  .          .             .
1117                  .          .             .
1118 offset:          .    0     .    A-B      .  A-b2+B     
1119                  .          .             .
1120
1121 */
1122
1123
1124 static void blend_pose_offset_bone(bActionStrip *strip, bPose *dst, bPose *src, float srcweight, short mode)
1125 {
1126         /* matching offset bones */
1127         /* take dst offset, and put src on on that location */
1128         
1129         if(strip->offs_bone[0]==0)
1130                 return;
1131         
1132         /* are we also blending with matching bones? */
1133         if(strip->prev && strip->start>=strip->prev->start) {
1134                 bPoseChannel *dpchan= get_pose_channel(dst, strip->offs_bone);
1135                 if(dpchan) {
1136                         bPoseChannel *spchan= get_pose_channel(src, strip->offs_bone);
1137                         if(spchan) {
1138                                 float vec[3];
1139                                 
1140                                 /* dst->ctime has the internal strip->prev action time */
1141                                 /* map this time to nla time */
1142                                 
1143                                 float ctime= get_actionstrip_frame(strip, src->ctime, 1);
1144                                 
1145                                 if( ctime > strip->prev->end) {
1146                                         bActionChannel *achan;
1147                                         
1148                                         /* add src to dest, minus the position of src on strip->prev->end */
1149                                         
1150                                         ctime= get_actionstrip_frame(strip, strip->prev->end, 0);
1151                                         
1152                                         achan= get_action_channel(strip->act, strip->offs_bone);
1153                                         if(achan && achan->ipo) {
1154                                                 bPoseChannel pchan;
1155                                                 /* Evaluates and sets the internal ipo value */
1156                                                 calc_ipo(achan->ipo, ctime);
1157                                                 /* This call also sets the pchan flags */
1158                                                 execute_action_ipo(achan, &pchan);
1159                                                 
1160                                                 /* store offset that moves src to location of pchan */
1161                                                 VecSubf(vec, dpchan->loc, pchan.loc);
1162                                                 
1163                                                 Mat4Mul3Vecfl(dpchan->bone->arm_mat, vec);
1164                                         }
1165                                 }
1166                                 else {
1167                                         /* store offset that moves src to location of dst */
1168                                         
1169                                         VecSubf(vec, dpchan->loc, spchan->loc);
1170                                         Mat4Mul3Vecfl(dpchan->bone->arm_mat, vec);
1171                                 }
1172                                 
1173                                 /* if blending, we only add with factor scrweight */
1174                                 VecMulf(vec, srcweight);
1175                                 
1176                                 VecAddf(dst->cyclic_offset, dst->cyclic_offset, vec);
1177                         }
1178                 }
1179         }
1180         
1181         VecAddf(dst->cyclic_offset, dst->cyclic_offset, src->cyclic_offset);
1182 }
1183
1184 /* added "sizecorr" here, to allow armatures to be scaled and still have striding.
1185    Only works for uniform scaling. In general I'd advise against scaling armatures ever though! (ton)
1186 */
1187 static float stridechannel_frame(Object *ob, float sizecorr, bActionStrip *strip, Path *path, float pathdist, float *stride_offset)
1188 {
1189         bAction *act= strip->act;
1190         const char *name= strip->stridechannel;
1191         bActionChannel *achan= get_action_channel(act, name);
1192         int stride_axis= strip->stride_axis;
1193
1194         if(achan && achan->ipo) {
1195                 IpoCurve *icu= NULL;
1196                 float minx=0.0f, maxx=0.0f, miny=0.0f, maxy=0.0f;
1197                 int foundvert= 0;
1198                 
1199                 if(stride_axis==0) stride_axis= AC_LOC_X;
1200                 else if(stride_axis==1) stride_axis= AC_LOC_Y;
1201                 else stride_axis= AC_LOC_Z;
1202                 
1203                 /* calculate the min/max */
1204                 for (icu=achan->ipo->curve.first; icu; icu=icu->next) {
1205                         if(icu->adrcode==stride_axis) {
1206                                 if(icu->totvert>1) {
1207                                         foundvert= 1;
1208                                         minx= icu->bezt[0].vec[1][0];
1209                                         maxx= icu->bezt[icu->totvert-1].vec[1][0];
1210                                         
1211                                         miny= icu->bezt[0].vec[1][1];
1212                                         maxy= icu->bezt[icu->totvert-1].vec[1][1];
1213                                 }
1214                                 break;
1215                         }
1216                 }
1217                 
1218                 if(foundvert && miny!=maxy) {
1219                         float stridelen= sizecorr*fabs(maxy-miny), striptime;
1220                         float actiondist, pdist, pdistNewNormalized, offs;
1221                         float vec1[4], vec2[4], dir[3];
1222                         
1223                         /* internal cycling, actoffs is in frames */
1224                         offs= stridelen*strip->actoffs/(maxx-minx);
1225                         
1226                         /* amount path moves object */
1227                         pdist = (float)fmod (pathdist+offs, stridelen);
1228                         striptime= pdist/stridelen;
1229                         
1230                         /* amount stride bone moves */
1231                         actiondist= sizecorr*eval_icu(icu, minx + striptime*(maxx-minx)) - miny;
1232                         
1233                         pdist = fabs(actiondist) - pdist;
1234                         pdistNewNormalized = (pathdist+pdist)/path->totdist;
1235                         
1236                         /* now we need to go pdist further (or less) on cu path */
1237                         where_on_path(ob, (pathdist)/path->totdist, vec1, dir); /* vec needs size 4 */
1238                         if (pdistNewNormalized <= 1) {
1239                                 // search for correction in positive path-direction
1240                                 where_on_path(ob, pdistNewNormalized, vec2, dir);       /* vec needs size 4 */
1241                                 VecSubf(stride_offset, vec2, vec1);
1242                         }
1243                         else {
1244                                 // we reached the end of the path, search backwards instead
1245                                 where_on_path(ob, (pathdist-pdist)/path->totdist, vec2, dir);   /* vec needs size 4 */
1246                                 VecSubf(stride_offset, vec1, vec2);
1247                         }
1248                         Mat4Mul3Vecfl(ob->obmat, stride_offset);
1249                         return striptime;
1250                 }
1251         }
1252         return 0.0f;
1253 }
1254
1255 static void cyclic_offs_bone(Object *ob, bPose *pose, bActionStrip *strip, float time)
1256 {
1257         /* only called when strip has cyclic, so >= 1.0f works... */
1258         if(time >= 1.0f) {
1259                 bActionChannel *achan= get_action_channel(strip->act, strip->offs_bone);
1260
1261                 if(achan && achan->ipo) {
1262                         IpoCurve *icu= NULL;
1263                         Bone *bone;
1264                         float min[3]={0.0f, 0.0f, 0.0f}, max[3]={0.0f, 0.0f, 0.0f};
1265                         int index=0, foundvert= 0;
1266                         
1267                         /* calculate the min/max */
1268                         for (icu=achan->ipo->curve.first; icu; icu=icu->next) {
1269                                 if(icu->totvert>1) {
1270                                         
1271                                         if(icu->adrcode==AC_LOC_X)
1272                                                 index= 0;
1273                                         else if(icu->adrcode==AC_LOC_Y)
1274                                                 index= 1;
1275                                         else if(icu->adrcode==AC_LOC_Z)
1276                                                 index= 2;
1277                                         else
1278                                                 continue;
1279                                 
1280                                         foundvert= 1;
1281                                         min[index]= icu->bezt[0].vec[1][1];
1282                                         max[index]= icu->bezt[icu->totvert-1].vec[1][1];
1283                                 }
1284                         }
1285                         if(foundvert) {
1286                                 /* bring it into armature space */
1287                                 VecSubf(min, max, min);
1288                                 bone= get_named_bone(ob->data, strip->offs_bone);       /* weak */
1289                                 if(bone) {
1290                                         Mat4Mul3Vecfl(bone->arm_mat, min);
1291                                         
1292                                         /* dominant motion, cyclic_offset was cleared in rest_pose */
1293                                         if (strip->flag & (ACTSTRIP_CYCLIC_USEX | ACTSTRIP_CYCLIC_USEY | ACTSTRIP_CYCLIC_USEZ)) {
1294                                                 if (strip->flag & ACTSTRIP_CYCLIC_USEX) pose->cyclic_offset[0]= time*min[0];
1295                                                 if (strip->flag & ACTSTRIP_CYCLIC_USEY) pose->cyclic_offset[1]= time*min[1];
1296                                                 if (strip->flag & ACTSTRIP_CYCLIC_USEZ) pose->cyclic_offset[2]= time*min[2];
1297                                         } else {
1298                                                 if( fabs(min[0]) >= fabs(min[1]) && fabs(min[0]) >= fabs(min[2]))
1299                                                         pose->cyclic_offset[0]= time*min[0];
1300                                                 else if( fabs(min[1]) >= fabs(min[0]) && fabs(min[1]) >= fabs(min[2]))
1301                                                         pose->cyclic_offset[1]= time*min[1];
1302                                                 else
1303                                                         pose->cyclic_offset[2]= time*min[2];
1304                                         }
1305                                 }
1306                         }
1307                 }
1308         }
1309 }
1310
1311 /* simple case for now; only the curve path with constraint value > 0.5 */
1312 /* blending we might do later... */
1313 static Object *get_parent_path(Object *ob)
1314 {
1315         bConstraint *con;
1316         
1317         if(ob->parent && ob->parent->type==OB_CURVE)
1318                 return ob->parent;
1319         
1320         for (con = ob->constraints.first; con; con=con->next) {
1321                 if(con->type==CONSTRAINT_TYPE_FOLLOWPATH) {
1322                         if(con->enforce>0.5f) {
1323                                 bFollowPathConstraint *data= con->data;
1324                                 return data->tar;
1325                         }
1326                 }
1327         }
1328         return NULL;
1329 }
1330
1331 /* ************** do the action ************ */
1332
1333 /* ----- nla, etc. --------- */
1334
1335 static void do_nla(Scene *scene, Object *ob, int blocktype)
1336 {
1337         bPose *tpose= NULL;
1338         Key *key= NULL;
1339         ListBase tchanbase={NULL, NULL}, chanbase={NULL, NULL};
1340         bActionStrip *strip, *striplast=NULL, *stripfirst=NULL;
1341         float striptime, frametime, length, actlength;
1342         float blendfac, stripframe;
1343         float scene_cfra= frame_to_float(scene, scene->r.cfra); 
1344         int     doit, dostride;
1345         
1346         if(blocktype==ID_AR) {
1347                 copy_pose(&tpose, ob->pose, 1);
1348                 rest_pose(ob->pose);            // potentially destroying current not-keyed pose
1349         }
1350         else {
1351                 key= ob_get_key(ob);
1352         }
1353         
1354         /* check on extend to left or right, when no strip is hit by 'cfra' */
1355         for (strip=ob->nlastrips.first; strip; strip=strip->next) {
1356                 /* escape loop on a hit */
1357                 if( scene_cfra >= strip->start && scene_cfra <= strip->end + 0.1f)      /* note 0.1 comes back below */
1358                         break;
1359                 if(scene_cfra < strip->start) {
1360                         if(stripfirst==NULL)
1361                                 stripfirst= strip;
1362                         else if(stripfirst->start > strip->start)
1363                                 stripfirst= strip;
1364                 }
1365                 else if(scene_cfra > strip->end) {
1366                         if(striplast==NULL)
1367                                 striplast= strip;
1368                         else if(striplast->end < strip->end)
1369                                 striplast= strip;
1370                 }
1371         }
1372         if(strip==NULL) {       /* extend */
1373                 if(striplast)
1374                         scene_cfra= striplast->end;
1375                 else if(stripfirst)
1376                         scene_cfra= stripfirst->start;
1377         }
1378         
1379         /* and now go over all strips */
1380         for (strip=ob->nlastrips.first; strip; strip=strip->next){
1381                 doit=dostride= 0;
1382                 
1383                 if (strip->act && !(strip->flag & ACTSTRIP_MUTE)) {     /* so theres an action */
1384                         
1385                         /* Determine if the current frame is within the strip's range */
1386                         length = strip->end-strip->start;
1387                         actlength = strip->actend-strip->actstart;
1388                         striptime = (scene_cfra-(strip->start)) / length;
1389                         stripframe = (scene_cfra-(strip->start)) ;
1390
1391                         if (striptime>=0.0){
1392                                 
1393                                 if(blocktype==ID_AR) 
1394                                         rest_pose(tpose);
1395                                 
1396                                 /* To handle repeat, we add 0.1 frame extra to make sure the last frame is included */
1397                                 if (striptime < 1.0f + 0.1f/length) {
1398                                         
1399                                         /* Handle path */
1400                                         if ((strip->flag & ACTSTRIP_USESTRIDE) && (blocktype==ID_AR) && (ob->ipoflag & OB_DISABLE_PATH)==0){
1401                                                 Object *parent= get_parent_path(ob);
1402                                                 
1403                                                 if (parent) {
1404                                                         Curve *cu = parent->data;
1405                                                         float ctime, pdist;
1406                                                         
1407                                                         if (cu->flag & CU_PATH){
1408                                                                 /* Ensure we have a valid path */
1409                                                                 if(cu->path==NULL || cu->path->data==NULL) makeDispListCurveTypes(scene, parent, 0);
1410                                                                 if(cu->path) {
1411                                                                         
1412                                                                         /* Find the position on the path */
1413                                                                         ctime= bsystem_time(scene, ob, scene_cfra, 0.0);
1414                                                                         
1415                                                                         if(calc_ipo_spec(cu->ipo, CU_SPEED, &ctime)==0) {
1416                                                                                 /* correct for actions not starting on zero */
1417                                                                                 ctime= (ctime - strip->actstart)/cu->pathlen;
1418                                                                                 CLAMP(ctime, 0.0, 1.0);
1419                                                                         }
1420                                                                         pdist = ctime*cu->path->totdist;
1421                                                                         
1422                                                                         if(tpose && strip->stridechannel[0]) {
1423                                                                                 striptime= stridechannel_frame(parent, ob->size[0], strip, cu->path, pdist, tpose->stride_offset);
1424                                                                         }                                                                       
1425                                                                         else {
1426                                                                                 if (strip->stridelen) {
1427                                                                                         striptime = pdist / strip->stridelen;
1428                                                                                         striptime = (float)fmod (striptime+strip->actoffs, 1.0);
1429                                                                                 }
1430                                                                                 else
1431                                                                                         striptime = 0;
1432                                                                         }
1433                                                                         
1434                                                                         frametime = (striptime * actlength) + strip->actstart;
1435                                                                         frametime= bsystem_time(scene, ob, frametime, 0.0);
1436                                                                         
1437                                                                         if(blocktype==ID_AR) {
1438                                                                                 extract_pose_from_action (tpose, strip->act, frametime);
1439                                                                         }
1440                                                                         else if(blocktype==ID_OB) {
1441                                                                                 extract_ipochannels_from_action(&tchanbase, &ob->id, strip->act, "Object", frametime);
1442                                                                                 if(key)
1443                                                                                         extract_ipochannels_from_action(&tchanbase, &key->id, strip->act, "Shape", frametime);
1444                                                                         }
1445                                                                         doit=dostride= 1;
1446                                                                 }
1447                                                         }
1448                                                 }
1449                                         }
1450                                         /* To handle repeat, we add 0.1 frame extra to make sure the last frame is included */
1451                                         else  {
1452                                                 
1453                                                 /* Mod to repeat */
1454                                                 if(strip->repeat!=1.0f) {
1455                                                         float cycle= striptime*strip->repeat;
1456                                                         
1457                                                         striptime = (float)fmod (cycle, 1.0f + 0.1f/length);
1458                                                         cycle-= striptime;
1459                                                         
1460                                                         if(blocktype==ID_AR)
1461                                                                 cyclic_offs_bone(ob, tpose, strip, cycle);
1462                                                 }
1463
1464                                                 frametime = (striptime * actlength) + strip->actstart;
1465                                                 frametime= nla_time(scene, frametime, (float)strip->repeat);
1466                                                         
1467                                                 if(blocktype==ID_AR) {
1468                                                         extract_pose_from_action (tpose, strip->act, frametime);
1469                                                 }
1470                                                 else if(blocktype==ID_OB) {
1471                                                         extract_ipochannels_from_action(&tchanbase, &ob->id, strip->act, "Object", frametime);
1472                                                         if(key)
1473                                                                 extract_ipochannels_from_action(&tchanbase, &key->id, strip->act, "Shape", frametime);
1474                                                 }
1475                                                 
1476                                                 doit=1;
1477                                         }
1478                                 }
1479                                 /* Handle extend */
1480                                 else {
1481                                         if (strip->flag & ACTSTRIP_HOLDLASTFRAME){
1482                                                 /* we want the strip to hold on the exact fraction of the repeat value */
1483                                                 
1484                                                 frametime = actlength * (strip->repeat-(int)strip->repeat);
1485                                                 if(frametime<=0.000001f) frametime= actlength;  /* rounding errors... */
1486                                                 frametime= bsystem_time(scene, ob, frametime+strip->actstart, 0.0);
1487                                                 
1488                                                 if(blocktype==ID_AR)
1489                                                         extract_pose_from_action (tpose, strip->act, frametime);
1490                                                 else if(blocktype==ID_OB) {
1491                                                         extract_ipochannels_from_action(&tchanbase, &ob->id, strip->act, "Object", frametime);
1492                                                         if(key)
1493                                                                 extract_ipochannels_from_action(&tchanbase, &key->id, strip->act, "Shape", frametime);
1494                                                 }
1495                                                 
1496                                                 /* handle cycle hold */
1497                                                 if(strip->repeat!=1.0f) {
1498                                                         if(blocktype==ID_AR)
1499                                                                 cyclic_offs_bone(ob, tpose, strip, strip->repeat-1.0f);
1500                                                 }
1501                                                 
1502                                                 doit=1;
1503                                         }
1504                                 }
1505                                 
1506                                 /* Handle blendin & blendout */
1507                                 if (doit){
1508                                         /* Handle blendin */
1509                                         
1510                                         if (strip->blendin>0.0 && stripframe<=strip->blendin && scene_cfra>=strip->start){
1511                                                 blendfac = stripframe/strip->blendin;
1512                                         }
1513                                         else if (strip->blendout>0.0 && stripframe>=(length-strip->blendout) && scene_cfra<=strip->end){
1514                                                 blendfac = (length-stripframe)/(strip->blendout);
1515                                         }
1516                                         else
1517                                                 blendfac = 1;
1518                                         
1519                                         if(blocktype==ID_AR) {/* Blend this pose with the accumulated pose */
1520                                                 /* offset bone, for matching cycles */
1521                                                 blend_pose_offset_bone (strip, ob->pose, tpose, blendfac, strip->mode);
1522                                                 
1523                                                 blend_poses (ob->pose, tpose, blendfac, strip->mode);
1524                                                 if(dostride)
1525                                                         blend_pose_strides (ob->pose, tpose, blendfac, strip->mode);
1526                                         }
1527                                         else {
1528                                                 blend_ipochannels(&chanbase, &tchanbase, blendfac, strip->mode);
1529                                                 BLI_freelistN(&tchanbase);
1530                                         }
1531                                 }
1532                         }                                       
1533                 }
1534         }
1535         
1536         if(blocktype==ID_OB) {
1537                 execute_ipochannels(&chanbase);
1538         }
1539         else if(blocktype==ID_AR) {
1540                 /* apply stride offset to object */
1541                 VecAddf(ob->obmat[3], ob->obmat[3], ob->pose->stride_offset);
1542         }
1543         
1544         /* free */
1545         if (tpose)
1546                 free_pose(tpose);
1547         if(chanbase.first)
1548                 BLI_freelistN(&chanbase);
1549 }
1550
1551 #endif // XXX OLD ANIMATION SYSTEM (TO BE REMOVED)