2.5 - Clear Constraints Operators
[blender.git] / source / blender / editors / object / editconstraint.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  * The Original Code is: all of this file.
24  *
25  * Contributor(s): Joshua Leung, Blender Foundation
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #include <stdio.h>
31 #include <string.h>
32
33 #include "MEM_guardedalloc.h"
34
35 #include "BLI_blenlib.h"
36 #include "BLI_arithb.h"
37 #include "BLI_dynstr.h"
38
39 #include "DNA_action_types.h"
40 #include "DNA_armature_types.h"
41 #include "DNA_constraint_types.h"
42 #include "DNA_curve_types.h"
43 #include "DNA_object_types.h"
44 #include "DNA_scene_types.h"
45 #include "DNA_screen_types.h"
46 #include "DNA_text_types.h"
47 #include "DNA_view3d_types.h"
48
49 #include "BKE_action.h"
50 #include "BKE_armature.h"
51 #include "BKE_constraint.h"
52 #include "BKE_context.h"
53 #include "BKE_depsgraph.h"
54 #include "BKE_global.h"
55 #include "BKE_main.h"
56 #include "BKE_object.h"
57 #include "BKE_report.h"
58 #include "BKE_utildefines.h"
59
60 #ifndef DISABLE_PYTHON
61 #include "BPY_extern.h"
62 #endif
63
64 #include "WM_api.h"
65 #include "WM_types.h"
66
67 #include "RNA_access.h"
68 #include "RNA_define.h"
69 #include "RNA_enum_types.h"
70
71 #include "ED_object.h"
72 #include "ED_screen.h"
73
74 #include "object_intern.h"
75
76 /* XXX */
77 static void BIF_undo_push() {}
78 static void error() {}
79 static int okee() {return 0;}
80 static int pupmenu() {return 0;}
81
82 /* -------------- Get Active Constraint Data ---------------------- */
83
84 /* if object in posemode, active bone constraints, else object constraints */
85 ListBase *get_active_constraints (Object *ob)
86 {
87         if (ob == NULL)
88                 return NULL;
89
90         if (ob->flag & OB_POSEMODE) {
91                 bPoseChannel *pchan;
92                 
93                 pchan = get_active_posechannel(ob);
94                 if (pchan)
95                         return &pchan->constraints;
96         }
97         else 
98                 return &ob->constraints;
99
100         return NULL;
101 }
102
103 /* single constraint */
104 bConstraint *get_active_constraint (Object *ob)
105 {
106         ListBase *lb= get_active_constraints(ob);
107
108         if (lb) {
109                 bConstraint *con;
110                 
111                 for (con= lb->first; con; con=con->next) {
112                         if (con->flag & CONSTRAINT_ACTIVE)
113                                 return con;
114                 }
115         }
116         
117         return NULL;
118 }
119 /* -------------- Constraint Management (Add New, Remove, Rename) -------------------- */
120 /* ------------- PyConstraints ------------------ */
121
122 /* this callback sets the text-file to be used for selected menu item */
123 void validate_pyconstraint_cb (void *arg1, void *arg2)
124 {
125         bPythonConstraint *data = arg1;
126         Text *text= NULL;
127         int index = *((int *)arg2);
128         int i;
129         
130         /* exception for no script */
131         if (index) {
132                 /* innovative use of a for...loop to search */
133                 for (text=G.main->text.first, i=1; text && index!=i; i++, text=text->id.next);
134         }
135         data->text = text;
136 }
137
138 #ifndef DISABLE_PYTHON
139 /* this returns a string for the list of usable pyconstraint script names */
140 char *buildmenu_pyconstraints (Text *con_text, int *pyconindex)
141 {
142         DynStr *pupds= BLI_dynstr_new();
143         Text *text;
144         char *str;
145         char buf[64];
146         int i;
147         
148         /* add title first */
149         sprintf(buf, "Scripts: %%t|[None]%%x0|");
150         BLI_dynstr_append(pupds, buf);
151         
152         /* init active-index first */
153         if (con_text == NULL)
154                 *pyconindex= 0;
155         
156         /* loop through markers, adding them */
157         for (text=G.main->text.first, i=1; text; i++, text=text->id.next) {
158                 /* this is important to ensure that right script is shown as active */
159                 if (text == con_text) *pyconindex = i;
160                 
161                 /* only include valid pyconstraint scripts */
162                 if (BPY_is_pyconstraint(text)) {
163                         BLI_dynstr_append(pupds, text->id.name+2);
164                         
165                         sprintf(buf, "%%x%d", i);
166                         BLI_dynstr_append(pupds, buf);
167                         
168                         if (text->id.next)
169                                 BLI_dynstr_append(pupds, "|");
170                 }
171         }
172         
173         /* convert to normal MEM_malloc'd string */
174         str= BLI_dynstr_get_cstring(pupds);
175         BLI_dynstr_free(pupds);
176         
177         return str;
178 }
179 #endif /* DISABLE_PYTHON */
180
181 /* this callback gets called when the 'refresh' button of a pyconstraint gets pressed */
182 void update_pyconstraint_cb (void *arg1, void *arg2)
183 {
184         Object *owner= (Object *)arg1;
185         bConstraint *con= (bConstraint *)arg2;
186 #ifndef DISABLE_PYTHON
187         if (owner && con)
188                 BPY_pyconstraint_update(owner, con);
189 #endif
190 }
191
192 /* Creates a new constraint, initialises its data, and returns it */
193 bConstraint *add_new_constraint (short type)
194 {
195         bConstraint *con;
196         bConstraintTypeInfo *cti;
197
198         con = MEM_callocN(sizeof(bConstraint), "Constraint");
199         
200         /* Set up a generic constraint datablock */
201         con->type = type;
202         con->flag |= CONSTRAINT_EXPAND;
203         con->enforce = 1.0F;
204         strcpy(con->name, "Const");
205         
206         /* Load the data for it */
207         cti = constraint_get_typeinfo(con);
208         if (cti) {
209                 con->data = MEM_callocN(cti->size, cti->structName);
210                 
211                 /* only constraints that change any settings need this */
212                 if (cti->new_data)
213                         cti->new_data(con->data);
214         }
215         
216         return con;
217 }
218
219 /* Adds the given constraint to the Object-level set of constraints for the given Object */
220 void add_constraint_to_object (bConstraint *con, Object *ob)
221 {
222         ListBase *list;
223         list = &ob->constraints;
224         
225         if (list) {
226                 unique_constraint_name(con, list);
227                 BLI_addtail(list, con);
228                 
229                 if (proxylocked_constraints_owner(ob, NULL))
230                         con->flag |= CONSTRAINT_PROXY_LOCAL;
231                 
232                 con->flag |= CONSTRAINT_ACTIVE;
233                 for (con= con->prev; con; con= con->prev)
234                         con->flag &= ~CONSTRAINT_ACTIVE;
235         }
236 }
237
238 /* helper function for add_constriant - sets the last target for the active constraint */
239 static void set_constraint_nth_target (bConstraint *con, Object *target, char subtarget[], int index)
240 {
241         bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
242         ListBase targets = {NULL, NULL};
243         bConstraintTarget *ct;
244         int num_targets, i;
245         
246         if (cti && cti->get_constraint_targets) {
247                 cti->get_constraint_targets(con, &targets);
248                 num_targets= BLI_countlist(&targets);
249                 
250                 if (index < 0) {
251                         if (abs(index) < num_targets)
252                                 index= num_targets - abs(index);
253                         else
254                                 index= num_targets - 1;
255                 }
256                 else if (index >= num_targets) {
257                         index= num_targets - 1;
258                 }
259                 
260                 for (ct=targets.first, i=0; ct; ct= ct->next, i++) {
261                         if (i == index) {
262                                 ct->tar= target;
263                                 strcpy(ct->subtarget, subtarget);
264                                 break;
265                         }
266                 }
267                 
268                 if (cti->flush_constraint_targets)
269                         cti->flush_constraint_targets(con, &targets, 0);
270         }
271 }
272
273 /* context: active object in posemode, active channel, optional selected channel */
274 void add_constraint (Scene *scene, View3D *v3d, short only_IK)
275 {
276         Object *ob= OBACT, *obsel=NULL;
277         bPoseChannel *pchanact=NULL, *pchansel=NULL;
278         bConstraint *con=NULL;
279         Base *base;
280         short nr;
281         
282         /* paranoia checks */
283         if ((ob==NULL) || (ob==scene->obedit)) 
284                 return;
285
286         if ((ob->pose) && (ob->flag & OB_POSEMODE)) {
287                 bArmature *arm= ob->data;
288                 
289                 /* find active channel */
290                 pchanact= get_active_posechannel(ob);
291                 if (pchanact==NULL) 
292                         return;
293                 
294                 /* find selected bone */
295                 for (pchansel=ob->pose->chanbase.first; pchansel; pchansel=pchansel->next) {
296                         if (pchansel != pchanact) {
297                                 if (pchansel->bone->flag & BONE_SELECTED)  {
298                                         if (pchansel->bone->layer & arm->layer)
299                                                 break;
300                                 }
301                         }
302                 }
303         }
304         
305         /* find selected object */
306         for (base= FIRSTBASE; base; base= base->next) {
307                 if ((TESTBASE(v3d, base)) && (base->object!=ob)) 
308                         obsel= base->object;
309         }
310         
311         /* the only_IK caller has checked for posemode! */
312         if (only_IK) {
313                 for (con= pchanact->constraints.first; con; con= con->next) {
314                         if (con->type==CONSTRAINT_TYPE_KINEMATIC) break;
315                 }
316                 if (con) {
317                         error("Pose Channel already has IK");
318                         return;
319                 }
320                 
321                 if (pchansel)
322                         nr= pupmenu("Add IK Constraint%t|To Active Bone%x10");
323                 else if (obsel)
324                         nr= pupmenu("Add IK Constraint%t|To Active Object%x10");
325                 else 
326                         nr= pupmenu("Add IK Constraint%t|To New Empty Object%x10|Without Target%x11");
327         }
328         else {
329                 if (pchanact) {
330                         if (pchansel)
331                                 nr= pupmenu("Add Constraint to Active Bone%t|Child Of%x19|Transformation%x20|%l|Copy Location%x1|Copy Rotation%x2|Copy Scale%x8|%l|Limit Location%x13|Limit Rotation%x14|Limit Scale%x15|Limit Distance%x21|%l|Track To%x3|Floor%x4|Locked Track%x5|Stretch To%x7|%l|Action%x16|Script%x18");
332                         else if ((obsel) && (obsel->type==OB_CURVE))
333                                 nr= pupmenu("Add Constraint to Active Object%t|Child Of%x19|Transformation%x20|%l|Copy Location%x1|Copy Rotation%x2|Copy Scale%x8|%l|Limit Location%x13|Limit Rotation%x14|Limit Scale%x15|Limit Distance%x21|%l|Track To%x3|Floor%x4|Locked Track%x5|Follow Path%x6|Clamp To%x17|Stretch To%x7|%l|Action%x16|Script%x18");
334                         else if ((obsel) && (obsel->type==OB_MESH))
335                                 nr= pupmenu("Add Constraint to Active Object%t|Child Of%x19|Transformation%x20|%l|Copy Location%x1|Copy Rotation%x2|Copy Scale%x8|%l|Limit Location%x13|Limit Rotation%x14|Limit Scale%x15|Limit Distance%x21|%l|Track To%x3|Floor%x4|Locked Track%x5|Shrinkwrap%x22|Stretch To%x7|%l|Action%x16|Script%x18");
336                         else if (obsel)
337                                 nr= pupmenu("Add Constraint to Active Object%t|Child Of%x19|Transformation%x20|%l|Copy Location%x1|Copy Rotation%x2|Copy Scale%x8|%l|Limit Location%x13|Limit Rotation%x14|Limit Scale%x15|Limit Distance%x21|%l|Track To%x3|Floor%x4|Locked Track%x5|Stretch To%x7|%l|Action%x16|Script%x18");
338                         else
339                                 nr= pupmenu("Add Constraint to New Empty Object%t|Child Of%x19|Transformation%x20|%l|Copy Location%x1|Copy Rotation%x2|Copy Scale%x8|%l|Limit Location%x13|Limit Rotation%x14|Limit Scale%x15|Limit Distance%x21|%l|Track To%x3|Floor%x4|Locked Track%x5|Stretch To%x7|%l|Action%x16|Script%x18");
340                 }
341                 else {
342                         if ((obsel) && (obsel->type==OB_CURVE))
343                                 nr= pupmenu("Add Constraint to Active Object%t|Child Of%x19|Transformation%x20|%l|Copy Location%x1|Copy Rotation%x2|Copy Scale%x8|%l|Limit Location%x13|Limit Rotation%x14|Limit Scale%x15|Limit Distance%x21|%l|Track To%x3|Floor%x4|Locked Track%x5|Follow Path%x6|Clamp To%x17|%l|Action%x16|Script%x18");
344                         else if ((obsel) && (obsel->type==OB_MESH))
345                                 nr= pupmenu("Add Constraint to Active Object%t|Child Of%x19|Transformation%x20|%l|Copy Location%x1|Copy Rotation%x2|Copy Scale%x8|%l|Limit Location%x13|Limit Rotation%x14|Limit Scale%x15|Limit Distance%x21|%l|Track To%x3|Floor%x4|Locked Track%x5|Shrinkwrap%x22|%l|Action%x16|Script%x18");
346                         else if (obsel)
347                                 nr= pupmenu("Add Constraint to Active Object%t|Child Of%x19|Transformation%x20|%l|Copy Location%x1|Copy Rotation%x2|Copy Scale%x8|%l|Limit Location%x13|Limit Rotation%x14|Limit Scale%x15|Limit Distance%x21|%l|Track To%x3|Floor%x4|Locked Track%x5|%l|Action%x16|Script%x18");
348                         else
349                                 nr= pupmenu("Add Constraint to New Empty Object%t|Child Of%x19|Transformation%x20|%l|Copy Location%x1|Copy Rotation%x2|Copy Scale%x8|%l|Limit Location%x13|Limit Rotation%x14|Limit Scale%x15|Limit Distance%x21|%l|Track To%x3|Floor%x4|Locked Track%x5|%l|Action%x16|Script%x18");
350                 }
351         }
352         
353         if (nr < 1) return;
354         
355         /* handle IK separate */
356         if (nr==10 || nr==11) {
357                 /* ik - prevent weird chains... */
358                 if (pchansel) {
359                         bPoseChannel *pchan= pchanact;
360                         while (pchan) {
361                                 if (pchan==pchansel) break;
362                                 pchan= pchan->parent;
363                         }
364                         if (pchan) {
365                                 error("IK root cannot be linked to IK tip");
366                                 return;
367                         }
368                         
369                         pchan= pchansel;
370                         while (pchan) {
371                                 if (pchan==pchanact) break;
372                                 pchan= pchan->parent;
373                         }
374                         if (pchan) {
375                                 error("IK tip cannot be linked to IK root");
376                                 return;
377                         }               
378                 }
379                 
380                 con = add_new_constraint(CONSTRAINT_TYPE_KINEMATIC);
381                 BLI_addtail(&pchanact->constraints, con);
382                 unique_constraint_name(con, &pchanact->constraints);
383                 pchanact->constflag |= PCHAN_HAS_IK;    /* for draw, but also for detecting while pose solving */
384                 if (nr==11) 
385                         pchanact->constflag |= PCHAN_HAS_TARGET;
386                 if (proxylocked_constraints_owner(ob, pchanact))
387                         con->flag |= CONSTRAINT_PROXY_LOCAL;
388         }
389         else {
390                 /* normal constraints - add data */
391                 if (nr==1) con = add_new_constraint(CONSTRAINT_TYPE_LOCLIKE);
392                 else if (nr==2) con = add_new_constraint(CONSTRAINT_TYPE_ROTLIKE);
393                 else if (nr==3) con = add_new_constraint(CONSTRAINT_TYPE_TRACKTO);
394                 else if (nr==4) con = add_new_constraint(CONSTRAINT_TYPE_MINMAX);
395                 else if (nr==5) con = add_new_constraint(CONSTRAINT_TYPE_LOCKTRACK);
396                 else if (nr==6) {
397                         Curve *cu= obsel->data;
398                         cu->flag |= CU_PATH;
399                         con = add_new_constraint(CONSTRAINT_TYPE_FOLLOWPATH);
400                 }
401                 else if (nr==7) con = add_new_constraint(CONSTRAINT_TYPE_STRETCHTO);
402                 else if (nr==8) con = add_new_constraint(CONSTRAINT_TYPE_SIZELIKE);
403                 else if (nr==13) con = add_new_constraint(CONSTRAINT_TYPE_LOCLIMIT);
404                 else if (nr==14) con = add_new_constraint(CONSTRAINT_TYPE_ROTLIMIT);
405                 else if (nr==15) con = add_new_constraint(CONSTRAINT_TYPE_SIZELIMIT);
406                 else if (nr==16) {
407                         /* TODO: add a popup-menu to display list of available actions to use (like for pyconstraints) */
408                         con = add_new_constraint(CONSTRAINT_TYPE_ACTION);
409                 }
410                 else if (nr==17) {
411                         Curve *cu= obsel->data;
412                         cu->flag |= CU_PATH;
413                         con = add_new_constraint(CONSTRAINT_TYPE_CLAMPTO);
414                 }
415                 else if (nr==18) {      
416                         char *menustr;
417                         int scriptint= 0;
418 #ifndef DISABLE_PYTHON
419                         /* popup a list of usable scripts */
420                         menustr = buildmenu_pyconstraints(NULL, &scriptint);
421                         scriptint = pupmenu(menustr);
422                         MEM_freeN(menustr);
423                         
424                         /* only add constraint if a script was chosen */
425                         if (scriptint) {
426                                 /* add constraint */
427                                 con = add_new_constraint(CONSTRAINT_TYPE_PYTHON);
428                                 validate_pyconstraint_cb(con->data, &scriptint);
429                                 
430                                 /* make sure target allowance is set correctly */
431                                 BPY_pyconstraint_update(ob, con);
432                         }
433 #endif
434                 }
435                 else if (nr==19) {
436                         con = add_new_constraint(CONSTRAINT_TYPE_CHILDOF);
437                 
438                         /* if this constraint is being added to a posechannel, make sure
439                          * the constraint gets evaluated in pose-space
440                          */
441                         if (pchanact) {
442                                 con->ownspace = CONSTRAINT_SPACE_POSE;
443                                 con->flag |= CONSTRAINT_SPACEONCE;
444                         }
445                 }
446                 else if (nr==20) con = add_new_constraint(CONSTRAINT_TYPE_TRANSFORM);
447                 else if (nr==21) con = add_new_constraint(CONSTRAINT_TYPE_DISTLIMIT);
448                 else if (nr==22) con = add_new_constraint(CONSTRAINT_TYPE_SHRINKWRAP);
449                 
450                 if (con==NULL) return;  /* paranoia */
451                 
452                 if (pchanact) {
453                         BLI_addtail(&pchanact->constraints, con);
454                         unique_constraint_name(con, &pchanact->constraints);
455                         pchanact->constflag |= PCHAN_HAS_CONST; /* for draw */
456                         if (proxylocked_constraints_owner(ob, pchanact))
457                                 con->flag |= CONSTRAINT_PROXY_LOCAL;
458                 }
459                 else {
460                         BLI_addtail(&ob->constraints, con);
461                         unique_constraint_name(con, &ob->constraints);
462                         if (proxylocked_constraints_owner(ob, NULL))
463                                 con->flag |= CONSTRAINT_PROXY_LOCAL;
464                 }
465         }
466         
467         /* set the target */
468         if (pchansel) {
469                 set_constraint_nth_target(con, ob, pchansel->name, 0);
470         }
471         else if (obsel) {
472                 set_constraint_nth_target(con, obsel, "", 0);
473         }
474         else if (ELEM4(nr, 11, 13, 14, 15)==0) {        /* add new empty as target */
475                 Base *base= BASACT, *newbase;
476                 Object *obt;
477                 
478                 obt= add_object(scene, OB_EMPTY);
479                 /* set layers OK */
480                 newbase= BASACT;
481                 newbase->lay= base->lay;
482                 obt->lay= newbase->lay;
483                 
484                 /* transform cent to global coords for loc */
485                 if (pchanact) {
486                         if (only_IK)
487                                 VecMat4MulVecfl(obt->loc, ob->obmat, pchanact->pose_tail);
488                         else
489                                 VecMat4MulVecfl(obt->loc, ob->obmat, pchanact->pose_head);
490                 }
491                 else
492                         VECCOPY(obt->loc, ob->obmat[3]);
493                 
494                 set_constraint_nth_target(con, obt, "", 0);
495                 
496                 /* restore, add_object sets active */
497                 BASACT= base;
498                 base->flag |= SELECT;
499         }
500         
501         /* active flag */
502         con->flag |= CONSTRAINT_ACTIVE;
503         for (con= con->prev; con; con= con->prev)
504                 con->flag &= ~CONSTRAINT_ACTIVE;
505
506         DAG_scene_sort(scene);          // sort order of objects
507         
508         if (pchanact) {
509                 ob->pose->flag |= POSE_RECALC;  // sort pose channels
510                 DAG_object_flush_update(scene, ob, OB_RECALC_DATA);     // and all its relations
511         }
512         else
513                 DAG_object_flush_update(scene, ob, OB_RECALC_OB);       // and all its relations
514
515         if (only_IK)
516                 BIF_undo_push("Add IK Constraint");
517         else
518                 BIF_undo_push("Add Constraint");
519
520 }
521
522 /* ------------- Constraint Sanity Testing ------------------- */
523
524 /* checks validity of object pointers, and NULLs,
525  * if Bone doesnt exist it sets the CONSTRAINT_DISABLE flag 
526  */
527 static void test_constraints (Object *owner, const char substring[])
528 {
529         bConstraint *curcon;
530         ListBase *conlist= NULL;
531         int type;
532         
533         if (owner==NULL) return;
534         
535         /* Check parents */
536         if (strlen(substring)) {
537                 switch (owner->type) {
538                         case OB_ARMATURE:
539                                 type = CONSTRAINT_OBTYPE_BONE;
540                                 break;
541                         default:
542                                 type = CONSTRAINT_OBTYPE_OBJECT;
543                                 break;
544                 }
545         }
546         else
547                 type = CONSTRAINT_OBTYPE_OBJECT;
548         
549         /* Get the constraint list for this object */
550         switch (type) {
551                 case CONSTRAINT_OBTYPE_OBJECT:
552                         conlist = &owner->constraints;
553                         break;
554                 case CONSTRAINT_OBTYPE_BONE:
555                         {
556                                 Bone *bone;
557                                 bPoseChannel *chan;
558                                 
559                                 bone = get_named_bone( ((bArmature *)owner->data), substring );
560                                 chan = get_pose_channel(owner->pose, substring);
561                                 if (bone && chan) {
562                                         conlist = &chan->constraints;
563                                 }
564                         }
565                         break;
566         }
567         
568         /* Check all constraints - is constraint valid? */
569         if (conlist) {
570                 for (curcon = conlist->first; curcon; curcon=curcon->next) {
571                         bConstraintTypeInfo *cti= constraint_get_typeinfo(curcon);
572                         ListBase targets = {NULL, NULL};
573                         bConstraintTarget *ct;
574                         
575                         /* clear disabled-flag first */
576                         curcon->flag &= ~CONSTRAINT_DISABLE;
577
578                         if (curcon->type == CONSTRAINT_TYPE_KINEMATIC) {
579                                 bKinematicConstraint *data = curcon->data;
580                                 
581                                 /* bad: we need a separate set of checks here as poletarget is 
582                                  *              optional... otherwise poletarget must exist too or else
583                                  *              the constraint is deemed invalid
584                                  */
585                                 if (exist_object(data->tar) == 0) {
586                                         data->tar = NULL;
587                                         curcon->flag |= CONSTRAINT_DISABLE;
588                                 }
589                                 else if (data->tar == owner) {
590                                         if (!get_named_bone(get_armature(owner), data->subtarget)) {
591                                                 curcon->flag |= CONSTRAINT_DISABLE;
592                                         }
593                                 }
594                                 
595                                 if (data->poletar) {
596                                         if (exist_object(data->poletar) == 0) {
597                                                 data->poletar = NULL;
598                                                 curcon->flag |= CONSTRAINT_DISABLE;
599                                         }
600                                         else if (data->poletar == owner) {
601                                                 if (!get_named_bone(get_armature(owner), data->polesubtarget)) {
602                                                         curcon->flag |= CONSTRAINT_DISABLE;
603                                                 }
604                                         }
605                                 }
606                                 
607                                 /* targets have already been checked for this */
608                                 continue;
609                         }
610                         else if (curcon->type == CONSTRAINT_TYPE_ACTION) {
611                                 bActionConstraint *data = curcon->data;
612                                 
613                                 /* validate action */
614                                 if (data->act == NULL) 
615                                         curcon->flag |= CONSTRAINT_DISABLE;
616                         }
617                         else if (curcon->type == CONSTRAINT_TYPE_FOLLOWPATH) {
618                                 bFollowPathConstraint *data = curcon->data;
619                                 
620                                 /* don't allow track/up axes to be the same */
621                                 if (data->upflag==data->trackflag)
622                                         curcon->flag |= CONSTRAINT_DISABLE;
623                                 if (data->upflag+3==data->trackflag)
624                                         curcon->flag |= CONSTRAINT_DISABLE;
625                         }
626                         else if (curcon->type == CONSTRAINT_TYPE_TRACKTO) {
627                                 bTrackToConstraint *data = curcon->data;
628                                 
629                                 /* don't allow track/up axes to be the same */
630                                 if (data->reserved2==data->reserved1)
631                                         curcon->flag |= CONSTRAINT_DISABLE;
632                                 if (data->reserved2+3==data->reserved1)
633                                         curcon->flag |= CONSTRAINT_DISABLE;
634                         }
635                         else if (curcon->type == CONSTRAINT_TYPE_LOCKTRACK) {
636                                 bLockTrackConstraint *data = curcon->data;
637                                 
638                                 if (data->lockflag==data->trackflag)
639                                         curcon->flag |= CONSTRAINT_DISABLE;
640                                 if (data->lockflag+3==data->trackflag)
641                                         curcon->flag |= CONSTRAINT_DISABLE;
642                         }
643                         
644                         /* Check targets for constraints */
645                         if (cti && cti->get_constraint_targets) {
646                                 cti->get_constraint_targets(curcon, &targets);
647                                 
648                                 /* disable and clear constraints targets that are incorrect */
649                                 for (ct= targets.first; ct; ct= ct->next) {
650                                         /* general validity checks (for those constraints that need this) */
651                                         if (exist_object(ct->tar) == 0) {
652                                                 ct->tar = NULL;
653                                                 curcon->flag |= CONSTRAINT_DISABLE;
654                                         }
655                                         else if (ct->tar == owner) {
656                                                 if (!get_named_bone(get_armature(owner), ct->subtarget)) {
657                                                         curcon->flag |= CONSTRAINT_DISABLE;
658                                                 }
659                                         }
660                                         
661                                         /* target checks for specific constraints */
662                                         if (ELEM(curcon->type, CONSTRAINT_TYPE_FOLLOWPATH, CONSTRAINT_TYPE_CLAMPTO)) {
663                                                 if (ct->tar) {
664                                                         if (ct->tar->type != OB_CURVE) {
665                                                                 ct->tar= NULL;
666                                                                 curcon->flag |= CONSTRAINT_DISABLE;
667                                                         }
668                                                         else {
669                                                                 Curve *cu= ct->tar->data;
670                                                                 
671                                                                 /* auto-set 'Path' setting on curve so this works  */
672                                                                 cu->flag |= CU_PATH;
673                                                         }
674                                                 }                                               
675                                         }
676                                 }       
677                                 
678                                 /* free any temporary targets */
679                                 if (cti->flush_constraint_targets)
680                                         cti->flush_constraint_targets(curcon, &targets, 0);
681                         }
682                 }
683         }
684 }
685
686 static void test_bonelist_constraints (Object *owner, ListBase *list)
687 {
688         Bone *bone;
689
690         for (bone = list->first; bone; bone = bone->next) {
691                 test_constraints(owner, bone->name);
692                 test_bonelist_constraints(owner, &bone->childbase);
693         }
694 }
695
696 void object_test_constraints (Object *owner)
697 {
698         test_constraints(owner, "");
699
700         if (owner->type==OB_ARMATURE) {
701                 bArmature *arm= get_armature(owner);
702                 
703                 if (arm)
704                         test_bonelist_constraints(owner, &arm->bonebase);
705         }
706 }
707
708 /* ********************** CONSTRAINT-SPECIFIC STUFF ********************* */
709
710 /* ------------- Child-Of Constraint ------------------ */
711
712 /* ChildOf Constraint - set inverse callback */
713 static int childof_set_inverse_exec (bContext *C, wmOperator *op)
714 {
715         PointerRNA ptr= CTX_data_pointer_get_type(C, "constraint", &RNA_ChildOfConstraint);
716         Scene *scene= CTX_data_scene(C);
717         Object *ob= ptr.id.data;
718         bConstraint *con= ptr.data;
719         bChildOfConstraint *data= (bChildOfConstraint *)con->data;
720         bPoseChannel *pchan= NULL;
721
722         /* try to find a pose channel */
723         // TODO: get from context instead?
724         if (ob && ob->pose)
725                 pchan= get_active_posechannel(ob);
726         
727         /* calculate/set inverse matrix */
728         if (pchan) {
729                 float pmat[4][4], cinf;
730                 float imat[4][4], tmat[4][4];
731                 
732                 /* make copy of pchan's original pose-mat (for use later) */
733                 Mat4CpyMat4(pmat, pchan->pose_mat);
734                 
735                 /* disable constraint for pose to be solved without it */
736                 cinf= con->enforce;
737                 con->enforce= 0.0f;
738                 
739                 /* solve pose without constraint */
740                 where_is_pose(scene, ob);
741                 
742                 /* determine effect of constraint by removing the newly calculated 
743                  * pchan->pose_mat from the original pchan->pose_mat, thus determining 
744                  * the effect of the constraint
745                  */
746                 Mat4Invert(imat, pchan->pose_mat);
747                 Mat4MulMat4(tmat, imat, pmat);
748                 Mat4Invert(data->invmat, tmat);
749                 
750                 /* recalculate pose with new inv-mat */
751                 con->enforce= cinf;
752                 where_is_pose(scene, ob);
753         }
754         else if (ob) {
755                 Object workob;
756                 /* use what_does_parent to find inverse - just like for normal parenting.
757                  * NOTE: what_does_parent uses a static workob defined in object.c 
758                  */
759                 what_does_parent(scene, ob, &workob);
760                 Mat4Invert(data->invmat, workob.obmat);
761         }
762         else
763                 Mat4One(data->invmat);
764                 
765         WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, ob);
766                 
767         return OPERATOR_FINISHED;
768 }
769
770 void CONSTRAINT_OT_childof_set_inverse (wmOperatorType *ot)
771 {
772         /* identifiers */
773         ot->name= "Set Inverse";
774         ot->idname= "CONSTRAINT_OT_childof_set_inverse";
775         ot->description= "Set inverse correction for ChildOf constraint.";
776         
777         ot->exec= childof_set_inverse_exec;
778         
779         /* flags */
780         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
781 }
782
783
784 /* ChildOf Constraint - clear inverse callback */
785 static int childof_clear_inverse_exec (bContext *C, wmOperator *op)
786 {
787         PointerRNA ptr= CTX_data_pointer_get_type(C, "constraint", &RNA_ChildOfConstraint);
788         Object *ob= ptr.id.data;
789         bConstraint *con= ptr.data;
790         bChildOfConstraint *data= (bChildOfConstraint *)con->data;
791         
792         /* simply clear the matrix */
793         Mat4One(data->invmat);
794         
795         WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, ob);
796         
797         return OPERATOR_FINISHED;
798 }
799
800 void CONSTRAINT_OT_childof_clear_inverse (wmOperatorType *ot)
801 {
802         /* identifiers */
803         ot->name= "Clear Inverse";
804         ot->idname= "CONSTRAINT_OT_childof_clear_inverse";
805         ot->description= "Clear inverse correction for ChildOf constraint.";
806         
807         ot->exec= childof_clear_inverse_exec;
808         
809         /* flags */
810         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
811 }
812
813 /***************************** BUTTONS ****************************/
814
815 /* Rename the given constraint, con already has the new name */
816 void ED_object_constraint_rename(Object *ob, bConstraint *con, char *oldname)
817 {
818         bConstraint *tcon;
819         ListBase *conlist= NULL;
820         int from_object= 0;
821         
822         /* get context by searching for con (primitive...) */
823         for (tcon= ob->constraints.first; tcon; tcon= tcon->next) {
824                 if (tcon==con)
825                         break;
826         }
827         
828         if (tcon) {
829                 conlist= &ob->constraints;
830                 from_object= 1;
831         }
832         else if (ob->pose) {
833                 bPoseChannel *pchan;
834                 
835                 for (pchan=ob->pose->chanbase.first; pchan; pchan=pchan->next) {
836                         for (tcon= pchan->constraints.first; tcon; tcon= tcon->next) {
837                                 if (tcon==con)
838                                         break;
839                         }
840                         if (tcon) 
841                                 break;
842                 }
843                 
844                 if (tcon) {
845                         conlist= &pchan->constraints;
846                 }
847         }
848         
849         if (conlist==NULL) {
850                 printf("rename constraint failed\n");   /* should not happen in UI */
851                 return;
852         }
853         
854         /* first make sure it's a unique name within context */
855         unique_constraint_name(con, conlist);
856 }
857
858
859
860
861 void ED_object_constraint_set_active(Object *ob, bConstraint *con)
862 {
863         ListBase *lb;
864         bConstraint *origcon= con;
865         
866         /* lets be nice and escape if its active already */
867         if(con && (con->flag & CONSTRAINT_ACTIVE))
868                 return ;
869         
870         lb= get_active_constraints(ob);
871         if(lb == NULL)
872                 return;
873         
874         for(con= lb->first; con; con= con->next) {
875                 if(con==origcon) con->flag |= CONSTRAINT_ACTIVE;
876                 else con->flag &= ~CONSTRAINT_ACTIVE;
877         }
878 }
879
880 static int constraint_delete_exec (bContext *C, wmOperator *op)
881 {
882         PointerRNA ptr= CTX_data_pointer_get_type(C, "constraint", &RNA_Constraint);
883         Object *ob= ptr.id.data;
884         bConstraint *con= ptr.data;
885         ListBase *lb;
886         
887         /* remove constraint itself */
888         lb= get_active_constraints(ob);
889         free_constraint_data(con);
890         BLI_freelinkN(lb, con);
891         
892         ED_object_constraint_set_active(ob, NULL);
893         WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, ob);
894
895         return OPERATOR_FINISHED;
896 }
897
898 void CONSTRAINT_OT_delete (wmOperatorType *ot)
899 {
900         /* identifiers */
901         ot->name= "Delete Constraint";
902         ot->idname= "CONSTRAINT_OT_delete";
903         ot->description= "Remove constraitn from constraint stack.";
904         
905         /* callbacks */
906         ot->exec= constraint_delete_exec;
907         
908         /* flags */
909         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; 
910 }
911
912 static int constraint_move_down_exec (bContext *C, wmOperator *op)
913 {
914         PointerRNA ptr= CTX_data_pointer_get_type(C, "constraint", &RNA_Constraint);
915         Object *ob= ptr.id.data;
916         bConstraint *con= ptr.data;
917         
918         if (con->next) {
919                 ListBase *conlist= get_active_constraints(ob);
920                 bConstraint *nextCon= con->next;
921                 
922                 /* insert the nominated constraint after the one that used to be after it */
923                 BLI_remlink(conlist, con);
924                 BLI_insertlinkafter(conlist, nextCon, con);
925                 
926                 WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, ob);
927                 
928                 return OPERATOR_FINISHED;
929         }
930         
931         return OPERATOR_CANCELLED;
932 }
933
934 void CONSTRAINT_OT_move_down (wmOperatorType *ot)
935 {
936         /* identifiers */
937         ot->name= "Move Constraint Down";
938         ot->idname= "CONSTRAINT_OT_move_down";
939         ot->description= "Move constraint down constraint stack.";
940         
941         /* callbacks */
942         ot->exec= constraint_move_down_exec;
943         
944         /* flags */
945         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; 
946 }
947
948
949 static int constraint_move_up_exec (bContext *C, wmOperator *op)
950 {
951         PointerRNA ptr= CTX_data_pointer_get_type(C, "constraint", &RNA_Constraint);
952         Object *ob= ptr.id.data;
953         bConstraint *con= ptr.data;
954         
955         if (con->prev) {
956                 ListBase *conlist= get_active_constraints(ob);
957                 bConstraint *prevCon= con->prev;
958                 
959                 /* insert the nominated constraint before the one that used to be before it */
960                 BLI_remlink(conlist, con);
961                 BLI_insertlinkbefore(conlist, prevCon, con);
962                 
963                 WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, ob);
964                 
965                 return OPERATOR_FINISHED;
966         }
967         
968         return OPERATOR_CANCELLED;
969 }
970
971 void CONSTRAINT_OT_move_up (wmOperatorType *ot)
972 {
973         /* identifiers */
974         ot->name= "Move Constraint Up";
975         ot->idname= "CONSTRAINT_OT_move_up";
976         ot->description= "Move constraint up constraint stack.";
977         
978         /* callbacks */
979         ot->exec= constraint_move_up_exec;
980         
981         /* flags */
982         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; 
983 }
984
985 /***************************** OPERATORS ****************************/
986
987 /************************ remove constraint operators *********************/
988
989 static int pose_constraints_clear_exec(bContext *C, wmOperator *op)
990 {
991         Scene *scene= CTX_data_scene(C);
992         Object *ob= CTX_data_active_object(C);
993         
994         /* free constraints for all selected bones */
995         CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pchans)
996         {
997                 free_constraints(&pchan->constraints);
998         }
999         CTX_DATA_END;
1000         
1001         /* do updates */
1002         DAG_object_flush_update(scene, ob, OB_RECALC_OB);
1003         WM_event_add_notifier(C, NC_OBJECT|ND_POSE|ND_CONSTRAINT|NA_REMOVED, ob);
1004         
1005         return OPERATOR_FINISHED;
1006 }
1007
1008 void POSE_OT_constraints_clear(wmOperatorType *ot)
1009 {
1010         /* identifiers */
1011         //ot->name = "Clear Constraints";
1012         ot->idname= "POSE_OT_constraints_clear";
1013         ot->description= "Clear all the constraints for the selected bones.";
1014         
1015         /* callbacks */
1016         //ot->invoke= WM_menu_confirm; // XXX do we want confirmations on these things anymore?
1017         ot->exec= pose_constraints_clear_exec;
1018         ot->poll= ED_operator_posemode; // XXX - do we want to ensure there are selected bones too?
1019 }
1020
1021
1022 static int object_constraints_clear_exec(bContext *C, wmOperator *op)
1023 {
1024         Scene *scene= CTX_data_scene(C);
1025         Object *ob= CTX_data_active_object(C);
1026         
1027         /* do freeing */
1028         // TODO: we should free constraints for all selected objects instead (to be more consistent with bones)
1029         free_constraints(&ob->constraints);
1030         
1031         /* do updates */
1032         DAG_object_flush_update(scene, ob, OB_RECALC_OB);
1033         WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT|NA_REMOVED, ob);
1034         
1035         return OPERATOR_FINISHED;
1036 }
1037
1038 void OBJECT_OT_constraints_clear(wmOperatorType *ot)
1039 {
1040         /* identifiers */
1041         ot->name = "Clear Constraints";
1042         ot->idname= "OBJECT_OT_constraints_clear";
1043         ot->description= "Clear all the constraints for the active Object only.";
1044         
1045         /* callbacks */
1046         //ot->invoke= WM_menu_confirm; // XXX do we want confirmations on these things anymore?
1047         ot->exec= object_constraints_clear_exec;
1048         ot->poll= ED_operator_object_active;
1049 }
1050
1051 /************************ add constraint operators *********************/
1052
1053 static int constraint_add_exec(bContext *C, wmOperator *op, ListBase *list)
1054 {
1055         Scene *scene= CTX_data_scene(C);
1056     Object *ob = CTX_data_active_object(C);
1057         bConstraint *con, *coniter;
1058         bPoseChannel *pchan= get_active_posechannel(ob);
1059         int type= RNA_enum_get(op->ptr, "type");
1060
1061         con = add_new_constraint(type);
1062
1063         if(list) {
1064                 unique_constraint_name(con, list);
1065                 BLI_addtail(list, con);
1066                 
1067                 if(proxylocked_constraints_owner(ob, pchan))
1068                         con->flag |= CONSTRAINT_PROXY_LOCAL;
1069                 
1070                 con->flag |= CONSTRAINT_ACTIVE;
1071                 for(coniter= con->prev; coniter; coniter= coniter->prev)
1072                         coniter->flag &= ~CONSTRAINT_ACTIVE;
1073         }
1074         
1075         switch(type) {
1076                 case CONSTRAINT_TYPE_CHILDOF:
1077                         {
1078                                 /* if this constraint is being added to a posechannel, make sure
1079                                  * the constraint gets evaluated in pose-space */
1080                                 if(ob->flag & OB_POSEMODE) {
1081                                         con->ownspace = CONSTRAINT_SPACE_POSE;
1082                                         con->flag |= CONSTRAINT_SPACEONCE;
1083                                 }
1084                         }
1085                         break;
1086                 case CONSTRAINT_TYPE_RIGIDBODYJOINT:
1087                         {
1088                                 bRigidBodyJointConstraint *data;
1089                                 
1090                                 /* set selected first object as target - moved from new_constraint_data */
1091                                 data = (bRigidBodyJointConstraint*)con->data;
1092                                 
1093                                 CTX_DATA_BEGIN(C, Object*, selob, selected_objects) {
1094                                         if(selob != ob) {
1095                                                 data->tar= selob;
1096                                                 break;
1097                                         }
1098                                 }
1099                                 CTX_DATA_END;
1100                         }
1101                         break;
1102                 default:
1103                         break;
1104         }
1105
1106         object_test_constraints(ob);
1107         
1108         if(ob->pose)
1109                 update_pose_constraint_flags(ob->pose);
1110         
1111         if(ob->type==OB_ARMATURE)
1112                 DAG_object_flush_update(scene, ob, OB_RECALC_DATA|OB_RECALC_OB);
1113         else
1114                 DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
1115
1116         WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT|NA_ADDED, ob);
1117         
1118         return OPERATOR_FINISHED;
1119 }
1120
1121 static int object_constraint_add_exec(bContext *C, wmOperator *op)
1122 {
1123         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1124
1125         if(!ob)
1126                 return OPERATOR_CANCELLED;
1127
1128         return constraint_add_exec(C, op, &ob->constraints);
1129 }
1130
1131 static int pose_constraint_add_exec(bContext *C, wmOperator *op)
1132 {
1133         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1134
1135         if(!ob)
1136                 return OPERATOR_CANCELLED;
1137         
1138         return constraint_add_exec(C, op, get_active_constraints(ob));
1139 }
1140
1141 void OBJECT_OT_constraint_add(wmOperatorType *ot)
1142 {
1143         /* identifiers */
1144         ot->name= "Add Constraint";
1145         ot->description = "Add a constraint to the active object.";
1146         ot->idname= "OBJECT_OT_constraint_add";
1147         
1148         /* api callbacks */
1149         ot->invoke= WM_menu_invoke;
1150         ot->exec= object_constraint_add_exec;
1151         ot->poll= ED_operator_object_active;
1152         
1153         /* flags */
1154         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1155         
1156         /* properties */
1157         RNA_def_enum(ot->srna, "type", constraint_type_items, 0, "Type", "");
1158 }
1159
1160 void POSE_OT_constraint_add(wmOperatorType *ot)
1161 {
1162         /* identifiers */
1163         ot->name= "Add Constraint";
1164         ot->description = "Add a constraint to the active bone.";
1165         ot->idname= "POSE_OT_constraint_add";
1166         
1167         /* api callbacks */
1168         ot->invoke= WM_menu_invoke;
1169         ot->exec= pose_constraint_add_exec;
1170         ot->poll= ED_operator_posemode;
1171         
1172         /* flags */
1173         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1174         
1175         /* properties */
1176         RNA_def_enum(ot->srna, "type", constraint_type_items, 0, "Type", "");
1177 }
1178