Blender V5.0
constraint.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9/* Allow using deprecated functionality for .blend file I/O. */
10#define DNA_DEPRECATED_ALLOW
11
12#include <algorithm>
13#include <cfloat>
14#include <cmath>
15#include <cstddef>
16#include <cstdio>
17#include <cstring>
18
19#include "MEM_guardedalloc.h"
20
21#include "BLI_kdopbvh.hh"
22#include "BLI_listbase.h"
23#include "BLI_math_matrix.h"
24#include "BLI_math_rotation.h"
25#include "BLI_math_vector.h"
26#include "BLI_math_vector.hh"
27#include "BLI_string.h"
28#include "BLI_string_utf8.h"
29#include "BLI_string_utils.hh"
30#include "BLI_utildefines.h"
31#include "BLT_translation.hh"
32
33#include "DNA_action_types.h"
34#include "DNA_armature_types.h"
35#include "DNA_cachefile_types.h"
37#include "DNA_curve_types.h"
38#include "DNA_mesh_types.h"
39#include "DNA_meshdata_types.h"
40#include "DNA_modifier_types.h"
41#include "DNA_object_types.h"
42#include "DNA_screen_types.h"
43
44#include "DNA_lattice_types.h"
45#include "DNA_movieclip_types.h"
46#include "DNA_scene_types.h"
47#include "DNA_tracking_types.h"
48
49#include "BKE_action.hh"
50#include "BKE_anim_path.h"
51#include "BKE_animsys.h"
52#include "BKE_armature.hh"
53#include "BKE_bvhutils.hh"
54#include "BKE_cachefile.hh"
55#include "BKE_camera.h"
56#include "BKE_constraint.h"
57#include "BKE_curve.hh"
58#include "BKE_deform.hh"
59#include "BKE_displist.h"
60#include "BKE_editmesh.hh"
61#include "BKE_fcurve_driver.h"
63#include "BKE_global.hh"
64#include "BKE_idprop.hh"
65#include "BKE_lib_id.hh"
66#include "BKE_lib_query.hh"
67#include "BKE_library.hh"
68#include "BKE_mesh_runtime.hh"
69#include "BKE_movieclip.h"
70#include "BKE_object.hh"
71#include "BKE_object_types.hh"
72#include "BKE_scene.hh"
73#include "BKE_shrinkwrap.hh"
74#include "BKE_tracking.h"
75
76#include "BIK_api.h"
77
78#include "RNA_prototypes.hh"
79
80#include "DEG_depsgraph.hh"
82
83#include "BLO_read_write.hh"
84
85#include "CLG_log.h"
86
87#ifdef WITH_PYTHON
88# include "BPY_extern.hh"
89#endif
90
91#ifdef WITH_ALEMBIC
92# include "ABC_alembic.h"
93#endif
94
95#ifdef WITH_USD
96# include "usd.hh"
97#endif
98
99/* ---------------------------------------------------------------------------- */
100/* Useful macros for testing various common flag combinations */
101
102/* Constraint Target Macros */
103#define VALID_CONS_TARGET(ct) ((ct) && (ct->tar))
104
105static CLG_LogRef LOG = {"object.constraint"};
106
107/* ************************ Constraints - General Utilities *************************** */
108/* These functions here don't act on any specific constraints, and are therefore should/will
109 * not require any of the special function-pointers afforded by the relevant constraint
110 * type-info structs.
111 */
112
113static void damptrack_do_transform(float matrix[4][4], const float tarvec[3], int track_axis);
114
116 bPoseChannel *pchan,
117 bConstraint *con,
118 Object **r_orig_ob);
120
121/* -------------- Naming -------------- */
122
124{
125 BLI_uniquename(list, con, DATA_("Const"), '.', offsetof(bConstraint, name), sizeof(con->name));
126}
127
128/* ----------------- Evaluation Loop Preparation --------------- */
129
131 Depsgraph *depsgraph, Scene *scene, Object *ob, void *subdata, short datatype)
132{
133 bConstraintOb *cob;
134
135 /* create regardless of whether we have any data! */
136 cob = MEM_callocN<bConstraintOb>("bConstraintOb");
137
138 /* NOTE(@ton): For system time, part of de-globalization, code nicer later with local time. */
139 cob->scene = scene;
140 cob->depsgraph = depsgraph;
141
142 /* based on type of available data */
143 switch (datatype) {
145 /* disregard subdata... calloc should set other values right */
146 if (ob) {
147 cob->ob = ob;
148 cob->type = datatype;
149
150 if (cob->ob->rotmode > 0) {
151 /* Should be some kind of Euler order, so use it */
152 /* NOTE: Versions <= 2.76 assumed that "default" order
153 * would always get used, so we may seem some rig
154 * breakage as a result. However, this change here
155 * is needed to fix #46599
156 */
157 cob->rotOrder = ob->rotmode;
158 }
159 else {
160 /* Quaternion/Axis-Angle, so Eulers should just use default order. */
162 }
163 copy_m4_m4(cob->matrix, ob->object_to_world().ptr());
164 }
165 else {
166 unit_m4(cob->matrix);
167 }
168
169 copy_m4_m4(cob->startmat, cob->matrix);
170 break;
171 }
173 /* only set if we have valid bone, otherwise default */
174 if (ob && subdata) {
175 cob->ob = ob;
176 cob->pchan = (bPoseChannel *)subdata;
177 cob->type = datatype;
178
179 if (cob->pchan->rotmode > 0) {
180 /* should be some type of Euler order */
181 cob->rotOrder = cob->pchan->rotmode;
182 }
183 else {
184 /* Quaternion, so eulers should just use default order */
186 }
187
188 /* matrix in world-space */
189 mul_m4_m4m4(cob->matrix, ob->object_to_world().ptr(), cob->pchan->pose_mat);
190 }
191 else {
192 unit_m4(cob->matrix);
193 }
194
195 copy_m4_m4(cob->startmat, cob->matrix);
196 break;
197 }
198 default: /* other types not yet handled */
199 unit_m4(cob->matrix);
200 unit_m4(cob->startmat);
201 break;
202 }
203
204 return cob;
205}
206
208{
209 float delta[4][4], imat[4][4];
210
211 /* prevent crashes */
212 if (cob == nullptr) {
213 return;
214 }
215
216 /* calculate delta of constraints evaluation */
217 invert_m4_m4(imat, cob->startmat);
218 /* XXX This would seem to be in wrong order. However, it does not work in 'right' order - would
219 * be nice to understand why pre-multiply is needed here instead of usual post-multiply?
220 * In any case, we *do not get a delta* here (e.g. startmat & matrix having same location,
221 * still gives a 'delta' with non-null translation component :/ ). */
222 mul_m4_m4m4(delta, cob->matrix, imat);
223
224 /* copy matrices back to source */
225 switch (cob->type) {
227 /* cob->ob might not exist! */
228 if (cob->ob) {
229 /* copy new ob-matrix back to owner */
230 copy_m4_m4(cob->ob->runtime->object_to_world.ptr(), cob->matrix);
231
232 /* copy inverse of delta back to owner */
233 invert_m4_m4(cob->ob->constinv, delta);
234 }
235 break;
236 }
238 /* cob->ob or cob->pchan might not exist */
239 if (cob->ob && cob->pchan) {
240 /* copy new pose-matrix back to owner */
241 mul_m4_m4m4(cob->pchan->pose_mat, cob->ob->world_to_object().ptr(), cob->matrix);
242
243 /* copy inverse of delta back to owner */
244 invert_m4_m4(cob->pchan->constinv, delta);
245 }
246 break;
247 }
248 }
249
250 /* Free temporary struct. */
251 MEM_freeN(cob);
252}
253
254/* -------------- Space-Conversion API -------------- */
255
257 bPoseChannel *pchan,
258 bConstraintOb *cob,
259 float mat[4][4],
260 short from,
261 short to,
262 const bool keep_scale)
263{
264 float diff_mat[4][4];
265 float imat[4][4];
266
267 /* Prevent crashes in these unlikely events. */
268 if (ob == nullptr || mat == nullptr) {
269 return;
270 }
271 /* optimize trick - check if need to do anything */
272 if (from == to) {
273 return;
274 }
275
276 /* are we dealing with pose-channels or objects */
277 if (pchan) {
278 /* pose channels */
279 switch (from) {
280 case CONSTRAINT_SPACE_WORLD: /* ---------- FROM WORLDSPACE ---------- */
281 {
282 if (to == CONSTRAINT_SPACE_CUSTOM) {
283 /* World to custom. */
284 BLI_assert(cob);
286 mul_m4_m4m4(mat, imat, mat);
287 }
288 else {
289 /* World to pose. */
290 invert_m4_m4(imat, ob->object_to_world().ptr());
291 mul_m4_m4m4(mat, imat, mat);
292
293 /* Use pose-space as stepping stone for other spaces. */
294 if (ELEM(to,
298 {
299 /* Call self with slightly different values. */
301 ob, pchan, cob, mat, CONSTRAINT_SPACE_POSE, to, keep_scale);
302 }
303 }
304 break;
305 }
306 case CONSTRAINT_SPACE_POSE: /* ---------- FROM POSESPACE ---------- */
307 {
308 /* pose to local */
309 if (to == CONSTRAINT_SPACE_LOCAL) {
310 if (pchan->bone) {
311 BKE_armature_mat_pose_to_bone(pchan, mat, mat);
312 }
313 }
314 /* pose to owner local */
315 else if (to == CONSTRAINT_SPACE_OWNLOCAL) {
316 /* pose to local */
317 if (pchan->bone) {
318 BKE_armature_mat_pose_to_bone(pchan, mat, mat);
319 }
320
321 /* local to owner local (recursive) */
323 ob, pchan, cob, mat, CONSTRAINT_SPACE_LOCAL, to, keep_scale);
324 }
325 /* pose to local with parent */
326 else if (to == CONSTRAINT_SPACE_PARLOCAL) {
327 if (pchan->bone) {
328 invert_m4_m4(imat, pchan->bone->arm_mat);
329 mul_m4_m4m4(mat, imat, mat);
330 }
331 }
332 else {
333 /* Pose to world. */
334 mul_m4_m4m4(mat, ob->object_to_world().ptr(), mat);
335 /* Use world-space as stepping stone for other spaces. */
336 if (to != CONSTRAINT_SPACE_WORLD) {
337 /* Call self with slightly different values. */
339 ob, pchan, cob, mat, CONSTRAINT_SPACE_WORLD, to, keep_scale);
340 }
341 }
342 break;
343 }
344 case CONSTRAINT_SPACE_LOCAL: /* ------------ FROM LOCALSPACE --------- */
345 {
346 /* local to owner local */
347 if (to == CONSTRAINT_SPACE_OWNLOCAL) {
348 if (pchan->bone) {
349 copy_m4_m4(diff_mat, pchan->bone->arm_mat);
350
351 if (cob && cob->pchan && cob->pchan->bone) {
352 invert_m4_m4(imat, cob->pchan->bone->arm_mat);
353 mul_m4_m4m4(diff_mat, imat, diff_mat);
354 }
355
356 zero_v3(diff_mat[3]);
357 invert_m4_m4(imat, diff_mat);
358 mul_m4_series(mat, diff_mat, mat, imat);
359 }
360 }
361 /* local to pose - do inverse procedure that was done for pose to local */
362 else {
363 if (pchan->bone) {
364 /* We need the:
365 * `posespace_matrix = local_matrix + (parent_posespace_matrix + restpos)`. */
366 BKE_armature_mat_bone_to_pose(pchan, mat, mat);
367 }
368
369 /* use pose-space as stepping stone for other spaces */
371 {
372 /* call self with slightly different values */
374 ob, pchan, cob, mat, CONSTRAINT_SPACE_POSE, to, keep_scale);
375 }
376 }
377 break;
378 }
379 case CONSTRAINT_SPACE_OWNLOCAL: { /* -------------- FROM OWNER LOCAL ---------- */
380 /* owner local to local */
381 if (pchan->bone) {
382 copy_m4_m4(diff_mat, pchan->bone->arm_mat);
383
384 if (cob && cob->pchan && cob->pchan->bone) {
385 invert_m4_m4(imat, cob->pchan->bone->arm_mat);
386 mul_m4_m4m4(diff_mat, imat, diff_mat);
387 }
388
389 zero_v3(diff_mat[3]);
390 invert_m4_m4(imat, diff_mat);
391 mul_m4_series(mat, imat, mat, diff_mat);
392 }
393
394 if (to != CONSTRAINT_SPACE_LOCAL) {
395 /* call self with slightly different values */
397 ob, pchan, cob, mat, CONSTRAINT_SPACE_LOCAL, to, keep_scale);
398 }
399 break;
400 }
401 case CONSTRAINT_SPACE_PARLOCAL: /* -------------- FROM LOCAL WITH PARENT ---------- */
402 {
403 /* local + parent to pose */
404 if (pchan->bone) {
405 mul_m4_m4m4(mat, pchan->bone->arm_mat, mat);
406 }
407
408 /* use pose-space as stepping stone for other spaces */
409 if (ELEM(to,
414 {
415 /* call self with slightly different values */
417 ob, pchan, cob, mat, CONSTRAINT_SPACE_POSE, to, keep_scale);
418 }
419 break;
420 }
421 case CONSTRAINT_SPACE_CUSTOM: /* -------------- FROM CUSTOM SPACE ---------- */
422 {
423 /* Custom to world. */
424 BLI_assert(cob);
425 mul_m4_m4m4(mat, cob->space_obj_world_matrix, mat);
426
427 /* Use world-space as stepping stone for other spaces. */
428 if (to != CONSTRAINT_SPACE_WORLD) {
429 /* Call self with slightly different values. */
431 ob, pchan, cob, mat, CONSTRAINT_SPACE_WORLD, to, keep_scale);
432 }
433 break;
434 }
435 }
436 }
437 else {
438 /* objects */
439 if (from == CONSTRAINT_SPACE_WORLD) {
440 if (to == CONSTRAINT_SPACE_LOCAL) {
441 /* Check if object has a parent. */
442 if (ob->parent) {
443 /* 'subtract' parent's effects from owner. */
444 mul_m4_m4m4(diff_mat, ob->parent->object_to_world().ptr(), ob->parentinv);
445 invert_m4_m4_safe(imat, diff_mat);
446 mul_m4_m4m4(mat, imat, mat);
447 }
448 else {
449 /* Local space in this case will have to be defined as local to the owner's
450 * transform-property-rotated axes. So subtract this rotation component.
451 */
452 /* XXX This is actually an ugly hack, local space of a parent-less object *is* the same
453 * as global space! Think what we want actually here is some kind of 'Final Space', i.e
454 * . once transformations are applied - users are often confused about this too,
455 * this is not consistent with bones
456 * local space either... Meh :|
457 * --mont29
458 */
459 BKE_object_to_mat4(ob, diff_mat);
460 if (!keep_scale) {
461 normalize_m4(diff_mat);
462 }
463 zero_v3(diff_mat[3]);
464
465 invert_m4_m4_safe(imat, diff_mat);
466 mul_m4_m4m4(mat, imat, mat);
467 }
468 }
469 else if (to == CONSTRAINT_SPACE_CUSTOM) {
470 /* 'subtract' custom objects's effects from owner. */
471 BLI_assert(cob);
473 mul_m4_m4m4(mat, imat, mat);
474 }
475 }
476 else if (from == CONSTRAINT_SPACE_LOCAL) {
477 /* check that object has a parent - otherwise this won't work */
478 if (ob->parent) {
479 /* 'add' parent's effect back to owner */
480 mul_m4_m4m4(diff_mat, ob->parent->object_to_world().ptr(), ob->parentinv);
481 mul_m4_m4m4(mat, diff_mat, mat);
482 }
483 else {
484 /* Local space in this case will have to be defined as local to the owner's
485 * transform-property-rotated axes. So add back this rotation component.
486 */
487 /* XXX See comment above for world->local case... */
488 BKE_object_to_mat4(ob, diff_mat);
489 if (!keep_scale) {
490 normalize_m4(diff_mat);
491 }
492 zero_v3(diff_mat[3]);
493
494 mul_m4_m4m4(mat, diff_mat, mat);
495 }
496 if (to == CONSTRAINT_SPACE_CUSTOM) {
497 /* 'subtract' objects's effects from owner. */
498 BLI_assert(cob);
500 mul_m4_m4m4(mat, imat, mat);
501 }
502 }
503 else if (from == CONSTRAINT_SPACE_CUSTOM) {
504 /* Custom to world. */
505 BLI_assert(cob);
506 mul_m4_m4m4(mat, cob->space_obj_world_matrix, mat);
507
508 /* Use world-space as stepping stone for other spaces. */
509 if (to != CONSTRAINT_SPACE_WORLD) {
510 /* Call self with slightly different values. */
512 ob, pchan, cob, mat, CONSTRAINT_SPACE_WORLD, to, keep_scale);
513 }
514 }
515 }
516}
517
518/* ------------ General Target Matrix Tools ---------- */
519
520/* function that sets the given matrix based on given vertex group in mesh */
521static void contarget_get_mesh_mat(Object *ob, const char *substring, float mat[4][4])
522{
523 /* when not in EditMode, use the 'final' evaluated mesh, depsgraph
524 * ensures we build with CD_MDEFORMVERT layer
525 */
526 const Mesh *mesh_eval = BKE_object_get_evaluated_mesh(ob);
528 float plane[3];
529 float imat[3][3], tmat[3][3];
530 const int defgroup = BKE_object_defgroup_name_index(ob, substring);
531
532 /* initialize target matrix using target matrix */
533 copy_m4_m4(mat, ob->object_to_world().ptr());
534
535 /* get index of vertex group */
536 if (defgroup == -1) {
537 return;
538 }
539
540 float vec[3] = {0.0f, 0.0f, 0.0f};
541 float normal[3] = {0.0f, 0.0f, 0.0f};
542 float weightsum = 0.0f;
543 if (em) {
545 BMVert *v;
546 BMIter iter;
547
548 BM_ITER_MESH (v, &iter, em->bm, BM_VERTS_OF_MESH) {
549 MDeformVert *dv = static_cast<MDeformVert *>(
550 CustomData_bmesh_get(&em->bm->vdata, v->head.data, CD_MDEFORMVERT));
551 MDeformWeight *dw = BKE_defvert_find_index(dv, defgroup);
552
553 if (dw && dw->weight > 0.0f) {
554 madd_v3_v3fl(vec, v->co, dw->weight);
555 madd_v3_v3fl(normal, v->no, dw->weight);
556 weightsum += dw->weight;
557 }
558 }
559 }
560 }
561 else if (mesh_eval) {
562 const blender::Span<blender::float3> positions = mesh_eval->vert_positions();
563 const blender::Span<blender::float3> vert_normals = mesh_eval->vert_normals();
564 const blender::Span<MDeformVert> dverts = mesh_eval->deform_verts();
565 /* check that dvert is a valid pointers (just in case) */
566 if (!dverts.is_empty()) {
567 /* get the average of all verts with that are in the vertex-group */
568 for (const int i : positions.index_range()) {
569 const MDeformVert *dv = &dverts[i];
570 const MDeformWeight *dw = BKE_defvert_find_index(dv, defgroup);
571
572 if (dw && dw->weight > 0.0f) {
573 madd_v3_v3fl(vec, positions[i], dw->weight);
574 madd_v3_v3fl(normal, vert_normals[i], dw->weight);
575 weightsum += dw->weight;
576 }
577 }
578 }
579 }
580 else {
581 /* No valid edit or evaluated mesh, just abort. */
582 return;
583 }
584
585 /* calculate averages of normal and coordinates */
586 if (weightsum > 0) {
587 mul_v3_fl(vec, 1.0f / weightsum);
588 mul_v3_fl(normal, 1.0f / weightsum);
589 }
590
591 /* derive the rotation from the average normal:
592 * - code taken from transform_gizmo.c,
593 * calc_gizmo_stats, V3D_ORIENT_NORMAL case */
594
595 /* We need the transpose of the inverse for a normal. */
596 copy_m3_m4(imat, ob->object_to_world().ptr());
597
598 invert_m3_m3(tmat, imat);
599 transpose_m3(tmat);
600 mul_m3_v3(tmat, normal);
601
602 normalize_v3(normal);
603 copy_v3_v3(plane, tmat[1]);
604
605 cross_v3_v3v3(mat[0], normal, plane);
606 if (len_squared_v3(mat[0]) < square_f(1e-3f)) {
607 copy_v3_v3(plane, tmat[0]);
608 cross_v3_v3v3(mat[0], normal, plane);
609 }
610
611 copy_v3_v3(mat[2], normal);
612 cross_v3_v3v3(mat[1], mat[2], mat[0]);
613
614 normalize_m4(mat);
615
616 /* apply the average coordinate as the new location */
617 mul_v3_m4v3(mat[3], ob->object_to_world().ptr(), vec);
618}
619
620/* function that sets the given matrix based on given vertex group in lattice */
621static void contarget_get_lattice_mat(Object *ob, const char *substring, float mat[4][4])
622{
623 Lattice *lt = (Lattice *)ob->data;
624
625 DispList *dl = ob->runtime->curve_cache ?
626 BKE_displist_find(&ob->runtime->curve_cache->disp, DL_VERTS) :
627 nullptr;
628 const float *co = dl ? dl->verts : nullptr;
629 BPoint *bp = lt->def;
630
631 MDeformVert *dv = lt->dvert;
632 int tot_verts = lt->pntsu * lt->pntsv * lt->pntsw;
633 float vec[3] = {0.0f, 0.0f, 0.0f}, tvec[3];
634 int grouped = 0;
635 int i, n;
636 const int defgroup = BKE_object_defgroup_name_index(ob, substring);
637
638 /* initialize target matrix using target matrix */
639 copy_m4_m4(mat, ob->object_to_world().ptr());
640
641 /* get index of vertex group */
642 if (defgroup == -1) {
643 return;
644 }
645 if (dv == nullptr) {
646 return;
647 }
648
649 /* 1. Loop through control-points checking if in nominated vertex-group.
650 * 2. If it is, add it to vec to find the average point.
651 */
652 for (i = 0; i < tot_verts; i++, dv++) {
653 for (n = 0; n < dv->totweight; n++) {
654 MDeformWeight *dw = BKE_defvert_find_index(dv, defgroup);
655 if (dw && dw->weight > 0.0f) {
656 /* copy coordinates of point to temporary vector, then add to find average */
657 memcpy(tvec, co ? co : bp->vec, sizeof(float[3]));
658
659 add_v3_v3(vec, tvec);
660 grouped++;
661 }
662 }
663
664 /* advance pointer to coordinate data */
665 if (co) {
666 co += 3;
667 }
668 else {
669 bp++;
670 }
671 }
672
673 /* find average location, then multiply by ob->object_to_world().ptr() to find world-space
674 * location */
675 if (grouped) {
676 mul_v3_fl(vec, 1.0f / grouped);
677 }
678 mul_v3_m4v3(tvec, ob->object_to_world().ptr(), vec);
679
680 /* copy new location to matrix */
681 copy_v3_v3(mat[3], tvec);
682}
683
684/* generic function to get the appropriate matrix for most target cases */
685/* The cases where the target can be object data have not been implemented */
687 const char *substring,
688 bConstraintOb *cob,
689 float mat[4][4],
690 short from,
691 short to,
692 short flag,
693 float headtail)
694{
695 /* Case OBJECT */
696 if (substring[0] == '\0') {
697 copy_m4_m4(mat, ob->object_to_world().ptr());
698 BKE_constraint_mat_convertspace(ob, nullptr, cob, mat, from, to, false);
699 }
700 /* Case VERTEXGROUP */
701 /* Current method just takes the average location of all the points in the
702 * VertexGroup, and uses that as the location value of the targets. Where
703 * possible, the orientation will also be calculated, by calculating an
704 * 'average' vertex normal, and deriving the rotation from that.
705 *
706 * NOTE: EditMode is not currently supported, and will most likely remain that
707 * way as constraints can only really affect things on object/bone level.
708 */
709 else if (ob->type == OB_MESH) {
710 contarget_get_mesh_mat(ob, substring, mat);
711 BKE_constraint_mat_convertspace(ob, nullptr, cob, mat, from, to, false);
712 }
713 else if (ob->type == OB_LATTICE) {
714 contarget_get_lattice_mat(ob, substring, mat);
715 BKE_constraint_mat_convertspace(ob, nullptr, cob, mat, from, to, false);
716 }
717 /* Case BONE */
718 else {
719 bPoseChannel *pchan;
720
721 pchan = BKE_pose_channel_find_name(ob->pose, substring);
722 if (pchan) {
723 /* Multiply the PoseSpace accumulation/final matrix for this
724 * PoseChannel by the Armature Object's Matrix to get a world-space matrix.
725 */
726 bool is_bbone = (pchan->bone) && (pchan->bone->segments > 1) &&
728 bool full_bbone = (flag & CONSTRAINT_BBONE_SHAPE_FULL) != 0;
729
730 if (headtail < 0.000001f && !(is_bbone && full_bbone)) {
731 /* skip length interpolation if set to head */
732 mul_m4_m4m4(mat, ob->object_to_world().ptr(), pchan->pose_mat);
733 }
734 else if (is_bbone && pchan->bone->segments == pchan->runtime.bbone_segments) {
735 /* use point along bbone */
736 Mat4 *bbone = pchan->runtime.bbone_pose_mats;
737 float tempmat[4][4];
738 float loc[3], fac;
739 int index;
740
741 /* figure out which segment(s) the headtail value falls in */
742 BKE_pchan_bbone_deform_clamp_segment_index(pchan, headtail, &index, &fac);
743
744 /* apply full transformation of the segment if requested */
745 if (full_bbone) {
746 interp_m4_m4m4(tempmat, bbone[index].mat, bbone[index + 1].mat, fac);
747
748 mul_m4_m4m4(tempmat, pchan->pose_mat, tempmat);
749 }
750 /* only interpolate location */
751 else {
752 interp_v3_v3v3(loc, bbone[index].mat[3], bbone[index + 1].mat[3], fac);
753
754 copy_m4_m4(tempmat, pchan->pose_mat);
755 mul_v3_m4v3(tempmat[3], pchan->pose_mat, loc);
756 }
757
758 mul_m4_m4m4(mat, ob->object_to_world().ptr(), tempmat);
759 }
760 else {
761 float tempmat[4][4], loc[3];
762
763 /* interpolate along length of bone */
764 interp_v3_v3v3(loc, pchan->pose_head, pchan->pose_tail, headtail);
765
766 /* use interpolated distance for subtarget */
767 copy_m4_m4(tempmat, pchan->pose_mat);
768 copy_v3_v3(tempmat[3], loc);
769
770 mul_m4_m4m4(mat, ob->object_to_world().ptr(), tempmat);
771 }
772 }
773 else {
774 copy_m4_m4(mat, ob->object_to_world().ptr());
775 }
776
777 /* convert matrix space as required */
778 BKE_constraint_mat_convertspace(ob, pchan, cob, mat, from, to, false);
779 }
780}
781
782/* ************************* Specific Constraints ***************************** */
783/* Each constraint defines a set of functions, which will be called at the appropriate
784 * times. In addition to this, each constraint should have a type-info struct, where
785 * its functions are attached for use.
786 */
787
788/* Template for type-info data:
789 * - make a copy of this when creating new constraints, and just change the functions
790 * pointed to as necessary
791 * - although the naming of functions doesn't matter, it would help for code
792 * readability, to follow the same naming convention as is presented here
793 * - any functions that a constraint doesn't need to define, don't define
794 * for such cases, just use nullptr
795 * - these should be defined after all the functions have been defined, so that
796 * forward-definitions/prototypes don't need to be used!
797 * - keep this copy #if-def'd so that future constraints can get based off this
798 */
799#if 0
800static bConstraintTypeInfo CTI_CONSTRNAME = {
801 /*type*/ CONSTRAINT_TYPE_CONSTRNAME,
802 /*size*/ sizeof(bConstrNameConstraint),
803 /*name*/ "ConstrName",
804 /*struct_name*/ "bConstrNameConstraint",
805 /*free_data*/ constrname_free,
806 /*id_looper*/ constrname_id_looper,
807 /*copy_data*/ constrname_copy,
808 /*new_data*/ constrname_new_data,
809 /*get_constraint_targets*/ constrname_get_tars,
810 /*flush_constraint_targets*/ constrname_flush_tars,
811 /*get_target_matrix*/ constrname_get_tarmat,
812 /*evaluate_constraint*/ constrname_evaluate,
813};
814#endif
815
817{
818 if (ct) {
819 unit_m4(ct->matrix);
820 }
821}
822
823/* This function should be used for the get_target_matrix member of all
824 * constraints that are not picky about what happens to their target matrix.
825 *
826 * \returns whether the constraint has a valid target.
827 */
828static bool default_get_tarmat(Depsgraph * /*depsgraph*/,
829 bConstraint *con,
830 bConstraintOb *cob,
832 float /*ctime*/)
833{
834 if (!VALID_CONS_TARGET(ct)) {
836 return false;
837 }
838
840 ct->subtarget,
841 cob,
842 ct->matrix,
844 ct->space,
845 con->flag,
846 con->headtail);
847 return true;
848}
849
850/* This is a variant that extracts full transformation from B-Bone segments.
851 */
852static bool default_get_tarmat_full_bbone(Depsgraph * /*depsgraph*/,
853 bConstraint *con,
854 bConstraintOb *cob,
856 float /*ctime*/)
857{
858 if (!VALID_CONS_TARGET(ct)) {
860 return false;
861 }
862
864 ct->subtarget,
865 cob,
866 ct->matrix,
868 ct->space,
870 con->headtail);
871 return true;
872}
873
874/* This following macro should be used for all standard single-target *_get_tars functions
875 * to save typing and reduce maintenance woes.
876 * (Hopefully all compilers will be happy with the lines with just a space on them.
877 * Those are really just to help this code easier to read).
878 */
879/* TODO: cope with getting rotation order... */
880#define SINGLETARGET_GET_TARS(con, datatar, datasubtarget, ct, list) \
881 { \
882 ct = MEM_callocN<bConstraintTarget>("tempConstraintTarget"); \
883\
884 ct->tar = datatar; \
885 STRNCPY_UTF8(ct->subtarget, datasubtarget); \
886 ct->space = con->tarspace; \
887 ct->flag = CONSTRAINT_TAR_TEMP; \
888\
889 if (ct->tar) { \
890 if ((ct->tar->type == OB_ARMATURE) && (ct->subtarget[0])) { \
891 bPoseChannel *pchan = BKE_pose_channel_find_name(ct->tar->pose, ct->subtarget); \
892 ct->type = CONSTRAINT_OBTYPE_BONE; \
893 ct->rotOrder = (pchan) ? (pchan->rotmode) : int(EULER_ORDER_DEFAULT); \
894 } \
895 else if (OB_TYPE_SUPPORT_VGROUP(ct->tar->type) && (ct->subtarget[0])) { \
896 ct->type = CONSTRAINT_OBTYPE_VERT; \
897 ct->rotOrder = EULER_ORDER_DEFAULT; \
898 } \
899 else { \
900 ct->type = CONSTRAINT_OBTYPE_OBJECT; \
901 ct->rotOrder = ct->tar->rotmode; \
902 } \
903 } \
904\
905 BLI_addtail(list, ct); \
906 } \
907 (void)0
908
909/* This following macro should be used for all standard single-target *_get_tars functions
910 * to save typing and reduce maintenance woes. It does not do the subtarget related operations
911 * (Hopefully all compilers will be happy with the lines with just a space on them. Those are
912 * really just to help this code easier to read)
913 */
914/* TODO: cope with getting rotation order... */
915#define SINGLETARGETNS_GET_TARS(con, datatar, ct, list) \
916 { \
917 ct = MEM_callocN<bConstraintTarget>("tempConstraintTarget"); \
918\
919 ct->tar = datatar; \
920 ct->space = con->tarspace; \
921 ct->flag = CONSTRAINT_TAR_TEMP; \
922\
923 if (ct->tar) { \
924 ct->type = CONSTRAINT_OBTYPE_OBJECT; \
925 } \
926 BLI_addtail(list, ct); \
927 } \
928 (void)0
929
930/* This following macro should be used for all standard single-target *_flush_tars functions
931 * to save typing and reduce maintenance woes.
932 * NOTE: the pointer to ct will be changed to point to the next in the list (as it gets removed)
933 * (Hopefully all compilers will be happy with the lines with just a space on them. Those are
934 * really just to help this code easier to read)
935 */
936#define SINGLETARGET_FLUSH_TARS(con, datatar, datasubtarget, ct, list, no_copy) \
937 { \
938 if (ct) { \
939 bConstraintTarget *ctn = ct->next; \
940 if (no_copy == 0) { \
941 datatar = ct->tar; \
942 STRNCPY_UTF8(datasubtarget, ct->subtarget); \
943 con->tarspace = char(ct->space); \
944 } \
945\
946 BLI_freelinkN(list, ct); \
947 ct = ctn; \
948 } \
949 } \
950 (void)0
951
952/* This following macro should be used for all standard single-target *_flush_tars functions
953 * to save typing and reduce maintenance woes. It does not do the subtarget related operations.
954 * NOTE: the pointer to ct will be changed to point to the next in the list (as it gets removed)
955 * (Hopefully all compilers will be happy with the lines with just a space on them. Those are
956 * really just to help this code easier to read)
957 */
958#define SINGLETARGETNS_FLUSH_TARS(con, datatar, ct, list, no_copy) \
959 { \
960 if (ct) { \
961 bConstraintTarget *ctn = ct->next; \
962 if (no_copy == 0) { \
963 datatar = ct->tar; \
964 con->tarspace = char(ct->space); \
965 } \
966\
967 BLI_freelinkN(list, ct); \
968 ct = ctn; \
969 } \
970 } \
971 (void)0
972
977
978/* --------- ChildOf Constraint ------------ */
979
989
990static void childof_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
991{
992 bChildOfConstraint *data = static_cast<bChildOfConstraint *>(con->data);
993
994 /* target only */
995 func(con, (ID **)&data->tar, false, userdata);
996}
997
998static int childof_get_tars(bConstraint *con, ListBase *list)
999{
1000 if (con && list) {
1001 bChildOfConstraint *data = static_cast<bChildOfConstraint *>(con->data);
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
1013static void childof_flush_tars(bConstraint *con, ListBase *list, bool no_copy)
1014{
1015 if (con && list) {
1016 bChildOfConstraint *data = static_cast<bChildOfConstraint *>(con->data);
1017 bConstraintTarget *ct = static_cast<bConstraintTarget *>(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, no_copy);
1021 }
1022}
1023
1024static void childof_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *targets)
1025{
1026 bChildOfConstraint *data = static_cast<bChildOfConstraint *>(con->data);
1027 bConstraintTarget *ct = static_cast<bConstraintTarget *>(targets->first);
1028
1029 /* Only evaluate if there is a target.
1030 *
1031 * NOTE: we're setting/unsetting the CONSTRAINT_SPACEONCE flag here because:
1032 *
1033 * 1. It's only used by the Child Of constraint anyway.
1034 * 2. It's only used to affect the steps taken immediately after this function
1035 * returns, and this way we ensure it's always set correctly for that.
1036 *
1037 * It was previously set in other places which resulted in bugs like #116567.
1038 * In the future we should ideally move to a different approach entirely. */
1039 if (!VALID_CONS_TARGET(ct)) {
1041 return;
1042 }
1043 con->flag |= CONSTRAINT_SPACEONCE;
1044
1045 float parmat[4][4];
1046 float inverse_matrix[4][4];
1047 /* Simple matrix parenting. */
1048 if ((data->flag & CHILDOF_ALL) == CHILDOF_ALL) {
1049 copy_m4_m4(parmat, ct->matrix);
1050 copy_m4_m4(inverse_matrix, data->invmat);
1051 }
1052 /* Filter the parent matrix by channel. */
1053 else {
1054 float loc[3], eul[3], size[3];
1055 float loco[3], eulo[3], sizeo[3];
1056
1057 /* extract components of both matrices */
1058 copy_v3_v3(loc, ct->matrix[3]);
1059 mat4_to_eulO(eul, ct->rotOrder, ct->matrix);
1060 mat4_to_size(size, ct->matrix);
1061
1062 copy_v3_v3(loco, data->invmat[3]);
1063 mat4_to_eulO(eulo, cob->rotOrder, data->invmat);
1064 mat4_to_size(sizeo, data->invmat);
1065
1066 /* Reset the locked channels to their no-op values. */
1067 if (!(data->flag & CHILDOF_LOCX)) {
1068 loc[0] = loco[0] = 0.0f;
1069 }
1070 if (!(data->flag & CHILDOF_LOCY)) {
1071 loc[1] = loco[1] = 0.0f;
1072 }
1073 if (!(data->flag & CHILDOF_LOCZ)) {
1074 loc[2] = loco[2] = 0.0f;
1075 }
1076 if (!(data->flag & CHILDOF_ROTX)) {
1077 eul[0] = eulo[0] = 0.0f;
1078 }
1079 if (!(data->flag & CHILDOF_ROTY)) {
1080 eul[1] = eulo[1] = 0.0f;
1081 }
1082 if (!(data->flag & CHILDOF_ROTZ)) {
1083 eul[2] = eulo[2] = 0.0f;
1084 }
1085 if (!(data->flag & CHILDOF_SIZEX)) {
1086 size[0] = sizeo[0] = 1.0f;
1087 }
1088 if (!(data->flag & CHILDOF_SIZEY)) {
1089 size[1] = sizeo[1] = 1.0f;
1090 }
1091 if (!(data->flag & CHILDOF_SIZEZ)) {
1092 size[2] = sizeo[2] = 1.0f;
1093 }
1094
1095 /* Construct the new matrices given the disabled channels. */
1096 loc_eulO_size_to_mat4(parmat, loc, eul, size, ct->rotOrder);
1097 loc_eulO_size_to_mat4(inverse_matrix, loco, eulo, sizeo, cob->rotOrder);
1098 }
1099
1100 /* If requested, compute the inverse matrix from the computed parent matrix. */
1101 if (data->flag & CHILDOF_SET_INVERSE) {
1102 invert_m4_m4(data->invmat, parmat);
1103 if (cob->pchan != nullptr) {
1104 mul_m4_series(data->invmat, data->invmat, cob->ob->object_to_world().ptr());
1105 }
1106
1107 copy_m4_m4(inverse_matrix, data->invmat);
1108
1109 data->flag &= ~CHILDOF_SET_INVERSE;
1110
1111 /* Write the computed matrix back to the master copy if in copy-on-eval evaluation. */
1113
1114 if (orig_con != nullptr) {
1115 bChildOfConstraint *orig_data = static_cast<bChildOfConstraint *>(orig_con->data);
1116
1117 copy_m4_m4(orig_data->invmat, data->invmat);
1118 orig_data->flag &= ~CHILDOF_SET_INVERSE;
1119 }
1120 }
1121
1122 /* Multiply together the target (parent) matrix, parent inverse,
1123 * and the owner transform matrix to get the effect of this constraint
1124 * (i.e. owner is 'parented' to parent). */
1125 float orig_cob_matrix[4][4];
1126 copy_m4_m4(orig_cob_matrix, cob->matrix);
1127 mul_m4_series(cob->matrix, parmat, inverse_matrix, orig_cob_matrix);
1128
1129 /* Without this, changes to scale and rotation can change location
1130 * of a parentless bone or a disconnected bone. Even though its set
1131 * to zero above. */
1132 if (!(data->flag & CHILDOF_LOCX)) {
1133 cob->matrix[3][0] = orig_cob_matrix[3][0];
1134 }
1135 if (!(data->flag & CHILDOF_LOCY)) {
1136 cob->matrix[3][1] = orig_cob_matrix[3][1];
1137 }
1138 if (!(data->flag & CHILDOF_LOCZ)) {
1139 cob->matrix[3][2] = orig_cob_matrix[3][2];
1140 }
1141}
1142
1143/* XXX NOTE: con->flag should be CONSTRAINT_SPACEONCE for bone-childof, patched in `readfile.cc`.
1144 */
1146 /*type*/ CONSTRAINT_TYPE_CHILDOF,
1147 /*size*/ sizeof(bChildOfConstraint),
1148 /*name*/ N_("Child Of"),
1149 /*struct_name*/ "bChildOfConstraint",
1150 /*free_data*/ nullptr,
1151 /*id_looper*/ childof_id_looper,
1152 /*copy_data*/ nullptr,
1153 /*new_data*/ childof_new_data,
1154 /*get_constraint_targets*/ childof_get_tars,
1155 /*flush_constraint_targets*/ childof_flush_tars,
1156 /*get_target_matrix*/ default_get_tarmat,
1157 /*evaluate_constraint*/ childof_evaluate,
1158};
1159
1160/* -------- TrackTo Constraint ------- */
1161
1162static void trackto_new_data(void *cdata)
1163{
1165
1166 data->reserved1 = TRACK_nZ;
1167 data->reserved2 = UP_Y;
1168}
1169
1170static void trackto_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
1171{
1172 bTrackToConstraint *data = static_cast<bTrackToConstraint *>(con->data);
1173
1174 /* target only */
1175 func(con, (ID **)&data->tar, false, userdata);
1176}
1177
1179{
1180 if (con && list) {
1181 bTrackToConstraint *data = static_cast<bTrackToConstraint *>(con->data);
1183
1184 /* standard target-getting macro for single-target constraints */
1185 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list);
1186
1187 return 1;
1188 }
1189
1190 return 0;
1191}
1192
1193static void trackto_flush_tars(bConstraint *con, ListBase *list, bool no_copy)
1194{
1195 if (con && list) {
1196 bTrackToConstraint *data = static_cast<bTrackToConstraint *>(con->data);
1197 bConstraintTarget *ct = static_cast<bConstraintTarget *>(list->first);
1198
1199 /* the following macro is used for all standard single-target constraints */
1200 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, no_copy);
1201 }
1202}
1203
1204static int basis_cross(int n, int m)
1205{
1206 switch (n - m) {
1207 case 1:
1208 case -2:
1209 return 1;
1210
1211 case -1:
1212 case 2:
1213 return -1;
1214
1215 default:
1216 return 0;
1217 }
1218}
1219
1220static void vectomat(const float vec[3],
1221 const float target_up[3],
1222 short axis,
1223 short upflag,
1224 short flags,
1225 float m[3][3])
1226{
1227 float n[3];
1228 float u[3]; /* vector specifying the up axis */
1229 float proj[3];
1230 float right[3];
1231 float neg = -1;
1232 int right_index;
1233
1234 if (normalize_v3_v3(n, vec) == 0.0f) {
1235 n[0] = 0.0f;
1236 n[1] = 0.0f;
1237 n[2] = 1.0f;
1238 }
1239 if (axis > 2) {
1240 axis -= 3;
1241 }
1242 else {
1243 negate_v3(n);
1244 }
1245
1246 /* n specifies the transformation of the track axis */
1247 if (flags & TARGET_Z_UP) {
1248 /* target Z axis is the global up axis */
1249 copy_v3_v3(u, target_up);
1250 }
1251 else {
1252 /* world Z axis is the global up axis */
1253 u[0] = 0;
1254 u[1] = 0;
1255 u[2] = 1;
1256 }
1257
1258 /* NOTE: even though 'n' is normalized, don't use 'project_v3_v3v3_normalized' below
1259 * because precision issues cause a problem in near degenerate states, see: #53455. */
1260
1261 /* project the up vector onto the plane specified by n */
1262 project_v3_v3v3(proj, u, n); /* first u onto n... */
1263 sub_v3_v3v3(proj, u, proj); /* then onto the plane */
1264 /* proj specifies the transformation of the up axis */
1265
1266 if (normalize_v3(proj) == 0.0f) { /* degenerate projection */
1267 proj[0] = 0.0f;
1268 proj[1] = 1.0f;
1269 proj[2] = 0.0f;
1270 }
1271
1272 /* Normalized cross product of n and proj specifies transformation of the right axis */
1273 cross_v3_v3v3(right, proj, n);
1274 normalize_v3(right);
1275
1276 if (axis != upflag) {
1277 right_index = 3 - axis - upflag;
1278 neg = float(basis_cross(axis, upflag));
1279
1280 /* account for up direction, track direction */
1281 m[right_index][0] = neg * right[0];
1282 m[right_index][1] = neg * right[1];
1283 m[right_index][2] = neg * right[2];
1284
1285 copy_v3_v3(m[upflag], proj);
1286
1287 copy_v3_v3(m[axis], n);
1288 }
1289 /* identity matrix - don't do anything if the two axes are the same */
1290 else {
1291 unit_m3(m);
1292 }
1293}
1294
1295static void trackto_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *targets)
1296{
1297 bTrackToConstraint *data = static_cast<bTrackToConstraint *>(con->data);
1298 bConstraintTarget *ct = static_cast<bConstraintTarget *>(targets->first);
1299
1300 if (VALID_CONS_TARGET(ct)) {
1301 float size[3], vec[3];
1302 float totmat[3][3];
1303
1304 /* Get size property, since ob->scale is only the object's own relative size,
1305 * not its global one. */
1306 mat4_to_size(size, cob->matrix);
1307
1308 /* Clear the object's rotation */
1309 cob->matrix[0][0] = size[0];
1310 cob->matrix[0][1] = 0;
1311 cob->matrix[0][2] = 0;
1312 cob->matrix[1][0] = 0;
1313 cob->matrix[1][1] = size[1];
1314 cob->matrix[1][2] = 0;
1315 cob->matrix[2][0] = 0;
1316 cob->matrix[2][1] = 0;
1317 cob->matrix[2][2] = size[2];
1318
1319 /* NOTE(@joshualung): `targetmat[2]` instead of `ownermat[2]` is passed to #vectomat
1320 * for backwards compatibility it seems. */
1321 sub_v3_v3v3(vec, cob->matrix[3], ct->matrix[3]);
1322 vectomat(vec,
1323 ct->matrix[2],
1324 std::clamp<short>(data->reserved1, 0, 5),
1325 std::clamp<short>(data->reserved2, 0, 2),
1326 data->flags,
1327 totmat);
1328
1329 mul_m4_m3m4(cob->matrix, totmat, cob->matrix);
1330 }
1331}
1332
1334 /*type*/ CONSTRAINT_TYPE_TRACKTO,
1335 /*size*/ sizeof(bTrackToConstraint),
1336 /*name*/ N_("Track To"),
1337 /*struct_name*/ "bTrackToConstraint",
1338 /*free_data*/ nullptr,
1339 /*id_looper*/ trackto_id_looper,
1340 /*copy_data*/ nullptr,
1341 /*new_data*/ trackto_new_data,
1342 /*get_constraint_targets*/ trackto_get_tars,
1343 /*flush_constraint_targets*/ trackto_flush_tars,
1344 /*get_target_matrix*/ default_get_tarmat,
1345 /*evaluate_constraint*/ trackto_evaluate,
1346};
1347
1348/* --------- Inverse-Kinematics --------- */
1349
1350static void kinematic_new_data(void *cdata)
1351{
1353
1354 data->weight = 1.0f;
1355 data->orientweight = 1.0f;
1356 data->iterations = 500;
1357 data->dist = 1.0f;
1359}
1360
1361static void kinematic_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
1362{
1363 bKinematicConstraint *data = static_cast<bKinematicConstraint *>(con->data);
1364
1365 /* chain target */
1366 func(con, (ID **)&data->tar, false, userdata);
1367
1368 /* poletarget */
1369 func(con, (ID **)&data->poletar, false, userdata);
1370}
1371
1373{
1374 if (con && list) {
1375 bKinematicConstraint *data = static_cast<bKinematicConstraint *>(con->data);
1377
1378 /* standard target-getting macro for single-target constraints is used twice here */
1379 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list);
1380 SINGLETARGET_GET_TARS(con, data->poletar, data->polesubtarget, ct, list);
1381
1382 return 2;
1383 }
1384
1385 return 0;
1386}
1387
1388static void kinematic_flush_tars(bConstraint *con, ListBase *list, bool no_copy)
1389{
1390 if (con && list) {
1391 bKinematicConstraint *data = static_cast<bKinematicConstraint *>(con->data);
1392 bConstraintTarget *ct = static_cast<bConstraintTarget *>(list->first);
1393
1394 /* the following macro is used for all standard single-target constraints */
1395 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, no_copy);
1396 SINGLETARGET_FLUSH_TARS(con, data->poletar, data->polesubtarget, ct, list, no_copy);
1397 }
1398}
1399
1400static bool kinematic_get_tarmat(Depsgraph * /*depsgraph*/,
1401 bConstraint *con,
1402 bConstraintOb *cob,
1404 float /*ctime*/)
1405{
1406 bKinematicConstraint *data = static_cast<bKinematicConstraint *>(con->data);
1407
1408 if (VALID_CONS_TARGET(ct)) {
1410 ct->subtarget,
1411 cob,
1412 ct->matrix,
1414 ct->space,
1415 con->flag,
1416 con->headtail);
1417 return true;
1418 }
1419
1420 if (!ct) {
1421 return false;
1422 }
1423 if ((data->flag & CONSTRAINT_IK_AUTO) == 0) {
1424 unit_m4(ct->matrix);
1425 return false;
1426 }
1427
1428 Object *ob = cob->ob;
1429 if (ob == nullptr) {
1430 unit_m4(ct->matrix);
1431 return false;
1432 }
1433
1434 float vec[3];
1435 /* move grabtarget into world space */
1436 mul_v3_m4v3(vec, ob->object_to_world().ptr(), data->grabtarget);
1437 copy_m4_m4(ct->matrix, ob->object_to_world().ptr());
1438 copy_v3_v3(ct->matrix[3], vec);
1439
1440 return true;
1441}
1442
1445 /*size*/ sizeof(bKinematicConstraint),
1446 /*name*/ N_("IK"),
1447 /*struct_name*/ "bKinematicConstraint",
1448 /*free_data*/ nullptr,
1449 /*id_looper*/ kinematic_id_looper,
1450 /*copy_data*/ nullptr,
1451 /*new_data*/ kinematic_new_data,
1452 /*get_constraint_targets*/ kinematic_get_tars,
1453 /*flush_constraint_targets*/ kinematic_flush_tars,
1454 /*get_target_matrix*/ kinematic_get_tarmat,
1455 /*evaluate_constraint*/ nullptr,
1456};
1457
1458/* -------- Follow-Path Constraint ---------- */
1459
1460static void followpath_new_data(void *cdata)
1461{
1463
1464 data->trackflag = TRACK_Y;
1465 data->upflag = UP_Z;
1466 data->offset = 0;
1467 data->followflag = 0;
1468}
1469
1470static void followpath_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
1471{
1472 bFollowPathConstraint *data = static_cast<bFollowPathConstraint *>(con->data);
1473
1474 /* target only */
1475 func(con, (ID **)&data->tar, false, userdata);
1476}
1477
1479{
1480 if (con && list) {
1481 bFollowPathConstraint *data = static_cast<bFollowPathConstraint *>(con->data);
1483
1484 /* Standard target-getting macro for single-target constraints without sub-targets. */
1485 SINGLETARGETNS_GET_TARS(con, data->tar, ct, list);
1486
1487 return 1;
1488 }
1489
1490 return 0;
1491}
1492
1493static void followpath_flush_tars(bConstraint *con, ListBase *list, bool no_copy)
1494{
1495 if (con && list) {
1496 bFollowPathConstraint *data = static_cast<bFollowPathConstraint *>(con->data);
1497 bConstraintTarget *ct = static_cast<bConstraintTarget *>(list->first);
1498
1499 /* the following macro is used for all standard single-target constraints */
1500 SINGLETARGETNS_FLUSH_TARS(con, data->tar, ct, list, no_copy);
1501 }
1502}
1503
1504static bool followpath_get_tarmat(Depsgraph * /*depsgraph*/,
1505 bConstraint *con,
1506 bConstraintOb * /*cob*/,
1508 float /*ctime*/)
1509{
1510 bFollowPathConstraint *data = static_cast<bFollowPathConstraint *>(con->data);
1511
1512 if (!VALID_CONS_TARGET(ct) || ct->tar->type != OB_CURVES_LEGACY) {
1514 return false;
1515 }
1516
1517 Curve *cu = static_cast<Curve *>(ct->tar->data);
1518 float vec[4], radius;
1519 float curvetime;
1520
1521 unit_m4(ct->matrix);
1522
1523 /* NOTE: when creating constraints that follow path, the curve gets the CU_PATH set now,
1524 * currently for paths to work it needs to go through the bevlist/displist system (ton)
1525 */
1526
1527 if (ct->tar->runtime->curve_cache == nullptr ||
1528 ct->tar->runtime->curve_cache->anim_path_accum_length == nullptr)
1529 {
1530 return false;
1531 }
1532
1533 float quat[4];
1534 if (data->followflag & FOLLOWPATH_STATIC) {
1535 /* fixed position along curve */
1536 curvetime = data->offset_fac;
1537 }
1538 else {
1539 /* animated position along curve depending on time */
1540 curvetime = cu->ctime - data->offset;
1541
1542 /* ctime is now a proper var setting of Curve which gets set by Animato like any other var
1543 * that's animated, but this will only work if it actually is animated...
1544 *
1545 * we divide the curvetime calculated in the previous step by the length of the path,
1546 * to get a time factor. */
1547 curvetime /= cu->pathlen;
1548
1549 Nurb *nu = static_cast<Nurb *>(cu->nurb.first);
1550 if (!(nu && nu->flagu & CU_NURB_CYCLIC) && cu->flag & CU_PATH_CLAMP) {
1551 /* If curve is not cyclic, clamp to the begin/end points if the curve clamp option is on.
1552 */
1553 CLAMP(curvetime, 0.0f, 1.0f);
1554 }
1555 }
1556
1557 if (!BKE_where_on_path(ct->tar,
1558 curvetime,
1559 vec,
1560 nullptr,
1561 (data->followflag & FOLLOWPATH_FOLLOW) ? quat : nullptr,
1562 &radius,
1563 nullptr))
1564 {
1565 return false;
1566 }
1567
1568 float totmat[4][4];
1569 unit_m4(totmat);
1570
1571 if (data->followflag & FOLLOWPATH_FOLLOW) {
1573 quat, std::clamp<short>(data->trackflag, 0, 5), std::clamp<short>(data->upflag, 0, 2));
1574 quat_to_mat4(totmat, quat);
1575 }
1576
1577 if (data->followflag & FOLLOWPATH_RADIUS) {
1578 float tmat[4][4], rmat[4][4];
1579 scale_m4_fl(tmat, radius);
1580 mul_m4_m4m4(rmat, tmat, totmat);
1581 copy_m4_m4(totmat, rmat);
1582 }
1583
1584 copy_v3_v3(totmat[3], vec);
1585
1586 mul_m4_m4m4(ct->matrix, ct->tar->object_to_world().ptr(), totmat);
1587 return true;
1588}
1589
1591{
1592 bConstraintTarget *ct = static_cast<bConstraintTarget *>(targets->first);
1593
1594 /* only evaluate if there is a target */
1595 if (VALID_CONS_TARGET(ct)) {
1596 float obmat[4][4];
1597 float size[3];
1598 bFollowPathConstraint *data = static_cast<bFollowPathConstraint *>(con->data);
1599
1600 /* get Object transform (loc/rot/size) to determine transformation from path */
1601 /* TODO: this used to be local at one point, but is probably more useful as-is */
1602 copy_m4_m4(obmat, cob->matrix);
1603
1604 /* get scaling of object before applying constraint */
1605 mat4_to_size(size, cob->matrix);
1606
1607 /* apply targetmat - containing location on path, and rotation */
1608 mul_m4_m4m4(cob->matrix, ct->matrix, obmat);
1609
1610 /* un-apply scaling caused by path */
1611 if ((data->followflag & FOLLOWPATH_RADIUS) == 0) {
1612 /* XXX(@ideasman42): Assume that scale correction means that radius
1613 * will have some scale error in it. */
1614 float obsize[3];
1615
1616 mat4_to_size(obsize, cob->matrix);
1617 if (obsize[0]) {
1618 mul_v3_fl(cob->matrix[0], size[0] / obsize[0]);
1619 }
1620 if (obsize[1]) {
1621 mul_v3_fl(cob->matrix[1], size[1] / obsize[1]);
1622 }
1623 if (obsize[2]) {
1624 mul_v3_fl(cob->matrix[2], size[2] / obsize[2]);
1625 }
1626 }
1627 }
1628}
1629
1632 /*size*/ sizeof(bFollowPathConstraint),
1633 /*name*/ N_("Follow Path"),
1634 /*struct_name*/ "bFollowPathConstraint",
1635 /*free_data*/ nullptr,
1636 /*id_looper*/ followpath_id_looper,
1637 /*copy_data*/ nullptr,
1638 /*new_data*/ followpath_new_data,
1639 /*get_constraint_targets*/ followpath_get_tars,
1640 /*flush_constraint_targets*/ followpath_flush_tars,
1641 /*get_target_matrix*/ followpath_get_tarmat,
1642 /*evaluate_constraint*/ followpath_evaluate,
1643};
1644
1645/* --------- Limit Location --------- */
1646
1647static void loclimit_evaluate(bConstraint *con, bConstraintOb *cob, ListBase * /*targets*/)
1648{
1649 bLocLimitConstraint *data = static_cast<bLocLimitConstraint *>(con->data);
1650
1651 if (data->flag & LIMIT_XMIN) {
1652 cob->matrix[3][0] = std::max(cob->matrix[3][0], data->xmin);
1653 }
1654 if (data->flag & LIMIT_XMAX) {
1655 cob->matrix[3][0] = std::min(cob->matrix[3][0], data->xmax);
1656 }
1657 if (data->flag & LIMIT_YMIN) {
1658 cob->matrix[3][1] = std::max(cob->matrix[3][1], data->ymin);
1659 }
1660 if (data->flag & LIMIT_YMAX) {
1661 cob->matrix[3][1] = std::min(cob->matrix[3][1], data->ymax);
1662 }
1663 if (data->flag & LIMIT_ZMIN) {
1664 cob->matrix[3][2] = std::max(cob->matrix[3][2], data->zmin);
1665 }
1666 if (data->flag & LIMIT_ZMAX) {
1667 cob->matrix[3][2] = std::min(cob->matrix[3][2], data->zmax);
1668 }
1669}
1670
1672 /*type*/ CONSTRAINT_TYPE_LOCLIMIT,
1673 /*size*/ sizeof(bLocLimitConstraint),
1674 /*name*/ N_("Limit Location"),
1675 /*struct_name*/ "bLocLimitConstraint",
1676 /*free_data*/ nullptr,
1677 /*id_looper*/ nullptr,
1678 /*copy_data*/ nullptr,
1679 /*new_data*/ nullptr,
1680 /*get_constraint_targets*/ nullptr,
1681 /*flush_constraint_targets*/ nullptr,
1682 /*get_target_matrix*/ nullptr,
1683 /*evaluate_constraint*/ loclimit_evaluate,
1684};
1685
1686/* -------- Limit Rotation --------- */
1687
1691static inline float wrap_rad_angle(const float angle)
1692{
1693 const float b = angle * (0.5 / M_PI) + 0.5;
1694 return ((b - std::floor(b)) - 0.5) * (2.0 * M_PI);
1695}
1696
1707static float clamp_angle(const float angle, const float min, const float max)
1708{
1709 /* If the allowed range exceeds 360 degrees no clamping can occur. */
1710 if ((max - min) >= (2 * M_PI)) {
1711 return angle;
1712 }
1713
1714 /* Invalid case, just return min. */
1715 if (max <= min) {
1716 return min;
1717 }
1718
1719 /* Move min and max into a space where `angle == 0.0`, and wrap them to
1720 * [-PI, +PI] in that space. This simplifies the cases below, as we can
1721 * just use 0.0 in place of `angle` and know that everything is in
1722 * [-PI, +PI]. */
1723 const float min_wrapped = wrap_rad_angle(min - angle);
1724 const float max_wrapped = wrap_rad_angle(max - angle);
1725
1726 /* If the range defined by `min`/`max` doesn't contain the boundary at
1727 * PI/-PI. This is the simple case, because it means we can do a simple
1728 * clamp. */
1729 if (min_wrapped < max_wrapped) {
1730 return angle + std::clamp(0.0f, min_wrapped, max_wrapped);
1731 }
1732
1733 /* At this point we know that `min_wrapped` >= `max_wrapped`, meaning the boundary is crossed.
1734 * With that we know that no clamping is needed in the following case. */
1735 if (max_wrapped >= 0.0 || min_wrapped <= 0.0) {
1736 return angle;
1737 }
1738
1739 /* If zero is outside of the range, we clamp to the closest of `min_wrapped` or `max_wrapped`. */
1740 if (std::fabs(max_wrapped) < std::fabs(min_wrapped)) {
1741 return angle + max_wrapped;
1742 }
1743 return angle + min_wrapped;
1744}
1745
1746static void rotlimit_evaluate(bConstraint *con, bConstraintOb *cob, ListBase * /*targets*/)
1747{
1748 bRotLimitConstraint *data = static_cast<bRotLimitConstraint *>(con->data);
1749 float loc[3];
1750 float eul[3];
1751 float size[3];
1752
1753 /* This constraint is based on euler rotation math, which doesn't work well with shear.
1754 * The Y axis is chosen as the main one because constraints are most commonly used on bones.
1755 * This also allows using the constraint to simply remove shear. */
1756 orthogonalize_m4_stable(cob->matrix, 1, false);
1757
1758 /* Only do the complex processing if some limits are actually enabled. */
1759 if (!(data->flag & (LIMIT_XROT | LIMIT_YROT | LIMIT_ZROT))) {
1760 return;
1761 }
1762
1763 /* Select the Euler rotation order, defaulting to the owner value. */
1764 short rot_order = cob->rotOrder;
1765
1766 if (data->euler_order != CONSTRAINT_EULER_AUTO) {
1767 rot_order = data->euler_order;
1768 }
1769
1770 /* Decompose the matrix using the specified order. */
1771 copy_v3_v3(loc, cob->matrix[3]);
1772 mat4_to_size(size, cob->matrix);
1773
1774 mat4_to_eulO(eul, rot_order, cob->matrix);
1775
1776 /* Limit the euler values. */
1777 if (data->flag & LIMIT_ROT_LEGACY_BEHAVIOR) {
1778 /* The legacy behavior, which just does a naive clamping of the angles as
1779 * simple numbers. Since the input angles are always in the range [-180,
1780 * 180] degrees due to being derived from matrix decomposition, this naive
1781 * approach causes problems when rotations cross 180 degrees. Specifically,
1782 * it results in unpredictable and unwanted rotation flips of the
1783 * constrained objects/bones, especially when the constraint isn't in local
1784 * space.
1785 *
1786 * The correct thing to do is a more sophisticated form of clamping that
1787 * treats the angles as existing on a continuous loop, which is what the
1788 * non-legacy behavior further below does. However, for backwards
1789 * compatibility we are preserving this old behavior behind an option.
1790 *
1791 * See issues #117927 and #123105 for additional background. */
1792 if (data->flag & LIMIT_XROT) {
1793 eul[0] = clamp_f(eul[0], data->xmin, data->xmax);
1794 }
1795 if (data->flag & LIMIT_YROT) {
1796 eul[1] = clamp_f(eul[1], data->ymin, data->ymax);
1797 }
1798 if (data->flag & LIMIT_ZROT) {
1799 eul[2] = clamp_f(eul[2], data->zmin, data->zmax);
1800 }
1801 }
1802 else {
1803 /* The correct, non-legacy behavior. */
1804 if (data->flag & LIMIT_XROT) {
1805 eul[0] = clamp_angle(eul[0], data->xmin, data->xmax);
1806 }
1807 if (data->flag & LIMIT_YROT) {
1808 eul[1] = clamp_angle(eul[1], data->ymin, data->ymax);
1809 }
1810 if (data->flag & LIMIT_ZROT) {
1811 eul[2] = clamp_angle(eul[2], data->zmin, data->zmax);
1812 }
1813 }
1814
1815 loc_eulO_size_to_mat4(cob->matrix, loc, eul, size, rot_order);
1816}
1817
1819 /*type*/ CONSTRAINT_TYPE_ROTLIMIT,
1820 /*size*/ sizeof(bRotLimitConstraint),
1821 /*name*/ N_("Limit Rotation"),
1822 /*struct_name*/ "bRotLimitConstraint",
1823 /*free_data*/ nullptr,
1824 /*id_looper*/ nullptr,
1825 /*copy_data*/ nullptr,
1826 /*new_data*/ nullptr,
1827 /*get_constraint_targets*/ nullptr,
1828 /*flush_constraint_targets*/ nullptr,
1829 /*get_target_matrix*/ nullptr,
1830 /*evaluate_constraint*/ rotlimit_evaluate,
1831};
1832
1833/* --------- Limit Scale --------- */
1834
1835static void sizelimit_evaluate(bConstraint *con, bConstraintOb *cob, ListBase * /*targets*/)
1836{
1837 bSizeLimitConstraint *data = static_cast<bSizeLimitConstraint *>(con->data);
1838 float obsize[3], size[3];
1839
1840 mat4_to_size(size, cob->matrix);
1841
1842 copy_v3_v3(obsize, size);
1843
1844 if (data->flag & LIMIT_XMIN) {
1845 size[0] = std::max(size[0], data->xmin);
1846 }
1847 if (data->flag & LIMIT_XMAX) {
1848 size[0] = std::min(size[0], data->xmax);
1849 }
1850 if (data->flag & LIMIT_YMIN) {
1851 size[1] = std::max(size[1], data->ymin);
1852 }
1853 if (data->flag & LIMIT_YMAX) {
1854 size[1] = std::min(size[1], data->ymax);
1855 }
1856 if (data->flag & LIMIT_ZMIN) {
1857 size[2] = std::max(size[2], data->zmin);
1858 }
1859 if (data->flag & LIMIT_ZMAX) {
1860 size[2] = std::min(size[2], data->zmax);
1861 }
1862
1863 if (obsize[0]) {
1864 mul_v3_fl(cob->matrix[0], size[0] / obsize[0]);
1865 }
1866 if (obsize[1]) {
1867 mul_v3_fl(cob->matrix[1], size[1] / obsize[1]);
1868 }
1869 if (obsize[2]) {
1870 mul_v3_fl(cob->matrix[2], size[2] / obsize[2]);
1871 }
1872}
1873
1876 /*size*/ sizeof(bSizeLimitConstraint),
1877 /*name*/ N_("Limit Scale"),
1878 /*struct_name*/ "bSizeLimitConstraint",
1879 /*free_data*/ nullptr,
1880 /*id_looper*/ nullptr,
1881 /*copy_data*/ nullptr,
1882 /*new_data*/ nullptr,
1883 /*get_constraint_targets*/ nullptr,
1884 /*flush_constraint_targets*/ nullptr,
1885 /*get_target_matrix*/ nullptr,
1886 /*evaluate_constraint*/ sizelimit_evaluate,
1887};
1888
1889/* ----------- Copy Location ------------- */
1890
1891static void loclike_new_data(void *cdata)
1892{
1894
1895 data->flag = LOCLIKE_X | LOCLIKE_Y | LOCLIKE_Z;
1896}
1897
1898static void loclike_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
1899{
1900 bLocateLikeConstraint *data = static_cast<bLocateLikeConstraint *>(con->data);
1901
1902 /* target only */
1903 func(con, (ID **)&data->tar, false, userdata);
1904}
1905
1907{
1908 if (con && list) {
1909 bLocateLikeConstraint *data = static_cast<bLocateLikeConstraint *>(con->data);
1911
1912 /* standard target-getting macro for single-target constraints */
1913 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list);
1914
1915 return 1;
1916 }
1917
1918 return 0;
1919}
1920
1921static void loclike_flush_tars(bConstraint *con, ListBase *list, bool no_copy)
1922{
1923 if (con && list) {
1924 bLocateLikeConstraint *data = static_cast<bLocateLikeConstraint *>(con->data);
1925 bConstraintTarget *ct = static_cast<bConstraintTarget *>(list->first);
1926
1927 /* the following macro is used for all standard single-target constraints */
1928 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, no_copy);
1929 }
1930}
1931
1932static void loclike_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *targets)
1933{
1934 bLocateLikeConstraint *data = static_cast<bLocateLikeConstraint *>(con->data);
1935 bConstraintTarget *ct = static_cast<bConstraintTarget *>(targets->first);
1936
1937 if (VALID_CONS_TARGET(ct)) {
1938 float offset[3] = {0.0f, 0.0f, 0.0f};
1939
1940 if (data->flag & LOCLIKE_OFFSET) {
1941 copy_v3_v3(offset, cob->matrix[3]);
1942 }
1943
1944 if (data->flag & LOCLIKE_X) {
1945 cob->matrix[3][0] = ct->matrix[3][0];
1946
1947 if (data->flag & LOCLIKE_X_INVERT) {
1948 cob->matrix[3][0] *= -1;
1949 }
1950 cob->matrix[3][0] += offset[0];
1951 }
1952 if (data->flag & LOCLIKE_Y) {
1953 cob->matrix[3][1] = ct->matrix[3][1];
1954
1955 if (data->flag & LOCLIKE_Y_INVERT) {
1956 cob->matrix[3][1] *= -1;
1957 }
1958 cob->matrix[3][1] += offset[1];
1959 }
1960 if (data->flag & LOCLIKE_Z) {
1961 cob->matrix[3][2] = ct->matrix[3][2];
1962
1963 if (data->flag & LOCLIKE_Z_INVERT) {
1964 cob->matrix[3][2] *= -1;
1965 }
1966 cob->matrix[3][2] += offset[2];
1967 }
1968 }
1969}
1970
1972 /*type*/ CONSTRAINT_TYPE_LOCLIKE,
1973 /*size*/ sizeof(bLocateLikeConstraint),
1974 /*name*/ N_("Copy Location"),
1975 /*struct_name*/ "bLocateLikeConstraint",
1976 /*free_data*/ nullptr,
1977 /*id_looper*/ loclike_id_looper,
1978 /*copy_data*/ nullptr,
1979 /*new_data*/ loclike_new_data,
1980 /*get_constraint_targets*/ loclike_get_tars,
1981 /*flush_constraint_targets*/ loclike_flush_tars,
1982 /*get_target_matrix*/ default_get_tarmat,
1983 /*evaluate_constraint*/ loclike_evaluate,
1984};
1985
1986/* ----------- Copy Rotation ------------- */
1987
1988static void rotlike_new_data(void *cdata)
1989{
1991
1992 data->flag = ROTLIKE_X | ROTLIKE_Y | ROTLIKE_Z;
1993}
1994
1995static void rotlike_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
1996{
1997 bRotateLikeConstraint *data = static_cast<bRotateLikeConstraint *>(con->data);
1998
1999 /* target only */
2000 func(con, (ID **)&data->tar, false, userdata);
2001}
2002
2004{
2005 if (con && list) {
2006 bRotateLikeConstraint *data = static_cast<bRotateLikeConstraint *>(con->data);
2008
2009 /* standard target-getting macro for single-target constraints */
2010 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list);
2011
2012 return 1;
2013 }
2014
2015 return 0;
2016}
2017
2018static void rotlike_flush_tars(bConstraint *con, ListBase *list, bool no_copy)
2019{
2020 if (con && list) {
2021 bRotateLikeConstraint *data = static_cast<bRotateLikeConstraint *>(con->data);
2022 bConstraintTarget *ct = static_cast<bConstraintTarget *>(list->first);
2023
2024 /* the following macro is used for all standard single-target constraints */
2025 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, no_copy);
2026 }
2027}
2028
2029static void rotlike_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *targets)
2030{
2031 bRotateLikeConstraint *data = static_cast<bRotateLikeConstraint *>(con->data);
2032 bConstraintTarget *ct = static_cast<bConstraintTarget *>(targets->first);
2033
2034 if (VALID_CONS_TARGET(ct)) {
2035 float loc[3], size[3], oldrot[3][3], newrot[3][3];
2036 float eul[3], obeul[3], defeul[3];
2037
2038 mat4_to_loc_rot_size(loc, oldrot, size, cob->matrix);
2039
2040 /* Select the Euler rotation order, defaulting to the owner. */
2041 short rot_order = cob->rotOrder;
2042
2043 if (data->euler_order != CONSTRAINT_EULER_AUTO) {
2044 rot_order = data->euler_order;
2045 }
2046
2047 /* To allow compatible rotations, must get both rotations in the order of the owner... */
2048 mat4_to_eulO(obeul, rot_order, cob->matrix);
2049 /* We must get compatible eulers from the beginning because
2050 * some of them can be modified below (see bug #21875).
2051 * Additionally, since this constraint is based on euler rotation math, it doesn't work well
2052 * with shear. The Y axis is chosen as the main axis when we orthogonalize the matrix because
2053 * constraints are used most commonly on bones. */
2054 float mat[4][4];
2055 copy_m4_m4(mat, ct->matrix);
2056 orthogonalize_m4_stable(mat, 1, true);
2057 mat4_to_compatible_eulO(eul, obeul, rot_order, mat);
2058
2059 /* Prepare the copied euler rotation. */
2060 bool legacy_offset = false;
2061
2062 switch (data->mix_mode) {
2063 case ROTLIKE_MIX_OFFSET:
2064 legacy_offset = true;
2065 copy_v3_v3(defeul, obeul);
2066 break;
2067
2069 copy_v3_v3(defeul, obeul);
2070 break;
2071
2072 default:
2073 zero_v3(defeul);
2074 }
2075
2076 if ((data->flag & ROTLIKE_X) == 0) {
2077 eul[0] = defeul[0];
2078 }
2079 else {
2080 if (legacy_offset) {
2081 rotate_eulO(eul, rot_order, 'X', obeul[0]);
2082 }
2083
2084 if (data->flag & ROTLIKE_X_INVERT) {
2085 eul[0] *= -1;
2086 }
2087 }
2088
2089 if ((data->flag & ROTLIKE_Y) == 0) {
2090 eul[1] = defeul[1];
2091 }
2092 else {
2093 if (legacy_offset) {
2094 rotate_eulO(eul, rot_order, 'Y', obeul[1]);
2095 }
2096
2097 if (data->flag & ROTLIKE_Y_INVERT) {
2098 eul[1] *= -1;
2099 }
2100 }
2101
2102 if ((data->flag & ROTLIKE_Z) == 0) {
2103 eul[2] = defeul[2];
2104 }
2105 else {
2106 if (legacy_offset) {
2107 rotate_eulO(eul, rot_order, 'Z', obeul[2]);
2108 }
2109
2110 if (data->flag & ROTLIKE_Z_INVERT) {
2111 eul[2] *= -1;
2112 }
2113 }
2114
2115 /* Add the euler components together if needed. */
2116 if (data->mix_mode == ROTLIKE_MIX_ADD) {
2117 add_v3_v3(eul, obeul);
2118 }
2119
2120 /* Good to make eulers compatible again,
2121 * since we don't know how much they were changed above. */
2122 compatible_eul(eul, obeul);
2123 eulO_to_mat3(newrot, eul, rot_order);
2124
2125 /* Mix the rotation matrices: */
2126 switch (data->mix_mode) {
2128 case ROTLIKE_MIX_OFFSET:
2129 case ROTLIKE_MIX_ADD:
2130 break;
2131
2132 case ROTLIKE_MIX_BEFORE:
2133 mul_m3_m3m3(newrot, newrot, oldrot);
2134 break;
2135
2136 case ROTLIKE_MIX_AFTER:
2137 mul_m3_m3m3(newrot, oldrot, newrot);
2138 break;
2139
2140 default:
2141 BLI_assert(false);
2142 }
2143
2144 loc_rot_size_to_mat4(cob->matrix, loc, newrot, size);
2145 }
2146}
2147
2149 /*type*/ CONSTRAINT_TYPE_ROTLIKE,
2150 /*size*/ sizeof(bRotateLikeConstraint),
2151 /*name*/ N_("Copy Rotation"),
2152 /*struct_name*/ "bRotateLikeConstraint",
2153 /*free_data*/ nullptr,
2154 /*id_looper*/ rotlike_id_looper,
2155 /*copy_data*/ nullptr,
2156 /*new_data*/ rotlike_new_data,
2157 /*get_constraint_targets*/ rotlike_get_tars,
2158 /*flush_constraint_targets*/ rotlike_flush_tars,
2159 /*get_target_matrix*/ default_get_tarmat,
2160 /*evaluate_constraint*/ rotlike_evaluate,
2161};
2162
2163/* ---------- Copy Scale ---------- */
2164
2165static void sizelike_new_data(void *cdata)
2166{
2168
2170 data->power = 1.0f;
2171}
2172
2173static void sizelike_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
2174{
2175 bSizeLikeConstraint *data = static_cast<bSizeLikeConstraint *>(con->data);
2176
2177 /* target only */
2178 func(con, (ID **)&data->tar, false, userdata);
2179}
2180
2182{
2183 if (con && list) {
2184 bSizeLikeConstraint *data = static_cast<bSizeLikeConstraint *>(con->data);
2186
2187 /* standard target-getting macro for single-target constraints */
2188 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list);
2189
2190 return 1;
2191 }
2192
2193 return 0;
2194}
2195
2196static void sizelike_flush_tars(bConstraint *con, ListBase *list, bool no_copy)
2197{
2198 if (con && list) {
2199 bSizeLikeConstraint *data = static_cast<bSizeLikeConstraint *>(con->data);
2200 bConstraintTarget *ct = static_cast<bConstraintTarget *>(list->first);
2201
2202 /* the following macro is used for all standard single-target constraints */
2203 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, no_copy);
2204 }
2205}
2206
2207static void sizelike_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *targets)
2208{
2209 bSizeLikeConstraint *data = static_cast<bSizeLikeConstraint *>(con->data);
2210 bConstraintTarget *ct = static_cast<bConstraintTarget *>(targets->first);
2211
2212 if (VALID_CONS_TARGET(ct)) {
2213 float obsize[3], size[3];
2214
2215 mat4_to_size(obsize, cob->matrix);
2216
2217 /* Compute one uniform scale factor to apply to all three axes. */
2218 if (data->flag & SIZELIKE_UNIFORM) {
2219 const int all_axes = SIZELIKE_X | SIZELIKE_Y | SIZELIKE_Z;
2220 float total = 1.0f;
2221
2222 /* If all axes are selected, use the determinant. */
2223 if ((data->flag & all_axes) == all_axes) {
2224 total = fabsf(mat4_to_volume_scale(ct->matrix));
2225 }
2226 /* Otherwise multiply individual values. */
2227 else {
2228 mat4_to_size(size, ct->matrix);
2229
2230 if (data->flag & SIZELIKE_X) {
2231 total *= size[0];
2232 }
2233 if (data->flag & SIZELIKE_Y) {
2234 total *= size[1];
2235 }
2236 if (data->flag & SIZELIKE_Z) {
2237 total *= size[2];
2238 }
2239 }
2240
2241 copy_v3_fl(size, cbrt(total));
2242 }
2243 /* Regular per-axis scaling. */
2244 else {
2245 mat4_to_size(size, ct->matrix);
2246 }
2247
2248 for (int i = 0; i < 3; i++) {
2249 size[i] = powf(size[i], data->power);
2250 }
2251
2252 if (data->flag & SIZELIKE_OFFSET) {
2253 /* Scale is a multiplicative quantity, so adding it makes no sense.
2254 * However, the additive mode has to stay for backward compatibility. */
2255 if (data->flag & SIZELIKE_MULTIPLY) {
2256 /* size[i] *= obsize[i] */
2257 mul_v3_v3(size, obsize);
2258 }
2259 else {
2260 /* 2.7 compatibility mode: size[i] += (obsize[i] - 1.0f) */
2261 add_v3_v3(size, obsize);
2262 add_v3_fl(size, -1.0f);
2263 }
2264 }
2265
2266 if ((data->flag & (SIZELIKE_X | SIZELIKE_UNIFORM)) && (obsize[0] != 0)) {
2267 mul_v3_fl(cob->matrix[0], size[0] / obsize[0]);
2268 }
2269 if ((data->flag & (SIZELIKE_Y | SIZELIKE_UNIFORM)) && (obsize[1] != 0)) {
2270 mul_v3_fl(cob->matrix[1], size[1] / obsize[1]);
2271 }
2272 if ((data->flag & (SIZELIKE_Z | SIZELIKE_UNIFORM)) && (obsize[2] != 0)) {
2273 mul_v3_fl(cob->matrix[2], size[2] / obsize[2]);
2274 }
2275 }
2276}
2277
2279 /*type*/ CONSTRAINT_TYPE_SIZELIKE,
2280 /*size*/ sizeof(bSizeLikeConstraint),
2281 /*name*/ N_("Copy Scale"),
2282 /*struct_name*/ "bSizeLikeConstraint",
2283 /*free_data*/ nullptr,
2284 /*id_looper*/ sizelike_id_looper,
2285 /*copy_data*/ nullptr,
2286 /*new_data*/ sizelike_new_data,
2287 /*get_constraint_targets*/ sizelike_get_tars,
2288 /*flush_constraint_targets*/ sizelike_flush_tars,
2289 /*get_target_matrix*/ default_get_tarmat,
2290 /*evaluate_constraint*/ sizelike_evaluate,
2291};
2292
2293/* ----------- Copy Transforms ------------- */
2294
2295static void translike_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
2296{
2297 bTransLikeConstraint *data = static_cast<bTransLikeConstraint *>(con->data);
2298
2299 /* target only */
2300 func(con, (ID **)&data->tar, false, userdata);
2301}
2302
2304{
2305 if (con && list) {
2306 bTransLikeConstraint *data = static_cast<bTransLikeConstraint *>(con->data);
2308
2309 /* standard target-getting macro for single-target constraints */
2310 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list);
2311
2312 return 1;
2313 }
2314
2315 return 0;
2316}
2317
2318static void translike_flush_tars(bConstraint *con, ListBase *list, bool no_copy)
2319{
2320 if (con && list) {
2321 bTransLikeConstraint *data = static_cast<bTransLikeConstraint *>(con->data);
2322 bConstraintTarget *ct = static_cast<bConstraintTarget *>(list->first);
2323
2324 /* the following macro is used for all standard single-target constraints */
2325 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, no_copy);
2326 }
2327}
2328
2329static void translike_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *targets)
2330{
2331 bTransLikeConstraint *data = static_cast<bTransLikeConstraint *>(con->data);
2332 bConstraintTarget *ct = static_cast<bConstraintTarget *>(targets->first);
2333
2334 if (VALID_CONS_TARGET(ct)) {
2335 float target_mat[4][4];
2336
2337 copy_m4_m4(target_mat, ct->matrix);
2338
2339 /* Remove the shear of the target matrix if enabled.
2340 * Use Y as the axis since it's the natural default for bones. */
2341 if (data->flag & TRANSLIKE_REMOVE_TARGET_SHEAR) {
2342 orthogonalize_m4_stable(target_mat, 1, false);
2343 }
2344
2345 /* Finally, combine the matrices. */
2346 switch (data->mix_mode) {
2348 copy_m4_m4(cob->matrix, target_mat);
2349 break;
2350
2351 /* Simple matrix multiplication. */
2353 mul_m4_m4m4(cob->matrix, target_mat, cob->matrix);
2354 break;
2355
2357 mul_m4_m4m4(cob->matrix, cob->matrix, target_mat);
2358 break;
2359
2360 /* Aligned Inherit Scale emulation. */
2362 mul_m4_m4m4_aligned_scale(cob->matrix, target_mat, cob->matrix);
2363 break;
2364
2366 mul_m4_m4m4_aligned_scale(cob->matrix, cob->matrix, target_mat);
2367 break;
2368
2369 /* Fully separate handling of channels. */
2371 mul_m4_m4m4_split_channels(cob->matrix, target_mat, cob->matrix);
2372 break;
2373
2375 mul_m4_m4m4_split_channels(cob->matrix, cob->matrix, target_mat);
2376 break;
2377
2378 default:
2379 BLI_assert_msg(0, "Unknown Copy Transforms mix mode");
2380 }
2381 }
2382}
2383
2386 /*size*/ sizeof(bTransLikeConstraint),
2387 /*name*/ N_("Copy Transforms"),
2388 /*struct_name*/ "bTransLikeConstraint",
2389 /*free_data*/ nullptr,
2390 /*id_looper*/ translike_id_looper,
2391 /*copy_data*/ nullptr,
2392 /*new_data*/ nullptr,
2393 /*get_constraint_targets*/ translike_get_tars,
2394 /*flush_constraint_targets*/ translike_flush_tars,
2395 /*get_target_matrix*/ default_get_tarmat_full_bbone,
2396 /*evaluate_constraint*/ translike_evaluate,
2397};
2398
2399/* ---------- Maintain Volume ---------- */
2400
2401static void samevolume_new_data(void *cdata)
2402{
2404
2405 data->free_axis = SAMEVOL_Y;
2406 data->volume = 1.0f;
2407}
2408
2409static void samevolume_evaluate(bConstraint *con, bConstraintOb *cob, ListBase * /*targets*/)
2410{
2411 bSameVolumeConstraint *data = static_cast<bSameVolumeConstraint *>(con->data);
2412
2413 float volume = data->volume;
2414 float fac = 1.0f, total_scale = 1.0f;
2415 float obsize[3];
2416
2417 mat4_to_size(obsize, cob->matrix);
2418
2419 /* calculate normalizing scale factor for non-essential values */
2420 switch (data->mode) {
2421 case SAMEVOL_STRICT:
2422 total_scale = obsize[0] * obsize[1] * obsize[2];
2423 break;
2424 case SAMEVOL_UNIFORM:
2425 total_scale = pow3f(obsize[data->free_axis]);
2426 break;
2428 total_scale = obsize[data->free_axis];
2429 break;
2430 }
2431
2432 if (total_scale != 0) {
2433 fac = sqrtf(volume / total_scale);
2434 }
2435
2436 /* apply scaling factor to the channels not being kept */
2437 switch (data->free_axis) {
2438 case SAMEVOL_X:
2439 mul_v3_fl(cob->matrix[1], fac);
2440 mul_v3_fl(cob->matrix[2], fac);
2441 break;
2442 case SAMEVOL_Y:
2443 mul_v3_fl(cob->matrix[0], fac);
2444 mul_v3_fl(cob->matrix[2], fac);
2445 break;
2446 case SAMEVOL_Z:
2447 mul_v3_fl(cob->matrix[0], fac);
2448 mul_v3_fl(cob->matrix[1], fac);
2449 break;
2450 }
2451}
2452
2454 /*type*/ CONSTRAINT_TYPE_SAMEVOL,
2455 /*size*/ sizeof(bSameVolumeConstraint),
2456 /*name*/ N_("Maintain Volume"),
2457 /*struct_name*/ "bSameVolumeConstraint",
2458 /*free_data*/ nullptr,
2459 /*id_looper*/ nullptr,
2460 /*copy_data*/ nullptr,
2461 /*new_data*/ samevolume_new_data,
2462 /*get_constraint_targets*/ nullptr,
2463 /*flush_constraint_targets*/ nullptr,
2464 /*get_target_matrix*/ nullptr,
2465 /*evaluate_constraint*/ samevolume_evaluate,
2466};
2467
2468/* ----------- Armature Constraint -------------- */
2469
2470static void armdef_free(bConstraint *con)
2471{
2472 bArmatureConstraint *data = static_cast<bArmatureConstraint *>(con->data);
2473
2474 /* Target list. */
2475 BLI_freelistN(&data->targets);
2476}
2477
2478static void armdef_copy(bConstraint *con, bConstraint *srccon)
2479{
2481 bArmatureConstraint *opcon = (bArmatureConstraint *)srccon->data;
2482
2483 BLI_duplicatelist(&pcon->targets, &opcon->targets);
2484}
2485
2486static int armdef_get_tars(bConstraint *con, ListBase *list)
2487{
2488 if (con && list) {
2489 bArmatureConstraint *data = static_cast<bArmatureConstraint *>(con->data);
2490
2491 *list = data->targets;
2492
2493 return BLI_listbase_count(&data->targets);
2494 }
2495
2496 return 0;
2497}
2498
2499static void armdef_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
2500{
2501 bArmatureConstraint *data = static_cast<bArmatureConstraint *>(con->data);
2502
2503 /* Target list. */
2504 LISTBASE_FOREACH (bConstraintTarget *, ct, &data->targets) {
2505 func(con, (ID **)&ct->tar, false, userdata);
2506 }
2507}
2508
2509/* Compute the world space pose matrix of the target bone. */
2510static bool armdef_get_tarmat(Depsgraph * /*depsgraph*/,
2511 bConstraint * /*con*/,
2512 bConstraintOb * /*cob*/,
2514 float /*ctime*/)
2515{
2516 if (!VALID_CONS_TARGET(ct) || ct->tar->type != OB_ARMATURE) {
2518 return false;
2519 }
2520
2522 if (pchan == nullptr) {
2523 unit_m4(ct->matrix);
2524 return false;
2525 }
2526
2527 mul_m4_m4m4(ct->matrix, ct->tar->object_to_world().ptr(), pchan->pose_mat);
2528 return true;
2529}
2530
2531static void armdef_accumulate_matrix(const float obmat[4][4],
2532 const float iobmat[4][4],
2533 const float basemat[4][4],
2534 const float bonemat[4][4],
2535 const float pivot[3],
2536 const float weight,
2537 float r_sum_mat[4][4],
2538 DualQuat *r_sum_dq)
2539{
2540 if (weight == 0.0f) {
2541 return;
2542 }
2543
2544 /* Convert the selected matrix into object space. */
2545 float mat[4][4];
2546 mul_m4_series(mat, obmat, bonemat, iobmat);
2547
2548 /* Accumulate the transformation. */
2549 if (r_sum_dq != nullptr) {
2550 float basemat_world[4][4];
2551 DualQuat tmpdq;
2552
2553 /* Compute the orthonormal rest matrix in world space. */
2554 mul_m4_m4m4(basemat_world, obmat, basemat);
2555 orthogonalize_m4_stable(basemat_world, 1, true);
2556
2557 mat4_to_dquat(&tmpdq, basemat_world, mat);
2558 add_weighted_dq_dq_pivot(r_sum_dq, &tmpdq, pivot, weight, true);
2559 }
2560 else {
2561 madd_m4_m4m4fl(r_sum_mat, r_sum_mat, mat, weight);
2562 }
2563}
2564
2565/* Compute and accumulate transformation for a single target bone. */
2567 const bPoseChannel *pchan,
2568 const float wco[3],
2569 const bool force_envelope,
2570 float *r_totweight,
2571 float r_sum_mat[4][4],
2572 DualQuat *r_sum_dq)
2573{
2574 float iobmat[4][4], co[3];
2575 const Bone *bone = pchan->bone;
2576 float weight = ct->weight;
2577
2578 /* Our object's location in target pose space. */
2579 invert_m4_m4(iobmat, ct->tar->object_to_world().ptr());
2580 mul_v3_m4v3(co, iobmat, wco);
2581
2582 /* Multiply by the envelope weight when appropriate. */
2583 if (force_envelope || (bone->flag & BONE_MULT_VG_ENV)) {
2584 weight *= distfactor_to_bone(
2585 co, bone->arm_head, bone->arm_tail, bone->rad_head, bone->rad_tail, bone->dist);
2586 }
2587
2588 /* Find the correct bone transform matrix in world space. */
2589 if (bone->segments > 1 && bone->segments == pchan->runtime.bbone_segments) {
2590 const Mat4 *b_bone_mats = pchan->runtime.bbone_deform_mats;
2591 const Mat4 *b_bone_rest_mats = pchan->runtime.bbone_rest_mats;
2592 float basemat[4][4];
2593
2594 /* Blend the matrix. */
2595 int index;
2596 float blend;
2597 BKE_pchan_bbone_deform_segment_index(pchan, co, &index, &blend);
2598
2599 if (r_sum_dq != nullptr) {
2600 /* Compute the object space rest matrix of the segment. */
2601 mul_m4_m4m4(basemat, bone->arm_mat, b_bone_rest_mats[index].mat);
2602 }
2603
2604 armdef_accumulate_matrix(ct->tar->object_to_world().ptr(),
2605 iobmat,
2606 basemat,
2607 b_bone_mats[index + 1].mat,
2608 wco,
2609 weight * (1.0f - blend),
2610 r_sum_mat,
2611 r_sum_dq);
2612
2613 if (r_sum_dq != nullptr) {
2614 /* Compute the object space rest matrix of the segment. */
2615 mul_m4_m4m4(basemat, bone->arm_mat, b_bone_rest_mats[index + 1].mat);
2616 }
2617
2618 armdef_accumulate_matrix(ct->tar->object_to_world().ptr(),
2619 iobmat,
2620 basemat,
2621 b_bone_mats[index + 2].mat,
2622 wco,
2623 weight * blend,
2624 r_sum_mat,
2625 r_sum_dq);
2626 }
2627 else {
2628 /* Simple bone. This requires DEG_OPCODE_BONE_DONE dependency due to chan_mat. */
2629 armdef_accumulate_matrix(ct->tar->object_to_world().ptr(),
2630 iobmat,
2631 bone->arm_mat,
2632 pchan->chan_mat,
2633 wco,
2634 weight,
2635 r_sum_mat,
2636 r_sum_dq);
2637 }
2638
2639 /* Accumulate the weight. */
2640 *r_totweight += weight;
2641}
2642
2643static void armdef_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *targets)
2644{
2645 bArmatureConstraint *data = static_cast<bArmatureConstraint *>(con->data);
2646
2647 /* Prepare for blending. */
2648 float sum_mat[4][4] = {};
2649 DualQuat sum_dq = {};
2650 float weight = 0.0f;
2651
2652 DualQuat *pdq = (data->flag & CONSTRAINT_ARMATURE_QUATERNION) ? &sum_dq : nullptr;
2653 bool use_envelopes = (data->flag & CONSTRAINT_ARMATURE_ENVELOPE) != 0;
2654
2655 float input_co[3];
2656 if (cob->pchan && cob->pchan->bone && !(data->flag & CONSTRAINT_ARMATURE_CUR_LOCATION)) {
2657 /* For constraints on bones, use the rest position to bind b-bone segments
2658 * and envelopes, to allow safely changing the bone location as if parented. */
2659 copy_v3_v3(input_co, cob->pchan->bone->arm_head);
2660 mul_m4_v3(cob->ob->object_to_world().ptr(), input_co);
2661 }
2662 else {
2663 copy_v3_v3(input_co, cob->matrix[3]);
2664 }
2665
2666 /* Process all targets. This can't use ct->matrix, as armdef_get_tarmat is not
2667 * called in solve for efficiency because the constraint needs bone data anyway. */
2668 LISTBASE_FOREACH (bConstraintTarget *, ct, targets) {
2669 if (ct->weight <= 0.0f) {
2670 continue;
2671 }
2672
2673 /* Lookup the bone and abort if failed. */
2674 if (!VALID_CONS_TARGET(ct) || ct->tar->type != OB_ARMATURE) {
2675 return;
2676 }
2677
2678 bPoseChannel *pchan = BKE_pose_channel_find_name(ct->tar->pose, ct->subtarget);
2679
2680 if (pchan == nullptr || pchan->bone == nullptr) {
2681 return;
2682 }
2683
2684 armdef_accumulate_bone(ct, pchan, input_co, use_envelopes, &weight, sum_mat, pdq);
2685 }
2686
2687 /* Compute the final transform. */
2688 if (weight > 0.0f) {
2689 if (pdq != nullptr) {
2690 normalize_dq(pdq, weight);
2691 dquat_to_mat4(sum_mat, pdq);
2692 }
2693 else {
2694 mul_m4_fl(sum_mat, 1.0f / weight);
2695 }
2696
2697 /* Apply the transform to the result matrix. */
2698 mul_m4_m4m4(cob->matrix, sum_mat, cob->matrix);
2699 }
2700}
2701
2703 /*type*/ CONSTRAINT_TYPE_ARMATURE,
2704 /*size*/ sizeof(bArmatureConstraint),
2705 /*name*/ N_("Armature"),
2706 /*struct_name*/ "bArmatureConstraint",
2707 /*free_data*/ armdef_free,
2708 /*id_looper*/ armdef_id_looper,
2709 /*copy_data*/ armdef_copy,
2710 /*new_data*/ nullptr,
2711 /*get_constraint_targets*/ armdef_get_tars,
2712 /*flush_constraint_targets*/ nullptr,
2713 /*get_target_matrix*/ armdef_get_tarmat,
2714 /*evaluate_constraint*/ armdef_evaluate,
2715};
2716
2717/* -------- Action Constraint ----------- */
2718
2719static void actcon_new_data(void *cdata)
2720{
2722
2723 /* set type to 20 (Loc X), as 0 is Rot X for backwards compatibility */
2724 data->type = 20;
2725
2726 /* Set the mix mode to After Original with anti-shear scale handling. */
2727 data->mix_mode = ACTCON_MIX_AFTER;
2728}
2729
2730static void actcon_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
2731{
2732 bActionConstraint *data = static_cast<bActionConstraint *>(con->data);
2733
2734 /* target */
2735 func(con, (ID **)&data->tar, false, userdata);
2736
2737 /* action */
2738 func(con, (ID **)&data->act, true, userdata);
2739}
2740
2741static int actcon_get_tars(bConstraint *con, ListBase *list)
2742{
2743 if (con && list) {
2744 bActionConstraint *data = static_cast<bActionConstraint *>(con->data);
2746
2747 /* standard target-getting macro for single-target constraints */
2748 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list);
2749
2750 return 1;
2751 }
2752
2753 return 0;
2754}
2755
2756static void actcon_flush_tars(bConstraint *con, ListBase *list, bool no_copy)
2757{
2758 if (con && list) {
2759 bActionConstraint *data = static_cast<bActionConstraint *>(con->data);
2760 bConstraintTarget *ct = static_cast<bConstraintTarget *>(list->first);
2761
2762 /* the following macro is used for all standard single-target constraints */
2763 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, no_copy);
2764 }
2765}
2766
2767static bool actcon_get_tarmat(Depsgraph *depsgraph,
2768 bConstraint *con,
2769 bConstraintOb *cob,
2771 float /*ctime*/)
2772{
2773 bActionConstraint *data = static_cast<bActionConstraint *>(con->data);
2774
2775 /* Initialize return matrix. This needs to happen even when there is no
2776 * Action, to avoid returning an all-zeroes matrix. */
2777 unit_m4(ct->matrix);
2778
2779 if (!data->act) {
2780 /* Without an Action, this constraint cannot do anything. */
2781 return false;
2782 }
2783
2784 const bool use_eval_time = data->flag & ACTCON_USE_EVAL_TIME;
2785 if (!VALID_CONS_TARGET(ct) && !use_eval_time) {
2786 return false;
2787 }
2788
2789 float tempmat[4][4], vec[3];
2790 float s, t;
2791 short axis;
2792
2793 /* Skip targets if we're using local float property to set action time */
2794 if (use_eval_time) {
2795 s = data->eval_time;
2796 }
2797 else {
2798 /* get the transform matrix of the target */
2800 ct->subtarget,
2801 cob,
2802 tempmat,
2804 ct->space,
2805 con->flag,
2806 con->headtail);
2807
2808 /* determine where in transform range target is */
2809 /* data->type is mapped as follows for backwards compatibility:
2810 * 00,01,02 - rotation (it used to be like this)
2811 * 10,11,12 - scaling
2812 * 20,21,22 - location
2813 */
2814 if (data->type < 10) {
2815 /* extract rotation (is in whatever space target should be in) */
2816 mat4_to_eul(vec, tempmat);
2817 mul_v3_fl(vec, RAD2DEGF(1.0f)); /* rad -> deg */
2818 axis = data->type;
2819 }
2820 else if (data->type < 20) {
2821 /* extract scaling (is in whatever space target should be in) */
2822 mat4_to_size(vec, tempmat);
2823 axis = data->type - 10;
2824 }
2825 else {
2826 /* extract location */
2827 copy_v3_v3(vec, tempmat[3]);
2828 axis = data->type - 20;
2829 }
2830
2831 BLI_assert(uint(axis) < 3);
2832
2833 /* Convert the target's value into a [0, 1] value that's later used to find the Action frame
2834 * to apply. This compares to the min/max boundary values first, before doing the
2835 * normalization by the (max-min) range, to get predictable, valid values when that range is
2836 * zero. */
2837 const float range = data->max - data->min;
2838 if ((range == 0.0f) || (ushort(axis) > 2)) {
2839 s = 0.0f;
2840 }
2841 else {
2842 s = (vec[axis] - data->min) / range;
2843 }
2844 }
2845
2846 CLAMP(s, 0, 1);
2847 t = (s * (data->end - data->start)) + data->start;
2849
2850 if (G.debug & G_DEBUG) {
2851 printf("do Action Constraint %s - Ob %s Pchan %s\n",
2852 con->name,
2853 cob->ob->id.name + 2,
2854 (cob->pchan) ? cob->pchan->name : nullptr);
2855 }
2856
2857 /* Get the appropriate information from the action */
2859 Object workob;
2860
2861 /* evaluate using workob */
2862 /* FIXME: we don't have any consistent standards on limiting effects on object... */
2864 &workob,
2865 nullptr,
2866 data->act,
2867 data->action_slot_handle,
2868 nullptr,
2869 &anim_eval_context);
2870 BKE_object_to_mat4(&workob, ct->matrix);
2871 }
2872 else if (cob->type == CONSTRAINT_OBTYPE_BONE) {
2873 Object workob;
2874 bPose pose = {{nullptr}};
2875 bPoseChannel *pchan, *tchan;
2876
2877 /* make a copy of the bone of interest in the temp pose before evaluating action,
2878 * so that it can get set - we need to manually copy over a few settings,
2879 * including rotation order, otherwise this fails. */
2880 pchan = cob->pchan;
2881
2882 tchan = BKE_pose_channel_ensure(&pose, pchan->name);
2883 tchan->rotmode = pchan->rotmode;
2884
2885 /* evaluate action using workob (it will only set the PoseChannel in question) */
2887 &workob,
2888 &pose,
2889 data->act,
2890 data->action_slot_handle,
2891 pchan->name,
2892 &anim_eval_context);
2893
2894 /* convert animation to matrices for use here */
2895 BKE_pchan_calc_mat(tchan);
2896 copy_m4_m4(ct->matrix, tchan->chan_mat);
2897
2898 /* Clean up */
2899 BKE_pose_free_data(&pose);
2900 }
2901 else {
2902 /* behavior undefined... */
2903 puts("Error: unknown owner type for Action Constraint");
2904 return false;
2905 }
2906
2907 return true;
2908}
2909
2910static void actcon_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *targets)
2911{
2912 bActionConstraint *data = static_cast<bActionConstraint *>(con->data);
2913 bConstraintTarget *ct = static_cast<bConstraintTarget *>(targets->first);
2914
2915 if (VALID_CONS_TARGET(ct) || data->flag & ACTCON_USE_EVAL_TIME) {
2916 switch (data->mix_mode) {
2917 /* Replace the input transformation. */
2918 case ACTCON_MIX_REPLACE:
2919 copy_m4_m4(cob->matrix, ct->matrix);
2920 break;
2921
2922 /* Simple matrix multiplication. */
2924 mul_m4_m4m4(cob->matrix, ct->matrix, cob->matrix);
2925 break;
2926
2928 mul_m4_m4m4(cob->matrix, cob->matrix, ct->matrix);
2929 break;
2930
2931 /* Aligned Inherit Scale emulation. */
2932 case ACTCON_MIX_BEFORE:
2934 break;
2935
2936 case ACTCON_MIX_AFTER:
2938 break;
2939
2940 /* Fully separate handling of channels. */
2943 break;
2944
2947 break;
2948
2949 default:
2950 BLI_assert_msg(0, "Unknown Action mix mode");
2951 }
2952 }
2953}
2954
2956 /*type*/ CONSTRAINT_TYPE_ACTION,
2957 /*size*/ sizeof(bActionConstraint),
2958 /*name*/ N_("Action"),
2959 /*struct_name*/ "bActionConstraint",
2960 /*free_data*/ nullptr,
2961 /*id_looper*/ actcon_id_looper,
2962 /*copy_data*/ nullptr,
2963 /*new_data*/ actcon_new_data,
2964 /*get_constraint_targets*/ actcon_get_tars,
2965 /*flush_constraint_targets*/ actcon_flush_tars,
2966 /*get_target_matrix*/ actcon_get_tarmat,
2967 /*evaluate_constraint*/ actcon_evaluate,
2968};
2969
2970/* --------- Locked Track ---------- */
2971
2972static void locktrack_new_data(void *cdata)
2973{
2975
2976 data->trackflag = TRACK_Y;
2977 data->lockflag = LOCK_Z;
2978}
2979
2980static void locktrack_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
2981{
2982 bLockTrackConstraint *data = static_cast<bLockTrackConstraint *>(con->data);
2983
2984 /* target only */
2985 func(con, (ID **)&data->tar, false, userdata);
2986}
2987
2989{
2990 if (con && list) {
2991 bLockTrackConstraint *data = static_cast<bLockTrackConstraint *>(con->data);
2993
2994 /* the following macro is used for all standard single-target constraints */
2995 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list);
2996
2997 return 1;
2998 }
2999
3000 return 0;
3001}
3002
3003static void locktrack_flush_tars(bConstraint *con, ListBase *list, bool no_copy)
3004{
3005 if (con && list) {
3006 bLockTrackConstraint *data = static_cast<bLockTrackConstraint *>(con->data);
3007 bConstraintTarget *ct = static_cast<bConstraintTarget *>(list->first);
3008
3009 /* the following macro is used for all standard single-target constraints */
3010 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, no_copy);
3011 }
3012}
3013
3014static void locktrack_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *targets)
3015{
3016 bLockTrackConstraint *data = static_cast<bLockTrackConstraint *>(con->data);
3017 bConstraintTarget *ct = static_cast<bConstraintTarget *>(targets->first);
3018
3019 if (VALID_CONS_TARGET(ct)) {
3020 float vec[3], vec2[3];
3021 float totmat[3][3];
3022 float tmpmat[3][3];
3023 float invmat[3][3];
3024 float mdet;
3025
3026 /* Vector object -> target */
3027 sub_v3_v3v3(vec, ct->matrix[3], cob->matrix[3]);
3028 switch (data->lockflag) {
3029 case LOCK_X: /* LOCK X */
3030 {
3031 switch (data->trackflag) {
3032 case TRACK_Y: /* LOCK X TRACK Y */
3033 {
3034 /* Projection of Vector on the plane */
3035 project_v3_v3v3(vec2, vec, cob->matrix[0]);
3036 sub_v3_v3v3(totmat[1], vec, vec2);
3037 normalize_v3(totmat[1]);
3038
3039 /* the x axis is fixed */
3040 normalize_v3_v3(totmat[0], cob->matrix[0]);
3041
3042 /* the z axis gets mapped onto a third orthogonal vector */
3043 cross_v3_v3v3(totmat[2], totmat[0], totmat[1]);
3044 break;
3045 }
3046 case TRACK_Z: /* LOCK X TRACK Z */
3047 {
3048 /* Projection of Vector on the plane */
3049 project_v3_v3v3(vec2, vec, cob->matrix[0]);
3050 sub_v3_v3v3(totmat[2], vec, vec2);
3051 normalize_v3(totmat[2]);
3052
3053 /* the x axis is fixed */
3054 normalize_v3_v3(totmat[0], cob->matrix[0]);
3055
3056 /* the z axis gets mapped onto a third orthogonal vector */
3057 cross_v3_v3v3(totmat[1], totmat[2], totmat[0]);
3058 break;
3059 }
3060 case TRACK_nY: /* LOCK X TRACK -Y */
3061 {
3062 /* Projection of Vector on the plane */
3063 project_v3_v3v3(vec2, vec, cob->matrix[0]);
3064 sub_v3_v3v3(totmat[1], vec, vec2);
3065 normalize_v3(totmat[1]);
3066 negate_v3(totmat[1]);
3067
3068 /* the x axis is fixed */
3069 normalize_v3_v3(totmat[0], cob->matrix[0]);
3070
3071 /* the z axis gets mapped onto a third orthogonal vector */
3072 cross_v3_v3v3(totmat[2], totmat[0], totmat[1]);
3073 break;
3074 }
3075 case TRACK_nZ: /* LOCK X TRACK -Z */
3076 {
3077 /* Projection of Vector on the plane */
3078 project_v3_v3v3(vec2, vec, cob->matrix[0]);
3079 sub_v3_v3v3(totmat[2], vec, vec2);
3080 normalize_v3(totmat[2]);
3081 negate_v3(totmat[2]);
3082
3083 /* the x axis is fixed */
3084 normalize_v3_v3(totmat[0], cob->matrix[0]);
3085
3086 /* the z axis gets mapped onto a third orthogonal vector */
3087 cross_v3_v3v3(totmat[1], totmat[2], totmat[0]);
3088 break;
3089 }
3090 default: {
3091 unit_m3(totmat);
3092 break;
3093 }
3094 }
3095 break;
3096 }
3097 case LOCK_Y: /* LOCK Y */
3098 {
3099 switch (data->trackflag) {
3100 case TRACK_X: /* LOCK Y TRACK X */
3101 {
3102 /* Projection of Vector on the plane */
3103 project_v3_v3v3(vec2, vec, cob->matrix[1]);
3104 sub_v3_v3v3(totmat[0], vec, vec2);
3105 normalize_v3(totmat[0]);
3106
3107 /* the y axis is fixed */
3108 normalize_v3_v3(totmat[1], cob->matrix[1]);
3109
3110 /* the z axis gets mapped onto a third orthogonal vector */
3111 cross_v3_v3v3(totmat[2], totmat[0], totmat[1]);
3112 break;
3113 }
3114 case TRACK_Z: /* LOCK Y TRACK Z */
3115 {
3116 /* Projection of Vector on the plane */
3117 project_v3_v3v3(vec2, vec, cob->matrix[1]);
3118 sub_v3_v3v3(totmat[2], vec, vec2);
3119 normalize_v3(totmat[2]);
3120
3121 /* the y axis is fixed */
3122 normalize_v3_v3(totmat[1], cob->matrix[1]);
3123
3124 /* the z axis gets mapped onto a third orthogonal vector */
3125 cross_v3_v3v3(totmat[0], totmat[1], totmat[2]);
3126 break;
3127 }
3128 case TRACK_nX: /* LOCK Y TRACK -X */
3129 {
3130 /* Projection of Vector on the plane */
3131 project_v3_v3v3(vec2, vec, cob->matrix[1]);
3132 sub_v3_v3v3(totmat[0], vec, vec2);
3133 normalize_v3(totmat[0]);
3134 negate_v3(totmat[0]);
3135
3136 /* the y axis is fixed */
3137 normalize_v3_v3(totmat[1], cob->matrix[1]);
3138
3139 /* the z axis gets mapped onto a third orthogonal vector */
3140 cross_v3_v3v3(totmat[2], totmat[0], totmat[1]);
3141 break;
3142 }
3143 case TRACK_nZ: /* LOCK Y TRACK -Z */
3144 {
3145 /* Projection of Vector on the plane */
3146 project_v3_v3v3(vec2, vec, cob->matrix[1]);
3147 sub_v3_v3v3(totmat[2], vec, vec2);
3148 normalize_v3(totmat[2]);
3149 negate_v3(totmat[2]);
3150
3151 /* the y axis is fixed */
3152 normalize_v3_v3(totmat[1], cob->matrix[1]);
3153
3154 /* the z axis gets mapped onto a third orthogonal vector */
3155 cross_v3_v3v3(totmat[0], totmat[1], totmat[2]);
3156 break;
3157 }
3158 default: {
3159 unit_m3(totmat);
3160 break;
3161 }
3162 }
3163 break;
3164 }
3165 case LOCK_Z: /* LOCK Z */
3166 {
3167 switch (data->trackflag) {
3168 case TRACK_X: /* LOCK Z TRACK X */
3169 {
3170 /* Projection of Vector on the plane */
3171 project_v3_v3v3(vec2, vec, cob->matrix[2]);
3172 sub_v3_v3v3(totmat[0], vec, vec2);
3173 normalize_v3(totmat[0]);
3174
3175 /* the z axis is fixed */
3176 normalize_v3_v3(totmat[2], cob->matrix[2]);
3177
3178 /* the x axis gets mapped onto a third orthogonal vector */
3179 cross_v3_v3v3(totmat[1], totmat[2], totmat[0]);
3180 break;
3181 }
3182 case TRACK_Y: /* LOCK Z TRACK Y */
3183 {
3184 /* Projection of Vector on the plane */
3185 project_v3_v3v3(vec2, vec, cob->matrix[2]);
3186 sub_v3_v3v3(totmat[1], vec, vec2);
3187 normalize_v3(totmat[1]);
3188
3189 /* the z axis is fixed */
3190 normalize_v3_v3(totmat[2], cob->matrix[2]);
3191
3192 /* the x axis gets mapped onto a third orthogonal vector */
3193 cross_v3_v3v3(totmat[0], totmat[1], totmat[2]);
3194 break;
3195 }
3196 case TRACK_nX: /* LOCK Z TRACK -X */
3197 {
3198 /* Projection of Vector on the plane */
3199 project_v3_v3v3(vec2, vec, cob->matrix[2]);
3200 sub_v3_v3v3(totmat[0], vec, vec2);
3201 normalize_v3(totmat[0]);
3202 negate_v3(totmat[0]);
3203
3204 /* the z axis is fixed */
3205 normalize_v3_v3(totmat[2], cob->matrix[2]);
3206
3207 /* the x axis gets mapped onto a third orthogonal vector */
3208 cross_v3_v3v3(totmat[1], totmat[2], totmat[0]);
3209 break;
3210 }
3211 case TRACK_nY: /* LOCK Z TRACK -Y */
3212 {
3213 /* Projection of Vector on the plane */
3214 project_v3_v3v3(vec2, vec, cob->matrix[2]);
3215 sub_v3_v3v3(totmat[1], vec, vec2);
3216 normalize_v3(totmat[1]);
3217 negate_v3(totmat[1]);
3218
3219 /* the z axis is fixed */
3220 normalize_v3_v3(totmat[2], cob->matrix[2]);
3221
3222 /* the x axis gets mapped onto a third orthogonal vector */
3223 cross_v3_v3v3(totmat[0], totmat[1], totmat[2]);
3224 break;
3225 }
3226 default: {
3227 unit_m3(totmat);
3228 break;
3229 }
3230 }
3231 break;
3232 }
3233 default: {
3234 unit_m3(totmat);
3235 break;
3236 }
3237 }
3238 /* Block to keep matrix heading */
3239 copy_m3_m4(tmpmat, cob->matrix);
3240 normalize_m3(tmpmat);
3241 invert_m3_m3(invmat, tmpmat);
3242 mul_m3_m3m3(tmpmat, totmat, invmat);
3243 totmat[0][0] = tmpmat[0][0];
3244 totmat[0][1] = tmpmat[0][1];
3245 totmat[0][2] = tmpmat[0][2];
3246 totmat[1][0] = tmpmat[1][0];
3247 totmat[1][1] = tmpmat[1][1];
3248 totmat[1][2] = tmpmat[1][2];
3249 totmat[2][0] = tmpmat[2][0];
3250 totmat[2][1] = tmpmat[2][1];
3251 totmat[2][2] = tmpmat[2][2];
3252
3253 mdet = determinant_m3(totmat[0][0],
3254 totmat[0][1],
3255 totmat[0][2],
3256 totmat[1][0],
3257 totmat[1][1],
3258 totmat[1][2],
3259 totmat[2][0],
3260 totmat[2][1],
3261 totmat[2][2]);
3262 if (mdet == 0) {
3263 unit_m3(totmat);
3264 }
3265
3266 /* apply out transformation to the object */
3267 mul_m4_m3m4(cob->matrix, totmat, cob->matrix);
3268 }
3269}
3270
3273 /*size*/ sizeof(bLockTrackConstraint),
3274 /*name*/ N_("Locked Track"),
3275 /*struct_name*/ "bLockTrackConstraint",
3276 /*free_data*/ nullptr,
3277 /*id_looper*/ locktrack_id_looper,
3278 /*copy_data*/ nullptr,
3279 /*new_data*/ locktrack_new_data,
3280 /*get_constraint_targets*/ locktrack_get_tars,
3281 /*flush_constraint_targets*/ locktrack_flush_tars,
3282 /*get_target_matrix*/ default_get_tarmat,
3283 /*evaluate_constraint*/ locktrack_evaluate,
3284};
3285
3286/* ---------- Limit Distance Constraint ----------- */
3287
3288static void distlimit_new_data(void *cdata)
3289{
3291
3292 data->dist = 0.0f;
3293}
3294
3295static void distlimit_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
3296{
3297 bDistLimitConstraint *data = static_cast<bDistLimitConstraint *>(con->data);
3298
3299 /* target only */
3300 func(con, (ID **)&data->tar, false, userdata);
3301}
3302
3304{
3305 if (con && list) {
3306 bDistLimitConstraint *data = static_cast<bDistLimitConstraint *>(con->data);
3308
3309 /* standard target-getting macro for single-target constraints */
3310 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list);
3311
3312 return 1;
3313 }
3314
3315 return 0;
3316}
3317
3318static void distlimit_flush_tars(bConstraint *con, ListBase *list, bool no_copy)
3319{
3320 if (con && list) {
3321 bDistLimitConstraint *data = static_cast<bDistLimitConstraint *>(con->data);
3322 bConstraintTarget *ct = static_cast<bConstraintTarget *>(list->first);
3323
3324 /* the following macro is used for all standard single-target constraints */
3325 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, no_copy);
3326 }
3327}
3328
3329static void distlimit_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *targets)
3330{
3331 bDistLimitConstraint *data = static_cast<bDistLimitConstraint *>(con->data);
3332 bConstraintTarget *ct = static_cast<bConstraintTarget *>(targets->first);
3333
3334 /* only evaluate if there is a target */
3335 if (VALID_CONS_TARGET(ct)) {
3336 float dvec[3], dist, sfac = 1.0f;
3337 short clamp_surf = 0;
3338
3339 /* calculate our current distance from the target */
3340 dist = len_v3v3(cob->matrix[3], ct->matrix[3]);
3341
3342 /* set distance (flag is only set when user demands it) */
3343 if (data->dist == 0) {
3344 data->dist = dist;
3345
3346 /* Write the computed distance back to the master copy if in copy-on-eval evaluation. */
3348
3349 if (orig_con != nullptr) {
3350 bDistLimitConstraint *orig_data = static_cast<bDistLimitConstraint *>(orig_con->data);
3351
3352 orig_data->dist = data->dist;
3353 }
3354 }
3355
3356 /* check if we're which way to clamp from, and calculate interpolation factor (if needed) */
3357 if (data->mode == LIMITDIST_OUTSIDE) {
3358 /* if inside, then move to surface */
3359 if (dist <= data->dist) {
3360 clamp_surf = 1;
3361 if (dist != 0.0f) {
3362 sfac = data->dist / dist;
3363 }
3364 }
3365 /* if soft-distance is enabled, start fading once owner is dist+softdist from the target */
3366 else if (data->flag & LIMITDIST_USESOFT) {
3367 if (dist <= (data->dist + data->soft)) {
3368 /* pass */
3369 }
3370 }
3371 }
3372 else if (data->mode == LIMITDIST_INSIDE) {
3373 /* if outside, then move to surface */
3374 if (dist >= data->dist) {
3375 clamp_surf = 1;
3376 if (dist != 0.0f) {
3377 sfac = data->dist / dist;
3378 }
3379 }
3380 /* if soft-distance is enabled, start fading once owner is dist-soft from the target */
3381 else if (data->flag & LIMITDIST_USESOFT) {
3382 /* FIXME: there's a problem with "jumping" when this kicks in */
3383 if (dist >= (data->dist - data->soft)) {
3384 sfac = (data->soft * (1.0f - expf(-(dist - data->dist) / data->soft)) + data->dist);
3385 if (dist != 0.0f) {
3386 sfac /= dist;
3387 }
3388
3389 clamp_surf = 1;
3390 }
3391 }
3392 }
3393 else {
3394 if (IS_EQF(dist, data->dist) == 0) {
3395 clamp_surf = 1;
3396 if (dist != 0.0f) {
3397 sfac = data->dist / dist;
3398 }
3399 }
3400 }
3401
3402 /* clamp to 'surface' (i.e. move owner so that dist == data->dist) */
3403 if (clamp_surf) {
3404 /* simply interpolate along line formed by target -> owner */
3405 interp_v3_v3v3(dvec, ct->matrix[3], cob->matrix[3], sfac);
3406
3407 /* copy new vector onto owner */
3408 copy_v3_v3(cob->matrix[3], dvec);
3409 }
3410 }
3411}
3412
3415 /*size*/ sizeof(bDistLimitConstraint),
3416 /*name*/ N_("Limit Distance"),
3417 /*struct_name*/ "bDistLimitConstraint",
3418 /*free_data*/ nullptr,
3419 /*id_looper*/ distlimit_id_looper,
3420 /*copy_data*/ nullptr,
3421 /*new_data*/ distlimit_new_data,
3422 /*get_constraint_targets*/ distlimit_get_tars,
3423 /*flush_constraint_targets*/ distlimit_flush_tars,
3424 /*get_target_matrix*/ default_get_tarmat,
3425 /*evaluate_constraint*/ distlimit_evaluate,
3426};
3427
3428/* ---------- Stretch To ------------ */
3429
3430static void stretchto_new_data(void *cdata)
3431{
3433
3434 data->volmode = 0;
3435 data->plane = SWING_Y;
3436 data->orglength = 0.0;
3437 data->bulge = 1.0;
3438 data->bulge_max = 1.0f;
3439 data->bulge_min = 1.0f;
3440}
3441
3442static void stretchto_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
3443{
3444 bStretchToConstraint *data = static_cast<bStretchToConstraint *>(con->data);
3445
3446 /* target only */
3447 func(con, (ID **)&data->tar, false, userdata);
3448}
3449
3451{
3452 if (con && list) {
3453 bStretchToConstraint *data = static_cast<bStretchToConstraint *>(con->data);
3455
3456 /* standard target-getting macro for single-target constraints */
3457 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list);
3458
3459 return 1;
3460 }
3461
3462 return 0;
3463}
3464
3465static void stretchto_flush_tars(bConstraint *con, ListBase *list, bool no_copy)
3466{
3467 if (con && list) {
3468 bStretchToConstraint *data = static_cast<bStretchToConstraint *>(con->data);
3469 bConstraintTarget *ct = static_cast<bConstraintTarget *>(list->first);
3470
3471 /* the following macro is used for all standard single-target constraints */
3472 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, no_copy);
3473 }
3474}
3475
3476static void stretchto_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *targets)
3477{
3478 bStretchToConstraint *data = static_cast<bStretchToConstraint *>(con->data);
3479 bConstraintTarget *ct = static_cast<bConstraintTarget *>(targets->first);
3480
3481 /* only evaluate if there is a target */
3482 if (VALID_CONS_TARGET(ct)) {
3483 float size[3], scale[3], vec[3], xx[3], zz[3], orth[3];
3484 float dist, bulge;
3485
3486 /* Remove shear if using the Damped Track mode; the other modes
3487 * do it as a side effect, which is relied on by rigs. */
3488 if (data->plane == SWING_Y) {
3489 orthogonalize_m4_stable(cob->matrix, 1, false);
3490 }
3491
3492 /* store scaling before destroying obmat */
3494
3495 /* store X orientation before destroying obmat */
3496 copy_v3_v3(xx, cob->matrix[0]);
3497
3498 /* store Z orientation before destroying obmat */
3499 copy_v3_v3(zz, cob->matrix[2]);
3500
3501 /* Compute distance and direction to target. */
3502 sub_v3_v3v3(vec, ct->matrix[3], cob->matrix[3]);
3503
3504 dist = normalize_v3(vec);
3505
3506 /* Only Y constrained object axis scale should be used, to keep same length when scaling it.
3507 * Use safe divide to avoid creating a matrix with NAN values, see: #141612. */
3508 dist = blender::math::safe_divide(dist, size[1]);
3509
3510 /* data->orglength==0 occurs on first run, and after 'R' button is clicked */
3511 if (data->orglength == 0) {
3512 data->orglength = dist;
3513
3514 /* Write the computed length back to the master copy if in copy-on-eval evaluation. */
3516
3517 if (orig_con != nullptr) {
3518 bStretchToConstraint *orig_data = static_cast<bStretchToConstraint *>(orig_con->data);
3519
3520 orig_data->orglength = data->orglength;
3521 }
3522 }
3523
3524 scale[1] = dist / data->orglength;
3525
3526 bulge = powf(data->orglength / dist, data->bulge);
3527
3528 if (bulge > 1.0f) {
3529 if (data->flag & STRETCHTOCON_USE_BULGE_MAX) {
3530 float bulge_max = max_ff(data->bulge_max, 1.0f);
3531 float hard = min_ff(bulge, bulge_max);
3532
3533 float range = bulge_max - 1.0f;
3534 float scale_fac = (range > 0.0f) ? 1.0f / range : 0.0f;
3535 float soft = 1.0f + range * atanf((bulge - 1.0f) * scale_fac) / float(M_PI_2);
3536
3537 bulge = interpf(soft, hard, data->bulge_smooth);
3538 }
3539 }
3540 if (bulge < 1.0f) {
3541 if (data->flag & STRETCHTOCON_USE_BULGE_MIN) {
3542 float bulge_min = std::clamp(data->bulge_min, 0.0f, 1.0f);
3543 float hard = max_ff(bulge, bulge_min);
3544
3545 float range = 1.0f - bulge_min;
3546 float scale_fac = (range > 0.0f) ? 1.0f / range : 0.0f;
3547 float soft = 1.0f - range * atanf((1.0f - bulge) * scale_fac) / float(M_PI_2);
3548
3549 bulge = interpf(soft, hard, data->bulge_smooth);
3550 }
3551 }
3552
3553 switch (data->volmode) {
3554 /* volume preserving scaling */
3555 case VOLUME_XZ:
3556 scale[0] = sqrtf(bulge);
3557 scale[2] = scale[0];
3558 break;
3559 case VOLUME_X:
3560 scale[0] = bulge;
3561 scale[2] = 1.0;
3562 break;
3563 case VOLUME_Z:
3564 scale[0] = 1.0;
3565 scale[2] = bulge;
3566 break;
3567 /* don't care for volume */
3568 case NO_VOLUME:
3569 scale[0] = 1.0;
3570 scale[2] = 1.0;
3571 break;
3572 default: /* Should not happen, but in case. */
3573 return;
3574 } /* switch (data->volmode) */
3575
3576 /* Compute final scale. */
3577 mul_v3_v3(size, scale);
3578
3579 switch (data->plane) {
3580 case SWING_Y:
3581 /* Point the Y axis using Damped Track math. */
3583 break;
3584 case PLANE_X:
3585 /* New Y aligns object target connection. */
3586 copy_v3_v3(cob->matrix[1], vec);
3587
3588 /* Build new Z vector. */
3589 /* Orthogonal to "new Y" "old X! plane. */
3590 cross_v3_v3v3(orth, xx, vec);
3592
3593 /* New Z. */
3594 copy_v3_v3(cob->matrix[2], orth);
3595
3596 /* We decided to keep X plane. */
3597 cross_v3_v3v3(xx, vec, orth);
3598 normalize_v3_v3(cob->matrix[0], xx);
3599 break;
3600 case PLANE_Z:
3601 /* New Y aligns object target connection. */
3602 copy_v3_v3(cob->matrix[1], vec);
3603
3604 /* Build new X vector. */
3605 /* Orthogonal to "new Y" "old Z! plane. */
3606 cross_v3_v3v3(orth, zz, vec);
3608
3609 /* New X. */
3610 negate_v3_v3(cob->matrix[0], orth);
3611
3612 /* We decided to keep Z. */
3613 cross_v3_v3v3(zz, vec, orth);
3614 normalize_v3_v3(cob->matrix[2], zz);
3615 break;
3616 } /* switch (data->plane) */
3617
3618 rescale_m4(cob->matrix, size);
3619 }
3620}
3621
3624 /*size*/ sizeof(bStretchToConstraint),
3625 /*name*/ N_("Stretch To"),
3626 /*struct_name*/ "bStretchToConstraint",
3627 /*free_data*/ nullptr,
3628 /*id_looper*/ stretchto_id_looper,
3629 /*copy_data*/ nullptr,
3630 /*new_data*/ stretchto_new_data,
3631 /*get_constraint_targets*/ stretchto_get_tars,
3632 /*flush_constraint_targets*/ stretchto_flush_tars,
3633 /*get_target_matrix*/ default_get_tarmat,
3634 /*evaluate_constraint*/ stretchto_evaluate,
3635};
3636
3637/* ---------- Floor ------------ */
3638
3639static void minmax_new_data(void *cdata)
3640{
3642
3643 data->minmaxflag = TRACK_Z;
3644 data->offset = 0.0f;
3645 data->flag = 0;
3646}
3647
3648static void minmax_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
3649{
3650 bMinMaxConstraint *data = static_cast<bMinMaxConstraint *>(con->data);
3651
3652 /* target only */
3653 func(con, (ID **)&data->tar, false, userdata);
3654}
3655
3656static int minmax_get_tars(bConstraint *con, ListBase *list)
3657{
3658 if (con && list) {
3659 bMinMaxConstraint *data = static_cast<bMinMaxConstraint *>(con->data);
3661
3662 /* standard target-getting macro for single-target constraints */
3663 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list);
3664
3665 return 1;
3666 }
3667
3668 return 0;
3669}
3670
3671static void minmax_flush_tars(bConstraint *con, ListBase *list, bool no_copy)
3672{
3673 if (con && list) {
3674 bMinMaxConstraint *data = static_cast<bMinMaxConstraint *>(con->data);
3675 bConstraintTarget *ct = static_cast<bConstraintTarget *>(list->first);
3676
3677 /* the following macro is used for all standard single-target constraints */
3678 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, no_copy);
3679 }
3680}
3681
3682static void minmax_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *targets)
3683{
3684 bMinMaxConstraint *data = static_cast<bMinMaxConstraint *>(con->data);
3685 bConstraintTarget *ct = static_cast<bConstraintTarget *>(targets->first);
3686
3687 /* only evaluate if there is a target */
3688 if (VALID_CONS_TARGET(ct)) {
3689 float obmat[4][4], imat[4][4], tarmat[4][4], tmat[4][4];
3690 float val1, val2;
3691 int index;
3692
3693 copy_m4_m4(obmat, cob->matrix);
3694 copy_m4_m4(tarmat, ct->matrix);
3695
3696 if (data->flag & MINMAX_USEROT) {
3697 /* Take rotation of target into account by doing the transaction in target's local-space. */
3698 invert_m4_m4(imat, tarmat);
3699 mul_m4_m4m4(tmat, imat, obmat);
3700 copy_m4_m4(obmat, tmat);
3701 unit_m4(tarmat);
3702 }
3703
3704 switch (data->minmaxflag) {
3705 case TRACK_Z:
3706 val1 = tarmat[3][2];
3707 val2 = obmat[3][2] - data->offset;
3708 index = 2;
3709 break;
3710 case TRACK_Y:
3711 val1 = tarmat[3][1];
3712 val2 = obmat[3][1] - data->offset;
3713 index = 1;
3714 break;
3715 case TRACK_X:
3716 val1 = tarmat[3][0];
3717 val2 = obmat[3][0] - data->offset;
3718 index = 0;
3719 break;
3720 case TRACK_nZ:
3721 val2 = tarmat[3][2];
3722 val1 = obmat[3][2] - data->offset;
3723 index = 2;
3724 break;
3725 case TRACK_nY:
3726 val2 = tarmat[3][1];
3727 val1 = obmat[3][1] - data->offset;
3728 index = 1;
3729 break;
3730 case TRACK_nX:
3731 val2 = tarmat[3][0];
3732 val1 = obmat[3][0] - data->offset;
3733 index = 0;
3734 break;
3735 default:
3736 return;
3737 }
3738
3739 if (val1 > val2) {
3740 obmat[3][index] = tarmat[3][index] + data->offset;
3741 if (data->flag & MINMAX_USEROT) {
3742 /* Get out of local-space. */
3743 mul_m4_m4m4(tmat, ct->matrix, obmat);
3744 copy_m4_m4(cob->matrix, tmat);
3745 }
3746 else {
3747 copy_v3_v3(cob->matrix[3], obmat[3]);
3748 }
3749 }
3750 }
3751}
3752
3754 /*type*/ CONSTRAINT_TYPE_MINMAX,
3755 /*size*/ sizeof(bMinMaxConstraint),
3756 /*name*/ N_("Floor"),
3757 /*struct_name*/ "bMinMaxConstraint",
3758 /*free_data*/ nullptr,
3759 /*id_looper*/ minmax_id_looper,
3760 /*copy_data*/ nullptr,
3761 /*new_data*/ minmax_new_data,
3762 /*get_constraint_targets*/ minmax_get_tars,
3763 /*flush_constraint_targets*/ minmax_flush_tars,
3764 /*get_target_matrix*/ default_get_tarmat,
3765 /*evaluate_constraint*/ minmax_evaluate,
3766};
3767
3768/* -------- Clamp To ---------- */
3769
3770static void clampto_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
3771{
3772 bClampToConstraint *data = static_cast<bClampToConstraint *>(con->data);
3773
3774 /* target only */
3775 func(con, (ID **)&data->tar, false, userdata);
3776}
3777
3779{
3780 if (con && list) {
3781 bClampToConstraint *data = static_cast<bClampToConstraint *>(con->data);
3783
3784 /* Standard target-getting macro for single-target constraints without sub-targets. */
3785 SINGLETARGETNS_GET_TARS(con, data->tar, ct, list);
3786
3787 return 1;
3788 }
3789
3790 return 0;
3791}
3792
3793static void clampto_flush_tars(bConstraint *con, ListBase *list, bool no_copy)
3794{
3795 if (con && list) {
3796 bClampToConstraint *data = static_cast<bClampToConstraint *>(con->data);
3797 bConstraintTarget *ct = static_cast<bConstraintTarget *>(list->first);
3798
3799 /* the following macro is used for all standard single-target constraints */
3800 SINGLETARGETNS_FLUSH_TARS(con, data->tar, ct, list, no_copy);
3801 }
3802}
3803
3804static bool clampto_get_tarmat(Depsgraph * /*depsgraph*/,
3805 bConstraint * /*con*/,
3806 bConstraintOb * /*cob*/,
3808 float /*ctime*/)
3809{
3810 /* technically, this isn't really needed for evaluation, but we don't know what else
3811 * might end up calling this...
3812 */
3814 return false;
3815}
3816
3817static void clampto_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *targets)
3818{
3819 using namespace blender;
3820 bClampToConstraint *data = static_cast<bClampToConstraint *>(con->data);
3821 bConstraintTarget *ct = static_cast<bConstraintTarget *>(targets->first);
3822
3823 /* only evaluate if there is a target and it is a curve */
3824 if (VALID_CONS_TARGET(ct) && (ct->tar->type == OB_CURVES_LEGACY)) {
3825 float obmat[4][4], ownLoc[3];
3826 float curveMin[3], curveMax[3];
3827 float targetMatrix[4][4];
3828
3829 copy_m4_m4(obmat, cob->matrix);
3830 copy_v3_v3(ownLoc, obmat[3]);
3831
3832 unit_m4(targetMatrix);
3833 INIT_MINMAX(curveMin, curveMax);
3834 if (const std::optional<Bounds<float3>> bounds = BKE_object_boundbox_get(ct->tar)) {
3835 copy_v3_v3(curveMin, bounds->min);
3836 copy_v3_v3(curveMax, bounds->max);
3837 }
3838
3839 /* Get target-matrix. */
3840 if (data->tar->runtime->curve_cache && data->tar->runtime->curve_cache->anim_path_accum_length)
3841 {
3842 float vec[4], totmat[4][4];
3843 float curvetime;
3844 short clamp_axis;
3845
3846 /* find best position on curve */
3847 /* 1. determine which axis to sample on? */
3848 if (data->flag == CLAMPTO_AUTO) {
3849 float size[3];
3850 sub_v3_v3v3(size, curveMax, curveMin);
3851
3852 /* find axis along which the bounding box has the greatest
3853 * extent. Otherwise, default to the x-axis, as that is quite
3854 * frequently used.
3855 */
3856 if ((size[2] > size[0]) && (size[2] > size[1])) {
3857 clamp_axis = CLAMPTO_Z - 1;
3858 }
3859 else if ((size[1] > size[0]) && (size[1] > size[2])) {
3860 clamp_axis = CLAMPTO_Y - 1;
3861 }
3862 else {
3863 clamp_axis = CLAMPTO_X - 1;
3864 }
3865 }
3866 else {
3867 clamp_axis = data->flag - 1;
3868 }
3869
3870 /* 2. determine position relative to curve on a 0-1 scale based on bounding box */
3871 if (data->flag2 & CLAMPTO_CYCLIC) {
3872 /* cyclic, so offset within relative bounding box is used */
3873 float len = (curveMax[clamp_axis] - curveMin[clamp_axis]);
3874 float offset;
3875
3876 /* check to make sure len is not so close to zero that it'll cause errors */
3877 if (IS_EQF(len, 0.0f) == false) {
3878 /* find bounding-box range where target is located */
3879 if (ownLoc[clamp_axis] < curveMin[clamp_axis]) {
3880 /* bounding-box range is before */
3881 offset = curveMin[clamp_axis] -
3882 ceilf((curveMin[clamp_axis] - ownLoc[clamp_axis]) / len) * len;
3883
3884 /* Now, we calculate as per normal,
3885 * except using offset instead of curveMin[clamp_axis]. */
3886 curvetime = (ownLoc[clamp_axis] - offset) / (len);
3887 }
3888 else if (ownLoc[clamp_axis] > curveMax[clamp_axis]) {
3889 /* bounding-box range is after */
3890 offset = curveMax[clamp_axis] +
3891 int((ownLoc[clamp_axis] - curveMax[clamp_axis]) / len) * len;
3892
3893 /* Now, we calculate as per normal,
3894 * except using offset instead of curveMax[clamp_axis]. */
3895 curvetime = (ownLoc[clamp_axis] - offset) / (len);
3896 }
3897 else {
3898 /* as the location falls within bounds, just calculate */
3899 curvetime = (ownLoc[clamp_axis] - curveMin[clamp_axis]) / (len);
3900 }
3901 }
3902 else {
3903 /* as length is close to zero, curvetime by default should be 0 (i.e. the start) */
3904 curvetime = 0.0f;
3905 }
3906 }
3907 else {
3908 /* no cyclic, so position is clamped to within the bounding box */
3909 if (ownLoc[clamp_axis] <= curveMin[clamp_axis]) {
3910 curvetime = 0.0f;
3911 }
3912 else if (ownLoc[clamp_axis] >= curveMax[clamp_axis]) {
3913 curvetime = 1.0f;
3914 }
3915 else if (IS_EQF((curveMax[clamp_axis] - curveMin[clamp_axis]), 0.0f) == false) {
3916 curvetime = (ownLoc[clamp_axis] - curveMin[clamp_axis]) /
3917 (curveMax[clamp_axis] - curveMin[clamp_axis]);
3918 }
3919 else {
3920 curvetime = 0.0f;
3921 }
3922 }
3923
3924 /* 3. position on curve */
3925 if (BKE_where_on_path(ct->tar, curvetime, vec, nullptr, nullptr, nullptr, nullptr)) {
3926 unit_m4(totmat);
3927 copy_v3_v3(totmat[3], vec);
3928
3929 mul_m4_m4m4(targetMatrix, ct->tar->object_to_world().ptr(), totmat);
3930 }
3931 }
3932
3933 /* obtain final object position */
3934 copy_v3_v3(cob->matrix[3], targetMatrix[3]);
3935 }
3936}
3937
3939 /*type*/ CONSTRAINT_TYPE_CLAMPTO,
3940 /*size*/ sizeof(bClampToConstraint),
3941 /*name*/ N_("Clamp To"),
3942 /*struct_name*/ "bClampToConstraint",
3943 /*free_data*/ nullptr,
3944 /*id_looper*/ clampto_id_looper,
3945 /*copy_data*/ nullptr,
3946 /*new_data*/ nullptr,
3947 /*get_constraint_targets*/ clampto_get_tars,
3948 /*flush_constraint_targets*/ clampto_flush_tars,
3949 /*get_target_matrix*/ clampto_get_tarmat,
3950 /*evaluate_constraint*/ clampto_evaluate,
3951};
3952
3953/* ---------- Transform Constraint ----------- */
3954
3955static void transform_new_data(void *cdata)
3956{
3958
3959 data->map[0] = 0;
3960 data->map[1] = 1;
3961 data->map[2] = 2;
3962
3963 for (int i = 0; i < 3; i++) {
3964 data->from_min_scale[i] = data->from_max_scale[i] = 1.0f;
3965 data->to_min_scale[i] = data->to_max_scale[i] = 1.0f;
3966 }
3967}
3968
3969static void transform_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
3970{
3971 bTransformConstraint *data = static_cast<bTransformConstraint *>(con->data);
3972
3973 /* target only */
3974 func(con, (ID **)&data->tar, false, userdata);
3975}
3976
3978{
3979 if (con && list) {
3980 bTransformConstraint *data = static_cast<bTransformConstraint *>(con->data);
3982
3983 /* standard target-getting macro for single-target constraints */
3984 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list);
3985
3986 return 1;
3987 }
3988
3989 return 0;
3990}
3991
3992static void transform_flush_tars(bConstraint *con, ListBase *list, bool no_copy)
3993{
3994 if (con && list) {
3995 bTransformConstraint *data = static_cast<bTransformConstraint *>(con->data);
3996 bConstraintTarget *ct = static_cast<bConstraintTarget *>(list->first);
3997
3998 /* the following macro is used for all standard single-target constraints */
3999 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, no_copy);
4000 }
4001}
4002
4003static void transform_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *targets)
4004{
4005 bTransformConstraint *data = static_cast<bTransformConstraint *>(con->data);
4006 bConstraintTarget *ct = static_cast<bConstraintTarget *>(targets->first);
4007
4008 /* only evaluate if there is a target */
4009 if (VALID_CONS_TARGET(ct)) {
4010 float *from_min, *from_max, *to_min, *to_max;
4011 float loc[3], rot[3][3], oldeul[3], size[3];
4012 float newloc[3], newrot[3][3], neweul[3], newsize[3];
4013 float dbuf[4], sval[3];
4014 float *const dvec = dbuf + 1;
4015
4016 /* obtain target effect */
4017 switch (data->from) {
4018 case TRANS_SCALE:
4019 mat4_to_size(dvec, ct->matrix);
4020
4021 if (is_negative_m4(ct->matrix)) {
4022 /* Bug-fix #27886: (this is a limitation that riggers will have to live with for now).
4023 * We can't be sure which axis/axes are negative,
4024 * though we know that something is negative.
4025 * Assume we don't care about negativity of separate axes. */
4026 negate_v3(dvec);
4027 }
4028 from_min = data->from_min_scale;
4029 from_max = data->from_max_scale;
4030 break;
4031 case TRANS_ROTATION:
4033 ct->matrix, cob->rotOrder, data->from_rotation_mode, -1, true, dbuf);
4034 from_min = data->from_min_rot;
4035 from_max = data->from_max_rot;
4036 break;
4037 case TRANS_LOCATION:
4038 default:
4039 copy_v3_v3(dvec, ct->matrix[3]);
4040 from_min = data->from_min;
4041 from_max = data->from_max;
4042 break;
4043 }
4044
4045 /* Select the output Euler rotation order, defaulting to the owner. */
4046 short rot_order = cob->rotOrder;
4047
4048 if (data->to == TRANS_ROTATION && data->to_euler_order != CONSTRAINT_EULER_AUTO) {
4049 rot_order = data->to_euler_order;
4050 }
4051
4052 /* extract components of owner's matrix */
4053 mat4_to_loc_rot_size(loc, rot, size, cob->matrix);
4054
4055 /* determine where in range current transforms lie */
4056 if (data->expo) {
4057 for (int i = 0; i < 3; i++) {
4058 if (from_max[i] - from_min[i]) {
4059 sval[i] = (dvec[i] - from_min[i]) / (from_max[i] - from_min[i]);
4060 }
4061 else {
4062 sval[i] = 0.0f;
4063 }
4064 }
4065 }
4066 else {
4067 /* clamp transforms out of range */
4068 for (int i = 0; i < 3; i++) {
4069 CLAMP(dvec[i], from_min[i], from_max[i]);
4070 if (from_max[i] - from_min[i]) {
4071 sval[i] = (dvec[i] - from_min[i]) / (from_max[i] - from_min[i]);
4072 }
4073 else {
4074 sval[i] = 0.0f;
4075 }
4076 }
4077 }
4078
4079 /* apply transforms */
4080 switch (data->to) {
4081 case TRANS_SCALE:
4082 to_min = data->to_min_scale;
4083 to_max = data->to_max_scale;
4084 for (int i = 0; i < 3; i++) {
4085 newsize[i] = to_min[i] + (sval[int(data->map[i])] * (to_max[i] - to_min[i]));
4086 }
4087 switch (data->mix_mode_scale) {
4089 mul_v3_v3(size, newsize);
4090 break;
4092 default:
4093 copy_v3_v3(size, newsize);
4094 break;
4095 }
4096 break;
4097 case TRANS_ROTATION:
4098 to_min = data->to_min_rot;
4099 to_max = data->to_max_rot;
4100 for (int i = 0; i < 3; i++) {
4101 neweul[i] = to_min[i] + (sval[int(data->map[i])] * (to_max[i] - to_min[i]));
4102 }
4103 switch (data->mix_mode_rot) {
4105 eulO_to_mat3(rot, neweul, rot_order);
4106 break;
4108 eulO_to_mat3(newrot, neweul, rot_order);
4109 mul_m3_m3m3(rot, newrot, rot);
4110 break;
4111 case TRANS_MIXROT_AFTER:
4112 eulO_to_mat3(newrot, neweul, rot_order);
4113 mul_m3_m3m3(rot, rot, newrot);
4114 break;
4115 case TRANS_MIXROT_ADD:
4116 default:
4117 mat3_to_eulO(oldeul, rot_order, rot);
4118 add_v3_v3(neweul, oldeul);
4119 eulO_to_mat3(rot, neweul, rot_order);
4120 break;
4121 }
4122 break;
4123 case TRANS_LOCATION:
4124 default:
4125 to_min = data->to_min;
4126 to_max = data->to_max;
4127 for (int i = 0; i < 3; i++) {
4128 newloc[i] = (to_min[i] + (sval[int(data->map[i])] * (to_max[i] - to_min[i])));
4129 }
4130 switch (data->mix_mode_loc) {
4132 copy_v3_v3(loc, newloc);
4133 break;
4134 case TRANS_MIXLOC_ADD:
4135 default:
4136 add_v3_v3(loc, newloc);
4137 break;
4138 }
4139 break;
4140 }
4141
4142 /* apply to matrix */
4143 loc_rot_size_to_mat4(cob->matrix, loc, rot, size);
4144 }
4145}
4146
4149 /*size*/ sizeof(bTransformConstraint),
4150 /*name*/ N_("Transformation"),
4151 /*struct_name*/ "bTransformConstraint",
4152 /*free_data*/ nullptr,
4153 /*id_looper*/ transform_id_looper,
4154 /*copy_data*/ nullptr,
4155 /*new_data*/ transform_new_data,
4156 /*get_constraint_targets*/ transform_get_tars,
4157 /*flush_constraint_targets*/ transform_flush_tars,
4158 /*get_target_matrix*/ default_get_tarmat,
4159 /*evaluate_constraint*/ transform_evaluate,
4160};
4161
4162/* ---------- Shrinkwrap Constraint ----------- */
4163
4164static void shrinkwrap_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
4165{
4166 bShrinkwrapConstraint *data = static_cast<bShrinkwrapConstraint *>(con->data);
4167
4168 /* target only */
4169 func(con, (ID **)&data->target, false, userdata);
4170}
4171
4172static void shrinkwrap_new_data(void *cdata)
4173{
4175
4176 data->projAxis = OB_POSZ;
4177 data->projAxisSpace = CONSTRAINT_SPACE_LOCAL;
4178}
4179
4181{
4182 if (con && list) {
4183 bShrinkwrapConstraint *data = static_cast<bShrinkwrapConstraint *>(con->data);
4185
4186 SINGLETARGETNS_GET_TARS(con, data->target, ct, list);
4187
4188 return 1;
4189 }
4190
4191 return 0;
4192}
4193
4194static void shrinkwrap_flush_tars(bConstraint *con, ListBase *list, bool no_copy)
4195{
4196 if (con && list) {
4197 bShrinkwrapConstraint *data = static_cast<bShrinkwrapConstraint *>(con->data);
4198 bConstraintTarget *ct = static_cast<bConstraintTarget *>(list->first);
4199
4200 SINGLETARGETNS_FLUSH_TARS(con, data->target, ct, list, no_copy);
4201 }
4202}
4203
4204static bool shrinkwrap_get_tarmat(Depsgraph * /*depsgraph*/,
4205 bConstraint *con,
4206 bConstraintOb *cob,
4208 float /*ctime*/)
4209{
4211
4212 if (!VALID_CONS_TARGET(ct) || ct->tar->type != OB_MESH) {
4213 return false;
4214 }
4215
4216 bool fail = false;
4217 float co[3] = {0.0f, 0.0f, 0.0f};
4218 bool track_normal = false;
4219 float track_no[3] = {0.0f, 0.0f, 0.0f};
4220
4222 Mesh *target_eval = BKE_object_get_evaluated_mesh(ct->tar);
4223
4224 copy_m4_m4(ct->matrix, cob->matrix);
4225
4226 bool do_track_normal = (scon->flag & CON_SHRINKWRAP_TRACK_NORMAL) != 0;
4228
4230 &tree, target_eval, scon->shrinkType, scon->shrinkMode, do_track_normal))
4231 {
4232 return false;
4233 }
4234
4235 BLI_space_transform_from_matrices(&transform, cob->matrix, ct->tar->object_to_world().ptr());
4236
4237 switch (scon->shrinkType) {
4241 BVHTreeNearest nearest;
4242
4243 nearest.index = -1;
4244 nearest.dist_sq = FLT_MAX;
4245
4247
4249
4250 if (nearest.index < 0) {
4251 fail = true;
4252 break;
4253 }
4254
4256 if (do_track_normal) {
4257 track_normal = true;
4259 &tree, nullptr, nearest.index, nearest.co, nearest.no, track_no);
4261 }
4262
4264 nullptr,
4265 scon->shrinkMode,
4266 nearest.index,
4267 nearest.co,
4268 nearest.no,
4269 scon->dist,
4270 co,
4271 co);
4272 }
4273 else {
4274 const float dist = len_v3v3(co, nearest.co);
4275
4276 if (dist != 0.0f) {
4278 co, co, nearest.co, (dist - scon->dist) / dist); /* linear interpolation */
4279 }
4280 }
4281
4283 break;
4284 }
4286 BVHTreeRayHit hit;
4287
4288 float mat[4][4];
4289 float no[3] = {0.0f, 0.0f, 0.0f};
4290
4291 /* TODO: should use FLT_MAX.. but normal projection doesn't yet supports it. */
4292 hit.index = -1;
4293 hit.dist = (scon->projLimit == 0.0f) ? BVH_RAYCAST_DIST_MAX : scon->projLimit;
4294
4295 switch (scon->projAxis) {
4296 case OB_POSX:
4297 case OB_POSY:
4298 case OB_POSZ:
4299 no[scon->projAxis - OB_POSX] = 1.0f;
4300 break;
4301 case OB_NEGX:
4302 case OB_NEGY:
4303 case OB_NEGZ:
4304 no[scon->projAxis - OB_NEGX] = -1.0f;
4305 break;
4306 }
4307
4308 /* Transform normal into requested space */
4309 /* Note that in this specific case, we need to keep scaling in non-parented 'local2world'
4310 * object case, because SpaceTransform also takes it into account when handling normals.
4311 * See #42447. */
4312 unit_m4(mat);
4314 cob->ob, cob->pchan, cob, mat, CONSTRAINT_SPACE_LOCAL, scon->projAxisSpace, true);
4315 invert_m4(mat);
4316 mul_mat3_m4_v3(mat, no);
4317
4318 if (normalize_v3(no) < FLT_EPSILON) {
4319 fail = true;
4320 break;
4321 }
4322
4323 char cull_mode = scon->flag & CON_SHRINKWRAP_PROJECT_CULL_MASK;
4324
4325 BKE_shrinkwrap_project_normal(cull_mode, co, no, 0.0f, &transform, &tree, &hit);
4326
4328 float inv_no[3];
4329 negate_v3_v3(inv_no, no);
4330
4331 if ((scon->flag & CON_SHRINKWRAP_PROJECT_INVERT_CULL) && (cull_mode != 0)) {
4333 }
4334
4335 BKE_shrinkwrap_project_normal(cull_mode, co, inv_no, 0.0f, &transform, &tree, &hit);
4336 }
4337
4338 if (hit.index < 0) {
4339 fail = true;
4340 break;
4341 }
4342
4343 if (do_track_normal) {
4344 track_normal = true;
4346 &tree, &transform, hit.index, hit.co, hit.no, track_no);
4347 }
4348
4350 &tree, &transform, scon->shrinkMode, hit.index, hit.co, hit.no, scon->dist, co, co);
4351 break;
4352 }
4353 }
4354
4356
4357 if (fail) {
4358 /* Don't move the point */
4359 zero_v3(co);
4360 }
4361
4362 /* co is in local object coordinates, change it to global and update target position */
4363 mul_m4_v3(cob->matrix, co);
4364 copy_v3_v3(ct->matrix[3], co);
4365
4366 if (track_normal) {
4367 mul_mat3_m4_v3(cob->matrix, track_no);
4368 damptrack_do_transform(ct->matrix, track_no, scon->trackAxis);
4369 }
4370
4371 return true;
4372}
4373
4374static void shrinkwrap_evaluate(bConstraint * /*con*/, bConstraintOb *cob, ListBase *targets)
4375{
4376 bConstraintTarget *ct = static_cast<bConstraintTarget *>(targets->first);
4377
4378 /* only evaluate if there is a target */
4379 if (VALID_CONS_TARGET(ct)) {
4380 copy_m4_m4(cob->matrix, ct->matrix);
4381 }
4382}
4383
4386 /*size*/ sizeof(bShrinkwrapConstraint),
4387 /*name*/ N_("Shrinkwrap"),
4388 /*struct_name*/ "bShrinkwrapConstraint",
4389 /*free_data*/ nullptr,
4390 /*id_looper*/ shrinkwrap_id_looper,
4391 /*copy_data*/ nullptr,
4392 /*new_data*/ shrinkwrap_new_data,
4393 /*get_constraint_targets*/ shrinkwrap_get_tars,
4394 /*flush_constraint_targets*/ shrinkwrap_flush_tars,
4395 /*get_target_matrix*/ shrinkwrap_get_tarmat,
4396 /*evaluate_constraint*/ shrinkwrap_evaluate,
4397};
4398
4399/* --------- Damped Track ---------- */
4400
4401static void damptrack_new_data(void *cdata)
4402{
4404
4405 data->trackflag = TRACK_Y;
4406}
4407
4408static void damptrack_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
4409{
4410 bDampTrackConstraint *data = static_cast<bDampTrackConstraint *>(con->data);
4411
4412 /* target only */
4413 func(con, (ID **)&data->tar, false, userdata);
4414}
4415
4417{
4418 if (con && list) {
4419 bDampTrackConstraint *data = static_cast<bDampTrackConstraint *>(con->data);
4421
4422 /* the following macro is used for all standard single-target constraints */
4423 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list);
4424
4425 return 1;
4426 }
4427
4428 return 0;
4429}
4430
4431static void damptrack_flush_tars(bConstraint *con, ListBase *list, bool no_copy)
4432{
4433 if (con && list) {
4434 bDampTrackConstraint *data = static_cast<bDampTrackConstraint *>(con->data);
4435 bConstraintTarget *ct = static_cast<bConstraintTarget *>(list->first);
4436
4437 /* the following macro is used for all standard single-target constraints */
4438 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, no_copy);
4439 }
4440}
4441
4442/* array of direction vectors for the tracking flags */
4443static const float track_dir_vecs[6][3] = {
4444 {+1, 0, 0},
4445 {0, +1, 0},
4446 {0, 0, +1}, /* TRACK_X, TRACK_Y, TRACK_Z */
4447 {-1, 0, 0},
4448 {0, -1, 0},
4449 {0, 0, -1} /* TRACK_NX, TRACK_NY, TRACK_NZ */
4450};
4451
4452static void damptrack_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *targets)
4453{
4454 bDampTrackConstraint *data = static_cast<bDampTrackConstraint *>(con->data);
4455 bConstraintTarget *ct = static_cast<bConstraintTarget *>(targets->first);
4456
4457 if (VALID_CONS_TARGET(ct)) {
4458 float tarvec[3];
4459
4460 /* find the (unit) direction vector going from the owner to the target */
4461 sub_v3_v3v3(tarvec, ct->matrix[3], cob->matrix[3]);
4462
4463 damptrack_do_transform(cob->matrix, tarvec, data->trackflag);
4464 }
4465}
4466
4467static void damptrack_do_transform(float matrix[4][4], const float tarvec_in[3], int track_axis)
4468{
4469 using namespace blender;
4470 /* find the (unit) direction vector going from the owner to the target */
4471 float3 tarvec;
4472
4473 if (normalize_v3_v3(tarvec, tarvec_in) != 0.0f) {
4474 float3 obvec, obloc;
4475 float3 raxis;
4476 float rangle;
4477 float rmat[3][3], tmat[4][4];
4478
4479 /* find the (unit) direction that the axis we're interested in currently points
4480 * - mul_mat3_m4_v3() only takes the 3x3 (rotation+scaling) components of the 4x4 matrix
4481 * - the normalization step at the end should take care of any unwanted scaling
4482 * left over in the 3x3 matrix we used
4483 */
4484 copy_v3_v3(obvec, track_dir_vecs[track_axis]);
4485 mul_mat3_m4_v3(matrix, obvec);
4486
4487 if (normalize_v3(obvec) == 0.0f) {
4488 /* exceptional case - just use the track vector as appropriate */
4489 copy_v3_v3(obvec, track_dir_vecs[track_axis]);
4490 }
4491
4492 copy_v3_v3(obloc, matrix[3]);
4493
4494 /* determine the axis-angle rotation, which represents the smallest possible rotation
4495 * between the two rotation vectors (i.e. the 'damping' referred to in the name)
4496 * - we take this to be the rotation around the normal axis/vector to the plane defined
4497 * by the current and destination vectors, which will 'map' the current axis to the
4498 * destination vector
4499 * - the min/max wrappers around (obvec . tarvec) result (stored temporarily in rangle)
4500 * are used to ensure that the smallest angle is chosen
4501 */
4502 raxis = math::cross_high_precision(obvec, tarvec);
4503
4504 rangle = dot_v3v3(obvec, tarvec);
4505 rangle = acosf(max_ff(-1.0f, min_ff(1.0f, rangle)));
4506
4507 /* construct rotation matrix from the axis-angle rotation found above
4508 * - this call takes care to make sure that the axis provided is a unit vector first
4509 */
4510 float norm = normalize_v3(raxis);
4511
4512 if (norm < FLT_EPSILON) {
4513 /* if dot product is nonzero, while cross is zero, we have two opposite vectors!
4514 * - this is an ambiguity in the math that needs to be resolved arbitrarily,
4515 * or there will be a case where damped track strangely does nothing
4516 * - to do that, rotate around a different local axis
4517 */
4518 float tmpvec[3];
4519
4520 if (fabsf(rangle) < M_PI - 0.01f) {
4521 return;
4522 }
4523
4524 rangle = M_PI;
4525 copy_v3_v3(tmpvec, track_dir_vecs[(track_axis + 1) % 6]);
4526 mul_mat3_m4_v3(matrix, tmpvec);
4527 cross_v3_v3v3(raxis, obvec, tmpvec);
4528
4529 if (normalize_v3(raxis) == 0.0f) {
4530 return;
4531 }
4532 }
4533 else if (norm < 0.1f) {
4534 /* Near 0 and Pi `arcsin` has way better precision than `arccos`. */
4535 rangle = (rangle > M_PI_2) ? M_PI - asinf(norm) : asinf(norm);
4536 }
4537
4538 axis_angle_normalized_to_mat3(rmat, raxis, rangle);
4539
4540 /* rotate the owner in the way defined by this rotation matrix, then reapply the location since
4541 * we may have destroyed that in the process of multiplying the matrix
4542 */
4543 unit_m4(tmat);
4544 mul_m4_m3m4(tmat, rmat, matrix); /* m1, m3, m2 */
4545
4546 copy_m4_m4(matrix, tmat);
4547 copy_v3_v3(matrix[3], obloc);
4548 }
4549}
4550
4553 /*size*/ sizeof(bDampTrackConstraint),
4554 /*name*/ N_("Damped Track"),
4555 /*struct_name*/ "bDampTrackConstraint",
4556 /*free_data*/ nullptr,
4557 /*id_looper*/ damptrack_id_looper,
4558 /*copy_data*/ nullptr,
4559 /*new_data*/ damptrack_new_data,
4560 /*get_constraint_targets*/ damptrack_get_tars,
4561 /*flush_constraint_targets*/ damptrack_flush_tars,
4562 /*get_target_matrix*/ default_get_tarmat,
4563 /*evaluate_constraint*/ damptrack_evaluate,
4564};
4565
4566/* ----------- Spline IK ------------ */
4567
4569{
4570 bSplineIKConstraint *data = static_cast<bSplineIKConstraint *>(con->data);
4571
4572 /* binding array */
4573 MEM_SAFE_FREE(data->points);
4574}
4575
4576static void splineik_copy(bConstraint *con, bConstraint *srccon)
4577{
4578 bSplineIKConstraint *src = static_cast<bSplineIKConstraint *>(srccon->data);
4579 bSplineIKConstraint *dst = static_cast<bSplineIKConstraint *>(con->data);
4580
4581 /* copy the binding array */
4582 dst->points = static_cast<float *>(MEM_dupallocN(src->points));
4583}
4584
4585static void splineik_new_data(void *cdata)
4586{
4588
4589 data->chainlen = 1;
4590 data->bulge = 1.0;
4591 data->bulge_max = 1.0f;
4592 data->bulge_min = 1.0f;
4593
4596}
4597
4598static void splineik_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
4599{
4600 bSplineIKConstraint *data = static_cast<bSplineIKConstraint *>(con->data);
4601
4602 /* target only */
4603 func(con, (ID **)&data->tar, false, userdata);
4604}
4605
4607{
4608 if (con && list) {
4609 bSplineIKConstraint *data = static_cast<bSplineIKConstraint *>(con->data);
4611
4612 /* Standard target-getting macro for single-target constraints without sub-targets. */
4613 SINGLETARGETNS_GET_TARS(con, data->tar, ct, list);
4614
4615 return 1;
4616 }
4617
4618 return 0;
4619}
4620
4621static void splineik_flush_tars(bConstraint *con, ListBase *list, bool no_copy)
4622{
4623 if (con && list) {
4624 bSplineIKConstraint *data = static_cast<bSplineIKConstraint *>(con->data);
4625 bConstraintTarget *ct = static_cast<bConstraintTarget *>(list->first);
4626
4627 /* the following macro is used for all standard single-target constraints */
4628 SINGLETARGETNS_FLUSH_TARS(con, data->tar, ct, list, no_copy);
4629 }
4630}
4631
4632static bool splineik_get_tarmat(Depsgraph * /*depsgraph*/,
4633 bConstraint * /*con*/,
4634 bConstraintOb * /*cob*/,
4636 float /*ctime*/)
4637{
4638 /* technically, this isn't really needed for evaluation, but we don't know what else
4639 * might end up calling this...
4640 */
4642 return false;
4643}
4644
4646 /*type*/ CONSTRAINT_TYPE_SPLINEIK,
4647 /*size*/ sizeof(bSplineIKConstraint),
4648 /*name*/ N_("Spline IK"),
4649 /*struct_name*/ "bSplineIKConstraint",
4650 /*free_data*/ splineik_free,
4651 /*id_looper*/ splineik_id_looper,
4652 /*copy_data*/ splineik_copy,
4653 /*new_data*/ splineik_new_data,
4654 /*get_constraint_targets*/ splineik_get_tars,
4655 /*flush_constraint_targets*/ splineik_flush_tars,
4656 /*get_target_matrix*/ splineik_get_tarmat,
4657 /*evaluate_constraint*/ nullptr,
4658};
4659
4660/* ----------- Pivot ------------- */
4661
4662static void pivotcon_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
4663{
4664 bPivotConstraint *data = static_cast<bPivotConstraint *>(con->data);
4665
4666 /* target only */
4667 func(con, (ID **)&data->tar, false, userdata);
4668}
4669
4671{
4672 if (con && list) {
4673 bPivotConstraint *data = static_cast<bPivotConstraint *>(con->data);
4675
4676 /* standard target-getting macro for single-target constraints */
4677 SINGLETARGET_GET_TARS(con, data->tar, data->subtarget, ct, list);
4678
4679 return 1;
4680 }
4681
4682 return 0;
4683}
4684
4685static void pivotcon_flush_tars(bConstraint *con, ListBase *list, bool no_copy)
4686{
4687 if (con && list) {
4688 bPivotConstraint *data = static_cast<bPivotConstraint *>(con->data);
4689 bConstraintTarget *ct = static_cast<bConstraintTarget *>(list->first);
4690
4691 /* the following macro is used for all standard single-target constraints */
4692 SINGLETARGET_FLUSH_TARS(con, data->tar, data->subtarget, ct, list, no_copy);
4693 }
4694}
4695
4696static void pivotcon_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *targets)
4697{
4698 bPivotConstraint *data = static_cast<bPivotConstraint *>(con->data);
4699 bConstraintTarget *ct = static_cast<bConstraintTarget *>(targets->first);
4700
4701 float pivot[3], vec[3];
4702 float rotMat[3][3];
4703
4704 /* pivot correction */
4705 float axis[3], angle;
4706
4707 const int rot_axis = std::clamp(
4708 int(data->rotAxis), int(PIVOTCON_AXIS_NONE), int(PIVOTCON_AXIS_Z));
4709
4710 /* firstly, check if pivoting should take place based on the current rotation */
4711 if (rot_axis != PIVOTCON_AXIS_NONE) {
4712
4713 float rot[3];
4714
4715 /* extract euler-rotation of target */
4716 mat4_to_eulO(rot, cob->rotOrder, cob->matrix);
4717
4718 /* check which range might be violated */
4719 if (rot_axis < PIVOTCON_AXIS_X) {
4720 /* Negative rotations (`rot_axis = 0 -> 2`). */
4721 if (rot[rot_axis] > 0.0f) {
4722 return;
4723 }
4724 }
4725 else {
4726 /* Positive rotations (`rot_axis = 3 -> 5`). */
4727 if (rot[rot_axis - PIVOTCON_AXIS_X] < 0.0f) {
4728 return;
4729 }
4730 }
4731 }
4732
4733 /* Find the pivot-point to use. */
4734 if (VALID_CONS_TARGET(ct)) {
4735 /* apply offset to target location */
4736 add_v3_v3v3(pivot, ct->matrix[3], data->offset);
4737 }
4738 else {
4739 /* no targets to worry about... */
4740 if ((data->flag & PIVOTCON_FLAG_OFFSET_ABS) == 0) {
4741 /* offset is relative to owner */
4742 add_v3_v3v3(pivot, cob->matrix[3], data->offset);
4743 }
4744 else {
4745 /* directly use the 'offset' specified as an absolute position instead */
4746 copy_v3_v3(pivot, data->offset);
4747 }
4748 }
4749
4750 /* get rotation matrix representing the rotation of the owner */
4751 /* TODO: perhaps we might want to include scaling based on the pivot too? */
4752 copy_m3_m4(rotMat, cob->matrix);
4753 normalize_m3(rotMat);
4754
4755 /* correct the pivot by the rotation axis otherwise the pivot translates when it shouldn't */
4756 mat3_normalized_to_axis_angle(axis, &angle, rotMat);
4757 if (angle) {
4758 float dvec[3];
4759 sub_v3_v3v3(vec, pivot, cob->matrix[3]);
4760 project_v3_v3v3(dvec, vec, axis);
4761 sub_v3_v3(pivot, dvec);
4762 }
4763
4764 /* perform the pivoting... */
4765 /* 1. take the vector from owner to the pivot */
4766 sub_v3_v3v3(vec, cob->matrix[3], pivot);
4767 /* 2. rotate this vector by the rotation of the object... */
4768 mul_m3_v3(rotMat, vec);
4769 /* 3. make the rotation in terms of the pivot now */
4770 add_v3_v3v3(cob->matrix[3], pivot, vec);
4771}
4772
4774 /*type*/ CONSTRAINT_TYPE_PIVOT,
4775 /*size*/ sizeof(bPivotConstraint),
4776 /*name*/ N_("Pivot"),
4777 /*struct_name*/ "bPivotConstraint",
4778 /*free_data*/ nullptr,
4779 /*id_looper*/ pivotcon_id_looper,
4780 /*copy_data*/ nullptr,
4781 /*new_data*/ nullptr,
4782 /* XXX: might be needed to get 'normal' pivot behavior. */
4783 /*get_constraint_targets*/ pivotcon_get_tars,
4784 /*flush_constraint_targets*/ pivotcon_flush_tars,
4785 /*get_target_matrix*/ default_get_tarmat,
4786 /*evaluate_constraint*/ pivotcon_evaluate,
4787};
4788
4789/* ----------- Follow Track ------------- */
4790
4791static void followtrack_new_data(void *cdata)
4792{
4794
4795 data->clip = nullptr;
4797}
4798
4799static void followtrack_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
4800{
4802
4803 func(con, (ID **)&data->clip, true, userdata);
4804 func(con, (ID **)&data->camera, false, userdata);
4805 func(con, (ID **)&data->depth_ob, false, userdata);
4806}
4807
4809{
4811
4812 if (data->flag & FOLLOWTRACK_ACTIVECLIP) {
4813 Scene *scene = cob->scene;
4814 return scene->clip;
4815 }
4816
4817 return data->clip;
4818}
4819
4821{
4822 MovieClip *clip = followtrack_tracking_clip_get(con, cob);
4823 MovieTracking *tracking = &clip->tracking;
4825
4826 if (data->object[0]) {
4827 return BKE_tracking_object_get_named(tracking, data->object);
4828 }
4829 return BKE_tracking_object_get_camera(tracking);
4830}
4831
4833{
4835
4836 if (data->camera == nullptr) {
4837 Scene *scene = cob->scene;
4838 return scene->camera;
4839 }
4840
4841 return data->camera;
4842}
4843
4862
4864 bConstraint *con,
4865 bConstraintOb *cob)
4866{
4868
4869 context->flag = data->flag;
4870 context->frame_method = data->frame_method;
4871
4872 context->depsgraph = cob->depsgraph;
4873 context->scene = cob->scene;
4874
4875 context->clip = followtrack_tracking_clip_get(con, cob);
4876 context->camera_object = followtrack_camera_object_get(con, cob);
4877 if (context->clip == nullptr || context->camera_object == nullptr) {
4878 return false;
4879 }
4880 context->depth_object = data->depth_ob;
4881
4882 context->tracking = &context->clip->tracking;
4883 context->tracking_object = followtrack_tracking_object_get(con, cob);
4884 if (context->tracking_object == nullptr) {
4885 return false;
4886 }
4887
4888 context->track = BKE_tracking_object_find_track_with_name(context->tracking_object, data->track);
4889 if (context->track == nullptr) {
4890 return false;
4891 }
4892
4893 context->depsgraph_time = DEG_get_ctime(context->depsgraph);
4894 context->clip_frame = BKE_movieclip_remap_scene_to_clip_frame(context->clip,
4895 context->depsgraph_time);
4896
4897 return true;
4898}
4899
4901 bConstraintOb *cob)
4902{
4903 Object *camera_object = context->camera_object;
4904 MovieTracking *tracking = context->tracking;
4905 MovieTrackingTrack *track = context->track;
4906 MovieTrackingObject *tracking_object = context->tracking_object;
4907
4908 /* Matrix of the object which is being solved prior to this constraint. */
4909 float obmat[4][4];
4910 copy_m4_m4(obmat, cob->matrix);
4911
4912 /* Object matrix of the camera. */
4913 float camera_obmat[4][4];
4914 copy_m4_m4(camera_obmat, camera_object->object_to_world().ptr());
4915
4916 /* Calculate inverted matrix of the solved camera at the current time. */
4917 float reconstructed_camera_mat[4][4];
4919 tracking, tracking_object, context->clip_frame, reconstructed_camera_mat);
4920 float reconstructed_camera_mat_inv[4][4];
4921 invert_m4_m4(reconstructed_camera_mat_inv, reconstructed_camera_mat);
4922
4923 mul_m4_series(cob->matrix, obmat, camera_obmat, reconstructed_camera_mat_inv);
4924 translate_m4(cob->matrix, track->bundle_pos[0], track->bundle_pos[1], track->bundle_pos[2]);
4925}
4926
4928 bConstraintOb *cob)
4929{
4930 Object *camera_object = context->camera_object;
4931 MovieTrackingTrack *track = context->track;
4932
4933 /* Matrix of the object which is being solved prior to this constraint. */
4934 float obmat[4][4];
4935 copy_m4_m4(obmat, cob->matrix);
4936
4937 float reconstructed_camera_mat[4][4];
4938 BKE_tracking_get_camera_object_matrix(camera_object, reconstructed_camera_mat);
4939
4940 mul_m4_m4m4(cob->matrix, obmat, reconstructed_camera_mat);
4941 translate_m4(cob->matrix, track->bundle_pos[0], track->bundle_pos[1], track->bundle_pos[2]);
4942}
4943
4945{
4946 MovieTrackingTrack *track = context->track;
4947 if ((track->flag & TRACK_HAS_BUNDLE) == 0) {
4948 return;
4949 }
4950
4951 if ((context->tracking_object->flag & TRACKING_OBJECT_CAMERA) == 0) {
4953 return;
4954 }
4955
4957}
4958
4959/* Apply undistortion if it is enabled in constraint settings. */
4961 const int clip_width,
4962 const int clip_height,
4963 float marker_position[2])
4964{
4965 if ((context->flag & FOLLOWTRACK_USE_UNDISTORTION) == 0) {
4966 return;
4967 }
4968
4969 /* Undistortion need to happen in pixel space. */
4970 marker_position[0] *= clip_width;
4971 marker_position[1] *= clip_height;
4972
4974 context->tracking, clip_width, clip_height, marker_position, marker_position);
4975
4976 /* Normalize pixel coordinates back. */
4977 marker_position[0] /= clip_width;
4978 marker_position[1] /= clip_height;
4979}
4980
4981/* Modify the marker position matching the frame fitting method. */
4983 const int clip_width,
4984 const int clip_height,
4985 float marker_position[2])
4986{
4987 if (context->frame_method == FOLLOWTRACK_FRAME_STRETCH) {
4988 return;
4989 }
4990
4991 Scene *scene = context->scene;
4992 MovieClip *clip = context->clip;
4993
4994 /* apply clip display aspect */
4995 const float w_src = clip_width * clip->aspx;
4996 const float h_src = clip_height * clip->aspy;
4997
4998 const float w_dst = scene->r.xsch * scene->r.xasp;
4999 const float h_dst = scene->r.ysch * scene->r.yasp;
5000
5001 const float asp_src = w_src / h_src;
5002 const float asp_dst = w_dst / h_dst;
5003
5004 if (fabsf(asp_src - asp_dst) < FLT_EPSILON) {
5005 return;
5006 }
5007
5008 if ((asp_src > asp_dst) == (context->frame_method == FOLLOWTRACK_FRAME_CROP)) {
5009 /* fit X */
5010 float div = asp_src / asp_dst;
5011 float cent = float(clip_width) / 2.0f;
5012
5013 marker_position[0] = (((marker_position[0] * clip_width - cent) * div) + cent) / clip_width;
5014 }
5015 else {
5016 /* fit Y */
5017 float div = asp_dst / asp_src;
5018 float cent = float(clip_height) / 2.0f;
5019
5020 marker_position[1] = (((marker_position[1] * clip_height - cent) * div) + cent) / clip_height;
5021 }
5022}
5023
5024/* Effectively this is a Z-depth of the object form the movie clip camera.
5025 * The idea is to preserve this depth while moving the object in 2D. */
5027 bConstraintOb *cob)
5028{
5029 Object *camera_object = context->camera_object;
5030
5031 float camera_matrix[4][4];
5032 BKE_object_where_is_calc_mat4(camera_object, camera_matrix);
5033
5034 const float z_axis[3] = {0.0f, 0.0f, 1.0f};
5035
5036 /* Direction of camera's local Z axis in the world space. */
5037 float camera_axis[3];
5038 mul_v3_mat3_m4v3(camera_axis, camera_matrix, z_axis);
5039
5040 /* Distance to projection plane. */
5041 float vec[3];
5042 copy_v3_v3(vec, cob->matrix[3]);
5043 sub_v3_v3(vec, camera_matrix[3]);
5044
5045 float projection[3];
5046 project_v3_v3v3(projection, vec, camera_axis);
5047
5048 return len_v3(projection);
5049}
5050
5051/* For the evaluated constraint object project it to the surface of the depth object. */
5053 bConstraintOb *cob)
5054{
5055 if (context->depth_object == nullptr) {
5056 return;
5057 }
5058
5059 Object *depth_object = context->depth_object;
5060 const Mesh *depth_mesh = BKE_object_get_evaluated_mesh(depth_object);
5061 if (depth_mesh == nullptr) {
5062 return;
5063 }
5064
5065 float depth_object_mat_inv[4][4];
5066 invert_m4_m4(depth_object_mat_inv, depth_object->object_to_world().ptr());
5067
5068 float ray_start[3], ray_end[3];
5070 ray_start, depth_object_mat_inv, context->camera_object->object_to_world().location());
5071 mul_v3_m4v3(ray_end, depth_object_mat_inv, cob->matrix[3]);
5072
5073 float ray_direction[3];
5074 sub_v3_v3v3(ray_direction, ray_end, ray_start);
5075 normalize_v3(ray_direction);
5076
5077 blender::bke::BVHTreeFromMesh tree_data = depth_mesh->bvh_corner_tris();
5078
5079 BVHTreeRayHit hit;
5081 hit.index = -1;
5082
5083 const int result = BLI_bvhtree_ray_cast(tree_data.tree,
5084 ray_start,
5085 ray_direction,
5086 0.0f,
5087 &hit,
5088 tree_data.raycast_callback,
5089 &tree_data);
5090
5091 if (result != -1) {
5092 mul_v3_m4v3(cob->matrix[3], depth_object->object_to_world().ptr(), hit.co);
5093 }
5094}
5095
5097{
5098 Scene *scene = context->scene;
5099 MovieClip *clip = context->clip;
5100 MovieTrackingTrack *track = context->track;
5101 Object *camera_object = context->camera_object;
5102 const float clip_frame = context->clip_frame;
5103 const float aspect = (scene->r.xsch * scene->r.xasp) / (scene->r.ysch * scene->r.yasp);
5104
5105 const float object_depth = followtrack_distance_from_viewplane_get(context, cob);
5106 if (object_depth < FLT_EPSILON) {
5107 return;
5108 }
5109
5110 int clip_width, clip_height;
5111 BKE_movieclip_get_size(clip, nullptr, &clip_width, &clip_height);
5112
5113 float marker_position[2];
5114 BKE_tracking_marker_get_subframe_position(track, clip_frame, marker_position);
5115
5116 followtrack_undistort_if_needed(context, clip_width, clip_height, marker_position);
5117 followtrack_fit_frame(context, clip_width, clip_height, marker_position);
5118
5119 float rmat[4][4];
5122 BKE_camera_params_from_object(&params, camera_object);
5123
5124 if (params.is_ortho) {
5125 float vec[3];
5126 vec[0] = params.ortho_scale * (marker_position[0] - 0.5f + params.shiftx);
5127 vec[1] = params.ortho_scale * (marker_position[1] - 0.5f + params.shifty);
5128 vec[2] = -object_depth;
5129
5130 if (aspect > 1.0f) {
5131 vec[1] /= aspect;
5132 }
5133 else {
5134 vec[0] *= aspect;
5135 }
5136
5137 float disp[3];
5138 mul_v3_m4v3(disp, camera_object->object_to_world().ptr(), vec);
5139
5140 copy_m4_m4(rmat, camera_object->object_to_world().ptr());
5141 zero_v3(rmat[3]);
5142 mul_m4_m4m4(cob->matrix, cob->matrix, rmat);
5143
5144 copy_v3_v3(cob->matrix[3], disp);
5145 }
5146 else {
5147 const float d = (object_depth * params.sensor_x) / (2.0f * params.lens);
5148
5149 float vec[3];
5150 vec[0] = d * (2.0f * (marker_position[0] + params.shiftx) - 1.0f);
5151 vec[1] = d * (2.0f * (marker_position[1] + params.shifty) - 1.0f);
5152 vec[2] = -object_depth;
5153
5154 if (aspect > 1.0f) {
5155 vec[1] /= aspect;
5156 }
5157 else {
5158 vec[0] *= aspect;
5159 }
5160
5161 float disp[3];
5162 mul_v3_m4v3(disp, camera_object->object_to_world().ptr(), vec);
5163
5164 /* apply camera rotation so Z-axis would be co-linear */
5165 copy_m4_m4(rmat, camera_object->object_to_world().ptr());
5166 zero_v3(rmat[3]);
5167 mul_m4_m4m4(cob->matrix, cob->matrix, rmat);
5168
5169 copy_v3_v3(cob->matrix[3], disp);
5170 }
5171
5173}
5174
5175static void followtrack_evaluate(bConstraint *con, bConstraintOb *cob, ListBase * /*targets*/)
5176{
5177 FollowTrackContext context;
5178 if (!followtrack_context_init(&context, con, cob)) {
5179 return;
5180 }
5181
5183 if (data->flag & FOLLOWTRACK_USE_3D_POSITION) {
5185 return;
5186 }
5187
5189}
5190
5193 /*size*/ sizeof(bFollowTrackConstraint),
5194 /*name*/ N_("Follow Track"),
5195 /*struct_name*/ "bFollowTrackConstraint",
5196 /*free_data*/ nullptr,
5197 /*id_looper*/ followtrack_id_looper,
5198 /*copy_data*/ nullptr,
5199 /*new_data*/ followtrack_new_data,
5200 /*get_constraint_targets*/ nullptr,
5201 /*flush_constraint_targets*/ nullptr,
5202 /*get_target_matrix*/ nullptr,
5203 /*evaluate_constraint*/ followtrack_evaluate,
5204};
5205
5206/* ----------- Camera Solver ------------- */
5207
5208static void camerasolver_new_data(void *cdata)
5209{
5211
5212 data->clip = nullptr;
5214}
5215
5216static void camerasolver_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
5217{
5219
5220 func(con, (ID **)&data->clip, true, userdata);
5221}
5222
5223static void camerasolver_evaluate(bConstraint *con, bConstraintOb *cob, ListBase * /*targets*/)
5224{
5225 Depsgraph *depsgraph = cob->depsgraph;
5226 Scene *scene = cob->scene;
5228 MovieClip *clip = data->clip;
5229
5230 if (data->flag & CAMERASOLVER_ACTIVECLIP) {
5231 clip = scene->clip;
5232 }
5233
5234 if (clip) {
5235 float mat[4][4], obmat[4][4];
5236 MovieTracking *tracking = &clip->tracking;
5237 MovieTrackingObject *tracking_object = BKE_tracking_object_get_camera(tracking);
5238 const float ctime = DEG_get_ctime(depsgraph);
5239 const float framenr = BKE_movieclip_remap_scene_to_clip_frame(clip, ctime);
5240
5241 BKE_tracking_camera_get_reconstructed_interpolate(tracking, tracking_object, framenr, mat);
5242
5243 copy_m4_m4(obmat, cob->matrix);
5244
5245 mul_m4_m4m4(cob->matrix, obmat, mat);
5246 }
5247}
5248
5251 /*size*/ sizeof(bCameraSolverConstraint),
5252 /*name*/ N_("Camera Solver"),
5253 /*struct_name*/ "bCameraSolverConstraint",
5254 /*free_data*/ nullptr,
5255 /*id_looper*/ camerasolver_id_looper,
5256 /*copy_data*/ nullptr,
5257 /*new_data*/ camerasolver_new_data,
5258 /*get_constraint_targets*/ nullptr,
5259 /*flush_constraint_targets*/ nullptr,
5260 /*get_target_matrix*/ nullptr,
5261 /*evaluate_constraint*/ camerasolver_evaluate,
5262};
5263
5264/* ----------- Object Solver ------------- */
5265
5266static void objectsolver_new_data(void *cdata)
5267{
5269
5270 data->clip = nullptr;
5272 unit_m4(data->invmat);
5273}
5274
5275static void objectsolver_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
5276{
5278
5279 func(con, (ID **)&data->clip, false, userdata);
5280 func(con, (ID **)&data->camera, false, userdata);
5281}
5282
5283static void objectsolver_evaluate(bConstraint *con, bConstraintOb *cob, ListBase * /*targets*/)
5284{
5285 Depsgraph *depsgraph = cob->depsgraph;
5286 Scene *scene = cob->scene;
5288 MovieClip *clip = data->clip;
5289 Object *camob = data->camera ? data->camera : scene->camera;
5290
5291 if (data->flag & OBJECTSOLVER_ACTIVECLIP) {
5292 clip = scene->clip;
5293 }
5294 if (!camob || !clip) {
5295 return;
5296 }
5297
5298 MovieTracking *tracking = &clip->tracking;
5299 MovieTrackingObject *tracking_object = BKE_tracking_object_get_named(tracking, data->object);
5300 if (!tracking_object) {
5301 return;
5302 }
5303
5304 float mat[4][4], obmat[4][4], imat[4][4], parmat[4][4];
5305 float ctime = DEG_get_ctime(depsgraph);
5306 float framenr = BKE_movieclip_remap_scene_to_clip_frame(clip, ctime);
5307
5308 BKE_tracking_camera_get_reconstructed_interpolate(tracking, tracking_object, framenr, mat);
5309
5310 invert_m4_m4(imat, mat);
5311 mul_m4_m4m4(parmat, camob->object_to_world().ptr(), imat);
5312
5313 copy_m4_m4(obmat, cob->matrix);
5314
5315 /* Recalculate the inverse matrix if requested. */
5316 if (data->flag & OBJECTSOLVER_SET_INVERSE) {
5317 invert_m4_m4(data->invmat, parmat);
5318
5320
5321 /* Write the computed matrix back to the master copy if in copy-on-eval evaluation. */
5323
5324 if (orig_con != nullptr) {
5325 bObjectSolverConstraint *orig_data = static_cast<bObjectSolverConstraint *>(orig_con->data);
5326
5327 copy_m4_m4(orig_data->invmat, data->invmat);
5328 orig_data->flag &= ~OBJECTSOLVER_SET_INVERSE;
5329 }
5330 }
5331
5332 mul_m4_series(cob->matrix, parmat, data->invmat, obmat);
5333}
5334
5337 /*size*/ sizeof(bObjectSolverConstraint),
5338 /*name*/ N_("Object Solver"),
5339 /*struct_name*/ "bObjectSolverConstraint",
5340 /*free_data*/ nullptr,
5341 /*id_looper*/ objectsolver_id_looper,
5342 /*copy_data*/ nullptr,
5343 /*new_data*/ objectsolver_new_data,
5344 /*get_constraint_targets*/ nullptr,
5345 /*flush_constraint_targets*/ nullptr,
5346 /*get_target_matrix*/ nullptr,
5347 /*evaluate_constraint*/ objectsolver_evaluate,
5348};
5349
5350/* ----------- Transform Cache ------------- */
5351
5352static void transformcache_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
5353{
5355 func(con, (ID **)&data->cache_file, true, userdata);
5356}
5357
5359{
5360#if defined(WITH_ALEMBIC) || defined(WITH_USD)
5362 Scene *scene = cob->scene;
5363
5364 CacheFile *cache_file = data->cache_file;
5365
5366 if (!cache_file) {
5367 return;
5368 }
5369
5370 const float frame = DEG_get_ctime(cob->depsgraph);
5371 const double time = BKE_cachefile_time_offset(
5372 cache_file, double(frame), scene->frames_per_second());
5373
5374 if (!data->reader || !STREQ(data->reader_object_path, data->object_path)) {
5375 STRNCPY(data->reader_object_path, data->object_path);
5376 BKE_cachefile_reader_open(cache_file, &data->reader, cob->ob, data->object_path);
5377 }
5378
5379 switch (cache_file->type) {
5381# ifdef WITH_ALEMBIC
5382 ABC_get_transform(data->reader, cob->matrix, time, cache_file->scale);
5383# endif
5384 break;
5385 case CACHEFILE_TYPE_USD:
5386# ifdef WITH_USD
5388 data->reader, cob->matrix, time * scene->frames_per_second(), cache_file->scale);
5389# endif
5390 break;
5392 break;
5393 }
5394#else
5395 UNUSED_VARS(con, cob);
5396#endif
5397
5398 UNUSED_VARS(targets);
5399}
5400
5402{
5403 bTransformCacheConstraint *src = static_cast<bTransformCacheConstraint *>(srccon->data);
5404 bTransformCacheConstraint *dst = static_cast<bTransformCacheConstraint *>(con->data);
5405
5406 STRNCPY(dst->object_path, src->object_path);
5407 dst->cache_file = src->cache_file;
5408 dst->reader = nullptr;
5409 dst->reader_object_path[0] = '\0';
5410}
5411
5413{
5415
5416 if (data->reader) {
5417 BKE_cachefile_reader_free(data->cache_file, &data->reader);
5418 data->reader_object_path[0] = '\0';
5419 }
5420}
5421
5422static void transformcache_new_data(void *cdata)
5423{
5425
5426 data->cache_file = nullptr;
5427}
5428
5431 /*size*/ sizeof(bTransformCacheConstraint),
5432 /*name*/ N_("Transform Cache"),
5433 /*struct_name*/ "bTransformCacheConstraint",
5434 /*free_data*/ transformcache_free,
5435 /*id_looper*/ transformcache_id_looper,
5436 /*copy_data*/ transformcache_copy,
5437 /*new_data*/ transformcache_new_data,
5438 /*get_constraint_targets*/ nullptr,
5439 /*flush_constraint_targets*/ nullptr,
5440 /*get_target_matrix*/ nullptr,
5441 /*evaluate_constraint*/ transformcache_evaluate,
5442};
5443
5444/* ---------- Geometry Attribute Constraint ----------- */
5445
5465
5479
5480static void value_attribute_to_matrix(float r_matrix[4][4],
5481 const blender::GPointer value,
5482 const Attribute_Data_Type data_type)
5483{
5484 switch (data_type) {
5486 copy_v3_v3(r_matrix[3], *value.get<blender::float3>());
5487 return;
5489 quat_to_mat4(r_matrix, *value.get<blender::float4>());
5490 return;
5492 copy_m4_m4(r_matrix, value.get<blender::float4x4>()->ptr());
5493 return;
5494 }
5496}
5497
5500 const blender::bke::AttrDomain domain)
5501{
5502 if (const blender::bke::GeometryComponent *component = geometry.get_component(type)) {
5503 return component->attribute_domain_size(domain) != 0;
5504 }
5505 return false;
5506}
5507
5510{
5511 /* Choose the other component based on a consistent order, rather than some more complicated
5512 * heuristic. This is the same order visible in the spreadsheet and used in the ray-cast node. */
5513 static const blender::Array<blender::bke::GeometryComponent::Type> supported_types = {
5519 for (const blender::bke::GeometryComponent::Type src_type : supported_types) {
5520 if (component_is_available(geometry, src_type, domain)) {
5521 return geometry.get_component(src_type);
5522 }
5523 }
5524
5525 return nullptr;
5526}
5527
5529{
5531 MEM_SAFE_FREE(data->attribute_name);
5532}
5533
5534static void geometry_attribute_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
5535{
5537 func(con, (ID **)&data->target, false, userdata);
5538}
5539
5541{
5542 const auto *src = static_cast<bGeometryAttributeConstraint *>(srccon->data);
5543 auto *dst = static_cast<bGeometryAttributeConstraint *>(con->data);
5544 dst->attribute_name = BLI_strdup_null(src->attribute_name);
5545}
5546
5547static void geometry_attribute_new_data(void *cdata)
5548{
5550 data->attribute_name = BLI_strdup("position");
5551 data->flags = MIX_LOC | MIX_ROT | MIX_SCALE;
5552}
5553
5555{
5556 if (!con || !list) {
5557 return 0;
5558 }
5561
5562 SINGLETARGETNS_GET_TARS(con, data->target, ct, list);
5563
5564 return 1;
5565}
5566
5567static void geometry_attribute_flush_tars(bConstraint *con, ListBase *list, const bool no_copy)
5568{
5569 if (!con || !list) {
5570 return;
5571 }
5573 bConstraintTarget *ct = static_cast<bConstraintTarget *>(list->first);
5574
5575 SINGLETARGETNS_FLUSH_TARS(con, data->target, ct, list, no_copy);
5576}
5577
5578static bool geometry_attribute_get_tarmat(Depsgraph * /*depsgraph*/,
5579 bConstraint *con,
5580 bConstraintOb * /*cob*/,
5582 float /*ctime*/)
5583{
5584 using namespace blender;
5586 con->data);
5587
5588 if (!VALID_CONS_TARGET(ct)) {
5589 return false;
5590 }
5591
5592 unit_m4(ct->matrix);
5593
5595 static_cast<Attribute_Domain>(acon->domain));
5596 const bke::AttrType sample_data_type = type_value_to_attribute(
5597 static_cast<Attribute_Data_Type>(acon->data_type));
5599
5600 const bke::GeometryComponent *component = find_source_component(target_eval, domain);
5601 if (component == nullptr) {
5602 return false;
5603 }
5604
5605 const std::optional<bke::AttributeAccessor> optional_attributes = component->attributes();
5606 if (!optional_attributes.has_value()) {
5607 return false;
5608 }
5609
5610 const bke::AttributeAccessor &attributes = *optional_attributes;
5611 const GVArray attribute = *attributes.lookup(acon->attribute_name, domain, sample_data_type);
5612
5613 if (attribute.is_empty()) {
5614 return false;
5615 }
5616
5617 const int index = std::clamp<int>(acon->sample_index, 0, attribute.size() - 1);
5618
5619 const CPPType &type = attribute.type();
5620 BUFFER_FOR_CPP_TYPE_VALUE(type, sampled_value);
5621 attribute.get_to_uninitialized(index, sampled_value);
5622
5624 GPointer(type, sampled_value),
5625 static_cast<Attribute_Data_Type>(acon->data_type));
5626 type.destruct(sampled_value);
5627
5628 return true;
5629}
5630
5632{
5633 bConstraintTarget *ct = static_cast<bConstraintTarget *>(targets->first);
5635 con->data);
5636
5637 /* Only evaluate if there is a target. */
5638 if (!VALID_CONS_TARGET(ct)) {
5639 return;
5640 }
5641
5642 float target_mat[4][4];
5643 if (data->mix_mode == CON_ATTRIBUTE_MIX_REPLACE) {
5644 copy_m4_m4(target_mat, cob->matrix);
5645 }
5646 else {
5647 unit_m4(target_mat);
5648 }
5649
5650 float prev_location[3];
5651 float prev_rotation[3][3];
5652 float prev_size[3];
5653 mat4_to_loc_rot_size(prev_location, prev_rotation, prev_size, target_mat);
5654
5655 float next_location[3];
5656 float next_rotation[3][3];
5657 float next_size[3];
5658 mat4_to_loc_rot_size(next_location, next_rotation, next_size, ct->matrix);
5659
5660 switch (data->data_type) {
5662 loc_rot_size_to_mat4(target_mat, next_location, prev_rotation, prev_size);
5663 break;
5665 loc_rot_size_to_mat4(target_mat, prev_location, next_rotation, prev_size);
5666 break;
5668 if ((data->flags & MIX_LOC) && (data->flags & MIX_ROT) && (data->flags & MIX_SCALE)) {
5669 copy_m4_m4(target_mat, ct->matrix);
5670 }
5671 else {
5672 if (data->flags & MIX_LOC) {
5673 copy_v3_v3(prev_location, next_location);
5674 }
5675 if (data->flags & MIX_ROT) {
5676 copy_m3_m3(prev_rotation, next_rotation);
5677 }
5678 if (data->flags & MIX_SCALE) {
5679 copy_v3_v3(prev_size, next_size);
5680 }
5681 loc_rot_size_to_mat4(target_mat, prev_location, prev_rotation, prev_size);
5682 }
5683 break;
5684 }
5685
5686 /* Finally, combine the matrices. */
5687 switch (data->mix_mode) {
5689 copy_m4_m4(cob->matrix, target_mat);
5690 break;
5691 /* Simple matrix multiplication. */
5693 mul_m4_m4m4(cob->matrix, target_mat, cob->matrix);
5694 break;
5696 mul_m4_m4m4(cob->matrix, cob->matrix, target_mat);
5697 break;
5698 /* Fully separate handling of channels. */
5700 mul_m4_m4m4_split_channels(cob->matrix, target_mat, cob->matrix);
5701 break;
5703 mul_m4_m4m4_split_channels(cob->matrix, cob->matrix, target_mat);
5704 break;
5705 }
5706
5707 if (data->apply_target_transform) {
5708 mul_m4_m4m4(cob->matrix, ct->tar->object_to_world().ptr(), cob->matrix);
5709 }
5710}
5711
5714 /*size*/ sizeof(bGeometryAttributeConstraint),
5715 /*name*/ N_("Geometry Attribute"),
5716 /*struct_name*/ "bGeometryAttributeConstraint",
5717 /*free_data*/ geometry_attribute_free_data,
5718 /*id_looper*/ geometry_attribute_id_looper,
5719 /*copy_data*/ geometry_attribute_copy_data,
5720 /*new_data*/ geometry_attribute_new_data,
5721 /*get_constraint_targets*/ geometry_attribute_get_tars,
5722 /*flush_constraint_targets*/ geometry_attribute_flush_tars,
5723 /*get_target_matrix*/ geometry_attribute_get_tarmat,
5724 /*evaluate_constraint*/ geometry_attribute_evaluate,
5725};
5726
5727/* ************************* Constraints Type-Info *************************** */
5728/* All of the constraints API functions use #bConstraintTypeInfo structs to carry out
5729 * and operations that involve constraint specific code.
5730 */
5731
5732/* These globals only ever get directly accessed in this file */
5734static short CTI_INIT = 1; /* when non-zero, the list needs to be updated */
5735
5736/* This function only gets called when CTI_INIT is non-zero */
5738{
5739 constraintsTypeInfo[0] = nullptr; /* 'Null' Constraint */
5740 constraintsTypeInfo[1] = &CTI_CHILDOF; /* ChildOf Constraint */
5741 constraintsTypeInfo[2] = &CTI_TRACKTO; /* TrackTo Constraint */
5742 constraintsTypeInfo[3] = &CTI_KINEMATIC; /* IK Constraint */
5743 constraintsTypeInfo[4] = &CTI_FOLLOWPATH; /* Follow-Path Constraint */
5744 constraintsTypeInfo[5] = &CTI_ROTLIMIT; /* Limit Rotation Constraint */
5745 constraintsTypeInfo[6] = &CTI_LOCLIMIT; /* Limit Location Constraint */
5746 constraintsTypeInfo[7] = &CTI_SIZELIMIT; /* Limit Scale Constraint */
5747 constraintsTypeInfo[8] = &CTI_ROTLIKE; /* Copy Rotation Constraint */
5748 constraintsTypeInfo[9] = &CTI_LOCLIKE; /* Copy Location Constraint */
5749 constraintsTypeInfo[10] = &CTI_SIZELIKE; /* Copy Scale Constraint */
5750 constraintsTypeInfo[11] = nullptr; /* Python/Script Constraint: DEPRECATED. */
5751 constraintsTypeInfo[12] = &CTI_ACTION; /* Action Constraint */
5752 constraintsTypeInfo[13] = &CTI_LOCKTRACK; /* Locked-Track Constraint */
5753 constraintsTypeInfo[14] = &CTI_DISTLIMIT; /* Limit Distance Constraint */
5754 constraintsTypeInfo[15] = &CTI_STRETCHTO; /* StretchTo Constraint */
5755 constraintsTypeInfo[16] = &CTI_MINMAX; /* Floor Constraint */
5756 constraintsTypeInfo[17] = nullptr; /* RigidBody Constraint: DEPRECATED. */
5757 constraintsTypeInfo[18] = &CTI_CLAMPTO; /* ClampTo Constraint */
5758 constraintsTypeInfo[19] = &CTI_TRANSFORM; /* Transformation Constraint */
5759 constraintsTypeInfo[20] = &CTI_SHRINKWRAP; /* Shrinkwrap Constraint */
5760 constraintsTypeInfo[21] = &CTI_DAMPTRACK; /* Damped TrackTo Constraint */
5761 constraintsTypeInfo[22] = &CTI_SPLINEIK; /* Spline IK Constraint */
5762 constraintsTypeInfo[23] = &CTI_TRANSLIKE; /* Copy Transforms Constraint */
5763 constraintsTypeInfo[24] = &CTI_SAMEVOL; /* Maintain Volume Constraint */
5764 constraintsTypeInfo[25] = &CTI_PIVOT; /* Pivot Constraint */
5765 constraintsTypeInfo[26] = &CTI_FOLLOWTRACK; /* Follow Track Constraint */
5766 constraintsTypeInfo[27] = &CTI_CAMERASOLVER; /* Camera Solver Constraint */
5767 constraintsTypeInfo[28] = &CTI_OBJECTSOLVER; /* Object Solver Constraint */
5768 constraintsTypeInfo[29] = &CTI_TRANSFORM_CACHE; /* Transform Cache Constraint */
5769 constraintsTypeInfo[30] = &CTI_ARMATURE; /* Armature Constraint */
5770 constraintsTypeInfo[31] = &CTI_ATTRIBUTE; /* Attribute Transform Constraint */
5771}
5772
5774{
5775 /* initialize the type-info list? */
5776 if (CTI_INIT) {
5778 CTI_INIT = 0;
5779 }
5780
5781 /* only return for valid types */
5782 if ((type >= CONSTRAINT_TYPE_NULL) && (type < NUM_CONSTRAINT_TYPES)) {
5783 /* there shouldn't be any segfaults here... */
5784 return constraintsTypeInfo[type];
5785 }
5786
5787 CLOG_WARN(&LOG, "No valid constraint type-info data available. Type = %i", type);
5788
5789 return nullptr;
5790}
5791
5793{
5794 /* only return typeinfo for valid constraints */
5795 if (con) {
5797 }
5798
5799 return nullptr;
5800}
5801
5802/* ************************* General Constraints API ************************** */
5803/* The functions here are called by various parts of Blender. Very few (should be none if possible)
5804 * constraint-specific code should occur here.
5805 */
5806
5807/* ---------- Data Management ------- */
5808
5812static void con_unlink_refs_cb(bConstraint * /*con*/,
5813 ID **idpoin,
5814 bool is_reference,
5815 void * /*user_data*/)
5816{
5817 if (*idpoin && is_reference) {
5818 id_us_min(*idpoin);
5819 }
5820}
5821
5829 bConstraint *con,
5830 ConstraintIDFunc func,
5831 const int /*flag*/,
5832 void *userdata)
5833{
5834 if (cti->id_looper) {
5835 cti->id_looper(con, func, userdata);
5836 }
5837
5838 func(con, (ID **)&con->space_object, false, userdata);
5839}
5840
5841void BKE_constraint_free_data_ex(bConstraint *con, bool do_id_user)
5842{
5843 if (con->data) {
5845
5846 if (cti) {
5847 /* perform any special freeing constraint may have */
5848 if (cti->free_data) {
5849 cti->free_data(con);
5850 }
5851
5852 /* unlink the referenced resources it uses */
5853 if (do_id_user) {
5855 }
5856 }
5857
5858 /* free constraint data now */
5859 MEM_freeN(con->data);
5860 }
5861}
5862
5867
5868void BKE_constraints_free_ex(ListBase *list, bool do_id_user)
5869{
5870 /* Free constraint data and also any extra data */
5871 LISTBASE_FOREACH (bConstraint *, con, list) {
5872 BKE_constraint_free_data_ex(con, do_id_user);
5873 }
5874
5875 /* Free the whole list */
5876 BLI_freelistN(list);
5877}
5878
5880{
5881 BKE_constraints_free_ex(list, true);
5882}
5883
5885{
5886 if (con) {
5888 BLI_freelinkN(list, con);
5889 return true;
5890 }
5891
5892 return false;
5893}
5894
5896{
5897 BKE_animdata_drivers_remove_for_rna_struct(ob->id, RNA_Constraint, con);
5898
5899 const short type = con->type;
5900 if (constraint_remove(list, con)) {
5901 /* ITASC needs to be rebuilt once a constraint is removed #26920. */
5903 BIK_clear_data(ob->pose);
5904 }
5905 return true;
5906 }
5907
5908 return false;
5909}
5910
5912 Scene *scene,
5913 Object *ob,
5914 bConstraint *con)
5915{
5916 if (!con) {
5917 return false;
5918 }
5919
5920 const float ctime = BKE_scene_frame_get(scene);
5921
5922 /* Do this all in the evaluated domain (e.g. shrinkwrap needs to access evaluated constraint
5923 * target mesh). */
5925 Object *ob_eval = DEG_get_evaluated(depsgraph, ob);
5926 bConstraint *con_eval = BKE_constraints_find_name(&ob_eval->constraints, con->name);
5927
5928 bConstraint *new_con = BKE_constraint_duplicate_ex(con_eval, 0, ID_IS_EDITABLE(ob));
5929 ListBase single_con = {new_con, new_con};
5930
5932 depsgraph, scene_eval, ob_eval, nullptr, CONSTRAINT_OBTYPE_OBJECT);
5933 /* Undo the effect of the current constraint stack evaluation. */
5934 mul_m4_m4m4(cob->matrix, ob_eval->constinv, cob->matrix);
5935
5936 /* Evaluate single constraint. */
5937 BKE_constraints_solve(depsgraph, &single_con, cob, ctime);
5938 /* Copy transforms back. This will leave the object in a bad state
5939 * as ob->constinv will be wrong until next evaluation. */
5941
5942 /* Free the copied constraint. */
5943 BKE_constraint_free_data(new_con);
5944 BLI_freelinkN(&single_con, new_con);
5945
5946 /* Apply transform from matrix. */
5947 BKE_object_apply_mat4(ob, ob_eval->object_to_world().ptr(), true, true);
5948
5949 return true;
5950}
5951
5953 Scene *scene,
5954 ListBase /*bConstraint*/ *constraints,
5955 Object *ob,
5956 bConstraint *con)
5957{
5958 if (!BKE_constraint_apply_for_object(depsgraph, scene, ob, con)) {
5959 return false;
5960 }
5961
5962 return BKE_constraint_remove_ex(constraints, ob, con);
5963}
5964
5966 Depsgraph *depsgraph, Scene *scene, Object *ob, bPoseChannel *pchan, bConstraint *con)
5967{
5968 if (!con) {
5969 return false;
5970 }
5971
5972 const float ctime = BKE_scene_frame_get(scene);
5973
5974 /* Do this all in the evaluated domain (e.g. shrinkwrap needs to access evaluated constraint
5975 * target mesh). */
5977 Object *ob_eval = DEG_get_evaluated(depsgraph, ob);
5978 bPoseChannel *pchan_eval = BKE_pose_channel_find_name(ob_eval->pose, pchan->name);
5979 bConstraint *con_eval = BKE_constraints_find_name(&pchan_eval->constraints, con->name);
5980
5981 bConstraint *new_con = BKE_constraint_duplicate_ex(con_eval, 0, ID_IS_EDITABLE(ob));
5982 ListBase single_con;
5983 single_con.first = new_con;
5984 single_con.last = new_con;
5985
5986 float vec[3];
5987 copy_v3_v3(vec, pchan_eval->pose_mat[3]);
5988
5990 depsgraph, scene_eval, ob_eval, pchan_eval, CONSTRAINT_OBTYPE_BONE);
5991 /* Undo the effects of currently applied constraints. */
5992 mul_m4_m4m4(cob->matrix, pchan_eval->constinv, cob->matrix);
5993 /* Evaluate single constraint. */
5994 BKE_constraints_solve(depsgraph, &single_con, cob, ctime);
5996
5997 /* Free the copied constraint. */
5998 BKE_constraint_free_data(new_con);
5999 BLI_freelinkN(&single_con, new_con);
6000
6001 /* Prevent constraints breaking a chain. */
6002 if (pchan->bone->flag & BONE_CONNECTED) {
6003 copy_v3_v3(pchan_eval->pose_mat[3], vec);
6004 }
6005
6006 /* Apply transform from matrix. */
6007 float mat[4][4];
6008 BKE_armature_mat_pose_to_bone(pchan, pchan_eval->pose_mat, mat);
6009 BKE_pchan_apply_mat4(pchan, mat, true);
6010
6011 return true;
6012}
6013
6015 Scene *scene,
6016 ListBase /*bConstraint*/ *constraints,
6017 Object *ob,
6018 bConstraint *con,
6019 bPoseChannel *pchan)
6020{
6021 if (!BKE_constraint_apply_for_pose(depsgraph, scene, ob, pchan, con)) {
6022 return false;
6023 }
6024
6025 return BKE_constraint_remove_ex(constraints, ob, con);
6026}
6027
6032
6033/* ......... */
6034
6035/* Creates a new constraint, initializes its data, and returns it */
6036static bConstraint *add_new_constraint_internal(const char *name, short type)
6037{
6038 bConstraint *con = MEM_callocN<bConstraint>("Constraint");
6040 const char *newName;
6041
6042 /* Set up a generic constraint data-block. */
6043 con->type = type;
6045 con->enforce = 1.0f;
6046
6047 /* Only open the main panel when constraints are created, not the sub-panels. */
6050 /* Expand the two sub-panels in the cases where the main panel barely has any properties. */
6052 }
6053
6054 /* Determine a basic name, and info */
6055 if (cti) {
6056 /* initialize constraint data */
6057 con->data = MEM_callocN(cti->size, cti->struct_name);
6058
6059 /* only constraints that change any settings need this */
6060 if (cti->new_data) {
6061 cti->new_data(con->data);
6062 }
6063
6064 /* if no name is provided, use the type of the constraint as the name */
6065 newName = (name && name[0]) ? name : DATA_(cti->name);
6066 }
6067 else {
6068 /* if no name is provided, use the generic "Const" name */
6069 /* NOTE: any constraint type that gets here really shouldn't get added... */
6070 newName = (name && name[0]) ? name : DATA_("Const");
6071 }
6072
6073 /* copy the name */
6074 STRNCPY_UTF8(con->name, newName);
6075
6076 /* return the new constraint */
6077 return con;
6078}
6079
6080/* Add a newly created constraint to the constraint list. */
6082{
6083 ListBase *list;
6084
6085 /* find the constraint stack - bone or object? */
6086 list = (pchan) ? (&pchan->constraints) : (&ob->constraints);
6087
6088 if (list) {
6089 /* add new constraint to end of list of constraints before ensuring that it has a unique name
6090 * (otherwise unique-naming code will fail, since it assumes element exists in list)
6091 */
6092 BLI_addtail(list, con);
6093 BKE_constraint_unique_name(con, list);
6094
6095 /* make this constraint the active one */
6096 BKE_constraints_active_set(list, con);
6097 }
6098}
6099
6100/* if pchan is not nullptr then assume we're adding a pose constraint */
6102 bPoseChannel *pchan,
6103 const char *name,
6104 short type)
6105{
6106 bConstraint *con;
6107
6108 /* add the constraint */
6109 con = add_new_constraint_internal(name, type);
6110
6111 add_new_constraint_to_list(ob, pchan, con);
6112
6113 /* set type+owner specific immutable settings */
6114 /* TODO: does action constraint need anything here - i.e. spaceonce? */
6115 switch (type) {
6117 /* If this constraint is being added to a pose-channel, make sure
6118 * the constraint gets evaluated in pose-space. */
6119 if (pchan) {
6121 }
6122 break;
6123 }
6125 /* The Before or Split modes require computing in local space, but
6126 * for objects the Local space doesn't make sense (#78462, D6095 etc).
6127 * So only default to Before (Split) if the constraint is on a bone. */
6128 if (pchan) {
6129 bActionConstraint *data = static_cast<bActionConstraint *>(con->data);
6130 data->mix_mode = ACTCON_MIX_BEFORE_SPLIT;
6132 }
6133 break;
6134 }
6135 }
6136
6137 return con;
6138}
6139
6141{
6143 return false;
6144 }
6145
6146 return (con->flag & CONSTRAINT_BBONE_SHAPE) || (con->type == CONSTRAINT_TYPE_ARMATURE);
6147}
6148
6149/* ......... */
6150
6152 bPoseChannel *pchan,
6153 const char *name,
6154 short type)
6155{
6156 if (pchan == nullptr) {
6157 return nullptr;
6158 }
6159
6160 return add_new_constraint(ob, pchan, name, type);
6161}
6162
6164{
6165 return add_new_constraint(ob, nullptr, name, type);
6166}
6167
6168/* ......... */
6169
6171 ConstraintIDFunc func,
6172 const int flag,
6173 void *userdata)
6174{
6175 LISTBASE_FOREACH (bConstraint *, con, conlist) {
6177
6178 if (cti) {
6179 con_invoke_id_looper(cti, con, func, flag, userdata);
6180 }
6181 }
6182}
6183
6184/* ......... */
6185
6186/* helper for BKE_constraints_copy(), to be used for making sure that ID's are valid */
6187static void con_extern_cb(bConstraint * /*con*/,
6188 ID **idpoin,
6189 bool /*is_reference*/,
6190 void * /*user_data*/)
6191{
6192 if (*idpoin && ID_IS_LINKED(*idpoin)) {
6193 id_lib_extern(*idpoin);
6194 }
6195}
6196
6202 ID **idpoin,
6203 bool is_reference,
6204 void * /*user_data*/)
6205{
6206 /* Increment user-count if this is a reference type. */
6207 if ((*idpoin) && (is_reference)) {
6208 id_us_plus(*idpoin);
6209 }
6210}
6211
6214 bConstraint *src,
6215 const int flag,
6216 const bool do_extern)
6217{
6219
6220 /* make a new copy of the constraint's data */
6221 dst->data = MEM_dupallocN(dst->data);
6222
6223 /* only do specific constraints if required */
6224 if (cti) {
6225 /* perform custom copying operations if needed */
6226 if (cti->copy_data) {
6227 cti->copy_data(dst, src);
6228 }
6229
6230 /* Fix user-counts for all referenced data that need it. */
6231 if ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) {
6233 }
6234
6235 /* For proxies we don't want to make external. */
6236 if (do_extern) {
6237 /* go over used ID-links for this constraint to ensure that they are valid for proxies */
6238 con_invoke_id_looper(cti, dst, con_extern_cb, IDWALK_NOP, nullptr);
6239 }
6240 }
6241}
6242
6243bConstraint *BKE_constraint_duplicate_ex(bConstraint *src, const int flag, const bool do_extern)
6244{
6245 bConstraint *dst = static_cast<bConstraint *>(MEM_dupallocN(src));
6246 constraint_copy_data_ex(dst, src, flag, do_extern);
6247 dst->next = dst->prev = nullptr;
6248 return dst;
6249}
6250
6252{
6253 if (pchan == nullptr) {
6254 return nullptr;
6255 }
6256
6258 add_new_constraint_to_list(ob, pchan, new_con);
6259 return new_con;
6260}
6261
6263{
6265 add_new_constraint_to_list(ob, nullptr, new_con);
6266 return new_con;
6267}
6268
6269void BKE_constraints_copy_ex(ListBase *dst, const ListBase *src, const int flag, bool do_extern)
6270{
6271 bConstraint *con, *srccon;
6272
6273 BLI_listbase_clear(dst);
6274 BLI_duplicatelist(dst, src);
6275
6276 for (con = static_cast<bConstraint *>(dst->first),
6277 srccon = static_cast<bConstraint *>(src->first);
6278 con && srccon;
6279 srccon = srccon->next, con = con->next)
6280 {
6281 constraint_copy_data_ex(con, srccon, flag, do_extern);
6284 }
6285 }
6286}
6287
6288void BKE_constraints_copy(ListBase *dst, const ListBase *src, bool do_extern)
6289{
6290 BKE_constraints_copy_ex(dst, src, 0, do_extern);
6291}
6292
6293/* ......... */
6294
6296{
6297 return static_cast<bConstraint *>(BLI_findstring(list, name, offsetof(bConstraint, name)));
6298}
6299
6301{
6302
6303 /* search for the first constraint with the 'active' flag set */
6304 if (list) {
6305 LISTBASE_FOREACH (bConstraint *, con, list) {
6306 if (con->flag & CONSTRAINT_ACTIVE) {
6307 return con;
6308 }
6309 }
6310 }
6311
6312 /* no active constraint found */
6313 return nullptr;
6314}
6315
6317{
6318
6319 if (list) {
6320 LISTBASE_FOREACH (bConstraint *, con_iter, list) {
6321 if (con_iter == con) {
6322 con_iter->flag |= CONSTRAINT_ACTIVE;
6323 }
6324 else {
6325 con_iter->flag &= ~CONSTRAINT_ACTIVE;
6326 }
6327 }
6328 }
6329}
6330
6332{
6334 ListBase *targets = nullptr;
6335
6336 if (con->type == CONSTRAINT_TYPE_ARMATURE) {
6337 targets = &((bArmatureConstraint *)con->data)->targets;
6338 }
6339
6340 if (targets && BLI_findindex(targets, tgt) != -1) {
6341 return con;
6342 }
6343 }
6344
6345 return nullptr;
6346}
6347
6349 bConstraintTarget *tgt,
6350 bPoseChannel **r_pchan)
6351{
6352 if (r_pchan != nullptr) {
6353 *r_pchan = nullptr;
6354 }
6355
6357
6358 if (result != nullptr) {
6359 return result;
6360 }
6361
6362 if (ob->pose != nullptr) {
6363 LISTBASE_FOREACH (bPoseChannel *, pchan, &ob->pose->chanbase) {
6364 result = constraint_list_find_from_target(&pchan->constraints, tgt);
6365
6366 if (result != nullptr) {
6367 if (r_pchan != nullptr) {
6368 *r_pchan = pchan;
6369 }
6370
6371 return result;
6372 }
6373 }
6374 }
6375
6376 return nullptr;
6377}
6378
6379/* Finds the original copy of the constraint based on an evaluated copy. */
6381 bPoseChannel *pchan,
6382 bConstraint *con,
6383 Object **r_orig_ob)
6384{
6385 Object *orig_ob = DEG_get_original(ob);
6386
6387 if (ELEM(orig_ob, nullptr, ob)) {
6388 return nullptr;
6389 }
6390
6391 /* Find which constraint list to use. */
6392 ListBase *constraints, *orig_constraints;
6393
6394 if (pchan != nullptr) {
6395 bPoseChannel *orig_pchan = pchan->orig_pchan;
6396
6397 if (orig_pchan == nullptr) {
6398 return nullptr;
6399 }
6400
6401 constraints = &pchan->constraints;
6402 orig_constraints = &orig_pchan->constraints;
6403 }
6404 else {
6405 constraints = &ob->constraints;
6406 orig_constraints = &orig_ob->constraints;
6407 }
6408
6409 /* Lookup the original constraint by index. */
6410 int index = BLI_findindex(constraints, con);
6411
6412 if (index >= 0) {
6413 bConstraint *orig_con = static_cast<bConstraint *>(BLI_findlink(orig_constraints, index));
6414
6415 /* Verify it has correct type and name. */
6416 if (orig_con && orig_con->type == con->type && STREQ(orig_con->name, con->name)) {
6417 if (r_orig_ob != nullptr) {
6418 *r_orig_ob = orig_ob;
6419 }
6420
6421 return orig_con;
6422 }
6423 }
6424
6425 return nullptr;
6426}
6427
6429{
6430 /* Write the computed distance back to the master copy if in copy-on-eval evaluation. */
6431 if (!DEG_is_active(cob->depsgraph)) {
6432 return nullptr;
6433 }
6434
6435 Object *orig_ob = nullptr;
6436 bConstraint *orig_con = constraint_find_original(cob->ob, cob->pchan, con, &orig_ob);
6437
6438 if (orig_con != nullptr) {
6440 }
6441
6442 return orig_con;
6443}
6444
6446{
6447 return (ID_IS_OVERRIDE_LIBRARY(ob) &&
6448 (con == nullptr || (con->flag & CONSTRAINT_OVERRIDE_LIBRARY_LOCAL) == 0));
6449}
6450
6451/* -------- Target-Matrix Stuff ------- */
6452
6454{
6455 BLI_listbase_clear(r_targets);
6456
6458
6459 if (!cti) {
6460 return 0;
6461 }
6462
6463 int count = 0;
6464
6465 /* Constraint-specific targets. */
6466 if (cti->get_constraint_targets) {
6467 count = cti->get_constraint_targets(con, r_targets);
6468 }
6469
6470 /* Add the custom target. */
6471 if (is_custom_space_needed(con)) {
6473 SINGLETARGET_GET_TARS(con, con->space_object, con->space_subtarget, ct, r_targets);
6476 count++;
6477 }
6478
6479 return count;
6480}
6481
6482void BKE_constraint_targets_flush(bConstraint *con, ListBase *targets, bool no_copy)
6483{
6485
6486 if (!cti) {
6487 return;
6488 }
6489
6490 /* Remove the custom target. */
6491 bConstraintTarget *ct = (bConstraintTarget *)targets->last;
6492
6493 if (ct && (ct->flag & CONSTRAINT_TAR_CUSTOM_SPACE)) {
6495
6496 if (!no_copy) {
6497 con->space_object = ct->tar;
6499 }
6500
6501 BLI_freelinkN(targets, ct);
6502 }
6503
6504 /* Release the constraint-specific targets. */
6505 if (cti->flush_constraint_targets) {
6506 cti->flush_constraint_targets(con, targets, no_copy);
6507 }
6508}
6509
6511 Scene *scene,
6512 bConstraint *con,
6513 int index,
6514 short ownertype,
6515 void *ownerdata,
6516 float mat[4][4],
6517 float ctime)
6518{
6520 ListBase targets = {nullptr, nullptr};
6521 bConstraintOb *cob;
6523
6524 if (cti && cti->get_constraint_targets) {
6525 /* make 'constraint-ob' */
6526 cob = MEM_callocN<bConstraintOb>("tempConstraintOb");
6527 cob->type = ownertype;
6528 cob->scene = scene;
6529 cob->depsgraph = depsgraph;
6530 switch (ownertype) {
6531 case CONSTRAINT_OBTYPE_OBJECT: /* it is usually this case */
6532 {
6533 cob->ob = (Object *)ownerdata;
6534 cob->pchan = nullptr;
6535 if (cob->ob) {
6536 copy_m4_m4(cob->matrix, cob->ob->object_to_world().ptr());
6537 copy_m4_m4(cob->startmat, cob->matrix);
6538 }
6539 else {
6540 unit_m4(cob->matrix);
6541 unit_m4(cob->startmat);
6542 }
6543 break;
6544 }
6545 case CONSTRAINT_OBTYPE_BONE: /* this may occur in some cases */
6546 {
6547 cob->ob = nullptr; /* this might not work at all :/ */
6548 cob->pchan = (bPoseChannel *)ownerdata;
6549 if (cob->pchan) {
6550 copy_m4_m4(cob->matrix, cob->pchan->pose_mat);
6551 copy_m4_m4(cob->startmat, cob->matrix);
6552 }
6553 else {
6554 unit_m4(cob->matrix);
6555 unit_m4(cob->startmat);
6556 }
6557 break;
6558 }
6559 }
6560
6561 /* Initialize the custom space for use in calculating the matrices. */
6563
6564 /* get targets - we only need the first one though (and there should only be one) */
6565 cti->get_constraint_targets(con, &targets);
6566
6567 /* only calculate the target matrix on the first target */
6568 ct = static_cast<bConstraintTarget *>(BLI_findlink(&targets, index));
6569
6570 if (ct) {
6571 if (cti->get_target_matrix) {
6572 cti->get_target_matrix(depsgraph, con, cob, ct, ctime);
6573 }
6574 copy_m4_m4(mat, ct->matrix);
6575 }
6576
6577 /* free targets + 'constraint-ob' */
6578 if (cti->flush_constraint_targets) {
6579 cti->flush_constraint_targets(con, &targets, true);
6580 }
6581 MEM_freeN(cob);
6582 }
6583 else {
6584 /* invalid constraint - perhaps... */
6585 unit_m4(mat);
6586 }
6587}
6588
6590 Depsgraph *depsgraph, bConstraint *con, bConstraintOb *cob, ListBase *targets, float ctime)
6591{
6593
6594 if (cti && cti->get_constraint_targets) {
6595 /* get targets
6596 * - constraints should use ct->matrix, not directly accessing values
6597 * - ct->matrix members have not yet been calculated here!
6598 */
6599 cti->get_constraint_targets(con, targets);
6600
6601 /* The Armature constraint doesn't need ct->matrix for evaluate at all. */
6602 if (ELEM(cti->type, CONSTRAINT_TYPE_ARMATURE)) {
6603 return;
6604 }
6605
6606 /* set matrices
6607 * - calculate if possible, otherwise just initialize as identity matrix
6608 */
6609 if (cti->get_target_matrix) {
6610 LISTBASE_FOREACH (bConstraintTarget *, ct, targets) {
6611 cti->get_target_matrix(depsgraph, con, cob, ct, ctime);
6612 }
6613 }
6614 else {
6615 LISTBASE_FOREACH (bConstraintTarget *, ct, targets) {
6616 unit_m4(ct->matrix);
6617 }
6618 }
6619 }
6620}
6621
6623{
6624 if (con && con->space_object && is_custom_space_needed(con)) {
6625 /* Basically default_get_tarmat but without the unused parameters. */
6627 con->space_subtarget,
6628 nullptr,
6632 0,
6633 0);
6634
6635 return;
6636 }
6637
6639}
6640
6641/* ---------- Evaluation ----------- */
6642
6644 ListBase *conlist,
6645 bConstraintOb *cob,
6646 float ctime)
6647{
6648 float oldmat[4][4];
6649 float enf;
6650
6651 /* check that there is a valid constraint object to evaluate */
6652 if (cob == nullptr) {
6653 return;
6654 }
6655
6656 /* loop over available constraints, solving and blending them */
6657 LISTBASE_FOREACH (bConstraint *, con, conlist) {
6659 ListBase targets = {nullptr, nullptr};
6660
6661 /* these we can skip completely (invalid constraints...) */
6662 if (cti == nullptr) {
6663 continue;
6664 }
6665 if (con->flag & (CONSTRAINT_DISABLE | CONSTRAINT_OFF)) {
6666 continue;
6667 }
6668 /* these constraints can't be evaluated anyway */
6669 if (cti->evaluate_constraint == nullptr) {
6670 continue;
6671 }
6672 /* influence == 0 should be ignored */
6673 if (con->enforce == 0.0f) {
6674 continue;
6675 }
6676
6677 /* influence of constraint
6678 * - value should have been set from animation data already
6679 */
6680 enf = con->enforce;
6681
6682 /* Initialize the custom space for use in calculating the matrices. */
6684
6685 /* make copy of world-space matrix pre-constraint for use with blending later */
6686 copy_m4_m4(oldmat, cob->matrix);
6687
6688 /* move owner matrix into right space */
6690 cob->ob, cob->pchan, cob, cob->matrix, CONSTRAINT_SPACE_WORLD, con->ownspace, false);
6691
6692 /* prepare targets for constraint solving */
6693 BKE_constraint_targets_for_solving_get(depsgraph, con, cob, &targets, ctime);
6694
6695 /* Solve the constraint and put result in cob->matrix */
6696 cti->evaluate_constraint(con, cob, &targets);
6697
6698 /* clear targets after use
6699 * - this should free temp targets but no data should be copied back
6700 * as constraints may have done some nasty things to it...
6701 */
6702 if (cti->flush_constraint_targets) {
6703 cti->flush_constraint_targets(con, &targets, true);
6704 }
6705
6706 /* move owner back into world-space for next constraint/other business */
6707 if ((con->flag & CONSTRAINT_SPACEONCE) == 0) {
6709 cob->ob, cob->pchan, cob, cob->matrix, con->ownspace, CONSTRAINT_SPACE_WORLD, false);
6710 }
6711
6712 /* Interpolate the enforcement, to blend result of constraint into final owner transform
6713 * - all this happens in world-space to prevent any weirdness creeping in
6714 * (#26014 and #25725), since some constraints may not convert the solution back to the input
6715 * space before blending but all are guaranteed to end up in good "world-space" result.
6716 */
6717 /* NOTE: all kind of stuff here before (caused trouble), much easier to just interpolate,
6718 * or did I miss something? -jahka (r.32105) */
6719 if (enf < 1.0f) {
6720 float solution[4][4];
6721 copy_m4_m4(solution, cob->matrix);
6722 interp_m4_m4m4(cob->matrix, oldmat, solution, enf);
6723 }
6724 }
6725}
6726
6728{
6729 LISTBASE_FOREACH (bConstraint *, con, conlist) {
6731
6732 /* Write the specific data */
6733 if (cti && con->data) {
6734 /* firstly, just write the plain con->data struct */
6735 BLO_write_struct_by_name(writer, cti->struct_name, con->data);
6736
6737 /* do any constraint specific stuff */
6738 switch (con->type) {
6740 bArmatureConstraint *data = static_cast<bArmatureConstraint *>(con->data);
6741
6742 /* write targets */
6743 LISTBASE_FOREACH (bConstraintTarget *, ct, &data->targets) {
6745 }
6746
6747 break;
6748 }
6750 bSplineIKConstraint *data = static_cast<bSplineIKConstraint *>(con->data);
6751
6752 /* write points array */
6753 BLO_write_float_array(writer, data->numpoints, data->points);
6754
6755 break;
6756 }
6759 con->data);
6760 BLO_write_string(writer, data->attribute_name);
6761 break;
6762 }
6763 }
6764 }
6765
6766 /* Write the constraint */
6767 BLO_write_struct(writer, bConstraint, con);
6768 }
6769}
6770
6772{
6773 BLO_read_struct_list(reader, bConstraint, lb);
6774 LISTBASE_FOREACH (bConstraint *, con, lb) {
6776 if (cti) {
6777 con->data = BLO_read_struct_by_name_array(reader, cti->struct_name, 1, con->data);
6778 }
6779 else {
6780 /* No `BLI_assert_unreachable()` here, this code can be reached in some cases, like the
6781 * deprecated RigidBody constraint. */
6782 con->data = nullptr;
6783 }
6784
6785 /* Patch for error introduced by changing constraints (don't know how). */
6786 /* NOTE(@ton): If `con->data` type changes, DNA cannot resolve the pointer!. */
6787 if (con->data == nullptr) {
6788 con->type = CONSTRAINT_TYPE_NULL;
6789 }
6790
6791 /* If linking from a library, clear 'local' library override flag. */
6792 if (ID_IS_LINKED(id_owner)) {
6794 }
6795
6796 switch (con->type) {
6798 bArmatureConstraint *data = static_cast<bArmatureConstraint *>(con->data);
6799
6800 BLO_read_struct_list(reader, bConstraintTarget, &data->targets);
6801
6802 break;
6803 }
6805 bSplineIKConstraint *data = static_cast<bSplineIKConstraint *>(con->data);
6806
6807 BLO_read_float_array(reader, data->numpoints, &data->points);
6808 break;
6809 }
6811 bKinematicConstraint *data = static_cast<bKinematicConstraint *>(con->data);
6812
6813 con->lin_error = 0.0f;
6814 con->rot_error = 0.0f;
6815
6816 /* version patch for runtime flag, was not cleared in some case */
6817 data->flag &= ~CONSTRAINT_IK_AUTO;
6818 break;
6819 }
6821 bTransformCacheConstraint *data = static_cast<bTransformCacheConstraint *>(con->data);
6822 data->reader = nullptr;
6823 data->reader_object_path[0] = '\0';
6824 break;
6825 }
6828 con->data);
6829 BLO_read_string(reader, &data->attribute_name);
6830 break;
6831 }
6832 }
6833 }
6834}
6835
6836/* Some static asserts to ensure that the bActionConstraint data is using the expected types for
6837 * some of the fields. This check is done here instead of in DNA_constraint_types.h to avoid the
6838 * inclusion of an DNA_anim_types.h in DNA_constraint_types.h just for this assert. */
6839static_assert(
6840 std::is_same_v<decltype(ActionSlot::handle), decltype(bActionConstraint::action_slot_handle)>);
6841static_assert(std::is_same_v<decltype(ActionSlot::identifier),
void ABC_get_transform(struct CacheReader *reader, float r_mat_world[4][4], double time, float scale)
void BIK_clear_data(struct bPose *pose)
Blender kernel action and pose functionality.
void BKE_pose_free_data(bPose *pose) ATTR_NONNULL(1)
void what_does_obaction(Object *ob, Object *workob, bPose *pose, bAction *act, int32_t action_slot_handle, char groupname[], const AnimationEvalContext *anim_eval_context) ATTR_NONNULL(1
bPoseChannel * BKE_pose_channel_find_name(const bPose *pose, const char *name)
bPoseChannel * BKE_pose_channel_ensure(bPose *pose, const char *name) ATTR_NONNULL(2)
bool BKE_where_on_path(const struct Object *ob, float ctime, float r_vec[4], float r_dir[3], float r_quat[4], float *r_radius, float *r_weight)
AnimationEvalContext BKE_animsys_eval_context_construct(struct Depsgraph *depsgraph, float eval_time) ATTR_WARN_UNUSED_RESULT
Definition anim_sys.cc:738
bool BKE_animdata_drivers_remove_for_rna_struct(struct ID &owner_id, struct StructRNA &type, void *data)
void BKE_pchan_bbone_deform_segment_index(const bPoseChannel *pchan, const float *co, int *r_index, float *r_blend_next)
float distfactor_to_bone(const blender::float3 &position, const blender::float3 &head, const blender::float3 &tail, float radius_head, float radius_tail, float falloff_distance)
void BKE_pchan_calc_mat(bPoseChannel *pchan)
void BKE_pchan_apply_mat4(bPoseChannel *pchan, const float mat[4][4], bool use_compat)
void BKE_armature_mat_bone_to_pose(const bPoseChannel *pchan, const float inmat[4][4], float outmat[4][4])
void BKE_armature_mat_pose_to_bone(const bPoseChannel *pchan, const float inmat[4][4], float outmat[4][4])
void BKE_pchan_bbone_deform_clamp_segment_index(const bPoseChannel *pchan, float head_tail, int *r_index, float *r_blend_next)
void BKE_cachefile_reader_free(CacheFile *cache_file, CacheReader **reader)
Definition cachefile.cc:210
double BKE_cachefile_time_offset(const CacheFile *cache_file, double time, double fps)
Definition cachefile.cc:404
void BKE_cachefile_reader_open(CacheFile *cache_file, CacheReader **reader, Object *object, const char *object_path)
Definition cachefile.cc:160
Camera data-block and utility functions.
void BKE_camera_params_init(CameraParams *params)
void BKE_camera_params_from_object(CameraParams *params, const struct Object *cam_ob)
void(* ConstraintIDFunc)(struct bConstraint *con, struct ID **idpoin, bool is_reference, void *userdata)
void * CustomData_bmesh_get(const CustomData *data, void *block, eCustomDataType type)
bool CustomData_has_layer(const CustomData *data, eCustomDataType type)
support for deformation groups and hooks.
MDeformWeight * BKE_defvert_find_index(const MDeformVert *dv, int defgroup)
Definition deform.cc:806
int BKE_object_defgroup_name_index(const Object *ob, blender::StringRef name)
Definition deform.cc:591
display list (or rather multi purpose list) stuff.
DispList * BKE_displist_find(struct ListBase *lb, int type)
Definition displist.cc:71
@ DL_VERTS
BMEditMesh * BKE_editmesh_from_object(Object *ob)
Return the BMEditMesh for a given object.
Definition editmesh.cc:61
void BKE_driver_target_matrix_to_rot_channels(float mat[4][4], int auto_order, int rotation_mode, int channel, bool angles, float r_buf[4])
@ G_DEBUG
void id_lib_extern(ID *id)
Definition lib_id.cc:290
void id_us_plus(ID *id)
Definition lib_id.cc:358
@ LIB_ID_COPY_NO_LIB_OVERRIDE_LOCAL_DATA_FLAG
@ LIB_ID_CREATE_NO_USER_REFCOUNT
void id_us_min(ID *id)
Definition lib_id.cc:366
@ IDWALK_NOP
float BKE_movieclip_remap_scene_to_clip_frame(const struct MovieClip *clip, float framenr)
void BKE_movieclip_get_size(struct MovieClip *clip, const struct MovieClipUser *user, int *r_width, int *r_height)
General operations, lookup, etc. for blender objects.
void BKE_object_where_is_calc_mat4(const Object *ob, float r_obmat[4][4])
void BKE_object_apply_mat4(Object *ob, const float mat[4][4], bool use_compat, bool use_parent)
Mesh * BKE_object_get_evaluated_mesh(const Object *object_eval)
std::optional< blender::Bounds< blender::float3 > > BKE_object_boundbox_get(const Object *ob)
void BKE_object_to_mat4(const Object *ob, float r_mat[4][4])
float BKE_scene_frame_get(const Scene *scene)
Definition scene.cc:2384
bool BKE_shrinkwrap_project_normal(char options, const float vert[3], const float dir[3], float ray_radius, const SpaceTransform *transf, ShrinkwrapTreeData *tree, BVHTreeRayHit *hit)
void BKE_shrinkwrap_free_tree(ShrinkwrapTreeData *data)
void BKE_shrinkwrap_snap_point_to_surface(const ShrinkwrapTreeData *tree, const SpaceTransform *transform, int mode, int hit_idx, const float hit_co[3], const float hit_no[3], float goal_dist, const float point_co[3], float r_point_co[3])
void BKE_shrinkwrap_compute_smooth_normal(const ShrinkwrapTreeData *tree, const SpaceTransform *transform, int tri_idx, const float hit_co[3], const float hit_no[3], float r_no[3])
bool BKE_shrinkwrap_init_tree(ShrinkwrapTreeData *data, Mesh *mesh, int shrinkType, int shrinkMode, bool force_normals)
Definition shrinkwrap.cc:96
void BKE_shrinkwrap_find_nearest_surface(ShrinkwrapTreeData *tree, BVHTreeNearest *nearest, float co[3], int type)
struct MovieTrackingObject * BKE_tracking_object_get_camera(const struct MovieTracking *tracking)
void BKE_tracking_get_camera_object_matrix(const struct Object *camera_object, float mat[4][4])
struct MovieTrackingObject * BKE_tracking_object_get_named(struct MovieTracking *tracking, const char *name)
Definition tracking.cc:1966
struct MovieTrackingTrack * BKE_tracking_object_find_track_with_name(struct MovieTrackingObject *tracking_object, const char *name)
Definition tracking.cc:1993
void BKE_tracking_marker_get_subframe_position(struct MovieTrackingTrack *track, float framenr, float pos[2])
Definition tracking.cc:1507
void BKE_tracking_undistort_v2(struct MovieTracking *tracking, int image_width, int image_height, const float co[2], float r_co[2])
Definition tracking.cc:2442
void BKE_tracking_camera_get_reconstructed_interpolate(struct MovieTracking *tracking, struct MovieTrackingObject *tracking_object, float framenr, float mat[4][4])
Definition tracking.cc:2146
#define BLI_assert_unreachable()
Definition BLI_assert.h:93
#define BLI_assert(a)
Definition BLI_assert.h:46
#define BLI_assert_msg(a, msg)
Definition BLI_assert.h:53
#define BUFFER_FOR_CPP_TYPE_VALUE(type, variable_name)
#define BVH_RAYCAST_DIST_MAX
int BLI_bvhtree_ray_cast(const BVHTree *tree, const float co[3], const float dir[3], float radius, BVHTreeRayHit *hit, BVHTree_RayCastCallback callback, void *userdata)
int BLI_findindex(const ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:586
void * BLI_findlink(const ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:534
#define LISTBASE_FOREACH(type, var, list)
BLI_INLINE void BLI_listbase_clear(ListBase *lb)
void * BLI_findstring(const ListBase *listbase, const char *id, int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:608
void BLI_freelinkN(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:270
void void BLI_freelistN(ListBase *listbase) ATTR_NONNULL(1)
Definition listbase.cc:497
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:111
int BLI_listbase_count(const ListBase *listbase) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:524
void void void void void void BLI_duplicatelist(ListBase *dst, const ListBase *src) ATTR_NONNULL(1
MINLINE float max_ff(float a, float b)
MINLINE float clamp_f(float value, float min, float max)
MINLINE float min_ff(float a, float b)
MINLINE float square_f(float a)
MINLINE float pow3f(float x)
MINLINE float interpf(float target, float origin, float t)
#define M_PI_2
#define RAD2DEGF(_rad)
#define M_PI
float mat4_to_volume_scale(const float mat[4][4])
void BLI_space_transform_from_matrices(struct SpaceTransform *data, const float local[4][4], const float target[4][4])
void interp_m4_m4m4(float R[4][4], const float A[4][4], const float B[4][4], float t)
void mul_m3_v3(const float M[3][3], float r[3])
void madd_m4_m4m4fl(float R[4][4], const float A[4][4], const float B[4][4], float f)
void mul_m4_fl(float R[4][4], float f)
void mul_m4_m4m4(float R[4][4], const float A[4][4], const float B[4][4])
void BLI_space_transform_apply(const struct SpaceTransform *data, float co[3])
void copy_m3_m3(float m1[3][3], const float m2[3][3])
void unit_m3(float m[3][3])
void copy_m3_m4(float m1[3][3], const float m2[4][4])
bool invert_m3_m3(float inverse[3][3], const float mat[3][3])
void loc_rot_size_to_mat4(float R[4][4], const float loc[3], const float rot[3][3], const float size[3])
void mat4_to_loc_rot_size(float loc[3], float rot[3][3], float size[3], const float wmat[4][4])
void translate_m4(float mat[4][4], float Tx, float Ty, float Tz)
void normalize_m3(float R[3][3]) ATTR_NONNULL()
void rescale_m4(float mat[4][4], const float scale[3])
void loc_eulO_size_to_mat4(float R[4][4], const float loc[3], const float eul[3], const float size[3], short order)
void mul_m4_v3(const float M[4][4], float r[3])
void invert_m4_m4_safe(float inverse[4][4], const float mat[4][4])
void orthogonalize_m4_stable(float R[4][4], int axis, bool normalize)
#define mul_m4_series(...)
void scale_m4_fl(float R[4][4], float scale)
void mul_m4_m4m4_split_channels(float R[4][4], const float A[4][4], const float B[4][4])
void normalize_m4_ex(float R[4][4], float r_scale[3]) ATTR_NONNULL()
void copy_m4_m4(float m1[4][4], const float m2[4][4])
void mul_v3_m4v3(float r[3], const float mat[4][4], const float vec[3])
bool is_negative_m4(const float mat[4][4])
bool invert_m4_m4(float inverse[4][4], const float mat[4][4])
void mul_m4_m4m4_aligned_scale(float R[4][4], const float A[4][4], const float B[4][4])
bool invert_m4(float mat[4][4])
void BLI_space_transform_invert_normal(const struct SpaceTransform *data, float no[3])
void mat4_to_size(float size[3], const float M[4][4])
void transpose_m3(float R[3][3])
void BLI_space_transform_invert(const struct SpaceTransform *data, float co[3])
float determinant_m3(float a1, float a2, float a3, float b1, float b2, float b3, float c1, float c2, float c3)
void mul_m3_m3m3(float R[3][3], const float A[3][3], const float B[3][3])
void mul_m4_m3m4(float R[4][4], const float A[3][3], const float B[4][4])
void mul_v3_mat3_m4v3(float r[3], const float mat[4][4], const float vec[3])
void mul_mat3_m4_v3(const float mat[4][4], float r[3])
void normalize_m4(float R[4][4]) ATTR_NONNULL()
void unit_m4(float m[4][4])
void rotate_eulO(float beul[3], short order, char axis, float angle)
void axis_angle_normalized_to_mat3(float R[3][3], const float axis[3], float angle)
@ EULER_ORDER_DEFAULT
void mat4_to_eul(float eul[3], const float mat[4][4])
void quat_to_mat4(float m[4][4], const float q[4])
void dquat_to_mat4(float R[4][4], const DualQuat *dq)
void mat4_to_eulO(float eul[3], short order, const float m[4][4])
void mat4_to_dquat(DualQuat *dq, const float basemat[4][4], const float mat[4][4])
void compatible_eul(float eul[3], const float oldrot[3])
void quat_apply_track(float quat[4], short axis, short upflag)
void eulO_to_mat3(float M[3][3], const float e[3], short order)
void add_weighted_dq_dq_pivot(DualQuat *dq_sum, const DualQuat *dq, const float pivot[3], float weight, bool compute_scale_matrix)
void normalize_dq(DualQuat *dq, float totweight)
void mat3_to_eulO(float eul[3], short order, const float m[3][3])
void mat3_normalized_to_axis_angle(float axis[3], float *angle, const float mat[3][3])
void mat4_to_compatible_eulO(float eul[3], const float oldrot[3], short order, const float mat[4][4])
MINLINE float len_squared_v3(const float v[3]) ATTR_WARN_UNUSED_RESULT
MINLINE float len_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void madd_v3_v3fl(float r[3], const float a[3], float f)
MINLINE void add_v3_fl(float r[3], float f)
MINLINE void mul_v3_v3(float r[3], const float a[3])
MINLINE void sub_v3_v3(float r[3], const float a[3])
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void mul_v3_fl(float r[3], float f)
MINLINE void copy_v3_v3(float r[3], const float a[3])
MINLINE void negate_v3_v3(float r[3], const float a[3])
void project_v3_v3v3(float out[3], const float p[3], const float v_proj[3])
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
void interp_v3_v3v3(float r[3], const float a[3], const float b[3], float t)
MINLINE void add_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void cross_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void negate_v3(float r[3])
MINLINE float normalize_v3_v3(float r[3], const float a[3])
MINLINE void copy_v3_fl(float r[3], float f)
MINLINE void zero_v3(float r[3])
MINLINE void add_v3_v3(float r[3], const float a[3])
MINLINE float normalize_v3(float n[3])
MINLINE float len_v3(const float a[3]) ATTR_WARN_UNUSED_RESULT
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC
Definition string.cc:41
char * BLI_strdup_null(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_MALLOC
Definition string.cc:46
char * STRNCPY(char(&dst)[N], const char *src)
Definition BLI_string.h:693
#define STRNCPY_UTF8(dst, src)
void BLI_uniquename(const struct ListBase *list, void *vlink, const char *defname, char delim, int name_offset, size_t name_maxncpy) ATTR_NONNULL(1
unsigned int uint
unsigned short ushort
#define CLAMP(a, b, c)
#define INIT_MINMAX(min, max)
#define UNUSED_VARS(...)
#define ELEM(...)
#define IS_EQF(a, b)
#define STREQ(a, b)
void BLO_write_float_array(BlendWriter *writer, int64_t num, const float *data_ptr)
void BLO_write_struct_by_name(BlendWriter *writer, const char *struct_name, const void *data_ptr)
void BLO_read_float_array(BlendDataReader *reader, int64_t array_size, float **ptr_p)
Definition readfile.cc:5809
#define BLO_write_struct(writer, struct_name, data_ptr)
void * BLO_read_struct_by_name_array(BlendDataReader *reader, const char *struct_name, int64_t items_num, const void *old_address)
Definition readfile.cc:5719
void BLO_read_string(BlendDataReader *reader, char **ptr_p)
Definition readfile.cc:5828
void BLO_write_string(BlendWriter *writer, const char *data_ptr)
#define BLO_read_struct_list(reader, struct_name, list)
#define DATA_(msgid)
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
void DEG_id_tag_update(ID *id, unsigned int flags)
bool DEG_is_active(const Depsgraph *depsgraph)
Definition depsgraph.cc:323
float DEG_get_ctime(const Depsgraph *graph)
Scene * DEG_get_evaluated_scene(const Depsgraph *graph)
T * DEG_get_original(T *id)
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
@ ID_RECALC_TRANSFORM
Definition DNA_ID.h:1054
@ ID_RECALC_SYNC_TO_EVAL
Definition DNA_ID.h:1118
#define ID_IS_LINKED(_id)
Definition DNA_ID.h:694
#define ID_IS_EDITABLE(_id)
Definition DNA_ID.h:705
#define ID_IS_OVERRIDE_LIBRARY(_id)
Definition DNA_ID.h:730
@ BONE_MULT_VG_ENV
@ BONE_CONNECTED
@ CACHE_FILE_TYPE_INVALID
@ CACHEFILE_TYPE_ALEMBIC
@ CACHEFILE_TYPE_USD
@ CONSTRAINT_BBONE_SHAPE
@ CONSTRAINT_OFF
@ CONSTRAINT_OVERRIDE_LIBRARY_LOCAL
@ CONSTRAINT_SPACEONCE
@ CONSTRAINT_ACTIVE
@ CONSTRAINT_DISABLE
@ CONSTRAINT_BBONE_SHAPE_FULL
@ SIZELIKE_MULTIPLY
@ SIZELIKE_UNIFORM
@ SIZELIKE_OFFSET
@ CONSTRAINT_IK_POS
@ CONSTRAINT_IK_AUTO
@ CONSTRAINT_IK_STRETCH
@ CONSTRAINT_IK_TIP
@ CONSTRAINT_TAR_CUSTOM_SPACE
@ ROTLIKE_MIX_OFFSET
@ ROTLIKE_MIX_BEFORE
@ ROTLIKE_MIX_AFTER
@ ROTLIKE_MIX_REPLACE
@ ROTLIKE_MIX_ADD
@ CONSTRAINT_TYPE_TRACKTO
@ CONSTRAINT_TYPE_PIVOT
@ CONSTRAINT_TYPE_CHILDOF
@ CONSTRAINT_TYPE_TRANSFORM
@ CONSTRAINT_TYPE_FOLLOWTRACK
@ CONSTRAINT_TYPE_OBJECTSOLVER
@ CONSTRAINT_TYPE_ARMATURE
@ CONSTRAINT_TYPE_LOCLIKE
@ CONSTRAINT_TYPE_SHRINKWRAP
@ CONSTRAINT_TYPE_MINMAX
@ CONSTRAINT_TYPE_ROTLIMIT
@ CONSTRAINT_TYPE_CAMERASOLVER
@ CONSTRAINT_TYPE_GEOMETRY_ATTRIBUTE
@ CONSTRAINT_TYPE_ROTLIKE
@ CONSTRAINT_TYPE_SPLINEIK
@ CONSTRAINT_TYPE_KINEMATIC
@ CONSTRAINT_TYPE_NULL
@ NUM_CONSTRAINT_TYPES
@ CONSTRAINT_TYPE_DISTLIMIT
@ CONSTRAINT_TYPE_TRANSLIKE
@ CONSTRAINT_TYPE_LOCLIMIT
@ CONSTRAINT_TYPE_CLAMPTO
@ CONSTRAINT_TYPE_LOCKTRACK
@ CONSTRAINT_TYPE_SIZELIMIT
@ CONSTRAINT_TYPE_ACTION
@ CONSTRAINT_TYPE_FOLLOWPATH
@ CONSTRAINT_TYPE_STRETCHTO
@ CONSTRAINT_TYPE_SIZELIKE
@ CONSTRAINT_TYPE_SAMEVOL
@ CONSTRAINT_TYPE_DAMPTRACK
@ CONSTRAINT_TYPE_TRANSFORM_CACHE
@ CONSTRAINT_OBTYPE_OBJECT
@ CONSTRAINT_OBTYPE_BONE
Attribute_Data_Type
@ CON_ATTRIBUTE_QUATERNION
@ CON_ATTRIBUTE_4X4MATRIX
@ CON_ATTRIBUTE_VECTOR
@ LIMITDIST_INSIDE
@ LIMITDIST_OUTSIDE
@ CONSTRAINT_ARMATURE_QUATERNION
@ CONSTRAINT_ARMATURE_ENVELOPE
@ CONSTRAINT_ARMATURE_CUR_LOCATION
@ CAMERASOLVER_ACTIVECLIP
@ FOLLOWTRACK_USE_UNDISTORTION
@ FOLLOWTRACK_USE_3D_POSITION
@ FOLLOWTRACK_ACTIVECLIP
@ ACTCON_BONE_USE_OBJECT_ACTION
@ ACTCON_USE_EVAL_TIME
@ LIMIT_ROT_LEGACY_BEHAVIOR
#define CON_SHRINKWRAP_PROJECT_CULL_MASK
@ TRANS_MIXSCALE_MULTIPLY
@ TRANS_MIXSCALE_REPLACE
@ CONSTRAINT_SPLINEIK_YS_FIT_CURVE
@ TRANS_MIXLOC_ADD
@ TRANS_MIXLOC_REPLACE
@ CONSTRAINT_SPACE_CUSTOM
@ CONSTRAINT_SPACE_POSE
@ CONSTRAINT_SPACE_WORLD
@ CONSTRAINT_SPACE_OWNLOCAL
@ CONSTRAINT_SPACE_LOCAL
@ CONSTRAINT_SPACE_PARLOCAL
@ CON_ATTRIBUTE_DOMAIN_EDGE
@ CON_ATTRIBUTE_DOMAIN_POINT
@ CON_ATTRIBUTE_DOMAIN_CURVE
@ CON_ATTRIBUTE_DOMAIN_FACE_CORNER
@ CON_ATTRIBUTE_DOMAIN_FACE
@ CON_ATTRIBUTE_DOMAIN_INSTANCE
@ ACTCON_MIX_BEFORE
@ ACTCON_MIX_REPLACE
@ ACTCON_MIX_BEFORE_SPLIT
@ ACTCON_MIX_BEFORE_FULL
@ ACTCON_MIX_AFTER_FULL
@ ACTCON_MIX_AFTER_SPLIT
@ ACTCON_MIX_AFTER
@ FOLLOWPATH_FOLLOW
@ FOLLOWPATH_RADIUS
@ FOLLOWPATH_STATIC
@ CON_ATTRIBUTE_MIX_AFTER_FULL
@ CON_ATTRIBUTE_MIX_AFTER_SPLIT
@ CON_ATTRIBUTE_MIX_BEFORE_SPLIT
@ CON_ATTRIBUTE_MIX_BEFORE_FULL
@ CON_ATTRIBUTE_MIX_REPLACE
@ CONSTRAINT_SPLINEIK_USE_ORIGINAL_SCALE
@ TRANSLIKE_MIX_AFTER
@ TRANSLIKE_MIX_AFTER_FULL
@ TRANSLIKE_MIX_BEFORE
@ TRANSLIKE_MIX_BEFORE_SPLIT
@ TRANSLIKE_MIX_REPLACE
@ TRANSLIKE_MIX_BEFORE_FULL
@ TRANSLIKE_MIX_AFTER_SPLIT
@ OBJECTSOLVER_ACTIVECLIP
@ OBJECTSOLVER_SET_INVERSE
@ CON_SHRINKWRAP_PROJECT_INVERT_CULL
@ CON_SHRINKWRAP_PROJECT_OPPOSITE
@ CON_SHRINKWRAP_TRACK_NORMAL
@ CLAMPTO_CYCLIC
@ PIVOTCON_AXIS_X
@ PIVOTCON_AXIS_Z
@ PIVOTCON_AXIS_NONE
@ SAMEVOL_SINGLE_AXIS
@ SAMEVOL_STRICT
@ SAMEVOL_UNIFORM
@ PIVOTCON_FLAG_OFFSET_ABS
@ CONSTRAINT_EULER_AUTO
@ TRANSLIKE_REMOVE_TARGET_SHEAR
@ LIMITDIST_USESOFT
@ STRETCHTOCON_USE_BULGE_MAX
@ STRETCHTOCON_USE_BULGE_MIN
@ ROTLIKE_Y_INVERT
@ ROTLIKE_Z_INVERT
@ ROTLIKE_X_INVERT
@ LOCLIKE_Z_INVERT
@ LOCLIKE_Y_INVERT
@ LOCLIKE_OFFSET
@ LOCLIKE_X_INVERT
@ CHILDOF_SET_INVERSE
@ TRANS_ROTATION
@ TRANS_LOCATION
@ TRANS_MIXROT_REPLACE
@ TRANS_MIXROT_ADD
@ TRANS_MIXROT_BEFORE
@ TRANS_MIXROT_AFTER
@ FOLLOWTRACK_FRAME_CROP
@ FOLLOWTRACK_FRAME_STRETCH
@ CU_NURB_CYCLIC
@ CU_PATH_CLAMP
@ CD_MDEFORMVERT
@ MOD_SHRINKWRAP_TARGET_PROJECT
@ MOD_SHRINKWRAP_NEAREST_VERTEX
@ MOD_SHRINKWRAP_PROJECT
@ MOD_SHRINKWRAP_NEAREST_SURFACE
Object is a sort of wrapper for general info.
@ OB_LATTICE
@ OB_ARMATURE
@ OB_MESH
@ OB_CURVES_LEGACY
@ OB_POSX
@ OB_NEGZ
@ OB_POSY
@ OB_NEGX
@ OB_POSZ
@ OB_NEGY
@ UI_PANEL_DATA_EXPAND_ROOT
@ UI_SUBPANEL_DATA_EXPAND_2
@ UI_SUBPANEL_DATA_EXPAND_1
@ TRACK_HAS_BUNDLE
@ TRACKING_OBJECT_CAMERA
static double angle(const Eigen::Vector3d &v1, const Eigen::Vector3d &v2)
Definition IK_Math.h:117
Read Guarded memory(de)allocation.
#define MEM_SAFE_FREE(v)
#define BM_ITER_MESH(ele, iter, bm, itype)
@ BM_VERTS_OF_MESH
BMesh const char void * data
ATTR_WARN_UNUSED_RESULT const BMVert * v
BPy_StructRNA * depsgraph
SIMD_FORCE_INLINE btVector3 transform(const btVector3 &point) const
btVector3 orth(const btVector3 &v)
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
static btDbvtVolume bounds(btDbvtNode **leaves, int count)
Definition btDbvt.cpp:299
btSequentialImpulseConstraintSolverMt int btPersistentManifold int btTypedConstraint ** constraints
SIMD_FORCE_INLINE btScalar norm() const
Return the norm (length) of the vector.
Definition btVector3.h:263
void destruct(void *ptr) const
const void * get() const
void get_to_uninitialized(int64_t index, void *r_value) const
constexpr IndexRange index_range() const
Definition BLI_span.hh:401
constexpr bool is_empty() const
Definition BLI_span.hh:260
GAttributeReader lookup(const StringRef attribute_id) const
virtual std::optional< AttributeAccessor > attributes() const
nullptr float
static void add_new_constraint_to_list(Object *ob, bPoseChannel *pchan, bConstraint *con)
static void minmax_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *targets)
static void minmax_new_data(void *cdata)
static bConstraint * constraint_find_original_for_update(bConstraintOb *cob, bConstraint *con)
static void stretchto_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
static blender::bke::AttrType type_value_to_attribute(const Attribute_Data_Type data_type)
static void minmax_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
static void followtrack_fit_frame(FollowTrackContext *context, const int clip_width, const int clip_height, float marker_position[2])
static void samevolume_new_data(void *cdata)
bool BKE_constraint_apply_and_remove_for_pose(Depsgraph *depsgraph, Scene *scene, ListBase *constraints, Object *ob, bConstraint *con, bPoseChannel *pchan)
static float followtrack_distance_from_viewplane_get(FollowTrackContext *context, bConstraintOb *cob)
void BKE_constraint_targets_for_solving_get(Depsgraph *depsgraph, bConstraint *con, bConstraintOb *cob, ListBase *targets, float ctime)
static void damptrack_do_transform(float matrix[4][4], const float tarvec[3], int track_axis)
static int kinematic_get_tars(bConstraint *con, ListBase *list)
static bConstraintTypeInfo CTI_TRANSFORM_CACHE
static bool splineik_get_tarmat(Depsgraph *, bConstraint *, bConstraintOb *, bConstraintTarget *ct, float)
static void camerasolver_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *)
static void stretchto_new_data(void *cdata)
bool BKE_constraint_is_nonlocal_in_liboverride(const Object *ob, const bConstraint *con)
static bool default_get_tarmat(Depsgraph *, bConstraint *con, bConstraintOb *cob, bConstraintTarget *ct, float)
void BKE_constraints_copy(ListBase *dst, const ListBase *src, bool do_extern)
static bConstraintTypeInfo CTI_ROTLIKE
bool BKE_constraint_target_uses_bbone(bConstraint *con, bConstraintTarget *ct)
static void locktrack_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *targets)
static void sizelike_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
static void transformcache_new_data(void *cdata)
static int followpath_get_tars(bConstraint *con, ListBase *list)
static void kinematic_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
static void loclike_flush_tars(bConstraint *con, ListBase *list, bool no_copy)
static void followtrack_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *)
static bConstraintTypeInfo CTI_ROTLIMIT
static void followtrack_evaluate_using_3d_position_object(FollowTrackContext *context, bConstraintOb *cob)
static void sizelike_flush_tars(bConstraint *con, ListBase *list, bool no_copy)
static int loclike_get_tars(bConstraint *con, ListBase *list)
static void transform_new_data(void *cdata)
static void armdef_accumulate_bone(const bConstraintTarget *ct, const bPoseChannel *pchan, const float wco[3], const bool force_envelope, float *r_totweight, float r_sum_mat[4][4], DualQuat *r_sum_dq)
static void transformcache_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
static const blender::bke::GeometryComponent * find_source_component(const blender::bke::GeometrySet &geometry, const blender::bke::AttrDomain domain)
static void rotlike_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *targets)
static void translike_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
static void transformcache_copy(bConstraint *con, bConstraint *srccon)
bConstraint * BKE_constraint_copy_for_object(Object *ob, bConstraint *src)
static int minmax_get_tars(bConstraint *con, ListBase *list)
static void followpath_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *targets)
static bool actcon_get_tarmat(Depsgraph *depsgraph, bConstraint *con, bConstraintOb *cob, bConstraintTarget *ct, float)
static void followtrack_evaluate_using_3d_position_camera(FollowTrackContext *context, bConstraintOb *cob)
static bConstraintTypeInfo CTI_STRETCHTO
bConstraint * BKE_constraint_copy_for_pose(Object *ob, bPoseChannel *pchan, bConstraint *src)
static bConstraint * constraint_list_find_from_target(ListBase *constraints, bConstraintTarget *tgt)
static void objectsolver_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *)
static bConstraintTypeInfo CTI_TRANSFORM
static bool armdef_get_tarmat(Depsgraph *, bConstraint *, bConstraintOb *, bConstraintTarget *ct, float)
static void vectomat(const float vec[3], const float target_up[3], short axis, short upflag, short flags, float m[3][3])
static bConstraintTypeInfo CTI_DAMPTRACK
static int stretchto_get_tars(bConstraint *con, ListBase *list)
static void rotlike_new_data(void *cdata)
static void followtrack_evaluate_using_3d_position(FollowTrackContext *context, bConstraintOb *cob)
static void childof_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *targets)
static void followpath_flush_tars(bConstraint *con, ListBase *list, bool no_copy)
static void con_extern_cb(bConstraint *, ID **idpoin, bool, void *)
static void geometry_attribute_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *targets)
static bool is_custom_space_needed(bConstraint *con)
static bConstraintTypeInfo CTI_CLAMPTO
static bool kinematic_get_tarmat(Depsgraph *, bConstraint *con, bConstraintOb *cob, bConstraintTarget *ct, float)
static void geometry_attribute_flush_tars(bConstraint *con, ListBase *list, const bool no_copy)
static void pivotcon_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *targets)
static bConstraintTypeInfo CTI_SAMEVOL
static void sizelimit_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *)
static void splineik_copy(bConstraint *con, bConstraint *srccon)
static int damptrack_get_tars(bConstraint *con, ListBase *list)
static void clampto_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *targets)
static int basis_cross(int n, int m)
static bConstraintTypeInfo CTI_TRACKTO
static void childof_new_data(void *cdata)
void BKE_constraint_free_data_ex(bConstraint *con, bool do_id_user)
static bConstraintTypeInfo CTI_KINEMATIC
static int splineik_get_tars(bConstraint *con, ListBase *list)
static void samevolume_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *)
static void actcon_flush_tars(bConstraint *con, ListBase *list, bool no_copy)
static void distlimit_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *targets)
void BKE_constraint_unique_name(bConstraint *con, ListBase *list)
static bConstraintTypeInfo CTI_TRANSLIKE
static bConstraintTypeInfo CTI_FOLLOWPATH
static void shrinkwrap_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
static void loclimit_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *)
void BKE_constraints_active_set(ListBase *list, bConstraint *con)
static bConstraintTypeInfo CTI_SIZELIKE
static bool clampto_get_tarmat(Depsgraph *, bConstraint *, bConstraintOb *, bConstraintTarget *ct, float)
static void geometry_attribute_new_data(void *cdata)
bool BKE_constraint_apply_and_remove_for_object(Depsgraph *depsgraph, Scene *scene, ListBase *constraints, Object *ob, bConstraint *con)
static void actcon_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
static void splineik_new_data(void *cdata)
void BKE_constraints_clear_evalob(bConstraintOb *cob)
static MovieClip * followtrack_tracking_clip_get(bConstraint *con, bConstraintOb *cob)
static void locktrack_flush_tars(bConstraint *con, ListBase *list, bool no_copy)
static void transform_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *targets)
static bConstraintTypeInfo CTI_CHILDOF
static void armdef_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *targets)
static void actcon_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *targets)
static void followpath_new_data(void *cdata)
static void transform_flush_tars(bConstraint *con, ListBase *list, bool no_copy)
static void camerasolver_new_data(void *cdata)
static int translike_get_tars(bConstraint *con, ListBase *list)
static bConstraintTypeInfo CTI_SIZELIMIT
static void armdef_accumulate_matrix(const float obmat[4][4], const float iobmat[4][4], const float basemat[4][4], const float bonemat[4][4], const float pivot[3], const float weight, float r_sum_mat[4][4], DualQuat *r_sum_dq)
void BKE_constraint_target_matrix_get(Depsgraph *depsgraph, Scene *scene, bConstraint *con, int index, short ownertype, void *ownerdata, float mat[4][4], float ctime)
static void clampto_flush_tars(bConstraint *con, ListBase *list, bool no_copy)
static bool default_get_tarmat_full_bbone(Depsgraph *, bConstraint *con, bConstraintOb *cob, bConstraintTarget *ct, float)
static bConstraintTypeInfo CTI_DISTLIMIT
static bConstraint * add_new_constraint(Object *ob, bPoseChannel *pchan, const char *name, short type)
static bConstraintTypeInfo CTI_CAMERASOLVER
static bConstraintTypeInfo CTI_LOCLIKE
static void stretchto_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *targets)
static void trackto_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
static void objectsolver_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
static bConstraintTypeInfo CTI_LOCLIMIT
void BKE_constraints_solve(Depsgraph *depsgraph, ListBase *conlist, bConstraintOb *cob, float ctime)
static void trackto_flush_tars(bConstraint *con, ListBase *list, bool no_copy)
static void geometry_attribute_copy_data(bConstraint *con, bConstraint *srccon)
static void camerasolver_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
static void armdef_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
bConstraint * BKE_constraints_find_name(ListBase *list, const char *name)
static void followtrack_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
static bConstraint * add_new_constraint_internal(const char *name, short type)
static void loclike_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
static void constraint_copy_data_ex(bConstraint *dst, bConstraint *src, const int flag, const bool do_extern)
static bool followtrack_context_init(FollowTrackContext *context, bConstraint *con, bConstraintOb *cob)
void BKE_constraint_mat_convertspace(Object *ob, bPoseChannel *pchan, bConstraintOb *cob, float mat[4][4], short from, short to, const bool keep_scale)
bConstraint * BKE_constraint_duplicate_ex(bConstraint *src, const int flag, const bool do_extern)
static void con_unlink_refs_cb(bConstraint *, ID **idpoin, bool is_reference, void *)
static void loclike_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *targets)
static int childof_get_tars(bConstraint *con, ListBase *list)
void BKE_constraint_panel_expand(bConstraint *con)
static int actcon_get_tars(bConstraint *con, ListBase *list)
static void shrinkwrap_new_data(void *cdata)
static void objectsolver_new_data(void *cdata)
static void con_fix_copied_refs_cb(bConstraint *, ID **idpoin, bool is_reference, void *)
static void contarget_get_mesh_mat(Object *ob, const char *substring, float mat[4][4])
static void splineik_flush_tars(bConstraint *con, ListBase *list, bool no_copy)
static void distlimit_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
static void rotlike_flush_tars(bConstraint *con, ListBase *list, bool no_copy)
static bConstraintTypeInfo CTI_FOLLOWTRACK
static void armdef_copy(bConstraint *con, bConstraint *srccon)
static bConstraintTypeInfo CTI_SHRINKWRAP
static void kinematic_new_data(void *cdata)
static bConstraintTypeInfo CTI_ARMATURE
static void sizelike_new_data(void *cdata)
void BKE_constraints_copy_ex(ListBase *dst, const ListBase *src, const int flag, bool do_extern)
static void childof_flush_tars(bConstraint *con, ListBase *list, bool no_copy)
bool BKE_constraint_apply_for_pose(Depsgraph *depsgraph, Scene *scene, Object *ob, bPoseChannel *pchan, bConstraint *con)
static int sizelike_get_tars(bConstraint *con, ListBase *list)
static void stretchto_flush_tars(bConstraint *con, ListBase *list, bool no_copy)
bConstraint * BKE_constraint_add_for_pose(Object *ob, bPoseChannel *pchan, const char *name, short type)
void BKE_constraints_free_ex(ListBase *list, bool do_id_user)
static bool component_is_available(const blender::bke::GeometrySet &geometry, const blender::bke::GeometryComponent::Type type, const blender::bke::AttrDomain domain)
static int rotlike_get_tars(bConstraint *con, ListBase *list)
static void translike_flush_tars(bConstraint *con, ListBase *list, bool no_copy)
static bool constraint_remove(ListBase *list, bConstraint *con)
const bConstraintTypeInfo * BKE_constraint_typeinfo_get(bConstraint *con)
static void constraints_init_typeinfo()
static bool geometry_attribute_get_tarmat(Depsgraph *, bConstraint *con, bConstraintOb *, bConstraintTarget *ct, float)
static void translike_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *targets)
static bool followpath_get_tarmat(Depsgraph *, bConstraint *con, bConstraintOb *, bConstraintTarget *ct, float)
static void rotlike_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
static void followpath_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
static void transformcache_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *targets)
static void locktrack_new_data(void *cdata)
static int locktrack_get_tars(bConstraint *con, ListBase *list)
static void shrinkwrap_evaluate(bConstraint *, bConstraintOb *cob, ListBase *targets)
static void shrinkwrap_flush_tars(bConstraint *con, ListBase *list, bool no_copy)
#define SINGLETARGETNS_GET_TARS(con, datatar, ct, list)
static float clamp_angle(const float angle, const float min, const float max)
void BKE_constraint_custom_object_space_init(bConstraintOb *cob, bConstraint *con)
static int distlimit_get_tars(bConstraint *con, ListBase *list)
static void unit_ct_matrix_nullsafe(bConstraintTarget *ct)
static bConstraintTypeInfo CTI_SPLINEIK
static void splineik_free(bConstraint *con)
static int pivotcon_get_tars(bConstraint *con, ListBase *list)
static void trackto_new_data(void *cdata)
void BKE_constraints_id_loop(ListBase *conlist, ConstraintIDFunc func, const int flag, void *userdata)
static void followtrack_undistort_if_needed(FollowTrackContext *context, const int clip_width, const int clip_height, float marker_position[2])
bool BKE_constraint_remove_ex(ListBase *list, Object *ob, bConstraint *con)
static void value_attribute_to_matrix(float r_matrix[4][4], const blender::GPointer value, const Attribute_Data_Type data_type)
bConstraint * BKE_constraints_active_get(ListBase *list)
void BKE_constraint_targets_flush(bConstraint *con, ListBase *targets, bool no_copy)
static float wrap_rad_angle(const float angle)
static bool shrinkwrap_get_tarmat(Depsgraph *, bConstraint *con, bConstraintOb *cob, bConstraintTarget *ct, float)
static void damptrack_new_data(void *cdata)
static void damptrack_flush_tars(bConstraint *con, ListBase *list, bool no_copy)
bConstraintOb * BKE_constraints_make_evalob(Depsgraph *depsgraph, Scene *scene, Object *ob, void *subdata, short datatype)
static Object * followtrack_camera_object_get(bConstraint *con, bConstraintOb *cob)
static void trackto_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *targets)
static void damptrack_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *targets)
static void followtrack_evaluate_using_2d_position(FollowTrackContext *context, bConstraintOb *cob)
static bConstraintTypeInfo CTI_ATTRIBUTE
static void constraint_target_to_mat4(Object *ob, const char *substring, bConstraintOb *cob, float mat[4][4], short from, short to, short flag, float headtail)
static void contarget_get_lattice_mat(Object *ob, const char *substring, float mat[4][4])
static bConstraintTypeInfo CTI_MINMAX
static int shrinkwrap_get_tars(bConstraint *con, ListBase *list)
void BKE_constraints_free(ListBase *list)
static void pivotcon_flush_tars(bConstraint *con, ListBase *list, bool no_copy)
static blender::bke::AttrDomain domain_value_to_attribute(const Attribute_Domain domain)
static short CTI_INIT
static bConstraint * constraint_find_original(Object *ob, bPoseChannel *pchan, bConstraint *con, Object **r_orig_ob)
static bConstraintTypeInfo * constraintsTypeInfo[NUM_CONSTRAINT_TYPES]
static void sizelike_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *targets)
static int armdef_get_tars(bConstraint *con, ListBase *list)
static void followtrack_project_to_depth_object_if_needed(FollowTrackContext *context, bConstraintOb *cob)
static void transformcache_free(bConstraint *con)
static void pivotcon_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
static MovieTrackingObject * followtrack_tracking_object_get(bConstraint *con, bConstraintOb *cob)
static const float track_dir_vecs[6][3]
static int trackto_get_tars(bConstraint *con, ListBase *list)
#define SINGLETARGETNS_FLUSH_TARS(con, datatar, ct, list, no_copy)
static void locktrack_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
int BKE_constraint_targets_get(bConstraint *con, ListBase *r_targets)
static void splineik_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
static void minmax_flush_tars(bConstraint *con, ListBase *list, bool no_copy)
void BKE_constraint_free_data(bConstraint *con)
const bConstraintTypeInfo * BKE_constraint_typeinfo_from_type(int type)
bConstraint * BKE_constraint_find_from_target(Object *ob, bConstraintTarget *tgt, bPoseChannel **r_pchan)
#define VALID_CONS_TARGET(ct)
static void actcon_new_data(void *cdata)
static void loclike_new_data(void *cdata)
bConstraint * BKE_constraint_add_for_object(Object *ob, const char *name, short type)
static void armdef_free(bConstraint *con)
static void damptrack_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
#define SINGLETARGET_GET_TARS(con, datatar, datasubtarget, ct, list)
static bConstraintTypeInfo CTI_LOCKTRACK
void BKE_constraint_blend_write(BlendWriter *writer, ListBase *conlist)
static void geometry_attribute_free_data(bConstraint *con)
static int transform_get_tars(bConstraint *con, ListBase *list)
void BKE_constraint_blend_read_data(BlendDataReader *reader, ID *id_owner, ListBase *lb)
static int geometry_attribute_get_tars(bConstraint *con, ListBase *list)
static bConstraintTypeInfo CTI_OBJECTSOLVER
static bConstraintTypeInfo CTI_PIVOT
bool BKE_constraint_apply_for_object(Depsgraph *depsgraph, Scene *scene, Object *ob, bConstraint *con)
static void con_invoke_id_looper(const bConstraintTypeInfo *cti, bConstraint *con, ConstraintIDFunc func, const int, void *userdata)
static void geometry_attribute_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
static bConstraintTypeInfo CTI_ACTION
static int clampto_get_tars(bConstraint *con, ListBase *list)
static void rotlimit_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *)
static void kinematic_flush_tars(bConstraint *con, ListBase *list, bool no_copy)
static void distlimit_flush_tars(bConstraint *con, ListBase *list, bool no_copy)
static void clampto_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
static void followtrack_new_data(void *cdata)
#define SINGLETARGET_FLUSH_TARS(con, datatar, datasubtarget, ct, list, no_copy)
static void childof_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
static void distlimit_new_data(void *cdata)
static void transform_id_looper(bConstraint *con, ConstraintIDFunc func, void *userdata)
#define expf(x)
#define powf(x, y)
#define asinf(x)
#define acosf(x)
#define offsetof(t, d)
KDTree_3d * tree
#define rot(x, k)
#define printf(...)
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
int count
#define LOG(level)
Definition log.h:97
void * MEM_callocN(size_t len, const char *str)
Definition mallocn.cc:118
void * MEM_dupallocN(const void *vmemh)
Definition mallocn.cc:143
void MEM_freeN(void *vmemh)
Definition mallocn.cc:113
#define G(x, y, z)
GeometrySet object_get_evaluated_geometry_set(const Object &object, bool apply_subdiv=true)
void USD_get_transform(CacheReader *reader, float r_mat_world[4][4], float time, float scale)
T safe_divide(const T &a, const T &b)
VecBase< float, 3 > cross_high_precision(const VecBase< float, 3 > &a, const VecBase< float, 3 > &b)
MatBase< float, 4, 4 > float4x4
VecBase< float, 4 > float4
VecBase< float, 3 > float3
const char * name
#define fabsf
#define sqrtf
#define atanf
#define ceilf
#define min(a, b)
Definition sort.cc:36
#define FLT_MAX
Definition stdcycles.h:14
char identifier[258]
CustomData vdata
float vec[4]
float arm_head[3]
float arm_tail[3]
float arm_mat[4][4]
ListBase nurb
float ctime
float * verts
MovieTracking * tracking
MovieTrackingObject * tracking_object
MovieTrackingTrack * track
Depsgraph * depsgraph
Definition DNA_ID.h:414
char name[258]
Definition DNA_ID.h:432
struct MDeformVert * dvert
struct BPoint * def
void * last
void * first
float mat[4][4]
struct MovieTracking tracking
short flagu
ListBase constraints
struct bPose * pose
ObjectRuntimeHandle * runtime
float constinv[4][4]
float parentinv[4][4]
struct Object * parent
struct MovieClip * clip
struct RenderData r
struct Object * camera
float startmat[4][4]
float matrix[4][4]
float space_obj_world_matrix[4][4]
struct bPoseChannel * pchan
struct Scene * scene
struct Object * ob
struct Depsgraph * depsgraph
void(* id_looper)(struct bConstraint *con, ConstraintIDFunc func, void *userdata)
int(* get_constraint_targets)(struct bConstraint *con, struct ListBase *list)
void(* evaluate_constraint)(struct bConstraint *con, struct bConstraintOb *cob, struct ListBase *targets)
void(* flush_constraint_targets)(struct bConstraint *con, struct ListBase *list, bool no_copy)
bool(* get_target_matrix)(struct Depsgraph *depsgraph, struct bConstraint *con, struct bConstraintOb *cob, struct bConstraintTarget *ct, float ctime)
void(* copy_data)(struct bConstraint *con, struct bConstraint *src)
void(* free_data)(struct bConstraint *con)
void(* new_data)(void *cdata)
struct Object * space_object
struct bConstraint * prev
struct bConstraint * next
struct Mat4 * bbone_deform_mats
struct Mat4 * bbone_pose_mats
struct Mat4 * bbone_rest_mats
struct Bone * bone
float chan_mat[4][4]
float constinv[4][4]
struct bPoseChannel_Runtime runtime
struct bPoseChannel * orig_pchan
float pose_mat[4][4]
ListBase chanbase
BVHTree_RayCastCallback raycast_callback
i
Definition text_draw.cc:230
max
Definition text_draw.cc:251
static int blend(const Tex *tex, const float texvec[3], TexResult *texres)
uint len
#define N_(msgid)
uint8_t flag
Definition wm_window.cc:145