svn merge -r 12294:12347 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[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                         /* 0 = do all ipos, not only drivers */
722                         do_constraint_channels(&pchan->constraints, &achan->constraintChannels, ctime, 0);
723                 }
724         }
725         
726         pose->ctime= ctime;     /* used for cyclic offset matching */
727 }
728
729 /* for do_all_pose_actions, clears the pose. Now also exported for proxy and tools */
730 void rest_pose(bPose *pose)
731 {
732         bPoseChannel *pchan;
733         int i;
734         
735         if (!pose)
736                 return;
737         
738         memset(pose->stride_offset, 0, sizeof(pose->stride_offset));
739         memset(pose->cyclic_offset, 0, sizeof(pose->cyclic_offset));
740         
741         for (pchan=pose->chanbase.first; pchan; pchan= pchan->next){
742                 for (i=0; i<3; i++) {
743                         pchan->loc[i]= 0.0f;
744                         pchan->quat[i+1]= 0.0f;
745                         pchan->size[i]= 1.0f;
746                 }
747                 pchan->quat[0]= 1.0f;
748                 
749                 pchan->flag &= ~(POSE_LOC|POSE_ROT|POSE_SIZE);
750         }
751 }
752
753 /* both poses should be in sync */
754 void copy_pose_result(bPose *to, bPose *from)
755 {
756         bPoseChannel *pchanto, *pchanfrom;
757         
758         if(to==NULL || from==NULL) {
759                 printf("pose result copy error\n"); // debug temp
760                 return;
761         }
762
763         for(pchanfrom= from->chanbase.first; pchanfrom; pchanfrom= pchanfrom->next) {
764                 pchanto= get_pose_channel(to, pchanfrom->name);
765                 if(pchanto) {
766                         Mat4CpyMat4(pchanto->pose_mat, pchanfrom->pose_mat);
767                         Mat4CpyMat4(pchanto->chan_mat, pchanfrom->chan_mat);
768                         /* used for local constraints */
769                         VECCOPY(pchanto->loc, pchanfrom->loc);
770                         QUATCOPY(pchanto->quat, pchanfrom->quat);
771                         VECCOPY(pchanto->size, pchanfrom->size);
772                         
773                         VECCOPY(pchanto->pose_head, pchanfrom->pose_head);
774                         VECCOPY(pchanto->pose_tail, pchanfrom->pose_tail);
775                         pchanto->flag= pchanfrom->flag;
776                 }
777         }
778 }
779
780 /* ********** NLA with non-poses works with ipo channels ********** */
781
782 typedef struct NlaIpoChannel {
783         struct NlaIpoChannel *next, *prev;
784         float val;
785         void *poin;
786         int type;
787 } NlaIpoChannel;
788
789 static void extract_ipochannels_from_action(ListBase *lb, ID *id, bAction *act, char *name, float ctime)
790 {
791         bActionChannel *achan= get_action_channel(act, name);
792         IpoCurve *icu;
793         NlaIpoChannel *nic;
794         
795         if(achan==NULL) return;
796         
797         if(achan->ipo) {
798                 calc_ipo(achan->ipo, ctime);
799                 
800                 for(icu= achan->ipo->curve.first; icu; icu= icu->next) {
801                         /* skip IPO_BITS, is for layers and cannot be blended */
802                         if(icu->vartype != IPO_BITS) {
803                                 nic= MEM_callocN(sizeof(NlaIpoChannel), "NlaIpoChannel");
804                                 BLI_addtail(lb, nic);
805                                 nic->val= icu->curval;
806                                 nic->poin= get_ipo_poin(id, icu, &nic->type);
807                         }
808                 }
809         }
810         
811         /* constraint channels only for objects */
812         if(GS(id->name)==ID_OB) {
813                 Object *ob= (Object *)id;
814                 bConstraint *con;
815                 bConstraintChannel *conchan;
816                 
817                 for (con=ob->constraints.first; con; con=con->next) {
818                         conchan = get_constraint_channel(&achan->constraintChannels, con->name);
819                         
820                         if(conchan && conchan->ipo) {
821                                 calc_ipo(conchan->ipo, ctime);
822                                 
823                                 icu= conchan->ipo->curve.first; // only one ipo now
824                                 if(icu) {
825                                         nic= MEM_callocN(sizeof(NlaIpoChannel), "NlaIpoChannel constr");
826                                         BLI_addtail(lb, nic);
827                                         nic->val= icu->curval;
828                                         nic->poin= &con->enforce;
829                                         nic->type= IPO_FLOAT;
830                                 }
831                         }
832                 }
833         }
834 }
835
836 static NlaIpoChannel *find_nla_ipochannel(ListBase *lb, void *poin)
837 {
838         NlaIpoChannel *nic;
839         
840         if(poin) {
841                 for(nic= lb->first; nic; nic= nic->next) {
842                         if(nic->poin==poin)
843                                 return nic;
844                 }
845         }
846         return NULL;
847 }
848
849
850 static void blend_ipochannels(ListBase *dst, ListBase *src, float srcweight, int mode)
851 {
852         NlaIpoChannel *snic, *dnic, *next;
853         float dstweight;
854         
855         switch (mode){
856                 case ACTSTRIPMODE_BLEND:
857                         dstweight = 1.0F - srcweight;
858                         break;
859                 case ACTSTRIPMODE_ADD:
860                         dstweight = 1.0F;
861                         break;
862                 default :
863                         dstweight = 1.0F;
864         }
865         
866         for(snic= src->first; snic; snic= next) {
867                 next= snic->next;
868                 
869                 dnic= find_nla_ipochannel(dst, snic->poin);
870                 if(dnic==NULL) {
871                         /* remove from src list, and insert in dest */
872                         BLI_remlink(src, snic);
873                         BLI_addtail(dst, snic);
874                 }
875                 else {
876                         /* we do the blend */
877                         dnic->val= dstweight*dnic->val + srcweight*snic->val;
878                 }
879         }
880 }
881
882 static void execute_ipochannels(ListBase *lb)
883 {
884         NlaIpoChannel *nic;
885         
886         for(nic= lb->first; nic; nic= nic->next) {
887                 if(nic->poin) {
888                         write_ipo_poin(nic->poin, nic->type, nic->val);
889                 }
890         }
891 }
892
893 /* nla timing */
894
895 /* this now only used for repeating cycles, to enable fields and blur. */
896 /* the whole time control in blender needs serious thinking... */
897 static float nla_time(float cfra, float unit)
898 {
899         extern float bluroffs;  // bad construct, borrowed from object.c for now
900         extern float fieldoffs;
901         
902         /* motion blur & fields */
903         cfra+= unit*(bluroffs+fieldoffs);
904         
905         /* global time */
906         cfra*= G.scene->r.framelen;     
907
908
909         /* decide later... */
910 //      if(no_speed_curve==0) if(ob && ob->ipo) cfra= calc_ipo_time(ob->ipo, cfra);
911
912         return cfra;
913 }
914
915 /* added "sizecorr" here, to allow armatures to be scaled and still have striding.
916    Only works for uniform scaling. In general I'd advise against scaling armatures ever though! (ton)
917 */
918 static float stridechannel_frame(Object *ob, float sizecorr, bActionStrip *strip, Path *path, float pathdist, float *stride_offset)
919 {
920         bAction *act= strip->act;
921         char *name= strip->stridechannel;
922         bActionChannel *achan= get_action_channel(act, name);
923         int stride_axis= strip->stride_axis;
924
925         if(achan && achan->ipo) {
926                 IpoCurve *icu= NULL;
927                 float minx=0.0f, maxx=0.0f, miny=0.0f, maxy=0.0f;
928                 int foundvert= 0;
929                 
930                 if(stride_axis==0) stride_axis= AC_LOC_X;
931                 else if(stride_axis==1) stride_axis= AC_LOC_Y;
932                 else stride_axis= AC_LOC_Z;
933                 
934                 /* calculate the min/max */
935                 for (icu=achan->ipo->curve.first; icu; icu=icu->next) {
936                         if(icu->adrcode==stride_axis) {
937                                 if(icu->totvert>1) {
938                                         foundvert= 1;
939                                         minx= icu->bezt[0].vec[1][0];
940                                         maxx= icu->bezt[icu->totvert-1].vec[1][0];
941                                         
942                                         miny= icu->bezt[0].vec[1][1];
943                                         maxy= icu->bezt[icu->totvert-1].vec[1][1];
944                                 }
945                                 break;
946                         }
947                 }
948                 
949                 if(foundvert && miny!=maxy) {
950                         float stridelen= sizecorr*fabs(maxy-miny), striptime;
951                         float actiondist, pdist, pdistNewNormalized, offs;
952                         float vec1[4], vec2[4], dir[3];
953                         
954                         /* internal cycling, actoffs is in frames */
955                         offs= stridelen*strip->actoffs/(maxx-minx);
956                         
957                         /* amount path moves object */
958                         pdist = (float)fmod (pathdist+offs, stridelen);
959                         striptime= pdist/stridelen;
960                         
961                         /* amount stride bone moves */
962                         actiondist= sizecorr*eval_icu(icu, minx + striptime*(maxx-minx)) - miny;
963                         
964                         pdist = fabs(actiondist) - pdist;
965                         pdistNewNormalized = (pathdist+pdist)/path->totdist;
966                         
967                         /* now we need to go pdist further (or less) on cu path */
968                         where_on_path(ob, (pathdist)/path->totdist, vec1, dir); /* vec needs size 4 */
969                         if (pdistNewNormalized <= 1) {
970                                 // search for correction in positive path-direction
971                                 where_on_path(ob, pdistNewNormalized, vec2, dir);       /* vec needs size 4 */
972                                 VecSubf(stride_offset, vec2, vec1);
973                         }
974                         else {
975                                 // we reached the end of the path, search backwards instead
976                                 where_on_path(ob, (pathdist-pdist)/path->totdist, vec2, dir);   /* vec needs size 4 */
977                                 VecSubf(stride_offset, vec1, vec2);
978                         }
979                         Mat4Mul3Vecfl(ob->obmat, stride_offset);
980                         return striptime;
981                 }
982         }
983         return 0.0f;
984 }
985
986 static void cyclic_offs_bone(Object *ob, bPose *pose, bActionStrip *strip, float time)
987 {
988         /* only called when strip has cyclic, so >= 1.0f works... */
989         if(time >= 1.0f) {
990                 bActionChannel *achan= get_action_channel(strip->act, strip->offs_bone);
991
992                 if(achan && achan->ipo) {
993                         IpoCurve *icu= NULL;
994                         Bone *bone;
995                         float min[3]={0.0f, 0.0f, 0.0f}, max[3]={0.0f, 0.0f, 0.0f};
996                         int index=0, foundvert= 0;
997                         
998                         /* calculate the min/max */
999                         for (icu=achan->ipo->curve.first; icu; icu=icu->next) {
1000                                 if(icu->totvert>1) {
1001                                         
1002                                         if(icu->adrcode==AC_LOC_X)
1003                                                 index= 0;
1004                                         else if(icu->adrcode==AC_LOC_Y)
1005                                                 index= 1;
1006                                         else if(icu->adrcode==AC_LOC_Z)
1007                                                 index= 2;
1008                                         else
1009                                                 continue;
1010                                 
1011                                         foundvert= 1;
1012                                         min[index]= icu->bezt[0].vec[1][1];
1013                                         max[index]= icu->bezt[icu->totvert-1].vec[1][1];
1014                                 }
1015                         }
1016                         if(foundvert) {
1017                                 /* bring it into armature space */
1018                                 VecSubf(min, max, min);
1019                                 bone= get_named_bone(ob->data, strip->offs_bone);       /* weak */
1020                                 if(bone) {
1021                                         Mat4Mul3Vecfl(bone->arm_mat, min);
1022                                         
1023                                         /* dominant motion, cyclic_offset was cleared in rest_pose */
1024                                         if (strip->flag & (ACTSTRIP_CYCLIC_USEX | ACTSTRIP_CYCLIC_USEY | ACTSTRIP_CYCLIC_USEZ)) {
1025                                                 if (strip->flag & ACTSTRIP_CYCLIC_USEX) pose->cyclic_offset[0]= time*min[0];
1026                                                 if (strip->flag & ACTSTRIP_CYCLIC_USEY) pose->cyclic_offset[1]= time*min[1];
1027                                                 if (strip->flag & ACTSTRIP_CYCLIC_USEZ) pose->cyclic_offset[2]= time*min[2];
1028                                         } else {
1029                                                 if( fabs(min[0]) >= fabs(min[1]) && fabs(min[0]) >= fabs(min[2]))
1030                                                         pose->cyclic_offset[0]= time*min[0];
1031                                                 else if( fabs(min[1]) >= fabs(min[0]) && fabs(min[1]) >= fabs(min[2]))
1032                                                         pose->cyclic_offset[1]= time*min[1];
1033                                                 else
1034                                                         pose->cyclic_offset[2]= time*min[2];
1035                                         }
1036                                 }
1037                         }
1038                 }
1039         }
1040 }
1041
1042 /* simple case for now; only the curve path with constraint value > 0.5 */
1043 /* blending we might do later... */
1044 static Object *get_parent_path(Object *ob)
1045 {
1046         bConstraint *con;
1047         
1048         if(ob->parent && ob->parent->type==OB_CURVE)
1049                 return ob->parent;
1050         
1051         for (con = ob->constraints.first; con; con=con->next) {
1052                 if(con->type==CONSTRAINT_TYPE_FOLLOWPATH) {
1053                         if(con->enforce>0.5f) {
1054                                 bFollowPathConstraint *data= con->data;
1055                                 return data->tar;
1056                         }
1057                 }
1058         }
1059         return NULL;
1060 }
1061
1062 /* ************** do the action ************ */
1063
1064 /* For the calculation of the effects of an action at the given frame on an object 
1065  * This is currently only used for the action constraint 
1066  */
1067 void what_does_obaction (Object *ob, bAction *act, float cframe)
1068 {
1069         ListBase tchanbase= {NULL, NULL};
1070         
1071         clear_workob();
1072         Mat4CpyMat4(workob.obmat, ob->obmat);
1073         Mat4CpyMat4(workob.parentinv, ob->parentinv);
1074         Mat4CpyMat4(workob.constinv, ob->constinv);
1075         workob.parent= ob->parent;
1076         workob.track= ob->track;
1077
1078         workob.trackflag= ob->trackflag;
1079         workob.upflag= ob->upflag;
1080         
1081         workob.partype= ob->partype;
1082         workob.par1= ob->par1;
1083         workob.par2= ob->par2;
1084         workob.par3= ob->par3;
1085
1086         workob.constraints.first = ob->constraints.first;
1087         workob.constraints.last = ob->constraints.last;
1088
1089         strcpy(workob.parsubstr, ob->parsubstr); 
1090         
1091         /* extract_ipochannels_from_action needs id's! */
1092         workob.action= act;
1093         
1094         extract_ipochannels_from_action(&tchanbase, &ob->id, act, "Object", bsystem_time(&workob, cframe, 0.0));
1095         
1096         if (tchanbase.first) {
1097                 execute_ipochannels(&tchanbase);
1098                 BLI_freelistN(&tchanbase);
1099         }
1100 }
1101
1102 /* ----- nla, etc. --------- */
1103
1104 static void do_nla(Object *ob, int blocktype)
1105 {
1106         bPose *tpose= NULL;
1107         Key *key= NULL;
1108         ListBase tchanbase={NULL, NULL}, chanbase={NULL, NULL};
1109         bActionStrip *strip, *striplast=NULL, *stripfirst=NULL;
1110         float striptime, frametime, length, actlength;
1111         float blendfac, stripframe;
1112         float scene_cfra= frame_to_float(G.scene->r.cfra); 
1113         int     doit, dostride;
1114         
1115         if(blocktype==ID_AR) {
1116                 copy_pose(&tpose, ob->pose, 1);
1117                 rest_pose(ob->pose);            // potentially destroying current not-keyed pose
1118         }
1119         else {
1120                 key= ob_get_key(ob);
1121         }
1122         
1123         /* check on extend to left or right, when no strip is hit by 'cfra' */
1124         for (strip=ob->nlastrips.first; strip; strip=strip->next) {
1125                 /* escape loop on a hit */
1126                 if( scene_cfra >= strip->start && scene_cfra <= strip->end + 0.1f)      /* note 0.1 comes back below */
1127                         break;
1128                 if(scene_cfra < strip->start) {
1129                         if(stripfirst==NULL)
1130                                 stripfirst= strip;
1131                         else if(stripfirst->start > strip->start)
1132                                 stripfirst= strip;
1133                 }
1134                 else if(scene_cfra > strip->end) {
1135                         if(striplast==NULL)
1136                                 striplast= strip;
1137                         else if(striplast->end < strip->end)
1138                                 striplast= strip;
1139                 }
1140         }
1141         if(strip==NULL) {       /* extend */
1142                 if(striplast)
1143                         scene_cfra= striplast->end;
1144                 else if(stripfirst)
1145                         scene_cfra= stripfirst->start;
1146         }
1147         
1148         /* and now go over all strips */
1149         for (strip=ob->nlastrips.first; strip; strip=strip->next){
1150                 doit=dostride= 0;
1151                 
1152                 if (strip->act && !(strip->flag & ACTSTRIP_MUTE)) {     /* so theres an action */
1153                         
1154                         /* Determine if the current frame is within the strip's range */
1155                         length = strip->end-strip->start;
1156                         actlength = strip->actend-strip->actstart;
1157                         striptime = (scene_cfra-(strip->start)) / length;
1158                         stripframe = (scene_cfra-(strip->start)) ;
1159
1160                         if (striptime>=0.0){
1161                                 
1162                                 if(blocktype==ID_AR) 
1163                                         rest_pose(tpose);
1164                                 
1165                                 /* To handle repeat, we add 0.1 frame extra to make sure the last frame is included */
1166                                 if (striptime < 1.0f + 0.1f/length) {
1167                                         
1168                                         /* Handle path */
1169                                         if ((strip->flag & ACTSTRIP_USESTRIDE) && (blocktype==ID_AR) && (ob->ipoflag & OB_DISABLE_PATH)==0){
1170                                                 Object *parent= get_parent_path(ob);
1171                                                 
1172                                                 if (parent) {
1173                                                         Curve *cu = parent->data;
1174                                                         float ctime, pdist;
1175                                                         
1176                                                         if (cu->flag & CU_PATH){
1177                                                                 /* Ensure we have a valid path */
1178                                                                 if(cu->path==NULL || cu->path->data==NULL) makeDispListCurveTypes(parent, 0);
1179                                                                 if(cu->path) {
1180                                                                         
1181                                                                         /* Find the position on the path */
1182                                                                         ctime= bsystem_time(ob, scene_cfra, 0.0);
1183                                                                         
1184                                                                         if(calc_ipo_spec(cu->ipo, CU_SPEED, &ctime)==0) {
1185                                                                                 /* correct for actions not starting on zero */
1186                                                                                 ctime= (ctime - strip->actstart)/cu->pathlen;
1187                                                                                 CLAMP(ctime, 0.0, 1.0);
1188                                                                         }
1189                                                                         pdist = ctime*cu->path->totdist;
1190                                                                         
1191                                                                         if(tpose && strip->stridechannel[0]) {
1192                                                                                 striptime= stridechannel_frame(parent, ob->size[0], strip, cu->path, pdist, tpose->stride_offset);
1193                                                                         }                                                                       
1194                                                                         else {
1195                                                                                 if (strip->stridelen) {
1196                                                                                         striptime = pdist / strip->stridelen;
1197                                                                                         striptime = (float)fmod (striptime+strip->actoffs, 1.0);
1198                                                                                 }
1199                                                                                 else
1200                                                                                         striptime = 0;
1201                                                                         }
1202                                                                         
1203                                                                         frametime = (striptime * actlength) + strip->actstart;
1204                                                                         frametime= bsystem_time(ob, frametime, 0.0);
1205                                                                         
1206                                                                         if(blocktype==ID_AR) {
1207                                                                                 extract_pose_from_action (tpose, strip->act, frametime);
1208                                                                         }
1209                                                                         else if(blocktype==ID_OB) {
1210                                                                                 extract_ipochannels_from_action(&tchanbase, &ob->id, strip->act, "Object", frametime);
1211                                                                                 if(key)
1212                                                                                         extract_ipochannels_from_action(&tchanbase, &key->id, strip->act, "Shape", frametime);
1213                                                                         }
1214                                                                         doit=dostride= 1;
1215                                                                 }
1216                                                         }
1217                                                 }
1218                                         }
1219                                         /* To handle repeat, we add 0.1 frame extra to make sure the last frame is included */
1220                                         else  {
1221                                                 
1222                                                 /* Mod to repeat */
1223                                                 if(strip->repeat!=1.0f) {
1224                                                         float cycle= striptime*strip->repeat;
1225                                                         
1226                                                         striptime = (float)fmod (cycle, 1.0f + 0.1f/length);
1227                                                         cycle-= striptime;
1228                                                         
1229                                                         if(blocktype==ID_AR)
1230                                                                 cyclic_offs_bone(ob, tpose, strip, cycle);
1231                                                 }
1232
1233                                                 frametime = (striptime * actlength) + strip->actstart;
1234                                                 frametime= nla_time(frametime, (float)strip->repeat);
1235                                                         
1236                                                 if(blocktype==ID_AR) {
1237                                                         extract_pose_from_action (tpose, strip->act, frametime);
1238                                                 }
1239                                                 else if(blocktype==ID_OB) {
1240                                                         extract_ipochannels_from_action(&tchanbase, &ob->id, strip->act, "Object", frametime);
1241                                                         if(key)
1242                                                                 extract_ipochannels_from_action(&tchanbase, &key->id, strip->act, "Shape", frametime);
1243                                                 }
1244                                                 
1245                                                 doit=1;
1246                                         }
1247                                 }
1248                                 /* Handle extend */
1249                                 else {
1250                                         if (strip->flag & ACTSTRIP_HOLDLASTFRAME){
1251                                                 /* we want the strip to hold on the exact fraction of the repeat value */
1252                                                 
1253                                                 frametime = actlength * (strip->repeat-(int)strip->repeat);
1254                                                 if(frametime<=0.000001f) frametime= actlength;  /* rounding errors... */
1255                                                 frametime= bsystem_time(ob, frametime+strip->actstart, 0.0);
1256                                                 
1257                                                 if(blocktype==ID_AR)
1258                                                         extract_pose_from_action (tpose, strip->act, frametime);
1259                                                 else if(blocktype==ID_OB) {
1260                                                         extract_ipochannels_from_action(&tchanbase, &ob->id, strip->act, "Object", frametime);
1261                                                         if(key)
1262                                                                 extract_ipochannels_from_action(&tchanbase, &key->id, strip->act, "Shape", frametime);
1263                                                 }
1264                                                 
1265                                                 /* handle cycle hold */
1266                                                 if(strip->repeat!=1.0f) {
1267                                                         if(blocktype==ID_AR)
1268                                                                 cyclic_offs_bone(ob, tpose, strip, strip->repeat-1.0f);
1269                                                 }
1270                                                 
1271                                                 doit=1;
1272                                         }
1273                                 }
1274                                 
1275                                 /* Handle blendin & blendout */
1276                                 if (doit){
1277                                         /* Handle blendin */
1278                                         
1279                                         if (strip->blendin>0.0 && stripframe<=strip->blendin && scene_cfra>=strip->start){
1280                                                 blendfac = stripframe/strip->blendin;
1281                                         }
1282                                         else if (strip->blendout>0.0 && stripframe>=(length-strip->blendout) && scene_cfra<=strip->end){
1283                                                 blendfac = (length-stripframe)/(strip->blendout);
1284                                         }
1285                                         else
1286                                                 blendfac = 1;
1287                                         
1288                                         if(blocktype==ID_AR) {/* Blend this pose with the accumulated pose */
1289                                                 /* offset bone, for matching cycles */
1290                                                 blend_pose_offset_bone (strip, ob->pose, tpose, blendfac, strip->mode);
1291                                                 
1292                                                 blend_poses (ob->pose, tpose, blendfac, strip->mode);
1293                                                 if(dostride)
1294                                                         blend_pose_strides (ob->pose, tpose, blendfac, strip->mode);
1295                                         }
1296                                         else {
1297                                                 blend_ipochannels(&chanbase, &tchanbase, blendfac, strip->mode);
1298                                                 BLI_freelistN(&tchanbase);
1299                                         }
1300                                 }
1301                         }                                       
1302                 }
1303         }
1304         
1305         if(blocktype==ID_OB) {
1306                 execute_ipochannels(&chanbase);
1307         }
1308         else if(blocktype==ID_AR) {
1309                 /* apply stride offset to object */
1310                 VecAddf(ob->obmat[3], ob->obmat[3], ob->pose->stride_offset);
1311         }
1312         
1313         /* free */
1314         if (tpose){
1315                 free_pose_channels(tpose);
1316                 MEM_freeN(tpose);
1317         }
1318         if(chanbase.first)
1319                 BLI_freelistN(&chanbase);
1320 }
1321
1322 void do_all_pose_actions(Object *ob)
1323 {
1324         /* only to have safe calls from editor */
1325         if(ob==NULL) return;
1326         if(ob->type!=OB_ARMATURE || ob->pose==NULL) return;
1327
1328         if(ob->pose->flag & POSE_LOCKED) {  /*  no actions to execute while transform */
1329                 if(ob->pose->flag & POSE_DO_UNLOCK)
1330                         ob->pose->flag &= ~(POSE_LOCKED|POSE_DO_UNLOCK);
1331         }
1332         else if(ob->action && ((ob->nlaflag & OB_NLA_OVERRIDE)==0 || ob->nlastrips.first==NULL) ) {
1333                 float cframe= (float) G.scene->r.cfra;
1334                 
1335                 cframe= get_action_frame(ob, cframe);
1336                 
1337                 extract_pose_from_action (ob->pose, ob->action, bsystem_time(ob, cframe, 0.0));
1338         }
1339         else if(ob->nlastrips.first) {
1340                 do_nla(ob, ID_AR);
1341         }
1342         
1343         /* clear POSE_DO_UNLOCK flags that might have slipped through (just in case) */
1344         ob->pose->flag &= ~POSE_DO_UNLOCK;
1345 }
1346
1347 /* called from where_is_object */
1348 void do_all_object_actions(Object *ob)
1349 {
1350         if(ob==NULL) return;
1351         if(ob->dup_group) return;       /* prevent conflicts, might add smarter check later */
1352         
1353         /* Do local action */
1354         if(ob->action && ((ob->nlaflag & OB_NLA_OVERRIDE)==0 || ob->nlastrips.first==NULL) ) {
1355                 ListBase tchanbase= {NULL, NULL};
1356                 Key *key= ob_get_key(ob);
1357                 float cframe= (float) G.scene->r.cfra;
1358                 
1359                 cframe= get_action_frame(ob, cframe);
1360                 
1361                 extract_ipochannels_from_action(&tchanbase, &ob->id, ob->action, "Object", bsystem_time(ob, cframe, 0.0));
1362                 if(key)
1363                         extract_ipochannels_from_action(&tchanbase, &key->id, ob->action, "Shape", bsystem_time(ob, cframe, 0.0));
1364                 
1365                 if(tchanbase.first) {
1366                         execute_ipochannels(&tchanbase);
1367                         BLI_freelistN(&tchanbase);
1368                 }
1369         }
1370         else if(ob->nlastrips.first) {
1371                 do_nla(ob, ID_OB);
1372         }
1373 }