5639f00ee39fce2a4b2895b80917c9bb946a8032
[blender.git] / source / blender / blenkernel / intern / 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., 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): 2007, Joshua Leung, major recode
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #include <stdio.h> 
31 #include <stddef.h>
32 #include <string.h>
33 #include <math.h>
34
35 #include "MEM_guardedalloc.h"
36 #include "nla.h"
37
38 #include "BLI_blenlib.h"
39 #include "BLI_arithb.h"
40
41 #include "DNA_armature_types.h"
42 #include "DNA_constraint_types.h"
43 #include "DNA_object_types.h"
44 #include "DNA_action_types.h"
45 #include "DNA_curve_types.h"
46 #include "DNA_meshdata_types.h"
47 #include "DNA_lattice_types.h"
48 #include "DNA_scene_types.h"
49 #include "DNA_text_types.h"
50
51 #include "BKE_utildefines.h"
52 #include "BKE_action.h"
53 #include "BKE_anim.h" /* for the curve calculation part */
54 #include "BKE_armature.h"
55 #include "BKE_blender.h"
56 #include "BKE_constraint.h"
57 #include "BKE_displist.h"
58 #include "BKE_deform.h"
59 #include "BKE_DerivedMesh.h"    /* for geometry targets */
60 #include "BKE_cdderivedmesh.h" /* for geometry targets */
61 #include "BKE_object.h"
62 #include "BKE_ipo.h"
63 #include "BKE_global.h"
64 #include "BKE_library.h"
65 #include "BKE_idprop.h"
66
67 #ifndef DISABLE_PYTHON
68 #include "BPY_extern.h"
69 #endif
70
71 #include "blendef.h"
72
73 #ifdef HAVE_CONFIG_H
74 #include <config.h>
75 #endif
76
77 #ifndef M_PI
78 #define M_PI            3.14159265358979323846
79 #endif
80
81
82 /* ******************* Constraint Channels ********************** */
83 /* Constraint Channels exist in one of two places:
84  *      - Under Action Channels in an Action  (act->chanbase->achan->constraintChannels)
85  *      - Under Object without Object-level Action yet (ob->constraintChannels)
86  * 
87  * The main purpose that Constraint Channels serve is to act as a link
88  * between an IPO-block (which provides values to interpolate between for some settings)
89  */
90
91 /* ------------ Data Management ----------- */
92  
93 /* Free constraint channels, and reduce the number of users of the related ipo-blocks */
94 void free_constraint_channels (ListBase *chanbase)
95 {
96         bConstraintChannel *chan;
97         
98         for (chan=chanbase->first; chan; chan=chan->next) {
99                 if (chan->ipo) {
100                         chan->ipo->id.us--;
101                 }
102         }
103         
104         BLI_freelistN(chanbase);
105 }
106
107 /* Make a copy of the constraint channels from dst to src, and also give the
108  * new constraint channels their own copy of the original's IPO.
109  */
110 void copy_constraint_channels (ListBase *dst, ListBase *src)
111 {
112         bConstraintChannel *dchan, *schan;
113         
114         dst->first = dst->last = NULL;
115         duplicatelist(dst, src);
116         
117         for (dchan=dst->first, schan=src->first; dchan; dchan=dchan->next, schan=schan->next) {
118                 dchan->ipo = copy_ipo(schan->ipo);
119         }
120 }
121
122 /* Make a copy of the constraint channels from dst to src, but make the
123  * new constraint channels use the same IPO-data as their twin.
124  */
125 void clone_constraint_channels (ListBase *dst, ListBase *src)
126 {
127         bConstraintChannel *dchan, *schan;
128         
129         dst->first = dst->last = NULL;
130         duplicatelist(dst, src);
131         
132         for (dchan=dst->first, schan=src->first; dchan; dchan=dchan->next, schan=schan->next) {
133                 id_us_plus((ID *)dchan->ipo);
134         }
135 }
136
137 /* ------------- Constraint Channel Tools ------------ */
138
139 /* Find the constraint channel with a given name */
140 bConstraintChannel *get_constraint_channel (ListBase *list, const char name[])
141 {
142         bConstraintChannel *chan;
143
144         if (list) {
145                 for (chan = list->first; chan; chan=chan->next) {
146                         if (!strcmp(name, chan->name)) {
147                                 return chan;
148                         }
149                 }
150         }
151         
152         return NULL;
153 }
154
155 /* Find or create a new constraint channel */
156 bConstraintChannel *verify_constraint_channel (ListBase *list, const char name[])
157 {
158         bConstraintChannel *chan;
159         
160         chan= get_constraint_channel(list, name);
161         
162         if (chan == NULL) {
163                 chan= MEM_callocN(sizeof(bConstraintChannel), "new constraint channel");
164                 BLI_addtail(list, chan);
165                 strcpy(chan->name, name);
166         }
167         
168         return chan;
169 }
170
171 /* --------- Constraint Channel Evaluation/Execution --------- */
172
173 /* IPO-system call: calculate IPO-block for constraint channels, and flush that
174  * info onto the corresponding constraint.
175  */
176 void do_constraint_channels (ListBase *conbase, ListBase *chanbase, float ctime, short onlydrivers)
177 {
178         bConstraint *con;
179         
180         /* for each Constraint, calculate its Influence from the corresponding ConstraintChannel */
181         for (con=conbase->first; con; con=con->next) {
182                 Ipo *ipo= NULL;
183                 
184                 if (con->flag & CONSTRAINT_OWN_IPO)
185                         ipo= con->ipo;
186                 else {
187                         bConstraintChannel *chan = get_constraint_channel(chanbase, con->name);
188                         if (chan) ipo= chan->ipo;
189                 }
190                 
191                 if (ipo) {
192                         IpoCurve *icu;
193                         
194                         calc_ipo(ipo, ctime);
195                         
196                         for (icu=ipo->curve.first; icu; icu=icu->next) {
197                                 if (!onlydrivers || icu->driver) {
198                                         switch (icu->adrcode) {
199                                                 case CO_ENFORCE:
200                                                 {
201                                                         /* Influence is clamped to 0.0f -> 1.0f range */
202                                                         con->enforce = CLAMPIS(icu->curval, 0.0f, 1.0f);
203                                                 }
204                                                         break;
205                                                 case CO_HEADTAIL:
206                                                 {
207                                                         /* we need to check types of constraints that can get this here, as user
208                                                          * may have created an IPO-curve for this from IPO-editor but for a constraint
209                                                          * that cannot support this
210                                                          */
211                                                         switch (con->type) {
212                                                                 /* supported constraints go here... */
213                                                                 case CONSTRAINT_TYPE_LOCLIKE:
214                                                                 case CONSTRAINT_TYPE_TRACKTO:
215                                                                 case CONSTRAINT_TYPE_MINMAX:
216                                                                 case CONSTRAINT_TYPE_STRETCHTO:
217                                                                 case CONSTRAINT_TYPE_DISTLIMIT:
218                                                                         con->headtail = icu->curval;
219                                                                         break;
220                                                                         
221                                                                 default:
222                                                                         /* not supported */
223                                                                         break;
224                                                         }
225                                                 }
226                                                         break;
227                                         }
228                                 }
229                         }
230                 }
231         }
232 }
233
234 /* ************************ Constraints - General Utilities *************************** */
235 /* These functions here don't act on any specific constraints, and are therefore should/will
236  * not require any of the special function-pointers afforded by the relevant constraint 
237  * type-info structs.
238  */
239
240 /* -------------- Naming -------------- */
241
242 /* Find the first available, non-duplicate name for a given constraint */
243 void unique_constraint_name (bConstraint *con, ListBase *list)
244 {
245         BLI_uniquename(list, con, "Const", offsetof(bConstraint, name), 32);
246 }
247
248 /* ----------------- Evaluation Loop Preparation --------------- */
249
250 /* package an object/bone for use in constraint evaluation */
251 /* This function MEM_calloc's a bConstraintOb struct, that will need to be freed after evaluation */
252 bConstraintOb *constraints_make_evalob (Object *ob, void *subdata, short datatype)
253 {
254         bConstraintOb *cob;
255         
256         /* create regardless of whether we have any data! */
257         cob= MEM_callocN(sizeof(bConstraintOb), "bConstraintOb");
258         
259         /* based on type of available data */
260         switch (datatype) {
261                 case CONSTRAINT_OBTYPE_OBJECT:
262                 {
263                         /* disregard subdata... calloc should set other values right */
264                         if (ob) {
265                                 cob->ob = ob;
266                                 cob->type = datatype;
267                                 Mat4CpyMat4(cob->matrix, ob->obmat);
268                         }
269                         else
270                                 Mat4One(cob->matrix);
271                         
272                         Mat4CpyMat4(cob->startmat, cob->matrix);
273                 }
274                         break;
275                 case CONSTRAINT_OBTYPE_BONE:
276                 {
277                         /* only set if we have valid bone, otherwise default */
278                         if (ob && subdata) {
279                                 cob->ob = ob;
280                                 cob->pchan = (bPoseChannel *)subdata;
281                                 cob->type = datatype;
282                                 
283                                 /* matrix in world-space */
284                                 Mat4MulMat4(cob->matrix, cob->pchan->pose_mat, ob->obmat);
285                         }
286                         else
287                                 Mat4One(cob->matrix);
288                                 
289                         Mat4CpyMat4(cob->startmat, cob->matrix);
290                 }
291                         break;
292                         
293                 default: /* other types not yet handled */
294                         Mat4One(cob->matrix);
295                         Mat4One(cob->startmat);
296                         break;
297         }
298         
299         return cob;
300 }
301
302 /* cleanup after constraint evaluation */
303 void constraints_clear_evalob (bConstraintOb *cob)
304 {
305         float delta[4][4], imat[4][4];
306         
307         /* prevent crashes */
308         if (cob == NULL) 
309                 return;
310         
311         /* calculate delta of constraints evaluation */
312         Mat4Invert(imat, cob->startmat);
313         Mat4MulMat4(delta, imat, cob->matrix);
314         
315         /* copy matrices back to source */
316         switch (cob->type) {
317                 case CONSTRAINT_OBTYPE_OBJECT:
318                 {
319                         /* cob->ob might not exist! */
320                         if (cob->ob) {
321                                 /* copy new ob-matrix back to owner */
322                                 Mat4CpyMat4(cob->ob->obmat, cob->matrix);
323                                 
324                                 /* copy inverse of delta back to owner */
325                                 Mat4Invert(cob->ob->constinv, delta);
326                         }
327                 }
328                         break;
329                 case CONSTRAINT_OBTYPE_BONE:
330                 {
331                         /* cob->ob or cob->pchan might not exist */
332                         if (cob->ob && cob->pchan) {
333                                 /* copy new pose-matrix back to owner */
334                                 Mat4MulMat4(cob->pchan->pose_mat, cob->matrix, cob->ob->imat);
335                                 
336                                 /* copy inverse of delta back to owner */
337                                 Mat4Invert(cob->pchan->constinv, delta);
338                         }
339                 }
340                         break;
341         }
342         
343         /* free tempolary struct */
344         MEM_freeN(cob);
345 }
346
347 /* -------------- Space-Conversion API -------------- */
348
349 /* This function is responsible for the correct transformations/conversions 
350  * of a matrix from one space to another for constraint evaluation.
351  * For now, this is only implemented for Objects and PoseChannels.
352  */
353 void constraint_mat_convertspace (Object *ob, bPoseChannel *pchan, float mat[][4], short from, short to)
354 {
355         float tempmat[4][4];
356         float diff_mat[4][4];
357         float imat[4][4];
358         
359         /* prevent crashes in these unlikely events  */
360         if (ob==NULL || mat==NULL) return;
361         /* optimise trick - check if need to do anything */
362         if (from == to) return;
363         
364         /* are we dealing with pose-channels or objects */
365         if (pchan) {
366                 /* pose channels */
367                 switch (from) {
368                         case CONSTRAINT_SPACE_WORLD: /* ---------- FROM WORLDSPACE ---------- */
369                         {
370                                 /* world to pose */
371                                 Mat4Invert(imat, ob->obmat);
372                                 Mat4CpyMat4(tempmat, mat);
373                                 Mat4MulMat4(mat, tempmat, imat);
374                                 
375                                 /* use pose-space as stepping stone for other spaces... */
376                                 if (ELEM(to, CONSTRAINT_SPACE_LOCAL, CONSTRAINT_SPACE_PARLOCAL)) {
377                                         /* call self with slightly different values */
378                                         constraint_mat_convertspace(ob, pchan, mat, CONSTRAINT_SPACE_POSE, to);
379                                 }
380                         }
381                                 break;
382                         case CONSTRAINT_SPACE_POSE:     /* ---------- FROM POSESPACE ---------- */
383                         {
384                                 /* pose to world */
385                                 if (to == CONSTRAINT_SPACE_WORLD) {
386                                         Mat4CpyMat4(tempmat, mat);
387                                         Mat4MulMat4(mat, tempmat, ob->obmat);
388                                 }
389                                 /* pose to local */
390                                 else if (to == CONSTRAINT_SPACE_LOCAL) {
391                                         if (pchan->bone) {
392                                                 if (pchan->parent) {
393                                                         float offs_bone[4][4];
394                                                                 
395                                                         /* construct offs_bone the same way it is done in armature.c */
396                                                         Mat4CpyMat3(offs_bone, pchan->bone->bone_mat);
397                                                         VECCOPY(offs_bone[3], pchan->bone->head);
398                                                         offs_bone[3][1]+= pchan->bone->parent->length;
399                                                         
400                                                         if (pchan->bone->flag & BONE_HINGE) {
401                                                                 /* pose_mat = par_pose-space_location * chan_mat */
402                                                                 float tmat[4][4];
403                                                                 
404                                                                 /* the rotation of the parent restposition */
405                                                                 Mat4CpyMat4(tmat, pchan->bone->parent->arm_mat);
406                                                                 
407                                                                 /* the location of actual parent transform */
408                                                                 VECCOPY(tmat[3], offs_bone[3]);
409                                                                 offs_bone[3][0]= offs_bone[3][1]= offs_bone[3][2]= 0.0f;
410                                                                 Mat4MulVecfl(pchan->parent->pose_mat, tmat[3]);
411                                                                 
412                                                                 Mat4MulMat4(diff_mat, offs_bone, tmat);
413                                                                 Mat4Invert(imat, diff_mat);
414                                                         }
415                                                         else {
416                                                                 /* pose_mat = par_pose_mat * bone_mat * chan_mat */
417                                                                 Mat4MulMat4(diff_mat, offs_bone, pchan->parent->pose_mat);
418                                                                 Mat4Invert(imat, diff_mat);
419                                                         }
420                                                 }
421                                                 else {
422                                                         /* pose_mat = chan_mat * arm_mat */
423                                                         Mat4Invert(imat, pchan->bone->arm_mat);
424                                                 }
425                                                 
426                                                 Mat4CpyMat4(tempmat, mat);
427                                                 Mat4MulMat4(mat, tempmat, imat);
428                                         }
429                                 }
430                                 /* pose to local with parent */
431                                 else if (to == CONSTRAINT_SPACE_PARLOCAL) {
432                                         if (pchan->bone) {
433                                                 Mat4Invert(imat, pchan->bone->arm_mat);
434                                                 Mat4CpyMat4(tempmat, mat);
435                                                 Mat4MulMat4(mat, tempmat, imat);
436                                         }
437                                 }
438                         }
439                                 break;
440                         case CONSTRAINT_SPACE_LOCAL: /* ------------ FROM LOCALSPACE --------- */
441                         {
442                                 /* local to pose - do inverse procedure that was done for pose to local */
443                                 if (pchan->bone) {
444                                         /* we need the posespace_matrix = local_matrix + (parent_posespace_matrix + restpos) */                                         
445                                         if (pchan->parent) {
446                                                 float offs_bone[4][4];
447                                                 
448                                                 /* construct offs_bone the same way it is done in armature.c */
449                                                 Mat4CpyMat3(offs_bone, pchan->bone->bone_mat);
450                                                 VECCOPY(offs_bone[3], pchan->bone->head);
451                                                 offs_bone[3][1]+= pchan->bone->parent->length;
452                                                 
453                                                 if (pchan->bone->flag & BONE_HINGE) {
454                                                         /* pose_mat = par_pose-space_location * chan_mat */
455                                                         float tmat[4][4];
456                                                         
457                                                         /* the rotation of the parent restposition */
458                                                         Mat4CpyMat4(tmat, pchan->bone->parent->arm_mat);
459                                                         
460                                                         /* the location of actual parent transform */
461                                                         VECCOPY(tmat[3], offs_bone[3]);
462                                                         offs_bone[3][0]= offs_bone[3][1]= offs_bone[3][2]= 0.0f;
463                                                         Mat4MulVecfl(pchan->parent->pose_mat, tmat[3]);
464                                                         
465                                                         Mat4MulMat4(diff_mat, offs_bone, tmat);
466                                                         Mat4CpyMat4(tempmat, mat);
467                                                         Mat4MulMat4(mat, tempmat, diff_mat);
468                                                 }
469                                                 else {
470                                                         /* pose_mat = par_pose_mat * bone_mat * chan_mat */
471                                                         Mat4MulMat4(diff_mat, offs_bone, pchan->parent->pose_mat);
472                                                         Mat4CpyMat4(tempmat, mat);
473                                                         Mat4MulMat4(mat, tempmat, diff_mat);
474                                                 }
475                                         }
476                                         else {
477                                                 Mat4CpyMat4(diff_mat, pchan->bone->arm_mat);
478                                                 
479                                                 Mat4CpyMat4(tempmat, mat);
480                                                 Mat4MulMat4(mat, tempmat, diff_mat);
481                                         }
482                                 }
483                                 
484                                 /* use pose-space as stepping stone for other spaces */
485                                 if (ELEM(to, CONSTRAINT_SPACE_WORLD, CONSTRAINT_SPACE_PARLOCAL)) {
486                                         /* call self with slightly different values */
487                                         constraint_mat_convertspace(ob, pchan, mat, CONSTRAINT_SPACE_POSE, to);
488                                 }                               
489                         }
490                                 break;
491                         case CONSTRAINT_SPACE_PARLOCAL: /* -------------- FROM LOCAL WITH PARENT ---------- */
492                         {
493                                 /* local + parent to pose */
494                                 if (pchan->bone) {                                      
495                                         Mat4CpyMat4(diff_mat, pchan->bone->arm_mat);
496                                         Mat4CpyMat4(tempmat, mat);
497                                         Mat4MulMat4(mat, diff_mat, tempmat);
498                                 }
499                                 
500                                 /* use pose-space as stepping stone for other spaces */
501                                 if (ELEM(to, CONSTRAINT_SPACE_WORLD, CONSTRAINT_SPACE_LOCAL)) {
502                                         /* call self with slightly different values */
503                                         constraint_mat_convertspace(ob, pchan, mat, CONSTRAINT_SPACE_POSE, to);
504                                 }
505                         }
506                                 break;
507                 }
508         }
509         else {
510                 /* objects */
511                 if (from==CONSTRAINT_SPACE_WORLD && to==CONSTRAINT_SPACE_LOCAL) {
512                         /* check if object has a parent - otherwise this won't work */
513                         if (ob->parent) {
514                                 /* 'subtract' parent's effects from owner */
515                                 Mat4MulMat4(diff_mat, ob->parentinv, ob->parent->obmat);
516                                 Mat4Invert(imat, diff_mat);
517                                 Mat4CpyMat4(tempmat, mat);
518                                 Mat4MulMat4(mat, tempmat, imat);
519                         }
520                 }
521                 else if (from==CONSTRAINT_SPACE_LOCAL && to==CONSTRAINT_SPACE_WORLD) {
522                         /* check that object has a parent - otherwise this won't work */
523                         if (ob->parent) {
524                                 /* 'add' parent's effect back to owner */
525                                 Mat4CpyMat4(tempmat, mat);
526                                 Mat4MulMat4(diff_mat, ob->parentinv, ob->parent->obmat);
527                                 Mat4MulMat4(mat, tempmat, diff_mat);
528                         }
529                 }
530         }
531 }
532
533 /* ------------ General Target Matrix Tools ---------- */
534
535 /* function that sets the given matrix based on given vertex group in mesh */
536 static void contarget_get_mesh_mat (Object *ob, char *substring, float mat[][4])
537 {
538         DerivedMesh *dm;
539         float vec[3] = {0.0f, 0.0f, 0.0f}, tvec[3];
540         float normal[3] = {0.0f, 0.0f, 0.0f}, plane[3];
541         float imat[3][3], tmat[3][3];
542         int dgroup;
543         
544         /* initialize target matrix using target matrix */
545         Mat4CpyMat4(mat, ob->obmat);
546         
547         /* get index of vertex group */
548         dgroup = get_named_vertexgroup_num(ob, substring);
549         if (dgroup < 0) return;
550         
551         /* get DerivedMesh */
552         if ((G.obedit == ob) && (G.editMesh)) {
553                 /* target is in editmode, so get a special derived mesh */
554                 dm = CDDM_from_editmesh(G.editMesh, ob->data);
555         }
556         else {
557                 /* when not in EditMode, this should exist */
558                 dm = (DerivedMesh *)ob->derivedFinal;
559         }
560         
561         /* only continue if there's a valid DerivedMesh */
562         if (dm) {
563                 MDeformVert *dvert = dm->getVertDataArray(dm, CD_MDEFORMVERT);
564                 int *index = (int *)dm->getVertDataArray(dm, CD_ORIGINDEX);
565                 int numVerts = dm->getNumVerts(dm);
566                 int i, j, count = 0;
567                 float co[3], nor[3];
568                 
569                 /* check that dvert and index are valid pointers (just in case) */
570                 if (dvert && index) {
571                         /* get the average of all verts with that are in the vertex-group */
572                         for (i = 0; i < numVerts; i++, index++) {       
573                                 for (j = 0; j < dvert[i].totweight; j++) {
574                                         /* does this vertex belong to nominated vertex group? */
575                                         if (dvert[i].dw[j].def_nr == dgroup) {
576                                                 dm->getVertCo(dm, i, co);
577                                                 dm->getVertNo(dm, i, nor);
578                                                 VecAddf(vec, vec, co);
579                                                 VecAddf(normal, normal, nor);
580                                                 count++;
581                                                 break;
582                                         }
583                                         
584                                 }
585                         }
586                         
587                         
588                         /* calculate averages of normal and coordinates */
589                         if (count > 0) {
590                                 VecMulf(vec, 1.0f / count);
591                                 VecMulf(normal, 1.0f / count);
592                         }
593                         
594                         
595                         /* derive the rotation from the average normal: 
596                          *              - code taken from transform_manipulator.c, 
597                          *                      calc_manipulator_stats, V3D_MANIP_NORMAL case
598                          */
599                         /*      we need the transpose of the inverse for a normal... */
600                         Mat3CpyMat4(imat, ob->obmat);
601                         
602                         Mat3Inv(tmat, imat);
603                         Mat3Transp(tmat);
604                         Mat3MulVecfl(tmat, normal);
605                         
606                         Normalize(normal);
607                         VECCOPY(plane, tmat[1]);
608                         
609                         VECCOPY(tmat[2], normal);
610                         Crossf(tmat[0], normal, plane);
611                         Crossf(tmat[1], tmat[2], tmat[0]);
612                         
613                         Mat4CpyMat3(mat, tmat);
614                         Mat4Ortho(mat);
615                         
616                         
617                         /* apply the average coordinate as the new location */
618                         VecMat4MulVecfl(tvec, ob->obmat, vec);
619                         VECCOPY(mat[3], tvec);
620                 }
621         }
622         
623         /* free temporary DerivedMesh created (in EditMode case) */
624         if (G.editMesh) {
625                 if (dm) dm->release(dm);
626         }
627 }
628
629 /* function that sets the given matrix based on given vertex group in lattice */
630 static void contarget_get_lattice_mat (Object *ob, char *substring, float mat[][4])
631 {
632         Lattice *lt= (Lattice *)ob->data;
633         
634         DispList *dl = find_displist(&ob->disp, DL_VERTS);
635         float *co = dl?dl->verts:NULL;
636         BPoint *bp = lt->def;
637         
638         MDeformVert *dvert = lt->dvert;
639         int tot_verts= lt->pntsu*lt->pntsv*lt->pntsw;
640         float vec[3]= {0.0f, 0.0f, 0.0f}, tvec[3];
641         int dgroup=0, grouped=0;
642         int i, n;
643         
644         /* initialize target matrix using target matrix */
645         Mat4CpyMat4(mat, ob->obmat);
646         
647         /* get index of vertex group */
648         dgroup = get_named_vertexgroup_num(ob, substring);
649         if (dgroup < 0) return;
650         if (dvert == NULL) return;
651         
652         /* 1. Loop through control-points checking if in nominated vertex-group.
653          * 2. If it is, add it to vec to find the average point.
654          */
655         for (i=0; i < tot_verts; i++, dvert++) {
656                 for (n= 0; n < dvert->totweight; n++) {
657                         /* found match - vert is in vgroup */
658                         if (dvert->dw[n].def_nr == dgroup) {
659                                 /* copy coordinates of point to temporary vector, then add to find average */
660                                 if (co)
661                                         memcpy(tvec, co, 3*sizeof(float));
662                                 else
663                                         memcpy(tvec, bp->vec, 3*sizeof(float));
664                                         
665                                 VecAddf(vec, vec, tvec);
666                                 grouped++;
667                                 
668                                 break;
669                         }
670                 }
671                 
672                 /* advance pointer to coordinate data */
673                 if (co) co+= 3;
674                 else bp++;
675         }
676         
677         /* find average location, then multiply by ob->obmat to find world-space location */
678         if (grouped)
679                 VecMulf(vec, 1.0f / grouped);
680         VecMat4MulVecfl(tvec, ob->obmat, vec);
681         
682         /* copy new location to matrix */
683         VECCOPY(mat[3], tvec);
684 }
685
686 /* generic function to get the appropriate matrix for most target cases */
687 /* The cases where the target can be object data have not been implemented */
688 static void constraint_target_to_mat4 (Object *ob, char *substring, float mat[][4], short from, short to, float headtail)
689 {
690         /*      Case OBJECT */
691         if (!strlen(substring)) {
692                 Mat4CpyMat4(mat, ob->obmat);
693                 constraint_mat_convertspace(ob, NULL, mat, from, to);
694         }
695         /*      Case VERTEXGROUP */
696         /* Current method just takes the average location of all the points in the
697          * VertexGroup, and uses that as the location value of the targets. Where 
698          * possible, the orientation will also be calculated, by calculating an
699          * 'average' vertex normal, and deriving the rotaation from that.
700          *
701          * NOTE: EditMode is not currently supported, and will most likely remain that
702          *              way as constraints can only really affect things on object/bone level.
703          */
704         else if (ob->type == OB_MESH) {
705                 contarget_get_mesh_mat(ob, substring, mat);
706                 constraint_mat_convertspace(ob, NULL, mat, from, to);
707         }
708         else if (ob->type == OB_LATTICE) {
709                 contarget_get_lattice_mat(ob, substring, mat);
710                 constraint_mat_convertspace(ob, NULL, mat, from, to);
711         }
712         /*      Case BONE */
713         else {
714                 bPoseChannel *pchan;
715                 
716                 pchan = get_pose_channel(ob->pose, substring);
717                 if (pchan) {
718                         /* Multiply the PoseSpace accumulation/final matrix for this
719                          * PoseChannel by the Armature Object's Matrix to get a worldspace
720                          * matrix.
721                          */
722                         if (headtail < 0.000001) {
723                                 /* skip length interpolation if set to head */
724                                 Mat4MulMat4(mat, pchan->pose_mat, ob->obmat);
725                         }
726                         else {
727                                 float tempmat[4][4], loc[3];
728                                 
729                                 /* interpolate along length of bone */
730                                 VecLerpf(loc, pchan->pose_head, pchan->pose_tail, headtail);    
731                                 
732                                 /* use interpolated distance for subtarget */
733                                 Mat4CpyMat4(tempmat, pchan->pose_mat);  
734                                 VecCopyf(tempmat[3], loc);
735                                 
736                                 Mat4MulMat4(mat, tempmat, ob->obmat);
737                         }
738                 } 
739                 else
740                         Mat4CpyMat4(mat, ob->obmat);
741                         
742                 /* convert matrix space as required */
743                 constraint_mat_convertspace(ob, pchan, mat, from, to);
744         }
745 }
746
747 /* ************************* Specific Constraints ***************************** */
748 /* Each constraint defines a set of functions, which will be called at the appropriate
749  * times. In addition to this, each constraint should have a type-info struct, where
750  * its functions are attached for use. 
751  */
752  
753 /* Template for type-info data:
754  *      - make a copy of this when creating new constraints, and just change the functions
755  *        pointed to as necessary
756  *      - although the naming of functions doesn't matter, it would help for code
757  *        readability, to follow the same naming convention as is presented here
758  *      - any functions that a constraint doesn't need to define, don't define
759  *        for such cases, just use NULL 
760  *      - these should be defined after all the functions have been defined, so that
761  *        forward-definitions/prototypes don't need to be used!
762  *      - keep this copy #if-def'd so that future constraints can get based off this
763  */
764 #if 0
765 static bConstraintTypeInfo CTI_CONSTRNAME = {
766         CONSTRAINT_TYPE_CONSTRNAME, /* type */
767         sizeof(bConstrNameConstraint), /* size */
768         "ConstrName", /* name */
769         "bConstrNameConstraint", /* struct name */
770         constrname_free, /* free data */
771         constrname_relink, /* relink data */
772         constrname_copy, /* copy data */
773         constrname_new_data, /* new data */
774         constrname_get_tars, /* get constraint targets */
775         constrname_flush_tars, /* flush constraint targets */
776         constrname_get_tarmat, /* get target matrix */
777         constrname_evaluate /* evaluate */
778 };
779 #endif
780
781 /* This function should be used for the get_target_matrix member of all 
782  * constraints that are not picky about what happens to their target matrix.
783  */
784 static void default_get_tarmat (bConstraint *con, bConstraintOb *cob, bConstraintTarget *ct, float ctime)
785 {
786         if (VALID_CONS_TARGET(ct))
787                 constraint_target_to_mat4(ct->tar, ct->subtarget, ct->matrix, CONSTRAINT_SPACE_WORLD, ct->space, con->headtail);
788         else if (ct)
789                 Mat4One(ct->matrix);
790 }
791
792 /* This following macro should be used for all standard single-target *_get_tars functions 
793  * to save typing and reduce maintainance woes.
794  * (Hopefully all compilers will be happy with the lines with just a space on them. Those are
795  *  really just to help this code easier to read)
796  */
797 #define SINGLETARGET_GET_TARS(con, datatar, datasubtarget, ct, list) \
798         { \
799                 ct= MEM_callocN(sizeof(bConstraintTarget), "tempConstraintTarget"); \
800                  \
801                 ct->tar= datatar; \
802                 strcpy(ct->subtarget, datasubtarget); \
803                 ct->space= con->tarspace; \
804                 ct->flag= CONSTRAINT_TAR_TEMP; \
805                  \
806                 if (ct->tar) { \
807                         if ((ct->tar->type==OB_ARMATURE) && (ct->subtarget[0])) ct->type = CONSTRAINT_OBTYPE_BONE; \
808                         else if (ELEM(ct->tar->type, OB_MESH, OB_LATTICE) && (ct->subtarget[0])) ct->type = CONSTRAINT_OBTYPE_VERT; \
809                         else ct->type = CONSTRAINT_OBTYPE_OBJECT; \
810                 } \
811                  \
812                 BLI_addtail(list, ct); \
813         }
814         
815 /* This following macro should be used for all standard single-target *_get_tars functions 
816  * to save typing and reduce maintainance woes. It does not do the subtarget related operations
817  * (Hopefully all compilers will be happy with the lines with just a space on them. Those are
818  *  really just to help this code easier to read)
819  */
820 #define SINGLETARGETNS_GET_TARS(con, datatar, ct, list) \
821         { \
822                 ct= MEM_callocN(sizeof(bConstraintTarget), "tempConstraintTarget"); \
823                  \
824                 ct->tar= datatar; \
825                 ct->space= con->tarspace; \
826                 ct->flag= CONSTRAINT_TAR_TEMP; \
827                  \
828                 if (ct->tar) ct->type = CONSTRAINT_OBTYPE_OBJECT; \
829                  \
830                 BLI_addtail(list, ct); \
831         }
832
833 /* This following macro should be used for all standard single-target *_flush_tars functions
834  * to save typing and reduce maintainance woes.
835  * Note: the pointer to ct will be changed to point to the next in the list (as it gets removed)
836  * (Hopefully all compilers will be happy with the lines with just a space on them. Those are
837  *  really just to help this code easier to read)
838  */
839 #define SINGLETARGET_FLUSH_TARS(con, datatar, datasubtarget, ct, list, nocopy) \
840         { \
841                 if (ct) { \
842                         bConstraintTarget *ctn = ct->next; \
843                         if (nocopy == 0) { \
844                                 datatar= ct->tar; \
845                                 strcpy(datasubtarget, ct->subtarget); \
846                                 con->tarspace= (char)ct->space; \
847                         } \
848                          \
849                         BLI_freelinkN(list, ct); \
850                         ct= ctn; \
851                 } \
852         }
853         
854 /* This following macro should be used for all standard single-target *_flush_tars functions
855  * to save typing and reduce maintainance woes. It does not do the subtarget related operations.
856  * Note: the pointer to ct will be changed to point to the next in the list (as it gets removed)
857  * (Hopefully all compilers will be happy with the lines with just a space on them. Those are
858  *  really just to help this code easier to read)
859  */
860 #define SINGLETARGETNS_FLUSH_TARS(con, datatar, ct, list, nocopy) \
861         { \
862                 if (ct) { \
863                         bConstraintTarget *ctn = ct->next; \
864                         if (nocopy == 0) { \
865                                 datatar= ct->tar; \
866                                 con->tarspace= (char)ct->space; \
867                         } \
868                          \
869                         BLI_freelinkN(list, ct); \
870                         ct= ctn; \
871                 } \
872         }
873  
874 /* --------- ChildOf Constraint ------------ */
875
876 static void childof_new_data (void *cdata)
877 {
878         bChildOfConstraint *data= (bChildOfConstraint *)cdata;
879         
880         data->flag = (CHILDOF_LOCX | CHILDOF_LOCY | CHILDOF_LOCZ |
881                                         CHILDOF_ROTX |CHILDOF_ROTY | CHILDOF_ROTZ |
882                                         CHILDOF_SIZEX | CHILDOF_SIZEY | CHILDOF_SIZEZ);
883         Mat4One(data->invmat);
884 }
885
886 static int childof_get_tars (bConstraint *con, ListBase *list)
887 {
888         if (con && list) {
889                 bChildOfConstraint *data= con->data;
890                 bConstraintTarget *ct;
891                 
892                 /* standard target-getting macro for single-target constraints */
893                 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list)
894                 
895                 return 1;
896         }
897         
898         return 0;
899 }
900
901 static void childof_flush_tars (bConstraint *con, ListBase *list, short nocopy)
902 {
903         if (con && list) {
904                 bChildOfConstraint *data= con->data;
905                 bConstraintTarget *ct= list->first;
906                 
907                 /* the following macro is used for all standard single-target constraints */
908                 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, nocopy)
909         }
910 }
911
912 static void childof_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
913 {
914         bChildOfConstraint *data= con->data;
915         bConstraintTarget *ct= targets->first;
916         
917         /* only evaluate if there is a target */
918         if (VALID_CONS_TARGET(ct)) {
919                 float parmat[4][4], invmat[4][4], tempmat[4][4];
920                 float loc[3], eul[3], size[3];
921                 float loco[3], eulo[3], sizo[3];
922                 
923                 /* get offset (parent-inverse) matrix */
924                 Mat4CpyMat4(invmat, data->invmat);
925                 
926                 /* extract components of both matrices */
927                 VECCOPY(loc, ct->matrix[3]);
928                 Mat4ToEul(ct->matrix, eul);
929                 Mat4ToSize(ct->matrix, size);
930                 
931                 VECCOPY(loco, invmat[3]);
932                 Mat4ToEul(invmat, eulo);
933                 Mat4ToSize(invmat, sizo);
934                 
935                 /* disable channels not enabled */
936                 if (!(data->flag & CHILDOF_LOCX)) loc[0]= loco[0]= 0.0f;
937                 if (!(data->flag & CHILDOF_LOCY)) loc[1]= loco[1]= 0.0f;
938                 if (!(data->flag & CHILDOF_LOCZ)) loc[2]= loco[2]= 0.0f;
939                 if (!(data->flag & CHILDOF_ROTX)) eul[0]= eulo[0]= 0.0f;
940                 if (!(data->flag & CHILDOF_ROTY)) eul[1]= eulo[1]= 0.0f;
941                 if (!(data->flag & CHILDOF_ROTZ)) eul[2]= eulo[2]= 0.0f;
942                 if (!(data->flag & CHILDOF_SIZEX)) size[0]= sizo[0]= 1.0f;
943                 if (!(data->flag & CHILDOF_SIZEY)) size[1]= sizo[1]= 1.0f;
944                 if (!(data->flag & CHILDOF_SIZEZ)) size[2]= sizo[2]= 1.0f;
945                 
946                 /* make new target mat and offset mat */
947                 LocEulSizeToMat4(ct->matrix, loc, eul, size);
948                 LocEulSizeToMat4(invmat, loco, eulo, sizo);
949                 
950                 /* multiply target (parent matrix) by offset (parent inverse) to get 
951                  * the effect of the parent that will be exherted on the owner
952                  */
953                 Mat4MulMat4(parmat, invmat, ct->matrix);
954                 
955                 /* now multiply the parent matrix by the owner matrix to get the 
956                  * the effect of this constraint (i.e.  owner is 'parented' to parent)
957                  */
958                 Mat4CpyMat4(tempmat, cob->matrix);
959                 Mat4MulMat4(cob->matrix, tempmat, parmat); 
960         }
961 }
962
963 static bConstraintTypeInfo CTI_CHILDOF = {
964         CONSTRAINT_TYPE_CHILDOF, /* type */
965         sizeof(bChildOfConstraint), /* size */
966         "ChildOf", /* name */
967         "bChildOfConstraint", /* struct name */
968         NULL, /* free data */
969         NULL, /* relink data */
970         NULL, /* copy data */
971         childof_new_data, /* new data */
972         childof_get_tars, /* get constraint targets */
973         childof_flush_tars, /* flush constraint targets */
974         default_get_tarmat, /* get a target matrix */
975         childof_evaluate /* evaluate */
976 };
977
978 /* -------- TrackTo Constraint ------- */
979
980 static void trackto_new_data (void *cdata)
981 {
982         bTrackToConstraint *data= (bTrackToConstraint *)cdata;
983         
984         data->reserved1 = TRACK_Y;
985         data->reserved2 = UP_Z;
986 }       
987
988 static int trackto_get_tars (bConstraint *con, ListBase *list)
989 {
990         if (con && list) {
991                 bTrackToConstraint *data= con->data;
992                 bConstraintTarget *ct;
993                 
994                 /* standard target-getting macro for single-target constraints */
995                 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list)
996                 
997                 return 1;
998         }
999         
1000         return 0;
1001 }
1002
1003 static void trackto_flush_tars (bConstraint *con, ListBase *list, short nocopy)
1004 {
1005         if (con && list) {
1006                 bTrackToConstraint *data= con->data;
1007                 bConstraintTarget *ct= list->first;
1008                 
1009                 /* the following macro is used for all standard single-target constraints */
1010                 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, nocopy)
1011         }
1012 }
1013
1014
1015 static int basis_cross (int n, int m)
1016 {
1017         switch (n-m) {
1018                 case 1: 
1019                 case -2:
1020                         return 1;
1021                         
1022                 case -1: 
1023                 case 2:
1024                         return -1;
1025                         
1026                 default:
1027                         return 0;
1028         }
1029 }
1030
1031 static void vectomat (float *vec, float *target_up, short axis, short upflag, short flags, float m[][3])
1032 {
1033         float n[3];
1034         float u[3]; /* vector specifying the up axis */
1035         float proj[3];
1036         float right[3];
1037         float neg = -1;
1038         int right_index;
1039         
1040         VecCopyf(n, vec);
1041         if (Normalize(n) == 0.0) { 
1042                 n[0] = 0.0;
1043                 n[1] = 0.0;
1044                 n[2] = 1.0;
1045         }
1046         if (axis > 2) axis -= 3;
1047         else VecMulf(n,-1);
1048
1049         /* n specifies the transformation of the track axis */
1050         if (flags & TARGET_Z_UP) { 
1051                 /* target Z axis is the global up axis */
1052                 u[0] = target_up[0];
1053                 u[1] = target_up[1];
1054                 u[2] = target_up[2];
1055         }
1056         else { 
1057                 /* world Z axis is the global up axis */
1058                 u[0] = 0;
1059                 u[1] = 0;
1060                 u[2] = 1;
1061         }
1062
1063         /* project the up vector onto the plane specified by n */
1064         Projf(proj, u, n); /* first u onto n... */
1065         VecSubf(proj, u, proj); /* then onto the plane */
1066         /* proj specifies the transformation of the up axis */
1067
1068         if (Normalize(proj) == 0.0) { /* degenerate projection */
1069                 proj[0] = 0.0;
1070                 proj[1] = 1.0;
1071                 proj[2] = 0.0;
1072         }
1073
1074         /* Normalized cross product of n and proj specifies transformation of the right axis */
1075         Crossf(right, proj, n);
1076         Normalize(right);
1077
1078         if (axis != upflag) {
1079                 right_index = 3 - axis - upflag;
1080                 neg = (float)basis_cross(axis, upflag);
1081                 
1082                 /* account for up direction, track direction */
1083                 m[right_index][0] = neg * right[0];
1084                 m[right_index][1] = neg * right[1];
1085                 m[right_index][2] = neg * right[2];
1086                 
1087                 m[upflag][0] = proj[0];
1088                 m[upflag][1] = proj[1];
1089                 m[upflag][2] = proj[2];
1090                 
1091                 m[axis][0] = n[0];
1092                 m[axis][1] = n[1];
1093                 m[axis][2] = n[2];
1094         }
1095         /* identity matrix - don't do anything if the two axes are the same */
1096         else {
1097                 m[0][0]= m[1][1]= m[2][2]= 1.0;
1098                 m[0][1]= m[0][2]= 0.0;
1099                 m[1][0]= m[1][2]= 0.0;
1100                 m[2][0]= m[2][1]= 0.0;
1101         }
1102 }
1103
1104
1105 static void trackto_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
1106 {
1107         bTrackToConstraint *data= con->data;
1108         bConstraintTarget *ct= targets->first;
1109         
1110         if (VALID_CONS_TARGET(ct)) {
1111                 float size[3], vec[3];
1112                 float totmat[3][3];
1113                 float tmat[4][4];
1114                 
1115                 /* Get size property, since ob->size is only the object's own relative size, not its global one */
1116                 Mat4ToSize(cob->matrix, size);
1117                 
1118                 /* Clear the object's rotation */       
1119                 cob->matrix[0][0]=size[0];
1120                 cob->matrix[0][1]=0;
1121                 cob->matrix[0][2]=0;
1122                 cob->matrix[1][0]=0;
1123                 cob->matrix[1][1]=size[1];
1124                 cob->matrix[1][2]=0;
1125                 cob->matrix[2][0]=0;
1126                 cob->matrix[2][1]=0;
1127                 cob->matrix[2][2]=size[2];
1128                 
1129                 /* targetmat[2] instead of ownermat[2] is passed to vectomat
1130                  * for backwards compatability it seems... (Aligorith)
1131                  */
1132                 VecSubf(vec, cob->matrix[3], ct->matrix[3]);
1133                 vectomat(vec, ct->matrix[2], 
1134                                 (short)data->reserved1, (short)data->reserved2, 
1135                                 data->flags, totmat);
1136                 
1137                 Mat4CpyMat4(tmat, cob->matrix);
1138                 Mat4MulMat34(cob->matrix, totmat, tmat);
1139         }
1140 }
1141
1142 static bConstraintTypeInfo CTI_TRACKTO = {
1143         CONSTRAINT_TYPE_TRACKTO, /* type */
1144         sizeof(bTrackToConstraint), /* size */
1145         "TrackTo", /* name */
1146         "bTrackToConstraint", /* struct name */
1147         NULL, /* free data */
1148         NULL, /* relink data */
1149         NULL, /* copy data */
1150         trackto_new_data, /* new data */
1151         trackto_get_tars, /* get constraint targets */
1152         trackto_flush_tars, /* flush constraint targets */
1153         default_get_tarmat, /* get target matrix */
1154         trackto_evaluate /* evaluate */
1155 };
1156
1157 /* --------- Inverse-Kinemetics --------- */
1158
1159 static void kinematic_new_data (void *cdata)
1160 {
1161         bKinematicConstraint *data= (bKinematicConstraint *)cdata;
1162         
1163         data->weight= (float)1.0;
1164         data->orientweight= (float)1.0;
1165         data->iterations = 500;
1166         data->flag= CONSTRAINT_IK_TIP|CONSTRAINT_IK_STRETCH|CONSTRAINT_IK_POS;
1167 }
1168
1169 static int kinematic_get_tars (bConstraint *con, ListBase *list)
1170 {
1171         if (con && list) {
1172                 bKinematicConstraint *data= con->data;
1173                 bConstraintTarget *ct;
1174                 
1175                 /* standard target-getting macro for single-target constraints is used twice here */
1176                 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list)
1177                 SINGLETARGET_GET_TARS(con, data->poletar, data->polesubtarget, ct, list)
1178                 
1179                 return 2;
1180         }
1181         
1182         return 0;
1183 }
1184
1185 static void kinematic_flush_tars (bConstraint *con, ListBase *list, short nocopy)
1186 {
1187         if (con && list) {
1188                 bKinematicConstraint *data= con->data;
1189                 bConstraintTarget *ct= list->first;
1190                 
1191                 /* the following macro is used for all standard single-target constraints */
1192                 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, nocopy)
1193                 SINGLETARGET_FLUSH_TARS(con, data->poletar, data->polesubtarget, ct, list, nocopy)
1194         }
1195 }
1196
1197 static void kinematic_get_tarmat (bConstraint *con, bConstraintOb *cob, bConstraintTarget *ct, float ctime)
1198 {
1199         bKinematicConstraint *data= con->data;
1200         
1201         if (VALID_CONS_TARGET(ct)) 
1202                 constraint_target_to_mat4(ct->tar, ct->subtarget, ct->matrix, CONSTRAINT_SPACE_WORLD, ct->space, con->headtail);
1203         else if (ct) {
1204                 if (data->flag & CONSTRAINT_IK_AUTO) {
1205                         Object *ob= cob->ob;
1206                         
1207                         if (ob == NULL) {
1208                                 Mat4One(ct->matrix);
1209                         }
1210                         else {
1211                                 float vec[3];
1212                                 /* move grabtarget into world space */
1213                                 VECCOPY(vec, data->grabtarget);
1214                                 Mat4MulVecfl(ob->obmat, vec);
1215                                 Mat4CpyMat4(ct->matrix, ob->obmat);
1216                                 VECCOPY(ct->matrix[3], vec);
1217                         }
1218                 }
1219                 else
1220                         Mat4One(ct->matrix);
1221         }
1222 }
1223
1224 static bConstraintTypeInfo CTI_KINEMATIC = {
1225         CONSTRAINT_TYPE_KINEMATIC, /* type */
1226         sizeof(bKinematicConstraint), /* size */
1227         "IK", /* name */
1228         "bKinematicConstraint", /* struct name */
1229         NULL, /* free data */
1230         NULL, /* relink data */
1231         NULL, /* copy data */
1232         kinematic_new_data, /* new data */
1233         kinematic_get_tars, /* get constraint targets */
1234         kinematic_flush_tars, /* flush constraint targets */
1235         kinematic_get_tarmat, /* get target matrix */
1236         NULL /* evaluate - solved as separate loop */
1237 };
1238
1239 /* -------- Follow-Path Constraint ---------- */
1240
1241 static void followpath_new_data (void *cdata)
1242 {
1243         bFollowPathConstraint *data= (bFollowPathConstraint *)cdata;
1244         
1245         data->trackflag = TRACK_Y;
1246         data->upflag = UP_Z;
1247         data->offset = 0;
1248         data->followflag = 0;
1249 }
1250
1251 static int followpath_get_tars (bConstraint *con, ListBase *list)
1252 {
1253         if (con && list) {
1254                 bFollowPathConstraint *data= con->data;
1255                 bConstraintTarget *ct;
1256                 
1257                 /* standard target-getting macro for single-target constraints without subtargets */
1258                 SINGLETARGETNS_GET_TARS(con, data->tar, ct, list)
1259                 
1260                 return 1;
1261         }
1262         
1263         return 0;
1264 }
1265
1266 static void followpath_flush_tars (bConstraint *con, ListBase *list, short nocopy)
1267 {
1268         if (con && list) {
1269                 bFollowPathConstraint *data= con->data;
1270                 bConstraintTarget *ct= list->first;
1271                 
1272                 /* the following macro is used for all standard single-target constraints */
1273                 SINGLETARGETNS_FLUSH_TARS(con, data->tar, ct, list, nocopy)
1274         }
1275 }
1276
1277 static void followpath_get_tarmat (bConstraint *con, bConstraintOb *cob, bConstraintTarget *ct, float ctime)
1278 {
1279         bFollowPathConstraint *data= con->data;
1280         
1281         if (VALID_CONS_TARGET(ct)) {
1282                 Curve *cu= ct->tar->data;
1283                 float q[4], vec[4], dir[3], quat[4], x1;
1284                 float totmat[4][4];
1285                 float curvetime;
1286                 
1287                 Mat4One(totmat);
1288                 Mat4One(ct->matrix);
1289                 
1290                 /* note: when creating constraints that follow path, the curve gets the CU_PATH set now,
1291                  *              currently for paths to work it needs to go through the bevlist/displist system (ton) 
1292                  */
1293                 
1294                 /* only happens on reload file, but violates depsgraph still... fix! */
1295                 if (cu->path==NULL || cu->path->data==NULL) 
1296                         makeDispListCurveTypes(ct->tar, 0);
1297                 
1298                 if (cu->path && cu->path->data) {
1299                         curvetime= bsystem_time(ct->tar, (float)ctime, 0.0) - data->offset;
1300                         
1301                         if (calc_ipo_spec(cu->ipo, CU_SPEED, &curvetime)==0) {
1302                                 curvetime /= cu->pathlen;
1303                                 CLAMP(curvetime, 0.0, 1.0);
1304                         }
1305                         
1306                         if ( where_on_path(ct->tar, curvetime, vec, dir) ) {
1307                                 if (data->followflag) {
1308                                         vectoquat(dir, (short) data->trackflag, (short) data->upflag, quat);
1309                                         
1310                                         Normalize(dir);
1311                                         q[0]= (float)cos(0.5*vec[3]);
1312                                         x1= (float)sin(0.5*vec[3]);
1313                                         q[1]= -x1*dir[0];
1314                                         q[2]= -x1*dir[1];
1315                                         q[3]= -x1*dir[2];
1316                                         QuatMul(quat, q, quat);
1317                                         
1318                                         QuatToMat4(quat, totmat);
1319                                 }
1320                                 VECCOPY(totmat[3], vec);
1321                                 
1322                                 Mat4MulSerie(ct->matrix, ct->tar->obmat, totmat, NULL, NULL, NULL, NULL, NULL, NULL);
1323                         }
1324                 }
1325         }
1326         else if (ct)
1327                 Mat4One(ct->matrix);
1328 }
1329
1330 static void followpath_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
1331 {
1332         bConstraintTarget *ct= targets->first;
1333         
1334         /* only evaluate if there is a target */
1335         if (VALID_CONS_TARGET(ct)) {
1336                 float obmat[4][4];
1337                 float size[3], obsize[3];
1338                 
1339                 /* get Object local transform (loc/rot/size) to determine transformation from path */
1340                 //object_to_mat4(ob, obmat);
1341                 Mat4CpyMat4(obmat, cob->matrix); // FIXME!!!
1342                 
1343                 /* get scaling of object before applying constraint */
1344                 Mat4ToSize(cob->matrix, size);
1345                 
1346                 /* apply targetmat - containing location on path, and rotation */
1347                 Mat4MulSerie(cob->matrix, ct->matrix, obmat, NULL, NULL, NULL, NULL, NULL, NULL);
1348                 
1349                 /* un-apply scaling caused by path */
1350                 Mat4ToSize(cob->matrix, obsize);
1351                 if (obsize[0])
1352                         VecMulf(cob->matrix[0], size[0] / obsize[0]);
1353                 if (obsize[1])
1354                         VecMulf(cob->matrix[1], size[1] / obsize[1]);
1355                 if (obsize[2])
1356                         VecMulf(cob->matrix[2], size[2] / obsize[2]);
1357         }
1358 }
1359
1360 static bConstraintTypeInfo CTI_FOLLOWPATH = {
1361         CONSTRAINT_TYPE_FOLLOWPATH, /* type */
1362         sizeof(bFollowPathConstraint), /* size */
1363         "Follow Path", /* name */
1364         "bFollowPathConstraint", /* struct name */
1365         NULL, /* free data */
1366         NULL, /* relink data */
1367         NULL, /* copy data */
1368         followpath_new_data, /* new data */
1369         followpath_get_tars, /* get constraint targets */
1370         followpath_flush_tars, /* flush constraint targets */
1371         followpath_get_tarmat, /* get target matrix */
1372         followpath_evaluate /* evaluate */
1373 };
1374
1375 /* --------- Limit Location --------- */
1376
1377
1378 static void loclimit_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
1379 {
1380         bLocLimitConstraint *data = con->data;
1381         
1382         if (data->flag & LIMIT_XMIN) {
1383                 if (cob->matrix[3][0] < data->xmin)
1384                         cob->matrix[3][0] = data->xmin;
1385         }
1386         if (data->flag & LIMIT_XMAX) {
1387                 if (cob->matrix[3][0] > data->xmax)
1388                         cob->matrix[3][0] = data->xmax;
1389         }
1390         if (data->flag & LIMIT_YMIN) {
1391                 if (cob->matrix[3][1] < data->ymin)
1392                         cob->matrix[3][1] = data->ymin;
1393         }
1394         if (data->flag & LIMIT_YMAX) {
1395                 if (cob->matrix[3][1] > data->ymax)
1396                         cob->matrix[3][1] = data->ymax;
1397         }
1398         if (data->flag & LIMIT_ZMIN) {
1399                 if (cob->matrix[3][2] < data->zmin) 
1400                         cob->matrix[3][2] = data->zmin;
1401         }
1402         if (data->flag & LIMIT_ZMAX) {
1403                 if (cob->matrix[3][2] > data->zmax)
1404                         cob->matrix[3][2] = data->zmax;
1405         }
1406 }
1407
1408 static bConstraintTypeInfo CTI_LOCLIMIT = {
1409         CONSTRAINT_TYPE_LOCLIMIT, /* type */
1410         sizeof(bLocLimitConstraint), /* size */
1411         "Limit Location", /* name */
1412         "bLocLimitConstraint", /* struct name */
1413         NULL, /* free data */
1414         NULL, /* relink data */
1415         NULL, /* copy data */
1416         NULL, /* new data */
1417         NULL, /* get constraint targets */
1418         NULL, /* flush constraint targets */
1419         NULL, /* get target matrix */
1420         loclimit_evaluate /* evaluate */
1421 };
1422
1423 /* -------- Limit Rotation --------- */
1424
1425 static void rotlimit_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
1426 {
1427         bRotLimitConstraint *data = con->data;
1428         float loc[3];
1429         float eul[3];
1430         float size[3];
1431         
1432         VECCOPY(loc, cob->matrix[3]);
1433         Mat4ToSize(cob->matrix, size);
1434         
1435         Mat4ToEul(cob->matrix, eul);
1436         
1437         /* eulers: radians to degrees! */
1438         eul[0] = (float)(eul[0] / M_PI * 180);
1439         eul[1] = (float)(eul[1] / M_PI * 180);
1440         eul[2] = (float)(eul[2] / M_PI * 180);
1441         
1442         /* limiting of euler values... */
1443         if (data->flag & LIMIT_XROT) {
1444                 if (eul[0] < data->xmin) 
1445                         eul[0] = data->xmin;
1446                         
1447                 if (eul[0] > data->xmax)
1448                         eul[0] = data->xmax;
1449         }
1450         if (data->flag & LIMIT_YROT) {
1451                 if (eul[1] < data->ymin)
1452                         eul[1] = data->ymin;
1453                         
1454                 if (eul[1] > data->ymax)
1455                         eul[1] = data->ymax;
1456         }
1457         if (data->flag & LIMIT_ZROT) {
1458                 if (eul[2] < data->zmin)
1459                         eul[2] = data->zmin;
1460                         
1461                 if (eul[2] > data->zmax)
1462                         eul[2] = data->zmax;
1463         }
1464                 
1465         /* eulers: degrees to radians ! */
1466         eul[0] = (float)(eul[0] / 180 * M_PI); 
1467         eul[1] = (float)(eul[1] / 180 * M_PI);
1468         eul[2] = (float)(eul[2] / 180 * M_PI);
1469         
1470         LocEulSizeToMat4(cob->matrix, loc, eul, size);
1471 }
1472
1473 static bConstraintTypeInfo CTI_ROTLIMIT = {
1474         CONSTRAINT_TYPE_ROTLIMIT, /* type */
1475         sizeof(bRotLimitConstraint), /* size */
1476         "Limit Rotation", /* name */
1477         "bRotLimitConstraint", /* struct name */
1478         NULL, /* free data */
1479         NULL, /* relink data */
1480         NULL, /* copy data */
1481         NULL, /* new data */
1482         NULL, /* get constraint targets */
1483         NULL, /* flush constraint targets */
1484         NULL, /* get target matrix */
1485         rotlimit_evaluate /* evaluate */
1486 };
1487
1488 /* --------- Limit Scaling --------- */
1489
1490
1491 static void sizelimit_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
1492 {
1493         bSizeLimitConstraint *data = con->data;
1494         float obsize[3], size[3];
1495         
1496         Mat4ToSize(cob->matrix, size);
1497         Mat4ToSize(cob->matrix, obsize);
1498         
1499         if (data->flag & LIMIT_XMIN) {
1500                 if (size[0] < data->xmin) 
1501                         size[0] = data->xmin;   
1502         }
1503         if (data->flag & LIMIT_XMAX) {
1504                 if (size[0] > data->xmax) 
1505                         size[0] = data->xmax;
1506         }
1507         if (data->flag & LIMIT_YMIN) {
1508                 if (size[1] < data->ymin) 
1509                         size[1] = data->ymin;   
1510         }
1511         if (data->flag & LIMIT_YMAX) {
1512                 if (size[1] > data->ymax) 
1513                         size[1] = data->ymax;
1514         }
1515         if (data->flag & LIMIT_ZMIN) {
1516                 if (size[2] < data->zmin) 
1517                         size[2] = data->zmin;   
1518         }
1519         if (data->flag & LIMIT_ZMAX) {
1520                 if (size[2] > data->zmax) 
1521                         size[2] = data->zmax;
1522         }
1523         
1524         if (obsize[0]) 
1525                 VecMulf(cob->matrix[0], size[0]/obsize[0]);
1526         if (obsize[1]) 
1527                 VecMulf(cob->matrix[1], size[1]/obsize[1]);
1528         if (obsize[2]) 
1529                 VecMulf(cob->matrix[2], size[2]/obsize[2]);
1530 }
1531
1532 static bConstraintTypeInfo CTI_SIZELIMIT = {
1533         CONSTRAINT_TYPE_SIZELIMIT, /* type */
1534         sizeof(bSizeLimitConstraint), /* size */
1535         "Limit Scaling", /* name */
1536         "bSizeLimitConstraint", /* struct name */
1537         NULL, /* free data */
1538         NULL, /* relink data */
1539         NULL, /* copy data */
1540         NULL, /* new data */
1541         NULL, /* get constraint targets */
1542         NULL, /* flush constraint targets */
1543         NULL, /* get target matrix */
1544         sizelimit_evaluate /* evaluate */
1545 };
1546
1547 /* ----------- Copy Location ------------- */
1548
1549 static void loclike_new_data (void *cdata)
1550 {
1551         bLocateLikeConstraint *data= (bLocateLikeConstraint *)cdata;
1552         
1553         data->flag = LOCLIKE_X|LOCLIKE_Y|LOCLIKE_Z;
1554 }
1555
1556 static int loclike_get_tars (bConstraint *con, ListBase *list)
1557 {
1558         if (con && list) {
1559                 bLocateLikeConstraint *data= con->data;
1560                 bConstraintTarget *ct;
1561                 
1562                 /* standard target-getting macro for single-target constraints */
1563                 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list)
1564                 
1565                 return 1;
1566         }
1567         
1568         return 0;
1569 }
1570
1571 static void loclike_flush_tars (bConstraint *con, ListBase *list, short nocopy)
1572 {
1573         if (con && list) {
1574                 bLocateLikeConstraint *data= con->data;
1575                 bConstraintTarget *ct= list->first;
1576                 
1577                 /* the following macro is used for all standard single-target constraints */
1578                 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, nocopy)
1579         }
1580 }
1581
1582 static void loclike_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
1583 {
1584         bLocateLikeConstraint *data= con->data;
1585         bConstraintTarget *ct= targets->first;
1586         
1587         if (VALID_CONS_TARGET(ct)) {
1588                 float offset[3] = {0.0f, 0.0f, 0.0f};
1589                 
1590                 if (data->flag & LOCLIKE_OFFSET)
1591                         VECCOPY(offset, cob->matrix[3]);
1592                         
1593                 if (data->flag & LOCLIKE_X) {
1594                         cob->matrix[3][0] = ct->matrix[3][0];
1595                         
1596                         if (data->flag & LOCLIKE_X_INVERT) cob->matrix[3][0] *= -1;
1597                         cob->matrix[3][0] += offset[0];
1598                 }
1599                 if (data->flag & LOCLIKE_Y) {
1600                         cob->matrix[3][1] = ct->matrix[3][1];
1601                         
1602                         if (data->flag & LOCLIKE_Y_INVERT) cob->matrix[3][1] *= -1;
1603                         cob->matrix[3][1] += offset[1];
1604                 }
1605                 if (data->flag & LOCLIKE_Z) {
1606                         cob->matrix[3][2] = ct->matrix[3][2];
1607                         
1608                         if (data->flag & LOCLIKE_Z_INVERT) cob->matrix[3][2] *= -1;
1609                         cob->matrix[3][2] += offset[2];
1610                 }
1611         }
1612 }
1613
1614 static bConstraintTypeInfo CTI_LOCLIKE = {
1615         CONSTRAINT_TYPE_LOCLIKE, /* type */
1616         sizeof(bLocateLikeConstraint), /* size */
1617         "Copy Location", /* name */
1618         "bLocateLikeConstraint", /* struct name */
1619         NULL, /* free data */
1620         NULL, /* relink data */
1621         NULL, /* copy data */
1622         loclike_new_data, /* new data */
1623         loclike_get_tars, /* get constraint targets */
1624         loclike_flush_tars, /* flush constraint targets */
1625         default_get_tarmat, /* get target matrix */
1626         loclike_evaluate /* evaluate */
1627 };
1628
1629 /* ----------- Copy Rotation ------------- */
1630
1631 static void rotlike_new_data (void *cdata)
1632 {
1633         bRotateLikeConstraint *data= (bRotateLikeConstraint *)cdata;
1634         
1635         data->flag = ROTLIKE_X|ROTLIKE_Y|ROTLIKE_Z;
1636 }
1637
1638 static int rotlike_get_tars (bConstraint *con, ListBase *list)
1639 {
1640         if (con && list) {
1641                 bRotateLikeConstraint *data= con->data;
1642                 bConstraintTarget *ct;
1643                 
1644                 /* standard target-getting macro for single-target constraints */
1645                 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list)
1646                 
1647                 return 1;
1648         }
1649         
1650         return 0;
1651 }
1652
1653 static void rotlike_flush_tars (bConstraint *con, ListBase *list, short nocopy)
1654 {
1655         if (con && list) {
1656                 bRotateLikeConstraint *data= con->data;
1657                 bConstraintTarget *ct= list->first;
1658                 
1659                 /* the following macro is used for all standard single-target constraints */
1660                 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, nocopy)
1661         }
1662 }
1663
1664 static void rotlike_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
1665 {
1666         bRotateLikeConstraint *data= con->data;
1667         bConstraintTarget *ct= targets->first;
1668         
1669         if (VALID_CONS_TARGET(ct)) {
1670                 float   loc[3];
1671                 float   eul[3], obeul[3];
1672                 float   size[3];
1673                 
1674                 VECCOPY(loc, cob->matrix[3]);
1675                 Mat4ToSize(cob->matrix, size);
1676                 
1677                 Mat4ToEul(ct->matrix, eul);
1678                 Mat4ToEul(cob->matrix, obeul);
1679                 
1680                 if ((data->flag & ROTLIKE_X)==0)
1681                         eul[0] = obeul[0];
1682                 else {
1683                         if (data->flag & ROTLIKE_OFFSET)
1684                                 euler_rot(eul, obeul[0], 'x');
1685                         
1686                         if (data->flag & ROTLIKE_X_INVERT)
1687                                 eul[0] *= -1;
1688                 }
1689                 
1690                 if ((data->flag & ROTLIKE_Y)==0)
1691                         eul[1] = obeul[1];
1692                 else {
1693                         if (data->flag & ROTLIKE_OFFSET)
1694                                 euler_rot(eul, obeul[1], 'y');
1695                         
1696                         if (data->flag & ROTLIKE_Y_INVERT)
1697                                 eul[1] *= -1;
1698                 }
1699                 
1700                 if ((data->flag & ROTLIKE_Z)==0)
1701                         eul[2] = obeul[2];
1702                 else {
1703                         if (data->flag & ROTLIKE_OFFSET)
1704                                 euler_rot(eul, obeul[2], 'z');
1705                         
1706                         if (data->flag & ROTLIKE_Z_INVERT)
1707                                 eul[2] *= -1;
1708                 }
1709                 
1710                 compatible_eul(eul, obeul);
1711                 LocEulSizeToMat4(cob->matrix, loc, eul, size);
1712         }
1713 }
1714
1715 static bConstraintTypeInfo CTI_ROTLIKE = {
1716         CONSTRAINT_TYPE_ROTLIKE, /* type */
1717         sizeof(bRotateLikeConstraint), /* size */
1718         "Copy Rotation", /* name */
1719         "bRotateLikeConstraint", /* struct name */
1720         NULL, /* free data */
1721         NULL, /* relink data */
1722         NULL, /* copy data */
1723         rotlike_new_data, /* new data */
1724         rotlike_get_tars, /* get constraint targets */
1725         rotlike_flush_tars, /* flush constraint targets */
1726         default_get_tarmat, /* get target matrix */
1727         rotlike_evaluate /* evaluate */
1728 };
1729
1730 /* ---------- Copy Scaling ---------- */
1731
1732 static void sizelike_new_data (void *cdata)
1733 {
1734         bSizeLikeConstraint *data= (bSizeLikeConstraint *)cdata;
1735         
1736         data->flag = SIZELIKE_X|SIZELIKE_Y|SIZELIKE_Z;
1737 }
1738
1739 static int sizelike_get_tars (bConstraint *con, ListBase *list)
1740 {
1741         if (con && list) {
1742                 bSizeLikeConstraint *data= con->data;
1743                 bConstraintTarget *ct;
1744                 
1745                 /* standard target-getting macro for single-target constraints */
1746                 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list)
1747                 
1748                 return 1;
1749         }
1750         
1751         return 0;
1752 }
1753
1754 static void sizelike_flush_tars (bConstraint *con, ListBase *list, short nocopy)
1755 {
1756         if (con && list) {
1757                 bSizeLikeConstraint *data= con->data;
1758                 bConstraintTarget *ct= list->first;
1759                 
1760                 /* the following macro is used for all standard single-target constraints */
1761                 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, nocopy)
1762         }
1763 }
1764
1765 static void sizelike_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
1766 {
1767         bSizeLikeConstraint *data= con->data;
1768         bConstraintTarget *ct= targets->first;
1769         
1770         if (VALID_CONS_TARGET(ct)) {
1771                 float obsize[3], size[3];
1772                 
1773                 Mat4ToSize(ct->matrix, size);
1774                 Mat4ToSize(cob->matrix, obsize);
1775                 
1776                 if ((data->flag & SIZELIKE_X) && (obsize[0] != 0)) {
1777                         if (data->flag & SIZELIKE_OFFSET) {
1778                                 size[0] += (obsize[0] - 1.0f);
1779                                 VecMulf(cob->matrix[0], size[0] / obsize[0]);
1780                         }
1781                         else
1782                                 VecMulf(cob->matrix[0], size[0] / obsize[0]);
1783                 }
1784                 if ((data->flag & SIZELIKE_Y) && (obsize[1] != 0)) {
1785                         if (data->flag & SIZELIKE_OFFSET) {
1786                                 size[1] += (obsize[1] - 1.0f);
1787                                 VecMulf(cob->matrix[1], size[1] / obsize[1]);
1788                         }
1789                         else
1790                                 VecMulf(cob->matrix[1], size[1] / obsize[1]);
1791                 }
1792                 if ((data->flag & SIZELIKE_Z) && (obsize[2] != 0)) {
1793                         if (data->flag & SIZELIKE_OFFSET) {
1794                                 size[2] += (obsize[2] - 1.0f);
1795                                 VecMulf(cob->matrix[2], size[2] / obsize[2]);
1796                         }
1797                         else
1798                                 VecMulf(cob->matrix[2], size[2] / obsize[2]);
1799                 }
1800         }
1801 }
1802
1803 static bConstraintTypeInfo CTI_SIZELIKE = {
1804         CONSTRAINT_TYPE_SIZELIKE, /* type */
1805         sizeof(bSizeLikeConstraint), /* size */
1806         "Copy Scale", /* name */
1807         "bSizeLikeConstraint", /* struct name */
1808         NULL, /* free data */
1809         NULL, /* relink data */
1810         NULL, /* copy data */
1811         sizelike_new_data, /* new data */
1812         sizelike_get_tars, /* get constraint targets */
1813         sizelike_flush_tars, /* flush constraint targets */
1814         default_get_tarmat, /* get target matrix */
1815         sizelike_evaluate /* evaluate */
1816 };
1817
1818
1819 /* ----------- Python Constraint -------------- */
1820
1821 static void pycon_free (bConstraint *con)
1822 {
1823         bPythonConstraint *data= con->data;
1824         
1825         /* id-properties */
1826         IDP_FreeProperty(data->prop);
1827         MEM_freeN(data->prop);
1828         
1829         /* multiple targets */
1830         BLI_freelistN(&data->targets);
1831 }       
1832
1833 static void pycon_relink (bConstraint *con)
1834 {
1835         bPythonConstraint *data= con->data;
1836         
1837         ID_NEW(data->text);
1838 }
1839
1840 static void pycon_copy (bConstraint *con, bConstraint *srccon)
1841 {
1842         bPythonConstraint *pycon = (bPythonConstraint *)con->data;
1843         bPythonConstraint *opycon = (bPythonConstraint *)srccon->data;
1844         
1845         pycon->prop = IDP_CopyProperty(opycon->prop);
1846         duplicatelist(&pycon->targets, &opycon->targets);
1847 }
1848
1849 static void pycon_new_data (void *cdata)
1850 {
1851         bPythonConstraint *data= (bPythonConstraint *)cdata;
1852         
1853         /* everything should be set correctly by calloc, except for the prop->type constant.*/
1854         data->prop = MEM_callocN(sizeof(IDProperty), "PyConstraintProps");
1855         data->prop->type = IDP_GROUP;
1856 }
1857
1858 static int pycon_get_tars (bConstraint *con, ListBase *list)
1859 {
1860         if (con && list) {
1861                 bPythonConstraint *data= con->data;
1862                 
1863                 list->first = data->targets.first;
1864                 list->last = data->targets.last;
1865                 
1866                 return data->tarnum;
1867         }
1868         
1869         return 0;
1870 }
1871
1872 /* Whether this approach is maintained remains to be seen (aligorith) */
1873 static void pycon_get_tarmat (bConstraint *con, bConstraintOb *cob, bConstraintTarget *ct, float ctime)
1874 {
1875         bPythonConstraint *data= con->data;
1876         
1877         if (VALID_CONS_TARGET(ct)) {
1878                 /* special exception for curves - depsgraph issues */
1879                 if (ct->tar->type == OB_CURVE) {
1880                         Curve *cu= ct->tar->data;
1881                         
1882                         /* this check is to make sure curve objects get updated on file load correctly.*/
1883                         if (cu->path==NULL || cu->path->data==NULL) /* only happens on reload file, but violates depsgraph still... fix! */
1884                                 makeDispListCurveTypes(ct->tar, 0);                             
1885                 }
1886                 
1887                 /* firstly calculate the matrix the normal way, then let the py-function override
1888                  * this matrix if it needs to do so
1889                  */
1890                 constraint_target_to_mat4(ct->tar, ct->subtarget, ct->matrix, CONSTRAINT_SPACE_WORLD, ct->space, con->headtail);
1891                 
1892                 /* only execute target calculation if allowed */
1893 #ifndef DISABLE_PYTHON
1894                 if (G.f & G_DOSCRIPTLINKS)
1895                         BPY_pyconstraint_target(data, ct);
1896 #endif
1897         }
1898         else if (ct)
1899                 Mat4One(ct->matrix);
1900 }
1901
1902 static void pycon_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
1903 {
1904 #ifdef DISABLE_PYTHON
1905         return;
1906 #else
1907         bPythonConstraint *data= con->data;
1908         
1909         /* only evaluate in python if we're allowed to do so */
1910         if ((G.f & G_DOSCRIPTLINKS)==0)  return;
1911         
1912 /* currently removed, until I this can be re-implemented for multiple targets */
1913 #if 0
1914         /* Firstly, run the 'driver' function which has direct access to the objects involved 
1915          * Technically, this is potentially dangerous as users may abuse this and cause dependency-problems,
1916          * but it also allows certain 'clever' rigging hacks to work.
1917          */
1918         BPY_pyconstraint_driver(data, cob, targets);
1919 #endif
1920         
1921         /* Now, run the actual 'constraint' function, which should only access the matrices */
1922         BPY_pyconstraint_eval(data, cob, targets);
1923 #endif /* DISABLE_PYTHON */
1924 }
1925
1926 static bConstraintTypeInfo CTI_PYTHON = {
1927         CONSTRAINT_TYPE_PYTHON, /* type */
1928         sizeof(bPythonConstraint), /* size */
1929         "Script", /* name */
1930         "bPythonConstraint", /* struct name */
1931         pycon_free, /* free data */
1932         pycon_relink, /* relink data */
1933         pycon_copy, /* copy data */
1934         pycon_new_data, /* new data */
1935         pycon_get_tars, /* get constraint targets */
1936         NULL, /* flush constraint targets */
1937         pycon_get_tarmat, /* get target matrix */
1938         pycon_evaluate /* evaluate */
1939 };
1940
1941 /* -------- Action Constraint ----------- */
1942
1943 static void actcon_relink (bConstraint *con)
1944 {
1945         bActionConstraint *data= con->data;
1946         ID_NEW(data->act);
1947 }
1948
1949 static void actcon_new_data (void *cdata)
1950 {
1951         bActionConstraint *data= (bActionConstraint *)cdata;
1952         
1953         /* set type to 20 (Loc X), as 0 is Rot X for backwards compatability */
1954         data->type = 20;
1955 }
1956
1957 static int actcon_get_tars (bConstraint *con, ListBase *list)
1958 {
1959         if (con && list) {
1960                 bActionConstraint *data= con->data;
1961                 bConstraintTarget *ct;
1962                 
1963                 /* standard target-getting macro for single-target constraints */
1964                 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list)
1965                 
1966                 return 1;
1967         }
1968         
1969         return 0;
1970 }
1971
1972 static void actcon_flush_tars (bConstraint *con, ListBase *list, short nocopy)
1973 {
1974         if (con && list) {
1975                 bActionConstraint *data= con->data;
1976                 bConstraintTarget *ct= list->first;
1977                 
1978                 /* the following macro is used for all standard single-target constraints */
1979                 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, nocopy)
1980         }
1981 }
1982
1983 static void actcon_get_tarmat (bConstraint *con, bConstraintOb *cob, bConstraintTarget *ct, float ctime)
1984 {
1985         extern void chan_calc_mat(bPoseChannel *chan);
1986         bActionConstraint *data = con->data;
1987         
1988         if (VALID_CONS_TARGET(ct)) {
1989                 float tempmat[4][4], vec[3];
1990                 float s, t;
1991                 short axis;
1992                 
1993                 /* initialise return matrix */
1994                 Mat4One(ct->matrix);
1995                 
1996                 /* get the transform matrix of the target */
1997                 constraint_target_to_mat4(ct->tar, ct->subtarget, tempmat, CONSTRAINT_SPACE_WORLD, ct->space, con->headtail);
1998                 
1999                 /* determine where in transform range target is */
2000                 /* data->type is mapped as follows for backwards compatability:
2001                  *      00,01,02        - rotation (it used to be like this)
2002                  *      10,11,12        - scaling
2003                  *      20,21,22        - location
2004                  */
2005                 if (data->type < 10) {
2006                         /* extract rotation (is in whatever space target should be in) */
2007                         Mat4ToEul(tempmat, vec);
2008                         vec[0] *= (float)(180.0/M_PI);
2009                         vec[1] *= (float)(180.0/M_PI);
2010                         vec[2] *= (float)(180.0/M_PI);
2011                         axis= data->type;
2012                 }
2013                 else if (data->type < 20) {
2014                         /* extract scaling (is in whatever space target should be in) */
2015                         Mat4ToSize(tempmat, vec);
2016                         axis= data->type - 10;
2017                 }
2018                 else {
2019                         /* extract location */
2020                         VECCOPY(vec, tempmat[3]);
2021                         axis= data->type - 20;
2022                 }
2023                 
2024                 /* Target defines the animation */
2025                 s = (vec[axis]-data->min) / (data->max-data->min);
2026                 CLAMP(s, 0, 1);
2027                 t = ( s * (data->end-data->start)) + data->start;
2028                 
2029                 /* Get the appropriate information from the action */
2030                 if (cob->type == CONSTRAINT_OBTYPE_BONE) {
2031                         bPose *pose;
2032                         bPoseChannel *pchan, *tchan;
2033                         
2034                         /* make a temporary pose and evaluate using that */
2035                         pose = MEM_callocN(sizeof(bPose), "pose");
2036                         
2037                         pchan = cob->pchan;
2038                         tchan= verify_pose_channel(pose, pchan->name);
2039                         extract_pose_from_action(pose, data->act, t);
2040                         
2041                         chan_calc_mat(tchan);
2042                         
2043                         Mat4CpyMat4(ct->matrix, tchan->chan_mat);
2044                         
2045                         /* Clean up */
2046                         free_pose(pose);
2047                 }
2048                 else if (cob->type == CONSTRAINT_OBTYPE_OBJECT) {
2049                         /* evaluate using workob */
2050                         what_does_obaction(cob->ob, data->act, t);
2051                         object_to_mat4(&workob, ct->matrix);
2052                 }
2053                 else {
2054                         /* behaviour undefined... */
2055                         puts("Error: unknown owner type for Action Constraint");
2056                 }
2057         }
2058 }
2059
2060 static void actcon_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
2061 {
2062         bConstraintTarget *ct= targets->first;
2063         
2064         if (VALID_CONS_TARGET(ct)) {
2065                 float temp[4][4];
2066                 
2067                 /* Nice and simple... we just need to multiply the matrices, as the get_target_matrix
2068                  * function has already taken care of everything else.
2069                  */
2070                 Mat4CpyMat4(temp, cob->matrix);
2071                 Mat4MulMat4(cob->matrix, ct->matrix, temp);
2072         }
2073 }
2074
2075 static bConstraintTypeInfo CTI_ACTION = {
2076         CONSTRAINT_TYPE_ACTION, /* type */
2077         sizeof(bActionConstraint), /* size */
2078         "Action", /* name */
2079         "bActionConstraint", /* struct name */
2080         NULL, /* free data */
2081         actcon_relink, /* relink data */
2082         NULL, /* copy data */
2083         actcon_new_data, /* new data */
2084         actcon_get_tars, /* get constraint targets */
2085         actcon_flush_tars, /* flush constraint targets */
2086         actcon_get_tarmat, /* get target matrix */
2087         actcon_evaluate /* evaluate */
2088 };
2089
2090 /* --------- Locked Track ---------- */
2091
2092 static void locktrack_new_data (void *cdata)
2093 {
2094         bLockTrackConstraint *data= (bLockTrackConstraint *)cdata;
2095         
2096         data->trackflag = TRACK_Y;
2097         data->lockflag = LOCK_Z;
2098 }       
2099
2100 static int locktrack_get_tars (bConstraint *con, ListBase *list)
2101 {
2102         if (con && list) {
2103                 bLockTrackConstraint *data= con->data;
2104                 bConstraintTarget *ct;
2105                 
2106                 /* the following macro is used for all standard single-target constraints */
2107                 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list)
2108                 
2109                 return 1;
2110         }
2111         
2112         return 0;
2113 }
2114
2115 static void locktrack_flush_tars (bConstraint *con, ListBase *list, short nocopy)
2116 {
2117         if (con && list) {
2118                 bLockTrackConstraint *data= con->data;
2119                 bConstraintTarget *ct= list->first;
2120                 
2121                 /* the following macro is used for all standard single-target constraints */
2122                 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, nocopy)
2123         }
2124 }
2125
2126 static void locktrack_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
2127 {
2128         bLockTrackConstraint *data= con->data;
2129         bConstraintTarget *ct= targets->first;
2130         
2131         if (VALID_CONS_TARGET(ct)) {
2132                 float vec[3],vec2[3];
2133                 float totmat[3][3];
2134                 float tmpmat[3][3];
2135                 float invmat[3][3];
2136                 float tmat[4][4];
2137                 float mdet;
2138                 
2139                 /* Vector object -> target */
2140                 VecSubf(vec, ct->matrix[3], cob->matrix[3]);
2141                 switch (data->lockflag){
2142                 case LOCK_X: /* LOCK X */
2143                 {
2144                         switch (data->trackflag) {
2145                                 case TRACK_Y: /* LOCK X TRACK Y */
2146                                 {
2147                                         /* Projection of Vector on the plane */
2148                                         Projf(vec2, vec, cob->matrix[0]);
2149                                         VecSubf(totmat[1], vec, vec2);
2150                                         Normalize(totmat[1]);
2151                                         
2152                                         /* the x axis is fixed */
2153                                         totmat[0][0] = cob->matrix[0][0];
2154                                         totmat[0][1] = cob->matrix[0][1];
2155                                         totmat[0][2] = cob->matrix[0][2];
2156                                         Normalize(totmat[0]);
2157                                         
2158                                         /* the z axis gets mapped onto a third orthogonal vector */
2159                                         Crossf(totmat[2], totmat[0], totmat[1]);
2160                                 }
2161                                         break;
2162                                 case TRACK_Z: /* LOCK X TRACK Z */
2163                                 {
2164                                         /* Projection of Vector on the plane */
2165                                         Projf(vec2, vec, cob->matrix[0]);
2166                                         VecSubf(totmat[2], vec, vec2);
2167                                         Normalize(totmat[2]);
2168                                         
2169                                         /* the x axis is fixed */
2170                                         totmat[0][0] = cob->matrix[0][0];
2171                                         totmat[0][1] = cob->matrix[0][1];
2172                                         totmat[0][2] = cob->matrix[0][2];
2173                                         Normalize(totmat[0]);
2174                                         
2175                                         /* the z axis gets mapped onto a third orthogonal vector */
2176                                         Crossf(totmat[1], totmat[2], totmat[0]);
2177                                 }
2178                                         break;
2179                                 case TRACK_nY: /* LOCK X TRACK -Y */
2180                                 {
2181                                         /* Projection of Vector on the plane */
2182                                         Projf(vec2, vec, cob->matrix[0]);
2183                                         VecSubf(totmat[1], vec, vec2);
2184                                         Normalize(totmat[1]);
2185                                         VecMulf(totmat[1],-1);
2186                                         
2187                                         /* the x axis is fixed */
2188                                         totmat[0][0] = cob->matrix[0][0];
2189                                         totmat[0][1] = cob->matrix[0][1];
2190                                         totmat[0][2] = cob->matrix[0][2];
2191                                         Normalize(totmat[0]);
2192                                         
2193                                         /* the z axis gets mapped onto a third orthogonal vector */
2194                                         Crossf(totmat[2], totmat[0], totmat[1]);
2195                                 }
2196                                         break;
2197                                 case TRACK_nZ: /* LOCK X TRACK -Z */
2198                                 {
2199                                         /* Projection of Vector on the plane */
2200                                         Projf(vec2, vec, cob->matrix[0]);
2201                                         VecSubf(totmat[2], vec, vec2);
2202                                         Normalize(totmat[2]);
2203                                         VecMulf(totmat[2],-1);
2204                                                 
2205                                         /* the x axis is fixed */
2206                                         totmat[0][0] = cob->matrix[0][0];
2207                                         totmat[0][1] = cob->matrix[0][1];
2208                                         totmat[0][2] = cob->matrix[0][2];
2209                                         Normalize(totmat[0]);
2210                                                 
2211                                         /* the z axis gets mapped onto a third orthogonal vector */
2212                                         Crossf(totmat[1], totmat[2], totmat[0]);
2213                                 }
2214                                         break;
2215                                 default:
2216                                 {
2217                                         totmat[0][0] = 1;totmat[0][1] = 0;totmat[0][2] = 0;
2218                                         totmat[1][0] = 0;totmat[1][1] = 1;totmat[1][2] = 0;
2219                                         totmat[2][0] = 0;totmat[2][1] = 0;totmat[2][2] = 1;
2220                                 }
2221                                         break;
2222                         }
2223                 }
2224                         break;
2225                 case LOCK_Y: /* LOCK Y */
2226                 {
2227                         switch (data->trackflag) {
2228                                 case TRACK_X: /* LOCK Y TRACK X */
2229                                 {
2230                                         /* Projection of Vector on the plane */
2231                                         Projf(vec2, vec, cob->matrix[1]);
2232                                         VecSubf(totmat[0], vec, vec2);
2233                                         Normalize(totmat[0]);
2234                                         
2235                                         /* the y axis is fixed */
2236                                         totmat[1][0] = cob->matrix[1][0];
2237                                         totmat[1][1] = cob->matrix[1][1];
2238                                         totmat[1][2] = cob->matrix[1][2];
2239                                         Normalize(totmat[1]);
2240                                         
2241                                         /* the z axis gets mapped onto a third orthogonal vector */
2242                                         Crossf(totmat[2], totmat[0], totmat[1]);
2243                                 }
2244                                         break;
2245                                 case TRACK_Z: /* LOCK Y TRACK Z */
2246                                 {
2247                                         /* Projection of Vector on the plane */
2248                                         Projf(vec2, vec, cob->matrix[1]);
2249                                         VecSubf(totmat[2], vec, vec2);
2250                                         Normalize(totmat[2]);
2251
2252                                         /* the y axis is fixed */
2253                                         totmat[1][0] = cob->matrix[1][0];
2254                                         totmat[1][1] = cob->matrix[1][1];
2255                                         totmat[1][2] = cob->matrix[1][2];
2256                                         Normalize(totmat[1]);
2257                                         
2258                                         /* the z axis gets mapped onto a third orthogonal vector */
2259                                         Crossf(totmat[0], totmat[1], totmat[2]);
2260                                 }
2261                                         break;
2262                                 case TRACK_nX: /* LOCK Y TRACK -X */
2263                                 {
2264                                         /* Projection of Vector on the plane */
2265                                         Projf(vec2, vec, cob->matrix[1]);
2266                                         VecSubf(totmat[0], vec, vec2);
2267                                         Normalize(totmat[0]);
2268                                         VecMulf(totmat[0],-1);
2269                                         
2270                                         /* the y axis is fixed */
2271                                         totmat[1][0] = cob->matrix[1][0];
2272                                         totmat[1][1] = cob->matrix[1][1];
2273                                         totmat[1][2] = cob->matrix[1][2];
2274                                         Normalize(totmat[1]);
2275                                         
2276                                         /* the z axis gets mapped onto a third orthogonal vector */
2277                                         Crossf(totmat[2], totmat[0], totmat[1]);
2278                                 }
2279                                         break;
2280                                 case TRACK_nZ: /* LOCK Y TRACK -Z */
2281                                 {
2282                                         /* Projection of Vector on the plane */
2283                                         Projf(vec2, vec, cob->matrix[1]);
2284                                         VecSubf(totmat[2], vec, vec2);
2285                                         Normalize(totmat[2]);
2286                                         VecMulf(totmat[2],-1);
2287                                         
2288                                         /* the y axis is fixed */
2289                                         totmat[1][0] = cob->matrix[1][0];
2290                                         totmat[1][1] = cob->matrix[1][1];
2291                                         totmat[1][2] = cob->matrix[1][2];
2292                                         Normalize(totmat[1]);
2293                                         
2294                                         /* the z axis gets mapped onto a third orthogonal vector */
2295                                         Crossf(totmat[0], totmat[1], totmat[2]);
2296                                 }
2297                                         break;
2298                                 default:
2299                                 {
2300                                         totmat[0][0] = 1;totmat[0][1] = 0;totmat[0][2] = 0;
2301                                         totmat[1][0] = 0;totmat[1][1] = 1;totmat[1][2] = 0;
2302                                         totmat[2][0] = 0;totmat[2][1] = 0;totmat[2][2] = 1;
2303                                 }
2304                                         break;
2305                         }
2306                 }
2307                         break;
2308                 case LOCK_Z: /* LOCK Z */
2309                 {
2310                         switch (data->trackflag) {
2311                                 case TRACK_X: /* LOCK Z TRACK X */
2312                                 {
2313                                         /* Projection of Vector on the plane */
2314                                         Projf(vec2, vec, cob->matrix[2]);
2315                                         VecSubf(totmat[0], vec, vec2);
2316                                         Normalize(totmat[0]);
2317                                         
2318                                         /* the z axis is fixed */
2319                                         totmat[2][0] = cob->matrix[2][0];
2320                                         totmat[2][1] = cob->matrix[2][1];
2321                                         totmat[2][2] = cob->matrix[2][2];
2322                                         Normalize(totmat[2]);
2323                                         
2324                                         /* the x axis gets mapped onto a third orthogonal vector */
2325                                         Crossf(totmat[1], totmat[2], totmat[0]);
2326                                 }
2327                                         break;
2328                                 case TRACK_Y: /* LOCK Z TRACK Y */
2329                                 {
2330                                         /* Projection of Vector on the plane */
2331                                         Projf(vec2, vec, cob->matrix[2]);
2332                                         VecSubf(totmat[1], vec, vec2);
2333                                         Normalize(totmat[1]);
2334                                         
2335                                         /* the z axis is fixed */
2336                                         totmat[2][0] = cob->matrix[2][0];
2337                                         totmat[2][1] = cob->matrix[2][1];
2338                                         totmat[2][2] = cob->matrix[2][2];
2339                                         Normalize(totmat[2]);
2340                                                 
2341                                         /* the x axis gets mapped onto a third orthogonal vector */
2342                                         Crossf(totmat[0], totmat[1], totmat[2]);
2343                                 }
2344                                         break;
2345                                 case TRACK_nX: /* LOCK Z TRACK -X */
2346                                 {
2347                                         /* Projection of Vector on the plane */
2348                                         Projf(vec2, vec, cob->matrix[2]);
2349                                         VecSubf(totmat[0], vec, vec2);
2350                                         Normalize(totmat[0]);
2351                                         VecMulf(totmat[0],-1);
2352                                         
2353                                         /* the z axis is fixed */
2354                                         totmat[2][0] = cob->matrix[2][0];
2355                                         totmat[2][1] = cob->matrix[2][1];
2356                                         totmat[2][2] = cob->matrix[2][2];
2357                                         Normalize(totmat[2]);
2358                                         
2359                                         /* the x axis gets mapped onto a third orthogonal vector */
2360                                         Crossf(totmat[1], totmat[2], totmat[0]);
2361                                 }
2362                                         break;
2363                                 case TRACK_nY: /* LOCK Z TRACK -Y */
2364                                 {
2365                                         /* Projection of Vector on the plane */
2366                                         Projf(vec2, vec, cob->matrix[2]);
2367                                         VecSubf(totmat[1], vec, vec2);
2368                                         Normalize(totmat[1]);
2369                                         VecMulf(totmat[1],-1);
2370                                         
2371                                         /* the z axis is fixed */
2372                                         totmat[2][0] = cob->matrix[2][0];
2373                                         totmat[2][1] = cob->matrix[2][1];
2374                                         totmat[2][2] = cob->matrix[2][2];
2375                                         Normalize(totmat[2]);
2376                                                 
2377                                         /* the x axis gets mapped onto a third orthogonal vector */
2378                                         Crossf(totmat[0], totmat[1], totmat[2]);
2379                                 }
2380                                         break;
2381                                 default:
2382                                 {
2383                                                 totmat[0][0] = 1;totmat[0][1] = 0;totmat[0][2] = 0;
2384                                                 totmat[1][0] = 0;totmat[1][1] = 1;totmat[1][2] = 0;
2385                                                 totmat[2][0] = 0;totmat[2][1] = 0;totmat[2][2] = 1;
2386                                 }
2387                                         break;
2388                         }
2389                 }
2390                         break;
2391                 default:
2392                         {
2393                                 totmat[0][0] = 1;totmat[0][1] = 0;totmat[0][2] = 0;
2394                                 totmat[1][0] = 0;totmat[1][1] = 1;totmat[1][2] = 0;
2395                                 totmat[2][0] = 0;totmat[2][1] = 0;totmat[2][2] = 1;
2396                         }
2397                         break;
2398                 }
2399                 /* Block to keep matrix heading */
2400                 tmpmat[0][0] = cob->matrix[0][0];tmpmat[0][1] = cob->matrix[0][1];tmpmat[0][2] = cob->matrix[0][2];
2401                 tmpmat[1][0] = cob->matrix[1][0];tmpmat[1][1] = cob->matrix[1][1];tmpmat[1][2] = cob->matrix[1][2];
2402                 tmpmat[2][0] = cob->matrix[2][0];tmpmat[2][1] = cob->matrix[2][1];tmpmat[2][2] = cob->matrix[2][2];
2403                 Normalize(tmpmat[0]);
2404                 Normalize(tmpmat[1]);
2405                 Normalize(tmpmat[2]);
2406                 Mat3Inv(invmat, tmpmat);
2407                 Mat3MulMat3(tmpmat, totmat, invmat);
2408                 totmat[0][0] = tmpmat[0][0];totmat[0][1] = tmpmat[0][1];totmat[0][2] = tmpmat[0][2];
2409                 totmat[1][0] = tmpmat[1][0];totmat[1][1] = tmpmat[1][1];totmat[1][2] = tmpmat[1][2];
2410                 totmat[2][0] = tmpmat[2][0];totmat[2][1] = tmpmat[2][1];totmat[2][2] = tmpmat[2][2];
2411                 
2412                 Mat4CpyMat4(tmat, cob->matrix);
2413                 
2414                 mdet = Det3x3(  totmat[0][0],totmat[0][1],totmat[0][2],
2415                                                 totmat[1][0],totmat[1][1],totmat[1][2],
2416                                                 totmat[2][0],totmat[2][1],totmat[2][2]);
2417                 if (mdet==0) {
2418                         totmat[0][0] = 1;totmat[0][1] = 0;totmat[0][2] = 0;
2419                         totmat[1][0] = 0;totmat[1][1] = 1;totmat[1][2] = 0;
2420                         totmat[2][0] = 0;totmat[2][1] = 0;totmat[2][2] = 1;
2421                 }
2422                 
2423                 /* apply out transformaton to the object */
2424                 Mat4MulMat34(cob->matrix, totmat, tmat);
2425         }
2426 }
2427
2428 static bConstraintTypeInfo CTI_LOCKTRACK = {
2429         CONSTRAINT_TYPE_LOCKTRACK, /* type */
2430         sizeof(bLockTrackConstraint), /* size */
2431         "Locked Track", /* name */
2432         "bLockTrackConstraint", /* struct name */
2433         NULL, /* free data */
2434         NULL, /* relink data */
2435         NULL, /* copy data */
2436         locktrack_new_data, /* new data */
2437         locktrack_get_tars, /* get constraint targets */
2438         locktrack_flush_tars, /* flush constraint targets */
2439         default_get_tarmat, /* get target matrix */
2440         locktrack_evaluate /* evaluate */
2441 };
2442
2443 /* ---------- Limit Distance Constraint ----------- */
2444
2445 static void distlimit_new_data (void *cdata)
2446 {
2447         bDistLimitConstraint *data= (bDistLimitConstraint *)cdata;
2448         
2449         data->dist= 0.0;
2450 }
2451
2452 static int distlimit_get_tars (bConstraint *con, ListBase *list)
2453 {
2454         if (con && list) {
2455                 bDistLimitConstraint *data= con->data;
2456                 bConstraintTarget *ct;
2457                 
2458                 /* standard target-getting macro for single-target constraints */
2459                 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list)
2460                 
2461                 return 1;
2462         }
2463         
2464         return 0;
2465 }
2466
2467 static void distlimit_flush_tars (bConstraint *con, ListBase *list, short nocopy)
2468 {
2469         if (con && list) {
2470                 bDistLimitConstraint *data= con->data;
2471                 bConstraintTarget *ct= list->first;
2472                 
2473                 /* the following macro is used for all standard single-target constraints */
2474                 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, nocopy)
2475         }
2476 }
2477
2478 static void distlimit_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
2479 {
2480         bDistLimitConstraint *data= con->data;
2481         bConstraintTarget *ct= targets->first;
2482         
2483         /* only evaluate if there is a target */
2484         if (VALID_CONS_TARGET(ct)) {
2485                 float dvec[3], dist=0.0f, sfac=1.0f;
2486                 short clamp_surf= 0;
2487                 
2488                 /* calculate our current distance from the target */
2489                 dist= VecLenf(cob->matrix[3], ct->matrix[3]);
2490                 
2491                 /* set distance (flag is only set when user demands it) */
2492                 if (data->dist == 0)
2493                         data->dist= dist;
2494                 
2495                 /* check if we're which way to clamp from, and calculate interpolation factor (if needed) */
2496                 if (data->mode == LIMITDIST_OUTSIDE) {
2497                         /* if inside, then move to surface */
2498                         if (dist <= data->dist) {
2499                                 clamp_surf= 1;
2500                                 sfac= data->dist / dist;
2501                         }
2502                         /* if soft-distance is enabled, start fading once owner is dist+softdist from the target */
2503                         else if (data->flag & LIMITDIST_USESOFT) {
2504                                 if (dist <= (data->dist + data->soft)) {
2505                                         
2506                                 }
2507                         }
2508                 }
2509                 else if (data->mode == LIMITDIST_INSIDE) {
2510                         /* if outside, then move to surface */
2511                         if (dist >= data->dist) {
2512                                 clamp_surf= 1;
2513                                 sfac= data->dist / dist;
2514                         }
2515                         /* if soft-distance is enabled, start fading once owner is dist-soft from the target */
2516                         else if (data->flag & LIMITDIST_USESOFT) {
2517                                 // FIXME: there's a problem with "jumping" when this kicks in
2518                                 if (dist >= (data->dist - data->soft)) {
2519                                         sfac = (float)( data->soft*(1.0 - exp(-(dist - data->dist)/data->soft)) + data->dist );
2520                                         sfac /= dist;
2521                                         
2522                                         clamp_surf= 1;
2523                                 }
2524                         }
2525                 }
2526                 else {
2527                         if (IS_EQ(dist, data->dist)==0) {
2528                                 clamp_surf= 1;
2529                                 sfac= data->dist / dist;
2530                         }
2531                 }
2532                 
2533                 /* clamp to 'surface' (i.e. move owner so that dist == data->dist) */
2534                 if (clamp_surf) {
2535                         /* simply interpolate along line formed by target -> owner */
2536                         VecLerpf(dvec, ct->matrix[3], cob->matrix[3], sfac);
2537                         
2538                         /* copy new vector onto owner */
2539                         VECCOPY(cob->matrix[3], dvec);
2540                 }
2541         }
2542 }
2543
2544 static bConstraintTypeInfo CTI_DISTLIMIT = {
2545         CONSTRAINT_TYPE_DISTLIMIT, /* type */
2546         sizeof(bDistLimitConstraint), /* size */
2547         "Limit Distance", /* name */
2548         "bDistLimitConstraint", /* struct name */
2549         NULL, /* free data */
2550         NULL, /* relink data */
2551         NULL, /* copy data */
2552         distlimit_new_data, /* new data */
2553         distlimit_get_tars, /* get constraint targets */
2554         distlimit_flush_tars, /* flush constraint targets */
2555         default_get_tarmat, /* get a target matrix */
2556         distlimit_evaluate /* evaluate */
2557 };
2558
2559 /* ---------- Stretch To ------------ */
2560
2561 static void stretchto_new_data (void *cdata)
2562 {
2563         bStretchToConstraint *data= (bStretchToConstraint *)cdata;
2564         
2565         data->volmode = 0;
2566         data->plane = 0;
2567         data->orglength = 0.0; 
2568         data->bulge = 1.0;
2569 }
2570
2571 static int stretchto_get_tars (bConstraint *con, ListBase *list)
2572 {
2573         if (con && list) {
2574                 bStretchToConstraint *data= con->data;
2575                 bConstraintTarget *ct;
2576                 
2577                 /* standard target-getting macro for single-target constraints */
2578                 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list)
2579                 
2580                 return 1;
2581         }
2582         
2583         return 0;
2584 }
2585
2586 static void stretchto_flush_tars (bConstraint *con, ListBase *list, short nocopy)
2587 {
2588         if (con && list) {
2589                 bStretchToConstraint *data= con->data;
2590                 bConstraintTarget *ct= list->first;
2591                 
2592                 /* the following macro is used for all standard single-target constraints */
2593                 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, nocopy)
2594         }
2595 }
2596
2597 static void stretchto_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
2598 {
2599         bStretchToConstraint *data= con->data;
2600         bConstraintTarget *ct= targets->first;
2601         
2602         /* only evaluate if there is a target */
2603         if (VALID_CONS_TARGET(ct)) {
2604                 float size[3], scale[3], vec[3], xx[3], zz[3], orth[3];
2605                 float totmat[3][3];
2606                 float tmat[4][4];
2607                 float dist;
2608                 
2609                 /* store scaling before destroying obmat */
2610                 Mat4ToSize(cob->matrix, size);
2611                 
2612                 /* store X orientation before destroying obmat */
2613                 xx[0] = cob->matrix[0][0];
2614                 xx[1] = cob->matrix[0][1];
2615                 xx[2] = cob->matrix[0][2];
2616                 Normalize(xx);
2617                 
2618                 /* store Z orientation before destroying obmat */
2619                 zz[0] = cob->matrix[2][0];
2620                 zz[1] = cob->matrix[2][1];
2621                 zz[2] = cob->matrix[2][2];
2622                 Normalize(zz);
2623                 
2624                 VecSubf(vec, cob->matrix[3], ct->matrix[3]);
2625                 vec[0] /= size[0];
2626                 vec[1] /= size[1];
2627                 vec[2] /= size[2];
2628                 
2629                 dist = Normalize(vec);
2630                 //dist = VecLenf( ob->obmat[3], targetmat[3]);
2631                 
2632                 /* data->orglength==0 occurs on first run, and after 'R' button is clicked */
2633                 if (data->orglength == 0)  
2634                         data->orglength = dist;
2635                 if (data->bulge == 0) 
2636                         data->bulge = 1.0;
2637                 
2638                 scale[1] = dist/data->orglength;
2639                 switch (data->volmode) {
2640                 /* volume preserving scaling */
2641                 case VOLUME_XZ :
2642                         scale[0] = 1.0f - (float)sqrt(data->bulge) + (float)sqrt(data->bulge*(data->orglength/dist));
2643                         scale[2] = scale[0];
2644                         break;
2645                 case VOLUME_X:
2646                         scale[0] = 1.0f + data->bulge * (data->orglength /dist - 1);
2647                         scale[2] = 1.0;
2648                         break;
2649                 case VOLUME_Z:
2650                         scale[0] = 1.0;
2651                         scale[2] = 1.0f + data->bulge * (data->orglength /dist - 1);
2652                         break;
2653                         /* don't care for volume */
2654                 case NO_VOLUME:
2655                         scale[0] = 1.0;
2656                         scale[2] = 1.0;
2657                         break;
2658                 default: /* should not happen, but in case*/
2659                         return;    
2660                 } /* switch (data->volmode) */
2661
2662                 /* Clear the object's rotation and scale */
2663                 cob->matrix[0][0]=size[0]*scale[0];
2664                 cob->matrix[0][1]=0;
2665                 cob->matrix[0][2]=0;
2666                 cob->matrix[1][0]=0;
2667                 cob->matrix[1][1]=size[1]*scale[1];
2668                 cob->matrix[1][2]=0;
2669                 cob->matrix[2][0]=0;
2670                 cob->matrix[2][1]=0;
2671                 cob->matrix[2][2]=size[2]*scale[2];
2672                 
2673                 VecSubf(vec, cob->matrix[3], ct->matrix[3]);
2674                 Normalize(vec);
2675                 
2676                 /* new Y aligns  object target connection*/
2677                 totmat[1][0] = -vec[0];
2678                 totmat[1][1] = -vec[1];
2679                 totmat[1][2] = -vec[2];
2680                 switch (data->plane) {
2681                 case PLANE_X:
2682                         /* build new Z vector */
2683                         /* othogonal to "new Y" "old X! plane */
2684                         Crossf(orth, vec, xx);
2685                         Normalize(orth);
2686                         
2687                         /* new Z*/
2688                         totmat[2][0] = orth[0];
2689                         totmat[2][1] = orth[1];
2690                         totmat[2][2] = orth[2];
2691                         
2692                         /* we decided to keep X plane*/
2693                         Crossf(xx, orth, vec);
2694                         Normalize(xx);
2695                         totmat[0][0] = xx[0];
2696                         totmat[0][1] = xx[1];
2697                         totmat[0][2] = xx[2];
2698                         break;
2699                 case PLANE_Z:
2700                         /* build new X vector */
2701                         /* othogonal to "new Y" "old Z! plane */
2702                         Crossf(orth, vec, zz);
2703                         Normalize(orth);
2704                         
2705                         /* new X */
2706                         totmat[0][0] = -orth[0];
2707                         totmat[0][1] = -orth[1];
2708                         totmat[0][2] = -orth[2];
2709                         
2710                         /* we decided to keep Z */
2711                         Crossf(zz, orth, vec);
2712                         Normalize(zz);
2713                         totmat[2][0] = zz[0];
2714                         totmat[2][1] = zz[1];
2715                         totmat[2][2] = zz[2];
2716                         break;
2717                 } /* switch (data->plane) */
2718                 
2719                 Mat4CpyMat4(tmat, cob->matrix);
2720                 Mat4MulMat34(cob->matrix, totmat, tmat);
2721         }
2722 }
2723
2724 static bConstraintTypeInfo CTI_STRETCHTO = {
2725         CONSTRAINT_TYPE_STRETCHTO, /* type */
2726         sizeof(bStretchToConstraint), /* size */
2727         "Stretch To", /* name */
2728         "bStretchToConstraint", /* struct name */
2729         NULL, /* free data */
2730         NULL, /* relink data */
2731         NULL, /* copy data */
2732         stretchto_new_data, /* new data */
2733         stretchto_get_tars, /* get constraint targets */
2734         stretchto_flush_tars, /* flush constraint targets */
2735         default_get_tarmat, /* get target matrix */
2736         stretchto_evaluate /* evaluate */
2737 };
2738
2739 /* ---------- Floor ------------ */
2740
2741 static void minmax_new_data (void *cdata)
2742 {
2743         bMinMaxConstraint *data= (bMinMaxConstraint *)cdata;
2744         
2745         data->minmaxflag = TRACK_Z;
2746         data->offset = 0.0f;
2747         data->cache[0] = data->cache[1] = data->cache[2] = 0.0f;
2748         data->flag = 0;
2749 }
2750
2751 static int minmax_get_tars (bConstraint *con, ListBase *list)
2752 {
2753         if (con && list) {
2754                 bMinMaxConstraint *data= con->data;
2755                 bConstraintTarget *ct;
2756                 
2757                 /* standard target-getting macro for single-target constraints */
2758                 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list)
2759                 
2760                 return 1;
2761         }
2762         
2763         return 0;
2764 }
2765
2766 static void minmax_flush_tars (bConstraint *con, ListBase *list, short nocopy)
2767 {
2768         if (con && list) {
2769                 bMinMaxConstraint *data= con->data;
2770                 bConstraintTarget *ct= list->first;
2771                 
2772                 /* the following macro is used for all standard single-target constraints */
2773                 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, nocopy)
2774         }
2775 }
2776
2777 static void minmax_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
2778 {
2779         bMinMaxConstraint *data= con->data;
2780         bConstraintTarget *ct= targets->first;
2781         
2782         /* only evaluate if there is a target */
2783         if (VALID_CONS_TARGET(ct)) {
2784                 float obmat[4][4], imat[4][4], tarmat[4][4], tmat[4][4];
2785                 float val1, val2;
2786                 int index;
2787                 
2788                 Mat4CpyMat4(obmat, cob->matrix);
2789                 Mat4CpyMat4(tarmat, ct->matrix);
2790                 
2791                 if (data->flag & MINMAX_USEROT) {
2792                         /* take rotation of target into account by doing the transaction in target's localspace */
2793                         Mat4Invert(imat, tarmat);
2794                         Mat4MulMat4(tmat, obmat, imat);
2795                         Mat4CpyMat4(obmat, tmat);
2796                         Mat4One(tarmat);
2797                 }
2798                 
2799                 switch (data->minmaxflag) {
2800                 case TRACK_Z:
2801                         val1 = tarmat[3][2];
2802                         val2 = obmat[3][2]-data->offset;
2803                         index = 2;
2804                         break;
2805                 case TRACK_Y:
2806                         val1 = tarmat[3][1];
2807                         val2 = obmat[3][1]-data->offset;
2808                         index = 1;
2809                         break;
2810                 case TRACK_X:
2811                         val1 = tarmat[3][0];
2812                         val2 = obmat[3][0]-data->offset;
2813                         index = 0;
2814                         break;
2815                 case TRACK_nZ:
2816                         val2 = tarmat[3][2];
2817                         val1 = obmat[3][2]-data->offset;
2818                         index = 2;
2819                         break;
2820                 case TRACK_nY:
2821                         val2 = tarmat[3][1];
2822                         val1 = obmat[3][1]-data->offset;
2823                         index = 1;
2824                         break;
2825                 case TRACK_nX:
2826                         val2 = tarmat[3][0];
2827                         val1 = obmat[3][0]-data->offset;
2828                         index = 0;
2829                         break;
2830                 default:
2831                         return;
2832                 }
2833                 
2834                 if (val1 > val2) {
2835                         obmat[3][index] = tarmat[3][index] + data->offset;
2836                         if (data->flag & MINMAX_STICKY) {
2837                                 if (data->flag & MINMAX_STUCK) {
2838                                         VECCOPY(obmat[3], data->cache);
2839                                 } 
2840                                 else {
2841                                         VECCOPY(data->cache, obmat[3]);
2842                                         data->flag |= MINMAX_STUCK;
2843                                 }
2844                         }
2845                         if (data->flag & MINMAX_USEROT) {
2846                                 /* get out of localspace */
2847                                 Mat4MulMat4(tmat, obmat, ct->matrix);
2848                                 Mat4CpyMat4(cob->matrix, tmat);
2849                         } 
2850                         else {                  
2851                                 VECCOPY(cob->matrix[3], obmat[3]);
2852                         }
2853                 } 
2854                 else {
2855                         data->flag &= ~MINMAX_STUCK;
2856                 }
2857         }
2858 }
2859
2860 static bConstraintTypeInfo CTI_MINMAX = {
2861         CONSTRAINT_TYPE_MINMAX, /* type */
2862         sizeof(bMinMaxConstraint), /* size */
2863         "Floor", /* name */
2864         "bMinMaxConstraint", /* struct name */
2865         NULL, /* free data */
2866         NULL, /* relink data */
2867         NULL, /* copy data */
2868         minmax_new_data, /* new data */
2869         minmax_get_tars, /* get constraint targets */
2870         minmax_flush_tars, /* flush constraint targets */
2871         default_get_tarmat, /* get target matrix */
2872         minmax_evaluate /* evaluate */
2873 };
2874
2875 /* ------- RigidBody Joint ---------- */
2876
2877 static void rbj_new_data (void *cdata)
2878 {
2879         bRigidBodyJointConstraint *data= (bRigidBodyJointConstraint *)cdata;
2880         
2881         // removed code which set target of this constraint  
2882     data->type=1;
2883 }
2884
2885 static int rbj_get_tars (bConstraint *con, ListBase *list)
2886 {
2887         if (con && list) {
2888                 bRigidBodyJointConstraint *data= con->data;
2889                 bConstraintTarget *ct;
2890                 
2891                 /* standard target-getting macro for single-target constraints without subtargets */
2892                 SINGLETARGETNS_GET_TARS(con, data->tar, ct, list)
2893                 
2894                 return 1;
2895         }
2896         
2897         return 0;
2898 }
2899
2900 static void rbj_flush_tars (bConstraint *con, ListBase *list, short nocopy)
2901 {
2902         if (con && list) {
2903                 bRigidBodyJointConstraint *data= con->data;
2904                 bConstraintTarget *ct= list->first;
2905