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