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