clear 2 warnings
[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
434 /************************ generic functions for operators using constraint names and data context *********************/
435
436 #define EDIT_CONSTRAINT_OWNER_OBJECT    0
437 #define EDIT_CONSTRAINT_OWNER_BONE              1
438
439 static EnumPropertyItem constraint_owner_items[] = {
440 {EDIT_CONSTRAINT_OWNER_OBJECT, "OBJECT", 0, "Object", "Edit a constraint on the active object"},
441 {EDIT_CONSTRAINT_OWNER_BONE, "BONE", 0, "Bone", "Edit a constraint on the active bone"},
442 {0, NULL, 0, NULL, NULL}};
443
444
445 static int edit_constraint_poll_generic(bContext *C, StructRNA *rna_type)
446 {
447         PointerRNA ptr= CTX_data_pointer_get_type(C, "constraint", rna_type);
448         Object *ob= (ptr.id.data)?ptr.id.data:ED_object_active_context(C);
449         
450         if (!ob || ob->id.lib) return 0;
451         if (ptr.data && ((ID*)ptr.id.data)->lib) return 0;
452         
453         return 1;
454 }
455
456 static int edit_constraint_poll(bContext *C)
457 {
458         return edit_constraint_poll_generic(C, &RNA_Constraint);
459 }
460
461 static void edit_constraint_properties(wmOperatorType *ot)
462 {
463         RNA_def_string(ot->srna, "constraint", "", 32, "Constraint", "Name of the constraint to edit");
464         RNA_def_enum(ot->srna, "owner", constraint_owner_items, 0, "Owner", "The owner of this constraint");
465 }
466
467 static int edit_constraint_invoke_properties(bContext *C, wmOperator *op)
468 {
469         PointerRNA ptr= CTX_data_pointer_get_type(C, "constraint", &RNA_Constraint);
470         Object *ob= (ptr.id.data)?ptr.id.data:ED_object_active_context(C);
471         bConstraint *con;
472         ListBase *list;
473         
474         if (RNA_property_is_set(op->ptr, "constraint") && RNA_property_is_set(op->ptr, "owner"))
475                 return 1;
476         
477         if (ptr.data) {
478                 con = ptr.data;
479                 RNA_string_set(op->ptr, "constraint", con->name);
480                 
481                 list = get_constraint_lb(ob, con, NULL);
482                 
483                 if (&ob->constraints == list)
484                         RNA_enum_set(op->ptr, "owner", EDIT_CONSTRAINT_OWNER_OBJECT);
485                 else
486                         RNA_enum_set(op->ptr, "owner", EDIT_CONSTRAINT_OWNER_BONE);
487                 
488                 return 1;
489         }
490         
491         return 0;
492 }
493
494 static bConstraint *edit_constraint_property_get(bContext *C, wmOperator *op, Object *ob, int type)
495 {
496         char constraint_name[32];
497         int owner = RNA_enum_get(op->ptr, "owner");
498         bConstraint *con;
499         ListBase *list;
500         
501         RNA_string_get(op->ptr, "constraint", constraint_name);
502         
503         if (owner == EDIT_CONSTRAINT_OWNER_OBJECT) {
504                 list = &ob->constraints;
505         } else if (owner == EDIT_CONSTRAINT_OWNER_BONE) {
506                 bPoseChannel *pchan= get_active_posechannel(ob);
507                 if (pchan)
508                         list = &pchan->constraints;
509                 else
510                         return NULL;
511         }
512         
513         con = constraints_findByName(list, constraint_name);
514         
515         if (con && type != 0 && con->type != type)
516                 con = NULL;
517         
518         return con;
519 }
520
521 /* ********************** CONSTRAINT-SPECIFIC STUFF ********************* */
522
523 /* ---------- Distance-Dependent Constraints ---------- */
524 /* StretchTo, Limit Distance */
525
526 static int stretchto_reset_exec (bContext *C, wmOperator *op)
527 {
528         Object *ob = ED_object_active_context(C);
529         bConstraint *con = edit_constraint_property_get(C, op, ob, CONSTRAINT_TYPE_STRETCHTO);
530         bStretchToConstraint *data= (bStretchToConstraint *)con->data;
531
532         /* just set original length to 0.0, which will cause a reset on next recalc */
533         data->orglength = 0.0f;
534         ED_object_constraint_update(ob);
535         
536         WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, NULL);
537         return OPERATOR_FINISHED;
538 }
539
540 static int stretchto_reset_invoke(bContext *C, wmOperator *op, wmEvent *event)
541 {
542         if (edit_constraint_invoke_properties(C, op))
543                 return stretchto_reset_exec(C, op);
544         else
545                 return OPERATOR_CANCELLED;
546 }
547
548 void CONSTRAINT_OT_stretchto_reset (wmOperatorType *ot)
549 {
550         /* identifiers */
551         ot->name= "Reset Original Length";
552         ot->idname= "CONSTRAINT_OT_stretchto_reset";
553         ot->description= "Reset original length of bone for Stretch To Constraint";
554         
555         ot->exec= stretchto_reset_exec;
556         ot->invoke= stretchto_reset_invoke;
557         ot->poll= edit_constraint_poll;
558         
559         /* flags */
560         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
561         edit_constraint_properties(ot);
562 }
563
564
565 static int limitdistance_reset_exec (bContext *C, wmOperator *op)
566 {
567         Object *ob = ED_object_active_context(C);
568         bConstraint *con = edit_constraint_property_get(C, op, ob, CONSTRAINT_TYPE_DISTLIMIT);
569         bDistLimitConstraint *data= (bDistLimitConstraint *)con->data;
570         
571         /* just set original length to 0.0, which will cause a reset on next recalc */
572         data->dist = 0.0f;
573         ED_object_constraint_update(ob);
574         
575         WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, NULL);
576         return OPERATOR_FINISHED;
577 }
578
579 static int limitdistance_reset_invoke(bContext *C, wmOperator *op, wmEvent *event)
580 {
581         if (edit_constraint_invoke_properties(C, op))
582                 return limitdistance_reset_exec(C, op);
583         else
584                 return OPERATOR_CANCELLED;
585 }
586
587 void CONSTRAINT_OT_limitdistance_reset (wmOperatorType *ot)
588 {
589         /* identifiers */
590         ot->name= "Reset Distance";
591         ot->idname= "CONSTRAINT_OT_limitdistance_reset";
592         ot->description= "Reset limiting distance for Limit Distance Constraint";
593         
594         ot->exec= limitdistance_reset_exec;
595         ot->invoke= limitdistance_reset_invoke;
596         ot->poll= edit_constraint_poll;
597         
598         /* flags */
599         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
600         edit_constraint_properties(ot);
601 }
602
603 /* ------------- Child-Of Constraint ------------------ */
604 #if 0 // unused
605 static int childof_poll(bContext *C)
606 {
607         PointerRNA ptr= CTX_data_pointer_get_type(C, "constraint", &RNA_ChildOfConstraint);
608         return (ptr.id.data && ptr.data);
609 }
610 #endif
611 /* ChildOf Constraint - set inverse callback */
612 static int childof_set_inverse_exec (bContext *C, wmOperator *op)
613 {
614         Scene *scene= CTX_data_scene(C);
615         Object *ob = ED_object_active_context(C);
616         bConstraint *con = edit_constraint_property_get(C, op, ob, CONSTRAINT_TYPE_CHILDOF);
617         bChildOfConstraint *data= (bChildOfConstraint *)con->data;
618         bPoseChannel *pchan= NULL;
619
620         /* try to find a pose channel */
621         // TODO: get from context instead?
622         if (ob && ob->pose)
623                 pchan= get_active_posechannel(ob);
624         
625         /* calculate/set inverse matrix */
626         if (pchan) {
627                 float pmat[4][4], cinf;
628                 float imat[4][4], tmat[4][4];
629                 
630                 /* make copy of pchan's original pose-mat (for use later) */
631                 copy_m4_m4(pmat, pchan->pose_mat);
632                 
633                 /* disable constraint for pose to be solved without it */
634                 cinf= con->enforce;
635                 con->enforce= 0.0f;
636                 
637                 /* solve pose without constraint */
638                 where_is_pose(scene, ob);
639                 
640                 /* determine effect of constraint by removing the newly calculated 
641                  * pchan->pose_mat from the original pchan->pose_mat, thus determining 
642                  * the effect of the constraint
643                  */
644                 invert_m4_m4(imat, pchan->pose_mat);
645                 mul_m4_m4m4(tmat, imat, pmat);
646                 invert_m4_m4(data->invmat, tmat);
647                 
648                 /* recalculate pose with new inv-mat */
649                 con->enforce= cinf;
650                 where_is_pose(scene, ob);
651         }
652         else if (ob) {
653                 Object workob;
654                 /* use what_does_parent to find inverse - just like for normal parenting.
655                  * NOTE: what_does_parent uses a static workob defined in object.c 
656                  */
657                 what_does_parent(scene, ob, &workob);
658                 invert_m4_m4(data->invmat, workob.obmat);
659         }
660         else
661                 unit_m4(data->invmat);
662                 
663         WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, ob);
664                 
665         return OPERATOR_FINISHED;
666 }
667
668 static int childof_set_inverse_invoke(bContext *C, wmOperator *op, wmEvent *event)
669 {
670         if (edit_constraint_invoke_properties(C, op))
671                 return childof_set_inverse_exec(C, op);
672         else
673                 return OPERATOR_CANCELLED;
674 }
675
676 void CONSTRAINT_OT_childof_set_inverse (wmOperatorType *ot)
677 {
678         /* identifiers */
679         ot->name= "Set Inverse";
680         ot->idname= "CONSTRAINT_OT_childof_set_inverse";
681         ot->description= "Set inverse correction for ChildOf constraint";
682         
683         ot->exec= childof_set_inverse_exec;
684         ot->invoke= childof_set_inverse_invoke;
685         ot->poll= edit_constraint_poll;
686         
687         /* flags */
688         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
689         edit_constraint_properties(ot);
690 }
691
692 /* ChildOf Constraint - clear inverse callback */
693 static int childof_clear_inverse_exec (bContext *C, wmOperator *op)
694 {
695         Object *ob = ED_object_active_context(C);
696         bConstraint *con = edit_constraint_property_get(C, op, ob, CONSTRAINT_TYPE_CHILDOF);
697         bChildOfConstraint *data= (bChildOfConstraint *)con->data;
698         
699         /* simply clear the matrix */
700         unit_m4(data->invmat);
701         
702         WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, ob);
703         
704         return OPERATOR_FINISHED;
705 }
706
707 static int childof_clear_inverse_invoke(bContext *C, wmOperator *op, wmEvent *event)
708 {
709         if (edit_constraint_invoke_properties(C, op))
710                 return childof_clear_inverse_exec(C, op);
711         else
712                 return OPERATOR_CANCELLED;
713 }
714
715 void CONSTRAINT_OT_childof_clear_inverse (wmOperatorType *ot)
716 {
717         /* identifiers */
718         ot->name= "Clear Inverse";
719         ot->idname= "CONSTRAINT_OT_childof_clear_inverse";
720         ot->description= "Clear inverse correction for ChildOf constraint";
721         
722         ot->exec= childof_clear_inverse_exec;
723         ot->invoke= childof_clear_inverse_invoke;
724         ot->poll= edit_constraint_poll;
725         
726         /* flags */
727         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
728         edit_constraint_properties(ot);
729 }
730
731 /***************************** BUTTONS ****************************/
732
733 void ED_object_constraint_set_active(Object *ob, bConstraint *con)
734 {       
735         ListBase *lb = get_constraint_lb(ob, con, NULL);
736         
737         /* lets be nice and escape if its active already */
738         // NOTE: this assumes that the stack doesn't have other active ones set...
739         if ((lb && con) && (con->flag & CONSTRAINT_ACTIVE))
740                 return;
741         
742         constraints_set_active(lb, con);
743 }
744
745 void ED_object_constraint_update(Object *ob)
746 {
747
748         if(ob->pose) update_pose_constraint_flags(ob->pose);
749
750         object_test_constraints(ob);
751
752         if(ob->type==OB_ARMATURE) DAG_id_flush_update(&ob->id, OB_RECALC_DATA|OB_RECALC_OB);
753         else DAG_id_flush_update(&ob->id, OB_RECALC_OB);
754 }
755
756 void ED_object_constraint_dependency_update(Scene *scene, Object *ob)
757 {
758         ED_object_constraint_update(ob);
759
760         if(ob->pose) ob->pose->flag |= POSE_RECALC;     // checks & sorts pose channels
761         DAG_scene_sort(scene);
762 }
763
764 static int constraint_poll(bContext *C)
765 {
766         PointerRNA ptr= CTX_data_pointer_get_type(C, "constraint", &RNA_Constraint);
767         return (ptr.id.data && ptr.data);
768 }
769
770 static int constraint_delete_exec (bContext *C, wmOperator *op)
771 {
772         PointerRNA ptr= CTX_data_pointer_get_type(C, "constraint", &RNA_Constraint);
773         Object *ob= ptr.id.data;
774         bConstraint *con= ptr.data;
775         ListBase *lb = get_constraint_lb(ob, con, NULL);
776         
777         /* free the constraint */
778         if (remove_constraint(lb, con)) {
779                 /* there's no active constraint now, so make sure this is the case */
780                 constraints_set_active(lb, NULL);
781                 
782                 /* notifiers */
783                 WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, ob);
784                 
785                 return OPERATOR_FINISHED;
786         }
787         else {
788                 /* couldn't remove due to some invalid data */
789                 return OPERATOR_CANCELLED;
790         }
791 }
792
793 void CONSTRAINT_OT_delete (wmOperatorType *ot)
794 {
795         /* identifiers */
796         ot->name= "Delete Constraint";
797         ot->idname= "CONSTRAINT_OT_delete";
798         ot->description= "Remove constraint from constraint stack";
799         
800         /* callbacks */
801         ot->exec= constraint_delete_exec;
802         ot->poll= constraint_poll;
803         
804         /* flags */
805         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; 
806 }
807
808 static int constraint_move_down_exec (bContext *C, wmOperator *op)
809 {
810         Object *ob = ED_object_active_context(C);
811         bConstraint *con = edit_constraint_property_get(C, op, ob, 0);
812         
813         if (con && con->next) {
814                 ListBase *conlist= get_constraint_lb(ob, con, NULL);
815                 bConstraint *nextCon= con->next;
816                 
817                 /* insert the nominated constraint after the one that used to be after it */
818                 BLI_remlink(conlist, con);
819                 BLI_insertlinkafter(conlist, nextCon, con);
820                 
821                 WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, ob);
822                 
823                 return OPERATOR_FINISHED;
824         }
825         
826         return OPERATOR_CANCELLED;
827 }
828
829 static int constraint_move_down_invoke(bContext *C, wmOperator *op, wmEvent *event)
830 {
831         if (edit_constraint_invoke_properties(C, op))
832                 return constraint_move_down_exec(C, op);
833         else
834                 return OPERATOR_CANCELLED;
835 }
836
837
838 void CONSTRAINT_OT_move_down (wmOperatorType *ot)
839 {
840         /* identifiers */
841         ot->name= "Move Constraint Down";
842         ot->idname= "CONSTRAINT_OT_move_down";
843         ot->description= "Move constraint down constraint stack";
844         
845         /* callbacks */
846         ot->exec= constraint_move_down_exec;
847         ot->invoke= constraint_move_down_invoke;
848         ot->poll= edit_constraint_poll;
849         
850         /* flags */
851         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; 
852         edit_constraint_properties(ot);
853 }
854
855
856 static int constraint_move_up_exec (bContext *C, wmOperator *op)
857 {
858         Object *ob = ED_object_active_context(C);
859         bConstraint *con = edit_constraint_property_get(C, op, ob, 0);
860         
861         if (con && con->prev) {
862                 ListBase *conlist= get_constraint_lb(ob, con, NULL);
863                 bConstraint *prevCon= con->prev;
864                 
865                 /* insert the nominated constraint before the one that used to be before it */
866                 BLI_remlink(conlist, con);
867                 BLI_insertlinkbefore(conlist, prevCon, con);
868                 
869                 WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, ob);
870                 
871                 return OPERATOR_FINISHED;
872         }
873         
874         return OPERATOR_CANCELLED;
875 }
876
877 static int constraint_move_up_invoke(bContext *C, wmOperator *op, wmEvent *event)
878 {
879         if (edit_constraint_invoke_properties(C, op))
880                 return constraint_move_up_exec(C, op);
881         else
882                 return OPERATOR_CANCELLED;
883 }
884
885 void CONSTRAINT_OT_move_up (wmOperatorType *ot)
886 {
887         /* identifiers */
888         ot->name= "Move Constraint Up";
889         ot->idname= "CONSTRAINT_OT_move_up";
890         ot->description= "Move constraint up constraint stack";
891         
892         /* callbacks */
893         ot->exec= constraint_move_up_exec;
894         ot->invoke= constraint_move_up_invoke;
895         ot->poll= edit_constraint_poll;
896         
897         /* flags */
898         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO; 
899         edit_constraint_properties(ot);
900 }
901
902 /***************************** OPERATORS ****************************/
903
904 /************************ remove constraint operators *********************/
905
906 static int pose_constraints_clear_exec(bContext *C, wmOperator *op)
907 {
908         Object *ob= CTX_data_active_object(C);
909         Scene *scene= CTX_data_scene(C);
910         
911         /* free constraints for all selected bones */
912         CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pose_bones)
913         {
914                 free_constraints(&pchan->constraints);
915                 pchan->constflag &= ~(PCHAN_HAS_IK|PCHAN_HAS_SPLINEIK|PCHAN_HAS_CONST);
916         }
917         CTX_DATA_END;
918         
919         /* force depsgraph to get recalculated since relationships removed */
920         DAG_scene_sort(scene);          /* sort order of objects */     
921         
922         /* do updates */
923         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
924         WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, ob);
925         
926         return OPERATOR_FINISHED;
927 }
928
929 void POSE_OT_constraints_clear(wmOperatorType *ot)
930 {
931         /* identifiers */
932         ot->name = "Clear Constraints";
933         ot->idname= "POSE_OT_constraints_clear";
934         ot->description= "Clear all the constraints for the selected bones";
935         
936         /* callbacks */
937         ot->exec= pose_constraints_clear_exec;
938         ot->poll= ED_operator_posemode; // XXX - do we want to ensure there are selected bones too?
939 }
940
941
942 static int object_constraints_clear_exec(bContext *C, wmOperator *op)
943 {
944         Object *ob= ED_object_active_context(C);
945         Scene *scene= CTX_data_scene(C);
946         
947         /* do freeing */
948         // TODO: we should free constraints for all selected objects instead (to be more consistent with bones)
949         free_constraints(&ob->constraints);
950         
951         /* force depsgraph to get recalculated since relationships removed */
952         DAG_scene_sort(scene);          /* sort order of objects */     
953         
954         /* do updates */
955         DAG_id_flush_update(&ob->id, OB_RECALC_OB);
956         WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, ob);
957         
958         return OPERATOR_FINISHED;
959 }
960
961 void OBJECT_OT_constraints_clear(wmOperatorType *ot)
962 {
963         /* identifiers */
964         ot->name = "Clear Constraints";
965         ot->idname= "OBJECT_OT_constraints_clear";
966         ot->description= "Clear all the constraints for the active Object only";
967         
968         /* callbacks */
969         ot->exec= object_constraints_clear_exec;
970         ot->poll= ED_operator_object_active_editable;
971 }
972
973 /************************ add constraint operators *********************/
974
975 /* get the Object and/or PoseChannel to use as target */
976 static short get_new_constraint_target(bContext *C, int con_type, Object **tar_ob, bPoseChannel **tar_pchan, short add)
977 {
978         Object *obact= ED_object_active_context(C);
979         bPoseChannel *pchanact= get_active_posechannel(obact);
980         short only_curve= 0, only_mesh= 0, only_ob= 0;
981         short found= 0;
982         
983         /* clear tar_ob and tar_pchan fields before use 
984          *      - assume for now that both always exist...
985          */
986         *tar_ob= NULL;
987         *tar_pchan= NULL;
988         
989         /* check if constraint type doesn't requires a target
990          *      - if so, no need to get any targets 
991          */
992         switch (con_type) {
993                 /* no-target constraints --------------------------- */
994                         /* null constraint - shouldn't even be added! */
995                 case CONSTRAINT_TYPE_NULL:
996                         /* limit constraints - no targets needed */
997                 case CONSTRAINT_TYPE_LOCLIMIT:
998                 case CONSTRAINT_TYPE_ROTLIMIT:
999                 case CONSTRAINT_TYPE_SIZELIMIT:
1000                 case CONSTRAINT_TYPE_SAMEVOL:
1001                         return 0;
1002                         
1003                 /* restricted target-type constraints -------------- */
1004                 /* NOTE: for these, we cannot try to add a target object if no valid ones are found, since that doesn't work */
1005                         /* curve-based constraints - set the only_curve and only_ob flags */
1006                 case CONSTRAINT_TYPE_CLAMPTO:
1007                 case CONSTRAINT_TYPE_FOLLOWPATH:
1008                 case CONSTRAINT_TYPE_SPLINEIK:
1009                         only_curve= 1;
1010                         only_ob= 1;
1011                         add= 0;
1012                         break;
1013                         
1014                         /* mesh only? */
1015                 case CONSTRAINT_TYPE_SHRINKWRAP:
1016                         only_mesh= 1;
1017                         only_ob= 1;
1018                         add= 0;
1019                         break;
1020                         
1021                         /* object only - add here is ok? */
1022                 case CONSTRAINT_TYPE_RIGIDBODYJOINT:
1023                         only_ob= 1;
1024                         break;
1025         }
1026         
1027         /* if the active Object is Armature, and we can search for bones, do so... */
1028         if ((obact->type == OB_ARMATURE) && (only_ob == 0)) {
1029                 /* search in list of selected Pose-Channels for target */
1030                 CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pose_bones) 
1031                 {
1032                         /* just use the first one that we encounter, as long as it is not the active one */
1033                         if (pchan != pchanact) {
1034                                 *tar_ob= obact;
1035                                 *tar_pchan= pchan;
1036                                 found= 1;
1037                                 
1038                                 break;
1039                         }
1040                 }
1041                 CTX_DATA_END;
1042         }
1043         
1044         /* if not yet found, try selected Objects... */
1045         if (found == 0) {
1046                 /* search in selected objects context */
1047                 CTX_DATA_BEGIN(C, Object*, ob, selected_objects) 
1048                 {
1049                         /* just use the first object we encounter (that isn't the active object) 
1050                          * and which fulfills the criteria for the object-target that we've got 
1051                          */
1052                         if ( (ob != obact) &&
1053                                  ((!only_curve) || (ob->type == OB_CURVE)) && 
1054                                  ((!only_mesh) || (ob->type == OB_MESH)) )
1055                         {
1056                                 /* set target */
1057                                 *tar_ob= ob;
1058                                 found= 1;
1059                                 
1060                                 /* perform some special operations on the target */
1061                                 if (only_curve) {
1062                                         /* Curve-Path option must be enabled for follow-path constraints to be able to work */
1063                                         Curve *cu= (Curve *)ob->data;
1064                                         cu->flag |= CU_PATH;
1065                                 }
1066                                 
1067                                 break;
1068                         }
1069                 }
1070                 CTX_DATA_END;
1071         }
1072         
1073         /* if still not found, add a new empty to act as a target (if allowed) */
1074         if ((found == 0) && (add)) {
1075                 Scene *scene= CTX_data_scene(C);
1076                 Base *base= BASACT, *newbase=NULL;
1077                 Object *obt;
1078                 
1079                 /* add new target object */
1080                 obt= add_object(scene, OB_EMPTY);
1081                 
1082                 /* set layers OK */
1083                 newbase= BASACT;
1084                 newbase->lay= base->lay;
1085                 obt->lay= newbase->lay;
1086                 
1087                 /* transform cent to global coords for loc */
1088                 if (pchanact) {
1089                         /* since by default, IK targets the tip of the last bone, use the tip of the active PoseChannel 
1090                          * if adding a target for an IK Constraint
1091                          */
1092                         if (con_type == CONSTRAINT_TYPE_KINEMATIC)
1093                                 mul_v3_m4v3(obt->loc, obact->obmat, pchanact->pose_tail);
1094                         else
1095                                 mul_v3_m4v3(obt->loc, obact->obmat, pchanact->pose_head);
1096                 }
1097                 else
1098                         VECCOPY(obt->loc, obact->obmat[3]);
1099                 
1100                 /* restore, add_object sets active */
1101                 BASACT= base;
1102                 base->flag |= SELECT;
1103                 
1104                 /* make our new target the new object */
1105                 *tar_ob= obt;
1106                 found= 1;
1107         }
1108         
1109         /* return whether there's any target */
1110         return found;
1111 }
1112
1113 /* used by add constraint operators to add the constraint required */
1114 static int constraint_add_exec(bContext *C, wmOperator *op, Object *ob, ListBase *list, int type, short setTarget)
1115 {
1116         Scene *scene= CTX_data_scene(C);
1117         bPoseChannel *pchan;
1118         bConstraint *con;
1119         
1120         if(list == &ob->constraints)
1121                 pchan= NULL;
1122         else
1123                 pchan= get_active_posechannel(ob);
1124
1125         /* check if constraint to be added is valid for the given constraints stack */
1126         if (type == CONSTRAINT_TYPE_NULL) {
1127                 return OPERATOR_CANCELLED;
1128         }
1129         if ( (type == CONSTRAINT_TYPE_RIGIDBODYJOINT) && (list != &ob->constraints) ) {
1130                 BKE_report(op->reports, RPT_ERROR, "Rigid Body Joint Constraint can only be added to Objects.");
1131                 return OPERATOR_CANCELLED;
1132         }
1133         if ( (type == CONSTRAINT_TYPE_KINEMATIC) && ((!pchan) || (list != &pchan->constraints)) ) {
1134                 BKE_report(op->reports, RPT_ERROR, "IK Constraint can only be added to Bones.");
1135                 return OPERATOR_CANCELLED;
1136         }
1137         if ( (type == CONSTRAINT_TYPE_SPLINEIK) && ((!pchan) || (list != &pchan->constraints)) ) {
1138                 BKE_report(op->reports, RPT_ERROR, "Spline IK Constraint can only be added to Bones.");
1139                 return OPERATOR_CANCELLED;
1140         }
1141         
1142         /* create a new constraint of the type requried, and add it to the active/given constraints list */
1143         if(pchan)
1144                 con = add_pose_constraint(ob, pchan, NULL, type);
1145         else
1146                 con = add_ob_constraint(ob, NULL, type);
1147         
1148         /* get the first selected object/bone, and make that the target
1149          *      - apart from the buttons-window add buttons, we shouldn't add in this way
1150          */
1151         if (setTarget) {
1152                 Object *tar_ob= NULL;
1153                 bPoseChannel *tar_pchan= NULL;
1154                 
1155                 /* get the target objects, adding them as need be */
1156                 if (get_new_constraint_target(C, type, &tar_ob, &tar_pchan, 1)) {
1157                         /* method of setting target depends on the type of target we've got 
1158                          *      - by default, just set the first target (distinction here is only for multiple-targetted constraints)
1159                          */
1160                         if (tar_pchan)
1161                                 set_constraint_nth_target(con, tar_ob, tar_pchan->name, 0);
1162                         else
1163                                 set_constraint_nth_target(con, tar_ob, "", 0);
1164                 }
1165         }
1166         
1167         /* do type-specific tweaking to the constraint settings  */
1168         // TODO: does action constraint need anything here - i.e. spaceonce?
1169         switch (type) {
1170                 case CONSTRAINT_TYPE_CHILDOF:
1171                 {
1172                         /* if this constraint is being added to a posechannel, make sure
1173                          * the constraint gets evaluated in pose-space */
1174                         if (ob->mode & OB_MODE_POSE) {
1175                                 con->ownspace = CONSTRAINT_SPACE_POSE;
1176                                 con->flag |= CONSTRAINT_SPACEONCE;
1177                         }
1178                 }
1179                         break;
1180                         
1181                 case CONSTRAINT_TYPE_PYTHON: // FIXME: this code is not really valid anymore
1182                 {
1183                         char *menustr;
1184                         int scriptint= 0;
1185 #ifndef DISABLE_PYTHON
1186                         /* popup a list of usable scripts */
1187                         menustr = buildmenu_pyconstraints(NULL, &scriptint);
1188                         // XXX scriptint = pupmenu(menustr);
1189                         MEM_freeN(menustr);
1190                         
1191                         /* only add constraint if a script was chosen */
1192                         if (scriptint) {
1193                                 /* add constraint */
1194                                 validate_pyconstraint_cb(con->data, &scriptint);
1195                                 
1196                                 /* make sure target allowance is set correctly */
1197                                 BPY_pyconstraint_update(ob, con);
1198                         }
1199 #endif
1200                 }
1201                 default:
1202                         break;
1203         }
1204         
1205         /* make sure all settings are valid - similar to above checks, but sometimes can be wrong */
1206         object_test_constraints(ob);
1207         
1208         if (ob->pose)
1209                 update_pose_constraint_flags(ob->pose);
1210         
1211         
1212         /* force depsgraph to get recalculated since new relationships added */
1213         DAG_scene_sort(scene);          /* sort order of objects */
1214         
1215         if ((ob->type==OB_ARMATURE) && (pchan)) {
1216                 ob->pose->flag |= POSE_RECALC;  /* sort pose channels */
1217                 DAG_id_flush_update(&ob->id, OB_RECALC_DATA|OB_RECALC_OB);
1218         }
1219         else
1220                 DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
1221         
1222         /* notifiers for updates */
1223         WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT|NA_ADDED, ob);
1224         
1225         return OPERATOR_FINISHED;
1226 }
1227
1228 /* ------------------ */
1229
1230 /* dummy operator callback */
1231 static int object_constraint_add_exec(bContext *C, wmOperator *op)
1232 {
1233         Object *ob=ED_object_active_context(C);
1234         int type= RNA_enum_get(op->ptr, "type");
1235         short with_targets= 0;
1236         
1237         if (!ob) {
1238                 BKE_report(op->reports, RPT_ERROR, "No active object to add constraint to.");
1239                 return OPERATOR_CANCELLED;
1240         }
1241                 
1242         /* hack: set constraint targets from selected objects in context is allowed when
1243          *              operator name included 'with_targets', since the menu doesn't allow multiple properties
1244          */
1245         if (strstr(op->idname, "with_targets"))
1246                 with_targets= 1;
1247
1248         return constraint_add_exec(C, op, ob, &ob->constraints, type, with_targets);
1249 }
1250
1251 /* dummy operator callback */
1252 static int object_constraint_copy_exec(bContext *C, wmOperator *op)
1253 {
1254         Object *ob=ED_object_active_context(C);
1255
1256         CTX_DATA_BEGIN(C, Object*, ob_iter, selected_editable_objects) {
1257                 if(ob != ob_iter) {
1258                         if (ob->data != ob_iter->data){
1259                                 copy_constraints(&ob_iter->constraints, &ob->constraints, TRUE);
1260                         }
1261                         
1262                         if(ob_iter->totcol==ob->totcol) {
1263                                 ob_iter->actcol= ob->actcol;
1264                                 WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob_iter);
1265                         }
1266                 }
1267         }
1268         CTX_DATA_END;
1269
1270         return OPERATOR_FINISHED;
1271 }
1272
1273 /* dummy operator callback */
1274 static int pose_constraint_add_exec(bContext *C, wmOperator *op)
1275 {
1276         Object *ob= ED_object_active_context(C);
1277         int type= RNA_enum_get(op->ptr, "type");
1278         short with_targets= 0;
1279         
1280         if (!ob) {
1281                 BKE_report(op->reports, RPT_ERROR, "No active object to add constraint to.");
1282                 return OPERATOR_CANCELLED;
1283         }
1284                 
1285         /* hack: set constraint targets from selected objects in context is allowed when
1286          *              operator name included 'with_targets', since the menu doesn't allow multiple properties
1287          */
1288         if (strstr(op->idname, "with_targets"))
1289                 with_targets= 1;
1290         
1291         return constraint_add_exec(C, op, ob, get_active_constraints(ob), type, with_targets);
1292 }
1293
1294 /* ------------------ */
1295
1296 void OBJECT_OT_constraint_add(wmOperatorType *ot)
1297 {
1298         /* identifiers */
1299         ot->name= "Add Constraint";
1300         ot->description = "Add a constraint to the active object";
1301         ot->idname= "OBJECT_OT_constraint_add";
1302         
1303         /* api callbacks */
1304         ot->invoke= WM_menu_invoke;
1305         ot->exec= object_constraint_add_exec;
1306         ot->poll= ED_operator_object_active_editable;
1307         
1308         /* flags */
1309         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1310         
1311         /* properties */
1312         ot->prop= RNA_def_enum(ot->srna, "type", constraint_type_items, 0, "Type", "");
1313 }
1314
1315 void OBJECT_OT_constraint_add_with_targets(wmOperatorType *ot)
1316 {
1317         /* identifiers */
1318         ot->name= "Add Constraint (with Targets)";
1319         ot->description = "Add a constraint to the active object, with target (where applicable) set to the selected Objects/Bones";
1320         ot->idname= "OBJECT_OT_constraint_add_with_targets";
1321         
1322         /* api callbacks */
1323         ot->invoke= WM_menu_invoke;
1324         ot->exec= object_constraint_add_exec;
1325         ot->poll= ED_operator_object_active_editable;
1326         
1327         /* flags */
1328         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1329         
1330         /* properties */
1331         ot->prop= RNA_def_enum(ot->srna, "type", constraint_type_items, 0, "Type", "");
1332 }
1333
1334 void OBJECT_OT_constraint_copy(wmOperatorType *ot)
1335 {
1336         /* identifiers */
1337         ot->name= "Copy Constraints to Others";
1338         ot->description = "Copy constraints to other selected objects.";
1339         ot->idname= "OBJECT_OT_constraint_copy";
1340
1341         /* api callbacks */
1342         ot->exec= object_constraint_copy_exec;
1343         ot->poll= ED_operator_object_active_editable;
1344
1345         /* flags */
1346         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1347 }
1348
1349 void POSE_OT_constraint_add(wmOperatorType *ot)
1350 {
1351         /* identifiers */
1352         ot->name= "Add Constraint";
1353         ot->description = "Add a constraint to the active bone";
1354         ot->idname= "POSE_OT_constraint_add";
1355         
1356         /* api callbacks */
1357         ot->invoke= WM_menu_invoke;
1358         ot->exec= pose_constraint_add_exec;
1359         ot->poll= ED_operator_posemode;
1360         
1361         /* flags */
1362         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1363         
1364         /* properties */
1365         ot->prop= RNA_def_enum(ot->srna, "type", constraint_type_items, 0, "Type", "");
1366 }
1367
1368 void POSE_OT_constraint_add_with_targets(wmOperatorType *ot)
1369 {
1370         /* identifiers */
1371         ot->name= "Add Constraint (with Targets)";
1372         ot->description = "Add a constraint to the active bone, with target (where applicable) set to the selected Objects/Bones";
1373         ot->idname= "POSE_OT_constraint_add_with_targets";
1374         
1375         /* api callbacks */
1376         ot->invoke= WM_menu_invoke;
1377         ot->exec= pose_constraint_add_exec;
1378         ot->poll= ED_operator_posemode;
1379         
1380         /* flags */
1381         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1382         
1383         /* properties */
1384         ot->prop= RNA_def_enum(ot->srna, "type", constraint_type_items, 0, "Type", "");
1385 }
1386
1387 /************************ IK Constraint operators *********************/
1388 /* NOTE: only for Pose-Channels */
1389 // TODO: should these be here, or back in editors/armature/poseobject.c again?
1390
1391 /* present menu with options + validation for targets to use */
1392 static int pose_ik_add_invoke(bContext *C, wmOperator *op, wmEvent *evt)
1393 {
1394         Object *ob= CTX_data_active_object(C);
1395         bPoseChannel *pchan= get_active_posechannel(ob);
1396         bConstraint *con= NULL;
1397         
1398         uiPopupMenu *pup;
1399         uiLayout *layout;
1400         Object *tar_ob= NULL;
1401         bPoseChannel *tar_pchan= NULL;
1402         
1403         /* must have active bone */
1404         if (ELEM(NULL, ob, pchan)) {
1405                 BKE_report(op->reports, RPT_ERROR, "Must have active bone to add IK Constraint to.");
1406                 return OPERATOR_CANCELLED;
1407         }
1408         
1409         /* bone must not have any constraints already */
1410         for (con= pchan->constraints.first; con; con= con->next) {
1411                 if (con->type==CONSTRAINT_TYPE_KINEMATIC) break;
1412         }
1413         if (con) {
1414                 BKE_report(op->reports, RPT_ERROR, "Bone already has IK Constraint.");
1415                 return OPERATOR_CANCELLED;
1416         }
1417         
1418         /* prepare popup menu to choose targetting options */
1419         pup= uiPupMenuBegin(C, "Add IK", 0);
1420         layout= uiPupMenuLayout(pup);
1421         
1422         /* the type of targets we'll set determines the menu entries to show... */
1423         if (get_new_constraint_target(C, CONSTRAINT_TYPE_KINEMATIC, &tar_ob, &tar_pchan, 0)) {
1424                 /* bone target, or object target? 
1425                  *      - the only thing that matters is that we want a target...
1426                  */
1427                 if (tar_pchan)
1428                         uiItemBooleanO(layout, "To Active Bone", 0, "POSE_OT_ik_add", "with_targets", 1);
1429                 else
1430                         uiItemBooleanO(layout, "To Active Object", 0, "POSE_OT_ik_add", "with_targets", 1);
1431         }
1432         else {
1433                 /* we have a choice of adding to a new empty, or not setting any target (targetless IK) */
1434                 uiItemBooleanO(layout, "To New Empty Object", 0, "POSE_OT_ik_add", "with_targets", 1);
1435                 uiItemBooleanO(layout, "Without Targets", 0, "POSE_OT_ik_add", "with_targets", 0);
1436         }
1437         
1438         /* finish building the menu, and process it (should result in calling self again) */
1439         uiPupMenuEnd(C, pup);
1440         
1441         return OPERATOR_CANCELLED;
1442 }
1443
1444 /* call constraint_add_exec() to add the IK constraint */
1445 static int pose_ik_add_exec(bContext *C, wmOperator *op)
1446 {
1447         Object *ob= CTX_data_active_object(C);
1448         int with_targets= RNA_boolean_get(op->ptr, "with_targets");
1449         
1450         /* add the constraint - all necessary checks should have been done by the invoke() callback already... */
1451         return constraint_add_exec(C, op, ob, get_active_constraints(ob), CONSTRAINT_TYPE_KINEMATIC, with_targets);
1452 }
1453
1454 void POSE_OT_ik_add(wmOperatorType *ot)
1455 {
1456         /* identifiers */
1457         ot->name= "Add IK to Bone";
1458         ot->description= "Add IK Constraint to the active Bone";
1459         ot->idname= "POSE_OT_ik_add";
1460         
1461         /* api callbacks */
1462         ot->invoke= pose_ik_add_invoke;
1463         ot->exec= pose_ik_add_exec;
1464         ot->poll= ED_operator_posemode;
1465         
1466         /* flags */
1467         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1468         
1469         /* properties */
1470         RNA_def_boolean(ot->srna, "with_targets", 1, "With Targets", "Assign IK Constraint with targets derived from the select bones/objects");
1471 }
1472
1473 /* ------------------ */
1474
1475 /* remove IK constraints from selected bones */
1476 static int pose_ik_clear_exec(bContext *C, wmOperator *op)
1477 {
1478         Object *ob= CTX_data_active_object(C);
1479         
1480         /* only remove IK Constraints */
1481         CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pose_bones) 
1482         {
1483                 bConstraint *con, *next;
1484                 
1485                 // TODO: should we be checking if these contraints were local before we try and remove them?
1486                 for (con= pchan->constraints.first; con; con= next) {
1487                         next= con->next;
1488                         if (con->type==CONSTRAINT_TYPE_KINEMATIC) {
1489                                 remove_constraint(&pchan->constraints, con);
1490                         }
1491                 }
1492                 pchan->constflag &= ~(PCHAN_HAS_IK|PCHAN_HAS_TARGET);
1493         }
1494         CTX_DATA_END;
1495         
1496         /* refresh depsgraph */
1497         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
1498
1499         /* note, notifier might evolve */
1500         WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT|NA_REMOVED, ob);
1501         
1502         return OPERATOR_FINISHED;
1503 }
1504
1505 void POSE_OT_ik_clear(wmOperatorType *ot)
1506 {
1507         /* identifiers */
1508         ot->name= "Remove IK";
1509         ot->description= "Remove all IK Constraints from selected bones";
1510         ot->idname= "POSE_OT_ik_clear";
1511         
1512         /* api callbacks */
1513         ot->exec= pose_ik_clear_exec;
1514         ot->poll= ED_operator_posemode;
1515         
1516         /* flags */
1517         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1518 }
1519