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