Bone Heat Weighting
[blender-staging.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_mesh_types.h"
41 #include "DNA_meshdata_types.h"
42 #include "DNA_modifier_types.h"
43 #include "DNA_object_types.h"
44 #include "DNA_scene_types.h"
45 #include "DNA_screen_types.h"
46 #include "DNA_view3d_types.h"
47
48 #include "BKE_action.h"
49 #include "BKE_armature.h"
50 #include "BKE_blender.h"
51 #include "BKE_constraint.h"
52 #include "BKE_deform.h"
53 #include "BKE_depsgraph.h"
54 #include "BKE_displist.h"
55 #include "BKE_global.h"
56 #include "BKE_modifier.h"
57 #include "BKE_object.h"
58 #include "BKE_utildefines.h"
59
60 #include "BIF_editarmature.h"
61 #include "BIF_editaction.h"
62 #include "BIF_editconstraint.h"
63 #include "BIF_editdeform.h"
64 #include "BIF_gl.h"
65 #include "BIF_graphics.h"
66 #include "BIF_interface.h"
67 #include "BIF_poseobject.h"
68 #include "BIF_space.h"
69 #include "BIF_toolbox.h"
70 #include "BIF_screen.h"
71
72 #include "BDR_editobject.h"
73
74 #include "BSE_edit.h"
75 #include "BSE_editipo.h"
76 #include "BSE_trans_types.h"
77
78 #include "mydevice.h"
79 #include "blendef.h"
80
81 void enter_posemode(void)
82 {
83         Base *base;
84         Object *ob;
85         bArmature *arm;
86         
87         if(G.scene->id.lib) return;
88         base= BASACT;
89         if(base==NULL) return;
90         
91         ob= base->object;
92         
93         if (ob->id.lib){
94                 error ("Can't pose libdata");
95                 return;
96         }
97
98         switch (ob->type){
99         case OB_ARMATURE:
100                 arm= get_armature(ob);
101                 if( arm==NULL ) return;
102                 
103                 ob->flag |= OB_POSEMODE;
104                 base->flag= ob->flag;
105                 
106                 allqueue(REDRAWHEADERS, 0);     
107                 allqueue(REDRAWBUTSALL, 0);     
108                 allqueue(REDRAWOOPS, 0);
109                 allqueue(REDRAWVIEW3D, 0);
110                 break;
111         default:
112                 return;
113         }
114
115         if (G.obedit) exit_editmode(EM_FREEDATA|EM_WAITCURSOR);
116         G.f &= ~(G_VERTEXPAINT | G_FACESELECT | G_TEXTUREPAINT | G_WEIGHTPAINT);
117 }
118
119 void set_pose_keys (Object *ob)
120 {
121         bArmature *arm= ob->data;
122         bPoseChannel *chan;
123
124         if (ob->pose){
125                 for (chan=ob->pose->chanbase.first; chan; chan=chan->next){
126                         Bone *bone= chan->bone;
127                         if(bone && (bone->flag & BONE_SELECTED) && (arm->layer & bone->layer)) {
128                                 chan->flag |= POSE_KEY;         
129                         }
130                         else {
131                                 chan->flag &= ~POSE_KEY;
132                         }
133                 }
134         }
135 }
136
137
138 void exit_posemode(void)
139 {
140         Object *ob= OBACT;
141         Base *base= BASACT;
142
143         if(ob==NULL) return;
144         
145         ob->flag &= ~OB_POSEMODE;
146         base->flag= ob->flag;
147         
148         countall();
149         allqueue(REDRAWVIEW3D, 0);
150         allqueue(REDRAWOOPS, 0);
151         allqueue(REDRAWHEADERS, 0);     
152         allqueue(REDRAWBUTSALL, 0);     
153
154         scrarea_queue_headredraw(curarea);
155 }
156
157 /* called by buttons to find a bone to display/edit values for */
158 bPoseChannel *get_active_posechannel (Object *ob)
159 {
160         bArmature *arm= ob->data;
161         bPoseChannel *pchan;
162         
163         /* find active */
164         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
165                 if(pchan->bone && (pchan->bone->flag & BONE_ACTIVE) && (pchan->bone->layer & arm->layer))
166                         return pchan;
167         }
168         
169         return NULL;
170 }
171
172 /* if a selected or active bone is protected, throw error and return 1 */
173 /* only_selected==1 : the active bone is allowed to be protected */
174 static int pose_has_protected_selected(Object *ob, int only_selected)
175 {
176         
177         /* check protection */
178         if(ob->proxy) {
179                 bPoseChannel *pchan;
180                 bArmature *arm= ob->data;
181                 
182                 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
183                         if(pchan->bone && (pchan->bone->layer & arm->layer)) {
184                                 if(pchan->bone->layer & arm->layer_protected) {
185                                         if(only_selected && (pchan->bone->flag & BONE_ACTIVE));
186                                         else if(pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) 
187                                            break;
188                                 }
189                         }
190                 }
191                 if(pchan) {
192                         error("Cannot change Proxy protected bones");
193                         return 1;
194                 }
195         }
196         return 0;
197 }
198
199 /* only for real IK, not for auto-IK */
200 int pose_channel_in_IK_chain(Object *ob, bPoseChannel *pchan)
201 {
202         bConstraint *con;
203         Bone *bone;
204         
205         for(con= pchan->constraints.first; con; con= con->next) {
206                 if(con->type==CONSTRAINT_TYPE_KINEMATIC) {
207                         bKinematicConstraint *data= con->data;
208                         if((data->flag & CONSTRAINT_IK_AUTO)==0)
209                                 return 1;
210                 }
211         }
212         for(bone= pchan->bone->childbase.first; bone; bone= bone->next) {
213                 pchan= get_pose_channel(ob->pose, bone->name);
214                 if(pchan && pose_channel_in_IK_chain(ob, pchan))
215                         return 1;
216         }
217         return 0;
218 }
219
220 /* ********************************************** */
221
222 /* for the object with pose/action: create path curves for selected bones */
223 void pose_calculate_path(Object *ob)
224 {
225         bArmature *arm;
226         bPoseChannel *pchan;
227         Base *base;
228         float *fp;
229         int cfra;
230         int sfra, efra;
231         
232         if(ob==NULL || ob->pose==NULL)
233                 return;
234         arm= ob->data;
235         
236         /* version patch for older files here (do_versions patch too complicated) */
237         if ((arm->pathsf == 0) || (arm->pathef == 0)) {
238                 arm->pathsf = SFRA;
239                 arm->pathef = EFRA;
240         }
241         if (arm->pathsize == 0) {
242                 arm->pathsize = 1;
243         }
244         
245         /* set frame values */
246         cfra= CFRA;
247         sfra = arm->pathsf;
248         efra = arm->pathef;
249         if (efra<=sfra) return;
250         
251         DAG_object_update_flags(G.scene, ob, screen_view3d_layers());
252         
253         /* malloc the path blocks */
254         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
255                 if(pchan->bone && (pchan->bone->flag & BONE_SELECTED)) {
256                         if(arm->layer & pchan->bone->layer) {
257                                 pchan->pathlen= efra-sfra+1;
258                                 pchan->pathsf= sfra;
259                                 pchan->pathef= efra+1;
260                                 if(pchan->path)
261                                         MEM_freeN(pchan->path);
262                                 pchan->path= MEM_callocN(3*pchan->pathlen*sizeof(float), "pchan path");
263                         }
264                 }
265         }
266         
267         for(CFRA=sfra; CFRA<=efra; CFRA++) {
268                 /* do all updates */
269                 for(base= FIRSTBASE; base; base= base->next) {
270                         if(base->object->recalc) {
271                                 int temp= base->object->recalc;
272                                 object_handle_update(base->object);
273                                 base->object->recalc= temp;
274                         }
275                 }
276                 
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                                         if(pchan->path) {
281                                                 fp= pchan->path+3*(CFRA-sfra);
282                                                 
283                                                 if (arm->pathflag & ARM_PATH_HEADS) { 
284                                                         VECCOPY(fp, pchan->pose_head);
285                                                 }
286                                                 else {
287                                                         VECCOPY(fp, pchan->pose_tail);
288                                                 }
289                                                 
290                                                 Mat4MulVecfl(ob->obmat, fp);
291                                         }
292                                 }
293                         }
294                 }
295         }
296         
297         CFRA= cfra;
298         allqueue(REDRAWVIEW3D, 0);      /* recalc tags are still there */
299         allqueue(REDRAWBUTSEDIT, 0);
300 }
301
302
303 /* for the object with pose/action: clear all path curves */
304 void pose_clear_paths(Object *ob)
305 {
306         bPoseChannel *pchan;
307         
308         if(ob==NULL || ob->pose==NULL)
309                 return;
310         
311         /* free the path blocks */
312         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
313                 if(pchan->path) {
314                         MEM_freeN(pchan->path);
315                         pchan->path= NULL;
316                 }
317         }
318         
319         allqueue(REDRAWVIEW3D, 0);
320 }
321
322
323
324 void pose_select_constraint_target(void)
325 {
326         Object *ob= OBACT;
327         bArmature *arm= ob->data;
328         bPoseChannel *pchan;
329         bConstraint *con;
330         
331         /* paranoia checks */
332         if(!ob && !ob->pose) return;
333         if(ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
334         
335         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
336                 if(arm->layer & pchan->bone->layer) {
337                         if(pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
338                                 
339                                 for(con= pchan->constraints.first; con; con= con->next) {
340                                         char *subtarget;
341                                         Object *target= get_constraint_target(con, &subtarget);
342                                         
343                                         if(ob==target) {
344                                                 if(subtarget) {
345                                                         bPoseChannel *pchanc= get_pose_channel(ob->pose, subtarget);
346                                                         if(pchanc)
347                                                                 pchanc->bone->flag |= BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL;
348                                                 }
349                                         }
350                                 }
351                         }
352                 }
353         }
354         
355         allqueue (REDRAWVIEW3D, 0);
356         allqueue (REDRAWBUTSOBJECT, 0);
357         allqueue (REDRAWOOPS, 0);
358         
359         BIF_undo_push("Select constraint target");
360
361 }
362
363 /* context: active channel */
364 void pose_special_editmenu(void)
365 {
366         Object *ob= OBACT;
367         short nr;
368         
369         /* paranoia checks */
370         if(!ob && !ob->pose) return;
371         if(ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
372         
373         nr= pupmenu("Specials%t|Select Constraint Target%x1|Flip Left-Right Names%x2|Calculate Paths%x3|Clear All Paths%x4|Clear User Transform %x5");
374         if(nr==1) {
375                 pose_select_constraint_target();
376         }
377         else if(nr==2) {
378                 pose_flip_names();
379         }
380         else if(nr==3) {
381                 pose_calculate_path(ob);
382         }
383         else if(nr==4) {
384                 pose_clear_paths(ob);
385         }
386         else if(nr==5) {
387                 rest_pose(ob->pose);
388                 DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
389                 BIF_undo_push("Clear User Transform Pose");
390         }
391 }
392
393 void pose_add_IK(void)
394 {
395         Object *ob= OBACT;
396         
397         /* paranoia checks */
398         if(!ob && !ob->pose) return;
399         if(ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
400         
401         add_constraint(1);      /* 1 means only IK */
402 }
403
404 /* context: all selected channels */
405 void pose_clear_IK(void)
406 {
407         Object *ob= OBACT;
408         bArmature *arm= ob->data;
409         bPoseChannel *pchan;
410         bConstraint *con;
411         bConstraint *next;
412         
413         /* paranoia checks */
414         if(!ob && !ob->pose) return;
415         if(ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
416         
417         if(pose_has_protected_selected(ob, 0))
418                 return;
419         
420         if(okee("Remove IK constraint(s)")==0) return;
421
422         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
423                 if(arm->layer & pchan->bone->layer) {
424                         if(pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
425                                 
426                                 for(con= pchan->constraints.first; con; con= next) {
427                                         next= con->next;
428                                         if(con->type==CONSTRAINT_TYPE_KINEMATIC) {
429                                                 BLI_remlink(&pchan->constraints, con);
430                                                 free_constraint_data(con);
431                                                 MEM_freeN(con);
432                                         }
433                                 }
434                                 pchan->constflag &= ~(PCHAN_HAS_IK|PCHAN_HAS_TARGET);
435                         }
436                 }
437         }
438         
439         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);   // and all its relations
440         
441         allqueue (REDRAWVIEW3D, 0);
442         allqueue (REDRAWBUTSOBJECT, 0);
443         allqueue (REDRAWOOPS, 0);
444         
445         BIF_undo_push("Remove IK constraint(s)");
446 }
447
448 void pose_clear_constraints(void)
449 {
450         Object *ob= OBACT;
451         bArmature *arm= ob->data;
452         bPoseChannel *pchan;
453         
454         /* paranoia checks */
455         if(!ob && !ob->pose) return;
456         if(ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
457         
458         if(pose_has_protected_selected(ob, 0))
459                 return;
460         
461         if(okee("Remove Constraints")==0) return;
462         
463         /* find active */
464         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
465                 if(arm->layer & pchan->bone->layer) {
466                         if(pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
467                                 free_constraints(&pchan->constraints);
468                                 pchan->constflag= 0;
469                         }
470                 }
471         }
472         
473         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);   // and all its relations
474         
475         allqueue (REDRAWVIEW3D, 0);
476         allqueue (REDRAWBUTSOBJECT, 0);
477         allqueue (REDRAWOOPS, 0);
478         
479         BIF_undo_push("Remove Constraint(s)");
480         
481 }
482
483
484 void pose_copy_menu(void)
485 {
486         Object *ob= OBACT;
487         bArmature *arm= ob->data;
488         bPoseChannel *pchan, *pchanact;
489         short nr;
490         int i=0;
491         
492         /* paranoia checks */
493         if(!ob && !ob->pose) return;
494         if(ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
495         
496         /* find active */
497         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
498                 if(pchan->bone->flag & BONE_ACTIVE) break;
499         }
500         
501         if(pchan==NULL) return;
502         
503         if(pose_has_protected_selected(ob, 1))
504                 return;
505
506         pchanact= pchan;
507         
508         i= BLI_countlist(&(pchanact->constraints)); /* if there are 24 or less, allow for the user to select constraints */
509         if (i<25)
510                 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");
511         else
512                 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");
513         
514         if(nr==-1) return;
515         if(nr!=5)  {
516                 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
517                         if(     (arm->layer & pchan->bone->layer) &&
518                                 (pchan->bone->flag & BONE_SELECTED) &&
519                                 (pchan!=pchanact)
520                         ) {
521                                 switch (nr) {
522                                         case 1: /* Local Location */
523                                                 VECCOPY(pchan->loc, pchanact->loc);
524                                                 break;
525                                         case 2: /* Local Rotation */
526                                                 QUATCOPY(pchan->quat, pchanact->quat);
527                                                 break;
528                                         case 3: /* Local Size */
529                                                 VECCOPY(pchan->size, pchanact->size);
530                                                 break;
531                                         case 4: /* All Constraints */
532                                         {
533                                                 free_constraints(&pchan->constraints);
534                                                 copy_constraints(&pchan->constraints, &pchanact->constraints);
535                                                 pchan->constflag = pchanact->constflag;
536                                         }
537                                                 break;
538                                         case 6: /* Transform Locks */
539                                                 pchan->protectflag = pchanact->protectflag;
540                                                 break;
541                                         case 7: /* IK (DOF) settings */
542                                         {
543                                                 pchan->ikflag = pchanact->ikflag;
544                                                 VECCOPY(pchan->limitmin, pchanact->limitmin);
545                                                 VECCOPY(pchan->limitmax, pchanact->limitmax);
546                                                 VECCOPY(pchan->stiffness, pchanact->stiffness);
547                                                 pchan->ikstretch= pchanact->ikstretch;
548                                         }
549                                                 break;
550                                         case 8: /* Custom Bone Shape */
551                                                 pchan->custom = pchanact->custom;
552                                                 break;
553                                         case 9: /* Visual Location */
554                                                 armature_loc_pose_to_bone(pchan, pchanact->pose_mat[3], pchan->loc);
555                                                 break;
556                                         case 10: /* Visual Rotation */
557                                         {
558                                                 float delta_mat[4][4], quat[4];
559                                                 
560                                                 armature_mat_pose_to_bone(pchan, pchanact->pose_mat, delta_mat);
561                                                 Mat4ToQuat(delta_mat, quat);
562                                                 QUATCOPY(pchan->quat, quat);
563                                         }
564                                                 break;
565                                         case 11: /* Visual Size */
566                                         {
567                                                 float delta_mat[4][4], size[4];
568                                                 
569                                                 armature_mat_pose_to_bone(pchan, pchanact->pose_mat, delta_mat);
570                                                 Mat4ToSize(delta_mat, size);
571                                                 VECCOPY(pchan->size, size);
572                                         }
573                                 }
574                         }
575                 }
576         } else { /* constraints, optional */
577                 bConstraint *con, *con_back;
578                 int const_toggle[24];
579                 ListBase const_copy={0, 0};
580                 
581                 duplicatelist (&const_copy, &(pchanact->constraints));
582                 
583                 /* build the puplist of constraints */
584                 for (con = pchanact->constraints.first, i=0; con; con=con->next, i++){
585                         const_toggle[i]= 1;
586                         add_numbut(i, TOG|INT, con->name, 0, 0, &(const_toggle[i]), "");
587                 }
588                 
589                 if (!do_clever_numbuts("Select Constraints", i, REDRAW)) {
590                         BLI_freelistN(&const_copy);
591                         return;
592                 }
593                 
594                 /* now build a new listbase from the options selected */
595                 for (i=0, con=const_copy.first; con; i++) {
596                         if (!const_toggle[i]) {
597                                 con_back= con->next;
598                                 BLI_freelinkN(&const_copy, con);
599                                 con= con_back;
600                         } else {
601                                 con= con->next;
602                         }
603                 }
604                 
605                 /* Copy the temo listbase to the selected posebones */
606                 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
607                         if(     (arm->layer & pchan->bone->layer) &&
608                                 (pchan->bone->flag & BONE_SELECTED) &&
609                                 (pchan!=pchanact)
610                         ) {
611                                 free_constraints(&pchan->constraints);
612                                 copy_constraints(&pchan->constraints, &const_copy);
613                                 pchan->constflag = pchanact->constflag;
614                         }
615                 }
616                 BLI_freelistN(&const_copy);
617                 update_pose_constraint_flags(ob->pose); /* we could work out the flags but its simpler to do this */
618         }
619         
620         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);   // and all its relations
621         
622         allqueue (REDRAWVIEW3D, 0);
623         allqueue (REDRAWBUTSOBJECT, 0);
624         allqueue (REDRAWOOPS, 0);
625         
626         BIF_undo_push("Copy Pose Attributes");
627         
628 }
629
630 /* ******************** copy/paste pose ********************** */
631
632 static bPose    *g_posebuf=NULL;
633
634 void free_posebuf(void) 
635 {
636         if (g_posebuf) {
637                 // was copied without constraints
638                 BLI_freelistN (&g_posebuf->chanbase);
639                 MEM_freeN (g_posebuf);
640         }
641         g_posebuf=NULL;
642 }
643
644 void copy_posebuf (void)
645 {
646         Object *ob= OBACT;
647
648         if (!ob || !ob->pose){
649                 error ("No Pose");
650                 return;
651         }
652
653         free_posebuf();
654         
655         set_pose_keys(ob);  // sets chan->flag to POSE_KEY if bone selected
656         copy_pose(&g_posebuf, ob->pose, 0);
657
658 }
659
660 void paste_posebuf (int flip)
661 {
662         Object *ob= OBACT;
663         bPoseChannel *chan, *pchan;
664         float eul[4];
665         char name[32];
666         
667         if (!ob || !ob->pose)
668                 return;
669
670         if (!g_posebuf){
671                 error ("Copy buffer is empty");
672                 return;
673         }
674         
675         /*
676         // disabled until protected bones in proxies follow the rules everywhere else!
677         if(pose_has_protected_selected(ob, 1))
678                 return;
679         */
680         
681         /* Safely merge all of the channels in this pose into
682         any existing pose */
683         for (chan=g_posebuf->chanbase.first; chan; chan=chan->next){
684                 if (chan->flag & POSE_KEY) {
685                         BLI_strncpy(name, chan->name, sizeof(name));
686                         if (flip)
687                                 bone_flip_name (name, 0);               // 0 = don't strip off number extensions
688                                 
689                         /* only copy when channel exists, poses are not meant to add random channels to anymore */
690                         pchan= get_pose_channel(ob->pose, name);
691                         
692                         if(pchan) {
693                                 /* only loc rot size */
694                                 /* only copies transform info for the pose */
695                                 VECCOPY(pchan->loc, chan->loc);
696                                 VECCOPY(pchan->size, chan->size);
697                                 QUATCOPY(pchan->quat, chan->quat);
698                                 pchan->flag= chan->flag;
699                                 
700                                 if (flip){
701                                         pchan->loc[0]*= -1;
702
703                                         QuatToEul(pchan->quat, eul);
704                                         eul[1]*= -1;
705                                         eul[2]*= -1;
706                                         EulToQuat(eul, pchan->quat);
707                                 }
708
709                                 if (G.flags & G_RECORDKEYS){
710                                         ID *id= &ob->id;
711
712                                         /* Set keys on pose */
713                                         if (chan->flag & POSE_ROT){
714                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_X);
715                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_Y);
716                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_Z);
717                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_W);
718                                         }
719                                         if (chan->flag & POSE_SIZE){
720                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_SIZE_X);
721                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_SIZE_Y);
722                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_SIZE_Z);
723                                         }
724                                         if (chan->flag & POSE_LOC){
725                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_LOC_X);
726                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_LOC_Y);
727                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_LOC_Z);
728                                         }
729                                 }
730                         }
731                 }
732         }
733
734         /* Update event for pose and deformation children */
735         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
736         
737         if (G.flags & G_RECORDKEYS) {
738                 remake_action_ipos(ob->action);
739                 allqueue (REDRAWIPO, 0);
740                 allqueue (REDRAWVIEW3D, 0);
741                 allqueue (REDRAWACTION, 0);             
742                 allqueue(REDRAWNLA, 0);
743         }
744         else {
745                 /* need to trick depgraph, action is not allowed to execute on pose */
746                 where_is_pose(ob);
747                 ob->recalc= 0;
748         }
749
750         BIF_undo_push("Paste Action Pose");
751 }
752
753 /* ********************************************** */
754
755 /* context weightpaint and deformer in posemode */
756 void pose_adds_vgroups(Object *meshobj, int heatweights)
757 {
758         extern VPaint Gwp;         /* from vpaint */
759         Object *poseobj= modifiers_isDeformedByArmature(meshobj);
760
761         if(poseobj==NULL || (poseobj->flag & OB_POSEMODE)==0) {
762                 error("The active object must have a deforming armature in pose mode");
763                 return;
764         }
765
766         add_verts_to_dgroups(meshobj, poseobj, heatweights, (Gwp.flag & VP_MIRROR_X));
767
768         if(heatweights)
769                 BIF_undo_push("Apply Bone Heat Weights to Vertex Groups");
770         else
771                 BIF_undo_push("Apply Bone Envelopes to Vertex Groups");
772
773         allqueue(REDRAWVIEW3D, 0);
774         allqueue(REDRAWBUTSEDIT, 0);
775         
776         // and all its relations
777         DAG_object_flush_update(G.scene, meshobj, OB_RECALC_DATA);
778 }
779
780 /* ********************************************** */
781
782 /* context active object */
783 void pose_flip_names(void)
784 {
785         Object *ob= OBACT;
786         bArmature *arm= ob->data;
787         bPoseChannel *pchan;
788         char newname[32];
789         
790         /* paranoia checks */
791         if(!ob && !ob->pose) return;
792         if(ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
793         
794         if(pose_has_protected_selected(ob, 0))
795                 return;
796         
797         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
798                 if(arm->layer & pchan->bone->layer) {
799                         if(pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
800                                 BLI_strncpy(newname, pchan->name, sizeof(newname));
801                                 bone_flip_name(newname, 1);     // 1 = do strip off number extensions
802                                 armature_bone_rename(ob->data, pchan->name, newname);
803                         }
804                 }
805         }
806         
807         allqueue(REDRAWVIEW3D, 0);
808         allqueue(REDRAWBUTSEDIT, 0);
809         allqueue(REDRAWBUTSOBJECT, 0);
810         allqueue (REDRAWACTION, 0);
811         allqueue(REDRAWOOPS, 0);
812         BIF_undo_push("Flip names");
813         
814 }
815
816 /* context active object, or weightpainted object with armature in posemode */
817 void pose_activate_flipped_bone(void)
818 {
819         Object *ob= OBACT;
820         bArmature *arm= ob->data;
821         
822         if(ob==NULL) return;
823
824         if(G.f & G_WEIGHTPAINT) {
825                 ob= modifiers_isDeformedByArmature(ob);
826         }
827         if(ob && (ob->flag & OB_POSEMODE)) {
828                 bPoseChannel *pchan, *pchanf;
829                 
830                 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
831                         if(arm->layer & pchan->bone->layer) {
832                                 if(pchan->bone->flag & BONE_ACTIVE)
833                                         break;
834                         }
835                 }
836                 if(pchan) {
837                         char name[32];
838                         
839                         BLI_strncpy(name, pchan->name, 32);
840                         bone_flip_name(name, 1);        // 0 = do not strip off number extensions
841                         
842                         pchanf= get_pose_channel(ob->pose, name);
843                         if(pchanf && pchanf!=pchan) {
844                                 pchan->bone->flag &= ~(BONE_SELECTED|BONE_ACTIVE);
845                                 pchanf->bone->flag |= (BONE_SELECTED|BONE_ACTIVE);
846                         
847                                 /* in weightpaint we select the associated vertex group too */
848                                 if(G.f & G_WEIGHTPAINT) {
849                                         vertexgroup_select_by_name(OBACT, name);
850                                         DAG_object_flush_update(G.scene, OBACT, OB_RECALC_DATA);
851                                 }
852                                 
853                                 select_actionchannel_by_name(ob->action, name, 1);
854                                 
855                                 allqueue(REDRAWVIEW3D, 0);
856                                 allqueue(REDRAWACTION, 0);
857                                 allqueue(REDRAWIPO, 0);         /* To force action/constraint ipo update */
858                                 allqueue(REDRAWBUTSEDIT, 0);
859                                 allqueue(REDRAWBUTSOBJECT, 0);
860                                 allqueue(REDRAWOOPS, 0);
861                         }                       
862                 }
863         }
864 }
865
866
867 void pose_movetolayer(void)
868 {
869         Object *ob= OBACT;
870         bArmature *arm;
871         short lay= 0;
872         
873         if(ob==NULL) return;
874         arm= ob->data;
875         
876         if(G.qual & LR_SHIFTKEY) {
877                 lay= arm->layer;
878                 if( movetolayer_short_buts(&lay, "Armature Layers")==0 ) return;
879                 if(lay==0) return;
880                 arm->layer= lay;
881                 if(ob->pose)
882                         ob->pose->proxy_layer= lay;
883                 
884                 allqueue(REDRAWVIEW3D, 0);
885                 allqueue(REDRAWACTION, 0);
886                 allqueue(REDRAWBUTSEDIT, 0);
887                 
888         }
889         else if(ob->flag & OB_POSEMODE) {
890                 bPoseChannel *pchan;
891                 
892                 if(pose_has_protected_selected(ob, 0))
893                         return;
894                 
895                 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
896                         if(arm->layer & pchan->bone->layer) {
897                                 if(pchan->bone->flag & BONE_SELECTED)
898                                         lay |= pchan->bone->layer;
899                         }
900                 }
901                 if(lay==0) return;
902                 
903                 if( movetolayer_short_buts(&lay, "Bone Layers")==0 ) return;
904                 if(lay==0) return;
905
906                 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
907                         if(arm->layer & pchan->bone->layer) {
908                                 if(pchan->bone->flag & BONE_SELECTED)
909                                         pchan->bone->layer= lay;
910                         }
911                 }
912                 
913                 BIF_undo_push("Move Bone layer");
914                 allqueue(REDRAWVIEW3D, 0);
915                 allqueue(REDRAWACTION, 0);
916                 allqueue(REDRAWBUTSEDIT, 0);
917         }
918 }