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