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