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