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