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