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