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