4 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * The Original Code is: all of this file.
25 * Contributor(s): 2007, Joshua Leung, major recode
27 * ***** END GPL LICENSE BLOCK *****
35 #include "MEM_guardedalloc.h"
38 #include "BLI_blenlib.h"
39 #include "BLI_arithb.h"
41 #include "DNA_armature_types.h"
42 #include "DNA_constraint_types.h"
43 #include "DNA_object_types.h"
44 #include "DNA_action_types.h"
45 #include "DNA_curve_types.h"
46 #include "DNA_meshdata_types.h"
47 #include "DNA_lattice_types.h"
48 #include "DNA_scene_types.h"
49 #include "DNA_text_types.h"
51 #include "BKE_utildefines.h"
52 #include "BKE_action.h"
53 #include "BKE_anim.h" /* for the curve calculation part */
54 #include "BKE_armature.h"
55 #include "BKE_blender.h"
56 #include "BKE_constraint.h"
57 #include "BKE_displist.h"
58 #include "BKE_deform.h"
59 #include "BKE_DerivedMesh.h" /* for geometry targets */
60 #include "BKE_cdderivedmesh.h" /* for geometry targets */
61 #include "BKE_object.h"
63 #include "BKE_global.h"
64 #include "BKE_library.h"
65 #include "BKE_idprop.h"
67 #ifndef DISABLE_PYTHON
68 #include "BPY_extern.h"
78 #define M_PI 3.14159265358979323846
82 /* ******************* Constraint Channels ********************** */
83 /* Constraint Channels exist in one of two places:
84 * - Under Action Channels in an Action (act->chanbase->achan->constraintChannels)
85 * - Under Object without Object-level Action yet (ob->constraintChannels)
87 * The main purpose that Constraint Channels serve is to act as a link
88 * between an IPO-block (which provides values to interpolate between for some settings)
91 /* ------------ Data Management ----------- */
93 /* Free constraint channels, and reduce the number of users of the related ipo-blocks */
94 void free_constraint_channels (ListBase *chanbase)
96 bConstraintChannel *chan;
98 for (chan=chanbase->first; chan; chan=chan->next) {
104 BLI_freelistN(chanbase);
107 /* Make a copy of the constraint channels from dst to src, and also give the
108 * new constraint channels their own copy of the original's IPO.
110 void copy_constraint_channels (ListBase *dst, ListBase *src)
112 bConstraintChannel *dchan, *schan;
114 dst->first = dst->last = NULL;
115 duplicatelist(dst, src);
117 for (dchan=dst->first, schan=src->first; dchan; dchan=dchan->next, schan=schan->next) {
118 dchan->ipo = copy_ipo(schan->ipo);
122 /* Make a copy of the constraint channels from dst to src, but make the
123 * new constraint channels use the same IPO-data as their twin.
125 void clone_constraint_channels (ListBase *dst, ListBase *src)
127 bConstraintChannel *dchan, *schan;
129 dst->first = dst->last = NULL;
130 duplicatelist(dst, src);
132 for (dchan=dst->first, schan=src->first; dchan; dchan=dchan->next, schan=schan->next) {
133 id_us_plus((ID *)dchan->ipo);
137 /* ------------- Constraint Channel Tools ------------ */
139 /* Find the constraint channel with a given name */
140 bConstraintChannel *get_constraint_channel (ListBase *list, const char name[])
142 bConstraintChannel *chan;
145 for (chan = list->first; chan; chan=chan->next) {
146 if (!strcmp(name, chan->name)) {
155 /* Find or create a new constraint channel */
156 bConstraintChannel *verify_constraint_channel (ListBase *list, const char name[])
158 bConstraintChannel *chan;
160 chan= get_constraint_channel(list, name);
163 chan= MEM_callocN(sizeof(bConstraintChannel), "new constraint channel");
164 BLI_addtail(list, chan);
165 strcpy(chan->name, name);
171 /* --------- Constraint Channel Evaluation/Execution --------- */
173 /* IPO-system call: calculate IPO-block for constraint channels, and flush that
174 * info onto the corresponding constraint.
176 void do_constraint_channels (ListBase *conbase, ListBase *chanbase, float ctime, short onlydrivers)
180 /* for each Constraint, calculate its Influence from the corresponding ConstraintChannel */
181 for (con=conbase->first; con; con=con->next) {
184 if (con->flag & CONSTRAINT_OWN_IPO)
187 bConstraintChannel *chan = get_constraint_channel(chanbase, con->name);
188 if (chan) ipo= chan->ipo;
194 calc_ipo(ipo, ctime);
196 for (icu=ipo->curve.first; icu; icu=icu->next) {
197 if (!onlydrivers || icu->driver) {
198 switch (icu->adrcode) {
201 /* Influence is clamped to 0.0f -> 1.0f range */
202 con->enforce = CLAMPIS(icu->curval, 0.0f, 1.0f);
207 /* we need to check types of constraints that can get this here, as user
208 * may have created an IPO-curve for this from IPO-editor but for a constraint
209 * that cannot support this
212 /* supported constraints go here... */
213 case CONSTRAINT_TYPE_LOCLIKE:
214 case CONSTRAINT_TYPE_TRACKTO:
215 case CONSTRAINT_TYPE_MINMAX:
216 case CONSTRAINT_TYPE_STRETCHTO:
217 case CONSTRAINT_TYPE_DISTLIMIT:
218 con->headtail = icu->curval;
234 /* ************************ Constraints - General Utilities *************************** */
235 /* These functions here don't act on any specific constraints, and are therefore should/will
236 * not require any of the special function-pointers afforded by the relevant constraint
240 /* -------------- Naming -------------- */
242 /* Find the first available, non-duplicate name for a given constraint */
243 void unique_constraint_name (bConstraint *con, ListBase *list)
245 BLI_uniquename(list, con, "Const", offsetof(bConstraint, name), 32);
248 /* ----------------- Evaluation Loop Preparation --------------- */
250 /* package an object/bone for use in constraint evaluation */
251 /* This function MEM_calloc's a bConstraintOb struct, that will need to be freed after evaluation */
252 bConstraintOb *constraints_make_evalob (Object *ob, void *subdata, short datatype)
256 /* create regardless of whether we have any data! */
257 cob= MEM_callocN(sizeof(bConstraintOb), "bConstraintOb");
259 /* based on type of available data */
261 case CONSTRAINT_OBTYPE_OBJECT:
263 /* disregard subdata... calloc should set other values right */
266 cob->type = datatype;
267 Mat4CpyMat4(cob->matrix, ob->obmat);
270 Mat4One(cob->matrix);
272 Mat4CpyMat4(cob->startmat, cob->matrix);
275 case CONSTRAINT_OBTYPE_BONE:
277 /* only set if we have valid bone, otherwise default */
280 cob->pchan = (bPoseChannel *)subdata;
281 cob->type = datatype;
283 /* matrix in world-space */
284 Mat4MulMat4(cob->matrix, cob->pchan->pose_mat, ob->obmat);
287 Mat4One(cob->matrix);
289 Mat4CpyMat4(cob->startmat, cob->matrix);
293 default: /* other types not yet handled */
294 Mat4One(cob->matrix);
295 Mat4One(cob->startmat);
302 /* cleanup after constraint evaluation */
303 void constraints_clear_evalob (bConstraintOb *cob)
305 float delta[4][4], imat[4][4];
307 /* prevent crashes */
311 /* calculate delta of constraints evaluation */
312 Mat4Invert(imat, cob->startmat);
313 Mat4MulMat4(delta, imat, cob->matrix);
315 /* copy matrices back to source */
317 case CONSTRAINT_OBTYPE_OBJECT:
319 /* cob->ob might not exist! */
321 /* copy new ob-matrix back to owner */
322 Mat4CpyMat4(cob->ob->obmat, cob->matrix);
324 /* copy inverse of delta back to owner */
325 Mat4Invert(cob->ob->constinv, delta);
329 case CONSTRAINT_OBTYPE_BONE:
331 /* cob->ob or cob->pchan might not exist */
332 if (cob->ob && cob->pchan) {
333 /* copy new pose-matrix back to owner */
334 Mat4MulMat4(cob->pchan->pose_mat, cob->matrix, cob->ob->imat);
336 /* copy inverse of delta back to owner */
337 Mat4Invert(cob->pchan->constinv, delta);
343 /* free tempolary struct */
347 /* -------------- Space-Conversion API -------------- */
349 /* This function is responsible for the correct transformations/conversions
350 * of a matrix from one space to another for constraint evaluation.
351 * For now, this is only implemented for Objects and PoseChannels.
353 void constraint_mat_convertspace (Object *ob, bPoseChannel *pchan, float mat[][4], short from, short to)
356 float diff_mat[4][4];
359 /* prevent crashes in these unlikely events */
360 if (ob==NULL || mat==NULL) return;
361 /* optimise trick - check if need to do anything */
362 if (from == to) return;
364 /* are we dealing with pose-channels or objects */
368 case CONSTRAINT_SPACE_WORLD: /* ---------- FROM WORLDSPACE ---------- */
371 Mat4Invert(imat, ob->obmat);
372 Mat4CpyMat4(tempmat, mat);
373 Mat4MulMat4(mat, tempmat, imat);
375 /* use pose-space as stepping stone for other spaces... */
376 if (ELEM(to, CONSTRAINT_SPACE_LOCAL, CONSTRAINT_SPACE_PARLOCAL)) {
377 /* call self with slightly different values */
378 constraint_mat_convertspace(ob, pchan, mat, CONSTRAINT_SPACE_POSE, to);
382 case CONSTRAINT_SPACE_POSE: /* ---------- FROM POSESPACE ---------- */
385 if (to == CONSTRAINT_SPACE_WORLD) {
386 Mat4CpyMat4(tempmat, mat);
387 Mat4MulMat4(mat, tempmat, ob->obmat);
390 else if (to == CONSTRAINT_SPACE_LOCAL) {
393 float offs_bone[4][4];
395 /* construct offs_bone the same way it is done in armature.c */
396 Mat4CpyMat3(offs_bone, pchan->bone->bone_mat);
397 VECCOPY(offs_bone[3], pchan->bone->head);
398 offs_bone[3][1]+= pchan->bone->parent->length;
400 if (pchan->bone->flag & BONE_HINGE) {
401 /* pose_mat = par_pose-space_location * chan_mat */
404 /* the rotation of the parent restposition */
405 Mat4CpyMat4(tmat, pchan->bone->parent->arm_mat);
407 /* the location of actual parent transform */
408 VECCOPY(tmat[3], offs_bone[3]);
409 offs_bone[3][0]= offs_bone[3][1]= offs_bone[3][2]= 0.0f;
410 Mat4MulVecfl(pchan->parent->pose_mat, tmat[3]);
412 Mat4MulMat4(diff_mat, offs_bone, tmat);
413 Mat4Invert(imat, diff_mat);
416 /* pose_mat = par_pose_mat * bone_mat * chan_mat */
417 Mat4MulMat4(diff_mat, offs_bone, pchan->parent->pose_mat);
418 Mat4Invert(imat, diff_mat);
422 /* pose_mat = chan_mat * arm_mat */
423 Mat4Invert(imat, pchan->bone->arm_mat);
426 Mat4CpyMat4(tempmat, mat);
427 Mat4MulMat4(mat, tempmat, imat);
430 /* pose to local with parent */
431 else if (to == CONSTRAINT_SPACE_PARLOCAL) {
433 Mat4Invert(imat, pchan->bone->arm_mat);
434 Mat4CpyMat4(tempmat, mat);
435 Mat4MulMat4(mat, tempmat, imat);
440 case CONSTRAINT_SPACE_LOCAL: /* ------------ FROM LOCALSPACE --------- */
442 /* local to pose - do inverse procedure that was done for pose to local */
444 /* we need the posespace_matrix = local_matrix + (parent_posespace_matrix + restpos) */
446 float offs_bone[4][4];
448 /* construct offs_bone the same way it is done in armature.c */
449 Mat4CpyMat3(offs_bone, pchan->bone->bone_mat);
450 VECCOPY(offs_bone[3], pchan->bone->head);
451 offs_bone[3][1]+= pchan->bone->parent->length;
453 if (pchan->bone->flag & BONE_HINGE) {
454 /* pose_mat = par_pose-space_location * chan_mat */
457 /* the rotation of the parent restposition */
458 Mat4CpyMat4(tmat, pchan->bone->parent->arm_mat);
460 /* the location of actual parent transform */
461 VECCOPY(tmat[3], offs_bone[3]);
462 offs_bone[3][0]= offs_bone[3][1]= offs_bone[3][2]= 0.0f;
463 Mat4MulVecfl(pchan->parent->pose_mat, tmat[3]);
465 Mat4MulMat4(diff_mat, offs_bone, tmat);
466 Mat4CpyMat4(tempmat, mat);
467 Mat4MulMat4(mat, tempmat, diff_mat);
470 /* pose_mat = par_pose_mat * bone_mat * chan_mat */
471 Mat4MulMat4(diff_mat, offs_bone, pchan->parent->pose_mat);
472 Mat4CpyMat4(tempmat, mat);
473 Mat4MulMat4(mat, tempmat, diff_mat);
477 Mat4CpyMat4(diff_mat, pchan->bone->arm_mat);
479 Mat4CpyMat4(tempmat, mat);
480 Mat4MulMat4(mat, tempmat, diff_mat);
484 /* use pose-space as stepping stone for other spaces */
485 if (ELEM(to, CONSTRAINT_SPACE_WORLD, CONSTRAINT_SPACE_PARLOCAL)) {
486 /* call self with slightly different values */
487 constraint_mat_convertspace(ob, pchan, mat, CONSTRAINT_SPACE_POSE, to);
491 case CONSTRAINT_SPACE_PARLOCAL: /* -------------- FROM LOCAL WITH PARENT ---------- */
493 /* local + parent to pose */
495 Mat4CpyMat4(diff_mat, pchan->bone->arm_mat);
496 Mat4CpyMat4(tempmat, mat);
497 Mat4MulMat4(mat, diff_mat, tempmat);
500 /* use pose-space as stepping stone for other spaces */
501 if (ELEM(to, CONSTRAINT_SPACE_WORLD, CONSTRAINT_SPACE_LOCAL)) {
502 /* call self with slightly different values */
503 constraint_mat_convertspace(ob, pchan, mat, CONSTRAINT_SPACE_POSE, to);
511 if (from==CONSTRAINT_SPACE_WORLD && to==CONSTRAINT_SPACE_LOCAL) {
512 /* check if object has a parent - otherwise this won't work */
514 /* 'subtract' parent's effects from owner */
515 Mat4MulMat4(diff_mat, ob->parentinv, ob->parent->obmat);
516 Mat4Invert(imat, diff_mat);
517 Mat4CpyMat4(tempmat, mat);
518 Mat4MulMat4(mat, tempmat, imat);
521 else if (from==CONSTRAINT_SPACE_LOCAL && to==CONSTRAINT_SPACE_WORLD) {
522 /* check that object has a parent - otherwise this won't work */
524 /* 'add' parent's effect back to owner */
525 Mat4CpyMat4(tempmat, mat);
526 Mat4MulMat4(diff_mat, ob->parentinv, ob->parent->obmat);
527 Mat4MulMat4(mat, tempmat, diff_mat);
533 /* ------------ General Target Matrix Tools ---------- */
535 /* function that sets the given matrix based on given vertex group in mesh */
536 static void contarget_get_mesh_mat (Object *ob, char *substring, float mat[][4])
539 float vec[3] = {0.0f, 0.0f, 0.0f}, tvec[3];
540 float normal[3] = {0.0f, 0.0f, 0.0f}, plane[3];
541 float imat[3][3], tmat[3][3];
544 /* initialize target matrix using target matrix */
545 Mat4CpyMat4(mat, ob->obmat);
547 /* get index of vertex group */
548 dgroup = get_named_vertexgroup_num(ob, substring);
549 if (dgroup < 0) return;
551 /* get DerivedMesh */
552 if ((G.obedit == ob) && (G.editMesh)) {
553 /* target is in editmode, so get a special derived mesh */
554 dm = CDDM_from_editmesh(G.editMesh, ob->data);
557 /* when not in EditMode, this should exist */
558 dm = (DerivedMesh *)ob->derivedFinal;
561 /* only continue if there's a valid DerivedMesh */
563 MDeformVert *dvert = dm->getVertDataArray(dm, CD_MDEFORMVERT);
564 int *index = (int *)dm->getVertDataArray(dm, CD_ORIGINDEX);
565 int numVerts = dm->getNumVerts(dm);
569 /* check that dvert and index are valid pointers (just in case) */
570 if (dvert && index) {
571 /* get the average of all verts with that are in the vertex-group */
572 for (i = 0; i < numVerts; i++, index++) {
573 for (j = 0; j < dvert[i].totweight; j++) {
574 /* does this vertex belong to nominated vertex group? */
575 if (dvert[i].dw[j].def_nr == dgroup) {
576 dm->getVertCo(dm, i, co);
577 dm->getVertNo(dm, i, nor);
578 VecAddf(vec, vec, co);
579 VecAddf(normal, normal, nor);
588 /* calculate averages of normal and coordinates */
590 VecMulf(vec, 1.0f / count);
591 VecMulf(normal, 1.0f / count);
595 /* derive the rotation from the average normal:
596 * - code taken from transform_manipulator.c,
597 * calc_manipulator_stats, V3D_MANIP_NORMAL case
599 /* we need the transpose of the inverse for a normal... */
600 Mat3CpyMat4(imat, ob->obmat);
604 Mat3MulVecfl(tmat, normal);
607 VECCOPY(plane, tmat[1]);
609 VECCOPY(tmat[2], normal);
610 Crossf(tmat[0], normal, plane);
611 Crossf(tmat[1], tmat[2], tmat[0]);
613 Mat4CpyMat3(mat, tmat);
617 /* apply the average coordinate as the new location */
618 VecMat4MulVecfl(tvec, ob->obmat, vec);
619 VECCOPY(mat[3], tvec);
623 /* free temporary DerivedMesh created (in EditMode case) */
625 if (dm) dm->release(dm);
629 /* function that sets the given matrix based on given vertex group in lattice */
630 static void contarget_get_lattice_mat (Object *ob, char *substring, float mat[][4])
632 Lattice *lt= (Lattice *)ob->data;
634 DispList *dl = find_displist(&ob->disp, DL_VERTS);
635 float *co = dl?dl->verts:NULL;
636 BPoint *bp = lt->def;
638 MDeformVert *dvert = lt->dvert;
639 int tot_verts= lt->pntsu*lt->pntsv*lt->pntsw;
640 float vec[3]= {0.0f, 0.0f, 0.0f}, tvec[3];
641 int dgroup=0, grouped=0;
644 /* initialize target matrix using target matrix */
645 Mat4CpyMat4(mat, ob->obmat);
647 /* get index of vertex group */
648 dgroup = get_named_vertexgroup_num(ob, substring);
649 if (dgroup < 0) return;
650 if (dvert == NULL) return;
652 /* 1. Loop through control-points checking if in nominated vertex-group.
653 * 2. If it is, add it to vec to find the average point.
655 for (i=0; i < tot_verts; i++, dvert++) {
656 for (n= 0; n < dvert->totweight; n++) {
657 /* found match - vert is in vgroup */
658 if (dvert->dw[n].def_nr == dgroup) {
659 /* copy coordinates of point to temporary vector, then add to find average */
661 memcpy(tvec, co, 3*sizeof(float));
663 memcpy(tvec, bp->vec, 3*sizeof(float));
665 VecAddf(vec, vec, tvec);
672 /* advance pointer to coordinate data */
677 /* find average location, then multiply by ob->obmat to find world-space location */
679 VecMulf(vec, 1.0f / grouped);
680 VecMat4MulVecfl(tvec, ob->obmat, vec);
682 /* copy new location to matrix */
683 VECCOPY(mat[3], tvec);
686 /* generic function to get the appropriate matrix for most target cases */
687 /* The cases where the target can be object data have not been implemented */
688 static void constraint_target_to_mat4 (Object *ob, char *substring, float mat[][4], short from, short to, float headtail)
691 if (!strlen(substring)) {
692 Mat4CpyMat4(mat, ob->obmat);
693 constraint_mat_convertspace(ob, NULL, mat, from, to);
695 /* Case VERTEXGROUP */
696 /* Current method just takes the average location of all the points in the
697 * VertexGroup, and uses that as the location value of the targets. Where
698 * possible, the orientation will also be calculated, by calculating an
699 * 'average' vertex normal, and deriving the rotaation from that.
701 * NOTE: EditMode is not currently supported, and will most likely remain that
702 * way as constraints can only really affect things on object/bone level.
704 else if (ob->type == OB_MESH) {
705 contarget_get_mesh_mat(ob, substring, mat);
706 constraint_mat_convertspace(ob, NULL, mat, from, to);
708 else if (ob->type == OB_LATTICE) {
709 contarget_get_lattice_mat(ob, substring, mat);
710 constraint_mat_convertspace(ob, NULL, mat, from, to);
716 pchan = get_pose_channel(ob->pose, substring);
718 /* Multiply the PoseSpace accumulation/final matrix for this
719 * PoseChannel by the Armature Object's Matrix to get a worldspace
722 if (headtail < 0.000001) {
723 /* skip length interpolation if set to head */
724 Mat4MulMat4(mat, pchan->pose_mat, ob->obmat);
727 float tempmat[4][4], loc[3];
729 /* interpolate along length of bone */
730 VecLerpf(loc, pchan->pose_head, pchan->pose_tail, headtail);
732 /* use interpolated distance for subtarget */
733 Mat4CpyMat4(tempmat, pchan->pose_mat);
734 VecCopyf(tempmat[3], loc);
736 Mat4MulMat4(mat, tempmat, ob->obmat);
740 Mat4CpyMat4(mat, ob->obmat);
742 /* convert matrix space as required */
743 constraint_mat_convertspace(ob, pchan, mat, from, to);
747 /* ************************* Specific Constraints ***************************** */
748 /* Each constraint defines a set of functions, which will be called at the appropriate
749 * times. In addition to this, each constraint should have a type-info struct, where
750 * its functions are attached for use.
753 /* Template for type-info data:
754 * - make a copy of this when creating new constraints, and just change the functions
755 * pointed to as necessary
756 * - although the naming of functions doesn't matter, it would help for code
757 * readability, to follow the same naming convention as is presented here
758 * - any functions that a constraint doesn't need to define, don't define
759 * for such cases, just use NULL
760 * - these should be defined after all the functions have been defined, so that
761 * forward-definitions/prototypes don't need to be used!
762 * - keep this copy #if-def'd so that future constraints can get based off this
765 static bConstraintTypeInfo CTI_CONSTRNAME = {
766 CONSTRAINT_TYPE_CONSTRNAME, /* type */
767 sizeof(bConstrNameConstraint), /* size */
768 "ConstrName", /* name */
769 "bConstrNameConstraint", /* struct name */
770 constrname_free, /* free data */
771 constrname_relink, /* relink data */
772 constrname_copy, /* copy data */
773 constrname_new_data, /* new data */
774 constrname_get_tars, /* get constraint targets */
775 constrname_flush_tars, /* flush constraint targets */
776 constrname_get_tarmat, /* get target matrix */
777 constrname_evaluate /* evaluate */
781 /* This function should be used for the get_target_matrix member of all
782 * constraints that are not picky about what happens to their target matrix.
784 static void default_get_tarmat (bConstraint *con, bConstraintOb *cob, bConstraintTarget *ct, float ctime)
786 if (VALID_CONS_TARGET(ct))
787 constraint_target_to_mat4(ct->tar, ct->subtarget, ct->matrix, CONSTRAINT_SPACE_WORLD, ct->space, con->headtail);
792 /* This following macro should be used for all standard single-target *_get_tars functions
793 * to save typing and reduce maintainance woes.
794 * (Hopefully all compilers will be happy with the lines with just a space on them. Those are
795 * really just to help this code easier to read)
797 #define SINGLETARGET_GET_TARS(con, datatar, datasubtarget, ct, list) \
799 ct= MEM_callocN(sizeof(bConstraintTarget), "tempConstraintTarget"); \
802 strcpy(ct->subtarget, datasubtarget); \
803 ct->space= con->tarspace; \
804 ct->flag= CONSTRAINT_TAR_TEMP; \
807 if ((ct->tar->type==OB_ARMATURE) && (ct->subtarget[0])) ct->type = CONSTRAINT_OBTYPE_BONE; \
808 else if (ELEM(ct->tar->type, OB_MESH, OB_LATTICE) && (ct->subtarget[0])) ct->type = CONSTRAINT_OBTYPE_VERT; \
809 else ct->type = CONSTRAINT_OBTYPE_OBJECT; \
812 BLI_addtail(list, ct); \
815 /* This following macro should be used for all standard single-target *_get_tars functions
816 * to save typing and reduce maintainance woes. It does not do the subtarget related operations
817 * (Hopefully all compilers will be happy with the lines with just a space on them. Those are
818 * really just to help this code easier to read)
820 #define SINGLETARGETNS_GET_TARS(con, datatar, ct, list) \
822 ct= MEM_callocN(sizeof(bConstraintTarget), "tempConstraintTarget"); \
825 ct->space= con->tarspace; \
826 ct->flag= CONSTRAINT_TAR_TEMP; \
828 if (ct->tar) ct->type = CONSTRAINT_OBTYPE_OBJECT; \
830 BLI_addtail(list, ct); \
833 /* This following macro should be used for all standard single-target *_flush_tars functions
834 * to save typing and reduce maintainance woes.
835 * Note: the pointer to ct will be changed to point to the next in the list (as it gets removed)
836 * (Hopefully all compilers will be happy with the lines with just a space on them. Those are
837 * really just to help this code easier to read)
839 #define SINGLETARGET_FLUSH_TARS(con, datatar, datasubtarget, ct, list, nocopy) \
842 bConstraintTarget *ctn = ct->next; \
845 strcpy(datasubtarget, ct->subtarget); \
846 con->tarspace= (char)ct->space; \
849 BLI_freelinkN(list, ct); \
854 /* This following macro should be used for all standard single-target *_flush_tars functions
855 * to save typing and reduce maintainance woes. It does not do the subtarget related operations.
856 * Note: the pointer to ct will be changed to point to the next in the list (as it gets removed)
857 * (Hopefully all compilers will be happy with the lines with just a space on them. Those are
858 * really just to help this code easier to read)
860 #define SINGLETARGETNS_FLUSH_TARS(con, datatar, ct, list, nocopy) \
863 bConstraintTarget *ctn = ct->next; \
866 con->tarspace= (char)ct->space; \
869 BLI_freelinkN(list, ct); \
874 /* --------- ChildOf Constraint ------------ */
876 static void childof_new_data (void *cdata)
878 bChildOfConstraint *data= (bChildOfConstraint *)cdata;
880 data->flag = (CHILDOF_LOCX | CHILDOF_LOCY | CHILDOF_LOCZ |
881 CHILDOF_ROTX |CHILDOF_ROTY | CHILDOF_ROTZ |
882 CHILDOF_SIZEX | CHILDOF_SIZEY | CHILDOF_SIZEZ);
883 Mat4One(data->invmat);
886 static int childof_get_tars (bConstraint *con, ListBase *list)
889 bChildOfConstraint *data= con->data;
890 bConstraintTarget *ct;
892 /* standard target-getting macro for single-target constraints */
893 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list)
901 static void childof_flush_tars (bConstraint *con, ListBase *list, short nocopy)
904 bChildOfConstraint *data= con->data;
905 bConstraintTarget *ct= list->first;
907 /* the following macro is used for all standard single-target constraints */
908 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, nocopy)
912 static void childof_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
914 bChildOfConstraint *data= con->data;
915 bConstraintTarget *ct= targets->first;
917 /* only evaluate if there is a target */
918 if (VALID_CONS_TARGET(ct)) {
919 float parmat[4][4], invmat[4][4], tempmat[4][4];
920 float loc[3], eul[3], size[3];
921 float loco[3], eulo[3], sizo[3];
923 /* get offset (parent-inverse) matrix */
924 Mat4CpyMat4(invmat, data->invmat);
926 /* extract components of both matrices */
927 VECCOPY(loc, ct->matrix[3]);
928 Mat4ToEul(ct->matrix, eul);
929 Mat4ToSize(ct->matrix, size);
931 VECCOPY(loco, invmat[3]);
932 Mat4ToEul(invmat, eulo);
933 Mat4ToSize(invmat, sizo);
935 /* disable channels not enabled */
936 if (!(data->flag & CHILDOF_LOCX)) loc[0]= loco[0]= 0.0f;
937 if (!(data->flag & CHILDOF_LOCY)) loc[1]= loco[1]= 0.0f;
938 if (!(data->flag & CHILDOF_LOCZ)) loc[2]= loco[2]= 0.0f;
939 if (!(data->flag & CHILDOF_ROTX)) eul[0]= eulo[0]= 0.0f;
940 if (!(data->flag & CHILDOF_ROTY)) eul[1]= eulo[1]= 0.0f;
941 if (!(data->flag & CHILDOF_ROTZ)) eul[2]= eulo[2]= 0.0f;
942 if (!(data->flag & CHILDOF_SIZEX)) size[0]= sizo[0]= 1.0f;
943 if (!(data->flag & CHILDOF_SIZEY)) size[1]= sizo[1]= 1.0f;
944 if (!(data->flag & CHILDOF_SIZEZ)) size[2]= sizo[2]= 1.0f;
946 /* make new target mat and offset mat */
947 LocEulSizeToMat4(ct->matrix, loc, eul, size);
948 LocEulSizeToMat4(invmat, loco, eulo, sizo);
950 /* multiply target (parent matrix) by offset (parent inverse) to get
951 * the effect of the parent that will be exherted on the owner
953 Mat4MulMat4(parmat, invmat, ct->matrix);
955 /* now multiply the parent matrix by the owner matrix to get the
956 * the effect of this constraint (i.e. owner is 'parented' to parent)
958 Mat4CpyMat4(tempmat, cob->matrix);
959 Mat4MulMat4(cob->matrix, tempmat, parmat);
963 static bConstraintTypeInfo CTI_CHILDOF = {
964 CONSTRAINT_TYPE_CHILDOF, /* type */
965 sizeof(bChildOfConstraint), /* size */
966 "ChildOf", /* name */
967 "bChildOfConstraint", /* struct name */
968 NULL, /* free data */
969 NULL, /* relink data */
970 NULL, /* copy data */
971 childof_new_data, /* new data */
972 childof_get_tars, /* get constraint targets */
973 childof_flush_tars, /* flush constraint targets */
974 default_get_tarmat, /* get a target matrix */
975 childof_evaluate /* evaluate */
978 /* -------- TrackTo Constraint ------- */
980 static void trackto_new_data (void *cdata)
982 bTrackToConstraint *data= (bTrackToConstraint *)cdata;
984 data->reserved1 = TRACK_Y;
985 data->reserved2 = UP_Z;
988 static int trackto_get_tars (bConstraint *con, ListBase *list)
991 bTrackToConstraint *data= con->data;
992 bConstraintTarget *ct;
994 /* standard target-getting macro for single-target constraints */
995 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list)
1003 static void trackto_flush_tars (bConstraint *con, ListBase *list, short nocopy)
1006 bTrackToConstraint *data= con->data;
1007 bConstraintTarget *ct= list->first;
1009 /* the following macro is used for all standard single-target constraints */
1010 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, nocopy)
1015 static int basis_cross (int n, int m)
1031 static void vectomat (float *vec, float *target_up, short axis, short upflag, short flags, float m[][3])
1034 float u[3]; /* vector specifying the up axis */
1041 if (Normalize(n) == 0.0) {
1046 if (axis > 2) axis -= 3;
1049 /* n specifies the transformation of the track axis */
1050 if (flags & TARGET_Z_UP) {
1051 /* target Z axis is the global up axis */
1052 u[0] = target_up[0];
1053 u[1] = target_up[1];
1054 u[2] = target_up[2];
1057 /* world Z axis is the global up axis */
1063 /* project the up vector onto the plane specified by n */
1064 Projf(proj, u, n); /* first u onto n... */
1065 VecSubf(proj, u, proj); /* then onto the plane */
1066 /* proj specifies the transformation of the up axis */
1068 if (Normalize(proj) == 0.0) { /* degenerate projection */
1074 /* Normalized cross product of n and proj specifies transformation of the right axis */
1075 Crossf(right, proj, n);
1078 if (axis != upflag) {
1079 right_index = 3 - axis - upflag;
1080 neg = (float)basis_cross(axis, upflag);
1082 /* account for up direction, track direction */
1083 m[right_index][0] = neg * right[0];
1084 m[right_index][1] = neg * right[1];
1085 m[right_index][2] = neg * right[2];
1087 m[upflag][0] = proj[0];
1088 m[upflag][1] = proj[1];
1089 m[upflag][2] = proj[2];
1095 /* identity matrix - don't do anything if the two axes are the same */
1097 m[0][0]= m[1][1]= m[2][2]= 1.0;
1098 m[0][1]= m[0][2]= 0.0;
1099 m[1][0]= m[1][2]= 0.0;
1100 m[2][0]= m[2][1]= 0.0;
1105 static void trackto_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
1107 bTrackToConstraint *data= con->data;
1108 bConstraintTarget *ct= targets->first;
1110 if (VALID_CONS_TARGET(ct)) {
1111 float size[3], vec[3];
1115 /* Get size property, since ob->size is only the object's own relative size, not its global one */
1116 Mat4ToSize(cob->matrix, size);
1118 /* Clear the object's rotation */
1119 cob->matrix[0][0]=size[0];
1120 cob->matrix[0][1]=0;
1121 cob->matrix[0][2]=0;
1122 cob->matrix[1][0]=0;
1123 cob->matrix[1][1]=size[1];
1124 cob->matrix[1][2]=0;
1125 cob->matrix[2][0]=0;
1126 cob->matrix[2][1]=0;
1127 cob->matrix[2][2]=size[2];
1129 /* targetmat[2] instead of ownermat[2] is passed to vectomat
1130 * for backwards compatability it seems... (Aligorith)
1132 VecSubf(vec, cob->matrix[3], ct->matrix[3]);
1133 vectomat(vec, ct->matrix[2],
1134 (short)data->reserved1, (short)data->reserved2,
1135 data->flags, totmat);
1137 Mat4CpyMat4(tmat, cob->matrix);
1138 Mat4MulMat34(cob->matrix, totmat, tmat);
1142 static bConstraintTypeInfo CTI_TRACKTO = {
1143 CONSTRAINT_TYPE_TRACKTO, /* type */
1144 sizeof(bTrackToConstraint), /* size */
1145 "TrackTo", /* name */
1146 "bTrackToConstraint", /* struct name */
1147 NULL, /* free data */
1148 NULL, /* relink data */
1149 NULL, /* copy data */
1150 trackto_new_data, /* new data */
1151 trackto_get_tars, /* get constraint targets */
1152 trackto_flush_tars, /* flush constraint targets */
1153 default_get_tarmat, /* get target matrix */
1154 trackto_evaluate /* evaluate */
1157 /* --------- Inverse-Kinemetics --------- */
1159 static void kinematic_new_data (void *cdata)
1161 bKinematicConstraint *data= (bKinematicConstraint *)cdata;
1163 data->weight= (float)1.0;
1164 data->orientweight= (float)1.0;
1165 data->iterations = 500;
1166 data->flag= CONSTRAINT_IK_TIP|CONSTRAINT_IK_STRETCH|CONSTRAINT_IK_POS;
1169 static int kinematic_get_tars (bConstraint *con, ListBase *list)
1172 bKinematicConstraint *data= con->data;
1173 bConstraintTarget *ct;
1175 /* standard target-getting macro for single-target constraints is used twice here */
1176 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list)
1177 SINGLETARGET_GET_TARS(con, data->poletar, data->polesubtarget, ct, list)
1185 static void kinematic_flush_tars (bConstraint *con, ListBase *list, short nocopy)
1188 bKinematicConstraint *data= con->data;
1189 bConstraintTarget *ct= list->first;
1191 /* the following macro is used for all standard single-target constraints */
1192 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, nocopy)
1193 SINGLETARGET_FLUSH_TARS(con, data->poletar, data->polesubtarget, ct, list, nocopy)
1197 static void kinematic_get_tarmat (bConstraint *con, bConstraintOb *cob, bConstraintTarget *ct, float ctime)
1199 bKinematicConstraint *data= con->data;
1201 if (VALID_CONS_TARGET(ct))
1202 constraint_target_to_mat4(ct->tar, ct->subtarget, ct->matrix, CONSTRAINT_SPACE_WORLD, ct->space, con->headtail);
1204 if (data->flag & CONSTRAINT_IK_AUTO) {
1205 Object *ob= cob->ob;
1208 Mat4One(ct->matrix);
1212 /* move grabtarget into world space */
1213 VECCOPY(vec, data->grabtarget);
1214 Mat4MulVecfl(ob->obmat, vec);
1215 Mat4CpyMat4(ct->matrix, ob->obmat);
1216 VECCOPY(ct->matrix[3], vec);
1220 Mat4One(ct->matrix);
1224 static bConstraintTypeInfo CTI_KINEMATIC = {
1225 CONSTRAINT_TYPE_KINEMATIC, /* type */
1226 sizeof(bKinematicConstraint), /* size */
1228 "bKinematicConstraint", /* struct name */
1229 NULL, /* free data */
1230 NULL, /* relink data */
1231 NULL, /* copy data */
1232 kinematic_new_data, /* new data */
1233 kinematic_get_tars, /* get constraint targets */
1234 kinematic_flush_tars, /* flush constraint targets */
1235 kinematic_get_tarmat, /* get target matrix */
1236 NULL /* evaluate - solved as separate loop */
1239 /* -------- Follow-Path Constraint ---------- */
1241 static void followpath_new_data (void *cdata)
1243 bFollowPathConstraint *data= (bFollowPathConstraint *)cdata;
1245 data->trackflag = TRACK_Y;
1246 data->upflag = UP_Z;
1248 data->followflag = 0;
1251 static int followpath_get_tars (bConstraint *con, ListBase *list)
1254 bFollowPathConstraint *data= con->data;
1255 bConstraintTarget *ct;
1257 /* standard target-getting macro for single-target constraints without subtargets */
1258 SINGLETARGETNS_GET_TARS(con, data->tar, ct, list)
1266 static void followpath_flush_tars (bConstraint *con, ListBase *list, short nocopy)
1269 bFollowPathConstraint *data= con->data;
1270 bConstraintTarget *ct= list->first;
1272 /* the following macro is used for all standard single-target constraints */
1273 SINGLETARGETNS_FLUSH_TARS(con, data->tar, ct, list, nocopy)
1277 static void followpath_get_tarmat (bConstraint *con, bConstraintOb *cob, bConstraintTarget *ct, float ctime)
1279 bFollowPathConstraint *data= con->data;
1281 if (VALID_CONS_TARGET(ct)) {
1282 Curve *cu= ct->tar->data;
1283 float q[4], vec[4], dir[3], quat[4], x1;
1288 Mat4One(ct->matrix);
1290 /* note: when creating constraints that follow path, the curve gets the CU_PATH set now,
1291 * currently for paths to work it needs to go through the bevlist/displist system (ton)
1294 /* only happens on reload file, but violates depsgraph still... fix! */
1295 if (cu->path==NULL || cu->path->data==NULL)
1296 makeDispListCurveTypes(ct->tar, 0);
1298 if (cu->path && cu->path->data) {
1299 curvetime= bsystem_time(ct->tar, (float)ctime, 0.0) - data->offset;
1301 if (calc_ipo_spec(cu->ipo, CU_SPEED, &curvetime)==0) {
1302 curvetime /= cu->pathlen;
1303 CLAMP(curvetime, 0.0, 1.0);
1306 if ( where_on_path(ct->tar, curvetime, vec, dir) ) {
1307 if (data->followflag) {
1308 vectoquat(dir, (short) data->trackflag, (short) data->upflag, quat);
1311 q[0]= (float)cos(0.5*vec[3]);
1312 x1= (float)sin(0.5*vec[3]);
1316 QuatMul(quat, q, quat);
1318 QuatToMat4(quat, totmat);
1320 VECCOPY(totmat[3], vec);
1322 Mat4MulSerie(ct->matrix, ct->tar->obmat, totmat, NULL, NULL, NULL, NULL, NULL, NULL);
1327 Mat4One(ct->matrix);
1330 static void followpath_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
1332 bConstraintTarget *ct= targets->first;
1334 /* only evaluate if there is a target */
1335 if (VALID_CONS_TARGET(ct)) {
1337 float size[3], obsize[3];
1339 /* get Object local transform (loc/rot/size) to determine transformation from path */
1340 //object_to_mat4(ob, obmat);
1341 Mat4CpyMat4(obmat, cob->matrix); // FIXME!!!
1343 /* get scaling of object before applying constraint */
1344 Mat4ToSize(cob->matrix, size);
1346 /* apply targetmat - containing location on path, and rotation */
1347 Mat4MulSerie(cob->matrix, ct->matrix, obmat, NULL, NULL, NULL, NULL, NULL, NULL);
1349 /* un-apply scaling caused by path */
1350 Mat4ToSize(cob->matrix, obsize);
1352 VecMulf(cob->matrix[0], size[0] / obsize[0]);
1354 VecMulf(cob->matrix[1], size[1] / obsize[1]);
1356 VecMulf(cob->matrix[2], size[2] / obsize[2]);
1360 static bConstraintTypeInfo CTI_FOLLOWPATH = {
1361 CONSTRAINT_TYPE_FOLLOWPATH, /* type */
1362 sizeof(bFollowPathConstraint), /* size */
1363 "Follow Path", /* name */
1364 "bFollowPathConstraint", /* struct name */
1365 NULL, /* free data */
1366 NULL, /* relink data */
1367 NULL, /* copy data */
1368 followpath_new_data, /* new data */
1369 followpath_get_tars, /* get constraint targets */
1370 followpath_flush_tars, /* flush constraint targets */
1371 followpath_get_tarmat, /* get target matrix */
1372 followpath_evaluate /* evaluate */
1375 /* --------- Limit Location --------- */
1378 static void loclimit_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
1380 bLocLimitConstraint *data = con->data;
1382 if (data->flag & LIMIT_XMIN) {
1383 if (cob->matrix[3][0] < data->xmin)
1384 cob->matrix[3][0] = data->xmin;
1386 if (data->flag & LIMIT_XMAX) {
1387 if (cob->matrix[3][0] > data->xmax)
1388 cob->matrix[3][0] = data->xmax;
1390 if (data->flag & LIMIT_YMIN) {
1391 if (cob->matrix[3][1] < data->ymin)
1392 cob->matrix[3][1] = data->ymin;
1394 if (data->flag & LIMIT_YMAX) {
1395 if (cob->matrix[3][1] > data->ymax)
1396 cob->matrix[3][1] = data->ymax;
1398 if (data->flag & LIMIT_ZMIN) {
1399 if (cob->matrix[3][2] < data->zmin)
1400 cob->matrix[3][2] = data->zmin;
1402 if (data->flag & LIMIT_ZMAX) {
1403 if (cob->matrix[3][2] > data->zmax)
1404 cob->matrix[3][2] = data->zmax;
1408 static bConstraintTypeInfo CTI_LOCLIMIT = {
1409 CONSTRAINT_TYPE_LOCLIMIT, /* type */
1410 sizeof(bLocLimitConstraint), /* size */
1411 "Limit Location", /* name */
1412 "bLocLimitConstraint", /* struct name */
1413 NULL, /* free data */
1414 NULL, /* relink data */
1415 NULL, /* copy data */
1416 NULL, /* new data */
1417 NULL, /* get constraint targets */
1418 NULL, /* flush constraint targets */
1419 NULL, /* get target matrix */
1420 loclimit_evaluate /* evaluate */
1423 /* -------- Limit Rotation --------- */
1425 static void rotlimit_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
1427 bRotLimitConstraint *data = con->data;
1432 VECCOPY(loc, cob->matrix[3]);
1433 Mat4ToSize(cob->matrix, size);
1435 Mat4ToEul(cob->matrix, eul);
1437 /* eulers: radians to degrees! */
1438 eul[0] = (float)(eul[0] / M_PI * 180);
1439 eul[1] = (float)(eul[1] / M_PI * 180);
1440 eul[2] = (float)(eul[2] / M_PI * 180);
1442 /* limiting of euler values... */
1443 if (data->flag & LIMIT_XROT) {
1444 if (eul[0] < data->xmin)
1445 eul[0] = data->xmin;
1447 if (eul[0] > data->xmax)
1448 eul[0] = data->xmax;
1450 if (data->flag & LIMIT_YROT) {
1451 if (eul[1] < data->ymin)
1452 eul[1] = data->ymin;
1454 if (eul[1] > data->ymax)
1455 eul[1] = data->ymax;
1457 if (data->flag & LIMIT_ZROT) {
1458 if (eul[2] < data->zmin)
1459 eul[2] = data->zmin;
1461 if (eul[2] > data->zmax)
1462 eul[2] = data->zmax;
1465 /* eulers: degrees to radians ! */
1466 eul[0] = (float)(eul[0] / 180 * M_PI);
1467 eul[1] = (float)(eul[1] / 180 * M_PI);
1468 eul[2] = (float)(eul[2] / 180 * M_PI);
1470 LocEulSizeToMat4(cob->matrix, loc, eul, size);
1473 static bConstraintTypeInfo CTI_ROTLIMIT = {
1474 CONSTRAINT_TYPE_ROTLIMIT, /* type */
1475 sizeof(bRotLimitConstraint), /* size */
1476 "Limit Rotation", /* name */
1477 "bRotLimitConstraint", /* struct name */
1478 NULL, /* free data */
1479 NULL, /* relink data */
1480 NULL, /* copy data */
1481 NULL, /* new data */
1482 NULL, /* get constraint targets */
1483 NULL, /* flush constraint targets */
1484 NULL, /* get target matrix */
1485 rotlimit_evaluate /* evaluate */
1488 /* --------- Limit Scaling --------- */
1491 static void sizelimit_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
1493 bSizeLimitConstraint *data = con->data;
1494 float obsize[3], size[3];
1496 Mat4ToSize(cob->matrix, size);
1497 Mat4ToSize(cob->matrix, obsize);
1499 if (data->flag & LIMIT_XMIN) {
1500 if (size[0] < data->xmin)
1501 size[0] = data->xmin;
1503 if (data->flag & LIMIT_XMAX) {
1504 if (size[0] > data->xmax)
1505 size[0] = data->xmax;
1507 if (data->flag & LIMIT_YMIN) {
1508 if (size[1] < data->ymin)
1509 size[1] = data->ymin;
1511 if (data->flag & LIMIT_YMAX) {
1512 if (size[1] > data->ymax)
1513 size[1] = data->ymax;
1515 if (data->flag & LIMIT_ZMIN) {
1516 if (size[2] < data->zmin)
1517 size[2] = data->zmin;
1519 if (data->flag & LIMIT_ZMAX) {
1520 if (size[2] > data->zmax)
1521 size[2] = data->zmax;
1525 VecMulf(cob->matrix[0], size[0]/obsize[0]);
1527 VecMulf(cob->matrix[1], size[1]/obsize[1]);
1529 VecMulf(cob->matrix[2], size[2]/obsize[2]);
1532 static bConstraintTypeInfo CTI_SIZELIMIT = {
1533 CONSTRAINT_TYPE_SIZELIMIT, /* type */
1534 sizeof(bSizeLimitConstraint), /* size */
1535 "Limit Scaling", /* name */
1536 "bSizeLimitConstraint", /* struct name */
1537 NULL, /* free data */
1538 NULL, /* relink data */
1539 NULL, /* copy data */
1540 NULL, /* new data */
1541 NULL, /* get constraint targets */
1542 NULL, /* flush constraint targets */
1543 NULL, /* get target matrix */
1544 sizelimit_evaluate /* evaluate */
1547 /* ----------- Copy Location ------------- */
1549 static void loclike_new_data (void *cdata)
1551 bLocateLikeConstraint *data= (bLocateLikeConstraint *)cdata;
1553 data->flag = LOCLIKE_X|LOCLIKE_Y|LOCLIKE_Z;
1556 static int loclike_get_tars (bConstraint *con, ListBase *list)
1559 bLocateLikeConstraint *data= con->data;
1560 bConstraintTarget *ct;
1562 /* standard target-getting macro for single-target constraints */
1563 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list)
1571 static void loclike_flush_tars (bConstraint *con, ListBase *list, short nocopy)
1574 bLocateLikeConstraint *data= con->data;
1575 bConstraintTarget *ct= list->first;
1577 /* the following macro is used for all standard single-target constraints */
1578 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, nocopy)
1582 static void loclike_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
1584 bLocateLikeConstraint *data= con->data;
1585 bConstraintTarget *ct= targets->first;
1587 if (VALID_CONS_TARGET(ct)) {
1588 float offset[3] = {0.0f, 0.0f, 0.0f};
1590 if (data->flag & LOCLIKE_OFFSET)
1591 VECCOPY(offset, cob->matrix[3]);
1593 if (data->flag & LOCLIKE_X) {
1594 cob->matrix[3][0] = ct->matrix[3][0];
1596 if (data->flag & LOCLIKE_X_INVERT) cob->matrix[3][0] *= -1;
1597 cob->matrix[3][0] += offset[0];
1599 if (data->flag & LOCLIKE_Y) {
1600 cob->matrix[3][1] = ct->matrix[3][1];
1602 if (data->flag & LOCLIKE_Y_INVERT) cob->matrix[3][1] *= -1;
1603 cob->matrix[3][1] += offset[1];
1605 if (data->flag & LOCLIKE_Z) {
1606 cob->matrix[3][2] = ct->matrix[3][2];
1608 if (data->flag & LOCLIKE_Z_INVERT) cob->matrix[3][2] *= -1;
1609 cob->matrix[3][2] += offset[2];
1614 static bConstraintTypeInfo CTI_LOCLIKE = {
1615 CONSTRAINT_TYPE_LOCLIKE, /* type */
1616 sizeof(bLocateLikeConstraint), /* size */
1617 "Copy Location", /* name */
1618 "bLocateLikeConstraint", /* struct name */
1619 NULL, /* free data */
1620 NULL, /* relink data */
1621 NULL, /* copy data */
1622 loclike_new_data, /* new data */
1623 loclike_get_tars, /* get constraint targets */
1624 loclike_flush_tars, /* flush constraint targets */
1625 default_get_tarmat, /* get target matrix */
1626 loclike_evaluate /* evaluate */
1629 /* ----------- Copy Rotation ------------- */
1631 static void rotlike_new_data (void *cdata)
1633 bRotateLikeConstraint *data= (bRotateLikeConstraint *)cdata;
1635 data->flag = ROTLIKE_X|ROTLIKE_Y|ROTLIKE_Z;
1638 static int rotlike_get_tars (bConstraint *con, ListBase *list)
1641 bRotateLikeConstraint *data= con->data;
1642 bConstraintTarget *ct;
1644 /* standard target-getting macro for single-target constraints */
1645 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list)
1653 static void rotlike_flush_tars (bConstraint *con, ListBase *list, short nocopy)
1656 bRotateLikeConstraint *data= con->data;
1657 bConstraintTarget *ct= list->first;
1659 /* the following macro is used for all standard single-target constraints */
1660 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, nocopy)
1664 static void rotlike_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
1666 bRotateLikeConstraint *data= con->data;
1667 bConstraintTarget *ct= targets->first;
1669 if (VALID_CONS_TARGET(ct)) {
1671 float eul[3], obeul[3];
1674 VECCOPY(loc, cob->matrix[3]);
1675 Mat4ToSize(cob->matrix, size);
1677 Mat4ToEul(ct->matrix, eul);
1678 Mat4ToEul(cob->matrix, obeul);
1680 if ((data->flag & ROTLIKE_X)==0)
1683 if (data->flag & ROTLIKE_OFFSET)
1684 euler_rot(eul, obeul[0], 'x');
1686 if (data->flag & ROTLIKE_X_INVERT)
1690 if ((data->flag & ROTLIKE_Y)==0)
1693 if (data->flag & ROTLIKE_OFFSET)
1694 euler_rot(eul, obeul[1], 'y');
1696 if (data->flag & ROTLIKE_Y_INVERT)
1700 if ((data->flag & ROTLIKE_Z)==0)
1703 if (data->flag & ROTLIKE_OFFSET)
1704 euler_rot(eul, obeul[2], 'z');
1706 if (data->flag & ROTLIKE_Z_INVERT)
1710 compatible_eul(eul, obeul);
1711 LocEulSizeToMat4(cob->matrix, loc, eul, size);
1715 static bConstraintTypeInfo CTI_ROTLIKE = {
1716 CONSTRAINT_TYPE_ROTLIKE, /* type */
1717 sizeof(bRotateLikeConstraint), /* size */
1718 "Copy Rotation", /* name */
1719 "bRotateLikeConstraint", /* struct name */
1720 NULL, /* free data */
1721 NULL, /* relink data */
1722 NULL, /* copy data */
1723 rotlike_new_data, /* new data */
1724 rotlike_get_tars, /* get constraint targets */
1725 rotlike_flush_tars, /* flush constraint targets */
1726 default_get_tarmat, /* get target matrix */
1727 rotlike_evaluate /* evaluate */
1730 /* ---------- Copy Scaling ---------- */
1732 static void sizelike_new_data (void *cdata)
1734 bSizeLikeConstraint *data= (bSizeLikeConstraint *)cdata;
1736 data->flag = SIZELIKE_X|SIZELIKE_Y|SIZELIKE_Z;
1739 static int sizelike_get_tars (bConstraint *con, ListBase *list)
1742 bSizeLikeConstraint *data= con->data;
1743 bConstraintTarget *ct;
1745 /* standard target-getting macro for single-target constraints */
1746 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list)
1754 static void sizelike_flush_tars (bConstraint *con, ListBase *list, short nocopy)
1757 bSizeLikeConstraint *data= con->data;
1758 bConstraintTarget *ct= list->first;
1760 /* the following macro is used for all standard single-target constraints */
1761 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, nocopy)
1765 static void sizelike_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
1767 bSizeLikeConstraint *data= con->data;
1768 bConstraintTarget *ct= targets->first;
1770 if (VALID_CONS_TARGET(ct)) {
1771 float obsize[3], size[3];
1773 Mat4ToSize(ct->matrix, size);
1774 Mat4ToSize(cob->matrix, obsize);
1776 if ((data->flag & SIZELIKE_X) && (obsize[0] != 0)) {
1777 if (data->flag & SIZELIKE_OFFSET) {
1778 size[0] += (obsize[0] - 1.0f);
1779 VecMulf(cob->matrix[0], size[0] / obsize[0]);
1782 VecMulf(cob->matrix[0], size[0] / obsize[0]);
1784 if ((data->flag & SIZELIKE_Y) && (obsize[1] != 0)) {
1785 if (data->flag & SIZELIKE_OFFSET) {
1786 size[1] += (obsize[1] - 1.0f);
1787 VecMulf(cob->matrix[1], size[1] / obsize[1]);
1790 VecMulf(cob->matrix[1], size[1] / obsize[1]);
1792 if ((data->flag & SIZELIKE_Z) && (obsize[2] != 0)) {
1793 if (data->flag & SIZELIKE_OFFSET) {
1794 size[2] += (obsize[2] - 1.0f);
1795 VecMulf(cob->matrix[2], size[2] / obsize[2]);
1798 VecMulf(cob->matrix[2], size[2] / obsize[2]);
1803 static bConstraintTypeInfo CTI_SIZELIKE = {
1804 CONSTRAINT_TYPE_SIZELIKE, /* type */
1805 sizeof(bSizeLikeConstraint), /* size */
1806 "Copy Scale", /* name */
1807 "bSizeLikeConstraint", /* struct name */
1808 NULL, /* free data */
1809 NULL, /* relink data */
1810 NULL, /* copy data */
1811 sizelike_new_data, /* new data */
1812 sizelike_get_tars, /* get constraint targets */
1813 sizelike_flush_tars, /* flush constraint targets */
1814 default_get_tarmat, /* get target matrix */
1815 sizelike_evaluate /* evaluate */
1819 /* ----------- Python Constraint -------------- */
1821 static void pycon_free (bConstraint *con)
1823 bPythonConstraint *data= con->data;
1826 IDP_FreeProperty(data->prop);
1827 MEM_freeN(data->prop);
1829 /* multiple targets */
1830 BLI_freelistN(&data->targets);
1833 static void pycon_relink (bConstraint *con)
1835 bPythonConstraint *data= con->data;
1840 static void pycon_copy (bConstraint *con, bConstraint *srccon)
1842 bPythonConstraint *pycon = (bPythonConstraint *)con->data;
1843 bPythonConstraint *opycon = (bPythonConstraint *)srccon->data;
1845 pycon->prop = IDP_CopyProperty(opycon->prop);
1846 duplicatelist(&pycon->targets, &opycon->targets);
1849 static void pycon_new_data (void *cdata)
1851 bPythonConstraint *data= (bPythonConstraint *)cdata;
1853 /* everything should be set correctly by calloc, except for the prop->type constant.*/
1854 data->prop = MEM_callocN(sizeof(IDProperty), "PyConstraintProps");
1855 data->prop->type = IDP_GROUP;
1858 static int pycon_get_tars (bConstraint *con, ListBase *list)
1861 bPythonConstraint *data= con->data;
1863 list->first = data->targets.first;
1864 list->last = data->targets.last;
1866 return data->tarnum;
1872 /* Whether this approach is maintained remains to be seen (aligorith) */
1873 static void pycon_get_tarmat (bConstraint *con, bConstraintOb *cob, bConstraintTarget *ct, float ctime)
1875 bPythonConstraint *data= con->data;
1877 if (VALID_CONS_TARGET(ct)) {
1878 /* special exception for curves - depsgraph issues */
1879 if (ct->tar->type == OB_CURVE) {
1880 Curve *cu= ct->tar->data;
1882 /* this check is to make sure curve objects get updated on file load correctly.*/
1883 if (cu->path==NULL || cu->path->data==NULL) /* only happens on reload file, but violates depsgraph still... fix! */
1884 makeDispListCurveTypes(ct->tar, 0);
1887 /* firstly calculate the matrix the normal way, then let the py-function override
1888 * this matrix if it needs to do so
1890 constraint_target_to_mat4(ct->tar, ct->subtarget, ct->matrix, CONSTRAINT_SPACE_WORLD, ct->space, con->headtail);
1892 /* only execute target calculation if allowed */
1893 #ifndef DISABLE_PYTHON
1894 if (G.f & G_DOSCRIPTLINKS)
1895 BPY_pyconstraint_target(data, ct);
1899 Mat4One(ct->matrix);
1902 static void pycon_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
1904 #ifdef DISABLE_PYTHON
1907 bPythonConstraint *data= con->data;
1909 /* only evaluate in python if we're allowed to do so */
1910 if ((G.f & G_DOSCRIPTLINKS)==0) return;
1912 /* currently removed, until I this can be re-implemented for multiple targets */
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.
1918 BPY_pyconstraint_driver(data, cob, targets);
1921 /* Now, run the actual 'constraint' function, which should only access the matrices */
1922 BPY_pyconstraint_eval(data, cob, targets);
1923 #endif /* DISABLE_PYTHON */
1926 static bConstraintTypeInfo CTI_PYTHON = {
1927 CONSTRAINT_TYPE_PYTHON, /* type */
1928 sizeof(bPythonConstraint), /* size */
1929 "Script", /* name */
1930 "bPythonConstraint", /* struct name */
1931 pycon_free, /* free data */
1932 pycon_relink, /* relink data */
1933 pycon_copy, /* copy data */
1934 pycon_new_data, /* new data */
1935 pycon_get_tars, /* get constraint targets */
1936 NULL, /* flush constraint targets */
1937 pycon_get_tarmat, /* get target matrix */
1938 pycon_evaluate /* evaluate */
1941 /* -------- Action Constraint ----------- */
1943 static void actcon_relink (bConstraint *con)
1945 bActionConstraint *data= con->data;
1949 static void actcon_new_data (void *cdata)
1951 bActionConstraint *data= (bActionConstraint *)cdata;
1953 /* set type to 20 (Loc X), as 0 is Rot X for backwards compatability */
1957 static int actcon_get_tars (bConstraint *con, ListBase *list)
1960 bActionConstraint *data= con->data;
1961 bConstraintTarget *ct;
1963 /* standard target-getting macro for single-target constraints */
1964 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list)
1972 static void actcon_flush_tars (bConstraint *con, ListBase *list, short nocopy)
1975 bActionConstraint *data= con->data;
1976 bConstraintTarget *ct= list->first;
1978 /* the following macro is used for all standard single-target constraints */
1979 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, nocopy)
1983 static void actcon_get_tarmat (bConstraint *con, bConstraintOb *cob, bConstraintTarget *ct, float ctime)
1985 extern void chan_calc_mat(bPoseChannel *chan);
1986 bActionConstraint *data = con->data;
1988 if (VALID_CONS_TARGET(ct)) {
1989 float tempmat[4][4], vec[3];
1993 /* initialise return matrix */
1994 Mat4One(ct->matrix);
1996 /* get the transform matrix of the target */
1997 constraint_target_to_mat4(ct->tar, ct->subtarget, tempmat, CONSTRAINT_SPACE_WORLD, ct->space, con->headtail);
1999 /* determine where in transform range target is */
2000 /* data->type is mapped as follows for backwards compatability:
2001 * 00,01,02 - rotation (it used to be like this)
2002 * 10,11,12 - scaling
2003 * 20,21,22 - location
2005 if (data->type < 10) {
2006 /* extract rotation (is in whatever space target should be in) */
2007 Mat4ToEul(tempmat, vec);
2008 vec[0] *= (float)(180.0/M_PI);
2009 vec[1] *= (float)(180.0/M_PI);
2010 vec[2] *= (float)(180.0/M_PI);
2013 else if (data->type < 20) {
2014 /* extract scaling (is in whatever space target should be in) */
2015 Mat4ToSize(tempmat, vec);
2016 axis= data->type - 10;
2019 /* extract location */
2020 VECCOPY(vec, tempmat[3]);
2021 axis= data->type - 20;
2024 /* Target defines the animation */
2025 s = (vec[axis]-data->min) / (data->max-data->min);
2027 t = ( s * (data->end-data->start)) + data->start;
2029 /* Get the appropriate information from the action */
2030 if (cob->type == CONSTRAINT_OBTYPE_BONE) {
2032 bPoseChannel *pchan, *tchan;
2034 /* make a temporary pose and evaluate using that */
2035 pose = MEM_callocN(sizeof(bPose), "pose");
2038 tchan= verify_pose_channel(pose, pchan->name);
2039 extract_pose_from_action(pose, data->act, t);
2041 chan_calc_mat(tchan);
2043 Mat4CpyMat4(ct->matrix, tchan->chan_mat);
2048 else if (cob->type == CONSTRAINT_OBTYPE_OBJECT) {
2049 /* evaluate using workob */
2050 what_does_obaction(cob->ob, data->act, t);
2051 object_to_mat4(&workob, ct->matrix);
2054 /* behaviour undefined... */
2055 puts("Error: unknown owner type for Action Constraint");
2060 static void actcon_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
2062 bConstraintTarget *ct= targets->first;
2064 if (VALID_CONS_TARGET(ct)) {
2067 /* Nice and simple... we just need to multiply the matrices, as the get_target_matrix
2068 * function has already taken care of everything else.
2070 Mat4CpyMat4(temp, cob->matrix);
2071 Mat4MulMat4(cob->matrix, ct->matrix, temp);
2075 static bConstraintTypeInfo CTI_ACTION = {
2076 CONSTRAINT_TYPE_ACTION, /* type */
2077 sizeof(bActionConstraint), /* size */
2078 "Action", /* name */
2079 "bActionConstraint", /* struct name */
2080 NULL, /* free data */
2081 actcon_relink, /* relink data */
2082 NULL, /* copy data */
2083 actcon_new_data, /* new data */
2084 actcon_get_tars, /* get constraint targets */
2085 actcon_flush_tars, /* flush constraint targets */
2086 actcon_get_tarmat, /* get target matrix */
2087 actcon_evaluate /* evaluate */
2090 /* --------- Locked Track ---------- */
2092 static void locktrack_new_data (void *cdata)
2094 bLockTrackConstraint *data= (bLockTrackConstraint *)cdata;
2096 data->trackflag = TRACK_Y;
2097 data->lockflag = LOCK_Z;
2100 static int locktrack_get_tars (bConstraint *con, ListBase *list)
2103 bLockTrackConstraint *data= con->data;
2104 bConstraintTarget *ct;
2106 /* the following macro is used for all standard single-target constraints */
2107 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list)
2115 static void locktrack_flush_tars (bConstraint *con, ListBase *list, short nocopy)
2118 bLockTrackConstraint *data= con->data;
2119 bConstraintTarget *ct= list->first;
2121 /* the following macro is used for all standard single-target constraints */
2122 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, nocopy)
2126 static void locktrack_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
2128 bLockTrackConstraint *data= con->data;
2129 bConstraintTarget *ct= targets->first;
2131 if (VALID_CONS_TARGET(ct)) {
2132 float vec[3],vec2[3];
2139 /* Vector object -> target */
2140 VecSubf(vec, ct->matrix[3], cob->matrix[3]);
2141 switch (data->lockflag){
2142 case LOCK_X: /* LOCK X */
2144 switch (data->trackflag) {
2145 case TRACK_Y: /* LOCK X TRACK Y */
2147 /* Projection of Vector on the plane */
2148 Projf(vec2, vec, cob->matrix[0]);
2149 VecSubf(totmat[1], vec, vec2);
2150 Normalize(totmat[1]);
2152 /* the x axis is fixed */
2153 totmat[0][0] = cob->matrix[0][0];
2154 totmat[0][1] = cob->matrix[0][1];
2155 totmat[0][2] = cob->matrix[0][2];
2156 Normalize(totmat[0]);
2158 /* the z axis gets mapped onto a third orthogonal vector */
2159 Crossf(totmat[2], totmat[0], totmat[1]);
2162 case TRACK_Z: /* LOCK X TRACK Z */
2164 /* Projection of Vector on the plane */
2165 Projf(vec2, vec, cob->matrix[0]);
2166 VecSubf(totmat[2], vec, vec2);
2167 Normalize(totmat[2]);
2169 /* the x axis is fixed */
2170 totmat[0][0] = cob->matrix[0][0];
2171 totmat[0][1] = cob->matrix[0][1];
2172 totmat[0][2] = cob->matrix[0][2];
2173 Normalize(totmat[0]);
2175 /* the z axis gets mapped onto a third orthogonal vector */
2176 Crossf(totmat[1], totmat[2], totmat[0]);
2179 case TRACK_nY: /* LOCK X TRACK -Y */
2181 /* Projection of Vector on the plane */
2182 Projf(vec2, vec, cob->matrix[0]);
2183 VecSubf(totmat[1], vec, vec2);
2184 Normalize(totmat[1]);
2185 VecMulf(totmat[1],-1);
2187 /* the x axis is fixed */
2188 totmat[0][0] = cob->matrix[0][0];
2189 totmat[0][1] = cob->matrix[0][1];
2190 totmat[0][2] = cob->matrix[0][2];
2191 Normalize(totmat[0]);
2193 /* the z axis gets mapped onto a third orthogonal vector */
2194 Crossf(totmat[2], totmat[0], totmat[1]);
2197 case TRACK_nZ: /* LOCK X TRACK -Z */
2199 /* Projection of Vector on the plane */
2200 Projf(vec2, vec, cob->matrix[0]);
2201 VecSubf(totmat[2], vec, vec2);
2202 Normalize(totmat[2]);
2203 VecMulf(totmat[2],-1);
2205 /* the x axis is fixed */
2206 totmat[0][0] = cob->matrix[0][0];
2207 totmat[0][1] = cob->matrix[0][1];
2208 totmat[0][2] = cob->matrix[0][2];
2209 Normalize(totmat[0]);
2211 /* the z axis gets mapped onto a third orthogonal vector */
2212 Crossf(totmat[1], totmat[2], totmat[0]);
2217 totmat[0][0] = 1;totmat[0][1] = 0;totmat[0][2] = 0;
2218 totmat[1][0] = 0;totmat[1][1] = 1;totmat[1][2] = 0;
2219 totmat[2][0] = 0;totmat[2][1] = 0;totmat[2][2] = 1;
2225 case LOCK_Y: /* LOCK Y */
2227 switch (data->trackflag) {
2228 case TRACK_X: /* LOCK Y TRACK X */
2230 /* Projection of Vector on the plane */
2231 Projf(vec2, vec, cob->matrix[1]);
2232 VecSubf(totmat[0], vec, vec2);
2233 Normalize(totmat[0]);
2235 /* the y axis is fixed */
2236 totmat[1][0] = cob->matrix[1][0];
2237 totmat[1][1] = cob->matrix[1][1];
2238 totmat[1][2] = cob->matrix[1][2];
2239 Normalize(totmat[1]);
2241 /* the z axis gets mapped onto a third orthogonal vector */
2242 Crossf(totmat[2], totmat[0], totmat[1]);
2245 case TRACK_Z: /* LOCK Y TRACK Z */
2247 /* Projection of Vector on the plane */
2248 Projf(vec2, vec, cob->matrix[1]);
2249 VecSubf(totmat[2], vec, vec2);
2250 Normalize(totmat[2]);
2252 /* the y axis is fixed */
2253 totmat[1][0] = cob->matrix[1][0];
2254 totmat[1][1] = cob->matrix[1][1];
2255 totmat[1][2] = cob->matrix[1][2];
2256 Normalize(totmat[1]);
2258 /* the z axis gets mapped onto a third orthogonal vector */
2259 Crossf(totmat[0], totmat[1], totmat[2]);
2262 case TRACK_nX: /* LOCK Y TRACK -X */
2264 /* Projection of Vector on the plane */
2265 Projf(vec2, vec, cob->matrix[1]);
2266 VecSubf(totmat[0], vec, vec2);
2267 Normalize(totmat[0]);
2268 VecMulf(totmat[0],-1);
2270 /* the y axis is fixed */
2271 totmat[1][0] = cob->matrix[1][0];
2272 totmat[1][1] = cob->matrix[1][1];
2273 totmat[1][2] = cob->matrix[1][2];
2274 Normalize(totmat[1]);
2276 /* the z axis gets mapped onto a third orthogonal vector */
2277 Crossf(totmat[2], totmat[0], totmat[1]);
2280 case TRACK_nZ: /* LOCK Y TRACK -Z */
2282 /* Projection of Vector on the plane */
2283 Projf(vec2, vec, cob->matrix[1]);
2284 VecSubf(totmat[2], vec, vec2);
2285 Normalize(totmat[2]);
2286 VecMulf(totmat[2],-1);
2288 /* the y axis is fixed */
2289 totmat[1][0] = cob->matrix[1][0];
2290 totmat[1][1] = cob->matrix[1][1];
2291 totmat[1][2] = cob->matrix[1][2];
2292 Normalize(totmat[1]);
2294 /* the z axis gets mapped onto a third orthogonal vector */
2295 Crossf(totmat[0], totmat[1], totmat[2]);
2300 totmat[0][0] = 1;totmat[0][1] = 0;totmat[0][2] = 0;
2301 totmat[1][0] = 0;totmat[1][1] = 1;totmat[1][2] = 0;
2302 totmat[2][0] = 0;totmat[2][1] = 0;totmat[2][2] = 1;
2308 case LOCK_Z: /* LOCK Z */
2310 switch (data->trackflag) {
2311 case TRACK_X: /* LOCK Z TRACK X */
2313 /* Projection of Vector on the plane */
2314 Projf(vec2, vec, cob->matrix[2]);
2315 VecSubf(totmat[0], vec, vec2);
2316 Normalize(totmat[0]);
2318 /* the z axis is fixed */
2319 totmat[2][0] = cob->matrix[2][0];
2320 totmat[2][1] = cob->matrix[2][1];
2321 totmat[2][2] = cob->matrix[2][2];
2322 Normalize(totmat[2]);
2324 /* the x axis gets mapped onto a third orthogonal vector */
2325 Crossf(totmat[1], totmat[2], totmat[0]);
2328 case TRACK_Y: /* LOCK Z TRACK Y */
2330 /* Projection of Vector on the plane */
2331 Projf(vec2, vec, cob->matrix[2]);
2332 VecSubf(totmat[1], vec, vec2);
2333 Normalize(totmat[1]);
2335 /* the z axis is fixed */
2336 totmat[2][0] = cob->matrix[2][0];
2337 totmat[2][1] = cob->matrix[2][1];
2338 totmat[2][2] = cob->matrix[2][2];
2339 Normalize(totmat[2]);
2341 /* the x axis gets mapped onto a third orthogonal vector */
2342 Crossf(totmat[0], totmat[1], totmat[2]);
2345 case TRACK_nX: /* LOCK Z TRACK -X */
2347 /* Projection of Vector on the plane */
2348 Projf(vec2, vec, cob->matrix[2]);
2349 VecSubf(totmat[0], vec, vec2);
2350 Normalize(totmat[0]);
2351 VecMulf(totmat[0],-1);
2353 /* the z axis is fixed */
2354 totmat[2][0] = cob->matrix[2][0];
2355 totmat[2][1] = cob->matrix[2][1];
2356 totmat[2][2] = cob->matrix[2][2];
2357 Normalize(totmat[2]);
2359 /* the x axis gets mapped onto a third orthogonal vector */
2360 Crossf(totmat[1], totmat[2], totmat[0]);
2363 case TRACK_nY: /* LOCK Z TRACK -Y */
2365 /* Projection of Vector on the plane */
2366 Projf(vec2, vec, cob->matrix[2]);
2367 VecSubf(totmat[1], vec, vec2);
2368 Normalize(totmat[1]);
2369 VecMulf(totmat[1],-1);
2371 /* the z axis is fixed */
2372 totmat[2][0] = cob->matrix[2][0];
2373 totmat[2][1] = cob->matrix[2][1];
2374 totmat[2][2] = cob->matrix[2][2];
2375 Normalize(totmat[2]);
2377 /* the x axis gets mapped onto a third orthogonal vector */
2378 Crossf(totmat[0], totmat[1], totmat[2]);
2383 totmat[0][0] = 1;totmat[0][1] = 0;totmat[0][2] = 0;
2384 totmat[1][0] = 0;totmat[1][1] = 1;totmat[1][2] = 0;
2385 totmat[2][0] = 0;totmat[2][1] = 0;totmat[2][2] = 1;
2393 totmat[0][0] = 1;totmat[0][1] = 0;totmat[0][2] = 0;
2394 totmat[1][0] = 0;totmat[1][1] = 1;totmat[1][2] = 0;
2395 totmat[2][0] = 0;totmat[2][1] = 0;totmat[2][2] = 1;
2399 /* Block to keep matrix heading */
2400 tmpmat[0][0] = cob->matrix[0][0];tmpmat[0][1] = cob->matrix[0][1];tmpmat[0][2] = cob->matrix[0][2];
2401 tmpmat[1][0] = cob->matrix[1][0];tmpmat[1][1] = cob->matrix[1][1];tmpmat[1][2] = cob->matrix[1][2];
2402 tmpmat[2][0] = cob->matrix[2][0];tmpmat[2][1] = cob->matrix[2][1];tmpmat[2][2] = cob->matrix[2][2];
2403 Normalize(tmpmat[0]);
2404 Normalize(tmpmat[1]);
2405 Normalize(tmpmat[2]);
2406 Mat3Inv(invmat, tmpmat);
2407 Mat3MulMat3(tmpmat, totmat, invmat);
2408 totmat[0][0] = tmpmat[0][0];totmat[0][1] = tmpmat[0][1];totmat[0][2] = tmpmat[0][2];
2409 totmat[1][0] = tmpmat[1][0];totmat[1][1] = tmpmat[1][1];totmat[1][2] = tmpmat[1][2];
2410 totmat[2][0] = tmpmat[2][0];totmat[2][1] = tmpmat[2][1];totmat[2][2] = tmpmat[2][2];
2412 Mat4CpyMat4(tmat, cob->matrix);
2414 mdet = Det3x3( totmat[0][0],totmat[0][1],totmat[0][2],
2415 totmat[1][0],totmat[1][1],totmat[1][2],
2416 totmat[2][0],totmat[2][1],totmat[2][2]);
2418 totmat[0][0] = 1;totmat[0][1] = 0;totmat[0][2] = 0;
2419 totmat[1][0] = 0;totmat[1][1] = 1;totmat[1][2] = 0;
2420 totmat[2][0] = 0;totmat[2][1] = 0;totmat[2][2] = 1;
2423 /* apply out transformaton to the object */
2424 Mat4MulMat34(cob->matrix, totmat, tmat);
2428 static bConstraintTypeInfo CTI_LOCKTRACK = {
2429 CONSTRAINT_TYPE_LOCKTRACK, /* type */
2430 sizeof(bLockTrackConstraint), /* size */
2431 "Locked Track", /* name */
2432 "bLockTrackConstraint", /* struct name */
2433 NULL, /* free data */
2434 NULL, /* relink data */
2435 NULL, /* copy data */
2436 locktrack_new_data, /* new data */
2437 locktrack_get_tars, /* get constraint targets */
2438 locktrack_flush_tars, /* flush constraint targets */
2439 default_get_tarmat, /* get target matrix */
2440 locktrack_evaluate /* evaluate */
2443 /* ---------- Limit Distance Constraint ----------- */
2445 static void distlimit_new_data (void *cdata)
2447 bDistLimitConstraint *data= (bDistLimitConstraint *)cdata;
2452 static int distlimit_get_tars (bConstraint *con, ListBase *list)
2455 bDistLimitConstraint *data= con->data;
2456 bConstraintTarget *ct;
2458 /* standard target-getting macro for single-target constraints */
2459 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list)
2467 static void distlimit_flush_tars (bConstraint *con, ListBase *list, short nocopy)
2470 bDistLimitConstraint *data= con->data;
2471 bConstraintTarget *ct= list->first;
2473 /* the following macro is used for all standard single-target constraints */
2474 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, nocopy)
2478 static void distlimit_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
2480 bDistLimitConstraint *data= con->data;
2481 bConstraintTarget *ct= targets->first;
2483 /* only evaluate if there is a target */
2484 if (VALID_CONS_TARGET(ct)) {
2485 float dvec[3], dist=0.0f, sfac=1.0f;
2486 short clamp_surf= 0;
2488 /* calculate our current distance from the target */
2489 dist= VecLenf(cob->matrix[3], ct->matrix[3]);
2491 /* set distance (flag is only set when user demands it) */
2492 if (data->dist == 0)
2495 /* check if we're which way to clamp from, and calculate interpolation factor (if needed) */
2496 if (data->mode == LIMITDIST_OUTSIDE) {
2497 /* if inside, then move to surface */
2498 if (dist <= data->dist) {
2500 sfac= data->dist / dist;
2502 /* if soft-distance is enabled, start fading once owner is dist+softdist from the target */
2503 else if (data->flag & LIMITDIST_USESOFT) {
2504 if (dist <= (data->dist + data->soft)) {
2509 else if (data->mode == LIMITDIST_INSIDE) {
2510 /* if outside, then move to surface */
2511 if (dist >= data->dist) {
2513 sfac= data->dist / dist;
2515 /* if soft-distance is enabled, start fading once owner is dist-soft from the target */
2516 else if (data->flag & LIMITDIST_USESOFT) {
2517 // FIXME: there's a problem with "jumping" when this kicks in
2518 if (dist >= (data->dist - data->soft)) {
2519 sfac = (float)( data->soft*(1.0 - exp(-(dist - data->dist)/data->soft)) + data->dist );
2527 if (IS_EQ(dist, data->dist)==0) {
2529 sfac= data->dist / dist;
2533 /* clamp to 'surface' (i.e. move owner so that dist == data->dist) */
2535 /* simply interpolate along line formed by target -> owner */
2536 VecLerpf(dvec, ct->matrix[3], cob->matrix[3], sfac);
2538 /* copy new vector onto owner */
2539 VECCOPY(cob->matrix[3], dvec);
2544 static bConstraintTypeInfo CTI_DISTLIMIT = {
2545 CONSTRAINT_TYPE_DISTLIMIT, /* type */
2546 sizeof(bDistLimitConstraint), /* size */
2547 "Limit Distance", /* name */
2548 "bDistLimitConstraint", /* struct name */
2549 NULL, /* free data */
2550 NULL, /* relink data */
2551 NULL, /* copy data */
2552 distlimit_new_data, /* new data */
2553 distlimit_get_tars, /* get constraint targets */
2554 distlimit_flush_tars, /* flush constraint targets */
2555 default_get_tarmat, /* get a target matrix */
2556 distlimit_evaluate /* evaluate */
2559 /* ---------- Stretch To ------------ */
2561 static void stretchto_new_data (void *cdata)
2563 bStretchToConstraint *data= (bStretchToConstraint *)cdata;
2567 data->orglength = 0.0;
2571 static int stretchto_get_tars (bConstraint *con, ListBase *list)
2574 bStretchToConstraint *data= con->data;
2575 bConstraintTarget *ct;
2577 /* standard target-getting macro for single-target constraints */
2578 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list)
2586 static void stretchto_flush_tars (bConstraint *con, ListBase *list, short nocopy)
2589 bStretchToConstraint *data= con->data;
2590 bConstraintTarget *ct= list->first;
2592 /* the following macro is used for all standard single-target constraints */
2593 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, nocopy)
2597 static void stretchto_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
2599 bStretchToConstraint *data= con->data;
2600 bConstraintTarget *ct= targets->first;
2602 /* only evaluate if there is a target */
2603 if (VALID_CONS_TARGET(ct)) {
2604 float size[3], scale[3], vec[3], xx[3], zz[3], orth[3];
2609 /* store scaling before destroying obmat */
2610 Mat4ToSize(cob->matrix, size);
2612 /* store X orientation before destroying obmat */
2613 xx[0] = cob->matrix[0][0];
2614 xx[1] = cob->matrix[0][1];
2615 xx[2] = cob->matrix[0][2];
2618 /* store Z orientation before destroying obmat */
2619 zz[0] = cob->matrix[2][0];
2620 zz[1] = cob->matrix[2][1];
2621 zz[2] = cob->matrix[2][2];
2624 VecSubf(vec, cob->matrix[3], ct->matrix[3]);
2629 dist = Normalize(vec);
2630 //dist = VecLenf( ob->obmat[3], targetmat[3]);
2632 /* data->orglength==0 occurs on first run, and after 'R' button is clicked */
2633 if (data->orglength == 0)
2634 data->orglength = dist;
2635 if (data->bulge == 0)
2638 scale[1] = dist/data->orglength;
2639 switch (data->volmode) {
2640 /* volume preserving scaling */
2642 scale[0] = 1.0f - (float)sqrt(data->bulge) + (float)sqrt(data->bulge*(data->orglength/dist));
2643 scale[2] = scale[0];
2646 scale[0] = 1.0f + data->bulge * (data->orglength /dist - 1);
2651 scale[2] = 1.0f + data->bulge * (data->orglength /dist - 1);
2653 /* don't care for volume */
2658 default: /* should not happen, but in case*/
2660 } /* switch (data->volmode) */
2662 /* Clear the object's rotation and scale */
2663 cob->matrix[0][0]=size[0]*scale[0];
2664 cob->matrix[0][1]=0;
2665 cob->matrix[0][2]=0;
2666 cob->matrix[1][0]=0;
2667 cob->matrix[1][1]=size[1]*scale[1];
2668 cob->matrix[1][2]=0;
2669 cob->matrix[2][0]=0;
2670 cob->matrix[2][1]=0;
2671 cob->matrix[2][2]=size[2]*scale[2];
2673 VecSubf(vec, cob->matrix[3], ct->matrix[3]);
2676 /* new Y aligns object target connection*/
2677 totmat[1][0] = -vec[0];
2678 totmat[1][1] = -vec[1];
2679 totmat[1][2] = -vec[2];
2680 switch (data->plane) {
2682 /* build new Z vector */
2683 /* othogonal to "new Y" "old X! plane */
2684 Crossf(orth, vec, xx);
2688 totmat[2][0] = orth[0];
2689 totmat[2][1] = orth[1];
2690 totmat[2][2] = orth[2];
2692 /* we decided to keep X plane*/
2693 Crossf(xx, orth, vec);
2695 totmat[0][0] = xx[0];
2696 totmat[0][1] = xx[1];
2697 totmat[0][2] = xx[2];
2700 /* build new X vector */
2701 /* othogonal to "new Y" "old Z! plane */
2702 Crossf(orth, vec, zz);
2706 totmat[0][0] = -orth[0];
2707 totmat[0][1] = -orth[1];
2708 totmat[0][2] = -orth[2];
2710 /* we decided to keep Z */
2711 Crossf(zz, orth, vec);
2713 totmat[2][0] = zz[0];
2714 totmat[2][1] = zz[1];
2715 totmat[2][2] = zz[2];
2717 } /* switch (data->plane) */
2719 Mat4CpyMat4(tmat, cob->matrix);
2720 Mat4MulMat34(cob->matrix, totmat, tmat);
2724 static bConstraintTypeInfo CTI_STRETCHTO = {
2725 CONSTRAINT_TYPE_STRETCHTO, /* type */
2726 sizeof(bStretchToConstraint), /* size */
2727 "Stretch To", /* name */
2728 "bStretchToConstraint", /* struct name */
2729 NULL, /* free data */
2730 NULL, /* relink data */
2731 NULL, /* copy data */
2732 stretchto_new_data, /* new data */
2733 stretchto_get_tars, /* get constraint targets */
2734 stretchto_flush_tars, /* flush constraint targets */
2735 default_get_tarmat, /* get target matrix */
2736 stretchto_evaluate /* evaluate */
2739 /* ---------- Floor ------------ */
2741 static void minmax_new_data (void *cdata)
2743 bMinMaxConstraint *data= (bMinMaxConstraint *)cdata;
2745 data->minmaxflag = TRACK_Z;
2746 data->offset = 0.0f;
2747 data->cache[0] = data->cache[1] = data->cache[2] = 0.0f;
2751 static int minmax_get_tars (bConstraint *con, ListBase *list)
2754 bMinMaxConstraint *data= con->data;
2755 bConstraintTarget *ct;
2757 /* standard target-getting macro for single-target constraints */
2758 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list)
2766 static void minmax_flush_tars (bConstraint *con, ListBase *list, short nocopy)
2769 bMinMaxConstraint *data= con->data;
2770 bConstraintTarget *ct= list->first;
2772 /* the following macro is used for all standard single-target constraints */
2773 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, nocopy)
2777 static void minmax_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *targets)
2779 bMinMaxConstraint *data= con->data;
2780 bConstraintTarget *ct= targets->first;
2782 /* only evaluate if there is a target */
2783 if (VALID_CONS_TARGET(ct)) {
2784 float obmat[4][4], imat[4][4], tarmat[4][4], tmat[4][4];
2788 Mat4CpyMat4(obmat, cob->matrix);
2789 Mat4CpyMat4(tarmat, ct->matrix);
2791 if (data->flag & MINMAX_USEROT) {
2792 /* take rotation of target into account by doing the transaction in target's localspace */
2793 Mat4Invert(imat, tarmat);
2794 Mat4MulMat4(tmat, obmat, imat);
2795 Mat4CpyMat4(obmat, tmat);
2799 switch (data->minmaxflag) {
2801 val1 = tarmat[3][2];
2802 val2 = obmat[3][2]-data->offset;
2806 val1 = tarmat[3][1];
2807 val2 = obmat[3][1]-data->offset;
2811 val1 = tarmat[3][0];
2812 val2 = obmat[3][0]-data->offset;
2816 val2 = tarmat[3][2];
2817 val1 = obmat[3][2]-data->offset;
2821 val2 = tarmat[3][1];
2822 val1 = obmat[3][1]-data->offset;
2826 val2 = tarmat[3][0];
2827 val1 = obmat[3][0]-data->offset;
2835 obmat[3][index] = tarmat[3][index] + data->offset;
2836 if (data->flag & MINMAX_STICKY) {
2837 if (data->flag & MINMAX_STUCK) {
2838 VECCOPY(obmat[3], data->cache);