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