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