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