4 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
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
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.
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.
23 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24 * All rights reserved.
26 * The Original Code is: all of this file.
28 * Contributor(s): none yet.
30 * ***** END GPL/BL DUAL LICENSE BLOCK *****
35 #include <stdlib.h> /* for NULL */
37 #include "MEM_guardedalloc.h"
38 #include "BLI_arithb.h"
39 #include "BLI_blenlib.h"
41 #include "BKE_action.h"
42 #include "BKE_global.h"
44 #include "BKE_utildefines.h"
46 #include "DNA_object_types.h"
47 #include "DNA_ipo_types.h"
48 #include "DNA_curve_types.h"
49 #include "DNA_scene_types.h"
50 #include "DNA_action_types.h"
51 #include "DNA_nla_types.h"
53 #include "BKE_blender.h"
55 #include "BKE_object.h"
56 #include "BKE_library.h"
58 #include "BKE_armature.h"
62 #include "BKE_constraint.h"
63 #include "DNA_constraint_types.h"
65 /* Local function prototypes */
68 do_pose_constraint_channels(
76 get_constraint_influence_from_pose (
106 for (chan=pose->chanbase.first; chan; chan=chan->next){
107 if (!strcmp (chan->name, name))
126 for (chan=pose->chanbase.first; chan; chan=chan->next){
147 const bConstraint *scon;
152 dstweight = 1.0F - srcweight;
159 /* Blend constraints */
160 for (dcon=dst->first; dcon; dcon=dcon->next){
161 for (scon = src->first; scon; scon=scon->next){
162 if (!strcmp(scon->name, dcon->name))
167 dcon->enforce = (dcon->enforce*dstweight) + (scon->enforce*srcweight);
168 if (mode == POSE_BLEND)
171 if (dcon->enforce>1.0)
173 if (dcon->enforce<0.0)
188 const bPoseChannel *schan;
189 float dquat[4], squat[4], mat[3][3];
195 dstweight = 1.0F - srcweight;
204 for (dchan = dst->chanbase.first; dchan; dchan=dchan->next){
205 schan = get_pose_channel(src, dchan->name);
207 if (schan->flag & (POSE_ROT|POSE_LOC|POSE_SIZE)){
209 /* Convert both quats to matrices and then back again.
210 * This prevents interpolation problems
211 * This sucks because it is slow and stupid
214 QuatToMat3(dchan->quat, mat);
215 Mat3ToQuat(mat, dquat);
216 QuatToMat3(schan->quat, mat);
217 Mat3ToQuat(mat, squat);
219 /* Do the transformation blend */
223 dchan->loc[i] = (dchan->loc[i]*dstweight) + (schan->loc[i]*srcweight);
225 dchan->size[i] = 1.0f + ((dchan->size[i]-1.0f)*dstweight) + ((schan->size[i]-1.0f)*srcweight);
227 dchan->quat[i+1] = (dquat[i+1]*dstweight) + (squat[i+1]*srcweight);
231 /* Do one more iteration for the quaternions only and normalize the quaternion if needed */
232 if (schan->flag & POSE_ROT)
233 dchan->quat[0] = 1.0f + ((dquat[0]-1.0f)*dstweight) + ((squat[0]-1.0f)*srcweight);
234 if (mode==POSE_BLEND)
235 NormalQuat (dchan->quat);
236 dchan->flag |= schan->flag;
243 clear_pose_constraint_status (
253 for (chan = ob->pose->chanbase.first; chan; chan=chan->next){
254 chan->flag &= ~PCHAN_DONE;
262 const bActionChannel *chan;
264 float size=999999999.0f;
267 const bConstraintChannel *conchan;
273 for (chan=act->chanbase.first; chan; chan=chan->next){
274 for (icu=chan->ipo->curve.first; icu; icu=icu->next)
275 for (i=0; i<icu->totvert; i++){
276 size = MIN2 (size, icu->bezt[i].vec[1][0]);
280 for (conchan=chan->constraintChannels.first; conchan; conchan=conchan->next){
281 for (icu=conchan->ipo->curve.first; icu; icu=icu->next)
282 for (i=0; i<icu->totvert; i++){
283 size = MIN2 (size, icu->bezt[i].vec[1][0]);
299 const bActionChannel *chan;
300 const bConstraintChannel *conchan;
308 for (chan=act->chanbase.first; chan; chan=chan->next){
309 for (icu=chan->ipo->curve.first; icu; icu=icu->next)
310 for (i=0; i<icu->totvert; i++)
311 size = MAX2 (size, icu->bezt[i].vec[1][0]);
313 for (conchan=chan->constraintChannels.first; conchan; conchan=conchan->next){
314 for (icu=conchan->ipo->curve.first; icu; icu=icu->next)
315 for (i=0; i<icu->totvert; i++)
316 size = MAX2 (size, icu->bezt[i].vec[1][0]);
323 verify_pose_channel (
333 /* See if this channel exists */
334 for (chan=pose->chanbase.first; chan; chan=chan->next){
335 if (!strcmp (name, chan->name))
339 /* If not, create it and add it */
340 chan = MEM_callocN(sizeof(bPoseChannel), "verifyPoseChannel");
342 strcpy (chan->name, name);
343 chan->loc[0] = chan->loc[1] = chan->loc[2] = 0.0F;
344 chan->quat[1] = chan->quat[2] = chan->quat[3] = 0.0F; chan->quat[0] = 1.0F;
345 chan->size[0] = chan->size[1] = chan->size[2] = 1.0F;
347 chan->flag |= POSE_ROT|POSE_SIZE|POSE_LOC;
349 BLI_addtail (&pose->chanbase, chan);
357 const bPoseChannel *pchan;
358 bPoseChannel *newchan;
365 /* If there is no pose, create one */
367 *pose=MEM_callocN (sizeof(bPose), "pose");
370 /* Copy the data from the action into the pose */
371 for (pchan=src->chanbase.first; pchan; pchan=pchan->next){
372 newchan = copy_pose_channel(pchan);
373 verify_pose_channel(*pose, pchan->name);
374 set_pose_channel(*pose, newchan);
378 static void get_constraint_influence_from_pose (bPose *dst, bPose *src)
380 bConstraint *dcon, *scon;
385 for (dcon = dst->chanbase.first; dcon; dcon=dcon->next){
386 for (scon=src->chanbase.first; scon; scon=scon->next){
387 if (!strcmp(scon->name, dcon->name))
391 dcon->enforce = scon->enforce;
396 /* If the pose does not exist, a new one is created */
399 get_pose_from_action (
404 bActionChannel *achan;
415 /* If there is no pose, create one */
417 *pose=MEM_callocN (sizeof(bPose), "pose");
420 /* Copy the data from the action into the pose */
421 for (achan=act->chanbase.first; achan; achan=achan->next){
426 pchan=MEM_callocN (sizeof(bPoseChannel), "gpfa_poseChannel");
427 strcpy (pchan->name, achan->name);
430 /* Evaluates and sets the internal ipo value */
431 calc_ipo(ipo, ctime);
433 /* Set the pchan flags */
434 for (curve = achan->ipo->curve.first; curve; curve=curve->next){
435 /* Skip empty curves */
439 switch (curve->adrcode){
444 pchan->flag |= POSE_ROT;
449 pchan->flag |= POSE_LOC;
454 pchan->flag |= POSE_SIZE;
459 execute_ipo((ID*)act, achan->ipo);
461 set_pose_channel(*pose, pchan);
475 float striptime, frametime, length, actlength;
476 float blendfac, stripframe;
480 /* NEW: current scene ob ipo's */
481 base= G.scene->base.first;
488 /* Retrieve data from the NLA */
489 if(ob->type==OB_ARMATURE){
504 copy_pose(&apose, ob->pose, 1);
505 copy_pose(&tpose, ob->pose, 1);
508 if (base->object->nlastrips.first){
509 rest_pose(base->object->pose, 0);
512 for (strip=base->object->nlastrips.first; strip; strip=strip->next){
516 /* Determine if the current frame is within the strip's range */
517 length = strip->end-strip->start;
518 actlength = strip->actend-strip->actstart;
519 striptime = (G.scene->r.cfra-(strip->start)) / length;
520 stripframe = (G.scene->r.cfra-(strip->start)) ;
528 if (strip->flag & ACTSTRIP_USESTRIDE){
529 if (ob->parent && ob->parent->type==OB_CURVE){
530 Curve *cu = ob->parent->data;
533 if (cu->flag & CU_PATH){
534 /* Ensure we have a valid path */
535 if(cu->path==0 || cu->path->data==0) calc_curvepath(ob->parent);
537 /* Find the position on the path */
538 ctime= bsystem_time(ob, ob->parent, (float)G.scene->r.cfra, 0.0);
540 if(calc_ipo_spec(cu->ipo, CU_SPEED, &ctime)==0) {
541 ctime /= cu->pathlen;
542 CLAMP(ctime, 0.0, 1.0);
544 pdist = ctime*cu->path->totdist;
546 if (strip->stridelen)
547 striptime = pdist / strip->stridelen;
551 striptime = (float)fmod (striptime, 1.0);
553 frametime = (striptime * actlength) + strip->actstart;
554 get_pose_from_action (&tpose, strip->act, bsystem_time(ob, 0, frametime, 0.0));
555 #ifdef __NLA_BLENDCON
556 do_pose_constraint_channels(tpose, strip->act, bsystem_time(ob, 0, frametime, 0.0));
565 else if (striptime < 1.0){
567 striptime*=strip->repeat;
568 striptime = (float)fmod (striptime, 1.0);
570 frametime = (striptime * actlength) + strip->actstart;
571 get_pose_from_action (&tpose, strip->act, bsystem_time(ob, 0, frametime, 0.0));
572 #ifdef __NLA_BLENDCON
573 do_pose_constraint_channels(tpose, strip->act, bsystem_time(ob, 0, frametime, 0.0));
579 if (strip->flag & ACTSTRIP_HOLDLASTFRAME){
581 frametime = (striptime * actlength) + strip->actstart;
582 get_pose_from_action (&tpose, strip->act, bsystem_time(ob, 0, frametime, 0.0));
583 #ifdef __NLA_BLENDCON
584 do_pose_constraint_channels(tpose, strip->act, bsystem_time(ob, 0, frametime, 0.0));
590 /* Handle blendin & blendout */
594 if (strip->blendin>0.0 && stripframe<=strip->blendin && G.scene->r.cfra>=strip->start){
595 blendfac = stripframe/strip->blendin;
597 else if (strip->blendout>0.0 && stripframe>=(length-strip->blendout) && G.scene->r.cfra<=strip->end){
598 blendfac = (length-stripframe)/(strip->blendout);
603 /* Blend this pose with the accumulated pose */
604 blend_poses (apose, tpose, blendfac, strip->mode);
605 #ifdef __NLA_BLENDCON
606 blend_constraints(&apose->chanbase, &tpose->chanbase, blendfac, strip->mode);
611 get_pose_from_pose(&ob->pose, apose);
612 #ifdef __NLA_BLENDCON
613 get_constraint_influence_from_pose(ob->pose, apose);
620 /* Do local action (always overrides the nla actions) */
621 /* At the moment, only constraint ipos on the local action have any effect */
622 if(base->object->action) {
623 get_pose_from_action (&ob->pose, ob->action, bsystem_time(ob, 0, (float) G.scene->r.cfra, 0.0));
624 do_pose_constraint_channels(ob->pose, ob->action, bsystem_time(ob, 0, (float) G.scene->r.cfra, 0.0));
629 apply_pose_armature(get_armature(ob), ob->pose, 1);
633 if(base==0 && set==0 && G.scene->set) {
635 base= G.scene->set->base.first;
653 static void do_pose_constraint_channels(bPose *pose, bAction *act, float ctime)
656 bActionChannel *achan;
661 for (pchan=pose->chanbase.first; pchan; pchan=pchan->next){
662 achan=get_named_actionchannel(act, pchan->name);
664 do_constraint_channels(&pchan->constraints, &achan->constraintChannels, ctime);
669 get_named_actionchannel (
673 bActionChannel *chan;
678 for (chan = act->chanbase.first; chan; chan=chan->next){
679 if (!strcmp (chan->name, name))
692 if (pose->chanbase.first){
693 for (chan = pose->chanbase.first; chan; chan=chan->next){
694 free_constraints(&chan->constraints);
696 BLI_freelistN (&pose->chanbase);
708 if(act->id.lib==0) return;
711 act->id.flag= LIB_LOCAL;
712 new_id(0, (ID *)act, 0);
716 ob= G.main->object.first;
718 if(ob->action==act) {
719 if(ob->id.lib) lib= 1;
725 if(local && lib==0) {
727 act->id.flag= LIB_LOCAL;
728 new_id(0, (ID *)act, 0);
730 else if(local && lib) {
731 actn= copy_action(act);
734 ob= G.main->object.first;
736 if(ob->action==act) {
740 ob->activecon = NULL;
755 bActionChannel *chan;
758 for (chan=act->chanbase.first; chan; chan=chan->next){
761 free_constraint_channels(&chan->constraintChannels);
764 if (act->chanbase.first)
765 BLI_freelistN (&act->chanbase);
773 bActionChannel *dchan, *schan;
775 if(!src) return NULL;
777 dst= copy_libblock(src);
778 duplicatelist(&(dst->chanbase), &(src->chanbase));
780 for (dchan=dst->chanbase.first, schan=src->chanbase.first; dchan; dchan=dchan->next, schan=schan->next){
781 dchan->ipo = copy_ipo(dchan->ipo);
782 copy_constraint_channels(&dchan->constraintChannels, &schan->constraintChannels);
784 dst->id.flag |= LIB_FAKEUSER;
791 const bPoseChannel* src
798 dst = MEM_callocN (sizeof(bPoseChannel), "copyPoseChannel");
799 memcpy (dst, src, sizeof(bPoseChannel));
800 dst->prev=dst->next=NULL;
812 const bPose * inPose;
813 bPoseChannel *newChan;
814 const bPoseChannel *curChan;
823 outPose=MEM_callocN(sizeof(bPose), "pose");
825 for (curChan=inPose->chanbase.first; curChan; curChan=curChan->next){
826 newChan=MEM_callocN(sizeof(bPoseChannel), "copyposePoseChannel");
828 strcpy (newChan->name, curChan->name);
829 newChan->flag=curChan->flag;
831 memcpy (newChan->loc, curChan->loc, sizeof (curChan->loc));
832 memcpy (newChan->size, curChan->size, sizeof (curChan->size));
833 memcpy (newChan->quat, curChan->quat, sizeof (curChan->quat));
834 Mat4CpyMat4 (newChan->obmat, curChan->obmat);
836 BLI_addtail (&outPose->chanbase, newChan);
838 copy_constraints(&newChan->constraints, &curChan->constraints);
845 bPoseChannel *set_pose_channel (bPose *pose, bPoseChannel *chan){
846 /* chan is no longer valid for the calling function.
847 and should not be used by that function after calling
850 bPoseChannel *curChan;
852 /* Determine if an equivalent channel exists already */
853 for (curChan=pose->chanbase.first; curChan; curChan=curChan->next){
854 if (!strcmp (curChan->name, chan->name)){
857 memcpy (curChan->quat, chan->quat, sizeof(chan->quat));
859 memcpy (curChan->size, chan->size, sizeof(chan->size));
861 memcpy (curChan->loc, chan->loc, sizeof(chan->loc));
863 Mat4CpyMat4 (curChan->obmat, chan->obmat);
865 curChan->flag |= chan->flag;
873 /* If an equivalent channel doesn't exist, then don't bother setting it. */