added scons option BF_WITH_PYTHON (defined as DISABLE_PYTHON)
[blender-staging.git] / source / blender / src / editconstraint.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): Joshua Leung
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #include <stdio.h>
31 #include <string.h>
32
33 #include "MEM_guardedalloc.h"
34
35 #include "BLI_blenlib.h"
36 #include "BLI_arithb.h"
37 #include "BLI_dynstr.h"
38
39 #include "DNA_action_types.h"
40 #include "DNA_armature_types.h"
41 #include "DNA_constraint_types.h"
42 #include "DNA_curve_types.h"
43 #include "DNA_object_types.h"
44 #include "DNA_scene_types.h"
45 #include "DNA_screen_types.h"
46 #include "DNA_text_types.h"
47 #include "DNA_view3d_types.h"
48
49 #include "BKE_action.h"
50 #include "BKE_armature.h"
51 #include "BKE_constraint.h"
52 #include "BKE_depsgraph.h"
53 #include "BKE_global.h"
54 #include "BKE_main.h"
55 #include "BKE_ipo.h"
56 #include "BKE_object.h"
57 #include "BKE_utildefines.h"
58
59 #include "BIF_editaction.h"
60 #include "BIF_editarmature.h"
61 #include "BIF_editconstraint.h"
62 #include "BIF_poseobject.h"
63 #include "BIF_interface.h"
64 #include "BIF_screen.h"
65 #include "BIF_space.h"
66 #include "BIF_toolbox.h"
67
68 #ifndef DISABLE_PYTHON
69 #include "BPY_extern.h"
70 #endif
71
72 #include "blendef.h"
73 #include "nla.h"
74 #include "mydevice.h"
75
76 /* -------------- Get Active Constraint Data ---------------------- */
77
78 ListBase *get_active_constraint_channels (Object *ob, int forcevalid)
79 {
80         char ipstr[64];
81         
82         if (ob == NULL)
83                 return NULL;
84         
85         /* See if we are a bone constraint */
86         if (ob->flag & OB_POSEMODE) {
87                 bActionChannel *achan;
88                 bPoseChannel *pchan;
89                 
90                 pchan = get_active_posechannel(ob);
91                 if (pchan) {
92                         /* Make sure we have an action */
93                         if (ob->action == NULL) {
94                                 if (forcevalid == 0)
95                                         return NULL;
96                                 
97                                 ob->action= add_empty_action("Action");
98                         }
99                         
100                         /* Make sure we have an actionchannel */
101                         achan = get_action_channel(ob->action, pchan->name);
102                         if (achan == NULL) {
103                                 if (forcevalid == 0)
104                                         return NULL;
105                                 
106                                 achan = MEM_callocN (sizeof(bActionChannel), "ActionChannel");
107                                 
108                                 strcpy(achan->name, pchan->name);
109                                 sprintf(ipstr, "%s.%s", ob->action->id.name+2, achan->name);
110                                 ipstr[23]=0;
111                                 achan->ipo=     add_ipo(ipstr, ID_AC);  
112                                 
113                                 BLI_addtail(&ob->action->chanbase, achan);
114                         }
115                         
116                         return &achan->constraintChannels;
117                 }
118                 else 
119                         return NULL;
120         }
121         /* else we return object constraints */
122         else {
123                 if (ob->ipoflag & OB_ACTION_OB) {
124                         bActionChannel *achan = get_action_channel(ob->action, "Object");
125                         if (achan)
126                                 return &achan->constraintChannels;
127                         else 
128                                 return NULL;
129                 }
130                 
131                 return &ob->constraintChannels;
132         }
133 }
134
135
136 /* if object in posemode, active bone constraints, else object constraints */
137 ListBase *get_active_constraints (Object *ob)
138 {
139         if (ob == NULL)
140                 return NULL;
141
142         if (ob->flag & OB_POSEMODE) {
143                 bPoseChannel *pchan;
144                 
145                 pchan = get_active_posechannel(ob);
146                 if (pchan)
147                         return &pchan->constraints;
148         }
149         else 
150                 return &ob->constraints;
151
152         return NULL;
153 }
154
155 /* single constraint */
156 bConstraint *get_active_constraint (Object *ob)
157 {
158         ListBase *lb= get_active_constraints(ob);
159
160         if (lb) {
161                 bConstraint *con;
162                 
163                 for (con= lb->first; con; con=con->next) {
164                         if (con->flag & CONSTRAINT_ACTIVE)
165                                 return con;
166                 }
167         }
168         
169         return NULL;
170 }
171
172 /* single channel, for ipo */
173 bConstraintChannel *get_active_constraint_channel (Object *ob)
174 {
175         bConstraint *con;
176         bConstraintChannel *chan;
177         
178         if (ob->flag & OB_POSEMODE) {
179                 if (ob->action) {
180                         bPoseChannel *pchan;
181                         
182                         pchan = get_active_posechannel(ob);
183                         if (pchan) {
184                                 for (con= pchan->constraints.first; con; con= con->next) {
185                                         if (con->flag & CONSTRAINT_ACTIVE)
186                                                 break;
187                                 }
188                                 
189                                 if (con) {
190                                         bActionChannel *achan = get_action_channel(ob->action, pchan->name);
191                                         if (achan) {
192                                                 for (chan= achan->constraintChannels.first; chan; chan= chan->next) {
193                                                         if (!strcmp(chan->name, con->name))
194                                                                 break;
195                                                 }
196                                                 return chan;
197                                         }
198                                 }
199                         }
200                 }
201         }
202         else {
203                 for (con= ob->constraints.first; con; con= con->next) {
204                         if (con->flag & CONSTRAINT_ACTIVE)
205                                 break;
206                 }
207                 
208                 if (con) {
209                         ListBase *lb= get_active_constraint_channels(ob, 0);
210                         
211                         if (lb) {
212                                 for (chan= lb->first; chan; chan= chan->next) {
213                                         if (!strcmp(chan->name, con->name))
214                                                 break;
215                                 }
216                                 
217                                 return chan;
218                         }
219                 }
220         }
221         
222         return NULL;
223 }
224
225 /* -------------- Constraint Management (Add New, Remove, Rename) -------------------- */
226
227 /* Creates a new constraint, initialises its data, and returns it */
228 bConstraint *add_new_constraint (short type)
229 {
230         bConstraint *con;
231         bConstraintTypeInfo *cti;
232
233         con = MEM_callocN(sizeof(bConstraint), "Constraint");
234         
235         /* Set up a generic constraint datablock */
236         con->type = type;
237         con->flag |= CONSTRAINT_EXPAND;
238         con->enforce = 1.0F;
239         strcpy(con->name, "Const");
240         
241         /* Load the data for it */
242         cti = constraint_get_typeinfo(con);
243         if (cti) {
244                 con->data = MEM_callocN(cti->size, cti->structName);
245                 
246                 /* only constraints that change any settings need this */
247                 if (cti->new_data)
248                         cti->new_data(con->data);
249         }
250         
251         return con;
252 }
253
254 /* Adds the given constraint to the Object-level set of constraints for the given Object */
255 void add_constraint_to_object (bConstraint *con, Object *ob)
256 {
257         ListBase *list;
258         list = &ob->constraints;
259         
260         if (list) {
261                 unique_constraint_name(con, list);
262                 BLI_addtail(list, con);
263                 
264                 if (proxylocked_constraints_owner(ob, NULL))
265                         con->flag |= CONSTRAINT_PROXY_LOCAL;
266                 
267                 con->flag |= CONSTRAINT_ACTIVE;
268                 for (con= con->prev; con; con= con->prev)
269                         con->flag &= ~CONSTRAINT_ACTIVE;
270         }
271 }
272
273 /* helper function for add_constriant - sets the last target for the active constraint */
274 static void set_constraint_nth_target (bConstraint *con, Object *target, char subtarget[], int index)
275 {
276         bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
277         ListBase targets = {NULL, NULL};
278         bConstraintTarget *ct;
279         int num_targets, i;
280         
281         if (cti && cti->get_constraint_targets) {
282                 cti->get_constraint_targets(con, &targets);
283                 num_targets= BLI_countlist(&targets);
284                 
285                 if (index < 0) {
286                         if (abs(index) < num_targets)
287                                 index= num_targets - abs(index);
288                         else
289                                 index= num_targets - 1;
290                 }
291                 else if (index >= num_targets) {
292                         index= num_targets - 1;
293                 }
294                 
295                 for (ct=targets.first, i=0; ct; ct= ct->next, i++) {
296                         if (i == index) {
297                                 ct->tar= target;
298                                 strcpy(ct->subtarget, subtarget);
299                                 break;
300                         }
301                 }
302                 
303                 if (cti->flush_constraint_targets)
304                         cti->flush_constraint_targets(con, &targets, 0);
305         }
306 }
307
308 /* context: active object in posemode, active channel, optional selected channel */
309 void add_constraint (short only_IK)
310 {
311         Object *ob= OBACT, *obsel=NULL;
312         bPoseChannel *pchanact=NULL, *pchansel=NULL;
313         bConstraint *con=NULL;
314         Base *base;
315         short nr;
316         
317         /* paranoia checks */
318         if ((ob==NULL) || (ob==G.obedit)) 
319                 return;
320
321         if ((ob->pose) && (ob->flag & OB_POSEMODE)) {
322                 bArmature *arm= ob->data;
323                 
324                 /* find active channel */
325                 pchanact= get_active_posechannel(ob);
326                 if (pchanact==NULL) 
327                         return;
328                 
329                 /* find selected bone */
330                 for (pchansel=ob->pose->chanbase.first; pchansel; pchansel=pchansel->next) {
331                         if (pchansel != pchanact) {
332                                 if (pchansel->bone->flag & BONE_SELECTED)  {
333                                         if (pchansel->bone->layer & arm->layer)
334                                                 break;
335                                 }
336                         }
337                 }
338         }
339         
340         /* find selected object */
341         for (base= FIRSTBASE; base; base= base->next) {
342                 if ((TESTBASE(base)) && (base->object!=ob)) 
343                         obsel= base->object;
344         }
345         
346         /* the only_IK caller has checked for posemode! */
347         if (only_IK) {
348                 for (con= pchanact->constraints.first; con; con= con->next) {
349                         if (con->type==CONSTRAINT_TYPE_KINEMATIC) break;
350                 }
351                 if (con) {
352                         error("Pose Channel already has IK");
353                         return;
354                 }
355                 
356                 if (pchansel)
357                         nr= pupmenu("Add IK Constraint%t|To Active Bone%x10");
358                 else if (obsel)
359                         nr= pupmenu("Add IK Constraint%t|To Active Object%x10");
360                 else 
361                         nr= pupmenu("Add IK Constraint%t|To New Empty Object%x10|Without Target%x11");
362         }
363         else {
364                 if (pchanact) {
365                         if (pchansel)
366                                 nr= pupmenu("Add Constraint to Active Bone%t|Child Of%x19|Transformation%x20|%l|Copy Location%x1|Copy Rotation%x2|Copy Scale%x8|%l|Limit Location%x13|Limit Rotation%x14|Limit Scale%x15|Limit Distance%x21|%l|Track To%x3|Floor%x4|Locked Track%x5|Stretch To%x7|%l|Action%x16|Script%x18");
367                         else if ((obsel) && (obsel->type==OB_CURVE))
368                                 nr= pupmenu("Add Constraint to Active Object%t|Child Of%x19|Transformation%x20|%l|Copy Location%x1|Copy Rotation%x2|Copy Scale%x8|%l|Limit Location%x13|Limit Rotation%x14|Limit Scale%x15|Limit Distance%x21|%l|Track To%x3|Floor%x4|Locked Track%x5|Follow Path%x6|Clamp To%x17|Stretch To%x7|%l|Action%x16|Script%x18");
369                         else if (obsel)
370                                 nr= pupmenu("Add Constraint to Active Object%t|Child Of%x19|Transformation%x20|%l|Copy Location%x1|Copy Rotation%x2|Copy Scale%x8|%l|Limit Location%x13|Limit Rotation%x14|Limit Scale%x15|Limit Distance%x21|%l|Track To%x3|Floor%x4|Locked Track%x5|Stretch To%x7|%l|Action%x16|Script%x18");
371                         else
372                                 nr= pupmenu("Add Constraint to New Empty Object%t|Child Of%x19|Transformation%x20|%l|Copy Location%x1|Copy Rotation%x2|Copy Scale%x8|%l|Limit Location%x13|Limit Rotation%x14|Limit Scale%x15|Limit Distance%x21|%l|Track To%x3|Floor%x4|Locked Track%x5|Stretch To%x7|%l|Action%x16|Script%x18");
373                 }
374                 else {
375                         if ((obsel) && (obsel->type==OB_CURVE))
376                                 nr= pupmenu("Add Constraint to Active Object%t|Child Of%x19|Transformation%x20|%l|Copy Location%x1|Copy Rotation%x2|Copy Scale%x8|%l|Limit Location%x13|Limit Rotation%x14|Limit Scale%x15|Limit Distance%x21|%l|Track To%x3|Floor%x4|Locked Track%x5|Follow Path%x6|Clamp To%x17|%l|Action%x16|Script%x18");
377                         else if (obsel)
378                                 nr= pupmenu("Add Constraint to Active Object%t|Child Of%x19|Transformation%x20|%l|Copy Location%x1|Copy Rotation%x2|Copy Scale%x8|%l|Limit Location%x13|Limit Rotation%x14|Limit Scale%x15|Limit Distance%x21|%l|Track To%x3|Floor%x4|Locked Track%x5|%l|Action%x16|Script%x18");
379                         else
380                                 nr= pupmenu("Add Constraint to New Empty Object%t|Child Of%x19|Transformation%x20|%l|Copy Location%x1|Copy Rotation%x2|Copy Scale%x8|%l|Limit Location%x13|Limit Rotation%x14|Limit Scale%x15|Limit Distance%x21|%l|Track To%x3|Floor%x4|Locked Track%x5|%l|Action%x16|Script%x18");
381                 }
382         }
383         
384         if (nr < 1) return;
385         
386         /* handle IK separate */
387         if (nr==10 || nr==11) {
388                 /* ik - prevent weird chains... */
389                 if (pchansel) {
390                         bPoseChannel *pchan= pchanact;
391                         while (pchan) {
392                                 if (pchan==pchansel) break;
393                                 pchan= pchan->parent;
394                         }
395                         if (pchan) {
396                                 error("IK root cannot be linked to IK tip");
397                                 return;
398                         }
399                         
400                         pchan= pchansel;
401                         while (pchan) {
402                                 if (pchan==pchanact) break;
403                                 pchan= pchan->parent;
404                         }
405                         if (pchan) {
406                                 error("IK tip cannot be linked to IK root");
407                                 return;
408                         }               
409                 }
410                 
411                 con = add_new_constraint(CONSTRAINT_TYPE_KINEMATIC);
412                 BLI_addtail(&pchanact->constraints, con);
413                 unique_constraint_name(con, &pchanact->constraints);
414                 pchanact->constflag |= PCHAN_HAS_IK;    /* for draw, but also for detecting while pose solving */
415                 if (nr==11) 
416                         pchanact->constflag |= PCHAN_HAS_TARGET;
417                 if (proxylocked_constraints_owner(ob, pchanact))
418                         con->flag |= CONSTRAINT_PROXY_LOCAL;
419         }
420         else {
421                 /* normal constraints - add data */
422                 if (nr==1) con = add_new_constraint(CONSTRAINT_TYPE_LOCLIKE);
423                 else if (nr==2) con = add_new_constraint(CONSTRAINT_TYPE_ROTLIKE);
424                 else if (nr==3) con = add_new_constraint(CONSTRAINT_TYPE_TRACKTO);
425                 else if (nr==4) con = add_new_constraint(CONSTRAINT_TYPE_MINMAX);
426                 else if (nr==5) con = add_new_constraint(CONSTRAINT_TYPE_LOCKTRACK);
427                 else if (nr==6) {
428                         Curve *cu= obsel->data;
429                         cu->flag |= CU_PATH;
430                         con = add_new_constraint(CONSTRAINT_TYPE_FOLLOWPATH);
431                 }
432                 else if (nr==7) con = add_new_constraint(CONSTRAINT_TYPE_STRETCHTO);
433                 else if (nr==8) con = add_new_constraint(CONSTRAINT_TYPE_SIZELIKE);
434                 else if (nr==13) con = add_new_constraint(CONSTRAINT_TYPE_LOCLIMIT);
435                 else if (nr==14) con = add_new_constraint(CONSTRAINT_TYPE_ROTLIMIT);
436                 else if (nr==15) con = add_new_constraint(CONSTRAINT_TYPE_SIZELIMIT);
437                 else if (nr==16) {
438                         /* TODO: add a popup-menu to display list of available actions to use (like for pyconstraints) */
439                         con = add_new_constraint(CONSTRAINT_TYPE_ACTION);
440                 }
441                 else if (nr==17) {
442                         Curve *cu= obsel->data;
443                         cu->flag |= CU_PATH;
444                         con = add_new_constraint(CONSTRAINT_TYPE_CLAMPTO);
445                 }
446                 else if (nr==18) {      
447                         char *menustr;
448                         int scriptint= 0;
449 #ifndef DISABLE_PYTHON
450                         /* popup a list of usable scripts */
451                         menustr = buildmenu_pyconstraints(NULL, &scriptint);
452                         scriptint = pupmenu(menustr);
453                         MEM_freeN(menustr);
454                         
455                         /* only add constraint if a script was chosen */
456                         if (scriptint) {
457                                 /* add constraint */
458                                 con = add_new_constraint(CONSTRAINT_TYPE_PYTHON);
459                                 validate_pyconstraint_cb(con->data, &scriptint);
460                                 
461                                 /* make sure target allowance is set correctly */
462                                 BPY_pyconstraint_update(ob, con);
463                         }
464 #endif
465                 }
466                 else if (nr==19) {
467                         con = add_new_constraint(CONSTRAINT_TYPE_CHILDOF);
468                         
469                         /* if this constraint is being added to a posechannel, make sure
470                          * the constraint gets evaluated in pose-space
471                          */
472                         if (pchanact) {
473                                 con->ownspace = CONSTRAINT_SPACE_POSE;
474                                 con->flag |= CONSTRAINT_SPACEONCE;
475                         }
476                 }
477                 else if (nr==20) con = add_new_constraint(CONSTRAINT_TYPE_TRANSFORM);
478                 else if (nr==21) con = add_new_constraint(CONSTRAINT_TYPE_DISTLIMIT);
479                 
480                 if (con==NULL) return;  /* paranoia */
481                 
482                 if (pchanact) {
483                         BLI_addtail(&pchanact->constraints, con);
484                         unique_constraint_name(con, &pchanact->constraints);
485                         pchanact->constflag |= PCHAN_HAS_CONST; /* for draw */
486                         if (proxylocked_constraints_owner(ob, pchanact))
487                                 con->flag |= CONSTRAINT_PROXY_LOCAL;
488                 }
489                 else {
490                         BLI_addtail(&ob->constraints, con);
491                         unique_constraint_name(con, &ob->constraints);
492                         if (proxylocked_constraints_owner(ob, NULL))
493                                 con->flag |= CONSTRAINT_PROXY_LOCAL;
494                 }
495         }
496         
497         /* set the target */
498         if (pchansel) {
499                 set_constraint_nth_target(con, ob, pchansel->name, 0);
500         }
501         else if (obsel) {
502                 set_constraint_nth_target(con, obsel, "", 0);
503         }
504         else if (ELEM4(nr, 11, 13, 14, 15)==0) {        /* add new empty as target */
505                 Base *base= BASACT, *newbase;
506                 Object *obt;
507                 
508                 obt= add_object(OB_EMPTY);
509                 /* set layers OK */
510                 newbase= BASACT;
511                 newbase->lay= base->lay;
512                 obt->lay= newbase->lay;
513                 
514                 /* transform cent to global coords for loc */
515                 if (pchanact) {
516                         if (only_IK)
517                                 VecMat4MulVecfl(obt->loc, ob->obmat, pchanact->pose_tail);
518                         else
519                                 VecMat4MulVecfl(obt->loc, ob->obmat, pchanact->pose_head);
520                 }
521                 else
522                         VECCOPY(obt->loc, ob->obmat[3]);
523                 
524                 set_constraint_nth_target(con, obt, "", 0);
525                 
526                 /* restore, add_object sets active */
527                 BASACT= base;
528                 base->flag |= SELECT;
529         }
530         
531         /* active flag */
532         con->flag |= CONSTRAINT_ACTIVE;
533         for (con= con->prev; con; con= con->prev)
534                 con->flag &= ~CONSTRAINT_ACTIVE;
535
536         DAG_scene_sort(G.scene);                // sort order of objects
537         
538         if (pchanact) {
539                 ob->pose->flag |= POSE_RECALC;  // sort pose channels
540                 DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);   // and all its relations
541         }
542         else
543                 DAG_object_flush_update(G.scene, ob, OB_RECALC_OB);     // and all its relations
544
545         allqueue(REDRAWVIEW3D, 0);
546         allqueue(REDRAWBUTSOBJECT, 0);
547         allqueue(REDRAWBUTSEDIT, 0);
548         allqueue(REDRAWOOPS, 0);
549         
550         if (only_IK)
551                 BIF_undo_push("Add IK Constraint");
552         else
553                 BIF_undo_push("Add Constraint");
554
555 }
556
557 /* Remove all constraints from the active object */
558 void ob_clear_constraints (void)
559 {
560         Object *ob= OBACT;
561         
562         /* paranoia checks */
563         if ((ob==NULL) || (ob==G.obedit) || (ob->flag & OB_POSEMODE)) 
564                 return;
565         
566         /* get user permission */
567         if (okee("Clear Constraints")==0) 
568                 return;
569         
570         /* do freeing */
571         free_constraints(&ob->constraints);
572         
573         /* do updates */
574         DAG_object_flush_update(G.scene, ob, OB_RECALC_OB);
575         
576         allqueue(REDRAWVIEW3D, 0);
577         allqueue(REDRAWBUTSOBJECT, 0);
578         allqueue(REDRAWOOPS, 0);
579         
580         BIF_undo_push("Clear Constraint(s)");
581 }
582
583 /* Rename the given constraint 
584  *      - con already has the new name 
585  */
586 void rename_constraint (Object *ob, bConstraint *con, char *oldname)
587 {
588         bConstraint *tcon;
589         bConstraintChannel *conchan;
590         ListBase *conlist= NULL;
591         int from_object= 0;
592         char *channame="";
593         
594         /* get context by searching for con (primitive...) */
595         for (tcon= ob->constraints.first; tcon; tcon= tcon->next) {
596                 if (tcon==con)
597                         break;
598         }
599         
600         if (tcon) {
601                 conlist= &ob->constraints;
602                 channame= "Object";
603                 from_object= 1;
604         }
605         else if (ob->pose) {
606                 bPoseChannel *pchan;
607                 
608                 for (pchan=ob->pose->chanbase.first; pchan; pchan=pchan->next) {
609                         for (tcon= pchan->constraints.first; tcon; tcon= tcon->next) {
610                                 if (tcon==con)
611                                         break;
612                         }
613                         if (tcon) 
614                                 break;
615                 }
616                 
617                 if (tcon) {
618                         conlist= &pchan->constraints;
619                         channame= pchan->name;
620                 }
621         }
622         
623         if (conlist==NULL) {
624                 printf("rename constraint failed\n");   /* should not happen in UI */
625                 return;
626         }
627         
628         /* first make sure it's a unique name within context */
629         unique_constraint_name (con, conlist);
630
631         /* own channels */
632         if (from_object) {
633                 for (conchan= ob->constraintChannels.first; conchan; conchan= conchan->next) {
634                         if ( strcmp(oldname, conchan->name)==0 )
635                                 BLI_strncpy(conchan->name, con->name, sizeof(conchan->name));
636                 }
637         }
638         
639         /* own action */
640         if (ob->action) {
641                 bActionChannel *achan= get_action_channel(ob->action, channame);
642                 if (achan) {
643                         conchan= get_constraint_channel(&achan->constraintChannels, oldname);
644                         if (conchan)
645                                 BLI_strncpy(conchan->name, con->name, sizeof(conchan->name));
646                 }
647         }
648 }
649
650
651 /* ------------- Constraint Sanity Testing ------------------- */
652
653 /* checks validity of object pointers, and NULLs,
654  * if Bone doesnt exist it sets the CONSTRAINT_DISABLE flag 
655  */
656 static void test_constraints (Object *owner, const char substring[])
657 {
658         bConstraint *curcon;
659         ListBase *conlist= NULL;
660         int type;
661         
662         if (owner==NULL) return;
663         
664         /* Check parents */
665         if (strlen(substring)) {
666                 switch (owner->type) {
667                         case OB_ARMATURE:
668                                 type = CONSTRAINT_OBTYPE_BONE;
669                                 break;
670                         default:
671                                 type = CONSTRAINT_OBTYPE_OBJECT;
672                                 break;
673                 }
674         }
675         else
676                 type = CONSTRAINT_OBTYPE_OBJECT;
677         
678         /* Get the constraint list for this object */
679         switch (type) {
680                 case CONSTRAINT_OBTYPE_OBJECT:
681                         conlist = &owner->constraints;
682                         break;
683                 case CONSTRAINT_OBTYPE_BONE:
684                         {
685                                 Bone *bone;
686                                 bPoseChannel *chan;
687                                 
688                                 bone = get_named_bone( ((bArmature *)owner->data), substring );
689                                 chan = get_pose_channel(owner->pose, substring);
690                                 if (bone && chan) {
691                                         conlist = &chan->constraints;
692                                 }
693                         }
694                         break;
695         }
696         
697         /* Check all constraints - is constraint valid? */
698         if (conlist) {
699                 for (curcon = conlist->first; curcon; curcon=curcon->next) {
700                         bConstraintTypeInfo *cti= constraint_get_typeinfo(curcon);
701                         ListBase targets = {NULL, NULL};
702                         bConstraintTarget *ct;
703                         
704                         /* clear disabled-flag first */
705                         curcon->flag &= ~CONSTRAINT_DISABLE;
706
707 #ifndef DISABLE_PYTHON
708                         /* Check specialised data (settings) for constraints that need this */
709                         if (curcon->type == CONSTRAINT_TYPE_PYTHON) {
710                                 bPythonConstraint *data = curcon->data;
711                                 
712                                 /* is there are valid script? */
713                                 if (data->text == NULL) {
714                                         curcon->flag |= CONSTRAINT_DISABLE;
715                                 }
716                                 else if (BPY_is_pyconstraint(data->text)==0) {
717                                         curcon->flag |= CONSTRAINT_DISABLE;
718                                 } else {        
719                                         /* does the constraint require target input... also validates targets */
720                                         BPY_pyconstraint_update(owner, curcon);
721                                 }
722                                 /* targets have already been checked for this */
723                                 continue;
724                         }
725                         else
726 #endif
727                         if (curcon->type == CONSTRAINT_TYPE_KINEMATIC) {
728                                 bKinematicConstraint *data = curcon->data;
729                                 
730                                 /* bad: we need a separate set of checks here as poletarget is 
731                                  *              optional... otherwise poletarget must exist too or else
732                                  *              the constraint is deemed invalid
733                                  */
734                                 if (exist_object(data->tar) == 0) {
735                                         data->tar = NULL;
736                                         curcon->flag |= CONSTRAINT_DISABLE;
737                                 }
738                                 else if (data->tar == owner) {
739                                         if (!get_named_bone(get_armature(owner), data->subtarget)) {
740                                                 curcon->flag |= CONSTRAINT_DISABLE;
741                                         }
742                                 }
743                                 
744                                 if (data->poletar) {
745                                         if (exist_object(data->poletar) == 0) {
746                                                 data->poletar = NULL;
747                                                 curcon->flag |= CONSTRAINT_DISABLE;
748                                         }
749                                         else if (data->poletar == owner) {
750                                                 if (!get_named_bone(get_armature(owner), data->polesubtarget)) {
751                                                         curcon->flag |= CONSTRAINT_DISABLE;
752                                                 }
753                                         }
754                                 }
755                                 
756                                 /* targets have already been checked for this */
757                                 continue;
758                         }
759                         else if (curcon->type == CONSTRAINT_TYPE_ACTION) {
760                                 bActionConstraint *data = curcon->data;
761                                 
762                                 /* validate action */
763                                 if (data->act == NULL) 
764                                         curcon->flag |= CONSTRAINT_DISABLE;
765                         }
766                         else if (curcon->type == CONSTRAINT_TYPE_FOLLOWPATH) {
767                                 bFollowPathConstraint *data = curcon->data;
768                                 
769                                 /* don't allow track/up axes to be the same */
770                                 if (data->upflag==data->trackflag)
771                                         curcon->flag |= CONSTRAINT_DISABLE;
772                                 if (data->upflag+3==data->trackflag)
773                                         curcon->flag |= CONSTRAINT_DISABLE;
774                         }
775                         else if (curcon->type == CONSTRAINT_TYPE_TRACKTO) {
776                                 bTrackToConstraint *data = curcon->data;
777                                 
778                                 /* don't allow track/up axes to be the same */
779                                 if (data->reserved2==data->reserved1)
780                                         curcon->flag |= CONSTRAINT_DISABLE;
781                                 if (data->reserved2+3==data->reserved1)
782                                         curcon->flag |= CONSTRAINT_DISABLE;
783                         }
784                         else if (curcon->type == CONSTRAINT_TYPE_LOCKTRACK) {
785                                 bLockTrackConstraint *data = curcon->data;
786                                 
787                                 if (data->lockflag==data->trackflag)
788                                         curcon->flag |= CONSTRAINT_DISABLE;
789                                 if (data->lockflag+3==data->trackflag)
790                                         curcon->flag |= CONSTRAINT_DISABLE;
791                         }
792                         
793                         /* Check targets for constraints */
794                         if (cti && cti->get_constraint_targets) {
795                                 cti->get_constraint_targets(curcon, &targets);
796                                 
797                                 /* disable and clear constraints targets that are incorrect */
798                                 for (ct= targets.first; ct; ct= ct->next) {
799                                         /* general validity checks (for those constraints that need this) */
800                                         if (exist_object(ct->tar) == 0) {
801                                                 ct->tar = NULL;
802                                                 curcon->flag |= CONSTRAINT_DISABLE;
803                                         }
804                                         else if (ct->tar == owner) {
805                                                 if (!get_named_bone(get_armature(owner), ct->subtarget)) {
806                                                         curcon->flag |= CONSTRAINT_DISABLE;
807                                                 }
808                                         }
809                                         
810                                         /* target checks for specific constraints */
811                                         if (ELEM(curcon->type, CONSTRAINT_TYPE_FOLLOWPATH, CONSTRAINT_TYPE_CLAMPTO)) {
812                                                 if (ct->tar) {
813                                                         if (ct->tar->type != OB_CURVE) {
814                                                                 ct->tar= NULL;
815                                                                 curcon->flag |= CONSTRAINT_DISABLE;
816                                                         }
817                                                         else {
818                                                                 Curve *cu= ct->tar->data;
819                                                                 
820                                                                 /* auto-set 'Path' setting on curve so this works  */
821                                                                 cu->flag |= CU_PATH;
822                                                         }
823                                                 }                                               
824                                         }
825                                 }       
826                                 
827                                 /* free any temporary targets */
828                                 if (cti->flush_constraint_targets)
829                                         cti->flush_constraint_targets(curcon, &targets, 0);
830                         }
831                 }
832         }
833 }
834
835 static void test_bonelist_constraints (Object *owner, ListBase *list)
836 {
837         Bone *bone;
838
839         for (bone = list->first; bone; bone = bone->next) {
840                 test_constraints(owner, bone->name);
841                 test_bonelist_constraints(owner, &bone->childbase);
842         }
843 }
844
845 void object_test_constraints (Object *owner)
846 {
847         test_constraints(owner, "");
848
849         if (owner->type==OB_ARMATURE) {
850                 bArmature *arm= get_armature(owner);
851                 
852                 if (arm)
853                         test_bonelist_constraints(owner, &arm->bonebase);
854         }
855 }
856
857 /* ********************** CONSTRAINT-SPECIFIC STUFF ********************* */
858 /* ------------- PyConstraints ------------------ */
859
860 /* this callback sets the text-file to be used for selected menu item */
861 void validate_pyconstraint_cb (void *arg1, void *arg2)
862 {
863         bPythonConstraint *data = arg1;
864         Text *text= NULL;
865         int index = *((int *)arg2);
866         int i;
867         
868         /* exception for no script */
869         if (index) {
870                 /* innovative use of a for...loop to search */
871                 for (text=G.main->text.first, i=1; text && index!=i; i++, text=text->id.next);
872         }
873         data->text = text;
874 }
875
876 #ifndef DISABLE_PYTHON
877 /* this returns a string for the list of usable pyconstraint script names */
878 char *buildmenu_pyconstraints (Text *con_text, int *pyconindex)
879 {
880         DynStr *pupds= BLI_dynstr_new();
881         Text *text;
882         char *str;
883         char buf[64];
884         int i;
885         
886         /* add title first */
887         sprintf(buf, "Scripts: %%t|[None]%%x0|");
888         BLI_dynstr_append(pupds, buf);
889         
890         /* init active-index first */
891         if (con_text == NULL)
892                 *pyconindex= 0;
893         
894         /* loop through markers, adding them */
895         for (text=G.main->text.first, i=1; text; i++, text=text->id.next) {
896                 /* this is important to ensure that right script is shown as active */
897                 if (text == con_text) *pyconindex = i;
898                 
899                 /* only include valid pyconstraint scripts */
900                 if (BPY_is_pyconstraint(text)) {
901                         BLI_dynstr_append(pupds, text->id.name+2);
902                         
903                         sprintf(buf, "%%x%d", i);
904                         BLI_dynstr_append(pupds, buf);
905                         
906                         if (text->id.next)
907                                 BLI_dynstr_append(pupds, "|");
908                 }
909         }
910         
911         /* convert to normal MEM_malloc'd string */
912         str= BLI_dynstr_get_cstring(pupds);
913         BLI_dynstr_free(pupds);
914         
915         return str;
916 }
917 #endif /* DISABLE_PYTHON */
918
919 /* this callback gets called when the 'refresh' button of a pyconstraint gets pressed */
920 void update_pyconstraint_cb (void *arg1, void *arg2)
921 {
922         Object *owner= (Object *)arg1;
923         bConstraint *con= (bConstraint *)arg2;
924 #ifndef DISABLE_PYTHON
925         if (owner && con)
926                 BPY_pyconstraint_update(owner, con);
927 #endif
928 }
929
930 /* ------------- Child-Of Constraint ------------------ */
931
932 /* ChildOf Constraint - set inverse callback */
933 void childof_const_setinv (void *conv, void *unused)
934 {
935         bConstraint *con= (bConstraint *)conv;
936         bChildOfConstraint *data= (bChildOfConstraint *)con->data;
937         Object *ob= OBACT;
938         bPoseChannel *pchan= NULL;
939
940         /* try to find a pose channel */
941         if (ob && ob->pose)
942                 pchan= get_active_posechannel(ob);
943         
944         /* calculate/set inverse matrix */
945         if (pchan) {
946                 float pmat[4][4], cinf;
947                 float imat[4][4], tmat[4][4];
948                 
949                 /* make copy of pchan's original pose-mat (for use later) */
950                 Mat4CpyMat4(pmat, pchan->pose_mat);
951                 
952                 /* disable constraint for pose to be solved without it */
953                 cinf= con->enforce;
954                 con->enforce= 0.0f;
955                 
956                 /* solve pose without constraint */
957                 where_is_pose(ob);
958                 
959                 /* determine effect of constraint by removing the newly calculated 
960                  * pchan->pose_mat from the original pchan->pose_mat, thus determining 
961                  * the effect of the constraint
962                  */
963                 Mat4Invert(imat, pchan->pose_mat);
964                 Mat4MulMat4(tmat, imat, pmat);
965                 Mat4Invert(data->invmat, tmat);
966                 
967                 /* recalculate pose with new inv-mat */
968                 con->enforce= cinf;
969                 where_is_pose(ob);
970         }
971         else if (ob) {
972                 /* use what_does_parent to find inverse - just like for normal parenting.
973                  * NOTE: what_does_parent uses a static workob defined in object.c 
974                  */
975                 what_does_parent(ob);
976                 Mat4Invert(data->invmat, workob.obmat);
977         }
978         else
979                 Mat4One(data->invmat);
980 }
981
982 /* ChildOf Constraint - clear inverse callback */
983 void childof_const_clearinv (void *conv, void *unused)
984 {
985         bConstraint *con= (bConstraint *)conv;
986         bChildOfConstraint *data= (bChildOfConstraint *)con->data;
987         
988         /* simply clear the matrix */
989         Mat4One(data->invmat);
990 }