Wednesday merger of Orange branch and bf-blender
[blender-staging.git] / source / blender / blenkernel / intern / action.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL 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. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * Contributor(s): Full recode, Ton Roosendaal, Crete 2005
27  *
28  * ***** END GPL/BL DUAL LICENSE BLOCK *****
29  */
30
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
34
35 #include <string.h>
36 #include <math.h>
37 #include <stdlib.h>     /* for NULL */
38
39 #include "MEM_guardedalloc.h"
40
41 #include "DNA_action_types.h"
42 #include "DNA_armature_types.h"
43 #include "DNA_constraint_types.h"
44 #include "DNA_curve_types.h"
45 #include "DNA_ipo_types.h"
46 #include "DNA_key_types.h"
47 #include "DNA_nla_types.h"
48 #include "DNA_object_types.h"
49 #include "DNA_scene_types.h"
50
51 #include "BKE_action.h"
52 #include "BKE_anim.h"
53 #include "BKE_armature.h"
54 #include "BKE_blender.h"
55 #include "BKE_constraint.h"
56 #include "BKE_displist.h"
57 #include "BKE_global.h"
58 #include "BKE_ipo.h"
59 #include "BKE_key.h"
60 #include "BKE_library.h"
61 #include "BKE_main.h"
62 #include "BKE_object.h"
63 #include "BKE_utildefines.h"
64
65 #include "BLI_arithb.h"
66 #include "BLI_blenlib.h"
67
68 #include "nla.h"
69 #include "render.h"
70
71 /* *********************** NOTE ON POSE AND ACTION **********************
72
73   - Pose is the local (object level) component of armature. The current
74     object pose is saved in files, and (will be) is presorted for dependency
75   - Actions have fewer (or other) channels, and write data to a Pose
76   - Currently ob->pose data is controlled in where_is_pose only. The (recalc)
77     event system takes care of calling that
78   - The NLA system (here too) uses Poses as interpolation format for Actions
79   - Therefore we assume poses to be static, and duplicates of poses have channels in
80     same order, for quick interpolation reasons
81
82   ****************************** (ton) ************************************ */
83
84 /* ***************** Library data level operations on action ************** */
85
86 void make_local_action(bAction *act)
87 {
88         Object *ob;
89         bAction *actn;
90         int local=0, lib=0;
91         
92         if(act->id.lib==0) return;
93         if(act->id.us==1) {
94                 act->id.lib= 0;
95                 act->id.flag= LIB_LOCAL;
96                 new_id(0, (ID *)act, 0);
97                 return;
98         }
99         
100         ob= G.main->object.first;
101         while(ob) {
102                 if(ob->action==act) {
103                         if(ob->id.lib) lib= 1;
104                         else local= 1;
105                 }
106                 ob= ob->id.next;
107         }
108         
109         if(local && lib==0) {
110                 act->id.lib= 0;
111                 act->id.flag= LIB_LOCAL;
112                 new_id(0, (ID *)act, 0);
113         }
114         else if(local && lib) {
115                 actn= copy_action(act);
116                 actn->id.us= 0;
117                 
118                 ob= G.main->object.first;
119                 while(ob) {
120                         if(ob->action==act) {
121                                 
122                                 if(ob->id.lib==0) {
123                                         ob->action = actn;
124                                         actn->id.us++;
125                                         act->id.us--;
126                                 }
127                         }
128                         ob= ob->id.next;
129                 }
130         }
131 }
132
133
134 void free_action(bAction *act)
135 {
136         bActionChannel *chan;
137         
138         /* Free channels */
139         for (chan=act->chanbase.first; chan; chan=chan->next){
140                 if (chan->ipo)
141                         chan->ipo->id.us--;
142                 free_constraint_channels(&chan->constraintChannels);
143         }
144         
145         if (act->chanbase.first)
146                 BLI_freelistN (&act->chanbase);
147 }
148
149 bAction* copy_action(bAction *src)
150 {
151         bAction *dst = NULL;
152         bActionChannel *dchan, *schan;
153         
154         if(!src) return NULL;
155         
156         dst= copy_libblock(src);
157         duplicatelist(&(dst->chanbase), &(src->chanbase));
158         
159         for (dchan=dst->chanbase.first, schan=src->chanbase.first; dchan; dchan=dchan->next, schan=schan->next){
160                 dchan->ipo = copy_ipo(dchan->ipo);
161                 copy_constraint_channels(&dchan->constraintChannels, &schan->constraintChannels);
162         }
163         dst->id.flag |= LIB_FAKEUSER;
164         dst->id.us++;
165         return dst;
166 }
167
168
169
170 /* ************************ Pose channels *************** */
171
172 /* usually used within a loop, so we got a N^2 slowdown */
173 bPoseChannel *get_pose_channel(const bPose *pose, const char *name)
174 {
175         bPoseChannel *chan;
176
177         if(pose==NULL) return NULL;
178         
179         for (chan=pose->chanbase.first; chan; chan=chan->next) {
180                 if(chan->name[0] == name[0])
181                         if (!strcmp (chan->name, name))
182                                 return chan;
183         }
184
185         return NULL;
186 }
187
188 /* Use with care, not on Armature poses but for temporal ones */
189 /* (currently used for action constraints and in rebuild_pose) */
190 bPoseChannel *verify_pose_channel(bPose* pose, const char* name)
191 {
192         bPoseChannel *chan;
193         
194         if (!pose){
195                 return NULL;
196         }
197         
198         /*      See if this channel exists */
199         for (chan=pose->chanbase.first; chan; chan=chan->next){
200                 if (!strcmp (name, chan->name))
201                         return chan;
202         }
203         
204         /* If not, create it and add it */
205         chan = MEM_callocN(sizeof(bPoseChannel), "verifyPoseChannel");
206         
207         strncpy (chan->name, name, 31);
208         /* init vars to prevent mat errors */
209         chan->quat[0] = 1.0F;
210         chan->size[0] = chan->size[1] = chan->size[2] = 1.0F;
211         
212         chan->limitmin[0]= chan->limitmin[1]= chan->limitmin[2]= -180.0f;
213         chan->limitmax[0]= chan->limitmax[1]= chan->limitmax[2]= 180.0f;
214         chan->stiffness[0]= chan->stiffness[1]= chan->stiffness[2]= 0.0f;
215         
216         BLI_addtail (&pose->chanbase, chan);
217         
218         return chan;
219 }
220
221
222 /* dst should be freed already, makes entire duplicate */
223 void copy_pose(bPose **dst, bPose *src, int copycon)
224 {
225         bPose *outPose;
226         bPoseChannel    *pchan;
227         ListBase listb;
228         
229         if (!src){
230                 *dst=NULL;
231                 return;
232         }
233         
234         outPose= MEM_callocN(sizeof(bPose), "pose");
235         
236         duplicatelist (&outPose->chanbase, &src->chanbase);
237         
238         if (copycon) {
239                 for (pchan=outPose->chanbase.first; pchan; pchan=pchan->next) {
240                         copy_constraints(&listb, &pchan->constraints);  // copy_constraints NULLs listb
241                         pchan->constraints= listb;
242                         pchan->path= NULL;
243                 }
244         }
245         
246         *dst=outPose;
247 }
248
249 void free_pose_channels(bPose *pose) 
250 {
251         bPoseChannel *pchan;
252         
253         if (pose->chanbase.first){
254                 for (pchan = pose->chanbase.first; pchan; pchan=pchan->next){
255                         if(pchan->path)
256                                 MEM_freeN(pchan->path);
257                         free_constraints(&pchan->constraints);
258                 }
259                 BLI_freelistN (&pose->chanbase);
260         }
261 }
262
263 static void copy_pose_channel_data(bPoseChannel *pchan, const bPoseChannel *chan)
264 {
265         bConstraint *pcon, *con;
266         
267         VECCOPY(pchan->loc, chan->loc);
268         VECCOPY(pchan->size, chan->size);
269         QUATCOPY(pchan->quat, chan->quat);
270         pchan->flag= chan->flag;
271         
272         con= chan->constraints.first;
273         for(pcon= pchan->constraints.first; pcon; pcon= pcon->next)
274                 pcon->enforce= con->enforce;
275 }
276
277 /* checks for IK constraint, can do more constraints flags later */
278 /* pose should be entirely OK */
279 void update_pose_constraint_flags(bPose *pose)
280 {
281         bPoseChannel *pchan, *parchan;
282         bConstraint *con;
283         
284         /* clear */
285         for (pchan = pose->chanbase.first; pchan; pchan=pchan->next) {
286                 pchan->constflag= 0;
287         }
288         /* detect */
289         for (pchan = pose->chanbase.first; pchan; pchan=pchan->next) {
290                 for(con= pchan->constraints.first; con; con= con->next) {
291                         if(con->type==CONSTRAINT_TYPE_KINEMATIC) {
292                                 bKinematicConstraint *data = (bKinematicConstraint*)con->data;
293                                 
294                                 pchan->constflag |= PCHAN_HAS_IK;
295                                 
296                                 if(data->tar==NULL || (data->tar->type==OB_ARMATURE && data->subtarget[0]==0))
297                                         pchan->constflag |= PCHAN_HAS_TARGET;
298                                 
299                                 /* negative rootbone = recalc rootbone index. used in do_versions */
300                                 if(data->rootbone<0) {
301                                         data->rootbone= 0;
302                                         
303                                         if(data->flag & CONSTRAINT_IK_TIP) parchan= pchan;
304                                         else parchan= pchan->parent;
305                                         
306                                         while(parchan) {
307                                                 data->rootbone++;
308                                                 if((parchan->bone->flag & BONE_CONNECTED)==0)
309                                                         break;
310                                                 parchan= parchan->parent;
311                                         }
312                                 }
313                         }
314                         else pchan->constflag |= PCHAN_HAS_CONST;
315                 }
316         }
317 }
318
319
320 /* ************************ END Pose channels *************** */
321
322 /* ************************ Action channels *************** */
323
324
325 bActionChannel *get_action_channel(bAction *act, const char *name)
326 {
327         bActionChannel *chan;
328         
329         if (!act)
330                 return NULL;
331         
332         for (chan = act->chanbase.first; chan; chan=chan->next){
333                 if (!strcmp (chan->name, name))
334                         return chan;
335         }
336         
337         return NULL;
338 }
339
340 /* returns existing channel, or adds new one. In latter case it doesnt activate it, context is required for that*/
341 bActionChannel *verify_action_channel(bAction *act, const char *name)
342 {
343         bActionChannel *chan;
344         
345         chan= get_action_channel(act, name);
346         if(chan==NULL) {
347                 if (!chan) {
348                         chan = MEM_callocN (sizeof(bActionChannel), "actionChannel");
349                         strncpy (chan->name, name, 31);
350                         BLI_addtail (&act->chanbase, chan);
351                 }
352         }
353         return chan;
354 }
355
356 /* ************************ Blending with NLA *************** */
357
358 static void blend_pose_strides(bPose *dst, bPose *src, float srcweight, short mode)
359 {
360         float dstweight;
361         
362         switch (mode){
363                 case POSE_BLEND:
364                         dstweight = 1.0F - srcweight;
365                         break;
366                 case POSE_ADD:
367                         dstweight = 1.0F;
368                         break;
369                 default :
370                         dstweight = 1.0F;
371         }
372         
373         VecLerpf(dst->stride_offset, dst->stride_offset, src->stride_offset, srcweight);
374 }
375
376 /* Only allowed for Poses with identical channels */
377 void blend_poses(bPose *dst, bPose *src, float srcweight, short mode)
378 {
379         bPoseChannel *dchan;
380         const bPoseChannel *schan;
381         bConstraint *dcon, *scon;
382         float   dquat[4], squat[4];
383         float dstweight;
384         int i;
385         
386         switch (mode){
387         case POSE_BLEND:
388                 dstweight = 1.0F - srcweight;
389                 break;
390         case POSE_ADD:
391                 dstweight = 1.0F;
392                 break;
393         default :
394                 dstweight = 1.0F;
395         }
396         
397         schan= src->chanbase.first;
398         for (dchan = dst->chanbase.first; dchan; dchan=dchan->next, schan= schan->next){
399                 if (schan->flag & (POSE_ROT|POSE_LOC|POSE_SIZE)) {
400                         /* replaced quat->matrix->quat conversion with decent quaternion interpol (ton) */
401                         
402                         /* Do the transformation blend */
403                         if (schan->flag & POSE_ROT) {
404                                 QUATCOPY(dquat, dchan->quat);
405                                 QUATCOPY(squat, schan->quat);
406                                 if(mode==POSE_BLEND)
407                                         QuatInterpol(dchan->quat, dquat, squat, srcweight);
408                                 else
409                                         QuatAdd(dchan->quat, dquat, squat, srcweight);
410                                 
411                                 NormalQuat (dchan->quat);
412                         }
413
414                         for (i=0; i<3; i++){
415                                 if (schan->flag & POSE_LOC)
416                                         dchan->loc[i] = (dchan->loc[i]*dstweight) + (schan->loc[i]*srcweight);
417                                 if (schan->flag & POSE_SIZE)
418                                         dchan->size[i] = 1.0f + ((dchan->size[i]-1.0f)*dstweight) + ((schan->size[i]-1.0f)*srcweight);
419                         }
420                         dchan->flag |= schan->flag;
421                 }
422                 for(dcon= dchan->constraints.first, scon= schan->constraints.first; dcon && scon; dcon= dcon->next, scon= scon->next) {
423                         /* no 'add' option for constraint blending */
424                         dcon->enforce= dcon->enforce*(1.0f-srcweight) + scon->enforce*srcweight;
425                 }
426         }
427 }
428
429
430 void calc_action_range(const bAction *act, float *start, float *end)
431 {
432         const bActionChannel *chan;
433         const bConstraintChannel *conchan;
434         const IpoCurve  *icu;
435         float min=999999999.0f, max=-999999999.0;
436         int     foundvert=0;
437
438         if(act) {
439                 for (chan=act->chanbase.first; chan; chan=chan->next) {
440                         if(chan->ipo) {
441                                 for (icu=chan->ipo->curve.first; icu; icu=icu->next) {
442                                         if(icu->totvert) {
443                                                 min= MIN2 (min, icu->bezt[0].vec[1][0]);
444                                                 max= MAX2 (max, icu->bezt[icu->totvert-1].vec[1][0]);
445                                                 foundvert=1;
446                                         }
447                                 }
448                         }
449                         for (conchan=chan->constraintChannels.first; conchan; conchan=conchan->next) {
450                                 if(conchan->ipo) {
451                                         for (icu=conchan->ipo->curve.first; icu; icu=icu->next) {
452                                                 if(icu->totvert) {
453                                                         min= MIN2 (min, icu->bezt[0].vec[1][0]);
454                                                         max= MAX2 (max, icu->bezt[icu->totvert-1].vec[1][0]);
455                                                         foundvert=1;
456                                                 }
457                                         }
458                                 }
459                         }
460                 }
461         }       
462         if (foundvert) {
463                 if(min==max) max+= 1.0f;
464                 *start= min;
465                 *end= max;
466         }
467         else {
468                 *start= 0.0f;
469                 *end= 1.0f;
470         }
471 }
472
473 /* Copy the data from the action-pose (src) into the pose */
474 /* both args are assumed to be valid */
475 /* exported to game engine */
476 void extract_pose_from_pose(bPose *pose, const bPose *src)
477 {
478         const bPoseChannel *schan;
479         bPoseChannel *pchan= pose->chanbase.first;
480
481         for (schan=src->chanbase.first; schan; schan=schan->next, pchan= pchan->next) {
482                 copy_pose_channel_data(pchan, schan);
483         }
484 }
485
486 /* Pose should exist, can have any number of channels too (used for constraint) */
487 void extract_pose_from_action(bPose *pose, bAction *act, float ctime) 
488 {
489         bActionChannel *achan;
490         bPoseChannel    *pchan;
491         Ipo                             *ipo;
492
493         if (!act)
494                 return;
495         if (!pose)
496                 return;
497         
498         /* Copy the data from the action into the pose */
499         for (pchan= pose->chanbase.first; pchan; pchan=pchan->next) {
500                 achan= get_action_channel(act, pchan->name);
501                 pchan->flag &= ~(POSE_LOC|POSE_ROT|POSE_SIZE);
502                 if(achan) {
503                         ipo = achan->ipo;
504                         if (ipo) {
505                                 /* Evaluates and sets the internal ipo value */
506                                 calc_ipo(ipo, ctime);
507                                 /* This call also sets the pchan flags */
508                                 execute_action_ipo(achan, pchan);
509                         }
510                         do_constraint_channels(&pchan->constraints, &achan->constraintChannels, ctime);
511                 }
512         }
513 }
514
515 /* for do_all_pose_actions, clears the pose */
516 static void rest_pose(bPose *pose)
517 {
518         bPoseChannel *pchan;
519         int i;
520         
521         if (!pose)
522                 return;
523         
524         pose->stride_offset[0]= 0.0f;
525         pose->stride_offset[1]= 0.0f;
526         pose->stride_offset[2]= 0.0f;
527         
528         for (pchan=pose->chanbase.first; pchan; pchan=pchan->next){
529                 for (i=0; i<3; i++){
530                         pchan->loc[i]=0.0;
531                         pchan->quat[i+1]=0.0;
532                         pchan->size[i]=1.0;
533                 }
534                 pchan->quat[0]=1.0;
535                 
536                 pchan->flag &= ~(POSE_LOC|POSE_ROT|POSE_SIZE);
537         }
538 }
539
540 /* ********** NLA with non-poses works with ipo channels ********** */
541
542 typedef struct NlaIpoChannel {
543         struct NlaIpoChannel *next, *prev;
544         float val;
545         void *poin;
546         int type;
547 } NlaIpoChannel;
548
549 static void extract_ipochannels_from_action(ListBase *lb, ID *id, bAction *act, char *name, float ctime)
550 {
551         bActionChannel *achan= get_action_channel(act, name);
552         IpoCurve *icu;
553         NlaIpoChannel *nic;
554         
555         if(achan==NULL) return;
556         
557         if(achan->ipo) {
558                 calc_ipo(achan->ipo, ctime);
559                 
560                 for(icu= achan->ipo->curve.first; icu; icu= icu->next) {
561                         
562                         nic= MEM_callocN(sizeof(NlaIpoChannel), "NlaIpoChannel");
563                         BLI_addtail(lb, nic);
564                         nic->val= icu->curval;
565                         nic->poin= get_ipo_poin(id, icu, &nic->type);
566                 }
567         }
568         
569         /* constraint channels only for objects */
570         if(GS(id->name)==ID_OB) {
571                 Object *ob= (Object *)id;
572                 bConstraint *con;
573                 bConstraintChannel *conchan;
574                 
575                 for (con=ob->constraints.first; con; con=con->next) {
576                         conchan = get_constraint_channel(&achan->constraintChannels, con->name);
577                         
578                         if(conchan && conchan->ipo) {
579                                 calc_ipo(conchan->ipo, ctime);
580
581                                 icu= conchan->ipo->curve.first; // only one ipo now
582                                 if(icu) {
583                                         nic= MEM_callocN(sizeof(NlaIpoChannel), "NlaIpoChannel constr");
584                                         BLI_addtail(lb, nic);
585                                         nic->val= icu->curval;
586                                         nic->poin= &con->enforce;
587                                         nic->type= IPO_FLOAT;
588                                 }
589                         }
590                 }
591         }
592 }
593
594 static NlaIpoChannel *find_nla_ipochannel(ListBase *lb, void *poin)
595 {
596         NlaIpoChannel *nic;
597         
598         if(poin) {
599                 for(nic= lb->first; nic; nic= nic->next) {
600                         if(nic->poin==poin)
601                                 return nic;
602                 }
603         }
604         return NULL;
605 }
606
607
608 static void blend_ipochannels(ListBase *dst, ListBase *src, float srcweight, int mode)
609 {
610         NlaIpoChannel *snic, *dnic, *next;
611         float dstweight;
612         
613         switch (mode){
614                 case POSE_BLEND:
615                         dstweight = 1.0F - srcweight;
616                         break;
617                 case POSE_ADD:
618                         dstweight = 1.0F;
619                         break;
620                 default :
621                         dstweight = 1.0F;
622         }
623         
624         for(snic= src->first; snic; snic= next) {
625                 next= snic->next;
626                 
627                 dnic= find_nla_ipochannel(dst, snic->poin);
628                 if(dnic==NULL) {
629                         /* remove from src list, and insert in dest */
630                         BLI_remlink(src, snic);
631                         BLI_addtail(dst, snic);
632                 }
633                 else {
634                         /* we do the blend */
635                         dnic->val= dstweight*dnic->val + srcweight*snic->val;
636                 }
637         }
638 }
639
640 static void execute_ipochannels(ListBase *lb)
641 {
642         NlaIpoChannel *nic;
643         
644         for(nic= lb->first; nic; nic= nic->next) {
645                 if(nic->poin) {
646                         write_ipo_poin(nic->poin, nic->type, nic->val);
647                 }
648         }
649 }
650
651
652 /* ************** time ****************** */
653
654 static bActionStrip *get_active_strip(Object *ob)
655 {
656         bActionStrip *strip;
657         
658         if(ob->action==NULL)
659                 return NULL;
660         
661         for (strip=ob->nlastrips.first; strip; strip=strip->next)
662                 if(strip->flag & ACTSTRIP_ACTIVE)
663                         break;
664         
665         if(strip && strip->act==ob->action)
666                 return strip;
667         return NULL;
668 }
669
670 /* non clipped mapping of strip */
671 static float get_actionstrip_frame(bActionStrip *strip, float cframe, int invert)
672 {
673         float length, actlength, repeat;
674         
675         if (strip->flag & ACTSTRIP_USESTRIDE)
676                 repeat= 1.0f;
677         else
678                 repeat= strip->repeat;
679         
680         length = strip->end-strip->start;
681         if(length==0.0f)
682                 length= 1.0f;
683         actlength = strip->actend-strip->actstart;
684
685         
686         
687         if(invert)
688                 return length*(cframe - strip->actstart)/(repeat*actlength) + strip->start;
689         else
690                 return repeat*actlength*(cframe - strip->start)/length + strip->actstart;
691 }
692
693 /* if the conditions match, it converts current time to strip time */
694 float get_action_frame(Object *ob, float cframe)
695 {
696         bActionStrip *strip= get_active_strip(ob);
697         
698         if(strip)
699                 return get_actionstrip_frame(strip, cframe, 0);
700         return cframe;
701 }
702
703 /* inverted, strip time to current time */
704 float get_action_frame_inv(Object *ob, float cframe)
705 {
706         bActionStrip *strip= get_active_strip(ob);
707         
708         if(strip)
709                 return get_actionstrip_frame(strip, cframe, 1);
710         return cframe;
711 }
712
713
714
715 /* this now only used for repeating cycles, to enable fields and blur. */
716 /* the whole time control in blender needs serious thinking... */
717 static float nla_time(float cfra, float unit)
718 {
719         extern float bluroffs;  // bad construct, borrowed from object.c for now
720         
721         /* 2nd field */
722         if(R.flag & R_SEC_FIELD) {
723                 if(R.r.mode & R_FIELDSTILL); else cfra+= 0.5f*unit;
724         }
725         
726         /* motion blur */
727         cfra+= unit*bluroffs;
728                 
729         /* global time */
730         cfra*= G.scene->r.framelen;     
731
732
733         /* decide later... */
734 //      if(no_speed_curve==0) if(ob && ob->ipo) cfra= calc_ipo_time(ob->ipo, cfra);
735
736         return cfra;
737 }
738
739 static float stridechannel_frame(Object *ob, bActionStrip *strip, Path *path, float pathdist, float *stride_offset)
740 {
741         bAction *act= strip->act;
742         char *name= strip->stridechannel;
743         bActionChannel *achan= get_action_channel(act, name);
744         int stride_axis= strip->stride_axis;
745
746         if(achan && achan->ipo) {
747                 IpoCurve *icu= NULL;
748                 float minx=0.0f, maxx=0.0f, miny=0.0f, maxy=0.0f;
749                 int foundvert= 0;
750                 
751                 if(stride_axis==0) stride_axis= AC_LOC_X;
752                 else if(stride_axis==1) stride_axis= AC_LOC_Y;
753                 else stride_axis= AC_LOC_Z;
754                 
755                 /* calculate the min/max */
756                 for (icu=achan->ipo->curve.first; icu; icu=icu->next) {
757                         if(icu->adrcode==stride_axis) {
758                                 if(icu->totvert>1) {
759                                         foundvert= 1;
760                                         minx= icu->bezt[0].vec[1][0];
761                                         maxx= icu->bezt[icu->totvert-1].vec[1][0];
762                                         
763                                         miny= icu->bezt[0].vec[1][1];
764                                         maxy= icu->bezt[icu->totvert-1].vec[1][1];
765                                 }
766                                 break;
767                         }
768                 }
769                 
770                 if(foundvert && miny!=maxy) {
771                         float stridelen= fabs(maxy-miny), striptime;
772                         float actiondist, pdist, pdistNewNormalized;
773                         float vec1[4], vec2[4], dir[3];
774                         
775                         /* amount path moves object */
776                         pdist = (float)fmod (pathdist, stridelen);
777                         striptime= pdist/stridelen;
778                         
779                         /* amount stride bone moves */
780                         actiondist= eval_icu(icu, minx + striptime*(maxx-minx)) - miny;
781                         
782                         pdist = fabs(actiondist) - pdist;
783                         pdistNewNormalized = (pathdist+pdist)/path->totdist;
784                         
785                         /* now we need to go pdist further (or less) on cu path */
786                         where_on_path(ob, (pathdist)/path->totdist, vec1, dir); /* vec needs size 4 */
787                         if (pdistNewNormalized <= 1) {
788                                 // search for correction in positive path-direction
789                                 where_on_path(ob, pdistNewNormalized, vec2, dir);       /* vec needs size 4 */
790                                 VecSubf(stride_offset, vec2, vec1);
791                         }
792                         else {
793                                 // we reached the end of the path, search backwards instead
794                                 where_on_path(ob, (pathdist-pdist)/path->totdist, vec2, dir);   /* vec needs size 4 */
795                                 VecSubf(stride_offset, vec1, vec2);
796                         }
797                         Mat4Mul3Vecfl(ob->obmat, stride_offset);
798                         return striptime;
799                 }
800         }
801         return 0.0f;
802 }
803
804 /* ************** do the action ************ */
805
806 static void do_nla(Object *ob, int blocktype)
807 {
808         bPose *tpose= NULL;
809         Key *key= NULL;
810         ListBase tchanbase={NULL, NULL}, chanbase={NULL, NULL};
811         bActionStrip *strip;
812         float striptime, frametime, length, actlength;
813         float blendfac, stripframe;
814         int     doit, dostride;
815         
816         if(blocktype==ID_AR) {
817                 copy_pose(&tpose, ob->pose, 1);
818                 rest_pose(ob->pose);            // potentially destroying current not-keyed pose
819         }
820         else {
821                 key= ob_get_key(ob);
822         }
823         
824         for (strip=ob->nlastrips.first; strip; strip=strip->next){
825                 doit=dostride= 0;
826                 
827                 if (strip->act){        /* so theres an action */
828                         
829                         /* Determine if the current frame is within the strip's range */
830                         length = strip->end-strip->start;
831                         actlength = strip->actend-strip->actstart;
832                         striptime = (G.scene->r.cfra-(strip->start)) / length;
833                         stripframe = (G.scene->r.cfra-(strip->start)) ;
834                         
835                         if (striptime>=0.0){
836                                 
837                                 if(blocktype==ID_AR) 
838                                         rest_pose(tpose);
839                                 
840                                 /* Handle path */
841                                 if ((strip->flag & ACTSTRIP_USESTRIDE) && (blocktype==ID_AR) && (ob->ipoflag & OB_DISABLE_PATH)==0){
842                                         if (ob->parent && ob->parent->type==OB_CURVE){
843                                                 Curve *cu = ob->parent->data;
844                                                 float ctime, pdist;
845                                                 
846                                                 if (cu->flag & CU_PATH){
847                                                         /* Ensure we have a valid path */
848                                                         if(cu->path==NULL || cu->path->data==NULL) makeDispListCurveTypes(ob->parent, 0);
849                                                         if(cu->path) {
850                                                                 
851                                                                 /* Find the position on the path */
852                                                                 ctime= bsystem_time(ob, ob->parent, (float)G.scene->r.cfra, 0.0);
853                                                                 
854                                                                 if(calc_ipo_spec(cu->ipo, CU_SPEED, &ctime)==0) {
855                                                                         ctime /= cu->pathlen;
856                                                                         CLAMP(ctime, 0.0, 1.0);
857                                                                 }
858                                                                 pdist = ctime*cu->path->totdist;
859                                                                 
860                                                                 if(tpose && strip->stridechannel[0]) {
861                                                                         striptime= stridechannel_frame(ob->parent, strip, cu->path, pdist, tpose->stride_offset);
862                                                                 }                                                                       
863                                                                 else {
864                                                                         if (strip->stridelen) {
865                                                                                 striptime = pdist / strip->stridelen;
866                                                                                 striptime = (float)fmod (striptime, 1.0);
867                                                                         }
868                                                                         else
869                                                                                 striptime = 0;
870                                                                 }
871                                                                 
872                                                                 frametime = (striptime * actlength) + strip->actstart;
873                                                                 frametime= bsystem_time(ob, 0, frametime, 0.0);
874                                                                 
875                                                                 if(blocktype==ID_AR) {
876                                                                         extract_pose_from_action (tpose, strip->act, frametime);
877                                                                 }
878                                                                 else if(blocktype==ID_OB) {
879                                                                         extract_ipochannels_from_action(&tchanbase, &ob->id, strip->act, "Object", frametime);
880                                                                         if(key)
881                                                                                 extract_ipochannels_from_action(&tchanbase, &key->id, strip->act, "Shape", frametime);
882                                                                 }
883                                                                 doit=dostride= 1;
884                                                         }
885                                                 }
886                                         }
887                                 }
888                                 /* Handle repeat, we add 0.1 frame extra to make sure the last frame is included */
889                                 else if (striptime < 1.0f + 0.1f/length) {
890                                         
891                                         /* Mod to repeat */
892                                         if(strip->repeat!=1.0f) {
893                                                 striptime*= strip->repeat;
894                                                 striptime = (float)fmod (striptime, 1.0f + 0.1f/length);
895                                         }
896
897                                         frametime = (striptime * actlength) + strip->actstart;
898                                         frametime= nla_time(frametime, (float)strip->repeat);
899                                                 
900                                         if(blocktype==ID_AR)
901                                                 extract_pose_from_action (tpose, strip->act, frametime);
902                                         else if(blocktype==ID_OB) {
903                                                 extract_ipochannels_from_action(&tchanbase, &ob->id, strip->act, "Object", frametime);
904                                                 if(key)
905                                                         extract_ipochannels_from_action(&tchanbase, &key->id, strip->act, "Shape", frametime);
906                                         }                                               
907                                         doit=1;
908                                 }
909                                 /* Handle extend */
910                                 else{
911                                         if (strip->flag & ACTSTRIP_HOLDLASTFRAME){
912                                                 /* we want the strip to hold on the exact fraction of the repeat value */
913                                                 
914                                                 frametime = actlength * (strip->repeat-(int)strip->repeat);
915                                                 if(frametime<=0.000001f) frametime= actlength;  /* rounding errors... */
916                                                 frametime= bsystem_time(ob, 0, frametime+strip->actstart, 0.0);
917                                                 
918                                                 if(blocktype==ID_AR)
919                                                         extract_pose_from_action (tpose, strip->act, frametime);
920                                                 else if(blocktype==ID_OB) {
921                                                         extract_ipochannels_from_action(&tchanbase, &ob->id, strip->act, "Object", frametime);
922                                                         if(key)
923                                                                 extract_ipochannels_from_action(&tchanbase, &key->id, strip->act, "Shape", frametime);
924                                                 }
925                                                 doit=1;
926                                         }
927                                 }
928                                 
929                                 /* Handle blendin & blendout */
930                                 if (doit){
931                                         /* Handle blendin */
932                                         
933                                         if (strip->blendin>0.0 && stripframe<=strip->blendin && G.scene->r.cfra>=strip->start){
934                                                 blendfac = stripframe/strip->blendin;
935                                         }
936                                         else if (strip->blendout>0.0 && stripframe>=(length-strip->blendout) && G.scene->r.cfra<=strip->end){
937                                                 blendfac = (length-stripframe)/(strip->blendout);
938                                         }
939                                         else
940                                                 blendfac = 1;
941                                         
942                                         if(blocktype==ID_AR) {/* Blend this pose with the accumulated pose */
943                                                 blend_poses (ob->pose, tpose, blendfac, strip->mode);
944                                                 if(dostride)
945                                                         blend_pose_strides (ob->pose, tpose, blendfac, strip->mode);
946                                         }
947                                         else {
948                                                 blend_ipochannels(&chanbase, &tchanbase, blendfac, strip->mode);
949                                                 BLI_freelistN(&tchanbase);
950                                         }
951                                 }
952                         }                                       
953                 }
954         }
955         
956         if(blocktype==ID_OB) {
957                 execute_ipochannels(&chanbase);
958         }
959         else if(blocktype==ID_AR) {
960                 /* apply stride offset to object */
961                 VecAddf(ob->obmat[3], ob->obmat[3], ob->pose->stride_offset);
962         }
963         
964         /* free */
965         if (tpose){
966                 free_pose_channels(tpose);
967                 MEM_freeN(tpose);
968         }
969         if(chanbase.first)
970                 BLI_freelistN(&chanbase);
971         
972 }
973
974 void do_all_pose_actions(Object *ob)
975 {
976
977         // only to have safe calls from editor
978         if(ob==NULL) return;
979         if(ob->type!=OB_ARMATURE || ob->pose==NULL) return;
980
981         if(ob->pose->flag & POSE_LOCKED) {  // no actions to execute while transform
982                 if(ob->pose->flag & POSE_DO_UNLOCK)
983                         ob->pose->flag &= ~(POSE_LOCKED|POSE_DO_UNLOCK);
984         }
985         else if(ob->action && ((ob->nlaflag & OB_NLA_OVERRIDE)==0 || ob->nlastrips.first==NULL) ) {
986                 float cframe= (float) G.scene->r.cfra;
987                 
988                 cframe= get_action_frame(ob, cframe);
989                 
990                 extract_pose_from_action (ob->pose, ob->action, bsystem_time(ob, 0, cframe, 0.0));
991         }
992         else if(ob->nlastrips.first) {
993                 do_nla(ob, ID_AR);
994         }
995 }
996
997 /* called from where_is_object */
998 void do_all_object_actions(Object *ob)
999 {
1000         if(ob==NULL) return;
1001         if(ob->dup_group) return;       /* prevent conflicts, might add smarter check later */
1002         
1003         /* Do local action */
1004         if(ob->action && ((ob->nlaflag & OB_NLA_OVERRIDE)==0 || ob->nlastrips.first==NULL) ) {
1005                 ListBase tchanbase= {NULL, NULL};
1006                 Key *key= ob_get_key(ob);
1007                 float cframe= (float) G.scene->r.cfra;
1008                 
1009                 cframe= get_action_frame(ob, cframe);
1010                 
1011                 extract_ipochannels_from_action(&tchanbase, &ob->id, ob->action, "Object", bsystem_time(ob, 0, cframe, 0.0));
1012                 if(key)
1013                         extract_ipochannels_from_action(&tchanbase, &key->id, ob->action, "Shape", bsystem_time(ob, 0, cframe, 0.0));
1014                 
1015                 if(tchanbase.first) {
1016                         execute_ipochannels(&tchanbase);
1017                         BLI_freelistN(&tchanbase);
1018                 }
1019         }
1020         else if(ob->nlastrips.first) {
1021                 do_nla(ob, ID_OB);
1022         }
1023 }
1024