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