Merge with 2.5 -r 21788:22040.
[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  *                               Joshua Leung
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  * support for animation modes - Reevan McKay
28  */
29
30 #include <stdlib.h>
31 #include <stddef.h>
32 #include <string.h>
33
34 #include "MEM_guardedalloc.h"
35
36 #include "BLI_arithb.h"
37 #include "BLI_blenlib.h"
38 #include "BLI_dynstr.h"
39
40 #include "DNA_anim_types.h"
41 #include "DNA_action_types.h"
42 #include "DNA_armature_types.h"
43 #include "DNA_constraint_types.h"
44 #include "DNA_curve_types.h"
45 #include "DNA_mesh_types.h"
46 #include "DNA_meshdata_types.h"
47 #include "DNA_modifier_types.h"
48 #include "DNA_object_types.h"
49 #include "DNA_scene_types.h"
50 #include "DNA_screen_types.h"
51 #include "DNA_view3d_types.h"
52 #include "DNA_userdef_types.h"
53
54 #include "BKE_animsys.h"
55 #include "BKE_action.h"
56 #include "BKE_armature.h"
57 #include "BKE_blender.h"
58 #include "BKE_context.h"
59 #include "BKE_constraint.h"
60 #include "BKE_deform.h"
61 #include "BKE_depsgraph.h"
62 #include "BKE_displist.h"
63 #include "BKE_fcurve.h"
64 #include "BKE_global.h"
65 #include "BKE_modifier.h"
66 #include "BKE_object.h"
67 #include "BKE_utildefines.h"
68 #include "BKE_report.h"
69
70 #include "BIF_gl.h"
71
72 #include "RNA_access.h"
73 #include "RNA_define.h"
74
75 #include "WM_api.h"
76 #include "WM_types.h"
77
78 #include "ED_armature.h"
79 #include "ED_anim_api.h"
80 #include "ED_keyframing.h"
81 #include "ED_object.h"
82 #include "ED_mesh.h"
83 #include "ED_screen.h"
84 #include "ED_transform.h" /* for autokey TFM_TRANSLATION, etc */
85 #include "ED_view3d.h"
86
87 #include "UI_interface.h"
88
89 #include "armature_intern.h"
90
91 /* ************* XXX *************** */
92 static int pupmenu() {return 0;}
93 static void error() {};
94 static void BIF_undo_push() {}
95 static void countall() {}
96 static void autokeyframe_pose_cb_func() {}
97 /* ************* XXX *************** */
98
99 /* This function is used to indicate that a bone is selected and needs keyframes inserted */
100 void set_pose_keys (Object *ob)
101 {
102         bArmature *arm= ob->data;
103         bPoseChannel *chan;
104
105         if (ob->pose){
106                 for (chan=ob->pose->chanbase.first; chan; chan=chan->next){
107                         Bone *bone= chan->bone;
108                         if ((bone) && (bone->flag & BONE_SELECTED) && (arm->layer & bone->layer))
109                                 chan->flag |= POSE_KEY; 
110                         else
111                                 chan->flag &= ~POSE_KEY;
112                 }
113         }
114 }
115
116 /* This function is used to process the necessary updates for */
117 void ED_armature_enter_posemode(bContext *C, Base *base)
118 {
119         Object *ob= base->object;
120         
121         if (ob->id.lib){
122                 error ("Can't pose libdata");
123                 return;
124         }
125         
126         switch (ob->type){
127                 case OB_ARMATURE:
128                         
129                         ob->flag |= OB_POSEMODE;
130                         base->flag= ob->flag;
131                         
132                         WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_MODE_POSE, NULL);
133                         
134                         break;
135                 default:
136                         return;
137         }
138         ED_view3d_exit_paint_modes(C);
139 }
140
141 void ED_armature_exit_posemode(bContext *C, Base *base)
142 {
143         if(base) {
144                 Object *ob= base->object;
145                 
146                 ob->flag &= ~OB_POSEMODE;
147                 base->flag= ob->flag;
148                 
149                 WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_MODE_OBJECT, NULL);
150         }       
151 }
152
153 /* if a selected or active bone is protected, throw error (oonly if warn==1) and return 1 */
154 /* only_selected==1 : the active bone is allowed to be protected */
155 static short pose_has_protected_selected(Object *ob, short only_selected, short warn)
156 {
157         /* check protection */
158         if (ob->proxy) {
159                 bPoseChannel *pchan;
160                 bArmature *arm= ob->data;
161                 
162                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
163                         if (pchan->bone && (pchan->bone->layer & arm->layer)) {
164                                 if (pchan->bone->layer & arm->layer_protected) {
165                                         if (only_selected && (pchan->bone->flag & BONE_ACTIVE));
166                                         else if (pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) 
167                                            break;
168                                 }
169                         }
170                 }
171                 if (pchan) {
172                         if (warn) error("Cannot change Proxy protected bones");
173                         return 1;
174                 }
175         }
176         return 0;
177 }
178
179 /* only for real IK, not for auto-IK */
180 int ED_pose_channel_in_IK_chain(Object *ob, bPoseChannel *pchan)
181 {
182         bConstraint *con;
183         Bone *bone;
184         
185         /* No need to check if constraint is active (has influence),
186          * since all constraints with CONSTRAINT_IK_AUTO are active */
187         for(con= pchan->constraints.first; con; con= con->next) {
188                 if(con->type==CONSTRAINT_TYPE_KINEMATIC) {
189                         bKinematicConstraint *data= con->data;
190                         if((data->flag & CONSTRAINT_IK_AUTO)==0)
191                                 return 1;
192                 }
193         }
194         for(bone= pchan->bone->childbase.first; bone; bone= bone->next) {
195                 pchan= get_pose_channel(ob->pose, bone->name);
196                 if(pchan && ED_pose_channel_in_IK_chain(ob, pchan))
197                         return 1;
198         }
199         return 0;
200 }
201
202 /* ********************************************** */
203
204 /* For the object with pose/action: update paths for those that have got them
205  * This should selectively update paths that exist...
206  *
207  * To be called from various tools that do incremental updates 
208  */
209 void ED_pose_recalculate_paths(bContext *C, Scene *scene, Object *ob)
210 {
211         bArmature *arm;
212         bPoseChannel *pchan;
213         Base *base;
214         float *fp;
215         int cfra;
216         int sfra, efra;
217         
218         /* sanity checks */
219         if ELEM(NULL, ob, ob->pose)
220                 return;
221         arm= ob->data;
222         
223         /* set frame values */
224         cfra = CFRA;
225         sfra = efra = cfra; 
226         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
227                 if ((pchan->bone) && (arm->layer & pchan->bone->layer)) {
228                         if (pchan->path) {
229                                 /* if the pathsf and pathef aren't initialised, abort! */
230                                 if (ELEM(0, pchan->pathsf, pchan->pathef))      
231                                         return;
232                                 
233                                 /* try to increase area to do (only as much as needed) */
234                                 sfra= MIN2(sfra, pchan->pathsf);
235                                 efra= MAX2(efra, pchan->pathef);
236                         }
237                 }
238         }
239         if (efra <= sfra) return;
240         
241         /* hack: for unsaved files, set OB_RECALC so that paths can get calculated */
242         if ((ob->recalc & OB_RECALC)==0) {
243                 ob->recalc |= OB_RECALC;
244                 ED_anim_object_flush_update(C, ob);
245         }
246         else
247                 ED_anim_object_flush_update(C, ob);
248         
249         /* calculate path over requested range */
250         for (CFRA=sfra; CFRA<=efra; CFRA++) {
251                 /* do all updates */
252                 for (base= FIRSTBASE; base; base= base->next) {
253                         if (base->object->recalc) {
254                                 int temp= base->object->recalc;
255                                 
256                                 if (base->object->adt)
257                                         BKE_animsys_evaluate_animdata(&base->object->id, base->object->adt, (float)CFRA, ADT_RECALC_ALL);
258                                 
259                                 /* update object */
260                                 object_handle_update(scene, base->object);
261                                 base->object->recalc= temp;
262                         }
263                 }
264                 
265                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
266                         if ((pchan->bone) && (arm->layer & pchan->bone->layer)) {
267                                 if (pchan->path) {
268                                         /* only update if:
269                                          *      - in range of this pchan's existing path
270                                          *      - ... insert evil filtering/optimising conditions here...
271                                          */
272                                         if (IN_RANGE(CFRA, pchan->pathsf, pchan->pathef)) {
273                                                 fp= pchan->path+3*(CFRA-sfra);
274                                                 
275                                                 if (arm->pathflag & ARM_PATH_HEADS) { 
276                                                         VECCOPY(fp, pchan->pose_head);
277                                                 }
278                                                 else {
279                                                         VECCOPY(fp, pchan->pose_tail);
280                                                 }
281                                                 
282                                                 Mat4MulVecfl(ob->obmat, fp);
283                                         }
284                                 }
285                         }
286                 }
287         }
288         
289         /* reset flags */
290         CFRA= cfra;
291         ob->pose->flag &= ~POSE_RECALCPATHS;
292         
293         /* flush one final time - to restore to the original state */
294         for (base= FIRSTBASE; base; base= base->next) {
295                 if (base->object->recalc) {
296                         int temp= base->object->recalc;
297                         
298                         if (base->object->adt)
299                                 BKE_animsys_evaluate_animdata(&base->object->id, base->object->adt, (float)CFRA, ADT_RECALC_ALL);
300                         
301                         object_handle_update(scene, base->object);
302                         base->object->recalc= temp;
303                 }
304         }
305 }
306
307 /* --------- */
308
309 /* For the object with pose/action: create path curves for selected bones 
310  * This recalculates the WHOLE path within the pchan->pathsf and pchan->pathef range
311  */
312 static int pose_calculate_paths_exec (bContext *C, wmOperator *op)
313 {
314         wmWindow *win= CTX_wm_window(C);
315         ScrArea *sa= CTX_wm_area(C);
316         Scene *scene= CTX_data_scene(C);
317         Object *ob;
318         bArmature *arm;
319         bPoseChannel *pchan;
320         Base *base;
321         float *fp;
322         int cfra;
323         int sfra, efra;
324         
325         /* since this call may also be used from the buttons window, we need to check for where to get the object */
326         if (sa->spacetype == SPACE_BUTS) 
327                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
328         else
329                 ob= CTX_data_active_object(C);
330                 
331         /* only continue if there's an object */
332         if ELEM(NULL, ob, ob->pose)
333                 return OPERATOR_CANCELLED;
334         arm= ob->data;
335         
336         /* version patch for older files here (do_versions patch too complicated) */
337         if ((arm->pathsf == 0) || (arm->pathef == 0)) {
338                 arm->pathsf = SFRA;
339                 arm->pathef = EFRA;
340         }
341         if (arm->pathsize == 0) {
342                 arm->pathsize = 1;
343         }
344         
345         /* get frame values to use */
346         cfra= CFRA;
347         sfra = arm->pathsf;
348         efra = arm->pathef;
349         
350         if (efra <= sfra) {
351                 BKE_report(op->reports, RPT_ERROR, "Can't calculate paths when pathlen <= 0");
352                 return OPERATOR_CANCELLED;
353         }
354         
355         /* hack: for unsaved files, set OB_RECALC so that paths can get calculated */
356         if ((ob->recalc & OB_RECALC)==0) {
357                 ob->recalc |= OB_RECALC;
358                 ED_anim_object_flush_update(C, ob);
359         }
360         else
361                 ED_anim_object_flush_update(C, ob);
362         
363         /* alloc the path cache arrays */
364         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
365                 if ((pchan->bone) && (pchan->bone->flag & BONE_SELECTED)) {
366                         if (arm->layer & pchan->bone->layer) {
367                                 pchan->pathlen= efra-sfra+1;
368                                 pchan->pathsf= sfra;
369                                 pchan->pathef= efra+1;
370                                 if (pchan->path)
371                                         MEM_freeN(pchan->path);
372                                 pchan->path= MEM_callocN(3*pchan->pathlen*sizeof(float), "pchan path");
373                         }
374                 }
375         }
376         
377         /* step through frame range sampling the values */
378         for (CFRA=sfra; CFRA<=efra; CFRA++) {
379                 /* for each frame we calculate, update time-cursor... (may be too slow) */
380                 WM_timecursor(win, CFRA);
381                 
382                 /* do all updates */
383                 for (base= FIRSTBASE; base; base= base->next) {
384                         if (base->object->recalc) {
385                                 int temp= base->object->recalc;
386                                 
387                                 if (base->object->adt)
388                                         BKE_animsys_evaluate_animdata(&base->object->id, base->object->adt, (float)CFRA, ADT_RECALC_ALL);
389                                 
390                                 object_handle_update(scene, base->object);
391                                 base->object->recalc= temp;
392                         }
393                 }
394                 
395                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
396                         if ((pchan->bone) && (pchan->bone->flag & BONE_SELECTED)) {
397                                 if (arm->layer & pchan->bone->layer) {
398                                         if (pchan->path) {
399                                                 fp= pchan->path+3*(CFRA-sfra);
400                                                 
401                                                 if (arm->pathflag & ARM_PATH_HEADS) { 
402                                                         VECCOPY(fp, pchan->pose_head);
403                                                 }
404                                                 else {
405                                                         VECCOPY(fp, pchan->pose_tail);
406                                                 }
407                                                 
408                                                 Mat4MulVecfl(ob->obmat, fp);
409                                         }
410                                 }
411                         }
412                 }
413         }
414         
415         /* restore original cursor */
416         WM_cursor_restore(win);
417         
418         /* reset current frame, and clear flags */
419         CFRA= cfra;
420         ob->pose->flag &= ~POSE_RECALCPATHS;
421         
422         /* flush one final time - to restore to the original state */
423         for (base= FIRSTBASE; base; base= base->next) {
424                 if (base->object->recalc) {
425                         int temp= base->object->recalc;
426                         
427                         if (base->object->adt)
428                                 BKE_animsys_evaluate_animdata(&base->object->id, base->object->adt, (float)CFRA, ADT_RECALC_ALL);
429                         
430                         object_handle_update(scene, base->object);
431                         base->object->recalc= temp;
432                 }
433         }
434         
435         /* notifiers for updates */
436         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
437         
438         return OPERATOR_FINISHED; 
439 }
440
441 void POSE_OT_paths_calculate (wmOperatorType *ot)
442 {
443         /* identifiers */
444         ot->name= "Calculate Bone Paths";
445         ot->idname= "POSE_OT_paths_calculate";
446         ot->description= "Calculate paths for the selected bones.";
447         
448         /* api callbacks */
449         ot->exec= pose_calculate_paths_exec;
450         ot->poll= ED_operator_posemode;
451         
452         /* flags */
453         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
454 }
455
456 /* --------- */
457
458 /* for the object with pose/action: clear path curves for selected bones only */
459 void ED_pose_clear_paths(Object *ob)
460 {
461         bPoseChannel *pchan;
462         
463         if ELEM(NULL, ob, ob->pose)
464                 return;
465         
466         /* free the path blocks */
467         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
468                 if ((pchan->bone) && (pchan->bone->flag & BONE_SELECTED)) {
469                         if (pchan->path) {
470                                 MEM_freeN(pchan->path);
471                                 pchan->path= NULL;
472                         }
473                 }
474         }
475 }
476
477 /* operator callback for this */
478 static int pose_clear_paths_exec (bContext *C, wmOperator *op)
479 {
480         ScrArea *sa= CTX_wm_area(C);
481         Object *ob;
482         
483         /* since this call may also be used from the buttons window, we need to check for where to get the object */
484         if (sa->spacetype == SPACE_BUTS) 
485                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
486         else
487                 ob= CTX_data_active_object(C);
488                 
489         /* only continue if there's an object */
490         if ELEM(NULL, ob, ob->pose)
491                 return OPERATOR_CANCELLED;
492         
493         /* for now, just call the API function for this (which is shared with backend functions) */
494         ED_pose_clear_paths(ob);
495         
496         /* notifiers for updates */
497         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
498         
499         return OPERATOR_FINISHED; 
500 }
501
502 void POSE_OT_paths_clear (wmOperatorType *ot)
503 {
504         /* identifiers */
505         ot->name= "Clear Bone Paths";
506         ot->idname= "POSE_OT_paths_clear";
507         ot->description= "Clear path caches for selected bones.";
508         
509         /* api callbacks */
510         ot->exec= pose_clear_paths_exec;
511         ot->poll= ED_operator_posemode;
512         
513         /* flags */
514         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
515 }
516
517 /* ******************* Select Constraint Target Operator ************* */
518
519 // XXX this function is to be removed when the other stuff is recoded
520 void pose_select_constraint_target(Scene *scene)
521 {
522         Object *obedit= scene->obedit; // XXX context
523         Object *ob= OBACT;
524         bArmature *arm= ob->data;
525         bPoseChannel *pchan;
526         bConstraint *con;
527         
528         /* paranoia checks */
529         if (!ob && !ob->pose) return;
530         if (ob==obedit || (ob->flag & OB_POSEMODE)==0) return;
531         
532         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
533                 if (arm->layer & pchan->bone->layer) {
534                         if (pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
535                                 for (con= pchan->constraints.first; con; con= con->next) {
536                                         bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
537                                         ListBase targets = {NULL, NULL};
538                                         bConstraintTarget *ct;
539                                         
540                                         if (cti && cti->get_constraint_targets) {
541                                                 cti->get_constraint_targets(con, &targets);
542                                                 
543                                                 for (ct= targets.first; ct; ct= ct->next) {
544                                                         if ((ct->tar == ob) && (ct->subtarget[0])) {
545                                                                 bPoseChannel *pchanc= get_pose_channel(ob->pose, ct->subtarget);
546                                                                 if(pchanc)
547                                                                         pchanc->bone->flag |= BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL;
548                                                         }
549                                                 }
550                                                 
551                                                 if (cti->flush_constraint_targets)
552                                                         cti->flush_constraint_targets(con, &targets, 1);
553                                         }
554                                 }
555                         }
556                 }
557         }
558         
559         BIF_undo_push("Select constraint target");
560
561 }
562
563 static int pose_select_constraint_target_exec(bContext *C, wmOperator *op)
564 {
565         Object *ob= CTX_data_active_object(C);
566         bArmature *arm= ob->data;
567         bPoseChannel *pchan;
568         bConstraint *con;
569         int found= 0;
570         
571         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
572                 if (arm->layer & pchan->bone->layer) {
573                         if (pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
574                                 for (con= pchan->constraints.first; con; con= con->next) {
575                                         bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
576                                         ListBase targets = {NULL, NULL};
577                                         bConstraintTarget *ct;
578                                         
579                                         if (cti && cti->get_constraint_targets) {
580                                                 cti->get_constraint_targets(con, &targets);
581                                                 
582                                                 for (ct= targets.first; ct; ct= ct->next) {
583                                                         if ((ct->tar == ob) && (ct->subtarget[0])) {
584                                                                 bPoseChannel *pchanc= get_pose_channel(ob->pose, ct->subtarget);
585                                                                 if(pchanc) {
586                                                                         pchanc->bone->flag |= BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL;
587                                                                         found= 1;
588                                                                 }
589                                                         }
590                                                 }
591                                                 
592                                                 if (cti->flush_constraint_targets)
593                                                         cti->flush_constraint_targets(con, &targets, 1);
594                                         }
595                                 }
596                         }
597                 }
598         }
599
600         if(!found)
601                 return OPERATOR_CANCELLED;
602
603         WM_event_add_notifier(C, NC_OBJECT|ND_BONE_SELECT, ob);
604
605         return OPERATOR_FINISHED;
606 }
607
608 void POSE_OT_select_constraint_target(wmOperatorType *ot)
609 {
610         /* identifiers */
611         ot->name= "Select Constraint Target";
612         ot->idname= "POSE_OT_select_constraint_target";
613         
614         /* api callbacks */
615         ot->exec= pose_select_constraint_target_exec;
616         ot->poll= ED_operator_posemode;
617         
618         /* flags */
619         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
620 }
621
622 /* ******************* select hierarchy operator ************* */
623
624 static int pose_select_hierarchy_exec(bContext *C, wmOperator *op)
625 {
626         Object *ob= CTX_data_active_object(C);
627         bArmature *arm= ob->data;
628         bPoseChannel *pchan;
629         Bone *curbone, *pabone, *chbone;
630         int direction = RNA_enum_get(op->ptr, "direction");
631         int add_to_sel = RNA_boolean_get(op->ptr, "extend");
632         int found= 0;
633         
634         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
635                 curbone= pchan->bone;
636                 
637                 if (arm->layer & curbone->layer) {
638                         if (curbone->flag & (BONE_ACTIVE)) {
639                                 if (direction == BONE_SELECT_PARENT) {
640                                 
641                                         if (pchan->parent == NULL) continue;
642                                         else pabone= pchan->parent->bone;
643                                         
644                                         if ((arm->layer & pabone->layer) && !(pabone->flag & BONE_HIDDEN_P)) {
645                                                 
646                                                 if (!add_to_sel) curbone->flag &= ~BONE_SELECTED;
647                                                 curbone->flag &= ~BONE_ACTIVE;
648                                                 pabone->flag |= (BONE_ACTIVE|BONE_SELECTED);
649
650                                                 found= 1;
651                                                 break;
652                                         }
653                                 } else { // BONE_SELECT_CHILD
654                                 
655                                         if (pchan->child == NULL) continue;
656                                         else chbone = pchan->child->bone;
657                                         
658                                         if ((arm->layer & chbone->layer) && !(chbone->flag & BONE_HIDDEN_P)) {
659                                         
660                                                 if (!add_to_sel) curbone->flag &= ~BONE_SELECTED;
661                                                 curbone->flag &= ~BONE_ACTIVE;
662                                                 chbone->flag |= (BONE_ACTIVE|BONE_SELECTED);
663
664                                                 found= 1;
665                                                 break;
666                                         }
667                                 }
668                         }
669                 }
670         }
671
672         if(!found)
673                 return OPERATOR_CANCELLED;
674
675         WM_event_add_notifier(C, NC_OBJECT|ND_BONE_SELECT, ob);
676
677         return OPERATOR_FINISHED;
678 }
679
680 void POSE_OT_select_hierarchy(wmOperatorType *ot)
681 {
682         static EnumPropertyItem direction_items[]= {
683         {BONE_SELECT_PARENT, "PARENT", 0, "Select Parent", ""},
684         {BONE_SELECT_CHILD, "CHILD", 0, "Select Child", ""},
685         {0, NULL, 0, NULL, NULL}
686         };
687         
688         /* identifiers */
689         ot->name= "Select Hierarchy";
690         ot->idname= "POSE_OT_select_hierarchy";
691         
692         /* api callbacks */
693         ot->exec= pose_select_hierarchy_exec;
694         ot->poll= ED_operator_posemode;
695         
696         /* flags */
697         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
698         
699         /* props */
700         RNA_def_enum(ot->srna, "direction", direction_items,
701                                  BONE_SELECT_PARENT, "Direction", "");
702         RNA_def_boolean(ot->srna, "extend", 0, "Add to Selection", "");
703         
704 }
705
706
707 void pose_copy_menu(Scene *scene)
708 {
709         Object *obedit= scene->obedit; // XXX context
710         Object *ob= OBACT;
711         bArmature *arm= ob->data;
712         bPoseChannel *pchan, *pchanact;
713         short nr=0;
714         int i=0;
715         
716         /* paranoia checks */
717         if (ELEM(NULL, ob, ob->pose)) return;
718         if ((ob==obedit) || (ob->flag & OB_POSEMODE)==0) return;
719         
720         /* find active */
721         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
722                 if (pchan->bone->flag & BONE_ACTIVE) 
723                         break;
724         }
725         
726         if (pchan==NULL) return;
727         pchanact= pchan;
728         
729         /* if proxy-protected bones selected, some things (such as locks + displays) shouldn't be changable, 
730          * but for constraints (just add local constraints)
731          */
732         if (pose_has_protected_selected(ob, 1, 0)) {
733                 i= BLI_countlist(&(pchanact->constraints)); /* if there are 24 or less, allow for the user to select constraints */
734                 if (i < 25)
735                         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");
736                 else
737                         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");
738         }
739         else {
740                 i= BLI_countlist(&(pchanact->constraints)); /* if there are 24 or less, allow for the user to select constraints */
741                 if (i < 25)
742                         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");
743                 else
744                         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");
745         }
746         
747         if (nr <= 0) 
748                 return;
749         
750         if (nr != 5)  {
751                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
752                         if ( (arm->layer & pchan->bone->layer) &&
753                                  (pchan->bone->flag & BONE_SELECTED) &&
754                                  (pchan != pchanact) ) 
755                         {
756                                 switch (nr) {
757                                         case 1: /* Local Location */
758                                                 VECCOPY(pchan->loc, pchanact->loc);
759                                                 break;
760                                         case 2: /* Local Rotation */
761                                                 QUATCOPY(pchan->quat, pchanact->quat);
762                                                 break;
763                                         case 3: /* Local Size */
764                                                 VECCOPY(pchan->size, pchanact->size);
765                                                 break;
766                                         case 4: /* All Constraints */
767                                         {
768                                                 ListBase tmp_constraints = {NULL, NULL};
769                                                 
770                                                 /* copy constraints to tmpbase and apply 'local' tags before 
771                                                  * appending to list of constraints for this channel
772                                                  */
773                                                 copy_constraints(&tmp_constraints, &pchanact->constraints);
774                                                 if ((ob->proxy) && (pchan->bone->layer & arm->layer_protected)) {
775                                                         bConstraint *con;
776                                                         
777                                                         /* add proxy-local tags */
778                                                         for (con= tmp_constraints.first; con; con= con->next)
779                                                                 con->flag |= CONSTRAINT_PROXY_LOCAL;
780                                                 }
781                                                 addlisttolist(&pchan->constraints, &tmp_constraints);
782                                                 
783                                                 /* update flags (need to add here, not just copy) */
784                                                 pchan->constflag |= pchanact->constflag;
785                                                 
786                                                 if (ob->pose)
787                                                         ob->pose->flag |= POSE_RECALC;
788                                         }
789                                                 break;
790                                         case 6: /* Transform Locks */
791                                                 pchan->protectflag = pchanact->protectflag;
792                                                 break;
793                                         case 7: /* IK (DOF) settings */
794                                         {
795                                                 pchan->ikflag = pchanact->ikflag;
796                                                 VECCOPY(pchan->limitmin, pchanact->limitmin);
797                                                 VECCOPY(pchan->limitmax, pchanact->limitmax);
798                                                 VECCOPY(pchan->stiffness, pchanact->stiffness);
799                                                 pchan->ikstretch= pchanact->ikstretch;
800                                         }
801                                                 break;
802                                         case 8: /* Custom Bone Shape */
803                                                 pchan->custom = pchanact->custom;
804                                                 break;
805                                         case 9: /* Visual Location */
806                                                 armature_loc_pose_to_bone(pchan, pchanact->pose_mat[3], pchan->loc);
807                                                 break;
808                                         case 10: /* Visual Rotation */
809                                         {
810                                                 float delta_mat[4][4], quat[4];
811                                                 
812                                                 armature_mat_pose_to_bone(pchan, pchanact->pose_mat, delta_mat);
813                                                 Mat4ToQuat(delta_mat, quat);
814                                                 QUATCOPY(pchan->quat, quat);
815                                         }
816                                                 break;
817                                         case 11: /* Visual Size */
818                                         {
819                                                 float delta_mat[4][4], size[4];
820                                                 
821                                                 armature_mat_pose_to_bone(pchan, pchanact->pose_mat, delta_mat);
822                                                 Mat4ToSize(delta_mat, size);
823                                                 VECCOPY(pchan->size, size);
824                                         }
825                                 }
826                         }
827                 }
828         } 
829         else { /* constraints, optional (note: max we can have is 24 constraints) */
830                 bConstraint *con, *con_back;
831                 int const_toggle[24];
832                 ListBase const_copy = {NULL, NULL};
833                 
834                 BLI_duplicatelist(&const_copy, &(pchanact->constraints));
835                 
836                 /* build the puplist of constraints */
837                 for (con = pchanact->constraints.first, i=0; con; con=con->next, i++){
838                         const_toggle[i]= 1;
839 //                      add_numbut(i, TOG|INT, con->name, 0, 0, &(const_toggle[i]), "");
840                 }
841                 
842 //              if (!do_clever_numbuts("Select Constraints", i, REDRAW)) {
843 //                      BLI_freelistN(&const_copy);
844 //                      return;
845 //              }
846                 
847                 /* now build a new listbase from the options selected */
848                 for (i=0, con=const_copy.first; con; i++) {
849                         /* if not selected, free/remove it from the list */
850                         if (!const_toggle[i]) {
851                                 con_back= con->next;
852                                 BLI_freelinkN(&const_copy, con);
853                                 con= con_back;
854                         } 
855                         else
856                                 con= con->next;
857                 }
858                 
859                 /* Copy the temo listbase to the selected posebones */
860                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
861                         if ( (arm->layer & pchan->bone->layer) &&
862                                  (pchan->bone->flag & BONE_SELECTED) &&
863                                  (pchan!=pchanact) ) 
864                         {
865                                 ListBase tmp_constraints = {NULL, NULL};
866                                 
867                                 /* copy constraints to tmpbase and apply 'local' tags before 
868                                  * appending to list of constraints for this channel
869                                  */
870                                 copy_constraints(&tmp_constraints, &const_copy);
871                                 if ((ob->proxy) && (pchan->bone->layer & arm->layer_protected)) {
872                                         bConstraint *con;
873                                         
874                                         /* add proxy-local tags */
875                                         for (con= tmp_constraints.first; con; con= con->next)
876                                                 con->flag |= CONSTRAINT_PROXY_LOCAL;
877                                 }
878                                 addlisttolist(&pchan->constraints, &tmp_constraints);
879                                 
880                                 /* update flags (need to add here, not just copy) */
881                                 pchan->constflag |= pchanact->constflag;
882                         }
883                 }
884                 BLI_freelistN(&const_copy);
885                 update_pose_constraint_flags(ob->pose); /* we could work out the flags but its simpler to do this */
886                 
887                 if (ob->pose)
888                         ob->pose->flag |= POSE_RECALC;
889         }
890         
891         DAG_object_flush_update(scene, ob, OB_RECALC_DATA);     // and all its relations
892         
893         BIF_undo_push("Copy Pose Attributes");
894         
895 }
896
897 /* ******************** copy/paste pose ********************** */
898
899 /* Global copy/paste buffer for pose - cleared on start/end session + before every copy operation */
900 static bPose *g_posebuf = NULL;
901
902 void free_posebuf(void) 
903 {
904         if (g_posebuf) {
905                 /* was copied without constraints */
906                 BLI_freelistN(&g_posebuf->chanbase);
907                 MEM_freeN(g_posebuf);
908         }
909         
910         g_posebuf=NULL;
911 }
912
913 /* ---- */
914
915 static int pose_copy_exec (bContext *C, wmOperator *op)
916 {
917         Object *ob= CTX_data_active_object(C);
918         
919         /* sanity checking */
920         if ELEM(NULL, ob, ob->pose) {
921                 BKE_report(op->reports, RPT_ERROR, "No Pose to Copy");
922                 return OPERATOR_CANCELLED;
923         }
924
925         /* free existing pose buffer */
926         free_posebuf();
927         
928         /* sets chan->flag to POSE_KEY if bone selected, then copy those bones to the buffer */
929         set_pose_keys(ob);  
930         copy_pose(&g_posebuf, ob->pose, 0);
931         
932         
933         return OPERATOR_FINISHED;
934 }
935
936 void POSE_OT_copy (wmOperatorType *ot) 
937 {
938         /* identifiers */
939         ot->name= "Copy Pose";
940         ot->idname= "POSE_OT_copy";
941         ot->description= "Copies the current pose of the selected bones to copy/paste buffer.";
942         
943         /* api callbacks */
944         ot->exec= pose_copy_exec;
945         ot->poll= ED_operator_posemode;
946         
947         /* flag */
948         ot->flag= OPTYPE_REGISTER;
949 }
950
951 /* ---- */
952
953 static int pose_paste_exec (bContext *C, wmOperator *op)
954 {
955         Scene *scene= CTX_data_scene(C);
956         Object *ob= CTX_data_active_object(C);
957         bPoseChannel *chan, *pchan;
958         char name[32];
959         int flip= RNA_boolean_get(op->ptr, "flipped");
960         
961         /* sanity checks */
962         if ELEM(NULL, ob, ob->pose)
963                 return OPERATOR_CANCELLED;
964
965         if (g_posebuf == NULL) {
966                 BKE_report(op->reports, RPT_ERROR, "Copy buffer is empty");
967                 return OPERATOR_CANCELLED;
968         }
969         
970         /* Safely merge all of the channels in the buffer pose into any existing pose */
971         for (chan= g_posebuf->chanbase.first; chan; chan=chan->next) {
972                 if (chan->flag & POSE_KEY) {
973                         /* get the name - if flipping, we must flip this first */
974                         BLI_strncpy(name, chan->name, sizeof(name));
975                         if (flip)
976                                 bone_flip_name(name, 0);                /* 0 = don't strip off number extensions */
977                                 
978                         /* only copy when channel exists, poses are not meant to add random channels to anymore */
979                         pchan= get_pose_channel(ob->pose, name);
980                         
981                         if (pchan) {
982                                 /* only loc rot size 
983                                  *      - only copies transform info for the pose 
984                                  */
985                                 VECCOPY(pchan->loc, chan->loc);
986                                 VECCOPY(pchan->size, chan->size);
987                                 pchan->flag= chan->flag;
988                                 
989                                 /* check if rotation modes are compatible (i.e. do they need any conversions) */
990                                 if (pchan->rotmode == chan->rotmode) {
991                                         /* copy the type of rotation in use */
992                                         if (pchan->rotmode) {
993                                                 VECCOPY(pchan->eul, chan->eul);
994                                         }
995                                         else {
996                                                 QUATCOPY(pchan->quat, chan->quat);
997                                         }
998                                 }
999                                 else if (pchan->rotmode) {
1000                                         /* quat to euler */
1001                                         QuatToEul(chan->quat, pchan->eul);
1002                                 }
1003                                 else {
1004                                         /* euler to quat */
1005                                         EulToQuat(chan->eul, pchan->quat);
1006                                 }
1007                                 
1008                                 /* paste flipped pose? */
1009                                 if (flip) {
1010                                         pchan->loc[0]*= -1;
1011                                         
1012                                         /* has to be done as eulers... */
1013                                         if (pchan->rotmode) {
1014                                                 pchan->eul[1] *= -1;
1015                                                 pchan->eul[2] *= -1;
1016                                         }
1017                                         else {
1018                                                 float eul[3];
1019                                                 
1020                                                 QuatToEul(pchan->quat, eul);
1021                                                 eul[1]*= -1;
1022                                                 eul[2]*= -1;
1023                                                 EulToQuat(eul, pchan->quat);
1024                                         }
1025                                 }
1026                                 
1027 #if 0 // XXX old animation system
1028                                 if (autokeyframe_cfra_can_key(ob)) {
1029                                         ID *id= &ob->id;
1030                                         
1031                                         /* Set keys on pose */
1032                                         // TODO: make these use keyingsets....
1033                                         if (chan->flag & POSE_ROT) {
1034                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_X, 0);
1035                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_Y, 0);
1036                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_Z, 0);
1037                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_W, 0);
1038                                         }
1039                                         if (chan->flag & POSE_SIZE) {
1040                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_SIZE_X, 0);
1041                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_SIZE_Y, 0);
1042                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_SIZE_Z, 0);
1043                                         }
1044                                         if (chan->flag & POSE_LOC) {
1045                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_LOC_X, 0);
1046                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_LOC_Y, 0);
1047                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_LOC_Z, 0);
1048                                         }
1049                                         
1050                                         /* clear any unkeyed tags */
1051                                         if (chan->bone)
1052                                                 chan->bone->flag &= ~BONE_UNKEYED;
1053                                 }
1054                                 else {
1055                                         /* add unkeyed tags */
1056                                         if (chan->bone)
1057                                                 chan->bone->flag |= BONE_UNKEYED;
1058                                 }
1059 #endif // XXX old animation system
1060                         }
1061                 }
1062         }
1063
1064         /* Update event for pose and deformation children */
1065         DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
1066         
1067         if (IS_AUTOKEY_ON(scene)) {
1068 // XXX          remake_action_ipos(ob->action);
1069         }
1070         else {
1071                 /* need to trick depgraph, action is not allowed to execute on pose */
1072                 where_is_pose(scene, ob);
1073                 ob->recalc= 0;
1074         }
1075         
1076         /* notifiers for updates */
1077         WM_event_add_notifier(C, NC_OBJECT|ND_POSE|ND_TRANSFORM, ob);
1078
1079         return OPERATOR_FINISHED;
1080 }
1081
1082 void POSE_OT_paste (wmOperatorType *ot)
1083 {
1084         /* identifiers */
1085         ot->name= "Paste Pose";
1086         ot->idname= "POSE_OT_paste";
1087         ot->description= "Pastes the stored pose on to the current pose.";
1088         
1089         /* api callbacks */
1090         ot->exec= pose_paste_exec;
1091         ot->poll= ED_operator_posemode;
1092         
1093         /* flag */
1094         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1095         
1096         /* properties */
1097         RNA_def_boolean(ot->srna, "flipped", 0, "Flipped on X-Axis", "");
1098 }
1099
1100 /* ********************************************** */
1101
1102 /* context weightpaint and deformer in posemode */
1103 void pose_adds_vgroups(Scene *scene, Object *meshobj, int heatweights)
1104 {
1105 // XXX  extern VPaint Gwp;         /* from vpaint */
1106         Object *poseobj= modifiers_isDeformedByArmature(meshobj);
1107
1108         if(poseobj==NULL || (poseobj->flag & OB_POSEMODE)==0) {
1109                 error("The active object must have a deforming armature in pose mode");
1110                 return;
1111         }
1112
1113 // XXX  add_verts_to_dgroups(meshobj, poseobj, heatweights, (Gwp.flag & VP_MIRROR_X));
1114
1115         if(heatweights)
1116                 BIF_undo_push("Apply Bone Heat Weights to Vertex Groups");
1117         else
1118                 BIF_undo_push("Apply Bone Envelopes to Vertex Groups");
1119
1120         
1121         // and all its relations
1122         DAG_object_flush_update(scene, meshobj, OB_RECALC_DATA);
1123 }
1124
1125 /* ********************************************** */
1126
1127
1128 static int pose_group_add_exec (bContext *C, wmOperator *op)
1129 {
1130         ScrArea *sa= CTX_wm_area(C);
1131         Object *ob;
1132         
1133         /* since this call may also be used from the buttons window, we need to check for where to get the object */
1134         if (sa->spacetype == SPACE_BUTS) 
1135                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1136         else
1137                 ob= CTX_data_active_object(C);
1138                 
1139         /* only continue if there's an object */
1140         if (ob == NULL)
1141                 return OPERATOR_CANCELLED;
1142         
1143         /* for now, just call the API function for this */
1144         pose_add_group(ob);
1145         
1146         /* notifiers for updates */
1147         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1148         
1149         return OPERATOR_FINISHED;
1150 }
1151
1152 void POSE_OT_group_add (wmOperatorType *ot)
1153 {
1154         /* identifiers */
1155         ot->name= "Add Bone Group";
1156         ot->idname= "POSE_OT_group_add";
1157         ot->description= "Add a new bone group.";
1158         
1159         /* api callbacks */
1160         ot->exec= pose_group_add_exec;
1161         ot->poll= ED_operator_posemode;
1162         
1163         /* flags */
1164         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1165 }
1166
1167
1168 static int pose_group_remove_exec (bContext *C, wmOperator *op)
1169 {
1170         ScrArea *sa= CTX_wm_area(C);
1171         Object *ob;
1172         
1173         /* since this call may also be used from the buttons window, we need to check for where to get the object */
1174         if (sa->spacetype == SPACE_BUTS) 
1175                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1176         else
1177                 ob= CTX_data_active_object(C);
1178         
1179         /* only continue if there's an object */
1180         if (ob == NULL)
1181                 return OPERATOR_CANCELLED;
1182         
1183         /* for now, just call the API function for this */
1184         pose_remove_group(ob);
1185         
1186         /* notifiers for updates */
1187         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1188         
1189         return OPERATOR_FINISHED;
1190 }
1191
1192 void POSE_OT_group_remove (wmOperatorType *ot)
1193 {
1194         /* identifiers */
1195         ot->name= "Remove Bone Group";
1196         ot->idname= "POSE_OT_group_remove";
1197         ot->description= "Removes the active bone group.";
1198         
1199         /* api callbacks */
1200         ot->exec= pose_group_remove_exec;
1201         ot->poll= ED_operator_posemode;
1202         
1203         /* flags */
1204         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1205 }
1206
1207 /* ------------ */
1208
1209 /* invoke callback which presents a list of bone-groups for the user to choose from */
1210 static int pose_groups_menu_invoke (bContext *C, wmOperator *op, wmEvent *evt)
1211 {
1212         ScrArea *sa= CTX_wm_area(C);
1213         Object *ob;
1214         bPose *pose;
1215         
1216         uiPopupMenu *pup;
1217         uiLayout *layout;
1218         bActionGroup *grp;
1219         int i;
1220         
1221         /* since this call may also be used from the buttons window, we need to check for where to get the object */
1222         if (sa->spacetype == SPACE_BUTS) 
1223                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1224         else
1225                 ob= CTX_data_active_object(C);
1226         
1227         /* only continue if there's an object, and a pose there too */
1228         if (ELEM(NULL, ob, ob->pose)) 
1229                 return OPERATOR_CANCELLED;
1230         pose= ob->pose;
1231         
1232         /* if there's no active group (or active is invalid), create a new menu to find it */
1233         if (pose->active_group <= 0) {
1234                 /* create a new menu, and start populating it with group names */
1235                 pup= uiPupMenuBegin(C, op->type->name, 0);
1236                 layout= uiPupMenuLayout(pup);
1237                 
1238                 /* special entry - allow to create new group, then use that 
1239                  *      (not to be used for removing though)
1240                  */
1241                 if (strstr(op->idname, "assign")) {
1242                         uiItemIntO(layout, "New Group", 0, op->idname, "type", 0);
1243                         uiItemS(layout);
1244                 }
1245                 
1246                 /* add entries for each group */
1247                 for (grp= pose->agroups.first, i=1; grp; grp=grp->next, i++)
1248                         uiItemIntO(layout, grp->name, 0, op->idname, "type", i);
1249                         
1250                 /* finish building the menu, and process it (should result in calling self again) */
1251                 uiPupMenuEnd(C, pup);
1252                 
1253                 return OPERATOR_CANCELLED;
1254         }
1255         else {
1256                 /* just use the active group index, and call the exec callback for the calling operator */
1257                 RNA_int_set(op->ptr, "type", pose->active_group);
1258                 return op->type->exec(C, op);
1259         }
1260 }
1261
1262 /* Assign selected pchans to the bone group that the user selects */
1263 static int pose_group_assign_exec (bContext *C, wmOperator *op)
1264 {
1265         ScrArea *sa= CTX_wm_area(C);
1266         Object *ob;
1267         bArmature *arm;
1268         bPose *pose;
1269         bPoseChannel *pchan;
1270         short done= 0;
1271         
1272         /* since this call may also be used from the buttons window, we need to check for where to get the object */
1273         if (sa->spacetype == SPACE_BUTS) 
1274                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1275         else
1276                 ob= CTX_data_active_object(C);
1277         
1278         /* only continue if there's an object, and a pose there too */
1279         if (ELEM(NULL, ob, ob->pose))
1280                 return OPERATOR_CANCELLED;
1281         arm= ob->data;
1282         pose= ob->pose;
1283         
1284         /* set the active group number to the one from operator props 
1285          *      - if 0 after this, make a new group...
1286          */
1287         pose->active_group= RNA_int_get(op->ptr, "type");
1288         if (pose->active_group == 0)
1289                 pose_add_group(ob);
1290         
1291         /* add selected bones to group then */
1292         // NOTE: unfortunately, we cannot use the context-iterators here, since they might not be defined...
1293         // CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pchans) 
1294         for (pchan= pose->chanbase.first; pchan; pchan= pchan->next) {
1295                 /* ensure that PoseChannel is on visible layer and is not hidden in PoseMode */
1296                 // NOTE: sync this view3d_context() in space_view3d.c
1297                 if ((pchan->bone) && (arm->layer & pchan->bone->layer) && !(pchan->bone->flag & BONE_HIDDEN_P)) {
1298                         if (pchan->bone->flag & (BONE_SELECTED|BONE_ACTIVE)) {
1299                                 pchan->agrp_index= pose->active_group;
1300                                 done= 1;
1301                         }
1302                 }
1303         }
1304         
1305         /* notifiers for updates */
1306         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1307         
1308         /* report done status */
1309         if (done)
1310                 return OPERATOR_FINISHED;
1311         else
1312                 return OPERATOR_CANCELLED;
1313 }
1314
1315 void POSE_OT_group_assign (wmOperatorType *ot)
1316 {
1317         /* identifiers */
1318         ot->name= "Add Selected to Bone Group";
1319         ot->idname= "POSE_OT_group_assign";
1320         ot->description= "Add selected bones to the chosen bone group.";
1321         
1322         /* api callbacks */
1323         ot->invoke= pose_groups_menu_invoke;
1324         ot->exec= pose_group_assign_exec;
1325         ot->poll= ED_operator_posemode;
1326         
1327         /* flags */
1328         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1329         
1330         /* properties */
1331         RNA_def_int(ot->srna, "type", 0, 0, 10, "Bone Group Index", "", 0, INT_MAX);
1332 }
1333
1334
1335 static int pose_group_unassign_exec (bContext *C, wmOperator *op)
1336 {
1337         ScrArea *sa= CTX_wm_area(C);
1338         Object *ob;
1339         bArmature *arm;
1340         bPose *pose;
1341         bPoseChannel *pchan;
1342         short done= 0;
1343         
1344         /* since this call may also be used from the buttons window, we need to check for where to get the object */
1345         if (sa->spacetype == SPACE_BUTS) 
1346                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1347         else
1348                 ob= CTX_data_active_object(C);
1349         
1350         /* only continue if there's an object, and a pose there too */
1351         if (ELEM(NULL, ob, ob->pose))
1352                 return OPERATOR_CANCELLED;
1353         pose= ob->pose;
1354         arm= ob->data;
1355         
1356         /* add selected bones to ungroup then */
1357         // NOTE: unfortunately, we cannot use the context-iterators here, since they might not be defined...
1358         // CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pchans) 
1359         for (pchan= pose->chanbase.first; pchan; pchan= pchan->next) {
1360                 /* ensure that PoseChannel is on visible layer and is not hidden in PoseMode */
1361                 // NOTE: sync this view3d_context() in space_view3d.c
1362                 if ((pchan->bone) && (arm->layer & pchan->bone->layer) && !(pchan->bone->flag & BONE_HIDDEN_P)) {
1363                         if (pchan->bone->flag & (BONE_SELECTED|BONE_ACTIVE)) {
1364                                 if (pchan->agrp_index) {
1365                                         pchan->agrp_index= 0;
1366                                         done= 1;
1367                                 }
1368                         }
1369                 }
1370         }
1371         
1372         /* notifiers for updates */
1373         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1374         
1375         /* report done status */
1376         if (done)
1377                 return OPERATOR_FINISHED;
1378         else
1379                 return OPERATOR_CANCELLED;
1380 }
1381
1382 void POSE_OT_group_unassign (wmOperatorType *ot)
1383 {
1384         /* identifiers */
1385         ot->name= "Remove Selected from Bone Groups";
1386         ot->idname= "POSE_OT_group_unassign";
1387         ot->description= "Add selected bones from all bone groups";
1388         
1389         /* api callbacks */
1390         ot->exec= pose_group_unassign_exec;
1391         ot->poll= ED_operator_posemode;
1392         
1393         /* flags */
1394         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1395 }
1396
1397 /* ----------------- */
1398
1399 static int pose_groupOps_menu_invoke (bContext *C, wmOperator *op, wmEvent *evt)
1400 {
1401         Object *ob= CTX_data_active_object(C);
1402         uiPopupMenu *pup= uiPupMenuBegin(C, op->type->name, 0);
1403         uiLayout *layout= uiPupMenuLayout(pup);
1404         
1405         /* sanity check - must have object with pose */
1406         if ELEM(NULL, ob, ob->pose)
1407                 return OPERATOR_CANCELLED;
1408         
1409         /* get mode of action */
1410         if (CTX_DATA_COUNT(C, selected_pchans)) {
1411                 /* if selected bone(s), include options to add/remove to active group */
1412                 uiItemO(layout, "Add Selected to Active Group", 0, "POSE_OT_group_assign");
1413                 
1414                 uiItemS(layout);
1415                 
1416                 uiItemO(layout, "Remove Selected from All Groups", 0, "POSE_OT_group_unassign");
1417                 uiItemO(layout, "Remove Active Group", 0, "POSE_OT_group_remove");
1418         }
1419         else {
1420                 /* no selected bones - so just options for groups management */
1421                 uiItemO(layout, "Add New Group", 0, "POSE_OT_group_add");
1422                 uiItemO(layout, "Remove Active Group", 0, "POSE_OT_group_remove");
1423         }
1424                 
1425         return OPERATOR_CANCELLED;
1426 }
1427
1428 void POSE_OT_groups_menu (wmOperatorType *ot)
1429 {
1430         /* identifiers */
1431         ot->name= "Bone Group Tools";
1432         ot->idname= "POSE_OT_groups_menu";
1433         ot->description= "Menu displaying available tools for Bone Groups.";
1434         
1435         /* api callbacks (only invoke needed) */
1436         ot->invoke= pose_groupOps_menu_invoke;
1437         ot->poll= ED_operator_posemode;
1438         
1439         /* flags */
1440         ot->flag= OPTYPE_REGISTER;
1441 }
1442
1443 /* ********************************************** */
1444
1445 static short pose_select_same_group (Object *ob)
1446 {
1447         bPose *pose= (ob)? ob->pose : NULL;
1448         bArmature *arm= (ob)? ob->data : NULL;
1449         bPoseChannel *pchan, *chan;
1450         short changed= 0;
1451         
1452         if (ELEM3(NULL, ob, pose, arm))
1453                 return 0;
1454         
1455         /* loop in loop... bad and slow! */
1456         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1457                 if (arm->layer & pchan->bone->layer) {
1458                         if (pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
1459                                 
1460                                 /* only if group matches (and is not selected or current bone) */
1461                                 for (chan= ob->pose->chanbase.first; chan; chan= chan->next) {
1462                                         if (arm->layer & chan->bone->layer) {
1463                                                 if (pchan->agrp_index == chan->agrp_index) {
1464                                                         chan->bone->flag |= BONE_SELECTED;
1465                                                         changed= 1;
1466                                                 }
1467                                         }
1468                                 }
1469                                 
1470                         }
1471                 }
1472         }
1473         
1474         return changed;
1475 }
1476
1477 static short pose_select_same_layer (Object *ob)
1478 {
1479         bPose *pose= (ob)? ob->pose : NULL;
1480         bArmature *arm= (ob)? ob->data : NULL;
1481         bPoseChannel *pchan;
1482         short layers= 0, changed= 0;
1483         
1484         if (ELEM3(NULL, ob, pose, arm))
1485                 return 0;
1486         
1487         /* figure out what bones are selected */
1488         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1489                 if (arm->layer & pchan->bone->layer) {
1490                         if (pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
1491                                 layers |= pchan->bone->layer;
1492                         }
1493                 }
1494         }
1495         if (layers == 0) 
1496                 return 0;
1497                 
1498         /* select bones that are on same layers as layers flag */
1499         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1500                 if (arm->layer & pchan->bone->layer) {
1501                         if (layers & pchan->bone->layer) {
1502                                 pchan->bone->flag |= BONE_SELECTED;
1503                                 changed= 1;
1504                         }
1505                 }
1506         }
1507         
1508         return changed;
1509 }
1510
1511 void pose_select_grouped (Scene *scene, short nr)
1512 {
1513         short changed = 0;
1514         
1515         if (nr == 1)            changed= pose_select_same_group(OBACT);
1516         else if (nr == 2)       changed= pose_select_same_layer(OBACT);
1517         
1518         if (changed) {
1519                 countall();
1520                 BIF_undo_push("Select Grouped");
1521         }
1522 }
1523
1524 /* Shift-G in 3D-View while in PoseMode */
1525 void pose_select_grouped_menu (Scene *scene)
1526 {
1527         short nr;
1528         
1529         /* here we go */
1530         nr= pupmenu("Select Grouped%t|In Same Group%x1|In Same Layer%x2");
1531         pose_select_grouped(scene, nr);
1532 }
1533
1534 /* ********************************************** */
1535
1536 static int pose_flip_names_exec (bContext *C, wmOperator *op)
1537 {
1538         Scene *scene= CTX_data_scene(C);
1539         Object *ob= CTX_data_active_object(C);
1540         bArmature *arm;
1541         char newname[32];
1542         
1543         /* paranoia checks */
1544         if (ELEM(NULL, ob, ob->pose)) 
1545                 return OPERATOR_CANCELLED;
1546         arm= ob->data;
1547         
1548         /* loop through selected bones, auto-naming them */
1549         CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pchans)
1550         {
1551                 BLI_strncpy(newname, pchan->name, sizeof(newname));
1552                 bone_flip_name(newname, 1);     // 1 = do strip off number extensions
1553                 ED_armature_bone_rename(arm, pchan->name, newname);
1554         }
1555         CTX_DATA_END;
1556         
1557         /* since we renamed stuff... */
1558         DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
1559
1560         /* note, notifier might evolve */
1561         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1562         
1563         return OPERATOR_FINISHED;
1564 }
1565
1566 void POSE_OT_flip_names (wmOperatorType *ot)
1567 {
1568         /* identifiers */
1569         ot->name= "Flip Names";
1570         ot->idname= "POSE_OT_flip_names";
1571         ot->description= "Flips (and corrects) the names of selected bones.";
1572         
1573         /* api callbacks */
1574         ot->exec= pose_flip_names_exec;
1575         ot->poll= ED_operator_posemode;
1576         
1577         /* flags */
1578         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1579 }
1580
1581 /* ------------------ */
1582
1583 static int pose_autoside_names_exec (bContext *C, wmOperator *op)
1584 {
1585         Scene *scene= CTX_data_scene(C);
1586         Object *ob= CTX_data_active_object(C);
1587         bArmature *arm;
1588         char newname[32];
1589         short axis= RNA_enum_get(op->ptr, "axis");
1590         
1591         /* paranoia checks */
1592         if (ELEM(NULL, ob, ob->pose)) 
1593                 return OPERATOR_CANCELLED;
1594         arm= ob->data;
1595         
1596         /* loop through selected bones, auto-naming them */
1597         CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pchans)
1598         {
1599                 BLI_strncpy(newname, pchan->name, sizeof(newname));
1600                 bone_autoside_name(newname, 1, axis, pchan->bone->head[axis], pchan->bone->tail[axis]);
1601                 ED_armature_bone_rename(arm, pchan->name, newname);
1602         }
1603         CTX_DATA_END;
1604         
1605         /* since we renamed stuff... */
1606         DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
1607
1608         /* note, notifier might evolve */
1609         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1610         
1611         return OPERATOR_FINISHED;
1612 }
1613
1614 void POSE_OT_autoside_names (wmOperatorType *ot)
1615 {
1616         static EnumPropertyItem axis_items[]= {
1617                 {0, "XAXIS", 0, "X-Axis", "Left/Right"},
1618                 {1, "YAXIS", 0, "Y-Axis", "Front/Back"},
1619                 {2, "ZAXIS", 0, "Z-Axis", "Top/Bottom"},
1620                 {0, NULL, 0, NULL, NULL}};
1621         
1622         /* identifiers */
1623         ot->name= "AutoName by Axis";
1624         ot->idname= "POSE_OT_autoside_names";
1625         ot->description= "Automatically renames the selected bones according to which side of the target axis they fall on.";
1626         
1627         /* api callbacks */
1628         ot->invoke= WM_menu_invoke;
1629         ot->exec= pose_autoside_names_exec;
1630         ot->poll= ED_operator_posemode;
1631         
1632         /* flags */
1633         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1634         
1635         /* settings */
1636         RNA_def_enum(ot->srna, "axis", axis_items, 0, "Axis", "Axis tag names with.");
1637 }
1638
1639 /* ********************************************** */
1640
1641 /* context active object, or weightpainted object with armature in posemode */
1642 void pose_activate_flipped_bone(Scene *scene)
1643 {
1644         Object *ob= OBACT;
1645         bArmature *arm= ob->data;
1646         
1647         if(ob==NULL) return;
1648
1649         if(G.f & G_WEIGHTPAINT) {
1650                 ob= modifiers_isDeformedByArmature(ob);
1651         }
1652         if(ob && (ob->flag & OB_POSEMODE)) {
1653                 bPoseChannel *pchan, *pchanf;
1654                 
1655                 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1656                         if(arm->layer & pchan->bone->layer) {
1657                                 if(pchan->bone->flag & BONE_ACTIVE)
1658                                         break;
1659                         }
1660                 }
1661                 if(pchan) {
1662                         char name[32];
1663                         
1664                         BLI_strncpy(name, pchan->name, 32);
1665                         bone_flip_name(name, 1);        // 0 = do not strip off number extensions
1666                         
1667                         pchanf= get_pose_channel(ob->pose, name);
1668                         if(pchanf && pchanf!=pchan) {
1669                                 pchan->bone->flag &= ~(BONE_SELECTED|BONE_ACTIVE);
1670                                 pchanf->bone->flag |= (BONE_SELECTED|BONE_ACTIVE);
1671                         
1672                                 /* in weightpaint we select the associated vertex group too */
1673                                 if(G.f & G_WEIGHTPAINT) {
1674                                         vertexgroup_select_by_name(OBACT, name);
1675                                         DAG_object_flush_update(scene, OBACT, OB_RECALC_DATA);
1676                                 }
1677                                 
1678                                 // XXX notifiers need to be sent to other editors to update
1679                                 
1680                         }                       
1681                 }
1682         }
1683 }
1684
1685
1686 /* ********************************************** */
1687
1688 /* Present a popup to get the layers that should be used */
1689 // TODO: move to wm?
1690 static uiBlock *wm_layers_select_create_menu(bContext *C, ARegion *ar, void *arg_op)
1691 {
1692         wmOperator *op= arg_op;
1693         uiBlock *block;
1694         uiLayout *layout;
1695         uiStyle *style= U.uistyles.first;
1696         
1697         block= uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
1698         uiBlockClearFlag(block, UI_BLOCK_LOOP);
1699         uiBlockSetFlag(block, UI_BLOCK_KEEP_OPEN);
1700         
1701         layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 0, 0, 150, 20, style);
1702                 uiItemL(layout, op->type->name, 0);
1703                 uiTemplateLayers(layout, op->ptr, "layers"); /* must have a property named layers setup */
1704                 
1705         uiPopupBoundsBlock(block, 4.0f, 0, 0);
1706         uiEndBlock(C, block);
1707         
1708         return block;
1709 }
1710
1711 /* ------------------- */
1712
1713 /* Present a popup to get the layers that should be used */
1714 static int pose_armature_layers_invoke (bContext *C, wmOperator *op, wmEvent *evt)
1715 {
1716         Object *ob= CTX_data_active_object(C);
1717         bArmature *arm= (ob)? ob->data : NULL;
1718         PointerRNA ptr;
1719         int layers[16]; /* hardcoded for now - we can only have 16 armature layers, so this should be fine... */
1720         
1721         /* sanity checking */
1722         if (arm == NULL)
1723                 return OPERATOR_CANCELLED;
1724                 
1725         /* get RNA pointer to armature data to use that to retrieve the layers as ints to init the operator */
1726         RNA_id_pointer_create((ID *)arm, &ptr);
1727         RNA_boolean_get_array(&ptr, "layer", layers);
1728         RNA_boolean_set_array(op->ptr, "layers", layers);
1729         
1730                 /* part to sync with other similar operators... */
1731         /* pass on operator, so return modal */
1732         uiPupBlockOperator(C, wm_layers_select_create_menu, op, WM_OP_EXEC_DEFAULT);
1733         return OPERATOR_RUNNING_MODAL|OPERATOR_PASS_THROUGH;
1734 }
1735
1736 /* Set the visible layers for the active armature (edit and pose modes) */
1737 static int pose_armature_layers_exec (bContext *C, wmOperator *op)
1738 {
1739         Object *ob= CTX_data_active_object(C);
1740         bArmature *arm= (ob)? ob->data : NULL;
1741         PointerRNA ptr;
1742         int layers[16]; /* hardcoded for now - we can only have 16 armature layers, so this should be fine... */
1743         
1744         /* get the values set in the operator properties */
1745         RNA_boolean_get_array(op->ptr, "layers", layers);
1746         
1747         /* get pointer for armature, and write data there... */
1748         RNA_id_pointer_create((ID *)arm, &ptr);
1749         RNA_boolean_set_array(&ptr, "layer", layers);
1750         
1751         /* note, notifier might evolve */
1752         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1753         
1754         return OPERATOR_FINISHED;
1755 }
1756
1757
1758 void POSE_OT_armature_layers (wmOperatorType *ot)
1759 {
1760         /* identifiers */
1761         ot->name= "Change Armature Layers";
1762         ot->idname= "POSE_OT_armature_layers";
1763         ot->description= "Change the visible armature layers.";
1764         
1765         /* callbacks */
1766         ot->invoke= pose_armature_layers_invoke;
1767         ot->exec= pose_armature_layers_exec;
1768         ot->poll= ED_operator_posemode;
1769         
1770         /* flags */
1771         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1772         
1773         /* properties */
1774         RNA_def_boolean_array(ot->srna, "layers", 16, NULL, "Layers", "Armature layers to make visible.");
1775 }
1776
1777 void ARMATURE_OT_armature_layers (wmOperatorType *ot)
1778 {
1779         /* identifiers */
1780         ot->name= "Change Armature Layers";
1781         ot->idname= "ARMATURE_OT_armature_layers";
1782         ot->description= "Change the visible armature layers.";
1783         
1784         /* callbacks */
1785         ot->invoke= pose_armature_layers_invoke;
1786         ot->exec= pose_armature_layers_exec;
1787         ot->poll= ED_operator_editarmature;
1788         
1789         /* flags */
1790         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1791         
1792         /* properties */
1793         RNA_def_boolean_array(ot->srna, "layers", 16, NULL, "Layers", "Armature layers to make visible.");
1794 }
1795
1796 /* ------------------- */
1797
1798 /* Present a popup to get the layers that should be used */
1799 static int pose_bone_layers_invoke (bContext *C, wmOperator *op, wmEvent *evt)
1800 {
1801         int layers[16]; /* hardcoded for now - we can only have 16 armature layers, so this should be fine... */
1802         
1803         /* get layers that are active already */
1804         memset(&layers, 0, sizeof(layers)); /* set all layers to be off by default */
1805         
1806         CTX_DATA_BEGIN(C, bPoseChannel *, pchan, selected_pchans) 
1807         {
1808                 short bit;
1809                 
1810                 /* loop over the bits for this pchan's layers, adding layers where they're needed */
1811                 for (bit= 0; bit < 16; bit++) {
1812                         if (pchan->bone->layer & (1<<bit))
1813                                 layers[bit]= 1;
1814                 }
1815         }
1816         CTX_DATA_END;
1817         
1818         /* copy layers to operator */
1819         RNA_boolean_set_array(op->ptr, "layers", layers);
1820         
1821                 /* part to sync with other similar operators... */
1822         /* pass on operator, so return modal */
1823         uiPupBlockOperator(C, wm_layers_select_create_menu, op, WM_OP_EXEC_DEFAULT);
1824         return OPERATOR_RUNNING_MODAL|OPERATOR_PASS_THROUGH;
1825 }
1826
1827 /* Set the visible layers for the active armature (edit and pose modes) */
1828 static int pose_bone_layers_exec (bContext *C, wmOperator *op)
1829 {
1830         Object *ob= CTX_data_active_object(C);
1831         bArmature *arm= (ob)? ob->data : NULL;
1832         PointerRNA ptr;
1833         int layers[16]; /* hardcoded for now - we can only have 16 armature layers, so this should be fine... */
1834         
1835         /* get the values set in the operator properties */
1836         RNA_boolean_get_array(op->ptr, "layers", layers);
1837         
1838         /* set layers of pchans based on the values set in the operator props */
1839         CTX_DATA_BEGIN(C, bPoseChannel *, pchan, selected_pchans) 
1840         {
1841                 /* get pointer for pchan, and write flags this way */
1842                 RNA_pointer_create((ID *)arm, &RNA_Bone, pchan->bone, &ptr);
1843                 RNA_boolean_set_array(&ptr, "layer", layers);
1844         }
1845         CTX_DATA_END;
1846         
1847         /* note, notifier might evolve */
1848         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1849         
1850         return OPERATOR_FINISHED;
1851 }
1852
1853 void POSE_OT_bone_layers (wmOperatorType *ot)
1854 {
1855         /* identifiers */
1856         ot->name= "Change Bone Layers";
1857         ot->idname= "POSE_OT_bone_layers";
1858         ot->description= "Change the layers that the selected bones belong to.";
1859         
1860         /* callbacks */
1861         ot->invoke= pose_bone_layers_invoke;
1862         ot->exec= pose_bone_layers_exec;
1863         ot->poll= ED_operator_posemode;
1864         
1865         /* flags */
1866         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1867         
1868         /* properties */
1869         RNA_def_boolean_array(ot->srna, "layers", 16, NULL, "Layers", "Armature layers that bone belongs to.");
1870 }
1871
1872 /* ------------------- */
1873
1874 /* Present a popup to get the layers that should be used */
1875 static int armature_bone_layers_invoke (bContext *C, wmOperator *op, wmEvent *evt)
1876 {
1877         int layers[16]; /* hardcoded for now - we can only have 16 armature layers, so this should be fine... */
1878         
1879         /* get layers that are active already */
1880         memset(&layers, 0, sizeof(layers)); /* set all layers to be off by default */
1881         
1882         CTX_DATA_BEGIN(C, EditBone *, ebone, selected_editable_bones) 
1883         {
1884                 short bit;
1885                 
1886                 /* loop over the bits for this pchan's layers, adding layers where they're needed */
1887                 for (bit= 0; bit < 16; bit++) {
1888                         if (ebone->layer & (1<<bit))
1889                                 layers[bit]= 1;
1890                 }
1891         }
1892         CTX_DATA_END;
1893         
1894         /* copy layers to operator */
1895         RNA_boolean_set_array(op->ptr, "layers", layers);
1896         
1897                 /* part to sync with other similar operators... */
1898         /* pass on operator, so return modal */
1899         uiPupBlockOperator(C, wm_layers_select_create_menu, op, WM_OP_EXEC_DEFAULT);
1900         return OPERATOR_RUNNING_MODAL|OPERATOR_PASS_THROUGH;
1901 }
1902
1903 /* Set the visible layers for the active armature (edit and pose modes) */
1904 static int armature_bone_layers_exec (bContext *C, wmOperator *op)
1905 {
1906         Object *ob= CTX_data_active_object(C);
1907         bArmature *arm= (ob)? ob->data : NULL;
1908         PointerRNA ptr;
1909         int layers[16]; /* hardcoded for now - we can only have 16 armature layers, so this should be fine... */
1910         
1911         /* get the values set in the operator properties */
1912         RNA_boolean_get_array(op->ptr, "layers", layers);
1913         
1914         /* set layers of pchans based on the values set in the operator props */
1915         CTX_DATA_BEGIN(C, EditBone *, ebone, selected_editable_bones) 
1916         {
1917                 /* get pointer for pchan, and write flags this way */
1918                 RNA_pointer_create((ID *)arm, &RNA_EditBone, ebone, &ptr);
1919                 RNA_boolean_set_array(&ptr, "layers", layers);
1920         }
1921         CTX_DATA_END;
1922         
1923         /* note, notifier might evolve */
1924         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1925         
1926         return OPERATOR_FINISHED;
1927 }
1928
1929 void ARMATURE_OT_bone_layers (wmOperatorType *ot)
1930 {
1931         /* identifiers */
1932         ot->name= "Change Bone Layers";
1933         ot->idname= "ARMATURE_OT_bone_layers";
1934         ot->description= "Change the layers that the selected bones belong to.";
1935         
1936         /* callbacks */
1937         ot->invoke= armature_bone_layers_invoke;
1938         ot->exec= armature_bone_layers_exec;
1939         ot->poll= ED_operator_editarmature;
1940         
1941         /* flags */
1942         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1943         
1944         /* properties */
1945         RNA_def_boolean_array(ot->srna, "layers", 16, NULL, "Layers", "Armature layers that bone belongs to.");
1946 }
1947
1948
1949 #if 0
1950 // XXX old sys
1951 /* for use with pose_relax only */
1952 static int pose_relax_icu(struct IpoCurve *icu, float framef, float *val, float *frame_prev, float *frame_next)
1953 {
1954         if (!icu) {
1955                 return 0;
1956         } 
1957         else {
1958                 BezTriple *bezt = icu->bezt;
1959                 
1960                 BezTriple *bezt_prev=NULL, *bezt_next=NULL;
1961                 float w1, w2, wtot;
1962                 int i;
1963                 
1964                 for (i=0; i < icu->totvert; i++, bezt++) {
1965                         if (bezt->vec[1][0] < framef - 0.5) {
1966                                 bezt_prev = bezt;
1967                         } else {
1968                                 break;
1969                         }
1970                 }
1971                 
1972                 if (bezt_prev==NULL) return 0;
1973                 
1974                 /* advance to the next, dont need to advance i */
1975                 bezt = bezt_prev+1;
1976                 
1977                 for (; i < icu->totvert; i++, bezt++) {
1978                         if (bezt->vec[1][0] > framef + 0.5) {
1979                                 bezt_next = bezt;
1980                                                 break;
1981                         }
1982                 }
1983                 
1984                 if (bezt_next==NULL) return 0;
1985         
1986                 if (val) {
1987                         w1 = framef - bezt_prev->vec[1][0];
1988                         w2 = bezt_next->vec[1][0] - framef;
1989                         wtot = w1 + w2;
1990                         w1=w1/wtot;
1991                         w2=w2/wtot;
1992 #if 0
1993                         val = (bezt_prev->vec[1][1] * w2) + (bezt_next->vec[1][1] * w1);
1994 #else
1995                         /* apply the value with a hard coded 6th */
1996                         *val = (((bezt_prev->vec[1][1] * w2) + (bezt_next->vec[1][1] * w1)) + (*val * 5.0f)) / 6.0f;
1997 #endif
1998                 }
1999                 
2000                 if (frame_prev) *frame_prev = bezt_prev->vec[1][0];
2001                 if (frame_next) *frame_next = bezt_next->vec[1][0];
2002                 
2003                 return 1;
2004         }
2005 }
2006 #endif
2007
2008 void pose_relax(Scene *scene)
2009 {
2010         Object *ob = OBACT;
2011         bPose *pose;
2012         bAction *act;
2013         bArmature *arm;
2014         
2015 //      IpoCurve *icu_w, *icu_x, *icu_y, *icu_z;
2016         
2017         bPoseChannel *pchan;
2018 //      bActionChannel *achan;
2019 //      float framef = F_CFRA;
2020 //      float frame_prev, frame_next;
2021 //      float quat_prev[4], quat_next[4], quat_interp[4], quat_orig[4];
2022         
2023         int do_scale = 0;
2024         int do_loc = 0;
2025         int do_quat = 0;
2026         int flag = 0;
2027 //      int do_x, do_y, do_z;
2028         
2029         if (!ob) return;
2030         
2031         pose = ob->pose;
2032         act = ob->action;
2033         arm = (bArmature *)ob->data;
2034         
2035         if (!pose || !act || !arm) return;
2036         
2037         for (pchan=pose->chanbase.first; pchan; pchan= pchan->next) {
2038                 
2039                 pchan->bone->flag &= ~BONE_TRANSFORM;
2040                 
2041                 if (pchan->bone->layer & arm->layer) {
2042                         if (pchan->bone->flag & BONE_SELECTED) {
2043                                 /* do we have an ipo curve? */
2044 #if 0 // XXX old animation system
2045                                 achan= get_action_channel(act, pchan->name);
2046                                 
2047                                 if (achan && achan->ipo) {
2048                                         /*calc_ipo(achan->ipo, ctime);*/
2049                                         
2050                                         do_x = pose_relax_icu(find_ipocurve(achan->ipo, AC_LOC_X), framef, &pchan->loc[0], NULL, NULL);
2051                                         do_y = pose_relax_icu(find_ipocurve(achan->ipo, AC_LOC_Y), framef, &pchan->loc[1], NULL, NULL);
2052                                         do_z = pose_relax_icu(find_ipocurve(achan->ipo, AC_LOC_Z), framef, &pchan->loc[2], NULL, NULL);
2053                                         do_loc += do_x + do_y + do_z;
2054                                         
2055                                         do_x = pose_relax_icu(find_ipocurve(achan->ipo, AC_SIZE_X), framef, &pchan->size[0], NULL, NULL);
2056                                         do_y = pose_relax_icu(find_ipocurve(achan->ipo, AC_SIZE_Y), framef, &pchan->size[1], NULL, NULL);
2057                                         do_z = pose_relax_icu(find_ipocurve(achan->ipo, AC_SIZE_Z), framef, &pchan->size[2], NULL, NULL);
2058                                         do_scale += do_x + do_y + do_z;
2059                                                 
2060                                         if(     ((icu_w = find_ipocurve(achan->ipo, AC_QUAT_W))) &&
2061                                                 ((icu_x = find_ipocurve(achan->ipo, AC_QUAT_X))) &&
2062                                                 ((icu_y = find_ipocurve(achan->ipo, AC_QUAT_Y))) &&
2063                                                 ((icu_z = find_ipocurve(achan->ipo, AC_QUAT_Z))) )
2064                                         {
2065                                                 /* use the quatw keyframe as a basis for others */
2066                                                 if (pose_relax_icu(icu_w, framef, NULL, &frame_prev, &frame_next)) {
2067                                                         /* get 2 quats */
2068                                                         quat_prev[0] = eval_icu(icu_w, frame_prev);
2069                                                         quat_prev[1] = eval_icu(icu_x, frame_prev);
2070                                                         quat_prev[2] = eval_icu(icu_y, frame_prev);
2071                                                         quat_prev[3] = eval_icu(icu_z, frame_prev);
2072                                                         
2073                                                         quat_next[0] = eval_icu(icu_w, frame_next);
2074                                                         quat_next[1] = eval_icu(icu_x, frame_next);
2075                                                         quat_next[2] = eval_icu(icu_y, frame_next);
2076                                                         quat_next[3] = eval_icu(icu_z, frame_next);
2077                                                         
2078 #if 0
2079                                                         /* apply the setting, completely smooth */
2080                                                         QuatInterpol(pchan->quat, quat_prev, quat_next, (framef-frame_prev) / (frame_next-frame_prev) );
2081 #else
2082                                                         /* tricky interpolation */
2083                                                         QuatInterpol(quat_interp, quat_prev, quat_next, (framef-frame_prev) / (frame_next-frame_prev) );
2084                                                         QUATCOPY(quat_orig, pchan->quat);
2085                                                         QuatInterpol(pchan->quat, quat_orig, quat_interp, 1.0f/6.0f);
2086                                                         /* done */
2087 #endif
2088                                                         do_quat++;
2089                                                 }
2090                                         }
2091                                         
2092                                         /* apply BONE_TRANSFORM tag so that autokeying will pick it up */
2093                                         pchan->bone->flag |= BONE_TRANSFORM;
2094                                 }
2095                                 
2096 #endif // XXX old animation system
2097                         }
2098                 }
2099         }
2100         
2101         ob->pose->flag |= (POSE_LOCKED|POSE_DO_UNLOCK);
2102         
2103         /* do auto-keying */
2104         if (do_loc)             flag |= TFM_TRANSLATION;
2105         if (do_scale)   flag |= TFM_RESIZE;
2106         if (do_quat)    flag |= TFM_ROTATION;
2107         autokeyframe_pose_cb_func(ob, flag, 0);
2108          
2109         /* clear BONE_TRANSFORM flags */
2110         for (pchan=pose->chanbase.first; pchan; pchan= pchan->next)
2111                 pchan->bone->flag &= ~ BONE_TRANSFORM;
2112         
2113         /* do depsgraph flush */
2114         DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
2115         BIF_undo_push("Relax Pose");
2116 }
2117
2118 /* for use in insertkey, ensure rotation goes other way around */
2119 void pose_flipquats(Scene *scene)
2120 {
2121         Object *ob = OBACT;
2122         bArmature *arm= ob->data;
2123         bPoseChannel *pchan;
2124         
2125         if(ob->pose==NULL)
2126                 return;
2127         
2128         /* find sel bones */
2129         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
2130                 if(pchan->bone && (pchan->bone->flag & BONE_SELECTED) && (pchan->bone->layer & arm->layer)) {
2131                         /* quaternions have 720 degree range */
2132                         pchan->quat[0]= -pchan->quat[0];
2133                         pchan->quat[1]= -pchan->quat[1];
2134                         pchan->quat[2]= -pchan->quat[2];
2135                         pchan->quat[3]= -pchan->quat[3];
2136                 }
2137         }
2138         
2139         /* do autokey */
2140         autokeyframe_pose_cb_func(ob, TFM_ROTATION, 0);
2141 }
2142
2143 /* context: active channel */
2144 void pose_special_editmenu(Scene *scene)
2145 {
2146 #if 0
2147         Object *obedit= scene->obedit; // XXX context
2148         Object *ob= OBACT;
2149         short nr;
2150         
2151         /* paranoia checks */
2152         if(!ob && !ob->pose) return;
2153         if(ob==obedit || (ob->flag & OB_POSEMODE)==0) return;
2154         
2155         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");
2156         if(nr==1) {
2157                 pose_select_constraint_target(scene);
2158         }
2159         else if(nr==2) {
2160                 pose_flip_names();
2161         }
2162         else if(nr==3) {
2163                 pose_calculate_path(C, ob);
2164         }
2165         else if(nr==4) {
2166                 pose_clear_paths(ob);
2167         }
2168         else if(nr==5) {
2169                 pose_clear_user_transforms(scene, ob);
2170         }
2171         else if(nr==6) {
2172                 pose_relax();
2173         }
2174         else if(ELEM3(nr, 7, 8, 9)) {
2175                 pose_autoside_names(nr-7);
2176         }
2177 #endif
2178 }
2179
2180 /* Restore selected pose-bones to 'action'-defined pose */
2181 void pose_clear_user_transforms(Scene *scene, Object *ob)
2182 {
2183         bArmature *arm= ob->data;
2184         bPoseChannel *pchan;
2185         
2186         if (ob->pose == NULL)
2187                 return;
2188         
2189         /* if the object has an action, restore pose to the pose defined by the action by clearing pose on selected bones */
2190         if (ob->action) {
2191                 /* find selected bones */
2192                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
2193                         if (pchan->bone && (pchan->bone->flag & BONE_SELECTED) && (pchan->bone->layer & arm->layer)) {
2194                                 /* just clear the BONE_UNKEYED flag, allowing this bone to get overwritten by actions again */
2195                                 pchan->bone->flag &= ~BONE_UNKEYED;
2196                         }
2197                 }
2198                 
2199                 /* clear pose locking flag 
2200                  *      - this will only clear the user-defined pose in the selected bones, where BONE_UNKEYED has been cleared
2201                  */
2202                 ob->pose->flag |= POSE_DO_UNLOCK;
2203         }
2204         else {
2205                 /* no action, so restore entire pose to rest pose (cannot restore only selected bones) */
2206                 rest_pose(ob->pose);
2207         }
2208         
2209         DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
2210         BIF_undo_push("Clear User Transform");
2211 }
2212