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