2.4x Bugfix - #18188: "Clear user transform" does not work as described
[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_keyframing.h"
73 #include "BIF_poseobject.h"
74 #include "BIF_space.h"
75 #include "BIF_toolbox.h"
76 #include "BIF_screen.h"
77
78 #include "BDR_editobject.h"
79
80 #include "BSE_edit.h"
81 #include "BSE_editipo.h"
82 #include "BSE_trans_types.h"
83
84 #include "mydevice.h"
85 #include "blendef.h"
86 #include "transform.h"
87
88 #include "BIF_transform.h" /* for autokey TFM_TRANSLATION, etc */
89
90 void enter_posemode(void)
91 {
92         Base *base;
93         Object *ob;
94         bArmature *arm;
95         
96         if(G.scene->id.lib) return;
97         base= BASACT;
98         if(base==NULL) return;
99         
100         ob= base->object;
101         
102         if (ob->id.lib){
103                 error ("Can't pose libdata");
104                 return;
105         }
106
107         switch (ob->type){
108         case OB_ARMATURE:
109                 arm= get_armature(ob);
110                 if( arm==NULL ) return;
111                 
112                 ob->flag |= OB_POSEMODE;
113                 base->flag= ob->flag;
114                 
115                 allqueue(REDRAWHEADERS, 0);     
116                 allqueue(REDRAWBUTSALL, 0);     
117                 allqueue(REDRAWOOPS, 0);
118                 allqueue(REDRAWVIEW3D, 0);
119                 break;
120         default:
121                 return;
122         }
123
124         if (G.obedit) exit_editmode(EM_FREEDATA|EM_WAITCURSOR);
125         G.f &= ~(G_VERTEXPAINT | G_TEXTUREPAINT | G_WEIGHTPAINT | G_SCULPTMODE);
126 }
127
128 void set_pose_keys (Object *ob)
129 {
130         bArmature *arm= ob->data;
131         bPoseChannel *chan;
132
133         if (ob->pose){
134                 for (chan=ob->pose->chanbase.first; chan; chan=chan->next){
135                         Bone *bone= chan->bone;
136                         if(bone && (bone->flag & BONE_SELECTED) && (arm->layer & bone->layer)) {
137                                 chan->flag |= POSE_KEY;         
138                         }
139                         else {
140                                 chan->flag &= ~POSE_KEY;
141                         }
142                 }
143         }
144 }
145
146
147 void exit_posemode(void)
148 {
149         Object *ob= OBACT;
150         Base *base= BASACT;
151
152         if(ob==NULL) return;
153         
154         ob->flag &= ~OB_POSEMODE;
155         base->flag= ob->flag;
156         
157         countall();
158         allqueue(REDRAWVIEW3D, 0);
159         allqueue(REDRAWOOPS, 0);
160         allqueue(REDRAWHEADERS, 0);     
161         allqueue(REDRAWBUTSALL, 0);     
162
163         scrarea_queue_headredraw(curarea);
164 }
165
166 /* called by buttons to find a bone to display/edit values for */
167 bPoseChannel *get_active_posechannel (Object *ob)
168 {
169         bArmature *arm= ob->data;
170         bPoseChannel *pchan;
171         
172         if ELEM(NULL, ob, ob->pose)
173                 return NULL;
174         
175         /* find active */
176         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
177                 if(pchan->bone && (pchan->bone->flag & BONE_ACTIVE) && (pchan->bone->layer & arm->layer))
178                         return pchan;
179         }
180         
181         return NULL;
182 }
183
184 /* if a selected or active bone is protected, throw error (oonly if warn==1) and return 1 */
185 /* only_selected==1 : the active bone is allowed to be protected */
186 static short pose_has_protected_selected(Object *ob, short only_selected, short warn)
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                         if (warn) 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         /* No need to check if constraint is active (has influence),
217          * since all constraints with CONSTRAINT_IK_AUTO are active */
218         for(con= pchan->constraints.first; con; con= con->next) {
219                 if(con->type==CONSTRAINT_TYPE_KINEMATIC) {
220                         bKinematicConstraint *data= con->data;
221                         if((data->flag & CONSTRAINT_IK_AUTO)==0)
222                                 return 1;
223                 }
224         }
225         for(bone= pchan->bone->childbase.first; bone; bone= bone->next) {
226                 pchan= get_pose_channel(ob->pose, bone->name);
227                 if(pchan && pose_channel_in_IK_chain(ob, pchan))
228                         return 1;
229         }
230         return 0;
231 }
232
233 /* ********************************************** */
234
235 /* For the object with pose/action: create path curves for selected bones 
236  * This recalculates the WHOLE path within the pchan->pathsf and pchan->pathef range
237  */
238 void pose_calculate_path(Object *ob)
239 {
240         bArmature *arm;
241         bPoseChannel *pchan;
242         Base *base;
243         float *fp;
244         int cfra;
245         int sfra, efra;
246         
247         if (ob==NULL || ob->pose==NULL)
248                 return;
249         arm= ob->data;
250         
251         /* version patch for older files here (do_versions patch too complicated) */
252         if ((arm->pathsf == 0) || (arm->pathef == 0)) {
253                 arm->pathsf = SFRA;
254                 arm->pathef = EFRA;
255         }
256         if (arm->pathsize == 0) {
257                 arm->pathsize = 1;
258         }
259         
260         /* set frame values */
261         cfra= CFRA;
262         sfra = arm->pathsf;
263         efra = arm->pathef;
264         if (efra <= sfra) {
265                 error("Can't calculate paths when pathlen <= 0");
266                 return;
267         }
268         
269         waitcursor(1);
270         
271         /* hack: for unsaved files, set OB_RECALC so that paths can get calculated */
272         if ((ob->recalc & OB_RECALC)==0) {
273                 ob->recalc |= OB_RECALC;
274                 DAG_object_update_flags(G.scene, ob, screen_view3d_layers());
275         }
276         else
277                 DAG_object_update_flags(G.scene, ob, screen_view3d_layers());
278         
279         
280         /* malloc the path blocks */
281         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
282                 if ((pchan->bone) && (pchan->bone->flag & BONE_SELECTED)) {
283                         if (arm->layer & pchan->bone->layer) {
284                                 pchan->pathlen= efra-sfra+1;
285                                 pchan->pathsf= sfra;
286                                 pchan->pathef= efra+1;
287                                 if (pchan->path)
288                                         MEM_freeN(pchan->path);
289                                 pchan->path= MEM_callocN(3*pchan->pathlen*sizeof(float), "pchan path");
290                         }
291                 }
292         }
293         
294         for (CFRA=sfra; CFRA<=efra; CFRA++) {
295                 /* do all updates */
296                 for (base= FIRSTBASE; base; base= base->next) {
297                         if (base->object->recalc) {
298                                 int temp= base->object->recalc;
299                                 object_handle_update(base->object);
300                                 base->object->recalc= temp;
301                         }
302                 }
303                 
304                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
305                         if ((pchan->bone) && (pchan->bone->flag & BONE_SELECTED)) {
306                                 if (arm->layer & pchan->bone->layer) {
307                                         if (pchan->path) {
308                                                 fp= pchan->path+3*(CFRA-sfra);
309                                                 
310                                                 if (arm->pathflag & ARM_PATH_HEADS) { 
311                                                         VECCOPY(fp, pchan->pose_head);
312                                                 }
313                                                 else {
314                                                         VECCOPY(fp, pchan->pose_tail);
315                                                 }
316                                                 
317                                                 Mat4MulVecfl(ob->obmat, fp);
318                                         }
319                                 }
320                         }
321                 }
322         }
323         
324         waitcursor(0);
325         
326         CFRA= cfra;
327         allqueue(REDRAWVIEW3D, 0);      /* recalc tags are still there */
328         allqueue(REDRAWBUTSEDIT, 0);
329 }
330
331 /* For the object with pose/action: update paths for those that have got them
332  * This should selectively update paths that exist...
333  */
334 void pose_recalculate_paths(Object *ob)
335 {
336         bArmature *arm;
337         bPoseChannel *pchan;
338         Base *base;
339         float *fp;
340         int cfra;
341         int sfra, efra;
342         
343         if (ob==NULL || ob->pose==NULL)
344                 return;
345         arm= ob->data;
346         
347         /* set frame values */
348         cfra = CFRA;
349         sfra = efra = cfra; 
350         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
351                 if ((pchan->bone) && (arm->layer & pchan->bone->layer)) {
352                         if (pchan->path) {
353                                 /* if the pathsf and pathef aren't initialised, abort! */
354                                 if (ELEM(0, pchan->pathsf, pchan->pathef))      
355                                         return;
356                                 
357                                 /* try to increase area to do (only as much as needed) */
358                                 sfra= MIN2(sfra, pchan->pathsf);
359                                 efra= MAX2(efra, pchan->pathef);
360                         }
361                 }
362         }
363         if (efra <= sfra) return;
364         
365         waitcursor(1);
366         
367         /* hack: for unsaved files, set OB_RECALC so that paths can get calculated */
368         if ((ob->recalc & OB_RECALC)==0) {
369                 ob->recalc |= OB_RECALC;
370                 DAG_object_update_flags(G.scene, ob, screen_view3d_layers());
371         }
372         else
373                 DAG_object_update_flags(G.scene, ob, screen_view3d_layers());
374         
375         for (CFRA=sfra; CFRA<=efra; CFRA++) {
376                 /* do all updates */
377                 for (base= FIRSTBASE; base; base= base->next) {
378                         if (base->object->recalc) {
379                                 int temp= base->object->recalc;
380                                 object_handle_update(base->object);
381                                 base->object->recalc= temp;
382                         }
383                 }
384                 
385                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
386                         if ((pchan->bone) && (arm->layer & pchan->bone->layer)) {
387                                 if (pchan->path) {
388                                         /* only update if:
389                                          *      - in range of this pchan's existing path
390                                          *      - ... insert evil filtering/optimising conditions here...
391                                          */
392                                         if (IN_RANGE(CFRA, pchan->pathsf, pchan->pathef)) {
393                                                 fp= pchan->path+3*(CFRA-sfra);
394                                                 
395                                                 if (arm->pathflag & ARM_PATH_HEADS) { 
396                                                         VECCOPY(fp, pchan->pose_head);
397                                                 }
398                                                 else {
399                                                         VECCOPY(fp, pchan->pose_tail);
400                                                 }
401                                                 
402                                                 Mat4MulVecfl(ob->obmat, fp);
403                                         }
404                                 }
405                         }
406                 }
407         }
408         
409         waitcursor(0);
410         
411         CFRA= cfra;
412         ob->pose->flag &= ~POSE_RECALCPATHS;
413         allqueue(REDRAWVIEW3D, 0);      /* recalc tags are still there */
414         allqueue(REDRAWBUTSEDIT, 0);
415 }
416
417 /* for the object with pose/action: clear path curves for selected bones only */
418 void pose_clear_paths(Object *ob)
419 {
420         bPoseChannel *pchan;
421         
422         if (ob==NULL || ob->pose==NULL)
423                 return;
424         
425         /* free the path blocks */
426         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
427                 if ((pchan->bone) && (pchan->bone->flag & BONE_SELECTED)) {
428                         if (pchan->path) {
429                                 MEM_freeN(pchan->path);
430                                 pchan->path= NULL;
431                         }
432                 }
433         }
434         
435         allqueue(REDRAWVIEW3D, 0);
436 }
437
438
439
440 void pose_select_constraint_target(void)
441 {
442         Object *ob= OBACT;
443         bArmature *arm= ob->data;
444         bPoseChannel *pchan;
445         bConstraint *con;
446         
447         /* paranoia checks */
448         if (!ob && !ob->pose) return;
449         if (ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
450         
451         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
452                 if (arm->layer & pchan->bone->layer) {
453                         if (pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
454                                 for (con= pchan->constraints.first; con; con= con->next) {
455                                         bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
456                                         ListBase targets = {NULL, NULL};
457                                         bConstraintTarget *ct;
458                                         
459                                         if (cti && cti->get_constraint_targets) {
460                                                 cti->get_constraint_targets(con, &targets);
461                                                 
462                                                 for (ct= targets.first; ct; ct= ct->next) {
463                                                         if ((ct->tar == ob) && (ct->subtarget[0])) {
464                                                                 bPoseChannel *pchanc= get_pose_channel(ob->pose, ct->subtarget);
465                                                                 if(pchanc)
466                                                                         pchanc->bone->flag |= BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL;
467                                                         }
468                                                 }
469                                                 
470                                                 if (cti->flush_constraint_targets)
471                                                         cti->flush_constraint_targets(con, &targets, 1);
472                                         }
473                                 }
474                         }
475                 }
476         }
477         
478         allqueue (REDRAWVIEW3D, 0);
479         allqueue (REDRAWBUTSOBJECT, 0);
480         allqueue (REDRAWOOPS, 0);
481         
482         BIF_undo_push("Select constraint target");
483
484 }
485
486 void pose_select_hierarchy(short direction, short add_to_sel)
487 {
488         Object *ob= OBACT;
489         bArmature *arm= ob->data;
490         bPoseChannel *pchan;
491         Bone *curbone, *pabone, *chbone;
492         
493         /* paranoia checks */
494         if (!ob && !ob->pose) return;
495         if (ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
496         
497         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
498                 curbone= pchan->bone;
499                 
500                 if (arm->layer & curbone->layer) {
501                         if (curbone->flag & (BONE_ACTIVE)) {
502                                 if (direction == BONE_SELECT_PARENT) {
503                                 
504                                         if (pchan->parent == NULL) continue;
505                                         else pabone= pchan->parent->bone;
506                                         
507                                         if ((arm->layer & pabone->layer) && !(pabone->flag & BONE_HIDDEN_P)) {
508                                                 
509                                                 if (!add_to_sel) curbone->flag &= ~BONE_SELECTED;
510                                                 curbone->flag &= ~BONE_ACTIVE;
511                                                 pabone->flag |= (BONE_ACTIVE|BONE_SELECTED);
512                                                 
513                                                 select_actionchannel_by_name (ob->action, pchan->name, 0);
514                                                 select_actionchannel_by_name (ob->action, pchan->parent->name, 1);
515                                                 break;
516                                         }
517                                 } else { // BONE_SELECT_CHILD
518                                 
519                                         if (pchan->child == NULL) continue;
520                                         else chbone = pchan->child->bone;
521                                         
522                                         if ((arm->layer & chbone->layer) && !(chbone->flag & BONE_HIDDEN_P)) {
523                                         
524                                                 if (!add_to_sel) curbone->flag &= ~BONE_SELECTED;
525                                                 curbone->flag &= ~BONE_ACTIVE;
526                                                 chbone->flag |= (BONE_ACTIVE|BONE_SELECTED);
527                                                 
528                                                 select_actionchannel_by_name (ob->action, pchan->name, 0);
529                                                 select_actionchannel_by_name (ob->action, pchan->child->name, 1);
530                                                 break;
531                                         }
532                                 }
533                         }
534                 }
535         }
536         
537         allqueue (REDRAWVIEW3D, 0);
538         allqueue (REDRAWBUTSOBJECT, 0);
539         allqueue (REDRAWOOPS, 0);
540         
541         if (direction==BONE_SELECT_PARENT)
542                 BIF_undo_push("Select pose bone parent");
543         if (direction==BONE_SELECT_CHILD)
544                 BIF_undo_push("Select pose bone child");
545 }
546
547 /* context: active channel */
548 void pose_special_editmenu(void)
549 {
550         Object *ob= OBACT;
551         short nr;
552         
553         /* paranoia checks */
554         if(!ob && !ob->pose) return;
555         if(ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
556         
557         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");
558         if(nr==1) {
559                 pose_select_constraint_target();
560         }
561         else if(nr==2) {
562                 pose_flip_names();
563         }
564         else if(nr==3) {
565                 pose_calculate_path(ob);
566         }
567         else if(nr==4) {
568                 pose_clear_paths(ob);
569         }
570         else if(nr==5) {
571                 pose_clear_user_transforms();
572         }
573         else if(nr==6) {
574                 pose_relax();
575         }
576         else if(ELEM3(nr, 7, 8, 9)) {
577                 pose_autoside_names(nr-7);
578         }
579 }
580
581 void pose_add_IK(void)
582 {
583         Object *ob= OBACT;
584         
585         /* paranoia checks */
586         if(!ob && !ob->pose) return;
587         if(ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
588         
589         add_constraint(1);      /* 1 means only IK */
590 }
591
592 /* context: all selected channels */
593 void pose_clear_IK(void)
594 {
595         Object *ob= OBACT;
596         bArmature *arm= ob->data;
597         bPoseChannel *pchan;
598         bConstraint *con;
599         bConstraint *next;
600         
601         /* paranoia checks */
602         if(!ob && !ob->pose) return;
603         if(ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
604         
605         if(pose_has_protected_selected(ob, 0, 1))
606                 return;
607         
608         if(okee("Remove IK constraint(s)")==0) return;
609
610         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
611                 if(arm->layer & pchan->bone->layer) {
612                         if(pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
613                                 
614                                 for(con= pchan->constraints.first; con; con= next) {
615                                         next= con->next;
616                                         if(con->type==CONSTRAINT_TYPE_KINEMATIC) {
617                                                 BLI_remlink(&pchan->constraints, con);
618                                                 free_constraint_data(con);
619                                                 MEM_freeN(con);
620                                         }
621                                 }
622                                 pchan->constflag &= ~(PCHAN_HAS_IK|PCHAN_HAS_TARGET);
623                         }
624                 }
625         }
626         
627         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);   // and all its relations
628         
629         allqueue (REDRAWVIEW3D, 0);
630         allqueue (REDRAWBUTSOBJECT, 0);
631         allqueue (REDRAWOOPS, 0);
632         
633         BIF_undo_push("Remove IK constraint(s)");
634 }
635
636 void pose_clear_constraints(void)
637 {
638         Object *ob= OBACT;
639         bArmature *arm= ob->data;
640         bPoseChannel *pchan;
641         
642         /* paranoia checks */
643         if(!ob && !ob->pose) return;
644         if(ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
645         
646         if(pose_has_protected_selected(ob, 0, 1))
647                 return;
648         
649         if(okee("Remove Constraints")==0) return;
650         
651         /* find active */
652         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
653                 if(arm->layer & pchan->bone->layer) {
654                         if(pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
655                                 free_constraints(&pchan->constraints);
656                                 pchan->constflag= 0;
657                         }
658                 }
659         }
660         
661         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);   // and all its relations
662         
663         allqueue (REDRAWVIEW3D, 0);
664         allqueue (REDRAWBUTSOBJECT, 0);
665         allqueue (REDRAWOOPS, 0);
666         
667         BIF_undo_push("Remove Constraint(s)");
668         
669 }
670
671
672 void pose_copy_menu(void)
673 {
674         Object *ob= OBACT;
675         bArmature *arm= ob->data;
676         bPoseChannel *pchan, *pchanact;
677         short nr=0;
678         int i=0;
679         
680         /* paranoia checks */
681         if (ELEM(NULL, ob, ob->pose)) return;
682         if ((ob==G.obedit) || (ob->flag & OB_POSEMODE)==0) return;
683         
684         /* find active */
685         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
686                 if (pchan->bone->flag & BONE_ACTIVE) 
687                         break;
688         }
689         
690         if (pchan==NULL) return;
691         pchanact= pchan;
692         
693         /* if proxy-protected bones selected, some things (such as locks + displays) shouldn't be changable, 
694          * but for constraints (just add local constraints)
695          */
696         if (pose_has_protected_selected(ob, 1, 0)) {
697                 i= BLI_countlist(&(pchanact->constraints)); /* if there are 24 or less, allow for the user to select constraints */
698                 if (i < 25)
699                         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");
700                 else
701                         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");
702         }
703         else {
704                 i= BLI_countlist(&(pchanact->constraints)); /* if there are 24 or less, allow for the user to select constraints */
705                 if (i < 25)
706                         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");
707                 else
708                         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");
709         }
710         
711         if (nr <= 0) 
712                 return;
713         
714         if (nr != 5)  {
715                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
716                         if ( (arm->layer & pchan->bone->layer) &&
717                                  (pchan->bone->flag & BONE_SELECTED) &&
718                                  (pchan != pchanact) ) 
719                         {
720                                 switch (nr) {
721                                         case 1: /* Local Location */
722                                                 VECCOPY(pchan->loc, pchanact->loc);
723                                                 break;
724                                         case 2: /* Local Rotation */
725                                                 QUATCOPY(pchan->quat, pchanact->quat);
726                                                 break;
727                                         case 3: /* Local Size */
728                                                 VECCOPY(pchan->size, pchanact->size);
729                                                 break;
730                                         case 4: /* All Constraints */
731                                         {
732                                                 ListBase tmp_constraints = {NULL, NULL};
733                                                 
734                                                 /* copy constraints to tmpbase and apply 'local' tags before 
735                                                  * appending to list of constraints for this channel
736                                                  */
737                                                 copy_constraints(&tmp_constraints, &pchanact->constraints);
738                                                 if ((ob->proxy) && (pchan->bone->layer & arm->layer_protected)) {
739                                                         bConstraint *con;
740                                                         
741                                                         /* add proxy-local tags */
742                                                         for (con= tmp_constraints.first; con; con= con->next)
743                                                                 con->flag |= CONSTRAINT_PROXY_LOCAL;
744                                                 }
745                                                 addlisttolist(&pchan->constraints, &tmp_constraints);
746                                                 
747                                                 /* update flags (need to add here, not just copy) */
748                                                 pchan->constflag |= pchanact->constflag;
749                                                 
750                                                 if (ob->pose)
751                                                         ob->pose->flag |= POSE_RECALC;
752                                         }
753                                                 break;
754                                         case 6: /* Transform Locks */
755                                                 pchan->protectflag = pchanact->protectflag;
756                                                 break;
757                                         case 7: /* IK (DOF) settings */
758                                         {
759                                                 pchan->ikflag = pchanact->ikflag;
760                                                 VECCOPY(pchan->limitmin, pchanact->limitmin);
761                                                 VECCOPY(pchan->limitmax, pchanact->limitmax);
762                                                 VECCOPY(pchan->stiffness, pchanact->stiffness);
763                                                 pchan->ikstretch= pchanact->ikstretch;
764                                         }
765                                                 break;
766                                         case 8: /* Custom Bone Shape */
767                                                 pchan->custom = pchanact->custom;
768                                                 break;
769                                         case 9: /* Visual Location */
770                                                 armature_loc_pose_to_bone(pchan, pchanact->pose_mat[3], pchan->loc);
771                                                 break;
772                                         case 10: /* Visual Rotation */
773                                         {
774                                                 float delta_mat[4][4], quat[4];
775                                                 
776                                                 armature_mat_pose_to_bone(pchan, pchanact->pose_mat, delta_mat);
777                                                 Mat4ToQuat(delta_mat, quat);
778                                                 QUATCOPY(pchan->quat, quat);
779                                         }
780                                                 break;
781                                         case 11: /* Visual Size */
782                                         {
783                                                 float delta_mat[4][4], size[4];
784                                                 
785                                                 armature_mat_pose_to_bone(pchan, pchanact->pose_mat, delta_mat);
786                                                 Mat4ToSize(delta_mat, size);
787                                                 VECCOPY(pchan->size, size);
788                                         }
789                                 }
790                         }
791                 }
792         } 
793         else { /* constraints, optional (note: max we can have is 24 constraints) */
794                 bConstraint *con, *con_back;
795                 int const_toggle[24];
796                 ListBase const_copy = {NULL, NULL};
797                 
798                 duplicatelist(&const_copy, &(pchanact->constraints));
799                 
800                 /* build the puplist of constraints */
801                 for (con = pchanact->constraints.first, i=0; con; con=con->next, i++){
802                         const_toggle[i]= 1;
803                         add_numbut(i, TOG|INT, con->name, 0, 0, &(const_toggle[i]), "");
804                 }
805                 
806                 if (!do_clever_numbuts("Select Constraints", i, REDRAW)) {
807                         BLI_freelistN(&const_copy);
808                         return;
809                 }
810                 
811                 /* now build a new listbase from the options selected */
812                 for (i=0, con=const_copy.first; con; i++) {
813                         /* if not selected, free/remove it from the list */
814                         if (!const_toggle[i]) {
815                                 con_back= con->next;
816                                 BLI_freelinkN(&const_copy, con);
817                                 con= con_back;
818                         } 
819                         else
820                                 con= con->next;
821                 }
822                 
823                 /* Copy the temo listbase to the selected posebones */
824                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
825                         if ( (arm->layer & pchan->bone->layer) &&
826                                  (pchan->bone->flag & BONE_SELECTED) &&
827                                  (pchan!=pchanact) ) 
828                         {
829                                 ListBase tmp_constraints = {NULL, NULL};
830                                 
831                                 /* copy constraints to tmpbase and apply 'local' tags before 
832                                  * appending to list of constraints for this channel
833                                  */
834                                 copy_constraints(&tmp_constraints, &const_copy);
835                                 if ((ob->proxy) && (pchan->bone->layer & arm->layer_protected)) {
836                                         bConstraint *con;
837                                         
838                                         /* add proxy-local tags */
839                                         for (con= tmp_constraints.first; con; con= con->next)
840                                                 con->flag |= CONSTRAINT_PROXY_LOCAL;
841                                 }
842                                 addlisttolist(&pchan->constraints, &tmp_constraints);
843                                 
844                                 /* update flags (need to add here, not just copy) */
845                                 pchan->constflag |= pchanact->constflag;
846                         }
847                 }
848                 BLI_freelistN(&const_copy);
849                 update_pose_constraint_flags(ob->pose); /* we could work out the flags but its simpler to do this */
850                 
851                 if (ob->pose)
852                         ob->pose->flag |= POSE_RECALC;
853         }
854         
855         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);   // and all its relations
856         
857         allqueue (REDRAWVIEW3D, 0);
858         allqueue (REDRAWBUTSOBJECT, 0);
859         allqueue (REDRAWOOPS, 0);
860         
861         BIF_undo_push("Copy Pose Attributes");
862         
863 }
864
865 /* ******************** copy/paste pose ********************** */
866
867 static bPose    *g_posebuf=NULL;
868
869 void free_posebuf(void) 
870 {
871         if (g_posebuf) {
872                 // was copied without constraints
873                 BLI_freelistN (&g_posebuf->chanbase);
874                 MEM_freeN (g_posebuf);
875         }
876         g_posebuf=NULL;
877 }
878
879 void copy_posebuf (void)
880 {
881         Object *ob= OBACT;
882
883         if (!ob || !ob->pose){
884                 error ("No Pose");
885                 return;
886         }
887
888         free_posebuf();
889         
890         set_pose_keys(ob);  // sets chan->flag to POSE_KEY if bone selected
891         copy_pose(&g_posebuf, ob->pose, 0);
892
893 }
894
895 void paste_posebuf (int flip)
896 {
897         Object *ob= OBACT;
898         bPoseChannel *chan, *pchan;
899         float eul[4];
900         char name[32];
901         
902         if (!ob || !ob->pose)
903                 return;
904
905         if (!g_posebuf){
906                 error ("Copy buffer is empty");
907                 return;
908         }
909         
910         /*
911         // disabled until protected bones in proxies follow the rules everywhere else!
912         if(pose_has_protected_selected(ob, 1, 1))
913                 return;
914         */
915         
916         /* Safely merge all of the channels in this pose into
917         any existing pose */
918         for (chan=g_posebuf->chanbase.first; chan; chan=chan->next) {
919                 if (chan->flag & POSE_KEY) {
920                         BLI_strncpy(name, chan->name, sizeof(name));
921                         if (flip)
922                                 bone_flip_name (name, 0);               // 0 = don't strip off number extensions
923                                 
924                         /* only copy when channel exists, poses are not meant to add random channels to anymore */
925                         pchan= get_pose_channel(ob->pose, name);
926                         
927                         if (pchan) {
928                                 /* only loc rot size */
929                                 /* only copies transform info for the pose */
930                                 VECCOPY(pchan->loc, chan->loc);
931                                 VECCOPY(pchan->size, chan->size);
932                                 QUATCOPY(pchan->quat, chan->quat);
933                                 pchan->flag= chan->flag;
934                                 
935                                 if (flip) {
936                                         pchan->loc[0]*= -1;
937                                         
938                                         QuatToEul(pchan->quat, eul);
939                                         eul[1]*= -1;
940                                         eul[2]*= -1;
941                                         EulToQuat(eul, pchan->quat);
942                                 }
943                                 
944                                 if (autokeyframe_cfra_can_key(ob)) {
945                                         ID *id= &ob->id;
946                                         
947                                         /* Set keys on pose */
948                                         if (chan->flag & POSE_ROT) {
949                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_X, 0);
950                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_Y, 0);
951                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_Z, 0);
952                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_W, 0);
953                                         }
954                                         if (chan->flag & POSE_SIZE) {
955                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_SIZE_X, 0);
956                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_SIZE_Y, 0);
957                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_SIZE_Z, 0);
958                                         }
959                                         if (chan->flag & POSE_LOC) {
960                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_LOC_X, 0);
961                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_LOC_Y, 0);
962                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_LOC_Z, 0);
963                                         }
964                                         
965                                         /* clear any unkeyed tags */
966                                         if (chan->bone)
967                                                 chan->bone->flag &= ~BONE_UNKEYED;
968                                 }
969                                 else {
970                                         /* add unkeyed tags */
971                                         if (chan->bone)
972                                                 chan->bone->flag |= BONE_UNKEYED;
973                                 }
974                         }
975                 }
976         }
977
978         /* Update event for pose and deformation children */
979         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
980         
981         if (IS_AUTOKEY_ON) {
982                 remake_action_ipos(ob->action);
983                 allqueue(REDRAWIPO, 0);
984                 allqueue(REDRAWVIEW3D, 0);
985                 allqueue(REDRAWACTION, 0);              
986                 allqueue(REDRAWNLA, 0);
987         }
988         else {
989                 /* need to trick depgraph, action is not allowed to execute on pose */
990                 where_is_pose(ob);
991                 ob->recalc= 0;
992         }
993
994         BIF_undo_push("Paste Action Pose");
995 }
996
997 /* ********************************************** */
998
999 /* context weightpaint and deformer in posemode */
1000 void pose_adds_vgroups(Object *meshobj, int heatweights)
1001 {
1002         extern VPaint Gwp;         /* from vpaint */
1003         Object *poseobj= modifiers_isDeformedByArmature(meshobj);
1004
1005         if(poseobj==NULL || (poseobj->flag & OB_POSEMODE)==0) {
1006                 error("The active object must have a deforming armature in pose mode");
1007                 return;
1008         }
1009
1010         add_verts_to_dgroups(meshobj, poseobj, heatweights, (Gwp.flag & VP_MIRROR_X));
1011
1012         if(heatweights)
1013                 BIF_undo_push("Apply Bone Heat Weights to Vertex Groups");
1014         else
1015                 BIF_undo_push("Apply Bone Envelopes to Vertex Groups");
1016
1017         allqueue(REDRAWVIEW3D, 0);
1018         allqueue(REDRAWBUTSEDIT, 0);
1019         
1020         // and all its relations
1021         DAG_object_flush_update(G.scene, meshobj, OB_RECALC_DATA);
1022 }
1023
1024 /* ********************************************** */
1025
1026 /* adds a new pose-group */
1027 void pose_add_posegroup ()
1028 {
1029         Object *ob= OBACT;
1030         bPose *pose= (ob) ? ob->pose : NULL;
1031         bActionGroup *grp;
1032         
1033         if (ELEM(NULL, ob, ob->pose))
1034                 return;
1035         
1036         grp= MEM_callocN(sizeof(bActionGroup), "PoseGroup");
1037         strcpy(grp->name, "Group");
1038         BLI_addtail(&pose->agroups, grp);
1039         BLI_uniquename(&pose->agroups, grp, "Group", offsetof(bActionGroup, name), 32);
1040         
1041         pose->active_group= BLI_countlist(&pose->agroups);
1042         
1043         BIF_undo_push("Add Bone Group");
1044         
1045         allqueue(REDRAWBUTSEDIT, 0);
1046         allqueue(REDRAWVIEW3D, 0);
1047 }
1048
1049 /* Remove the active bone-group */
1050 void pose_remove_posegroup ()
1051 {
1052         Object *ob= OBACT;
1053         bPose *pose= (ob) ? ob->pose : NULL;
1054         bActionGroup *grp = NULL;
1055         bPoseChannel *pchan;
1056         
1057         /* sanity checks */
1058         if (ELEM(NULL, ob, pose))
1059                 return;
1060         if (pose->active_group <= 0)
1061                 return;
1062         
1063         /* get group to remove */
1064         grp= BLI_findlink(&pose->agroups, pose->active_group-1);
1065         if (grp) {
1066                 /* adjust group references (the trouble of using indices!):
1067                  *      - firstly, make sure nothing references it 
1068                  *      - also, make sure that those after this item get corrected
1069                  */
1070                 for (pchan= pose->chanbase.first; pchan; pchan= pchan->next) {
1071                         if (pchan->agrp_index == pose->active_group)
1072                                 pchan->agrp_index= 0;
1073                         else if (pchan->agrp_index > pose->active_group)
1074                                 pchan->agrp_index--;
1075                 }
1076                 
1077                 /* now, remove it from the pose */
1078                 BLI_freelinkN(&pose->agroups, grp);
1079                 pose->active_group= 0;
1080                 
1081                 BIF_undo_push("Remove Bone Group");
1082         }
1083         
1084         allqueue(REDRAWBUTSEDIT, 0);
1085         allqueue(REDRAWVIEW3D, 0);
1086 }
1087
1088 char *build_posegroups_menustr (bPose *pose, short for_pupmenu)
1089 {
1090         DynStr *pupds= BLI_dynstr_new();
1091         bActionGroup *grp;
1092         char *str;
1093         char buf[16];
1094         int i;
1095         
1096         /* add title first (and the "none" entry) */
1097         BLI_dynstr_append(pupds, "Bone Group%t|");
1098         if (for_pupmenu)
1099                 BLI_dynstr_append(pupds, "Add New%x0|");
1100         else
1101                 BLI_dynstr_append(pupds, "BG: [None]%x0|");
1102         
1103         /* loop through groups, adding them */
1104         for (grp= pose->agroups.first, i=1; grp; grp=grp->next, i++) {
1105                 if (for_pupmenu == 0)
1106                         BLI_dynstr_append(pupds, "BG: ");
1107                 BLI_dynstr_append(pupds, grp->name);
1108                 
1109                 sprintf(buf, "%%x%d", i);
1110                 BLI_dynstr_append(pupds, buf);
1111                 
1112                 if (grp->next)
1113                         BLI_dynstr_append(pupds, "|");
1114         }
1115         
1116         /* convert to normal MEM_malloc'd string */
1117         str= BLI_dynstr_get_cstring(pupds);
1118         BLI_dynstr_free(pupds);
1119         
1120         return str;
1121 }
1122
1123 /* Assign selected pchans to the bone group that the user selects */
1124 void pose_assign_to_posegroup (short active)
1125 {
1126         Object *ob= OBACT;
1127         bArmature *arm= (ob) ? ob->data : NULL;
1128         bPose *pose= (ob) ? ob->pose : NULL;
1129         bPoseChannel *pchan;
1130         char *menustr;
1131         int nr;
1132         short done= 0;
1133         
1134         /* sanity checks */
1135         if (ELEM3(NULL, ob, pose, arm))
1136                 return;
1137
1138         /* get group to affect */
1139         if ((active==0) || (pose->active_group <= 0)) {
1140                 menustr= build_posegroups_menustr(pose, 1);
1141                 nr= pupmenu_col(menustr, 20);
1142                 MEM_freeN(menustr);
1143                 
1144                 if (nr < 0) 
1145                         return;
1146                 else if (nr == 0) {
1147                         /* add new - note: this does an undo push and sets active group */
1148                         pose_add_posegroup();
1149                 }
1150                 else
1151                         pose->active_group= nr;
1152         }
1153         
1154         /* add selected bones to group then */
1155         for (pchan= pose->chanbase.first; pchan; pchan= pchan->next) {
1156                 if ((pchan->bone->flag & BONE_SELECTED) && (pchan->bone->layer & arm->layer)) {
1157                         pchan->agrp_index= pose->active_group;
1158                         done= 1;
1159                 }
1160         }
1161         
1162         if (done)
1163                 BIF_undo_push("Add Bones To Group");
1164                 
1165         allqueue(REDRAWBUTSEDIT, 0);
1166         allqueue(REDRAWVIEW3D, 0);
1167 }
1168
1169 /* Remove selected pchans from their bone groups */
1170 void pose_remove_from_posegroups ()
1171 {
1172         Object *ob= OBACT;
1173         bArmature *arm= (ob) ? ob->data : NULL;
1174         bPose *pose= (ob) ? ob->pose : NULL;
1175         bPoseChannel *pchan;
1176         short done= 0;
1177         
1178         /* sanity checks */
1179         if (ELEM3(NULL, ob, pose, arm))
1180                 return;
1181         
1182         /* remove selected bones from their groups */
1183         for (pchan= pose->chanbase.first; pchan; pchan= pchan->next) {
1184                 if ((pchan->bone->flag & BONE_SELECTED) && (pchan->bone->layer & arm->layer)) {
1185                         if (pchan->agrp_index) {
1186                                 pchan->agrp_index= 0;
1187                                 done= 1;
1188                         }
1189                 }
1190         }
1191         
1192         if (done)
1193                 BIF_undo_push("Remove Bones From Groups");
1194                 
1195         allqueue(REDRAWBUTSEDIT, 0);
1196         allqueue(REDRAWVIEW3D, 0);
1197 }
1198
1199 /* Ctrl-G in 3D-View while in PoseMode */
1200 void pgroup_operation_with_menu (void)
1201 {
1202         Object *ob= OBACT;
1203         bArmature *arm= (ob) ? ob->data : NULL;
1204         bPose *pose= (ob) ? ob->pose : NULL;
1205         bPoseChannel *pchan= NULL;
1206         int mode;
1207         
1208         /* sanity checks */
1209         if (ELEM3(NULL, ob, pose, arm))
1210                 return;
1211         
1212         /* check that something is selected */
1213         for (pchan= pose->chanbase.first; pchan; pchan= pchan->next) {
1214                 if ((pchan->bone->flag & BONE_SELECTED) && (pchan->bone->layer & arm->layer)) 
1215                         break;
1216         }
1217         if (pchan == NULL)
1218                 return;
1219         
1220         /* get mode of action */
1221         if (pchan)
1222                 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");
1223         else
1224                 mode= pupmenu("Bone Groups%t|Add New Group%x5|Remove Active Group%x4");
1225                 
1226         /* handle mode */
1227         switch (mode) {
1228                 case 1:
1229                         pose_assign_to_posegroup(1);
1230                         break;
1231                 case 2:
1232                         pose_assign_to_posegroup(0);
1233                         break;
1234                 case 5:
1235                         pose_add_posegroup();
1236                         break;
1237                 case 3:
1238                         pose_remove_from_posegroups();
1239                         break;
1240                 case 4:
1241                         pose_remove_posegroup();
1242                         break;
1243         }
1244 }
1245
1246 /* ********************************************** */
1247
1248 static short pose_select_same_group (Object *ob)
1249 {
1250         bPose *pose= (ob)? ob->pose : NULL;
1251         bArmature *arm= (ob)? ob->data : NULL;
1252         bPoseChannel *pchan, *chan;
1253         short changed= 0;
1254         
1255         if (ELEM3(NULL, ob, pose, arm))
1256                 return 0;
1257         
1258         /* loop in loop... bad and slow! */
1259         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1260                 if (arm->layer & pchan->bone->layer) {
1261                         if (pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
1262                                 
1263                                 /* only if group matches (and is not selected or current bone) */
1264                                 for (chan= ob->pose->chanbase.first; chan; chan= chan->next) {
1265                                         if (arm->layer & chan->bone->layer) {
1266                                                 if (pchan->agrp_index == chan->agrp_index) {
1267                                                         chan->bone->flag |= BONE_SELECTED;
1268                                                         changed= 1;
1269                                                 }
1270                                         }
1271                                 }
1272                                 
1273                         }
1274                 }
1275         }
1276         
1277         return changed;
1278 }
1279
1280 static short pose_select_same_layer (Object *ob)
1281 {
1282         bPose *pose= (ob)? ob->pose : NULL;
1283         bArmature *arm= (ob)? ob->data : NULL;
1284         bPoseChannel *pchan;
1285         short layers= 0, changed= 0;
1286         
1287         if (ELEM3(NULL, ob, pose, arm))
1288                 return 0;
1289         
1290         /* figure out what bones are selected */
1291         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1292                 if (arm->layer & pchan->bone->layer) {
1293                         if (pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
1294                                 layers |= pchan->bone->layer;
1295                         }
1296                 }
1297         }
1298         if (layers == 0) 
1299                 return 0;
1300                 
1301         /* select bones that are on same layers as layers flag */
1302         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1303                 if (arm->layer & pchan->bone->layer) {
1304                         if (layers & pchan->bone->layer) {
1305                                 pchan->bone->flag |= BONE_SELECTED;
1306                                 changed= 1;
1307                         }
1308                 }
1309         }
1310         
1311         return changed;
1312 }
1313
1314
1315 void pose_select_grouped (short nr)
1316 {
1317         short changed = 0;
1318         
1319         if (nr == 1)            changed= pose_select_same_group(OBACT);
1320         else if (nr == 2)       changed= pose_select_same_layer(OBACT);
1321         
1322         if (changed) {
1323                 countall();
1324                 allqueue(REDRAWVIEW3D, 0);
1325                 allqueue(REDRAWBUTSOBJECT, 0);
1326                 allqueue(REDRAWBUTSEDIT, 0);
1327                 allspace(REMAKEIPO, 0);
1328                 allqueue(REDRAWIPO, 0);
1329                 allqueue(REDRAWACTION, 0);
1330                 BIF_undo_push("Select Grouped");
1331         }
1332 }
1333
1334 /* Shift-G in 3D-View while in PoseMode */
1335 void pose_select_grouped_menu (void)
1336 {
1337         short nr;
1338         
1339         /* here we go */
1340         nr= pupmenu("Select Grouped%t|In Same Group%x1|In Same Layer%x2");
1341         pose_select_grouped(nr);
1342 }
1343
1344 /* ********************************************** */
1345
1346 /* context active object */
1347 void pose_flip_names(void)
1348 {
1349         Object *ob= OBACT;
1350         bArmature *arm= ob->data;
1351         bPoseChannel *pchan;
1352         char newname[32];
1353         
1354         /* paranoia checks */
1355         if(!ob && !ob->pose) return;
1356         if(ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
1357         
1358         if(pose_has_protected_selected(ob, 0, 1))
1359                 return;
1360         
1361         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1362                 if(arm->layer & pchan->bone->layer) {
1363                         if(pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
1364                                 BLI_strncpy(newname, pchan->name, sizeof(newname));
1365                                 bone_flip_name(newname, 1);     // 1 = do strip off number extensions
1366                                 armature_bone_rename(ob->data, pchan->name, newname);
1367                         }
1368                 }
1369         }
1370         
1371         allqueue(REDRAWVIEW3D, 0);
1372         allqueue(REDRAWBUTSEDIT, 0);
1373         allqueue(REDRAWBUTSOBJECT, 0);
1374         allqueue (REDRAWACTION, 0);
1375         allqueue(REDRAWOOPS, 0);
1376         BIF_undo_push("Flip names");
1377 }
1378
1379 /* context active object */
1380 void pose_autoside_names(short axis)
1381 {
1382         Object *ob= OBACT;
1383         bArmature *arm= ob->data;
1384         bPoseChannel *pchan;
1385         char newname[32];
1386         
1387         /* paranoia checks */
1388         if (ELEM(NULL, ob, ob->pose)) return;
1389         if (ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
1390         
1391         if (pose_has_protected_selected(ob, 0, 1))
1392                 return;
1393         
1394         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1395                 if(arm->layer & pchan->bone->layer) {
1396                         if(pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
1397                                 BLI_strncpy(newname, pchan->name, sizeof(newname));
1398                                 bone_autoside_name(newname, 1, axis, pchan->bone->head[axis], pchan->bone->tail[axis]);
1399                                 armature_bone_rename(ob->data, pchan->name, newname);
1400                         }
1401                 }
1402         }
1403         
1404         allqueue(REDRAWVIEW3D, 0);
1405         allqueue(REDRAWBUTSEDIT, 0);
1406         allqueue(REDRAWBUTSOBJECT, 0);
1407         allqueue(REDRAWACTION, 0);
1408         allqueue(REDRAWOOPS, 0);
1409         BIF_undo_push("Flip names");
1410 }
1411
1412 /* context active object, or weightpainted object with armature in posemode */
1413 void pose_activate_flipped_bone(void)
1414 {
1415         Object *ob= OBACT;
1416         bArmature *arm= ob->data;
1417         
1418         if(ob==NULL) return;
1419
1420         if(G.f & G_WEIGHTPAINT) {
1421                 ob= modifiers_isDeformedByArmature(ob);
1422         }
1423         if(ob && (ob->flag & OB_POSEMODE)) {
1424                 bPoseChannel *pchan, *pchanf;
1425                 
1426                 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1427                         if(arm->layer & pchan->bone->layer) {
1428                                 if(pchan->bone->flag & BONE_ACTIVE)
1429                                         break;
1430                         }
1431                 }
1432                 if(pchan) {
1433                         char name[32];
1434                         
1435                         BLI_strncpy(name, pchan->name, 32);
1436                         bone_flip_name(name, 1);        // 0 = do not strip off number extensions
1437                         
1438                         pchanf= get_pose_channel(ob->pose, name);
1439                         if(pchanf && pchanf!=pchan) {
1440                                 pchan->bone->flag &= ~(BONE_SELECTED|BONE_ACTIVE);
1441                                 pchanf->bone->flag |= (BONE_SELECTED|BONE_ACTIVE);
1442                         
1443                                 /* in weightpaint we select the associated vertex group too */
1444                                 if(G.f & G_WEIGHTPAINT) {
1445                                         vertexgroup_select_by_name(OBACT, name);
1446                                         DAG_object_flush_update(G.scene, OBACT, OB_RECALC_DATA);
1447                                 }
1448                                 
1449                                 select_actionchannel_by_name(ob->action, name, 1);
1450                                 
1451                                 allqueue(REDRAWVIEW3D, 0);
1452                                 allqueue(REDRAWACTION, 0);
1453                                 allqueue(REDRAWIPO, 0);         /* To force action/constraint ipo update */
1454                                 allqueue(REDRAWBUTSEDIT, 0);
1455                                 allqueue(REDRAWBUTSOBJECT, 0);
1456                                 allqueue(REDRAWOOPS, 0);
1457                         }                       
1458                 }
1459         }
1460 }
1461
1462 /* This function pops up the move-to-layer popup widgets when the user
1463  * presses either SHIFT-MKEY or MKEY in PoseMode OR EditMode (for Armatures)
1464  */
1465 void pose_movetolayer(void)
1466 {
1467         Object *ob= OBACT;
1468         bArmature *arm;
1469         short lay= 0;
1470         
1471         if (ob==NULL) return;
1472         arm= ob->data;
1473         
1474         if (G.qual & LR_SHIFTKEY) {
1475                 /* armature layers */
1476                 lay= arm->layer;
1477                 if ( movetolayer_short_buts(&lay, "Armature Layers")==0 ) return;
1478                 if (lay==0) return;
1479                 arm->layer= lay;
1480                 if(ob->pose)
1481                         ob->pose->proxy_layer= lay;
1482                 
1483                 allqueue(REDRAWVIEW3D, 0);
1484                 allqueue(REDRAWACTION, 0);
1485                 allqueue(REDRAWBUTSEDIT, 0);
1486         }
1487         else if (G.obedit) {
1488                 /* the check for editbone layer moving needs to occur before posemode one to work */
1489                 EditBone *ebo;
1490                 EditBone *flipBone;
1491                 
1492                 for (ebo= G.edbo.first; ebo; ebo= ebo->next) {
1493                         if (arm->layer & ebo->layer) {
1494                                 if (ebo->flag & BONE_SELECTED)
1495                                         lay |= ebo->layer;
1496                         }
1497                 }
1498                 if (lay==0) return;
1499                 
1500                 if ( movetolayer_short_buts(&lay, "Bone Layers")==0 ) return;
1501                 if (lay==0) return;
1502                 
1503                 for (ebo= G.edbo.first; ebo; ebo= ebo->next) {
1504                         if (arm->layer & ebo->layer) {
1505                                 if (ebo->flag & BONE_SELECTED) {
1506                                         ebo->layer= lay;
1507                                         if (arm->flag & ARM_MIRROR_EDIT) {
1508                                                 flipBone = armature_bone_get_mirrored(ebo);
1509                                                 if (flipBone)
1510                                                         flipBone->layer = lay;
1511                                         }
1512                                 }
1513                         }
1514                 }
1515                 
1516                 BIF_undo_push("Move Bone Layer");
1517                 allqueue(REDRAWVIEW3D, 0);
1518                 allqueue(REDRAWBUTSEDIT, 0);
1519         }
1520         else if (ob->flag & OB_POSEMODE) {
1521                 /* pose-channel layers */
1522                 bPoseChannel *pchan;
1523                 
1524                 if (pose_has_protected_selected(ob, 0, 1))
1525                         return;
1526                 
1527                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1528                         if (arm->layer & pchan->bone->layer) {
1529                                 if (pchan->bone->flag & BONE_SELECTED)
1530                                         lay |= pchan->bone->layer;
1531                         }
1532                 }
1533                 if (lay==0) return;
1534                 
1535                 if ( movetolayer_short_buts(&lay, "Bone Layers")==0 ) return;
1536                 if (lay==0) return;
1537                 
1538                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1539                         if (arm->layer & pchan->bone->layer) {
1540                                 if (pchan->bone->flag & BONE_SELECTED)
1541                                         pchan->bone->layer= lay;
1542                         }
1543                 }
1544                 
1545                 BIF_undo_push("Move Bone Layer");
1546                 allqueue(REDRAWVIEW3D, 0);
1547                 allqueue(REDRAWACTION, 0);
1548                 allqueue(REDRAWBUTSEDIT, 0);
1549         }
1550 }
1551
1552
1553 /* for use with pose_relax only */
1554 static int pose_relax_icu(struct IpoCurve *icu, float framef, float *val, float *frame_prev, float *frame_next)
1555 {
1556         if (!icu) {
1557                 return 0;
1558         } 
1559         else {
1560                 BezTriple *bezt = icu->bezt;
1561                 
1562                 BezTriple *bezt_prev=NULL, *bezt_next=NULL;
1563                 float w1, w2, wtot;
1564                 int i;
1565                 
1566                 for (i=0; i < icu->totvert; i++, bezt++) {
1567                         if (bezt->vec[1][0] < framef - 0.5) {
1568                                 bezt_prev = bezt;
1569                         } else {
1570                                 break;
1571                         }
1572                 }
1573                 
1574                 if (bezt_prev==NULL) return 0;
1575                 
1576                 /* advance to the next, dont need to advance i */
1577                 bezt = bezt_prev+1;
1578                 
1579                 for (; i < icu->totvert; i++, bezt++) {
1580                         if (bezt->vec[1][0] > framef + 0.5) {
1581                                 bezt_next = bezt;
1582                                                 break;
1583                         }
1584                 }
1585                 
1586                 if (bezt_next==NULL) return 0;
1587         
1588                 if (val) {
1589                         w1 = framef - bezt_prev->vec[1][0];
1590                         w2 = bezt_next->vec[1][0] - framef;
1591                         wtot = w1 + w2;
1592                         w1=w1/wtot;
1593                         w2=w2/wtot;
1594 #if 0
1595                         val = (bezt_prev->vec[1][1] * w2) + (bezt_next->vec[1][1] * w1);
1596 #else
1597                         /* apply the value with a hard coded 6th */
1598                         *val = (((bezt_prev->vec[1][1] * w2) + (bezt_next->vec[1][1] * w1)) + (*val * 5.0f)) / 6.0f;
1599 #endif
1600                 }
1601                 
1602                 if (frame_prev) *frame_prev = bezt_prev->vec[1][0];
1603                 if (frame_next) *frame_next = bezt_next->vec[1][0];
1604                 
1605                 return 1;
1606         }
1607 }
1608
1609 void pose_relax()
1610 {
1611         Object *ob = OBACT;
1612         bPose *pose;
1613         bAction *act;
1614         bArmature *arm;
1615         
1616         IpoCurve *icu_w, *icu_x, *icu_y, *icu_z;
1617         
1618         bPoseChannel *pchan;
1619         bActionChannel *achan;
1620         float framef = F_CFRA;
1621         float frame_prev, frame_next;
1622         float quat_prev[4], quat_next[4], quat_interp[4], quat_orig[4];
1623         
1624         int do_scale = 0;
1625         int do_loc = 0;
1626         int do_quat = 0;
1627         int flag = 0;
1628         int do_x, do_y, do_z;
1629         
1630         if (!ob) return;
1631         
1632         pose = ob->pose;
1633         act = ob->action;
1634         arm = (bArmature *)ob->data;
1635         
1636         if (!pose || !act || !arm) return;
1637         
1638         for (pchan=pose->chanbase.first; pchan; pchan= pchan->next) {
1639                 
1640                 pchan->bone->flag &= ~BONE_TRANSFORM;
1641                 
1642                 if (pchan->bone->layer & arm->layer) {
1643                         if (pchan->bone->flag & BONE_SELECTED) {
1644                                 /* do we have an ipo curve? */
1645                                 achan= get_action_channel(act, pchan->name);
1646                                 
1647                                 if (achan && achan->ipo) {
1648                                         /*calc_ipo(achan->ipo, ctime);*/
1649                                         
1650                                         do_x = pose_relax_icu(find_ipocurve(achan->ipo, AC_LOC_X), framef, &pchan->loc[0], NULL, NULL);
1651                                         do_y = pose_relax_icu(find_ipocurve(achan->ipo, AC_LOC_Y), framef, &pchan->loc[1], NULL, NULL);
1652                                         do_z = pose_relax_icu(find_ipocurve(achan->ipo, AC_LOC_Z), framef, &pchan->loc[2], NULL, NULL);
1653                                         do_loc += do_x + do_y + do_z;
1654                                         
1655                                         do_x = pose_relax_icu(find_ipocurve(achan->ipo, AC_SIZE_X), framef, &pchan->size[0], NULL, NULL);
1656                                         do_y = pose_relax_icu(find_ipocurve(achan->ipo, AC_SIZE_Y), framef, &pchan->size[1], NULL, NULL);
1657                                         do_z = pose_relax_icu(find_ipocurve(achan->ipo, AC_SIZE_Z), framef, &pchan->size[2], NULL, NULL);
1658                                         do_scale += do_x + do_y + do_z;
1659                                                 
1660                                         if(     ((icu_w = find_ipocurve(achan->ipo, AC_QUAT_W))) &&
1661                                                 ((icu_x = find_ipocurve(achan->ipo, AC_QUAT_X))) &&
1662                                                 ((icu_y = find_ipocurve(achan->ipo, AC_QUAT_Y))) &&
1663                                                 ((icu_z = find_ipocurve(achan->ipo, AC_QUAT_Z))) )
1664                                         {
1665                                                 /* use the quatw keyframe as a basis for others */
1666                                                 if (pose_relax_icu(icu_w, framef, NULL, &frame_prev, &frame_next)) {
1667                                                         /* get 2 quats */
1668                                                         quat_prev[0] = eval_icu(icu_w, frame_prev);
1669                                                         quat_prev[1] = eval_icu(icu_x, frame_prev);
1670                                                         quat_prev[2] = eval_icu(icu_y, frame_prev);
1671                                                         quat_prev[3] = eval_icu(icu_z, frame_prev);
1672                                                         
1673                                                         quat_next[0] = eval_icu(icu_w, frame_next);
1674                                                         quat_next[1] = eval_icu(icu_x, frame_next);
1675                                                         quat_next[2] = eval_icu(icu_y, frame_next);
1676                                                         quat_next[3] = eval_icu(icu_z, frame_next);
1677                                                         
1678 #if 0
1679                                                         /* apply the setting, completely smooth */
1680                                                         QuatInterpol(pchan->quat, quat_prev, quat_next, (framef-frame_prev) / (frame_next-frame_prev) );
1681 #else
1682                                                         /* tricky interpolation */
1683                                                         QuatInterpol(quat_interp, quat_prev, quat_next, (framef-frame_prev) / (frame_next-frame_prev) );
1684                                                         QUATCOPY(quat_orig, pchan->quat);
1685                                                         QuatInterpol(pchan->quat, quat_orig, quat_interp, 1.0f/6.0f);
1686                                                         /* done */
1687 #endif
1688                                                         do_quat++;
1689                                                 }
1690                                         }
1691                                         
1692                                         /* apply BONE_TRANSFORM tag so that autokeying will pick it up */
1693                                         pchan->bone->flag |= BONE_TRANSFORM;
1694                                 }
1695                         }
1696                 }
1697         }
1698         
1699         ob->pose->flag |= (POSE_LOCKED|POSE_DO_UNLOCK);
1700         
1701         /* do auto-keying */
1702         if (do_loc)             flag |= TFM_TRANSLATION;
1703         if (do_scale)   flag |= TFM_RESIZE;
1704         if (do_quat)    flag |= TFM_ROTATION;
1705         autokeyframe_pose_cb_func(ob, flag, 0);
1706          
1707         /* clear BONE_TRANSFORM flags */
1708         for (pchan=pose->chanbase.first; pchan; pchan= pchan->next)
1709                 pchan->bone->flag &= ~ BONE_TRANSFORM;
1710         
1711         /* do depsgraph flush */
1712         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
1713         BIF_undo_push("Relax Pose");
1714 }
1715
1716 /* for use in insertkey, ensure rotation goes other way around */
1717 void pose_flipquats(void)
1718 {
1719         Object *ob = OBACT;
1720         bArmature *arm= ob->data;
1721         bPoseChannel *pchan;
1722         
1723         if(ob->pose==NULL)
1724                 return;
1725         
1726         /* find sel bones */
1727         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1728                 if(pchan->bone && (pchan->bone->flag & BONE_SELECTED) && (pchan->bone->layer & arm->layer)) {
1729                         /* quaternions have 720 degree range */
1730                         pchan->quat[0]= -pchan->quat[0];
1731                         pchan->quat[1]= -pchan->quat[1];
1732                         pchan->quat[2]= -pchan->quat[2];
1733                         pchan->quat[3]= -pchan->quat[3];
1734                 }
1735         }
1736         
1737         /* do autokey */
1738         autokeyframe_pose_cb_func(ob, TFM_ROTATION, 0);
1739 }
1740
1741 /* Restore selected pose-bones to 'action'-defined pose */
1742 void pose_clear_user_transforms (void) 
1743 {
1744         Object *ob = OBACT;
1745         bArmature *arm= ob->data;
1746         bPoseChannel *pchan;
1747         
1748         if (ob->pose == NULL)
1749                 return;
1750         
1751         /* find selected bones */
1752         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1753                 if (pchan->bone && (pchan->bone->flag & BONE_SELECTED) && (pchan->bone->layer & arm->layer)) {
1754                         /* just clear the BONE_UNKEYED flag, allowing this bone to get overwritten by actions again */
1755                         pchan->bone->flag &= ~BONE_UNKEYED;
1756                 }
1757         }
1758         
1759         /* clear pose locking flag 
1760          *      - this will only clear the user-defined pose in the selected bones, where BONE_UNKEYED has been cleared
1761          */
1762         ob->pose->flag |= POSE_DO_UNLOCK;
1763         
1764         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
1765         BIF_undo_push("Clear User Transform");
1766 }
1767