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