Merge from trunk -r 23000:23968.
[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] = chan->rotAxis[1]= 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         
515         outPose->iksolver = src->iksolver;
516         outPose->ikdata = NULL;
517         outPose->ikparam = MEM_dupallocN(src->ikparam);
518         
519         // TODO: rename this argument...
520         if (copycon) {
521                 for (pchan=outPose->chanbase.first; pchan; pchan=pchan->next) {
522                         copy_constraints(&listb, &pchan->constraints);  // copy_constraints NULLs listb
523                         pchan->constraints= listb;
524                         pchan->path= NULL;
525                 }
526                 
527                 /* for now, duplicate Bone Groups too when doing this */
528                 BLI_duplicatelist(&outPose->agroups, &src->agroups);
529         }
530         
531         *dst=outPose;
532 }
533
534 void init_pose_itasc(bItasc *itasc)
535 {
536         if (itasc) {
537                 itasc->iksolver = IKSOLVER_ITASC;
538                 itasc->minstep = 0.01f;
539                 itasc->maxstep = 0.06f;
540                 itasc->numiter = 100;
541                 itasc->numstep = 4;
542                 itasc->precision = 0.005f;
543                 itasc->flag = ITASC_AUTO_STEP|ITASC_INITIAL_REITERATION;
544                 itasc->feedback = 20.f;
545                 itasc->maxvel = 50.f;
546                 itasc->solver = ITASC_SOLVER_SDLS;
547                 itasc->dampmax = 0.5;
548                 itasc->dampeps = 0.15;
549         }
550 }
551 void init_pose_ikparam(bPose *pose)
552 {
553         bItasc *itasc;
554         switch (pose->iksolver) {
555         case IKSOLVER_ITASC:
556                 itasc = MEM_callocN(sizeof(bItasc), "itasc");
557                 init_pose_itasc(itasc);
558                 pose->ikparam = itasc;
559                 break;
560         case IKSOLVER_LEGACY:
561         default:
562                 pose->ikparam = NULL;
563                 break;
564         }
565 }
566
567 void free_pose_channels(bPose *pose) 
568 {
569         bPoseChannel *pchan;
570         
571         if (pose->chanbase.first) {
572                 for (pchan = pose->chanbase.first; pchan; pchan=pchan->next){
573                         if (pchan->path)
574                                 MEM_freeN(pchan->path);
575                         free_constraints(&pchan->constraints);
576                 }
577                 BLI_freelistN(&pose->chanbase);
578         }
579 }
580
581 void free_pose(bPose *pose)
582 {
583         if (pose) {
584                 /* free pose-channels */
585                 free_pose_channels(pose);
586                 
587                 /* free pose-groups */
588                 if (pose->agroups.first)
589                         BLI_freelistN(&pose->agroups);
590
591                 /* free IK solver state */
592                 BIK_clear_data(pose);
593
594                 /* free IK solver param */
595                 if (pose->ikparam)
596                         MEM_freeN(pose->ikparam);
597
598                 /* free pose */
599                 MEM_freeN(pose);
600         }
601 }
602
603 static void copy_pose_channel_data(bPoseChannel *pchan, const bPoseChannel *chan)
604 {
605         bConstraint *pcon, *con;
606         
607         VECCOPY(pchan->loc, chan->loc);
608         VECCOPY(pchan->size, chan->size);
609         VECCOPY(pchan->eul, chan->eul);
610         VECCOPY(pchan->rotAxis, chan->rotAxis);
611         pchan->rotAngle= chan->rotAngle;
612         QUATCOPY(pchan->quat, chan->quat);
613         pchan->rotmode= chan->rotmode;
614         Mat4CpyMat4(pchan->chan_mat, (float(*)[4])chan->chan_mat);
615         Mat4CpyMat4(pchan->pose_mat, (float(*)[4])chan->pose_mat);
616         pchan->flag= chan->flag;
617         
618         con= chan->constraints.first;
619         for(pcon= pchan->constraints.first; pcon; pcon= pcon->next, con= con->next) {
620                 pcon->enforce= con->enforce;
621                 pcon->headtail= con->headtail;
622         }
623 }
624
625 /* checks for IK constraint, and also for Follow-Path constraint.
626  * can do more constraints flags later 
627  */
628 /* pose should be entirely OK */
629 void update_pose_constraint_flags(bPose *pose)
630 {
631         bPoseChannel *pchan, *parchan;
632         bConstraint *con;
633         
634         /* clear */
635         for (pchan= pose->chanbase.first; pchan; pchan= pchan->next) {
636                 pchan->constflag= 0;
637         }
638         pose->flag &= ~POSE_CONSTRAINTS_TIMEDEPEND;
639         
640         /* detect */
641         for (pchan= pose->chanbase.first; pchan; pchan=pchan->next) {
642                 for (con= pchan->constraints.first; con; con= con->next) {
643                         if (con->type==CONSTRAINT_TYPE_KINEMATIC) {
644                                 bKinematicConstraint *data = (bKinematicConstraint*)con->data;
645                                 
646                                 pchan->constflag |= PCHAN_HAS_IK;
647                                 
648                                 if(data->tar==NULL || (data->tar->type==OB_ARMATURE && data->subtarget[0]==0))
649                                         pchan->constflag |= PCHAN_HAS_TARGET;
650                                 
651                                 /* negative rootbone = recalc rootbone index. used in do_versions */
652                                 if(data->rootbone<0) {
653                                         data->rootbone= 0;
654                                         
655                                         if(data->flag & CONSTRAINT_IK_TIP) parchan= pchan;
656                                         else parchan= pchan->parent;
657                                         
658                                         while(parchan) {
659                                                 data->rootbone++;
660                                                 if((parchan->bone->flag & BONE_CONNECTED)==0)
661                                                         break;
662                                                 parchan= parchan->parent;
663                                         }
664                                 }
665                         }
666                         else if (con->type == CONSTRAINT_TYPE_FOLLOWPATH) {
667                                 bFollowPathConstraint *data= (bFollowPathConstraint *)con->data;
668                                 
669                                 /* for drawing constraint colors when color set allows this */
670                                 pchan->constflag |= PCHAN_HAS_CONST;
671                                 
672                                 /* if we have a valid target, make sure that this will get updated on frame-change
673                                  * (needed for when there is no anim-data for this pose)
674                                  */
675                                 if ((data->tar) && (data->tar->type==OB_CURVE))
676                                         pose->flag |= POSE_CONSTRAINTS_TIMEDEPEND;
677                         }
678                         else 
679                                 pchan->constflag |= PCHAN_HAS_CONST;
680                 }
681         }
682 }
683
684 /* Clears all BONE_UNKEYED flags for every pose channel in every pose 
685  * This should only be called on frame changing, when it is acceptable to
686  * do this. Otherwise, these flags should not get cleared as poses may get lost.
687  */
688 void framechange_poses_clear_unkeyed(void)
689 {
690         Object *ob;
691         bPose *pose;
692         bPoseChannel *pchan;
693         
694         /* This needs to be done for each object that has a pose */
695         // TODO: proxies may/may not be correctly handled here... (this needs checking) 
696         for (ob= G.main->object.first; ob; ob= ob->id.next) {
697                 /* we only need to do this on objects with a pose */
698                 if ( (pose= ob->pose) ) {
699                         for (pchan= pose->chanbase.first; pchan; pchan= pchan->next) {
700                                 if (pchan->bone) 
701                                         pchan->bone->flag &= ~BONE_UNKEYED;
702                         }
703                 }
704         }
705 }
706
707 /* ************************** Bone Groups ************************** */
708
709 /* Adds a new bone-group */
710 void pose_add_group (Object *ob)
711 {
712         bPose *pose= (ob) ? ob->pose : NULL;
713         bActionGroup *grp;
714         
715         if (ELEM(NULL, ob, ob->pose))
716                 return;
717         
718         grp= MEM_callocN(sizeof(bActionGroup), "PoseGroup");
719         strcpy(grp->name, "Group");
720         BLI_addtail(&pose->agroups, grp);
721         BLI_uniquename(&pose->agroups, grp, "Group", '.', offsetof(bActionGroup, name), 32);
722         
723         pose->active_group= BLI_countlist(&pose->agroups);
724 }
725
726 /* Remove the active bone-group */
727 void pose_remove_group (Object *ob)
728 {
729         bPose *pose= (ob) ? ob->pose : NULL;
730         bActionGroup *grp = NULL;
731         bPoseChannel *pchan;
732         
733         /* sanity checks */
734         if (ELEM(NULL, ob, pose))
735                 return;
736         if (pose->active_group <= 0)
737                 return;
738         
739         /* get group to remove */
740         grp= BLI_findlink(&pose->agroups, pose->active_group-1);
741         if (grp) {
742                 /* adjust group references (the trouble of using indices!):
743                  *      - firstly, make sure nothing references it 
744                  *      - also, make sure that those after this item get corrected
745                  */
746                 for (pchan= pose->chanbase.first; pchan; pchan= pchan->next) {
747                         if (pchan->agrp_index == pose->active_group)
748                                 pchan->agrp_index= 0;
749                         else if (pchan->agrp_index > pose->active_group)
750                                 pchan->agrp_index--;
751                 }
752                 
753                 /* now, remove it from the pose */
754                 BLI_freelinkN(&pose->agroups, grp);
755                 pose->active_group= 0;
756         }
757 }
758
759 /* ************** F-Curve Utilities for Actions ****************** */
760
761 /* Check if the given action has any keyframes */
762 short action_has_motion(const bAction *act)
763 {
764         FCurve *fcu;
765         
766         /* return on the first F-Curve that has some keyframes/samples defined */
767         if (act) {
768                 for (fcu= act->curves.first; fcu; fcu= fcu->next) {
769                         if (fcu->totvert)
770                                 return 1;
771                 }
772         }
773         
774         /* nothing found */
775         return 0;
776 }
777
778 /* Calculate the extents of given action */
779 void calc_action_range(const bAction *act, float *start, float *end, short incl_modifiers)
780 {
781         FCurve *fcu;
782         float min=999999999.0f, max=-999999999.0f;
783         short foundvert=0, foundmod=0;
784
785         if (act) {
786                 for (fcu= act->curves.first; fcu; fcu= fcu->next) {
787                         /* if curve has keyframes, consider them first */
788                         if (fcu->totvert) {
789                                 float nmin, nmax;
790                                 
791                                 /* get extents for this curve */
792                                 calc_fcurve_range(fcu, &nmin, &nmax);
793                                 
794                                 /* compare to the running tally */
795                                 min= MIN2(min, nmin);
796                                 max= MAX2(max, nmax);
797                                 
798                                 foundvert= 1;
799                         }
800                         
801                         /* if incl_modifiers is enabled, need to consider modifiers too
802                          *      - only really care about the last modifier
803                          */
804                         if ((incl_modifiers) && (fcu->modifiers.last)) {
805                                 FModifier *fcm= fcu->modifiers.last;
806                                 
807                                 /* only use the maximum sensible limits of the modifiers if they are more extreme */
808                                 switch (fcm->type) {
809                                         case FMODIFIER_TYPE_LIMITS: /* Limits F-Modifier */
810                                         {
811                                                 FMod_Limits *fmd= (FMod_Limits *)fcm->data;
812                                                 
813                                                 if (fmd->flag & FCM_LIMIT_XMIN) {
814                                                         min= MIN2(min, fmd->rect.xmin);
815                                                 }
816                                                 if (fmd->flag & FCM_LIMIT_XMAX) {
817                                                         max= MAX2(max, fmd->rect.xmax);
818                                                 }
819                                         }
820                                                 break;
821                                                 
822                                         case FMODIFIER_TYPE_CYCLES: /* Cycles F-Modifier */
823                                         {
824                                                 FMod_Cycles *fmd= (FMod_Cycles *)fcm->data;
825                                                 
826                                                 if (fmd->before_mode != FCM_EXTRAPOLATE_NONE)
827                                                         min= MINAFRAMEF;
828                                                 if (fmd->after_mode != FCM_EXTRAPOLATE_NONE)
829                                                         max= MAXFRAMEF;
830                                         }
831                                                 break;
832                                                 
833                                         // TODO: function modifier may need some special limits
834                                                 
835                                         default: /* all other standard modifiers are on the infinite range... */
836                                                 min= MINAFRAMEF;
837                                                 max= MAXFRAMEF;
838                                                 break;
839                                 }
840                                 
841                                 foundmod= 1;
842                         }
843                 }
844         }       
845         
846         if (foundvert || foundmod) {
847                 if(min==max) max+= 1.0f;
848                 *start= min;
849                 *end= max;
850         }
851         else {
852                 *start= 0.0f;
853                 *end= 1.0f;
854         }
855 }
856
857 /* Return flags indicating which transforms the given object/posechannel has 
858  *      - if 'curves' is provided, a list of links to these curves are also returned
859  */
860 short action_get_item_transforms (bAction *act, Object *ob, bPoseChannel *pchan, ListBase *curves)
861 {
862         PointerRNA ptr;
863         FCurve *fcu;
864         char *basePath=NULL;
865         short flags=0;
866         
867         /* build PointerRNA from provided data to obtain the paths to use */
868         if (pchan)
869                 RNA_pointer_create((ID *)ob, &RNA_PoseChannel, pchan, &ptr);
870         else if (ob)
871                 RNA_id_pointer_create((ID *)ob, &ptr);
872         else    
873                 return 0;
874                 
875         /* get the basic path to the properties of interest */
876         basePath= RNA_path_from_ID_to_struct(&ptr);
877         if (basePath == NULL)
878                 return 0;
879                 
880         /* search F-Curves for the given properties 
881          *      - we cannot use the groups, since they may not be grouped in that way...
882          */
883         for (fcu= act->curves.first; fcu; fcu= fcu->next) {
884                 char *bPtr=NULL, *pPtr=NULL;
885                 
886                 /* if enough flags have been found, we can stop checking unless we're also getting the curves */
887                 if ((flags == ACT_TRANS_ALL) && (curves == NULL))
888                         break;
889                         
890                 /* just in case... */
891                 if (fcu->rna_path == NULL)
892                         continue;
893                 
894                 /* step 1: check for matching base path */
895                 bPtr= strstr(fcu->rna_path, basePath);
896                 
897                 if (bPtr) {
898                         /* step 2: check for some property with transforms 
899                          *      - to speed things up, only check for the ones not yet found 
900                          *        unless we're getting the curves too
901                          *      - if we're getting the curves, the BLI_genericNodeN() creates a LinkData
902                          *        node wrapping the F-Curve, which then gets added to the list
903                          *      - once a match has been found, the curve cannot possibly be any other one
904                          */
905                         if ((curves) || (flags & ACT_TRANS_LOC) == 0) {
906                                 pPtr= strstr(fcu->rna_path, "location");
907                                 if ((pPtr) && (pPtr >= bPtr)) {
908                                         flags |= ACT_TRANS_LOC;
909                                         
910                                         if (curves) 
911                                                 BLI_addtail(curves, BLI_genericNodeN(fcu));
912                                         continue;
913                                 }
914                         }
915                         
916                         if ((curves) || (flags & ACT_TRANS_SCALE) == 0) {
917                                 pPtr= strstr(fcu->rna_path, "scale");
918                                 if ((pPtr) && (pPtr >= bPtr)) {
919                                         flags |= ACT_TRANS_SCALE;
920                                         
921                                         if (curves) 
922                                                 BLI_addtail(curves, BLI_genericNodeN(fcu));
923                                         continue;
924                                 }
925                         }
926                         
927                         if ((curves) || (flags & ACT_TRANS_ROT) == 0) {
928                                 pPtr= strstr(fcu->rna_path, "rotation");
929                                 if ((pPtr) && (pPtr >= bPtr)) {
930                                         flags |= ACT_TRANS_ROT;
931                                         
932                                         if (curves) 
933                                                 BLI_addtail(curves, BLI_genericNodeN(fcu));
934                                         continue;
935                                 }
936                         }
937                 }
938         }
939         
940         /* free basePath */
941         MEM_freeN(basePath);
942         
943         /* return flags found */
944         return flags;
945 }
946
947 /* ************** Pose Management Tools ****************** */
948
949 /* Copy the data from the action-pose (src) into the pose */
950 /* both args are assumed to be valid */
951 /* exported to game engine */
952 /* Note! this assumes both poses are aligned, this isnt always true when dealing with user poses */
953 void extract_pose_from_pose(bPose *pose, const bPose *src)
954 {
955         const bPoseChannel *schan;
956         bPoseChannel *pchan= pose->chanbase.first;
957
958         if (pose==src) {
959                 printf("extract_pose_from_pose source and target are the same\n");
960                 return;
961         }
962
963         for (schan=src->chanbase.first; (schan && pchan); schan=schan->next, pchan= pchan->next) {
964                 copy_pose_channel_data(pchan, schan);
965         }
966 }
967
968 /* for do_all_pose_actions, clears the pose. Now also exported for proxy and tools */
969 void rest_pose(bPose *pose)
970 {
971         bPoseChannel *pchan;
972         int i;
973         
974         if (!pose)
975                 return;
976         
977         memset(pose->stride_offset, 0, sizeof(pose->stride_offset));
978         memset(pose->cyclic_offset, 0, sizeof(pose->cyclic_offset));
979         
980         for (pchan=pose->chanbase.first; pchan; pchan= pchan->next) {
981                 for (i=0; i<3; i++) {
982                         pchan->loc[i]= 0.0f;
983                         pchan->quat[i+1]= 0.0f;
984                         pchan->eul[i]= 0.0f;
985                         pchan->size[i]= 1.0f;
986                         pchan->rotAxis[i]= 0.0f;
987                 }
988                 pchan->quat[0]= pchan->rotAxis[1]= 1.0f;
989                 pchan->rotAngle= 0.0f;
990                 
991                 pchan->flag &= ~(POSE_LOC|POSE_ROT|POSE_SIZE);
992         }
993 }
994
995 /* both poses should be in sync */
996 void copy_pose_result(bPose *to, bPose *from)
997 {
998         bPoseChannel *pchanto, *pchanfrom;
999         
1000         if(to==NULL || from==NULL) {
1001                 printf("pose result copy error\n"); // debug temp
1002                 return;
1003         }
1004
1005         if (to==from) {
1006                 printf("copy_pose_result source and target are the same\n");
1007                 return;
1008         }
1009
1010
1011         for(pchanfrom= from->chanbase.first; pchanfrom; pchanfrom= pchanfrom->next) {
1012                 pchanto= get_pose_channel(to, pchanfrom->name);
1013                 if(pchanto) {
1014                         Mat4CpyMat4(pchanto->pose_mat, pchanfrom->pose_mat);
1015                         Mat4CpyMat4(pchanto->chan_mat, pchanfrom->chan_mat);
1016                         
1017                         /* used for local constraints */
1018                         VECCOPY(pchanto->loc, pchanfrom->loc);
1019                         QUATCOPY(pchanto->quat, pchanfrom->quat);
1020                         VECCOPY(pchanto->eul, pchanfrom->eul);
1021                         VECCOPY(pchanto->size, pchanfrom->size);
1022                         
1023                         VECCOPY(pchanto->pose_head, pchanfrom->pose_head);
1024                         VECCOPY(pchanto->pose_tail, pchanfrom->pose_tail);
1025                         pchanto->flag= pchanfrom->flag;
1026                 }
1027         }
1028 }
1029
1030 /* For the calculation of the effects of an Action at the given frame on an object 
1031  * This is currently only used for the Action Constraint 
1032  */
1033 void what_does_obaction (Scene *scene, Object *ob, Object *workob, bPose *pose, bAction *act, char groupname[], float cframe)
1034 {
1035         bActionGroup *agrp= action_groups_find_named(act, groupname);
1036         
1037         /* clear workob */
1038         clear_workob(workob);
1039         
1040         /* init workob */
1041         Mat4CpyMat4(workob->obmat, ob->obmat);
1042         Mat4CpyMat4(workob->parentinv, ob->parentinv);
1043         Mat4CpyMat4(workob->constinv, ob->constinv);
1044         workob->parent= ob->parent;
1045         workob->track= ob->track;
1046
1047         workob->trackflag= ob->trackflag;
1048         workob->upflag= ob->upflag;
1049         
1050         workob->partype= ob->partype;
1051         workob->par1= ob->par1;
1052         workob->par2= ob->par2;
1053         workob->par3= ob->par3;
1054
1055         workob->constraints.first = ob->constraints.first;
1056         workob->constraints.last = ob->constraints.last;
1057         
1058         workob->pose= pose;     /* need to set pose too, since this is used for both types of Action Constraint */
1059
1060         strcpy(workob->parsubstr, ob->parsubstr);
1061         strcpy(workob->id.name, "OB<ConstrWorkOb>"); /* we don't use real object name, otherwise RNA screws with the real thing */
1062         
1063         /* if we're given a group to use, it's likely to be more efficient (though a bit more dangerous) */
1064         if (agrp) {
1065                 /* specifically evaluate this group only */
1066                 PointerRNA id_ptr;
1067                 
1068                 /* get RNA-pointer for the workob's ID */
1069                 RNA_id_pointer_create(&workob->id, &id_ptr);
1070                 
1071                 /* execute action for this group only */
1072                 animsys_evaluate_action_group(&id_ptr, act, agrp, NULL, cframe);
1073         }
1074         else {
1075                 AnimData adt;
1076                 
1077                 /* init animdata, and attach to workob */
1078                 memset(&adt, 0, sizeof(AnimData));
1079                 workob->adt= &adt;
1080                 
1081                 adt.recalc= ADT_RECALC_ANIM;
1082                 adt.action= act;
1083                 
1084                 /* execute effects of Action on to workob (or it's PoseChannels) */
1085                 BKE_animsys_evaluate_animdata(&workob->id, &adt, cframe, ADT_RECALC_ANIM);
1086         }
1087 }
1088
1089 /* ********** NLA with non-poses works with ipo channels ********** */
1090
1091 #if 0 // XXX OLD ANIMATION SYSTEM (TO BE REMOVED)
1092
1093 /* ************************ Blending with NLA *************** */
1094
1095 static void blend_pose_strides(bPose *dst, bPose *src, float srcweight, short mode)
1096 {
1097         float dstweight;
1098         
1099         switch (mode){
1100                 case ACTSTRIPMODE_BLEND:
1101                         dstweight = 1.0F - srcweight;
1102                         break;
1103                 case ACTSTRIPMODE_ADD:
1104                         dstweight = 1.0F;
1105                         break;
1106                 default :
1107                         dstweight = 1.0F;
1108         }
1109         
1110         VecLerpf(dst->stride_offset, dst->stride_offset, src->stride_offset, srcweight);
1111 }
1112
1113
1114 /* 
1115
1116 bone matching diagram, strips A and B
1117
1118                  .------------------------.
1119                  |         A              |
1120                  '------------------------'
1121                                  .          .             b2
1122                  .          .-------------v----------.
1123                  .              |         B   .          |
1124                  .          '------------------------'
1125                  .          .             .
1126                  .          .             .
1127 offset:          .    0     .    A-B      .  A-b2+B     
1128                  .          .             .
1129
1130 */
1131
1132
1133 static void blend_pose_offset_bone(bActionStrip *strip, bPose *dst, bPose *src, float srcweight, short mode)
1134 {
1135         /* matching offset bones */
1136         /* take dst offset, and put src on on that location */
1137         
1138         if(strip->offs_bone[0]==0)
1139                 return;
1140         
1141         /* are we also blending with matching bones? */
1142         if(strip->prev && strip->start>=strip->prev->start) {
1143                 bPoseChannel *dpchan= get_pose_channel(dst, strip->offs_bone);
1144                 if(dpchan) {
1145                         bPoseChannel *spchan= get_pose_channel(src, strip->offs_bone);
1146                         if(spchan) {
1147                                 float vec[3];
1148                                 
1149                                 /* dst->ctime has the internal strip->prev action time */
1150                                 /* map this time to nla time */
1151                                 
1152                                 float ctime= get_actionstrip_frame(strip, src->ctime, 1);
1153                                 
1154                                 if( ctime > strip->prev->end) {
1155                                         bActionChannel *achan;
1156                                         
1157                                         /* add src to dest, minus the position of src on strip->prev->end */
1158                                         
1159                                         ctime= get_actionstrip_frame(strip, strip->prev->end, 0);
1160                                         
1161                                         achan= get_action_channel(strip->act, strip->offs_bone);
1162                                         if(achan && achan->ipo) {
1163                                                 bPoseChannel pchan;
1164                                                 /* Evaluates and sets the internal ipo value */
1165                                                 calc_ipo(achan->ipo, ctime);
1166                                                 /* This call also sets the pchan flags */
1167                                                 execute_action_ipo(achan, &pchan);
1168                                                 
1169                                                 /* store offset that moves src to location of pchan */
1170                                                 VecSubf(vec, dpchan->loc, pchan.loc);
1171                                                 
1172                                                 Mat4Mul3Vecfl(dpchan->bone->arm_mat, vec);
1173                                         }
1174                                 }
1175                                 else {
1176                                         /* store offset that moves src to location of dst */
1177                                         
1178                                         VecSubf(vec, dpchan->loc, spchan->loc);
1179                                         Mat4Mul3Vecfl(dpchan->bone->arm_mat, vec);
1180                                 }
1181                                 
1182                                 /* if blending, we only add with factor scrweight */
1183                                 VecMulf(vec, srcweight);
1184                                 
1185                                 VecAddf(dst->cyclic_offset, dst->cyclic_offset, vec);
1186                         }
1187                 }
1188         }
1189         
1190         VecAddf(dst->cyclic_offset, dst->cyclic_offset, src->cyclic_offset);
1191 }
1192
1193 /* added "sizecorr" here, to allow armatures to be scaled and still have striding.
1194    Only works for uniform scaling. In general I'd advise against scaling armatures ever though! (ton)
1195 */
1196 static float stridechannel_frame(Object *ob, float sizecorr, bActionStrip *strip, Path *path, float pathdist, float *stride_offset)
1197 {
1198         bAction *act= strip->act;
1199         const char *name= strip->stridechannel;
1200         bActionChannel *achan= get_action_channel(act, name);
1201         int stride_axis= strip->stride_axis;
1202
1203         if(achan && achan->ipo) {
1204                 IpoCurve *icu= NULL;
1205                 float minx=0.0f, maxx=0.0f, miny=0.0f, maxy=0.0f;
1206                 int foundvert= 0;
1207                 
1208                 if(stride_axis==0) stride_axis= AC_LOC_X;
1209                 else if(stride_axis==1) stride_axis= AC_LOC_Y;
1210                 else stride_axis= AC_LOC_Z;
1211                 
1212                 /* calculate the min/max */
1213                 for (icu=achan->ipo->curve.first; icu; icu=icu->next) {
1214                         if(icu->adrcode==stride_axis) {
1215                                 if(icu->totvert>1) {
1216                                         foundvert= 1;
1217                                         minx= icu->bezt[0].vec[1][0];
1218                                         maxx= icu->bezt[icu->totvert-1].vec[1][0];
1219                                         
1220                                         miny= icu->bezt[0].vec[1][1];
1221                                         maxy= icu->bezt[icu->totvert-1].vec[1][1];
1222                                 }
1223                                 break;
1224                         }
1225                 }
1226                 
1227                 if(foundvert && miny!=maxy) {
1228                         float stridelen= sizecorr*fabs(maxy-miny), striptime;
1229                         float actiondist, pdist, pdistNewNormalized, offs;
1230                         float vec1[4], vec2[4], dir[3];
1231                         
1232                         /* internal cycling, actoffs is in frames */
1233                         offs= stridelen*strip->actoffs/(maxx-minx);
1234                         
1235                         /* amount path moves object */
1236                         pdist = (float)fmod (pathdist+offs, stridelen);
1237                         striptime= pdist/stridelen;
1238                         
1239                         /* amount stride bone moves */
1240                         actiondist= sizecorr*eval_icu(icu, minx + striptime*(maxx-minx)) - miny;
1241                         
1242                         pdist = fabs(actiondist) - pdist;
1243                         pdistNewNormalized = (pathdist+pdist)/path->totdist;
1244                         
1245                         /* now we need to go pdist further (or less) on cu path */
1246                         where_on_path(ob, (pathdist)/path->totdist, vec1, dir); /* vec needs size 4 */
1247                         if (pdistNewNormalized <= 1) {
1248                                 // search for correction in positive path-direction
1249                                 where_on_path(ob, pdistNewNormalized, vec2, dir);       /* vec needs size 4 */
1250                                 VecSubf(stride_offset, vec2, vec1);
1251                         }
1252                         else {
1253                                 // we reached the end of the path, search backwards instead
1254                                 where_on_path(ob, (pathdist-pdist)/path->totdist, vec2, dir);   /* vec needs size 4 */
1255                                 VecSubf(stride_offset, vec1, vec2);
1256                         }
1257                         Mat4Mul3Vecfl(ob->obmat, stride_offset);
1258                         return striptime;
1259                 }
1260         }
1261         return 0.0f;
1262 }
1263
1264 static void cyclic_offs_bone(Object *ob, bPose *pose, bActionStrip *strip, float time)
1265 {
1266         /* only called when strip has cyclic, so >= 1.0f works... */
1267         if(time >= 1.0f) {
1268                 bActionChannel *achan= get_action_channel(strip->act, strip->offs_bone);
1269
1270                 if(achan && achan->ipo) {
1271                         IpoCurve *icu= NULL;
1272                         Bone *bone;
1273                         float min[3]={0.0f, 0.0f, 0.0f}, max[3]={0.0f, 0.0f, 0.0f};
1274                         int index=0, foundvert= 0;
1275                         
1276                         /* calculate the min/max */
1277                         for (icu=achan->ipo->curve.first; icu; icu=icu->next) {
1278                                 if(icu->totvert>1) {
1279                                         
1280                                         if(icu->adrcode==AC_LOC_X)
1281                                                 index= 0;
1282                                         else if(icu->adrcode==AC_LOC_Y)
1283                                                 index= 1;
1284                                         else if(icu->adrcode==AC_LOC_Z)
1285                                                 index= 2;
1286                                         else
1287                                                 continue;
1288                                 
1289                                         foundvert= 1;
1290                                         min[index]= icu->bezt[0].vec[1][1];
1291                                         max[index]= icu->bezt[icu->totvert-1].vec[1][1];
1292                                 }
1293                         }
1294                         if(foundvert) {
1295                                 /* bring it into armature space */
1296                                 VecSubf(min, max, min);
1297                                 bone= get_named_bone(ob->data, strip->offs_bone);       /* weak */
1298                                 if(bone) {
1299                                         Mat4Mul3Vecfl(bone->arm_mat, min);
1300                                         
1301                                         /* dominant motion, cyclic_offset was cleared in rest_pose */
1302                                         if (strip->flag & (ACTSTRIP_CYCLIC_USEX | ACTSTRIP_CYCLIC_USEY | ACTSTRIP_CYCLIC_USEZ)) {
1303                                                 if (strip->flag & ACTSTRIP_CYCLIC_USEX) pose->cyclic_offset[0]= time*min[0];
1304                                                 if (strip->flag & ACTSTRIP_CYCLIC_USEY) pose->cyclic_offset[1]= time*min[1];
1305                                                 if (strip->flag & ACTSTRIP_CYCLIC_USEZ) pose->cyclic_offset[2]= time*min[2];
1306                                         } else {
1307                                                 if( fabs(min[0]) >= fabs(min[1]) && fabs(min[0]) >= fabs(min[2]))
1308                                                         pose->cyclic_offset[0]= time*min[0];
1309                                                 else if( fabs(min[1]) >= fabs(min[0]) && fabs(min[1]) >= fabs(min[2]))
1310                                                         pose->cyclic_offset[1]= time*min[1];
1311                                                 else
1312                                                         pose->cyclic_offset[2]= time*min[2];
1313                                         }
1314                                 }
1315                         }
1316                 }
1317         }
1318 }
1319
1320 /* simple case for now; only the curve path with constraint value > 0.5 */
1321 /* blending we might do later... */
1322 static Object *get_parent_path(Object *ob)
1323 {
1324         bConstraint *con;
1325         
1326         if(ob->parent && ob->parent->type==OB_CURVE)
1327                 return ob->parent;
1328         
1329         for (con = ob->constraints.first; con; con=con->next) {
1330                 if(con->type==CONSTRAINT_TYPE_FOLLOWPATH) {
1331                         if(con->enforce>0.5f) {
1332                                 bFollowPathConstraint *data= con->data;
1333                                 return data->tar;
1334                         }
1335                 }
1336         }
1337         return NULL;
1338 }
1339
1340 /* ************** do the action ************ */
1341
1342 /* ----- nla, etc. --------- */
1343
1344 static void do_nla(Scene *scene, Object *ob, int blocktype)
1345 {
1346         bPose *tpose= NULL;
1347         Key *key= NULL;
1348         ListBase tchanbase={NULL, NULL}, chanbase={NULL, NULL};
1349         bActionStrip *strip, *striplast=NULL, *stripfirst=NULL;
1350         float striptime, frametime, length, actlength;
1351         float blendfac, stripframe;
1352         float scene_cfra= frame_to_float(scene, scene->r.cfra); 
1353         int     doit, dostride;
1354         
1355         if(blocktype==ID_AR) {
1356                 copy_pose(&tpose, ob->pose, 1);
1357                 rest_pose(ob->pose);            // potentially destroying current not-keyed pose
1358         }
1359         else {
1360                 key= ob_get_key(ob);
1361         }
1362         
1363         /* check on extend to left or right, when no strip is hit by 'cfra' */
1364         for (strip=ob->nlastrips.first; strip; strip=strip->next) {
1365                 /* escape loop on a hit */
1366                 if( scene_cfra >= strip->start && scene_cfra <= strip->end + 0.1f)      /* note 0.1 comes back below */
1367                         break;
1368                 if(scene_cfra < strip->start) {
1369                         if(stripfirst==NULL)
1370                                 stripfirst= strip;
1371                         else if(stripfirst->start > strip->start)
1372                                 stripfirst= strip;
1373                 }
1374                 else if(scene_cfra > strip->end) {
1375                         if(striplast==NULL)
1376                                 striplast= strip;
1377                         else if(striplast->end < strip->end)
1378                                 striplast= strip;
1379                 }
1380         }
1381         if(strip==NULL) {       /* extend */
1382                 if(striplast)
1383                         scene_cfra= striplast->end;
1384                 else if(stripfirst)
1385                         scene_cfra= stripfirst->start;
1386         }
1387         
1388         /* and now go over all strips */
1389         for (strip=ob->nlastrips.first; strip; strip=strip->next){
1390                 doit=dostride= 0;
1391                 
1392                 if (strip->act && !(strip->flag & ACTSTRIP_MUTE)) {     /* so theres an action */
1393                         
1394                         /* Determine if the current frame is within the strip's range */
1395                         length = strip->end-strip->start;
1396                         actlength = strip->actend-strip->actstart;
1397                         striptime = (scene_cfra-(strip->start)) / length;
1398                         stripframe = (scene_cfra-(strip->start)) ;
1399
1400                         if (striptime>=0.0){
1401                                 
1402                                 if(blocktype==ID_AR) 
1403                                         rest_pose(tpose);
1404                                 
1405                                 /* To handle repeat, we add 0.1 frame extra to make sure the last frame is included */
1406                                 if (striptime < 1.0f + 0.1f/length) {
1407                                         
1408                                         /* Handle path */
1409                                         if ((strip->flag & ACTSTRIP_USESTRIDE) && (blocktype==ID_AR) && (ob->ipoflag & OB_DISABLE_PATH)==0){
1410                                                 Object *parent= get_parent_path(ob);
1411                                                 
1412                                                 if (parent) {
1413                                                         Curve *cu = parent->data;
1414                                                         float ctime, pdist;
1415                                                         
1416                                                         if (cu->flag & CU_PATH){
1417                                                                 /* Ensure we have a valid path */
1418                                                                 if(cu->path==NULL || cu->path->data==NULL) makeDispListCurveTypes(scene, parent, 0);
1419                                                                 if(cu->path) {
1420                                                                         
1421                                                                         /* Find the position on the path */
1422                                                                         ctime= bsystem_time(scene, ob, scene_cfra, 0.0);
1423                                                                         
1424                                                                         if(calc_ipo_spec(cu->ipo, CU_SPEED, &ctime)==0) {
1425                                                                                 /* correct for actions not starting on zero */
1426                                                                                 ctime= (ctime - strip->actstart)/cu->pathlen;
1427                                                                                 CLAMP(ctime, 0.0, 1.0);
1428                                                                         }
1429                                                                         pdist = ctime*cu->path->totdist;
1430                                                                         
1431                                                                         if(tpose && strip->stridechannel[0]) {
1432                                                                                 striptime= stridechannel_frame(parent, ob->size[0], strip, cu->path, pdist, tpose->stride_offset);
1433                                                                         }                                                                       
1434                                                                         else {
1435                                                                                 if (strip->stridelen) {
1436                                                                                         striptime = pdist / strip->stridelen;
1437                                                                                         striptime = (float)fmod (striptime+strip->actoffs, 1.0);
1438                                                                                 }
1439                                                                                 else
1440                                                                                         striptime = 0;
1441                                                                         }
1442                                                                         
1443                                                                         frametime = (striptime * actlength) + strip->actstart;
1444                                                                         frametime= bsystem_time(scene, ob, frametime, 0.0);
1445                                                                         
1446                                                                         if(blocktype==ID_AR) {
1447                                                                                 extract_pose_from_action (tpose, strip->act, frametime);
1448                                                                         }
1449                                                                         else if(blocktype==ID_OB) {
1450                                                                                 extract_ipochannels_from_action(&tchanbase, &ob->id, strip->act, "Object", frametime);
1451                                                                                 if(key)
1452                                                                                         extract_ipochannels_from_action(&tchanbase, &key->id, strip->act, "Shape", frametime);
1453                                                                         }
1454                                                                         doit=dostride= 1;
1455                                                                 }
1456                                                         }
1457                                                 }
1458                                         }
1459                                         /* To handle repeat, we add 0.1 frame extra to make sure the last frame is included */
1460                                         else  {
1461                                                 
1462                                                 /* Mod to repeat */
1463                                                 if(strip->repeat!=1.0f) {
1464                                                         float cycle= striptime*strip->repeat;
1465                                                         
1466                                                         striptime = (float)fmod (cycle, 1.0f + 0.1f/length);
1467                                                         cycle-= striptime;
1468                                                         
1469                                                         if(blocktype==ID_AR)
1470                                                                 cyclic_offs_bone(ob, tpose, strip, cycle);
1471                                                 }
1472
1473                                                 frametime = (striptime * actlength) + strip->actstart;
1474                                                 frametime= nla_time(scene, frametime, (float)strip->repeat);
1475                                                         
1476                                                 if(blocktype==ID_AR) {
1477                                                         extract_pose_from_action (tpose, strip->act, frametime);
1478                                                 }
1479                                                 else if(blocktype==ID_OB) {
1480                                                         extract_ipochannels_from_action(&tchanbase, &ob->id, strip->act, "Object", frametime);
1481                                                         if(key)
1482                                                                 extract_ipochannels_from_action(&tchanbase, &key->id, strip->act, "Shape", frametime);
1483                                                 }
1484                                                 
1485                                                 doit=1;
1486                                         }
1487                                 }
1488                                 /* Handle extend */
1489                                 else {
1490                                         if (strip->flag & ACTSTRIP_HOLDLASTFRAME){
1491                                                 /* we want the strip to hold on the exact fraction of the repeat value */
1492                                                 
1493                                                 frametime = actlength * (strip->repeat-(int)strip->repeat);
1494                                                 if(frametime<=0.000001f) frametime= actlength;  /* rounding errors... */
1495                                                 frametime= bsystem_time(scene, ob, frametime+strip->actstart, 0.0);
1496                                                 
1497                                                 if(blocktype==ID_AR)
1498                                                         extract_pose_from_action (tpose, strip->act, frametime);
1499                                                 else if(blocktype==ID_OB) {
1500                                                         extract_ipochannels_from_action(&tchanbase, &ob->id, strip->act, "Object", frametime);
1501                                                         if(key)
1502                                                                 extract_ipochannels_from_action(&tchanbase, &key->id, strip->act, "Shape", frametime);
1503                                                 }
1504                                                 
1505                                                 /* handle cycle hold */
1506                                                 if(strip->repeat!=1.0f) {
1507                                                         if(blocktype==ID_AR)
1508                                                                 cyclic_offs_bone(ob, tpose, strip, strip->repeat-1.0f);
1509                                                 }
1510                                                 
1511                                                 doit=1;
1512                                         }
1513                                 }
1514                                 
1515                                 /* Handle blendin & blendout */
1516                                 if (doit){
1517                                         /* Handle blendin */
1518                                         
1519                                         if (strip->blendin>0.0 && stripframe<=strip->blendin && scene_cfra>=strip->start){
1520                                                 blendfac = stripframe/strip->blendin;
1521                                         }
1522                                         else if (strip->blendout>0.0 && stripframe>=(length-strip->blendout) && scene_cfra<=strip->end){
1523                                                 blendfac = (length-stripframe)/(strip->blendout);
1524                                         }
1525                                         else
1526                                                 blendfac = 1;
1527                                         
1528                                         if(blocktype==ID_AR) {/* Blend this pose with the accumulated pose */
1529                                                 /* offset bone, for matching cycles */
1530                                                 blend_pose_offset_bone (strip, ob->pose, tpose, blendfac, strip->mode);
1531                                                 
1532                                                 blend_poses (ob->pose, tpose, blendfac, strip->mode);
1533                                                 if(dostride)
1534                                                         blend_pose_strides (ob->pose, tpose, blendfac, strip->mode);
1535                                         }
1536                                         else {
1537                                                 blend_ipochannels(&chanbase, &tchanbase, blendfac, strip->mode);
1538                                                 BLI_freelistN(&tchanbase);
1539                                         }
1540                                 }
1541                         }                                       
1542                 }
1543         }
1544         
1545         if(blocktype==ID_OB) {
1546                 execute_ipochannels(&chanbase);
1547         }
1548         else if(blocktype==ID_AR) {
1549                 /* apply stride offset to object */
1550                 VecAddf(ob->obmat[3], ob->obmat[3], ob->pose->stride_offset);
1551         }
1552         
1553         /* free */
1554         if (tpose)
1555                 free_pose(tpose);
1556         if(chanbase.first)
1557                 BLI_freelistN(&chanbase);
1558 }
1559
1560 #endif // XXX OLD ANIMATION SYSTEM (TO BE REMOVED)