NLA SoC: Merge from 2.5
[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  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31
32 #include <string.h>
33 #include <math.h>
34 #include <stdlib.h>     /* for NULL */
35
36 #include "MEM_guardedalloc.h"
37
38 #include "DNA_anim_types.h"
39 #include "DNA_action_types.h"
40 #include "DNA_armature_types.h"
41 #include "DNA_constraint_types.h"
42 #include "DNA_curve_types.h"
43 #include "DNA_key_types.h"
44 #include "DNA_nla_types.h"
45 #include "DNA_object_types.h"
46 #include "DNA_scene_types.h"
47
48 #include "BKE_animsys.h"
49 #include "BKE_action.h"
50 #include "BKE_anim.h"
51 #include "BKE_armature.h"
52 #include "BKE_blender.h"
53 #include "BKE_constraint.h"
54 #include "BKE_displist.h"
55 #include "BKE_global.h"
56 #include "BKE_fcurve.h"
57 #include "BKE_key.h"
58 #include "BKE_lattice.h"
59 #include "BKE_library.h"
60 #include "BKE_main.h"
61 #include "BKE_object.h"
62 #include "BKE_utildefines.h"
63
64 #include "BLI_arithb.h"
65 #include "BLI_blenlib.h"
66 #include "BLI_ghash.h"
67
68 #include "RNA_access.h"
69 #include "RNA_types.h"
70
71 //XXX #include "nla.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 void game_free_pose(bPose *pose)
585 {
586         if (pose) {
587                 /* we don't free constraints, those are owned by the original pose */
588                 if(pose->chanbase.first)
589                         BLI_freelistN(&pose->chanbase);
590                 
591                 MEM_freeN(pose);
592         }
593 }
594
595 static void copy_pose_channel_data(bPoseChannel *pchan, const bPoseChannel *chan)
596 {
597         bConstraint *pcon, *con;
598         
599         VECCOPY(pchan->loc, chan->loc);
600         VECCOPY(pchan->size, chan->size);
601         VECCOPY(pchan->eul, chan->eul);
602         QUATCOPY(pchan->quat, chan->quat);
603         pchan->rotmode= chan->rotmode;
604         Mat4CpyMat4(pchan->chan_mat, (float(*)[4])chan->chan_mat);
605         Mat4CpyMat4(pchan->pose_mat, (float(*)[4])chan->pose_mat);
606         pchan->flag= chan->flag;
607         
608         con= chan->constraints.first;
609         for(pcon= pchan->constraints.first; pcon; pcon= pcon->next, con= con->next) {
610                 pcon->enforce= con->enforce;
611                 pcon->headtail= con->headtail;
612         }
613 }
614
615 /* checks for IK constraint, and also for Follow-Path constraint.
616  * can do more constraints flags later 
617  */
618 /* pose should be entirely OK */
619 void update_pose_constraint_flags(bPose *pose)
620 {
621         bPoseChannel *pchan, *parchan;
622         bConstraint *con;
623         
624         /* clear */
625         for (pchan= pose->chanbase.first; pchan; pchan= pchan->next) {
626                 pchan->constflag= 0;
627         }
628         pose->flag &= ~POSE_CONSTRAINTS_TIMEDEPEND;
629         
630         /* detect */
631         for (pchan= pose->chanbase.first; pchan; pchan=pchan->next) {
632                 for (con= pchan->constraints.first; con; con= con->next) {
633                         if (con->type==CONSTRAINT_TYPE_KINEMATIC) {
634                                 bKinematicConstraint *data = (bKinematicConstraint*)con->data;
635                                 
636                                 pchan->constflag |= PCHAN_HAS_IK;
637                                 
638                                 if(data->tar==NULL || (data->tar->type==OB_ARMATURE && data->subtarget[0]==0))
639                                         pchan->constflag |= PCHAN_HAS_TARGET;
640                                 
641                                 /* negative rootbone = recalc rootbone index. used in do_versions */
642                                 if(data->rootbone<0) {
643                                         data->rootbone= 0;
644                                         
645                                         if(data->flag & CONSTRAINT_IK_TIP) parchan= pchan;
646                                         else parchan= pchan->parent;
647                                         
648                                         while(parchan) {
649                                                 data->rootbone++;
650                                                 if((parchan->bone->flag & BONE_CONNECTED)==0)
651                                                         break;
652                                                 parchan= parchan->parent;
653                                         }
654                                 }
655                         }
656                         else if (con->type == CONSTRAINT_TYPE_FOLLOWPATH) {
657                                 bFollowPathConstraint *data= (bFollowPathConstraint *)con->data;
658                                 
659                                 /* for drawing constraint colors when color set allows this */
660                                 pchan->constflag |= PCHAN_HAS_CONST;
661                                 
662                                 /* if we have a valid target, make sure that this will get updated on frame-change
663                                  * (needed for when there is no anim-data for this pose)
664                                  */
665                                 if ((data->tar) && (data->tar->type==OB_CURVE))
666                                         pose->flag |= POSE_CONSTRAINTS_TIMEDEPEND;
667                         }
668                         else 
669                                 pchan->constflag |= PCHAN_HAS_CONST;
670                 }
671         }
672 }
673
674 /* Clears all BONE_UNKEYED flags for every pose channel in every pose 
675  * This should only be called on frame changing, when it is acceptable to
676  * do this. Otherwise, these flags should not get cleared as poses may get lost.
677  */
678 void framechange_poses_clear_unkeyed(void)
679 {
680         Object *ob;
681         bPose *pose;
682         bPoseChannel *pchan;
683         
684         /* This needs to be done for each object that has a pose */
685         // TODO: proxies may/may not be correctly handled here... (this needs checking) 
686         for (ob= G.main->object.first; ob; ob= ob->id.next) {
687                 /* we only need to do this on objects with a pose */
688                 if ( (pose= ob->pose) ) {
689                         for (pchan= pose->chanbase.first; pchan; pchan= pchan->next) {
690                                 if (pchan->bone) 
691                                         pchan->bone->flag &= ~BONE_UNKEYED;
692                         }
693                 }
694         }
695 }
696
697 /* ************************ END Pose channels *************** */
698
699 /* ************** time ****************** */
700
701 static bActionStrip *get_active_strip(Object *ob)
702 {
703 #if 0   // XXX old animation system
704         bActionStrip *strip;
705         
706         if(ob->action==NULL)
707                 return NULL;
708                 
709         for (strip=ob->nlastrips.first; strip; strip=strip->next)
710                 if(strip->flag & ACTSTRIP_ACTIVE)
711                         break;
712         
713         if(strip && strip->act==ob->action)
714                 return strip;
715 #endif // XXX old animation system
716                 
717         return NULL;
718 }
719
720 /* if the conditions match, it converts current time to strip time */
721 // TODO: change this adt
722 float get_action_frame(Object *ob, float cframe)
723 {
724         bActionStrip *strip= get_active_strip(ob);
725         
726         //if(strip)
727         //      return get_actionstrip_frame(strip, cframe, 0);
728         return cframe;
729 }
730
731 /* inverted, strip time to current time */
732 // TODO: change this to adt
733 float get_action_frame_inv(Object *ob, float cframe)
734 {
735         bActionStrip *strip= get_active_strip(ob);
736         
737         //if(strip)
738         //      return get_actionstrip_frame(strip, cframe, 1);
739         return cframe;
740 }
741
742
743 /* Check if the given action has any keyframes */
744 short action_has_motion(const bAction *act)
745 {
746         FCurve *fcu;
747         
748         /* return on the first F-Curve that has some keyframes/samples defined */
749         if (act) {
750                 for (fcu= act->curves.first; fcu; fcu= fcu->next) {
751                         if (fcu->totvert)
752                                 return 1;
753                 }
754         }
755         
756         /* nothing found */
757         return 0;
758 }
759
760 /* Calculate the extents of given action */
761 void calc_action_range(const bAction *act, float *start, float *end, int incl_hidden)
762 {
763         FCurve *fcu;
764         float min=999999999.0f, max=-999999999.0f;
765         short foundvert=0;
766
767         if (act) {
768                 for (fcu= act->curves.first; fcu; fcu= fcu->next) {
769                         if (fcu->totvert) {
770                                 float nmin, nmax;
771                                 
772                                 /* get extents for this curve */
773                                 calc_fcurve_range(fcu, &nmin, &nmax);
774                                 
775                                 /* compare to the running tally */
776                                 min= MIN2(min, nmin);
777                                 max= MAX2(max, nmax);
778                                 
779                                 foundvert= 1;
780                         }
781                 }
782         }       
783         
784         if (foundvert) {
785                 if(min==max) max+= 1.0f;
786                 *start= min;
787                 *end= max;
788         }
789         else {
790                 *start= 0.0f;
791                 *end= 1.0f;
792         }
793 }
794
795 /* Copy the data from the action-pose (src) into the pose */
796 /* both args are assumed to be valid */
797 /* exported to game engine */
798 void extract_pose_from_pose(bPose *pose, const bPose *src)
799 {
800         const bPoseChannel *schan;
801         bPoseChannel *pchan= pose->chanbase.first;
802
803         if (pose==src) {
804                 printf("extract_pose_from_pose source and target are the same\n");
805                 return;
806         }
807
808         for (schan=src->chanbase.first; schan; schan=schan->next, pchan= pchan->next) {
809                 copy_pose_channel_data(pchan, schan);
810         }
811 }
812
813 /* for do_all_pose_actions, clears the pose. Now also exported for proxy and tools */
814 void rest_pose(bPose *pose)
815 {
816         bPoseChannel *pchan;
817         int i;
818         
819         if (!pose)
820                 return;
821         
822         memset(pose->stride_offset, 0, sizeof(pose->stride_offset));
823         memset(pose->cyclic_offset, 0, sizeof(pose->cyclic_offset));
824         
825         for (pchan=pose->chanbase.first; pchan; pchan= pchan->next){
826                 for (i=0; i<3; i++) {
827                         pchan->loc[i]= 0.0f;
828                         pchan->quat[i+1]= 0.0f;
829                         pchan->eul[i]= 0.0f;
830                         pchan->size[i]= 1.0f;
831                 }
832                 pchan->quat[0]= 1.0f;
833                 
834                 pchan->flag &= ~(POSE_LOC|POSE_ROT|POSE_SIZE);
835         }
836 }
837
838 /* both poses should be in sync */
839 void copy_pose_result(bPose *to, bPose *from)
840 {
841         bPoseChannel *pchanto, *pchanfrom;
842         
843         if(to==NULL || from==NULL) {
844                 printf("pose result copy error\n"); // debug temp
845                 return;
846         }
847
848         if (to==from) {
849                 printf("copy_pose_result source and target are the same\n");
850                 return;
851         }
852
853
854         for(pchanfrom= from->chanbase.first; pchanfrom; pchanfrom= pchanfrom->next) {
855                 pchanto= get_pose_channel(to, pchanfrom->name);
856                 if(pchanto) {
857                         Mat4CpyMat4(pchanto->pose_mat, pchanfrom->pose_mat);
858                         Mat4CpyMat4(pchanto->chan_mat, pchanfrom->chan_mat);
859                         
860                         /* used for local constraints */
861                         VECCOPY(pchanto->loc, pchanfrom->loc);
862                         QUATCOPY(pchanto->quat, pchanfrom->quat);
863                         VECCOPY(pchanto->eul, pchanfrom->eul);
864                         VECCOPY(pchanto->size, pchanfrom->size);
865                         
866                         VECCOPY(pchanto->pose_head, pchanfrom->pose_head);
867                         VECCOPY(pchanto->pose_tail, pchanfrom->pose_tail);
868                         pchanto->flag= pchanfrom->flag;
869                 }
870         }
871 }
872
873 /* For the calculation of the effects of an Action at the given frame on an object 
874  * This is currently only used for the Action Constraint 
875  */
876 void what_does_obaction (Scene *scene, Object *ob, Object *workob, bPose *pose, bAction *act, char groupname[], float cframe)
877 {
878         bActionGroup *agrp= action_groups_find_named(act, groupname);
879         
880         /* clear workob */
881         clear_workob(workob);
882         
883         /* init workob */
884         Mat4CpyMat4(workob->obmat, ob->obmat);
885         Mat4CpyMat4(workob->parentinv, ob->parentinv);
886         Mat4CpyMat4(workob->constinv, ob->constinv);
887         workob->parent= ob->parent;
888         workob->track= ob->track;
889
890         workob->trackflag= ob->trackflag;
891         workob->upflag= ob->upflag;
892         
893         workob->partype= ob->partype;
894         workob->par1= ob->par1;
895         workob->par2= ob->par2;
896         workob->par3= ob->par3;
897
898         workob->constraints.first = ob->constraints.first;
899         workob->constraints.last = ob->constraints.last;
900         
901         workob->pose= pose;     /* need to set pose too, since this is used for both types of Action Constraint */
902
903         strcpy(workob->parsubstr, ob->parsubstr);
904         strcpy(workob->id.name, "OB<ConstrWorkOb>"); /* we don't use real object name, otherwise RNA screws with the real thing */
905         
906         /* if we're given a group to use, it's likely to be more efficient (though a bit more dangerous) */
907         if (agrp) {
908                 /* specifically evaluate this group only */
909                 PointerRNA id_ptr;
910                 
911                 /* get RNA-pointer for the workob's ID */
912                 RNA_id_pointer_create(&workob->id, &id_ptr);
913                 
914                 /* execute action for this group only */
915                 animsys_evaluate_action_group(&id_ptr, act, agrp, NULL, cframe);
916         }
917         else {
918                 AnimData adt;
919                 
920                 /* init animdata, and attach to workob */
921                 memset(&adt, 0, sizeof(AnimData));
922                 workob->adt= &adt;
923                 
924                 adt.recalc= ADT_RECALC_ANIM;
925                 adt.action= act;
926                 
927                 /* execute effects of Action on to workob (or it's PoseChannels) */
928                 BKE_animsys_evaluate_animdata(&workob->id, &adt, cframe, ADT_RECALC_ANIM);
929         }
930 }
931
932 /* ********** NLA with non-poses works with ipo channels ********** */
933
934 #if 0 // XXX OLD ANIMATION SYSTEM (TO BE REMOVED)
935
936 /* ************************ Blending with NLA *************** */
937
938 static void blend_pose_strides(bPose *dst, bPose *src, float srcweight, short mode)
939 {
940         float dstweight;
941         
942         switch (mode){
943                 case ACTSTRIPMODE_BLEND:
944                         dstweight = 1.0F - srcweight;
945                         break;
946                 case ACTSTRIPMODE_ADD:
947                         dstweight = 1.0F;
948                         break;
949                 default :
950                         dstweight = 1.0F;
951         }
952         
953         VecLerpf(dst->stride_offset, dst->stride_offset, src->stride_offset, srcweight);
954 }
955
956
957 /* 
958
959 bone matching diagram, strips A and B
960
961                  .------------------------.
962                  |         A              |
963                  '------------------------'
964                                  .          .             b2
965                  .          .-------------v----------.
966                  .              |         B   .          |
967                  .          '------------------------'
968                  .          .             .
969                  .          .             .
970 offset:          .    0     .    A-B      .  A-b2+B     
971                  .          .             .
972
973 */
974
975
976 static void blend_pose_offset_bone(bActionStrip *strip, bPose *dst, bPose *src, float srcweight, short mode)
977 {
978         /* matching offset bones */
979         /* take dst offset, and put src on on that location */
980         
981         if(strip->offs_bone[0]==0)
982                 return;
983         
984         /* are we also blending with matching bones? */
985         if(strip->prev && strip->start>=strip->prev->start) {
986                 bPoseChannel *dpchan= get_pose_channel(dst, strip->offs_bone);
987                 if(dpchan) {
988                         bPoseChannel *spchan= get_pose_channel(src, strip->offs_bone);
989                         if(spchan) {
990                                 float vec[3];
991                                 
992                                 /* dst->ctime has the internal strip->prev action time */
993                                 /* map this time to nla time */
994                                 
995                                 float ctime= get_actionstrip_frame(strip, src->ctime, 1);
996                                 
997                                 if( ctime > strip->prev->end) {
998                                         bActionChannel *achan;
999                                         
1000                                         /* add src to dest, minus the position of src on strip->prev->end */
1001                                         
1002                                         ctime= get_actionstrip_frame(strip, strip->prev->end, 0);
1003                                         
1004                                         achan= get_action_channel(strip->act, strip->offs_bone);
1005                                         if(achan && achan->ipo) {
1006                                                 bPoseChannel pchan;
1007                                                 /* Evaluates and sets the internal ipo value */
1008                                                 calc_ipo(achan->ipo, ctime);
1009                                                 /* This call also sets the pchan flags */
1010                                                 execute_action_ipo(achan, &pchan);
1011                                                 
1012                                                 /* store offset that moves src to location of pchan */
1013                                                 VecSubf(vec, dpchan->loc, pchan.loc);
1014                                                 
1015                                                 Mat4Mul3Vecfl(dpchan->bone->arm_mat, vec);
1016                                         }
1017                                 }
1018                                 else {
1019                                         /* store offset that moves src to location of dst */
1020                                         
1021                                         VecSubf(vec, dpchan->loc, spchan->loc);
1022                                         Mat4Mul3Vecfl(dpchan->bone->arm_mat, vec);
1023                                 }
1024                                 
1025                                 /* if blending, we only add with factor scrweight */
1026                                 VecMulf(vec, srcweight);
1027                                 
1028                                 VecAddf(dst->cyclic_offset, dst->cyclic_offset, vec);
1029                         }
1030                 }
1031         }
1032         
1033         VecAddf(dst->cyclic_offset, dst->cyclic_offset, src->cyclic_offset);
1034 }
1035
1036
1037 /* Only allowed for Poses with identical channels */
1038 void blend_poses(bPose *dst, bPose *src, float srcweight, short mode)
1039 {
1040         bPoseChannel *dchan;
1041         const bPoseChannel *schan;
1042         bConstraint *dcon, *scon;
1043         float dstweight;
1044         int i;
1045         
1046         switch (mode){
1047         case ACTSTRIPMODE_BLEND:
1048                 dstweight = 1.0F - srcweight;
1049                 break;
1050         case ACTSTRIPMODE_ADD:
1051                 dstweight = 1.0F;
1052                 break;
1053         default :
1054                 dstweight = 1.0F;
1055         }
1056         
1057         schan= src->chanbase.first;
1058         for (dchan = dst->chanbase.first; dchan; dchan=dchan->next, schan= schan->next){
1059                 if (schan->flag & (POSE_ROT|POSE_LOC|POSE_SIZE)) {
1060                         /* replaced quat->matrix->quat conversion with decent quaternion interpol (ton) */
1061                         
1062                         /* Do the transformation blend */
1063                         if (schan->flag & POSE_ROT) {
1064                                 /* quat interpolation done separate */
1065                                 if (schan->rotmode == PCHAN_ROT_QUAT) {
1066                                         float dquat[4], squat[4];
1067                                         
1068                                         QUATCOPY(dquat, dchan->quat);
1069                                         QUATCOPY(squat, schan->quat);
1070                                         if (mode==ACTSTRIPMODE_BLEND)
1071                                                 QuatInterpol(dchan->quat, dquat, squat, srcweight);
1072                                         else {
1073                                                 QuatMulFac(squat, srcweight);
1074                                                 QuatMul(dchan->quat, dquat, squat);
1075                                         }
1076                                         
1077                                         NormalQuat(dchan->quat);
1078                                 }
1079                         }
1080
1081                         for (i=0; i<3; i++) {
1082                                 /* blending for loc and scale are pretty self-explanatory... */
1083                                 if (schan->flag & POSE_LOC)
1084                                         dchan->loc[i] = (dchan->loc[i]*dstweight) + (schan->loc[i]*srcweight);
1085                                 if (schan->flag & POSE_SIZE)
1086                                         dchan->size[i] = 1.0f + ((dchan->size[i]-1.0f)*dstweight) + ((schan->size[i]-1.0f)*srcweight);
1087                                 
1088                                 /* euler-rotation interpolation done here instead... */
1089                                 // FIXME: are these results decent?
1090                                 if ((schan->flag & POSE_ROT) && (schan->rotmode))
1091                                         dchan->eul[i] = (dchan->eul[i]*dstweight) + (schan->eul[i]*srcweight);
1092                         }
1093                         dchan->flag |= schan->flag;
1094                 }
1095                 for(dcon= dchan->constraints.first, scon= schan->constraints.first; dcon && scon; dcon= dcon->next, scon= scon->next) {
1096                         /* no 'add' option for constraint blending */
1097                         dcon->enforce= dcon->enforce*(1.0f-srcweight) + scon->enforce*srcweight;
1098                 }
1099         }
1100         
1101         /* this pose is now in src time */
1102         dst->ctime= src->ctime;
1103 }
1104
1105 typedef struct NlaIpoChannel {
1106         struct NlaIpoChannel *next, *prev;
1107         float val;
1108         void *poin;
1109         int type;
1110 } NlaIpoChannel;
1111
1112 void extract_ipochannels_from_action(ListBase *lb, ID *id, bAction *act, const char *name, float ctime)
1113 {
1114         bActionChannel *achan= get_action_channel(act, name);
1115         IpoCurve *icu;
1116         NlaIpoChannel *nic;
1117         
1118         if(achan==NULL) return;
1119         
1120         if(achan->ipo) {
1121                 calc_ipo(achan->ipo, ctime);
1122                 
1123                 for(icu= achan->ipo->curve.first; icu; icu= icu->next) {
1124                         /* skip IPO_BITS, is for layers and cannot be blended */
1125                         if(icu->vartype != IPO_BITS) {
1126                                 nic= MEM_callocN(sizeof(NlaIpoChannel), "NlaIpoChannel");
1127                                 BLI_addtail(lb, nic);
1128                                 nic->val= icu->curval;
1129                                 nic->poin= get_ipo_poin(id, icu, &nic->type);
1130                         }
1131                 }
1132         }
1133         
1134         /* constraint channels only for objects */
1135         if(GS(id->name)==ID_OB) {
1136                 Object *ob= (Object *)id;
1137                 bConstraint *con;
1138                 bConstraintChannel *conchan;
1139                 
1140                 for (con=ob->constraints.first; con; con=con->next) {
1141                         conchan = get_constraint_channel(&achan->constraintChannels, con->name);
1142                         
1143                         if(conchan && conchan->ipo) {
1144                                 calc_ipo(conchan->ipo, ctime);
1145                                 
1146                                 icu= conchan->ipo->curve.first; // only one ipo now
1147                                 if(icu) {
1148                                         nic= MEM_callocN(sizeof(NlaIpoChannel), "NlaIpoChannel constr");
1149                                         BLI_addtail(lb, nic);
1150                                         nic->val= icu->curval;
1151                                         nic->poin= &con->enforce;
1152                                         nic->type= IPO_FLOAT;
1153                                 }
1154                         }
1155                 }
1156         }
1157 }
1158
1159 static NlaIpoChannel *find_nla_ipochannel(ListBase *lb, void *poin)
1160 {
1161         NlaIpoChannel *nic;
1162         
1163         if(poin) {
1164                 for(nic= lb->first; nic; nic= nic->next) {
1165                         if(nic->poin==poin)
1166                                 return nic;
1167                 }
1168         }
1169         return NULL;
1170 }
1171
1172
1173 static void blend_ipochannels(ListBase *dst, ListBase *src, float srcweight, int mode)
1174 {
1175         NlaIpoChannel *snic, *dnic, *next;
1176         float dstweight;
1177         
1178         switch (mode){
1179                 case ACTSTRIPMODE_BLEND:
1180                         dstweight = 1.0F - srcweight;
1181                         break;
1182                 case ACTSTRIPMODE_ADD:
1183                         dstweight = 1.0F;
1184                         break;
1185                 default :
1186                         dstweight = 1.0F;
1187         }
1188         
1189         for(snic= src->first; snic; snic= next) {
1190                 next= snic->next;
1191                 
1192                 dnic= find_nla_ipochannel(dst, snic->poin);
1193                 if(dnic==NULL) {
1194                         /* remove from src list, and insert in dest */
1195                         BLI_remlink(src, snic);
1196                         BLI_addtail(dst, snic);
1197                 }
1198                 else {
1199                         /* we do the blend */
1200                         dnic->val= dstweight*dnic->val + srcweight*snic->val;
1201                 }
1202         }
1203 }
1204
1205 int execute_ipochannels(ListBase *lb)
1206 {
1207         NlaIpoChannel *nic;
1208         int count = 0;
1209         
1210         for(nic= lb->first; nic; nic= nic->next) {
1211                 if(nic->poin) {
1212                         write_ipo_poin(nic->poin, nic->type, nic->val);
1213                         count++;
1214                 }
1215         }
1216         return count;
1217 }
1218
1219 /* nla timing */
1220
1221 /* this now only used for repeating cycles, to enable fields and blur. */
1222 /* the whole time control in blender needs serious thinking... */
1223 static float nla_time(Scene *scene, float cfra, float unit)
1224 {
1225         extern float bluroffs;  // bad construct, borrowed from object.c for now
1226         extern float fieldoffs;
1227         
1228         /* motion blur & fields */
1229         cfra+= unit*(bluroffs+fieldoffs);
1230         
1231         /* global time */
1232         cfra*= scene->r.framelen;       
1233         
1234         return cfra;
1235 }
1236
1237 /* added "sizecorr" here, to allow armatures to be scaled and still have striding.
1238    Only works for uniform scaling. In general I'd advise against scaling armatures ever though! (ton)
1239 */
1240 static float stridechannel_frame(Object *ob, float sizecorr, bActionStrip *strip, Path *path, float pathdist, float *stride_offset)
1241 {
1242         bAction *act= strip->act;
1243         const char *name= strip->stridechannel;
1244         bActionChannel *achan= get_action_channel(act, name);
1245         int stride_axis= strip->stride_axis;
1246
1247         if(achan && achan->ipo) {
1248                 IpoCurve *icu= NULL;
1249                 float minx=0.0f, maxx=0.0f, miny=0.0f, maxy=0.0f;
1250                 int foundvert= 0;
1251                 
1252                 if(stride_axis==0) stride_axis= AC_LOC_X;
1253                 else if(stride_axis==1) stride_axis= AC_LOC_Y;
1254                 else stride_axis= AC_LOC_Z;
1255                 
1256                 /* calculate the min/max */
1257                 for (icu=achan->ipo->curve.first; icu; icu=icu->next) {
1258                         if(icu->adrcode==stride_axis) {
1259                                 if(icu->totvert>1) {
1260                                         foundvert= 1;
1261                                         minx= icu->bezt[0].vec[1][0];
1262                                         maxx= icu->bezt[icu->totvert-1].vec[1][0];
1263                                         
1264                                         miny= icu->bezt[0].vec[1][1];
1265                                         maxy= icu->bezt[icu->totvert-1].vec[1][1];
1266                                 }
1267                                 break;
1268                         }
1269                 }
1270                 
1271                 if(foundvert && miny!=maxy) {
1272                         float stridelen= sizecorr*fabs(maxy-miny), striptime;
1273                         float actiondist, pdist, pdistNewNormalized, offs;
1274                         float vec1[4], vec2[4], dir[3];
1275                         
1276                         /* internal cycling, actoffs is in frames */
1277                         offs= stridelen*strip->actoffs/(maxx-minx);
1278                         
1279                         /* amount path moves object */
1280                         pdist = (float)fmod (pathdist+offs, stridelen);
1281                         striptime= pdist/stridelen;
1282                         
1283                         /* amount stride bone moves */
1284                         actiondist= sizecorr*eval_icu(icu, minx + striptime*(maxx-minx)) - miny;
1285                         
1286                         pdist = fabs(actiondist) - pdist;
1287                         pdistNewNormalized = (pathdist+pdist)/path->totdist;
1288                         
1289                         /* now we need to go pdist further (or less) on cu path */
1290                         where_on_path(ob, (pathdist)/path->totdist, vec1, dir); /* vec needs size 4 */
1291                         if (pdistNewNormalized <= 1) {
1292                                 // search for correction in positive path-direction
1293                                 where_on_path(ob, pdistNewNormalized, vec2, dir);       /* vec needs size 4 */
1294                                 VecSubf(stride_offset, vec2, vec1);
1295                         }
1296                         else {
1297                                 // we reached the end of the path, search backwards instead
1298                                 where_on_path(ob, (pathdist-pdist)/path->totdist, vec2, dir);   /* vec needs size 4 */
1299                                 VecSubf(stride_offset, vec1, vec2);
1300                         }
1301                         Mat4Mul3Vecfl(ob->obmat, stride_offset);
1302                         return striptime;
1303                 }
1304         }
1305         return 0.0f;
1306 }
1307
1308 static void cyclic_offs_bone(Object *ob, bPose *pose, bActionStrip *strip, float time)
1309 {
1310         /* only called when strip has cyclic, so >= 1.0f works... */
1311         if(time >= 1.0f) {
1312                 bActionChannel *achan= get_action_channel(strip->act, strip->offs_bone);
1313
1314                 if(achan && achan->ipo) {
1315                         IpoCurve *icu= NULL;
1316                         Bone *bone;
1317                         float min[3]={0.0f, 0.0f, 0.0f}, max[3]={0.0f, 0.0f, 0.0f};
1318                         int index=0, foundvert= 0;
1319                         
1320                         /* calculate the min/max */
1321                         for (icu=achan->ipo->curve.first; icu; icu=icu->next) {
1322                                 if(icu->totvert>1) {
1323                                         
1324                                         if(icu->adrcode==AC_LOC_X)
1325                                                 index= 0;
1326                                         else if(icu->adrcode==AC_LOC_Y)
1327                                                 index= 1;
1328                                         else if(icu->adrcode==AC_LOC_Z)
1329                                                 index= 2;
1330                                         else
1331                                                 continue;
1332                                 
1333                                         foundvert= 1;
1334                                         min[index]= icu->bezt[0].vec[1][1];
1335                                         max[index]= icu->bezt[icu->totvert-1].vec[1][1];
1336                                 }
1337                         }
1338                         if(foundvert) {
1339                                 /* bring it into armature space */
1340                                 VecSubf(min, max, min);
1341                                 bone= get_named_bone(ob->data, strip->offs_bone);       /* weak */
1342                                 if(bone) {
1343                                         Mat4Mul3Vecfl(bone->arm_mat, min);
1344                                         
1345                                         /* dominant motion, cyclic_offset was cleared in rest_pose */
1346                                         if (strip->flag & (ACTSTRIP_CYCLIC_USEX | ACTSTRIP_CYCLIC_USEY | ACTSTRIP_CYCLIC_USEZ)) {
1347                                                 if (strip->flag & ACTSTRIP_CYCLIC_USEX) pose->cyclic_offset[0]= time*min[0];
1348                                                 if (strip->flag & ACTSTRIP_CYCLIC_USEY) pose->cyclic_offset[1]= time*min[1];
1349                                                 if (strip->flag & ACTSTRIP_CYCLIC_USEZ) pose->cyclic_offset[2]= time*min[2];
1350                                         } else {
1351                                                 if( fabs(min[0]) >= fabs(min[1]) && fabs(min[0]) >= fabs(min[2]))
1352                                                         pose->cyclic_offset[0]= time*min[0];
1353                                                 else if( fabs(min[1]) >= fabs(min[0]) && fabs(min[1]) >= fabs(min[2]))
1354                                                         pose->cyclic_offset[1]= time*min[1];
1355                                                 else
1356                                                         pose->cyclic_offset[2]= time*min[2];
1357                                         }
1358                                 }
1359                         }
1360                 }
1361         }
1362 }
1363
1364 /* simple case for now; only the curve path with constraint value > 0.5 */
1365 /* blending we might do later... */
1366 static Object *get_parent_path(Object *ob)
1367 {
1368         bConstraint *con;
1369         
1370         if(ob->parent && ob->parent->type==OB_CURVE)
1371                 return ob->parent;
1372         
1373         for (con = ob->constraints.first; con; con=con->next) {
1374                 if(con->type==CONSTRAINT_TYPE_FOLLOWPATH) {
1375                         if(con->enforce>0.5f) {
1376                                 bFollowPathConstraint *data= con->data;
1377                                 return data->tar;
1378                         }
1379                 }
1380         }
1381         return NULL;
1382 }
1383
1384 /* ************** do the action ************ */
1385
1386 /* ----- nla, etc. --------- */
1387
1388 static void do_nla(Scene *scene, Object *ob, int blocktype)
1389 {
1390         bPose *tpose= NULL;
1391         Key *key= NULL;
1392         ListBase tchanbase={NULL, NULL}, chanbase={NULL, NULL};
1393         bActionStrip *strip, *striplast=NULL, *stripfirst=NULL;
1394         float striptime, frametime, length, actlength;
1395         float blendfac, stripframe;
1396         float scene_cfra= frame_to_float(scene, scene->r.cfra); 
1397         int     doit, dostride;
1398         
1399         if(blocktype==ID_AR) {
1400                 copy_pose(&tpose, ob->pose, 1);
1401                 rest_pose(ob->pose);            // potentially destroying current not-keyed pose
1402         }
1403         else {
1404                 key= ob_get_key(ob);
1405         }
1406         
1407         /* check on extend to left or right, when no strip is hit by 'cfra' */
1408         for (strip=ob->nlastrips.first; strip; strip=strip->next) {
1409                 /* escape loop on a hit */
1410                 if( scene_cfra >= strip->start && scene_cfra <= strip->end + 0.1f)      /* note 0.1 comes back below */
1411                         break;
1412                 if(scene_cfra < strip->start) {
1413                         if(stripfirst==NULL)
1414                                 stripfirst= strip;
1415                         else if(stripfirst->start > strip->start)
1416                                 stripfirst= strip;
1417                 }
1418                 else if(scene_cfra > strip->end) {
1419                         if(striplast==NULL)
1420                                 striplast= strip;
1421                         else if(striplast->end < strip->end)
1422                                 striplast= strip;
1423                 }
1424         }
1425         if(strip==NULL) {       /* extend */
1426                 if(striplast)
1427                         scene_cfra= striplast->end;
1428                 else if(stripfirst)
1429                         scene_cfra= stripfirst->start;
1430         }
1431         
1432         /* and now go over all strips */
1433         for (strip=ob->nlastrips.first; strip; strip=strip->next){
1434                 doit=dostride= 0;
1435                 
1436                 if (strip->act && !(strip->flag & ACTSTRIP_MUTE)) {     /* so theres an action */
1437                         
1438                         /* Determine if the current frame is within the strip's range */
1439                         length = strip->end-strip->start;
1440                         actlength = strip->actend-strip->actstart;
1441                         striptime = (scene_cfra-(strip->start)) / length;
1442                         stripframe = (scene_cfra-(strip->start)) ;
1443
1444                         if (striptime>=0.0){
1445                                 
1446                                 if(blocktype==ID_AR) 
1447                                         rest_pose(tpose);
1448                                 
1449                                 /* To handle repeat, we add 0.1 frame extra to make sure the last frame is included */
1450                                 if (striptime < 1.0f + 0.1f/length) {
1451                                         
1452                                         /* Handle path */
1453                                         if ((strip->flag & ACTSTRIP_USESTRIDE) && (blocktype==ID_AR) && (ob->ipoflag & OB_DISABLE_PATH)==0){
1454                                                 Object *parent= get_parent_path(ob);
1455                                                 
1456                                                 if (parent) {
1457                                                         Curve *cu = parent->data;
1458                                                         float ctime, pdist;
1459                                                         
1460                                                         if (cu->flag & CU_PATH){
1461                                                                 /* Ensure we have a valid path */
1462                                                                 if(cu->path==NULL || cu->path->data==NULL) makeDispListCurveTypes(scene, parent, 0);
1463                                                                 if(cu->path) {
1464                                                                         
1465                                                                         /* Find the position on the path */
1466                                                                         ctime= bsystem_time(scene, ob, scene_cfra, 0.0);
1467                                                                         
1468                                                                         if(calc_ipo_spec(cu->ipo, CU_SPEED, &ctime)==0) {
1469                                                                                 /* correct for actions not starting on zero */
1470                                                                                 ctime= (ctime - strip->actstart)/cu->pathlen;
1471                                                                                 CLAMP(ctime, 0.0, 1.0);
1472                                                                         }
1473                                                                         pdist = ctime*cu->path->totdist;
1474                                                                         
1475                                                                         if(tpose && strip->stridechannel[0]) {
1476                                                                                 striptime= stridechannel_frame(parent, ob->size[0], strip, cu->path, pdist, tpose->stride_offset);
1477                                                                         }                                                                       
1478                                                                         else {
1479                                                                                 if (strip->stridelen) {
1480                                                                                         striptime = pdist / strip->stridelen;
1481                                                                                         striptime = (float)fmod (striptime+strip->actoffs, 1.0);
1482                                                                                 }
1483                                                                                 else
1484                                                                                         striptime = 0;
1485                                                                         }
1486                                                                         
1487                                                                         frametime = (striptime * actlength) + strip->actstart;
1488                                                                         frametime= bsystem_time(scene, ob, frametime, 0.0);
1489                                                                         
1490                                                                         if(blocktype==ID_AR) {
1491                                                                                 extract_pose_from_action (tpose, strip->act, frametime);
1492                                                                         }
1493                                                                         else if(blocktype==ID_OB) {
1494                                                                                 extract_ipochannels_from_action(&tchanbase, &ob->id, strip->act, "Object", frametime);
1495                                                                                 if(key)
1496                                                                                         extract_ipochannels_from_action(&tchanbase, &key->id, strip->act, "Shape", frametime);
1497                                                                         }
1498                                                                         doit=dostride= 1;
1499                                                                 }
1500                                                         }
1501                                                 }
1502                                         }
1503                                         /* To handle repeat, we add 0.1 frame extra to make sure the last frame is included */
1504                                         else  {
1505                                                 
1506                                                 /* Mod to repeat */
1507                                                 if(strip->repeat!=1.0f) {
1508                                                         float cycle= striptime*strip->repeat;
1509                                                         
1510                                                         striptime = (float)fmod (cycle, 1.0f + 0.1f/length);
1511                                                         cycle-= striptime;
1512                                                         
1513                                                         if(blocktype==ID_AR)
1514                                                                 cyclic_offs_bone(ob, tpose, strip, cycle);
1515                                                 }
1516
1517                                                 frametime = (striptime * actlength) + strip->actstart;
1518                                                 frametime= nla_time(scene, frametime, (float)strip->repeat);
1519                                                         
1520                                                 if(blocktype==ID_AR) {
1521                                                         extract_pose_from_action (tpose, strip->act, frametime);
1522                                                 }
1523                                                 else if(blocktype==ID_OB) {
1524                                                         extract_ipochannels_from_action(&tchanbase, &ob->id, strip->act, "Object", frametime);
1525                                                         if(key)
1526                                                                 extract_ipochannels_from_action(&tchanbase, &key->id, strip->act, "Shape", frametime);
1527                                                 }
1528                                                 
1529                                                 doit=1;
1530                                         }
1531                                 }
1532                                 /* Handle extend */
1533                                 else {
1534                                         if (strip->flag & ACTSTRIP_HOLDLASTFRAME){
1535                                                 /* we want the strip to hold on the exact fraction of the repeat value */
1536                                                 
1537                                                 frametime = actlength * (strip->repeat-(int)strip->repeat);
1538                                                 if(frametime<=0.000001f) frametime= actlength;  /* rounding errors... */
1539                                                 frametime= bsystem_time(scene, ob, frametime+strip->actstart, 0.0);
1540                                                 
1541                                                 if(blocktype==ID_AR)
1542                                                         extract_pose_from_action (tpose, strip->act, frametime);
1543                                                 else if(blocktype==ID_OB) {
1544                                                         extract_ipochannels_from_action(&tchanbase, &ob->id, strip->act, "Object", frametime);
1545                                                         if(key)
1546                                                                 extract_ipochannels_from_action(&tchanbase, &key->id, strip->act, "Shape", frametime);
1547                                                 }
1548                                                 
1549                                                 /* handle cycle hold */
1550                                                 if(strip->repeat!=1.0f) {
1551                                                         if(blocktype==ID_AR)
1552                                                                 cyclic_offs_bone(ob, tpose, strip, strip->repeat-1.0f);
1553                                                 }
1554                                                 
1555                                                 doit=1;
1556                                         }
1557                                 }
1558                                 
1559                                 /* Handle blendin & blendout */
1560                                 if (doit){
1561                                         /* Handle blendin */
1562                                         
1563                                         if (strip->blendin>0.0 && stripframe<=strip->blendin && scene_cfra>=strip->start){
1564                                                 blendfac = stripframe/strip->blendin;
1565                                         }
1566                                         else if (strip->blendout>0.0 && stripframe>=(length-strip->blendout) && scene_cfra<=strip->end){
1567                                                 blendfac = (length-stripframe)/(strip->blendout);
1568                                         }
1569                                         else
1570                                                 blendfac = 1;
1571                                         
1572                                         if(blocktype==ID_AR) {/* Blend this pose with the accumulated pose */
1573                                                 /* offset bone, for matching cycles */
1574                                                 blend_pose_offset_bone (strip, ob->pose, tpose, blendfac, strip->mode);
1575                                                 
1576                                                 blend_poses (ob->pose, tpose, blendfac, strip->mode);
1577                                                 if(dostride)
1578                                                         blend_pose_strides (ob->pose, tpose, blendfac, strip->mode);
1579                                         }
1580                                         else {
1581                                                 blend_ipochannels(&chanbase, &tchanbase, blendfac, strip->mode);
1582                                                 BLI_freelistN(&tchanbase);
1583                                         }
1584                                 }
1585                         }                                       
1586                 }
1587         }
1588         
1589         if(blocktype==ID_OB) {
1590                 execute_ipochannels(&chanbase);
1591         }
1592         else if(blocktype==ID_AR) {
1593                 /* apply stride offset to object */
1594                 VecAddf(ob->obmat[3], ob->obmat[3], ob->pose->stride_offset);
1595         }
1596         
1597         /* free */
1598         if (tpose)
1599                 free_pose(tpose);
1600         if(chanbase.first)
1601                 BLI_freelistN(&chanbase);
1602 }
1603
1604 #endif // XXX OLD ANIMATION SYSTEM (TO BE REMOVED)