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