616089659f74f3f2dff0ceb94812696dd8db3455
[blender.git] / source / blender / src / poseobject.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL 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/BL DUAL 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_object_types.h"
43 #include "DNA_scene_types.h"
44 #include "DNA_screen_types.h"
45 #include "DNA_view3d_types.h"
46
47 #include "BKE_action.h"
48 #include "BKE_armature.h"
49 #include "BKE_constraint.h"
50 #include "BKE_deform.h"
51 #include "BKE_depsgraph.h"
52 #include "BKE_displist.h"
53 #include "BKE_global.h"
54 #include "BKE_object.h"
55 #include "BKE_utildefines.h"
56
57 #include "BIF_editarmature.h"
58 #include "BIF_editaction.h"
59 #include "BIF_editconstraint.h"
60 #include "BIF_editdeform.h"
61 #include "BIF_gl.h"
62 #include "BIF_graphics.h"
63 #include "BIF_interface.h"
64 #include "BIF_poseobject.h"
65 #include "BIF_space.h"
66 #include "BIF_toolbox.h"
67 #include "BIF_screen.h"
68
69 #include "BDR_editobject.h"
70
71 #include "BSE_edit.h"
72
73 #include "mydevice.h"
74 #include "blendef.h"
75
76 void enter_posemode(void)
77 {
78         Base *base;
79         Object *ob;
80         bArmature *arm;
81         
82         if(G.scene->id.lib) return;
83         base= BASACT;
84         if(base==NULL) return;
85         
86         ob= base->object;
87         
88         if (ob->id.lib){
89                 error ("Can't pose libdata");
90                 return;
91         }
92
93         switch (ob->type){
94         case OB_ARMATURE:
95                 arm= get_armature(ob);
96                 if( arm==NULL ) return;
97                 
98                 ob->flag |= OB_POSEMODE;
99                 base->flag= ob->flag;
100                 
101                 allqueue(REDRAWHEADERS, 0);     
102                 allqueue(REDRAWBUTSALL, 0);     
103                 allqueue(REDRAWOOPS, 0);
104                 allqueue(REDRAWVIEW3D, 0);
105                 break;
106         default:
107                 return;
108         }
109
110         if (G.obedit) exit_editmode(1);
111         G.f &= ~(G_VERTEXPAINT | G_FACESELECT | G_TEXTUREPAINT | G_WEIGHTPAINT);
112 }
113
114 void set_pose_keys (Object *ob)
115 {
116         bPoseChannel *chan;
117
118         if (ob->pose){
119                 for (chan=ob->pose->chanbase.first; chan; chan=chan->next){
120                         Bone *bone= chan->bone;
121                         if(bone && (bone->flag & BONE_SELECTED)) {
122                                 chan->flag |= POSE_KEY;         
123                         }
124                         else {
125                                 chan->flag &= ~POSE_KEY;
126                         }
127                 }
128         }
129 }
130
131
132 void exit_posemode(void)
133 {
134         Object *ob= OBACT;
135         Base *base= BASACT;
136
137         if(ob==NULL) return;
138         
139         ob->flag &= ~OB_POSEMODE;
140         base->flag= ob->flag;
141         
142         countall();
143         allqueue(REDRAWVIEW3D, 0);
144         allqueue(REDRAWOOPS, 0);
145         allqueue(REDRAWHEADERS, 0);     
146         allqueue(REDRAWBUTSALL, 0);     
147
148         scrarea_queue_headredraw(curarea);
149 }
150
151 void pose_select_constraint_target(void)
152 {
153         Object *ob= OBACT;
154         bPoseChannel *pchan;
155         bConstraint *con;
156         
157         /* paranoia checks */
158         if(!ob && !ob->pose) return;
159         if(ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
160         
161         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
162                 if(pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
163                         
164                         for(con= pchan->constraints.first; con; con= con->next) {
165                                 char *subtarget;
166                                 Object *target= get_constraint_target(con, &subtarget);
167                                 
168                                 if(ob==target) {
169                                         if(subtarget) {
170                                                 bPoseChannel *pchanc= get_pose_channel(ob->pose, subtarget);
171                                                 pchanc->bone->flag |= BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL;
172                                         }
173                                 }
174                         }
175                 }
176         }
177         
178         allqueue (REDRAWVIEW3D, 0);
179         allqueue (REDRAWBUTSOBJECT, 0);
180         allqueue (REDRAWOOPS, 0);
181         
182         BIF_undo_push("Select constraint target");
183
184 }
185
186 /* context: active channel */
187 void pose_special_editmenu(void)
188 {
189         Object *ob= OBACT;
190         short nr;
191         
192         /* paranoia checks */
193         if(!ob && !ob->pose) return;
194         if(ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
195         
196         nr= pupmenu("Specials%t|Select Constraint Target%x1|Flip Left-Right Names%x2");
197         if(nr==1) {
198                 pose_select_constraint_target();
199         }
200         else if(nr==2) {
201                 pose_flip_names();
202         }
203 }
204
205 /* context: active object, active channel, optional selected channel */
206 void pose_add_IK(void)
207 {
208         Object *ob= OBACT;
209         bPoseChannel *pchanact, *pchansel;
210         bConstraint *con;
211         short nr;
212         
213         /* paranoia checks */
214         if(!ob && !ob->pose) return;
215         if(ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
216         
217         /* find active */
218         for(pchanact= ob->pose->chanbase.first; pchanact; pchanact= pchanact->next)
219                 if(pchanact->bone->flag & BONE_ACTIVE) break;
220         if(pchanact==NULL) return;
221         
222         /* find selected */
223         for(pchansel= ob->pose->chanbase.first; pchansel; pchansel= pchansel->next) {
224                 if(pchansel!=pchanact)
225                         if(pchansel->bone->flag & BONE_SELECTED) break;
226         }
227         
228         for(con= pchanact->constraints.first; con; con= con->next) {
229                 if(con->type==CONSTRAINT_TYPE_KINEMATIC) break;
230         }
231         if(con) {
232                 error("Pose Channel already has IK");
233                 return;
234         }
235         
236         if(pchansel)
237                 nr= pupmenu("Add IK Constraint%t|To new Empty Object%x1|To selected Bone%x2");
238         else
239                 nr= pupmenu("Add IK Constraint%t|To new Empty Object%x1");
240
241         if(nr<1) return;
242         
243         /* prevent weird chains... */
244         if(nr==2) {
245                 bPoseChannel *pchan= pchanact;
246                 while(pchan) {
247                         if(pchan==pchansel) break;
248                         if(pchan->bone->flag & BONE_IK_TOPARENT)
249                                 pchan= pchan->parent;
250                         else pchan= NULL;
251                 }
252                 if(pchan) {
253                         error("IK target should not be in the IK chain itself");
254                         return;
255                 }
256         }
257
258         con = add_new_constraint(CONSTRAINT_TYPE_KINEMATIC);
259         BLI_addtail(&pchanact->constraints, con);
260         pchanact->constflag |= PCHAN_HAS_IK;    // for draw, but also for detecting while pose solving
261         
262         /* add new empty as target */
263         if(nr==1) {
264                 Base *base= BASACT, *newbase;
265                 Object *obt;
266                 
267                 obt= add_object(OB_EMPTY);
268                 /* set layers OK */
269                 newbase= BASACT;
270                 newbase->lay= base->lay;
271                 obt->lay= newbase->lay;
272                 
273                 /* transform cent to global coords for loc */
274                 VecMat4MulVecfl(obt->loc, ob->obmat, pchanact->pose_tail);
275                 
276                 set_constraint_target(con, obt, NULL);
277                 
278                 /* restore, add_object sets active */
279                 BASACT= base;
280                 base->flag |= SELECT;
281         }
282         else if(nr==2) {
283                 set_constraint_target(con, ob, pchansel->name);
284         }
285         
286         ob->pose->flag |= POSE_RECALC;  // sort pose channels
287         DAG_scene_sort(G.scene);                // sort order of objects
288         
289         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);   // and all its relations
290         
291         allqueue (REDRAWVIEW3D, 0);
292         allqueue (REDRAWBUTSOBJECT, 0);
293         allqueue (REDRAWOOPS, 0);
294         
295         BIF_undo_push("Add IK constraint");
296 }
297
298 /* context: all selected channels */
299 void pose_clear_IK(void)
300 {
301         Object *ob= OBACT;
302         bPoseChannel *pchan;
303         bConstraint *con;
304         bConstraint *next;
305         
306         /* paranoia checks */
307         if(!ob && !ob->pose) return;
308         if(ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
309         
310         if(okee("Remove IK constraint(s)")==0) return;
311
312         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
313                 if(pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
314                         
315                         for(con= pchan->constraints.first; con; con= next) {
316                                 next= con->next;
317                                 if(con->type==CONSTRAINT_TYPE_KINEMATIC) {
318                                         BLI_remlink(&pchan->constraints, con);
319                                         free_constraint_data(con);
320                                         MEM_freeN(con);
321                                 }
322                         }
323                         pchan->constflag &= ~PCHAN_HAS_IK;
324                 }
325         }
326         
327         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);   // and all its relations
328         
329         allqueue (REDRAWVIEW3D, 0);
330         allqueue (REDRAWBUTSOBJECT, 0);
331         allqueue (REDRAWOOPS, 0);
332         
333         BIF_undo_push("Remove IK constraint(s)");
334 }
335
336 void pose_clear_constraints(void)
337 {
338         Object *ob= OBACT;
339         bPoseChannel *pchan;
340         
341         /* paranoia checks */
342         if(!ob && !ob->pose) return;
343         if(ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
344         
345         if(okee("Remove Constraints")==0) return;
346         
347         /* find active */
348         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
349                 if(pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
350                         free_constraints(&pchan->constraints);
351                         pchan->constflag= 0;
352                 }
353         }
354         
355         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);   // and all its relations
356         
357         allqueue (REDRAWVIEW3D, 0);
358         allqueue (REDRAWBUTSOBJECT, 0);
359         allqueue (REDRAWOOPS, 0);
360         
361         BIF_undo_push("Remove Constraint(s)");
362         
363 }
364
365
366 void pose_copy_menu(void)
367 {
368         Object *ob= OBACT;
369         bPoseChannel *pchan, *pchanact;
370         short nr;
371         
372         /* paranoia checks */
373         if(!ob && !ob->pose) return;
374         if(ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
375         
376         /* find active */
377         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
378                 if(pchan->bone->flag & BONE_ACTIVE) break;
379         }
380         
381         if(pchan==NULL) return;
382         pchanact= pchan;
383         
384         nr= pupmenu("Copy Pose Attributes %t|Location%x1|Rotation%x2|Size%x3|Constraints");
385         
386         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
387                 if(pchan->bone->flag & BONE_SELECTED) {
388                         if(pchan!=pchanact) {
389                                 if(nr==1) {
390                                         VECCOPY(pchan->loc, pchanact->loc);
391                                 }
392                                 else if(nr==2) {
393                                         QUATCOPY(pchan->quat, pchanact->quat);
394                                 }
395                                 else if(nr==3) {
396                                         VECCOPY(pchan->size, pchanact->size);
397                                 }
398                                 else if(nr==4) {
399                                         free_constraints(&pchan->constraints);
400                                         copy_constraints(&pchan->constraints, &pchanact->constraints);
401                                         pchan->constflag = pchanact->constflag;
402                                 }
403                         }
404                 }
405         }
406         
407         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);   // and all its relations
408         
409         allqueue (REDRAWVIEW3D, 0);
410         allqueue (REDRAWBUTSOBJECT, 0);
411         allqueue (REDRAWOOPS, 0);
412         
413         BIF_undo_push("Copy Pose Attributes");
414         
415 }
416
417 /* ******************** copy/paste pose ********************** */
418
419 static bPose    *g_posebuf=NULL;
420
421 void free_posebuf(void) 
422 {
423         if (g_posebuf) {
424                 // was copied without constraints
425                 BLI_freelistN (&g_posebuf->chanbase);
426                 MEM_freeN (g_posebuf);
427         }
428         g_posebuf=NULL;
429 }
430
431 void copy_posebuf (void)
432 {
433         Object *ob= OBACT;
434
435         if (!ob || !ob->pose){
436                 error ("No Pose");
437                 return;
438         }
439
440         free_posebuf();
441         
442         set_pose_keys(ob);  // sets chan->flag to POSE_KEY if bone selected
443         copy_pose(&g_posebuf, ob->pose, 0);
444
445 }
446
447 void paste_posebuf (int flip)
448 {
449         Object *ob= OBACT;
450         bPoseChannel *chan, *pchan;
451         float eul[4];
452         int newchan = 0;
453         char name[32];
454         
455         if (!ob || !ob->pose)
456                 return;
457
458         if (!g_posebuf){
459                 error ("Copy buffer is empty");
460                 return;
461         }
462         
463         /* Safely merge all of the channels in this pose into
464         any existing pose */
465         for (chan=g_posebuf->chanbase.first; chan; chan=chan->next){
466                 if (chan->flag & POSE_KEY) {
467                         BLI_strncpy(name, chan->name, sizeof(name));
468                         if (flip)
469                                 bone_flip_name (name, 0);               // 0 = don't strip off number extensions
470                                 
471                         /* only copy when channel exists, poses are not meant to add random channels to anymore */
472                         pchan= get_pose_channel(ob->pose, name);
473                         
474                         if(pchan) {
475                                 /* only loc rot size */
476                                 /* only copies transform info for the pose */
477                                 VECCOPY(pchan->loc, chan->loc);
478                                 VECCOPY(pchan->size, chan->size);
479                                 QUATCOPY(pchan->quat, chan->quat);
480                                 pchan->flag= chan->flag;
481                                 
482                                 if (flip){
483                                         pchan->loc[0]*= -1;
484
485                                         QuatToEul(pchan->quat, eul);
486                                         eul[1]*= -1;
487                                         eul[2]*= -1;
488                                         EulToQuat(eul, pchan->quat);
489                                 }
490
491                                 if (G.flags & G_RECORDKEYS){
492                                         /* Set keys on pose */
493                                         if (chan->flag & POSE_ROT){
494                                                 set_action_key(ob->action, pchan, AC_QUAT_X, newchan);
495                                                 set_action_key(ob->action, pchan, AC_QUAT_Y, newchan);
496                                                 set_action_key(ob->action, pchan, AC_QUAT_Z, newchan);
497                                                 set_action_key(ob->action, pchan, AC_QUAT_W, newchan);
498                                         }
499                                         if (chan->flag & POSE_SIZE){
500                                                 set_action_key(ob->action, pchan, AC_SIZE_X, newchan);
501                                                 set_action_key(ob->action, pchan, AC_SIZE_Y, newchan);
502                                                 set_action_key(ob->action, pchan, AC_SIZE_Z, newchan);
503                                         }
504                                         if (chan->flag & POSE_LOC){
505                                                 set_action_key(ob->action, pchan, AC_LOC_X, newchan);
506                                                 set_action_key(ob->action, pchan, AC_LOC_Y, newchan);
507                                                 set_action_key(ob->action, pchan, AC_LOC_Z, newchan);
508                                         }
509                                 }
510                         }
511                 }
512         }
513
514         /* Update event for pose and deformation children */
515         ob->pose->ctime= -123456.0f;
516         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
517         
518         if (G.flags & G_RECORDKEYS) {
519                 remake_action_ipos(ob->action);
520                 allqueue (REDRAWIPO, 0);
521                 allqueue (REDRAWVIEW3D, 0);
522                 allqueue (REDRAWACTION, 0);             
523                 allqueue(REDRAWNLA, 0);
524         }
525         else {
526                 /* need to trick depgraph, action is not allowed to execute on pose */
527                 where_is_pose(ob);
528                 ob->recalc= 0;
529         }
530
531         BIF_undo_push("Paste Action Pose");
532 }
533
534 /* ********************************************** */
535
536 void pose_adds_vgroups(Object *meshobj)
537 {
538         Object *poseobj= meshobj->parent;
539         bPoseChannel *pchan;
540         Bone *bone;
541         bDeformGroup *dg;
542         Mesh *me= meshobj->data;
543         MVert *mvert;
544         float head[3], tail[3], vec[3], fac;
545         int i;
546         
547         if(poseobj==NULL || (poseobj->flag & OB_POSEMODE)==0) return;
548         
549         for(pchan= poseobj->pose->chanbase.first; pchan; pchan= pchan->next) {
550                 bone= pchan->bone;
551                 if(bone->flag & (BONE_SELECTED)) {
552                         
553                         /* check if mesh has vgroups */
554                         dg= get_named_vertexgroup(meshobj, bone->name);
555                         if(dg==NULL)
556                                 dg= add_defgroup_name(meshobj, bone->name);
557                         
558                         /* get the root of the bone in global coords */
559                         VECCOPY(head, bone->arm_head);
560                         Mat4MulVecfl(poseobj->obmat, head);
561                         
562             /* get the tip of the bone in global coords */
563                         VECCOPY(tail, bone->arm_tail);
564             Mat4MulVecfl(poseobj->obmat, tail);
565                         
566                         /* todo; get the optimal vertices instead of mverts */
567                         mvert= me->mvert;
568                         for ( i=0 ; i < me->totvert ; i++ , mvert++) {
569                                 VECCOPY(vec, mvert->co);
570                                  Mat4MulVecfl(meshobj->obmat, vec);
571                                  
572                                  /* get the distance-factor from the vertex to bone */
573                                  fac= distfactor_to_bone (vec, head, tail, bone->rad_head, bone->rad_tail, bone->dist);
574                                  
575                                  /* add to vgroup. this call also makes me->dverts */
576                                  if(fac!=0.0f) 
577                                          add_vert_to_defgroup (meshobj, dg, i, fac, WEIGHT_REPLACE);
578                                  else
579                                          remove_vert_defgroup (meshobj, dg, i);
580                         }
581                 }
582         }
583         
584         allqueue(REDRAWVIEW3D, 0);
585         allqueue(REDRAWBUTSEDIT, 0);
586         
587         DAG_object_flush_update(G.scene, meshobj, OB_RECALC_DATA);      // and all its relations
588
589 }
590
591 /* ********************************************** */
592
593 /* context active object */
594 void pose_flip_names(void)
595 {
596         Object *ob= OBACT;
597         bPoseChannel *pchan;
598         char newname[32];
599         
600         /* paranoia checks */
601         if(!ob && !ob->pose) return;
602         if(ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
603         
604         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
605                 if(pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
606                         BLI_strncpy(newname, pchan->name, sizeof(newname));
607                         bone_flip_name(newname, 1);     // 1 = do strip off number extensions
608                         armature_bone_rename(ob->data, pchan->name, newname);
609                 }
610         }
611         
612         allqueue(REDRAWVIEW3D, 0);
613         allqueue(REDRAWBUTSEDIT, 0);
614         allqueue(REDRAWBUTSOBJECT, 0);
615         allqueue (REDRAWACTION, 0);
616         allqueue(REDRAWOOPS, 0);
617         BIF_undo_push("Flip names");
618         
619 }