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