Orange's buglist!
[blender.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         strcpy (chan->name, name);
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                                 /* negative rootbone = recalc rootbone index. used in do_versions */
296                                 if(data->rootbone<0) {
297                                         data->rootbone= 0;
298                                         
299                                         if(data->flag & CONSTRAINT_IK_TIP) parchan= pchan;
300                                         else parchan= pchan->parent;
301                                         
302                                         while(parchan) {
303                                                 data->rootbone++;
304                                                 if((parchan->bone->flag & BONE_CONNECTED)==0)
305                                                         break;
306                                                 parchan= parchan->parent;
307                                         }
308                                 }
309                         }
310                         else pchan->constflag |= PCHAN_HAS_CONST;
311                 }
312         }
313 }
314
315
316 /* ************************ END Pose channels *************** */
317
318 /* ************************ Action channels *************** */
319
320
321 bActionChannel *get_action_channel(bAction *act, const char *name)
322 {
323         bActionChannel *chan;
324         
325         if (!act)
326                 return NULL;
327         
328         for (chan = act->chanbase.first; chan; chan=chan->next){
329                 if (!strcmp (chan->name, name))
330                         return chan;
331         }
332         
333         return NULL;
334 }
335
336 /* returns existing channel, or adds new one. In latter case it doesnt activate it, context is required for that*/
337 bActionChannel *verify_action_channel(bAction *act, const char *name)
338 {
339         bActionChannel *chan;
340         
341         chan= get_action_channel(act, name);
342         if(chan==NULL) {
343                 if (!chan) {
344                         chan = MEM_callocN (sizeof(bActionChannel), "actionChannel");
345                         strcpy (chan->name, name);
346                         BLI_addtail (&act->chanbase, chan);
347                 }
348         }
349         return chan;
350 }
351
352 /* ************************ Blending with NLA *************** */
353
354
355 /* Only allowed for Poses with identical channels */
356 void blend_poses(bPose *dst, const bPose *src, float srcweight, short mode)
357 {
358         bPoseChannel *dchan;
359         const bPoseChannel *schan;
360         bConstraint *dcon, *scon;
361         float   dquat[4], squat[4];
362         float dstweight;
363         int i;
364         
365         switch (mode){
366         case POSE_BLEND:
367                 dstweight = 1.0F - srcweight;
368                 break;
369         case POSE_ADD:
370                 dstweight = 1.0F;
371                 break;
372         default :
373                 dstweight = 1.0F;
374         }
375         
376         schan= src->chanbase.first;
377         for (dchan = dst->chanbase.first; dchan; dchan=dchan->next, schan= schan->next){
378                 if (schan->flag & (POSE_ROT|POSE_LOC|POSE_SIZE)) {
379                         /* replaced quat->matrix->quat conversion with decent quaternion interpol (ton) */
380                         
381                         /* Do the transformation blend */
382                         if (schan->flag & POSE_ROT) {
383                                 QUATCOPY(dquat, dchan->quat);
384                                 QUATCOPY(squat, schan->quat);
385                                 if(mode==POSE_BLEND)
386                                         QuatInterpol(dchan->quat, dquat, squat, srcweight);
387                                 else
388                                         QuatAdd(dchan->quat, dquat, squat, srcweight);
389                                 
390                                 NormalQuat (dchan->quat);
391                         }
392
393                         for (i=0; i<3; i++){
394                                 if (schan->flag & POSE_LOC)
395                                         dchan->loc[i] = (dchan->loc[i]*dstweight) + (schan->loc[i]*srcweight);
396                                 if (schan->flag & POSE_SIZE)
397                                         dchan->size[i] = 1.0f + ((dchan->size[i]-1.0f)*dstweight) + ((schan->size[i]-1.0f)*srcweight);
398                         }
399                         dchan->flag |= schan->flag;
400                 }
401                 for(dcon= dchan->constraints.first, scon= schan->constraints.first; dcon && scon; dcon= dcon->next, scon= scon->next) {
402                         /* no 'add' option for constraint blending */
403                         dcon->enforce= dcon->enforce*(1.0f-srcweight) + scon->enforce*srcweight;
404                 }
405         }
406 }
407
408
409 void calc_action_range(const bAction *act, float *start, float *end)
410 {
411         const bActionChannel *chan;
412         const bConstraintChannel *conchan;
413         const IpoCurve  *icu;
414         float min=999999999.0f, max=-999999999.0;
415         int     i;
416         int     foundvert=0;
417
418         if(act) {
419                 for (chan=act->chanbase.first; chan; chan=chan->next) {
420                         if(chan->ipo) {
421                                 for (icu=chan->ipo->curve.first; icu; icu=icu->next) {
422                                         for (i=0; i<icu->totvert; i++) {
423                                                 min = MIN2 (min, icu->bezt[i].vec[1][0]);
424                                                 max = MAX2 (max, icu->bezt[i].vec[1][0]);
425                                                 foundvert=1;
426                                         }
427                                 }
428                         }
429                         for (conchan=chan->constraintChannels.first; conchan; conchan=conchan->next) {
430                                 if(conchan->ipo) {
431                                         for (icu=conchan->ipo->curve.first; icu; icu=icu->next) {
432                                                 for (i=0; i<icu->totvert; i++){
433                                                         min = MIN2 (min, icu->bezt[i].vec[1][0]);
434                                                         max = MAX2 (max, icu->bezt[i].vec[1][0]);
435                                                         foundvert=1;
436                                                 }
437                                         }
438                                 }
439                         }
440                 }
441         }       
442         if (foundvert) {
443                 if(min==max) max+= 1.0f;
444                 *start= min;
445                 *end= max;
446         }
447         else {
448                 *start= 0.0f;
449                 *end= 1.0f;
450         }
451 }
452
453 /* Copy the data from the action-pose (src) into the pose */
454 /* both args are assumed to be valid */
455 /* exported to game engine */
456 void extract_pose_from_pose(bPose *pose, const bPose *src)
457 {
458         const bPoseChannel *schan;
459         bPoseChannel *pchan= pose->chanbase.first;
460
461         for (schan=src->chanbase.first; schan; schan=schan->next, pchan= pchan->next) {
462                 copy_pose_channel_data(pchan, schan);
463         }
464 }
465
466 /* Pose should exist, can have any number of channels too (used for constraint) */
467 void extract_pose_from_action(bPose *pose, bAction *act, float ctime) 
468 {
469         bActionChannel *achan;
470         bPoseChannel    *pchan;
471         Ipo                             *ipo;
472
473         if (!act)
474                 return;
475         if (!pose)
476                 return;
477         
478         /* Copy the data from the action into the pose */
479         for (pchan= pose->chanbase.first; pchan; pchan=pchan->next) {
480                 achan= get_action_channel(act, pchan->name);
481                 pchan->flag= 0;
482                 if(achan) {
483                         ipo = achan->ipo;
484                         if (ipo) {
485                                 /* Evaluates and sets the internal ipo value */
486                                 calc_ipo(ipo, ctime);
487                                 /* This call also sets the pchan flags */
488                                 execute_action_ipo(achan, pchan);
489                         }
490                         do_constraint_channels(&pchan->constraints, &achan->constraintChannels, ctime);
491                 }
492         }
493 }
494
495 /* for do_all_pose_actions, clears the pose */
496 static void rest_pose(bPose *pose)
497 {
498         bPoseChannel *chan;
499         int i;
500         
501         if (!pose)
502                 return;
503         
504         for (chan=pose->chanbase.first; chan; chan=chan->next){
505                 for (i=0; i<3; i++){
506                         chan->loc[i]=0.0;
507                         chan->quat[i+1]=0.0;
508                         chan->size[i]=1.0;
509                 }
510                 chan->quat[0]=1.0;
511                 
512                 chan->flag =0;
513         }
514 }
515
516 /* ********** NLA with non-poses works with ipo channels ********** */
517
518 typedef struct NlaIpoChannel {
519         struct NlaIpoChannel *next, *prev;
520         float val;
521         void *poin;
522         int type;
523 } NlaIpoChannel;
524
525 static void extract_ipochannels_from_action(ListBase *lb, ID *id, bAction *act, char *name, float ctime)
526 {
527         bActionChannel *achan= get_action_channel(act, name);
528         IpoCurve *icu;
529         NlaIpoChannel *nic;
530         
531         if(achan==NULL) return;
532         
533         if(achan->ipo) {
534                 calc_ipo(achan->ipo, ctime);
535                 
536                 for(icu= achan->ipo->curve.first; icu; icu= icu->next) {
537                         
538                         nic= MEM_callocN(sizeof(NlaIpoChannel), "NlaIpoChannel");
539                         BLI_addtail(lb, nic);
540                         nic->val= icu->curval;
541                         nic->poin= get_ipo_poin(id, icu, &nic->type);
542                 }
543         }
544         
545         /* constraint channels only for objects */
546         if(GS(id->name)==ID_OB) {
547                 Object *ob= (Object *)id;
548                 bConstraint *con;
549                 bConstraintChannel *conchan;
550                 
551                 for (con=ob->constraints.first; con; con=con->next) {
552                         conchan = get_constraint_channel(&achan->constraintChannels, con->name);
553                         
554                         if(conchan && conchan->ipo) {
555                                 calc_ipo(conchan->ipo, ctime);
556
557                                 icu= conchan->ipo->curve.first; // only one ipo now
558                                 if(icu) {
559                                         nic= MEM_callocN(sizeof(NlaIpoChannel), "NlaIpoChannel constr");
560                                         BLI_addtail(lb, nic);
561                                         nic->val= icu->curval;
562                                         nic->poin= &con->enforce;
563                                         nic->type= IPO_FLOAT;
564                                 }
565                         }
566                 }
567         }
568 }
569
570 static NlaIpoChannel *find_nla_ipochannel(ListBase *lb, void *poin)
571 {
572         NlaIpoChannel *nic;
573         
574         if(poin) {
575                 for(nic= lb->first; nic; nic= nic->next) {
576                         if(nic->poin==poin)
577                                 return nic;
578                 }
579         }
580         return NULL;
581 }
582
583
584 static void blend_ipochannels(ListBase *dst, ListBase *src, float srcweight, int mode)
585 {
586         NlaIpoChannel *snic, *dnic, *next;
587         float dstweight;
588         
589         switch (mode){
590                 case POSE_BLEND:
591                         dstweight = 1.0F - srcweight;
592                         break;
593                 case POSE_ADD:
594                         dstweight = 1.0F;
595                         break;
596                 default :
597                         dstweight = 1.0F;
598         }
599         
600         for(snic= src->first; snic; snic= next) {
601                 next= snic->next;
602                 
603                 dnic= find_nla_ipochannel(dst, snic->poin);
604                 if(dnic==NULL) {
605                         /* remove from src list, and insert in dest */
606                         BLI_remlink(src, snic);
607                         BLI_addtail(dst, snic);
608                 }
609                 else {
610                         /* we do the blend */
611                         dnic->val= dstweight*dnic->val + srcweight*snic->val;
612                 }
613         }
614 }
615
616 static void execute_ipochannels(ListBase *lb)
617 {
618         NlaIpoChannel *nic;
619         
620         for(nic= lb->first; nic; nic= nic->next) {
621                 if(nic->poin) {
622                         write_ipo_poin(nic->poin, nic->type, nic->val);
623                 }
624         }
625 }
626
627
628 /* ************** time ****************** */
629
630 static bActionStrip *get_active_strip(Object *ob)
631 {
632         bActionStrip *strip;
633         
634         if(ob->action==NULL)
635                 return NULL;
636         
637         for (strip=ob->nlastrips.first; strip; strip=strip->next)
638                 if(strip->flag & ACTSTRIP_ACTIVE)
639                         break;
640         
641         if(strip && strip->act==ob->action)
642                 return strip;
643         return NULL;
644 }
645
646 /* non clipped mapping of strip */
647 static float get_actionstrip_frame(bActionStrip *strip, float cframe)
648 {
649         float length, actlength, repeat;
650         
651         if (strip->flag & ACTSTRIP_USESTRIDE)
652                 repeat= 1.0f;
653         else
654                 repeat= strip->repeat;
655         
656         length = strip->end-strip->start;
657         if(length==0.0f)
658                 length= 1.0f;
659         actlength = strip->actend-strip->actstart;
660         
661         return repeat*actlength*(cframe - strip->start)/length + strip->actstart;
662 }
663
664 /* if the conditions match, it converts current time to strip time */
665 float get_action_frame(Object *ob, float cframe)
666 {
667         bActionStrip *strip= get_active_strip(ob);
668         
669         if(strip)
670                 return get_actionstrip_frame(strip, cframe);
671         return cframe;
672 }
673
674 /* this now only used for repeating cycles, to enable fields and blur. */
675 /* the whole time control in blender needs serious thinking... */
676 static float nla_time(float cfra, float unit)
677 {
678         extern float bluroffs;  // bad construct, borrowed from object.c for now
679         
680         /* 2nd field */
681         if(R.flag & R_SEC_FIELD) {
682                 if(R.r.mode & R_FIELDSTILL); else cfra+= 0.5f*unit;
683         }
684         
685         /* motion blur */
686         cfra+= unit*bluroffs;
687                 
688         /* global time */
689         cfra*= G.scene->r.framelen;     
690
691
692         /* decide later... */
693 //      if(no_speed_curve==0) if(ob && ob->ipo) cfra= calc_ipo_time(ob->ipo, cfra);
694
695         return cfra;
696 }
697
698 /* ************** do the action ************ */
699
700 static void do_nla(Object *ob, int blocktype)
701 {
702         bPose *tpose= NULL;
703         Key *key= NULL;
704         ListBase tchanbase={NULL, NULL}, chanbase={NULL, NULL};
705         bActionStrip *strip;
706         float striptime, frametime, length, actlength;
707         float blendfac, stripframe;
708         int     doit;
709         
710         if(blocktype==ID_AR) {
711                 copy_pose(&tpose, ob->pose, 1);
712                 rest_pose(ob->pose);            // potentially destroying current not-keyed pose
713         }
714         else {
715                 key= ob_get_key(ob);
716         }
717         
718         for (strip=ob->nlastrips.first; strip; strip=strip->next){
719                 doit=0;
720                 
721                 if (strip->act){        /* so theres an action */
722                         
723                         /* Determine if the current frame is within the strip's range */
724                         length = strip->end-strip->start;
725                         actlength = strip->actend-strip->actstart;
726                         striptime = (G.scene->r.cfra-(strip->start)) / length;
727                         stripframe = (G.scene->r.cfra-(strip->start)) ;
728                         
729                         if (striptime>=0.0){
730                                 
731                                 if(blocktype==ID_AR) 
732                                         rest_pose(tpose);
733                                 
734                                 /* Handle path */
735                                 if (strip->flag & ACTSTRIP_USESTRIDE){
736                                         if (ob->parent && ob->parent->type==OB_CURVE){
737                                                 Curve *cu = ob->parent->data;
738                                                 float ctime, pdist;
739                                                 
740                                                 if (cu->flag & CU_PATH){
741                                                         /* Ensure we have a valid path */
742                                                         if(cu->path==NULL || cu->path->data==NULL) makeDispListCurveTypes(ob->parent, 0);
743                                                         if(cu->path) {
744                                                                 
745                                                                 /* Find the position on the path */
746                                                                 ctime= bsystem_time(ob, ob->parent, (float)G.scene->r.cfra, 0.0);
747                                                                 
748                                                                 if(calc_ipo_spec(cu->ipo, CU_SPEED, &ctime)==0) {
749                                                                         ctime /= cu->pathlen;
750                                                                         CLAMP(ctime, 0.0, 1.0);
751                                                                 }
752                                                                 pdist = ctime*cu->path->totdist;
753                                                                 
754                                                                 if (strip->stridelen)
755                                                                         striptime = pdist / strip->stridelen;
756                                                                 else
757                                                                         striptime = 0;
758                                                                 
759                                                                 striptime = (float)fmod (striptime, 1.0);
760                                                                 
761                                                                 frametime = (striptime * actlength) + strip->actstart;
762                                                                 frametime= bsystem_time(ob, 0, frametime, 0.0);
763                                                                 
764                                                                 if(blocktype==ID_AR)
765                                                                         extract_pose_from_action (tpose, strip->act, frametime);
766                                                                 else if(blocktype==ID_OB) {
767                                                                         extract_ipochannels_from_action(&tchanbase, &ob->id, strip->act, "Object", frametime);
768                                                                         if(key)
769                                                                                 extract_ipochannels_from_action(&tchanbase, &key->id, strip->act, "Shape", frametime);
770                                                                 }
771                                                                 doit=1;
772                                                         }
773                                                 }
774                                         }
775                                 }
776                                 /* Handle repeat, we add 1 frame extra to make sure the last frame is included */
777                                 else if (striptime < 1.0f + 1.0f/length) {
778                                         
779                                         /* Mod to repeat */
780                                         striptime*= strip->repeat;
781                                         striptime = (float)fmod (striptime, 1.0f + 1.0f/length);
782                                         
783                                         frametime = (striptime * actlength) + strip->actstart;
784                                         frametime= nla_time(frametime, (float)strip->repeat);
785                                                 
786                                         if(blocktype==ID_AR)
787                                                 extract_pose_from_action (tpose, strip->act, frametime);
788                                         else if(blocktype==ID_OB) {
789                                                 extract_ipochannels_from_action(&tchanbase, &ob->id, strip->act, "Object", frametime);
790                                                 if(key)
791                                                         extract_ipochannels_from_action(&tchanbase, &key->id, strip->act, "Shape", frametime);
792                                         }                                               
793                                         doit=1;
794                                 }
795                                 /* Handle extend */
796                                 else{
797                                         if (strip->flag & ACTSTRIP_HOLDLASTFRAME){
798                                                 striptime = 1.0;
799                                                 
800                                                 frametime = (striptime * actlength) + strip->actstart;
801                                                 frametime= bsystem_time(ob, 0, frametime, 0.0);
802                                                 
803                                                 if(blocktype==ID_AR)
804                                                         extract_pose_from_action (tpose, strip->act, frametime);
805                                                 else if(blocktype==ID_OB) {
806                                                         extract_ipochannels_from_action(&tchanbase, &ob->id, strip->act, "Object", frametime);
807                                                         if(key)
808                                                                 extract_ipochannels_from_action(&tchanbase, &key->id, strip->act, "Shape", frametime);
809                                                 }
810                                                 doit=1;
811                                         }
812                                 }
813                                 
814                                 /* Handle blendin & blendout */
815                                 if (doit){
816                                         /* Handle blendin */
817                                         
818                                         if (strip->blendin>0.0 && stripframe<=strip->blendin && G.scene->r.cfra>=strip->start){
819                                                 blendfac = stripframe/strip->blendin;
820                                         }
821                                         else if (strip->blendout>0.0 && stripframe>=(length-strip->blendout) && G.scene->r.cfra<=strip->end){
822                                                 blendfac = (length-stripframe)/(strip->blendout);
823                                         }
824                                         else
825                                                 blendfac = 1;
826                                         
827                                         if(blocktype==ID_AR) /* Blend this pose with the accumulated pose */
828                                                 blend_poses (ob->pose, tpose, blendfac, strip->mode);
829                                         else {
830                                                 blend_ipochannels(&chanbase, &tchanbase, blendfac, strip->mode);
831                                                 BLI_freelistN(&tchanbase);
832                                         }
833                                 }
834                         }                                       
835                 }
836         }
837         
838         if(blocktype==ID_OB) {
839                 execute_ipochannels(&chanbase);
840         }
841
842         if (tpose){
843                 free_pose_channels(tpose);
844                 MEM_freeN(tpose);
845         }
846         if(chanbase.first)
847                 BLI_freelistN(&chanbase);
848         
849         
850 }
851
852 void do_all_pose_actions(Object *ob)
853 {
854
855         // only to have safe calls from editor
856         if(ob==NULL) return;
857         if(ob->type!=OB_ARMATURE || ob->pose==NULL) return;
858
859         if(ob->pose->flag & POSE_LOCKED) {  // no actions to execute while transform
860                 if(ob->pose->flag & POSE_DO_UNLOCK)
861                         ob->pose->flag &= ~(POSE_LOCKED|POSE_DO_UNLOCK);
862         }
863         else if(ob->action && ((ob->nlaflag & OB_NLA_OVERRIDE)==0 || ob->nlastrips.first==NULL) ) {
864                 float cframe= (float) G.scene->r.cfra;
865                 
866                 cframe= get_action_frame(ob, cframe);
867                 
868                 extract_pose_from_action (ob->pose, ob->action, bsystem_time(ob, 0, cframe, 0.0));
869         }
870         else if(ob->nlastrips.first) {
871                 do_nla(ob, ID_AR);
872         }
873 }
874
875 /* called from where_is_object */
876 void do_all_object_actions(Object *ob)
877 {
878         if(ob==NULL) return;
879         
880         /* Do local action */
881         if(ob->action && ((ob->nlaflag & OB_NLA_OVERRIDE)==0 || ob->nlastrips.first==NULL) ) {
882                 ListBase tchanbase= {NULL, NULL};
883                 Key *key= ob_get_key(ob);
884                 float cframe= (float) G.scene->r.cfra;
885                 
886                 cframe= get_action_frame(ob, cframe);
887                 
888                 extract_ipochannels_from_action(&tchanbase, &ob->id, ob->action, "Object", bsystem_time(ob, 0, cframe, 0.0));
889                 if(key)
890                         extract_ipochannels_from_action(&tchanbase, &key->id, ob->action, "Shape", bsystem_time(ob, 0, cframe, 0.0));
891                 
892                 if(tchanbase.first) {
893                         execute_ipochannels(&tchanbase);
894                         BLI_freelistN(&tchanbase);
895                 }
896         }
897         else if(ob->nlastrips.first) {
898                 do_nla(ob, ID_OB);
899         }
900 }
901