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