Cleaned up the old call to do_all_actions(). It only is supposed to do
[blender.git] / source / blender / blenkernel / intern / action.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * Contributor(s): Full recode, Ton Roosendaal, Crete 2005
27  *
28  * ***** END GPL/BL DUAL LICENSE BLOCK *****
29  */
30
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
34
35 #include <string.h>
36 #include <math.h>
37 #include <stdlib.h>     /* for NULL */
38
39 #include "MEM_guardedalloc.h"
40
41 #include "DNA_action_types.h"
42 #include "DNA_armature_types.h"
43 #include "DNA_constraint_types.h"
44 #include "DNA_curve_types.h"
45 #include "DNA_ipo_types.h"
46 #include "DNA_nla_types.h"
47 #include "DNA_object_types.h"
48 #include "DNA_scene_types.h"
49
50 #include "BKE_action.h"
51 #include "BKE_anim.h"
52 #include "BKE_armature.h"
53 #include "BKE_blender.h"
54 #include "BKE_constraint.h"
55 #include "BKE_global.h"
56 #include "BKE_ipo.h"
57 #include "BKE_library.h"
58 #include "BKE_main.h"
59 #include "BKE_object.h"
60 #include "BKE_utildefines.h"
61
62 #include "BLI_arithb.h"
63 #include "BLI_blenlib.h"
64
65 #include "nla.h"
66
67 /* *********************** NOTE ON POSE AND ACTION **********************
68
69   - Pose is the local (object level) component of armature. The current
70     object pose is saved in files, and (will be) is presorted for dependency
71   - Actions have fewer (or other) channels, and write data to a Pose
72   - Currently ob->pose data is controlled in where_is_pose only. The (recalc)
73     event system takes care of calling that
74   - The NLA system (here too) uses Poses as interpolation format for Actions
75   - Therefore we assume poses to be static, and duplicates of poses have channels in
76     same order, for quick interpolation reasons
77
78   ****************************** (ton) ************************************ */
79
80 /* ***************** Library data level operations on action ************** */
81
82 void make_local_action(bAction *act)
83 {
84         Object *ob;
85         bAction *actn;
86         int local=0, lib=0;
87         
88         if(act->id.lib==0) return;
89         if(act->id.us==1) {
90                 act->id.lib= 0;
91                 act->id.flag= LIB_LOCAL;
92                 new_id(0, (ID *)act, 0);
93                 return;
94         }
95         
96         ob= G.main->object.first;
97         while(ob) {
98                 if(ob->action==act) {
99                         if(ob->id.lib) lib= 1;
100                         else local= 1;
101                 }
102                 ob= ob->id.next;
103         }
104         
105         if(local && lib==0) {
106                 act->id.lib= 0;
107                 act->id.flag= LIB_LOCAL;
108                 new_id(0, (ID *)act, 0);
109         }
110         else if(local && lib) {
111                 actn= copy_action(act);
112                 actn->id.us= 0;
113                 
114                 ob= G.main->object.first;
115                 while(ob) {
116                         if(ob->action==act) {
117                                 
118                                 if(ob->id.lib==0) {
119                                         ob->action = actn;
120                                         ob->activecon = NULL;
121                                         actn->id.us++;
122                                         act->id.us--;
123                                 }
124                         }
125                         ob= ob->id.next;
126                 }
127         }
128 }
129
130
131 void free_action(bAction *act)
132 {
133         bActionChannel *chan;
134         
135         /* Free channels */
136         for (chan=act->chanbase.first; chan; chan=chan->next){
137                 if (chan->ipo)
138                         chan->ipo->id.us--;
139                 free_constraint_channels(&chan->constraintChannels);
140         }
141         
142         if (act->chanbase.first)
143                 BLI_freelistN (&act->chanbase);
144 }
145
146 bAction* copy_action(bAction *src)
147 {
148         bAction *dst = NULL;
149         bActionChannel *dchan, *schan;
150         
151         if(!src) return NULL;
152         
153         dst= copy_libblock(src);
154         duplicatelist(&(dst->chanbase), &(src->chanbase));
155         
156         for (dchan=dst->chanbase.first, schan=src->chanbase.first; dchan; dchan=dchan->next, schan=schan->next){
157                 dchan->ipo = copy_ipo(dchan->ipo);
158                 copy_constraint_channels(&dchan->constraintChannels, &schan->constraintChannels);
159         }
160         dst->id.flag |= LIB_FAKEUSER;
161         dst->id.us++;
162         return dst;
163 }
164
165
166
167 /* ************************ Pose channels *************** */
168
169 /* usually used within a loop, so we got a N^2 slowdown */
170 bPoseChannel *get_pose_channel(const bPose *pose, const char *name)
171 {
172         bPoseChannel *chan;
173
174         if(pose==NULL) return NULL;
175         
176         for (chan=pose->chanbase.first; chan; chan=chan->next) {
177                 if(chan->name[0] == name[0])
178                         if (!strcmp (chan->name, name))
179                                 return chan;
180         }
181
182         return NULL;
183 }
184
185 /* Use with care, not on Armature poses but for temporal ones */
186 /* (currently used for action constraints and in rebuild_pose) */
187 bPoseChannel *verify_pose_channel(bPose* pose, const char* name)
188 {
189         bPoseChannel *chan;
190         
191         if (!pose){
192                 return NULL;
193         }
194         
195         /*      See if this channel exists */
196         for (chan=pose->chanbase.first; chan; chan=chan->next){
197                 if (!strcmp (name, chan->name))
198                         return chan;
199         }
200         
201         /* If not, create it and add it */
202         chan = MEM_callocN(sizeof(bPoseChannel), "verifyPoseChannel");
203         
204         strcpy (chan->name, name);
205         /* init vars to prevent mat errors */
206         chan->quat[0] = 1.0F;
207         chan->size[0] = chan->size[1] = chan->size[2] = 1.0F;
208         Mat3One(chan->ik_mat);
209         
210         BLI_addtail (&pose->chanbase, chan);
211         
212         return chan;
213 }
214
215
216 /* dst should be freed already, makes entire duplicate */
217 void copy_pose(bPose **dst, bPose *src, int copycon)
218 {
219         bPose *outPose;
220         bPoseChannel    *pchan;
221         ListBase listb;
222         
223         if (!src){
224                 *dst=NULL;
225                 return;
226         }
227         
228         outPose= MEM_callocN(sizeof(bPose), "pose");
229         
230         duplicatelist (&outPose->chanbase, &src->chanbase);
231         
232         if (copycon) {
233                 for (pchan=outPose->chanbase.first; pchan; pchan=pchan->next) {
234                         copy_constraints(&listb, &pchan->constraints);  // copy_constraints NULLs listb
235                         pchan->constraints= listb;
236                 }
237         }
238         
239         *dst=outPose;
240 }
241
242 void free_pose_channels(bPose *pose) 
243 {
244         bPoseChannel *chan;
245         
246         if (pose->chanbase.first){
247                 for (chan = pose->chanbase.first; chan; chan=chan->next){
248                         free_constraints(&chan->constraints);
249                 }
250                 BLI_freelistN (&pose->chanbase);
251         }
252 }
253
254 static void copy_pose_channel_data(bPoseChannel *pchan, const bPoseChannel *chan)
255 {
256         bConstraint *pcon, *con;
257         
258         VECCOPY(pchan->loc, chan->loc);
259         VECCOPY(pchan->size, chan->size);
260         QUATCOPY(pchan->quat, chan->quat);
261         pchan->flag= chan->flag;
262         
263         con= chan->constraints.first;
264         for(pcon= pchan->constraints.first; pcon; pcon= pcon->next)
265                 pcon->enforce= con->enforce;
266 }
267
268 /* checks for IK constraint, can do more constraints flags later */
269 /* pose should be entirely OK */
270 void update_pose_constraint_flags(bPose *pose)
271 {
272         bPoseChannel *pchan;
273         bConstraint *con;
274         
275         /* clear */
276         for (pchan = pose->chanbase.first; pchan; pchan=pchan->next) {
277                 pchan->constflag= 0;
278         }
279         /* detect */
280         for (pchan = pose->chanbase.first; pchan; pchan=pchan->next) {
281                 for(con= pchan->constraints.first; con; con= con->next) {
282                         if(con->type==CONSTRAINT_TYPE_KINEMATIC) {
283                                 pchan->constflag |= PCHAN_HAS_IK;
284                         }
285                         else pchan->constflag |= PCHAN_HAS_CONST;
286                 }
287         }
288 }
289
290
291 /* ************************ END Pose channels *************** */
292
293 bActionChannel *get_named_actionchannel(bAction *act, const char *name)
294 {
295         bActionChannel *chan;
296         
297         if (!act)
298                 return NULL;
299         
300         for (chan = act->chanbase.first; chan; chan=chan->next){
301                 if (!strcmp (chan->name, name))
302                         return chan;
303         }
304         
305         return NULL;
306 }
307
308 /* ************************ Blending with NLA *************** */
309
310
311 /* Only allowed for Poses with identical channels */
312 void blend_poses(bPose *dst, const bPose *src, float srcweight, short mode)
313 {
314         bPoseChannel *dchan;
315         const bPoseChannel *schan;
316         bConstraint *dcon, *scon;
317         float   dquat[4], squat[4];
318         float dstweight;
319         int i;
320         
321         switch (mode){
322         case POSE_BLEND:
323                 dstweight = 1.0F - srcweight;
324                 break;
325         case POSE_ADD:
326                 dstweight = 1.0F;
327                 break;
328         default :
329                 dstweight = 1.0F;
330         }
331         
332         schan= src->chanbase.first;
333         for (dchan = dst->chanbase.first; dchan; dchan=dchan->next, schan= schan->next){
334                 if (schan->flag & (POSE_ROT|POSE_LOC|POSE_SIZE)) {
335                         
336                         /* replaced quat->matrix->quat conversion with decent quaternion interpol (ton) */
337                         
338                         /* Do the transformation blend */
339                         if (schan->flag & POSE_ROT) {
340                                 QUATCOPY(dquat, dchan->quat);
341                                 QUATCOPY(squat, schan->quat);
342                                 if(mode==POSE_BLEND)
343                                         QuatInterpol(dchan->quat, dquat, squat, srcweight);
344                                 else
345                                         QuatAdd(dchan->quat, dquat, squat, srcweight);
346                                 
347                                 NormalQuat (dchan->quat);
348                         }
349
350                         for (i=0; i<3; i++){
351                                 if (schan->flag & POSE_LOC)
352                                         dchan->loc[i] = (dchan->loc[i]*dstweight) + (schan->loc[i]*srcweight);
353                                 if (schan->flag & POSE_SIZE)
354                                         dchan->size[i] = 1.0f + ((dchan->size[i]-1.0f)*dstweight) + ((schan->size[i]-1.0f)*srcweight);
355                         }
356                         dchan->flag |= schan->flag;
357                 }
358                 for(dcon= dchan->constraints.first, scon= schan->constraints.first; dcon && scon; dcon= dcon->next, scon= scon->next) {
359                         dcon->enforce= dcon->enforce*dstweight + scon->enforce*srcweight;
360                 }
361         }
362 }
363
364
365 float calc_action_start(const bAction *act)
366 {
367         const bActionChannel *chan;
368         const IpoCurve  *icu;
369         float size=999999999.0f;
370         int     i;
371         int     foundvert=0;
372         const bConstraintChannel *conchan;
373
374
375         if (!act)
376                 return 0;
377
378         for (chan=act->chanbase.first; chan; chan=chan->next){
379                 for (icu=chan->ipo->curve.first; icu; icu=icu->next)
380                         for (i=0; i<icu->totvert; i++){
381                                 size = MIN2 (size, icu->bezt[i].vec[1][0]);
382                                 foundvert=1;
383                                 
384                         }
385                         for (conchan=chan->constraintChannels.first; conchan; conchan=conchan->next){
386                                 for (icu=conchan->ipo->curve.first; icu; icu=icu->next)
387                                         for (i=0; i<icu->totvert; i++){
388                                                 size = MIN2 (size, icu->bezt[i].vec[1][0]);
389                                                 foundvert=1;
390                                         }
391                         }
392         }
393         
394         if (!foundvert)
395                 return 0;
396         else
397                 return size;
398 }
399
400 float calc_action_end(const bAction *act)
401 {
402         const bActionChannel    *chan;
403         const bConstraintChannel *conchan;
404         const IpoCurve          *icu;
405         float size=0;
406         int     i;
407
408         if (!act)
409                 return 0;
410
411         for (chan=act->chanbase.first; chan; chan=chan->next){
412                 for (icu=chan->ipo->curve.first; icu; icu=icu->next)
413                         for (i=0; i<icu->totvert; i++)
414                                 size = MAX2 (size, icu->bezt[i].vec[1][0]);
415                         
416                         for (conchan=chan->constraintChannels.first; conchan; conchan=conchan->next){
417                                 for (icu=conchan->ipo->curve.first; icu; icu=icu->next)
418                                         for (i=0; i<icu->totvert; i++)
419                                                 size = MAX2 (size, icu->bezt[i].vec[1][0]);
420                         }
421         }
422         return size;
423 }
424
425 /* Copy the data from the action-pose (src) into the pose */
426 /* both args are assumed to be valid */
427 /* exported to game engine */
428 void extract_pose_from_pose(bPose *pose, const bPose *src)
429 {
430         const bPoseChannel *schan;
431         bPoseChannel *pchan= pose->chanbase.first;
432
433         for (schan=src->chanbase.first; schan; schan=schan->next, pchan= pchan->next) {
434                 copy_pose_channel_data(pchan, schan);
435         }
436 }
437
438 /* Pose should exist, can have any number of channels too (used for constraint) */
439 void extract_pose_from_action(bPose *pose, bAction *act, float ctime) 
440 {
441         bActionChannel *achan;
442         bPoseChannel    *pchan;
443         Ipo                             *ipo;
444
445         if (!act)
446                 return;
447         if (!pose)
448                 return;
449
450         /* Copy the data from the action into the pose */
451         for (achan=act->chanbase.first; achan; achan=achan->next){
452                 act->achan= achan;  // for ipos
453
454                 ipo = achan->ipo;
455                 if (ipo) {
456                         pchan= get_pose_channel(pose, achan->name);
457                         if(pchan) {
458                                 act->pchan= pchan;  // for ipos
459                                 
460                                 /* Evaluates and sets the internal ipo value */
461                                 calc_ipo(ipo, ctime);
462                                 /* This call also sets the pchan flags */
463                                 execute_ipo((ID*)act, achan->ipo);
464                                 
465                                 do_constraint_channels(&pchan->constraints, &achan->constraintChannels, ctime);
466                         }
467                 }
468         }
469 }
470
471 /* for do_all_actions, clears the pose */
472 static void rest_pose(bPose *pose, int clearflag)
473 {
474         bPoseChannel *chan;
475         int i;
476         
477         if (!pose)
478                 return;
479         
480         for (chan=pose->chanbase.first; chan; chan=chan->next){
481                 for (i=0; i<3; i++){
482                         chan->loc[i]=0.0;
483                         chan->quat[i+1]=0.0;
484                         chan->size[i]=1.0;
485                 }
486                 chan->quat[0]=1.0;
487                 
488                 if (clearflag)
489                         chan->flag =0;
490         }
491 }
492
493 void do_all_actions(Object *ob)
494 {
495         bPose *tpose=NULL;
496         bActionStrip *strip;
497         int     doit;
498         float striptime, frametime, length, actlength;
499         float blendfac, stripframe;
500
501         if(ob==NULL) return;    // only to have safe calls from editor
502         
503         /* Retrieve data from the NLA */
504         if(ob->type==OB_ARMATURE && ob->pose) {
505                 bArmature *arm= ob->data;
506
507                 if(arm->flag & ARM_NO_ACTION) {  // no action set while transform
508                         ;
509                 }
510                 else if(ob->action) {
511                         /* Do local action (always overrides the nla actions) */
512                         extract_pose_from_action (ob->pose, ob->action, bsystem_time(ob, 0, (float) G.scene->r.cfra, 0.0));
513                 }
514                 else {
515                         doit=0;
516
517                         copy_pose(&tpose, ob->pose, 1);
518                         rest_pose(ob->pose, 1);
519  
520                         for (strip=ob->nlastrips.first; strip; strip=strip->next){
521                                 doit = 0;
522                                 if (strip->act){
523                         
524                                         /* Determine if the current frame is within the strip's range */
525                                         length = strip->end-strip->start;
526                                         actlength = strip->actend-strip->actstart;
527                                         striptime = (G.scene->r.cfra-(strip->start)) / length;
528                                         stripframe = (G.scene->r.cfra-(strip->start)) ;
529
530
531                                         if (striptime>=0.0){
532                                                 
533                                                 rest_pose(tpose, 1);
534
535                                                 /* Handle path */
536                                                 if (strip->flag & ACTSTRIP_USESTRIDE){
537                                                         if (ob->parent && ob->parent->type==OB_CURVE){
538                                                                 Curve *cu = ob->parent->data;
539                                                                 float ctime, pdist;
540
541                                                                 if (cu->flag & CU_PATH){
542                                                                         /* Ensure we have a valid path */
543                                                                         if(cu->path==NULL || cu->path->data==NULL) printf("action path error\n");
544                                                                         else {
545
546                                                                                 /* Find the position on the path */
547                                                                                 ctime= bsystem_time(ob, ob->parent, (float)G.scene->r.cfra, 0.0);
548                                                                                 
549                                                                                 if(calc_ipo_spec(cu->ipo, CU_SPEED, &ctime)==0) {
550                                                                                         ctime /= cu->pathlen;
551                                                                                         CLAMP(ctime, 0.0, 1.0);
552                                                                                 }
553                                                                                 pdist = ctime*cu->path->totdist;
554                                                                                 
555                                                                                 if (strip->stridelen)
556                                                                                         striptime = pdist / strip->stridelen;
557                                                                                 else
558                                                                                         striptime = 0;
559                                                                                 
560                                                                                 striptime = (float)fmod (striptime, 1.0);
561                                                                                 
562                                                                                 frametime = (striptime * actlength) + strip->actstart;
563                                                                                 extract_pose_from_action (tpose, strip->act, bsystem_time(ob, 0, frametime, 0.0));
564                                                                                 doit=1;
565                                                                         }
566                                                                 }
567                                                         }
568                                                 }
569
570                                                 /* Handle repeat */
571                 
572                                                 else if (striptime < 1.0){
573                                                         /* Mod to repeat */
574                                                         striptime*=strip->repeat;
575                                                         striptime = (float)fmod (striptime, 1.0);
576                                                         
577                                                         frametime = (striptime * actlength) + strip->actstart;
578                                                         extract_pose_from_action (tpose, strip->act, bsystem_time(ob, 0, frametime, 0.0));
579                                                         doit=1;
580                                                 }
581                                                 /* Handle extend */
582                                                 else{
583                                                         if (strip->flag & ACTSTRIP_HOLDLASTFRAME){
584                                                                 striptime = 1.0;
585                                                                 frametime = (striptime * actlength) + strip->actstart;
586                                                                 extract_pose_from_action (tpose, strip->act, bsystem_time(ob, 0, frametime, 0.0));
587                                                                 doit=1;
588                                                         }
589                                                 }
590
591                                                 /* Handle blendin & blendout */
592                                                 if (doit){
593                                                         /* Handle blendin */
594
595                                                         if (strip->blendin>0.0 && stripframe<=strip->blendin && G.scene->r.cfra>=strip->start){
596                                                                 blendfac = stripframe/strip->blendin;
597                                                         }
598                                                         else if (strip->blendout>0.0 && stripframe>=(length-strip->blendout) && G.scene->r.cfra<=strip->end){
599                                                                 blendfac = (length-stripframe)/(strip->blendout);
600                                                         }
601                                                         else
602                                                                 blendfac = 1;
603
604                                                         /* Blend this pose with the accumulated pose */
605                                                         blend_poses (ob->pose, tpose, blendfac, strip->mode);
606                                                 }
607                                         }                                       
608                                 }
609                         }
610                 }
611         }
612         
613         if (tpose){
614                 free_pose_channels(tpose);
615                 MEM_freeN(tpose);
616         }
617 }
618