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