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