e9ebdb87a3cd04b02b470359fee285e18032b877
[blender-staging.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 movetolayer_short_buts() {return 1;}
93 static int pupmenu() {return 0;}
94 static void error() {};
95 static void BIF_undo_push() {}
96 static void countall() {}
97 static void autokeyframe_pose_cb_func() {}
98 /* ************* XXX *************** */
99
100 /* This function is used to indicate that a bone is selected and needs keyframes inserted */
101 void set_pose_keys (Object *ob)
102 {
103         bArmature *arm= ob->data;
104         bPoseChannel *chan;
105
106         if (ob->pose){
107                 for (chan=ob->pose->chanbase.first; chan; chan=chan->next){
108                         Bone *bone= chan->bone;
109                         if ((bone) && (bone->flag & BONE_SELECTED) && (arm->layer & bone->layer))
110                                 chan->flag |= POSE_KEY; 
111                         else
112                                 chan->flag &= ~POSE_KEY;
113                 }
114         }
115 }
116
117 /* This function is used to process the necessary updates for */
118 void ED_armature_enter_posemode(bContext *C, Base *base)
119 {
120         Object *ob= base->object;
121         
122         if (ob->id.lib){
123                 error ("Can't pose libdata");
124                 return;
125         }
126         
127         switch (ob->type){
128                 case OB_ARMATURE:
129                         
130                         ob->flag |= OB_POSEMODE;
131                         base->flag= ob->flag;
132                         
133                         WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_MODE_POSE, NULL);
134                         
135                         break;
136                 default:
137                         return;
138         }
139         ED_view3d_exit_paint_modes(C);
140 }
141
142 void ED_armature_exit_posemode(bContext *C, Base *base)
143 {
144         if(base) {
145                 Object *ob= base->object;
146                 
147                 ob->flag &= ~OB_POSEMODE;
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->flag & OB_POSEMODE)==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->flag & OB_POSEMODE)==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->flag & OB_POSEMODE)==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(G.f & G_WEIGHTPAINT) {
1651                 ob= modifiers_isDeformedByArmature(ob);
1652         }
1653         if(ob && (ob->flag & OB_POSEMODE)) {
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(G.f & G_WEIGHTPAINT) {
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 /* This function pops up the move-to-layer popup widgets when the user
1687  * presses either SHIFT-MKEY or MKEY in PoseMode OR EditMode (for Armatures)
1688  */
1689 void pose_movetolayer(Scene *scene)
1690 {
1691         Object *obedit= scene->obedit; // XXX context
1692         Object *ob= OBACT;
1693         bArmature *arm;
1694         short lay= 0;
1695         short shift= 0; // XXX
1696         
1697         if (ob==NULL) return;
1698         arm= ob->data;
1699         
1700         if (shift) {
1701                 /* armature layers */
1702                 lay= arm->layer;
1703                 if ( movetolayer_short_buts(&lay, "Armature Layers")==0 ) return;
1704                 if (lay==0) return;
1705                 arm->layer= lay;
1706                 if(ob->pose)
1707                         ob->pose->proxy_layer= lay;
1708                 
1709         }
1710         else if (obedit) {
1711                 /* the check for editbone layer moving needs to occur before posemode one to work */
1712                 EditBone *ebo;
1713                 EditBone *flipBone;
1714                 
1715                 for (ebo= arm->edbo->first; ebo; ebo= ebo->next) {
1716                         if (arm->layer & ebo->layer) {
1717                                 if (ebo->flag & BONE_SELECTED)
1718                                         lay |= ebo->layer;
1719                         }
1720                 }
1721                 if (lay==0) return;
1722                 
1723                 if ( movetolayer_short_buts(&lay, "Bone Layers")==0 ) return;
1724                 if (lay==0) return;
1725                 
1726                 for (ebo= arm->edbo->first; ebo; ebo= ebo->next) {
1727                         if (arm->layer & ebo->layer) {
1728                                 if (ebo->flag & BONE_SELECTED) {
1729                                         ebo->layer= lay;
1730                                         if (arm->flag & ARM_MIRROR_EDIT) {
1731                                                 flipBone = ED_armature_bone_get_mirrored(arm->edbo, ebo);
1732                                                 if (flipBone)
1733                                                         flipBone->layer = lay;
1734                                         }
1735                                 }
1736                         }
1737                 }
1738                 
1739                 BIF_undo_push("Move Bone Layer");
1740         }
1741         else if (ob->flag & OB_POSEMODE) {
1742                 /* pose-channel layers */
1743                 bPoseChannel *pchan;
1744                 
1745                 if (pose_has_protected_selected(ob, 0, 1))
1746                         return;
1747                 
1748                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1749                         if (arm->layer & pchan->bone->layer) {
1750                                 if (pchan->bone->flag & BONE_SELECTED)
1751                                         lay |= pchan->bone->layer;
1752                         }
1753                 }
1754                 if (lay==0) return;
1755                 
1756                 if ( movetolayer_short_buts(&lay, "Bone Layers")==0 ) return;
1757                 if (lay==0) return;
1758                 
1759                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1760                         if (arm->layer & pchan->bone->layer) {
1761                                 if (pchan->bone->flag & BONE_SELECTED)
1762                                         pchan->bone->layer= lay;
1763                         }
1764                 }
1765                 
1766                 BIF_undo_push("Move Bone Layer");
1767         }
1768 }
1769
1770 #if 0
1771 // XXX old sys
1772 /* for use with pose_relax only */
1773 static int pose_relax_icu(struct IpoCurve *icu, float framef, float *val, float *frame_prev, float *frame_next)
1774 {
1775         if (!icu) {
1776                 return 0;
1777         } 
1778         else {
1779                 BezTriple *bezt = icu->bezt;
1780                 
1781                 BezTriple *bezt_prev=NULL, *bezt_next=NULL;
1782                 float w1, w2, wtot;
1783                 int i;
1784                 
1785                 for (i=0; i < icu->totvert; i++, bezt++) {
1786                         if (bezt->vec[1][0] < framef - 0.5) {
1787                                 bezt_prev = bezt;
1788                         } else {
1789                                 break;
1790                         }
1791                 }
1792                 
1793                 if (bezt_prev==NULL) return 0;
1794                 
1795                 /* advance to the next, dont need to advance i */
1796                 bezt = bezt_prev+1;
1797                 
1798                 for (; i < icu->totvert; i++, bezt++) {
1799                         if (bezt->vec[1][0] > framef + 0.5) {
1800                                 bezt_next = bezt;
1801                                                 break;
1802                         }
1803                 }
1804                 
1805                 if (bezt_next==NULL) return 0;
1806         
1807                 if (val) {
1808                         w1 = framef - bezt_prev->vec[1][0];
1809                         w2 = bezt_next->vec[1][0] - framef;
1810                         wtot = w1 + w2;
1811                         w1=w1/wtot;
1812                         w2=w2/wtot;
1813 #if 0
1814                         val = (bezt_prev->vec[1][1] * w2) + (bezt_next->vec[1][1] * w1);
1815 #else
1816                         /* apply the value with a hard coded 6th */
1817                         *val = (((bezt_prev->vec[1][1] * w2) + (bezt_next->vec[1][1] * w1)) + (*val * 5.0f)) / 6.0f;
1818 #endif
1819                 }
1820                 
1821                 if (frame_prev) *frame_prev = bezt_prev->vec[1][0];
1822                 if (frame_next) *frame_next = bezt_next->vec[1][0];
1823                 
1824                 return 1;
1825         }
1826 }
1827 #endif
1828
1829 void pose_relax(Scene *scene)
1830 {
1831         Object *ob = OBACT;
1832         bPose *pose;
1833         bAction *act;
1834         bArmature *arm;
1835         
1836 //      IpoCurve *icu_w, *icu_x, *icu_y, *icu_z;
1837         
1838         bPoseChannel *pchan;
1839 //      bActionChannel *achan;
1840 //      float framef = F_CFRA;
1841 //      float frame_prev, frame_next;
1842 //      float quat_prev[4], quat_next[4], quat_interp[4], quat_orig[4];
1843         
1844         int do_scale = 0;
1845         int do_loc = 0;
1846         int do_quat = 0;
1847         int flag = 0;
1848 //      int do_x, do_y, do_z;
1849         
1850         if (!ob) return;
1851         
1852         pose = ob->pose;
1853         act = ob->action;
1854         arm = (bArmature *)ob->data;
1855         
1856         if (!pose || !act || !arm) return;
1857         
1858         for (pchan=pose->chanbase.first; pchan; pchan= pchan->next) {
1859                 
1860                 pchan->bone->flag &= ~BONE_TRANSFORM;
1861                 
1862                 if (pchan->bone->layer & arm->layer) {
1863                         if (pchan->bone->flag & BONE_SELECTED) {
1864                                 /* do we have an ipo curve? */
1865 #if 0 // XXX old animation system
1866                                 achan= get_action_channel(act, pchan->name);
1867                                 
1868                                 if (achan && achan->ipo) {
1869                                         /*calc_ipo(achan->ipo, ctime);*/
1870                                         
1871                                         do_x = pose_relax_icu(find_ipocurve(achan->ipo, AC_LOC_X), framef, &pchan->loc[0], NULL, NULL);
1872                                         do_y = pose_relax_icu(find_ipocurve(achan->ipo, AC_LOC_Y), framef, &pchan->loc[1], NULL, NULL);
1873                                         do_z = pose_relax_icu(find_ipocurve(achan->ipo, AC_LOC_Z), framef, &pchan->loc[2], NULL, NULL);
1874                                         do_loc += do_x + do_y + do_z;
1875                                         
1876                                         do_x = pose_relax_icu(find_ipocurve(achan->ipo, AC_SIZE_X), framef, &pchan->size[0], NULL, NULL);
1877                                         do_y = pose_relax_icu(find_ipocurve(achan->ipo, AC_SIZE_Y), framef, &pchan->size[1], NULL, NULL);
1878                                         do_z = pose_relax_icu(find_ipocurve(achan->ipo, AC_SIZE_Z), framef, &pchan->size[2], NULL, NULL);
1879                                         do_scale += do_x + do_y + do_z;
1880                                                 
1881                                         if(     ((icu_w = find_ipocurve(achan->ipo, AC_QUAT_W))) &&
1882                                                 ((icu_x = find_ipocurve(achan->ipo, AC_QUAT_X))) &&
1883                                                 ((icu_y = find_ipocurve(achan->ipo, AC_QUAT_Y))) &&
1884                                                 ((icu_z = find_ipocurve(achan->ipo, AC_QUAT_Z))) )
1885                                         {
1886                                                 /* use the quatw keyframe as a basis for others */
1887                                                 if (pose_relax_icu(icu_w, framef, NULL, &frame_prev, &frame_next)) {
1888                                                         /* get 2 quats */
1889                                                         quat_prev[0] = eval_icu(icu_w, frame_prev);
1890                                                         quat_prev[1] = eval_icu(icu_x, frame_prev);
1891                                                         quat_prev[2] = eval_icu(icu_y, frame_prev);
1892                                                         quat_prev[3] = eval_icu(icu_z, frame_prev);
1893                                                         
1894                                                         quat_next[0] = eval_icu(icu_w, frame_next);
1895                                                         quat_next[1] = eval_icu(icu_x, frame_next);
1896                                                         quat_next[2] = eval_icu(icu_y, frame_next);
1897                                                         quat_next[3] = eval_icu(icu_z, frame_next);
1898                                                         
1899 #if 0
1900                                                         /* apply the setting, completely smooth */
1901                                                         QuatInterpol(pchan->quat, quat_prev, quat_next, (framef-frame_prev) / (frame_next-frame_prev) );
1902 #else
1903                                                         /* tricky interpolation */
1904                                                         QuatInterpol(quat_interp, quat_prev, quat_next, (framef-frame_prev) / (frame_next-frame_prev) );
1905                                                         QUATCOPY(quat_orig, pchan->quat);
1906                                                         QuatInterpol(pchan->quat, quat_orig, quat_interp, 1.0f/6.0f);
1907                                                         /* done */
1908 #endif
1909                                                         do_quat++;
1910                                                 }
1911                                         }
1912                                         
1913                                         /* apply BONE_TRANSFORM tag so that autokeying will pick it up */
1914                                         pchan->bone->flag |= BONE_TRANSFORM;
1915                                 }
1916                                 
1917 #endif // XXX old animation system
1918                         }
1919                 }
1920         }
1921         
1922         ob->pose->flag |= (POSE_LOCKED|POSE_DO_UNLOCK);
1923         
1924         /* do auto-keying */
1925         if (do_loc)             flag |= TFM_TRANSLATION;
1926         if (do_scale)   flag |= TFM_RESIZE;
1927         if (do_quat)    flag |= TFM_ROTATION;
1928         autokeyframe_pose_cb_func(ob, flag, 0);
1929          
1930         /* clear BONE_TRANSFORM flags */
1931         for (pchan=pose->chanbase.first; pchan; pchan= pchan->next)
1932                 pchan->bone->flag &= ~ BONE_TRANSFORM;
1933         
1934         /* do depsgraph flush */
1935         DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
1936         BIF_undo_push("Relax Pose");
1937 }
1938
1939 /* for use in insertkey, ensure rotation goes other way around */
1940 void pose_flipquats(Scene *scene)
1941 {
1942         Object *ob = OBACT;
1943         bArmature *arm= ob->data;
1944         bPoseChannel *pchan;
1945         
1946         if(ob->pose==NULL)
1947                 return;
1948         
1949         /* find sel bones */
1950         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1951                 if(pchan->bone && (pchan->bone->flag & BONE_SELECTED) && (pchan->bone->layer & arm->layer)) {
1952                         /* quaternions have 720 degree range */
1953                         pchan->quat[0]= -pchan->quat[0];
1954                         pchan->quat[1]= -pchan->quat[1];
1955                         pchan->quat[2]= -pchan->quat[2];
1956                         pchan->quat[3]= -pchan->quat[3];
1957                 }
1958         }
1959         
1960         /* do autokey */
1961         autokeyframe_pose_cb_func(ob, TFM_ROTATION, 0);
1962 }
1963
1964 /* context: active channel */
1965 void pose_special_editmenu(Scene *scene)
1966 {
1967 #if 0
1968         Object *obedit= scene->obedit; // XXX context
1969         Object *ob= OBACT;
1970         short nr;
1971         
1972         /* paranoia checks */
1973         if(!ob && !ob->pose) return;
1974         if(ob==obedit || (ob->flag & OB_POSEMODE)==0) return;
1975         
1976         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");
1977         if(nr==1) {
1978                 pose_select_constraint_target(scene);
1979         }
1980         else if(nr==2) {
1981                 pose_flip_names();
1982         }
1983         else if(nr==3) {
1984                 pose_calculate_path(C, ob);
1985         }
1986         else if(nr==4) {
1987                 pose_clear_paths(ob);
1988         }
1989         else if(nr==5) {
1990                 pose_clear_user_transforms(scene, ob);
1991         }
1992         else if(nr==6) {
1993                 pose_relax();
1994         }
1995         else if(ELEM3(nr, 7, 8, 9)) {
1996                 pose_autoside_names(nr-7);
1997         }
1998 #endif
1999 }
2000
2001 /* Restore selected pose-bones to 'action'-defined pose */
2002 void pose_clear_user_transforms(Scene *scene, Object *ob)
2003 {
2004         bArmature *arm= ob->data;
2005         bPoseChannel *pchan;
2006         
2007         if (ob->pose == NULL)
2008                 return;
2009         
2010         /* if the object has an action, restore pose to the pose defined by the action by clearing pose on selected bones */
2011         if (ob->action) {
2012                 /* find selected bones */
2013                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
2014                         if (pchan->bone && (pchan->bone->flag & BONE_SELECTED) && (pchan->bone->layer & arm->layer)) {
2015                                 /* just clear the BONE_UNKEYED flag, allowing this bone to get overwritten by actions again */
2016                                 pchan->bone->flag &= ~BONE_UNKEYED;
2017                         }
2018                 }
2019                 
2020                 /* clear pose locking flag 
2021                  *      - this will only clear the user-defined pose in the selected bones, where BONE_UNKEYED has been cleared
2022                  */
2023                 ob->pose->flag |= POSE_DO_UNLOCK;
2024         }
2025         else {
2026                 /* no action, so restore entire pose to rest pose (cannot restore only selected bones) */
2027                 rest_pose(ob->pose);
2028         }
2029         
2030         DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
2031         BIF_undo_push("Clear User Transform");
2032 }
2033