4 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * Contributor(s): Ton Roosendaal, Blender Foundation '05, full recode.
25 * ***** END GPL LICENSE BLOCK *****
26 * support for animation modes - Reevan McKay
32 #include "MEM_guardedalloc.h"
34 #include "BLI_arithb.h"
35 #include "BLI_blenlib.h"
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"
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"
57 #include "BIF_editarmature.h"
58 #include "BIF_editaction.h"
59 #include "BIF_editconstraint.h"
60 #include "BIF_editdeform.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"
69 #include "BDR_editobject.h"
76 void enter_posemode(void)
82 if(G.scene->id.lib) return;
84 if(base==NULL) return;
89 error ("Can't pose libdata");
95 arm= get_armature(ob);
96 if( arm==NULL ) return;
98 ob->flag |= OB_POSEMODE;
101 allqueue(REDRAWHEADERS, 0);
102 allqueue(REDRAWBUTSALL, 0);
103 allqueue(REDRAWOOPS, 0);
104 allqueue(REDRAWVIEW3D, 0);
110 if (G.obedit) exit_editmode(1);
111 G.f &= ~(G_VERTEXPAINT | G_FACESELECT | G_TEXTUREPAINT | G_WEIGHTPAINT);
114 void set_pose_keys (Object *ob)
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;
125 chan->flag &= ~POSE_KEY;
132 void exit_posemode(void)
139 ob->flag &= ~OB_POSEMODE;
140 base->flag= ob->flag;
143 allqueue(REDRAWVIEW3D, 0);
144 allqueue(REDRAWOOPS, 0);
145 allqueue(REDRAWHEADERS, 0);
146 allqueue(REDRAWBUTSALL, 0);
148 scrarea_queue_headredraw(curarea);
151 void pose_select_constraint_target(void)
157 /* paranoia checks */
158 if(!ob && !ob->pose) return;
159 if(ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
161 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
162 if(pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
164 for(con= pchan->constraints.first; con; con= con->next) {
166 Object *target= get_constraint_target(con, &subtarget);
170 bPoseChannel *pchanc= get_pose_channel(ob->pose, subtarget);
171 pchanc->bone->flag |= BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL;
178 allqueue (REDRAWVIEW3D, 0);
179 allqueue (REDRAWBUTSOBJECT, 0);
180 allqueue (REDRAWOOPS, 0);
182 BIF_undo_push("Select constraint target");
186 /* context: active channel */
187 void pose_special_editmenu(void)
192 /* paranoia checks */
193 if(!ob && !ob->pose) return;
194 if(ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
196 nr= pupmenu("Specials%t|Select Constraint Target%x1|Flip Left-Right Names%x2");
198 pose_select_constraint_target();
205 /* context: active object, active channel, optional selected channel */
206 void pose_add_IK(void)
209 bPoseChannel *pchanact, *pchansel;
213 /* paranoia checks */
214 if(!ob && !ob->pose) return;
215 if(ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
218 for(pchanact= ob->pose->chanbase.first; pchanact; pchanact= pchanact->next)
219 if(pchanact->bone->flag & BONE_ACTIVE) break;
220 if(pchanact==NULL) return;
223 for(pchansel= ob->pose->chanbase.first; pchansel; pchansel= pchansel->next) {
224 if(pchansel!=pchanact)
225 if(pchansel->bone->flag & BONE_SELECTED) break;
228 for(con= pchanact->constraints.first; con; con= con->next) {
229 if(con->type==CONSTRAINT_TYPE_KINEMATIC) break;
232 error("Pose Channel already has IK");
237 nr= pupmenu("Add IK Constraint%t|To new Empty Object%x1|To selected Bone%x2");
239 nr= pupmenu("Add IK Constraint%t|To new Empty Object%x1");
243 /* prevent weird chains... */
245 bPoseChannel *pchan= pchanact;
247 if(pchan==pchansel) break;
248 if(pchan->bone->flag & BONE_IK_TOPARENT)
249 pchan= pchan->parent;
253 error("IK target should not be in the IK chain itself");
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
262 /* add new empty as target */
264 Base *base= BASACT, *newbase;
267 obt= add_object(OB_EMPTY);
270 newbase->lay= base->lay;
271 obt->lay= newbase->lay;
273 /* transform cent to global coords for loc */
274 VecMat4MulVecfl(obt->loc, ob->obmat, pchanact->pose_tail);
276 set_constraint_target(con, obt, NULL);
278 /* restore, add_object sets active */
280 base->flag |= SELECT;
283 set_constraint_target(con, ob, pchansel->name);
286 ob->pose->flag |= POSE_RECALC; // sort pose channels
287 DAG_scene_sort(G.scene); // sort order of objects
289 DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA); // and all its relations
291 allqueue (REDRAWVIEW3D, 0);
292 allqueue (REDRAWBUTSOBJECT, 0);
293 allqueue (REDRAWOOPS, 0);
295 BIF_undo_push("Add IK constraint");
298 /* context: all selected channels */
299 void pose_clear_IK(void)
306 /* paranoia checks */
307 if(!ob && !ob->pose) return;
308 if(ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
310 if(okee("Remove IK constraint(s)")==0) return;
312 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
313 if(pchan->bone->flag & (BONE_ACTIVE|BONE_SELECTED)) {
315 for(con= pchan->constraints.first; con; con= next) {
317 if(con->type==CONSTRAINT_TYPE_KINEMATIC) {
318 BLI_remlink(&pchan->constraints, con);
319 free_constraint_data(con);
323 pchan->constflag &= ~PCHAN_HAS_IK;
327 DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA); // and all its relations
329 allqueue (REDRAWVIEW3D, 0);
330 allqueue (REDRAWBUTSOBJECT, 0);
331 allqueue (REDRAWOOPS, 0);
333 BIF_undo_push("Remove IK constraint(s)");
336 void pose_clear_constraints(void)
341 /* paranoia checks */
342 if(!ob && !ob->pose) return;
343 if(ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
345 if(okee("Remove Constraints")==0) return;
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);
355 DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA); // and all its relations
357 allqueue (REDRAWVIEW3D, 0);
358 allqueue (REDRAWBUTSOBJECT, 0);
359 allqueue (REDRAWOOPS, 0);
361 BIF_undo_push("Remove Constraint(s)");
366 void pose_copy_menu(void)
369 bPoseChannel *pchan, *pchanact;
372 /* paranoia checks */
373 if(!ob && !ob->pose) return;
374 if(ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
377 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
378 if(pchan->bone->flag & BONE_ACTIVE) break;
381 if(pchan==NULL) return;
384 nr= pupmenu("Copy Pose Attributes %t|Location%x1|Rotation%x2|Size%x3|Constraints");
386 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
387 if(pchan->bone->flag & BONE_SELECTED) {
388 if(pchan!=pchanact) {
390 VECCOPY(pchan->loc, pchanact->loc);
393 QUATCOPY(pchan->quat, pchanact->quat);
396 VECCOPY(pchan->size, pchanact->size);
399 free_constraints(&pchan->constraints);
400 copy_constraints(&pchan->constraints, &pchanact->constraints);
401 pchan->constflag = pchanact->constflag;
407 DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA); // and all its relations
409 allqueue (REDRAWVIEW3D, 0);
410 allqueue (REDRAWBUTSOBJECT, 0);
411 allqueue (REDRAWOOPS, 0);
413 BIF_undo_push("Copy Pose Attributes");
417 /* ******************** copy/paste pose ********************** */
419 static bPose *g_posebuf=NULL;
421 void free_posebuf(void)
424 // was copied without constraints
425 BLI_freelistN (&g_posebuf->chanbase);
426 MEM_freeN (g_posebuf);
431 void copy_posebuf (void)
435 if (!ob || !ob->pose){
442 set_pose_keys(ob); // sets chan->flag to POSE_KEY if bone selected
443 copy_pose(&g_posebuf, ob->pose, 0);
447 void paste_posebuf (int flip)
450 bPoseChannel *chan, *pchan;
455 if (!ob || !ob->pose)
459 error ("Copy buffer is empty");
463 /* Safely merge all of the channels in this pose into
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));
469 bone_flip_name (name, 0); // 0 = don't strip off number extensions
471 /* only copy when channel exists, poses are not meant to add random channels to anymore */
472 pchan= get_pose_channel(ob->pose, name);
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;
485 QuatToEul(pchan->quat, eul);
488 EulToQuat(eul, pchan->quat);
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);
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);
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);
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);
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);
526 /* need to trick depgraph, action is not allowed to execute on pose */
531 BIF_undo_push("Paste Action Pose");
534 /* ********************************************** */
536 void pose_adds_vgroups(Object *meshobj)
538 Object *poseobj= meshobj->parent;
542 Mesh *me= meshobj->data;
544 float head[3], tail[3], vec[3], fac;
547 if(poseobj==NULL || (poseobj->flag & OB_POSEMODE)==0) return;
549 for(pchan= poseobj->pose->chanbase.first; pchan; pchan= pchan->next) {
551 if(bone->flag & (BONE_SELECTED)) {
553 /* check if mesh has vgroups */
554 dg= get_named_vertexgroup(meshobj, bone->name);
556 dg= add_defgroup_name(meshobj, bone->name);
558 /* get the root of the bone in global coords */
559 VECCOPY(head, bone->arm_head);
560 Mat4MulVecfl(poseobj->obmat, head);
562 /* get the tip of the bone in global coords */
563 VECCOPY(tail, bone->arm_tail);
564 Mat4MulVecfl(poseobj->obmat, tail);
566 /* todo; get the optimal vertices instead of mverts */
568 for ( i=0 ; i < me->totvert ; i++ , mvert++) {
569 VECCOPY(vec, mvert->co);
570 Mat4MulVecfl(meshobj->obmat, vec);
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);
575 /* add to vgroup. this call also makes me->dverts */
577 add_vert_to_defgroup (meshobj, dg, i, fac, WEIGHT_REPLACE);
579 remove_vert_defgroup (meshobj, dg, i);
584 allqueue(REDRAWVIEW3D, 0);
585 allqueue(REDRAWBUTSEDIT, 0);
587 DAG_object_flush_update(G.scene, meshobj, OB_RECALC_DATA); // and all its relations
591 /* ********************************************** */
593 /* context active object */
594 void pose_flip_names(void)
600 /* paranoia checks */
601 if(!ob && !ob->pose) return;
602 if(ob==G.obedit || (ob->flag & OB_POSEMODE)==0) return;
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);
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");