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