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