generic operator menu was searching for "type" and using the first enum property...
[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_math.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                         ob->restore_mode = ob->mode;
129                         ob->mode |= OB_MODE_POSE;
130                         
131                         WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_MODE_POSE, NULL);
132                         
133                         break;
134                 default:
135                         return;
136         }
137
138         //ED_object_toggle_modes(C, ob->mode);
139 }
140
141 void ED_armature_exit_posemode(bContext *C, Base *base)
142 {
143         if(base) {
144                 Object *ob= base->object;
145                 
146                 ob->restore_mode = ob->mode;
147                 ob->mode &= ~OB_MODE_POSE;
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 == arm->act_bone));
166                                         else if (pchan->bone->flag & BONE_SELECTED || pchan->bone == arm->act_bone)
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                                 // XXX can now have negative frames, so this check needs improvement
231                                 if (ELEM(0, pchan->pathsf, pchan->pathef))      
232                                         return;
233                                 
234                                 /* try to increase area to do (only as much as needed) */
235                                 sfra= MIN2(sfra, pchan->pathsf);
236                                 efra= MAX2(efra, pchan->pathef);
237                         }
238                 }
239         }
240         if (efra <= sfra) return;
241         
242         /* hack: for unsaved files, set OB_RECALC so that paths can get calculated */
243         if ((ob->recalc & OB_RECALC)==0) {
244                 ob->recalc |= OB_RECALC;
245                 DAG_id_update_flags(&ob->id);
246         }
247         else
248                 DAG_id_update_flags(&ob->id);
249         
250         /* calculate path over requested range */
251         for (CFRA=sfra; CFRA<=efra; CFRA++) {
252                 /* do all updates */
253                 for (base= FIRSTBASE; base; base= base->next) {
254                         if (base->object->recalc) {
255                                 int temp= base->object->recalc;
256                                 
257                                 if (base->object->adt)
258                                         BKE_animsys_evaluate_animdata(&base->object->id, base->object->adt, (float)CFRA, ADT_RECALC_ALL);
259                                 
260                                 /* update object */
261                                 object_handle_update(scene, base->object);
262                                 base->object->recalc= temp;
263                         }
264                 }
265                 
266                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
267                         if ((pchan->bone) && (arm->layer & pchan->bone->layer)) {
268                                 if (pchan->path) {
269                                         /* only update if:
270                                          *      - in range of this pchan's existing path
271                                          *      - ... insert evil filtering/optimising conditions here...
272                                          */
273                                         if (IN_RANGE(CFRA, pchan->pathsf, pchan->pathef)) {
274                                                 fp= pchan->path+3*(CFRA-sfra);
275                                                 
276                                                 if (arm->pathflag & ARM_PATH_HEADS) { 
277                                                         VECCOPY(fp, pchan->pose_head);
278                                                 }
279                                                 else {
280                                                         VECCOPY(fp, pchan->pose_tail);
281                                                 }
282                                                 
283                                                 mul_m4_v3(ob->obmat, fp);
284                                         }
285                                 }
286                         }
287                 }
288         }
289         
290         /* reset flags */
291         CFRA= cfra;
292         ob->pose->flag &= ~POSE_RECALCPATHS;
293         
294         /* flush one final time - to restore to the original state */
295         for (base= FIRSTBASE; base; base= base->next) {
296                 if (base->object->recalc) {
297                         int temp= base->object->recalc;
298                         
299                         if (base->object->adt)
300                                 BKE_animsys_evaluate_animdata(&base->object->id, base->object->adt, (float)CFRA, ADT_RECALC_ALL);
301                         
302                         object_handle_update(scene, base->object);
303                         base->object->recalc= temp;
304                 }
305         }
306 }
307
308 /* --------- */
309
310 /* For the object with pose/action: create path curves for selected bones 
311  * This recalculates the WHOLE path within the pchan->pathsf and pchan->pathef range
312  */
313 static int pose_calculate_paths_exec (bContext *C, wmOperator *op)
314 {
315         wmWindow *win= CTX_wm_window(C);
316         ScrArea *sa= CTX_wm_area(C);
317         Scene *scene= CTX_data_scene(C);
318         Object *ob;
319         bArmature *arm;
320         bPoseChannel *pchan;
321         Base *base;
322         float *fp;
323         int cfra;
324         int sfra, efra;
325         
326         /* since this call may also be used from the buttons window, we need to check for where to get the object */
327         if (sa->spacetype == SPACE_BUTS) 
328                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
329         else
330                 ob= CTX_data_active_object(C);
331                 
332         /* only continue if there's an object */
333         if ELEM(NULL, ob, ob->pose)
334                 return OPERATOR_CANCELLED;
335         arm= ob->data;
336         
337         /* version patch for older files here (do_versions patch too complicated) */
338         if ((arm->pathsf == 0) || (arm->pathef == 0)) {
339                 arm->pathsf = SFRA;
340                 arm->pathef = EFRA;
341         }
342         if (arm->pathsize == 0) {
343                 arm->pathsize = 1;
344         }
345         
346         /* get frame values to use */
347         cfra= CFRA;
348         sfra = arm->pathsf;
349         efra = arm->pathef;
350         
351         if (efra <= sfra) {
352                 BKE_report(op->reports, RPT_ERROR, "Can't calculate paths when pathlen <= 0");
353                 return OPERATOR_CANCELLED;
354         }
355         
356         /* hack: for unsaved files, set OB_RECALC so that paths can get calculated */
357         if ((ob->recalc & OB_RECALC)==0) {
358                 ob->recalc |= OB_RECALC;
359                 DAG_id_update_flags(&ob->id);
360         }
361         else
362                 DAG_id_update_flags(&ob->id);
363         
364         /* alloc the path cache arrays */
365         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
366                 if ((pchan->bone) && (pchan->bone->flag & BONE_SELECTED)) {
367                         if (arm->layer & pchan->bone->layer) {
368                                 pchan->pathlen= efra-sfra+1;
369                                 pchan->pathsf= sfra;
370                                 pchan->pathef= efra+1;
371                                 if (pchan->path)
372                                         MEM_freeN(pchan->path);
373                                 pchan->path= MEM_callocN(3*pchan->pathlen*sizeof(float), "pchan path");
374                         }
375                 }
376         }
377         
378         /* step through frame range sampling the values */
379         for (CFRA=sfra; CFRA<=efra; CFRA++) {
380                 /* for each frame we calculate, update time-cursor... (may be too slow) */
381                 WM_timecursor(win, CFRA);
382                 
383                 /* do all updates */
384                 for (base= FIRSTBASE; base; base= base->next) {
385                         if (base->object->recalc) {
386                                 int temp= base->object->recalc;
387                                 
388                                 if (base->object->adt)
389                                         BKE_animsys_evaluate_animdata(&base->object->id, base->object->adt, (float)CFRA, ADT_RECALC_ALL);
390                                 
391                                 object_handle_update(scene, base->object);
392                                 base->object->recalc= temp;
393                         }
394                 }
395                 
396                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
397                         if ((pchan->bone) && (pchan->bone->flag & BONE_SELECTED)) {
398                                 if (arm->layer & pchan->bone->layer) {
399                                         if (pchan->path) {
400                                                 fp= pchan->path+3*(CFRA-sfra);
401                                                 
402                                                 if (arm->pathflag & ARM_PATH_HEADS) { 
403                                                         VECCOPY(fp, pchan->pose_head);
404                                                 }
405                                                 else {
406                                                         VECCOPY(fp, pchan->pose_tail);
407                                                 }
408                                                 
409                                                 mul_m4_v3(ob->obmat, fp);
410                                         }
411                                 }
412                         }
413                 }
414         }
415         
416         /* restore original cursor */
417         WM_cursor_restore(win);
418         
419         /* reset current frame, and clear flags */
420         CFRA= cfra;
421         ob->pose->flag &= ~POSE_RECALCPATHS;
422         
423         /* flush one final time - to restore to the original state */
424         for (base= FIRSTBASE; base; base= base->next) {
425                 if (base->object->recalc) {
426                         int temp= base->object->recalc;
427                         
428                         if (base->object->adt)
429                                 BKE_animsys_evaluate_animdata(&base->object->id, base->object->adt, (float)CFRA, ADT_RECALC_ALL);
430                         
431                         object_handle_update(scene, base->object);
432                         base->object->recalc= temp;
433                 }
434         }
435         
436         /* notifiers for updates */
437         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
438         
439         return OPERATOR_FINISHED; 
440 }
441
442 void POSE_OT_paths_calculate (wmOperatorType *ot)
443 {
444         /* identifiers */
445         ot->name= "Calculate Bone Paths";
446         ot->idname= "POSE_OT_paths_calculate";
447         ot->description= "Calculate paths for the selected bones.";
448         
449         /* api callbacks */
450         ot->exec= pose_calculate_paths_exec;
451         ot->poll= ED_operator_posemode;
452         
453         /* flags */
454         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
455 }
456
457 /* --------- */
458
459 /* for the object with pose/action: clear path curves for selected bones only */
460 void ED_pose_clear_paths(Object *ob)
461 {
462         bPoseChannel *pchan;
463         
464         if ELEM(NULL, ob, ob->pose)
465                 return;
466         
467         /* free the path blocks */
468         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
469                 if ((pchan->bone) && (pchan->bone->flag & BONE_SELECTED)) {
470                         if (pchan->path) {
471                                 MEM_freeN(pchan->path);
472                                 pchan->path= NULL;
473                         }
474                 }
475         }
476 }
477
478 /* operator callback for this */
479 static int pose_clear_paths_exec (bContext *C, wmOperator *op)
480 {
481         ScrArea *sa= CTX_wm_area(C);
482         Object *ob;
483         
484         /* since this call may also be used from the buttons window, we need to check for where to get the object */
485         if (sa->spacetype == SPACE_BUTS) 
486                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
487         else
488                 ob= CTX_data_active_object(C);
489                 
490         /* only continue if there's an object */
491         if ELEM(NULL, ob, ob->pose)
492                 return OPERATOR_CANCELLED;
493         
494         /* for now, just call the API function for this (which is shared with backend functions) */
495         ED_pose_clear_paths(ob);
496         
497         /* notifiers for updates */
498         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
499         
500         return OPERATOR_FINISHED; 
501 }
502
503 void POSE_OT_paths_clear (wmOperatorType *ot)
504 {
505         /* identifiers */
506         ot->name= "Clear Bone Paths";
507         ot->idname= "POSE_OT_paths_clear";
508         ot->description= "Clear path caches for selected bones.";
509         
510         /* api callbacks */
511         ot->exec= pose_clear_paths_exec;
512         ot->poll= ED_operator_posemode;
513         
514         /* flags */
515         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
516 }
517
518 /* ******************* Select Constraint Target Operator ************* */
519
520 // XXX this function is to be removed when the other stuff is recoded
521 void pose_select_constraint_target(Scene *scene)
522 {
523         Object *obedit= scene->obedit; // XXX context
524         Object *ob= OBACT;
525         bArmature *arm= ob->data;
526         bPoseChannel *pchan;
527         bConstraint *con;
528         
529         /* paranoia checks */
530         if (!ob && !ob->pose) return;
531         if (ob==obedit || (ob->mode & OB_MODE_POSE)==0) return;
532         
533         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
534                 if (arm->layer & pchan->bone->layer) {
535                         if (pchan->bone->flag & BONE_SELECTED || pchan->bone == arm->act_bone) {
536                                 for (con= pchan->constraints.first; con; con= con->next) {
537                                         bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
538                                         ListBase targets = {NULL, NULL};
539                                         bConstraintTarget *ct;
540                                         
541                                         if (cti && cti->get_constraint_targets) {
542                                                 cti->get_constraint_targets(con, &targets);
543                                                 
544                                                 for (ct= targets.first; ct; ct= ct->next) {
545                                                         if ((ct->tar == ob) && (ct->subtarget[0])) {
546                                                                 bPoseChannel *pchanc= get_pose_channel(ob->pose, ct->subtarget);
547                                                                 if((pchanc) && !(pchanc->bone->flag & BONE_UNSELECTABLE))
548                                                                         pchanc->bone->flag |= BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL;
549                                                         }
550                                                 }
551                                                 
552                                                 if (cti->flush_constraint_targets)
553                                                         cti->flush_constraint_targets(con, &targets, 1);
554                                         }
555                                 }
556                         }
557                 }
558         }
559         
560         BIF_undo_push("Select constraint target");
561
562 }
563
564 static int pose_select_constraint_target_exec(bContext *C, wmOperator *op)
565 {
566         Object *ob= CTX_data_active_object(C);
567         bArmature *arm= ob->data;
568         bPoseChannel *pchan;
569         bConstraint *con;
570         int found= 0;
571         
572         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
573                 if (arm->layer & pchan->bone->layer) {
574                         if (pchan->bone->flag & BONE_SELECTED || pchan->bone == arm->act_bone) {
575                                 for (con= pchan->constraints.first; con; con= con->next) {
576                                         bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
577                                         ListBase targets = {NULL, NULL};
578                                         bConstraintTarget *ct;
579                                         
580                                         if (cti && cti->get_constraint_targets) {
581                                                 cti->get_constraint_targets(con, &targets);
582                                                 
583                                                 for (ct= targets.first; ct; ct= ct->next) {
584                                                         if ((ct->tar == ob) && (ct->subtarget[0])) {
585                                                                 bPoseChannel *pchanc= get_pose_channel(ob->pose, ct->subtarget);
586                                                                 if((pchanc) && !(pchanc->bone->flag & BONE_UNSELECTABLE)) {
587                                                                         pchanc->bone->flag |= BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL;
588                                                                         found= 1;
589                                                                 }
590                                                         }
591                                                 }
592                                                 
593                                                 if (cti->flush_constraint_targets)
594                                                         cti->flush_constraint_targets(con, &targets, 1);
595                                         }
596                                 }
597                         }
598                 }
599         }
600
601         if(!found)
602                 return OPERATOR_CANCELLED;
603
604         WM_event_add_notifier(C, NC_OBJECT|ND_BONE_SELECT, ob);
605
606         return OPERATOR_FINISHED;
607 }
608
609 void POSE_OT_select_constraint_target(wmOperatorType *ot)
610 {
611         /* identifiers */
612         ot->name= "Select Constraint Target";
613         ot->idname= "POSE_OT_select_constraint_target";
614         
615         /* api callbacks */
616         ot->exec= pose_select_constraint_target_exec;
617         ot->poll= ED_operator_posemode;
618         
619         /* flags */
620         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
621 }
622
623 /* ******************* select hierarchy operator ************* */
624
625 static int pose_select_hierarchy_exec(bContext *C, wmOperator *op)
626 {
627         Object *ob= CTX_data_active_object(C);
628         bArmature *arm= ob->data;
629         bPoseChannel *pchan;
630         Bone *curbone, *pabone, *chbone;
631         int direction = RNA_enum_get(op->ptr, "direction");
632         int add_to_sel = RNA_boolean_get(op->ptr, "extend");
633         int found= 0;
634         
635         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
636                 curbone= pchan->bone;
637                 
638                 if ((arm->layer & curbone->layer) && (curbone->flag & BONE_UNSELECTABLE)==0) {
639                         if (curbone == arm->act_bone) {
640                                 if (direction == BONE_SELECT_PARENT) {
641                                 
642                                         if (pchan->parent == NULL) continue;
643                                         else pabone= pchan->parent->bone;
644                                         
645                                         if ((arm->layer & pabone->layer) && !(pabone->flag & BONE_HIDDEN_P)) {
646                                                 
647                                                 if (!add_to_sel) curbone->flag &= ~BONE_SELECTED;
648                                                 pabone->flag |= BONE_SELECTED;
649                                                 arm->act_bone= pabone;
650                                                 
651                                                 found= 1;
652                                                 break;
653                                         }
654                                 } else { // BONE_SELECT_CHILD
655                                 
656                                         if (pchan->child == NULL) continue;
657                                         else chbone = pchan->child->bone;
658                                         
659                                         if ((arm->layer & chbone->layer) && !(chbone->flag & BONE_HIDDEN_P)) {
660                                         
661                                                 if (!add_to_sel) curbone->flag &= ~BONE_SELECTED;
662                                                 chbone->flag |= BONE_SELECTED;
663                                                 arm->act_bone= chbone;
664                                                 
665                                                 found= 1;
666                                                 break;
667                                         }
668                                 }
669                         }
670                 }
671         }
672
673         if(!found)
674                 return OPERATOR_CANCELLED;
675
676         WM_event_add_notifier(C, NC_OBJECT|ND_BONE_SELECT, ob);
677
678         return OPERATOR_FINISHED;
679 }
680
681 void POSE_OT_select_hierarchy(wmOperatorType *ot)
682 {
683         static EnumPropertyItem direction_items[]= {
684         {BONE_SELECT_PARENT, "PARENT", 0, "Select Parent", ""},
685         {BONE_SELECT_CHILD, "CHILD", 0, "Select Child", ""},
686         {0, NULL, 0, NULL, NULL}
687         };
688         
689         /* identifiers */
690         ot->name= "Select Hierarchy";
691         ot->idname= "POSE_OT_select_hierarchy";
692         
693         /* api callbacks */
694         ot->exec= pose_select_hierarchy_exec;
695         ot->poll= ED_operator_posemode;
696         
697         /* flags */
698         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
699         
700         /* props */
701         RNA_def_enum(ot->srna, "direction", direction_items,
702                                  BONE_SELECT_PARENT, "Direction", "");
703         RNA_def_boolean(ot->srna, "extend", 0, "Add to Selection", "");
704         
705 }
706
707
708 void pose_copy_menu(Scene *scene)
709 {
710         Object *obedit= scene->obedit; // XXX context
711         Object *ob= OBACT;
712         bArmature *arm= ob->data;
713         bPoseChannel *pchan, *pchanact;
714         short nr=0;
715         int i=0;
716         
717         /* paranoia checks */
718         if (ELEM(NULL, ob, ob->pose)) return;
719         if ((ob==obedit) || (ob->mode & OB_MODE_POSE)==0) return;
720         
721         pchan= get_active_posechannel(ob);
722         
723         if (pchan==NULL) return;
724         pchanact= pchan;
725         
726         /* if proxy-protected bones selected, some things (such as locks + displays) shouldn't be changable, 
727          * but for constraints (just add local constraints)
728          */
729         if (pose_has_protected_selected(ob, 1, 0)) {
730                 i= BLI_countlist(&(pchanact->constraints)); /* if there are 24 or less, allow for the user to select constraints */
731                 if (i < 25)
732                         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");
733                 else
734                         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");
735         }
736         else {
737                 i= BLI_countlist(&(pchanact->constraints)); /* if there are 24 or less, allow for the user to select constraints */
738                 if (i < 25)
739                         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");
740                 else
741                         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");
742         }
743         
744         if (nr <= 0) 
745                 return;
746         
747         if (nr != 5)  {
748                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
749                         if ( (arm->layer & pchan->bone->layer) &&
750                                  (pchan->bone->flag & BONE_SELECTED) &&
751                                  (pchan != pchanact) ) 
752                         {
753                                 switch (nr) {
754                                         case 1: /* Local Location */
755                                                 VECCOPY(pchan->loc, pchanact->loc);
756                                                 break;
757                                         case 2: /* Local Rotation */
758                                                 QUATCOPY(pchan->quat, pchanact->quat);
759                                                 VECCOPY(pchan->eul, pchanact->eul);
760                                                 break;
761                                         case 3: /* Local Size */
762                                                 VECCOPY(pchan->size, pchanact->size);
763                                                 break;
764                                         case 4: /* All Constraints */
765                                         {
766                                                 ListBase tmp_constraints = {NULL, NULL};
767                                                 
768                                                 /* copy constraints to tmpbase and apply 'local' tags before 
769                                                  * appending to list of constraints for this channel
770                                                  */
771                                                 copy_constraints(&tmp_constraints, &pchanact->constraints);
772                                                 if ((ob->proxy) && (pchan->bone->layer & arm->layer_protected)) {
773                                                         bConstraint *con;
774                                                         
775                                                         /* add proxy-local tags */
776                                                         for (con= tmp_constraints.first; con; con= con->next)
777                                                                 con->flag |= CONSTRAINT_PROXY_LOCAL;
778                                                 }
779                                                 addlisttolist(&pchan->constraints, &tmp_constraints);
780                                                 
781                                                 /* update flags (need to add here, not just copy) */
782                                                 pchan->constflag |= pchanact->constflag;
783                                                 
784                                                 if (ob->pose)
785                                                         ob->pose->flag |= POSE_RECALC;
786                                         }
787                                                 break;
788                                         case 6: /* Transform Locks */
789                                                 pchan->protectflag = pchanact->protectflag;
790                                                 break;
791                                         case 7: /* IK (DOF) settings */
792                                         {
793                                                 pchan->ikflag = pchanact->ikflag;
794                                                 VECCOPY(pchan->limitmin, pchanact->limitmin);
795                                                 VECCOPY(pchan->limitmax, pchanact->limitmax);
796                                                 VECCOPY(pchan->stiffness, pchanact->stiffness);
797                                                 pchan->ikstretch= pchanact->ikstretch;
798                                                 pchan->ikrotweight= pchanact->ikrotweight;
799                                                 pchan->iklinweight= pchanact->iklinweight;
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];
811                                                 
812                                                 armature_mat_pose_to_bone(pchan, pchanact->pose_mat, delta_mat);
813                                                 
814                                                 if (pchan->rotmode == ROT_MODE_AXISANGLE) {
815                                                         float tmp_quat[4];
816                                                         
817                                                         /* need to convert to quat first (in temp var)... */
818                                                         mat4_to_quat( tmp_quat,delta_mat);
819                                                         quat_to_axis_angle( pchan->rotAxis, &pchan->rotAngle,tmp_quat);
820                                                 }
821                                                 else if (pchan->rotmode == ROT_MODE_QUAT)
822                                                         mat4_to_quat( pchan->quat,delta_mat);
823                                                 else
824                                                         mat4_to_eulO( pchan->eul, pchan->rotmode,delta_mat);
825                                         }
826                                                 break;
827                                         case 11: /* Visual Size */
828                                         {
829                                                 float delta_mat[4][4], size[4];
830                                                 
831                                                 armature_mat_pose_to_bone(pchan, pchanact->pose_mat, delta_mat);
832                                                 mat4_to_size( size,delta_mat);
833                                                 VECCOPY(pchan->size, size);
834                                         }
835                                 }
836                         }
837                 }
838         } 
839         else { /* constraints, optional (note: max we can have is 24 constraints) */
840                 bConstraint *con, *con_back;
841                 int const_toggle[24];
842                 ListBase const_copy = {NULL, NULL};
843                 
844                 BLI_duplicatelist(&const_copy, &(pchanact->constraints));
845                 
846                 /* build the puplist of constraints */
847                 for (con = pchanact->constraints.first, i=0; con; con=con->next, i++){
848                         const_toggle[i]= 1;
849 //                      add_numbut(i, TOG|INT, con->name, 0, 0, &(const_toggle[i]), "");
850                 }
851                 
852 //              if (!do_clever_numbuts("Select Constraints", i, REDRAW)) {
853 //                      BLI_freelistN(&const_copy);
854 //                      return;
855 //              }
856                 
857                 /* now build a new listbase from the options selected */
858                 for (i=0, con=const_copy.first; con; i++) {
859                         /* if not selected, free/remove it from the list */
860                         if (!const_toggle[i]) {
861                                 con_back= con->next;
862                                 BLI_freelinkN(&const_copy, con);
863                                 con= con_back;
864                         } 
865                         else
866                                 con= con->next;
867                 }
868                 
869                 /* Copy the temo listbase to the selected posebones */
870                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
871                         if ( (arm->layer & pchan->bone->layer) &&
872                                  (pchan->bone->flag & BONE_SELECTED) &&
873                                  (pchan!=pchanact) ) 
874                         {
875                                 ListBase tmp_constraints = {NULL, NULL};
876                                 
877                                 /* copy constraints to tmpbase and apply 'local' tags before 
878                                  * appending to list of constraints for this channel
879                                  */
880                                 copy_constraints(&tmp_constraints, &const_copy);
881                                 if ((ob->proxy) && (pchan->bone->layer & arm->layer_protected)) {
882                                         bConstraint *con;
883                                         
884                                         /* add proxy-local tags */
885                                         for (con= tmp_constraints.first; con; con= con->next)
886                                                 con->flag |= CONSTRAINT_PROXY_LOCAL;
887                                 }
888                                 addlisttolist(&pchan->constraints, &tmp_constraints);
889                                 
890                                 /* update flags (need to add here, not just copy) */
891                                 pchan->constflag |= pchanact->constflag;
892                         }
893                 }
894                 BLI_freelistN(&const_copy);
895                 update_pose_constraint_flags(ob->pose); /* we could work out the flags but its simpler to do this */
896                 
897                 if (ob->pose)
898                         ob->pose->flag |= POSE_RECALC;
899         }
900         
901         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);   // and all its relations
902         
903         BIF_undo_push("Copy Pose Attributes");
904         
905 }
906
907 /* ******************** copy/paste pose ********************** */
908
909 /* Global copy/paste buffer for pose - cleared on start/end session + before every copy operation */
910 static bPose *g_posebuf = NULL;
911
912 void free_posebuf(void) 
913 {
914         if (g_posebuf) {
915                 /* was copied without constraints */
916                 BLI_freelistN(&g_posebuf->chanbase);
917                 MEM_freeN(g_posebuf);
918         }
919         
920         g_posebuf=NULL;
921 }
922
923 /* ---- */
924
925 static int pose_copy_exec (bContext *C, wmOperator *op)
926 {
927         Object *ob= CTX_data_active_object(C);
928         
929         /* sanity checking */
930         if ELEM(NULL, ob, ob->pose) {
931                 BKE_report(op->reports, RPT_ERROR, "No Pose to Copy");
932                 return OPERATOR_CANCELLED;
933         }
934
935         /* free existing pose buffer */
936         free_posebuf();
937         
938         /* sets chan->flag to POSE_KEY if bone selected, then copy those bones to the buffer */
939         set_pose_keys(ob);  
940         copy_pose(&g_posebuf, ob->pose, 0);
941         
942         
943         return OPERATOR_FINISHED;
944 }
945
946 void POSE_OT_copy (wmOperatorType *ot) 
947 {
948         /* identifiers */
949         ot->name= "Copy Pose";
950         ot->idname= "POSE_OT_copy";
951         ot->description= "Copies the current pose of the selected bones to copy/paste buffer.";
952         
953         /* api callbacks */
954         ot->exec= pose_copy_exec;
955         ot->poll= ED_operator_posemode;
956         
957         /* flag */
958         ot->flag= OPTYPE_REGISTER;
959 }
960
961 /* ---- */
962
963 /* Pointers to the builtin KeyingSets that we want to use */
964 static KeyingSet *posePaste_ks_locrotscale = NULL;              /* the only keyingset we'll need */
965
966 /* ---- */
967
968 static int pose_paste_exec (bContext *C, wmOperator *op)
969 {
970         Scene *scene= CTX_data_scene(C);
971         Object *ob= CTX_data_active_object(C);
972         bPoseChannel *chan, *pchan;
973         char name[32];
974         int flip= RNA_boolean_get(op->ptr, "flipped");
975         
976         bCommonKeySrc cks;
977         ListBase dsources = {&cks, &cks};
978         
979         /* init common-key-source for use by KeyingSets */
980         memset(&cks, 0, sizeof(bCommonKeySrc));
981         cks.id= &ob->id;
982         
983         /* sanity checks */
984         if ELEM(NULL, ob, ob->pose)
985                 return OPERATOR_CANCELLED;
986
987         if (g_posebuf == NULL) {
988                 BKE_report(op->reports, RPT_ERROR, "Copy buffer is empty");
989                 return OPERATOR_CANCELLED;
990         }
991         
992         /* Safely merge all of the channels in the buffer pose into any existing pose */
993         for (chan= g_posebuf->chanbase.first; chan; chan=chan->next) {
994                 if (chan->flag & POSE_KEY) {
995                         /* get the name - if flipping, we must flip this first */
996                         BLI_strncpy(name, chan->name, sizeof(name));
997                         if (flip)
998                                 bone_flip_name(name, 0);                /* 0 = don't strip off number extensions */
999                                 
1000                         /* only copy when channel exists, poses are not meant to add random channels to anymore */
1001                         pchan= get_pose_channel(ob->pose, name);
1002                         
1003                         if (pchan) {
1004                                 /* only loc rot size 
1005                                  *      - only copies transform info for the pose 
1006                                  */
1007                                 VECCOPY(pchan->loc, chan->loc);
1008                                 VECCOPY(pchan->size, chan->size);
1009                                 pchan->flag= chan->flag;
1010                                 
1011                                 /* check if rotation modes are compatible (i.e. do they need any conversions) */
1012                                 if (pchan->rotmode == chan->rotmode) {
1013                                         /* copy the type of rotation in use */
1014                                         if (pchan->rotmode > 0) {
1015                                                 VECCOPY(pchan->eul, chan->eul);
1016                                         }
1017                                         else {
1018                                                 QUATCOPY(pchan->quat, chan->quat);
1019                                         }
1020                                 }
1021                                 else if (pchan->rotmode > 0) {
1022                                         /* quat/axis-angle to euler */
1023                                         if (chan->rotmode == ROT_MODE_AXISANGLE)
1024                                                 axis_angle_to_eulO( pchan->eul, pchan->rotmode,chan->rotAxis, chan->rotAngle);
1025                                         else
1026                                                 quat_to_eulO( pchan->eul, pchan->rotmode,chan->quat);
1027                                 }
1028                                 else if (pchan->rotmode == ROT_MODE_AXISANGLE) {
1029                                         /* quat/euler to axis angle */
1030                                         if (chan->rotmode > 0)
1031                                                 eulO_to_axis_angle( pchan->rotAxis, &pchan->rotAngle,chan->eul, chan->rotmode);
1032                                         else    
1033                                                 quat_to_axis_angle( pchan->rotAxis, &pchan->rotAngle,chan->quat);
1034                                 }
1035                                 else {
1036                                         /* euler/axis-angle to quat */
1037                                         if (chan->rotmode > 0)
1038                                                 eulO_to_quat( pchan->quat,chan->eul, chan->rotmode);
1039                                         else
1040                                                 axis_angle_to_quat(pchan->quat, chan->rotAxis, pchan->rotAngle);
1041                                 }
1042                                 
1043                                 /* paste flipped pose? */
1044                                 if (flip) {
1045                                         pchan->loc[0]*= -1;
1046                                         
1047                                         /* has to be done as eulers... */
1048                                         if (pchan->rotmode > 0) {
1049                                                 pchan->eul[1] *= -1;
1050                                                 pchan->eul[2] *= -1;
1051                                         }
1052                                         else if (pchan->rotmode == ROT_MODE_AXISANGLE) {
1053                                                 float eul[3];
1054                                                 
1055                                                 axis_angle_to_eulO( eul, EULER_ORDER_DEFAULT,pchan->rotAxis, pchan->rotAngle);
1056                                                 eul[1]*= -1;
1057                                                 eul[2]*= -1;
1058                                                 eulO_to_axis_angle( pchan->rotAxis, &pchan->rotAngle,eul, EULER_ORDER_DEFAULT);
1059                                                 
1060                                                 // experimental method (uncomment to test):
1061 #if 0
1062                                                 /* experimental method: just flip the orientation of the axis on x/y axes */
1063                                                 pchan->quat[1] *= -1;
1064                                                 pchan->quat[2] *= -1;
1065 #endif
1066                                         }
1067                                         else {
1068                                                 float eul[3];
1069                                                 
1070                                                 quat_to_eul( eul,pchan->quat);
1071                                                 eul[1]*= -1;
1072                                                 eul[2]*= -1;
1073                                                 eul_to_quat( pchan->quat,eul);
1074                                         }
1075                                 }
1076                                 
1077                                 if (autokeyframe_cfra_can_key(scene, &ob->id)) {
1078                                         /* Set keys on pose
1079                                          *      - KeyingSet to use depends on rotation mode 
1080                                          *      (but that's handled by the templates code)  
1081                                          */
1082                                         // TODO: for getting the KeyingSet used, we should really check which channels were affected
1083                                         if (posePaste_ks_locrotscale == NULL)
1084                                                 posePaste_ks_locrotscale= ANIM_builtin_keyingset_get_named(NULL, "LocRotScale");
1085                                         
1086                                         /* init cks for this PoseChannel, then use the relative KeyingSets to keyframe it */
1087                                         cks.pchan= pchan;
1088                                         
1089                                         modify_keyframes(scene, &dsources, NULL, posePaste_ks_locrotscale, MODIFYKEY_MODE_INSERT, (float)CFRA);
1090                                         
1091                                         /* clear any unkeyed tags */
1092                                         if (chan->bone)
1093                                                 chan->bone->flag &= ~BONE_UNKEYED;
1094                                 }
1095                                 else {
1096                                         /* add unkeyed tags */
1097                                         if (chan->bone)
1098                                                 chan->bone->flag |= BONE_UNKEYED;
1099                                 }
1100                         }
1101                 }
1102         }
1103
1104         /* Update event for pose and deformation children */
1105         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
1106         
1107         if (IS_AUTOKEY_ON(scene)) {
1108 // XXX          remake_action_ipos(ob->action);
1109         }
1110         else {
1111                 /* need to trick depgraph, action is not allowed to execute on pose */
1112                 where_is_pose(scene, ob);
1113                 ob->recalc= 0;
1114         }
1115         
1116         /* notifiers for updates */
1117         WM_event_add_notifier(C, NC_OBJECT|ND_POSE|ND_TRANSFORM, ob);
1118         WM_event_add_notifier(C, NC_ANIMATION|ND_KEYFRAME_EDIT, NULL); // XXX not really needed, but here for completeness...
1119
1120         return OPERATOR_FINISHED;
1121 }
1122
1123 void POSE_OT_paste (wmOperatorType *ot)
1124 {
1125         /* identifiers */
1126         ot->name= "Paste Pose";
1127         ot->idname= "POSE_OT_paste";
1128         ot->description= "Pastes the stored pose on to the current pose.";
1129         
1130         /* api callbacks */
1131         ot->exec= pose_paste_exec;
1132         ot->poll= ED_operator_posemode;
1133         
1134         /* flag */
1135         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1136         
1137         /* properties */
1138         RNA_def_boolean(ot->srna, "flipped", 0, "Flipped on X-Axis", "");
1139 }
1140
1141 /* ********************************************** */
1142
1143 /* context weightpaint and deformer in posemode */
1144 void pose_adds_vgroups(Scene *scene, Object *meshobj, int heatweights)
1145 {
1146 // XXX  extern VPaint Gwp;         /* from vpaint */
1147         Object *poseobj= modifiers_isDeformedByArmature(meshobj);
1148
1149         if(poseobj==NULL || (poseobj->mode & OB_MODE_POSE)==0) {
1150                 error("The active object must have a deforming armature in pose mode");
1151                 return;
1152         }
1153
1154 // XXX  add_verts_to_dgroups(meshobj, poseobj, heatweights, ((Mesh *)(meshobj->data))->editflag & ME_EDIT_MIRROR_X);
1155
1156         if(heatweights)
1157                 BIF_undo_push("Apply Bone Heat Weights to Vertex Groups");
1158         else
1159                 BIF_undo_push("Apply Bone Envelopes to Vertex Groups");
1160
1161         
1162         // and all its relations
1163         DAG_id_flush_update(&meshobj->id, OB_RECALC_DATA);
1164 }
1165
1166 /* ********************************************** */
1167
1168
1169 static int pose_group_add_exec (bContext *C, wmOperator *op)
1170 {
1171         ScrArea *sa= CTX_wm_area(C);
1172         Object *ob;
1173         
1174         /* since this call may also be used from the buttons window, we need to check for where to get the object */
1175         if (sa->spacetype == SPACE_BUTS) 
1176                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1177         else
1178                 ob= CTX_data_active_object(C);
1179                 
1180         /* only continue if there's an object */
1181         if (ob == NULL)
1182                 return OPERATOR_CANCELLED;
1183         
1184         /* for now, just call the API function for this */
1185         pose_add_group(ob);
1186         
1187         /* notifiers for updates */
1188         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1189         
1190         return OPERATOR_FINISHED;
1191 }
1192
1193 void POSE_OT_group_add (wmOperatorType *ot)
1194 {
1195         /* identifiers */
1196         ot->name= "Add Bone Group";
1197         ot->idname= "POSE_OT_group_add";
1198         ot->description= "Add a new bone group.";
1199         
1200         /* api callbacks */
1201         ot->exec= pose_group_add_exec;
1202         ot->poll= ED_operator_posemode;
1203         
1204         /* flags */
1205         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1206 }
1207
1208
1209 static int pose_group_remove_exec (bContext *C, wmOperator *op)
1210 {
1211         ScrArea *sa= CTX_wm_area(C);
1212         Object *ob;
1213         
1214         /* since this call may also be used from the buttons window, we need to check for where to get the object */
1215         if (sa->spacetype == SPACE_BUTS) 
1216                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1217         else
1218                 ob= CTX_data_active_object(C);
1219         
1220         /* only continue if there's an object */
1221         if (ob == NULL)
1222                 return OPERATOR_CANCELLED;
1223         
1224         /* for now, just call the API function for this */
1225         pose_remove_group(ob);
1226         
1227         /* notifiers for updates */
1228         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1229         
1230         return OPERATOR_FINISHED;
1231 }
1232
1233 void POSE_OT_group_remove (wmOperatorType *ot)
1234 {
1235         /* identifiers */
1236         ot->name= "Remove Bone Group";
1237         ot->idname= "POSE_OT_group_remove";
1238         ot->description= "Removes the active bone group.";
1239         
1240         /* api callbacks */
1241         ot->exec= pose_group_remove_exec;
1242         ot->poll= ED_operator_posemode;
1243         
1244         /* flags */
1245         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1246 }
1247
1248 /* ------------ */
1249
1250 /* invoke callback which presents a list of bone-groups for the user to choose from */
1251 static int pose_groups_menu_invoke (bContext *C, wmOperator *op, wmEvent *evt)
1252 {
1253         ScrArea *sa= CTX_wm_area(C);
1254         Object *ob;
1255         bPose *pose;
1256         
1257         uiPopupMenu *pup;
1258         uiLayout *layout;
1259         bActionGroup *grp;
1260         int i;
1261         
1262         /* since this call may also be used from the buttons window, we need to check for where to get the object */
1263         if (sa->spacetype == SPACE_BUTS) 
1264                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1265         else
1266                 ob= CTX_data_active_object(C);
1267         
1268         /* only continue if there's an object, and a pose there too */
1269         if (ELEM(NULL, ob, ob->pose)) 
1270                 return OPERATOR_CANCELLED;
1271         pose= ob->pose;
1272         
1273         /* if there's no active group (or active is invalid), create a new menu to find it */
1274         if (pose->active_group <= 0) {
1275                 /* create a new menu, and start populating it with group names */
1276                 pup= uiPupMenuBegin(C, op->type->name, 0);
1277                 layout= uiPupMenuLayout(pup);
1278                 
1279                 /* special entry - allow to create new group, then use that 
1280                  *      (not to be used for removing though)
1281                  */
1282                 if (strstr(op->idname, "assign")) {
1283                         uiItemIntO(layout, "New Group", 0, op->idname, "type", 0);
1284                         uiItemS(layout);
1285                 }
1286                 
1287                 /* add entries for each group */
1288                 for (grp= pose->agroups.first, i=1; grp; grp=grp->next, i++)
1289                         uiItemIntO(layout, grp->name, 0, op->idname, "type", i);
1290                         
1291                 /* finish building the menu, and process it (should result in calling self again) */
1292                 uiPupMenuEnd(C, pup);
1293                 
1294                 return OPERATOR_CANCELLED;
1295         }
1296         else {
1297                 /* just use the active group index, and call the exec callback for the calling operator */
1298                 RNA_int_set(op->ptr, "type", pose->active_group);
1299                 return op->type->exec(C, op);
1300         }
1301 }
1302
1303 /* Assign selected pchans to the bone group that the user selects */
1304 static int pose_group_assign_exec (bContext *C, wmOperator *op)
1305 {
1306         ScrArea *sa= CTX_wm_area(C);
1307         Object *ob;
1308         bArmature *arm;
1309         bPose *pose;
1310         short done= 0;
1311         
1312         /* since this call may also be used from the buttons window, we need to check for where to get the object */
1313         if (sa->spacetype == SPACE_BUTS) 
1314                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1315         else
1316                 ob= CTX_data_active_object(C);
1317         
1318         /* only continue if there's an object, and a pose there too */
1319         if (ELEM(NULL, ob, ob->pose))
1320                 return OPERATOR_CANCELLED;
1321         arm= ob->data;
1322         pose= ob->pose;
1323         
1324         /* set the active group number to the one from operator props 
1325          *      - if 0 after this, make a new group...
1326          */
1327         pose->active_group= RNA_int_get(op->ptr, "type");
1328         if (pose->active_group == 0)
1329                 pose_add_group(ob);
1330         
1331         /* add selected bones to group then */
1332         // NOTE: unfortunately, we cannot use the context-iterators here, since they might not be defined...
1333         CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pose_bones) {
1334                 pchan->agrp_index= pose->active_group;
1335                 done= 1;
1336         }
1337         CTX_DATA_END;
1338
1339         /* notifiers for updates */
1340         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1341         
1342         /* report done status */
1343         if (done)
1344                 return OPERATOR_FINISHED;
1345         else
1346                 return OPERATOR_CANCELLED;
1347 }
1348
1349 void POSE_OT_group_assign (wmOperatorType *ot)
1350 {
1351         /* identifiers */
1352         ot->name= "Add Selected to Bone Group";
1353         ot->idname= "POSE_OT_group_assign";
1354         ot->description= "Add selected bones to the chosen bone group.";
1355         
1356         /* api callbacks */
1357         ot->invoke= pose_groups_menu_invoke;
1358         ot->exec= pose_group_assign_exec;
1359         ot->poll= ED_operator_posemode;
1360         
1361         /* flags */
1362         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1363         
1364         /* properties */
1365         RNA_def_int(ot->srna, "type", 0, 0, 10, "Bone Group Index", "", 0, INT_MAX);
1366 }
1367
1368
1369 static int pose_group_unassign_exec (bContext *C, wmOperator *op)
1370 {
1371         ScrArea *sa= CTX_wm_area(C);
1372         Object *ob;
1373         bArmature *arm;
1374         bPose *pose;
1375         bPoseChannel *pchan;
1376         short done= 0;
1377         
1378         /* since this call may also be used from the buttons window, we need to check for where to get the object */
1379         if (sa->spacetype == SPACE_BUTS) 
1380                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1381         else
1382                 ob= CTX_data_active_object(C);
1383         
1384         /* only continue if there's an object, and a pose there too */
1385         if (ELEM(NULL, ob, ob->pose))
1386                 return OPERATOR_CANCELLED;
1387         pose= ob->pose;
1388         arm= ob->data;
1389         
1390         /* add selected bones to ungroup then */
1391         // NOTE: unfortunately, we cannot use the context-iterators here, since they might not be defined...
1392         // CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pose_bones) 
1393         for (pchan= pose->chanbase.first; pchan; pchan= pchan->next) {
1394                 /* ensure that PoseChannel is on visible layer and is not hidden in PoseMode */
1395                 // NOTE: sync this view3d_context() in space_view3d.c
1396                 if ((pchan->bone) && (arm->layer & pchan->bone->layer) && !(pchan->bone->flag & BONE_HIDDEN_P)) {
1397                         if (pchan->bone->flag & BONE_SELECTED || pchan->bone == arm->act_bone) {
1398                                 if (pchan->agrp_index) {
1399                                         pchan->agrp_index= 0;
1400                                         done= 1;
1401                                 }
1402                         }
1403                 }
1404         }
1405         
1406         /* notifiers for updates */
1407         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1408         
1409         /* report done status */
1410         if (done)
1411                 return OPERATOR_FINISHED;
1412         else
1413                 return OPERATOR_CANCELLED;
1414 }
1415
1416 void POSE_OT_group_unassign (wmOperatorType *ot)
1417 {
1418         /* identifiers */
1419         ot->name= "Remove Selected from Bone Groups";
1420         ot->idname= "POSE_OT_group_unassign";
1421         ot->description= "Add selected bones from all bone groups";
1422         
1423         /* api callbacks */
1424         ot->exec= pose_group_unassign_exec;
1425         ot->poll= ED_operator_posemode;
1426         
1427         /* flags */
1428         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1429 }
1430
1431 static short pose_select_same_group (Object *ob)
1432 {
1433         bPose *pose= (ob)? ob->pose : NULL;
1434         bArmature *arm= (ob)? ob->data : NULL;
1435         bPoseChannel *pchan, *chan;
1436         short changed= 0;
1437         
1438         if (ELEM3(NULL, ob, pose, arm))
1439                 return 0;
1440         
1441         /* loop in loop... bad and slow! */
1442         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1443                 if (arm->layer & pchan->bone->layer) {
1444                         if (pchan->bone->flag & BONE_SELECTED || pchan->bone == arm->act_bone) {
1445                                 
1446                                 /* only if group matches (and is not selected or current bone) */
1447                                 for (chan= ob->pose->chanbase.first; chan; chan= chan->next) {
1448                                         if (arm->layer & chan->bone->layer) {
1449                                                 if (pchan->agrp_index == chan->agrp_index) {
1450                                                         chan->bone->flag |= BONE_SELECTED;
1451                                                         changed= 1;
1452                                                 }
1453                                         }
1454                                 }
1455                                 
1456                         }
1457                 }
1458         }
1459         
1460         return changed;
1461 }
1462
1463 static short pose_select_same_layer (Object *ob)
1464 {
1465         bPose *pose= (ob)? ob->pose : NULL;
1466         bArmature *arm= (ob)? ob->data : NULL;
1467         bPoseChannel *pchan;
1468         int layers= 0, changed= 0;
1469         
1470         if (ELEM3(NULL, ob, pose, arm))
1471                 return 0;
1472         
1473         /* figure out what bones are selected */
1474         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1475                 if (arm->layer & pchan->bone->layer) {
1476                         if (pchan->bone->flag & BONE_SELECTED || pchan->bone == arm->act_bone) {
1477                                 layers |= pchan->bone->layer;
1478                         }
1479                 }
1480         }
1481         if (layers == 0) 
1482                 return 0;
1483                 
1484         /* select bones that are on same layers as layers flag */
1485         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1486                 if (arm->layer & pchan->bone->layer) {
1487                         if (layers & pchan->bone->layer) {
1488                                 pchan->bone->flag |= BONE_SELECTED;
1489                                 changed= 1;
1490                         }
1491                 }
1492         }
1493         
1494         return changed;
1495 }
1496
1497 void pose_select_grouped (Scene *scene, short nr)
1498 {
1499         short changed = 0;
1500         
1501         if (nr == 1)            changed= pose_select_same_group(OBACT);
1502         else if (nr == 2)       changed= pose_select_same_layer(OBACT);
1503         
1504         if (changed) {
1505                 countall();
1506                 BIF_undo_push("Select Grouped");
1507         }
1508 }
1509
1510 /* Shift-G in 3D-View while in PoseMode */
1511 void pose_select_grouped_menu (Scene *scene)
1512 {
1513         short nr;
1514         
1515         /* here we go */
1516         nr= pupmenu("Select Grouped%t|In Same Group%x1|In Same Layer%x2");
1517         pose_select_grouped(scene, nr);
1518 }
1519
1520 /* ********************************************** */
1521
1522 static int pose_flip_names_exec (bContext *C, wmOperator *op)
1523 {
1524         Object *ob= CTX_data_active_object(C);
1525         bArmature *arm;
1526         char newname[32];
1527         
1528         /* paranoia checks */
1529         if (ELEM(NULL, ob, ob->pose)) 
1530                 return OPERATOR_CANCELLED;
1531         arm= ob->data;
1532         
1533         /* loop through selected bones, auto-naming them */
1534         CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pose_bones)
1535         {
1536                 BLI_strncpy(newname, pchan->name, sizeof(newname));
1537                 bone_flip_name(newname, 1);     // 1 = do strip off number extensions
1538                 ED_armature_bone_rename(arm, pchan->name, newname);
1539         }
1540         CTX_DATA_END;
1541         
1542         /* since we renamed stuff... */
1543         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
1544
1545         /* note, notifier might evolve */
1546         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1547         
1548         return OPERATOR_FINISHED;
1549 }
1550
1551 void POSE_OT_flip_names (wmOperatorType *ot)
1552 {
1553         /* identifiers */
1554         ot->name= "Flip Names";
1555         ot->idname= "POSE_OT_flip_names";
1556         ot->description= "Flips (and corrects) the names of selected bones.";
1557         
1558         /* api callbacks */
1559         ot->exec= pose_flip_names_exec;
1560         ot->poll= ED_operator_posemode;
1561         
1562         /* flags */
1563         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1564 }
1565
1566 /* ------------------ */
1567
1568 static int pose_autoside_names_exec (bContext *C, wmOperator *op)
1569 {
1570         Object *ob= CTX_data_active_object(C);
1571         bArmature *arm;
1572         char newname[32];
1573         short axis= RNA_enum_get(op->ptr, "axis");
1574         
1575         /* paranoia checks */
1576         if (ELEM(NULL, ob, ob->pose)) 
1577                 return OPERATOR_CANCELLED;
1578         arm= ob->data;
1579         
1580         /* loop through selected bones, auto-naming them */
1581         CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pose_bones)
1582         {
1583                 BLI_strncpy(newname, pchan->name, sizeof(newname));
1584                 bone_autoside_name(newname, 1, axis, pchan->bone->head[axis], pchan->bone->tail[axis]);
1585                 ED_armature_bone_rename(arm, pchan->name, newname);
1586         }
1587         CTX_DATA_END;
1588         
1589         /* since we renamed stuff... */
1590         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
1591
1592         /* note, notifier might evolve */
1593         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1594         
1595         return OPERATOR_FINISHED;
1596 }
1597
1598 void POSE_OT_autoside_names (wmOperatorType *ot)
1599 {
1600         static EnumPropertyItem axis_items[]= {
1601                 {0, "XAXIS", 0, "X-Axis", "Left/Right"},
1602                 {1, "YAXIS", 0, "Y-Axis", "Front/Back"},
1603                 {2, "ZAXIS", 0, "Z-Axis", "Top/Bottom"},
1604                 {0, NULL, 0, NULL, NULL}};
1605         
1606         /* identifiers */
1607         ot->name= "AutoName by Axis";
1608         ot->idname= "POSE_OT_autoside_names";
1609         ot->description= "Automatically renames the selected bones according to which side of the target axis they fall on.";
1610         
1611         /* api callbacks */
1612         ot->invoke= WM_menu_invoke;
1613         ot->exec= pose_autoside_names_exec;
1614         ot->poll= ED_operator_posemode;
1615         
1616         /* flags */
1617         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1618         
1619         /* settings */
1620         ot->prop= RNA_def_enum(ot->srna, "axis", axis_items, 0, "Axis", "Axis tag names with.");
1621 }
1622
1623 /* ********************************************** */
1624
1625 /* context active object, or weightpainted object with armature in posemode */
1626 void pose_activate_flipped_bone(Scene *scene)
1627 {
1628         Object *ob= OBACT;
1629         bArmature *arm= ob->data;
1630         
1631         if(ob==NULL) return;
1632
1633         if(ob->mode && OB_MODE_WEIGHT_PAINT) {
1634                 ob= modifiers_isDeformedByArmature(ob);
1635         }
1636         if(ob && (ob->mode & OB_MODE_POSE)) {
1637                 bPoseChannel *pchanf;
1638                 
1639                 if(arm->act_bone) {
1640                         char name[32];
1641                         
1642                         BLI_strncpy(name, arm->act_bone->name, 32);
1643                         bone_flip_name(name, 1);        // 0 = do not strip off number extensions
1644                         
1645                         pchanf= get_pose_channel(ob->pose, name);
1646                         if(pchanf && pchanf->bone != arm->act_bone) {
1647                                 arm->act_bone->flag &= ~BONE_SELECTED;
1648                                 pchanf->bone->flag |= BONE_SELECTED;
1649
1650                                 arm->act_bone= pchanf->bone;
1651
1652                                 /* in weightpaint we select the associated vertex group too */
1653                                 if(ob->mode & OB_MODE_WEIGHT_PAINT) {
1654                                         ED_vgroup_select_by_name(OBACT, name);
1655                                         DAG_id_flush_update(&OBACT->id, OB_RECALC_DATA);
1656                                 }
1657                                 
1658                                 // XXX notifiers need to be sent to other editors to update
1659                                 
1660                         }                       
1661                 }
1662         }
1663 }
1664
1665
1666 /* ********************************************** */
1667
1668 /* Present a popup to get the layers that should be used */
1669 static int pose_armature_layers_invoke (bContext *C, wmOperator *op, wmEvent *evt)
1670 {
1671         Object *ob= CTX_data_active_object(C);
1672         bArmature *arm= (ob)? ob->data : NULL;
1673         PointerRNA ptr;
1674         int layers[32]; /* hardcoded for now - we can only have 32 armature layers, so this should be fine... */
1675         
1676         /* sanity checking */
1677         if (arm == NULL)
1678                 return OPERATOR_CANCELLED;
1679                 
1680         /* get RNA pointer to armature data to use that to retrieve the layers as ints to init the operator */
1681         RNA_id_pointer_create((ID *)arm, &ptr);
1682         RNA_boolean_get_array(&ptr, "layer", layers);
1683         RNA_boolean_set_array(op->ptr, "layers", layers);
1684         
1685         /* part to sync with other similar operators... */
1686         return WM_operator_props_popup(C, op, evt);
1687 }
1688
1689 /* Set the visible layers for the active armature (edit and pose modes) */
1690 static int pose_armature_layers_exec (bContext *C, wmOperator *op)
1691 {
1692         Object *ob= CTX_data_active_object(C);
1693         bArmature *arm= (ob)? ob->data : NULL;
1694         PointerRNA ptr;
1695         int layers[32]; /* hardcoded for now - we can only have 32 armature layers, so this should be fine... */
1696         
1697         /* get the values set in the operator properties */
1698         RNA_boolean_get_array(op->ptr, "layers", layers);
1699         
1700         /* get pointer for armature, and write data there... */
1701         RNA_id_pointer_create((ID *)arm, &ptr);
1702         RNA_boolean_set_array(&ptr, "layer", layers);
1703         
1704         /* note, notifier might evolve */
1705         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1706         
1707         return OPERATOR_FINISHED;
1708 }
1709
1710
1711 void POSE_OT_armature_layers (wmOperatorType *ot)
1712 {
1713         /* identifiers */
1714         ot->name= "Change Armature Layers";
1715         ot->idname= "POSE_OT_armature_layers";
1716         ot->description= "Change the visible armature layers.";
1717         
1718         /* callbacks */
1719         ot->invoke= pose_armature_layers_invoke;
1720         ot->exec= pose_armature_layers_exec;
1721         ot->poll= ED_operator_posemode;
1722         
1723         /* flags */
1724         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1725         
1726         /* properties */
1727         RNA_def_boolean_layer_member(ot->srna, "layers", 32, NULL, "Layer", "Armature layers to make visible");
1728 }
1729
1730 void ARMATURE_OT_armature_layers (wmOperatorType *ot)
1731 {
1732         /* identifiers */
1733         ot->name= "Change Armature Layers";
1734         ot->idname= "ARMATURE_OT_armature_layers";
1735         ot->description= "Change the visible armature layers.";
1736         
1737         /* callbacks */
1738         ot->invoke= pose_armature_layers_invoke;
1739         ot->exec= pose_armature_layers_exec;
1740         ot->poll= ED_operator_editarmature;
1741         
1742         /* flags */
1743         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1744         
1745         /* properties */
1746         RNA_def_boolean_layer_member(ot->srna, "layers", 32, NULL, "Layer", "Armature layers to make visible");
1747 }
1748
1749 /* ------------------- */
1750
1751 /* Present a popup to get the layers that should be used */
1752 static int pose_bone_layers_invoke (bContext *C, wmOperator *op, wmEvent *evt)
1753 {
1754         int layers[32]; /* hardcoded for now - we can only have 32 armature layers, so this should be fine... */
1755         
1756         /* get layers that are active already */
1757         memset(&layers, 0, sizeof(layers)); /* set all layers to be off by default */
1758         
1759         CTX_DATA_BEGIN(C, bPoseChannel *, pchan, selected_pose_bones) 
1760         {
1761                 short bit;
1762                 
1763                 /* loop over the bits for this pchan's layers, adding layers where they're needed */
1764                 for (bit= 0; bit < 32; bit++) {
1765                         if (pchan->bone->layer & (1<<bit))
1766                                 layers[bit]= 1;
1767                 }
1768         }
1769         CTX_DATA_END;
1770         
1771         /* copy layers to operator */
1772         RNA_boolean_set_array(op->ptr, "layers", layers);
1773         
1774                 /* part to sync with other similar operators... */
1775         return WM_operator_props_popup(C, op, evt);
1776 }
1777
1778 /* Set the visible layers for the active armature (edit and pose modes) */
1779 static int pose_bone_layers_exec (bContext *C, wmOperator *op)
1780 {
1781         Object *ob= CTX_data_active_object(C);
1782         bArmature *arm= (ob)? ob->data : NULL;
1783         PointerRNA ptr;
1784         int layers[32]; /* hardcoded for now - we can only have 32 armature layers, so this should be fine... */
1785         
1786         /* get the values set in the operator properties */
1787         RNA_boolean_get_array(op->ptr, "layers", layers);
1788         
1789         /* set layers of pchans based on the values set in the operator props */
1790         CTX_DATA_BEGIN(C, bPoseChannel *, pchan, selected_pose_bones) 
1791         {
1792                 /* get pointer for pchan, and write flags this way */
1793                 RNA_pointer_create((ID *)arm, &RNA_Bone, pchan->bone, &ptr);
1794                 RNA_boolean_set_array(&ptr, "layer", layers);
1795         }
1796         CTX_DATA_END;
1797         
1798         /* note, notifier might evolve */
1799         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1800         
1801         return OPERATOR_FINISHED;
1802 }
1803
1804 void POSE_OT_bone_layers (wmOperatorType *ot)
1805 {
1806         /* identifiers */
1807         ot->name= "Change Bone Layers";
1808         ot->idname= "POSE_OT_bone_layers";
1809         ot->description= "Change the layers that the selected bones belong to.";
1810         
1811         /* callbacks */
1812         ot->invoke= pose_bone_layers_invoke;
1813         ot->exec= pose_bone_layers_exec;
1814         ot->poll= ED_operator_posemode;
1815         
1816         /* flags */
1817         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1818         
1819         /* properties */
1820         RNA_def_boolean_layer_member(ot->srna, "layers", 32, NULL, "Layer", "Armature layers that bone belongs to");
1821 }
1822
1823 /* ------------------- */
1824
1825 /* Present a popup to get the layers that should be used */
1826 static int armature_bone_layers_invoke (bContext *C, wmOperator *op, wmEvent *evt)
1827 {
1828         int layers[32]; /* hardcoded for now - we can only have 32 armature layers, so this should be fine... */
1829         
1830         /* get layers that are active already */
1831         memset(&layers, 0, sizeof(layers)); /* set all layers to be off by default */
1832         
1833         CTX_DATA_BEGIN(C, EditBone *, ebone, selected_editable_bones) 
1834         {
1835                 short bit;
1836                 
1837                 /* loop over the bits for this pchan's layers, adding layers where they're needed */
1838                 for (bit= 0; bit < 32; bit++) {
1839                         if (ebone->layer & (1<<bit))
1840                                 layers[bit]= 1;
1841                 }
1842         }
1843         CTX_DATA_END;
1844         
1845         /* copy layers to operator */
1846         RNA_boolean_set_array(op->ptr, "layers", layers);
1847         
1848                 /* part to sync with other similar operators... */
1849         return WM_operator_props_popup(C, op, evt);
1850 }
1851
1852 /* Set the visible layers for the active armature (edit and pose modes) */
1853 static int armature_bone_layers_exec (bContext *C, wmOperator *op)
1854 {
1855         Object *ob= CTX_data_edit_object(C);
1856         bArmature *arm= (ob)? ob->data : NULL;
1857         PointerRNA ptr;
1858         int layers[32]; /* hardcoded for now - we can only have 32 armature layers, so this should be fine... */
1859         
1860         /* get the values set in the operator properties */
1861         RNA_boolean_get_array(op->ptr, "layers", layers);
1862         
1863         /* set layers of pchans based on the values set in the operator props */
1864         CTX_DATA_BEGIN(C, EditBone *, ebone, selected_editable_bones) 
1865         {
1866                 /* get pointer for pchan, and write flags this way */
1867                 RNA_pointer_create((ID *)arm, &RNA_EditBone, ebone, &ptr);
1868                 RNA_boolean_set_array(&ptr, "layer", layers);
1869         }
1870         CTX_DATA_END;
1871         
1872         /* note, notifier might evolve */
1873         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1874         
1875         return OPERATOR_FINISHED;
1876 }
1877
1878 void ARMATURE_OT_bone_layers (wmOperatorType *ot)
1879 {
1880         /* identifiers */
1881         ot->name= "Change Bone Layers";
1882         ot->idname= "ARMATURE_OT_bone_layers";
1883         ot->description= "Change the layers that the selected bones belong to.";
1884         
1885         /* callbacks */
1886         ot->invoke= armature_bone_layers_invoke;
1887         ot->exec= armature_bone_layers_exec;
1888         ot->poll= ED_operator_editarmature;
1889         
1890         /* flags */
1891         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1892         
1893         /* properties */
1894         RNA_def_boolean_layer_member(ot->srna, "layers", 32, NULL, "Layer", "Armature layers that bone belongs to");
1895 }
1896
1897 /* ********************************************** */
1898
1899 /* for use in insertkey, ensure rotation goes other way around */
1900 void pose_flipquats(Scene *scene)
1901 {
1902         Object *ob = OBACT;
1903         bArmature *arm= ob->data;
1904         bPoseChannel *pchan;
1905         
1906         if(ob->pose==NULL)
1907                 return;
1908         
1909         /* find sel bones */
1910         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1911                 if(pchan->bone && (pchan->bone->flag & BONE_SELECTED) && (pchan->bone->layer & arm->layer)) {
1912                         /* quaternions have 720 degree range */
1913                         pchan->quat[0]= -pchan->quat[0];
1914                         pchan->quat[1]= -pchan->quat[1];
1915                         pchan->quat[2]= -pchan->quat[2];
1916                         pchan->quat[3]= -pchan->quat[3];
1917                 }
1918         }
1919         
1920         /* do autokey */
1921         autokeyframe_pose_cb_func(ob, TFM_ROTATION, 0);
1922 }
1923
1924 /* context: active channel */
1925 void pose_special_editmenu(Scene *scene)
1926 {
1927 #if 0
1928         Object *obedit= scene->obedit; // XXX context
1929         Object *ob= OBACT;
1930         short nr;
1931         
1932         /* paranoia checks */
1933         if(!ob && !ob->pose) return;
1934         if(ob==obedit || (ob->mode & OB_MODE_POSE)==0) return;
1935         
1936         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");
1937         if(nr==1) {
1938                 pose_select_constraint_target(scene);
1939         }
1940         else if(nr==2) {
1941                 pose_flip_names();
1942         }
1943         else if(nr==3) {
1944                 pose_calculate_path(C, ob);
1945         }
1946         else if(nr==4) {
1947                 pose_clear_paths(ob);
1948         }
1949         else if(nr==5) {
1950                 pose_clear_user_transforms(scene, ob);
1951         }
1952         else if(nr==6) {
1953                 pose_relax();
1954         }
1955         else if(ELEM3(nr, 7, 8, 9)) {
1956                 pose_autoside_names(nr-7);
1957         }
1958 #endif
1959 }
1960
1961 /* Restore selected pose-bones to 'action'-defined pose */
1962 void pose_clear_user_transforms(Scene *scene, Object *ob)
1963 {
1964         bArmature *arm= ob->data;
1965         bPoseChannel *pchan;
1966         
1967         if (ob->pose == NULL)
1968                 return;
1969         
1970         /* if the object has an action, restore pose to the pose defined by the action by clearing pose on selected bones */
1971         if (ob->action) {
1972                 /* find selected bones */
1973                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1974                         if (pchan->bone && (pchan->bone->flag & BONE_SELECTED) && (pchan->bone->layer & arm->layer)) {
1975                                 /* just clear the BONE_UNKEYED flag, allowing this bone to get overwritten by actions again */
1976                                 pchan->bone->flag &= ~BONE_UNKEYED;
1977                         }
1978                 }
1979                 
1980                 /* clear pose locking flag 
1981                  *      - this will only clear the user-defined pose in the selected bones, where BONE_UNKEYED has been cleared
1982                  */
1983                 ob->pose->flag |= POSE_DO_UNLOCK;
1984         }
1985         else {
1986                 /* no action, so restore entire pose to rest pose (cannot restore only selected bones) */
1987                 rest_pose(ob->pose);
1988         }
1989         
1990         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
1991         BIF_undo_push("Clear User Transform");
1992 }
1993