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