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