RNA
[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  *
25  * ***** END GPL LICENSE BLOCK *****
26  * support for animation modes - Reevan McKay
27  */
28
29 #include <stdlib.h>
30 #include <stddef.h>
31 #include <string.h>
32
33 #include "MEM_guardedalloc.h"
34
35 #include "BLI_arithb.h"
36 #include "BLI_blenlib.h"
37 #include "BLI_dynstr.h"
38
39 #include "DNA_anim_types.h"
40 #include "DNA_action_types.h"
41 #include "DNA_armature_types.h"
42 #include "DNA_constraint_types.h"
43 #include "DNA_curve_types.h"
44 #include "DNA_mesh_types.h"
45 #include "DNA_meshdata_types.h"
46 #include "DNA_modifier_types.h"
47 #include "DNA_object_types.h"
48 #include "DNA_scene_types.h"
49 #include "DNA_screen_types.h"
50 #include "DNA_view3d_types.h"
51 #include "DNA_userdef_types.h"
52
53 #include "BKE_action.h"
54 #include "BKE_armature.h"
55 #include "BKE_blender.h"
56 #include "BKE_context.h"
57 #include "BKE_constraint.h"
58 #include "BKE_deform.h"
59 #include "BKE_depsgraph.h"
60 #include "BKE_displist.h"
61 #include "BKE_fcurve.h"
62 #include "BKE_global.h"
63 #include "BKE_modifier.h"
64 #include "BKE_object.h"
65 #include "BKE_utildefines.h"
66
67 #include "BIF_transform.h" /* for autokey TFM_TRANSLATION, etc */
68 #include "BIF_gl.h"
69
70 #include "RNA_access.h"
71 #include "RNA_define.h"
72
73 #include "WM_api.h"
74 #include "WM_types.h"
75
76 #include "ED_armature.h"
77 #include "ED_anim_api.h"
78 #include "ED_keyframing.h"
79 #include "ED_object.h"
80 #include "ED_mesh.h"
81 #include "ED_screen.h"
82 #include "ED_view3d.h"
83
84 #include "armature_intern.h"
85
86 /* ************* XXX *************** */
87 static int movetolayer_short_buts() {return 1;}
88 static int okee() {return 0;}
89 static int pupmenu() {return 0;}
90 static void waitcursor() {};
91 static void error() {};
92 static void BIF_undo_push() {}
93 static void countall() {}
94 static void add_constraint() {}
95 static void autokeyframe_pose_cb_func() {}
96 /* ************* XXX *************** */
97
98 /* This function is used to indicate that a bone is selected and needs keyframes inserted */
99 void set_pose_keys (Object *ob)
100 {
101         bArmature *arm= ob->data;
102         bPoseChannel *chan;
103
104         if (ob->pose){
105                 for (chan=ob->pose->chanbase.first; chan; chan=chan->next){
106                         Bone *bone= chan->bone;
107                         if ((bone) && (bone->flag & BONE_SELECTED) && (arm->layer & bone->layer))
108                                 chan->flag |= POSE_KEY; 
109                         else
110                                 chan->flag &= ~POSE_KEY;
111                 }
112         }
113 }
114
115 /* This function is used to process the necessary updates for */
116 void ED_armature_enter_posemode(bContext *C, Base *base)
117 {
118         Object *ob= base->object;
119         
120         if (ob->id.lib){
121                 error ("Can't pose libdata");
122                 return;
123         }
124         
125         switch (ob->type){
126                 case OB_ARMATURE:
127                         
128                         ob->flag |= OB_POSEMODE;
129                         base->flag= ob->flag;
130                         
131                         WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_MODE_POSE, NULL);
132                         
133                         break;
134                 default:
135                         return;
136         }
137         ED_view3d_exit_paint_modes(C);
138 }
139
140 void ED_armature_exit_posemode(bContext *C, Base *base)
141 {
142         if(base) {
143                 Object *ob= base->object;
144                 
145                 ob->flag &= ~OB_POSEMODE;
146                 base->flag= ob->flag;
147                 
148                 WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_MODE_OBJECT, NULL);
149         }       
150 }
151
152 /* if a selected or active bone is protected, throw error (oonly if warn==1) and return 1 */
153 /* only_selected==1 : the active bone is allowed to be protected */
154 static short pose_has_protected_selected(Object *ob, short only_selected, short warn)
155 {
156         /* check protection */
157         if (ob->proxy) {
158                 bPoseChannel *pchan;
159                 bArmature *arm= ob->data;
160                 
161                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
162                         if (pchan->bone && (pchan->bone->layer & arm->layer)) {
163                                 if (pchan->bone->layer & arm->layer_protected) {
164                                         if (only_selected && (pchan->bone->flag & BONE_ACTIVE));
165                                         else if (pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) 
166                                            break;
167                                 }
168                         }
169                 }
170                 if (pchan) {
171                         if (warn) error("Cannot change Proxy protected bones");
172                         return 1;
173                 }
174         }
175         return 0;
176 }
177
178 /* only for real IK, not for auto-IK */
179 int ED_pose_channel_in_IK_chain(Object *ob, bPoseChannel *pchan)
180 {
181         bConstraint *con;
182         Bone *bone;
183         
184         /* No need to check if constraint is active (has influence),
185          * since all constraints with CONSTRAINT_IK_AUTO are active */
186         for(con= pchan->constraints.first; con; con= con->next) {
187                 if(con->type==CONSTRAINT_TYPE_KINEMATIC) {
188                         bKinematicConstraint *data= con->data;
189                         if((data->flag & CONSTRAINT_IK_AUTO)==0)
190                                 return 1;
191                 }
192         }
193         for(bone= pchan->bone->childbase.first; bone; bone= bone->next) {
194                 pchan= get_pose_channel(ob->pose, bone->name);
195                 if(pchan && ED_pose_channel_in_IK_chain(ob, pchan))
196                         return 1;
197         }
198         return 0;
199 }
200
201 /* ********************************************** */
202
203 /* For the object with pose/action: create path curves for selected bones 
204  * This recalculates the WHOLE path within the pchan->pathsf and pchan->pathef range
205  */
206 void pose_calculate_path(bContext *C, Scene *scene, Object *ob)
207 {
208         bArmature *arm;
209         bPoseChannel *pchan;
210         Base *base;
211         float *fp;
212         int cfra;
213         int sfra, efra;
214         
215         if (ob==NULL || ob->pose==NULL)
216                 return;
217         arm= ob->data;
218         
219         /* version patch for older files here (do_versions patch too complicated) */
220         if ((arm->pathsf == 0) || (arm->pathef == 0)) {
221                 arm->pathsf = SFRA;
222                 arm->pathef = EFRA;
223         }
224         if (arm->pathsize == 0) {
225                 arm->pathsize = 1;
226         }
227         
228         /* set frame values */
229         cfra= CFRA;
230         sfra = arm->pathsf;
231         efra = arm->pathef;
232         if (efra <= sfra) {
233                 error("Can't calculate paths when pathlen <= 0");
234                 return;
235         }
236         
237         waitcursor(1);
238         
239         /* hack: for unsaved files, set OB_RECALC so that paths can get calculated */
240         if ((ob->recalc & OB_RECALC)==0) {
241                 ob->recalc |= OB_RECALC;
242                 ED_anim_object_flush_update(C, ob);
243         }
244         else
245                 ED_anim_object_flush_update(C, ob);
246         
247         
248         /* malloc the path blocks */
249         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
250                 if ((pchan->bone) && (pchan->bone->flag & BONE_SELECTED)) {
251                         if (arm->layer & pchan->bone->layer) {
252                                 pchan->pathlen= efra-sfra+1;
253                                 pchan->pathsf= sfra;
254                                 pchan->pathef= efra+1;
255                                 if (pchan->path)
256                                         MEM_freeN(pchan->path);
257                                 pchan->path= MEM_callocN(3*pchan->pathlen*sizeof(float), "pchan path");
258                         }
259                 }
260         }
261         
262         for (CFRA=sfra; CFRA<=efra; CFRA++) {
263                 /* do all updates */
264                 for (base= FIRSTBASE; base; base= base->next) {
265                         if (base->object->recalc) {
266                                 int temp= base->object->recalc;
267                                 object_handle_update(scene, base->object);
268                                 base->object->recalc= temp;
269                         }
270                 }
271                 
272                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
273                         if ((pchan->bone) && (pchan->bone->flag & BONE_SELECTED)) {
274                                 if (arm->layer & pchan->bone->layer) {
275                                         if (pchan->path) {
276                                                 fp= pchan->path+3*(CFRA-sfra);
277                                                 
278                                                 if (arm->pathflag & ARM_PATH_HEADS) { 
279                                                         VECCOPY(fp, pchan->pose_head);
280                                                 }
281                                                 else {
282                                                         VECCOPY(fp, pchan->pose_tail);
283                                                 }
284                                                 
285                                                 Mat4MulVecfl(ob->obmat, fp);
286                                         }
287                                 }
288                         }
289                 }
290         }
291         
292         waitcursor(0);
293         
294         CFRA= cfra;
295 }
296
297 /* For the object with pose/action: update paths for those that have got them
298  * This should selectively update paths that exist...
299  */
300 void pose_recalculate_paths(bContext *C, Scene *scene, Object *ob)
301 {
302         bArmature *arm;
303         bPoseChannel *pchan;
304         Base *base;
305         float *fp;
306         int cfra;
307         int sfra, efra;
308         
309         if (ob==NULL || ob->pose==NULL)
310                 return;
311         arm= ob->data;
312         
313         /* set frame values */
314         cfra = CFRA;
315         sfra = efra = cfra; 
316         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
317                 if ((pchan->bone) && (arm->layer & pchan->bone->layer)) {
318                         if (pchan->path) {
319                                 /* if the pathsf and pathef aren't initialised, abort! */
320                                 if (ELEM(0, pchan->pathsf, pchan->pathef))      
321                                         return;
322                                 
323                                 /* try to increase area to do (only as much as needed) */
324                                 sfra= MIN2(sfra, pchan->pathsf);
325                                 efra= MAX2(efra, pchan->pathef);
326                         }
327                 }
328         }
329         if (efra <= sfra) return;
330         
331         waitcursor(1);
332         
333         /* hack: for unsaved files, set OB_RECALC so that paths can get calculated */
334         if ((ob->recalc & OB_RECALC)==0) {
335                 ob->recalc |= OB_RECALC;
336                 ED_anim_object_flush_update(C, ob);
337         }
338         else
339                 ED_anim_object_flush_update(C, ob);
340         
341         for (CFRA=sfra; CFRA<=efra; CFRA++) {
342                 /* do all updates */
343                 for (base= FIRSTBASE; base; base= base->next) {
344                         if (base->object->recalc) {
345                                 int temp= base->object->recalc;
346                                 object_handle_update(scene, base->object);
347                                 base->object->recalc= temp;
348                         }
349                 }
350                 
351                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
352                         if ((pchan->bone) && (arm->layer & pchan->bone->layer)) {
353                                 if (pchan->path) {
354                                         /* only update if:
355                                          *      - in range of this pchan's existing path
356                                          *      - ... insert evil filtering/optimising conditions here...
357                                          */
358                                         if (IN_RANGE(CFRA, pchan->pathsf, pchan->pathef)) {
359                                                 fp= pchan->path+3*(CFRA-sfra);
360                                                 
361                                                 if (arm->pathflag & ARM_PATH_HEADS) { 
362                                                         VECCOPY(fp, pchan->pose_head);
363                                                 }
364                                                 else {
365                                                         VECCOPY(fp, pchan->pose_tail);
366                                                 }
367                                                 
368                                                 Mat4MulVecfl(ob->obmat, fp);
369                                         }
370                                 }
371                         }
372                 }
373         }
374         
375         waitcursor(0);
376         
377         CFRA= cfra;
378         ob->pose->flag &= ~POSE_RECALCPATHS;
379 }
380
381 /* for the object with pose/action: clear path curves for selected bones only */
382 void pose_clear_paths(Object *ob)
383 {
384         bPoseChannel *pchan;
385         
386         if (ob==NULL || ob->pose==NULL)
387                 return;
388         
389         /* free the path blocks */
390         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
391                 if ((pchan->bone) && (pchan->bone->flag & BONE_SELECTED)) {
392                         if (pchan->path) {
393                                 MEM_freeN(pchan->path);
394                                 pchan->path= NULL;
395                         }
396                 }
397         }
398         
399 }
400
401
402
403 void pose_select_constraint_target(Scene *scene)
404 {
405         Object *obedit= scene->obedit; // XXX context
406         Object *ob= OBACT;
407         bArmature *arm= ob->data;
408         bPoseChannel *pchan;
409         bConstraint *con;
410         
411         /* paranoia checks */
412         if (!ob && !ob->pose) return;
413         if (ob==obedit || (ob->flag & OB_POSEMODE)==0) return;
414         
415         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
416                 if (arm->layer & pchan->bone->layer) {
417                         if (pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
418                                 for (con= pchan->constraints.first; con; con= con->next) {
419                                         bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
420                                         ListBase targets = {NULL, NULL};
421                                         bConstraintTarget *ct;
422                                         
423                                         if (cti && cti->get_constraint_targets) {
424                                                 cti->get_constraint_targets(con, &targets);
425                                                 
426                                                 for (ct= targets.first; ct; ct= ct->next) {
427                                                         if ((ct->tar == ob) && (ct->subtarget[0])) {
428                                                                 bPoseChannel *pchanc= get_pose_channel(ob->pose, ct->subtarget);
429                                                                 if(pchanc)
430                                                                         pchanc->bone->flag |= BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL;
431                                                         }
432                                                 }
433                                                 
434                                                 if (cti->flush_constraint_targets)
435                                                         cti->flush_constraint_targets(con, &targets, 1);
436                                         }
437                                 }
438                         }
439                 }
440         }
441         
442         BIF_undo_push("Select constraint target");
443
444 }
445
446 /* ******************* select hierarchy operator ************* */
447
448 static int pose_select_hierarchy_exec(bContext *C, wmOperator *op)
449 {
450         Object *ob= CTX_data_active_object(C);
451         bArmature *arm= ob->data;
452         bPoseChannel *pchan;
453         Bone *curbone, *pabone, *chbone;
454         int direction = RNA_enum_get(op->ptr, "direction");
455         int add_to_sel = RNA_boolean_get(op->ptr, "extend");
456         
457         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
458                 curbone= pchan->bone;
459                 
460                 if (arm->layer & curbone->layer) {
461                         if (curbone->flag & (BONE_ACTIVE)) {
462                                 if (direction == BONE_SELECT_PARENT) {
463                                 
464                                         if (pchan->parent == NULL) continue;
465                                         else pabone= pchan->parent->bone;
466                                         
467                                         if ((arm->layer & pabone->layer) && !(pabone->flag & BONE_HIDDEN_P)) {
468                                                 
469                                                 if (!add_to_sel) curbone->flag &= ~BONE_SELECTED;
470                                                 curbone->flag &= ~BONE_ACTIVE;
471                                                 pabone->flag |= (BONE_ACTIVE|BONE_SELECTED);
472                                                 
473                                                 // XXX notifiers need to be sent to other editors to update
474                                                 break;
475                                         }
476                                 } else { // BONE_SELECT_CHILD
477                                 
478                                         if (pchan->child == NULL) continue;
479                                         else chbone = pchan->child->bone;
480                                         
481                                         if ((arm->layer & chbone->layer) && !(chbone->flag & BONE_HIDDEN_P)) {
482                                         
483                                                 if (!add_to_sel) curbone->flag &= ~BONE_SELECTED;
484                                                 curbone->flag &= ~BONE_ACTIVE;
485                                                 chbone->flag |= (BONE_ACTIVE|BONE_SELECTED);
486                                                 
487                                                 // XXX notifiers need to be sent to other editors to update
488                                                 break;
489                                         }
490                                 }
491                         }
492                 }
493         }
494
495         WM_event_add_notifier(C, NC_OBJECT|ND_BONE_SELECT, ob);
496
497         return OPERATOR_FINISHED;
498 }
499
500 void POSE_OT_select_hierarchy(wmOperatorType *ot)
501 {
502         static EnumPropertyItem direction_items[]= {
503         {BONE_SELECT_PARENT, "PARENT", 0, "Select Parent", ""},
504         {BONE_SELECT_CHILD, "CHILD", 0, "Select Child", ""},
505         {0, NULL, 0, NULL, NULL}
506         };
507         
508         /* identifiers */
509         ot->name= "Select Hierarchy";
510         ot->idname= "POSE_OT_select_hierarchy";
511         
512         /* api callbacks */
513         ot->exec= pose_select_hierarchy_exec;
514         ot->poll= ED_operator_posemode;
515         
516         /* flags */
517         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
518         
519         /* props */
520         RNA_def_enum(ot->srna, "direction", direction_items,
521                                  BONE_SELECT_PARENT, "Direction", "");
522         RNA_def_boolean(ot->srna, "extend", 0, "Add to Selection", "");
523         
524 }
525
526
527 void pose_add_IK(Scene *scene)
528 {
529         Object *obedit= scene->obedit; // XXX context
530         Object *ob= OBACT;
531         
532         /* paranoia checks */
533         if(!ob && !ob->pose) return;
534         if(ob==obedit || (ob->flag & OB_POSEMODE)==0) return;
535         
536         add_constraint(1);      /* 1 means only IK */
537 }
538
539 /* context: all selected channels */
540 void pose_clear_IK(Scene *scene)
541 {
542         Object *obedit= scene->obedit; // XXX context
543         Object *ob= OBACT;
544         bArmature *arm= ob->data;
545         bPoseChannel *pchan;
546         bConstraint *con;
547         bConstraint *next;
548         
549         /* paranoia checks */
550         if(!ob && !ob->pose) return;
551         if(ob==obedit || (ob->flag & OB_POSEMODE)==0) return;
552         
553         if(pose_has_protected_selected(ob, 0, 1))
554                 return;
555         
556         if(okee("Remove IK constraint(s)")==0) return;
557
558         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
559                 if(arm->layer & pchan->bone->layer) {
560                         if(pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
561                                 
562                                 for(con= pchan->constraints.first; con; con= next) {
563                                         next= con->next;
564                                         if(con->type==CONSTRAINT_TYPE_KINEMATIC) {
565                                                 BLI_remlink(&pchan->constraints, con);
566                                                 free_constraint_data(con);
567                                                 MEM_freeN(con);
568                                         }
569                                 }
570                                 pchan->constflag &= ~(PCHAN_HAS_IK|PCHAN_HAS_TARGET);
571                         }
572                 }
573         }
574         
575         DAG_object_flush_update(scene, ob, OB_RECALC_DATA);     // and all its relations
576         
577         BIF_undo_push("Remove IK constraint(s)");
578 }
579
580 void pose_clear_constraints(Scene *scene)
581 {
582         Object *obedit= scene->obedit; // XXX context
583         Object *ob= OBACT;
584         bArmature *arm= ob->data;
585         bPoseChannel *pchan;
586         
587         /* paranoia checks */
588         if(!ob && !ob->pose) return;
589         if(ob==obedit || (ob->flag & OB_POSEMODE)==0) return;
590         
591         if(pose_has_protected_selected(ob, 0, 1))
592                 return;
593         
594         if(okee("Remove Constraints")==0) return;
595         
596         /* find active */
597         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
598                 if(arm->layer & pchan->bone->layer) {
599                         if(pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
600                                 free_constraints(&pchan->constraints);
601                                 pchan->constflag= 0;
602                         }
603                 }
604         }
605         
606         DAG_object_flush_update(scene, ob, OB_RECALC_DATA);     // and all its relations
607         
608         BIF_undo_push("Remove Constraint(s)");
609         
610 }
611
612
613 void pose_copy_menu(Scene *scene)
614 {
615         Object *obedit= scene->obedit; // XXX context
616         Object *ob= OBACT;
617         bArmature *arm= ob->data;
618         bPoseChannel *pchan, *pchanact;
619         short nr=0;
620         int i=0;
621         
622         /* paranoia checks */
623         if (ELEM(NULL, ob, ob->pose)) return;
624         if ((ob==obedit) || (ob->flag & OB_POSEMODE)==0) return;
625         
626         /* find active */
627         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
628                 if (pchan->bone->flag & BONE_ACTIVE) 
629                         break;
630         }
631         
632         if (pchan==NULL) return;
633         pchanact= pchan;
634         
635         /* if proxy-protected bones selected, some things (such as locks + displays) shouldn't be changable, 
636          * but for constraints (just add local constraints)
637          */
638         if (pose_has_protected_selected(ob, 1, 0)) {
639                 i= BLI_countlist(&(pchanact->constraints)); /* if there are 24 or less, allow for the user to select constraints */
640                 if (i < 25)
641                         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");
642                 else
643                         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");
644         }
645         else {
646                 i= BLI_countlist(&(pchanact->constraints)); /* if there are 24 or less, allow for the user to select constraints */
647                 if (i < 25)
648                         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");
649                 else
650                         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");
651         }
652         
653         if (nr <= 0) 
654                 return;
655         
656         if (nr != 5)  {
657                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
658                         if ( (arm->layer & pchan->bone->layer) &&
659                                  (pchan->bone->flag & BONE_SELECTED) &&
660                                  (pchan != pchanact) ) 
661                         {
662                                 switch (nr) {
663                                         case 1: /* Local Location */
664                                                 VECCOPY(pchan->loc, pchanact->loc);
665                                                 break;
666                                         case 2: /* Local Rotation */
667                                                 QUATCOPY(pchan->quat, pchanact->quat);
668                                                 break;
669                                         case 3: /* Local Size */
670                                                 VECCOPY(pchan->size, pchanact->size);
671                                                 break;
672                                         case 4: /* All Constraints */
673                                         {
674                                                 ListBase tmp_constraints = {NULL, NULL};
675                                                 
676                                                 /* copy constraints to tmpbase and apply 'local' tags before 
677                                                  * appending to list of constraints for this channel
678                                                  */
679                                                 copy_constraints(&tmp_constraints, &pchanact->constraints);
680                                                 if ((ob->proxy) && (pchan->bone->layer & arm->layer_protected)) {
681                                                         bConstraint *con;
682                                                         
683                                                         /* add proxy-local tags */
684                                                         for (con= tmp_constraints.first; con; con= con->next)
685                                                                 con->flag |= CONSTRAINT_PROXY_LOCAL;
686                                                 }
687                                                 addlisttolist(&pchan->constraints, &tmp_constraints);
688                                                 
689                                                 /* update flags (need to add here, not just copy) */
690                                                 pchan->constflag |= pchanact->constflag;
691                                                 
692                                                 if (ob->pose)
693                                                         ob->pose->flag |= POSE_RECALC;
694                                         }
695                                                 break;
696                                         case 6: /* Transform Locks */
697                                                 pchan->protectflag = pchanact->protectflag;
698                                                 break;
699                                         case 7: /* IK (DOF) settings */
700                                         {
701                                                 pchan->ikflag = pchanact->ikflag;
702                                                 VECCOPY(pchan->limitmin, pchanact->limitmin);
703                                                 VECCOPY(pchan->limitmax, pchanact->limitmax);
704                                                 VECCOPY(pchan->stiffness, pchanact->stiffness);
705                                                 pchan->ikstretch= pchanact->ikstretch;
706                                         }
707                                                 break;
708                                         case 8: /* Custom Bone Shape */
709                                                 pchan->custom = pchanact->custom;
710                                                 break;
711                                         case 9: /* Visual Location */
712                                                 armature_loc_pose_to_bone(pchan, pchanact->pose_mat[3], pchan->loc);
713                                                 break;
714                                         case 10: /* Visual Rotation */
715                                         {
716                                                 float delta_mat[4][4], quat[4];
717                                                 
718                                                 armature_mat_pose_to_bone(pchan, pchanact->pose_mat, delta_mat);
719                                                 Mat4ToQuat(delta_mat, quat);
720                                                 QUATCOPY(pchan->quat, quat);
721                                         }
722                                                 break;
723                                         case 11: /* Visual Size */
724                                         {
725                                                 float delta_mat[4][4], size[4];
726                                                 
727                                                 armature_mat_pose_to_bone(pchan, pchanact->pose_mat, delta_mat);
728                                                 Mat4ToSize(delta_mat, size);
729                                                 VECCOPY(pchan->size, size);
730                                         }
731                                 }
732                         }
733                 }
734         } 
735         else { /* constraints, optional (note: max we can have is 24 constraints) */
736                 bConstraint *con, *con_back;
737                 int const_toggle[24];
738                 ListBase const_copy = {NULL, NULL};
739                 
740                 BLI_duplicatelist(&const_copy, &(pchanact->constraints));
741                 
742                 /* build the puplist of constraints */
743                 for (con = pchanact->constraints.first, i=0; con; con=con->next, i++){
744                         const_toggle[i]= 1;
745 //                      add_numbut(i, TOG|INT, con->name, 0, 0, &(const_toggle[i]), "");
746                 }
747                 
748 //              if (!do_clever_numbuts("Select Constraints", i, REDRAW)) {
749 //                      BLI_freelistN(&const_copy);
750 //                      return;
751 //              }
752                 
753                 /* now build a new listbase from the options selected */
754                 for (i=0, con=const_copy.first; con; i++) {
755                         /* if not selected, free/remove it from the list */
756                         if (!const_toggle[i]) {
757                                 con_back= con->next;
758                                 BLI_freelinkN(&const_copy, con);
759                                 con= con_back;
760                         } 
761                         else
762                                 con= con->next;
763                 }
764                 
765                 /* Copy the temo listbase to the selected posebones */
766                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
767                         if ( (arm->layer & pchan->bone->layer) &&
768                                  (pchan->bone->flag & BONE_SELECTED) &&
769                                  (pchan!=pchanact) ) 
770                         {
771                                 ListBase tmp_constraints = {NULL, NULL};
772                                 
773                                 /* copy constraints to tmpbase and apply 'local' tags before 
774                                  * appending to list of constraints for this channel
775                                  */
776                                 copy_constraints(&tmp_constraints, &const_copy);
777                                 if ((ob->proxy) && (pchan->bone->layer & arm->layer_protected)) {
778                                         bConstraint *con;
779                                         
780                                         /* add proxy-local tags */
781                                         for (con= tmp_constraints.first; con; con= con->next)
782                                                 con->flag |= CONSTRAINT_PROXY_LOCAL;
783                                 }
784                                 addlisttolist(&pchan->constraints, &tmp_constraints);
785                                 
786                                 /* update flags (need to add here, not just copy) */
787                                 pchan->constflag |= pchanact->constflag;
788                         }
789                 }
790                 BLI_freelistN(&const_copy);
791                 update_pose_constraint_flags(ob->pose); /* we could work out the flags but its simpler to do this */
792                 
793                 if (ob->pose)
794                         ob->pose->flag |= POSE_RECALC;
795         }
796         
797         DAG_object_flush_update(scene, ob, OB_RECALC_DATA);     // and all its relations
798         
799         BIF_undo_push("Copy Pose Attributes");
800         
801 }
802
803 /* ******************** copy/paste pose ********************** */
804
805 static bPose    *g_posebuf=NULL;
806
807 void free_posebuf(void) 
808 {
809         if (g_posebuf) {
810                 // was copied without constraints
811                 BLI_freelistN (&g_posebuf->chanbase);
812                 MEM_freeN (g_posebuf);
813         }
814         g_posebuf=NULL;
815 }
816
817 void copy_posebuf (Scene *scene)
818 {
819         Object *ob= OBACT;
820
821         if (!ob || !ob->pose){
822                 error ("No Pose");
823                 return;
824         }
825
826         free_posebuf();
827         
828         set_pose_keys(ob);  // sets chan->flag to POSE_KEY if bone selected
829         copy_pose(&g_posebuf, ob->pose, 0);
830
831 }
832
833 void paste_posebuf (Scene *scene, int flip)
834 {
835         Object *ob= OBACT;
836         bPoseChannel *chan, *pchan;
837         float eul[4];
838         char name[32];
839         
840         if (!ob || !ob->pose)
841                 return;
842
843         if (!g_posebuf){
844                 error ("Copy buffer is empty");
845                 return;
846         }
847         
848         /*
849         // disabled until protected bones in proxies follow the rules everywhere else!
850         if(pose_has_protected_selected(ob, 1, 1))
851                 return;
852         */
853         
854         /* Safely merge all of the channels in this pose into
855         any existing pose */
856         for (chan=g_posebuf->chanbase.first; chan; chan=chan->next) {
857                 if (chan->flag & POSE_KEY) {
858                         BLI_strncpy(name, chan->name, sizeof(name));
859                         if (flip)
860                                 bone_flip_name (name, 0);               // 0 = don't strip off number extensions
861                                 
862                         /* only copy when channel exists, poses are not meant to add random channels to anymore */
863                         pchan= get_pose_channel(ob->pose, name);
864                         
865                         if (pchan) {
866                                 /* only loc rot size */
867                                 /* only copies transform info for the pose */
868                                 VECCOPY(pchan->loc, chan->loc);
869                                 VECCOPY(pchan->size, chan->size);
870                                 QUATCOPY(pchan->quat, chan->quat);
871                                 pchan->flag= chan->flag;
872                                 
873                                 if (flip) {
874                                         pchan->loc[0]*= -1;
875                                         
876                                         QuatToEul(pchan->quat, eul);
877                                         eul[1]*= -1;
878                                         eul[2]*= -1;
879                                         EulToQuat(eul, pchan->quat);
880                                 }
881                                 
882 #if 0 // XXX old animation system
883                                 if (autokeyframe_cfra_can_key(ob)) {
884                                         ID *id= &ob->id;
885                                         
886                                         /* Set keys on pose */
887                                         if (chan->flag & POSE_ROT) {
888                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_X, 0);
889                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_Y, 0);
890                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_Z, 0);
891                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_W, 0);
892                                         }
893                                         if (chan->flag & POSE_SIZE) {
894                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_SIZE_X, 0);
895                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_SIZE_Y, 0);
896                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_SIZE_Z, 0);
897                                         }
898                                         if (chan->flag & POSE_LOC) {
899                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_LOC_X, 0);
900                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_LOC_Y, 0);
901                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_LOC_Z, 0);
902                                         }
903                                         
904                                         /* clear any unkeyed tags */
905                                         if (chan->bone)
906                                                 chan->bone->flag &= ~BONE_UNKEYED;
907                                 }
908                                 else {
909                                         /* add unkeyed tags */
910                                         if (chan->bone)
911                                                 chan->bone->flag |= BONE_UNKEYED;
912                                 }
913 #endif // XXX old animation system
914                         }
915                 }
916         }
917
918         /* Update event for pose and deformation children */
919         DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
920         
921         if (IS_AUTOKEY_ON(scene)) {
922 // XXX          remake_action_ipos(ob->action);
923         }
924         else {
925                 /* need to trick depgraph, action is not allowed to execute on pose */
926                 where_is_pose(scene, ob);
927                 ob->recalc= 0;
928         }
929
930         BIF_undo_push("Paste Action Pose");
931 }
932
933 /* ********************************************** */
934
935 /* context weightpaint and deformer in posemode */
936 void pose_adds_vgroups(Scene *scene, Object *meshobj, int heatweights)
937 {
938 // XXX  extern VPaint Gwp;         /* from vpaint */
939         Object *poseobj= modifiers_isDeformedByArmature(meshobj);
940
941         if(poseobj==NULL || (poseobj->flag & OB_POSEMODE)==0) {
942                 error("The active object must have a deforming armature in pose mode");
943                 return;
944         }
945
946 // XXX  add_verts_to_dgroups(meshobj, poseobj, heatweights, (Gwp.flag & VP_MIRROR_X));
947
948         if(heatweights)
949                 BIF_undo_push("Apply Bone Heat Weights to Vertex Groups");
950         else
951                 BIF_undo_push("Apply Bone Envelopes to Vertex Groups");
952
953         
954         // and all its relations
955         DAG_object_flush_update(scene, meshobj, OB_RECALC_DATA);
956 }
957
958 /* ********************************************** */
959
960 /* adds a new pose-group */
961 void pose_add_posegroup (Scene *scene)
962 {
963         Object *ob= OBACT;
964         bPose *pose= (ob) ? ob->pose : NULL;
965         bActionGroup *grp;
966         
967         if (ELEM(NULL, ob, ob->pose))
968                 return;
969         
970         grp= MEM_callocN(sizeof(bActionGroup), "PoseGroup");
971         strcpy(grp->name, "Group");
972         BLI_addtail(&pose->agroups, grp);
973         BLI_uniquename(&pose->agroups, grp, "Group", '.', offsetof(bActionGroup, name), 32);
974         
975         pose->active_group= BLI_countlist(&pose->agroups);
976         
977         BIF_undo_push("Add Bone Group");
978         
979 }
980
981 /* Remove the active bone-group */
982 void pose_remove_posegroup (Scene *scene)
983 {
984         Object *ob= OBACT;
985         bPose *pose= (ob) ? ob->pose : NULL;
986         bActionGroup *grp = NULL;
987         bPoseChannel *pchan;
988         
989         /* sanity checks */
990         if (ELEM(NULL, ob, pose))
991                 return;
992         if (pose->active_group <= 0)
993                 return;
994         
995         /* get group to remove */
996         grp= BLI_findlink(&pose->agroups, pose->active_group-1);
997         if (grp) {
998                 /* adjust group references (the trouble of using indices!):
999                  *      - firstly, make sure nothing references it 
1000                  *      - also, make sure that those after this item get corrected
1001                  */
1002                 for (pchan= pose->chanbase.first; pchan; pchan= pchan->next) {
1003                         if (pchan->agrp_index == pose->active_group)
1004                                 pchan->agrp_index= 0;
1005                         else if (pchan->agrp_index > pose->active_group)
1006                                 pchan->agrp_index--;
1007                 }
1008                 
1009                 /* now, remove it from the pose */
1010                 BLI_freelinkN(&pose->agroups, grp);
1011                 pose->active_group= 0;
1012                 
1013                 BIF_undo_push("Remove Bone Group");
1014         }
1015         
1016 }
1017
1018 char *build_posegroups_menustr (bPose *pose, short for_pupmenu)
1019 {
1020         DynStr *pupds= BLI_dynstr_new();
1021         bActionGroup *grp;
1022         char *str;
1023         char buf[16];
1024         int i;
1025         
1026         /* add title first (and the "none" entry) */
1027         BLI_dynstr_append(pupds, "Bone Group%t|");
1028         if (for_pupmenu)
1029                 BLI_dynstr_append(pupds, "Add New%x0|");
1030         else
1031                 BLI_dynstr_append(pupds, "BG: [None]%x0|");
1032         
1033         /* loop through groups, adding them */
1034         for (grp= pose->agroups.first, i=1; grp; grp=grp->next, i++) {
1035                 if (for_pupmenu == 0)
1036                         BLI_dynstr_append(pupds, "BG: ");
1037                 BLI_dynstr_append(pupds, grp->name);
1038                 
1039                 sprintf(buf, "%%x%d", i);
1040                 BLI_dynstr_append(pupds, buf);
1041                 
1042                 if (grp->next)
1043                         BLI_dynstr_append(pupds, "|");
1044         }
1045         
1046         /* convert to normal MEM_malloc'd string */
1047         str= BLI_dynstr_get_cstring(pupds);
1048         BLI_dynstr_free(pupds);
1049         
1050         return str;
1051 }
1052
1053 /* Assign selected pchans to the bone group that the user selects */
1054 void pose_assign_to_posegroup (Scene *scene, short active)
1055 {
1056         Object *ob= OBACT;
1057         bArmature *arm= (ob) ? ob->data : NULL;
1058         bPose *pose= (ob) ? ob->pose : NULL;
1059         bPoseChannel *pchan;
1060         char *menustr;
1061         int nr;
1062         short done= 0;
1063         
1064         /* sanity checks */
1065         if (ELEM3(NULL, ob, pose, arm))
1066                 return;
1067
1068         /* get group to affect */
1069         if ((active==0) || (pose->active_group <= 0)) {
1070                 menustr= build_posegroups_menustr(pose, 1);
1071                 nr= 0; // XXX pupmenu_col(menustr, 20);
1072                 MEM_freeN(menustr);
1073                 
1074                 if (nr < 0) 
1075                         return;
1076                 else if (nr == 0) {
1077                         /* add new - note: this does an undo push and sets active group */
1078                         pose_add_posegroup(scene);
1079                 }
1080                 else
1081                         pose->active_group= nr;
1082         }
1083         
1084         /* add selected bones to group then */
1085         for (pchan= pose->chanbase.first; pchan; pchan= pchan->next) {
1086                 if ((pchan->bone->flag & BONE_SELECTED) && (pchan->bone->layer & arm->layer)) {
1087                         pchan->agrp_index= pose->active_group;
1088                         done= 1;
1089                 }
1090         }
1091         
1092         if (done)
1093                 BIF_undo_push("Add Bones To Group");
1094                 
1095 }
1096
1097 /* Remove selected pchans from their bone groups */
1098 void pose_remove_from_posegroups (Scene *scene)
1099 {
1100         Object *ob= OBACT;
1101         bArmature *arm= (ob) ? ob->data : NULL;
1102         bPose *pose= (ob) ? ob->pose : NULL;
1103         bPoseChannel *pchan;
1104         short done= 0;
1105         
1106         /* sanity checks */
1107         if (ELEM3(NULL, ob, pose, arm))
1108                 return;
1109         
1110         /* remove selected bones from their groups */
1111         for (pchan= pose->chanbase.first; pchan; pchan= pchan->next) {
1112                 if ((pchan->bone->flag & BONE_SELECTED) && (pchan->bone->layer & arm->layer)) {
1113                         if (pchan->agrp_index) {
1114                                 pchan->agrp_index= 0;
1115                                 done= 1;
1116                         }
1117                 }
1118         }
1119         
1120         if (done)
1121                 BIF_undo_push("Remove Bones From Groups");
1122                 
1123 }
1124
1125 /* Ctrl-G in 3D-View while in PoseMode */
1126 void pgroup_operation_with_menu (Scene *scene)
1127 {
1128         Object *ob= OBACT;
1129         bArmature *arm= (ob) ? ob->data : NULL;
1130         bPose *pose= (ob) ? ob->pose : NULL;
1131         bPoseChannel *pchan= NULL;
1132         int mode;
1133         
1134         /* sanity checks */
1135         if (ELEM3(NULL, ob, pose, arm))
1136                 return;
1137         
1138         /* check that something is selected */
1139         for (pchan= pose->chanbase.first; pchan; pchan= pchan->next) {
1140                 if ((pchan->bone->flag & BONE_SELECTED) && (pchan->bone->layer & arm->layer)) 
1141                         break;
1142         }
1143         if (pchan == NULL)
1144                 return;
1145         
1146         /* get mode of action */
1147         if (pchan)
1148                 mode= pupmenu("Bone Groups%t|Add Selected to Active Group%x1|Add Selected to Group%x2|%|Remove Selected From Groups%x3|Remove Active Group%x4");
1149         else
1150                 mode= pupmenu("Bone Groups%t|Add New Group%x5|Remove Active Group%x4");
1151                 
1152         /* handle mode */
1153         switch (mode) {
1154                 case 1:
1155                         pose_assign_to_posegroup(scene, 1);
1156                         break;
1157                 case 2:
1158                         pose_assign_to_posegroup(scene, 0);
1159                         break;
1160                 case 5:
1161                         pose_add_posegroup(scene);
1162                         break;
1163                 case 3:
1164                         pose_remove_from_posegroups(scene);
1165                         break;
1166                 case 4:
1167                         pose_remove_posegroup(scene);
1168                         break;
1169         }
1170 }
1171
1172 /* ********************************************** */
1173
1174 static short pose_select_same_group (Object *ob)
1175 {
1176         bPose *pose= (ob)? ob->pose : NULL;
1177         bArmature *arm= (ob)? ob->data : NULL;
1178         bPoseChannel *pchan, *chan;
1179         short changed= 0;
1180         
1181         if (ELEM3(NULL, ob, pose, arm))
1182                 return 0;
1183         
1184         /* loop in loop... bad and slow! */
1185         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1186                 if (arm->layer & pchan->bone->layer) {
1187                         if (pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
1188                                 
1189                                 /* only if group matches (and is not selected or current bone) */
1190                                 for (chan= ob->pose->chanbase.first; chan; chan= chan->next) {
1191                                         if (arm->layer & chan->bone->layer) {
1192                                                 if (pchan->agrp_index == chan->agrp_index) {
1193                                                         chan->bone->flag |= BONE_SELECTED;
1194                                                         changed= 1;
1195                                                 }
1196                                         }
1197                                 }
1198                                 
1199                         }
1200                 }
1201         }
1202         
1203         return changed;
1204 }
1205
1206 static short pose_select_same_layer (Object *ob)
1207 {
1208         bPose *pose= (ob)? ob->pose : NULL;
1209         bArmature *arm= (ob)? ob->data : NULL;
1210         bPoseChannel *pchan;
1211         short layers= 0, changed= 0;
1212         
1213         if (ELEM3(NULL, ob, pose, arm))
1214                 return 0;
1215         
1216         /* figure out what bones are selected */
1217         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1218                 if (arm->layer & pchan->bone->layer) {
1219                         if (pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
1220                                 layers |= pchan->bone->layer;
1221                         }
1222                 }
1223         }
1224         if (layers == 0) 
1225                 return 0;
1226                 
1227         /* select bones that are on same layers as layers flag */
1228         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1229                 if (arm->layer & pchan->bone->layer) {
1230                         if (layers & pchan->bone->layer) {
1231                                 pchan->bone->flag |= BONE_SELECTED;
1232                                 changed= 1;
1233                         }
1234                 }
1235         }
1236         
1237         return changed;
1238 }
1239
1240 void pose_select_grouped (Scene *scene, short nr)
1241 {
1242         short changed = 0;
1243         
1244         if (nr == 1)            changed= pose_select_same_group(OBACT);
1245         else if (nr == 2)       changed= pose_select_same_layer(OBACT);
1246         
1247         if (changed) {
1248                 countall();
1249                 BIF_undo_push("Select Grouped");
1250         }
1251 }
1252
1253 /* Shift-G in 3D-View while in PoseMode */
1254 void pose_select_grouped_menu (Scene *scene)
1255 {
1256         short nr;
1257         
1258         /* here we go */
1259         nr= pupmenu("Select Grouped%t|In Same Group%x1|In Same Layer%x2");
1260         pose_select_grouped(scene, nr);
1261 }
1262
1263 /* ********************************************** */
1264
1265 /* context active object */
1266 void pose_flip_names(Scene *scene)
1267 {
1268         Object *obedit= scene->obedit; // XXX context
1269         Object *ob= OBACT;
1270         bArmature *arm= ob->data;
1271         bPoseChannel *pchan;
1272         char newname[32];
1273         
1274         /* paranoia checks */
1275         if(!ob && !ob->pose) return;
1276         if(ob==obedit || (ob->flag & OB_POSEMODE)==0) return;
1277         
1278         if(pose_has_protected_selected(ob, 0, 1))
1279                 return;
1280         
1281         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1282                 if(arm->layer & pchan->bone->layer) {
1283                         if(pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
1284                                 BLI_strncpy(newname, pchan->name, sizeof(newname));
1285                                 bone_flip_name(newname, 1);     // 1 = do strip off number extensions
1286                                 armature_bone_rename(ob, pchan->name, newname);
1287                         }
1288                 }
1289         }
1290         
1291         BIF_undo_push("Flip names");
1292 }
1293
1294 /* context active object */
1295 void pose_autoside_names(Scene *scene, short axis)
1296 {
1297         Object *obedit= scene->obedit; // XXX context
1298         Object *ob= OBACT;
1299         bArmature *arm= ob->data;
1300         bPoseChannel *pchan;
1301         char newname[32];
1302         
1303         /* paranoia checks */
1304         if (ELEM(NULL, ob, ob->pose)) return;
1305         if (ob==obedit || (ob->flag & OB_POSEMODE)==0) return;
1306         
1307         if (pose_has_protected_selected(ob, 0, 1))
1308                 return;
1309         
1310         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1311                 if(arm->layer & pchan->bone->layer) {
1312                         if(pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
1313                                 BLI_strncpy(newname, pchan->name, sizeof(newname));
1314                                 bone_autoside_name(newname, 1, axis, pchan->bone->head[axis], pchan->bone->tail[axis]);
1315                                 armature_bone_rename(ob, pchan->name, newname);
1316                         }
1317                 }
1318         }
1319         
1320         BIF_undo_push("Flip names");
1321 }
1322
1323 /* context active object, or weightpainted object with armature in posemode */
1324 void pose_activate_flipped_bone(Scene *scene)
1325 {
1326         Object *ob= OBACT;
1327         bArmature *arm= ob->data;
1328         
1329         if(ob==NULL) return;
1330
1331         if(G.f & G_WEIGHTPAINT) {
1332                 ob= modifiers_isDeformedByArmature(ob);
1333         }
1334         if(ob && (ob->flag & OB_POSEMODE)) {
1335                 bPoseChannel *pchan, *pchanf;
1336                 
1337                 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1338                         if(arm->layer & pchan->bone->layer) {
1339                                 if(pchan->bone->flag & BONE_ACTIVE)
1340                                         break;
1341                         }
1342                 }
1343                 if(pchan) {
1344                         char name[32];
1345                         
1346                         BLI_strncpy(name, pchan->name, 32);
1347                         bone_flip_name(name, 1);        // 0 = do not strip off number extensions
1348                         
1349                         pchanf= get_pose_channel(ob->pose, name);
1350                         if(pchanf && pchanf!=pchan) {
1351                                 pchan->bone->flag &= ~(BONE_SELECTED|BONE_ACTIVE);
1352                                 pchanf->bone->flag |= (BONE_SELECTED|BONE_ACTIVE);
1353                         
1354                                 /* in weightpaint we select the associated vertex group too */
1355                                 if(G.f & G_WEIGHTPAINT) {
1356                                         vertexgroup_select_by_name(OBACT, name);
1357                                         DAG_object_flush_update(scene, OBACT, OB_RECALC_DATA);
1358                                 }
1359                                 
1360                                 // XXX notifiers need to be sent to other editors to update
1361                                 
1362                         }                       
1363                 }
1364         }
1365 }
1366
1367 /* This function pops up the move-to-layer popup widgets when the user
1368  * presses either SHIFT-MKEY or MKEY in PoseMode OR EditMode (for Armatures)
1369  */
1370 void pose_movetolayer(Scene *scene)
1371 {
1372         Object *obedit= scene->obedit; // XXX context
1373         Object *ob= OBACT;
1374         bArmature *arm;
1375         short lay= 0;
1376         short shift= 0; // XXX
1377         
1378         if (ob==NULL) return;
1379         arm= ob->data;
1380         
1381         if (shift) {
1382                 /* armature layers */
1383                 lay= arm->layer;
1384                 if ( movetolayer_short_buts(&lay, "Armature Layers")==0 ) return;
1385                 if (lay==0) return;
1386                 arm->layer= lay;
1387                 if(ob->pose)
1388                         ob->pose->proxy_layer= lay;
1389                 
1390         }
1391         else if (obedit) {
1392                 /* the check for editbone layer moving needs to occur before posemode one to work */
1393                 EditBone *ebo;
1394                 EditBone *flipBone;
1395                 
1396                 for (ebo= arm->edbo->first; ebo; ebo= ebo->next) {
1397                         if (arm->layer & ebo->layer) {
1398                                 if (ebo->flag & BONE_SELECTED)
1399                                         lay |= ebo->layer;
1400                         }
1401                 }
1402                 if (lay==0) return;
1403                 
1404                 if ( movetolayer_short_buts(&lay, "Bone Layers")==0 ) return;
1405                 if (lay==0) return;
1406                 
1407                 for (ebo= arm->edbo->first; ebo; ebo= ebo->next) {
1408                         if (arm->layer & ebo->layer) {
1409                                 if (ebo->flag & BONE_SELECTED) {
1410                                         ebo->layer= lay;
1411                                         if (arm->flag & ARM_MIRROR_EDIT) {
1412                                                 flipBone = ED_armature_bone_get_mirrored(arm->edbo, ebo);
1413                                                 if (flipBone)
1414                                                         flipBone->layer = lay;
1415                                         }
1416                                 }
1417                         }
1418                 }
1419                 
1420                 BIF_undo_push("Move Bone Layer");
1421         }
1422         else if (ob->flag & OB_POSEMODE) {
1423                 /* pose-channel layers */
1424                 bPoseChannel *pchan;
1425                 
1426                 if (pose_has_protected_selected(ob, 0, 1))
1427                         return;
1428                 
1429                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1430                         if (arm->layer & pchan->bone->layer) {
1431                                 if (pchan->bone->flag & BONE_SELECTED)
1432                                         lay |= pchan->bone->layer;
1433                         }
1434                 }
1435                 if (lay==0) return;
1436                 
1437                 if ( movetolayer_short_buts(&lay, "Bone Layers")==0 ) return;
1438                 if (lay==0) return;
1439                 
1440                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1441                         if (arm->layer & pchan->bone->layer) {
1442                                 if (pchan->bone->flag & BONE_SELECTED)
1443                                         pchan->bone->layer= lay;
1444                         }
1445                 }
1446                 
1447                 BIF_undo_push("Move Bone Layer");
1448         }
1449 }
1450
1451 #if 0
1452 // XXX old sys
1453 /* for use with pose_relax only */
1454 static int pose_relax_icu(struct IpoCurve *icu, float framef, float *val, float *frame_prev, float *frame_next)
1455 {
1456         if (!icu) {
1457                 return 0;
1458         } 
1459         else {
1460                 BezTriple *bezt = icu->bezt;
1461                 
1462                 BezTriple *bezt_prev=NULL, *bezt_next=NULL;
1463                 float w1, w2, wtot;
1464                 int i;
1465                 
1466                 for (i=0; i < icu->totvert; i++, bezt++) {
1467                         if (bezt->vec[1][0] < framef - 0.5) {
1468                                 bezt_prev = bezt;
1469                         } else {
1470                                 break;
1471                         }
1472                 }
1473                 
1474                 if (bezt_prev==NULL) return 0;
1475                 
1476                 /* advance to the next, dont need to advance i */
1477                 bezt = bezt_prev+1;
1478                 
1479                 for (; i < icu->totvert; i++, bezt++) {
1480                         if (bezt->vec[1][0] > framef + 0.5) {
1481                                 bezt_next = bezt;
1482                                                 break;
1483                         }
1484                 }
1485                 
1486                 if (bezt_next==NULL) return 0;
1487         
1488                 if (val) {
1489                         w1 = framef - bezt_prev->vec[1][0];
1490                         w2 = bezt_next->vec[1][0] - framef;
1491                         wtot = w1 + w2;
1492                         w1=w1/wtot;
1493                         w2=w2/wtot;
1494 #if 0
1495                         val = (bezt_prev->vec[1][1] * w2) + (bezt_next->vec[1][1] * w1);
1496 #else
1497                         /* apply the value with a hard coded 6th */
1498                         *val = (((bezt_prev->vec[1][1] * w2) + (bezt_next->vec[1][1] * w1)) + (*val * 5.0f)) / 6.0f;
1499 #endif
1500                 }
1501                 
1502                 if (frame_prev) *frame_prev = bezt_prev->vec[1][0];
1503                 if (frame_next) *frame_next = bezt_next->vec[1][0];
1504                 
1505                 return 1;
1506         }
1507 }
1508 #endif
1509
1510 void pose_relax(Scene *scene)
1511 {
1512         Object *ob = OBACT;
1513         bPose *pose;
1514         bAction *act;
1515         bArmature *arm;
1516         
1517 //      IpoCurve *icu_w, *icu_x, *icu_y, *icu_z;
1518         
1519         bPoseChannel *pchan;
1520 //      bActionChannel *achan;
1521 //      float framef = F_CFRA;
1522 //      float frame_prev, frame_next;
1523 //      float quat_prev[4], quat_next[4], quat_interp[4], quat_orig[4];
1524         
1525         int do_scale = 0;
1526         int do_loc = 0;
1527         int do_quat = 0;
1528         int flag = 0;
1529 //      int do_x, do_y, do_z;
1530         
1531         if (!ob) return;
1532         
1533         pose = ob->pose;
1534         act = ob->action;
1535         arm = (bArmature *)ob->data;
1536         
1537         if (!pose || !act || !arm) return;
1538         
1539         for (pchan=pose->chanbase.first; pchan; pchan= pchan->next) {
1540                 
1541                 pchan->bone->flag &= ~BONE_TRANSFORM;
1542                 
1543                 if (pchan->bone->layer & arm->layer) {
1544                         if (pchan->bone->flag & BONE_SELECTED) {
1545                                 /* do we have an ipo curve? */
1546 #if 0 // XXX old animation system
1547                                 achan= get_action_channel(act, pchan->name);
1548                                 
1549                                 if (achan && achan->ipo) {
1550                                         /*calc_ipo(achan->ipo, ctime);*/
1551                                         
1552                                         do_x = pose_relax_icu(find_ipocurve(achan->ipo, AC_LOC_X), framef, &pchan->loc[0], NULL, NULL);
1553                                         do_y = pose_relax_icu(find_ipocurve(achan->ipo, AC_LOC_Y), framef, &pchan->loc[1], NULL, NULL);
1554                                         do_z = pose_relax_icu(find_ipocurve(achan->ipo, AC_LOC_Z), framef, &pchan->loc[2], NULL, NULL);
1555                                         do_loc += do_x + do_y + do_z;
1556                                         
1557                                         do_x = pose_relax_icu(find_ipocurve(achan->ipo, AC_SIZE_X), framef, &pchan->size[0], NULL, NULL);
1558                                         do_y = pose_relax_icu(find_ipocurve(achan->ipo, AC_SIZE_Y), framef, &pchan->size[1], NULL, NULL);
1559                                         do_z = pose_relax_icu(find_ipocurve(achan->ipo, AC_SIZE_Z), framef, &pchan->size[2], NULL, NULL);
1560                                         do_scale += do_x + do_y + do_z;
1561                                                 
1562                                         if(     ((icu_w = find_ipocurve(achan->ipo, AC_QUAT_W))) &&
1563                                                 ((icu_x = find_ipocurve(achan->ipo, AC_QUAT_X))) &&
1564                                                 ((icu_y = find_ipocurve(achan->ipo, AC_QUAT_Y))) &&
1565                                                 ((icu_z = find_ipocurve(achan->ipo, AC_QUAT_Z))) )
1566                                         {
1567                                                 /* use the quatw keyframe as a basis for others */
1568                                                 if (pose_relax_icu(icu_w, framef, NULL, &frame_prev, &frame_next)) {
1569                                                         /* get 2 quats */
1570                                                         quat_prev[0] = eval_icu(icu_w, frame_prev);
1571                                                         quat_prev[1] = eval_icu(icu_x, frame_prev);
1572                                                         quat_prev[2] = eval_icu(icu_y, frame_prev);
1573                                                         quat_prev[3] = eval_icu(icu_z, frame_prev);
1574                                                         
1575                                                         quat_next[0] = eval_icu(icu_w, frame_next);
1576                                                         quat_next[1] = eval_icu(icu_x, frame_next);
1577                                                         quat_next[2] = eval_icu(icu_y, frame_next);
1578                                                         quat_next[3] = eval_icu(icu_z, frame_next);
1579                                                         
1580 #if 0
1581                                                         /* apply the setting, completely smooth */
1582                                                         QuatInterpol(pchan->quat, quat_prev, quat_next, (framef-frame_prev) / (frame_next-frame_prev) );
1583 #else
1584                                                         /* tricky interpolation */
1585                                                         QuatInterpol(quat_interp, quat_prev, quat_next, (framef-frame_prev) / (frame_next-frame_prev) );
1586                                                         QUATCOPY(quat_orig, pchan->quat);
1587                                                         QuatInterpol(pchan->quat, quat_orig, quat_interp, 1.0f/6.0f);
1588                                                         /* done */
1589 #endif
1590                                                         do_quat++;
1591                                                 }
1592                                         }
1593                                         
1594                                         /* apply BONE_TRANSFORM tag so that autokeying will pick it up */
1595                                         pchan->bone->flag |= BONE_TRANSFORM;
1596                                 }
1597                                 
1598 #endif // XXX old animation system
1599                         }
1600                 }
1601         }
1602         
1603         ob->pose->flag |= (POSE_LOCKED|POSE_DO_UNLOCK);
1604         
1605         /* do auto-keying */
1606         if (do_loc)             flag |= TFM_TRANSLATION;
1607         if (do_scale)   flag |= TFM_RESIZE;
1608         if (do_quat)    flag |= TFM_ROTATION;
1609         autokeyframe_pose_cb_func(ob, flag, 0);
1610          
1611         /* clear BONE_TRANSFORM flags */
1612         for (pchan=pose->chanbase.first; pchan; pchan= pchan->next)
1613                 pchan->bone->flag &= ~ BONE_TRANSFORM;
1614         
1615         /* do depsgraph flush */
1616         DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
1617         BIF_undo_push("Relax Pose");
1618 }
1619
1620 /* for use in insertkey, ensure rotation goes other way around */
1621 void pose_flipquats(Scene *scene)
1622 {
1623         Object *ob = OBACT;
1624         bArmature *arm= ob->data;
1625         bPoseChannel *pchan;
1626         
1627         if(ob->pose==NULL)
1628                 return;
1629         
1630         /* find sel bones */
1631         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1632                 if(pchan->bone && (pchan->bone->flag & BONE_SELECTED) && (pchan->bone->layer & arm->layer)) {
1633                         /* quaternions have 720 degree range */
1634                         pchan->quat[0]= -pchan->quat[0];
1635                         pchan->quat[1]= -pchan->quat[1];
1636                         pchan->quat[2]= -pchan->quat[2];
1637                         pchan->quat[3]= -pchan->quat[3];
1638                 }
1639         }
1640         
1641         /* do autokey */
1642         autokeyframe_pose_cb_func(ob, TFM_ROTATION, 0);
1643 }
1644
1645 /* context: active channel */
1646 void pose_special_editmenu(Scene *scene)
1647 {
1648 #if 0
1649         Object *obedit= scene->obedit; // XXX context
1650         Object *ob= OBACT;
1651         short nr;
1652         
1653         /* paranoia checks */
1654         if(!ob && !ob->pose) return;
1655         if(ob==obedit || (ob->flag & OB_POSEMODE)==0) return;
1656         
1657         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");
1658         if(nr==1) {
1659                 pose_select_constraint_target(scene);
1660         }
1661         else if(nr==2) {
1662                 pose_flip_names();
1663         }
1664         else if(nr==3) {
1665                 pose_calculate_path(C, ob);
1666         }
1667         else if(nr==4) {
1668                 pose_clear_paths(ob);
1669         }
1670         else if(nr==5) {
1671                 pose_clear_user_transforms(scene, ob);
1672         }
1673         else if(nr==6) {
1674                 pose_relax();
1675         }
1676         else if(ELEM3(nr, 7, 8, 9)) {
1677                 pose_autoside_names(nr-7);
1678         }
1679 #endif
1680 }
1681
1682 /* Restore selected pose-bones to 'action'-defined pose */
1683 void pose_clear_user_transforms(Scene *scene, Object *ob)
1684 {
1685         bArmature *arm= ob->data;
1686         bPoseChannel *pchan;
1687         
1688         if (ob->pose == NULL)
1689                 return;
1690         
1691         /* if the object has an action, restore pose to the pose defined by the action by clearing pose on selected bones */
1692         if (ob->action) {
1693                 /* find selected bones */
1694                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1695                         if (pchan->bone && (pchan->bone->flag & BONE_SELECTED) && (pchan->bone->layer & arm->layer)) {
1696                                 /* just clear the BONE_UNKEYED flag, allowing this bone to get overwritten by actions again */
1697                                 pchan->bone->flag &= ~BONE_UNKEYED;
1698                         }
1699                 }
1700                 
1701                 /* clear pose locking flag 
1702                  *      - this will only clear the user-defined pose in the selected bones, where BONE_UNKEYED has been cleared
1703                  */
1704                 ob->pose->flag |= POSE_DO_UNLOCK;
1705         }
1706         else {
1707                 /* no action, so restore entire pose to rest pose (cannot restore only selected bones) */
1708                 rest_pose(ob->pose);
1709         }
1710         
1711         DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
1712         BIF_undo_push("Clear User Transform");
1713 }
1714