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