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