== Action/Pose Groups - Keyframing Integration ==
[blender.git] / source / blender / src / 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 <string.h>
31
32 #include "MEM_guardedalloc.h"
33
34 #include "BLI_arithb.h"
35 #include "BLI_blenlib.h"
36
37 #include "DNA_action_types.h"
38 #include "DNA_armature_types.h"
39 #include "DNA_constraint_types.h"
40 #include "DNA_curve_types.h"
41 #include "DNA_mesh_types.h"
42 #include "DNA_meshdata_types.h"
43 #include "DNA_modifier_types.h"
44 #include "DNA_object_types.h"
45 #include "DNA_scene_types.h"
46 #include "DNA_screen_types.h"
47 #include "DNA_view3d_types.h"
48 #include "DNA_userdef_types.h"
49
50 #include "BKE_action.h"
51 #include "BKE_armature.h"
52 #include "BKE_blender.h"
53 #include "BKE_constraint.h"
54 #include "BKE_deform.h"
55 #include "BKE_depsgraph.h"
56 #include "BKE_displist.h"
57 #include "BKE_global.h"
58 #include "BKE_modifier.h"
59 #include "BKE_object.h"
60 #include "BKE_utildefines.h"
61 #include "BKE_ipo.h"
62
63 #include "BIF_editarmature.h"
64 #include "BIF_editaction.h"
65 #include "BIF_editconstraint.h"
66 #include "BIF_editdeform.h"
67 #include "BIF_gl.h"
68 #include "BIF_graphics.h"
69 #include "BIF_interface.h"
70 #include "BIF_poseobject.h"
71 #include "BIF_space.h"
72 #include "BIF_toolbox.h"
73 #include "BIF_screen.h"
74
75 #include "BDR_editobject.h"
76
77 #include "BSE_edit.h"
78 #include "BSE_editipo.h"
79 #include "BSE_trans_types.h"
80
81 #include "mydevice.h"
82 #include "blendef.h"
83 #include "transform.h"
84
85 #include "BIF_transform.h" /* for autokey TFM_TRANSLATION, etc */
86
87 void enter_posemode(void)
88 {
89         Base *base;
90         Object *ob;
91         bArmature *arm;
92         
93         if(G.scene->id.lib) return;
94         base= BASACT;
95         if(base==NULL) return;
96         
97         ob= base->object;
98         
99         if (ob->id.lib){
100                 error ("Can't pose libdata");
101                 return;
102         }
103
104         switch (ob->type){
105         case OB_ARMATURE:
106                 arm= get_armature(ob);
107                 if( arm==NULL ) return;
108                 
109                 ob->flag |= OB_POSEMODE;
110                 base->flag= ob->flag;
111                 
112                 allqueue(REDRAWHEADERS, 0);     
113                 allqueue(REDRAWBUTSALL, 0);     
114                 allqueue(REDRAWOOPS, 0);
115                 allqueue(REDRAWVIEW3D, 0);
116                 break;
117         default:
118                 return;
119         }
120
121         if (G.obedit) exit_editmode(EM_FREEDATA|EM_WAITCURSOR);
122         G.f &= ~(G_VERTEXPAINT | G_TEXTUREPAINT | G_WEIGHTPAINT);
123 }
124
125 void set_pose_keys (Object *ob)
126 {
127         bArmature *arm= ob->data;
128         bPoseChannel *chan;
129
130         if (ob->pose){
131                 for (chan=ob->pose->chanbase.first; chan; chan=chan->next){
132                         Bone *bone= chan->bone;
133                         if(bone && (bone->flag & BONE_SELECTED) && (arm->layer & bone->layer)) {
134                                 chan->flag |= POSE_KEY;         
135                         }
136                         else {
137                                 chan->flag &= ~POSE_KEY;
138                         }
139                 }
140         }
141 }
142
143
144 void exit_posemode(void)
145 {
146         Object *ob= OBACT;
147         Base *base= BASACT;
148
149         if(ob==NULL) return;
150         
151         ob->flag &= ~OB_POSEMODE;
152         base->flag= ob->flag;
153         
154         countall();
155         allqueue(REDRAWVIEW3D, 0);
156         allqueue(REDRAWOOPS, 0);
157         allqueue(REDRAWHEADERS, 0);     
158         allqueue(REDRAWBUTSALL, 0);     
159
160         scrarea_queue_headredraw(curarea);
161 }
162
163 /* called by buttons to find a bone to display/edit values for */
164 bPoseChannel *get_active_posechannel (Object *ob)
165 {
166         bArmature *arm= ob->data;
167         bPoseChannel *pchan;
168         
169         if ELEM(NULL, ob, ob->pose)
170                 return NULL;
171         
172         /* find active */
173         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
174                 if(pchan->bone && (pchan->bone->flag & BONE_ACTIVE) && (pchan->bone->layer & arm->layer))
175                         return pchan;
176         }
177         
178         return NULL;
179 }
180
181 /* if a selected or active bone is protected, throw error and return 1 */
182 /* only_selected==1 : the active bone is allowed to be protected */
183 static int pose_has_protected_selected(Object *ob, int only_selected)
184 {
185         
186         /* check protection */
187         if(ob->proxy) {
188                 bPoseChannel *pchan;
189                 bArmature *arm= ob->data;
190                 
191                 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
192                         if(pchan->bone && (pchan->bone->layer & arm->layer)) {
193                                 if(pchan->bone->layer & arm->layer_protected) {
194                                         if(only_selected && (pchan->bone->flag & BONE_ACTIVE));
195                                         else if(pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) 
196                                            break;
197                                 }
198                         }
199                 }
200                 if(pchan) {
201                         error("Cannot change Proxy protected bones");
202                         return 1;
203                 }
204         }
205         return 0;
206 }
207
208 /* only for real IK, not for auto-IK */
209 int pose_channel_in_IK_chain(Object *ob, bPoseChannel *pchan)
210 {
211         bConstraint *con;
212         Bone *bone;
213         
214         for(con= pchan->constraints.first; con; con= con->next) {
215                 if(con->type==CONSTRAINT_TYPE_KINEMATIC) {
216                         bKinematicConstraint *data= con->data;
217                         if((data->flag & CONSTRAINT_IK_AUTO)==0)
218                                 return 1;
219                 }
220         }
221         for(bone= pchan->bone->childbase.first; bone; bone= bone->next) {
222                 pchan= get_pose_channel(ob->pose, bone->name);
223                 if(pchan && pose_channel_in_IK_chain(ob, pchan))
224                         return 1;
225         }
226         return 0;
227 }
228
229 /* ********************************************** */
230
231 /* For the object with pose/action: create path curves for selected bones 
232  * This recalculates the WHOLE path within the pchan->pathsf and pchan->pathef range
233  */
234 void pose_calculate_path(Object *ob)
235 {
236         bArmature *arm;
237         bPoseChannel *pchan;
238         Base *base;
239         float *fp;
240         int cfra;
241         int sfra, efra;
242         
243         if (ob==NULL || ob->pose==NULL)
244                 return;
245         arm= ob->data;
246         
247         /* version patch for older files here (do_versions patch too complicated) */
248         if ((arm->pathsf == 0) || (arm->pathef == 0)) {
249                 arm->pathsf = SFRA;
250                 arm->pathef = EFRA;
251         }
252         if (arm->pathsize == 0) {
253                 arm->pathsize = 1;
254         }
255         
256         /* set frame values */
257         cfra= CFRA;
258         sfra = arm->pathsf;
259         efra = arm->pathef;
260         if (efra <= sfra) {
261                 error("Can't calculate paths when pathlen <= 0");
262                 return;
263         }
264         
265         waitcursor(1);
266         
267         /* hack: for unsaved files, set OB_RECALC so that paths can get calculated */
268         if ((ob->recalc & OB_RECALC)==0) {
269                 ob->recalc |= OB_RECALC;
270                 DAG_object_update_flags(G.scene, ob, screen_view3d_layers());
271         }
272         else
273                 DAG_object_update_flags(G.scene, ob, screen_view3d_layers());
274         
275         
276         /* malloc the path blocks */
277         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
278                 if ((pchan->bone) && (pchan->bone->flag & BONE_SELECTED)) {
279                         if (arm->layer & pchan->bone->layer) {
280                                 pchan->pathlen= efra-sfra+1;
281                                 pchan->pathsf= sfra;
282                                 pchan->pathef= efra+1;
283                                 if (pchan->path)
284                                         MEM_freeN(pchan->path);
285                                 pchan->path= MEM_callocN(3*pchan->pathlen*sizeof(float), "pchan path");
286                         }
287                 }
288         }
289         
290         for (CFRA=sfra; CFRA<=efra; CFRA++) {
291                 /* do all updates */
292                 for (base= FIRSTBASE; base; base= base->next) {
293                         if (base->object->recalc) {
294                                 int temp= base->object->recalc;
295                                 object_handle_update(base->object);
296                                 base->object->recalc= temp;
297                         }
298                 }
299                 
300                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
301                         if ((pchan->bone) && (pchan->bone->flag & BONE_SELECTED)) {
302                                 if (arm->layer & pchan->bone->layer) {
303                                         if (pchan->path) {
304                                                 fp= pchan->path+3*(CFRA-sfra);
305                                                 
306                                                 if (arm->pathflag & ARM_PATH_HEADS) { 
307                                                         VECCOPY(fp, pchan->pose_head);
308                                                 }
309                                                 else {
310                                                         VECCOPY(fp, pchan->pose_tail);
311                                                 }
312                                                 
313                                                 Mat4MulVecfl(ob->obmat, fp);
314                                         }
315                                 }
316                         }
317                 }
318         }
319         
320         waitcursor(0);
321         
322         CFRA= cfra;
323         allqueue(REDRAWVIEW3D, 0);      /* recalc tags are still there */
324         allqueue(REDRAWBUTSEDIT, 0);
325 }
326
327 /* For the object with pose/action: update paths for those that have got them
328  * This should selectively update paths that exist...
329  */
330 void pose_recalculate_paths(Object *ob)
331 {
332         bArmature *arm;
333         bPoseChannel *pchan;
334         Base *base;
335         float *fp;
336         int cfra;
337         int sfra, efra;
338         
339         if (ob==NULL || ob->pose==NULL)
340                 return;
341         arm= ob->data;
342         
343         /* set frame values */
344         cfra = CFRA;
345         sfra = efra = cfra; 
346         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
347                 if ((pchan->bone) && (arm->layer & pchan->bone->layer)) {
348                         if (pchan->path) {
349                                 /* if the pathsf and pathef aren't initialised, abort! */
350                                 if (ELEM(0, pchan->pathsf, pchan->pathef))      
351                                         return;
352                                 
353                                 /* try to increase area to do (only as much as needed) */
354                                 sfra= MIN2(sfra, pchan->pathsf);
355                                 efra= MAX2(efra, pchan->pathef);
356                         }
357                 }
358         }
359         if (efra <= sfra) return;
360         
361         waitcursor(1);
362         
363         /* hack: for unsaved files, set OB_RECALC so that paths can get calculated */
364         if ((ob->recalc & OB_RECALC)==0) {
365                 ob->recalc |= OB_RECALC;
366                 DAG_object_update_flags(G.scene, ob, screen_view3d_layers());
367         }
368         else
369                 DAG_object_update_flags(G.scene, ob, screen_view3d_layers());
370         
371         for (CFRA=sfra; CFRA<=efra; CFRA++) {
372                 /* do all updates */
373                 for (base= FIRSTBASE; base; base= base->next) {
374                         if (base->object->recalc) {
375                                 int temp= base->object->recalc;
376                                 object_handle_update(base->object);
377                                 base->object->recalc= temp;
378                         }
379                 }
380                 
381                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
382                         if ((pchan->bone) && (arm->layer & pchan->bone->layer)) {
383                                 if (pchan->path) {
384                                         /* only update if:
385                                          *      - in range of this pchan's existing path
386                                          *      - ... insert evil filtering/optimising conditions here...
387                                          */
388                                         if (IN_RANGE(CFRA, pchan->pathsf, pchan->pathef)) {
389                                                 fp= pchan->path+3*(CFRA-sfra);
390                                                 
391                                                 if (arm->pathflag & ARM_PATH_HEADS) { 
392                                                         VECCOPY(fp, pchan->pose_head);
393                                                 }
394                                                 else {
395                                                         VECCOPY(fp, pchan->pose_tail);
396                                                 }
397                                                 
398                                                 Mat4MulVecfl(ob->obmat, fp);
399                                         }
400                                 }
401                         }
402                 }
403         }
404         
405         waitcursor(0);
406         
407         CFRA= cfra;
408         allqueue(REDRAWVIEW3D, 0);      /* recalc tags are still there */
409         allqueue(REDRAWBUTSEDIT, 0);
410 }
411
412 /* for the object with pose/action: clear path curves for selected bones only */
413 void pose_clear_paths(Object *ob)
414 {
415         bPoseChannel *pchan;
416         
417         if (ob==NULL || ob->pose==NULL)
418                 return;
419         
420         /* free the path blocks */
421         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
422                 if ((pchan->bone) && (pchan->bone->flag & BONE_SELECTED)) {
423                         if (pchan->path) {
424                                 MEM_freeN(pchan->path);
425                                 pchan->path= NULL;
426                         }
427                 }
428         }
429         
430         allqueue(REDRAWVIEW3D, 0);
431 }
432
433
434
435 void pose_select_constraint_target(void)
436 {
437         Object *ob= OBACT;
438         bArmature *arm= ob->data;
439         bPoseChannel *pchan;
440         bConstraint *con;
441         
442         /* paranoia checks */
443         if (!ob && !ob->pose) return;
444         if (ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
445         
446         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
447                 if (arm->layer & pchan->bone->layer) {
448                         if (pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
449                                 for (con= pchan->constraints.first; con; con= con->next) {
450                                         bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
451                                         ListBase targets = {NULL, NULL};
452                                         bConstraintTarget *ct;
453                                         
454                                         if (cti && cti->get_constraint_targets) {
455                                                 cti->get_constraint_targets(con, &targets);
456                                                 
457                                                 for (ct= targets.first; ct; ct= ct->next) {
458                                                         if ((ct->tar == ob) && (ct->subtarget[0])) {
459                                                                 bPoseChannel *pchanc= get_pose_channel(ob->pose, ct->subtarget);
460                                                                 if(pchanc)
461                                                                         pchanc->bone->flag |= BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL;
462                                                         }
463                                                 }
464                                                 
465                                                 if (cti->flush_constraint_targets)
466                                                         cti->flush_constraint_targets(con, &targets, 1);
467                                         }
468                                 }
469                         }
470                 }
471         }
472         
473         allqueue (REDRAWVIEW3D, 0);
474         allqueue (REDRAWBUTSOBJECT, 0);
475         allqueue (REDRAWOOPS, 0);
476         
477         BIF_undo_push("Select constraint target");
478
479 }
480
481 /* context: active channel */
482 void pose_special_editmenu(void)
483 {
484         Object *ob= OBACT;
485         short nr;
486         
487         /* paranoia checks */
488         if(!ob && !ob->pose) return;
489         if(ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
490         
491         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");
492         if(nr==1) {
493                 pose_select_constraint_target();
494         }
495         else if(nr==2) {
496                 pose_flip_names();
497         }
498         else if(nr==3) {
499                 pose_calculate_path(ob);
500         }
501         else if(nr==4) {
502                 pose_clear_paths(ob);
503         }
504         else if(nr==5) {
505                 rest_pose(ob->pose);
506                 DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
507                 BIF_undo_push("Clear User Transform Pose");
508         }
509         else if(nr==6) {
510                 pose_relax();
511         }
512 }
513
514 void pose_add_IK(void)
515 {
516         Object *ob= OBACT;
517         
518         /* paranoia checks */
519         if(!ob && !ob->pose) return;
520         if(ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
521         
522         add_constraint(1);      /* 1 means only IK */
523 }
524
525 /* context: all selected channels */
526 void pose_clear_IK(void)
527 {
528         Object *ob= OBACT;
529         bArmature *arm= ob->data;
530         bPoseChannel *pchan;
531         bConstraint *con;
532         bConstraint *next;
533         
534         /* paranoia checks */
535         if(!ob && !ob->pose) return;
536         if(ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
537         
538         if(pose_has_protected_selected(ob, 0))
539                 return;
540         
541         if(okee("Remove IK constraint(s)")==0) return;
542
543         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
544                 if(arm->layer & pchan->bone->layer) {
545                         if(pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
546                                 
547                                 for(con= pchan->constraints.first; con; con= next) {
548                                         next= con->next;
549                                         if(con->type==CONSTRAINT_TYPE_KINEMATIC) {
550                                                 BLI_remlink(&pchan->constraints, con);
551                                                 free_constraint_data(con);
552                                                 MEM_freeN(con);
553                                         }
554                                 }
555                                 pchan->constflag &= ~(PCHAN_HAS_IK|PCHAN_HAS_TARGET);
556                         }
557                 }
558         }
559         
560         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);   // and all its relations
561         
562         allqueue (REDRAWVIEW3D, 0);
563         allqueue (REDRAWBUTSOBJECT, 0);
564         allqueue (REDRAWOOPS, 0);
565         
566         BIF_undo_push("Remove IK constraint(s)");
567 }
568
569 void pose_clear_constraints(void)
570 {
571         Object *ob= OBACT;
572         bArmature *arm= ob->data;
573         bPoseChannel *pchan;
574         
575         /* paranoia checks */
576         if(!ob && !ob->pose) return;
577         if(ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
578         
579         if(pose_has_protected_selected(ob, 0))
580                 return;
581         
582         if(okee("Remove Constraints")==0) return;
583         
584         /* find active */
585         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
586                 if(arm->layer & pchan->bone->layer) {
587                         if(pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
588                                 free_constraints(&pchan->constraints);
589                                 pchan->constflag= 0;
590                         }
591                 }
592         }
593         
594         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);   // and all its relations
595         
596         allqueue (REDRAWVIEW3D, 0);
597         allqueue (REDRAWBUTSOBJECT, 0);
598         allqueue (REDRAWOOPS, 0);
599         
600         BIF_undo_push("Remove Constraint(s)");
601         
602 }
603
604
605 void pose_copy_menu(void)
606 {
607         Object *ob= OBACT;
608         bArmature *arm= ob->data;
609         bPoseChannel *pchan, *pchanact;
610         short nr;
611         int i=0;
612         
613         /* paranoia checks */
614         if(!ob && !ob->pose) return;
615         if(ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
616         
617         /* find active */
618         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
619                 if(pchan->bone->flag & BONE_ACTIVE) break;
620         }
621         
622         if(pchan==NULL) return;
623         
624         if(pose_has_protected_selected(ob, 1))
625                 return;
626
627         pchanact= pchan;
628         
629         i= BLI_countlist(&(pchanact->constraints)); /* if there are 24 or less, allow for the user to select constraints */
630         if (i<25)
631                 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");
632         else
633                 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");
634         
635         if(nr==-1) return;
636         if(nr!=5)  {
637                 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
638                         if(     (arm->layer & pchan->bone->layer) &&
639                                 (pchan->bone->flag & BONE_SELECTED) &&
640                                 (pchan!=pchanact)
641                         ) {
642                                 switch (nr) {
643                                         case 1: /* Local Location */
644                                                 VECCOPY(pchan->loc, pchanact->loc);
645                                                 break;
646                                         case 2: /* Local Rotation */
647                                                 QUATCOPY(pchan->quat, pchanact->quat);
648                                                 break;
649                                         case 3: /* Local Size */
650                                                 VECCOPY(pchan->size, pchanact->size);
651                                                 break;
652                                         case 4: /* All Constraints */
653                                         {
654                                                 free_constraints(&pchan->constraints);
655                                                 copy_constraints(&pchan->constraints, &pchanact->constraints);
656                                                 pchan->constflag = pchanact->constflag;
657                                         }
658                                                 break;
659                                         case 6: /* Transform Locks */
660                                                 pchan->protectflag = pchanact->protectflag;
661                                                 break;
662                                         case 7: /* IK (DOF) settings */
663                                         {
664                                                 pchan->ikflag = pchanact->ikflag;
665                                                 VECCOPY(pchan->limitmin, pchanact->limitmin);
666                                                 VECCOPY(pchan->limitmax, pchanact->limitmax);
667                                                 VECCOPY(pchan->stiffness, pchanact->stiffness);
668                                                 pchan->ikstretch= pchanact->ikstretch;
669                                         }
670                                                 break;
671                                         case 8: /* Custom Bone Shape */
672                                                 pchan->custom = pchanact->custom;
673                                                 break;
674                                         case 9: /* Visual Location */
675                                                 armature_loc_pose_to_bone(pchan, pchanact->pose_mat[3], pchan->loc);
676                                                 break;
677                                         case 10: /* Visual Rotation */
678                                         {
679                                                 float delta_mat[4][4], quat[4];
680                                                 
681                                                 armature_mat_pose_to_bone(pchan, pchanact->pose_mat, delta_mat);
682                                                 Mat4ToQuat(delta_mat, quat);
683                                                 QUATCOPY(pchan->quat, quat);
684                                         }
685                                                 break;
686                                         case 11: /* Visual Size */
687                                         {
688                                                 float delta_mat[4][4], size[4];
689                                                 
690                                                 armature_mat_pose_to_bone(pchan, pchanact->pose_mat, delta_mat);
691                                                 Mat4ToSize(delta_mat, size);
692                                                 VECCOPY(pchan->size, size);
693                                         }
694                                 }
695                         }
696                 }
697         } else { /* constraints, optional */
698                 bConstraint *con, *con_back;
699                 int const_toggle[24];
700                 ListBase const_copy={0, 0};
701                 
702                 duplicatelist (&const_copy, &(pchanact->constraints));
703                 
704                 /* build the puplist of constraints */
705                 for (con = pchanact->constraints.first, i=0; con; con=con->next, i++){
706                         const_toggle[i]= 1;
707                         add_numbut(i, TOG|INT, con->name, 0, 0, &(const_toggle[i]), "");
708                 }
709                 
710                 if (!do_clever_numbuts("Select Constraints", i, REDRAW)) {
711                         BLI_freelistN(&const_copy);
712                         return;
713                 }
714                 
715                 /* now build a new listbase from the options selected */
716                 for (i=0, con=const_copy.first; con; i++) {
717                         if (!const_toggle[i]) {
718                                 con_back= con->next;
719                                 BLI_freelinkN(&const_copy, con);
720                                 con= con_back;
721                         } else {
722                                 con= con->next;
723                         }
724                 }
725                 
726                 /* Copy the temo listbase to the selected posebones */
727                 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
728                         if(     (arm->layer & pchan->bone->layer) &&
729                                 (pchan->bone->flag & BONE_SELECTED) &&
730                                 (pchan!=pchanact)
731                         ) {
732                                 free_constraints(&pchan->constraints);
733                                 copy_constraints(&pchan->constraints, &const_copy);
734                                 pchan->constflag = pchanact->constflag;
735                         }
736                 }
737                 BLI_freelistN(&const_copy);
738                 update_pose_constraint_flags(ob->pose); /* we could work out the flags but its simpler to do this */
739         }
740         
741         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);   // and all its relations
742         
743         allqueue (REDRAWVIEW3D, 0);
744         allqueue (REDRAWBUTSOBJECT, 0);
745         allqueue (REDRAWOOPS, 0);
746         
747         BIF_undo_push("Copy Pose Attributes");
748         
749 }
750
751 /* ******************** copy/paste pose ********************** */
752
753 static bPose    *g_posebuf=NULL;
754
755 void free_posebuf(void) 
756 {
757         if (g_posebuf) {
758                 // was copied without constraints
759                 BLI_freelistN (&g_posebuf->chanbase);
760                 MEM_freeN (g_posebuf);
761         }
762         g_posebuf=NULL;
763 }
764
765 void copy_posebuf (void)
766 {
767         Object *ob= OBACT;
768
769         if (!ob || !ob->pose){
770                 error ("No Pose");
771                 return;
772         }
773
774         free_posebuf();
775         
776         set_pose_keys(ob);  // sets chan->flag to POSE_KEY if bone selected
777         copy_pose(&g_posebuf, ob->pose, 0);
778
779 }
780
781 void paste_posebuf (int flip)
782 {
783         Object *ob= OBACT;
784         bPoseChannel *chan, *pchan;
785         float eul[4];
786         char name[32];
787         
788         if (!ob || !ob->pose)
789                 return;
790
791         if (!g_posebuf){
792                 error ("Copy buffer is empty");
793                 return;
794         }
795         
796         /*
797         // disabled until protected bones in proxies follow the rules everywhere else!
798         if(pose_has_protected_selected(ob, 1))
799                 return;
800         */
801         
802         /* Safely merge all of the channels in this pose into
803         any existing pose */
804         for (chan=g_posebuf->chanbase.first; chan; chan=chan->next) {
805                 if (chan->flag & POSE_KEY) {
806                         BLI_strncpy(name, chan->name, sizeof(name));
807                         if (flip)
808                                 bone_flip_name (name, 0);               // 0 = don't strip off number extensions
809                                 
810                         /* only copy when channel exists, poses are not meant to add random channels to anymore */
811                         pchan= get_pose_channel(ob->pose, name);
812                         
813                         if (pchan) {
814                                 /* only loc rot size */
815                                 /* only copies transform info for the pose */
816                                 VECCOPY(pchan->loc, chan->loc);
817                                 VECCOPY(pchan->size, chan->size);
818                                 QUATCOPY(pchan->quat, chan->quat);
819                                 pchan->flag= chan->flag;
820                                 
821                                 if (flip) {
822                                         pchan->loc[0]*= -1;
823                                         
824                                         QuatToEul(pchan->quat, eul);
825                                         eul[1]*= -1;
826                                         eul[2]*= -1;
827                                         EulToQuat(eul, pchan->quat);
828                                 }
829                                 
830                                 if (autokeyframe_cfra_can_key(ob)) {
831                                         ID *id= &ob->id;
832                                         
833                                         /* Set keys on pose */
834                                         if (chan->flag & POSE_ROT) {
835                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_X, 0);
836                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_Y, 0);
837                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_Z, 0);
838                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_W, 0);
839                                         }
840                                         if (chan->flag & POSE_SIZE) {
841                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_SIZE_X, 0);
842                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_SIZE_Y, 0);
843                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_SIZE_Z, 0);
844                                         }
845                                         if (chan->flag & POSE_LOC) {
846                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_LOC_X, 0);
847                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_LOC_Y, 0);
848                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_LOC_Z, 0);
849                                         }
850                                         
851                                         /* clear any unkeyed tags */
852                                         if (chan->bone)
853                                                 chan->bone->flag &= ~BONE_UNKEYED;
854                                 }
855                                 else {
856                                         /* add unkeyed tags */
857                                         if (chan->bone)
858                                                 chan->bone->flag |= BONE_UNKEYED;
859                                 }
860                         }
861                 }
862         }
863
864         /* Update event for pose and deformation children */
865         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
866         
867         if ((IS_AUTOKEY_MODE(NORMAL))) {
868                 remake_action_ipos(ob->action);
869                 allqueue (REDRAWIPO, 0);
870                 allqueue (REDRAWVIEW3D, 0);
871                 allqueue (REDRAWACTION, 0);             
872                 allqueue(REDRAWNLA, 0);
873         }
874         else {
875                 /* need to trick depgraph, action is not allowed to execute on pose */
876                 where_is_pose(ob);
877                 ob->recalc= 0;
878         }
879
880         BIF_undo_push("Paste Action Pose");
881 }
882
883 /* ********************************************** */
884
885 /* context weightpaint and deformer in posemode */
886 void pose_adds_vgroups(Object *meshobj, int heatweights)
887 {
888         extern VPaint Gwp;         /* from vpaint */
889         Object *poseobj= modifiers_isDeformedByArmature(meshobj);
890
891         if(poseobj==NULL || (poseobj->flag & OB_POSEMODE)==0) {
892                 error("The active object must have a deforming armature in pose mode");
893                 return;
894         }
895
896         add_verts_to_dgroups(meshobj, poseobj, heatweights, (Gwp.flag & VP_MIRROR_X));
897
898         if(heatweights)
899                 BIF_undo_push("Apply Bone Heat Weights to Vertex Groups");
900         else
901                 BIF_undo_push("Apply Bone Envelopes to Vertex Groups");
902
903         allqueue(REDRAWVIEW3D, 0);
904         allqueue(REDRAWBUTSEDIT, 0);
905         
906         // and all its relations
907         DAG_object_flush_update(G.scene, meshobj, OB_RECALC_DATA);
908 }
909
910 /* ********************************************** */
911
912 /* adds a new pose-group */
913 // TODO... 
914 void pose_add_posegroup ()
915 {
916
917 }
918
919 /* ********************************************** */
920
921 /* context active object */
922 void pose_flip_names(void)
923 {
924         Object *ob= OBACT;
925         bArmature *arm= ob->data;
926         bPoseChannel *pchan;
927         char newname[32];
928         
929         /* paranoia checks */
930         if(!ob && !ob->pose) return;
931         if(ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
932         
933         if(pose_has_protected_selected(ob, 0))
934                 return;
935         
936         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
937                 if(arm->layer & pchan->bone->layer) {
938                         if(pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
939                                 BLI_strncpy(newname, pchan->name, sizeof(newname));
940                                 bone_flip_name(newname, 1);     // 1 = do strip off number extensions
941                                 armature_bone_rename(ob->data, pchan->name, newname);
942                         }
943                 }
944         }
945         
946         allqueue(REDRAWVIEW3D, 0);
947         allqueue(REDRAWBUTSEDIT, 0);
948         allqueue(REDRAWBUTSOBJECT, 0);
949         allqueue (REDRAWACTION, 0);
950         allqueue(REDRAWOOPS, 0);
951         BIF_undo_push("Flip names");
952         
953 }
954
955 /* context active object, or weightpainted object with armature in posemode */
956 void pose_activate_flipped_bone(void)
957 {
958         Object *ob= OBACT;
959         bArmature *arm= ob->data;
960         
961         if(ob==NULL) return;
962
963         if(G.f & G_WEIGHTPAINT) {
964                 ob= modifiers_isDeformedByArmature(ob);
965         }
966         if(ob && (ob->flag & OB_POSEMODE)) {
967                 bPoseChannel *pchan, *pchanf;
968                 
969                 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
970                         if(arm->layer & pchan->bone->layer) {
971                                 if(pchan->bone->flag & BONE_ACTIVE)
972                                         break;
973                         }
974                 }
975                 if(pchan) {
976                         char name[32];
977                         
978                         BLI_strncpy(name, pchan->name, 32);
979                         bone_flip_name(name, 1);        // 0 = do not strip off number extensions
980                         
981                         pchanf= get_pose_channel(ob->pose, name);
982                         if(pchanf && pchanf!=pchan) {
983                                 pchan->bone->flag &= ~(BONE_SELECTED|BONE_ACTIVE);
984                                 pchanf->bone->flag |= (BONE_SELECTED|BONE_ACTIVE);
985                         
986                                 /* in weightpaint we select the associated vertex group too */
987                                 if(G.f & G_WEIGHTPAINT) {
988                                         vertexgroup_select_by_name(OBACT, name);
989                                         DAG_object_flush_update(G.scene, OBACT, OB_RECALC_DATA);
990                                 }
991                                 
992                                 select_actionchannel_by_name(ob->action, name, 1);
993                                 
994                                 allqueue(REDRAWVIEW3D, 0);
995                                 allqueue(REDRAWACTION, 0);
996                                 allqueue(REDRAWIPO, 0);         /* To force action/constraint ipo update */
997                                 allqueue(REDRAWBUTSEDIT, 0);
998                                 allqueue(REDRAWBUTSOBJECT, 0);
999                                 allqueue(REDRAWOOPS, 0);
1000                         }                       
1001                 }
1002         }
1003 }
1004
1005 /* This function pops up the move-to-layer popup widgets when the user
1006  * presses either SHIFT-MKEY or MKEY in PoseMode OR EditMode (for Armatures)
1007  */
1008 void pose_movetolayer(void)
1009 {
1010         Object *ob= OBACT;
1011         bArmature *arm;
1012         short lay= 0;
1013         
1014         if (ob==NULL) return;
1015         arm= ob->data;
1016         
1017         if (G.qual & LR_SHIFTKEY) {
1018                 /* armature layers */
1019                 lay= arm->layer;
1020                 if ( movetolayer_short_buts(&lay, "Armature Layers")==0 ) return;
1021                 if (lay==0) return;
1022                 arm->layer= lay;
1023                 if(ob->pose)
1024                         ob->pose->proxy_layer= lay;
1025                 
1026                 allqueue(REDRAWVIEW3D, 0);
1027                 allqueue(REDRAWACTION, 0);
1028                 allqueue(REDRAWBUTSEDIT, 0);
1029         }
1030         else if (G.obedit) {
1031                 /* the check for editbone layer moving needs to occur before posemode one to work */
1032                 EditBone *ebo;
1033                 EditBone *flipBone;
1034                 
1035                 for (ebo= G.edbo.first; ebo; ebo= ebo->next) {
1036                         if (arm->layer & ebo->layer) {
1037                                 if (ebo->flag & BONE_SELECTED)
1038                                         lay |= ebo->layer;
1039                         }
1040                 }
1041                 if (lay==0) return;
1042                 
1043                 if ( movetolayer_short_buts(&lay, "Bone Layers")==0 ) return;
1044                 if (lay==0) return;
1045                 
1046                 for (ebo= G.edbo.first; ebo; ebo= ebo->next) {
1047                         if (arm->layer & ebo->layer) {
1048                                 if (ebo->flag & BONE_SELECTED) {
1049                                         ebo->layer= lay;
1050                                         if (arm->flag & ARM_MIRROR_EDIT) {
1051                                                 flipBone = armature_bone_get_mirrored(ebo);
1052                                                 if (flipBone)
1053                                                         flipBone->layer = lay;
1054                                         }
1055                                 }
1056                         }
1057                 }
1058                 
1059                 BIF_undo_push("Move Bone Layer");
1060                 allqueue(REDRAWVIEW3D, 0);
1061                 allqueue(REDRAWBUTSEDIT, 0);
1062         }
1063         else if (ob->flag & OB_POSEMODE) {
1064                 /* pose-channel layers */
1065                 bPoseChannel *pchan;
1066                 
1067                 if (pose_has_protected_selected(ob, 0))
1068                         return;
1069                 
1070                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1071                         if (arm->layer & pchan->bone->layer) {
1072                                 if (pchan->bone->flag & BONE_SELECTED)
1073                                         lay |= pchan->bone->layer;
1074                         }
1075                 }
1076                 if (lay==0) return;
1077                 
1078                 if ( movetolayer_short_buts(&lay, "Bone Layers")==0 ) return;
1079                 if (lay==0) return;
1080                 
1081                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1082                         if (arm->layer & pchan->bone->layer) {
1083                                 if (pchan->bone->flag & BONE_SELECTED)
1084                                         pchan->bone->layer= lay;
1085                         }
1086                 }
1087                 
1088                 BIF_undo_push("Move Bone Layer");
1089                 allqueue(REDRAWVIEW3D, 0);
1090                 allqueue(REDRAWACTION, 0);
1091                 allqueue(REDRAWBUTSEDIT, 0);
1092         }
1093 }
1094
1095
1096 /* for use with pose_relax only */
1097 static int pose_relax_icu(struct IpoCurve *icu, float framef, float *val, float *frame_prev, float *frame_next)
1098 {
1099         if (!icu) {
1100                 return 0;
1101         } else {
1102                 BezTriple *bezt = icu->bezt;
1103                 
1104                 BezTriple *bezt_prev=NULL, *bezt_next=NULL;
1105                 float w1, w2, wtot;
1106                 int i;
1107                 
1108                 for (i=0; i < icu->totvert; i++, bezt++) {
1109                         if (bezt->vec[1][0] < framef - 0.5) {
1110                                 bezt_prev = bezt;
1111                         } else {
1112                                 break;
1113                         }
1114                 }
1115                 
1116                 if (bezt_prev==NULL) return 0;
1117                 
1118                 /* advance to the next, dont need to advance i */
1119                 bezt = bezt_prev+1;
1120                 
1121                 for (; i < icu->totvert; i++, bezt++) {
1122                         if (bezt->vec[1][0] > framef + 0.5) {
1123                                 bezt_next = bezt;
1124                                                 break;
1125                         }
1126                 }
1127                 
1128                 if (bezt_next==NULL) return 0;
1129         
1130                 if (val) {
1131                         w1 = framef - bezt_prev->vec[1][0];
1132                         w2 = bezt_next->vec[1][0] - framef;
1133                         wtot = w1 + w2;
1134                         w1=w1/wtot;
1135                         w2=w2/wtot;
1136 #if 0
1137                         val = (bezt_prev->vec[1][1] * w2) + (bezt_next->vec[1][1] * w1);
1138 #else
1139                         /* apply the value with a hard coded 6th */
1140                         *val = (((bezt_prev->vec[1][1] * w2) + (bezt_next->vec[1][1] * w1)) + (*val * 5.0f)) / 6.0f;
1141 #endif
1142                 }
1143                 
1144                 if (frame_prev) *frame_prev = bezt_prev->vec[1][0];
1145                 if (frame_next) *frame_next = bezt_next->vec[1][0];
1146                 
1147                 return 1;
1148         }
1149 }
1150
1151 void pose_relax()
1152 {
1153         Object *ob = OBACT;
1154         bPose *pose;
1155         bAction *act;
1156         bArmature *arm;
1157         
1158         IpoCurve *icu_w, *icu_x, *icu_y, *icu_z;
1159         
1160         bPoseChannel *pchan;
1161         bActionChannel *achan;
1162         float framef = F_CFRA;
1163         float frame_prev, frame_next;
1164         float quat_prev[4], quat_next[4], quat_interp[4], quat_orig[4];
1165         
1166         int do_scale = 0;
1167         int do_loc = 0;
1168         int do_quat = 0;
1169         int flag = 0;
1170         int do_x, do_y, do_z;
1171         
1172         if (!ob) return;
1173         
1174         pose = ob->pose;
1175         act = ob->action;
1176         arm = (bArmature *)ob->data;
1177         
1178         if (!pose || !act || !arm) return;
1179         
1180         for (pchan=pose->chanbase.first; pchan; pchan= pchan->next) {
1181                 if (pchan->bone->layer & arm->layer) {
1182                         if (pchan->bone->flag & BONE_SELECTED) {
1183                                 /* do we have an ipo curve? */
1184                                 achan= get_action_channel(act, pchan->name);
1185                                 
1186                                 if (achan && achan->ipo) {
1187                                         /*calc_ipo(achan->ipo, ctime);*/
1188                                         
1189                                         do_x = pose_relax_icu(find_ipocurve(achan->ipo, AC_LOC_X), framef, &pchan->loc[0], NULL, NULL);
1190                                         do_y = pose_relax_icu(find_ipocurve(achan->ipo, AC_LOC_Y), framef, &pchan->loc[1], NULL, NULL);
1191                                         do_z = pose_relax_icu(find_ipocurve(achan->ipo, AC_LOC_Z), framef, &pchan->loc[2], NULL, NULL);
1192                                         do_loc += do_x + do_y + do_z;
1193                                         
1194                                         do_x = pose_relax_icu(find_ipocurve(achan->ipo, AC_SIZE_X), framef, &pchan->size[0], NULL, NULL);
1195                                         do_y = pose_relax_icu(find_ipocurve(achan->ipo, AC_SIZE_Y), framef, &pchan->size[1], NULL, NULL);
1196                                         do_z = pose_relax_icu(find_ipocurve(achan->ipo, AC_SIZE_Z), framef, &pchan->size[2], NULL, NULL);
1197                                         do_scale += do_x + do_y + do_z;
1198                                                 
1199                                         if(     ((icu_w = find_ipocurve(achan->ipo, AC_QUAT_W))) &&
1200                                                 ((icu_x = find_ipocurve(achan->ipo, AC_QUAT_X))) &&
1201                                                 ((icu_y = find_ipocurve(achan->ipo, AC_QUAT_Y))) &&
1202                                                 ((icu_z = find_ipocurve(achan->ipo, AC_QUAT_Z))) )
1203                                         {
1204                                                 /* use the quatw keyframe as a basis for others */
1205                                                 if (pose_relax_icu(icu_w, framef, NULL, &frame_prev, &frame_next)) {
1206                                                         /* get 2 quats */
1207                                                         quat_prev[0] = eval_icu(icu_w, frame_prev);
1208                                                         quat_prev[1] = eval_icu(icu_x, frame_prev);
1209                                                         quat_prev[2] = eval_icu(icu_y, frame_prev);
1210                                                         quat_prev[3] = eval_icu(icu_z, frame_prev);
1211                                                         
1212                                                         quat_next[0] = eval_icu(icu_w, frame_next);
1213                                                         quat_next[1] = eval_icu(icu_x, frame_next);
1214                                                         quat_next[2] = eval_icu(icu_y, frame_next);
1215                                                         quat_next[3] = eval_icu(icu_z, frame_next);
1216                                                         
1217 #if 0
1218                                                         /* apply the setting, completely smooth */
1219                                                         QuatInterpol(pchan->quat, quat_prev, quat_next, (framef-frame_prev) / (frame_next-frame_prev) );
1220 #else
1221                                                         /* tricky interpolation */
1222                                                         QuatInterpol(quat_interp, quat_prev, quat_next, (framef-frame_prev) / (frame_next-frame_prev) );
1223                                                         QUATCOPY(quat_orig, pchan->quat);
1224                                                         QuatInterpol(pchan->quat, quat_orig, quat_interp, 1.0f/6.0f);
1225                                                         /* done */
1226 #endif
1227                                                         do_quat++;
1228                                                 }
1229                                         }
1230                                         
1231                                         /* apply BONE_TRANSFORM tag so that autokeying will pick it up */
1232                                         pchan->bone->flag |= BONE_TRANSFORM;
1233                                 }
1234                         }
1235                 }
1236         }
1237         
1238         ob->pose->flag |= (POSE_LOCKED|POSE_DO_UNLOCK);
1239         
1240         /* do auto-keying */
1241         if (do_loc)             flag |= TFM_TRANSLATION;
1242         if (do_scale)   flag |= TFM_RESIZE;
1243         if (do_quat)    flag |= TFM_ROTATION;
1244         autokeyframe_pose_cb_func(ob, flag, 0);
1245          
1246         /* clear BONE_TRANSFORM flags */
1247         for (pchan=pose->chanbase.first; pchan; pchan= pchan->next)
1248                 pchan->bone->flag &= ~ BONE_TRANSFORM;
1249         
1250         /* do depsgraph flush */
1251         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
1252         BIF_undo_push("Relax Pose");
1253 }