2.5 - Constraints Editing + Keyframe Drawing Tweaks
[blender-staging.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 #include "RNA_types.h"
71
72 #include "ED_object.h"
73 #include "ED_screen.h"
74
75 #include "UI_interface.h"
76
77 #include "object_intern.h"
78
79 /* XXX */
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 /* ------------- Constraint Sanity Testing ------------------- */
274
275 /* checks validity of object pointers, and NULLs,
276  * if Bone doesnt exist it sets the CONSTRAINT_DISABLE flag 
277  */
278 static void test_constraints (Object *owner, const char substring[])
279 {
280         bConstraint *curcon;
281         ListBase *conlist= NULL;
282         int type;
283         
284         if (owner==NULL) return;
285         
286         /* Check parents */
287         if (strlen(substring)) {
288                 switch (owner->type) {
289                         case OB_ARMATURE:
290                                 type = CONSTRAINT_OBTYPE_BONE;
291                                 break;
292                         default:
293                                 type = CONSTRAINT_OBTYPE_OBJECT;
294                                 break;
295                 }
296         }
297         else
298                 type = CONSTRAINT_OBTYPE_OBJECT;
299         
300         /* Get the constraint list for this object */
301         switch (type) {
302                 case CONSTRAINT_OBTYPE_OBJECT:
303                         conlist = &owner->constraints;
304                         break;
305                 case CONSTRAINT_OBTYPE_BONE:
306                         {
307                                 Bone *bone;
308                                 bPoseChannel *chan;
309                                 
310                                 bone = get_named_bone( ((bArmature *)owner->data), substring );
311                                 chan = get_pose_channel(owner->pose, substring);
312                                 if (bone && chan) {
313                                         conlist = &chan->constraints;
314                                 }
315                         }
316                         break;
317         }
318         
319         /* Check all constraints - is constraint valid? */
320         if (conlist) {
321                 for (curcon = conlist->first; curcon; curcon=curcon->next) {
322                         bConstraintTypeInfo *cti= constraint_get_typeinfo(curcon);
323                         ListBase targets = {NULL, NULL};
324                         bConstraintTarget *ct;
325                         
326                         /* clear disabled-flag first */
327                         curcon->flag &= ~CONSTRAINT_DISABLE;
328
329                         if (curcon->type == CONSTRAINT_TYPE_KINEMATIC) {
330                                 bKinematicConstraint *data = curcon->data;
331                                 
332                                 /* bad: we need a separate set of checks here as poletarget is 
333                                  *              optional... otherwise poletarget must exist too or else
334                                  *              the constraint is deemed invalid
335                                  */
336                                 if (exist_object(data->tar) == 0) {
337                                         data->tar = NULL;
338                                         curcon->flag |= CONSTRAINT_DISABLE;
339                                 }
340                                 else if (data->tar == owner) {
341                                         if (!get_named_bone(get_armature(owner), data->subtarget)) {
342                                                 curcon->flag |= CONSTRAINT_DISABLE;
343                                         }
344                                 }
345                                 
346                                 if (data->poletar) {
347                                         if (exist_object(data->poletar) == 0) {
348                                                 data->poletar = NULL;
349                                                 curcon->flag |= CONSTRAINT_DISABLE;
350                                         }
351                                         else if (data->poletar == owner) {
352                                                 if (!get_named_bone(get_armature(owner), data->polesubtarget)) {
353                                                         curcon->flag |= CONSTRAINT_DISABLE;
354                                                 }
355                                         }
356                                 }
357                                 
358                                 /* targets have already been checked for this */
359                                 continue;
360                         }
361                         else if (curcon->type == CONSTRAINT_TYPE_ACTION) {
362                                 bActionConstraint *data = curcon->data;
363                                 
364                                 /* validate action */
365                                 if (data->act == NULL) 
366                                         curcon->flag |= CONSTRAINT_DISABLE;
367                         }
368                         else if (curcon->type == CONSTRAINT_TYPE_FOLLOWPATH) {
369                                 bFollowPathConstraint *data = curcon->data;
370                                 
371                                 /* don't allow track/up axes to be the same */
372                                 if (data->upflag==data->trackflag)
373                                         curcon->flag |= CONSTRAINT_DISABLE;
374                                 if (data->upflag+3==data->trackflag)
375                                         curcon->flag |= CONSTRAINT_DISABLE;
376                         }
377                         else if (curcon->type == CONSTRAINT_TYPE_TRACKTO) {
378                                 bTrackToConstraint *data = curcon->data;
379                                 
380                                 /* don't allow track/up axes to be the same */
381                                 if (data->reserved2==data->reserved1)
382                                         curcon->flag |= CONSTRAINT_DISABLE;
383                                 if (data->reserved2+3==data->reserved1)
384                                         curcon->flag |= CONSTRAINT_DISABLE;
385                         }
386                         else if (curcon->type == CONSTRAINT_TYPE_LOCKTRACK) {
387                                 bLockTrackConstraint *data = curcon->data;
388                                 
389                                 if (data->lockflag==data->trackflag)
390                                         curcon->flag |= CONSTRAINT_DISABLE;
391                                 if (data->lockflag+3==data->trackflag)
392                                         curcon->flag |= CONSTRAINT_DISABLE;
393                         }
394                         
395                         /* Check targets for constraints */
396                         if (cti && cti->get_constraint_targets) {
397                                 cti->get_constraint_targets(curcon, &targets);
398                                 
399                                 /* disable and clear constraints targets that are incorrect */
400                                 for (ct= targets.first; ct; ct= ct->next) {
401                                         /* general validity checks (for those constraints that need this) */
402                                         if (exist_object(ct->tar) == 0) {
403                                                 ct->tar = NULL;
404                                                 curcon->flag |= CONSTRAINT_DISABLE;
405                                         }
406                                         else if (ct->tar == owner) {
407                                                 if (!get_named_bone(get_armature(owner), ct->subtarget)) {
408                                                         curcon->flag |= CONSTRAINT_DISABLE;
409                                                 }
410                                         }
411                                         
412                                         /* target checks for specific constraints */
413                                         if (ELEM(curcon->type, CONSTRAINT_TYPE_FOLLOWPATH, CONSTRAINT_TYPE_CLAMPTO)) {
414                                                 if (ct->tar) {
415                                                         if (ct->tar->type != OB_CURVE) {
416                                                                 ct->tar= NULL;
417                                                                 curcon->flag |= CONSTRAINT_DISABLE;
418                                                         }
419                                                         else {
420                                                                 Curve *cu= ct->tar->data;
421                                                                 
422                                                                 /* auto-set 'Path' setting on curve so this works  */
423                                                                 cu->flag |= CU_PATH;
424                                                         }
425                                                 }                                               
426                                         }
427                                 }       
428                                 
429                                 /* free any temporary targets */
430                                 if (cti->flush_constraint_targets)
431                                         cti->flush_constraint_targets(curcon, &targets, 0);
432                         }
433                 }
434         }
435 }
436
437 static void test_bonelist_constraints (Object *owner, ListBase *list)
438 {
439         Bone *bone;
440
441         for (bone = list->first; bone; bone = bone->next) {
442                 test_constraints(owner, bone->name);
443                 test_bonelist_constraints(owner, &bone->childbase);
444         }
445 }
446
447 void object_test_constraints (Object *owner)
448 {
449         test_constraints(owner, "");
450
451         if (owner->type==OB_ARMATURE) {
452                 bArmature *arm= get_armature(owner);
453                 
454                 if (arm)
455                         test_bonelist_constraints(owner, &arm->bonebase);
456         }
457 }
458
459 /* ********************** CONSTRAINT-SPECIFIC STUFF ********************* */
460
461 /* ------------- Child-Of Constraint ------------------ */
462
463 /* ChildOf Constraint - set inverse callback */
464 static int childof_set_inverse_exec (bContext *C, wmOperator *op)
465 {
466         PointerRNA ptr= CTX_data_pointer_get_type(C, "constraint", &RNA_ChildOfConstraint);
467         Scene *scene= CTX_data_scene(C);
468         Object *ob= ptr.id.data;
469         bConstraint *con= ptr.data;
470         bChildOfConstraint *data= (bChildOfConstraint *)con->data;
471         bPoseChannel *pchan= NULL;
472
473         /* try to find a pose channel */
474         // TODO: get from context instead?
475         if (ob && ob->pose)
476                 pchan= get_active_posechannel(ob);
477         
478         /* calculate/set inverse matrix */
479         if (pchan) {
480                 float pmat[4][4], cinf;
481                 float imat[4][4], tmat[4][4];
482                 
483                 /* make copy of pchan's original pose-mat (for use later) */
484                 Mat4CpyMat4(pmat, pchan->pose_mat);
485                 
486                 /* disable constraint for pose to be solved without it */
487                 cinf= con->enforce;
488                 con->enforce= 0.0f;
489                 
490                 /* solve pose without constraint */
491                 where_is_pose(scene, ob);
492                 
493                 /* determine effect of constraint by removing the newly calculated 
494                  * pchan->pose_mat from the original pchan->pose_mat, thus determining 
495                  * the effect of the constraint
496                  */
497                 Mat4Invert(imat, pchan->pose_mat);
498                 Mat4MulMat4(tmat, imat, pmat);
499                 Mat4Invert(data->invmat, tmat);
500                 
501                 /* recalculate pose with new inv-mat */
502                 con->enforce= cinf;
503                 where_is_pose(scene, ob);
504         }
505         else if (ob) {
506                 Object workob;
507                 /* use what_does_parent to find inverse - just like for normal parenting.
508                  * NOTE: what_does_parent uses a static workob defined in object.c 
509                  */
510                 what_does_parent(scene, ob, &workob);
511                 Mat4Invert(data->invmat, workob.obmat);
512         }
513         else
514                 Mat4One(data->invmat);
515                 
516         WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, ob);
517                 
518         return OPERATOR_FINISHED;
519 }
520
521 void CONSTRAINT_OT_childof_set_inverse (wmOperatorType *ot)
522 {
523         /* identifiers */
524         ot->name= "Set Inverse";
525         ot->idname= "CONSTRAINT_OT_childof_set_inverse";
526         ot->description= "Set inverse correction for ChildOf constraint.";
527         
528         ot->exec= childof_set_inverse_exec;
529         
530         /* flags */
531         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
532 }
533
534
535 /* ChildOf Constraint - clear inverse callback */
536 static int childof_clear_inverse_exec (bContext *C, wmOperator *op)
537 {
538         PointerRNA ptr= CTX_data_pointer_get_type(C, "constraint", &RNA_ChildOfConstraint);
539         Object *ob= ptr.id.data;
540         bConstraint *con= ptr.data;
541         bChildOfConstraint *data= (bChildOfConstraint *)con->data;
542         
543         /* simply clear the matrix */
544         Mat4One(data->invmat);
545         
546         WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, ob);
547         
548         return OPERATOR_FINISHED;
549 }
550
551 void CONSTRAINT_OT_childof_clear_inverse (wmOperatorType *ot)
552 {
553         /* identifiers */
554         ot->name= "Clear Inverse";
555         ot->idname= "CONSTRAINT_OT_childof_clear_inverse";
556         ot->description= "Clear inverse correction for ChildOf constraint.";
557         
558         ot->exec= childof_clear_inverse_exec;
559         
560         /* flags */
561         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
562 }
563
564 /***************************** BUTTONS ****************************/
565
566 /* Rename the given constraint, con already has the new name */
567 void ED_object_constraint_rename(Object *ob, bConstraint *con, char *oldname)
568 {
569         bConstraint *tcon;
570         ListBase *conlist= NULL;
571         int from_object= 0;
572         
573         /* get context by searching for con (primitive...) */
574         for (tcon= ob->constraints.first; tcon; tcon= tcon->next) {
575                 if (tcon==con)
576                         break;
577         }
578         
579         if (tcon) {
580                 conlist= &ob->constraints;
581                 from_object= 1;
582         }
583         else if (ob->pose) {
584                 bPoseChannel *pchan;
585                 
586                 for (pchan=ob->pose->chanbase.first; pchan; pchan=pchan->next) {
587                         for (tcon= pchan->constraints.first; tcon; tcon= tcon->next) {
588                                 if (tcon==con)
589                                         break;
590                         }
591                         if (tcon) 
592                                 break;
593                 }
594                 
595                 if (tcon) {
596                         conlist= &pchan->constraints;
597                 }
598         }
599         
600         if (conlist==NULL) {
601                 printf("rename constraint failed\n");   /* should not happen in UI */
602                 return;
603         }
604         
605         /* first make sure it's a unique name within context */
606         unique_constraint_name(con, conlist);
607 }
608
609
610
611
612 void ED_object_constraint_set_active(Object *ob, bConstraint *con)
613 {
614         ListBase *lb;
615         bConstraint *origcon= con;
616         
617         /* lets be nice and escape if its active already */
618         if(con && (con->flag & CONSTRAINT_ACTIVE))
619                 return ;
620         
621         lb= get_active_constraints(ob);
622         if(lb == NULL)
623                 return;
624         
625         for(con= lb->first; con; con= con->next) {
626                 if(con==origcon) con->flag |= CONSTRAINT_ACTIVE;
627                 else con->flag &= ~CONSTRAINT_ACTIVE;
628         }
629 }
630
631 static int constraint_delete_exec (bContext *C, wmOperator *op)
632 {
633         PointerRNA ptr= CTX_data_pointer_get_type(C, "constraint", &RNA_Constraint);
634         Object *ob= ptr.id.data;
635         bConstraint *con= ptr.data;
636         ListBase *lb;
637         
638         /* remove constraint itself */
639         lb= get_active_constraints(ob);
640         free_constraint_data(con);
641         BLI_freelinkN(lb, con);
642         
643         ED_object_constraint_set_active(ob, NULL);
644         WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, ob);
645
646         return OPERATOR_FINISHED;
647 }
648
649 void CONSTRAINT_OT_delete (wmOperatorType *ot)
650 {
651         /* identifiers */
652         ot->name= "Delete Constraint";
653         ot->idname= "CONSTRAINT_OT_delete";
654         ot->description= "Remove constraitn from constraint stack.";
655         
656         /* callbacks */
657         ot->exec= constraint_delete_exec;
658         
659         /* flags */
660         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; 
661 }
662
663 static int constraint_move_down_exec (bContext *C, wmOperator *op)
664 {
665         PointerRNA ptr= CTX_data_pointer_get_type(C, "constraint", &RNA_Constraint);
666         Object *ob= ptr.id.data;
667         bConstraint *con= ptr.data;
668         
669         if (con->next) {
670                 ListBase *conlist= get_active_constraints(ob);
671                 bConstraint *nextCon= con->next;
672                 
673                 /* insert the nominated constraint after the one that used to be after it */
674                 BLI_remlink(conlist, con);
675                 BLI_insertlinkafter(conlist, nextCon, con);
676                 
677                 WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, ob);
678                 
679                 return OPERATOR_FINISHED;
680         }
681         
682         return OPERATOR_CANCELLED;
683 }
684
685 void CONSTRAINT_OT_move_down (wmOperatorType *ot)
686 {
687         /* identifiers */
688         ot->name= "Move Constraint Down";
689         ot->idname= "CONSTRAINT_OT_move_down";
690         ot->description= "Move constraint down constraint stack.";
691         
692         /* callbacks */
693         ot->exec= constraint_move_down_exec;
694         
695         /* flags */
696         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; 
697 }
698
699
700 static int constraint_move_up_exec (bContext *C, wmOperator *op)
701 {
702         PointerRNA ptr= CTX_data_pointer_get_type(C, "constraint", &RNA_Constraint);
703         Object *ob= ptr.id.data;
704         bConstraint *con= ptr.data;
705         
706         if (con->prev) {
707                 ListBase *conlist= get_active_constraints(ob);
708                 bConstraint *prevCon= con->prev;
709                 
710                 /* insert the nominated constraint before the one that used to be before it */
711                 BLI_remlink(conlist, con);
712                 BLI_insertlinkbefore(conlist, prevCon, con);
713                 
714                 WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, ob);
715                 
716                 return OPERATOR_FINISHED;
717         }
718         
719         return OPERATOR_CANCELLED;
720 }
721
722 void CONSTRAINT_OT_move_up (wmOperatorType *ot)
723 {
724         /* identifiers */
725         ot->name= "Move Constraint Up";
726         ot->idname= "CONSTRAINT_OT_move_up";
727         ot->description= "Move constraint up constraint stack.";
728         
729         /* callbacks */
730         ot->exec= constraint_move_up_exec;
731         
732         /* flags */
733         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; 
734 }
735
736 /***************************** OPERATORS ****************************/
737
738 /************************ remove constraint operators *********************/
739
740 static int pose_constraints_clear_exec(bContext *C, wmOperator *op)
741 {
742         Scene *scene= CTX_data_scene(C);
743         Object *ob= CTX_data_active_object(C);
744         
745         /* free constraints for all selected bones */
746         CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pchans)
747         {
748                 free_constraints(&pchan->constraints);
749         }
750         CTX_DATA_END;
751         
752         /* do updates */
753         DAG_object_flush_update(scene, ob, OB_RECALC_OB);
754         WM_event_add_notifier(C, NC_OBJECT|ND_POSE|ND_CONSTRAINT|NA_REMOVED, ob);
755         
756         return OPERATOR_FINISHED;
757 }
758
759 void POSE_OT_constraints_clear(wmOperatorType *ot)
760 {
761         /* identifiers */
762         ot->name = "Clear Constraints";
763         ot->idname= "POSE_OT_constraints_clear";
764         ot->description= "Clear all the constraints for the selected bones.";
765         
766         /* callbacks */
767         ot->exec= pose_constraints_clear_exec;
768         ot->poll= ED_operator_posemode; // XXX - do we want to ensure there are selected bones too?
769 }
770
771
772 static int object_constraints_clear_exec(bContext *C, wmOperator *op)
773 {
774         Scene *scene= CTX_data_scene(C);
775         Object *ob= CTX_data_active_object(C);
776         
777         /* do freeing */
778         // TODO: we should free constraints for all selected objects instead (to be more consistent with bones)
779         free_constraints(&ob->constraints);
780         
781         /* do updates */
782         DAG_object_flush_update(scene, ob, OB_RECALC_OB);
783         WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT|NA_REMOVED, ob);
784         
785         return OPERATOR_FINISHED;
786 }
787
788 void OBJECT_OT_constraints_clear(wmOperatorType *ot)
789 {
790         /* identifiers */
791         ot->name = "Clear Constraints";
792         ot->idname= "OBJECT_OT_constraints_clear";
793         ot->description= "Clear all the constraints for the active Object only.";
794         
795         /* callbacks */
796         ot->exec= object_constraints_clear_exec;
797         ot->poll= ED_operator_object_active;
798 }
799
800 /************************ add constraint operators *********************/
801
802 /* get the Object and/or PoseChannel to use as target */
803 static short get_new_constraint_target(bContext *C, int con_type, Object **tar_ob, bPoseChannel **tar_pchan, short add)
804 {
805         Object *obact= CTX_data_active_object(C);
806         short only_curve= 0, only_mesh= 0, only_ob= 0;
807         short found= 0;
808         
809         /* clear tar_ob and tar_pchan fields before use 
810          *      - assume for now that both always exist...
811          */
812         *tar_ob= NULL;
813         *tar_pchan= NULL;
814         
815         /* check if constraint type doesn't requires a target
816          *      - if so, no need to get any targets 
817          */
818         switch (con_type) {
819                 /* no-target constraints --------------------------- */
820                         /* null constraint - shouldn't even be added! */
821                 case CONSTRAINT_TYPE_NULL:
822                         /* limit constraints - no targets needed */
823                 case CONSTRAINT_TYPE_LOCLIMIT:
824                 case CONSTRAINT_TYPE_ROTLIMIT:
825                 case CONSTRAINT_TYPE_SIZELIMIT:
826                         return 0;
827                         
828                 /* restricted target-type constraints -------------- */
829                         /* curve-based constraints - set the only_curve and only_ob flags */
830                 case CONSTRAINT_TYPE_TRACKTO:
831                 case CONSTRAINT_TYPE_CLAMPTO:
832                 case CONSTRAINT_TYPE_FOLLOWPATH:
833                         only_curve= 1;
834                         only_ob= 1;
835                         break;
836                         
837                         /* mesh only? */
838                 case CONSTRAINT_TYPE_SHRINKWRAP:
839                         only_mesh= 1;
840                         only_ob= 1;
841                         break;
842                         
843                         /* object only */
844                 case CONSTRAINT_TYPE_RIGIDBODYJOINT:
845                         only_ob= 1;
846                         break;
847         }
848         
849         /* if the active Object is Armature, and we can search for bones, do so... */
850         if ((obact->type == OB_ARMATURE) && (only_ob == 0)) {
851                 /* search in list of selected Pose-Channels for target */
852                 CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pchans) 
853                 {
854                         /* just use the first one that we encounter... */
855                         *tar_ob= obact;
856                         *tar_pchan= pchan;
857                         found= 1;
858                         
859                         break;
860                 }
861                 CTX_DATA_END;
862         }
863         
864         /* if not yet found, try selected Objects... */
865         if (found == 0) {
866                 /* search in selected objects context */
867                 CTX_DATA_BEGIN(C, Object*, ob, selected_objects) 
868                 {
869                         /* just use the first object we encounter (that isn't the active object) 
870                          * and which fulfills the criteria for the object-target that we've got 
871                          */
872                         if ( (ob != obact) &&
873                                  ((!only_curve) || (ob->type == OB_CURVE)) && 
874                                  ((!only_mesh) || (ob->type == OB_MESH)) )
875                         {
876                                 /* set target */
877                                 *tar_ob= ob;
878                                 found= 1;
879                                 
880                                 /* perform some special operations on the target */
881                                 if (only_curve) {
882                                         /* Curve-Path option must be enabled for follow-path constraints to be able to work */
883                                         Curve *cu= (Curve *)ob->data;
884                                         cu->flag |= CU_PATH;
885                                 }
886                                 
887                                 break;
888                         }
889                 }
890                 CTX_DATA_END;
891         }
892         
893         /* if still not found, add a new empty to act as a target (if allowed) */
894         if ((found == 0) && (add)) {
895 #if 0 // XXX old code to be fixed
896                 Base *base= BASACT, *newbase;
897                 Object *obt;
898                 
899                 obt= add_object(scene, OB_EMPTY);
900                 /* set layers OK */
901                 newbase= BASACT;
902                 newbase->lay= base->lay;
903                 obt->lay= newbase->lay;
904                 
905                 /* transform cent to global coords for loc */
906                 if (pchanact) {
907                         if (only_IK)
908                                 VecMat4MulVecfl(obt->loc, ob->obmat, pchanact->pose_tail);
909                         else
910                                 VecMat4MulVecfl(obt->loc, ob->obmat, pchanact->pose_head);
911                 }
912                 else
913                         VECCOPY(obt->loc, ob->obmat[3]);
914                 
915                 //set_constraint_nth_target(con, obt, "", 0);
916                 
917                 /* restore, add_object sets active */
918                 BASACT= base;
919                 base->flag |= SELECT;
920 #endif // XXX old code to be ported
921         }
922         
923         /* return whether there's any target */
924         return found;
925 }
926
927 /* used by add constraint operators to add the constraint required */
928 static int constraint_add_exec(bContext *C, wmOperator *op, Object *ob, ListBase *list, int type, short setTarget)
929 {
930         Scene *scene= CTX_data_scene(C);
931         bPoseChannel *pchan= get_active_posechannel(ob);
932         bConstraint *con;
933         
934         /* check if constraint to be added is valid for the given constraints stack */
935         if (type == CONSTRAINT_TYPE_NULL) {
936                 return OPERATOR_CANCELLED;
937         }
938         if ( (type == CONSTRAINT_TYPE_RIGIDBODYJOINT) && (list != &ob->constraints) ) {
939                 BKE_report(op->reports, RPT_ERROR, "Rigid Body Joint Constraint can only be added to Objects.");
940                 return OPERATOR_CANCELLED;
941         }
942         if ( (type == CONSTRAINT_TYPE_KINEMATIC) && ((!pchan) || (list != &pchan->constraints)) ) {
943                 BKE_report(op->reports, RPT_ERROR, "IK Constraint can only be added to Bones.");
944                 return OPERATOR_CANCELLED;
945         }
946         
947         /* create a new constraint of the type requried, and add it to the active/given constraints list */
948         con = add_new_constraint(type);
949         
950         if (list) {
951                 bConstraint *coniter; 
952                 
953                 /* add new constraint to end of list of constraints before ensuring that it has a unique name 
954                  * (otherwise unique-naming code will fail, since it assumes element exists in list)
955                  */
956                 BLI_addtail(list, con);
957                 unique_constraint_name(con, list);
958                 
959                 /* if the target list is a list on some PoseChannel belonging to a proxy-protected 
960                  * Armature layer, we must tag newly added constraints with a flag which allows them
961                  * to persist after proxy syncing has been done
962                  */
963                 if (proxylocked_constraints_owner(ob, pchan))
964                         con->flag |= CONSTRAINT_PROXY_LOCAL;
965                 
966                 /* make this constraint the active one 
967                  *      - since constraint was added at end of stack, we can just go 
968                  *        through deactivating all previous ones
969                  */
970                 con->flag |= CONSTRAINT_ACTIVE;
971                 for (coniter= con->prev; coniter; coniter= coniter->prev)
972                         coniter->flag &= ~CONSTRAINT_ACTIVE;
973         }
974         
975         /* get the first selected object/bone, and make that the target
976          *      - apart from the buttons-window add buttons, we shouldn't add in this way
977          */
978         if (setTarget) {
979                 Object *tar_ob= NULL;
980                 bPoseChannel *tar_pchan= NULL;
981                 
982                 /* get the target objects, adding them as need be */
983                 if (get_new_constraint_target(C, type, &tar_ob, &tar_pchan, 1)) {
984                         /* method of setting target depends on the type of target we've got 
985                          *      - by default, just set the first target (distinction here is only for multiple-targetted constraints)
986                          */
987                         if (tar_pchan)
988                                 set_constraint_nth_target(con, tar_ob, tar_pchan->name, 0);
989                         else
990                                 set_constraint_nth_target(con, tar_ob, "", 0);
991                 }
992         }
993         
994         /* do type-specific tweaking to the constraint settings  */
995         switch (type) {
996                 case CONSTRAINT_TYPE_CHILDOF:
997                 {
998                         /* if this constraint is being added to a posechannel, make sure
999                          * the constraint gets evaluated in pose-space */
1000                         if (ob->flag & OB_POSEMODE) {
1001                                 con->ownspace = CONSTRAINT_SPACE_POSE;
1002                                 con->flag |= CONSTRAINT_SPACEONCE;
1003                         }
1004                 }
1005                         break;
1006                         
1007                 case CONSTRAINT_TYPE_PYTHON: // FIXME: this code is not really valid anymore
1008                 {
1009                         char *menustr;
1010                         int scriptint= 0;
1011 #ifndef DISABLE_PYTHON
1012                         /* popup a list of usable scripts */
1013                         menustr = buildmenu_pyconstraints(NULL, &scriptint);
1014                         scriptint = pupmenu(menustr);
1015                         MEM_freeN(menustr);
1016                         
1017                         /* only add constraint if a script was chosen */
1018                         if (scriptint) {
1019                                 /* add constraint */
1020                                 validate_pyconstraint_cb(con->data, &scriptint);
1021                                 
1022                                 /* make sure target allowance is set correctly */
1023                                 BPY_pyconstraint_update(ob, con);
1024                         }
1025 #endif
1026                 }
1027                 default:
1028                         break;
1029         }
1030         
1031         /* make sure all settings are valid - similar to above checks, but sometimes can be wrong */
1032         object_test_constraints(ob);
1033         
1034         if (ob->pose)
1035                 update_pose_constraint_flags(ob->pose);
1036         
1037         
1038         /* force depsgraph to get recalculated since new relationships added */
1039         DAG_scene_sort(scene);          /* sort order of objects */
1040         
1041         if ((ob->type==OB_ARMATURE) && (pchan)) {
1042                 ob->pose->flag |= POSE_RECALC;  /* sort pose channels */
1043                 DAG_object_flush_update(scene, ob, OB_RECALC_DATA|OB_RECALC_OB);
1044         }
1045         else
1046                 DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
1047         
1048         /* notifiers for updates */
1049         WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT|NA_ADDED, ob);
1050         
1051         return OPERATOR_FINISHED;
1052 }
1053
1054 /* ------------------ */
1055
1056 /* dummy operator callback */
1057 static int object_constraint_add_exec(bContext *C, wmOperator *op)
1058 {
1059         ScrArea *sa= CTX_wm_area(C);
1060         Object *ob;
1061         int type= RNA_enum_get(op->ptr, "type");
1062         short with_targets= 0;
1063         
1064         /* get active object from context */
1065         if (sa->spacetype == SPACE_BUTS)
1066                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1067         else
1068                 ob= CTX_data_active_object(C);
1069         
1070         if (!ob) {
1071                 BKE_report(op->reports, RPT_ERROR, "No active object to add constraint to.");
1072                 return OPERATOR_CANCELLED;
1073         }
1074                 
1075         /* hack: set constraint targets from selected objects in context is allowed when
1076          *              operator name included 'with_targets', since the menu doesn't allow multiple properties
1077          */
1078         if (strstr(op->idname, "with_targets"))
1079                 with_targets= 1;
1080
1081         return constraint_add_exec(C, op, ob, &ob->constraints, type, with_targets);
1082 }
1083
1084 /* dummy operator callback */
1085 static int pose_constraint_add_exec(bContext *C, wmOperator *op)
1086 {
1087         ScrArea *sa= CTX_wm_area(C);
1088         Object *ob;
1089         int type= RNA_enum_get(op->ptr, "type");
1090         short with_targets= 0;
1091         
1092         /* get active object from context */
1093         if (sa->spacetype == SPACE_BUTS)
1094                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1095         else
1096                 ob= CTX_data_active_object(C);
1097         
1098         if (!ob) {
1099                 BKE_report(op->reports, RPT_ERROR, "No active object to add constraint to.");
1100                 return OPERATOR_CANCELLED;
1101         }
1102                 
1103         /* hack: set constraint targets from selected objects in context is allowed when
1104          *              operator name included 'with_targets', since the menu doesn't allow multiple properties
1105          */
1106         if (strstr(op->idname, "with_targets"))
1107                 with_targets= 1;
1108         
1109         return constraint_add_exec(C, op, ob, get_active_constraints(ob), type, with_targets);
1110 }
1111
1112 /* ------------------ */
1113
1114 void OBJECT_OT_constraint_add(wmOperatorType *ot)
1115 {
1116         /* identifiers */
1117         ot->name= "Add Constraint";
1118         ot->description = "Add a constraint to the active object.";
1119         ot->idname= "OBJECT_OT_constraint_add";
1120         
1121         /* api callbacks */
1122         ot->invoke= WM_menu_invoke;
1123         ot->exec= object_constraint_add_exec;
1124         ot->poll= ED_operator_object_active;
1125         
1126         /* flags */
1127         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1128         
1129         /* properties */
1130         RNA_def_enum(ot->srna, "type", constraint_type_items, 0, "Type", "");
1131 }
1132
1133 void OBJECT_OT_constraint_add_with_targets(wmOperatorType *ot)
1134 {
1135         /* identifiers */
1136         ot->name= "Add Constraint (with Targets)";
1137         ot->description = "Add a constraint to the active object, with target (where applicable) set to the selected Objects/Bones.";
1138         ot->idname= "OBJECT_OT_constraint_add_with_targets";
1139         
1140         /* api callbacks */
1141         ot->invoke= WM_menu_invoke;
1142         ot->exec= object_constraint_add_exec;
1143         ot->poll= ED_operator_object_active;
1144         
1145         /* flags */
1146         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1147         
1148         /* properties */
1149         RNA_def_enum(ot->srna, "type", constraint_type_items, 0, "Type", "");
1150 }
1151
1152 void POSE_OT_constraint_add(wmOperatorType *ot)
1153 {
1154         /* identifiers */
1155         ot->name= "Add Constraint";
1156         ot->description = "Add a constraint to the active bone.";
1157         ot->idname= "POSE_OT_constraint_add";
1158         
1159         /* api callbacks */
1160         ot->invoke= WM_menu_invoke;
1161         ot->exec= pose_constraint_add_exec;
1162         ot->poll= ED_operator_posemode;
1163         
1164         /* flags */
1165         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1166         
1167         /* properties */
1168         RNA_def_enum(ot->srna, "type", constraint_type_items, 0, "Type", "");
1169 }
1170
1171 void POSE_OT_constraint_add_with_targets(wmOperatorType *ot)
1172 {
1173         /* identifiers */
1174         ot->name= "Add Constraint (with Targets)";
1175         ot->description = "Add a constraint to the active bone, with target (where applicable) set to the selected Objects/Bones.";
1176         ot->idname= "POSE_OT_constraint_add_with_targets";
1177         
1178         /* api callbacks */
1179         ot->invoke= WM_menu_invoke;
1180         ot->exec= pose_constraint_add_exec;
1181         ot->poll= ED_operator_posemode;
1182         
1183         /* flags */
1184         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1185         
1186         /* properties */
1187         RNA_def_enum(ot->srna, "type", constraint_type_items, 0, "Type", "");
1188 }
1189