* Menus for Bone-Groups
[blender.git] / source / blender / src / poseobject.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): Ton Roosendaal, Blender Foundation '05, full recode.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  * support for animation modes - Reevan McKay
27  */
28
29 #include <stdlib.h>
30 #include <stddef.h>
31 #include <string.h>
32
33 #include "MEM_guardedalloc.h"
34
35 #include "BLI_arithb.h"
36 #include "BLI_blenlib.h"
37 #include "BLI_dynstr.h"
38
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_mesh_types.h"
44 #include "DNA_meshdata_types.h"
45 #include "DNA_modifier_types.h"
46 #include "DNA_object_types.h"
47 #include "DNA_scene_types.h"
48 #include "DNA_screen_types.h"
49 #include "DNA_view3d_types.h"
50 #include "DNA_userdef_types.h"
51
52 #include "BKE_action.h"
53 #include "BKE_armature.h"
54 #include "BKE_blender.h"
55 #include "BKE_constraint.h"
56 #include "BKE_deform.h"
57 #include "BKE_depsgraph.h"
58 #include "BKE_displist.h"
59 #include "BKE_global.h"
60 #include "BKE_modifier.h"
61 #include "BKE_object.h"
62 #include "BKE_utildefines.h"
63 #include "BKE_ipo.h"
64
65 #include "BIF_editarmature.h"
66 #include "BIF_editaction.h"
67 #include "BIF_editconstraint.h"
68 #include "BIF_editdeform.h"
69 #include "BIF_gl.h"
70 #include "BIF_graphics.h"
71 #include "BIF_interface.h"
72 #include "BIF_poseobject.h"
73 #include "BIF_space.h"
74 #include "BIF_toolbox.h"
75 #include "BIF_screen.h"
76
77 #include "BDR_editobject.h"
78
79 #include "BSE_edit.h"
80 #include "BSE_editipo.h"
81 #include "BSE_trans_types.h"
82
83 #include "mydevice.h"
84 #include "blendef.h"
85 #include "transform.h"
86
87 #include "BIF_transform.h" /* for autokey TFM_TRANSLATION, etc */
88
89 void enter_posemode(void)
90 {
91         Base *base;
92         Object *ob;
93         bArmature *arm;
94         
95         if(G.scene->id.lib) return;
96         base= BASACT;
97         if(base==NULL) return;
98         
99         ob= base->object;
100         
101         if (ob->id.lib){
102                 error ("Can't pose libdata");
103                 return;
104         }
105
106         switch (ob->type){
107         case OB_ARMATURE:
108                 arm= get_armature(ob);
109                 if( arm==NULL ) return;
110                 
111                 ob->flag |= OB_POSEMODE;
112                 base->flag= ob->flag;
113                 
114                 allqueue(REDRAWHEADERS, 0);     
115                 allqueue(REDRAWBUTSALL, 0);     
116                 allqueue(REDRAWOOPS, 0);
117                 allqueue(REDRAWVIEW3D, 0);
118                 break;
119         default:
120                 return;
121         }
122
123         if (G.obedit) exit_editmode(EM_FREEDATA|EM_WAITCURSOR);
124         G.f &= ~(G_VERTEXPAINT | G_TEXTUREPAINT | G_WEIGHTPAINT);
125 }
126
127 void set_pose_keys (Object *ob)
128 {
129         bArmature *arm= ob->data;
130         bPoseChannel *chan;
131
132         if (ob->pose){
133                 for (chan=ob->pose->chanbase.first; chan; chan=chan->next){
134                         Bone *bone= chan->bone;
135                         if(bone && (bone->flag & BONE_SELECTED) && (arm->layer & bone->layer)) {
136                                 chan->flag |= POSE_KEY;         
137                         }
138                         else {
139                                 chan->flag &= ~POSE_KEY;
140                         }
141                 }
142         }
143 }
144
145
146 void exit_posemode(void)
147 {
148         Object *ob= OBACT;
149         Base *base= BASACT;
150
151         if(ob==NULL) return;
152         
153         ob->flag &= ~OB_POSEMODE;
154         base->flag= ob->flag;
155         
156         countall();
157         allqueue(REDRAWVIEW3D, 0);
158         allqueue(REDRAWOOPS, 0);
159         allqueue(REDRAWHEADERS, 0);     
160         allqueue(REDRAWBUTSALL, 0);     
161
162         scrarea_queue_headredraw(curarea);
163 }
164
165 /* called by buttons to find a bone to display/edit values for */
166 bPoseChannel *get_active_posechannel (Object *ob)
167 {
168         bArmature *arm= ob->data;
169         bPoseChannel *pchan;
170         
171         if ELEM(NULL, ob, ob->pose)
172                 return NULL;
173         
174         /* find active */
175         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
176                 if(pchan->bone && (pchan->bone->flag & BONE_ACTIVE) && (pchan->bone->layer & arm->layer))
177                         return pchan;
178         }
179         
180         return NULL;
181 }
182
183 /* if a selected or active bone is protected, throw error and return 1 */
184 /* only_selected==1 : the active bone is allowed to be protected */
185 static int pose_has_protected_selected(Object *ob, int only_selected)
186 {
187         
188         /* check protection */
189         if(ob->proxy) {
190                 bPoseChannel *pchan;
191                 bArmature *arm= ob->data;
192                 
193                 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
194                         if(pchan->bone && (pchan->bone->layer & arm->layer)) {
195                                 if(pchan->bone->layer & arm->layer_protected) {
196                                         if(only_selected && (pchan->bone->flag & BONE_ACTIVE));
197                                         else if(pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) 
198                                            break;
199                                 }
200                         }
201                 }
202                 if(pchan) {
203                         error("Cannot change Proxy protected bones");
204                         return 1;
205                 }
206         }
207         return 0;
208 }
209
210 /* only for real IK, not for auto-IK */
211 int pose_channel_in_IK_chain(Object *ob, bPoseChannel *pchan)
212 {
213         bConstraint *con;
214         Bone *bone;
215         
216         for(con= pchan->constraints.first; con; con= con->next) {
217                 if(con->type==CONSTRAINT_TYPE_KINEMATIC) {
218                         bKinematicConstraint *data= con->data;
219                         if((data->flag & CONSTRAINT_IK_AUTO)==0)
220                                 return 1;
221                 }
222         }
223         for(bone= pchan->bone->childbase.first; bone; bone= bone->next) {
224                 pchan= get_pose_channel(ob->pose, bone->name);
225                 if(pchan && pose_channel_in_IK_chain(ob, pchan))
226                         return 1;
227         }
228         return 0;
229 }
230
231 /* ********************************************** */
232
233 /* For the object with pose/action: create path curves for selected bones 
234  * This recalculates the WHOLE path within the pchan->pathsf and pchan->pathef range
235  */
236 void pose_calculate_path(Object *ob)
237 {
238         bArmature *arm;
239         bPoseChannel *pchan;
240         Base *base;
241         float *fp;
242         int cfra;
243         int sfra, efra;
244         
245         if (ob==NULL || ob->pose==NULL)
246                 return;
247         arm= ob->data;
248         
249         /* version patch for older files here (do_versions patch too complicated) */
250         if ((arm->pathsf == 0) || (arm->pathef == 0)) {
251                 arm->pathsf = SFRA;
252                 arm->pathef = EFRA;
253         }
254         if (arm->pathsize == 0) {
255                 arm->pathsize = 1;
256         }
257         
258         /* set frame values */
259         cfra= CFRA;
260         sfra = arm->pathsf;
261         efra = arm->pathef;
262         if (efra <= sfra) {
263                 error("Can't calculate paths when pathlen <= 0");
264                 return;
265         }
266         
267         waitcursor(1);
268         
269         /* hack: for unsaved files, set OB_RECALC so that paths can get calculated */
270         if ((ob->recalc & OB_RECALC)==0) {
271                 ob->recalc |= OB_RECALC;
272                 DAG_object_update_flags(G.scene, ob, screen_view3d_layers());
273         }
274         else
275                 DAG_object_update_flags(G.scene, ob, screen_view3d_layers());
276         
277         
278         /* malloc the path blocks */
279         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
280                 if ((pchan->bone) && (pchan->bone->flag & BONE_SELECTED)) {
281                         if (arm->layer & pchan->bone->layer) {
282                                 pchan->pathlen= efra-sfra+1;
283                                 pchan->pathsf= sfra;
284                                 pchan->pathef= efra+1;
285                                 if (pchan->path)
286                                         MEM_freeN(pchan->path);
287                                 pchan->path= MEM_callocN(3*pchan->pathlen*sizeof(float), "pchan path");
288                         }
289                 }
290         }
291         
292         for (CFRA=sfra; CFRA<=efra; CFRA++) {
293                 /* do all updates */
294                 for (base= FIRSTBASE; base; base= base->next) {
295                         if (base->object->recalc) {
296                                 int temp= base->object->recalc;
297                                 object_handle_update(base->object);
298                                 base->object->recalc= temp;
299                         }
300                 }
301                 
302                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
303                         if ((pchan->bone) && (pchan->bone->flag & BONE_SELECTED)) {
304                                 if (arm->layer & pchan->bone->layer) {
305                                         if (pchan->path) {
306                                                 fp= pchan->path+3*(CFRA-sfra);
307                                                 
308                                                 if (arm->pathflag & ARM_PATH_HEADS) { 
309                                                         VECCOPY(fp, pchan->pose_head);
310                                                 }
311                                                 else {
312                                                         VECCOPY(fp, pchan->pose_tail);
313                                                 }
314                                                 
315                                                 Mat4MulVecfl(ob->obmat, fp);
316                                         }
317                                 }
318                         }
319                 }
320         }
321         
322         waitcursor(0);
323         
324         CFRA= cfra;
325         allqueue(REDRAWVIEW3D, 0);      /* recalc tags are still there */
326         allqueue(REDRAWBUTSEDIT, 0);
327 }
328
329 /* For the object with pose/action: update paths for those that have got them
330  * This should selectively update paths that exist...
331  */
332 void pose_recalculate_paths(Object *ob)
333 {
334         bArmature *arm;
335         bPoseChannel *pchan;
336         Base *base;
337         float *fp;
338         int cfra;
339         int sfra, efra;
340         
341         if (ob==NULL || ob->pose==NULL)
342                 return;
343         arm= ob->data;
344         
345         /* set frame values */
346         cfra = CFRA;
347         sfra = efra = cfra; 
348         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
349                 if ((pchan->bone) && (arm->layer & pchan->bone->layer)) {
350                         if (pchan->path) {
351                                 /* if the pathsf and pathef aren't initialised, abort! */
352                                 if (ELEM(0, pchan->pathsf, pchan->pathef))      
353                                         return;
354                                 
355                                 /* try to increase area to do (only as much as needed) */
356                                 sfra= MIN2(sfra, pchan->pathsf);
357                                 efra= MAX2(efra, pchan->pathef);
358                         }
359                 }
360         }
361         if (efra <= sfra) return;
362         
363         waitcursor(1);
364         
365         /* hack: for unsaved files, set OB_RECALC so that paths can get calculated */
366         if ((ob->recalc & OB_RECALC)==0) {
367                 ob->recalc |= OB_RECALC;
368                 DAG_object_update_flags(G.scene, ob, screen_view3d_layers());
369         }
370         else
371                 DAG_object_update_flags(G.scene, ob, screen_view3d_layers());
372         
373         for (CFRA=sfra; CFRA<=efra; CFRA++) {
374                 /* do all updates */
375                 for (base= FIRSTBASE; base; base= base->next) {
376                         if (base->object->recalc) {
377                                 int temp= base->object->recalc;
378                                 object_handle_update(base->object);
379                                 base->object->recalc= temp;
380                         }
381                 }
382                 
383                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
384                         if ((pchan->bone) && (arm->layer & pchan->bone->layer)) {
385                                 if (pchan->path) {
386                                         /* only update if:
387                                          *      - in range of this pchan's existing path
388                                          *      - ... insert evil filtering/optimising conditions here...
389                                          */
390                                         if (IN_RANGE(CFRA, pchan->pathsf, pchan->pathef)) {
391                                                 fp= pchan->path+3*(CFRA-sfra);
392                                                 
393                                                 if (arm->pathflag & ARM_PATH_HEADS) { 
394                                                         VECCOPY(fp, pchan->pose_head);
395                                                 }
396                                                 else {
397                                                         VECCOPY(fp, pchan->pose_tail);
398                                                 }
399                                                 
400                                                 Mat4MulVecfl(ob->obmat, fp);
401                                         }
402                                 }
403                         }
404                 }
405         }
406         
407         waitcursor(0);
408         
409         CFRA= cfra;
410         allqueue(REDRAWVIEW3D, 0);      /* recalc tags are still there */
411         allqueue(REDRAWBUTSEDIT, 0);
412 }
413
414 /* for the object with pose/action: clear path curves for selected bones only */
415 void pose_clear_paths(Object *ob)
416 {
417         bPoseChannel *pchan;
418         
419         if (ob==NULL || ob->pose==NULL)
420                 return;
421         
422         /* free the path blocks */
423         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
424                 if ((pchan->bone) && (pchan->bone->flag & BONE_SELECTED)) {
425                         if (pchan->path) {
426                                 MEM_freeN(pchan->path);
427                                 pchan->path= NULL;
428                         }
429                 }
430         }
431         
432         allqueue(REDRAWVIEW3D, 0);
433 }
434
435
436
437 void pose_select_constraint_target(void)
438 {
439         Object *ob= OBACT;
440         bArmature *arm= ob->data;
441         bPoseChannel *pchan;
442         bConstraint *con;
443         
444         /* paranoia checks */
445         if (!ob && !ob->pose) return;
446         if (ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
447         
448         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
449                 if (arm->layer & pchan->bone->layer) {
450                         if (pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
451                                 for (con= pchan->constraints.first; con; con= con->next) {
452                                         bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
453                                         ListBase targets = {NULL, NULL};
454                                         bConstraintTarget *ct;
455                                         
456                                         if (cti && cti->get_constraint_targets) {
457                                                 cti->get_constraint_targets(con, &targets);
458                                                 
459                                                 for (ct= targets.first; ct; ct= ct->next) {
460                                                         if ((ct->tar == ob) && (ct->subtarget[0])) {
461                                                                 bPoseChannel *pchanc= get_pose_channel(ob->pose, ct->subtarget);
462                                                                 if(pchanc)
463                                                                         pchanc->bone->flag |= BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL;
464                                                         }
465                                                 }
466                                                 
467                                                 if (cti->flush_constraint_targets)
468                                                         cti->flush_constraint_targets(con, &targets, 1);
469                                         }
470                                 }
471                         }
472                 }
473         }
474         
475         allqueue (REDRAWVIEW3D, 0);
476         allqueue (REDRAWBUTSOBJECT, 0);
477         allqueue (REDRAWOOPS, 0);
478         
479         BIF_undo_push("Select constraint target");
480
481 }
482
483 /* context: active channel */
484 void pose_special_editmenu(void)
485 {
486         Object *ob= OBACT;
487         short nr;
488         
489         /* paranoia checks */
490         if(!ob && !ob->pose) return;
491         if(ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
492         
493         nr= pupmenu("Specials%t|Select Constraint Target%x1|Flip Left-Right Names%x2|Calculate Paths%x3|Clear Paths%x4|Clear User Transform %x5|Relax Pose %x6");
494         if(nr==1) {
495                 pose_select_constraint_target();
496         }
497         else if(nr==2) {
498                 pose_flip_names();
499         }
500         else if(nr==3) {
501                 pose_calculate_path(ob);
502         }
503         else if(nr==4) {
504                 pose_clear_paths(ob);
505         }
506         else if(nr==5) {
507                 rest_pose(ob->pose);
508                 DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
509                 BIF_undo_push("Clear User Transform Pose");
510         }
511         else if(nr==6) {
512                 pose_relax();
513         }
514 }
515
516 void pose_add_IK(void)
517 {
518         Object *ob= OBACT;
519         
520         /* paranoia checks */
521         if(!ob && !ob->pose) return;
522         if(ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
523         
524         add_constraint(1);      /* 1 means only IK */
525 }
526
527 /* context: all selected channels */
528 void pose_clear_IK(void)
529 {
530         Object *ob= OBACT;
531         bArmature *arm= ob->data;
532         bPoseChannel *pchan;
533         bConstraint *con;
534         bConstraint *next;
535         
536         /* paranoia checks */
537         if(!ob && !ob->pose) return;
538         if(ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
539         
540         if(pose_has_protected_selected(ob, 0))
541                 return;
542         
543         if(okee("Remove IK constraint(s)")==0) return;
544
545         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
546                 if(arm->layer & pchan->bone->layer) {
547                         if(pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
548                                 
549                                 for(con= pchan->constraints.first; con; con= next) {
550                                         next= con->next;
551                                         if(con->type==CONSTRAINT_TYPE_KINEMATIC) {
552                                                 BLI_remlink(&pchan->constraints, con);
553                                                 free_constraint_data(con);
554                                                 MEM_freeN(con);
555                                         }
556                                 }
557                                 pchan->constflag &= ~(PCHAN_HAS_IK|PCHAN_HAS_TARGET);
558                         }
559                 }
560         }
561         
562         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);   // and all its relations
563         
564         allqueue (REDRAWVIEW3D, 0);
565         allqueue (REDRAWBUTSOBJECT, 0);
566         allqueue (REDRAWOOPS, 0);
567         
568         BIF_undo_push("Remove IK constraint(s)");
569 }
570
571 void pose_clear_constraints(void)
572 {
573         Object *ob= OBACT;
574         bArmature *arm= ob->data;
575         bPoseChannel *pchan;
576         
577         /* paranoia checks */
578         if(!ob && !ob->pose) return;
579         if(ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
580         
581         if(pose_has_protected_selected(ob, 0))
582                 return;
583         
584         if(okee("Remove Constraints")==0) return;
585         
586         /* find active */
587         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
588                 if(arm->layer & pchan->bone->layer) {
589                         if(pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
590                                 free_constraints(&pchan->constraints);
591                                 pchan->constflag= 0;
592                         }
593                 }
594         }
595         
596         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);   // and all its relations
597         
598         allqueue (REDRAWVIEW3D, 0);
599         allqueue (REDRAWBUTSOBJECT, 0);
600         allqueue (REDRAWOOPS, 0);
601         
602         BIF_undo_push("Remove Constraint(s)");
603         
604 }
605
606
607 void pose_copy_menu(void)
608 {
609         Object *ob= OBACT;
610         bArmature *arm= ob->data;
611         bPoseChannel *pchan, *pchanact;
612         short nr;
613         int i=0;
614         
615         /* paranoia checks */
616         if(!ob && !ob->pose) return;
617         if(ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
618         
619         /* find active */
620         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
621                 if(pchan->bone->flag & BONE_ACTIVE) break;
622         }
623         
624         if(pchan==NULL) return;
625         
626         if(pose_has_protected_selected(ob, 1))
627                 return;
628
629         pchanact= pchan;
630         
631         i= BLI_countlist(&(pchanact->constraints)); /* if there are 24 or less, allow for the user to select constraints */
632         if (i<25)
633                 nr= pupmenu("Copy Pose Attributes %t|Local Location%x1|Local Rotation%x2|Local Size%x3|%l|Visual Location %x9|Visual Rotation%x10|Visual Size%x11|%l|Constraints (All)%x4|Constraints...%x5|%l|Transform Locks%x6|IK Limits%x7|Bone Shape%x8");
634         else
635                 nr= pupmenu("Copy Pose Attributes %t|Local Location%x1|Local Rotation%x2|Local Size%x3|%l|Visual Location %x9|Visual Rotation%x10|Visual Size%x11|%l|Constraints (All)%x4|%l|Transform Locks%x6|IK Limits%x7|Bone Shape%x8");
636         
637         if(nr==-1) return;
638         if(nr!=5)  {
639                 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
640                         if(     (arm->layer & pchan->bone->layer) &&
641                                 (pchan->bone->flag & BONE_SELECTED) &&
642                                 (pchan!=pchanact)
643                         ) {
644                                 switch (nr) {
645                                         case 1: /* Local Location */
646                                                 VECCOPY(pchan->loc, pchanact->loc);
647                                                 break;
648                                         case 2: /* Local Rotation */
649                                                 QUATCOPY(pchan->quat, pchanact->quat);
650                                                 break;
651                                         case 3: /* Local Size */
652                                                 VECCOPY(pchan->size, pchanact->size);
653                                                 break;
654                                         case 4: /* All Constraints */
655                                         {
656                                                 free_constraints(&pchan->constraints);
657                                                 copy_constraints(&pchan->constraints, &pchanact->constraints);
658                                                 pchan->constflag = pchanact->constflag;
659                                         }
660                                                 break;
661                                         case 6: /* Transform Locks */
662                                                 pchan->protectflag = pchanact->protectflag;
663                                                 break;
664                                         case 7: /* IK (DOF) settings */
665                                         {
666                                                 pchan->ikflag = pchanact->ikflag;
667                                                 VECCOPY(pchan->limitmin, pchanact->limitmin);
668                                                 VECCOPY(pchan->limitmax, pchanact->limitmax);
669                                                 VECCOPY(pchan->stiffness, pchanact->stiffness);
670                                                 pchan->ikstretch= pchanact->ikstretch;
671                                         }
672                                                 break;
673                                         case 8: /* Custom Bone Shape */
674                                                 pchan->custom = pchanact->custom;
675                                                 break;
676                                         case 9: /* Visual Location */
677                                                 armature_loc_pose_to_bone(pchan, pchanact->pose_mat[3], pchan->loc);
678                                                 break;
679                                         case 10: /* Visual Rotation */
680                                         {
681                                                 float delta_mat[4][4], quat[4];
682                                                 
683                                                 armature_mat_pose_to_bone(pchan, pchanact->pose_mat, delta_mat);
684                                                 Mat4ToQuat(delta_mat, quat);
685                                                 QUATCOPY(pchan->quat, quat);
686                                         }
687                                                 break;
688                                         case 11: /* Visual Size */
689                                         {
690                                                 float delta_mat[4][4], size[4];
691                                                 
692                                                 armature_mat_pose_to_bone(pchan, pchanact->pose_mat, delta_mat);
693                                                 Mat4ToSize(delta_mat, size);
694                                                 VECCOPY(pchan->size, size);
695                                         }
696                                 }
697                         }
698                 }
699         } else { /* constraints, optional */
700                 bConstraint *con, *con_back;
701                 int const_toggle[24];
702                 ListBase const_copy={0, 0};
703                 
704                 duplicatelist (&const_copy, &(pchanact->constraints));
705                 
706                 /* build the puplist of constraints */
707                 for (con = pchanact->constraints.first, i=0; con; con=con->next, i++){
708                         const_toggle[i]= 1;
709                         add_numbut(i, TOG|INT, con->name, 0, 0, &(const_toggle[i]), "");
710                 }
711                 
712                 if (!do_clever_numbuts("Select Constraints", i, REDRAW)) {
713                         BLI_freelistN(&const_copy);
714                         return;
715                 }
716                 
717                 /* now build a new listbase from the options selected */
718                 for (i=0, con=const_copy.first; con; i++) {
719                         if (!const_toggle[i]) {
720                                 con_back= con->next;
721                                 BLI_freelinkN(&const_copy, con);
722                                 con= con_back;
723                         } else {
724                                 con= con->next;
725                         }
726                 }
727                 
728                 /* Copy the temo listbase to the selected posebones */
729                 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
730                         if(     (arm->layer & pchan->bone->layer) &&
731                                 (pchan->bone->flag & BONE_SELECTED) &&
732                                 (pchan!=pchanact)
733                         ) {
734                                 free_constraints(&pchan->constraints);
735                                 copy_constraints(&pchan->constraints, &const_copy);
736                                 pchan->constflag = pchanact->constflag;
737                         }
738                 }
739                 BLI_freelistN(&const_copy);
740                 update_pose_constraint_flags(ob->pose); /* we could work out the flags but its simpler to do this */
741         }
742         
743         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);   // and all its relations
744         
745         allqueue (REDRAWVIEW3D, 0);
746         allqueue (REDRAWBUTSOBJECT, 0);
747         allqueue (REDRAWOOPS, 0);
748         
749         BIF_undo_push("Copy Pose Attributes");
750         
751 }
752
753 /* ******************** copy/paste pose ********************** */
754
755 static bPose    *g_posebuf=NULL;
756
757 void free_posebuf(void) 
758 {
759         if (g_posebuf) {
760                 // was copied without constraints
761                 BLI_freelistN (&g_posebuf->chanbase);
762                 MEM_freeN (g_posebuf);
763         }
764         g_posebuf=NULL;
765 }
766
767 void copy_posebuf (void)
768 {
769         Object *ob= OBACT;
770
771         if (!ob || !ob->pose){
772                 error ("No Pose");
773                 return;
774         }
775
776         free_posebuf();
777         
778         set_pose_keys(ob);  // sets chan->flag to POSE_KEY if bone selected
779         copy_pose(&g_posebuf, ob->pose, 0);
780
781 }
782
783 void paste_posebuf (int flip)
784 {
785         Object *ob= OBACT;
786         bPoseChannel *chan, *pchan;
787         float eul[4];
788         char name[32];
789         
790         if (!ob || !ob->pose)
791                 return;
792
793         if (!g_posebuf){
794                 error ("Copy buffer is empty");
795                 return;
796         }
797         
798         /*
799         // disabled until protected bones in proxies follow the rules everywhere else!
800         if(pose_has_protected_selected(ob, 1))
801                 return;
802         */
803         
804         /* Safely merge all of the channels in this pose into
805         any existing pose */
806         for (chan=g_posebuf->chanbase.first; chan; chan=chan->next) {
807                 if (chan->flag & POSE_KEY) {
808                         BLI_strncpy(name, chan->name, sizeof(name));
809                         if (flip)
810                                 bone_flip_name (name, 0);               // 0 = don't strip off number extensions
811                                 
812                         /* only copy when channel exists, poses are not meant to add random channels to anymore */
813                         pchan= get_pose_channel(ob->pose, name);
814                         
815                         if (pchan) {
816                                 /* only loc rot size */
817                                 /* only copies transform info for the pose */
818                                 VECCOPY(pchan->loc, chan->loc);
819                                 VECCOPY(pchan->size, chan->size);
820                                 QUATCOPY(pchan->quat, chan->quat);
821                                 pchan->flag= chan->flag;
822                                 
823                                 if (flip) {
824                                         pchan->loc[0]*= -1;
825                                         
826                                         QuatToEul(pchan->quat, eul);
827                                         eul[1]*= -1;
828                                         eul[2]*= -1;
829                                         EulToQuat(eul, pchan->quat);
830                                 }
831                                 
832                                 if (autokeyframe_cfra_can_key(ob)) {
833                                         ID *id= &ob->id;
834                                         
835                                         /* Set keys on pose */
836                                         if (chan->flag & POSE_ROT) {
837                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_X, 0);
838                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_Y, 0);
839                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_Z, 0);
840                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_W, 0);
841                                         }
842                                         if (chan->flag & POSE_SIZE) {
843                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_SIZE_X, 0);
844                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_SIZE_Y, 0);
845                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_SIZE_Z, 0);
846                                         }
847                                         if (chan->flag & POSE_LOC) {
848                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_LOC_X, 0);
849                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_LOC_Y, 0);
850                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_LOC_Z, 0);
851                                         }
852                                         
853                                         /* clear any unkeyed tags */
854                                         if (chan->bone)
855                                                 chan->bone->flag &= ~BONE_UNKEYED;
856                                 }
857                                 else {
858                                         /* add unkeyed tags */
859                                         if (chan->bone)
860                                                 chan->bone->flag |= BONE_UNKEYED;
861                                 }
862                         }
863                 }
864         }
865
866         /* Update event for pose and deformation children */
867         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
868         
869         if ((IS_AUTOKEY_MODE(NORMAL))) {
870                 remake_action_ipos(ob->action);
871                 allqueue (REDRAWIPO, 0);
872                 allqueue (REDRAWVIEW3D, 0);
873                 allqueue (REDRAWACTION, 0);             
874                 allqueue(REDRAWNLA, 0);
875         }
876         else {
877                 /* need to trick depgraph, action is not allowed to execute on pose */
878                 where_is_pose(ob);
879                 ob->recalc= 0;
880         }
881
882         BIF_undo_push("Paste Action Pose");
883 }
884
885 /* ********************************************** */
886
887 /* context weightpaint and deformer in posemode */
888 void pose_adds_vgroups(Object *meshobj, int heatweights)
889 {
890         extern VPaint Gwp;         /* from vpaint */
891         Object *poseobj= modifiers_isDeformedByArmature(meshobj);
892
893         if(poseobj==NULL || (poseobj->flag & OB_POSEMODE)==0) {
894                 error("The active object must have a deforming armature in pose mode");
895                 return;
896         }
897
898         add_verts_to_dgroups(meshobj, poseobj, heatweights, (Gwp.flag & VP_MIRROR_X));
899
900         if(heatweights)
901                 BIF_undo_push("Apply Bone Heat Weights to Vertex Groups");
902         else
903                 BIF_undo_push("Apply Bone Envelopes to Vertex Groups");
904
905         allqueue(REDRAWVIEW3D, 0);
906         allqueue(REDRAWBUTSEDIT, 0);
907         
908         // and all its relations
909         DAG_object_flush_update(G.scene, meshobj, OB_RECALC_DATA);
910 }
911
912 /* ********************************************** */
913
914 /* adds a new pose-group */
915 void pose_add_posegroup ()
916 {
917         Object *ob= OBACT;
918         bPose *pose= (ob) ? ob->pose : NULL;
919         bActionGroup *grp;
920         
921         if (ELEM(NULL, ob, ob->pose))
922                 return;
923         
924         grp= MEM_callocN(sizeof(bActionGroup), "PoseGroup");
925         strcpy(grp->name, "Group");
926         BLI_addtail(&pose->agroups, grp);
927         BLI_uniquename(&pose->agroups, grp, "Group", offsetof(bActionGroup, name), 32);
928         
929         pose->active_group= BLI_countlist(&pose->agroups);
930         
931         BIF_undo_push("Add Bone Group");
932         
933         allqueue(REDRAWBUTSEDIT, 0);
934         allqueue(REDRAWVIEW3D, 0);
935 }
936
937 /* Remove the active bone-group */
938 void pose_remove_posegroup ()
939 {
940         Object *ob= OBACT;
941         bPose *pose= (ob) ? ob->pose : NULL;
942         bActionGroup *grp = NULL;
943         bPoseChannel *pchan;
944         
945         /* sanity checks */
946         if (ELEM(NULL, ob, pose))
947                 return;
948         if (pose->active_group <= 0)
949                 return;
950         
951         /* get group to remove */
952         grp= BLI_findlink(&pose->agroups, pose->active_group-1);
953         if (grp) {
954                 /* firstly, make sure nothing references it */
955                 for (pchan= pose->chanbase.first; pchan; pchan= pchan->next) {
956                         if (pchan->agrp_index == pose->active_group)
957                                 pchan->agrp_index= 0;
958                 }
959                 
960                 /* now, remove it from the pose */
961                 BLI_freelinkN(&pose->agroups, grp);
962                 pose->active_group= 0;
963                 
964                 BIF_undo_push("Remove Bone Group");
965         }
966         
967         allqueue(REDRAWBUTSEDIT, 0);
968         allqueue(REDRAWVIEW3D, 0);
969 }
970
971 char *build_posegroups_menustr (bPose *pose, short for_pupmenu)
972 {
973         DynStr *pupds= BLI_dynstr_new();
974         bActionGroup *grp;
975         char *str;
976         char buf[16];
977         int i;
978         
979         /* add title first (and the "none" entry) */
980         BLI_dynstr_append(pupds, "Bone Group%t|");
981         if (for_pupmenu)
982                 BLI_dynstr_append(pupds, "Add New%x0|");
983         else
984                 BLI_dynstr_append(pupds, "BG: [None]%x0|");
985         
986         /* loop through markers, adding them */
987         for (grp= pose->agroups.first, i=1; grp; grp=grp->next, i++) {
988                 if (for_pupmenu == 0)
989                         BLI_dynstr_append(pupds, "BG: ");
990                 BLI_dynstr_append(pupds, grp->name);
991                 
992                 sprintf(buf, "%%x%d", i);
993                 BLI_dynstr_append(pupds, buf);
994                 
995                 if (grp->next)
996                         BLI_dynstr_append(pupds, "|");
997         }
998         
999         /* convert to normal MEM_malloc'd string */
1000         str= BLI_dynstr_get_cstring(pupds);
1001         BLI_dynstr_free(pupds);
1002         
1003         return str;
1004 }
1005
1006 /* Assign selected pchans to the bone group that the user selects */
1007 void pose_assign_to_posegroup ()
1008 {
1009         Object *ob= OBACT;
1010         bArmature *arm= (ob) ? ob->data : NULL;
1011         bPose *pose= (ob) ? ob->pose : NULL;
1012         bPoseChannel *pchan;
1013         char *menustr;
1014         int nr;
1015         short done= 0;
1016         
1017         /* sanity checks */
1018         if (ELEM3(NULL, ob, pose, arm))
1019                 return;
1020
1021         /* get group to affect */
1022         menustr= build_posegroups_menustr(pose, 1);
1023         nr= pupmenu(menustr);
1024         MEM_freeN(menustr);
1025         
1026         if (nr < 0) 
1027                 return;
1028         else if (nr == 0) {
1029                 /* add new - note: this does an undo push and sets active group */
1030                 pose_add_posegroup();
1031         }
1032         else
1033                 pose->active_group= nr;
1034         
1035         /* add selected bones to group then */
1036         for (pchan= pose->chanbase.first; pchan; pchan= pchan->next) {
1037                 if ((pchan->bone->flag & BONE_SELECTED) && (pchan->bone->layer & arm->layer)) {
1038                         pchan->agrp_index= pose->active_group;
1039                         done= 1;
1040                 }
1041         }
1042         
1043         if (done)
1044                 BIF_undo_push("Add Bones To Group");
1045                 
1046         allqueue(REDRAWBUTSEDIT, 0);
1047         allqueue(REDRAWVIEW3D, 0);
1048 }
1049
1050 /* Remove selected pchans from their bone groups */
1051 void pose_remove_from_posegroups ()
1052 {
1053         Object *ob= OBACT;
1054         bArmature *arm= (ob) ? ob->data : NULL;
1055         bPose *pose= (ob) ? ob->pose : NULL;
1056         bPoseChannel *pchan;
1057         short done= 0;
1058         
1059         /* sanity checks */
1060         if (ELEM3(NULL, ob, pose, arm))
1061                 return;
1062         
1063         /* remove selected bones from their groups */
1064         for (pchan= pose->chanbase.first; pchan; pchan= pchan->next) {
1065                 if ((pchan->bone->flag & BONE_SELECTED) && (pchan->bone->layer & arm->layer)) {
1066                         if (pchan->agrp_index) {
1067                                 pchan->agrp_index= 0;
1068                                 done= 1;
1069                         }
1070                 }
1071         }
1072         
1073         if (done)
1074                 BIF_undo_push("Remove Bones From Groups");
1075                 
1076         allqueue(REDRAWBUTSEDIT, 0);
1077         allqueue(REDRAWVIEW3D, 0);
1078 }
1079
1080 /* Ctrl-G in 3D-View while in PoseMode */
1081 void pgroup_operation_with_menu (void)
1082 {
1083         Object *ob= OBACT;
1084         bArmature *arm= (ob) ? ob->data : NULL;
1085         bPose *pose= (ob) ? ob->pose : NULL;
1086         bPoseChannel *pchan= NULL;
1087         int mode;
1088         
1089         /* sanity checks */
1090         if (ELEM3(NULL, ob, pose, arm))
1091                 return;
1092         
1093         /* check that something is selected */
1094         for (pchan= pose->chanbase.first; pchan; pchan= pchan->next) {
1095                 if ((pchan->bone->flag & BONE_SELECTED) && (pchan->bone->layer & arm->layer)) 
1096                         break;
1097         }
1098         if (pchan == NULL)
1099                 return;
1100         
1101         /* get mode of action */
1102         if (pchan)
1103                 mode= pupmenu("Bone Groups%t|Add Selected to Group%x1|Add New Group%x2|Remove Selected From Groups%x3|Remove Active Group%x4");
1104         else
1105                 mode= pupmenu("Bone Groups%t|Add New Group%x2|Remove Active Group%x4");
1106                 
1107         /* handle mode */
1108         switch (mode) {
1109                 case 1:
1110                         pose_assign_to_posegroup();
1111                         break;
1112                 case 2:
1113                         pose_add_posegroup();
1114                         break;
1115                 case 3:
1116                         pose_remove_from_posegroups();
1117                         break;
1118                 case 4:
1119                         pose_remove_posegroup();
1120                         break;
1121         }
1122 }
1123
1124 /* ********************************************** */
1125
1126 /* context active object */
1127 void pose_flip_names(void)
1128 {
1129         Object *ob= OBACT;
1130         bArmature *arm= ob->data;
1131         bPoseChannel *pchan;
1132         char newname[32];
1133         
1134         /* paranoia checks */
1135         if(!ob && !ob->pose) return;
1136         if(ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
1137         
1138         if(pose_has_protected_selected(ob, 0))
1139                 return;
1140         
1141         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1142                 if(arm->layer & pchan->bone->layer) {
1143                         if(pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
1144                                 BLI_strncpy(newname, pchan->name, sizeof(newname));
1145                                 bone_flip_name(newname, 1);     // 1 = do strip off number extensions
1146                                 armature_bone_rename(ob->data, pchan->name, newname);
1147                         }
1148                 }
1149         }
1150         
1151         allqueue(REDRAWVIEW3D, 0);
1152         allqueue(REDRAWBUTSEDIT, 0);
1153         allqueue(REDRAWBUTSOBJECT, 0);
1154         allqueue (REDRAWACTION, 0);
1155         allqueue(REDRAWOOPS, 0);
1156         BIF_undo_push("Flip names");
1157         
1158 }
1159
1160 /* context active object, or weightpainted object with armature in posemode */
1161 void pose_activate_flipped_bone(void)
1162 {
1163         Object *ob= OBACT;
1164         bArmature *arm= ob->data;
1165         
1166         if(ob==NULL) return;
1167
1168         if(G.f & G_WEIGHTPAINT) {
1169                 ob= modifiers_isDeformedByArmature(ob);
1170         }
1171         if(ob && (ob->flag & OB_POSEMODE)) {
1172                 bPoseChannel *pchan, *pchanf;
1173                 
1174                 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1175                         if(arm->layer & pchan->bone->layer) {
1176                                 if(pchan->bone->flag & BONE_ACTIVE)
1177                                         break;
1178                         }
1179                 }
1180                 if(pchan) {
1181                         char name[32];
1182                         
1183                         BLI_strncpy(name, pchan->name, 32);
1184                         bone_flip_name(name, 1);        // 0 = do not strip off number extensions
1185                         
1186                         pchanf= get_pose_channel(ob->pose, name);
1187                         if(pchanf && pchanf!=pchan) {
1188                                 pchan->bone->flag &= ~(BONE_SELECTED|BONE_ACTIVE);
1189                                 pchanf->bone->flag |= (BONE_SELECTED|BONE_ACTIVE);
1190                         
1191                                 /* in weightpaint we select the associated vertex group too */
1192                                 if(G.f & G_WEIGHTPAINT) {
1193                                         vertexgroup_select_by_name(OBACT, name);
1194                                         DAG_object_flush_update(G.scene, OBACT, OB_RECALC_DATA);
1195                                 }
1196                                 
1197                                 select_actionchannel_by_name(ob->action, name, 1);
1198                                 
1199                                 allqueue(REDRAWVIEW3D, 0);
1200                                 allqueue(REDRAWACTION, 0);
1201                                 allqueue(REDRAWIPO, 0);         /* To force action/constraint ipo update */
1202                                 allqueue(REDRAWBUTSEDIT, 0);
1203                                 allqueue(REDRAWBUTSOBJECT, 0);
1204                                 allqueue(REDRAWOOPS, 0);
1205                         }                       
1206                 }
1207         }
1208 }
1209
1210 /* This function pops up the move-to-layer popup widgets when the user
1211  * presses either SHIFT-MKEY or MKEY in PoseMode OR EditMode (for Armatures)
1212  */
1213 void pose_movetolayer(void)
1214 {
1215         Object *ob= OBACT;
1216         bArmature *arm;
1217         short lay= 0;
1218         
1219         if (ob==NULL) return;
1220         arm= ob->data;
1221         
1222         if (G.qual & LR_SHIFTKEY) {
1223                 /* armature layers */
1224                 lay= arm->layer;
1225                 if ( movetolayer_short_buts(&lay, "Armature Layers")==0 ) return;
1226                 if (lay==0) return;
1227                 arm->layer= lay;
1228                 if(ob->pose)
1229                         ob->pose->proxy_layer= lay;
1230                 
1231                 allqueue(REDRAWVIEW3D, 0);
1232                 allqueue(REDRAWACTION, 0);
1233                 allqueue(REDRAWBUTSEDIT, 0);
1234         }
1235         else if (G.obedit) {
1236                 /* the check for editbone layer moving needs to occur before posemode one to work */
1237                 EditBone *ebo;
1238                 EditBone *flipBone;
1239                 
1240                 for (ebo= G.edbo.first; ebo; ebo= ebo->next) {
1241                         if (arm->layer & ebo->layer) {
1242                                 if (ebo->flag & BONE_SELECTED)
1243                                         lay |= ebo->layer;
1244                         }
1245                 }
1246                 if (lay==0) return;
1247                 
1248                 if ( movetolayer_short_buts(&lay, "Bone Layers")==0 ) return;
1249                 if (lay==0) return;
1250                 
1251                 for (ebo= G.edbo.first; ebo; ebo= ebo->next) {
1252                         if (arm->layer & ebo->layer) {
1253                                 if (ebo->flag & BONE_SELECTED) {
1254                                         ebo->layer= lay;
1255                                         if (arm->flag & ARM_MIRROR_EDIT) {
1256                                                 flipBone = armature_bone_get_mirrored(ebo);
1257                                                 if (flipBone)
1258                                                         flipBone->layer = lay;
1259                                         }
1260                                 }
1261                         }
1262                 }
1263                 
1264                 BIF_undo_push("Move Bone Layer");
1265                 allqueue(REDRAWVIEW3D, 0);
1266                 allqueue(REDRAWBUTSEDIT, 0);
1267         }
1268         else if (ob->flag & OB_POSEMODE) {
1269                 /* pose-channel layers */
1270                 bPoseChannel *pchan;
1271                 
1272                 if (pose_has_protected_selected(ob, 0))
1273                         return;
1274                 
1275                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1276                         if (arm->layer & pchan->bone->layer) {
1277                                 if (pchan->bone->flag & BONE_SELECTED)
1278                                         lay |= pchan->bone->layer;
1279                         }
1280                 }
1281                 if (lay==0) return;
1282                 
1283                 if ( movetolayer_short_buts(&lay, "Bone Layers")==0 ) return;
1284                 if (lay==0) return;
1285                 
1286                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1287                         if (arm->layer & pchan->bone->layer) {
1288                                 if (pchan->bone->flag & BONE_SELECTED)
1289                                         pchan->bone->layer= lay;
1290                         }
1291                 }
1292                 
1293                 BIF_undo_push("Move Bone Layer");
1294                 allqueue(REDRAWVIEW3D, 0);
1295                 allqueue(REDRAWACTION, 0);
1296                 allqueue(REDRAWBUTSEDIT, 0);
1297         }
1298 }
1299
1300
1301 /* for use with pose_relax only */
1302 static int pose_relax_icu(struct IpoCurve *icu, float framef, float *val, float *frame_prev, float *frame_next)
1303 {
1304         if (!icu) {
1305                 return 0;
1306         } else {
1307                 BezTriple *bezt = icu->bezt;
1308                 
1309                 BezTriple *bezt_prev=NULL, *bezt_next=NULL;
1310                 float w1, w2, wtot;
1311                 int i;
1312                 
1313                 for (i=0; i < icu->totvert; i++, bezt++) {
1314                         if (bezt->vec[1][0] < framef - 0.5) {
1315                                 bezt_prev = bezt;
1316                         } else {
1317                                 break;
1318                         }
1319                 }
1320                 
1321                 if (bezt_prev==NULL) return 0;
1322                 
1323                 /* advance to the next, dont need to advance i */
1324                 bezt = bezt_prev+1;
1325                 
1326                 for (; i < icu->totvert; i++, bezt++) {
1327                         if (bezt->vec[1][0] > framef + 0.5) {
1328                                 bezt_next = bezt;
1329                                                 break;
1330                         }
1331                 }
1332                 
1333                 if (bezt_next==NULL) return 0;
1334         
1335                 if (val) {
1336                         w1 = framef - bezt_prev->vec[1][0];
1337                         w2 = bezt_next->vec[1][0] - framef;
1338                         wtot = w1 + w2;
1339                         w1=w1/wtot;
1340                         w2=w2/wtot;
1341 #if 0
1342                         val = (bezt_prev->vec[1][1] * w2) + (bezt_next->vec[1][1] * w1);
1343 #else
1344                         /* apply the value with a hard coded 6th */
1345                         *val = (((bezt_prev->vec[1][1] * w2) + (bezt_next->vec[1][1] * w1)) + (*val * 5.0f)) / 6.0f;
1346 #endif
1347                 }
1348                 
1349                 if (frame_prev) *frame_prev = bezt_prev->vec[1][0];
1350                 if (frame_next) *frame_next = bezt_next->vec[1][0];
1351                 
1352                 return 1;
1353         }
1354 }
1355
1356 void pose_relax()
1357 {
1358         Object *ob = OBACT;
1359         bPose *pose;
1360         bAction *act;
1361         bArmature *arm;
1362         
1363         IpoCurve *icu_w, *icu_x, *icu_y, *icu_z;
1364         
1365         bPoseChannel *pchan;
1366         bActionChannel *achan;
1367         float framef = F_CFRA;
1368         float frame_prev, frame_next;
1369         float quat_prev[4], quat_next[4], quat_interp[4], quat_orig[4];
1370         
1371         int do_scale = 0;
1372         int do_loc = 0;
1373         int do_quat = 0;
1374         int flag = 0;
1375         int do_x, do_y, do_z;
1376         
1377         if (!ob) return;
1378         
1379         pose = ob->pose;
1380         act = ob->action;
1381         arm = (bArmature *)ob->data;
1382         
1383         if (!pose || !act || !arm) return;
1384         
1385         for (pchan=pose->chanbase.first; pchan; pchan= pchan->next) {
1386                 if (pchan->bone->layer & arm->layer) {
1387                         if (pchan->bone->flag & BONE_SELECTED) {
1388                                 /* do we have an ipo curve? */
1389                                 achan= get_action_channel(act, pchan->name);
1390                                 
1391                                 if (achan && achan->ipo) {
1392                                         /*calc_ipo(achan->ipo, ctime);*/
1393                                         
1394                                         do_x = pose_relax_icu(find_ipocurve(achan->ipo, AC_LOC_X), framef, &pchan->loc[0], NULL, NULL);
1395                                         do_y = pose_relax_icu(find_ipocurve(achan->ipo, AC_LOC_Y), framef, &pchan->loc[1], NULL, NULL);
1396                                         do_z = pose_relax_icu(find_ipocurve(achan->ipo, AC_LOC_Z), framef, &pchan->loc[2], NULL, NULL);
1397                                         do_loc += do_x + do_y + do_z;
1398                                         
1399                                         do_x = pose_relax_icu(find_ipocurve(achan->ipo, AC_SIZE_X), framef, &pchan->size[0], NULL, NULL);
1400                                         do_y = pose_relax_icu(find_ipocurve(achan->ipo, AC_SIZE_Y), framef, &pchan->size[1], NULL, NULL);
1401                                         do_z = pose_relax_icu(find_ipocurve(achan->ipo, AC_SIZE_Z), framef, &pchan->size[2], NULL, NULL);
1402                                         do_scale += do_x + do_y + do_z;
1403                                                 
1404                                         if(     ((icu_w = find_ipocurve(achan->ipo, AC_QUAT_W))) &&
1405                                                 ((icu_x = find_ipocurve(achan->ipo, AC_QUAT_X))) &&
1406                                                 ((icu_y = find_ipocurve(achan->ipo, AC_QUAT_Y))) &&
1407                                                 ((icu_z = find_ipocurve(achan->ipo, AC_QUAT_Z))) )
1408                                         {
1409                                                 /* use the quatw keyframe as a basis for others */
1410                                                 if (pose_relax_icu(icu_w, framef, NULL, &frame_prev, &frame_next)) {
1411                                                         /* get 2 quats */
1412                                                         quat_prev[0] = eval_icu(icu_w, frame_prev);
1413                                                         quat_prev[1] = eval_icu(icu_x, frame_prev);
1414                                                         quat_prev[2] = eval_icu(icu_y, frame_prev);
1415                                                         quat_prev[3] = eval_icu(icu_z, frame_prev);
1416                                                         
1417                                                         quat_next[0] = eval_icu(icu_w, frame_next);
1418                                                         quat_next[1] = eval_icu(icu_x, frame_next);
1419                                                         quat_next[2] = eval_icu(icu_y, frame_next);
1420                                                         quat_next[3] = eval_icu(icu_z, frame_next);
1421                                                         
1422 #if 0
1423                                                         /* apply the setting, completely smooth */
1424                                                         QuatInterpol(pchan->quat, quat_prev, quat_next, (framef-frame_prev) / (frame_next-frame_prev) );
1425 #else
1426                                                         /* tricky interpolation */
1427                                                         QuatInterpol(quat_interp, quat_prev, quat_next, (framef-frame_prev) / (frame_next-frame_prev) );
1428                                                         QUATCOPY(quat_orig, pchan->quat);
1429                                                         QuatInterpol(pchan->quat, quat_orig, quat_interp, 1.0f/6.0f);
1430                                                         /* done */
1431 #endif
1432                                                         do_quat++;
1433                                                 }
1434                                         }
1435                                         
1436                                         /* apply BONE_TRANSFORM tag so that autokeying will pick it up */
1437                                         pchan->bone->flag |= BONE_TRANSFORM;
1438                                 }
1439                         }
1440                 }
1441         }
1442         
1443         ob->pose->flag |= (POSE_LOCKED|POSE_DO_UNLOCK);
1444         
1445         /* do auto-keying */
1446         if (do_loc)             flag |= TFM_TRANSLATION;
1447         if (do_scale)   flag |= TFM_RESIZE;
1448         if (do_quat)    flag |= TFM_ROTATION;
1449         autokeyframe_pose_cb_func(ob, flag, 0);
1450          
1451         /* clear BONE_TRANSFORM flags */
1452         for (pchan=pose->chanbase.first; pchan; pchan= pchan->next)
1453                 pchan->bone->flag &= ~ BONE_TRANSFORM;
1454         
1455         /* do depsgraph flush */
1456         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
1457         BIF_undo_push("Relax Pose");
1458 }