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