7d7f54309a8bcf15e741d44803f08fcce97c8344
[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)
547                                                                         pchanc->bone->flag |= BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL;
548                                                         }
549                                                 }
550                                                 
551                                                 if (cti->flush_constraint_targets)
552                                                         cti->flush_constraint_targets(con, &targets, 1);
553                                         }
554                                 }
555                         }
556                 }
557         }
558         
559         BIF_undo_push("Select constraint target");
560
561 }
562
563 static int pose_select_constraint_target_exec(bContext *C, wmOperator *op)
564 {
565         Object *ob= CTX_data_active_object(C);
566         bArmature *arm= ob->data;
567         bPoseChannel *pchan;
568         bConstraint *con;
569         int found= 0;
570         
571         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
572                 if (arm->layer & pchan->bone->layer) {
573                         if (pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
574                                 for (con= pchan->constraints.first; con; con= con->next) {
575                                         bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
576                                         ListBase targets = {NULL, NULL};
577                                         bConstraintTarget *ct;
578                                         
579                                         if (cti && cti->get_constraint_targets) {
580                                                 cti->get_constraint_targets(con, &targets);
581                                                 
582                                                 for (ct= targets.first; ct; ct= ct->next) {
583                                                         if ((ct->tar == ob) && (ct->subtarget[0])) {
584                                                                 bPoseChannel *pchanc= get_pose_channel(ob->pose, ct->subtarget);
585                                                                 if(pchanc) {
586                                                                         pchanc->bone->flag |= BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL;
587                                                                         found= 1;
588                                                                 }
589                                                         }
590                                                 }
591                                                 
592                                                 if (cti->flush_constraint_targets)
593                                                         cti->flush_constraint_targets(con, &targets, 1);
594                                         }
595                                 }
596                         }
597                 }
598         }
599
600         if(!found)
601                 return OPERATOR_CANCELLED;
602
603         WM_event_add_notifier(C, NC_OBJECT|ND_BONE_SELECT, ob);
604
605         return OPERATOR_FINISHED;
606 }
607
608 void POSE_OT_select_constraint_target(wmOperatorType *ot)
609 {
610         /* identifiers */
611         ot->name= "Select Constraint Target";
612         ot->idname= "POSE_OT_select_constraint_target";
613         
614         /* api callbacks */
615         ot->exec= pose_select_constraint_target_exec;
616         ot->poll= ED_operator_posemode;
617         
618         /* flags */
619         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
620 }
621
622 /* ******************* select hierarchy operator ************* */
623
624 static int pose_select_hierarchy_exec(bContext *C, wmOperator *op)
625 {
626         Object *ob= CTX_data_active_object(C);
627         bArmature *arm= ob->data;
628         bPoseChannel *pchan;
629         Bone *curbone, *pabone, *chbone;
630         int direction = RNA_enum_get(op->ptr, "direction");
631         int add_to_sel = RNA_boolean_get(op->ptr, "extend");
632         int found= 0;
633         
634         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
635                 curbone= pchan->bone;
636                 
637                 if (arm->layer & curbone->layer) {
638                         if (curbone->flag & (BONE_ACTIVE)) {
639                                 if (direction == BONE_SELECT_PARENT) {
640                                 
641                                         if (pchan->parent == NULL) continue;
642                                         else pabone= pchan->parent->bone;
643                                         
644                                         if ((arm->layer & pabone->layer) && !(pabone->flag & BONE_HIDDEN_P)) {
645                                                 
646                                                 if (!add_to_sel) curbone->flag &= ~BONE_SELECTED;
647                                                 curbone->flag &= ~BONE_ACTIVE;
648                                                 pabone->flag |= (BONE_ACTIVE|BONE_SELECTED);
649
650                                                 found= 1;
651                                                 break;
652                                         }
653                                 } else { // BONE_SELECT_CHILD
654                                 
655                                         if (pchan->child == NULL) continue;
656                                         else chbone = pchan->child->bone;
657                                         
658                                         if ((arm->layer & chbone->layer) && !(chbone->flag & BONE_HIDDEN_P)) {
659                                         
660                                                 if (!add_to_sel) curbone->flag &= ~BONE_SELECTED;
661                                                 curbone->flag &= ~BONE_ACTIVE;
662                                                 chbone->flag |= (BONE_ACTIVE|BONE_SELECTED);
663
664                                                 found= 1;
665                                                 break;
666                                         }
667                                 }
668                         }
669                 }
670         }
671
672         if(!found)
673                 return OPERATOR_CANCELLED;
674
675         WM_event_add_notifier(C, NC_OBJECT|ND_BONE_SELECT, ob);
676
677         return OPERATOR_FINISHED;
678 }
679
680 void POSE_OT_select_hierarchy(wmOperatorType *ot)
681 {
682         static EnumPropertyItem direction_items[]= {
683         {BONE_SELECT_PARENT, "PARENT", 0, "Select Parent", ""},
684         {BONE_SELECT_CHILD, "CHILD", 0, "Select Child", ""},
685         {0, NULL, 0, NULL, NULL}
686         };
687         
688         /* identifiers */
689         ot->name= "Select Hierarchy";
690         ot->idname= "POSE_OT_select_hierarchy";
691         
692         /* api callbacks */
693         ot->exec= pose_select_hierarchy_exec;
694         ot->poll= ED_operator_posemode;
695         
696         /* flags */
697         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
698         
699         /* props */
700         RNA_def_enum(ot->srna, "direction", direction_items,
701                                  BONE_SELECT_PARENT, "Direction", "");
702         RNA_def_boolean(ot->srna, "extend", 0, "Add to Selection", "");
703         
704 }
705
706
707 void pose_copy_menu(Scene *scene)
708 {
709         Object *obedit= scene->obedit; // XXX context
710         Object *ob= OBACT;
711         bArmature *arm= ob->data;
712         bPoseChannel *pchan, *pchanact;
713         short nr=0;
714         int i=0;
715         
716         /* paranoia checks */
717         if (ELEM(NULL, ob, ob->pose)) return;
718         if ((ob==obedit) || (ob->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                                         }
802                                                 break;
803                                         case 8: /* Custom Bone Shape */
804                                                 pchan->custom = pchanact->custom;
805                                                 break;
806                                         case 9: /* Visual Location */
807                                                 armature_loc_pose_to_bone(pchan, pchanact->pose_mat[3], pchan->loc);
808                                                 break;
809                                         case 10: /* Visual Rotation */
810                                         {
811                                                 float delta_mat[4][4];
812                                                 
813                                                 armature_mat_pose_to_bone(pchan, pchanact->pose_mat, delta_mat);
814                                                 
815                                                 if (pchan->rotmode == PCHAN_ROT_AXISANGLE) {
816                                                         float tmp_quat[4];
817                                                         
818                                                         /* need to convert to quat first (in temp var)... */
819                                                         Mat4ToQuat(delta_mat, tmp_quat);
820                                                         QuatToAxisAngle(tmp_quat, &pchan->quat[1], &pchan->quat[0]);
821                                                 }
822                                                 else if (pchan->rotmode == PCHAN_ROT_QUAT)
823                                                         Mat4ToQuat(delta_mat, pchan->quat);
824                                                 else
825                                                         Mat4ToEulO(delta_mat, pchan->eul, pchan->rotmode);
826                                         }
827                                                 break;
828                                         case 11: /* Visual Size */
829                                         {
830                                                 float delta_mat[4][4], size[4];
831                                                 
832                                                 armature_mat_pose_to_bone(pchan, pchanact->pose_mat, delta_mat);
833                                                 Mat4ToSize(delta_mat, size);
834                                                 VECCOPY(pchan->size, size);
835                                         }
836                                 }
837                         }
838                 }
839         } 
840         else { /* constraints, optional (note: max we can have is 24 constraints) */
841                 bConstraint *con, *con_back;
842                 int const_toggle[24];
843                 ListBase const_copy = {NULL, NULL};
844                 
845                 BLI_duplicatelist(&const_copy, &(pchanact->constraints));
846                 
847                 /* build the puplist of constraints */
848                 for (con = pchanact->constraints.first, i=0; con; con=con->next, i++){
849                         const_toggle[i]= 1;
850 //                      add_numbut(i, TOG|INT, con->name, 0, 0, &(const_toggle[i]), "");
851                 }
852                 
853 //              if (!do_clever_numbuts("Select Constraints", i, REDRAW)) {
854 //                      BLI_freelistN(&const_copy);
855 //                      return;
856 //              }
857                 
858                 /* now build a new listbase from the options selected */
859                 for (i=0, con=const_copy.first; con; i++) {
860                         /* if not selected, free/remove it from the list */
861                         if (!const_toggle[i]) {
862                                 con_back= con->next;
863                                 BLI_freelinkN(&const_copy, con);
864                                 con= con_back;
865                         } 
866                         else
867                                 con= con->next;
868                 }
869                 
870                 /* Copy the temo listbase to the selected posebones */
871                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
872                         if ( (arm->layer & pchan->bone->layer) &&
873                                  (pchan->bone->flag & BONE_SELECTED) &&
874                                  (pchan!=pchanact) ) 
875                         {
876                                 ListBase tmp_constraints = {NULL, NULL};
877                                 
878                                 /* copy constraints to tmpbase and apply 'local' tags before 
879                                  * appending to list of constraints for this channel
880                                  */
881                                 copy_constraints(&tmp_constraints, &const_copy);
882                                 if ((ob->proxy) && (pchan->bone->layer & arm->layer_protected)) {
883                                         bConstraint *con;
884                                         
885                                         /* add proxy-local tags */
886                                         for (con= tmp_constraints.first; con; con= con->next)
887                                                 con->flag |= CONSTRAINT_PROXY_LOCAL;
888                                 }
889                                 addlisttolist(&pchan->constraints, &tmp_constraints);
890                                 
891                                 /* update flags (need to add here, not just copy) */
892                                 pchan->constflag |= pchanact->constflag;
893                         }
894                 }
895                 BLI_freelistN(&const_copy);
896                 update_pose_constraint_flags(ob->pose); /* we could work out the flags but its simpler to do this */
897                 
898                 if (ob->pose)
899                         ob->pose->flag |= POSE_RECALC;
900         }
901         
902         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);   // and all its relations
903         
904         BIF_undo_push("Copy Pose Attributes");
905         
906 }
907
908 /* ******************** copy/paste pose ********************** */
909
910 /* Global copy/paste buffer for pose - cleared on start/end session + before every copy operation */
911 static bPose *g_posebuf = NULL;
912
913 void free_posebuf(void) 
914 {
915         if (g_posebuf) {
916                 /* was copied without constraints */
917                 BLI_freelistN(&g_posebuf->chanbase);
918                 MEM_freeN(g_posebuf);
919         }
920         
921         g_posebuf=NULL;
922 }
923
924 /* ---- */
925
926 static int pose_copy_exec (bContext *C, wmOperator *op)
927 {
928         Object *ob= CTX_data_active_object(C);
929         
930         /* sanity checking */
931         if ELEM(NULL, ob, ob->pose) {
932                 BKE_report(op->reports, RPT_ERROR, "No Pose to Copy");
933                 return OPERATOR_CANCELLED;
934         }
935
936         /* free existing pose buffer */
937         free_posebuf();
938         
939         /* sets chan->flag to POSE_KEY if bone selected, then copy those bones to the buffer */
940         set_pose_keys(ob);  
941         copy_pose(&g_posebuf, ob->pose, 0);
942         
943         
944         return OPERATOR_FINISHED;
945 }
946
947 void POSE_OT_copy (wmOperatorType *ot) 
948 {
949         /* identifiers */
950         ot->name= "Copy Pose";
951         ot->idname= "POSE_OT_copy";
952         ot->description= "Copies the current pose of the selected bones to copy/paste buffer.";
953         
954         /* api callbacks */
955         ot->exec= pose_copy_exec;
956         ot->poll= ED_operator_posemode;
957         
958         /* flag */
959         ot->flag= OPTYPE_REGISTER;
960 }
961
962 /* ---- */
963
964 static int pose_paste_exec (bContext *C, wmOperator *op)
965 {
966         Scene *scene= CTX_data_scene(C);
967         Object *ob= CTX_data_active_object(C);
968         bPoseChannel *chan, *pchan;
969         char name[32];
970         int flip= RNA_boolean_get(op->ptr, "flipped");
971         
972         /* sanity checks */
973         if ELEM(NULL, ob, ob->pose)
974                 return OPERATOR_CANCELLED;
975
976         if (g_posebuf == NULL) {
977                 BKE_report(op->reports, RPT_ERROR, "Copy buffer is empty");
978                 return OPERATOR_CANCELLED;
979         }
980         
981         /* Safely merge all of the channels in the buffer pose into any existing pose */
982         for (chan= g_posebuf->chanbase.first; chan; chan=chan->next) {
983                 if (chan->flag & POSE_KEY) {
984                         /* get the name - if flipping, we must flip this first */
985                         BLI_strncpy(name, chan->name, sizeof(name));
986                         if (flip)
987                                 bone_flip_name(name, 0);                /* 0 = don't strip off number extensions */
988                                 
989                         /* only copy when channel exists, poses are not meant to add random channels to anymore */
990                         pchan= get_pose_channel(ob->pose, name);
991                         
992                         if (pchan) {
993                                 /* only loc rot size 
994                                  *      - only copies transform info for the pose 
995                                  */
996                                 VECCOPY(pchan->loc, chan->loc);
997                                 VECCOPY(pchan->size, chan->size);
998                                 pchan->flag= chan->flag;
999                                 
1000                                 /* check if rotation modes are compatible (i.e. do they need any conversions) */
1001                                 if (pchan->rotmode == chan->rotmode) {
1002                                         /* copy the type of rotation in use */
1003                                         if (pchan->rotmode > 0) {
1004                                                 VECCOPY(pchan->eul, chan->eul);
1005                                         }
1006                                         else {
1007                                                 QUATCOPY(pchan->quat, chan->quat);
1008                                         }
1009                                 }
1010                                 else if (pchan->rotmode > 0) {
1011                                         /* quat/axis-angle to euler */
1012                                         if (chan->rotmode == PCHAN_ROT_AXISANGLE)
1013                                                 AxisAngleToEulO(&chan->quat[1], chan->quat[0], pchan->eul, pchan->rotmode);
1014                                         else
1015                                                 QuatToEulO(chan->quat, pchan->eul, pchan->rotmode);
1016                                 }
1017                                 else if (pchan->rotmode == PCHAN_ROT_AXISANGLE) {
1018                                         /* quat/euler to axis angle */
1019                                         if (chan->rotmode > 0)
1020                                                 EulOToAxisAngle(chan->eul, chan->rotmode, &pchan->quat[1], &pchan->quat[0]);
1021                                         else    
1022                                                 QuatToAxisAngle(chan->quat, &pchan->quat[1], &pchan->quat[0]);
1023                                 }
1024                                 else {
1025                                         /* euler/axis-angle to quat */
1026                                         if (chan->rotmode > 0)
1027                                                 EulOToQuat(chan->eul, chan->rotmode, pchan->quat);
1028                                         else
1029                                                 AxisAngleToQuat(pchan->quat, &chan->quat[1], chan->quat[0]);
1030                                 }
1031                                 
1032                                 /* paste flipped pose? */
1033                                 if (flip) {
1034                                         pchan->loc[0]*= -1;
1035                                         
1036                                         /* has to be done as eulers... */
1037                                         if (pchan->rotmode > 0) {
1038                                                 pchan->eul[1] *= -1;
1039                                                 pchan->eul[2] *= -1;
1040                                         }
1041                                         else if (pchan->rotmode == PCHAN_ROT_AXISANGLE) {
1042                                                 float eul[3];
1043                                                 
1044                                                 AxisAngleToEulO(&pchan->quat[1], pchan->quat[0], eul, EULER_ORDER_DEFAULT);
1045                                                 eul[1]*= -1;
1046                                                 eul[2]*= -1;
1047                                                 EulOToAxisAngle(eul, EULER_ORDER_DEFAULT, &pchan->quat[1], &pchan->quat[0]);
1048                                         }
1049                                         else {
1050                                                 float eul[3];
1051                                                 
1052                                                 QuatToEul(pchan->quat, eul);
1053                                                 eul[1]*= -1;
1054                                                 eul[2]*= -1;
1055                                                 EulToQuat(eul, pchan->quat);
1056                                         }
1057                                 }
1058                                 
1059 #if 0 // XXX old animation system
1060                                 if (autokeyframe_cfra_can_key(ob)) {
1061                                         ID *id= &ob->id;
1062                                         
1063                                         /* Set keys on pose */
1064                                         // TODO: make these use keyingsets....
1065                                         if (chan->flag & POSE_ROT) {
1066                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_X, 0);
1067                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_Y, 0);
1068                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_Z, 0);
1069                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_W, 0);
1070                                         }
1071                                         if (chan->flag & POSE_SIZE) {
1072                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_SIZE_X, 0);
1073                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_SIZE_Y, 0);
1074                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_SIZE_Z, 0);
1075                                         }
1076                                         if (chan->flag & POSE_LOC) {
1077                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_LOC_X, 0);
1078                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_LOC_Y, 0);
1079                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_LOC_Z, 0);
1080                                         }
1081                                         
1082                                         /* clear any unkeyed tags */
1083                                         if (chan->bone)
1084                                                 chan->bone->flag &= ~BONE_UNKEYED;
1085                                 }
1086                                 else {
1087                                         /* add unkeyed tags */
1088                                         if (chan->bone)
1089                                                 chan->bone->flag |= BONE_UNKEYED;
1090                                 }
1091 #endif // XXX old animation system
1092                         }
1093                 }
1094         }
1095
1096         /* Update event for pose and deformation children */
1097         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
1098         
1099         if (IS_AUTOKEY_ON(scene)) {
1100 // XXX          remake_action_ipos(ob->action);
1101         }
1102         else {
1103                 /* need to trick depgraph, action is not allowed to execute on pose */
1104                 where_is_pose(scene, ob);
1105                 ob->recalc= 0;
1106         }
1107         
1108         /* notifiers for updates */
1109         WM_event_add_notifier(C, NC_OBJECT|ND_POSE|ND_TRANSFORM, ob);
1110
1111         return OPERATOR_FINISHED;
1112 }
1113
1114 void POSE_OT_paste (wmOperatorType *ot)
1115 {
1116         /* identifiers */
1117         ot->name= "Paste Pose";
1118         ot->idname= "POSE_OT_paste";
1119         ot->description= "Pastes the stored pose on to the current pose.";
1120         
1121         /* api callbacks */
1122         ot->exec= pose_paste_exec;
1123         ot->poll= ED_operator_posemode;
1124         
1125         /* flag */
1126         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1127         
1128         /* properties */
1129         RNA_def_boolean(ot->srna, "flipped", 0, "Flipped on X-Axis", "");
1130 }
1131
1132 /* ********************************************** */
1133
1134 /* context weightpaint and deformer in posemode */
1135 void pose_adds_vgroups(Scene *scene, Object *meshobj, int heatweights)
1136 {
1137 // XXX  extern VPaint Gwp;         /* from vpaint */
1138         Object *poseobj= modifiers_isDeformedByArmature(meshobj);
1139
1140         if(poseobj==NULL || (poseobj->mode & OB_MODE_POSE)==0) {
1141                 error("The active object must have a deforming armature in pose mode");
1142                 return;
1143         }
1144
1145 // XXX  add_verts_to_dgroups(meshobj, poseobj, heatweights, (Gwp.flag & VP_MIRROR_X));
1146
1147         if(heatweights)
1148                 BIF_undo_push("Apply Bone Heat Weights to Vertex Groups");
1149         else
1150                 BIF_undo_push("Apply Bone Envelopes to Vertex Groups");
1151
1152         
1153         // and all its relations
1154         DAG_id_flush_update(&meshobj->id, OB_RECALC_DATA);
1155 }
1156
1157 /* ********************************************** */
1158
1159
1160 static int pose_group_add_exec (bContext *C, wmOperator *op)
1161 {
1162         ScrArea *sa= CTX_wm_area(C);
1163         Object *ob;
1164         
1165         /* since this call may also be used from the buttons window, we need to check for where to get the object */
1166         if (sa->spacetype == SPACE_BUTS) 
1167                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1168         else
1169                 ob= CTX_data_active_object(C);
1170                 
1171         /* only continue if there's an object */
1172         if (ob == NULL)
1173                 return OPERATOR_CANCELLED;
1174         
1175         /* for now, just call the API function for this */
1176         pose_add_group(ob);
1177         
1178         /* notifiers for updates */
1179         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1180         
1181         return OPERATOR_FINISHED;
1182 }
1183
1184 void POSE_OT_group_add (wmOperatorType *ot)
1185 {
1186         /* identifiers */
1187         ot->name= "Add Bone Group";
1188         ot->idname= "POSE_OT_group_add";
1189         ot->description= "Add a new bone group.";
1190         
1191         /* api callbacks */
1192         ot->exec= pose_group_add_exec;
1193         ot->poll= ED_operator_posemode;
1194         
1195         /* flags */
1196         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1197 }
1198
1199
1200 static int pose_group_remove_exec (bContext *C, wmOperator *op)
1201 {
1202         ScrArea *sa= CTX_wm_area(C);
1203         Object *ob;
1204         
1205         /* since this call may also be used from the buttons window, we need to check for where to get the object */
1206         if (sa->spacetype == SPACE_BUTS) 
1207                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1208         else
1209                 ob= CTX_data_active_object(C);
1210         
1211         /* only continue if there's an object */
1212         if (ob == NULL)
1213                 return OPERATOR_CANCELLED;
1214         
1215         /* for now, just call the API function for this */
1216         pose_remove_group(ob);
1217         
1218         /* notifiers for updates */
1219         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1220         
1221         return OPERATOR_FINISHED;
1222 }
1223
1224 void POSE_OT_group_remove (wmOperatorType *ot)
1225 {
1226         /* identifiers */
1227         ot->name= "Remove Bone Group";
1228         ot->idname= "POSE_OT_group_remove";
1229         ot->description= "Removes the active bone group.";
1230         
1231         /* api callbacks */
1232         ot->exec= pose_group_remove_exec;
1233         ot->poll= ED_operator_posemode;
1234         
1235         /* flags */
1236         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1237 }
1238
1239 /* ------------ */
1240
1241 /* invoke callback which presents a list of bone-groups for the user to choose from */
1242 static int pose_groups_menu_invoke (bContext *C, wmOperator *op, wmEvent *evt)
1243 {
1244         ScrArea *sa= CTX_wm_area(C);
1245         Object *ob;
1246         bPose *pose;
1247         
1248         uiPopupMenu *pup;
1249         uiLayout *layout;
1250         bActionGroup *grp;
1251         int i;
1252         
1253         /* since this call may also be used from the buttons window, we need to check for where to get the object */
1254         if (sa->spacetype == SPACE_BUTS) 
1255                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1256         else
1257                 ob= CTX_data_active_object(C);
1258         
1259         /* only continue if there's an object, and a pose there too */
1260         if (ELEM(NULL, ob, ob->pose)) 
1261                 return OPERATOR_CANCELLED;
1262         pose= ob->pose;
1263         
1264         /* if there's no active group (or active is invalid), create a new menu to find it */
1265         if (pose->active_group <= 0) {
1266                 /* create a new menu, and start populating it with group names */
1267                 pup= uiPupMenuBegin(C, op->type->name, 0);
1268                 layout= uiPupMenuLayout(pup);
1269                 
1270                 /* special entry - allow to create new group, then use that 
1271                  *      (not to be used for removing though)
1272                  */
1273                 if (strstr(op->idname, "assign")) {
1274                         uiItemIntO(layout, "New Group", 0, op->idname, "type", 0);
1275                         uiItemS(layout);
1276                 }
1277                 
1278                 /* add entries for each group */
1279                 for (grp= pose->agroups.first, i=1; grp; grp=grp->next, i++)
1280                         uiItemIntO(layout, grp->name, 0, op->idname, "type", i);
1281                         
1282                 /* finish building the menu, and process it (should result in calling self again) */
1283                 uiPupMenuEnd(C, pup);
1284                 
1285                 return OPERATOR_CANCELLED;
1286         }
1287         else {
1288                 /* just use the active group index, and call the exec callback for the calling operator */
1289                 RNA_int_set(op->ptr, "type", pose->active_group);
1290                 return op->type->exec(C, op);
1291         }
1292 }
1293
1294 /* Assign selected pchans to the bone group that the user selects */
1295 static int pose_group_assign_exec (bContext *C, wmOperator *op)
1296 {
1297         ScrArea *sa= CTX_wm_area(C);
1298         Object *ob;
1299         bArmature *arm;
1300         bPose *pose;
1301         bPoseChannel *pchan;
1302         short done= 0;
1303         
1304         /* since this call may also be used from the buttons window, we need to check for where to get the object */
1305         if (sa->spacetype == SPACE_BUTS) 
1306                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1307         else
1308                 ob= CTX_data_active_object(C);
1309         
1310         /* only continue if there's an object, and a pose there too */
1311         if (ELEM(NULL, ob, ob->pose))
1312                 return OPERATOR_CANCELLED;
1313         arm= ob->data;
1314         pose= ob->pose;
1315         
1316         /* set the active group number to the one from operator props 
1317          *      - if 0 after this, make a new group...
1318          */
1319         pose->active_group= RNA_int_get(op->ptr, "type");
1320         if (pose->active_group == 0)
1321                 pose_add_group(ob);
1322         
1323         /* add selected bones to group then */
1324         // NOTE: unfortunately, we cannot use the context-iterators here, since they might not be defined...
1325         // CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pchans) 
1326         for (pchan= pose->chanbase.first; pchan; pchan= pchan->next) {
1327                 /* ensure that PoseChannel is on visible layer and is not hidden in PoseMode */
1328                 // NOTE: sync this view3d_context() in space_view3d.c
1329                 if ((pchan->bone) && (arm->layer & pchan->bone->layer) && !(pchan->bone->flag & BONE_HIDDEN_P)) {
1330                         if (pchan->bone->flag & (BONE_SELECTED|BONE_ACTIVE)) {
1331                                 pchan->agrp_index= pose->active_group;
1332                                 done= 1;
1333                         }
1334                 }
1335         }
1336         
1337         /* notifiers for updates */
1338         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1339         
1340         /* report done status */
1341         if (done)
1342                 return OPERATOR_FINISHED;
1343         else
1344                 return OPERATOR_CANCELLED;
1345 }
1346
1347 void POSE_OT_group_assign (wmOperatorType *ot)
1348 {
1349         /* identifiers */
1350         ot->name= "Add Selected to Bone Group";
1351         ot->idname= "POSE_OT_group_assign";
1352         ot->description= "Add selected bones to the chosen bone group.";
1353         
1354         /* api callbacks */
1355         ot->invoke= pose_groups_menu_invoke;
1356         ot->exec= pose_group_assign_exec;
1357         ot->poll= ED_operator_posemode;
1358         
1359         /* flags */
1360         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1361         
1362         /* properties */
1363         RNA_def_int(ot->srna, "type", 0, 0, 10, "Bone Group Index", "", 0, INT_MAX);
1364 }
1365
1366
1367 static int pose_group_unassign_exec (bContext *C, wmOperator *op)
1368 {
1369         ScrArea *sa= CTX_wm_area(C);
1370         Object *ob;
1371         bArmature *arm;
1372         bPose *pose;
1373         bPoseChannel *pchan;
1374         short done= 0;
1375         
1376         /* since this call may also be used from the buttons window, we need to check for where to get the object */
1377         if (sa->spacetype == SPACE_BUTS) 
1378                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1379         else
1380                 ob= CTX_data_active_object(C);
1381         
1382         /* only continue if there's an object, and a pose there too */
1383         if (ELEM(NULL, ob, ob->pose))
1384                 return OPERATOR_CANCELLED;
1385         pose= ob->pose;
1386         arm= ob->data;
1387         
1388         /* add selected bones to ungroup then */
1389         // NOTE: unfortunately, we cannot use the context-iterators here, since they might not be defined...
1390         // CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pchans) 
1391         for (pchan= pose->chanbase.first; pchan; pchan= pchan->next) {
1392                 /* ensure that PoseChannel is on visible layer and is not hidden in PoseMode */
1393                 // NOTE: sync this view3d_context() in space_view3d.c
1394                 if ((pchan->bone) && (arm->layer & pchan->bone->layer) && !(pchan->bone->flag & BONE_HIDDEN_P)) {
1395                         if (pchan->bone->flag & (BONE_SELECTED|BONE_ACTIVE)) {
1396                                 if (pchan->agrp_index) {
1397                                         pchan->agrp_index= 0;
1398                                         done= 1;
1399                                 }
1400                         }
1401                 }
1402         }
1403         
1404         /* notifiers for updates */
1405         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1406         
1407         /* report done status */
1408         if (done)
1409                 return OPERATOR_FINISHED;
1410         else
1411                 return OPERATOR_CANCELLED;
1412 }
1413
1414 void POSE_OT_group_unassign (wmOperatorType *ot)
1415 {
1416         /* identifiers */
1417         ot->name= "Remove Selected from Bone Groups";
1418         ot->idname= "POSE_OT_group_unassign";
1419         ot->description= "Add selected bones from all bone groups";
1420         
1421         /* api callbacks */
1422         ot->exec= pose_group_unassign_exec;
1423         ot->poll= ED_operator_posemode;
1424         
1425         /* flags */
1426         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1427 }
1428
1429 /* ----------------- */
1430
1431 static int pose_groupOps_menu_invoke (bContext *C, wmOperator *op, wmEvent *evt)
1432 {
1433         Object *ob= CTX_data_active_object(C);
1434         uiPopupMenu *pup= uiPupMenuBegin(C, op->type->name, 0);
1435         uiLayout *layout= uiPupMenuLayout(pup);
1436         
1437         /* sanity check - must have object with pose */
1438         if ELEM(NULL, ob, ob->pose)
1439                 return OPERATOR_CANCELLED;
1440         
1441         /* get mode of action */
1442         if (CTX_DATA_COUNT(C, selected_pchans)) {
1443                 /* if selected bone(s), include options to add/remove to active group */
1444                 uiItemO(layout, "Add Selected to Active Group", 0, "POSE_OT_group_assign");
1445                 
1446                 uiItemS(layout);
1447                 
1448                 uiItemO(layout, "Remove Selected from All Groups", 0, "POSE_OT_group_unassign");
1449                 uiItemO(layout, "Remove Active Group", 0, "POSE_OT_group_remove");
1450         }
1451         else {
1452                 /* no selected bones - so just options for groups management */
1453                 uiItemO(layout, "Add New Group", 0, "POSE_OT_group_add");
1454                 uiItemO(layout, "Remove Active Group", 0, "POSE_OT_group_remove");
1455         }
1456                 
1457         return OPERATOR_CANCELLED;
1458 }
1459
1460 void POSE_OT_groups_menu (wmOperatorType *ot)
1461 {
1462         /* identifiers */
1463         ot->name= "Bone Group Tools";
1464         ot->idname= "POSE_OT_groups_menu";
1465         ot->description= "Menu displaying available tools for Bone Groups.";
1466         
1467         /* api callbacks (only invoke needed) */
1468         ot->invoke= pose_groupOps_menu_invoke;
1469         ot->poll= ED_operator_posemode;
1470         
1471         /* flags */
1472         ot->flag= OPTYPE_REGISTER;
1473 }
1474
1475 /* ********************************************** */
1476
1477 static short pose_select_same_group (Object *ob)
1478 {
1479         bPose *pose= (ob)? ob->pose : NULL;
1480         bArmature *arm= (ob)? ob->data : NULL;
1481         bPoseChannel *pchan, *chan;
1482         short changed= 0;
1483         
1484         if (ELEM3(NULL, ob, pose, arm))
1485                 return 0;
1486         
1487         /* loop in loop... bad and slow! */
1488         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1489                 if (arm->layer & pchan->bone->layer) {
1490                         if (pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
1491                                 
1492                                 /* only if group matches (and is not selected or current bone) */
1493                                 for (chan= ob->pose->chanbase.first; chan; chan= chan->next) {
1494                                         if (arm->layer & chan->bone->layer) {
1495                                                 if (pchan->agrp_index == chan->agrp_index) {
1496                                                         chan->bone->flag |= BONE_SELECTED;
1497                                                         changed= 1;
1498                                                 }
1499                                         }
1500                                 }
1501                                 
1502                         }
1503                 }
1504         }
1505         
1506         return changed;
1507 }
1508
1509 static short pose_select_same_layer (Object *ob)
1510 {
1511         bPose *pose= (ob)? ob->pose : NULL;
1512         bArmature *arm= (ob)? ob->data : NULL;
1513         bPoseChannel *pchan;
1514         short layers= 0, changed= 0;
1515         
1516         if (ELEM3(NULL, ob, pose, arm))
1517                 return 0;
1518         
1519         /* figure out what bones are selected */
1520         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1521                 if (arm->layer & pchan->bone->layer) {
1522                         if (pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
1523                                 layers |= pchan->bone->layer;
1524                         }
1525                 }
1526         }
1527         if (layers == 0) 
1528                 return 0;
1529                 
1530         /* select bones that are on same layers as layers flag */
1531         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1532                 if (arm->layer & pchan->bone->layer) {
1533                         if (layers & pchan->bone->layer) {
1534                                 pchan->bone->flag |= BONE_SELECTED;
1535                                 changed= 1;
1536                         }
1537                 }
1538         }
1539         
1540         return changed;
1541 }
1542
1543 void pose_select_grouped (Scene *scene, short nr)
1544 {
1545         short changed = 0;
1546         
1547         if (nr == 1)            changed= pose_select_same_group(OBACT);
1548         else if (nr == 2)       changed= pose_select_same_layer(OBACT);
1549         
1550         if (changed) {
1551                 countall();
1552                 BIF_undo_push("Select Grouped");
1553         }
1554 }
1555
1556 /* Shift-G in 3D-View while in PoseMode */
1557 void pose_select_grouped_menu (Scene *scene)
1558 {
1559         short nr;
1560         
1561         /* here we go */
1562         nr= pupmenu("Select Grouped%t|In Same Group%x1|In Same Layer%x2");
1563         pose_select_grouped(scene, nr);
1564 }
1565
1566 /* ********************************************** */
1567
1568 static int pose_flip_names_exec (bContext *C, wmOperator *op)
1569 {
1570         Object *ob= CTX_data_active_object(C);
1571         bArmature *arm;
1572         char newname[32];
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_flip_name(newname, 1);     // 1 = do strip off number extensions
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_flip_names (wmOperatorType *ot)
1598 {
1599         /* identifiers */
1600         ot->name= "Flip Names";
1601         ot->idname= "POSE_OT_flip_names";
1602         ot->description= "Flips (and corrects) the names of selected bones.";
1603         
1604         /* api callbacks */
1605         ot->exec= pose_flip_names_exec;
1606         ot->poll= ED_operator_posemode;
1607         
1608         /* flags */
1609         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1610 }
1611
1612 /* ------------------ */
1613
1614 static int pose_autoside_names_exec (bContext *C, wmOperator *op)
1615 {
1616         Object *ob= CTX_data_active_object(C);
1617         bArmature *arm;
1618         char newname[32];
1619         short axis= RNA_enum_get(op->ptr, "axis");
1620         
1621         /* paranoia checks */
1622         if (ELEM(NULL, ob, ob->pose)) 
1623                 return OPERATOR_CANCELLED;
1624         arm= ob->data;
1625         
1626         /* loop through selected bones, auto-naming them */
1627         CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pchans)
1628         {
1629                 BLI_strncpy(newname, pchan->name, sizeof(newname));
1630                 bone_autoside_name(newname, 1, axis, pchan->bone->head[axis], pchan->bone->tail[axis]);
1631                 ED_armature_bone_rename(arm, pchan->name, newname);
1632         }
1633         CTX_DATA_END;
1634         
1635         /* since we renamed stuff... */
1636         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
1637
1638         /* note, notifier might evolve */
1639         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1640         
1641         return OPERATOR_FINISHED;
1642 }
1643
1644 void POSE_OT_autoside_names (wmOperatorType *ot)
1645 {
1646         static EnumPropertyItem axis_items[]= {
1647                 {0, "XAXIS", 0, "X-Axis", "Left/Right"},
1648                 {1, "YAXIS", 0, "Y-Axis", "Front/Back"},
1649                 {2, "ZAXIS", 0, "Z-Axis", "Top/Bottom"},
1650                 {0, NULL, 0, NULL, NULL}};
1651         
1652         /* identifiers */
1653         ot->name= "AutoName by Axis";
1654         ot->idname= "POSE_OT_autoside_names";
1655         ot->description= "Automatically renames the selected bones according to which side of the target axis they fall on.";
1656         
1657         /* api callbacks */
1658         ot->invoke= WM_menu_invoke;
1659         ot->exec= pose_autoside_names_exec;
1660         ot->poll= ED_operator_posemode;
1661         
1662         /* flags */
1663         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1664         
1665         /* settings */
1666         RNA_def_enum(ot->srna, "axis", axis_items, 0, "Axis", "Axis tag names with.");
1667 }
1668
1669 /* ********************************************** */
1670
1671 /* context active object, or weightpainted object with armature in posemode */
1672 void pose_activate_flipped_bone(Scene *scene)
1673 {
1674         Object *ob= OBACT;
1675         bArmature *arm= ob->data;
1676         
1677         if(ob==NULL) return;
1678
1679         if(ob->mode && OB_MODE_WEIGHT_PAINT) {
1680                 ob= modifiers_isDeformedByArmature(ob);
1681         }
1682         if(ob && (ob->mode & OB_MODE_POSE)) {
1683                 bPoseChannel *pchan, *pchanf;
1684                 
1685                 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1686                         if(arm->layer & pchan->bone->layer) {
1687                                 if(pchan->bone->flag & BONE_ACTIVE)
1688                                         break;
1689                         }
1690                 }
1691                 if(pchan) {
1692                         char name[32];
1693                         
1694                         BLI_strncpy(name, pchan->name, 32);
1695                         bone_flip_name(name, 1);        // 0 = do not strip off number extensions
1696                         
1697                         pchanf= get_pose_channel(ob->pose, name);
1698                         if(pchanf && pchanf!=pchan) {
1699                                 pchan->bone->flag &= ~(BONE_SELECTED|BONE_ACTIVE);
1700                                 pchanf->bone->flag |= (BONE_SELECTED|BONE_ACTIVE);
1701                         
1702                                 /* in weightpaint we select the associated vertex group too */
1703                                 if(ob->mode & OB_MODE_WEIGHT_PAINT) {
1704                                         ED_vgroup_select_by_name(OBACT, name);
1705                                         DAG_id_flush_update(&OBACT->id, OB_RECALC_DATA);
1706                                 }
1707                                 
1708                                 // XXX notifiers need to be sent to other editors to update
1709                                 
1710                         }                       
1711                 }
1712         }
1713 }
1714
1715
1716 /* ********************************************** */
1717
1718 /* Present a popup to get the layers that should be used */
1719 // TODO: move to wm?
1720 static uiBlock *wm_layers_select_create_menu(bContext *C, ARegion *ar, void *arg_op)
1721 {
1722         wmOperator *op= arg_op;
1723         uiBlock *block;
1724         uiLayout *layout;
1725         uiStyle *style= U.uistyles.first;
1726         
1727         block= uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
1728         uiBlockClearFlag(block, UI_BLOCK_LOOP);
1729         uiBlockSetFlag(block, UI_BLOCK_KEEP_OPEN);
1730         
1731         layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 0, 0, 150, 20, style);
1732                 uiItemL(layout, op->type->name, 0);
1733                 uiTemplateLayers(layout, op->ptr, "layers"); /* must have a property named layers setup */
1734                 
1735         uiPopupBoundsBlock(block, 4.0f, 0, 0);
1736         uiEndBlock(C, block);
1737         
1738         return block;
1739 }
1740
1741 /* ------------------- */
1742
1743 /* Present a popup to get the layers that should be used */
1744 static int pose_armature_layers_invoke (bContext *C, wmOperator *op, wmEvent *evt)
1745 {
1746         Object *ob= CTX_data_active_object(C);
1747         bArmature *arm= (ob)? ob->data : NULL;
1748         PointerRNA ptr;
1749         int layers[16]; /* hardcoded for now - we can only have 16 armature layers, so this should be fine... */
1750         
1751         /* sanity checking */
1752         if (arm == NULL)
1753                 return OPERATOR_CANCELLED;
1754                 
1755         /* get RNA pointer to armature data to use that to retrieve the layers as ints to init the operator */
1756         RNA_id_pointer_create((ID *)arm, &ptr);
1757         RNA_boolean_get_array(&ptr, "layer", layers);
1758         RNA_boolean_set_array(op->ptr, "layers", layers);
1759         
1760                 /* part to sync with other similar operators... */
1761         /* pass on operator, so return modal */
1762         uiPupBlockOperator(C, wm_layers_select_create_menu, op, WM_OP_EXEC_DEFAULT);
1763         return OPERATOR_RUNNING_MODAL|OPERATOR_PASS_THROUGH;
1764 }
1765
1766 /* Set the visible layers for the active armature (edit and pose modes) */
1767 static int pose_armature_layers_exec (bContext *C, wmOperator *op)
1768 {
1769         Object *ob= CTX_data_active_object(C);
1770         bArmature *arm= (ob)? ob->data : NULL;
1771         PointerRNA ptr;
1772         int layers[16]; /* hardcoded for now - we can only have 16 armature layers, so this should be fine... */
1773         
1774         /* get the values set in the operator properties */
1775         RNA_boolean_get_array(op->ptr, "layers", layers);
1776         
1777         /* get pointer for armature, and write data there... */
1778         RNA_id_pointer_create((ID *)arm, &ptr);
1779         RNA_boolean_set_array(&ptr, "layer", layers);
1780         
1781         /* note, notifier might evolve */
1782         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1783         
1784         return OPERATOR_FINISHED;
1785 }
1786
1787
1788 void POSE_OT_armature_layers (wmOperatorType *ot)
1789 {
1790         /* identifiers */
1791         ot->name= "Change Armature Layers";
1792         ot->idname= "POSE_OT_armature_layers";
1793         ot->description= "Change the visible armature layers.";
1794         
1795         /* callbacks */
1796         ot->invoke= pose_armature_layers_invoke;
1797         ot->exec= pose_armature_layers_exec;
1798         ot->poll= ED_operator_posemode;
1799         
1800         /* flags */
1801         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1802         
1803         /* properties */
1804         RNA_def_boolean_array(ot->srna, "layers", 16, NULL, "Layers", "Armature layers to make visible.");
1805 }
1806
1807 void ARMATURE_OT_armature_layers (wmOperatorType *ot)
1808 {
1809         /* identifiers */
1810         ot->name= "Change Armature Layers";
1811         ot->idname= "ARMATURE_OT_armature_layers";
1812         ot->description= "Change the visible armature layers.";
1813         
1814         /* callbacks */
1815         ot->invoke= pose_armature_layers_invoke;
1816         ot->exec= pose_armature_layers_exec;
1817         ot->poll= ED_operator_editarmature;
1818         
1819         /* flags */
1820         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1821         
1822         /* properties */
1823         RNA_def_boolean_array(ot->srna, "layers", 16, NULL, "Layers", "Armature layers to make visible.");
1824 }
1825
1826 /* ------------------- */
1827
1828 /* Present a popup to get the layers that should be used */
1829 static int pose_bone_layers_invoke (bContext *C, wmOperator *op, wmEvent *evt)
1830 {
1831         int layers[16]; /* hardcoded for now - we can only have 16 armature layers, so this should be fine... */
1832         
1833         /* get layers that are active already */
1834         memset(&layers, 0, sizeof(layers)); /* set all layers to be off by default */
1835         
1836         CTX_DATA_BEGIN(C, bPoseChannel *, pchan, selected_pchans) 
1837         {
1838                 short bit;
1839                 
1840                 /* loop over the bits for this pchan's layers, adding layers where they're needed */
1841                 for (bit= 0; bit < 16; bit++) {
1842                         if (pchan->bone->layer & (1<<bit))
1843                                 layers[bit]= 1;
1844                 }
1845         }
1846         CTX_DATA_END;
1847         
1848         /* copy layers to operator */
1849         RNA_boolean_set_array(op->ptr, "layers", layers);
1850         
1851                 /* part to sync with other similar operators... */
1852         /* pass on operator, so return modal */
1853         uiPupBlockOperator(C, wm_layers_select_create_menu, op, WM_OP_EXEC_DEFAULT);
1854         return OPERATOR_RUNNING_MODAL|OPERATOR_PASS_THROUGH;
1855 }
1856
1857 /* Set the visible layers for the active armature (edit and pose modes) */
1858 static int pose_bone_layers_exec (bContext *C, wmOperator *op)
1859 {
1860         Object *ob= CTX_data_active_object(C);
1861         bArmature *arm= (ob)? ob->data : NULL;
1862         PointerRNA ptr;
1863         int layers[16]; /* hardcoded for now - we can only have 16 armature layers, so this should be fine... */
1864         
1865         /* get the values set in the operator properties */
1866         RNA_boolean_get_array(op->ptr, "layers", layers);
1867         
1868         /* set layers of pchans based on the values set in the operator props */
1869         CTX_DATA_BEGIN(C, bPoseChannel *, pchan, selected_pchans) 
1870         {
1871                 /* get pointer for pchan, and write flags this way */
1872                 RNA_pointer_create((ID *)arm, &RNA_Bone, pchan->bone, &ptr);
1873                 RNA_boolean_set_array(&ptr, "layer", layers);
1874         }
1875         CTX_DATA_END;
1876         
1877         /* note, notifier might evolve */
1878         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1879         
1880         return OPERATOR_FINISHED;
1881 }
1882
1883 void POSE_OT_bone_layers (wmOperatorType *ot)
1884 {
1885         /* identifiers */
1886         ot->name= "Change Bone Layers";
1887         ot->idname= "POSE_OT_bone_layers";
1888         ot->description= "Change the layers that the selected bones belong to.";
1889         
1890         /* callbacks */
1891         ot->invoke= pose_bone_layers_invoke;
1892         ot->exec= pose_bone_layers_exec;
1893         ot->poll= ED_operator_posemode;
1894         
1895         /* flags */
1896         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1897         
1898         /* properties */
1899         RNA_def_boolean_array(ot->srna, "layers", 16, NULL, "Layers", "Armature layers that bone belongs to.");
1900 }
1901
1902 /* ------------------- */
1903
1904 /* Present a popup to get the layers that should be used */
1905 static int armature_bone_layers_invoke (bContext *C, wmOperator *op, wmEvent *evt)
1906 {
1907         int layers[16]; /* hardcoded for now - we can only have 16 armature layers, so this should be fine... */
1908         
1909         /* get layers that are active already */
1910         memset(&layers, 0, sizeof(layers)); /* set all layers to be off by default */
1911         
1912         CTX_DATA_BEGIN(C, EditBone *, ebone, selected_editable_bones) 
1913         {
1914                 short bit;
1915                 
1916                 /* loop over the bits for this pchan's layers, adding layers where they're needed */
1917                 for (bit= 0; bit < 16; bit++) {
1918                         if (ebone->layer & (1<<bit))
1919                                 layers[bit]= 1;
1920                 }
1921         }
1922         CTX_DATA_END;
1923         
1924         /* copy layers to operator */
1925         RNA_boolean_set_array(op->ptr, "layers", layers);
1926         
1927                 /* part to sync with other similar operators... */
1928         /* pass on operator, so return modal */
1929         uiPupBlockOperator(C, wm_layers_select_create_menu, op, WM_OP_EXEC_DEFAULT);
1930         return OPERATOR_RUNNING_MODAL|OPERATOR_PASS_THROUGH;
1931 }
1932
1933 /* Set the visible layers for the active armature (edit and pose modes) */
1934 static int armature_bone_layers_exec (bContext *C, wmOperator *op)
1935 {
1936         Object *ob= CTX_data_active_object(C);
1937         bArmature *arm= (ob)? ob->data : NULL;
1938         PointerRNA ptr;
1939         int layers[16]; /* hardcoded for now - we can only have 16 armature layers, so this should be fine... */
1940         
1941         /* get the values set in the operator properties */
1942         RNA_boolean_get_array(op->ptr, "layers", layers);
1943         
1944         /* set layers of pchans based on the values set in the operator props */
1945         CTX_DATA_BEGIN(C, EditBone *, ebone, selected_editable_bones) 
1946         {
1947                 /* get pointer for pchan, and write flags this way */
1948                 RNA_pointer_create((ID *)arm, &RNA_EditBone, ebone, &ptr);
1949                 RNA_boolean_set_array(&ptr, "layers", layers);
1950         }
1951         CTX_DATA_END;
1952         
1953         /* note, notifier might evolve */
1954         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1955         
1956         return OPERATOR_FINISHED;
1957 }
1958
1959 void ARMATURE_OT_bone_layers (wmOperatorType *ot)
1960 {
1961         /* identifiers */
1962         ot->name= "Change Bone Layers";
1963         ot->idname= "ARMATURE_OT_bone_layers";
1964         ot->description= "Change the layers that the selected bones belong to.";
1965         
1966         /* callbacks */
1967         ot->invoke= armature_bone_layers_invoke;
1968         ot->exec= armature_bone_layers_exec;
1969         ot->poll= ED_operator_editarmature;
1970         
1971         /* flags */
1972         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1973         
1974         /* properties */
1975         RNA_def_boolean_array(ot->srna, "layers", 16, NULL, "Layers", "Armature layers that bone belongs to.");
1976 }
1977
1978
1979 #if 0
1980 // XXX old sys
1981 /* for use with pose_relax only */
1982 static int pose_relax_icu(struct IpoCurve *icu, float framef, float *val, float *frame_prev, float *frame_next)
1983 {
1984         if (!icu) {
1985                 return 0;
1986         } 
1987         else {
1988                 BezTriple *bezt = icu->bezt;
1989                 
1990                 BezTriple *bezt_prev=NULL, *bezt_next=NULL;
1991                 float w1, w2, wtot;
1992                 int i;
1993                 
1994                 for (i=0; i < icu->totvert; i++, bezt++) {
1995                         if (bezt->vec[1][0] < framef - 0.5) {
1996                                 bezt_prev = bezt;
1997                         } else {
1998                                 break;
1999                         }
2000                 }
2001                 
2002                 if (bezt_prev==NULL) return 0;
2003                 
2004                 /* advance to the next, dont need to advance i */
2005                 bezt = bezt_prev+1;
2006                 
2007                 for (; i < icu->totvert; i++, bezt++) {
2008                         if (bezt->vec[1][0] > framef + 0.5) {
2009                                 bezt_next = bezt;
2010                                                 break;
2011                         }
2012                 }
2013                 
2014                 if (bezt_next==NULL) return 0;
2015         
2016                 if (val) {
2017                         w1 = framef - bezt_prev->vec[1][0];
2018                         w2 = bezt_next->vec[1][0] - framef;
2019                         wtot = w1 + w2;
2020                         w1=w1/wtot;
2021                         w2=w2/wtot;
2022 #if 0
2023                         val = (bezt_prev->vec[1][1] * w2) + (bezt_next->vec[1][1] * w1);
2024 #else
2025                         /* apply the value with a hard coded 6th */
2026                         *val = (((bezt_prev->vec[1][1] * w2) + (bezt_next->vec[1][1] * w1)) + (*val * 5.0f)) / 6.0f;
2027 #endif
2028                 }
2029                 
2030                 if (frame_prev) *frame_prev = bezt_prev->vec[1][0];
2031                 if (frame_next) *frame_next = bezt_next->vec[1][0];
2032                 
2033                 return 1;
2034         }
2035 }
2036 #endif
2037
2038 void pose_relax(Scene *scene)
2039 {
2040         Object *ob = OBACT;
2041         bPose *pose;
2042         bAction *act;
2043         bArmature *arm;
2044         
2045 //      IpoCurve *icu_w, *icu_x, *icu_y, *icu_z;
2046         
2047         bPoseChannel *pchan;
2048 //      bActionChannel *achan;
2049 //      float framef = F_CFRA;
2050 //      float frame_prev, frame_next;
2051 //      float quat_prev[4], quat_next[4], quat_interp[4], quat_orig[4];
2052         
2053         int do_scale = 0;
2054         int do_loc = 0;
2055         int do_quat = 0;
2056         int flag = 0;
2057 //      int do_x, do_y, do_z;
2058         
2059         if (!ob) return;
2060         
2061         pose = ob->pose;
2062         act = ob->action;
2063         arm = (bArmature *)ob->data;
2064         
2065         if (!pose || !act || !arm) return;
2066         
2067         for (pchan=pose->chanbase.first; pchan; pchan= pchan->next) {
2068                 
2069                 pchan->bone->flag &= ~BONE_TRANSFORM;
2070                 
2071                 if (pchan->bone->layer & arm->layer) {
2072                         if (pchan->bone->flag & BONE_SELECTED) {
2073                                 /* do we have an ipo curve? */
2074 #if 0 // XXX old animation system
2075                                 achan= get_action_channel(act, pchan->name);
2076                                 
2077                                 if (achan && achan->ipo) {
2078                                         /*calc_ipo(achan->ipo, ctime);*/
2079                                         
2080                                         do_x = pose_relax_icu(find_ipocurve(achan->ipo, AC_LOC_X), framef, &pchan->loc[0], NULL, NULL);
2081                                         do_y = pose_relax_icu(find_ipocurve(achan->ipo, AC_LOC_Y), framef, &pchan->loc[1], NULL, NULL);
2082                                         do_z = pose_relax_icu(find_ipocurve(achan->ipo, AC_LOC_Z), framef, &pchan->loc[2], NULL, NULL);
2083                                         do_loc += do_x + do_y + do_z;
2084                                         
2085                                         do_x = pose_relax_icu(find_ipocurve(achan->ipo, AC_SIZE_X), framef, &pchan->size[0], NULL, NULL);
2086                                         do_y = pose_relax_icu(find_ipocurve(achan->ipo, AC_SIZE_Y), framef, &pchan->size[1], NULL, NULL);
2087                                         do_z = pose_relax_icu(find_ipocurve(achan->ipo, AC_SIZE_Z), framef, &pchan->size[2], NULL, NULL);
2088                                         do_scale += do_x + do_y + do_z;
2089                                                 
2090                                         if(     ((icu_w = find_ipocurve(achan->ipo, AC_QUAT_W))) &&
2091                                                 ((icu_x = find_ipocurve(achan->ipo, AC_QUAT_X))) &&
2092                                                 ((icu_y = find_ipocurve(achan->ipo, AC_QUAT_Y))) &&
2093                                                 ((icu_z = find_ipocurve(achan->ipo, AC_QUAT_Z))) )
2094                                         {
2095                                                 /* use the quatw keyframe as a basis for others */
2096                                                 if (pose_relax_icu(icu_w, framef, NULL, &frame_prev, &frame_next)) {
2097                                                         /* get 2 quats */
2098                                                         quat_prev[0] = eval_icu(icu_w, frame_prev);
2099                                                         quat_prev[1] = eval_icu(icu_x, frame_prev);
2100                                                         quat_prev[2] = eval_icu(icu_y, frame_prev);
2101                                                         quat_prev[3] = eval_icu(icu_z, frame_prev);
2102                                                         
2103                                                         quat_next[0] = eval_icu(icu_w, frame_next);
2104                                                         quat_next[1] = eval_icu(icu_x, frame_next);
2105                                                         quat_next[2] = eval_icu(icu_y, frame_next);
2106                                                         quat_next[3] = eval_icu(icu_z, frame_next);
2107                                                         
2108 #if 0
2109                                                         /* apply the setting, completely smooth */
2110                                                         QuatInterpol(pchan->quat, quat_prev, quat_next, (framef-frame_prev) / (frame_next-frame_prev) );
2111 #else
2112                                                         /* tricky interpolation */
2113                                                         QuatInterpol(quat_interp, quat_prev, quat_next, (framef-frame_prev) / (frame_next-frame_prev) );
2114                                                         QUATCOPY(quat_orig, pchan->quat);
2115                                                         QuatInterpol(pchan->quat, quat_orig, quat_interp, 1.0f/6.0f);
2116                                                         /* done */
2117 #endif
2118                                                         do_quat++;
2119                                                 }
2120                                         }
2121                                         
2122                                         /* apply BONE_TRANSFORM tag so that autokeying will pick it up */
2123                                         pchan->bone->flag |= BONE_TRANSFORM;
2124                                 }
2125                                 
2126 #endif // XXX old animation system
2127                         }
2128                 }
2129         }
2130         
2131         ob->pose->flag |= (POSE_LOCKED|POSE_DO_UNLOCK);
2132         
2133         /* do auto-keying */
2134         if (do_loc)             flag |= TFM_TRANSLATION;
2135         if (do_scale)   flag |= TFM_RESIZE;
2136         if (do_quat)    flag |= TFM_ROTATION;
2137         autokeyframe_pose_cb_func(ob, flag, 0);
2138          
2139         /* clear BONE_TRANSFORM flags */
2140         for (pchan=pose->chanbase.first; pchan; pchan= pchan->next)
2141                 pchan->bone->flag &= ~ BONE_TRANSFORM;
2142         
2143         /* do depsgraph flush */
2144         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
2145         BIF_undo_push("Relax Pose");
2146 }
2147
2148 /* for use in insertkey, ensure rotation goes other way around */
2149 void pose_flipquats(Scene *scene)
2150 {
2151         Object *ob = OBACT;
2152         bArmature *arm= ob->data;
2153         bPoseChannel *pchan;
2154         
2155         if(ob->pose==NULL)
2156                 return;
2157         
2158         /* find sel bones */
2159         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
2160                 if(pchan->bone && (pchan->bone->flag & BONE_SELECTED) && (pchan->bone->layer & arm->layer)) {
2161                         /* quaternions have 720 degree range */
2162                         pchan->quat[0]= -pchan->quat[0];
2163                         pchan->quat[1]= -pchan->quat[1];
2164                         pchan->quat[2]= -pchan->quat[2];
2165                         pchan->quat[3]= -pchan->quat[3];
2166                 }
2167         }
2168         
2169         /* do autokey */
2170         autokeyframe_pose_cb_func(ob, TFM_ROTATION, 0);
2171 }
2172
2173 /* context: active channel */
2174 void pose_special_editmenu(Scene *scene)
2175 {
2176 #if 0
2177         Object *obedit= scene->obedit; // XXX context
2178         Object *ob= OBACT;
2179         short nr;
2180         
2181         /* paranoia checks */
2182         if(!ob && !ob->pose) return;
2183         if(ob==obedit || (ob->mode & OB_MODE_POSE)==0) return;
2184         
2185         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");
2186         if(nr==1) {
2187                 pose_select_constraint_target(scene);
2188         }
2189         else if(nr==2) {
2190                 pose_flip_names();
2191         }
2192         else if(nr==3) {
2193                 pose_calculate_path(C, ob);
2194         }
2195         else if(nr==4) {
2196                 pose_clear_paths(ob);
2197         }
2198         else if(nr==5) {
2199                 pose_clear_user_transforms(scene, ob);
2200         }
2201         else if(nr==6) {
2202                 pose_relax();
2203         }
2204         else if(ELEM3(nr, 7, 8, 9)) {
2205                 pose_autoside_names(nr-7);
2206         }
2207 #endif
2208 }
2209
2210 /* Restore selected pose-bones to 'action'-defined pose */
2211 void pose_clear_user_transforms(Scene *scene, Object *ob)
2212 {
2213         bArmature *arm= ob->data;
2214         bPoseChannel *pchan;
2215         
2216         if (ob->pose == NULL)
2217                 return;
2218         
2219         /* if the object has an action, restore pose to the pose defined by the action by clearing pose on selected bones */
2220         if (ob->action) {
2221                 /* find selected bones */
2222                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
2223                         if (pchan->bone && (pchan->bone->flag & BONE_SELECTED) && (pchan->bone->layer & arm->layer)) {
2224                                 /* just clear the BONE_UNKEYED flag, allowing this bone to get overwritten by actions again */
2225                                 pchan->bone->flag &= ~BONE_UNKEYED;
2226                         }
2227                 }
2228                 
2229                 /* clear pose locking flag 
2230                  *      - this will only clear the user-defined pose in the selected bones, where BONE_UNKEYED has been cleared
2231                  */
2232                 ob->pose->flag |= POSE_DO_UNLOCK;
2233         }
2234         else {
2235                 /* no action, so restore entire pose to rest pose (cannot restore only selected bones) */
2236                 rest_pose(ob->pose);
2237         }
2238         
2239         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
2240         BIF_undo_push("Clear User Transform");
2241 }
2242