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