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