merge with trunk at r27259 and commit of a patch by anthony jones to fix msvc (though...
[blender-staging.git] / source / blender / editors / object / object_constraint.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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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_math.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 #include "BIK_api.h"
60
61 #ifndef DISABLE_PYTHON
62 #include "BPY_extern.h"
63 #endif
64
65 #include "WM_api.h"
66 #include "WM_types.h"
67
68 #include "RNA_access.h"
69 #include "RNA_define.h"
70 #include "RNA_enum_types.h"
71 #include "RNA_types.h"
72
73 #include "ED_object.h"
74 #include "ED_screen.h"
75
76 #include "UI_interface.h"
77
78 #include "object_intern.h"
79
80 /* -------------- Get Active Constraint Data ---------------------- */
81
82 /* if object in posemode, active bone constraints, else object constraints */
83 ListBase *get_active_constraints (Object *ob)
84 {
85         if (ob == NULL)
86                 return NULL;
87         
88         if (ob->mode & OB_MODE_POSE) {
89                 bPoseChannel *pchan;
90                 
91                 pchan = get_active_posechannel(ob);
92                 if (pchan)
93                         return &pchan->constraints;
94         }
95         else 
96                 return &ob->constraints;
97         
98         return NULL;
99 }
100
101 /* Find the list that a given constraint belongs to, and/or also get the posechannel this is from (if applicable) */
102 ListBase *get_constraint_lb (Object *ob, bConstraint *con, bPoseChannel **pchan_r)
103 {
104         if (pchan_r)
105                 *pchan_r= NULL;
106         
107         if (ELEM(NULL, ob, con))
108                 return NULL;
109         
110         /* try object constraints first */
111         if ((BLI_findindex(&ob->constraints, con) != -1)) {
112                 return &ob->constraints;
113         }
114         
115         /* if armature, try pose bones too */
116         if (ob->pose) {
117                 bPoseChannel *pchan;
118                 
119                 /* try each bone in order 
120                  * NOTE: it's not possible to directly look up the active bone yet, so this will have to do
121                  */
122                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
123                         if ((BLI_findindex(&pchan->constraints, con) != -1)) {
124                                 
125                                 if (pchan_r)
126                                         *pchan_r= pchan;
127                                 
128                                 return &pchan->constraints;
129                         }
130                 }
131         }
132         
133         /* done */
134         return NULL;
135 }
136
137 /* single constraint */
138 bConstraint *get_active_constraint (Object *ob)
139 {
140         return constraints_get_active(get_active_constraints(ob));
141 }
142 /* -------------- Constraint Management (Add New, Remove, Rename) -------------------- */
143 /* ------------- PyConstraints ------------------ */
144
145 /* this callback sets the text-file to be used for selected menu item */
146 void validate_pyconstraint_cb (void *arg1, void *arg2)
147 {
148         bPythonConstraint *data = arg1;
149         Text *text= NULL;
150         int index = *((int *)arg2);
151         int i;
152         
153         /* exception for no script */
154         if (index) {
155                 /* innovative use of a for...loop to search */
156                 for (text=G.main->text.first, i=1; text && index!=i; i++, text=text->id.next);
157         }
158         data->text = text;
159 }
160
161 #ifndef DISABLE_PYTHON
162 /* this returns a string for the list of usable pyconstraint script names */
163 char *buildmenu_pyconstraints (Text *con_text, int *pyconindex)
164 {
165         DynStr *pupds= BLI_dynstr_new();
166         Text *text;
167         char *str;
168         char buf[64];
169         int i;
170         
171         /* add title first */
172         sprintf(buf, "Scripts: %%t|[None]%%x0|");
173         BLI_dynstr_append(pupds, buf);
174         
175         /* init active-index first */
176         if (con_text == NULL)
177                 *pyconindex= 0;
178         
179         /* loop through markers, adding them */
180         for (text=G.main->text.first, i=1; text; i++, text=text->id.next) {
181                 /* this is important to ensure that right script is shown as active */
182                 if (text == con_text) *pyconindex = i;
183                 
184                 /* only include valid pyconstraint scripts */
185                 if (BPY_is_pyconstraint(text)) {
186                         BLI_dynstr_append(pupds, text->id.name+2);
187                         
188                         sprintf(buf, "%%x%d", i);
189                         BLI_dynstr_append(pupds, buf);
190                         
191                         if (text->id.next)
192                                 BLI_dynstr_append(pupds, "|");
193                 }
194         }
195         
196         /* convert to normal MEM_malloc'd string */
197         str= BLI_dynstr_get_cstring(pupds);
198         BLI_dynstr_free(pupds);
199         
200         return str;
201 }
202 #endif /* DISABLE_PYTHON */
203
204 /* this callback gets called when the 'refresh' button of a pyconstraint gets pressed */
205 void update_pyconstraint_cb (void *arg1, void *arg2)
206 {
207         Object *owner= (Object *)arg1;
208         bConstraint *con= (bConstraint *)arg2;
209 #ifndef DISABLE_PYTHON
210         if (owner && con)
211                 BPY_pyconstraint_update(owner, con);
212 #endif
213 }
214
215 /* helper function for add_constriant - sets the last target for the active constraint */
216 static void set_constraint_nth_target (bConstraint *con, Object *target, char subtarget[], int index)
217 {
218         bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
219         ListBase targets = {NULL, NULL};
220         bConstraintTarget *ct;
221         int num_targets, i;
222         
223         if (cti && cti->get_constraint_targets) {
224                 cti->get_constraint_targets(con, &targets);
225                 num_targets= BLI_countlist(&targets);
226                 
227                 if (index < 0) {
228                         if (abs(index) < num_targets)
229                                 index= num_targets - abs(index);
230                         else
231                                 index= num_targets - 1;
232                 }
233                 else if (index >= num_targets) {
234                         index= num_targets - 1;
235                 }
236                 
237                 for (ct=targets.first, i=0; ct; ct= ct->next, i++) {
238                         if (i == index) {
239                                 ct->tar= target;
240                                 strcpy(ct->subtarget, subtarget);
241                                 break;
242                         }
243                 }
244                 
245                 if (cti->flush_constraint_targets)
246                         cti->flush_constraint_targets(con, &targets, 0);
247         }
248 }
249
250 /* ------------- Constraint Sanity Testing ------------------- */
251
252 /* checks validity of object pointers, and NULLs,
253  * if Bone doesnt exist it sets the CONSTRAINT_DISABLE flag.
254  */
255 static void test_constraints (Object *owner, bPoseChannel *pchan)
256 {
257         bConstraint *curcon;
258         ListBase *conlist= NULL;
259         int type;
260         
261         if (owner==NULL) return;
262         
263         /* Check parents */
264         if (pchan) {
265                 switch (owner->type) {
266                         case OB_ARMATURE:
267                                 type = CONSTRAINT_OBTYPE_BONE;
268                                 break;
269                         default:
270                                 type = CONSTRAINT_OBTYPE_OBJECT;
271                                 break;
272                 }
273         }
274         else
275                 type = CONSTRAINT_OBTYPE_OBJECT;
276         
277         /* Get the constraint list for this object */
278         switch (type) {
279                 case CONSTRAINT_OBTYPE_OBJECT:
280                         conlist = &owner->constraints;
281                         break;
282                 case CONSTRAINT_OBTYPE_BONE:
283                         conlist = &pchan->constraints;
284                         break;
285         }
286         
287         /* Check all constraints - is constraint valid? */
288         if (conlist) {
289                 for (curcon = conlist->first; curcon; curcon=curcon->next) {
290                         bConstraintTypeInfo *cti= constraint_get_typeinfo(curcon);
291                         ListBase targets = {NULL, NULL};
292                         bConstraintTarget *ct;
293                         
294                         /* clear disabled-flag first */
295                         curcon->flag &= ~CONSTRAINT_DISABLE;
296                         
297                         if (curcon->type == CONSTRAINT_TYPE_KINEMATIC) {
298                                 bKinematicConstraint *data = curcon->data;
299                                 
300                                 /* bad: we need a separate set of checks here as poletarget is 
301                                  *              optional... otherwise poletarget must exist too or else
302                                  *              the constraint is deemed invalid
303                                  */
304                                 /* default IK check ... */
305                                 if (exist_object(data->tar) == 0) {
306                                         data->tar = NULL;
307                                         curcon->flag |= CONSTRAINT_DISABLE;
308                                 }
309                                 else if (data->tar == owner) {
310                                         if (!get_named_bone(get_armature(owner), data->subtarget)) {
311                                                 curcon->flag |= CONSTRAINT_DISABLE;
312                                         }
313                                 }
314                                 
315                                 if (data->poletar) {
316                                         if (exist_object(data->poletar) == 0) {
317                                                 data->poletar = NULL;
318                                                 curcon->flag |= CONSTRAINT_DISABLE;
319                                         }
320                                         else if (data->poletar == owner) {
321                                                 if (!get_named_bone(get_armature(owner), data->polesubtarget)) {
322                                                         curcon->flag |= CONSTRAINT_DISABLE;
323                                                 }
324                                         }
325                                 }
326                                 /* ... can be overwritten here */
327                                 BIK_test_constraint(owner, curcon);
328                                 /* targets have already been checked for this */
329                                 continue;
330                         }
331                         else if (curcon->type == CONSTRAINT_TYPE_ACTION) {
332                                 bActionConstraint *data = curcon->data;
333                                 
334                                 /* validate action */
335                                 if (data->act == NULL) 
336                                         curcon->flag |= CONSTRAINT_DISABLE;
337                         }
338                         else if (curcon->type == CONSTRAINT_TYPE_FOLLOWPATH) {
339                                 bFollowPathConstraint *data = curcon->data;
340                                 
341                                 /* don't allow track/up axes to be the same */
342                                 if (data->upflag==data->trackflag)
343                                         curcon->flag |= CONSTRAINT_DISABLE;
344                                 if (data->upflag+3==data->trackflag)
345                                         curcon->flag |= CONSTRAINT_DISABLE;
346                         }
347                         else if (curcon->type == CONSTRAINT_TYPE_TRACKTO) {
348                                 bTrackToConstraint *data = curcon->data;
349                                 
350                                 /* don't allow track/up axes to be the same */
351                                 if (data->reserved2==data->reserved1)
352                                         curcon->flag |= CONSTRAINT_DISABLE;
353                                 if (data->reserved2+3==data->reserved1)
354                                         curcon->flag |= CONSTRAINT_DISABLE;
355                         }
356                         else if (curcon->type == CONSTRAINT_TYPE_LOCKTRACK) {
357                                 bLockTrackConstraint *data = curcon->data;
358                                 
359                                 if (data->lockflag==data->trackflag)
360                                         curcon->flag |= CONSTRAINT_DISABLE;
361                                 if (data->lockflag+3==data->trackflag)
362                                         curcon->flag |= CONSTRAINT_DISABLE;
363                         }
364                         else if (curcon->type == CONSTRAINT_TYPE_SPLINEIK) {
365                                 bSplineIKConstraint *data = curcon->data;
366                         
367                                 /* if the number of points does not match the amount required by the chain length,
368                                  * free the points array and request a rebind...
369                                  */
370                                 if ((data->points == NULL) || (data->numpoints != data->chainlen+1))
371                                 {
372                                         /* free the points array */
373                                         if (data->points) {
374                                                 MEM_freeN(data->points);
375                                                 data->points = NULL;
376                                         }
377                                         
378                                         /* clear the bound flag, forcing a rebind next time this is evaluated */
379                                         data->flag &= ~CONSTRAINT_SPLINEIK_BOUND;
380                                 }
381                         }
382                         
383                         /* Check targets for constraints */
384                         if (cti && cti->get_constraint_targets) {
385                                 cti->get_constraint_targets(curcon, &targets);
386                                 
387                                 /* disable and clear constraints targets that are incorrect */
388                                 for (ct= targets.first; ct; ct= ct->next) {
389                                         /* general validity checks (for those constraints that need this) */
390                                         if (exist_object(ct->tar) == 0) {
391                                                 ct->tar = NULL;
392                                                 curcon->flag |= CONSTRAINT_DISABLE;
393                                         }
394                                         else if (ct->tar == owner) {
395                                                 if (!get_named_bone(get_armature(owner), ct->subtarget)) {
396                                                         curcon->flag |= CONSTRAINT_DISABLE;
397                                                 }
398                                         }
399                                         
400                                         /* target checks for specific constraints */
401                                         if (ELEM3(curcon->type, CONSTRAINT_TYPE_FOLLOWPATH, CONSTRAINT_TYPE_CLAMPTO, CONSTRAINT_TYPE_SPLINEIK)) {
402                                                 if (ct->tar) {
403                                                         if (ct->tar->type != OB_CURVE) {
404                                                                 ct->tar= NULL;
405                                                                 curcon->flag |= CONSTRAINT_DISABLE;
406                                                         }
407                                                         else {
408                                                                 Curve *cu= ct->tar->data;
409                                                                 
410                                                                 /* auto-set 'Path' setting on curve so this works  */
411                                                                 cu->flag |= CU_PATH;
412                                                         }
413                                                 }                                               
414                                         }
415                                 }       
416                                 
417                                 /* free any temporary targets */
418                                 if (cti->flush_constraint_targets)
419                                         cti->flush_constraint_targets(curcon, &targets, 0);
420                         }
421                 }
422         }
423 }
424
425 void object_test_constraints (Object *owner)
426 {
427         if(owner->constraints.first)
428                 test_constraints(owner, NULL);
429
430         if (owner->type==OB_ARMATURE && owner->pose) {
431                 bPoseChannel *pchan;
432
433                 for (pchan= owner->pose->chanbase.first; pchan; pchan= pchan->next)
434                         if(pchan->constraints.first)
435                                 test_constraints(owner, pchan);
436         }
437 }
438
439 /* ********************** CONSTRAINT-SPECIFIC STUFF ********************* */
440
441 /* ---------- Distance-Dependent Constraints ---------- */
442 /* StretchTo, Limit Distance */
443
444 static int stretchto_poll(bContext *C)
445 {
446         PointerRNA ptr= CTX_data_pointer_get_type(C, "constraint", &RNA_StretchToConstraint);
447         return (ptr.id.data && ptr.data);
448 }
449
450 static int stretchto_reset_exec (bContext *C, wmOperator *op)
451 {
452         PointerRNA ptr= CTX_data_pointer_get_type(C, "constraint", &RNA_StretchToConstraint);
453         
454         /* just set original length to 0.0, which will cause a reset on next recalc */
455         RNA_float_set(&ptr, "original_length", 0.0f);
456         
457         WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, NULL);
458         return OPERATOR_FINISHED;
459 }
460
461 void CONSTRAINT_OT_stretchto_reset (wmOperatorType *ot)
462 {
463         /* identifiers */
464         ot->name= "Reset Original Length";
465         ot->idname= "CONSTRAINT_OT_stretchto_reset";
466         ot->description= "Reset original length of bone for Stretch To Constraint";
467         
468         ot->exec= stretchto_reset_exec;
469         ot->poll= stretchto_poll;
470         
471         /* flags */
472         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
473 }
474
475
476 static int limitdistance_reset_exec (bContext *C, wmOperator *op)
477 {
478         PointerRNA ptr= CTX_data_pointer_get_type(C, "constraint", &RNA_LimitDistanceConstraint);
479         
480         /* just set distance to 0.0, which will cause a reset on next recalc */
481         RNA_float_set(&ptr, "distance", 0.0f);
482         
483         WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, NULL);
484         return OPERATOR_FINISHED;
485 }
486
487 static int limitdistance_poll(bContext *C)
488 {
489         PointerRNA ptr= CTX_data_pointer_get_type(C, "constraint", &RNA_LimitDistanceConstraint);
490         return (ptr.id.data && ptr.data);
491 }
492
493 void CONSTRAINT_OT_limitdistance_reset (wmOperatorType *ot)
494 {
495         /* identifiers */
496         ot->name= "Reset Distance";
497         ot->idname= "CONSTRAINT_OT_limitdistance_reset";
498         ot->description= "Reset limiting distance for Limit Distance Constraint";
499         
500         ot->exec= limitdistance_reset_exec;
501         ot->poll= limitdistance_poll;
502         
503         /* flags */
504         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
505 }
506
507 /* ------------- Child-Of Constraint ------------------ */
508
509 static int childof_poll(bContext *C)
510 {
511         PointerRNA ptr= CTX_data_pointer_get_type(C, "constraint", &RNA_ChildOfConstraint);
512         return (ptr.id.data && ptr.data);
513 }
514
515 /* ChildOf Constraint - set inverse callback */
516 static int childof_set_inverse_exec (bContext *C, wmOperator *op)
517 {
518         PointerRNA ptr= CTX_data_pointer_get_type(C, "constraint", &RNA_ChildOfConstraint);
519         Scene *scene= CTX_data_scene(C);
520         Object *ob= ptr.id.data;
521         bConstraint *con= ptr.data;
522         bChildOfConstraint *data= (bChildOfConstraint *)con->data;
523         bPoseChannel *pchan= NULL;
524
525         /* try to find a pose channel */
526         // TODO: get from context instead?
527         if (ob && ob->pose)
528                 pchan= get_active_posechannel(ob);
529         
530         /* calculate/set inverse matrix */
531         if (pchan) {
532                 float pmat[4][4], cinf;
533                 float imat[4][4], tmat[4][4];
534                 
535                 /* make copy of pchan's original pose-mat (for use later) */
536                 copy_m4_m4(pmat, pchan->pose_mat);
537                 
538                 /* disable constraint for pose to be solved without it */
539                 cinf= con->enforce;
540                 con->enforce= 0.0f;
541                 
542                 /* solve pose without constraint */
543                 where_is_pose(scene, ob);
544                 
545                 /* determine effect of constraint by removing the newly calculated 
546                  * pchan->pose_mat from the original pchan->pose_mat, thus determining 
547                  * the effect of the constraint
548                  */
549                 invert_m4_m4(imat, pchan->pose_mat);
550                 mul_m4_m4m4(tmat, imat, pmat);
551                 invert_m4_m4(data->invmat, tmat);
552                 
553                 /* recalculate pose with new inv-mat */
554                 con->enforce= cinf;
555                 where_is_pose(scene, ob);
556         }
557         else if (ob) {
558                 Object workob;
559                 /* use what_does_parent to find inverse - just like for normal parenting.
560                  * NOTE: what_does_parent uses a static workob defined in object.c 
561                  */
562                 what_does_parent(scene, ob, &workob);
563                 invert_m4_m4(data->invmat, workob.obmat);
564         }
565         else
566                 unit_m4(data->invmat);
567                 
568         WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, ob);
569                 
570         return OPERATOR_FINISHED;
571 }
572
573 void CONSTRAINT_OT_childof_set_inverse (wmOperatorType *ot)
574 {
575         /* identifiers */
576         ot->name= "Set Inverse";
577         ot->idname= "CONSTRAINT_OT_childof_set_inverse";
578         ot->description= "Set inverse correction for ChildOf constraint";
579         
580         ot->exec= childof_set_inverse_exec;
581         ot->poll= childof_poll;
582         
583         /* flags */
584         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
585 }
586
587 /* ChildOf Constraint - clear inverse callback */
588 static int childof_clear_inverse_exec (bContext *C, wmOperator *op)
589 {
590         PointerRNA ptr= CTX_data_pointer_get_type(C, "constraint", &RNA_ChildOfConstraint);
591         Object *ob= ptr.id.data;
592         bConstraint *con= ptr.data;
593         bChildOfConstraint *data= (bChildOfConstraint *)con->data;
594         
595         /* simply clear the matrix */
596         unit_m4(data->invmat);
597         
598         WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, ob);
599         
600         return OPERATOR_FINISHED;
601 }
602
603 void CONSTRAINT_OT_childof_clear_inverse (wmOperatorType *ot)
604 {
605         /* identifiers */
606         ot->name= "Clear Inverse";
607         ot->idname= "CONSTRAINT_OT_childof_clear_inverse";
608         ot->description= "Clear inverse correction for ChildOf constraint";
609         
610         ot->exec= childof_clear_inverse_exec;
611         ot->poll= childof_poll;
612         
613         /* flags */
614         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
615 }
616
617 /***************************** BUTTONS ****************************/
618
619 void ED_object_constraint_set_active(Object *ob, bConstraint *con)
620 {       
621         ListBase *lb = get_constraint_lb(ob, con, NULL);
622         
623         /* lets be nice and escape if its active already */
624         // NOTE: this assumes that the stack doesn't have other active ones set...
625         if ((lb && con) && (con->flag & CONSTRAINT_ACTIVE))
626                 return;
627         
628         constraints_set_active(lb, con);
629 }
630
631 void ED_object_constraint_update(Object *ob)
632 {
633
634         if(ob->pose) update_pose_constraint_flags(ob->pose);
635
636         object_test_constraints(ob);
637
638         if(ob->type==OB_ARMATURE) DAG_id_flush_update(&ob->id, OB_RECALC_DATA|OB_RECALC_OB);
639         else DAG_id_flush_update(&ob->id, OB_RECALC_OB);
640 }
641
642 void ED_object_constraint_dependency_update(Scene *scene, Object *ob)
643 {
644         ED_object_constraint_update(ob);
645
646         if(ob->pose) ob->pose->flag |= POSE_RECALC;     // checks & sorts pose channels
647     DAG_scene_sort(scene);
648 }
649
650 static int constraint_poll(bContext *C)
651 {
652         PointerRNA ptr= CTX_data_pointer_get_type(C, "constraint", &RNA_Constraint);
653         return (ptr.id.data && ptr.data);
654 }
655
656 static int constraint_delete_exec (bContext *C, wmOperator *op)
657 {
658         PointerRNA ptr= CTX_data_pointer_get_type(C, "constraint", &RNA_Constraint);
659         Object *ob= ptr.id.data;
660         bConstraint *con= ptr.data;
661         ListBase *lb = get_constraint_lb(ob, con, NULL);
662         
663         /* free the constraint */
664         if (remove_constraint(lb, con)) {
665                 /* there's no active constraint now, so make sure this is the case */
666                 constraints_set_active(lb, NULL);
667                 
668                 /* notifiers */
669                 WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, ob);
670                 
671                 return OPERATOR_FINISHED;
672         }
673         else {
674                 /* couldn't remove due to some invalid data */
675                 return OPERATOR_CANCELLED;
676         }
677 }
678
679 void CONSTRAINT_OT_delete (wmOperatorType *ot)
680 {
681         /* identifiers */
682         ot->name= "Delete Constraint";
683         ot->idname= "CONSTRAINT_OT_delete";
684         ot->description= "Remove constraitn from constraint stack";
685         
686         /* callbacks */
687         ot->exec= constraint_delete_exec;
688         ot->poll= constraint_poll;
689         
690         /* flags */
691         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; 
692 }
693
694 static int constraint_move_down_exec (bContext *C, wmOperator *op)
695 {
696         PointerRNA ptr= CTX_data_pointer_get_type(C, "constraint", &RNA_Constraint);
697         Object *ob= ptr.id.data;
698         bConstraint *con= ptr.data;
699         
700         if (con->next) {
701                 ListBase *conlist= get_constraint_lb(ob, con, NULL);
702                 bConstraint *nextCon= con->next;
703                 
704                 /* insert the nominated constraint after the one that used to be after it */
705                 BLI_remlink(conlist, con);
706                 BLI_insertlinkafter(conlist, nextCon, con);
707                 
708                 WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, ob);
709                 
710                 return OPERATOR_FINISHED;
711         }
712         
713         return OPERATOR_CANCELLED;
714 }
715
716 void CONSTRAINT_OT_move_down (wmOperatorType *ot)
717 {
718         /* identifiers */
719         ot->name= "Move Constraint Down";
720         ot->idname= "CONSTRAINT_OT_move_down";
721         ot->description= "Move constraint down constraint stack";
722         
723         /* callbacks */
724         ot->exec= constraint_move_down_exec;
725         ot->poll= constraint_poll;
726         
727         /* flags */
728         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; 
729 }
730
731
732 static int constraint_move_up_exec (bContext *C, wmOperator *op)
733 {
734         PointerRNA ptr= CTX_data_pointer_get_type(C, "constraint", &RNA_Constraint);
735         Object *ob= ptr.id.data;
736         bConstraint *con= ptr.data;
737         
738         if (con->prev) {
739                 ListBase *conlist= get_constraint_lb(ob, con, NULL);
740                 bConstraint *prevCon= con->prev;
741                 
742                 /* insert the nominated constraint before the one that used to be before it */
743                 BLI_remlink(conlist, con);
744                 BLI_insertlinkbefore(conlist, prevCon, con);
745                 
746                 WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, ob);
747                 
748                 return OPERATOR_FINISHED;
749         }
750         
751         return OPERATOR_CANCELLED;
752 }
753
754 void CONSTRAINT_OT_move_up (wmOperatorType *ot)
755 {
756         /* identifiers */
757         ot->name= "Move Constraint Up";
758         ot->idname= "CONSTRAINT_OT_move_up";
759         ot->description= "Move constraint up constraint stack";
760         
761         /* callbacks */
762         ot->exec= constraint_move_up_exec;
763         ot->poll= constraint_poll;
764         
765         /* flags */
766         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; 
767 }
768
769 /***************************** OPERATORS ****************************/
770
771 /************************ remove constraint operators *********************/
772
773 static int pose_constraints_clear_exec(bContext *C, wmOperator *op)
774 {
775         Object *ob= CTX_data_active_object(C);
776         Scene *scene= CTX_data_scene(C);
777         
778         /* free constraints for all selected bones */
779         CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pose_bones)
780         {
781                 free_constraints(&pchan->constraints);
782                 pchan->constflag &= ~(PCHAN_HAS_IK|PCHAN_HAS_SPLINEIK|PCHAN_HAS_CONST);
783         }
784         CTX_DATA_END;
785         
786         /* force depsgraph to get recalculated since relationships removed */
787         DAG_scene_sort(scene);          /* sort order of objects */     
788         
789         /* do updates */
790         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
791         WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, ob);
792         
793         return OPERATOR_FINISHED;
794 }
795
796 void POSE_OT_constraints_clear(wmOperatorType *ot)
797 {
798         /* identifiers */
799         ot->name = "Clear Constraints";
800         ot->idname= "POSE_OT_constraints_clear";
801         ot->description= "Clear all the constraints for the selected bones";
802         
803         /* callbacks */
804         ot->exec= pose_constraints_clear_exec;
805         ot->poll= ED_operator_posemode; // XXX - do we want to ensure there are selected bones too?
806 }
807
808
809 static int object_constraints_clear_exec(bContext *C, wmOperator *op)
810 {
811         Object *ob= CTX_data_active_object(C);
812         Scene *scene= CTX_data_scene(C);
813         
814         /* do freeing */
815         // TODO: we should free constraints for all selected objects instead (to be more consistent with bones)
816         free_constraints(&ob->constraints);
817         
818         /* force depsgraph to get recalculated since relationships removed */
819         DAG_scene_sort(scene);          /* sort order of objects */     
820         
821         /* do updates */
822         DAG_id_flush_update(&ob->id, OB_RECALC_OB);
823         WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, ob);
824         
825         return OPERATOR_FINISHED;
826 }
827
828 void OBJECT_OT_constraints_clear(wmOperatorType *ot)
829 {
830         /* identifiers */
831         ot->name = "Clear Constraints";
832         ot->idname= "OBJECT_OT_constraints_clear";
833         ot->description= "Clear all the constraints for the active Object only";
834         
835         /* callbacks */
836         ot->exec= object_constraints_clear_exec;
837         ot->poll= ED_operator_object_active_editable;
838 }
839
840 /************************ add constraint operators *********************/
841
842 /* get the Object and/or PoseChannel to use as target */
843 static short get_new_constraint_target(bContext *C, int con_type, Object **tar_ob, bPoseChannel **tar_pchan, short add)
844 {
845         Object *obact= CTX_data_active_object(C);
846         bPoseChannel *pchanact= get_active_posechannel(obact);
847         short only_curve= 0, only_mesh= 0, only_ob= 0;
848         short found= 0;
849         
850         /* clear tar_ob and tar_pchan fields before use 
851          *      - assume for now that both always exist...
852          */
853         *tar_ob= NULL;
854         *tar_pchan= NULL;
855         
856         /* check if constraint type doesn't requires a target
857          *      - if so, no need to get any targets 
858          */
859         switch (con_type) {
860                 /* no-target constraints --------------------------- */
861                         /* null constraint - shouldn't even be added! */
862                 case CONSTRAINT_TYPE_NULL:
863                         /* limit constraints - no targets needed */
864                 case CONSTRAINT_TYPE_LOCLIMIT:
865                 case CONSTRAINT_TYPE_ROTLIMIT:
866                 case CONSTRAINT_TYPE_SIZELIMIT:
867                         return 0;
868                         
869                 /* restricted target-type constraints -------------- */
870                 /* NOTE: for these, we cannot try to add a target object if no valid ones are found, since that doesn't work */
871                         /* curve-based constraints - set the only_curve and only_ob flags */
872                 case CONSTRAINT_TYPE_CLAMPTO:
873                 case CONSTRAINT_TYPE_FOLLOWPATH:
874                 case CONSTRAINT_TYPE_SPLINEIK:
875                         only_curve= 1;
876                         only_ob= 1;
877                         add= 0;
878                         break;
879                         
880                         /* mesh only? */
881                 case CONSTRAINT_TYPE_SHRINKWRAP:
882                         only_mesh= 1;
883                         only_ob= 1;
884                         add= 0;
885                         break;
886                         
887                         /* object only - add here is ok? */
888                 case CONSTRAINT_TYPE_RIGIDBODYJOINT:
889                         only_ob= 1;
890                         break;
891         }
892         
893         /* if the active Object is Armature, and we can search for bones, do so... */
894         if ((obact->type == OB_ARMATURE) && (only_ob == 0)) {
895                 /* search in list of selected Pose-Channels for target */
896                 CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pose_bones) 
897                 {
898                         /* just use the first one that we encounter, as long as it is not the active one */
899                         if (pchan != pchanact) {
900                                 *tar_ob= obact;
901                                 *tar_pchan= pchan;
902                                 found= 1;
903                                 
904                                 break;
905                         }
906                 }
907                 CTX_DATA_END;
908         }
909         
910         /* if not yet found, try selected Objects... */
911         if (found == 0) {
912                 /* search in selected objects context */
913                 CTX_DATA_BEGIN(C, Object*, ob, selected_objects) 
914                 {
915                         /* just use the first object we encounter (that isn't the active object) 
916                          * and which fulfills the criteria for the object-target that we've got 
917                          */
918                         if ( (ob != obact) &&
919                                  ((!only_curve) || (ob->type == OB_CURVE)) && 
920                                  ((!only_mesh) || (ob->type == OB_MESH)) )
921                         {
922                                 /* set target */
923                                 *tar_ob= ob;
924                                 found= 1;
925                                 
926                                 /* perform some special operations on the target */
927                                 if (only_curve) {
928                                         /* Curve-Path option must be enabled for follow-path constraints to be able to work */
929                                         Curve *cu= (Curve *)ob->data;
930                                         cu->flag |= CU_PATH;
931                                 }
932                                 
933                                 break;
934                         }
935                 }
936                 CTX_DATA_END;
937         }
938         
939         /* if still not found, add a new empty to act as a target (if allowed) */
940         if ((found == 0) && (add)) {
941                 Scene *scene= CTX_data_scene(C);
942                 Base *base= BASACT, *newbase=NULL;
943                 Object *obt;
944                 
945                 /* add new target object */
946                 obt= add_object(scene, OB_EMPTY);
947                 
948                 /* set layers OK */
949                 newbase= BASACT;
950                 newbase->lay= base->lay;
951                 obt->lay= newbase->lay;
952                 
953                 /* transform cent to global coords for loc */
954                 if (pchanact) {
955                         /* since by default, IK targets the tip of the last bone, use the tip of the active PoseChannel 
956                          * if adding a target for an IK Constraint
957                          */
958                         if (con_type == CONSTRAINT_TYPE_KINEMATIC)
959                                 mul_v3_m4v3(obt->loc, obact->obmat, pchanact->pose_tail);
960                         else
961                                 mul_v3_m4v3(obt->loc, obact->obmat, pchanact->pose_head);
962                 }
963                 else
964                         VECCOPY(obt->loc, obact->obmat[3]);
965                 
966                 /* restore, add_object sets active */
967                 BASACT= base;
968                 base->flag |= SELECT;
969                 
970                 /* make our new target the new object */
971                 *tar_ob= obt;
972                 found= 1;
973         }
974         
975         /* return whether there's any target */
976         return found;
977 }
978
979 /* used by add constraint operators to add the constraint required */
980 static int constraint_add_exec(bContext *C, wmOperator *op, Object *ob, ListBase *list, int type, short setTarget)
981 {
982         Scene *scene= CTX_data_scene(C);
983         bPoseChannel *pchan;
984         bConstraint *con;
985         
986         if(list == &ob->constraints)
987                 pchan= NULL;
988         else
989                 pchan= get_active_posechannel(ob);
990
991         /* check if constraint to be added is valid for the given constraints stack */
992         if (type == CONSTRAINT_TYPE_NULL) {
993                 return OPERATOR_CANCELLED;
994         }
995         if ( (type == CONSTRAINT_TYPE_RIGIDBODYJOINT) && (list != &ob->constraints) ) {
996                 BKE_report(op->reports, RPT_ERROR, "Rigid Body Joint Constraint can only be added to Objects.");
997                 return OPERATOR_CANCELLED;
998         }
999         if ( (type == CONSTRAINT_TYPE_KINEMATIC) && ((!pchan) || (list != &pchan->constraints)) ) {
1000                 BKE_report(op->reports, RPT_ERROR, "IK Constraint can only be added to Bones.");
1001                 return OPERATOR_CANCELLED;
1002         }
1003         if ( (type == CONSTRAINT_TYPE_SPLINEIK) && ((!pchan) || (list != &pchan->constraints)) ) {
1004                 BKE_report(op->reports, RPT_ERROR, "Spline IK Constraint can only be added to Bones.");
1005                 return OPERATOR_CANCELLED;
1006         }
1007         
1008         /* create a new constraint of the type requried, and add it to the active/given constraints list */
1009         if(pchan)
1010                 con = add_pose_constraint(ob, pchan, NULL, type);
1011         else
1012                 con = add_ob_constraint(ob, NULL, type);
1013         
1014         /* get the first selected object/bone, and make that the target
1015          *      - apart from the buttons-window add buttons, we shouldn't add in this way
1016          */
1017         if (setTarget) {
1018                 Object *tar_ob= NULL;
1019                 bPoseChannel *tar_pchan= NULL;
1020                 
1021                 /* get the target objects, adding them as need be */
1022                 if (get_new_constraint_target(C, type, &tar_ob, &tar_pchan, 1)) {
1023                         /* method of setting target depends on the type of target we've got 
1024                          *      - by default, just set the first target (distinction here is only for multiple-targetted constraints)
1025                          */
1026                         if (tar_pchan)
1027                                 set_constraint_nth_target(con, tar_ob, tar_pchan->name, 0);
1028                         else
1029                                 set_constraint_nth_target(con, tar_ob, "", 0);
1030                 }
1031         }
1032         
1033         /* do type-specific tweaking to the constraint settings  */
1034         // TODO: does action constraint need anything here - i.e. spaceonce?
1035         switch (type) {
1036                 case CONSTRAINT_TYPE_CHILDOF:
1037                 {
1038                         /* if this constraint is being added to a posechannel, make sure
1039                          * the constraint gets evaluated in pose-space */
1040                         if (ob->mode & OB_MODE_POSE) {
1041                                 con->ownspace = CONSTRAINT_SPACE_POSE;
1042                                 con->flag |= CONSTRAINT_SPACEONCE;
1043                         }
1044                 }
1045                         break;
1046                         
1047                 case CONSTRAINT_TYPE_PYTHON: // FIXME: this code is not really valid anymore
1048                 {
1049                         char *menustr;
1050                         int scriptint= 0;
1051 #ifndef DISABLE_PYTHON
1052                         /* popup a list of usable scripts */
1053                         menustr = buildmenu_pyconstraints(NULL, &scriptint);
1054                         // XXX scriptint = pupmenu(menustr);
1055                         MEM_freeN(menustr);
1056                         
1057                         /* only add constraint if a script was chosen */
1058                         if (scriptint) {
1059                                 /* add constraint */
1060                                 validate_pyconstraint_cb(con->data, &scriptint);
1061                                 
1062                                 /* make sure target allowance is set correctly */
1063                                 BPY_pyconstraint_update(ob, con);
1064                         }
1065 #endif
1066                 }
1067                 default:
1068                         break;
1069         }
1070         
1071         /* make sure all settings are valid - similar to above checks, but sometimes can be wrong */
1072         object_test_constraints(ob);
1073         
1074         if (ob->pose)
1075                 update_pose_constraint_flags(ob->pose);
1076         
1077         
1078         /* force depsgraph to get recalculated since new relationships added */
1079         DAG_scene_sort(scene);          /* sort order of objects */
1080         
1081         if ((ob->type==OB_ARMATURE) && (pchan)) {
1082                 ob->pose->flag |= POSE_RECALC;  /* sort pose channels */
1083                 DAG_id_flush_update(&ob->id, OB_RECALC_DATA|OB_RECALC_OB);
1084         }
1085         else
1086                 DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
1087         
1088         /* notifiers for updates */
1089         WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT|NA_ADDED, ob);
1090         
1091         return OPERATOR_FINISHED;
1092 }
1093
1094 /* ------------------ */
1095
1096 /* dummy operator callback */
1097 static int object_constraint_add_exec(bContext *C, wmOperator *op)
1098 {
1099         ScrArea *sa= CTX_wm_area(C);
1100         Object *ob;
1101         int type= RNA_enum_get(op->ptr, "type");
1102         short with_targets= 0;
1103         
1104         /* get active object from context */
1105         if (sa->spacetype == SPACE_BUTS)
1106                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1107         else
1108                 ob= CTX_data_active_object(C);
1109         
1110         if (!ob) {
1111                 BKE_report(op->reports, RPT_ERROR, "No active object to add constraint to.");
1112                 return OPERATOR_CANCELLED;
1113         }
1114                 
1115         /* hack: set constraint targets from selected objects in context is allowed when
1116          *              operator name included 'with_targets', since the menu doesn't allow multiple properties
1117          */
1118         if (strstr(op->idname, "with_targets"))
1119                 with_targets= 1;
1120
1121         return constraint_add_exec(C, op, ob, &ob->constraints, type, with_targets);
1122 }
1123
1124 /* dummy operator callback */
1125 static int pose_constraint_add_exec(bContext *C, wmOperator *op)
1126 {
1127         ScrArea *sa= CTX_wm_area(C);
1128         Object *ob;
1129         int type= RNA_enum_get(op->ptr, "type");
1130         short with_targets= 0;
1131         
1132         /* get active object from context */
1133         if (sa->spacetype == SPACE_BUTS)
1134                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1135         else
1136                 ob= CTX_data_active_object(C);
1137         
1138         if (!ob) {
1139                 BKE_report(op->reports, RPT_ERROR, "No active object to add constraint to.");
1140                 return OPERATOR_CANCELLED;
1141         }
1142                 
1143         /* hack: set constraint targets from selected objects in context is allowed when
1144          *              operator name included 'with_targets', since the menu doesn't allow multiple properties
1145          */
1146         if (strstr(op->idname, "with_targets"))
1147                 with_targets= 1;
1148         
1149         return constraint_add_exec(C, op, ob, get_active_constraints(ob), type, with_targets);
1150 }
1151
1152 /* ------------------ */
1153
1154 void OBJECT_OT_constraint_add(wmOperatorType *ot)
1155 {
1156         /* identifiers */
1157         ot->name= "Add Constraint";
1158         ot->description = "Add a constraint to the active object";
1159         ot->idname= "OBJECT_OT_constraint_add";
1160         
1161         /* api callbacks */
1162         ot->invoke= WM_menu_invoke;
1163         ot->exec= object_constraint_add_exec;
1164         ot->poll= ED_operator_object_active_editable;
1165         
1166         /* flags */
1167         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1168         
1169         /* properties */
1170         ot->prop= RNA_def_enum(ot->srna, "type", constraint_type_items, 0, "Type", "");
1171 }
1172
1173 void OBJECT_OT_constraint_add_with_targets(wmOperatorType *ot)
1174 {
1175         /* identifiers */
1176         ot->name= "Add Constraint (with Targets)";
1177         ot->description = "Add a constraint to the active object, with target (where applicable) set to the selected Objects/Bones";
1178         ot->idname= "OBJECT_OT_constraint_add_with_targets";
1179         
1180         /* api callbacks */
1181         ot->invoke= WM_menu_invoke;
1182         ot->exec= object_constraint_add_exec;
1183         ot->poll= ED_operator_object_active_editable;
1184         
1185         /* flags */
1186         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1187         
1188         /* properties */
1189         ot->prop= RNA_def_enum(ot->srna, "type", constraint_type_items, 0, "Type", "");
1190 }
1191
1192 void POSE_OT_constraint_add(wmOperatorType *ot)
1193 {
1194         /* identifiers */
1195         ot->name= "Add Constraint";
1196         ot->description = "Add a constraint to the active bone";
1197         ot->idname= "POSE_OT_constraint_add";
1198         
1199         /* api callbacks */
1200         ot->invoke= WM_menu_invoke;
1201         ot->exec= pose_constraint_add_exec;
1202         ot->poll= ED_operator_posemode;
1203         
1204         /* flags */
1205         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1206         
1207         /* properties */
1208         ot->prop= RNA_def_enum(ot->srna, "type", constraint_type_items, 0, "Type", "");
1209 }
1210
1211 void POSE_OT_constraint_add_with_targets(wmOperatorType *ot)
1212 {
1213         /* identifiers */
1214         ot->name= "Add Constraint (with Targets)";
1215         ot->description = "Add a constraint to the active bone, with target (where applicable) set to the selected Objects/Bones";
1216         ot->idname= "POSE_OT_constraint_add_with_targets";
1217         
1218         /* api callbacks */
1219         ot->invoke= WM_menu_invoke;
1220         ot->exec= pose_constraint_add_exec;
1221         ot->poll= ED_operator_posemode;
1222         
1223         /* flags */
1224         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1225         
1226         /* properties */
1227         ot->prop= RNA_def_enum(ot->srna, "type", constraint_type_items, 0, "Type", "");
1228 }
1229
1230 /************************ IK Constraint operators *********************/
1231 /* NOTE: only for Pose-Channels */
1232 // TODO: should these be here, or back in editors/armature/poseobject.c again?
1233
1234 /* present menu with options + validation for targets to use */
1235 static int pose_ik_add_invoke(bContext *C, wmOperator *op, wmEvent *evt)
1236 {
1237         Object *ob= CTX_data_active_object(C);
1238         bPoseChannel *pchan= get_active_posechannel(ob);
1239         bConstraint *con= NULL;
1240         
1241         uiPopupMenu *pup;
1242         uiLayout *layout;
1243         Object *tar_ob= NULL;
1244         bPoseChannel *tar_pchan= NULL;
1245         
1246         /* must have active bone */
1247         if (ELEM(NULL, ob, pchan)) {
1248                 BKE_report(op->reports, RPT_ERROR, "Must have active bone to add IK Constraint to.");
1249                 return OPERATOR_CANCELLED;
1250         }
1251         
1252         /* bone must not have any constraints already */
1253         for (con= pchan->constraints.first; con; con= con->next) {
1254                 if (con->type==CONSTRAINT_TYPE_KINEMATIC) break;
1255         }
1256         if (con) {
1257                 BKE_report(op->reports, RPT_ERROR, "Bone already has IK Constraint.");
1258                 return OPERATOR_CANCELLED;
1259         }
1260         
1261         /* prepare popup menu to choose targetting options */
1262         pup= uiPupMenuBegin(C, "Add IK", 0);
1263         layout= uiPupMenuLayout(pup);
1264         
1265         /* the type of targets we'll set determines the menu entries to show... */
1266         if (get_new_constraint_target(C, CONSTRAINT_TYPE_KINEMATIC, &tar_ob, &tar_pchan, 0)) {
1267                 /* bone target, or object target? 
1268                  *      - the only thing that matters is that we want a target...
1269                  */
1270                 if (tar_pchan)
1271                         uiItemBooleanO(layout, "To Active Bone", 0, "POSE_OT_ik_add", "with_targets", 1);
1272                 else
1273                         uiItemBooleanO(layout, "To Active Object", 0, "POSE_OT_ik_add", "with_targets", 1);
1274         }
1275         else {
1276                 /* we have a choice of adding to a new empty, or not setting any target (targetless IK) */
1277                 uiItemBooleanO(layout, "To New Empty Object", 0, "POSE_OT_ik_add", "with_targets", 1);
1278                 uiItemBooleanO(layout, "Without Targets", 0, "POSE_OT_ik_add", "with_targets", 0);
1279         }
1280         
1281         /* finish building the menu, and process it (should result in calling self again) */
1282         uiPupMenuEnd(C, pup);
1283         
1284         return OPERATOR_CANCELLED;
1285 }
1286
1287 /* call constraint_add_exec() to add the IK constraint */
1288 static int pose_ik_add_exec(bContext *C, wmOperator *op)
1289 {
1290         Object *ob= CTX_data_active_object(C);
1291         int with_targets= RNA_boolean_get(op->ptr, "with_targets");
1292         
1293         /* add the constraint - all necessary checks should have been done by the invoke() callback already... */
1294         return constraint_add_exec(C, op, ob, get_active_constraints(ob), CONSTRAINT_TYPE_KINEMATIC, with_targets);
1295 }
1296
1297 void POSE_OT_ik_add(wmOperatorType *ot)
1298 {
1299         /* identifiers */
1300         ot->name= "Add IK to Bone";
1301         ot->description= "Add IK Constraint to the active Bone";
1302         ot->idname= "POSE_OT_ik_add";
1303         
1304         /* api callbacks */
1305         ot->invoke= pose_ik_add_invoke;
1306         ot->exec= pose_ik_add_exec;
1307         ot->poll= ED_operator_posemode;
1308         
1309         /* flags */
1310         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1311         
1312         /* properties */
1313         RNA_def_boolean(ot->srna, "with_targets", 1, "With Targets", "Assign IK Constraint with targets derived from the select bones/objects");
1314 }
1315
1316 /* ------------------ */
1317
1318 /* remove IK constraints from selected bones */
1319 static int pose_ik_clear_exec(bContext *C, wmOperator *op)
1320 {
1321         Object *ob= CTX_data_active_object(C);
1322         
1323         /* only remove IK Constraints */
1324         CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pose_bones) 
1325         {
1326                 bConstraint *con, *next;
1327                 
1328                 // TODO: should we be checking if these contraints were local before we try and remove them?
1329                 for (con= pchan->constraints.first; con; con= next) {
1330                         next= con->next;
1331                         if (con->type==CONSTRAINT_TYPE_KINEMATIC) {
1332                                 remove_constraint(&pchan->constraints, con);
1333                         }
1334                 }
1335                 pchan->constflag &= ~(PCHAN_HAS_IK|PCHAN_HAS_TARGET);
1336         }
1337         CTX_DATA_END;
1338         
1339         /* refresh depsgraph */
1340         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
1341
1342         /* note, notifier might evolve */
1343         WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT|NA_REMOVED, ob);
1344         
1345         return OPERATOR_FINISHED;
1346 }
1347
1348 void POSE_OT_ik_clear(wmOperatorType *ot)
1349 {
1350         /* identifiers */
1351         ot->name= "Remove IK";
1352         ot->description= "Remove all IK Constraints from selected bones";
1353         ot->idname= "POSE_OT_ik_clear";
1354         
1355         /* api callbacks */
1356         ot->exec= pose_ik_clear_exec;
1357         ot->poll= ED_operator_posemode;
1358         
1359         /* flags */
1360         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1361 }
1362