bugfix, copy posebone constraints didnt update properly, canceling transform would...
[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 | G_SCULPTMODE);
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|%l|AutoName Left-Right%x7|AutoName Front-Back%x8|AutoName Top-Bottom%x9");
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         else if(ELEM3(nr, 7, 8, 9)) {
515                 pose_autoside_names(nr-7);
516         }
517 }
518
519 void pose_add_IK(void)
520 {
521         Object *ob= OBACT;
522         
523         /* paranoia checks */
524         if(!ob && !ob->pose) return;
525         if(ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
526         
527         add_constraint(1);      /* 1 means only IK */
528 }
529
530 /* context: all selected channels */
531 void pose_clear_IK(void)
532 {
533         Object *ob= OBACT;
534         bArmature *arm= ob->data;
535         bPoseChannel *pchan;
536         bConstraint *con;
537         bConstraint *next;
538         
539         /* paranoia checks */
540         if(!ob && !ob->pose) return;
541         if(ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
542         
543         if(pose_has_protected_selected(ob, 0))
544                 return;
545         
546         if(okee("Remove IK constraint(s)")==0) return;
547
548         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
549                 if(arm->layer & pchan->bone->layer) {
550                         if(pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
551                                 
552                                 for(con= pchan->constraints.first; con; con= next) {
553                                         next= con->next;
554                                         if(con->type==CONSTRAINT_TYPE_KINEMATIC) {
555                                                 BLI_remlink(&pchan->constraints, con);
556                                                 free_constraint_data(con);
557                                                 MEM_freeN(con);
558                                         }
559                                 }
560                                 pchan->constflag &= ~(PCHAN_HAS_IK|PCHAN_HAS_TARGET);
561                         }
562                 }
563         }
564         
565         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);   // and all its relations
566         
567         allqueue (REDRAWVIEW3D, 0);
568         allqueue (REDRAWBUTSOBJECT, 0);
569         allqueue (REDRAWOOPS, 0);
570         
571         BIF_undo_push("Remove IK constraint(s)");
572 }
573
574 void pose_clear_constraints(void)
575 {
576         Object *ob= OBACT;
577         bArmature *arm= ob->data;
578         bPoseChannel *pchan;
579         
580         /* paranoia checks */
581         if(!ob && !ob->pose) return;
582         if(ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
583         
584         if(pose_has_protected_selected(ob, 0))
585                 return;
586         
587         if(okee("Remove Constraints")==0) return;
588         
589         /* find active */
590         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
591                 if(arm->layer & pchan->bone->layer) {
592                         if(pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
593                                 free_constraints(&pchan->constraints);
594                                 pchan->constflag= 0;
595                         }
596                 }
597         }
598         
599         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);   // and all its relations
600         
601         allqueue (REDRAWVIEW3D, 0);
602         allqueue (REDRAWBUTSOBJECT, 0);
603         allqueue (REDRAWOOPS, 0);
604         
605         BIF_undo_push("Remove Constraint(s)");
606         
607 }
608
609
610 void pose_copy_menu(void)
611 {
612         Object *ob= OBACT;
613         bArmature *arm= ob->data;
614         bPoseChannel *pchan, *pchanact;
615         short nr;
616         int i=0;
617         
618         /* paranoia checks */
619         if(!ob && !ob->pose) return;
620         if(ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
621         
622         /* find active */
623         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
624                 if(pchan->bone->flag & BONE_ACTIVE) break;
625         }
626         
627         if(pchan==NULL) return;
628         
629         if(pose_has_protected_selected(ob, 1))
630                 return;
631
632         pchanact= pchan;
633         
634         i= BLI_countlist(&(pchanact->constraints)); /* if there are 24 or less, allow for the user to select constraints */
635         if (i<25)
636                 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");
637         else
638                 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");
639         
640         if(nr==-1) return;
641         if(nr!=5)  {
642                 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
643                         if(     (arm->layer & pchan->bone->layer) &&
644                                 (pchan->bone->flag & BONE_SELECTED) &&
645                                 (pchan!=pchanact)
646                         ) {
647                                 switch (nr) {
648                                         case 1: /* Local Location */
649                                                 VECCOPY(pchan->loc, pchanact->loc);
650                                                 break;
651                                         case 2: /* Local Rotation */
652                                                 QUATCOPY(pchan->quat, pchanact->quat);
653                                                 break;
654                                         case 3: /* Local Size */
655                                                 VECCOPY(pchan->size, pchanact->size);
656                                                 break;
657                                         case 4: /* All Constraints */
658                                         {
659                                                 free_constraints(&pchan->constraints);
660                                                 copy_constraints(&pchan->constraints, &pchanact->constraints);
661                                                 pchan->constflag = pchanact->constflag;
662                                                 
663                                                 if (ob->pose) {
664                                                         ob->pose->flag |= POSE_RECALC;
665                                                 }
666                                         }
667                                                 break;
668                                         case 6: /* Transform Locks */
669                                                 pchan->protectflag = pchanact->protectflag;
670                                                 break;
671                                         case 7: /* IK (DOF) settings */
672                                         {
673                                                 pchan->ikflag = pchanact->ikflag;
674                                                 VECCOPY(pchan->limitmin, pchanact->limitmin);
675                                                 VECCOPY(pchan->limitmax, pchanact->limitmax);
676                                                 VECCOPY(pchan->stiffness, pchanact->stiffness);
677                                                 pchan->ikstretch= pchanact->ikstretch;
678                                         }
679                                                 break;
680                                         case 8: /* Custom Bone Shape */
681                                                 pchan->custom = pchanact->custom;
682                                                 break;
683                                         case 9: /* Visual Location */
684                                                 armature_loc_pose_to_bone(pchan, pchanact->pose_mat[3], pchan->loc);
685                                                 break;
686                                         case 10: /* Visual Rotation */
687                                         {
688                                                 float delta_mat[4][4], quat[4];
689                                                 
690                                                 armature_mat_pose_to_bone(pchan, pchanact->pose_mat, delta_mat);
691                                                 Mat4ToQuat(delta_mat, quat);
692                                                 QUATCOPY(pchan->quat, quat);
693                                         }
694                                                 break;
695                                         case 11: /* Visual Size */
696                                         {
697                                                 float delta_mat[4][4], size[4];
698                                                 
699                                                 armature_mat_pose_to_bone(pchan, pchanact->pose_mat, delta_mat);
700                                                 Mat4ToSize(delta_mat, size);
701                                                 VECCOPY(pchan->size, size);
702                                         }
703                                 }
704                         }
705                 }
706         } else { /* constraints, optional */
707                 bConstraint *con, *con_back;
708                 int const_toggle[24];
709                 ListBase const_copy={0, 0};
710                 
711                 duplicatelist (&const_copy, &(pchanact->constraints));
712                 
713                 /* build the puplist of constraints */
714                 for (con = pchanact->constraints.first, i=0; con; con=con->next, i++){
715                         const_toggle[i]= 1;
716                         add_numbut(i, TOG|INT, con->name, 0, 0, &(const_toggle[i]), "");
717                 }
718                 
719                 if (!do_clever_numbuts("Select Constraints", i, REDRAW)) {
720                         BLI_freelistN(&const_copy);
721                         return;
722                 }
723                 
724                 /* now build a new listbase from the options selected */
725                 for (i=0, con=const_copy.first; con; i++) {
726                         if (!const_toggle[i]) {
727                                 con_back= con->next;
728                                 BLI_freelinkN(&const_copy, con);
729                                 con= con_back;
730                         } else {
731                                 con= con->next;
732                         }
733                 }
734                 
735                 /* Copy the temo listbase to the selected posebones */
736                 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
737                         if(     (arm->layer & pchan->bone->layer) &&
738                                 (pchan->bone->flag & BONE_SELECTED) &&
739                                 (pchan!=pchanact)
740                         ) {
741                                 free_constraints(&pchan->constraints);
742                                 copy_constraints(&pchan->constraints, &const_copy);
743                                 pchan->constflag = pchanact->constflag;
744                         }
745                 }
746                 BLI_freelistN(&const_copy);
747                 update_pose_constraint_flags(ob->pose); /* we could work out the flags but its simpler to do this */
748                 
749                 if (ob->pose) {
750                         ob->pose->flag |= POSE_RECALC;
751                 }
752         }
753         
754         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);   // and all its relations
755         
756         allqueue (REDRAWVIEW3D, 0);
757         allqueue (REDRAWBUTSOBJECT, 0);
758         allqueue (REDRAWOOPS, 0);
759         
760         BIF_undo_push("Copy Pose Attributes");
761         
762 }
763
764 /* ******************** copy/paste pose ********************** */
765
766 static bPose    *g_posebuf=NULL;
767
768 void free_posebuf(void) 
769 {
770         if (g_posebuf) {
771                 // was copied without constraints
772                 BLI_freelistN (&g_posebuf->chanbase);
773                 MEM_freeN (g_posebuf);
774         }
775         g_posebuf=NULL;
776 }
777
778 void copy_posebuf (void)
779 {
780         Object *ob= OBACT;
781
782         if (!ob || !ob->pose){
783                 error ("No Pose");
784                 return;
785         }
786
787         free_posebuf();
788         
789         set_pose_keys(ob);  // sets chan->flag to POSE_KEY if bone selected
790         copy_pose(&g_posebuf, ob->pose, 0);
791
792 }
793
794 void paste_posebuf (int flip)
795 {
796         Object *ob= OBACT;
797         bPoseChannel *chan, *pchan;
798         float eul[4];
799         char name[32];
800         
801         if (!ob || !ob->pose)
802                 return;
803
804         if (!g_posebuf){
805                 error ("Copy buffer is empty");
806                 return;
807         }
808         
809         /*
810         // disabled until protected bones in proxies follow the rules everywhere else!
811         if(pose_has_protected_selected(ob, 1))
812                 return;
813         */
814         
815         /* Safely merge all of the channels in this pose into
816         any existing pose */
817         for (chan=g_posebuf->chanbase.first; chan; chan=chan->next) {
818                 if (chan->flag & POSE_KEY) {
819                         BLI_strncpy(name, chan->name, sizeof(name));
820                         if (flip)
821                                 bone_flip_name (name, 0);               // 0 = don't strip off number extensions
822                                 
823                         /* only copy when channel exists, poses are not meant to add random channels to anymore */
824                         pchan= get_pose_channel(ob->pose, name);
825                         
826                         if (pchan) {
827                                 /* only loc rot size */
828                                 /* only copies transform info for the pose */
829                                 VECCOPY(pchan->loc, chan->loc);
830                                 VECCOPY(pchan->size, chan->size);
831                                 QUATCOPY(pchan->quat, chan->quat);
832                                 pchan->flag= chan->flag;
833                                 
834                                 if (flip) {
835                                         pchan->loc[0]*= -1;
836                                         
837                                         QuatToEul(pchan->quat, eul);
838                                         eul[1]*= -1;
839                                         eul[2]*= -1;
840                                         EulToQuat(eul, pchan->quat);
841                                 }
842                                 
843                                 if (autokeyframe_cfra_can_key(ob)) {
844                                         ID *id= &ob->id;
845                                         
846                                         /* Set keys on pose */
847                                         if (chan->flag & POSE_ROT) {
848                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_X, 0);
849                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_Y, 0);
850                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_Z, 0);
851                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_W, 0);
852                                         }
853                                         if (chan->flag & POSE_SIZE) {
854                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_SIZE_X, 0);
855                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_SIZE_Y, 0);
856                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_SIZE_Z, 0);
857                                         }
858                                         if (chan->flag & POSE_LOC) {
859                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_LOC_X, 0);
860                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_LOC_Y, 0);
861                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_LOC_Z, 0);
862                                         }
863                                         
864                                         /* clear any unkeyed tags */
865                                         if (chan->bone)
866                                                 chan->bone->flag &= ~BONE_UNKEYED;
867                                 }
868                                 else {
869                                         /* add unkeyed tags */
870                                         if (chan->bone)
871                                                 chan->bone->flag |= BONE_UNKEYED;
872                                 }
873                         }
874                 }
875         }
876
877         /* Update event for pose and deformation children */
878         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
879         
880         if (IS_AUTOKEY_ON) {
881                 remake_action_ipos(ob->action);
882                 allqueue(REDRAWIPO, 0);
883                 allqueue(REDRAWVIEW3D, 0);
884                 allqueue(REDRAWACTION, 0);              
885                 allqueue(REDRAWNLA, 0);
886         }
887         else {
888                 /* need to trick depgraph, action is not allowed to execute on pose */
889                 where_is_pose(ob);
890                 ob->recalc= 0;
891         }
892
893         BIF_undo_push("Paste Action Pose");
894 }
895
896 /* ********************************************** */
897
898 /* context weightpaint and deformer in posemode */
899 void pose_adds_vgroups(Object *meshobj, int heatweights)
900 {
901         extern VPaint Gwp;         /* from vpaint */
902         Object *poseobj= modifiers_isDeformedByArmature(meshobj);
903
904         if(poseobj==NULL || (poseobj->flag & OB_POSEMODE)==0) {
905                 error("The active object must have a deforming armature in pose mode");
906                 return;
907         }
908
909         add_verts_to_dgroups(meshobj, poseobj, heatweights, (Gwp.flag & VP_MIRROR_X));
910
911         if(heatweights)
912                 BIF_undo_push("Apply Bone Heat Weights to Vertex Groups");
913         else
914                 BIF_undo_push("Apply Bone Envelopes to Vertex Groups");
915
916         allqueue(REDRAWVIEW3D, 0);
917         allqueue(REDRAWBUTSEDIT, 0);
918         
919         // and all its relations
920         DAG_object_flush_update(G.scene, meshobj, OB_RECALC_DATA);
921 }
922
923 /* ********************************************** */
924
925 /* adds a new pose-group */
926 void pose_add_posegroup ()
927 {
928         Object *ob= OBACT;
929         bPose *pose= (ob) ? ob->pose : NULL;
930         bActionGroup *grp;
931         
932         if (ELEM(NULL, ob, ob->pose))
933                 return;
934         
935         grp= MEM_callocN(sizeof(bActionGroup), "PoseGroup");
936         strcpy(grp->name, "Group");
937         BLI_addtail(&pose->agroups, grp);
938         BLI_uniquename(&pose->agroups, grp, "Group", offsetof(bActionGroup, name), 32);
939         
940         pose->active_group= BLI_countlist(&pose->agroups);
941         
942         BIF_undo_push("Add Bone Group");
943         
944         allqueue(REDRAWBUTSEDIT, 0);
945         allqueue(REDRAWVIEW3D, 0);
946 }
947
948 /* Remove the active bone-group */
949 void pose_remove_posegroup ()
950 {
951         Object *ob= OBACT;
952         bPose *pose= (ob) ? ob->pose : NULL;
953         bActionGroup *grp = NULL;
954         bPoseChannel *pchan;
955         
956         /* sanity checks */
957         if (ELEM(NULL, ob, pose))
958                 return;
959         if (pose->active_group <= 0)
960                 return;
961         
962         /* get group to remove */
963         grp= BLI_findlink(&pose->agroups, pose->active_group-1);
964         if (grp) {
965                 /* firstly, make sure nothing references it */
966                 for (pchan= pose->chanbase.first; pchan; pchan= pchan->next) {
967                         if (pchan->agrp_index == pose->active_group)
968                                 pchan->agrp_index= 0;
969                 }
970                 
971                 /* now, remove it from the pose */
972                 BLI_freelinkN(&pose->agroups, grp);
973                 pose->active_group= 0;
974                 
975                 BIF_undo_push("Remove Bone Group");
976         }
977         
978         allqueue(REDRAWBUTSEDIT, 0);
979         allqueue(REDRAWVIEW3D, 0);
980 }
981
982 char *build_posegroups_menustr (bPose *pose, short for_pupmenu)
983 {
984         DynStr *pupds= BLI_dynstr_new();
985         bActionGroup *grp;
986         char *str;
987         char buf[16];
988         int i;
989         
990         /* add title first (and the "none" entry) */
991         BLI_dynstr_append(pupds, "Bone Group%t|");
992         if (for_pupmenu)
993                 BLI_dynstr_append(pupds, "Add New%x0|");
994         else
995                 BLI_dynstr_append(pupds, "BG: [None]%x0|");
996         
997         /* loop through groups, adding them */
998         for (grp= pose->agroups.first, i=1; grp; grp=grp->next, i++) {
999                 if (for_pupmenu == 0)
1000                         BLI_dynstr_append(pupds, "BG: ");
1001                 BLI_dynstr_append(pupds, grp->name);
1002                 
1003                 sprintf(buf, "%%x%d", i);
1004                 BLI_dynstr_append(pupds, buf);
1005                 
1006                 if (grp->next)
1007                         BLI_dynstr_append(pupds, "|");
1008         }
1009         
1010         /* convert to normal MEM_malloc'd string */
1011         str= BLI_dynstr_get_cstring(pupds);
1012         BLI_dynstr_free(pupds);
1013         
1014         return str;
1015 }
1016
1017 /* Assign selected pchans to the bone group that the user selects */
1018 void pose_assign_to_posegroup (short active)
1019 {
1020         Object *ob= OBACT;
1021         bArmature *arm= (ob) ? ob->data : NULL;
1022         bPose *pose= (ob) ? ob->pose : NULL;
1023         bPoseChannel *pchan;
1024         char *menustr;
1025         int nr;
1026         short done= 0;
1027         
1028         /* sanity checks */
1029         if (ELEM3(NULL, ob, pose, arm))
1030                 return;
1031
1032         /* get group to affect */
1033         if ((active==0) || (pose->active_group <= 0)) {
1034                 menustr= build_posegroups_menustr(pose, 1);
1035                 nr= pupmenu_col(menustr, 20);
1036                 MEM_freeN(menustr);
1037                 
1038                 if (nr < 0) 
1039                         return;
1040                 else if (nr == 0) {
1041                         /* add new - note: this does an undo push and sets active group */
1042                         pose_add_posegroup();
1043                 }
1044                 else
1045                         pose->active_group= nr;
1046         }
1047         
1048         /* add selected bones to group then */
1049         for (pchan= pose->chanbase.first; pchan; pchan= pchan->next) {
1050                 if ((pchan->bone->flag & BONE_SELECTED) && (pchan->bone->layer & arm->layer)) {
1051                         pchan->agrp_index= pose->active_group;
1052                         done= 1;
1053                 }
1054         }
1055         
1056         if (done)
1057                 BIF_undo_push("Add Bones To Group");
1058                 
1059         allqueue(REDRAWBUTSEDIT, 0);
1060         allqueue(REDRAWVIEW3D, 0);
1061 }
1062
1063 /* Remove selected pchans from their bone groups */
1064 void pose_remove_from_posegroups ()
1065 {
1066         Object *ob= OBACT;
1067         bArmature *arm= (ob) ? ob->data : NULL;
1068         bPose *pose= (ob) ? ob->pose : NULL;
1069         bPoseChannel *pchan;
1070         short done= 0;
1071         
1072         /* sanity checks */
1073         if (ELEM3(NULL, ob, pose, arm))
1074                 return;
1075         
1076         /* remove selected bones from their groups */
1077         for (pchan= pose->chanbase.first; pchan; pchan= pchan->next) {
1078                 if ((pchan->bone->flag & BONE_SELECTED) && (pchan->bone->layer & arm->layer)) {
1079                         if (pchan->agrp_index) {
1080                                 pchan->agrp_index= 0;
1081                                 done= 1;
1082                         }
1083                 }
1084         }
1085         
1086         if (done)
1087                 BIF_undo_push("Remove Bones From Groups");
1088                 
1089         allqueue(REDRAWBUTSEDIT, 0);
1090         allqueue(REDRAWVIEW3D, 0);
1091 }
1092
1093 /* Ctrl-G in 3D-View while in PoseMode */
1094 void pgroup_operation_with_menu (void)
1095 {
1096         Object *ob= OBACT;
1097         bArmature *arm= (ob) ? ob->data : NULL;
1098         bPose *pose= (ob) ? ob->pose : NULL;
1099         bPoseChannel *pchan= NULL;
1100         int mode;
1101         
1102         /* sanity checks */
1103         if (ELEM3(NULL, ob, pose, arm))
1104                 return;
1105         
1106         /* check that something is selected */
1107         for (pchan= pose->chanbase.first; pchan; pchan= pchan->next) {
1108                 if ((pchan->bone->flag & BONE_SELECTED) && (pchan->bone->layer & arm->layer)) 
1109                         break;
1110         }
1111         if (pchan == NULL)
1112                 return;
1113         
1114         /* get mode of action */
1115         if (pchan)
1116                 mode= pupmenu("Bone Groups%t|Add Selected to Active Group%x1|Add Selected to Group%x2|%|Remove Selected From Groups%x3|Remove Active Group%x4");
1117         else
1118                 mode= pupmenu("Bone Groups%t|Add New Group%x5|Remove Active Group%x4");
1119                 
1120         /* handle mode */
1121         switch (mode) {
1122                 case 1:
1123                         pose_assign_to_posegroup(1);
1124                         break;
1125                 case 2:
1126                         pose_assign_to_posegroup(0);
1127                         break;
1128                 case 5:
1129                         pose_add_posegroup();
1130                         break;
1131                 case 3:
1132                         pose_remove_from_posegroups();
1133                         break;
1134                 case 4:
1135                         pose_remove_posegroup();
1136                         break;
1137         }
1138 }
1139
1140 /* ********************************************** */
1141
1142 static short pose_select_same_group (Object *ob)
1143 {
1144         bPose *pose= (ob)? ob->pose : NULL;
1145         bArmature *arm= (ob)? ob->data : NULL;
1146         bPoseChannel *pchan, *chan;
1147         short changed= 0;
1148         
1149         if (ELEM3(NULL, ob, pose, arm))
1150                 return 0;
1151         
1152         /* loop in loop... bad and slow! */
1153         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1154                 if (arm->layer & pchan->bone->layer) {
1155                         if (pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
1156                                 
1157                                 /* only if group matches (and is not selected or current bone) */
1158                                 for (chan= ob->pose->chanbase.first; chan; chan= chan->next) {
1159                                         if (arm->layer & chan->bone->layer) {
1160                                                 if (pchan->agrp_index == chan->agrp_index) {
1161                                                         chan->bone->flag |= BONE_SELECTED;
1162                                                         changed= 1;
1163                                                 }
1164                                         }
1165                                 }
1166                                 
1167                         }
1168                 }
1169         }
1170         
1171         return changed;
1172 }
1173
1174 static short pose_select_same_layer (Object *ob)
1175 {
1176         bPose *pose= (ob)? ob->pose : NULL;
1177         bArmature *arm= (ob)? ob->data : NULL;
1178         bPoseChannel *pchan;
1179         short layers= 0, changed= 0;
1180         
1181         if (ELEM3(NULL, ob, pose, arm))
1182                 return 0;
1183         
1184         /* figure out what bones are selected */
1185         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1186                 if (arm->layer & pchan->bone->layer) {
1187                         if (pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
1188                                 layers |= pchan->bone->layer;
1189                         }
1190                 }
1191         }
1192         if (layers == 0) 
1193                 return 0;
1194                 
1195         /* select bones that are on same layers as layers flag */
1196         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1197                 if (arm->layer & pchan->bone->layer) {
1198                         if (layers & pchan->bone->layer) {
1199                                 pchan->bone->flag |= BONE_SELECTED;
1200                                 changed= 1;
1201                         }
1202                 }
1203         }
1204         
1205         return changed;
1206 }
1207
1208
1209 void pose_select_grouped (short nr)
1210 {
1211         short changed = 0;
1212         
1213         if (nr == 1)            changed= pose_select_same_group(OBACT);
1214         else if (nr == 2)       changed= pose_select_same_layer(OBACT);
1215         
1216         if (changed) {
1217                 countall();
1218                 allqueue(REDRAWVIEW3D, 0);
1219                 allqueue(REDRAWBUTSOBJECT, 0);
1220                 allqueue(REDRAWBUTSEDIT, 0);
1221                 allspace(REMAKEIPO, 0);
1222                 allqueue(REDRAWIPO, 0);
1223                 allqueue(REDRAWACTION, 0);
1224                 BIF_undo_push("Select Grouped");
1225         }
1226 }
1227
1228 /* Shift-G in 3D-View while in PoseMode */
1229 void pose_select_grouped_menu (void)
1230 {
1231         short nr;
1232         
1233         /* here we go */
1234         nr= pupmenu("Select Grouped%t|In Same Group%x1|In Same Layer%x2");
1235         pose_select_grouped(nr);
1236 }
1237
1238 /* ********************************************** */
1239
1240 /* context active object */
1241 void pose_flip_names(void)
1242 {
1243         Object *ob= OBACT;
1244         bArmature *arm= ob->data;
1245         bPoseChannel *pchan;
1246         char newname[32];
1247         
1248         /* paranoia checks */
1249         if(!ob && !ob->pose) return;
1250         if(ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
1251         
1252         if(pose_has_protected_selected(ob, 0))
1253                 return;
1254         
1255         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1256                 if(arm->layer & pchan->bone->layer) {
1257                         if(pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
1258                                 BLI_strncpy(newname, pchan->name, sizeof(newname));
1259                                 bone_flip_name(newname, 1);     // 1 = do strip off number extensions
1260                                 armature_bone_rename(ob->data, pchan->name, newname);
1261                         }
1262                 }
1263         }
1264         
1265         allqueue(REDRAWVIEW3D, 0);
1266         allqueue(REDRAWBUTSEDIT, 0);
1267         allqueue(REDRAWBUTSOBJECT, 0);
1268         allqueue (REDRAWACTION, 0);
1269         allqueue(REDRAWOOPS, 0);
1270         BIF_undo_push("Flip names");
1271 }
1272
1273 /* context active object */
1274 void pose_autoside_names(short axis)
1275 {
1276         Object *ob= OBACT;
1277         bArmature *arm= ob->data;
1278         bPoseChannel *pchan;
1279         char newname[32];
1280         
1281         /* paranoia checks */
1282         if (ELEM(NULL, ob, ob->pose)) return;
1283         if (ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
1284         
1285         if (pose_has_protected_selected(ob, 0))
1286                 return;
1287         
1288         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1289                 if(arm->layer & pchan->bone->layer) {
1290                         if(pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
1291                                 BLI_strncpy(newname, pchan->name, sizeof(newname));
1292                                 bone_autoside_name(newname, 1, axis, pchan->bone->head[axis], pchan->bone->tail[axis]);
1293                                 armature_bone_rename(ob->data, pchan->name, newname);
1294                         }
1295                 }
1296         }
1297         
1298         allqueue(REDRAWVIEW3D, 0);
1299         allqueue(REDRAWBUTSEDIT, 0);
1300         allqueue(REDRAWBUTSOBJECT, 0);
1301         allqueue(REDRAWACTION, 0);
1302         allqueue(REDRAWOOPS, 0);
1303         BIF_undo_push("Flip names");
1304 }
1305
1306 /* context active object, or weightpainted object with armature in posemode */
1307 void pose_activate_flipped_bone(void)
1308 {
1309         Object *ob= OBACT;
1310         bArmature *arm= ob->data;
1311         
1312         if(ob==NULL) return;
1313
1314         if(G.f & G_WEIGHTPAINT) {
1315                 ob= modifiers_isDeformedByArmature(ob);
1316         }
1317         if(ob && (ob->flag & OB_POSEMODE)) {
1318                 bPoseChannel *pchan, *pchanf;
1319                 
1320                 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1321                         if(arm->layer & pchan->bone->layer) {
1322                                 if(pchan->bone->flag & BONE_ACTIVE)
1323                                         break;
1324                         }
1325                 }
1326                 if(pchan) {
1327                         char name[32];
1328                         
1329                         BLI_strncpy(name, pchan->name, 32);
1330                         bone_flip_name(name, 1);        // 0 = do not strip off number extensions
1331                         
1332                         pchanf= get_pose_channel(ob->pose, name);
1333                         if(pchanf && pchanf!=pchan) {
1334                                 pchan->bone->flag &= ~(BONE_SELECTED|BONE_ACTIVE);
1335                                 pchanf->bone->flag |= (BONE_SELECTED|BONE_ACTIVE);
1336                         
1337                                 /* in weightpaint we select the associated vertex group too */
1338                                 if(G.f & G_WEIGHTPAINT) {
1339                                         vertexgroup_select_by_name(OBACT, name);
1340                                         DAG_object_flush_update(G.scene, OBACT, OB_RECALC_DATA);
1341                                 }
1342                                 
1343                                 select_actionchannel_by_name(ob->action, name, 1);
1344                                 
1345                                 allqueue(REDRAWVIEW3D, 0);
1346                                 allqueue(REDRAWACTION, 0);
1347                                 allqueue(REDRAWIPO, 0);         /* To force action/constraint ipo update */
1348                                 allqueue(REDRAWBUTSEDIT, 0);
1349                                 allqueue(REDRAWBUTSOBJECT, 0);
1350                                 allqueue(REDRAWOOPS, 0);
1351                         }                       
1352                 }
1353         }
1354 }
1355
1356 /* This function pops up the move-to-layer popup widgets when the user
1357  * presses either SHIFT-MKEY or MKEY in PoseMode OR EditMode (for Armatures)
1358  */
1359 void pose_movetolayer(void)
1360 {
1361         Object *ob= OBACT;
1362         bArmature *arm;
1363         short lay= 0;
1364         
1365         if (ob==NULL) return;
1366         arm= ob->data;
1367         
1368         if (G.qual & LR_SHIFTKEY) {
1369                 /* armature layers */
1370                 lay= arm->layer;
1371                 if ( movetolayer_short_buts(&lay, "Armature Layers")==0 ) return;
1372                 if (lay==0) return;
1373                 arm->layer= lay;
1374                 if(ob->pose)
1375                         ob->pose->proxy_layer= lay;
1376                 
1377                 allqueue(REDRAWVIEW3D, 0);
1378                 allqueue(REDRAWACTION, 0);
1379                 allqueue(REDRAWBUTSEDIT, 0);
1380         }
1381         else if (G.obedit) {
1382                 /* the check for editbone layer moving needs to occur before posemode one to work */
1383                 EditBone *ebo;
1384                 EditBone *flipBone;
1385                 
1386                 for (ebo= G.edbo.first; ebo; ebo= ebo->next) {
1387                         if (arm->layer & ebo->layer) {
1388                                 if (ebo->flag & BONE_SELECTED)
1389                                         lay |= ebo->layer;
1390                         }
1391                 }
1392                 if (lay==0) return;
1393                 
1394                 if ( movetolayer_short_buts(&lay, "Bone Layers")==0 ) return;
1395                 if (lay==0) return;
1396                 
1397                 for (ebo= G.edbo.first; ebo; ebo= ebo->next) {
1398                         if (arm->layer & ebo->layer) {
1399                                 if (ebo->flag & BONE_SELECTED) {
1400                                         ebo->layer= lay;
1401                                         if (arm->flag & ARM_MIRROR_EDIT) {
1402                                                 flipBone = armature_bone_get_mirrored(ebo);
1403                                                 if (flipBone)
1404                                                         flipBone->layer = lay;
1405                                         }
1406                                 }
1407                         }
1408                 }
1409                 
1410                 BIF_undo_push("Move Bone Layer");
1411                 allqueue(REDRAWVIEW3D, 0);
1412                 allqueue(REDRAWBUTSEDIT, 0);
1413         }
1414         else if (ob->flag & OB_POSEMODE) {
1415                 /* pose-channel layers */
1416                 bPoseChannel *pchan;
1417                 
1418                 if (pose_has_protected_selected(ob, 0))
1419                         return;
1420                 
1421                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1422                         if (arm->layer & pchan->bone->layer) {
1423                                 if (pchan->bone->flag & BONE_SELECTED)
1424                                         lay |= pchan->bone->layer;
1425                         }
1426                 }
1427                 if (lay==0) return;
1428                 
1429                 if ( movetolayer_short_buts(&lay, "Bone Layers")==0 ) return;
1430                 if (lay==0) return;
1431                 
1432                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1433                         if (arm->layer & pchan->bone->layer) {
1434                                 if (pchan->bone->flag & BONE_SELECTED)
1435                                         pchan->bone->layer= lay;
1436                         }
1437                 }
1438                 
1439                 BIF_undo_push("Move Bone Layer");
1440                 allqueue(REDRAWVIEW3D, 0);
1441                 allqueue(REDRAWACTION, 0);
1442                 allqueue(REDRAWBUTSEDIT, 0);
1443         }
1444 }
1445
1446
1447 /* for use with pose_relax only */
1448 static int pose_relax_icu(struct IpoCurve *icu, float framef, float *val, float *frame_prev, float *frame_next)
1449 {
1450         if (!icu) {
1451                 return 0;
1452         } 
1453         else {
1454                 BezTriple *bezt = icu->bezt;
1455                 
1456                 BezTriple *bezt_prev=NULL, *bezt_next=NULL;
1457                 float w1, w2, wtot;
1458                 int i;
1459                 
1460                 for (i=0; i < icu->totvert; i++, bezt++) {
1461                         if (bezt->vec[1][0] < framef - 0.5) {
1462                                 bezt_prev = bezt;
1463                         } else {
1464                                 break;
1465                         }
1466                 }
1467                 
1468                 if (bezt_prev==NULL) return 0;
1469                 
1470                 /* advance to the next, dont need to advance i */
1471                 bezt = bezt_prev+1;
1472                 
1473                 for (; i < icu->totvert; i++, bezt++) {
1474                         if (bezt->vec[1][0] > framef + 0.5) {
1475                                 bezt_next = bezt;
1476                                                 break;
1477                         }
1478                 }
1479                 
1480                 if (bezt_next==NULL) return 0;
1481         
1482                 if (val) {
1483                         w1 = framef - bezt_prev->vec[1][0];
1484                         w2 = bezt_next->vec[1][0] - framef;
1485                         wtot = w1 + w2;
1486                         w1=w1/wtot;
1487                         w2=w2/wtot;
1488 #if 0
1489                         val = (bezt_prev->vec[1][1] * w2) + (bezt_next->vec[1][1] * w1);
1490 #else
1491                         /* apply the value with a hard coded 6th */
1492                         *val = (((bezt_prev->vec[1][1] * w2) + (bezt_next->vec[1][1] * w1)) + (*val * 5.0f)) / 6.0f;
1493 #endif
1494                 }
1495                 
1496                 if (frame_prev) *frame_prev = bezt_prev->vec[1][0];
1497                 if (frame_next) *frame_next = bezt_next->vec[1][0];
1498                 
1499                 return 1;
1500         }
1501 }
1502
1503 void pose_relax()
1504 {
1505         Object *ob = OBACT;
1506         bPose *pose;
1507         bAction *act;
1508         bArmature *arm;
1509         
1510         IpoCurve *icu_w, *icu_x, *icu_y, *icu_z;
1511         
1512         bPoseChannel *pchan;
1513         bActionChannel *achan;
1514         float framef = F_CFRA;
1515         float frame_prev, frame_next;
1516         float quat_prev[4], quat_next[4], quat_interp[4], quat_orig[4];
1517         
1518         int do_scale = 0;
1519         int do_loc = 0;
1520         int do_quat = 0;
1521         int flag = 0;
1522         int do_x, do_y, do_z;
1523         
1524         if (!ob) return;
1525         
1526         pose = ob->pose;
1527         act = ob->action;
1528         arm = (bArmature *)ob->data;
1529         
1530         if (!pose || !act || !arm) return;
1531         
1532         for (pchan=pose->chanbase.first; pchan; pchan= pchan->next) {
1533                 
1534                 pchan->bone->flag &= ~BONE_TRANSFORM;
1535                 
1536                 if (pchan->bone->layer & arm->layer) {
1537                         if (pchan->bone->flag & BONE_SELECTED) {
1538                                 /* do we have an ipo curve? */
1539                                 achan= get_action_channel(act, pchan->name);
1540                                 
1541                                 if (achan && achan->ipo) {
1542                                         /*calc_ipo(achan->ipo, ctime);*/
1543                                         
1544                                         do_x = pose_relax_icu(find_ipocurve(achan->ipo, AC_LOC_X), framef, &pchan->loc[0], NULL, NULL);
1545                                         do_y = pose_relax_icu(find_ipocurve(achan->ipo, AC_LOC_Y), framef, &pchan->loc[1], NULL, NULL);
1546                                         do_z = pose_relax_icu(find_ipocurve(achan->ipo, AC_LOC_Z), framef, &pchan->loc[2], NULL, NULL);
1547                                         do_loc += do_x + do_y + do_z;
1548                                         
1549                                         do_x = pose_relax_icu(find_ipocurve(achan->ipo, AC_SIZE_X), framef, &pchan->size[0], NULL, NULL);
1550                                         do_y = pose_relax_icu(find_ipocurve(achan->ipo, AC_SIZE_Y), framef, &pchan->size[1], NULL, NULL);
1551                                         do_z = pose_relax_icu(find_ipocurve(achan->ipo, AC_SIZE_Z), framef, &pchan->size[2], NULL, NULL);
1552                                         do_scale += do_x + do_y + do_z;
1553                                                 
1554                                         if(     ((icu_w = find_ipocurve(achan->ipo, AC_QUAT_W))) &&
1555                                                 ((icu_x = find_ipocurve(achan->ipo, AC_QUAT_X))) &&
1556                                                 ((icu_y = find_ipocurve(achan->ipo, AC_QUAT_Y))) &&
1557                                                 ((icu_z = find_ipocurve(achan->ipo, AC_QUAT_Z))) )
1558                                         {
1559                                                 /* use the quatw keyframe as a basis for others */
1560                                                 if (pose_relax_icu(icu_w, framef, NULL, &frame_prev, &frame_next)) {
1561                                                         /* get 2 quats */
1562                                                         quat_prev[0] = eval_icu(icu_w, frame_prev);
1563                                                         quat_prev[1] = eval_icu(icu_x, frame_prev);
1564                                                         quat_prev[2] = eval_icu(icu_y, frame_prev);
1565                                                         quat_prev[3] = eval_icu(icu_z, frame_prev);
1566                                                         
1567                                                         quat_next[0] = eval_icu(icu_w, frame_next);
1568                                                         quat_next[1] = eval_icu(icu_x, frame_next);
1569                                                         quat_next[2] = eval_icu(icu_y, frame_next);
1570                                                         quat_next[3] = eval_icu(icu_z, frame_next);
1571                                                         
1572 #if 0
1573                                                         /* apply the setting, completely smooth */
1574                                                         QuatInterpol(pchan->quat, quat_prev, quat_next, (framef-frame_prev) / (frame_next-frame_prev) );
1575 #else
1576                                                         /* tricky interpolation */
1577                                                         QuatInterpol(quat_interp, quat_prev, quat_next, (framef-frame_prev) / (frame_next-frame_prev) );
1578                                                         QUATCOPY(quat_orig, pchan->quat);
1579                                                         QuatInterpol(pchan->quat, quat_orig, quat_interp, 1.0f/6.0f);
1580                                                         /* done */
1581 #endif
1582                                                         do_quat++;
1583                                                 }
1584                                         }
1585                                         
1586                                         /* apply BONE_TRANSFORM tag so that autokeying will pick it up */
1587                                         pchan->bone->flag |= BONE_TRANSFORM;
1588                                 }
1589                         }
1590                 }
1591         }
1592         
1593         ob->pose->flag |= (POSE_LOCKED|POSE_DO_UNLOCK);
1594         
1595         /* do auto-keying */
1596         if (do_loc)             flag |= TFM_TRANSLATION;
1597         if (do_scale)   flag |= TFM_RESIZE;
1598         if (do_quat)    flag |= TFM_ROTATION;
1599         autokeyframe_pose_cb_func(ob, flag, 0);
1600          
1601         /* clear BONE_TRANSFORM flags */
1602         for (pchan=pose->chanbase.first; pchan; pchan= pchan->next)
1603                 pchan->bone->flag &= ~ BONE_TRANSFORM;
1604         
1605         /* do depsgraph flush */
1606         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
1607         BIF_undo_push("Relax Pose");
1608 }
1609
1610 /* for use in insertkey, ensure rotation goes other way around */
1611 void pose_flipquats(void)
1612 {
1613         Object *ob = OBACT;
1614         bArmature *arm= ob->data;
1615         bPoseChannel *pchan;
1616         
1617         if(ob->pose==NULL)
1618                 return;
1619         
1620         /* find sel bones */
1621         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1622                 if(pchan->bone && (pchan->bone->flag & BONE_SELECTED) && (pchan->bone->layer & arm->layer)) {
1623                         /* quaternions have 720 degree range */
1624                         pchan->quat[0]= -pchan->quat[0];
1625                         pchan->quat[1]= -pchan->quat[1];
1626                         pchan->quat[2]= -pchan->quat[2];
1627                         pchan->quat[3]= -pchan->quat[3];
1628                 }
1629         }
1630                         
1631 }
1632