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