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