Bone Layer feature: now also Armature layers have a hotkey menu; Shift+m.
[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_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_IS_PROXY(ob)) {
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         
233         if(ob==NULL || ob->pose==NULL)
234                 return;
235         arm= ob->data;
236         
237         if(EFRA<=SFRA) return;
238         
239         DAG_object_update_flags(G.scene, ob, screen_view3d_layers());
240         
241         /* malloc the path blocks */
242         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
243                 if(pchan->bone && (pchan->bone->flag & BONE_SELECTED)) {
244                         if(arm->layer & pchan->bone->layer) {
245                                 pchan->pathlen= EFRA-SFRA+1;
246                                 if(pchan->path)
247                                         MEM_freeN(pchan->path);
248                                 pchan->path= MEM_callocN(3*pchan->pathlen*sizeof(float), "pchan path");
249                         }
250                 }
251         }
252         
253         cfra= CFRA;
254         for(CFRA=SFRA; CFRA<=EFRA; CFRA++) {
255                 
256                 /* do all updates */
257                 for(base= FIRSTBASE; base; base= base->next) {
258                         if(base->object->recalc) {
259                                 int temp= base->object->recalc;
260                                 object_handle_update(base->object);
261                                 base->object->recalc= temp;
262                         }
263                 }
264                 
265                 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
266                         if(pchan->bone && (pchan->bone->flag & BONE_SELECTED)) {
267                                 if(arm->layer & pchan->bone->layer) {
268                                         if(pchan->path) {
269                                                 fp= pchan->path+3*(CFRA-SFRA);
270                                                 VECCOPY(fp, pchan->pose_tail);
271                                                 Mat4MulVecfl(ob->obmat, fp);
272                                         }
273                                 }
274                         }
275                 }
276         }
277         
278         CFRA= cfra;
279         allqueue(REDRAWVIEW3D, 0);      /* recalc tags are still there */
280 }
281
282
283 /* for the object with pose/action: clear all path curves */
284 void pose_clear_paths(Object *ob)
285 {
286         bPoseChannel *pchan;
287         
288         if(ob==NULL || ob->pose==NULL)
289                 return;
290         
291         /* free the path blocks */
292         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
293                 if(pchan->path) {
294                         MEM_freeN(pchan->path);
295                         pchan->path= NULL;
296                 }
297         }
298         
299         allqueue(REDRAWVIEW3D, 0);
300 }
301
302
303
304 void pose_select_constraint_target(void)
305 {
306         Object *ob= OBACT;
307         bArmature *arm= ob->data;
308         bPoseChannel *pchan;
309         bConstraint *con;
310         
311         /* paranoia checks */
312         if(!ob && !ob->pose) return;
313         if(ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
314         
315         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
316                 if(arm->layer & pchan->bone->layer) {
317                         if(pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
318                                 
319                                 for(con= pchan->constraints.first; con; con= con->next) {
320                                         char *subtarget;
321                                         Object *target= get_constraint_target(con, &subtarget);
322                                         
323                                         if(ob==target) {
324                                                 if(subtarget) {
325                                                         bPoseChannel *pchanc= get_pose_channel(ob->pose, subtarget);
326                                                         if(pchanc)
327                                                                 pchanc->bone->flag |= BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL;
328                                                 }
329                                         }
330                                 }
331                         }
332                 }
333         }
334         
335         allqueue (REDRAWVIEW3D, 0);
336         allqueue (REDRAWBUTSOBJECT, 0);
337         allqueue (REDRAWOOPS, 0);
338         
339         BIF_undo_push("Select constraint target");
340
341 }
342
343 /* context: active channel */
344 void pose_special_editmenu(void)
345 {
346         Object *ob= OBACT;
347         short nr;
348         
349         /* paranoia checks */
350         if(!ob && !ob->pose) return;
351         if(ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
352         
353         nr= pupmenu("Specials%t|Select Constraint Target%x1|Flip Left-Right Names%x2|Calculate Paths%x3|Clear All Paths%x4|Clear Pose %x5");
354         if(nr==1) {
355                 pose_select_constraint_target();
356         }
357         else if(nr==2) {
358                 pose_flip_names();
359         }
360         else if(nr==3) {
361                 pose_calculate_path(ob);
362         }
363         else if(nr==4) {
364                 pose_clear_paths(ob);
365         }
366         else if(nr==5) {
367                 rest_pose(ob->pose);
368                 DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
369                 BIF_undo_push("Clear Pose");
370         }
371 }
372
373 void pose_add_IK(void)
374 {
375         Object *ob= OBACT;
376         
377         /* paranoia checks */
378         if(!ob && !ob->pose) return;
379         if(ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
380         
381         add_constraint(1);      /* 1 means only IK */
382 }
383
384 /* context: all selected channels */
385 void pose_clear_IK(void)
386 {
387         Object *ob= OBACT;
388         bArmature *arm= ob->data;
389         bPoseChannel *pchan;
390         bConstraint *con;
391         bConstraint *next;
392         
393         /* paranoia checks */
394         if(!ob && !ob->pose) return;
395         if(ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
396         
397         if(pose_has_protected_selected(ob, 0))
398                 return;
399         
400         if(okee("Remove IK constraint(s)")==0) return;
401
402         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
403                 if(arm->layer & pchan->bone->layer) {
404                         if(pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
405                                 
406                                 for(con= pchan->constraints.first; con; con= next) {
407                                         next= con->next;
408                                         if(con->type==CONSTRAINT_TYPE_KINEMATIC) {
409                                                 BLI_remlink(&pchan->constraints, con);
410                                                 free_constraint_data(con);
411                                                 MEM_freeN(con);
412                                         }
413                                 }
414                                 pchan->constflag &= ~(PCHAN_HAS_IK|PCHAN_HAS_TARGET);
415                         }
416                 }
417         }
418         
419         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);   // and all its relations
420         
421         allqueue (REDRAWVIEW3D, 0);
422         allqueue (REDRAWBUTSOBJECT, 0);
423         allqueue (REDRAWOOPS, 0);
424         
425         BIF_undo_push("Remove IK constraint(s)");
426 }
427
428 void pose_clear_constraints(void)
429 {
430         Object *ob= OBACT;
431         bArmature *arm= ob->data;
432         bPoseChannel *pchan;
433         
434         /* paranoia checks */
435         if(!ob && !ob->pose) return;
436         if(ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
437         
438         if(pose_has_protected_selected(ob, 0))
439                 return;
440         
441         if(okee("Remove Constraints")==0) return;
442         
443         /* find active */
444         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
445                 if(arm->layer & pchan->bone->layer) {
446                         if(pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
447                                 free_constraints(&pchan->constraints);
448                                 pchan->constflag= 0;
449                         }
450                 }
451         }
452         
453         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);   // and all its relations
454         
455         allqueue (REDRAWVIEW3D, 0);
456         allqueue (REDRAWBUTSOBJECT, 0);
457         allqueue (REDRAWOOPS, 0);
458         
459         BIF_undo_push("Remove Constraint(s)");
460         
461 }
462
463
464 void pose_copy_menu(void)
465 {
466         Object *ob= OBACT;
467         bArmature *arm= ob->data;
468         bPoseChannel *pchan, *pchanact;
469         short nr;
470         int i=0;
471         
472         /* paranoia checks */
473         if(!ob && !ob->pose) return;
474         if(ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
475         
476         /* find active */
477         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
478                 if(pchan->bone->flag & BONE_ACTIVE) break;
479         }
480         
481         if(pchan==NULL) return;
482         
483         if(pose_has_protected_selected(ob, 1))
484                 return;
485
486         pchanact= pchan;
487         
488         i= BLI_countlist(&(pchanact->constraints)); /* if there are 24 or less, allow for the user to select constraints */
489         if (i<25)
490                 nr= pupmenu("Copy Pose Attributes %t|Location%x1|Rotation%x2|Size%x3|Constraints (All)%x4|Constraints...%x5|Transform Locks%x6|IK Limits%x7|Bone Shape%x8");
491         else
492                 nr= pupmenu("Copy Pose Attributes %t|Location%x1|Rotation%x2|Size%x3|Constraints (All)%x4|Transform Locks%x6|IK Limits%x7|Bone Shape%x8");
493         
494         if(nr==-1) return;
495         if(nr!=5)  {
496                 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
497                         if(     (arm->layer & pchan->bone->layer) &&
498                                 (pchan->bone->flag & BONE_SELECTED) &&
499                                 (pchan!=pchanact)
500                         ) {
501                                 switch (nr) {
502                                         case 1:
503                                                 VECCOPY(pchan->loc, pchanact->loc);
504                                                 break;
505                                         case 2:
506                                                 QUATCOPY(pchan->quat, pchanact->quat);
507                                                 break;
508                                         case 3:
509                                                 VECCOPY(pchan->size, pchanact->size);
510                                                 break;
511                                         case 4:
512                                         {
513                                                 free_constraints(&pchan->constraints);
514                                                 copy_constraints(&pchan->constraints, &pchanact->constraints);
515                                                 pchan->constflag = pchanact->constflag;
516                                         }
517                                                 break;
518                                         case 6:
519                                                 pchan->protectflag = pchanact->protectflag;
520                                                 break;
521                                         case 7:
522                                         {
523                                                 pchan->ikflag = pchanact->ikflag;
524                                                 VECCOPY(pchan->limitmin, pchanact->limitmin);
525                                                 VECCOPY(pchan->limitmax, pchanact->limitmax);
526                                                 VECCOPY(pchan->stiffness, pchanact->stiffness);
527                                                 pchan->ikstretch= pchanact->ikstretch;
528                                         }
529                                                 break;
530                                         case 8:
531                                                 pchan->custom = pchanact->custom;
532                                                 break;
533                                 }
534                         }
535                 }
536         } else { /* constraints, optional */
537                 bConstraint *con, *con_back;
538                 int const_toggle[24];
539                 ListBase const_copy={0, 0};
540                 
541                 duplicatelist (&const_copy, &(pchanact->constraints));
542                 
543                 /* build the puplist of constraints */
544                 for (con = pchanact->constraints.first, i=0; con; con=con->next, i++){
545                         const_toggle[i]= 1;
546                         add_numbut(i, TOG|INT, con->name, 0, 0, &(const_toggle[i]), "");
547                 }
548                 
549                 if (!do_clever_numbuts("Select Constraints", i, REDRAW)) {
550                         BLI_freelistN(&const_copy);
551                         return;
552                 }
553                 
554                 /* now build a new listbase from the options selected */
555                 for (i=0, con=const_copy.first; con; i++) {
556                         if (!const_toggle[i]) {
557                                 con_back= con->next;
558                                 BLI_freelinkN(&const_copy, con);
559                                 con= con_back;
560                         } else {
561                                 con= con->next;
562                         }
563                 }
564                 
565                 /* Copy the temo listbase to the selected posebones */
566                 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
567                         if(     (arm->layer & pchan->bone->layer) &&
568                                 (pchan->bone->flag & BONE_SELECTED) &&
569                                 (pchan!=pchanact)
570                         ) {
571                                 free_constraints(&pchan->constraints);
572                                 copy_constraints(&pchan->constraints, &const_copy);
573                                 pchan->constflag = pchanact->constflag;
574                         }
575                 }
576                 BLI_freelistN(&const_copy);
577                 update_pose_constraint_flags(ob->pose); /* we could work out the flags but its simpler to do this */
578         }
579         
580         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);   // and all its relations
581         
582         allqueue (REDRAWVIEW3D, 0);
583         allqueue (REDRAWBUTSOBJECT, 0);
584         allqueue (REDRAWOOPS, 0);
585         
586         BIF_undo_push("Copy Pose Attributes");
587         
588 }
589
590 /* ******************** copy/paste pose ********************** */
591
592 static bPose    *g_posebuf=NULL;
593
594 void free_posebuf(void) 
595 {
596         if (g_posebuf) {
597                 // was copied without constraints
598                 BLI_freelistN (&g_posebuf->chanbase);
599                 MEM_freeN (g_posebuf);
600         }
601         g_posebuf=NULL;
602 }
603
604 void copy_posebuf (void)
605 {
606         Object *ob= OBACT;
607
608         if (!ob || !ob->pose){
609                 error ("No Pose");
610                 return;
611         }
612
613         free_posebuf();
614         
615         set_pose_keys(ob);  // sets chan->flag to POSE_KEY if bone selected
616         copy_pose(&g_posebuf, ob->pose, 0);
617
618 }
619
620 void paste_posebuf (int flip)
621 {
622         Object *ob= OBACT;
623         bPoseChannel *chan, *pchan;
624         float eul[4];
625         char name[32];
626         
627         if (!ob || !ob->pose)
628                 return;
629
630         if (!g_posebuf){
631                 error ("Copy buffer is empty");
632                 return;
633         }
634         
635         /* Safely merge all of the channels in this pose into
636         any existing pose */
637         for (chan=g_posebuf->chanbase.first; chan; chan=chan->next){
638                 if (chan->flag & POSE_KEY) {
639                         BLI_strncpy(name, chan->name, sizeof(name));
640                         if (flip)
641                                 bone_flip_name (name, 0);               // 0 = don't strip off number extensions
642                                 
643                         /* only copy when channel exists, poses are not meant to add random channels to anymore */
644                         pchan= get_pose_channel(ob->pose, name);
645                         
646                         if(pchan) {
647                                 /* only loc rot size */
648                                 /* only copies transform info for the pose */
649                                 VECCOPY(pchan->loc, chan->loc);
650                                 VECCOPY(pchan->size, chan->size);
651                                 QUATCOPY(pchan->quat, chan->quat);
652                                 pchan->flag= chan->flag;
653                                 
654                                 if (flip){
655                                         pchan->loc[0]*= -1;
656
657                                         QuatToEul(pchan->quat, eul);
658                                         eul[1]*= -1;
659                                         eul[2]*= -1;
660                                         EulToQuat(eul, pchan->quat);
661                                 }
662
663                                 if (G.flags & G_RECORDKEYS){
664                                         ID *id= &ob->id;
665
666                                         /* Set keys on pose */
667                                         if (chan->flag & POSE_ROT){
668                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_X);
669                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_Y);
670                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_Z);
671                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_W);
672                                         }
673                                         if (chan->flag & POSE_SIZE){
674                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_SIZE_X);
675                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_SIZE_Y);
676                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_SIZE_Z);
677                                         }
678                                         if (chan->flag & POSE_LOC){
679                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_LOC_X);
680                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_LOC_Y);
681                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_LOC_Z);
682                                         }
683                                 }
684                         }
685                 }
686         }
687
688         /* Update event for pose and deformation children */
689         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
690         
691         if (G.flags & G_RECORDKEYS) {
692                 remake_action_ipos(ob->action);
693                 allqueue (REDRAWIPO, 0);
694                 allqueue (REDRAWVIEW3D, 0);
695                 allqueue (REDRAWACTION, 0);             
696                 allqueue(REDRAWNLA, 0);
697         }
698         else {
699                 /* need to trick depgraph, action is not allowed to execute on pose */
700                 where_is_pose(ob);
701                 ob->recalc= 0;
702         }
703
704         BIF_undo_push("Paste Action Pose");
705 }
706
707 /* ********************************************** */
708
709 struct vgroup_map {
710         float head[3], tail[3];
711         Bone *bone;
712         bDeformGroup *dg, *dgflip;
713         Object *meshobj;
714 };
715
716 static void pose_adds_vgroups__mapFunc(void *userData, int index, float *co, float *no_f, short *no_s)
717 {
718         struct vgroup_map *map= userData;
719         float vec[3], fac;
720         
721         VECCOPY(vec, co);
722         Mat4MulVecfl(map->meshobj->obmat, vec);
723                 
724         /* get the distance-factor from the vertex to bone */
725         fac= distfactor_to_bone (vec, map->head, map->tail, map->bone->rad_head, map->bone->rad_tail, map->bone->dist);
726         
727         /* add to vgroup. this call also makes me->dverts */
728         if(fac!=0.0f) 
729                 add_vert_to_defgroup (map->meshobj, map->dg, index, fac, WEIGHT_REPLACE);
730         else
731                 remove_vert_defgroup (map->meshobj, map->dg, index);
732         
733         if(map->dgflip) {
734                 int j= mesh_get_x_mirror_vert(map->meshobj, index);
735                 if(j>=0) {
736                         if(fac!=0.0f) 
737                                 add_vert_to_defgroup (map->meshobj, map->dgflip, j, fac, WEIGHT_REPLACE);
738                         else
739                                 remove_vert_defgroup (map->meshobj, map->dgflip, j);
740                 }
741         }
742 }
743
744 /* context weightpaint and deformer in posemode */
745 void pose_adds_vgroups(Object *meshobj)
746 {
747         extern VPaint Gwp;         /* from vpaint */
748         struct vgroup_map map;
749         DerivedMesh *dm;
750         Object *poseobj= modifiers_isDeformedByArmature(meshobj);
751         bArmature *arm= poseobj->data;
752         bPoseChannel *pchan;
753         Bone *bone;
754         bDeformGroup *dg, *curdef;
755         
756         if(poseobj==NULL || (poseobj->flag & OB_POSEMODE)==0) return;
757         
758         dm = mesh_get_derived_final(meshobj);
759         
760         map.meshobj= meshobj;
761         
762         for(pchan= poseobj->pose->chanbase.first; pchan; pchan= pchan->next) {
763                 bone= pchan->bone;
764                 if(arm->layer & pchan->bone->layer) {
765                         if(bone->flag & (BONE_SELECTED)) {
766                                 
767                                 /* check if mesh has vgroups */
768                                 dg= get_named_vertexgroup(meshobj, bone->name);
769                                 if(dg==NULL)
770                                         dg= add_defgroup_name(meshobj, bone->name);
771                                 
772                                 /* flipped bone */
773                                 if(Gwp.flag & VP_MIRROR_X) {
774                                         char name[32];
775                                         
776                                         BLI_strncpy(name, dg->name, 32);
777                                         bone_flip_name(name, 0);                // 0 = don't strip off number extensions
778                                         
779                                         for (curdef = meshobj->defbase.first; curdef; curdef=curdef->next)
780                                                 if (!strcmp(curdef->name, name))
781                                                         break;
782                                         map.dgflip= curdef;
783                                 }
784                                 else map.dgflip= NULL;
785                                 
786                                 /* get the root of the bone in global coords */
787                                 VECCOPY(map.head, bone->arm_head);
788                                 Mat4MulVecfl(poseobj->obmat, map.head);
789                                 
790                                 /* get the tip of the bone in global coords */
791                                 VECCOPY(map.tail, bone->arm_tail);
792                                 Mat4MulVecfl(poseobj->obmat, map.tail);
793                                 
794                                 /* use the optimal vertices instead of mverts */
795                                 map.dg= dg;
796                                 map.bone= bone;
797                                 if(dm->foreachMappedVert) 
798                                         dm->foreachMappedVert(dm, pose_adds_vgroups__mapFunc, (void*) &map);
799                                 else {
800                                         Mesh *me= meshobj->data;
801                                         int i;
802                                         for(i=0; i<me->totvert; i++) 
803                                                 pose_adds_vgroups__mapFunc(&map, i, (me->mvert+i)->co, NULL, NULL);
804                                 }
805                                 
806                         }
807                 }
808         }
809         
810         dm->release(dm);
811
812         allqueue(REDRAWVIEW3D, 0);
813         allqueue(REDRAWBUTSEDIT, 0);
814         
815         DAG_object_flush_update(G.scene, meshobj, OB_RECALC_DATA);      // and all its relations
816
817 }
818
819 /* ********************************************** */
820
821 /* context active object */
822 void pose_flip_names(void)
823 {
824         Object *ob= OBACT;
825         bArmature *arm= ob->data;
826         bPoseChannel *pchan;
827         char newname[32];
828         
829         /* paranoia checks */
830         if(!ob && !ob->pose) return;
831         if(ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
832         
833         if(pose_has_protected_selected(ob, 0))
834                 return;
835         
836         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
837                 if(arm->layer & pchan->bone->layer) {
838                         if(pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
839                                 BLI_strncpy(newname, pchan->name, sizeof(newname));
840                                 bone_flip_name(newname, 1);     // 1 = do strip off number extensions
841                                 armature_bone_rename(ob->data, pchan->name, newname);
842                         }
843                 }
844         }
845         
846         allqueue(REDRAWVIEW3D, 0);
847         allqueue(REDRAWBUTSEDIT, 0);
848         allqueue(REDRAWBUTSOBJECT, 0);
849         allqueue (REDRAWACTION, 0);
850         allqueue(REDRAWOOPS, 0);
851         BIF_undo_push("Flip names");
852         
853 }
854
855 /* context active object, or weightpainted object with armature in posemode */
856 void pose_activate_flipped_bone(void)
857 {
858         Object *ob= OBACT;
859         bArmature *arm= ob->data;
860         
861         if(ob==NULL) return;
862
863         if(G.f & G_WEIGHTPAINT) {
864                 ob= modifiers_isDeformedByArmature(ob);
865         }
866         if(ob && (ob->flag & OB_POSEMODE)) {
867                 bPoseChannel *pchan, *pchanf;
868                 
869                 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
870                         if(arm->layer & pchan->bone->layer) {
871                                 if(pchan->bone->flag & BONE_ACTIVE)
872                                         break;
873                         }
874                 }
875                 if(pchan) {
876                         char name[32];
877                         
878                         BLI_strncpy(name, pchan->name, 32);
879                         bone_flip_name(name, 1);        // 0 = do not strip off number extensions
880                         
881                         pchanf= get_pose_channel(ob->pose, name);
882                         if(pchanf && pchanf!=pchan) {
883                                 pchan->bone->flag &= ~(BONE_SELECTED|BONE_ACTIVE);
884                                 pchanf->bone->flag |= (BONE_SELECTED|BONE_ACTIVE);
885                         
886                                 /* in weightpaint we select the associated vertex group too */
887                                 if(G.f & G_WEIGHTPAINT) {
888                                         vertexgroup_select_by_name(OBACT, name);
889                                         DAG_object_flush_update(G.scene, OBACT, OB_RECALC_DATA);
890                                 }
891                                 
892                                 select_actionchannel_by_name(ob->action, name, 1);
893                                 
894                                 allqueue(REDRAWVIEW3D, 0);
895                                 allqueue(REDRAWACTION, 0);
896                                 allqueue(REDRAWIPO, 0);         /* To force action/constraint ipo update */
897                                 allqueue(REDRAWBUTSEDIT, 0);
898                                 allqueue(REDRAWBUTSOBJECT, 0);
899                                 allqueue(REDRAWOOPS, 0);
900                         }                       
901                 }
902         }
903 }
904
905
906 void pose_movetolayer(void)
907 {
908         Object *ob= OBACT;
909         bArmature *arm;
910         short lay= 0;
911         
912         if(ob==NULL) return;
913         arm= ob->data;
914         
915         if(G.qual & LR_SHIFTKEY) {
916                 lay= arm->layer;
917                 if( movetolayer_short_buts(&lay, "Armature Layers")==0 ) return;
918                 if(lay==0) return;
919                 arm->layer= lay;
920                 
921                 allqueue(REDRAWVIEW3D, 0);
922                 allqueue(REDRAWACTION, 0);
923                 allqueue(REDRAWBUTSEDIT, 0);
924                 
925         }
926         else if(ob->flag & OB_POSEMODE) {
927                 bPoseChannel *pchan;
928                 
929                 if(pose_has_protected_selected(ob, 0))
930                         return;
931                 
932                 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
933                         if(arm->layer & pchan->bone->layer) {
934                                 if(pchan->bone->flag & BONE_SELECTED)
935                                         lay |= pchan->bone->layer;
936                         }
937                 }
938                 if(lay==0) return;
939                 
940                 if( movetolayer_short_buts(&lay, "Bone Layers")==0 ) return;
941                 if(lay==0) return;
942
943                 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
944                         if(arm->layer & pchan->bone->layer) {
945                                 if(pchan->bone->flag & BONE_SELECTED)
946                                         pchan->bone->layer= lay;
947                         }
948                 }
949                 
950                 allqueue(REDRAWVIEW3D, 0);
951                 allqueue(REDRAWACTION, 0);
952                 allqueue(REDRAWBUTSEDIT, 0);
953         }
954 }