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