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