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