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