Use curve radius for paths
[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])) ct->type = CONSTRAINT_OBTYPE_BONE; \
689                         else if (ELEM(ct->tar->type, OB_MESH, OB_LATTICE) && (ct->subtarget[0])) ct->type = CONSTRAINT_OBTYPE_VERT; \
690                         else ct->type = CONSTRAINT_OBTYPE_OBJECT; \
691                 } \
692                  \
693                 BLI_addtail(list, ct); \
694         }
695         
696 /* This following macro should be used for all standard single-target *_get_tars functions 
697  * to save typing and reduce maintainance woes. It does not do the subtarget related operations
698  * (Hopefully all compilers will be happy with the lines with just a space on them. Those are
699  *  really just to help this code easier to read)
700  */
701 // TODO: cope with getting rotation order...
702 #define SINGLETARGETNS_GET_TARS(con, datatar, ct, list) \
703         { \
704                 ct= MEM_callocN(sizeof(bConstraintTarget), "tempConstraintTarget"); \
705                  \
706                 ct->tar= datatar; \
707                 ct->space= con->tarspace; \
708                 ct->flag= CONSTRAINT_TAR_TEMP; \
709                  \
710                 if (ct->tar) ct->type = CONSTRAINT_OBTYPE_OBJECT; \
711                  \
712                 BLI_addtail(list, ct); \
713         }
714
715 /* This following macro should be used for all standard single-target *_flush_tars functions
716  * to save typing and reduce maintainance woes.
717  * Note: the pointer to ct will be changed to point to the next in the list (as it gets removed)
718  * (Hopefully all compilers will be happy with the lines with just a space on them. Those are
719  *  really just to help this code easier to read)
720  */
721 #define SINGLETARGET_FLUSH_TARS(con, datatar, datasubtarget, ct, list, nocopy) \
722         { \
723                 if (ct) { \
724                         bConstraintTarget *ctn = ct->next; \
725                         if (nocopy == 0) { \
726                                 datatar= ct->tar; \
727                                 strcpy(datasubtarget, ct->subtarget); \
728                                 con->tarspace= (char)ct->space; \
729                         } \
730                          \
731                         BLI_freelinkN(list, ct); \
732                         ct= ctn; \
733                 } \
734         }
735         
736 /* This following macro should be used for all standard single-target *_flush_tars functions
737  * to save typing and reduce maintainance woes. It does not do the subtarget related operations.
738  * Note: the pointer to ct will be changed to point to the next in the list (as it gets removed)
739  * (Hopefully all compilers will be happy with the lines with just a space on them. Those are
740  *  really just to help this code easier to read)
741  */
742 #define SINGLETARGETNS_FLUSH_TARS(con, datatar, ct, list, nocopy) \
743         { \
744                 if (ct) { \
745                         bConstraintTarget *ctn = ct->next; \
746                         if (nocopy == 0) { \
747                                 datatar= ct->tar; \
748                                 con->tarspace= (char)ct->space; \
749                         } \
750                          \
751                         BLI_freelinkN(list, ct); \
752                         ct= ctn; \
753                 } \
754         }
755  
756 /* --------- ChildOf Constraint ------------ */
757
758 static void childof_new_data (void *cdata)
759 {
760         bChildOfConstraint *data= (bChildOfConstraint *)cdata;
761         
762         data->flag = (CHILDOF_LOCX | CHILDOF_LOCY | CHILDOF_LOCZ |
763                                         CHILDOF_ROTX |CHILDOF_ROTY | CHILDOF_ROTZ |
764                                         CHILDOF_SIZEX | CHILDOF_SIZEY | CHILDOF_SIZEZ);
765         Mat4One(data->invmat);
766 }
767
768 static int childof_get_tars (bConstraint *con, ListBase *list)
769 {
770         if (con && list) {
771                 bChildOfConstraint *data= con->data;
772                 bConstraintTarget *ct;
773                 
774                 /* standard target-getting macro for single-target constraints */
775                 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list)
776                 
777                 return 1;
778         }
779         
780         return 0;
781 }
782
783 static void childof_flush_tars (bConstraint *con, ListBase *list, short nocopy)
784 {
785         if (con && list) {
786                 bChildOfConstraint *data= con->data;
787                 bConstraintTarget *ct= list->first;
788                 
789                 /* the following macro is used for all standard single-target constraints */
790                 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, nocopy)
791         }
792 }
793
794 static void childof_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
795 {
796         bChildOfConstraint *data= con->data;
797         bConstraintTarget *ct= targets->first;
798         
799         /* only evaluate if there is a target */
800         if (VALID_CONS_TARGET(ct)) {
801                 float parmat[4][4], invmat[4][4], tempmat[4][4];
802                 float loc[3], eul[3], size[3];
803                 float loco[3], eulo[3], sizo[3];
804                 
805                 /* get offset (parent-inverse) matrix */
806                 Mat4CpyMat4(invmat, data->invmat);
807                 
808                 /* extract components of both matrices */
809                 VECCOPY(loc, ct->matrix[3]);
810                 Mat4ToEulO(ct->matrix, eul, ct->rotOrder);
811                 Mat4ToSize(ct->matrix, size);
812                 
813                 VECCOPY(loco, invmat[3]);
814                 Mat4ToEulO(invmat, eulo, cob->rotOrder);
815                 Mat4ToSize(invmat, sizo);
816                 
817                 /* disable channels not enabled */
818                 if (!(data->flag & CHILDOF_LOCX)) loc[0]= loco[0]= 0.0f;
819                 if (!(data->flag & CHILDOF_LOCY)) loc[1]= loco[1]= 0.0f;
820                 if (!(data->flag & CHILDOF_LOCZ)) loc[2]= loco[2]= 0.0f;
821                 if (!(data->flag & CHILDOF_ROTX)) eul[0]= eulo[0]= 0.0f;
822                 if (!(data->flag & CHILDOF_ROTY)) eul[1]= eulo[1]= 0.0f;
823                 if (!(data->flag & CHILDOF_ROTZ)) eul[2]= eulo[2]= 0.0f;
824                 if (!(data->flag & CHILDOF_SIZEX)) size[0]= sizo[0]= 1.0f;
825                 if (!(data->flag & CHILDOF_SIZEY)) size[1]= sizo[1]= 1.0f;
826                 if (!(data->flag & CHILDOF_SIZEZ)) size[2]= sizo[2]= 1.0f;
827                 
828                 /* make new target mat and offset mat */
829                 LocEulOSizeToMat4(ct->matrix, loc, eul, size, ct->rotOrder);
830                 LocEulOSizeToMat4(invmat, loco, eulo, sizo, cob->rotOrder);
831                 
832                 /* multiply target (parent matrix) by offset (parent inverse) to get 
833                  * the effect of the parent that will be exherted on the owner
834                  */
835                 Mat4MulMat4(parmat, invmat, ct->matrix);
836                 
837                 /* now multiply the parent matrix by the owner matrix to get the 
838                  * the effect of this constraint (i.e.  owner is 'parented' to parent)
839                  */
840                 Mat4CpyMat4(tempmat, cob->matrix);
841                 Mat4MulMat4(cob->matrix, tempmat, parmat); 
842         }
843 }
844
845 static bConstraintTypeInfo CTI_CHILDOF = {
846         CONSTRAINT_TYPE_CHILDOF, /* type */
847         sizeof(bChildOfConstraint), /* size */
848         "ChildOf", /* name */
849         "bChildOfConstraint", /* struct name */
850         NULL, /* free data */
851         NULL, /* relink data */
852         NULL, /* copy data */
853         childof_new_data, /* new data */
854         childof_get_tars, /* get constraint targets */
855         childof_flush_tars, /* flush constraint targets */
856         default_get_tarmat, /* get a target matrix */
857         childof_evaluate /* evaluate */
858 };
859
860 /* -------- TrackTo Constraint ------- */
861
862 static void trackto_new_data (void *cdata)
863 {
864         bTrackToConstraint *data= (bTrackToConstraint *)cdata;
865         
866         data->reserved1 = TRACK_Y;
867         data->reserved2 = UP_Z;
868 }       
869
870 static int trackto_get_tars (bConstraint *con, ListBase *list)
871 {
872         if (con && list) {
873                 bTrackToConstraint *data= con->data;
874                 bConstraintTarget *ct;
875                 
876                 /* standard target-getting macro for single-target constraints */
877                 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list)
878                 
879                 return 1;
880         }
881         
882         return 0;
883 }
884
885 static void trackto_flush_tars (bConstraint *con, ListBase *list, short nocopy)
886 {
887         if (con && list) {
888                 bTrackToConstraint *data= con->data;
889                 bConstraintTarget *ct= list->first;
890                 
891                 /* the following macro is used for all standard single-target constraints */
892                 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, nocopy)
893         }
894 }
895
896
897 static int basis_cross (int n, int m)
898 {
899         switch (n-m) {
900                 case 1: 
901                 case -2:
902                         return 1;
903                         
904                 case -1: 
905                 case 2:
906                         return -1;
907                         
908                 default:
909                         return 0;
910         }
911 }
912
913 static void vectomat (float *vec, float *target_up, short axis, short upflag, short flags, float m[][3])
914 {
915         float n[3];
916         float u[3]; /* vector specifying the up axis */
917         float proj[3];
918         float right[3];
919         float neg = -1;
920         int right_index;
921         
922         VecCopyf(n, vec);
923         if (Normalize(n) == 0.0) { 
924                 n[0] = 0.0;
925                 n[1] = 0.0;
926                 n[2] = 1.0;
927         }
928         if (axis > 2) axis -= 3;
929         else VecNegf(n);
930
931         /* n specifies the transformation of the track axis */
932         if (flags & TARGET_Z_UP) { 
933                 /* target Z axis is the global up axis */
934                 u[0] = target_up[0];
935                 u[1] = target_up[1];
936                 u[2] = target_up[2];
937         }
938         else { 
939                 /* world Z axis is the global up axis */
940                 u[0] = 0;
941                 u[1] = 0;
942                 u[2] = 1;
943         }
944
945         /* project the up vector onto the plane specified by n */
946         Projf(proj, u, n); /* first u onto n... */
947         VecSubf(proj, u, proj); /* then onto the plane */
948         /* proj specifies the transformation of the up axis */
949
950         if (Normalize(proj) == 0.0) { /* degenerate projection */
951                 proj[0] = 0.0;
952                 proj[1] = 1.0;
953                 proj[2] = 0.0;
954         }
955
956         /* Normalized cross product of n and proj specifies transformation of the right axis */
957         Crossf(right, proj, n);
958         Normalize(right);
959
960         if (axis != upflag) {
961                 right_index = 3 - axis - upflag;
962                 neg = (float)basis_cross(axis, upflag);
963                 
964                 /* account for up direction, track direction */
965                 m[right_index][0] = neg * right[0];
966                 m[right_index][1] = neg * right[1];
967                 m[right_index][2] = neg * right[2];
968                 
969                 m[upflag][0] = proj[0];
970                 m[upflag][1] = proj[1];
971                 m[upflag][2] = proj[2];
972                 
973                 m[axis][0] = n[0];
974                 m[axis][1] = n[1];
975                 m[axis][2] = n[2];
976         }
977         /* identity matrix - don't do anything if the two axes are the same */
978         else {
979                 m[0][0]= m[1][1]= m[2][2]= 1.0;
980                 m[0][1]= m[0][2]= 0.0;
981                 m[1][0]= m[1][2]= 0.0;
982                 m[2][0]= m[2][1]= 0.0;
983         }
984 }
985
986
987 static void trackto_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
988 {
989         bTrackToConstraint *data= con->data;
990         bConstraintTarget *ct= targets->first;
991         
992         if (VALID_CONS_TARGET(ct)) {
993                 float size[3], vec[3];
994                 float totmat[3][3];
995                 float tmat[4][4];
996                 
997                 /* Get size property, since ob->size is only the object's own relative size, not its global one */
998                 Mat4ToSize(cob->matrix, size);
999                 
1000                 /* Clear the object's rotation */       
1001                 cob->matrix[0][0]=size[0];
1002                 cob->matrix[0][1]=0;
1003                 cob->matrix[0][2]=0;
1004                 cob->matrix[1][0]=0;
1005                 cob->matrix[1][1]=size[1];
1006                 cob->matrix[1][2]=0;
1007                 cob->matrix[2][0]=0;
1008                 cob->matrix[2][1]=0;
1009                 cob->matrix[2][2]=size[2];
1010                 
1011                 /* targetmat[2] instead of ownermat[2] is passed to vectomat
1012                  * for backwards compatability it seems... (Aligorith)
1013                  */
1014                 VecSubf(vec, cob->matrix[3], ct->matrix[3]);
1015                 vectomat(vec, ct->matrix[2], 
1016                                 (short)data->reserved1, (short)data->reserved2, 
1017                                 data->flags, totmat);
1018                 
1019                 Mat4CpyMat4(tmat, cob->matrix);
1020                 Mat4MulMat34(cob->matrix, totmat, tmat);
1021         }
1022 }
1023
1024 static bConstraintTypeInfo CTI_TRACKTO = {
1025         CONSTRAINT_TYPE_TRACKTO, /* type */
1026         sizeof(bTrackToConstraint), /* size */
1027         "TrackTo", /* name */
1028         "bTrackToConstraint", /* struct name */
1029         NULL, /* free data */
1030         NULL, /* relink data */
1031         NULL, /* copy data */
1032         trackto_new_data, /* new data */
1033         trackto_get_tars, /* get constraint targets */
1034         trackto_flush_tars, /* flush constraint targets */
1035         default_get_tarmat, /* get target matrix */
1036         trackto_evaluate /* evaluate */
1037 };
1038
1039 /* --------- Inverse-Kinemetics --------- */
1040
1041 static void kinematic_new_data (void *cdata)
1042 {
1043         bKinematicConstraint *data= (bKinematicConstraint *)cdata;
1044         
1045         data->weight= (float)1.0;
1046         data->orientweight= (float)1.0;
1047         data->iterations = 500;
1048         data->flag= CONSTRAINT_IK_TIP|CONSTRAINT_IK_STRETCH|CONSTRAINT_IK_POS;
1049 }
1050
1051 static int kinematic_get_tars (bConstraint *con, ListBase *list)
1052 {
1053         if (con && list) {
1054                 bKinematicConstraint *data= con->data;
1055                 bConstraintTarget *ct;
1056                 
1057                 /* standard target-getting macro for single-target constraints is used twice here */
1058                 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list)
1059                 SINGLETARGET_GET_TARS(con, data->poletar, data->polesubtarget, ct, list)
1060                 
1061                 return 2;
1062         }
1063         
1064         return 0;
1065 }
1066
1067 static void kinematic_flush_tars (bConstraint *con, ListBase *list, short nocopy)
1068 {
1069         if (con && list) {
1070                 bKinematicConstraint *data= con->data;
1071                 bConstraintTarget *ct= list->first;
1072                 
1073                 /* the following macro is used for all standard single-target constraints */
1074                 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, nocopy)
1075                 SINGLETARGET_FLUSH_TARS(con, data->poletar, data->polesubtarget, ct, list, nocopy)
1076         }
1077 }
1078
1079 static void kinematic_get_tarmat (bConstraint *con, bConstraintOb *cob, bConstraintTarget *ct, float ctime)
1080 {
1081         bKinematicConstraint *data= con->data;
1082         
1083         if (VALID_CONS_TARGET(ct)) 
1084                 constraint_target_to_mat4(cob->scene, ct->tar, ct->subtarget, ct->matrix, CONSTRAINT_SPACE_WORLD, ct->space, con->headtail);
1085         else if (ct) {
1086                 if (data->flag & CONSTRAINT_IK_AUTO) {
1087                         Object *ob= cob->ob;
1088                         
1089                         if (ob == NULL) {
1090                                 Mat4One(ct->matrix);
1091                         }
1092                         else {
1093                                 float vec[3];
1094                                 /* move grabtarget into world space */
1095                                 VECCOPY(vec, data->grabtarget);
1096                                 Mat4MulVecfl(ob->obmat, vec);
1097                                 Mat4CpyMat4(ct->matrix, ob->obmat);
1098                                 VECCOPY(ct->matrix[3], vec);
1099                         }
1100                 }
1101                 else
1102                         Mat4One(ct->matrix);
1103         }
1104 }
1105
1106 static bConstraintTypeInfo CTI_KINEMATIC = {
1107         CONSTRAINT_TYPE_KINEMATIC, /* type */
1108         sizeof(bKinematicConstraint), /* size */
1109         "IK", /* name */
1110         "bKinematicConstraint", /* struct name */
1111         NULL, /* free data */
1112         NULL, /* relink data */
1113         NULL, /* copy data */
1114         kinematic_new_data, /* new data */
1115         kinematic_get_tars, /* get constraint targets */
1116         kinematic_flush_tars, /* flush constraint targets */
1117         kinematic_get_tarmat, /* get target matrix */
1118         NULL /* evaluate - solved as separate loop */
1119 };
1120
1121 /* -------- Follow-Path Constraint ---------- */
1122
1123 static void followpath_new_data (void *cdata)
1124 {
1125         bFollowPathConstraint *data= (bFollowPathConstraint *)cdata;
1126         
1127         data->trackflag = TRACK_Y;
1128         data->upflag = UP_Z;
1129         data->offset = 0;
1130         data->followflag = 0;
1131 }
1132
1133 static int followpath_get_tars (bConstraint *con, ListBase *list)
1134 {
1135         if (con && list) {
1136                 bFollowPathConstraint *data= con->data;
1137                 bConstraintTarget *ct;
1138                 
1139                 /* standard target-getting macro for single-target constraints without subtargets */
1140                 SINGLETARGETNS_GET_TARS(con, data->tar, ct, list)
1141                 
1142                 return 1;
1143         }
1144         
1145         return 0;
1146 }
1147
1148 static void followpath_flush_tars (bConstraint *con, ListBase *list, short nocopy)
1149 {
1150         if (con && list) {
1151                 bFollowPathConstraint *data= con->data;
1152                 bConstraintTarget *ct= list->first;
1153                 
1154                 /* the following macro is used for all standard single-target constraints */
1155                 SINGLETARGETNS_FLUSH_TARS(con, data->tar, ct, list, nocopy)
1156         }
1157 }
1158
1159 static void followpath_get_tarmat (bConstraint *con, bConstraintOb *cob, bConstraintTarget *ct, float ctime)
1160 {
1161         bFollowPathConstraint *data= con->data;
1162         
1163         if (VALID_CONS_TARGET(ct)) {
1164                 Curve *cu= ct->tar->data;
1165                 float q[4], vec[4], dir[3], quat[4], radius, x1;
1166                 float totmat[4][4];
1167                 float curvetime;
1168                 
1169                 Mat4One(totmat);
1170                 Mat4One(ct->matrix);
1171                 
1172                 /* note: when creating constraints that follow path, the curve gets the CU_PATH set now,
1173                  *              currently for paths to work it needs to go through the bevlist/displist system (ton) 
1174                  */
1175                 
1176                 /* only happens on reload file, but violates depsgraph still... fix! */
1177                 if (cu->path==NULL || cu->path->data==NULL)
1178                         makeDispListCurveTypes(cob->scene, ct->tar, 0);
1179                 
1180                 if (cu->path && cu->path->data) {
1181                         if ((data->followflag & FOLLOWPATH_STATIC) == 0) { 
1182                                 /* animated position along curve depending on time */
1183                                 curvetime= bsystem_time(cob->scene, ct->tar, ctime, 0.0) - data->offset;
1184                                 
1185                                 /* ctime is now a proper var setting of Curve which gets set by Animato like any other var that's animated,
1186                                  * but this will only work if it actually is animated... 
1187                                  *
1188                                  * we firstly calculate the modulus of cu->ctime/cu->pathlen to clamp ctime within the 0.0 to 1.0 times pathlen
1189                                  * range, then divide this (the modulus) by pathlen to get a value between 0.0 and 1.0
1190                                  */
1191                                 curvetime= fmod(cu->ctime, cu->pathlen) / cu->pathlen;
1192                                 CLAMP(curvetime, 0.0, 1.0);
1193                         }
1194                         else {
1195                                 /* fixed position along curve */
1196                                 curvetime= data->offset; // XXX might need a more sensible value
1197                         }
1198                         
1199                         if ( where_on_path(ct->tar, curvetime, vec, dir, NULL, &radius) ) {
1200                                 if (data->followflag & FOLLOWPATH_FOLLOW) {
1201                                         vectoquat(dir, (short) data->trackflag, (short) data->upflag, quat);
1202                                         
1203                                         Normalize(dir);
1204                                         q[0]= (float)cos(0.5*vec[3]);
1205                                         x1= (float)sin(0.5*vec[3]);
1206                                         q[1]= -x1*dir[0];
1207                                         q[2]= -x1*dir[1];
1208                                         q[3]= -x1*dir[2];
1209                                         QuatMul(quat, q, quat);
1210                                         
1211                                         QuatToMat4(quat, totmat);
1212                                 }
1213
1214                                 if (data->followflag & FOLLOWPATH_RADIUS) {
1215                                         float tmat[4][4], rmat[4][4];
1216                                         Mat4Scale(tmat, radius);
1217                                         Mat4MulMat4(rmat, totmat, tmat);
1218                                         Mat4CpyMat4(totmat, rmat);
1219                                 }
1220
1221                                 VECCOPY(totmat[3], vec);
1222                                 
1223                                 Mat4MulSerie(ct->matrix, ct->tar->obmat, totmat, NULL, NULL, NULL, NULL, NULL, NULL);
1224                         }
1225                 }
1226         }
1227         else if (ct)
1228                 Mat4One(ct->matrix);
1229 }
1230
1231 static void followpath_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
1232 {
1233         bConstraintTarget *ct= targets->first;
1234         
1235         /* only evaluate if there is a target */
1236         if (VALID_CONS_TARGET(ct)) {
1237                 float obmat[4][4];
1238                 float size[3];
1239                 bFollowPathConstraint *data= con->data;
1240                 
1241                 /* get Object local transform (loc/rot/size) to determine transformation from path */
1242                 //object_to_mat4(ob, obmat);
1243                 Mat4CpyMat4(obmat, cob->matrix); // FIXME!!!
1244                 
1245                 /* get scaling of object before applying constraint */
1246                 Mat4ToSize(cob->matrix, size);
1247                 
1248                 /* apply targetmat - containing location on path, and rotation */
1249                 Mat4MulSerie(cob->matrix, ct->matrix, obmat, NULL, NULL, NULL, NULL, NULL, NULL);
1250                 
1251                 /* un-apply scaling caused by path */
1252                 if ((data->followflag & FOLLOWPATH_RADIUS)==0) { /* XXX - assume that scale correction means that radius will have some scale error in it - Campbell */
1253                         float obsize[3];
1254
1255                         Mat4ToSize(cob->matrix, obsize);
1256                         if (obsize[0])
1257                                 VecMulf(cob->matrix[0], size[0] / obsize[0]);
1258                         if (obsize[1])
1259                                 VecMulf(cob->matrix[1], size[1] / obsize[1]);
1260                         if (obsize[2])
1261                                 VecMulf(cob->matrix[2], size[2] / obsize[2]);
1262                 }
1263         }
1264 }
1265
1266 static bConstraintTypeInfo CTI_FOLLOWPATH = {
1267         CONSTRAINT_TYPE_FOLLOWPATH, /* type */
1268         sizeof(bFollowPathConstraint), /* size */
1269         "Follow Path", /* name */
1270         "bFollowPathConstraint", /* struct name */
1271         NULL, /* free data */
1272         NULL, /* relink data */
1273         NULL, /* copy data */
1274         followpath_new_data, /* new data */
1275         followpath_get_tars, /* get constraint targets */
1276         followpath_flush_tars, /* flush constraint targets */
1277         followpath_get_tarmat, /* get target matrix */
1278         followpath_evaluate /* evaluate */
1279 };
1280
1281 /* --------- Limit Location --------- */
1282
1283
1284 static void loclimit_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
1285 {
1286         bLocLimitConstraint *data = con->data;
1287         
1288         if (data->flag & LIMIT_XMIN) {
1289                 if (cob->matrix[3][0] < data->xmin)
1290                         cob->matrix[3][0] = data->xmin;
1291         }
1292         if (data->flag & LIMIT_XMAX) {
1293                 if (cob->matrix[3][0] > data->xmax)
1294                         cob->matrix[3][0] = data->xmax;
1295         }
1296         if (data->flag & LIMIT_YMIN) {
1297                 if (cob->matrix[3][1] < data->ymin)
1298                         cob->matrix[3][1] = data->ymin;
1299         }
1300         if (data->flag & LIMIT_YMAX) {
1301                 if (cob->matrix[3][1] > data->ymax)
1302                         cob->matrix[3][1] = data->ymax;
1303         }
1304         if (data->flag & LIMIT_ZMIN) {
1305                 if (cob->matrix[3][2] < data->zmin) 
1306                         cob->matrix[3][2] = data->zmin;
1307         }
1308         if (data->flag & LIMIT_ZMAX) {
1309                 if (cob->matrix[3][2] > data->zmax)
1310                         cob->matrix[3][2] = data->zmax;
1311         }
1312 }
1313
1314 static bConstraintTypeInfo CTI_LOCLIMIT = {
1315         CONSTRAINT_TYPE_LOCLIMIT, /* type */
1316         sizeof(bLocLimitConstraint), /* size */
1317         "Limit Location", /* name */
1318         "bLocLimitConstraint", /* struct name */
1319         NULL, /* free data */
1320         NULL, /* relink data */
1321         NULL, /* copy data */
1322         NULL, /* new data */
1323         NULL, /* get constraint targets */
1324         NULL, /* flush constraint targets */
1325         NULL, /* get target matrix */
1326         loclimit_evaluate /* evaluate */
1327 };
1328
1329 /* -------- Limit Rotation --------- */
1330
1331 static void rotlimit_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
1332 {
1333         bRotLimitConstraint *data = con->data;
1334         float loc[3];
1335         float eul[3];
1336         float size[3];
1337         
1338         VECCOPY(loc, cob->matrix[3]);
1339         Mat4ToSize(cob->matrix, size);
1340         
1341         Mat4ToEulO(cob->matrix, eul, cob->rotOrder);
1342         
1343         /* eulers: radians to degrees! */
1344         eul[0] = (float)(eul[0] / M_PI * 180);
1345         eul[1] = (float)(eul[1] / M_PI * 180);
1346         eul[2] = (float)(eul[2] / M_PI * 180);
1347         
1348         /* limiting of euler values... */
1349         if (data->flag & LIMIT_XROT) {
1350                 if (eul[0] < data->xmin) 
1351                         eul[0] = data->xmin;
1352                         
1353                 if (eul[0] > data->xmax)
1354                         eul[0] = data->xmax;
1355         }
1356         if (data->flag & LIMIT_YROT) {
1357                 if (eul[1] < data->ymin)
1358                         eul[1] = data->ymin;
1359                         
1360                 if (eul[1] > data->ymax)
1361                         eul[1] = data->ymax;
1362         }
1363         if (data->flag & LIMIT_ZROT) {
1364                 if (eul[2] < data->zmin)
1365                         eul[2] = data->zmin;
1366                         
1367                 if (eul[2] > data->zmax)
1368                         eul[2] = data->zmax;
1369         }
1370                 
1371         /* eulers: degrees to radians ! */
1372         eul[0] = (float)(eul[0] / 180 * M_PI); 
1373         eul[1] = (float)(eul[1] / 180 * M_PI);
1374         eul[2] = (float)(eul[2] / 180 * M_PI);
1375         
1376         LocEulOSizeToMat4(cob->matrix, loc, eul, size, cob->rotOrder);
1377 }
1378
1379 static bConstraintTypeInfo CTI_ROTLIMIT = {
1380         CONSTRAINT_TYPE_ROTLIMIT, /* type */
1381         sizeof(bRotLimitConstraint), /* size */
1382         "Limit Rotation", /* name */
1383         "bRotLimitConstraint", /* struct name */
1384         NULL, /* free data */
1385         NULL, /* relink data */
1386         NULL, /* copy data */
1387         NULL, /* new data */
1388         NULL, /* get constraint targets */
1389         NULL, /* flush constraint targets */
1390         NULL, /* get target matrix */
1391         rotlimit_evaluate /* evaluate */
1392 };
1393
1394 /* --------- Limit Scaling --------- */
1395
1396
1397 static void sizelimit_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
1398 {
1399         bSizeLimitConstraint *data = con->data;
1400         float obsize[3], size[3];
1401         
1402         Mat4ToSize(cob->matrix, size);
1403         Mat4ToSize(cob->matrix, obsize);
1404         
1405         if (data->flag & LIMIT_XMIN) {
1406                 if (size[0] < data->xmin) 
1407                         size[0] = data->xmin;   
1408         }
1409         if (data->flag & LIMIT_XMAX) {
1410                 if (size[0] > data->xmax) 
1411                         size[0] = data->xmax;
1412         }
1413         if (data->flag & LIMIT_YMIN) {
1414                 if (size[1] < data->ymin) 
1415                         size[1] = data->ymin;   
1416         }
1417         if (data->flag & LIMIT_YMAX) {
1418                 if (size[1] > data->ymax) 
1419                         size[1] = data->ymax;
1420         }
1421         if (data->flag & LIMIT_ZMIN) {
1422                 if (size[2] < data->zmin) 
1423                         size[2] = data->zmin;   
1424         }
1425         if (data->flag & LIMIT_ZMAX) {
1426                 if (size[2] > data->zmax) 
1427                         size[2] = data->zmax;
1428         }
1429         
1430         if (obsize[0]) 
1431                 VecMulf(cob->matrix[0], size[0]/obsize[0]);
1432         if (obsize[1]) 
1433                 VecMulf(cob->matrix[1], size[1]/obsize[1]);
1434         if (obsize[2]) 
1435                 VecMulf(cob->matrix[2], size[2]/obsize[2]);
1436 }
1437
1438 static bConstraintTypeInfo CTI_SIZELIMIT = {
1439         CONSTRAINT_TYPE_SIZELIMIT, /* type */
1440         sizeof(bSizeLimitConstraint), /* size */
1441         "Limit Scaling", /* name */
1442         "bSizeLimitConstraint", /* struct name */
1443         NULL, /* free data */
1444         NULL, /* relink data */
1445         NULL, /* copy data */
1446         NULL, /* new data */
1447         NULL, /* get constraint targets */
1448         NULL, /* flush constraint targets */
1449         NULL, /* get target matrix */
1450         sizelimit_evaluate /* evaluate */
1451 };
1452
1453 /* ----------- Copy Location ------------- */
1454
1455 static void loclike_new_data (void *cdata)
1456 {
1457         bLocateLikeConstraint *data= (bLocateLikeConstraint *)cdata;
1458         
1459         data->flag = LOCLIKE_X|LOCLIKE_Y|LOCLIKE_Z;
1460 }
1461
1462 static int loclike_get_tars (bConstraint *con, ListBase *list)
1463 {
1464         if (con && list) {
1465                 bLocateLikeConstraint *data= con->data;
1466                 bConstraintTarget *ct;
1467                 
1468                 /* standard target-getting macro for single-target constraints */
1469                 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list)
1470                 
1471                 return 1;
1472         }
1473         
1474         return 0;
1475 }
1476
1477 static void loclike_flush_tars (bConstraint *con, ListBase *list, short nocopy)
1478 {
1479         if (con && list) {
1480                 bLocateLikeConstraint *data= con->data;
1481                 bConstraintTarget *ct= list->first;
1482                 
1483                 /* the following macro is used for all standard single-target constraints */
1484                 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, nocopy)
1485         }
1486 }
1487
1488 static void loclike_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
1489 {
1490         bLocateLikeConstraint *data= con->data;
1491         bConstraintTarget *ct= targets->first;
1492         
1493         if (VALID_CONS_TARGET(ct)) {
1494                 float offset[3] = {0.0f, 0.0f, 0.0f};
1495                 
1496                 if (data->flag & LOCLIKE_OFFSET)
1497                         VECCOPY(offset, cob->matrix[3]);
1498                         
1499                 if (data->flag & LOCLIKE_X) {
1500                         cob->matrix[3][0] = ct->matrix[3][0];
1501                         
1502                         if (data->flag & LOCLIKE_X_INVERT) cob->matrix[3][0] *= -1;
1503                         cob->matrix[3][0] += offset[0];
1504                 }
1505                 if (data->flag & LOCLIKE_Y) {
1506                         cob->matrix[3][1] = ct->matrix[3][1];
1507                         
1508                         if (data->flag & LOCLIKE_Y_INVERT) cob->matrix[3][1] *= -1;
1509                         cob->matrix[3][1] += offset[1];
1510                 }
1511                 if (data->flag & LOCLIKE_Z) {
1512                         cob->matrix[3][2] = ct->matrix[3][2];
1513                         
1514                         if (data->flag & LOCLIKE_Z_INVERT) cob->matrix[3][2] *= -1;
1515                         cob->matrix[3][2] += offset[2];
1516                 }
1517         }
1518 }
1519
1520 static bConstraintTypeInfo CTI_LOCLIKE = {
1521         CONSTRAINT_TYPE_LOCLIKE, /* type */
1522         sizeof(bLocateLikeConstraint), /* size */
1523         "Copy Location", /* name */
1524         "bLocateLikeConstraint", /* struct name */
1525         NULL, /* free data */
1526         NULL, /* relink data */
1527         NULL, /* copy data */
1528         loclike_new_data, /* new data */
1529         loclike_get_tars, /* get constraint targets */
1530         loclike_flush_tars, /* flush constraint targets */
1531         default_get_tarmat, /* get target matrix */
1532         loclike_evaluate /* evaluate */
1533 };
1534
1535 /* ----------- Copy Rotation ------------- */
1536
1537 static void rotlike_new_data (void *cdata)
1538 {
1539         bRotateLikeConstraint *data= (bRotateLikeConstraint *)cdata;
1540         
1541         data->flag = ROTLIKE_X|ROTLIKE_Y|ROTLIKE_Z;
1542 }
1543
1544 static int rotlike_get_tars (bConstraint *con, ListBase *list)
1545 {
1546         if (con && list) {
1547                 bRotateLikeConstraint *data= con->data;
1548                 bConstraintTarget *ct;
1549                 
1550                 /* standard target-getting macro for single-target constraints */
1551                 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list)
1552                 
1553                 return 1;
1554         }
1555         
1556         return 0;
1557 }
1558
1559 static void rotlike_flush_tars (bConstraint *con, ListBase *list, short nocopy)
1560 {
1561         if (con && list) {
1562                 bRotateLikeConstraint *data= con->data;
1563                 bConstraintTarget *ct= list->first;
1564                 
1565                 /* the following macro is used for all standard single-target constraints */
1566                 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, nocopy)
1567         }
1568 }
1569
1570 static void rotlike_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
1571 {
1572         bRotateLikeConstraint *data= con->data;
1573         bConstraintTarget *ct= targets->first;
1574         
1575         if (VALID_CONS_TARGET(ct)) {
1576                 float   loc[3];
1577                 float   eul[3], obeul[3];
1578                 float   size[3];
1579                 
1580                 VECCOPY(loc, cob->matrix[3]);
1581                 Mat4ToSize(cob->matrix, size);
1582                 
1583                 Mat4ToEulO(ct->matrix, eul, ct->rotOrder);
1584                 Mat4ToEulO(cob->matrix, obeul, cob->rotOrder);
1585                 
1586                 if ((data->flag & ROTLIKE_X)==0)
1587                         eul[0] = obeul[0];
1588                 else {
1589                         if (data->flag & ROTLIKE_OFFSET)
1590                                 eulerO_rot(eul, obeul[0], 'x', cob->rotOrder);
1591                         
1592                         if (data->flag & ROTLIKE_X_INVERT)
1593                                 eul[0] *= -1;
1594                 }
1595                 
1596                 if ((data->flag & ROTLIKE_Y)==0)
1597                         eul[1] = obeul[1];
1598                 else {
1599                         if (data->flag & ROTLIKE_OFFSET)
1600                                 eulerO_rot(eul, obeul[1], 'y', cob->rotOrder);
1601                         
1602                         if (data->flag & ROTLIKE_Y_INVERT)
1603                                 eul[1] *= -1;
1604                 }
1605                 
1606                 if ((data->flag & ROTLIKE_Z)==0)
1607                         eul[2] = obeul[2];
1608                 else {
1609                         if (data->flag & ROTLIKE_OFFSET)
1610                                 eulerO_rot(eul, obeul[2], 'z', cob->rotOrder);
1611                         
1612                         if (data->flag & ROTLIKE_Z_INVERT)
1613                                 eul[2] *= -1;
1614                 }
1615                 
1616                 compatible_eul(eul, obeul);
1617                 LocEulOSizeToMat4(cob->matrix, loc, eul, size, cob->rotOrder);
1618         }
1619 }
1620
1621 static bConstraintTypeInfo CTI_ROTLIKE = {
1622         CONSTRAINT_TYPE_ROTLIKE, /* type */
1623         sizeof(bRotateLikeConstraint), /* size */
1624         "Copy Rotation", /* name */
1625         "bRotateLikeConstraint", /* struct name */
1626         NULL, /* free data */
1627         NULL, /* relink data */
1628         NULL, /* copy data */
1629         rotlike_new_data, /* new data */
1630         rotlike_get_tars, /* get constraint targets */
1631         rotlike_flush_tars, /* flush constraint targets */
1632         default_get_tarmat, /* get target matrix */
1633         rotlike_evaluate /* evaluate */
1634 };
1635
1636 /* ---------- Copy Scaling ---------- */
1637
1638 static void sizelike_new_data (void *cdata)
1639 {
1640         bSizeLikeConstraint *data= (bSizeLikeConstraint *)cdata;
1641         
1642         data->flag = SIZELIKE_X|SIZELIKE_Y|SIZELIKE_Z;
1643 }
1644
1645 static int sizelike_get_tars (bConstraint *con, ListBase *list)
1646 {
1647         if (con && list) {
1648                 bSizeLikeConstraint *data= con->data;
1649                 bConstraintTarget *ct;
1650                 
1651                 /* standard target-getting macro for single-target constraints */
1652                 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list)
1653                 
1654                 return 1;
1655         }
1656         
1657         return 0;
1658 }
1659
1660 static void sizelike_flush_tars (bConstraint *con, ListBase *list, short nocopy)
1661 {
1662         if (con && list) {
1663                 bSizeLikeConstraint *data= con->data;
1664                 bConstraintTarget *ct= list->first;
1665                 
1666                 /* the following macro is used for all standard single-target constraints */
1667                 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, nocopy)
1668         }
1669 }
1670
1671 static void sizelike_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
1672 {
1673         bSizeLikeConstraint *data= con->data;
1674         bConstraintTarget *ct= targets->first;
1675         
1676         if (VALID_CONS_TARGET(ct)) {
1677                 float obsize[3], size[3];
1678                 
1679                 Mat4ToSize(ct->matrix, size);
1680                 Mat4ToSize(cob->matrix, obsize);
1681                 
1682                 if ((data->flag & SIZELIKE_X) && (obsize[0] != 0)) {
1683                         if (data->flag & SIZELIKE_OFFSET) {
1684                                 size[0] += (obsize[0] - 1.0f);
1685                                 VecMulf(cob->matrix[0], size[0] / obsize[0]);
1686                         }
1687                         else
1688                                 VecMulf(cob->matrix[0], size[0] / obsize[0]);
1689                 }
1690                 if ((data->flag & SIZELIKE_Y) && (obsize[1] != 0)) {
1691                         if (data->flag & SIZELIKE_OFFSET) {
1692                                 size[1] += (obsize[1] - 1.0f);
1693                                 VecMulf(cob->matrix[1], size[1] / obsize[1]);
1694                         }
1695                         else
1696                                 VecMulf(cob->matrix[1], size[1] / obsize[1]);
1697                 }
1698                 if ((data->flag & SIZELIKE_Z) && (obsize[2] != 0)) {
1699                         if (data->flag & SIZELIKE_OFFSET) {
1700                                 size[2] += (obsize[2] - 1.0f);
1701                                 VecMulf(cob->matrix[2], size[2] / obsize[2]);
1702                         }
1703                         else
1704                                 VecMulf(cob->matrix[2], size[2] / obsize[2]);
1705                 }
1706         }
1707 }
1708
1709 static bConstraintTypeInfo CTI_SIZELIKE = {
1710         CONSTRAINT_TYPE_SIZELIKE, /* type */
1711         sizeof(bSizeLikeConstraint), /* size */
1712         "Copy Scale", /* name */
1713         "bSizeLikeConstraint", /* struct name */
1714         NULL, /* free data */
1715         NULL, /* relink data */
1716         NULL, /* copy data */
1717         sizelike_new_data, /* new data */
1718         sizelike_get_tars, /* get constraint targets */
1719         sizelike_flush_tars, /* flush constraint targets */
1720         default_get_tarmat, /* get target matrix */
1721         sizelike_evaluate /* evaluate */
1722 };
1723
1724
1725 /* ----------- Python Constraint -------------- */
1726
1727 static void pycon_free (bConstraint *con)
1728 {
1729         bPythonConstraint *data= con->data;
1730         
1731         /* id-properties */
1732         IDP_FreeProperty(data->prop);
1733         MEM_freeN(data->prop);
1734         
1735         /* multiple targets */
1736         BLI_freelistN(&data->targets);
1737 }       
1738
1739 static void pycon_relink (bConstraint *con)
1740 {
1741         bPythonConstraint *data= con->data;
1742         
1743         ID_NEW(data->text);
1744 }
1745
1746 static void pycon_copy (bConstraint *con, bConstraint *srccon)
1747 {
1748         bPythonConstraint *pycon = (bPythonConstraint *)con->data;
1749         bPythonConstraint *opycon = (bPythonConstraint *)srccon->data;
1750         
1751         pycon->prop = IDP_CopyProperty(opycon->prop);
1752         BLI_duplicatelist(&pycon->targets, &opycon->targets);
1753 }
1754
1755 static void pycon_new_data (void *cdata)
1756 {
1757         bPythonConstraint *data= (bPythonConstraint *)cdata;
1758         
1759         /* everything should be set correctly by calloc, except for the prop->type constant.*/
1760         data->prop = MEM_callocN(sizeof(IDProperty), "PyConstraintProps");
1761         data->prop->type = IDP_GROUP;
1762 }
1763
1764 static int pycon_get_tars (bConstraint *con, ListBase *list)
1765 {
1766         if (con && list) {
1767                 bPythonConstraint *data= con->data;
1768                 
1769                 list->first = data->targets.first;
1770                 list->last = data->targets.last;
1771                 
1772                 return data->tarnum;
1773         }
1774         
1775         return 0;
1776 }
1777
1778 /* Whether this approach is maintained remains to be seen (aligorith) */
1779 static void pycon_get_tarmat (bConstraint *con, bConstraintOb *cob, bConstraintTarget *ct, float ctime)
1780 {
1781         bPythonConstraint *data= con->data;
1782         
1783         if (VALID_CONS_TARGET(ct)) {
1784                 /* special exception for curves - depsgraph issues */
1785                 if (ct->tar->type == OB_CURVE) {
1786                         Curve *cu= ct->tar->data;
1787                         
1788                         /* this check is to make sure curve objects get updated on file load correctly.*/
1789                         if (cu->path==NULL || cu->path->data==NULL) /* only happens on reload file, but violates depsgraph still... fix! */
1790                                 makeDispListCurveTypes(cob->scene, ct->tar, 0);                         
1791                 }
1792                 
1793                 /* firstly calculate the matrix the normal way, then let the py-function override
1794                  * this matrix if it needs to do so
1795                  */
1796                 constraint_target_to_mat4(cob->scene, ct->tar, ct->subtarget, ct->matrix, CONSTRAINT_SPACE_WORLD, ct->space, con->headtail);
1797                 
1798                 /* only execute target calculation if allowed */
1799 #ifndef DISABLE_PYTHON
1800                 if (G.f & G_DOSCRIPTLINKS)
1801                         BPY_pyconstraint_target(data, ct);
1802 #endif
1803         }
1804         else if (ct)
1805                 Mat4One(ct->matrix);
1806 }
1807
1808 static void pycon_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
1809 {
1810 #ifdef DISABLE_PYTHON
1811         return;
1812 #else
1813         bPythonConstraint *data= con->data;
1814         
1815         /* only evaluate in python if we're allowed to do so */
1816         if ((G.f & G_DOSCRIPTLINKS)==0)  return;
1817         
1818 /* currently removed, until I this can be re-implemented for multiple targets */
1819 #if 0
1820         /* Firstly, run the 'driver' function which has direct access to the objects involved 
1821          * Technically, this is potentially dangerous as users may abuse this and cause dependency-problems,
1822          * but it also allows certain 'clever' rigging hacks to work.
1823          */
1824         BPY_pyconstraint_driver(data, cob, targets);
1825 #endif
1826         
1827         /* Now, run the actual 'constraint' function, which should only access the matrices */
1828         BPY_pyconstraint_eval(data, cob, targets);
1829 #endif /* DISABLE_PYTHON */
1830 }
1831
1832 static bConstraintTypeInfo CTI_PYTHON = {
1833         CONSTRAINT_TYPE_PYTHON, /* type */
1834         sizeof(bPythonConstraint), /* size */
1835         "Script", /* name */
1836         "bPythonConstraint", /* struct name */
1837         pycon_free, /* free data */
1838         pycon_relink, /* relink data */
1839         pycon_copy, /* copy data */
1840         pycon_new_data, /* new data */
1841         pycon_get_tars, /* get constraint targets */
1842         NULL, /* flush constraint targets */
1843         pycon_get_tarmat, /* get target matrix */
1844         pycon_evaluate /* evaluate */
1845 };
1846
1847 /* -------- Action Constraint ----------- */
1848
1849 static void actcon_relink (bConstraint *con)
1850 {
1851         bActionConstraint *data= con->data;
1852         ID_NEW(data->act);
1853 }
1854
1855 static void actcon_new_data (void *cdata)
1856 {
1857         bActionConstraint *data= (bActionConstraint *)cdata;
1858         
1859         /* set type to 20 (Loc X), as 0 is Rot X for backwards compatability */
1860         data->type = 20;
1861 }
1862
1863 static int actcon_get_tars (bConstraint *con, ListBase *list)
1864 {
1865         if (con && list) {
1866                 bActionConstraint *data= con->data;
1867                 bConstraintTarget *ct;
1868                 
1869                 /* standard target-getting macro for single-target constraints */
1870                 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list)
1871                 
1872                 return 1;
1873         }
1874         
1875         return 0;
1876 }
1877
1878 static void actcon_flush_tars (bConstraint *con, ListBase *list, short nocopy)
1879 {
1880         if (con && list) {
1881                 bActionConstraint *data= con->data;
1882                 bConstraintTarget *ct= list->first;
1883                 
1884                 /* the following macro is used for all standard single-target constraints */
1885                 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, nocopy)
1886         }
1887 }
1888
1889 static void actcon_get_tarmat (bConstraint *con, bConstraintOb *cob, bConstraintTarget *ct, float ctime)
1890 {
1891         extern void chan_calc_mat(bPoseChannel *chan);
1892         bActionConstraint *data = con->data;
1893         
1894         if (VALID_CONS_TARGET(ct)) {
1895                 float tempmat[4][4], vec[3];
1896                 float s, t;
1897                 short axis;
1898                 
1899                 /* initialise return matrix */
1900                 Mat4One(ct->matrix);
1901                 
1902                 /* get the transform matrix of the target */
1903                 constraint_target_to_mat4(cob->scene, ct->tar, ct->subtarget, tempmat, CONSTRAINT_SPACE_WORLD, ct->space, con->headtail);
1904                 
1905                 /* determine where in transform range target is */
1906                 /* data->type is mapped as follows for backwards compatability:
1907                  *      00,01,02        - rotation (it used to be like this)
1908                  *      10,11,12        - scaling
1909                  *      20,21,22        - location
1910                  */
1911                 if (data->type < 10) {
1912                         /* extract rotation (is in whatever space target should be in) */
1913                         Mat4ToEul(tempmat, vec);
1914                         vec[0] *= (float)(180.0/M_PI);
1915                         vec[1] *= (float)(180.0/M_PI);
1916                         vec[2] *= (float)(180.0/M_PI);
1917                         axis= data->type;
1918                 }
1919                 else if (data->type < 20) {
1920                         /* extract scaling (is in whatever space target should be in) */
1921                         Mat4ToSize(tempmat, vec);
1922                         axis= data->type - 10;
1923                 }
1924                 else {
1925                         /* extract location */
1926                         VECCOPY(vec, tempmat[3]);
1927                         axis= data->type - 20;
1928                 }
1929                 
1930                 /* Target defines the animation */
1931                 s = (vec[axis]-data->min) / (data->max-data->min);
1932                 CLAMP(s, 0, 1);
1933                 t = ( s * (data->end-data->start)) + data->start;
1934                 
1935                 if (G.f & G_DEBUG)
1936                         printf("do Action Constraint %s - Ob %s Pchan %s \n", con->name, cob->ob->id.name+2, (cob->pchan)?cob->pchan->name:NULL);
1937                 
1938                 /* Get the appropriate information from the action */
1939                 if (cob->type == CONSTRAINT_OBTYPE_BONE) {
1940                         Object workob;
1941                         bPose *pose;
1942                         bPoseChannel *pchan, *tchan;
1943                         
1944                         /* make a temporary pose and evaluate using that */
1945                         pose = MEM_callocN(sizeof(bPose), "pose");
1946                         
1947                         /* make a copy of the bone of interest in the temp pose before evaluating action, so that it can get set */
1948                         pchan = cob->pchan;
1949                         tchan= verify_pose_channel(pose, pchan->name);
1950                         
1951                         /* evaluate action using workob (it will only set the PoseChannel in question) */
1952                         what_does_obaction(cob->scene, cob->ob, &workob, pose, data->act, pchan->name, t);
1953                         
1954                         /* convert animation to matrices for use here */
1955                         chan_calc_mat(tchan);
1956                         Mat4CpyMat4(ct->matrix, tchan->chan_mat);
1957                         
1958                         /* Clean up */
1959                         free_pose(pose);
1960                 }
1961                 else if (cob->type == CONSTRAINT_OBTYPE_OBJECT) {
1962                         Object workob;
1963                         
1964                         /* evaluate using workob */
1965                         // FIXME: we don't have any consistent standards on limiting effects on object...
1966                         what_does_obaction(cob->scene, cob->ob, &workob, NULL, data->act, NULL, t);
1967                         object_to_mat4(&workob, ct->matrix);
1968                 }
1969                 else {
1970                         /* behaviour undefined... */
1971                         puts("Error: unknown owner type for Action Constraint");
1972                 }
1973         }
1974 }
1975
1976 static void actcon_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
1977 {
1978         bConstraintTarget *ct= targets->first;
1979         
1980         if (VALID_CONS_TARGET(ct)) {
1981                 float temp[4][4];
1982                 
1983                 /* Nice and simple... we just need to multiply the matrices, as the get_target_matrix
1984                  * function has already taken care of everything else.
1985                  */
1986                 Mat4CpyMat4(temp, cob->matrix);
1987                 Mat4MulMat4(cob->matrix, ct->matrix, temp);
1988         }
1989 }
1990
1991 static bConstraintTypeInfo CTI_ACTION = {
1992         CONSTRAINT_TYPE_ACTION, /* type */
1993         sizeof(bActionConstraint), /* size */
1994         "Action", /* name */
1995         "bActionConstraint", /* struct name */
1996         NULL, /* free data */
1997         actcon_relink, /* relink data */
1998         NULL, /* copy data */
1999         actcon_new_data, /* new data */
2000         actcon_get_tars, /* get constraint targets */
2001         actcon_flush_tars, /* flush constraint targets */
2002         actcon_get_tarmat, /* get target matrix */
2003         actcon_evaluate /* evaluate */
2004 };
2005
2006 /* --------- Locked Track ---------- */
2007
2008 static void locktrack_new_data (void *cdata)
2009 {
2010         bLockTrackConstraint *data= (bLockTrackConstraint *)cdata;
2011         
2012         data->trackflag = TRACK_Y;
2013         data->lockflag = LOCK_Z;
2014 }       
2015
2016 static int locktrack_get_tars (bConstraint *con, ListBase *list)
2017 {
2018         if (con && list) {
2019                 bLockTrackConstraint *data= con->data;
2020                 bConstraintTarget *ct;
2021                 
2022                 /* the following macro is used for all standard single-target constraints */
2023                 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list)
2024                 
2025                 return 1;
2026         }
2027         
2028         return 0;
2029 }
2030
2031 static void locktrack_flush_tars (bConstraint *con, ListBase *list, short nocopy)
2032 {
2033         if (con && list) {
2034                 bLockTrackConstraint *data= con->data;
2035                 bConstraintTarget *ct= list->first;
2036                 
2037                 /* the following macro is used for all standard single-target constraints */
2038                 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, nocopy)
2039         }
2040 }
2041
2042 static void locktrack_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
2043 {
2044         bLockTrackConstraint *data= con->data;
2045         bConstraintTarget *ct= targets->first;
2046         
2047         if (VALID_CONS_TARGET(ct)) {
2048                 float vec[3],vec2[3];
2049                 float totmat[3][3];
2050                 float tmpmat[3][3];
2051                 float invmat[3][3];
2052                 float tmat[4][4];
2053                 float mdet;
2054                 
2055                 /* Vector object -> target */
2056                 VecSubf(vec, ct->matrix[3], cob->matrix[3]);
2057                 switch (data->lockflag){
2058                 case LOCK_X: /* LOCK X */
2059                 {
2060                         switch (data->trackflag) {
2061                                 case TRACK_Y: /* LOCK X TRACK Y */
2062                                 {
2063                                         /* Projection of Vector on the plane */
2064                                         Projf(vec2, vec, cob->matrix[0]);
2065                                         VecSubf(totmat[1], vec, vec2);
2066                                         Normalize(totmat[1]);
2067                                         
2068                                         /* the x axis is fixed */
2069                                         totmat[0][0] = cob->matrix[0][0];
2070                                         totmat[0][1] = cob->matrix[0][1];
2071                                         totmat[0][2] = cob->matrix[0][2];
2072                                         Normalize(totmat[0]);
2073                                         
2074                                         /* the z axis gets mapped onto a third orthogonal vector */
2075                                         Crossf(totmat[2], totmat[0], totmat[1]);
2076                                 }
2077                                         break;
2078                                 case TRACK_Z: /* LOCK X TRACK Z */
2079                                 {
2080                                         /* Projection of Vector on the plane */
2081                                         Projf(vec2, vec, cob->matrix[0]);
2082                                         VecSubf(totmat[2], vec, vec2);
2083                                         Normalize(totmat[2]);
2084                                         
2085                                         /* the x axis is fixed */
2086                                         totmat[0][0] = cob->matrix[0][0];
2087                                         totmat[0][1] = cob->matrix[0][1];
2088                                         totmat[0][2] = cob->matrix[0][2];
2089                                         Normalize(totmat[0]);
2090                                         
2091                                         /* the z axis gets mapped onto a third orthogonal vector */
2092                                         Crossf(totmat[1], totmat[2], totmat[0]);
2093                                 }
2094                                         break;
2095                                 case TRACK_nY: /* LOCK X TRACK -Y */
2096                                 {
2097                                         /* Projection of Vector on the plane */
2098                                         Projf(vec2, vec, cob->matrix[0]);
2099                                         VecSubf(totmat[1], vec, vec2);
2100                                         Normalize(totmat[1]);
2101                                         VecNegf(totmat[1]);
2102                                         
2103                                         /* the x axis is fixed */
2104                                         totmat[0][0] = cob->matrix[0][0];
2105                                         totmat[0][1] = cob->matrix[0][1];
2106                                         totmat[0][2] = cob->matrix[0][2];
2107                                         Normalize(totmat[0]);
2108                                         
2109                                         /* the z axis gets mapped onto a third orthogonal vector */
2110                                         Crossf(totmat[2], totmat[0], totmat[1]);
2111                                 }
2112                                         break;
2113                                 case TRACK_nZ: /* LOCK X TRACK -Z */
2114                                 {
2115                                         /* Projection of Vector on the plane */
2116                                         Projf(vec2, vec, cob->matrix[0]);
2117                                         VecSubf(totmat[2], vec, vec2);
2118                                         Normalize(totmat[2]);
2119                                         VecNegf(totmat[2]);
2120                                                 
2121                                         /* the x axis is fixed */
2122                                         totmat[0][0] = cob->matrix[0][0];
2123                                         totmat[0][1] = cob->matrix[0][1];
2124                                         totmat[0][2] = cob->matrix[0][2];
2125                                         Normalize(totmat[0]);
2126                                                 
2127                                         /* the z axis gets mapped onto a third orthogonal vector */
2128                                         Crossf(totmat[1], totmat[2], totmat[0]);
2129                                 }
2130                                         break;
2131                                 default:
2132                                 {
2133                                         totmat[0][0] = 1;totmat[0][1] = 0;totmat[0][2] = 0;
2134                                         totmat[1][0] = 0;totmat[1][1] = 1;totmat[1][2] = 0;
2135                                         totmat[2][0] = 0;totmat[2][1] = 0;totmat[2][2] = 1;
2136                                 }
2137                                         break;
2138                         }
2139                 }
2140                         break;
2141                 case LOCK_Y: /* LOCK Y */
2142                 {
2143                         switch (data->trackflag) {
2144                                 case TRACK_X: /* LOCK Y TRACK X */
2145                                 {
2146                                         /* Projection of Vector on the plane */
2147                                         Projf(vec2, vec, cob->matrix[1]);
2148                                         VecSubf(totmat[0], vec, vec2);
2149                                         Normalize(totmat[0]);
2150                                         
2151                                         /* the y axis is fixed */
2152                                         totmat[1][0] = cob->matrix[1][0];
2153                                         totmat[1][1] = cob->matrix[1][1];
2154                                         totmat[1][2] = cob->matrix[1][2];
2155                                         Normalize(totmat[1]);
2156                                         
2157                                         /* the z axis gets mapped onto a third orthogonal vector */
2158                                         Crossf(totmat[2], totmat[0], totmat[1]);
2159                                 }
2160                                         break;
2161                                 case TRACK_Z: /* LOCK Y TRACK Z */
2162                                 {
2163                                         /* Projection of Vector on the plane */
2164                                         Projf(vec2, vec, cob->matrix[1]);
2165                                         VecSubf(totmat[2], vec, vec2);
2166                                         Normalize(totmat[2]);
2167
2168                                         /* the y axis is fixed */
2169                                         totmat[1][0] = cob->matrix[1][0];
2170                                         totmat[1][1] = cob->matrix[1][1];
2171                                         totmat[1][2] = cob->matrix[1][2];
2172                                         Normalize(totmat[1]);
2173                                         
2174                                         /* the z axis gets mapped onto a third orthogonal vector */
2175                                         Crossf(totmat[0], totmat[1], totmat[2]);
2176                                 }
2177                                         break;
2178                                 case TRACK_nX: /* LOCK Y TRACK -X */
2179                                 {
2180                                         /* Projection of Vector on the plane */
2181                                         Projf(vec2, vec, cob->matrix[1]);
2182                                         VecSubf(totmat[0], vec, vec2);
2183                                         Normalize(totmat[0]);
2184                                         VecNegf(totmat[0]);
2185                                         
2186                                         /* the y axis is fixed */
2187                                         totmat[1][0] = cob->matrix[1][0];
2188                                         totmat[1][1] = cob->matrix[1][1];
2189                                         totmat[1][2] = cob->matrix[1][2];
2190                                         Normalize(totmat[1]);
2191                                         
2192                                         /* the z axis gets mapped onto a third orthogonal vector */
2193                                         Crossf(totmat[2], totmat[0], totmat[1]);
2194                                 }
2195                                         break;
2196                                 case TRACK_nZ: /* LOCK Y TRACK -Z */
2197                                 {
2198                                         /* Projection of Vector on the plane */
2199                                         Projf(vec2, vec, cob->matrix[1]);
2200                                         VecSubf(totmat[2], vec, vec2);
2201                                         Normalize(totmat[2]);
2202                                         VecNegf(totmat[2]);
2203                                         
2204                                         /* the y axis is fixed */
2205                                         totmat[1][0] = cob->matrix[1][0];
2206                                         totmat[1][1] = cob->matrix[1][1];
2207                                         totmat[1][2] = cob->matrix[1][2];
2208                                         Normalize(totmat[1]);
2209                                         
2210                                         /* the z axis gets mapped onto a third orthogonal vector */
2211                                         Crossf(totmat[0], totmat[1], totmat[2]);
2212                                 }
2213                                         break;
2214                                 default:
2215                                 {
2216                                         totmat[0][0] = 1;totmat[0][1] = 0;totmat[0][2] = 0;
2217                                         totmat[1][0] = 0;totmat[1][1] = 1;totmat[1][2] = 0;
2218                                         totmat[2][0] = 0;totmat[2][1] = 0;totmat[2][2] = 1;
2219                                 }
2220                                         break;
2221                         }
2222                 }
2223                         break;
2224                 case LOCK_Z: /* LOCK Z */
2225                 {
2226                         switch (data->trackflag) {
2227                                 case TRACK_X: /* LOCK Z TRACK X */
2228                                 {
2229                                         /* Projection of Vector on the plane */
2230                                         Projf(vec2, vec, cob->matrix[2]);
2231                                         VecSubf(totmat[0], vec, vec2);
2232                                         Normalize(totmat[0]);
2233                                         
2234                                         /* the z axis is fixed */
2235                                         totmat[2][0] = cob->matrix[2][0];
2236                                         totmat[2][1] = cob->matrix[2][1];
2237                                         totmat[2][2] = cob->matrix[2][2];
2238                                         Normalize(totmat[2]);
2239                                         
2240                                         /* the x axis gets mapped onto a third orthogonal vector */
2241                                         Crossf(totmat[1], totmat[2], totmat[0]);
2242                                 }
2243                                         break;
2244                                 case TRACK_Y: /* LOCK Z TRACK Y */
2245                                 {
2246                                         /* Projection of Vector on the plane */
2247                                         Projf(vec2, vec, cob->matrix[2]);
2248                                         VecSubf(totmat[1], vec, vec2);
2249                                         Normalize(totmat[1]);
2250                                         
2251                                         /* the z axis is fixed */
2252                                         totmat[2][0] = cob->matrix[2][0];
2253                                         totmat[2][1] = cob->matrix[2][1];
2254                                         totmat[2][2] = cob->matrix[2][2];
2255                                         Normalize(totmat[2]);
2256                                                 
2257                                         /* the x axis gets mapped onto a third orthogonal vector */
2258                                         Crossf(totmat[0], totmat[1], totmat[2]);
2259                                 }
2260                                         break;
2261                                 case TRACK_nX: /* LOCK Z TRACK -X */
2262                                 {
2263                                         /* Projection of Vector on the plane */
2264                                         Projf(vec2, vec, cob->matrix[2]);
2265                                         VecSubf(totmat[0], vec, vec2);
2266                                         Normalize(totmat[0]);
2267                                         VecNegf(totmat[0]);
2268                                         
2269                                         /* the z axis is fixed */
2270                                         totmat[2][0] = cob->matrix[2][0];
2271                                         totmat[2][1] = cob->matrix[2][1];
2272                                         totmat[2][2] = cob->matrix[2][2];
2273                                         Normalize(totmat[2]);
2274                                         
2275                                         /* the x axis gets mapped onto a third orthogonal vector */
2276                                         Crossf(totmat[1], totmat[2], totmat[0]);
2277                                 }
2278                                         break;
2279                                 case TRACK_nY: /* LOCK Z TRACK -Y */
2280                                 {
2281                                         /* Projection of Vector on the plane */
2282                                         Projf(vec2, vec, cob->matrix[2]);
2283                                         VecSubf(totmat[1], vec, vec2);
2284                                         Normalize(totmat[1]);
2285                                         VecNegf(totmat[1]);
2286                                         
2287                                         /* the z axis is fixed */
2288                                         totmat[2][0] = cob->matrix[2][0];
2289                                         totmat[2][1] = cob->matrix[2][1];
2290                                         totmat[2][2] = cob->matrix[2][2];
2291                                         Normalize(totmat[2]);
2292                                                 
2293                                         /* the x axis gets mapped onto a third orthogonal vector */
2294                                         Crossf(totmat[0], totmat[1], totmat[2]);
2295                                 }
2296                                         break;
2297                                 default:
2298                                 {
2299                                                 totmat[0][0] = 1;totmat[0][1] = 0;totmat[0][2] = 0;
2300                                                 totmat[1][0] = 0;totmat[1][1] = 1;totmat[1][2] = 0;
2301                                                 totmat[2][0] = 0;totmat[2][1] = 0;totmat[2][2] = 1;
2302                                 }
2303                                         break;
2304                         }
2305                 }
2306                         break;
2307                 default:
2308                         {
2309                                 totmat[0][0] = 1;totmat[0][1] = 0;totmat[0][2] = 0;
2310                                 totmat[1][0] = 0;totmat[1][1] = 1;totmat[1][2] = 0;
2311                                 totmat[2][0] = 0;totmat[2][1] = 0;totmat[2][2] = 1;
2312                         }
2313                         break;
2314                 }
2315                 /* Block to keep matrix heading */
2316                 tmpmat[0][0] = cob->matrix[0][0];tmpmat[0][1] = cob->matrix[0][1];tmpmat[0][2] = cob->matrix[0][2];
2317                 tmpmat[1][0] = cob->matrix[1][0];tmpmat[1][1] = cob->matrix[1][1];tmpmat[1][2] = cob->matrix[1][2];
2318                 tmpmat[2][0] = cob->matrix[2][0];tmpmat[2][1] = cob->matrix[2][1];tmpmat[2][2] = cob->matrix[2][2];
2319                 Normalize(tmpmat[0]);
2320                 Normalize(tmpmat[1]);
2321                 Normalize(tmpmat[2]);
2322                 Mat3Inv(invmat, tmpmat);
2323                 Mat3MulMat3(tmpmat, totmat, invmat);
2324                 totmat[0][0] = tmpmat[0][0];totmat[0][1] = tmpmat[0][1];totmat[0][2] = tmpmat[0][2];
2325                 totmat[1][0] = tmpmat[1][0];totmat[1][1] = tmpmat[1][1];totmat[1][2] = tmpmat[1][2];
2326                 totmat[2][0] = tmpmat[2][0];totmat[2][1] = tmpmat[2][1];totmat[2][2] = tmpmat[2][2];
2327                 
2328                 Mat4CpyMat4(tmat, cob->matrix);
2329                 
2330                 mdet = Det3x3(  totmat[0][0],totmat[0][1],totmat[0][2],
2331                                                 totmat[1][0],totmat[1][1],totmat[1][2],
2332                                                 totmat[2][0],totmat[2][1],totmat[2][2]);
2333                 if (mdet==0) {
2334                         totmat[0][0] = 1;totmat[0][1] = 0;totmat[0][2] = 0;
2335                         totmat[1][0] = 0;totmat[1][1] = 1;totmat[1][2] = 0;
2336                         totmat[2][0] = 0;totmat[2][1] = 0;totmat[2][2] = 1;
2337                 }
2338                 
2339                 /* apply out transformaton to the object */
2340                 Mat4MulMat34(cob->matrix, totmat, tmat);
2341         }
2342 }
2343
2344 static bConstraintTypeInfo CTI_LOCKTRACK = {
2345         CONSTRAINT_TYPE_LOCKTRACK, /* type */
2346         sizeof(bLockTrackConstraint), /* size */
2347         "Locked Track", /* name */
2348         "bLockTrackConstraint", /* struct name */
2349         NULL, /* free data */
2350         NULL, /* relink data */
2351         NULL, /* copy data */
2352         locktrack_new_data, /* new data */
2353         locktrack_get_tars, /* get constraint targets */
2354         locktrack_flush_tars, /* flush constraint targets */
2355         default_get_tarmat, /* get target matrix */
2356         locktrack_evaluate /* evaluate */
2357 };
2358
2359 /* ---------- Limit Distance Constraint ----------- */
2360
2361 static void distlimit_new_data (void *cdata)
2362 {
2363         bDistLimitConstraint *data= (bDistLimitConstraint *)cdata;
2364         
2365         data->dist= 0.0;
2366 }
2367
2368 static int distlimit_get_tars (bConstraint *con, ListBase *list)
2369 {
2370         if (con && list) {
2371                 bDistLimitConstraint *data= con->data;
2372                 bConstraintTarget *ct;
2373                 
2374                 /* standard target-getting macro for single-target constraints */
2375                 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list)
2376                 
2377                 return 1;
2378         }
2379         
2380         return 0;
2381 }
2382
2383 static void distlimit_flush_tars (bConstraint *con, ListBase *list, short nocopy)
2384 {
2385         if (con && list) {
2386                 bDistLimitConstraint *data= con->data;
2387                 bConstraintTarget *ct= list->first;
2388                 
2389                 /* the following macro is used for all standard single-target constraints */
2390                 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, nocopy)
2391         }
2392 }
2393
2394 static void distlimit_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
2395 {
2396         bDistLimitConstraint *data= con->data;
2397         bConstraintTarget *ct= targets->first;
2398         
2399         /* only evaluate if there is a target */
2400         if (VALID_CONS_TARGET(ct)) {
2401                 float dvec[3], dist=0.0f, sfac=1.0f;
2402                 short clamp_surf= 0;
2403                 
2404                 /* calculate our current distance from the target */
2405                 dist= VecLenf(cob->matrix[3], ct->matrix[3]);
2406                 
2407                 /* set distance (flag is only set when user demands it) */
2408                 if (data->dist == 0)
2409                         data->dist= dist;
2410                 
2411                 /* check if we're which way to clamp from, and calculate interpolation factor (if needed) */
2412                 if (data->mode == LIMITDIST_OUTSIDE) {
2413                         /* if inside, then move to surface */
2414                         if (dist <= data->dist) {
2415                                 clamp_surf= 1;
2416                                 sfac= data->dist / dist;
2417                         }
2418                         /* if soft-distance is enabled, start fading once owner is dist+softdist from the target */
2419                         else if (data->flag & LIMITDIST_USESOFT) {
2420                                 if (dist <= (data->dist + data->soft)) {
2421                                         
2422                                 }
2423                         }
2424                 }
2425                 else if (data->mode == LIMITDIST_INSIDE) {
2426                         /* if outside, then move to surface */
2427                         if (dist >= data->dist) {
2428                                 clamp_surf= 1;
2429                                 sfac= data->dist / dist;
2430                         }
2431                         /* if soft-distance is enabled, start fading once owner is dist-soft from the target */
2432                         else if (data->flag & LIMITDIST_USESOFT) {
2433                                 // FIXME: there's a problem with "jumping" when this kicks in
2434                                 if (dist >= (data->dist - data->soft)) {
2435                                         sfac = (float)( data->soft*(1.0 - exp(-(dist - data->dist)/data->soft)) + data->dist );
2436                                         sfac /= dist;
2437                                         
2438                                         clamp_surf= 1;
2439                                 }
2440                         }
2441                 }
2442                 else {
2443                         if (IS_EQ(dist, data->dist)==0) {
2444                                 clamp_surf= 1;
2445                                 sfac= data->dist / dist;
2446                         }
2447                 }
2448                 
2449                 /* clamp to 'surface' (i.e. move owner so that dist == data->dist) */
2450                 if (clamp_surf) {
2451                         /* simply interpolate along line formed by target -> owner */
2452                         VecLerpf(dvec, ct->matrix[3], cob->matrix[3], sfac);
2453                         
2454                         /* copy new vector onto owner */
2455                         VECCOPY(cob->matrix[3], dvec);
2456                 }
2457         }
2458 }
2459
2460 static bConstraintTypeInfo CTI_DISTLIMIT = {
2461         CONSTRAINT_TYPE_DISTLIMIT, /* type */
2462         sizeof(bDistLimitConstraint), /* size */
2463         "Limit Distance", /* name */
2464         "bDistLimitConstraint", /* struct name */
2465         NULL, /* free data */
2466         NULL, /* relink data */
2467         NULL, /* copy data */
2468         distlimit_new_data, /* new data */
2469         distlimit_get_tars, /* get constraint targets */
2470         distlimit_flush_tars, /* flush constraint targets */
2471         default_get_tarmat, /* get a target matrix */
2472         distlimit_evaluate /* evaluate */
2473 };
2474
2475 /* ---------- Stretch To ------------ */
2476
2477 static void stretchto_new_data (void *cdata)
2478 {
2479         bStretchToConstraint *data= (bStretchToConstraint *)cdata;
2480         
2481         data->volmode = 0;
2482         data->plane = 0;
2483         data->orglength = 0.0; 
2484         data->bulge = 1.0;
2485 }
2486
2487 static int stretchto_get_tars (bConstraint *con, ListBase *list)
2488 {
2489         if (con && list) {
2490                 bStretchToConstraint *data= con->data;
2491                 bConstraintTarget *ct;
2492                 
2493                 /* standard target-getting macro for single-target constraints */
2494                 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list)
2495                 
2496                 return 1;
2497         }
2498         
2499         return 0;
2500 }
2501
2502 static void stretchto_flush_tars (bConstraint *con, ListBase *list, short nocopy)
2503 {
2504         if (con && list) {
2505                 bStretchToConstraint *data= con->data;
2506                 bConstraintTarget *ct= list->first;
2507                 
2508                 /* the following macro is used for all standard single-target constraints */
2509                 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, nocopy)
2510         }
2511 }
2512
2513 static void stretchto_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
2514 {
2515         bStretchToConstraint *data= con->data;
2516         bConstraintTarget *ct= targets->first;
2517         
2518         /* only evaluate if there is a target */
2519         if (VALID_CONS_TARGET(ct)) {
2520                 float size[3], scale[3], vec[3], xx[3], zz[3], orth[3];
2521                 float totmat[3][3];
2522                 float tmat[4][4];
2523                 float dist;
2524                 
2525                 /* store scaling before destroying obmat */
2526                 Mat4ToSize(cob->matrix, size);
2527                 
2528                 /* store X orientation before destroying obmat */
2529                 xx[0] = cob->matrix[0][0];
2530                 xx[1] = cob->matrix[0][1];
2531                 xx[2] = cob->matrix[0][2];
2532                 Normalize(xx);
2533                 
2534                 /* store Z orientation before destroying obmat */
2535                 zz[0] = cob->matrix[2][0];
2536                 zz[1] = cob->matrix[2][1];
2537                 zz[2] = cob->matrix[2][2];
2538                 Normalize(zz);
2539                 
2540                 VecSubf(vec, cob->matrix[3], ct->matrix[3]);
2541                 vec[0] /= size[0];
2542                 vec[1] /= size[1];
2543                 vec[2] /= size[2];
2544                 
2545                 dist = Normalize(vec);
2546                 //dist = VecLenf( ob->obmat[3], targetmat[3]);
2547                 
2548                 /* data->orglength==0 occurs on first run, and after 'R' button is clicked */
2549                 if (data->orglength == 0)  
2550                         data->orglength = dist;
2551                 if (data->bulge == 0) 
2552                         data->bulge = 1.0;
2553                 
2554                 scale[1] = dist/data->orglength;
2555                 switch (data->volmode) {
2556                 /* volume preserving scaling */
2557                 case VOLUME_XZ :
2558                         scale[0] = 1.0f - (float)sqrt(data->bulge) + (float)sqrt(data->bulge*(data->orglength/dist));
2559                         scale[2] = scale[0];
2560                         break;
2561                 case VOLUME_X:
2562                         scale[0] = 1.0f + data->bulge * (data->orglength /dist - 1);
2563                         scale[2] = 1.0;
2564                         break;
2565                 case VOLUME_Z:
2566                         scale[0] = 1.0;
2567                         scale[2] = 1.0f + data->bulge * (data->orglength /dist - 1);
2568                         break;
2569                         /* don't care for volume */
2570                 case NO_VOLUME:
2571                         scale[0] = 1.0;
2572                         scale[2] = 1.0;
2573                         break;
2574                 default: /* should not happen, but in case*/
2575                         return;    
2576                 } /* switch (data->volmode) */
2577
2578                 /* Clear the object's rotation and scale */
2579                 cob->matrix[0][0]=size[0]*scale[0];
2580                 cob->matrix[0][1]=0;
2581                 cob->matrix[0][2]=0;
2582                 cob->matrix[1][0]=0;
2583                 cob->matrix[1][1]=size[1]*scale[1];
2584                 cob->matrix[1][2]=0;
2585                 cob->matrix[2][0]=0;
2586                 cob->matrix[2][1]=0;
2587                 cob->matrix[2][2]=size[2]*scale[2];
2588                 
2589                 VecSubf(vec, cob->matrix[3], ct->matrix[3]);
2590                 Normalize(vec);
2591                 
2592                 /* new Y aligns  object target connection*/
2593                 totmat[1][0] = -vec[0];
2594                 totmat[1][1] = -vec[1];
2595                 totmat[1][2] = -vec[2];
2596                 switch (data->plane) {
2597                 case PLANE_X:
2598                         /* build new Z vector */
2599                         /* othogonal to "new Y" "old X! plane */
2600                         Crossf(orth, vec, xx);
2601                         Normalize(orth);
2602                         
2603                         /* new Z*/
2604                         totmat[2][0] = orth[0];
2605                         totmat[2][1] = orth[1];
2606                         totmat[2][2] = orth[2];
2607                         
2608                         /* we decided to keep X plane*/
2609                         Crossf(xx, orth, vec);
2610                         Normalize(xx);
2611                         totmat[0][0] = xx[0];
2612                         totmat[0][1] = xx[1];
2613                         totmat[0][2] = xx[2];
2614                         break;
2615                 case PLANE_Z:
2616                         /* build new X vector */
2617                         /* othogonal to "new Y" "old Z! plane */
2618                         Crossf(orth, vec, zz);
2619                         Normalize(orth);
2620                         
2621                         /* new X */
2622                         totmat[0][0] = -orth[0];
2623                         totmat[0][1] = -orth[1];
2624                         totmat[0][2] = -orth[2];
2625                         
2626                         /* we decided to keep Z */
2627                         Crossf(zz, orth, vec);
2628                         Normalize(zz);
2629                         totmat[2][0] = zz[0];
2630                         totmat[2][1] = zz[1];
2631                         totmat[2][2] = zz[2];
2632                         break;
2633                 } /* switch (data->plane) */
2634                 
2635                 Mat4CpyMat4(tmat, cob->matrix);
2636                 Mat4MulMat34(cob->matrix, totmat, tmat);
2637         }
2638 }
2639
2640 static bConstraintTypeInfo CTI_STRETCHTO = {
2641         CONSTRAINT_TYPE_STRETCHTO, /* type */
2642         sizeof(bStretchToConstraint), /* size */
2643         "Stretch To", /* name */
2644         "bStretchToConstraint", /* struct name */
2645         NULL, /* free data */
2646         NULL, /* relink data */
2647         NULL, /* copy data */
2648         stretchto_new_data, /* new data */
2649         stretchto_get_tars, /* get constraint targets */
2650         stretchto_flush_tars, /* flush constraint targets */
2651         default_get_tarmat, /* get target matrix */
2652         stretchto_evaluate /* evaluate */
2653 };
2654
2655 /* ---------- Floor ------------ */
2656
2657 static void minmax_new_data (void *cdata)
2658 {
2659         bMinMaxConstraint *data= (bMinMaxConstraint *)cdata;
2660         
2661         data->minmaxflag = TRACK_Z;
2662         data->offset = 0.0f;
2663         data->cache[0] = data->cache[1] = data->cache[2] = 0.0f;
2664         data->flag = 0;
2665 }
2666
2667 static int minmax_get_tars (bConstraint *con, ListBase *list)
2668 {
2669         if (con && list) {
2670                 bMinMaxConstraint *data= con->data;
2671                 bConstraintTarget *ct;
2672                 
2673                 /* standard target-getting macro for single-target constraints */
2674                 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list)
2675                 
2676                 return 1;
2677         }
2678         
2679         return 0;
2680 }
2681
2682 static void minmax_flush_tars (bConstraint *con, ListBase *list, short nocopy)
2683 {
2684         if (con && list) {
2685                 bMinMaxConstraint *data= con->data;
2686                 bConstraintTarget *ct= list->first;
2687                 
2688                 /* the following macro is used for all standard single-target constraints */
2689                 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, nocopy)
2690         }
2691 }
2692
2693 static void minmax_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
2694 {
2695         bMinMaxConstraint *data= con->data;
2696         bConstraintTarget *ct= targets->first;
2697         
2698         /* only evaluate if there is a target */
2699         if (VALID_CONS_TARGET(ct)) {
2700                 float obmat[4][4], imat[4][4], tarmat[4][4], tmat[4][4];
2701                 float val1, val2;
2702                 int index;
2703                 
2704                 Mat4CpyMat4(obmat, cob->matrix);
2705                 Mat4CpyMat4(tarmat, ct->matrix);
2706                 
2707                 if (data->flag & MINMAX_USEROT) {
2708                         /* take rotation of target into account by doing the transaction in target's localspace */
2709                         Mat4Invert(imat, tarmat);
2710                         Mat4MulMat4(tmat, obmat, imat);
2711                         Mat4CpyMat4(obmat, tmat);
2712                         Mat4One(tarmat);
2713                 }
2714                 
2715                 switch (data->minmaxflag) {
2716                 case TRACK_Z:
2717                         val1 = tarmat[3][2];
2718                         val2 = obmat[3][2]-data->offset;
2719                         index = 2;
2720                         break;
2721                 case TRACK_Y:
2722                         val1 = tarmat[3][1];
2723                         val2 = obmat[3][1]-data->offset;
2724                         index = 1;
2725                         break;
2726                 case TRACK_X:
2727                         val1 = tarmat[3][0];
2728                         val2 = obmat[3][0]-data->offset;
2729                         index = 0;
2730                         break;
2731                 case TRACK_nZ:
2732                         val2 = tarmat[3][2];
2733                         val1 = obmat[3][2]-data->offset;
2734                         index = 2;
2735                         break;
2736                 case TRACK_nY:
2737                         val2 = tarmat[3][1];
2738                         val1 = obmat[3][1]-data->offset;
2739                         index = 1;
2740                         break;
2741                 case TRACK_nX:
2742                         val2 = tarmat[3][0];
2743                         val1 = obmat[3][0]-data->offset;
2744                         index = 0;
2745                         break;
2746                 default:
2747                         return;
2748                 }
2749                 
2750                 if (val1 > val2) {
2751                         obmat[3][index] = tarmat[3][index] + data->offset;
2752                         if (data->flag & MINMAX_STICKY) {
2753                                 if (data->flag & MINMAX_STUCK) {
2754                                         VECCOPY(obmat[3], data->cache);
2755                                 } 
2756                                 else {
2757                                         VECCOPY(data->cache, obmat[3]);
2758                                         data->flag |= MINMAX_STUCK;
2759                                 }
2760                         }
2761                         if (data->flag & MINMAX_USEROT) {
2762                                 /* get out of localspace */
2763                                 Mat4MulMat4(tmat, obmat, ct->matrix);
2764                                 Mat4CpyMat4(cob->matrix, tmat);
2765                         } 
2766                         else {                  
2767                                 VECCOPY(cob->matrix[3], obmat[3]);
2768                         }
2769                 } 
2770                 else {
2771                         data->flag &= ~MINMAX_STUCK;
2772                 }
2773         }
2774 }
2775
2776 static bConstraintTypeInfo CTI_MINMAX = {
2777         CONSTRAINT_TYPE_MINMAX, /* type */
2778         sizeof(bMinMaxConstraint), /* size */
2779         "Floor", /* name */
2780         "bMinMaxConstraint", /* struct name */
2781         NULL, /* free data */
2782         NULL, /* relink data */
2783         NULL, /* copy data */
2784         minmax_new_data, /* new data */
2785         minmax_get_tars, /* get constraint targets */
2786         minmax_flush_tars, /* flush constraint targets */
2787         default_get_tarmat, /* get target matrix */
2788         minmax_evaluate /* evaluate */
2789 };
2790
2791 /* ------- RigidBody Joint ---------- */
2792
2793 static void rbj_new_data (void *cdata)
2794 {
2795         bRigidBodyJointConstraint *data= (bRigidBodyJointConstraint *)cdata;
2796         
2797         // removed code which set target of this constraint  
2798     data->type=1;
2799 }
2800
2801 static int rbj_get_tars (bConstraint *con, ListBase *list)
2802 {
2803         if (con && list) {
2804                 bRigidBodyJointConstraint *data= con->data;
2805                 bConstraintTarget *ct;
2806                 
2807                 /* standard target-getting macro for single-target constraints without subtargets */
2808                 SINGLETARGETNS_GET_TARS(con, data->tar, ct, list)
2809                 
2810                 return 1;
2811         }
2812         
2813         return 0;
2814 }
2815
2816 static void rbj_flush_tars (bConstraint *con, ListBase *list, short nocopy)
2817 {
2818         if (con && list) {
2819                 bRigidBodyJointConstraint *data= con->data;
2820                 bConstraintTarget *ct= list->first;
2821                 
2822                 /* the following macro is used for all standard single-target constraints */
2823                 SINGLETARGETNS_FLUSH_TARS(con, data->tar, ct, list, nocopy)
2824         }
2825 }
2826
2827 static bConstraintTypeInfo CTI_RIGIDBODYJOINT = {
2828         CONSTRAINT_TYPE_RIGIDBODYJOINT, /* type */
2829         sizeof(bRigidBodyJointConstraint), /* size */
2830         "RigidBody Joint", /* name */
2831         "bRigidBodyJointConstraint", /* struct name */
2832         NULL, /* free data */
2833         NULL, /* relink data */
2834         NULL, /* copy data */
2835         rbj_new_data, /* new data */
2836         rbj_get_tars, /* get constraint targets */
2837         rbj_flush_tars, /* flush constraint targets */
2838         default_get_tarmat, /* get target matrix */
2839         NULL /* evaluate - this is not solved here... is just an interface for game-engine */
2840 };
2841
2842 /* -------- Clamp To ---------- */
2843
2844 static int clampto_get_tars (bConstraint *con, ListBase *list)
2845 {
2846         if (con && list) {
2847                 bClampToConstraint *data= con->data;
2848                 bConstraintTarget *ct;
2849                 
2850                 /* standard target-getting macro for single-target constraints without subtargets */
2851                 SINGLETARGETNS_GET_TARS(con, data->tar, ct, list)
2852                 
2853                 return 1;
2854         }
2855         
2856         return 0;
2857 }
2858
2859 static void clampto_flush_tars (bConstraint *con, ListBase *list, short nocopy)
2860 {
2861         if (con && list) {
2862                 bClampToConstraint *data= con->data;
2863                 bConstraintTarget *ct= list->first;
2864                 
2865                 /* the following macro is used for all standard single-target constraints */
2866                 SINGLETARGETNS_FLUSH_TARS(con, data->tar, ct, list, nocopy)
2867         }
2868 }
2869
2870 static void clampto_get_tarmat (bConstraint *con, bConstraintOb *cob, bConstraintTarget *ct, float ctime)
2871 {
2872         if (VALID_CONS_TARGET(ct)) {
2873                 Curve *cu= ct->tar->data;
2874                 
2875                 /* note: when creating constraints that follow path, the curve gets the CU_PATH set now,
2876                  *              currently for paths to work it needs to go through the bevlist/displist system (ton) 
2877                  */
2878                 
2879                 /* only happens on reload file, but violates depsgraph still... fix! */
2880                 if (cu->path==NULL || cu->path->data==NULL)
2881                         makeDispListCurveTypes(cob->scene, ct->tar, 0);
2882         }
2883         
2884         /* technically, this isn't really needed for evaluation, but we don't know what else
2885          * might end up calling this...
2886          */
2887         if (ct)
2888                 Mat4One(ct->matrix);
2889 }
2890
2891 static void clampto_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
2892 {
2893         bClampToConstraint *data= con->data;
2894         bConstraintTarget *ct= targets->first;
2895         
2896         /* only evaluate if there is a target and it is a curve */
2897         if (VALID_CONS_TARGET(ct) && (ct->tar->type == OB_CURVE)) {
2898                 Curve *cu= data->tar->data;
2899                 float obmat[4][4], targetMatrix[4][4], ownLoc[3];
2900                 float curveMin[3], curveMax[3];
2901                 
2902                 Mat4CpyMat4(obmat, cob->matrix);
2903                 Mat4One(targetMatrix);
2904                 VECCOPY(ownLoc, obmat[3]);
2905                 
2906                 INIT_MINMAX(curveMin, curveMax)
2907                 minmax_object(ct->tar, curveMin, curveMax);
2908            &nb