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