Sculpt: svn merge https://svn.blender.org/svnroot/bf-blender/trunk/blender -r24152...
[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                         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->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->mode & OB_MODE_POSE)==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) && !(pchanc->bone->flag & BONE_UNSELECTABLE))
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) && !(pchanc->bone->flag & BONE_UNSELECTABLE)) {
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) && (curbone->flag & BONE_UNSELECTABLE)==0) {
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->mode & OB_MODE_POSE)==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                                                 VECCOPY(pchan->eul, pchanact->eul);
763                                                 break;
764                                         case 3: /* Local Size */
765                                                 VECCOPY(pchan->size, pchanact->size);
766                                                 break;
767                                         case 4: /* All Constraints */
768                                         {
769                                                 ListBase tmp_constraints = {NULL, NULL};
770                                                 
771                                                 /* copy constraints to tmpbase and apply 'local' tags before 
772                                                  * appending to list of constraints for this channel
773                                                  */
774                                                 copy_constraints(&tmp_constraints, &pchanact->constraints);
775                                                 if ((ob->proxy) && (pchan->bone->layer & arm->layer_protected)) {
776                                                         bConstraint *con;
777                                                         
778                                                         /* add proxy-local tags */
779                                                         for (con= tmp_constraints.first; con; con= con->next)
780                                                                 con->flag |= CONSTRAINT_PROXY_LOCAL;
781                                                 }
782                                                 addlisttolist(&pchan->constraints, &tmp_constraints);
783                                                 
784                                                 /* update flags (need to add here, not just copy) */
785                                                 pchan->constflag |= pchanact->constflag;
786                                                 
787                                                 if (ob->pose)
788                                                         ob->pose->flag |= POSE_RECALC;
789                                         }
790                                                 break;
791                                         case 6: /* Transform Locks */
792                                                 pchan->protectflag = pchanact->protectflag;
793                                                 break;
794                                         case 7: /* IK (DOF) settings */
795                                         {
796                                                 pchan->ikflag = pchanact->ikflag;
797                                                 VECCOPY(pchan->limitmin, pchanact->limitmin);
798                                                 VECCOPY(pchan->limitmax, pchanact->limitmax);
799                                                 VECCOPY(pchan->stiffness, pchanact->stiffness);
800                                                 pchan->ikstretch= pchanact->ikstretch;
801                                                 pchan->ikrotweight= pchanact->ikrotweight;
802                                                 pchan->iklinweight= pchanact->iklinweight;
803                                         }
804                                                 break;
805                                         case 8: /* Custom Bone Shape */
806                                                 pchan->custom = pchanact->custom;
807                                                 break;
808                                         case 9: /* Visual Location */
809                                                 armature_loc_pose_to_bone(pchan, pchanact->pose_mat[3], pchan->loc);
810                                                 break;
811                                         case 10: /* Visual Rotation */
812                                         {
813                                                 float delta_mat[4][4];
814                                                 
815                                                 armature_mat_pose_to_bone(pchan, pchanact->pose_mat, delta_mat);
816                                                 
817                                                 if (pchan->rotmode == ROT_MODE_AXISANGLE) {
818                                                         float tmp_quat[4];
819                                                         
820                                                         /* need to convert to quat first (in temp var)... */
821                                                         Mat4ToQuat(delta_mat, tmp_quat);
822                                                         QuatToAxisAngle(tmp_quat, pchan->rotAxis, &pchan->rotAngle);
823                                                 }
824                                                 else if (pchan->rotmode == ROT_MODE_QUAT)
825                                                         Mat4ToQuat(delta_mat, pchan->quat);
826                                                 else
827                                                         Mat4ToEulO(delta_mat, pchan->eul, pchan->rotmode);
828                                         }
829                                                 break;
830                                         case 11: /* Visual Size */
831                                         {
832                                                 float delta_mat[4][4], size[4];
833                                                 
834                                                 armature_mat_pose_to_bone(pchan, pchanact->pose_mat, delta_mat);
835                                                 Mat4ToSize(delta_mat, size);
836                                                 VECCOPY(pchan->size, size);
837                                         }
838                                 }
839                         }
840                 }
841         } 
842         else { /* constraints, optional (note: max we can have is 24 constraints) */
843                 bConstraint *con, *con_back;
844                 int const_toggle[24];
845                 ListBase const_copy = {NULL, NULL};
846                 
847                 BLI_duplicatelist(&const_copy, &(pchanact->constraints));
848                 
849                 /* build the puplist of constraints */
850                 for (con = pchanact->constraints.first, i=0; con; con=con->next, i++){
851                         const_toggle[i]= 1;
852 //                      add_numbut(i, TOG|INT, con->name, 0, 0, &(const_toggle[i]), "");
853                 }
854                 
855 //              if (!do_clever_numbuts("Select Constraints", i, REDRAW)) {
856 //                      BLI_freelistN(&const_copy);
857 //                      return;
858 //              }
859                 
860                 /* now build a new listbase from the options selected */
861                 for (i=0, con=const_copy.first; con; i++) {
862                         /* if not selected, free/remove it from the list */
863                         if (!const_toggle[i]) {
864                                 con_back= con->next;
865                                 BLI_freelinkN(&const_copy, con);
866                                 con= con_back;
867                         } 
868                         else
869                                 con= con->next;
870                 }
871                 
872                 /* Copy the temo listbase to the selected posebones */
873                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
874                         if ( (arm->layer & pchan->bone->layer) &&
875                                  (pchan->bone->flag & BONE_SELECTED) &&
876                                  (pchan!=pchanact) ) 
877                         {
878                                 ListBase tmp_constraints = {NULL, NULL};
879                                 
880                                 /* copy constraints to tmpbase and apply 'local' tags before 
881                                  * appending to list of constraints for this channel
882                                  */
883                                 copy_constraints(&tmp_constraints, &const_copy);
884                                 if ((ob->proxy) && (pchan->bone->layer & arm->layer_protected)) {
885                                         bConstraint *con;
886                                         
887                                         /* add proxy-local tags */
888                                         for (con= tmp_constraints.first; con; con= con->next)
889                                                 con->flag |= CONSTRAINT_PROXY_LOCAL;
890                                 }
891                                 addlisttolist(&pchan->constraints, &tmp_constraints);
892                                 
893                                 /* update flags (need to add here, not just copy) */
894                                 pchan->constflag |= pchanact->constflag;
895                         }
896                 }
897                 BLI_freelistN(&const_copy);
898                 update_pose_constraint_flags(ob->pose); /* we could work out the flags but its simpler to do this */
899                 
900                 if (ob->pose)
901                         ob->pose->flag |= POSE_RECALC;
902         }
903         
904         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);   // and all its relations
905         
906         BIF_undo_push("Copy Pose Attributes");
907         
908 }
909
910 /* ******************** copy/paste pose ********************** */
911
912 /* Global copy/paste buffer for pose - cleared on start/end session + before every copy operation */
913 static bPose *g_posebuf = NULL;
914
915 void free_posebuf(void) 
916 {
917         if (g_posebuf) {
918                 /* was copied without constraints */
919                 BLI_freelistN(&g_posebuf->chanbase);
920                 MEM_freeN(g_posebuf);
921         }
922         
923         g_posebuf=NULL;
924 }
925
926 /* ---- */
927
928 static int pose_copy_exec (bContext *C, wmOperator *op)
929 {
930         Object *ob= CTX_data_active_object(C);
931         
932         /* sanity checking */
933         if ELEM(NULL, ob, ob->pose) {
934                 BKE_report(op->reports, RPT_ERROR, "No Pose to Copy");
935                 return OPERATOR_CANCELLED;
936         }
937
938         /* free existing pose buffer */
939         free_posebuf();
940         
941         /* sets chan->flag to POSE_KEY if bone selected, then copy those bones to the buffer */
942         set_pose_keys(ob);  
943         copy_pose(&g_posebuf, ob->pose, 0);
944         
945         
946         return OPERATOR_FINISHED;
947 }
948
949 void POSE_OT_copy (wmOperatorType *ot) 
950 {
951         /* identifiers */
952         ot->name= "Copy Pose";
953         ot->idname= "POSE_OT_copy";
954         ot->description= "Copies the current pose of the selected bones to copy/paste buffer.";
955         
956         /* api callbacks */
957         ot->exec= pose_copy_exec;
958         ot->poll= ED_operator_posemode;
959         
960         /* flag */
961         ot->flag= OPTYPE_REGISTER;
962 }
963
964 /* ---- */
965
966 /* Pointers to the builtin KeyingSets that we want to use */
967 static KeyingSet *posePaste_ks_locrotscale = NULL;              /* the only keyingset we'll need */
968
969 /* ---- */
970
971 static int pose_paste_exec (bContext *C, wmOperator *op)
972 {
973         Scene *scene= CTX_data_scene(C);
974         Object *ob= CTX_data_active_object(C);
975         bPoseChannel *chan, *pchan;
976         char name[32];
977         int flip= RNA_boolean_get(op->ptr, "flipped");
978         
979         bCommonKeySrc cks;
980         ListBase dsources = {&cks, &cks};
981         
982         /* init common-key-source for use by KeyingSets */
983         memset(&cks, 0, sizeof(bCommonKeySrc));
984         cks.id= &ob->id;
985         
986         /* sanity checks */
987         if ELEM(NULL, ob, ob->pose)
988                 return OPERATOR_CANCELLED;
989
990         if (g_posebuf == NULL) {
991                 BKE_report(op->reports, RPT_ERROR, "Copy buffer is empty");
992                 return OPERATOR_CANCELLED;
993         }
994         
995         /* Safely merge all of the channels in the buffer pose into any existing pose */
996         for (chan= g_posebuf->chanbase.first; chan; chan=chan->next) {
997                 if (chan->flag & POSE_KEY) {
998                         /* get the name - if flipping, we must flip this first */
999                         BLI_strncpy(name, chan->name, sizeof(name));
1000                         if (flip)
1001                                 bone_flip_name(name, 0);                /* 0 = don't strip off number extensions */
1002                                 
1003                         /* only copy when channel exists, poses are not meant to add random channels to anymore */
1004                         pchan= get_pose_channel(ob->pose, name);
1005                         
1006                         if (pchan) {
1007                                 /* only loc rot size 
1008                                  *      - only copies transform info for the pose 
1009                                  */
1010                                 VECCOPY(pchan->loc, chan->loc);
1011                                 VECCOPY(pchan->size, chan->size);
1012                                 pchan->flag= chan->flag;
1013                                 
1014                                 /* check if rotation modes are compatible (i.e. do they need any conversions) */
1015                                 if (pchan->rotmode == chan->rotmode) {
1016                                         /* copy the type of rotation in use */
1017                                         if (pchan->rotmode > 0) {
1018                                                 VECCOPY(pchan->eul, chan->eul);
1019                                         }
1020                                         else {
1021                                                 QUATCOPY(pchan->quat, chan->quat);
1022                                         }
1023                                 }
1024                                 else if (pchan->rotmode > 0) {
1025                                         /* quat/axis-angle to euler */
1026                                         if (chan->rotmode == ROT_MODE_AXISANGLE)
1027                                                 AxisAngleToEulO(chan->rotAxis, chan->rotAngle, pchan->eul, pchan->rotmode);
1028                                         else
1029                                                 QuatToEulO(chan->quat, pchan->eul, pchan->rotmode);
1030                                 }
1031                                 else if (pchan->rotmode == ROT_MODE_AXISANGLE) {
1032                                         /* quat/euler to axis angle */
1033                                         if (chan->rotmode > 0)
1034                                                 EulOToAxisAngle(chan->eul, chan->rotmode, pchan->rotAxis, &pchan->rotAngle);
1035                                         else    
1036                                                 QuatToAxisAngle(chan->quat, pchan->rotAxis, &pchan->rotAngle);
1037                                 }
1038                                 else {
1039                                         /* euler/axis-angle to quat */
1040                                         if (chan->rotmode > 0)
1041                                                 EulOToQuat(chan->eul, chan->rotmode, pchan->quat);
1042                                         else
1043                                                 AxisAngleToQuat(pchan->quat, chan->rotAxis, pchan->rotAngle);
1044                                 }
1045                                 
1046                                 /* paste flipped pose? */
1047                                 if (flip) {
1048                                         pchan->loc[0]*= -1;
1049                                         
1050                                         /* has to be done as eulers... */
1051                                         if (pchan->rotmode > 0) {
1052                                                 pchan->eul[1] *= -1;
1053                                                 pchan->eul[2] *= -1;
1054                                         }
1055                                         else if (pchan->rotmode == ROT_MODE_AXISANGLE) {
1056                                                 float eul[3];
1057                                                 
1058                                                 AxisAngleToEulO(pchan->rotAxis, pchan->rotAngle, eul, EULER_ORDER_DEFAULT);
1059                                                 eul[1]*= -1;
1060                                                 eul[2]*= -1;
1061                                                 EulOToAxisAngle(eul, EULER_ORDER_DEFAULT, pchan->rotAxis, &pchan->rotAngle);
1062                                                 
1063                                                 // experimental method (uncomment to test):
1064 #if 0
1065                                                 /* experimental method: just flip the orientation of the axis on x/y axes */
1066                                                 pchan->quat[1] *= -1;
1067                                                 pchan->quat[2] *= -1;
1068 #endif
1069                                         }
1070                                         else {
1071                                                 float eul[3];
1072                                                 
1073                                                 QuatToEul(pchan->quat, eul);
1074                                                 eul[1]*= -1;
1075                                                 eul[2]*= -1;
1076                                                 EulToQuat(eul, pchan->quat);
1077                                         }
1078                                 }
1079                                 
1080                                 if (autokeyframe_cfra_can_key(scene, &ob->id)) {
1081                                         /* Set keys on pose
1082                                          *      - KeyingSet to use depends on rotation mode 
1083                                          *      (but that's handled by the templates code)  
1084                                          */
1085                                         // TODO: for getting the KeyingSet used, we should really check which channels were affected
1086                                         if (posePaste_ks_locrotscale == NULL)
1087                                                 posePaste_ks_locrotscale= ANIM_builtin_keyingset_get_named(NULL, "LocRotScale");
1088                                         
1089                                         /* init cks for this PoseChannel, then use the relative KeyingSets to keyframe it */
1090                                         cks.pchan= pchan;
1091                                         
1092                                         modify_keyframes(scene, &dsources, NULL, posePaste_ks_locrotscale, MODIFYKEY_MODE_INSERT, (float)CFRA);
1093                                         
1094                                         /* clear any unkeyed tags */
1095                                         if (chan->bone)
1096                                                 chan->bone->flag &= ~BONE_UNKEYED;
1097                                 }
1098                                 else {
1099                                         /* add unkeyed tags */
1100                                         if (chan->bone)
1101                                                 chan->bone->flag |= BONE_UNKEYED;
1102                                 }
1103                         }
1104                 }
1105         }
1106
1107         /* Update event for pose and deformation children */
1108         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
1109         
1110         if (IS_AUTOKEY_ON(scene)) {
1111 // XXX          remake_action_ipos(ob->action);
1112         }
1113         else {
1114                 /* need to trick depgraph, action is not allowed to execute on pose */
1115                 where_is_pose(scene, ob);
1116                 ob->recalc= 0;
1117         }
1118         
1119         /* notifiers for updates */
1120         WM_event_add_notifier(C, NC_OBJECT|ND_POSE|ND_TRANSFORM, ob);
1121         WM_event_add_notifier(C, NC_ANIMATION|ND_KEYFRAME_EDIT, NULL); // XXX not really needed, but here for completeness...
1122
1123         return OPERATOR_FINISHED;
1124 }
1125
1126 void POSE_OT_paste (wmOperatorType *ot)
1127 {
1128         /* identifiers */
1129         ot->name= "Paste Pose";
1130         ot->idname= "POSE_OT_paste";
1131         ot->description= "Pastes the stored pose on to the current pose.";
1132         
1133         /* api callbacks */
1134         ot->exec= pose_paste_exec;
1135         ot->poll= ED_operator_posemode;
1136         
1137         /* flag */
1138         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1139         
1140         /* properties */
1141         RNA_def_boolean(ot->srna, "flipped", 0, "Flipped on X-Axis", "");
1142 }
1143
1144 /* ********************************************** */
1145
1146 /* context weightpaint and deformer in posemode */
1147 void pose_adds_vgroups(Scene *scene, Object *meshobj, int heatweights)
1148 {
1149 // XXX  extern VPaint Gwp;         /* from vpaint */
1150         Object *poseobj= modifiers_isDeformedByArmature(meshobj);
1151
1152         if(poseobj==NULL || (poseobj->mode & OB_MODE_POSE)==0) {
1153                 error("The active object must have a deforming armature in pose mode");
1154                 return;
1155         }
1156
1157 // XXX  add_verts_to_dgroups(meshobj, poseobj, heatweights, ((Mesh *)(meshobj->data))->editflag & ME_EDIT_MIRROR_X);
1158
1159         if(heatweights)
1160                 BIF_undo_push("Apply Bone Heat Weights to Vertex Groups");
1161         else
1162                 BIF_undo_push("Apply Bone Envelopes to Vertex Groups");
1163
1164         
1165         // and all its relations
1166         DAG_id_flush_update(&meshobj->id, OB_RECALC_DATA);
1167 }
1168
1169 /* ********************************************** */
1170
1171
1172 static int pose_group_add_exec (bContext *C, wmOperator *op)
1173 {
1174         ScrArea *sa= CTX_wm_area(C);
1175         Object *ob;
1176         
1177         /* since this call may also be used from the buttons window, we need to check for where to get the object */
1178         if (sa->spacetype == SPACE_BUTS) 
1179                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1180         else
1181                 ob= CTX_data_active_object(C);
1182                 
1183         /* only continue if there's an object */
1184         if (ob == NULL)
1185                 return OPERATOR_CANCELLED;
1186         
1187         /* for now, just call the API function for this */
1188         pose_add_group(ob);
1189         
1190         /* notifiers for updates */
1191         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1192         
1193         return OPERATOR_FINISHED;
1194 }
1195
1196 void POSE_OT_group_add (wmOperatorType *ot)
1197 {
1198         /* identifiers */
1199         ot->name= "Add Bone Group";
1200         ot->idname= "POSE_OT_group_add";
1201         ot->description= "Add a new bone group.";
1202         
1203         /* api callbacks */
1204         ot->exec= pose_group_add_exec;
1205         ot->poll= ED_operator_posemode;
1206         
1207         /* flags */
1208         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1209 }
1210
1211
1212 static int pose_group_remove_exec (bContext *C, wmOperator *op)
1213 {
1214         ScrArea *sa= CTX_wm_area(C);
1215         Object *ob;
1216         
1217         /* since this call may also be used from the buttons window, we need to check for where to get the object */
1218         if (sa->spacetype == SPACE_BUTS) 
1219                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1220         else
1221                 ob= CTX_data_active_object(C);
1222         
1223         /* only continue if there's an object */
1224         if (ob == NULL)
1225                 return OPERATOR_CANCELLED;
1226         
1227         /* for now, just call the API function for this */
1228         pose_remove_group(ob);
1229         
1230         /* notifiers for updates */
1231         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1232         
1233         return OPERATOR_FINISHED;
1234 }
1235
1236 void POSE_OT_group_remove (wmOperatorType *ot)
1237 {
1238         /* identifiers */
1239         ot->name= "Remove Bone Group";
1240         ot->idname= "POSE_OT_group_remove";
1241         ot->description= "Removes the active bone group.";
1242         
1243         /* api callbacks */
1244         ot->exec= pose_group_remove_exec;
1245         ot->poll= ED_operator_posemode;
1246         
1247         /* flags */
1248         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1249 }
1250
1251 /* ------------ */
1252
1253 /* invoke callback which presents a list of bone-groups for the user to choose from */
1254 static int pose_groups_menu_invoke (bContext *C, wmOperator *op, wmEvent *evt)
1255 {
1256         ScrArea *sa= CTX_wm_area(C);
1257         Object *ob;
1258         bPose *pose;
1259         
1260         uiPopupMenu *pup;
1261         uiLayout *layout;
1262         bActionGroup *grp;
1263         int i;
1264         
1265         /* since this call may also be used from the buttons window, we need to check for where to get the object */
1266         if (sa->spacetype == SPACE_BUTS) 
1267                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1268         else
1269                 ob= CTX_data_active_object(C);
1270         
1271         /* only continue if there's an object, and a pose there too */
1272         if (ELEM(NULL, ob, ob->pose)) 
1273                 return OPERATOR_CANCELLED;
1274         pose= ob->pose;
1275         
1276         /* if there's no active group (or active is invalid), create a new menu to find it */
1277         if (pose->active_group <= 0) {
1278                 /* create a new menu, and start populating it with group names */
1279                 pup= uiPupMenuBegin(C, op->type->name, 0);
1280                 layout= uiPupMenuLayout(pup);
1281                 
1282                 /* special entry - allow to create new group, then use that 
1283                  *      (not to be used for removing though)
1284                  */
1285                 if (strstr(op->idname, "assign")) {
1286                         uiItemIntO(layout, "New Group", 0, op->idname, "type", 0);
1287                         uiItemS(layout);
1288                 }
1289                 
1290                 /* add entries for each group */
1291                 for (grp= pose->agroups.first, i=1; grp; grp=grp->next, i++)
1292                         uiItemIntO(layout, grp->name, 0, op->idname, "type", i);
1293                         
1294                 /* finish building the menu, and process it (should result in calling self again) */
1295                 uiPupMenuEnd(C, pup);
1296                 
1297                 return OPERATOR_CANCELLED;
1298         }
1299         else {
1300                 /* just use the active group index, and call the exec callback for the calling operator */
1301                 RNA_int_set(op->ptr, "type", pose->active_group);
1302                 return op->type->exec(C, op);
1303         }
1304 }
1305
1306 /* Assign selected pchans to the bone group that the user selects */
1307 static int pose_group_assign_exec (bContext *C, wmOperator *op)
1308 {
1309         ScrArea *sa= CTX_wm_area(C);
1310         Object *ob;
1311         bArmature *arm;
1312         bPose *pose;
1313         short done= 0;
1314         
1315         /* since this call may also be used from the buttons window, we need to check for where to get the object */
1316         if (sa->spacetype == SPACE_BUTS) 
1317                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1318         else
1319                 ob= CTX_data_active_object(C);
1320         
1321         /* only continue if there's an object, and a pose there too */
1322         if (ELEM(NULL, ob, ob->pose))
1323                 return OPERATOR_CANCELLED;
1324         arm= ob->data;
1325         pose= ob->pose;
1326         
1327         /* set the active group number to the one from operator props 
1328          *      - if 0 after this, make a new group...
1329          */
1330         pose->active_group= RNA_int_get(op->ptr, "type");
1331         if (pose->active_group == 0)
1332                 pose_add_group(ob);
1333         
1334         /* add selected bones to group then */
1335         // NOTE: unfortunately, we cannot use the context-iterators here, since they might not be defined...
1336         CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pchans) {
1337                 pchan->agrp_index= pose->active_group;
1338                 done= 1;
1339         }
1340         CTX_DATA_END;
1341
1342         /* notifiers for updates */
1343         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1344         
1345         /* report done status */
1346         if (done)
1347                 return OPERATOR_FINISHED;
1348         else
1349                 return OPERATOR_CANCELLED;
1350 }
1351
1352 void POSE_OT_group_assign (wmOperatorType *ot)
1353 {
1354         /* identifiers */
1355         ot->name= "Add Selected to Bone Group";
1356         ot->idname= "POSE_OT_group_assign";
1357         ot->description= "Add selected bones to the chosen bone group.";
1358         
1359         /* api callbacks */
1360         ot->invoke= pose_groups_menu_invoke;
1361         ot->exec= pose_group_assign_exec;
1362         ot->poll= ED_operator_posemode;
1363         
1364         /* flags */
1365         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1366         
1367         /* properties */
1368         RNA_def_int(ot->srna, "type", 0, 0, 10, "Bone Group Index", "", 0, INT_MAX);
1369 }
1370
1371
1372 static int pose_group_unassign_exec (bContext *C, wmOperator *op)
1373 {
1374         ScrArea *sa= CTX_wm_area(C);
1375         Object *ob;
1376         bArmature *arm;
1377         bPose *pose;
1378         bPoseChannel *pchan;
1379         short done= 0;
1380         
1381         /* since this call may also be used from the buttons window, we need to check for where to get the object */
1382         if (sa->spacetype == SPACE_BUTS) 
1383                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1384         else
1385                 ob= CTX_data_active_object(C);
1386         
1387         /* only continue if there's an object, and a pose there too */
1388         if (ELEM(NULL, ob, ob->pose))
1389                 return OPERATOR_CANCELLED;
1390         pose= ob->pose;
1391         arm= ob->data;
1392         
1393         /* add selected bones to ungroup then */
1394         // NOTE: unfortunately, we cannot use the context-iterators here, since they might not be defined...
1395         // CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pchans) 
1396         for (pchan= pose->chanbase.first; pchan; pchan= pchan->next) {
1397                 /* ensure that PoseChannel is on visible layer and is not hidden in PoseMode */
1398                 // NOTE: sync this view3d_context() in space_view3d.c
1399                 if ((pchan->bone) && (arm->layer & pchan->bone->layer) && !(pchan->bone->flag & BONE_HIDDEN_P)) {
1400                         if (pchan->bone->flag & (BONE_SELECTED|BONE_ACTIVE)) {
1401                                 if (pchan->agrp_index) {
1402                                         pchan->agrp_index= 0;
1403                                         done= 1;
1404                                 }
1405                         }
1406                 }
1407         }
1408         
1409         /* notifiers for updates */
1410         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1411         
1412         /* report done status */
1413         if (done)
1414                 return OPERATOR_FINISHED;
1415         else
1416                 return OPERATOR_CANCELLED;
1417 }
1418
1419 void POSE_OT_group_unassign (wmOperatorType *ot)
1420 {
1421         /* identifiers */
1422         ot->name= "Remove Selected from Bone Groups";
1423         ot->idname= "POSE_OT_group_unassign";
1424         ot->description= "Add selected bones from all bone groups";
1425         
1426         /* api callbacks */
1427         ot->exec= pose_group_unassign_exec;
1428         ot->poll= ED_operator_posemode;
1429         
1430         /* flags */
1431         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1432 }
1433
1434 static short pose_select_same_group (Object *ob)
1435 {
1436         bPose *pose= (ob)? ob->pose : NULL;
1437         bArmature *arm= (ob)? ob->data : NULL;
1438         bPoseChannel *pchan, *chan;
1439         short changed= 0;
1440         
1441         if (ELEM3(NULL, ob, pose, arm))
1442                 return 0;
1443         
1444         /* loop in loop... bad and slow! */
1445         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1446                 if (arm->layer & pchan->bone->layer) {
1447                         if (pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
1448                                 
1449                                 /* only if group matches (and is not selected or current bone) */
1450                                 for (chan= ob->pose->chanbase.first; chan; chan= chan->next) {
1451                                         if (arm->layer & chan->bone->layer) {
1452                                                 if (pchan->agrp_index == chan->agrp_index) {
1453                                                         chan->bone->flag |= BONE_SELECTED;
1454                                                         changed= 1;
1455                                                 }
1456                                         }
1457                                 }
1458                                 
1459                         }
1460                 }
1461         }
1462         
1463         return changed;
1464 }
1465
1466 static short pose_select_same_layer (Object *ob)
1467 {
1468         bPose *pose= (ob)? ob->pose : NULL;
1469         bArmature *arm= (ob)? ob->data : NULL;
1470         bPoseChannel *pchan;
1471         short layers= 0, changed= 0;
1472         
1473         if (ELEM3(NULL, ob, pose, arm))
1474                 return 0;
1475         
1476         /* figure out what bones are selected */
1477         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1478                 if (arm->layer & pchan->bone->layer) {
1479                         if (pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
1480                                 layers |= pchan->bone->layer;
1481                         }
1482                 }
1483         }
1484         if (layers == 0) 
1485                 return 0;
1486                 
1487         /* select bones that are on same layers as layers flag */
1488         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1489                 if (arm->layer & pchan->bone->layer) {
1490                         if (layers & pchan->bone->layer) {
1491                                 pchan->bone->flag |= BONE_SELECTED;
1492                                 changed= 1;
1493                         }
1494                 }
1495         }
1496         
1497         return changed;
1498 }
1499
1500 void pose_select_grouped (Scene *scene, short nr)
1501 {
1502         short changed = 0;
1503         
1504         if (nr == 1)            changed= pose_select_same_group(OBACT);
1505         else if (nr == 2)       changed= pose_select_same_layer(OBACT);
1506         
1507         if (changed) {
1508                 countall();
1509                 BIF_undo_push("Select Grouped");
1510         }
1511 }
1512
1513 /* Shift-G in 3D-View while in PoseMode */
1514 void pose_select_grouped_menu (Scene *scene)
1515 {
1516         short nr;
1517         
1518         /* here we go */
1519         nr= pupmenu("Select Grouped%t|In Same Group%x1|In Same Layer%x2");
1520         pose_select_grouped(scene, nr);
1521 }
1522
1523 /* ********************************************** */
1524
1525 static int pose_flip_names_exec (bContext *C, wmOperator *op)
1526 {
1527         Object *ob= CTX_data_active_object(C);
1528         bArmature *arm;
1529         char newname[32];
1530         
1531         /* paranoia checks */
1532         if (ELEM(NULL, ob, ob->pose)) 
1533                 return OPERATOR_CANCELLED;
1534         arm= ob->data;
1535         
1536         /* loop through selected bones, auto-naming them */
1537         CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pchans)
1538         {
1539                 BLI_strncpy(newname, pchan->name, sizeof(newname));
1540                 bone_flip_name(newname, 1);     // 1 = do strip off number extensions
1541                 ED_armature_bone_rename(arm, pchan->name, newname);
1542         }
1543         CTX_DATA_END;
1544         
1545         /* since we renamed stuff... */
1546         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
1547
1548         /* note, notifier might evolve */
1549         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1550         
1551         return OPERATOR_FINISHED;
1552 }
1553
1554 void POSE_OT_flip_names (wmOperatorType *ot)
1555 {
1556         /* identifiers */
1557         ot->name= "Flip Names";
1558         ot->idname= "POSE_OT_flip_names";
1559         ot->description= "Flips (and corrects) the names of selected bones.";
1560         
1561         /* api callbacks */
1562         ot->exec= pose_flip_names_exec;
1563         ot->poll= ED_operator_posemode;
1564         
1565         /* flags */
1566         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1567 }
1568
1569 /* ------------------ */
1570
1571 static int pose_autoside_names_exec (bContext *C, wmOperator *op)
1572 {
1573         Object *ob= CTX_data_active_object(C);
1574         bArmature *arm;
1575         char newname[32];
1576         short axis= RNA_enum_get(op->ptr, "axis");
1577         
1578         /* paranoia checks */
1579         if (ELEM(NULL, ob, ob->pose)) 
1580                 return OPERATOR_CANCELLED;
1581         arm= ob->data;
1582         
1583         /* loop through selected bones, auto-naming them */
1584         CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pchans)
1585         {
1586                 BLI_strncpy(newname, pchan->name, sizeof(newname));
1587                 bone_autoside_name(newname, 1, axis, pchan->bone->head[axis], pchan->bone->tail[axis]);
1588                 ED_armature_bone_rename(arm, pchan->name, newname);
1589         }
1590         CTX_DATA_END;
1591         
1592         /* since we renamed stuff... */
1593         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
1594
1595         /* note, notifier might evolve */
1596         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1597         
1598         return OPERATOR_FINISHED;
1599 }
1600
1601 void POSE_OT_autoside_names (wmOperatorType *ot)
1602 {
1603         static EnumPropertyItem axis_items[]= {
1604                 {0, "XAXIS", 0, "X-Axis", "Left/Right"},
1605                 {1, "YAXIS", 0, "Y-Axis", "Front/Back"},
1606                 {2, "ZAXIS", 0, "Z-Axis", "Top/Bottom"},
1607                 {0, NULL, 0, NULL, NULL}};
1608         
1609         /* identifiers */
1610         ot->name= "AutoName by Axis";
1611         ot->idname= "POSE_OT_autoside_names";
1612         ot->description= "Automatically renames the selected bones according to which side of the target axis they fall on.";
1613         
1614         /* api callbacks */
1615         ot->invoke= WM_menu_invoke;
1616         ot->exec= pose_autoside_names_exec;
1617         ot->poll= ED_operator_posemode;
1618         
1619         /* flags */
1620         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1621         
1622         /* settings */
1623         RNA_def_enum(ot->srna, "axis", axis_items, 0, "Axis", "Axis tag names with.");
1624 }
1625
1626 /* ********************************************** */
1627
1628 /* context active object, or weightpainted object with armature in posemode */
1629 void pose_activate_flipped_bone(Scene *scene)
1630 {
1631         Object *ob= OBACT;
1632         bArmature *arm= ob->data;
1633         
1634         if(ob==NULL) return;
1635
1636         if(ob->mode && OB_MODE_WEIGHT_PAINT) {
1637                 ob= modifiers_isDeformedByArmature(ob);
1638         }
1639         if(ob && (ob->mode & OB_MODE_POSE)) {
1640                 bPoseChannel *pchan, *pchanf;
1641                 
1642                 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1643                         if(arm->layer & pchan->bone->layer) {
1644                                 if(pchan->bone->flag & BONE_ACTIVE)
1645                                         break;
1646                         }
1647                 }
1648                 if(pchan) {
1649                         char name[32];
1650                         
1651                         BLI_strncpy(name, pchan->name, 32);
1652                         bone_flip_name(name, 1);        // 0 = do not strip off number extensions
1653                         
1654                         pchanf= get_pose_channel(ob->pose, name);
1655                         if(pchanf && pchanf!=pchan) {
1656                                 pchan->bone->flag &= ~(BONE_SELECTED|BONE_ACTIVE);
1657                                 pchanf->bone->flag |= (BONE_SELECTED|BONE_ACTIVE);
1658                         
1659                                 /* in weightpaint we select the associated vertex group too */
1660                                 if(ob->mode & OB_MODE_WEIGHT_PAINT) {
1661                                         ED_vgroup_select_by_name(OBACT, name);
1662                                         DAG_id_flush_update(&OBACT->id, OB_RECALC_DATA);
1663                                 }
1664                                 
1665                                 // XXX notifiers need to be sent to other editors to update
1666                                 
1667                         }                       
1668                 }
1669         }
1670 }
1671
1672
1673 /* ********************************************** */
1674
1675 /* Present a popup to get the layers that should be used */
1676 static int pose_armature_layers_invoke (bContext *C, wmOperator *op, wmEvent *evt)
1677 {
1678         Object *ob= CTX_data_active_object(C);
1679         bArmature *arm= (ob)? ob->data : NULL;
1680         PointerRNA ptr;
1681         int layers[16]; /* hardcoded for now - we can only have 16 armature layers, so this should be fine... */
1682         
1683         /* sanity checking */
1684         if (arm == NULL)
1685                 return OPERATOR_CANCELLED;
1686                 
1687         /* get RNA pointer to armature data to use that to retrieve the layers as ints to init the operator */
1688         RNA_id_pointer_create((ID *)arm, &ptr);
1689         RNA_boolean_get_array(&ptr, "layer", layers);
1690         RNA_boolean_set_array(op->ptr, "layers", layers);
1691         
1692         /* part to sync with other similar operators... */
1693         return WM_operator_props_popup(C, op, evt);
1694 }
1695
1696 /* Set the visible layers for the active armature (edit and pose modes) */
1697 static int pose_armature_layers_exec (bContext *C, wmOperator *op)
1698 {
1699         Object *ob= CTX_data_active_object(C);
1700         bArmature *arm= (ob)? ob->data : NULL;
1701         PointerRNA ptr;
1702         int layers[16]; /* hardcoded for now - we can only have 16 armature layers, so this should be fine... */
1703         
1704         /* get the values set in the operator properties */
1705         RNA_boolean_get_array(op->ptr, "layers", layers);
1706         
1707         /* get pointer for armature, and write data there... */
1708         RNA_id_pointer_create((ID *)arm, &ptr);
1709         RNA_boolean_set_array(&ptr, "layer", layers);
1710         
1711         /* note, notifier might evolve */
1712         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1713         
1714         return OPERATOR_FINISHED;
1715 }
1716
1717
1718 void POSE_OT_armature_layers (wmOperatorType *ot)
1719 {
1720         /* identifiers */
1721         ot->name= "Change Armature Layers";
1722         ot->idname= "POSE_OT_armature_layers";
1723         ot->description= "Change the visible armature layers.";
1724         
1725         /* callbacks */
1726         ot->invoke= pose_armature_layers_invoke;
1727         ot->exec= pose_armature_layers_exec;
1728         ot->poll= ED_operator_posemode;
1729         
1730         /* flags */
1731         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1732         
1733         /* properties */
1734         RNA_def_boolean_layer_member(ot->srna, "layers", 16, NULL, "Layer", "Armature layers to make visible");
1735 }
1736
1737 void ARMATURE_OT_armature_layers (wmOperatorType *ot)
1738 {
1739         /* identifiers */
1740         ot->name= "Change Armature Layers";
1741         ot->idname= "ARMATURE_OT_armature_layers";
1742         ot->description= "Change the visible armature layers.";
1743         
1744         /* callbacks */
1745         ot->invoke= pose_armature_layers_invoke;
1746         ot->exec= pose_armature_layers_exec;
1747         ot->poll= ED_operator_editarmature;
1748         
1749         /* flags */
1750         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1751         
1752         /* properties */
1753         RNA_def_boolean_layer_member(ot->srna, "layers", 16, NULL, "Layer", "Armature layers to make visible");
1754 }
1755
1756 /* ------------------- */
1757
1758 /* Present a popup to get the layers that should be used */
1759 static int pose_bone_layers_invoke (bContext *C, wmOperator *op, wmEvent *evt)
1760 {
1761         int layers[16]; /* hardcoded for now - we can only have 16 armature layers, so this should be fine... */
1762         
1763         /* get layers that are active already */
1764         memset(&layers, 0, sizeof(layers)); /* set all layers to be off by default */
1765         
1766         CTX_DATA_BEGIN(C, bPoseChannel *, pchan, selected_pchans) 
1767         {
1768                 short bit;
1769                 
1770                 /* loop over the bits for this pchan's layers, adding layers where they're needed */
1771                 for (bit= 0; bit < 16; bit++) {
1772                         if (pchan->bone->layer & (1<<bit))
1773                                 layers[bit]= 1;
1774                 }
1775         }
1776         CTX_DATA_END;
1777         
1778         /* copy layers to operator */
1779         RNA_boolean_set_array(op->ptr, "layers", layers);
1780         
1781                 /* part to sync with other similar operators... */
1782         return WM_operator_props_popup(C, op, evt);
1783 }
1784
1785 /* Set the visible layers for the active armature (edit and pose modes) */
1786 static int pose_bone_layers_exec (bContext *C, wmOperator *op)
1787 {
1788         Object *ob= CTX_data_active_object(C);
1789         bArmature *arm= (ob)? ob->data : NULL;
1790         PointerRNA ptr;
1791         int layers[16]; /* hardcoded for now - we can only have 16 armature layers, so this should be fine... */
1792         
1793         /* get the values set in the operator properties */
1794         RNA_boolean_get_array(op->ptr, "layers", layers);
1795         
1796         /* set layers of pchans based on the values set in the operator props */
1797         CTX_DATA_BEGIN(C, bPoseChannel *, pchan, selected_pchans) 
1798         {
1799                 /* get pointer for pchan, and write flags this way */
1800                 RNA_pointer_create((ID *)arm, &RNA_Bone, pchan->bone, &ptr);
1801                 RNA_boolean_set_array(&ptr, "layer", layers);
1802         }
1803         CTX_DATA_END;
1804         
1805         /* note, notifier might evolve */
1806         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1807         
1808         return OPERATOR_FINISHED;
1809 }
1810
1811 void POSE_OT_bone_layers (wmOperatorType *ot)
1812 {
1813         /* identifiers */
1814         ot->name= "Change Bone Layers";
1815         ot->idname= "POSE_OT_bone_layers";
1816         ot->description= "Change the layers that the selected bones belong to.";
1817         
1818         /* callbacks */
1819         ot->invoke= pose_bone_layers_invoke;
1820         ot->exec= pose_bone_layers_exec;
1821         ot->poll= ED_operator_posemode;
1822         
1823         /* flags */
1824         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1825         
1826         /* properties */
1827         RNA_def_boolean_layer_member(ot->srna, "layers", 16, NULL, "Layer", "Armature layers that bone belongs to");
1828 }
1829
1830 /* ------------------- */
1831
1832 /* Present a popup to get the layers that should be used */
1833 static int armature_bone_layers_invoke (bContext *C, wmOperator *op, wmEvent *evt)
1834 {
1835         int layers[16]; /* hardcoded for now - we can only have 16 armature layers, so this should be fine... */
1836         
1837         /* get layers that are active already */
1838         memset(&layers, 0, sizeof(layers)); /* set all layers to be off by default */
1839         
1840         CTX_DATA_BEGIN(C, EditBone *, ebone, selected_editable_bones) 
1841         {
1842                 short bit;
1843                 
1844                 /* loop over the bits for this pchan's layers, adding layers where they're needed */
1845                 for (bit= 0; bit < 16; bit++) {
1846                         if (ebone->layer & (1<<bit))
1847                                 layers[bit]= 1;
1848                 }
1849         }
1850         CTX_DATA_END;
1851         
1852         /* copy layers to operator */
1853         RNA_boolean_set_array(op->ptr, "layers", layers);
1854         
1855                 /* part to sync with other similar operators... */
1856         return WM_operator_props_popup(C, op, evt);
1857 }
1858
1859 /* Set the visible layers for the active armature (edit and pose modes) */
1860 static int armature_bone_layers_exec (bContext *C, wmOperator *op)
1861 {
1862         Object *ob= CTX_data_edit_object(C);
1863         bArmature *arm= (ob)? ob->data : NULL;
1864         PointerRNA ptr;
1865         int layers[16]; /* hardcoded for now - we can only have 16 armature layers, so this should be fine... */
1866         
1867         /* get the values set in the operator properties */
1868         RNA_boolean_get_array(op->ptr, "layers", layers);
1869         
1870         /* set layers of pchans based on the values set in the operator props */
1871         CTX_DATA_BEGIN(C, EditBone *, ebone, selected_editable_bones) 
1872         {
1873                 /* get pointer for pchan, and write flags this way */
1874                 RNA_pointer_create((ID *)arm, &RNA_EditBone, ebone, &ptr);
1875                 RNA_boolean_set_array(&ptr, "layer", layers);
1876         }
1877         CTX_DATA_END;
1878         
1879         /* note, notifier might evolve */
1880         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1881         
1882         return OPERATOR_FINISHED;
1883 }
1884
1885 void ARMATURE_OT_bone_layers (wmOperatorType *ot)
1886 {
1887         /* identifiers */
1888         ot->name= "Change Bone Layers";
1889         ot->idname= "ARMATURE_OT_bone_layers";
1890         ot->description= "Change the layers that the selected bones belong to.";
1891         
1892         /* callbacks */
1893         ot->invoke= armature_bone_layers_invoke;
1894         ot->exec= armature_bone_layers_exec;
1895         ot->poll= ED_operator_editarmature;
1896         
1897         /* flags */
1898         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1899         
1900         /* properties */
1901         RNA_def_boolean_layer_member(ot->srna, "layers", 16, NULL, "Layer", "Armature layers that bone belongs to");
1902 }
1903
1904 /* ********************************************** */
1905
1906 /* for use in insertkey, ensure rotation goes other way around */
1907 void pose_flipquats(Scene *scene)
1908 {
1909         Object *ob = OBACT;
1910         bArmature *arm= ob->data;
1911         bPoseChannel *pchan;
1912         
1913         if(ob->pose==NULL)
1914                 return;
1915         
1916         /* find sel bones */
1917         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1918                 if(pchan->bone && (pchan->bone->flag & BONE_SELECTED) && (pchan->bone->layer & arm->layer)) {
1919                         /* quaternions have 720 degree range */
1920                         pchan->quat[0]= -pchan->quat[0];
1921                         pchan->quat[1]= -pchan->quat[1];
1922                         pchan->quat[2]= -pchan->quat[2];
1923                         pchan->quat[3]= -pchan->quat[3];
1924                 }
1925         }
1926         
1927         /* do autokey */
1928         autokeyframe_pose_cb_func(ob, TFM_ROTATION, 0);
1929 }
1930
1931 /* context: active channel */
1932 void pose_special_editmenu(Scene *scene)
1933 {
1934 #if 0
1935         Object *obedit= scene->obedit; // XXX context
1936         Object *ob= OBACT;
1937         short nr;
1938         
1939         /* paranoia checks */
1940         if(!ob && !ob->pose) return;
1941         if(ob==obedit || (ob->mode & OB_MODE_POSE)==0) return;
1942         
1943         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");
1944         if(nr==1) {
1945                 pose_select_constraint_target(scene);
1946         }
1947         else if(nr==2) {
1948                 pose_flip_names();
1949         }
1950         else if(nr==3) {
1951                 pose_calculate_path(C, ob);
1952         }
1953         else if(nr==4) {
1954                 pose_clear_paths(ob);
1955         }
1956         else if(nr==5) {
1957                 pose_clear_user_transforms(scene, ob);
1958         }
1959         else if(nr==6) {
1960                 pose_relax();
1961         }
1962         else if(ELEM3(nr, 7, 8, 9)) {
1963                 pose_autoside_names(nr-7);
1964         }
1965 #endif
1966 }
1967
1968 /* Restore selected pose-bones to 'action'-defined pose */
1969 void pose_clear_user_transforms(Scene *scene, Object *ob)
1970 {
1971         bArmature *arm= ob->data;
1972         bPoseChannel *pchan;
1973         
1974         if (ob->pose == NULL)
1975                 return;
1976         
1977         /* if the object has an action, restore pose to the pose defined by the action by clearing pose on selected bones */
1978         if (ob->action) {
1979                 /* find selected bones */
1980                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1981                         if (pchan->bone && (pchan->bone->flag & BONE_SELECTED) && (pchan->bone->layer & arm->layer)) {
1982                                 /* just clear the BONE_UNKEYED flag, allowing this bone to get overwritten by actions again */
1983                                 pchan->bone->flag &= ~BONE_UNKEYED;
1984                         }
1985                 }
1986                 
1987                 /* clear pose locking flag 
1988                  *      - this will only clear the user-defined pose in the selected bones, where BONE_UNKEYED has been cleared
1989                  */
1990                 ob->pose->flag |= POSE_DO_UNLOCK;
1991         }
1992         else {
1993                 /* no action, so restore entire pose to rest pose (cannot restore only selected bones) */
1994                 rest_pose(ob->pose);
1995         }
1996         
1997         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
1998         BIF_undo_push("Clear User Transform");
1999 }
2000