Blender V5.3
object_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#include <algorithm>
10#include <cstdio>
11#include <cstring>
12
13#include "MEM_guardedalloc.h"
14
15#include "BLI_dynstr.hh"
16#include "BLI_listbase.hh"
17#include "BLI_math_matrix_c.hh"
18#include "BLI_math_vector_c.hh"
19#include "BLI_string_utf8.hh"
20#include "BLI_utildefines.hh"
21
22#include "BLT_translation.hh"
23
24#include "DNA_anim_types.h"
25#include "DNA_armature_types.h"
27#include "DNA_curve_types.h"
28#include "DNA_object_types.h"
29#include "DNA_scene_types.h"
30
31#include "BIK_api.h"
32#include "BKE_action.hh"
33#include "BKE_armature.hh"
34#include "BKE_constraint.h"
35#include "BKE_context.hh"
36#include "BKE_fcurve.hh"
37#include "BKE_layer.hh"
38#include "BKE_library.hh"
39#include "BKE_main.hh"
40#include "BKE_object.hh"
41#include "BKE_report.hh"
42#include "BKE_tracking.hh"
43
44#include "DEG_depsgraph.hh"
47
48#ifdef WITH_PYTHON
49# include "BPY_extern.hh"
50#endif
51
52#include "WM_api.hh"
53#include "WM_types.hh"
54
55#include "RNA_access.hh"
56#include "RNA_define.hh"
57#include "RNA_enum_types.hh"
58#include "RNA_path.hh"
59#include "RNA_prototypes.hh"
60
61#include "ED_object.hh"
62#include "ED_screen.hh"
63
64#include "ANIM_action.hh"
65#include "ANIM_action_legacy.hh"
66#include "ANIM_animdata.hh"
67
68#include "UI_interface.hh"
70#include "UI_resources.hh"
71
72#include "object_intern.hh"
73
74namespace blender::ed::object {
75
76/* ------------------------------------------------------------------- */
79
80ListBaseT<bConstraint> *constraint_active_list(Object *ob)
81{
82 if (ob == nullptr) {
83 return nullptr;
84 }
85
86 if (ob->mode & OB_MODE_POSE) {
87 bPoseChannel *pchan;
88
90 if (pchan) {
91 return &pchan->constraints;
92 }
93 }
94 else {
95 return &ob->constraints;
96 }
97
98 return nullptr;
99}
100
101ListBaseT<bConstraint> *pose_constraint_list(const bContext *C)
102{
103 bPoseChannel *pose_bone = static_cast<bPoseChannel *>(CTX_data_pointer_get(C, "pose_bone").data);
104 if (pose_bone == nullptr) {
105 pose_bone = static_cast<bPoseChannel *>(CTX_data_pointer_get(C, "active_pose_bone").data);
106 if (pose_bone == nullptr) {
107 return nullptr;
108 }
109 }
110
111 return &pose_bone->constraints;
112}
113
114ListBaseT<bConstraint> *constraint_list_from_constraint(Object *ob,
115 bConstraint *con,
116 bPoseChannel **r_pchan)
117{
118 if (r_pchan) {
119 *r_pchan = nullptr;
120 }
121
122 if (ELEM(nullptr, ob, con)) {
123 return nullptr;
124 }
125
126 /* try object constraints first */
127 if (BLI_findindex(&ob->constraints, con) != -1) {
128 return &ob->constraints;
129 }
130
131 /* if armature, try pose bones too */
132 if (ob->pose) {
133 /* try each bone in order
134 * NOTE: it's not possible to directly look up the active bone yet, so this will have to do
135 */
136 for (bPoseChannel &pchan : ob->pose->chanbase) {
137 if (BLI_findindex(&pchan.constraints, con) != -1) {
138
139 if (r_pchan) {
140 *r_pchan = &pchan;
141 }
142
143 return &pchan.constraints;
144 }
145 }
146 }
147
148 /* done */
149 return nullptr;
150}
151
156
158
159/* ------------------------------------------------------------------- */
162
163/* helper function for add_constraint - sets the last target for the active constraint */
165 Object *target,
166 const char subtarget[],
167 int index)
168{
169 ListBaseT<bConstraintTarget> targets = {nullptr, nullptr};
171 int targets_num, i;
172
173 if (BKE_constraint_targets_get(con, &targets)) {
174 targets_num = targets.count();
175
176 if (index < 0) {
177 if (abs(index) < targets_num) {
178 index = targets_num - abs(index);
179 }
180 else {
181 index = targets_num - 1;
182 }
183 }
184 else if (index >= targets_num) {
185 index = targets_num - 1;
186 }
187
188 for (ct = targets.first(), i = 0; ct; ct = ct->next, i++) {
189 if (i == index) {
190 ct->tar = target;
191 STRNCPY_UTF8(ct->subtarget, subtarget);
192 break;
193 }
194 }
195
196 BKE_constraint_targets_flush(con, &targets, false);
197 }
198}
199
201
202/* ------------------------------------------------------------------- */
205
206static void test_constraint(
207 Main *bmain, Object *owner, bPoseChannel *pchan, bConstraint *con, int type)
208{
209 ListBaseT<bConstraintTarget> targets = {nullptr, nullptr};
210 bool check_targets = true;
211
212 /* clear disabled-flag first */
214
215 if (con->type == CONSTRAINT_TYPE_KINEMATIC) {
216 bKinematicConstraint *data = static_cast<bKinematicConstraint *>(con->data);
217
218 /* Bad: we need a separate set of checks here as pole-target is optional...
219 * otherwise pole-target must exist too or else the constraint is deemed invalid. */
220
221 /* default IK check ... */
222 if (BKE_object_exists_check(bmain, data->tar) == 0) {
223 data->tar = nullptr;
224 con->flag |= CONSTRAINT_DISABLE;
225 }
226 else if (data->tar == owner) {
228 con->flag |= CONSTRAINT_DISABLE;
229 }
230 }
231
232 if (data->poletar) {
233 if (BKE_object_exists_check(bmain, data->poletar) == 0) {
234 data->poletar = nullptr;
235 con->flag |= CONSTRAINT_DISABLE;
236 }
237 else if (data->poletar == owner) {
238 if (!BKE_armature_find_bone_name(BKE_armature_from_object(owner), data->polesubtarget)) {
239 con->flag |= CONSTRAINT_DISABLE;
240 }
241 }
242 }
243 /* ... can be overwritten here */
244 BIK_test_constraint(owner, con);
245 /* targets have already been checked for this */
246 check_targets = false;
247 }
248 else if (con->type == CONSTRAINT_TYPE_PIVOT) {
249 bPivotConstraint *data = static_cast<bPivotConstraint *>(con->data);
250
251 /* target doesn't have to exist, but if it is non-null, it must exist! */
252 if (data->tar && BKE_object_exists_check(bmain, data->tar) == 0) {
253 data->tar = nullptr;
254 con->flag |= CONSTRAINT_DISABLE;
255 }
256 else if (data->tar == owner) {
258 con->flag |= CONSTRAINT_DISABLE;
259 }
260 }
261
262 /* targets have already been checked for this */
263 check_targets = false;
264 }
265 else if (con->type == CONSTRAINT_TYPE_ACTION) {
266 bActionConstraint *data = static_cast<bActionConstraint *>(con->data);
267
268 /* validate action */
269 if (data->act == nullptr) {
270 /* must have action */
271 con->flag |= CONSTRAINT_DISABLE;
272 }
273 else {
274 /* The slot was assigned, so assume that it is suitable to animate the
275 * owner (only suitable slots appear in the drop-down). */
276 animrig::Action &action = data->act->wrap();
277 animrig::Slot *slot = action.slot_for_handle(data->action_slot_handle);
278 if (!slot) {
279 con->flag |= CONSTRAINT_DISABLE;
280 }
281 }
282
283 /* Skip target checking if we're not using it */
284 if (data->flag & ACTCON_USE_EVAL_TIME) {
285 check_targets = false;
286 }
287 }
288 else if (con->type == CONSTRAINT_TYPE_FOLLOWPATH) {
290
291 /* don't allow track/up axes to be the same */
292 if (data->upflag == data->trackflag) {
293 con->flag |= CONSTRAINT_DISABLE;
294 }
295 if (data->upflag + 3 == data->trackflag) {
296 con->flag |= CONSTRAINT_DISABLE;
297 }
298 }
299 else if (con->type == CONSTRAINT_TYPE_TRACKTO) {
300 bTrackToConstraint *data = static_cast<bTrackToConstraint *>(con->data);
301
302 /* don't allow track/up axes to be the same */
303 if (int(data->reserved2) == int(data->reserved1)) {
304 con->flag |= CONSTRAINT_DISABLE;
305 }
306 if (int(data->reserved2) + 3 == int(data->reserved1)) {
307 con->flag |= CONSTRAINT_DISABLE;
308 }
309 }
310 else if (con->type == CONSTRAINT_TYPE_LOCKTRACK) {
311 bLockTrackConstraint *data = static_cast<bLockTrackConstraint *>(con->data);
312
313 if (int(data->lockflag) == int(data->trackflag)) {
314 con->flag |= CONSTRAINT_DISABLE;
315 }
316 if (int(data->lockflag) + 3 == int(data->trackflag)) {
317 con->flag |= CONSTRAINT_DISABLE;
318 }
319 }
320 else if (con->type == CONSTRAINT_TYPE_SPLINEIK) {
321 bSplineIKConstraint *data = static_cast<bSplineIKConstraint *>(con->data);
322
323 /* if the number of points does not match the amount required by the chain length,
324 * free the points array and request a rebind...
325 */
326 if ((data->points == nullptr) || (data->numpoints != data->chainlen + 1)) {
327 MEM_SAFE_DELETE(data->points);
328 data->numpoints = 0;
329
330 /* clear the bound flag, forcing a rebind next time this is evaluated */
332 }
333 }
334 else if (con->type == CONSTRAINT_TYPE_FOLLOWTRACK) {
336
337 if ((data->flag & FOLLOWTRACK_ACTIVECLIP) == 0) {
338 if (data->clip != nullptr && data->track[0]) {
339 MovieTracking *tracking = &data->clip->tracking;
340 MovieTrackingObject *tracking_object;
341
342 if (data->object[0]) {
343 tracking_object = BKE_tracking_object_get_named(tracking, data->object);
344 }
345 else {
346 tracking_object = BKE_tracking_object_get_camera(tracking);
347 }
348
349 if (!tracking_object) {
350 con->flag |= CONSTRAINT_DISABLE;
351 }
352 else {
353 if (!BKE_tracking_object_find_track_with_name(tracking_object, data->track)) {
354 con->flag |= CONSTRAINT_DISABLE;
355 }
356 }
357 }
358 else {
359 con->flag |= CONSTRAINT_DISABLE;
360 }
361 }
362 }
363 else if (con->type == CONSTRAINT_TYPE_CAMERASOLVER) {
365
366 if ((data->flag & CAMERASOLVER_ACTIVECLIP) == 0 && (data->clip == nullptr)) {
367 con->flag |= CONSTRAINT_DISABLE;
368 }
369 }
370 else if (con->type == CONSTRAINT_TYPE_OBJECTSOLVER) {
372
373 if ((data->flag & OBJECTSOLVER_ACTIVECLIP) == 0 && (data->clip == nullptr)) {
374 con->flag |= CONSTRAINT_DISABLE;
375 }
376 }
377 else if (con->type == CONSTRAINT_TYPE_TRANSFORM_CACHE) {
379
380 if ((data->cache_file == nullptr) || (data->object_path[0] == '\0')) {
381 con->flag |= CONSTRAINT_DISABLE;
382 }
383 }
384
385 /* Check targets for constraints */
386 if (check_targets && BKE_constraint_targets_get(con, &targets)) {
387 /* disable and clear constraints targets that are incorrect */
388 for (bConstraintTarget &ct : targets) {
389 /* general validity checks (for those constraints that need this) */
390 if (BKE_object_exists_check(bmain, ct.tar) == 0) {
391 /* object doesn't exist, but constraint requires target */
392 ct.tar = nullptr;
393 con->flag |= CONSTRAINT_DISABLE;
394 }
395 else if (ct.tar == owner) {
396 if (type == CONSTRAINT_OBTYPE_BONE) {
397 if (!BKE_armature_find_bone_name(BKE_armature_from_object(owner), ct.subtarget)) {
398 /* bone must exist in armature... */
399 /* TODO: clear subtarget? */
400 con->flag |= CONSTRAINT_DISABLE;
401 }
402 else if (STREQ(pchan->name, ct.subtarget)) {
403 /* cannot target self */
404 ct.subtarget[0] = '\0';
405 con->flag |= CONSTRAINT_DISABLE;
406 }
407 }
408 else {
409 /* cannot use self as target */
410 ct.tar = nullptr;
411 con->flag |= CONSTRAINT_DISABLE;
412 }
413 }
414
415 /* target checks for specific constraints */
416 if (ELEM(con->type,
420 {
421 if (ct.tar) {
422 /* The object type check is only needed here in case we have a placeholder
423 * object assigned (because the library containing the curve is missing).
424 *
425 * In other cases it should be impossible to have a type mismatch.
426 */
427 if (ct.tar->type != OB_CURVES_LEGACY) {
428 con->flag |= CONSTRAINT_DISABLE;
429 }
430 else {
431 Curve *cu = id_cast<Curve *>(ct.tar->data);
432
433 /* auto-set 'Path' setting on curve so this works. */
434 cu->flag |= CU_PATH;
435 }
436 }
437 }
438 else if (con->type == CONSTRAINT_TYPE_ARMATURE) {
439 if (ct.tar) {
440 /* The object type check is only needed here in case we have a placeholder
441 * object assigned (because the library containing the armature is missing).
442 *
443 * In other cases it should be impossible to have a type mismatch.
444 */
445 if (ct.tar->type != OB_ARMATURE) {
446 con->flag |= CONSTRAINT_DISABLE;
447 }
448 else if (!BKE_armature_find_bone_name(BKE_armature_from_object(ct.tar), ct.subtarget)) {
449 /* bone must exist in armature... */
450 con->flag |= CONSTRAINT_DISABLE;
451 }
452 }
453 }
454 }
455
456 /* free any temporary targets */
457 BKE_constraint_targets_flush(con, &targets, false);
458 }
459 else if (check_targets) {
460 /* constraints with empty target list that actually require targets */
462 con->flag |= CONSTRAINT_DISABLE;
463 }
464 }
465}
466
467static int constraint_type_get(Object *owner, bPoseChannel *pchan)
468{
469 int type;
470 /* Check parents */
471 if (pchan) {
472 switch (owner->type) {
473 case OB_ARMATURE:
475 break;
476 default:
478 break;
479 }
480 }
481 else {
483 }
484 return type;
485}
486
487/* checks validity of object pointers, and NULLs,
488 * if Bone doesn't exist it sets the CONSTRAINT_DISABLE flag.
489 */
490static void test_constraints(Main *bmain, Object *ob, bPoseChannel *pchan)
491{
492 ListBaseT<bConstraint> *conlist = nullptr;
493 int type;
494
495 if (ob == nullptr) {
496 return;
497 }
498
499 type = constraint_type_get(ob, pchan);
500
501 /* Get the constraint list for this object */
502 switch (type) {
504 conlist = &ob->constraints;
505 break;
507 conlist = &pchan->constraints;
508 break;
509 }
510
511 /* Check all constraints - is constraint valid? */
512 if (conlist) {
513 for (bConstraint &curcon : *conlist) {
514 test_constraint(bmain, ob, pchan, &curcon, type);
515 }
516 }
517}
518
520{
521 if (ob->constraints.first_) {
522 test_constraints(bmain, ob, nullptr);
523 }
524
525 if (ob->type == OB_ARMATURE && ob->pose) {
526 for (bPoseChannel &pchan : ob->pose->chanbase) {
527 if (pchan.constraints.first_) {
528 test_constraints(bmain, ob, &pchan);
529 }
530 }
531 }
532}
533
534static void object_test_constraint(Main *bmain, Object *ob, bConstraint *con)
535{
536 if (ob->type == OB_ARMATURE && ob->pose) {
537 if (BLI_findindex(&ob->constraints, con) != -1) {
538 test_constraint(bmain, ob, nullptr, con, CONSTRAINT_OBTYPE_OBJECT);
539 }
540 else {
541 for (bPoseChannel &pchan : ob->pose->chanbase) {
542 if (BLI_findindex(&pchan.constraints, con) != -1) {
543 test_constraint(bmain, ob, &pchan, con, CONSTRAINT_OBTYPE_BONE);
544 break;
545 }
546 }
547 }
548 }
549 else {
550 test_constraint(bmain, ob, nullptr, con, CONSTRAINT_OBTYPE_OBJECT);
551 }
552}
553
555
556/* ------------------------------------------------------------------- */
559
560#define EDIT_CONSTRAINT_OWNER_OBJECT 0
561#define EDIT_CONSTRAINT_OWNER_BONE 1
562
565 "OBJECT",
566 0,
567 "Object",
568 "Edit a constraint on the active object"},
569 {EDIT_CONSTRAINT_OWNER_BONE, "BONE", 0, "Bone", "Edit a constraint on the active bone"},
570 {0, nullptr, 0, nullptr, nullptr},
571};
572
574 StructRNA *rna_type,
575 const bool is_liboverride_allowed)
576{
577 PointerRNA ptr = CTX_data_pointer_get_type(C, "constraint", rna_type);
578 Object *ob = (ptr.owner_id) ? id_cast<Object *>(ptr.owner_id) : context_active_object(C);
579 bConstraint *con = static_cast<bConstraint *>(ptr.data);
580
582 return false;
583 }
584
585 if (ptr.owner_id != nullptr && !ID_IS_EDITABLE(ptr.owner_id)) {
586 CTX_wm_operator_poll_msg_set(C, "Cannot edit library data");
587 return false;
588 }
589
590 if (!is_liboverride_allowed && BKE_constraint_is_nonlocal_in_liboverride(ob, con)) {
592 C, "Cannot edit constraints coming from linked data in a library override");
593 return false;
594 }
595
596 return true;
597}
598
600{
601 return edit_constraint_poll_generic(C, RNA_Constraint, false);
602}
603
604/* Used by operators performing actions allowed also on constraints from the overridden linked
605 * object (not only from added 'local' ones). */
607{
608 return edit_constraint_poll_generic(C, RNA_Constraint, true);
609}
610
612{
613 PropertyRNA *prop;
614 prop = RNA_def_string(
615 ot->srna, "constraint", nullptr, MAX_NAME, "Constraint", "Name of the constraint to edit");
617 prop = RNA_def_enum(
618 ot->srna, "owner", constraint_owner_items, 0, "Owner", "The owner of this constraint");
620}
621
623{
625 ot->srna, "report", false, "Report", "Create a notification after the operation");
627}
628
630 wmOperator *op,
631 const wmEvent *event,
632 wmOperatorStatus *r_retval)
633{
634 PointerRNA ptr = CTX_data_pointer_get_type(C, "constraint", RNA_Constraint);
635 Object *ob = (ptr.owner_id) ? id_cast<Object *>(ptr.owner_id) : context_active_object(C);
636 bConstraint *con;
637 ListBaseT<bConstraint> *list;
638
639 if (RNA_struct_property_is_set(op->ptr, "constraint") &&
640 RNA_struct_property_is_set(op->ptr, "owner"))
641 {
642 return true;
643 }
644
645 if (ptr) {
646 con = static_cast<bConstraint *>(ptr.data);
647 RNA_string_set(op->ptr, "constraint", con->name);
648
649 list = constraint_list_from_constraint(ob, con, nullptr);
650
651 if (&ob->constraints == list) {
653 }
654 else {
656 }
657
658 return true;
659 }
660
661 if (event == nullptr) {
662 if (r_retval != nullptr) {
663 *r_retval = OPERATOR_CANCELLED;
664 }
665 return false;
666 }
667
668 /* Check the custom data of panels under the mouse for an effect. */
670
671 if (panel_ptr == nullptr || !*panel_ptr) {
672 /* The operators using this function can typically be called from UIs that aren't related to
673 * the constraints UI at all. So include #OPERATOR_PASS_THROUGH to not block events from
674 * reaching other operators/handlers. */
676 return false;
677 }
678
679 if (!RNA_struct_is_a(panel_ptr->type, RNA_Constraint)) {
680 /* Work around multiple operators using the same shortcut. The operators for the other
681 * stacks in the property editor use the same key, and will not run after these return
682 * OPERATOR_CANCELLED. */
684 return false;
685 }
686
687 con = static_cast<bConstraint *>(panel_ptr->data);
688 RNA_string_set(op->ptr, "constraint", con->name);
689 list = constraint_list_from_constraint(ob, con, nullptr);
690 RNA_enum_set(op->ptr,
691 "owner",
694
695 return true;
696}
697
699{
700 char constraint_name[MAX_NAME];
701 int owner = RNA_enum_get(op->ptr, "owner");
702 bConstraint *con;
703 ListBaseT<bConstraint> *list = nullptr;
704
705 RNA_string_get(op->ptr, "constraint", constraint_name);
706
707 if (owner == EDIT_CONSTRAINT_OWNER_BONE) {
709 if (!list) {
710 return nullptr;
711 }
712 }
713 else {
714 list = &ob->constraints;
715 }
716
717 con = BKE_constraints_find_name(list, constraint_name);
718#if 0
719 if (G.debug & G_DEBUG) {
720 printf("constraint found = %p, %s\n", (void *)con, (con) ? con->name : "<Not found>");
721 }
722#endif
723
724 if (con && (type != 0) && (con->type != type)) {
725 con = nullptr;
726 }
727
728 return con;
729}
730
732
733/* ------------------------------------------------------------------- */
738
740{
741 Main *bmain = CTX_data_main(C);
744 bStretchToConstraint *data = (con) ? static_cast<bStretchToConstraint *>(con->data) : nullptr;
745
746 /* despite 3 layers of checks, we may still not be able to find a constraint */
747 if (data == nullptr) {
748 return OPERATOR_CANCELLED;
749 }
750
751 /* just set original length to 0.0, which will cause a reset on next recalc */
752 data->orglength = 0.0f;
753 constraint_update(bmain, ob);
754
756 return OPERATOR_FINISHED;
757}
758
760 wmOperator *op,
761 const wmEvent * /*event*/)
762{
763 if (edit_constraint_invoke_properties(C, op, nullptr, nullptr)) {
764 return stretchto_reset_exec(C, op);
765 }
766 return OPERATOR_CANCELLED;
767}
768
770{
771 /* identifiers */
772 ot->name = "Reset Original Length";
773 ot->idname = "CONSTRAINT_OT_stretchto_reset";
774 ot->description = "Reset original length of bone for Stretch To Constraint";
775
776 /* callbacks */
780
781 /* flags */
783
784 /* properties */
786}
787
789
790/* ------------------------------------------------------------------- */
795
797{
798 Main *bmain = CTX_data_main(C);
801 bDistLimitConstraint *data = (con) ? static_cast<bDistLimitConstraint *>(con->data) : nullptr;
802
803 /* despite 3 layers of checks, we may still not be able to find a constraint */
804 if (data == nullptr) {
805 return OPERATOR_CANCELLED;
806 }
807
808 /* just set original length to 0.0, which will cause a reset on next recalc */
809 data->dist = 0.0f;
810 constraint_update(bmain, ob);
811
813 return OPERATOR_FINISHED;
814}
815
817 wmOperator *op,
818 const wmEvent * /*event*/)
819{
820 if (edit_constraint_invoke_properties(C, op, nullptr, nullptr)) {
821 return limitdistance_reset_exec(C, op);
822 }
823 return OPERATOR_CANCELLED;
824}
825
827{
828 /* identifiers */
829 ot->name = "Reset Distance";
830 ot->idname = "CONSTRAINT_OT_limitdistance_reset";
831 ot->description = "Reset limiting distance for Limit Distance Constraint";
832
833 /* callbacks */
837
838 /* flags */
840
841 /* properties */
843}
844
846
847/* ------------------------------------------------------------------- */
850
851/* Force evaluation so that the 'set inverse' flag is handled.
852 * No-op when the constraint is enabled, as in such cases the evaluation will happen anyway.
853 */
855{
856 if ((con->flag & (CONSTRAINT_DISABLE | CONSTRAINT_OFF)) == 0) {
857 return;
858 }
859
860 Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
861 Scene *scene = DEG_get_evaluated_scene(depsgraph);
862
863 eBConstraint_Flags flag_backup = con->flag;
865 BKE_object_eval_constraints(depsgraph, scene, ob);
866 con->flag = flag_backup;
867}
868
869/* ChildOf Constraint - set inverse callback */
871{
872 Main *bmain = CTX_data_main(C);
875 bChildOfConstraint *data = (con) ? static_cast<bChildOfConstraint *>(con->data) : nullptr;
876
877 /* despite 3 layers of checks, we may still not be able to find a constraint */
878 if (data == nullptr) {
879 printf("DEBUG: Child-Of Set Inverse - object = '%s'\n", (ob) ? ob->id.name + 2 : "<None>");
880 BKE_report(op->reports, RPT_ERROR, "Could not find constraint data for Child-Of Set Inverse");
881 return OPERATOR_CANCELLED;
882 }
883
884 /* Set a flag to request recalculation on next update. */
885 data->flag |= CHILDOF_SET_INVERSE;
886
887 /* Force constraint to run, it will perform the recalculation. */
889
890 constraint_update(bmain, ob);
892
893 return OPERATOR_FINISHED;
894}
895
897 wmOperator *op,
898 const wmEvent * /*event*/)
899{
900 if (edit_constraint_invoke_properties(C, op, nullptr, nullptr)) {
901 return childof_set_inverse_exec(C, op);
902 }
903 return OPERATOR_CANCELLED;
904}
905
907{
908 /* identifiers */
909 ot->name = "Set Inverse";
910 ot->idname = "CONSTRAINT_OT_childof_set_inverse";
911 ot->description = "Set inverse correction for Child Of constraint";
912
913 /* callbacks */
917
918 /* flags */
920
921 /* properties */
923}
924
925/* ChildOf Constraint - clear inverse callback */
927{
928 Main *bmain = CTX_data_main(C);
931 bChildOfConstraint *data = (con) ? static_cast<bChildOfConstraint *>(con->data) : nullptr;
932
933 if (data == nullptr) {
934 BKE_report(op->reports, RPT_ERROR, "Child Of constraint not found");
935 return OPERATOR_CANCELLED;
936 }
937
938 /* simply clear the matrix */
939 unit_m4(data->invmat);
940
941 constraint_update(bmain, ob);
943
944 return OPERATOR_FINISHED;
945}
946
948 wmOperator *op,
949 const wmEvent * /*event*/)
950{
951 if (edit_constraint_invoke_properties(C, op, nullptr, nullptr)) {
952 return childof_clear_inverse_exec(C, op);
953 }
954 return OPERATOR_CANCELLED;
955}
956
958{
960 return false;
961 }
962
963 PointerRNA ptr = CTX_data_pointer_get_type(C, "constraint", RNA_Constraint);
964 bConstraint *con = static_cast<bConstraint *>(ptr.data);
965
966 /* Allow workflows with unset context's constraint.
967 * The constraint can also be provided as an operator's property. */
968 if (con == nullptr) {
969 return true;
970 }
971
972 if (con->type != CONSTRAINT_TYPE_CHILDOF) {
973 return false;
974 }
975
976 bChildOfConstraint *data = static_cast<bChildOfConstraint *>(con->data);
977
978 if (is_identity_m4(data->invmat)) {
979 CTX_wm_operator_poll_msg_set(C, "No inverse correction is set, so there is nothing to clear");
980 return false;
981 }
982 return true;
983}
984
986{
987 /* identifiers */
988 ot->name = "Clear Inverse";
989 ot->idname = "CONSTRAINT_OT_childof_clear_inverse";
990 ot->description = "Clear inverse correction for Child Of constraint";
991
992 /* callbacks */
996
997 /* flags */
999
1000 /* properties */
1002}
1003
1005
1006/* ------------------------------------------------------------------- */
1009
1011{
1012 Main *bmain = CTX_data_main(C);
1015 bFollowPathConstraint *data = (con) ? static_cast<bFollowPathConstraint *>(con->data) : nullptr;
1016
1017 bAction *act = nullptr;
1018 FCurve *fcu = nullptr;
1019 int sfra = RNA_int_get(op->ptr, "frame_start");
1020 int len = RNA_int_get(op->ptr, "length");
1021 float standardRange = 1.0;
1022
1023 /* nearly impossible sanity check */
1024 if (data == nullptr) {
1025 BKE_report(op->reports, RPT_ERROR, "Follow Path constraint not found");
1026 return OPERATOR_CANCELLED;
1027 }
1028
1029 /* add F-Curve as appropriate */
1030 if (data->tar) {
1031 Curve *cu = id_cast<Curve *>(data->tar->data);
1032
1033 if (ELEM(nullptr, cu->adt, cu->adt->action) ||
1034 (animrig::fcurve_find_in_assigned_slot(*cu->adt, {"eval_time", 0}) == nullptr))
1035 {
1036 /* create F-Curve for path animation */
1037 act = animrig::id_action_ensure(bmain, &cu->id);
1038 PointerRNA id_ptr = RNA_id_pointer_create(&cu->id);
1039 fcu = animrig::action_fcurve_ensure_ex(bmain, act, &id_ptr, {"eval_time", 0});
1040
1041 /* standard vertical range - 1:1 = 100 frames */
1042 standardRange = 100.0f;
1043 }
1044 else {
1045 /* path anim exists already - abort for now as this may well be what was intended */
1046 BKE_report(op->reports, RPT_WARNING, "Path is already animated");
1047 return OPERATOR_CANCELLED;
1048 }
1049 }
1050 else {
1051 /* animate constraint's "fixed offset" */
1052 PropertyRNA *prop;
1053
1054 /* get RNA pointer to constraint's "offset_factor" property - to build RNA path */
1055 PointerRNA ptr = RNA_pointer_create_discrete(&ob->id, RNA_FollowPathConstraint, con);
1056 prop = RNA_struct_find_property(&ptr, "offset_factor");
1057
1058 const std::optional<std::string> path = RNA_path_from_ID_to_property(&ptr, prop);
1059 BLI_assert(path.has_value());
1060
1061 /* create F-Curve for constraint */
1062 act = animrig::id_action_ensure(bmain, &ob->id);
1063 PointerRNA id_ptr = RNA_id_pointer_create(&ob->id);
1064 fcu = animrig::action_fcurve_ensure_ex(bmain, act, &id_ptr, {path->c_str(), 0});
1065
1066 /* standard vertical range - 0.0 to 1.0 */
1067 standardRange = 1.0f;
1068
1069 /* enable "Use Fixed Position" so that animating this has effect */
1070 data->followflag |= FOLLOWPATH_STATIC;
1071 }
1072
1073 /* setup dummy 'generator' modifier here to get 1-1 correspondence still working
1074 * and define basic slope of this curve based on the properties
1075 */
1076 if (!fcu->bezt && !fcu->fpt && !fcu->modifiers.first_) {
1078 FMod_Generator *gen = static_cast<FMod_Generator *>(fcm->data);
1079
1080 /* Assume that we have the following equation:
1081 * y = Ax + B
1082 * 1 0 <-- coefficients array indices
1083 */
1084 float A = standardRange / float(len);
1085 float B = float(-sfra) * A;
1086
1087 gen->coefficients[1] = A;
1088 gen->coefficients[0] = B;
1089 }
1090
1091 /* updates... */
1093 return OPERATOR_FINISHED;
1094}
1095
1097 wmOperator *op,
1098 const wmEvent * /*event*/)
1099{
1100 /* hook up invoke properties for figuring out which constraint we're dealing with */
1101 if (edit_constraint_invoke_properties(C, op, nullptr, nullptr)) {
1102 return followpath_path_animate_exec(C, op);
1103 }
1104 return OPERATOR_CANCELLED;
1105}
1106
1108{
1109 /* identifiers */
1110 ot->name = "Auto Animate Path";
1111 ot->idname = "CONSTRAINT_OT_followpath_path_animate";
1112 ot->description =
1113 "Add default animation for path used by constraint if it isn't animated already";
1114
1115 /* callbacks */
1119
1120 /* flags */
1122
1123 /* props */
1125 RNA_def_int(ot->srna,
1126 "frame_start",
1127 1,
1128 MINAFRAME,
1129 MAXFRAME,
1130 "Start Frame",
1131 "First frame of path animation",
1132 MINAFRAME,
1133 MAXFRAME);
1134 RNA_def_int(ot->srna,
1135 "length",
1136 100,
1137 0,
1138 MAXFRAME,
1139 "Length",
1140 "Number of frames that path animation should take",
1141 0,
1142 MAXFRAME);
1143}
1144
1146
1147/* ------------------------------------------------------------------- */
1150
1152{
1153 Main *bmain = CTX_data_main(C);
1156 bObjectSolverConstraint *data = (con) ? static_cast<bObjectSolverConstraint *>(con->data) :
1157 nullptr;
1158
1159 /* despite 3 layers of checks, we may still not be able to find a constraint */
1160 if (data == nullptr) {
1161 printf("DEBUG: ObjectSolver Set Inverse - object = '%s'\n", (ob) ? ob->id.name + 2 : "<None>");
1162 BKE_report(
1163 op->reports, RPT_ERROR, "Could not find constraint data for ObjectSolver Set Inverse");
1164 return OPERATOR_CANCELLED;
1165 }
1166
1167 /* Set a flag to request recalculation on next update. */
1169
1170 /* Force constraint to run, it will perform the recalculation. */
1172
1173 constraint_update(bmain, ob);
1175
1176 return OPERATOR_FINISHED;
1177}
1178
1180 wmOperator *op,
1181 const wmEvent * /*event*/)
1182{
1183 if (edit_constraint_invoke_properties(C, op, nullptr, nullptr)) {
1184 return objectsolver_set_inverse_exec(C, op);
1185 }
1186 return OPERATOR_CANCELLED;
1187}
1188
1190{
1191 /* identifiers */
1192 ot->name = "Set Inverse";
1193 ot->idname = "CONSTRAINT_OT_objectsolver_set_inverse";
1194 ot->description = "Set inverse correction for Object Solver constraint";
1195
1196 /* callbacks */
1200
1201 /* flags */
1203
1204 /* properties */
1206}
1207
1209
1210/* ------------------------------------------------------------------- */
1213
1215{
1216 Main *bmain = CTX_data_main(C);
1219 bObjectSolverConstraint *data = (con) ? static_cast<bObjectSolverConstraint *>(con->data) :
1220 nullptr;
1221
1222 if (data == nullptr) {
1223 BKE_report(op->reports, RPT_ERROR, "Child Of constraint not found");
1224 return OPERATOR_CANCELLED;
1225 }
1226
1227 /* simply clear the matrix */
1228 unit_m4(data->invmat);
1229
1230 constraint_update(bmain, ob);
1232
1233 return OPERATOR_FINISHED;
1234}
1235
1237 wmOperator *op,
1238 const wmEvent * /*event*/)
1239{
1240 if (edit_constraint_invoke_properties(C, op, nullptr, nullptr)) {
1242 }
1243 return OPERATOR_CANCELLED;
1244}
1245
1247{
1248 if (!edit_constraint_poll(C)) {
1249 return false;
1250 }
1251
1252 PointerRNA ptr = CTX_data_pointer_get_type(C, "constraint", RNA_Constraint);
1253 bConstraint *con = static_cast<bConstraint *>(ptr.data);
1254 if (con == nullptr) {
1255 return true;
1256 }
1257
1259
1260 if (is_identity_m4(data->invmat)) {
1261 CTX_wm_operator_poll_msg_set(C, "No inverse correction is set, so there is nothing to clear");
1262 return false;
1263 }
1264 return true;
1265}
1266
1268{
1269 /* identifiers */
1270 ot->name = "Clear Inverse";
1271 ot->idname = "CONSTRAINT_OT_objectsolver_clear_inverse";
1272 ot->description = "Clear inverse correction for Object Solver constraint";
1273
1274 /* callbacks */
1278
1279 /* flags */
1281
1282 /* properties */
1284}
1285
1287
1288/* ------------------------------------------------------------------- */
1291
1293{
1294 ListBaseT<bConstraint> *lb = constraint_list_from_constraint(ob, con, nullptr);
1295
1296 /* lets be nice and escape if its active already */
1297 /* NOTE: this assumes that the stack doesn't have other active ones set... */
1298 if ((lb && con) && (con->flag & CONSTRAINT_ACTIVE)) {
1299 return;
1300 }
1301
1303}
1304
1306{
1307 if (ob->pose) {
1309 }
1310
1311 object_test_constraints(bmain, ob);
1312
1313 if (ob->type == OB_ARMATURE) {
1315 }
1316 else {
1318 }
1319}
1320
1321static void object_pose_tag_update(Main *bmain, Object *ob)
1322{
1323 BKE_pose_tag_recalc(bmain, ob->pose); /* Checks & sort pose channels. */
1324}
1325
1327{
1328 constraint_update(bmain, ob);
1329
1330 if (ob->pose) {
1331 object_pose_tag_update(bmain, ob);
1332 }
1334}
1335
1337{
1338 if (ob->pose) {
1340 }
1341
1342 if (con) {
1343 object_test_constraint(bmain, ob, con);
1344 }
1345
1346 if (ob->type == OB_ARMATURE) {
1348 }
1349 else {
1351 }
1352
1353 /* Do Copy-on-Write tag here too, otherwise constraint
1354 * influence/mute buttons in UI have no effect
1355 */
1357}
1358
1360{
1361 constraint_tag_update(bmain, ob, con);
1362
1363 if (ob->pose) {
1364 object_pose_tag_update(bmain, ob);
1365 }
1367}
1368
1370{
1371 BLI_assert(con != nullptr);
1372 BLI_assert(index >= 0);
1373
1374 ListBaseT<bConstraint> *conlist = constraint_list_from_constraint(ob, con, nullptr);
1375 int current_index = BLI_findindex(conlist, con);
1376 BLI_assert(current_index >= 0);
1377
1378 BLI_listbase_link_move(conlist, con, index - current_index);
1379
1381
1382 return true;
1383}
1384
1386 Object *ob_dst,
1387 ListBaseT<bConstraint> *dst,
1388 ListBaseT<bConstraint> *src)
1389{
1391 BKE_constraints_copy(dst, src, true);
1392 for (bConstraint &con : *dst) {
1393 constraint_dependency_tag_update(bmain, ob_dst, &con);
1394 }
1396}
1397
1399{
1400 bConstraint *copy_con = BKE_constraint_copy_for_object(ob_dst, con);
1402
1403 constraint_dependency_tag_update(bmain, ob_dst, con);
1405}
1406
1408{
1409 bConstraint *copy_con = BKE_constraint_copy_for_pose(ob_dst, pchan, con);
1411
1412 constraint_dependency_tag_update(bmain, ob_dst, con);
1414}
1415
1417
1418/* ------------------------------------------------------------------- */
1421
1423{
1424 Main *bmain = CTX_data_main(C);
1426 bConstraint *con = edit_constraint_property_get(C, op, ob, 0);
1427
1428 if (con == nullptr) {
1429 return OPERATOR_CANCELLED;
1430 }
1431
1432 ListBaseT<bConstraint> *lb = constraint_list_from_constraint(ob, con, nullptr);
1433
1434 /* Store name temporarily for report. */
1435 char name[MAX_NAME];
1436 STRNCPY_UTF8(name, con->name);
1437
1438 /* free the constraint */
1439 if (BKE_constraint_remove_ex(lb, ob, con)) {
1440 /* Needed to set the flags on pose-bones correctly. */
1441 constraint_update(bmain, ob);
1442
1443 /* relations */
1445
1446 /* notifiers */
1448
1449 if (RNA_boolean_get(op->ptr, "report")) {
1450 BKE_reportf(op->reports, RPT_INFO, "Removed constraint: %s", name);
1451 }
1452
1453 return OPERATOR_FINISHED;
1454 }
1455 /* couldn't remove due to some invalid data */
1456 return OPERATOR_CANCELLED;
1457}
1458
1460{
1461 wmOperatorStatus retval;
1462 if (!edit_constraint_invoke_properties(C, op, event, &retval)) {
1463 return retval;
1464 }
1465 return constraint_delete_exec(C, op);
1466}
1467
1469{
1470 /* identifiers */
1471 ot->name = "Delete Constraint";
1472 ot->idname = "CONSTRAINT_OT_delete";
1473 ot->description = "Remove constraint from constraint stack";
1474
1475 /* callbacks */
1479
1480 /* flags */
1484}
1485
1487
1488/* ------------------------------------------------------------------- */
1491
1493{
1494 Scene *scene = CTX_data_scene(C);
1495 Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
1496 Main *bmain = CTX_data_main(C);
1498 bConstraint *con = edit_constraint_property_get(C, op, ob, 0);
1499
1500 if (con == nullptr) {
1501 return OPERATOR_CANCELLED;
1502 }
1503
1504 bPoseChannel *pchan;
1505 ListBaseT<bConstraint> *constraints = constraint_list_from_constraint(ob, con, &pchan);
1506
1507 /* Store name temporarily for report. */
1508 char name[MAX_NAME];
1509 STRNCPY_UTF8(name, con->name);
1510 const bool is_first_constraint = con != constraints->first_;
1511
1512 /* Copy the constraint. */
1513 bool success;
1514 if (pchan) {
1516 depsgraph, scene, constraints, ob, con, pchan);
1517 }
1518 else {
1519 success = BKE_constraint_apply_and_remove_for_object(depsgraph, scene, constraints, ob, con);
1520 }
1521
1522 if (!success) {
1523 /* Couldn't remove due to some invalid data. */
1524 return OPERATOR_CANCELLED;
1525 }
1526
1527 /* Update for any children that may get moved. */
1529
1530 /* Needed to set the flags on pose-bones correctly. */
1531 constraint_update(bmain, ob);
1532
1535 if (pchan) {
1537 }
1538 else {
1540 }
1541
1542 if (RNA_boolean_get(op->ptr, "report")) {
1543 if (is_first_constraint) {
1544 BKE_report(op->reports,
1545 RPT_INFO,
1546 "Applied constraint was not first, result may not be as expected");
1547 }
1548 else {
1549 /* Only add this report if the operator didn't cause another one. The purpose here is
1550 * to alert that something happened, and the previous report will do that anyway. */
1551 BKE_reportf(op->reports, RPT_INFO, "Applied constraint: %s", name);
1552 }
1553 }
1554
1555 return OPERATOR_FINISHED;
1556}
1557
1559{
1560 wmOperatorStatus retval;
1561 if (!edit_constraint_invoke_properties(C, op, event, &retval)) {
1562 return retval;
1563 }
1564 return constraint_apply_exec(C, op);
1565}
1566
1568{
1569 /* identifiers */
1570 ot->name = "Apply Constraint";
1571 ot->idname = "CONSTRAINT_OT_apply";
1572 ot->description = "Apply constraint and remove from the stack";
1573
1574 /* callbacks */
1578
1579 /* flags */
1583}
1584
1586
1587/* ------------------------------------------------------------------- */
1590
1592{
1593 Main *bmain = CTX_data_main(C);
1595 bConstraint *con = edit_constraint_property_get(C, op, ob, 0);
1596
1597 if (con == nullptr) {
1598 return OPERATOR_CANCELLED;
1599 }
1600
1601 bPoseChannel *pchan;
1602 ListBaseT<bConstraint> *constraints = constraint_list_from_constraint(ob, con, &pchan);
1603
1604 /* Store name temporarily for report. */
1605 char name[MAX_NAME];
1606 STRNCPY_UTF8(name, con->name);
1607
1608 /* Copy the constraint. */
1609 bConstraint *copy_con;
1610 if (pchan) {
1611 copy_con = BKE_constraint_copy_for_pose(ob, pchan, con);
1612 }
1613 else {
1614 copy_con = BKE_constraint_copy_for_object(ob, con);
1615 }
1616
1617 if (!copy_con) {
1618 /* Couldn't remove due to some invalid data. */
1619 return OPERATOR_CANCELLED;
1620 }
1622
1623 /* Move constraint to correct position. */
1624 const int new_index = BLI_findindex(constraints, con) + 1;
1625 const int current_index = BLI_findindex(constraints, copy_con);
1626 BLI_assert(new_index >= 0);
1627 BLI_assert(current_index >= 0);
1628 BLI_listbase_link_move(constraints, copy_con, new_index - current_index);
1629
1630 /* Needed to set the flags on pose-bones correctly. */
1631 constraint_update(bmain, ob);
1632
1635
1636 if (RNA_boolean_get(op->ptr, "report")) {
1637 BKE_reportf(op->reports, RPT_INFO, "Copied constraint: %s", name);
1638 }
1639
1640 return OPERATOR_FINISHED;
1641}
1642
1644{
1645 wmOperatorStatus retval;
1646 if (!edit_constraint_invoke_properties(C, op, event, &retval)) {
1647 return retval;
1648 }
1649 return constraint_copy_exec(C, op);
1650}
1651
1653{
1654 /* identifiers */
1655 ot->name = "Duplicate Constraint";
1656 ot->idname = "CONSTRAINT_OT_copy";
1657 ot->description = "Duplicate constraint at the same position in the stack";
1658
1659 /* callbacks */
1663
1664 /* flags */
1668}
1669
1671
1672/* ------------------------------------------------------------------- */
1675
1677{
1678 Main *bmain = CTX_data_main(C);
1679 Object *obact = context_active_object(C);
1680 bConstraint *con = edit_constraint_property_get(C, op, obact, 0);
1681
1682 if (con == nullptr) {
1683 return OPERATOR_CANCELLED;
1684 }
1685
1686 bPoseChannel *pchan;
1687 constraint_list_from_constraint(obact, con, &pchan);
1688
1689 if (pchan) {
1690 /* Don't do anything if bone doesn't exist or doesn't have any constraints. */
1691 if (pchan->constraints.first_ == nullptr) {
1692 BKE_report(op->reports, RPT_ERROR, "No constraints for copying");
1693 return OPERATOR_CANCELLED;
1694 }
1695
1696 Object *prev_ob = nullptr;
1697
1698 /* Copy all constraints from active pose-bone to all selected pose-bones. */
1699 CTX_DATA_BEGIN_WITH_ID (C, bPoseChannel *, chan, selected_pose_bones, Object *, ob) {
1700 /* If we're not handling the object we're copying from, copy all constraints over. */
1701 if (pchan == chan) {
1702 continue;
1703 }
1704
1705 bConstraint *copy_con = BKE_constraint_copy_for_pose(ob, chan, con);
1707
1708 /* Update flags (need to add here, not just copy). */
1709 chan->constflag |= pchan->constflag;
1710
1711 if (prev_ob == ob) {
1712 continue;
1713 }
1714
1715 BKE_pose_tag_recalc(bmain, ob->pose);
1716 DEG_id_tag_update(id_cast<ID *>(ob), ID_RECALC_GEOMETRY);
1717 prev_ob = ob;
1718 }
1720 }
1721 else {
1722 /* Copy all constraints from active object to all selected objects. */
1723 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects) {
1724 /* If we're not handling the object we're copying from, copy all constraints over. */
1725 if (obact == ob) {
1726 continue;
1727 }
1728
1729 bConstraint *copy_con = BKE_constraint_copy_for_object(ob, con);
1731
1733 }
1735 }
1736
1737 /* Force depsgraph to get recalculated since new relationships added. */
1739
1741
1742 return OPERATOR_FINISHED;
1743}
1744
1746 wmOperator *op,
1747 const wmEvent *event)
1748{
1749 wmOperatorStatus retval;
1750 if (!edit_constraint_invoke_properties(C, op, event, &retval)) {
1751 return retval;
1752 }
1754}
1755
1757{
1758 PointerRNA ptr = CTX_data_pointer_get_type(C, "constraint", RNA_Constraint);
1759 Object *obact = (ptr.owner_id) ? id_cast<Object *>(ptr.owner_id) : context_active_object(C);
1760 bConstraint *con = static_cast<bConstraint *>(ptr.data);
1761 bPoseChannel *pchan;
1762 constraint_list_from_constraint(obact, con, &pchan);
1763
1764 if (pchan) {
1765 bool found = false;
1766 CTX_DATA_BEGIN_WITH_ID (C, bPoseChannel *, chan, selected_pose_bones, Object *, ob) {
1767 UNUSED_VARS(ob);
1768 if (pchan != chan) {
1771 found = true;
1772 break;
1773 }
1774 }
1776 if (found) {
1777 return true;
1778 }
1779
1780 CTX_wm_operator_poll_msg_set(C, "No other bones are selected");
1781 return false;
1782 }
1783
1784 if (!obact) {
1785 CTX_wm_operator_poll_msg_set(C, "No selected object to copy from");
1786 return false;
1787 }
1788
1789 bool found = false;
1790 CTX_DATA_BEGIN (C, Object *, ob, selected_objects) {
1791 if (ob != obact) {
1794 found = true;
1795 break;
1796 }
1797 }
1799 if (found) {
1800 return true;
1801 }
1802
1803 CTX_wm_operator_poll_msg_set(C, "No other objects are selected");
1804 return false;
1805}
1806
1808{
1809 /* identifiers */
1810 ot->name = "Copy Constraint To Selected";
1811 ot->idname = "CONSTRAINT_OT_copy_to_selected";
1812 ot->description = "Copy constraint to other selected objects/bones";
1813
1814 /* API callbacks. */
1818
1819 /* flags */
1822}
1823
1825
1826/* ------------------------------------------------------------------- */
1829
1831{
1833 bConstraint *con = edit_constraint_property_get(C, op, ob, 0);
1834
1835 if (con && con->next) {
1836 ListBaseT<bConstraint> *conlist = constraint_list_from_constraint(ob, con, nullptr);
1837 bConstraint *nextCon = con->next;
1838
1839 /* insert the nominated constraint after the one that used to be after it */
1840 BLI_remlink(conlist, con);
1841 BLI_insertlinkafter(conlist, nextCon, con);
1842
1845
1846 return OPERATOR_FINISHED;
1847 }
1848
1849 return OPERATOR_CANCELLED;
1850}
1851
1853 wmOperator *op,
1854 const wmEvent *event)
1855{
1856 wmOperatorStatus retval;
1857 if (edit_constraint_invoke_properties(C, op, event, &retval)) {
1858 return constraint_move_down_exec(C, op);
1859 }
1860 return retval;
1861}
1862
1864{
1865 /* identifiers */
1866 ot->name = "Move Constraint Down";
1867 ot->idname = "CONSTRAINT_OT_move_down";
1868 ot->description = "Move constraint down in constraint stack";
1869
1870 /* callbacks */
1874
1875 /* flags */
1877
1878 /* properties */
1880}
1881
1883
1884/* ------------------------------------------------------------------- */
1887
1889{
1891 bConstraint *con = edit_constraint_property_get(C, op, ob, 0);
1892
1893 if (con && con->prev) {
1894 ListBaseT<bConstraint> *conlist = constraint_list_from_constraint(ob, con, nullptr);
1895 bConstraint *prevCon = con->prev;
1896
1897 /* insert the nominated constraint before the one that used to be before it */
1898 BLI_remlink(conlist, con);
1899 BLI_insertlinkbefore(conlist, prevCon, con);
1900
1903
1904 return OPERATOR_FINISHED;
1905 }
1906
1907 return OPERATOR_CANCELLED;
1908}
1909
1911 wmOperator *op,
1912 const wmEvent *event)
1913{
1914 wmOperatorStatus retval;
1915 if (edit_constraint_invoke_properties(C, op, event, &retval)) {
1916 return constraint_move_up_exec(C, op);
1917 }
1918 return retval;
1919}
1920
1922{
1923 /* identifiers */
1924 ot->name = "Move Constraint Up";
1925 ot->idname = "CONSTRAINT_OT_move_up";
1926 ot->description = "Move constraint up in constraint stack";
1927
1928 /* callbacks */
1932
1933 /* flags */
1936}
1937
1939
1940/* ------------------------------------------------------------------- */
1943
1945{
1947 bConstraint *con = edit_constraint_property_get(C, op, ob, 0);
1948
1949 int new_index = RNA_int_get(op->ptr, "index");
1950 new_index = std::max(new_index, 0);
1951
1952 if (con) {
1953 constraint_move_to_index(ob, con, new_index);
1954
1956
1957 return OPERATOR_FINISHED;
1958 }
1959
1960 return OPERATOR_CANCELLED;
1961}
1962
1964 wmOperator *op,
1965 const wmEvent *event)
1966{
1967 wmOperatorStatus retval;
1968 if (edit_constraint_invoke_properties(C, op, event, &retval)) {
1969 return constraint_move_to_index_exec(C, op);
1970 }
1971 return retval;
1972}
1973
1975{
1976 /* identifiers */
1977 ot->name = "Move Constraint to Index";
1978 ot->idname = "CONSTRAINT_OT_move_to_index";
1979 ot->description =
1980 "Change the constraint's position in the list so it evaluates after the set number of "
1981 "others";
1982
1983 /* callbacks */
1987
1988 /* flags */
1991 RNA_def_int(ot->srna,
1992 "index",
1993 0,
1994 0,
1995 INT_MAX,
1996 "Index",
1997 "The index to move the constraint to",
1998 0,
1999 INT_MAX);
2000}
2001
2003
2004/* ------------------------------------------------------------------- */
2007
2009{
2010 Main *bmain = CTX_data_main(C);
2011 Object *prev_ob = nullptr;
2012
2013 /* free constraints for all selected bones */
2014 CTX_DATA_BEGIN_WITH_ID (C, bPoseChannel *, pchan, selected_pose_bones, Object *, ob) {
2015 BKE_constraints_free(&pchan->constraints);
2016 pchan->constflag = ePchan_ConstFlag{};
2017
2018 if (prev_ob != ob) {
2021 prev_ob = ob;
2022 }
2023 }
2025
2026 /* force depsgraph to get recalculated since relationships removed */
2028
2029 /* NOTE: calling BIK_clear_data() isn't needed here. */
2030
2031 return OPERATOR_FINISHED;
2032}
2033
2035{
2036 /* identifiers */
2037 ot->name = "Clear Pose Constraints";
2038 ot->idname = "POSE_OT_constraints_clear";
2039 ot->description = "Clear all constraints from the selected bones";
2040
2041 /* callbacks */
2043 /* XXX: do we want to ensure there are selected bones too? */
2045}
2046
2048{
2049 Main *bmain = CTX_data_main(C);
2050
2051 /* do freeing */
2052 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects) {
2053 BKE_constraints_free(&ob->constraints);
2055 }
2057
2058 /* force depsgraph to get recalculated since relationships removed */
2060
2061 /* do updates */
2063
2064 return OPERATOR_FINISHED;
2065}
2066
2068
2069/* ------------------------------------------------------------------- */
2072
2074{
2075 /* identifiers */
2076 ot->name = "Clear Object Constraints";
2077 ot->idname = "OBJECT_OT_constraints_clear";
2078 ot->description = "Clear all constraints from the selected objects";
2079
2080 /* callbacks */
2083}
2084
2086
2087/* ------------------------------------------------------------------- */
2090
2092{
2093 Main *bmain = CTX_data_main(C);
2095
2096 /* don't do anything if bone doesn't exist or doesn't have any constraints */
2097 if (ELEM(nullptr, pchan, pchan->constraints.first_)) {
2098 BKE_report(op->reports, RPT_ERROR, "No active bone with constraints for copying");
2099 return OPERATOR_CANCELLED;
2100 }
2101
2102 Object *prev_ob = nullptr;
2103
2104 /* Copy all constraints from active pose-bone to all selected pose-bones. */
2105 CTX_DATA_BEGIN_WITH_ID (C, bPoseChannel *, chan, selected_pose_bones, Object *, ob) {
2106 /* if we're not handling the object we're copying from, copy all constraints over */
2107 if (pchan != chan) {
2108 BKE_constraints_copy(&chan->constraints, &pchan->constraints, true);
2109 /* update flags (need to add here, not just copy) */
2110 chan->constflag |= pchan->constflag;
2111
2112 if (prev_ob != ob) {
2113 BKE_pose_tag_recalc(bmain, ob->pose);
2114 DEG_id_tag_update(id_cast<ID *>(ob), ID_RECALC_GEOMETRY);
2115 prev_ob = ob;
2116 }
2117 }
2118 }
2120
2121 /* force depsgraph to get recalculated since new relationships added */
2123
2125
2126 return OPERATOR_FINISHED;
2127}
2128
2130{
2131 /* identifiers */
2132 ot->name = "Copy Constraints to Selected Bones";
2133 ot->idname = "POSE_OT_constraints_copy";
2134 ot->description = "Copy constraints to other selected bones";
2135
2136 /* API callbacks. */
2139
2140 /* flags */
2142}
2143
2145
2146/* ------------------------------------------------------------------- */
2149
2151{
2152 Main *bmain = CTX_data_main(C);
2153 Object *obact = context_active_object(C);
2154
2155 /* copy all constraints from active object to all selected objects */
2156 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects) {
2157 /* if we're not handling the object we're copying from, copy all constraints over */
2158 if (obact != ob) {
2159 BKE_constraints_free(&ob->constraints);
2160 BKE_constraints_copy(&ob->constraints, &obact->constraints, true);
2162 }
2163 }
2165
2166 /* force depsgraph to get recalculated since new relationships added */
2168
2169 /* notifiers for updates */
2171
2172 return OPERATOR_FINISHED;
2173}
2174
2176{
2177 /* identifiers */
2178 ot->name = "Copy Constraints to Selected Objects";
2179 ot->idname = "OBJECT_OT_constraints_copy";
2180 ot->description = "Copy constraints to other selected objects";
2181
2182 /* API callbacks. */
2185
2186 /* flags */
2188}
2189
2191
2192/* ------------------------------------------------------------------- */
2195
2196/* get the Object and/or PoseChannel to use as target */
2198 bContext *C, int con_type, Object **tar_ob, bPoseChannel **tar_pchan, bool add)
2199{
2200 Object *obact = context_active_object(C);
2202 bool only_curve = false, only_mesh = false, only_ob = false;
2203 bool found = false;
2204
2205 /* clear tar_ob and tar_pchan fields before use
2206 * - assume for now that both always exist...
2207 */
2208 *tar_ob = nullptr;
2209 *tar_pchan = nullptr;
2210
2211 /* check if constraint type doesn't requires a target
2212 * - if so, no need to get any targets
2213 */
2214 switch (con_type) {
2215 /* no-target constraints --------------------------- */
2216 /* null constraint - shouldn't even be added! */
2218 /* limit constraints - no targets needed */
2223 return false;
2224
2225 /* restricted target-type constraints -------------- */
2226 /* NOTE: for these, we cannot try to add a target object if no valid ones are found,
2227 * since that doesn't work */
2228 /* curve-based constraints - set the only_curve and only_ob flags */
2232 only_curve = true;
2233 only_ob = true;
2234 add = false;
2235 break;
2236
2237 /* mesh only? */
2239 only_mesh = true;
2240 only_ob = true;
2241 add = false;
2242 break;
2243
2244 /* Armature only. */
2246 add = false;
2247 break;
2248 }
2249
2250 /* if the active Object is Armature, and we can search for bones, do so... */
2251 if ((obact->type == OB_ARMATURE) && (only_ob == false)) {
2252 /* search in list of selected Pose-Channels for target */
2253 CTX_DATA_BEGIN (C, bPoseChannel *, pchan, selected_pose_bones_from_active_object) {
2254 /* just use the first one that we encounter, as long as it is not the active one */
2255 if (pchan != pchanact) {
2256 *tar_ob = obact;
2257 *tar_pchan = pchan;
2258 found = true;
2259
2260 break;
2261 }
2262 }
2264 }
2265
2266 /* if not yet found, try selected Objects... */
2267 if (found == false) {
2268 /* search in selected objects context */
2269 CTX_DATA_BEGIN (C, Object *, ob, selected_objects) {
2270 /* just use the first object we encounter (that isn't the active object)
2271 * and which fulfills the criteria for the object-target that we've got
2272 */
2273 if (ob != obact) {
2274 /* for armatures in pose mode, look inside the armature for the active bone
2275 * so that we set up cross-armature constraints with less effort
2276 */
2277 if ((ob->type == OB_ARMATURE) && (ob->mode & OB_MODE_POSE) && (!only_curve && !only_mesh))
2278 {
2279
2280 /* Only use the object & bone if the bone is visible & selected
2281 * since we may have multiple objects in pose mode at once. */
2283 if (pchan != nullptr) {
2284 *tar_pchan = pchan;
2285 *tar_ob = ob;
2286 found = true;
2287 }
2288
2289 break;
2290 }
2291 if (((!only_curve) || (ob->type == OB_CURVES_LEGACY)) &&
2292 ((!only_mesh) || (ob->type == OB_MESH)))
2293 {
2294 /* set target */
2295 *tar_ob = ob;
2296 found = true;
2297
2298 /* perform some special operations on the target */
2299 if (only_curve) {
2300 /* Curve-Path option must be enabled for follow-path constraints to be able to work
2301 */
2302 Curve *cu = id_cast<Curve *>(ob->data);
2303 cu->flag |= CU_PATH;
2304 }
2305
2306 break;
2307 }
2308 }
2309 }
2311 }
2312
2313 /* if still not found, add a new empty to act as a target (if allowed) */
2314 if ((found == false) && (add)) {
2315 Main *bmain = CTX_data_main(C);
2316 Scene *scene = CTX_data_scene(C);
2317 ViewLayer *view_layer = CTX_data_view_layer(C);
2318 BKE_view_layer_synced_ensure(*bmain, scene, view_layer);
2319 Base *base = BKE_view_layer_active_base_get(view_layer);
2320 Object *obt;
2321
2322 /* add new target object */
2323 obt = BKE_object_add(bmain, scene, view_layer, OB_EMPTY, nullptr);
2324
2325 /* transform cent to global coords for loc */
2326 if (pchanact) {
2327 /* Since by default, IK targets the tip of the last bone,
2328 * use the tip of the active PoseChannel if adding a target for an IK Constraint. */
2329 if (con_type == CONSTRAINT_TYPE_KINEMATIC) {
2330 mul_v3_m4v3(obt->loc, obact->object_to_world().ptr(), pchanact->pose_tail);
2331 }
2332 else {
2333 mul_v3_m4v3(obt->loc, obact->object_to_world().ptr(), pchanact->pose_head);
2334 }
2335 }
2336 else {
2337 copy_v3_v3(obt->loc, obact->object_to_world().location());
2338 }
2339
2340 /* restore, BKE_object_add sets active */
2341 view_layer->basact = base;
2342 base_select(base, BA_SELECT);
2343
2344 /* make our new target the new object */
2345 *tar_ob = obt;
2346 found = true;
2347 }
2348
2349 /* return whether there's any target */
2350 return found;
2351}
2352
2353/* used by add constraint operators to add the constraint required */
2355 wmOperator *op,
2356 Object *ob,
2357 ListBaseT<bConstraint> *list,
2358 eBConstraint_Types type,
2359 const bool setTarget)
2360{
2361 Main *bmain = CTX_data_main(C);
2362 bPoseChannel *pchan;
2363 bConstraint *con;
2364
2365 if (list == &ob->constraints) {
2366 pchan = nullptr;
2367 }
2368 else {
2370
2371 /* ensure not to confuse object/pose adding */
2372 if (pchan == nullptr) {
2373 BKE_report(op->reports, RPT_ERROR, "No active pose bone to add a constraint to");
2374 return OPERATOR_CANCELLED;
2375 }
2376 }
2377 /* check if constraint to be added is valid for the given constraints stack */
2378 if (type == CONSTRAINT_TYPE_NULL) {
2379 return OPERATOR_CANCELLED;
2380 }
2381
2382 /* Create a new constraint of the type required,
2383 * and add it to the active/given constraints list. */
2384 if (pchan) {
2385 con = BKE_constraint_add_for_pose(ob, pchan, nullptr, type);
2386 }
2387 else {
2388 con = BKE_constraint_add_for_object(ob, nullptr, type);
2389 }
2390
2391 /* get the first selected object/bone, and make that the target
2392 * - apart from the buttons-window add buttons, we shouldn't add in this way
2393 */
2394 if (setTarget) {
2395 Object *tar_ob = nullptr;
2396 bPoseChannel *tar_pchan = nullptr;
2397
2398 /* get the target objects, adding them as need be */
2399 if (get_new_constraint_target(C, type, &tar_ob, &tar_pchan, true)) {
2400
2401 /* Armature constraints don't have a target by default, add one. */
2402 if (type == CONSTRAINT_TYPE_ARMATURE) {
2403 bArmatureConstraint *acon = static_cast<bArmatureConstraint *>(con->data);
2404 bConstraintTarget *ct = MEM_new<bConstraintTarget>("Constraint Target");
2405
2406 ct->weight = 1.0f;
2407 BLI_addtail(&acon->targets, ct);
2408
2409 constraint_dependency_tag_update(bmain, ob, con);
2410 }
2411
2412 /* Method of setting target depends on the type of target we've got - by default,
2413 * just set the first target (distinction here is only for multiple-targeted constraints).
2414 */
2415 if (tar_pchan) {
2416 set_constraint_nth_target(con, tar_ob, tar_pchan->name, 0);
2417 }
2418 else {
2419 set_constraint_nth_target(con, tar_ob, "", 0);
2420 }
2421 }
2422 }
2423
2424 /* make sure all settings are valid - similar to above checks, but sometimes can be wrong */
2425 object_test_constraints(bmain, ob);
2426
2427 if (pchan) {
2429 }
2430
2431 /* force depsgraph to get recalculated since new relationships added */
2433
2434 if ((ob->type == OB_ARMATURE) && (pchan)) {
2435 BKE_pose_tag_recalc(bmain, ob->pose); /* sort pose channels */
2437 }
2438 else {
2440 }
2441
2442 /* notifiers for updates */
2444
2445 return OPERATOR_FINISHED;
2446}
2447
2448/* ------------------ */
2449
2450/* dummy operator callback */
2452{
2455 short with_targets = 0;
2456
2457 if (!ob) {
2458 BKE_report(op->reports, RPT_ERROR, "No active object to add constraint to");
2459 return OPERATOR_CANCELLED;
2460 }
2461
2462 /* hack: set constraint targets from selected objects in context is allowed when
2463 * operator name included 'with_targets', since the menu doesn't allow multiple properties
2464 */
2465 if (strstr(op->idname, "with_targets")) {
2466 with_targets = 1;
2467 }
2468
2469 return constraint_add_exec(C, op, ob, &ob->constraints, type, with_targets);
2470}
2471
2472/* dummy operator callback */
2474{
2477 short with_targets = 0;
2478
2479 if (!ob) {
2480 BKE_report(op->reports, RPT_ERROR, "No active object to add constraint to");
2481 return OPERATOR_CANCELLED;
2482 }
2483
2484 /* hack: set constraint targets from selected objects in context is allowed when
2485 * operator name included 'with_targets', since the menu doesn't allow multiple properties
2486 */
2487 if (strstr(op->idname, "with_targets")) {
2488 with_targets = 1;
2489 }
2490
2491 return constraint_add_exec(C, op, ob, constraint_active_list(ob), type, with_targets);
2492}
2493
2494/* ------------------ */
2495
2496/* Filters constraints that are only compatible with bones */
2498 PointerRNA * /*ptr*/,
2499 PropertyRNA * /*prop*/,
2500 bool *r_free)
2501{
2503 EnumPropertyItem *object_constraint_items = nullptr;
2504 int totitem = 0;
2505
2506 while (item->identifier) {
2508 RNA_enum_item_add(&object_constraint_items, &totitem, item);
2509 }
2510 item++;
2511 }
2512
2513 RNA_enum_item_end(&object_constraint_items, &totitem);
2514 *r_free = true;
2515
2516 return object_constraint_items;
2517}
2518
2520{
2521 PropertyRNA *prop;
2522
2523 /* identifiers */
2524 ot->name = "Add Constraint";
2525 ot->description = "Add a constraint to the active object";
2526 ot->idname = "OBJECT_OT_constraint_add";
2527
2528 /* API callbacks. */
2529 ot->invoke = WM_menu_invoke;
2532
2533 /* flags */
2535
2536 /* properties */
2537 prop = RNA_def_enum(ot->srna, "type", rna_enum_dummy_NULL_items, 0, "Type", "");
2539 ot->prop = prop;
2540}
2541
2543
2544/* ------------------------------------------------------------------- */
2547
2549{
2550 PropertyRNA *prop;
2551
2552 /* identifiers */
2553 ot->name = "Add Constraint (with Targets)";
2554 ot->description =
2555 "Add a constraint to the active object, with target (where applicable) set to the "
2556 "selected objects/bones";
2557 ot->idname = "OBJECT_OT_constraint_add_with_targets";
2558
2559 /* API callbacks. */
2560 ot->invoke = WM_menu_invoke;
2563
2564 /* flags */
2566
2567 /* properties */
2568 prop = RNA_def_enum(ot->srna, "type", rna_enum_dummy_NULL_items, 0, "Type", "");
2570 ot->prop = prop;
2571}
2572
2574{
2575 /* identifiers */
2576 ot->name = "Add Constraint";
2577 ot->description = "Add a constraint to the active bone";
2578 ot->idname = "POSE_OT_constraint_add";
2579
2580 /* API callbacks. */
2581 ot->invoke = WM_menu_invoke;
2584
2585 /* flags */
2587
2588 /* properties */
2589 ot->prop = RNA_def_enum(ot->srna, "type", rna_enum_constraint_type_items, 1, "Type", "");
2590}
2591
2593{
2594 /* identifiers */
2595 ot->name = "Add Constraint (with Targets)";
2596 ot->description =
2597 "Add a constraint to the active bone, with target (where applicable) set to the selected "
2598 "Objects/Bones";
2599 ot->idname = "POSE_OT_constraint_add_with_targets";
2600
2601 /* API callbacks. */
2602 ot->invoke = WM_menu_invoke;
2605
2606 /* flags */
2608
2609 /* properties */
2610 ot->prop = RNA_def_enum(ot->srna, "type", rna_enum_constraint_type_items, 1, "Type", "");
2611}
2612
2614
2615/* ------------------------------------------------------------------- */
2620
2621/* TODO: should these be here, or back in `editors/armature/poseobject.c` again? */
2622
2623/* present menu with options + validation for targets to use */
2625{
2628 bConstraint *con = nullptr;
2629
2630 Object *tar_ob = nullptr;
2631 bPoseChannel *tar_pchan = nullptr;
2632
2633 /* must have active bone */
2634 if (ELEM(nullptr, ob, pchan)) {
2635 BKE_report(op->reports, RPT_ERROR, "Must have an active bone to add IK constraint to");
2636 return OPERATOR_CANCELLED;
2637 }
2638
2639 /* bone must not have any constraints already */
2640 for (con = pchan->constraints.first(); con; con = con->next) {
2641 if (con->type == CONSTRAINT_TYPE_KINEMATIC) {
2642 break;
2643 }
2644 }
2645 if (con) {
2646 BKE_report(op->reports, RPT_ERROR, "Bone already has an IK constraint");
2647 return OPERATOR_CANCELLED;
2648 }
2649
2650 /* prepare popup menu to choose targeting options */
2651 ui::PopupMenu *pup = ui::popup_menu_begin(C, IFACE_("Add IK"), ICON_NONE);
2652 ui::Layout &layout = *popup_menu_layout(pup);
2653
2654 /* the type of targets we'll set determines the menu entries to show... */
2655 if (get_new_constraint_target(C, CONSTRAINT_TYPE_KINEMATIC, &tar_ob, &tar_pchan, false)) {
2656 /* bone target, or object target?
2657 * - the only thing that matters is that we want a target...
2658 */
2659 if (tar_pchan) {
2660 PointerRNA op_ptr = layout.op("POSE_OT_ik_add", IFACE_("Target Selected Bone"), ICON_NONE);
2661 RNA_boolean_set(&op_ptr, "with_targets", true);
2662 }
2663 else {
2664 PointerRNA op_ptr = layout.op("POSE_OT_ik_add", IFACE_("Target Selected Object"), ICON_NONE);
2665 RNA_boolean_set(&op_ptr, "with_targets", true);
2666 }
2667 }
2668 else {
2669 /* we have a choice of adding to a new empty, or not setting any target (targetless IK) */
2670 PointerRNA op_ptr = layout.op("POSE_OT_ik_add", IFACE_("Target New Empty Object"), ICON_NONE);
2671 RNA_boolean_set(&op_ptr, "with_targets", true);
2672 op_ptr = layout.op("POSE_OT_ik_add", IFACE_("Without Target"), ICON_NONE);
2673 RNA_boolean_set(&op_ptr, "with_targets", false);
2674 }
2675
2676 /* finish building the menu, and process it (should result in calling self again) */
2677 popup_menu_end(C, pup);
2678
2679 return OPERATOR_INTERFACE;
2680}
2681
2682/* call constraint_add_exec() to add the IK constraint */
2684{
2686 const bool with_targets = RNA_boolean_get(op->ptr, "with_targets");
2687
2688 /* add the constraint - all necessary checks should have
2689 * been done by the invoke() callback already... */
2690 return constraint_add_exec(
2691 C, op, ob, constraint_active_list(ob), CONSTRAINT_TYPE_KINEMATIC, with_targets);
2692}
2693
2695{
2696 /* identifiers */
2697 ot->name = "Add IK to Bone";
2698 ot->description =
2699 "Add an IK Constraint to the active Bone. The target can be a selected bone or object";
2700 ot->idname = "POSE_OT_ik_add";
2701
2702 /* API callbacks. */
2704 ot->exec = pose_ik_add_exec;
2706
2707 /* flags */
2709
2710 /* properties */
2712 "with_targets",
2713 true,
2714 "With Targets",
2715 "Assign IK Constraint with targets derived from the select bones/objects");
2716}
2717
2719
2720/* ------------------------------------------------------------------- */
2725
2727{
2728 Object *prev_ob = nullptr;
2729
2730 /* only remove IK Constraints */
2731 CTX_DATA_BEGIN_WITH_ID (C, bPoseChannel *, pchan, selected_pose_bones, Object *, ob) {
2732 bConstraint *con, *next;
2733
2734 /* TODO: should we be checking if these constraints were local
2735 * before we try and remove them? */
2736 for (con = pchan->constraints.first(); con; con = next) {
2737 next = con->next;
2738 if (con->type == CONSTRAINT_TYPE_KINEMATIC) {
2739 BKE_constraint_remove_ex(&pchan->constraints, ob, con);
2740 }
2741 }
2742 pchan->constflag &= ~(PCHAN_HAS_IK | PCHAN_HAS_NO_TARGET);
2743
2744 if (prev_ob != ob) {
2745 prev_ob = ob;
2746
2747 /* Refresh depsgraph. */
2749
2750 /* NOTE: notifier might evolve. */
2752 }
2753 }
2755
2756 return OPERATOR_FINISHED;
2757}
2758
2760{
2761 /* identifiers */
2762 ot->name = "Remove IK";
2763 ot->description = "Remove all IK Constraints from selected bones";
2764 ot->idname = "POSE_OT_ik_clear";
2765
2766 /* API callbacks. */
2769
2770 /* flags */
2772}
2773
2775
2776} // namespace blender::ed::object
Functions and classes to work with Actions.
Functions for backward compatibility with the legacy Action API.
Functions to work with AnimData.
Blender kernel action and pose functionality.
#define CTX_DATA_BEGIN_WITH_ID(C, Type, instance, member, Type_id, instance_id)
#define CTX_DATA_BEGIN(C, Type, instance, member)
#define CTX_DATA_END
General operations, lookup, etc. for blender objects.
A dynamically sized string ADT.
#define STRNCPY_UTF8(dst, src)
#define IFACE_(msgid)
#define ID_IS_EDITABLE(_id)
Definition DNA_ID.h:708
#define MAX_NAME
Definition DNA_defs.h:63
Object is a sort of wrapper for general info.
#define MINAFRAME
#define MAXFRAME
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define NA_ADDED
Definition WM_types.hh:636
#define ND_POSE
Definition WM_types.hh:482
#define ND_CONSTRAINT
Definition WM_types.hh:505
#define NA_REMOVED
Definition WM_types.hh:637
#define ND_TRANSFORM
Definition WM_types.hh:475
#define NC_OBJECT
Definition WM_types.hh:392
#define UNUSED_VARS(...)
#define ELEM(...)
#define STREQ(a, b)
#define A
btSequentialImpulseConstraintSolverMt int btPersistentManifold int btTypedConstraint ** constraints
Slot * slot_for_handle(slot_handle_t handle)
#define INT_MAX
#define abs
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
#define B
bAction * id_action_ensure(Main *bmain, ID *id)
Definition animdata.cc:195
FCurve * action_fcurve_ensure_ex(Main *bmain, bAction *act, PointerRNA *ptr, const FCurveDescriptor &fcurve_descriptor)
FCurve * fcurve_find_in_assigned_slot(AnimData &adt, const FCurveDescriptor &fcurve_descriptor)
static int constraint_type_get(Object *owner, bPoseChannel *pchan)
void object_test_constraints(Main *bmain, Object *ob)
static wmOperatorStatus objectsolver_set_inverse_exec(bContext *C, wmOperator *op)
static bool edit_constraint_poll(bContext *C)
bConstraint * constraint_active_get(Object *ob)
static wmOperatorStatus constraint_move_up_invoke(bContext *C, wmOperator *op, const wmEvent *event)
static wmOperatorStatus followpath_path_animate_exec(bContext *C, wmOperator *op)
static wmOperatorStatus constraint_move_down_invoke(bContext *C, wmOperator *op, const wmEvent *event)
static bool edit_constraint_poll_generic(bContext *C, StructRNA *rna_type, const bool is_liboverride_allowed)
void constraint_copy_for_object(Main *bmain, Object *ob_dst, bConstraint *con)
static void object_pose_tag_update(Main *bmain, Object *ob)
static wmOperatorStatus constraint_copy_invoke(bContext *C, wmOperator *op, const wmEvent *event)
void CONSTRAINT_OT_move_down(wmOperatorType *ot)
static void edit_constraint_report_property(wmOperatorType *ot)
static void object_test_constraint(Main *bmain, Object *ob, bConstraint *con)
void constraint_link(Main *bmain, Object *ob_dst, ListBaseT< bConstraint > *dst, ListBaseT< bConstraint > *src)
void constraint_active_set(Object *ob, bConstraint *con)
static bool objectsolver_clear_inverse_poll(bContext *C)
static wmOperatorStatus constraint_delete_exec(bContext *C, wmOperator *op)
static wmOperatorStatus childof_set_inverse_invoke(bContext *C, wmOperator *op, const wmEvent *)
void constraint_copy_for_pose(Main *bmain, Object *ob_dst, bPoseChannel *pchan, bConstraint *con)
static wmOperatorStatus constraint_apply_exec(bContext *C, wmOperator *op)
static void test_constraints(Main *bmain, Object *ob, bPoseChannel *pchan)
static wmOperatorStatus constraint_copy_to_selected_invoke(bContext *C, wmOperator *op, const wmEvent *event)
void constraint_dependency_tag_update(Main *bmain, Object *ob, bConstraint *con)
static bool edit_constraint_liboverride_allowed_poll(bContext *C)
void constraint_update(Main *bmain, Object *ob)
static bConstraint * edit_constraint_property_get(bContext *C, wmOperator *op, Object *ob, int type)
ListBaseT< bConstraint > * constraint_list_from_constraint(Object *ob, bConstraint *con, bPoseChannel **r_pchan)
static wmOperatorStatus childof_clear_inverse_invoke(bContext *C, wmOperator *op, const wmEvent *)
static const EnumPropertyItem * object_constraint_add_itemf(bContext *, PointerRNA *, PropertyRNA *, bool *r_free)
static wmOperatorStatus object_constraint_add_exec(bContext *C, wmOperator *op)
static wmOperatorStatus constraint_copy_exec(bContext *C, wmOperator *op)
void OBJECT_OT_constraint_add_with_targets(wmOperatorType *ot)
void CONSTRAINT_OT_objectsolver_set_inverse(wmOperatorType *ot)
static bool constraint_copy_to_selected_poll(bContext *C)
static void force_evaluation_if_constraint_disabled(bContext *C, Object *ob, bConstraint *con)
static wmOperatorStatus stretchto_reset_exec(bContext *C, wmOperator *op)
void base_select(Base *base, eObjectSelect_Mode mode)
static wmOperatorStatus constraint_copy_to_selected_exec(bContext *C, wmOperator *op)
void CONSTRAINT_OT_limitdistance_reset(wmOperatorType *ot)
static wmOperatorStatus pose_ik_clear_exec(bContext *C, wmOperator *)
static wmOperatorStatus objectsolver_clear_inverse_invoke(bContext *C, wmOperator *op, const wmEvent *)
static bool get_new_constraint_target(bContext *C, int con_type, Object **tar_ob, bPoseChannel **tar_pchan, bool add)
static wmOperatorStatus constraint_move_up_exec(bContext *C, wmOperator *op)
static wmOperatorStatus pose_ik_add_invoke(bContext *C, wmOperator *op, const wmEvent *)
static wmOperatorStatus limitdistance_reset_exec(bContext *C, wmOperator *op)
static wmOperatorStatus constraint_move_down_exec(bContext *C, wmOperator *op)
void CONSTRAINT_OT_copy(wmOperatorType *ot)
void CONSTRAINT_OT_apply(wmOperatorType *ot)
void constraint_tag_update(Main *bmain, Object *ob, bConstraint *con)
static wmOperatorStatus objectsolver_set_inverse_invoke(bContext *C, wmOperator *op, const wmEvent *)
void CONSTRAINT_OT_move_up(wmOperatorType *ot)
void POSE_OT_constraints_copy(wmOperatorType *ot)
ListBaseT< bConstraint > * constraint_active_list(Object *ob)
static wmOperatorStatus childof_clear_inverse_exec(bContext *C, wmOperator *op)
static wmOperatorStatus object_constraint_copy_exec(bContext *C, wmOperator *)
static wmOperatorStatus objectsolver_clear_inverse_exec(bContext *C, wmOperator *op)
static wmOperatorStatus childof_set_inverse_exec(bContext *C, wmOperator *op)
static wmOperatorStatus limitdistance_reset_invoke(bContext *C, wmOperator *op, const wmEvent *)
static wmOperatorStatus constraint_apply_invoke(bContext *C, wmOperator *op, const wmEvent *event)
Object * context_active_object(const bContext *C)
static wmOperatorStatus constraint_delete_invoke(bContext *C, wmOperator *op, const wmEvent *event)
static wmOperatorStatus constraint_move_to_index_invoke(bContext *C, wmOperator *op, const wmEvent *event)
static wmOperatorStatus followpath_path_animate_invoke(bContext *C, wmOperator *op, const wmEvent *)
void POSE_OT_ik_add(wmOperatorType *ot)
void POSE_OT_ik_clear(wmOperatorType *ot)
void POSE_OT_constraint_add(wmOperatorType *ot)
void constraint_dependency_update(Main *bmain, Object *ob)
static wmOperatorStatus constraint_move_to_index_exec(bContext *C, wmOperator *op)
void CONSTRAINT_OT_delete(wmOperatorType *ot)
bool constraint_move_to_index(Object *ob, bConstraint *con, int index)
static wmOperatorStatus stretchto_reset_invoke(bContext *C, wmOperator *op, const wmEvent *)
void CONSTRAINT_OT_stretchto_reset(wmOperatorType *ot)
void OBJECT_OT_constraint_add(wmOperatorType *ot)
static wmOperatorStatus pose_constraints_clear_exec(bContext *C, wmOperator *)
ListBaseT< bConstraint > * pose_constraint_list(const bContext *C)
void CONSTRAINT_OT_move_to_index(wmOperatorType *ot)
void OBJECT_OT_constraints_copy(wmOperatorType *ot)
static void test_constraint(Main *bmain, Object *owner, bPoseChannel *pchan, bConstraint *con, int type)
static wmOperatorStatus pose_constraint_add_exec(bContext *C, wmOperator *op)
static wmOperatorStatus constraint_add_exec(bContext *C, wmOperator *op, Object *ob, ListBaseT< bConstraint > *list, eBConstraint_Types type, const bool setTarget)
void OBJECT_OT_constraints_clear(wmOperatorType *ot)
static void edit_constraint_properties(wmOperatorType *ot)
static wmOperatorStatus pose_constraint_copy_exec(bContext *C, wmOperator *op)
static void set_constraint_nth_target(bConstraint *con, Object *target, const char subtarget[], int index)
static wmOperatorStatus pose_ik_add_exec(bContext *C, wmOperator *op)
void CONSTRAINT_OT_objectsolver_clear_inverse(wmOperatorType *ot)
static bool childof_clear_inverse_poll(bContext *C)
void CONSTRAINT_OT_childof_clear_inverse(wmOperatorType *ot)
void CONSTRAINT_OT_followpath_path_animate(wmOperatorType *ot)
static const EnumPropertyItem constraint_owner_items[]
void CONSTRAINT_OT_childof_set_inverse(wmOperatorType *ot)
void POSE_OT_constraint_add_with_targets(wmOperatorType *ot)
static wmOperatorStatus object_constraints_clear_exec(bContext *C, wmOperator *)
void CONSTRAINT_OT_copy_to_selected(wmOperatorType *ot)
void POSE_OT_constraints_clear(wmOperatorType *ot)
static bool edit_constraint_invoke_properties(bContext *C, wmOperator *op, const wmEvent *event, wmOperatorStatus *r_retval)
PointerRNA * region_panel_custom_data_under_cursor(const bContext *C, const wmEvent *event)
PopupMenu * popup_menu_begin(bContext *C, const char *title, int icon) ATTR_NONNULL()
void WM_main_add_notifier(uint type, void *reference)
void BKE_constraints_free(ListBaseT< bConstraint > *list)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void RNA_def_enum_funcs(PropertyRNA *prop, EnumPropertyItemFunc itemfunc)
bArmature * BKE_armature_from_object(Object *ob)
Object * BKE_object_add(Main *bmain, Scene *scene, ViewLayer *view_layer, ObjectType type, const char *name) ATTR_NONNULL(1
Definition object.cc:2296
wmOperatorStatus WM_menu_invoke(bContext *C, wmOperator *op, const wmEvent *)
void RNA_enum_item_add(EnumPropertyItem **items, int *totitem, const EnumPropertyItem *item)
bool ED_operator_posemode_exclusive(bContext *C)
int RNA_enum_get(PointerRNA *ptr, const char *name)
void BKE_pose_tag_recalc(Main *bmain, bPose *pose) ATTR_NONNULL(1
struct bConstraint * BKE_constraint_add_for_pose(struct Object *ob, struct bPoseChannel *pchan, const char *name, eBConstraint_Types type)
PropertyRNA * RNA_def_string(StructOrFunctionRNA *cont_, const char *identifier, const char *default_value, const int maxlen, const char *ui_name, const char *ui_description)
@ RPT_WARNING
Definition BKE_report.hh:40
bool is_identity_m4(const float m[4][4])
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
bool RNA_struct_is_a(const StructRNA *type, const StructRNA *srna)
MINLINE void copy_v3_v3(float r[3], const float a[3])
const EnumPropertyItem rna_enum_constraint_type_items[]
void BKE_constraint_targets_flush(struct bConstraint *con, ListBaseT< bConstraintTarget > *targets, bool no_copy)
@ CONSTRAINT_TYPE_TRANSFORM_CACHE
PointerRNA RNA_id_pointer_create(ID *id)
std::optional< std::string > RNA_path_from_ID_to_property(const PointerRNA *ptr, PropertyRNA *prop)
Definition rna_path.cc:1237
void mul_v3_m4v3(float r[3], const float mat[4][4], const float vec[3])
void RNA_string_set(PointerRNA *ptr, const char *name, const char *value)
struct bConstraint * BKE_constraint_copy_for_object(struct Object *ob, struct bConstraint *src)
void BKE_pose_tag_update_constraint_flags(bPose *pose) ATTR_NONNULL(1)
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
int RNA_int_get(PointerRNA *ptr, const char *name)
void RNA_enum_set(PointerRNA *ptr, const char *name, int value)
Base * BKE_view_layer_active_base_get(ViewLayer *view_layer)
FModifier * add_fmodifier(ListBaseT< FModifier > *modifiers, int type, FCurve *owner_fcu)
struct bConstraint * BKE_constraint_add_for_object(struct Object *ob, const char *name, eBConstraint_Types type)
bool ED_operator_object_active_local_editable(bContext *C)
struct bConstraint * BKE_constraint_copy_for_pose(struct Object *ob, struct bPoseChannel *pchan, struct bConstraint *src)
ViewLayer * CTX_data_view_layer(const bContext *C)
Global G
Definition blender.cc:57
@ FMODIFIER_TYPE_GENERATOR
int BLI_findindex(const ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:593
void CTX_wm_operator_poll_msg_set(bContext *C, const char *msg)
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
void DEG_id_tag_update(ID *id, unsigned int flags)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
BMesh const char void * data
void BLI_insertlinkafter(ListBase *listbase, void *vprevlink, void *vnewlink) ATTR_NONNULL(1)
Definition listbase.cc:339
void BKE_constraints_copy(ListBaseT< bConstraint > *dst, const ListBaseT< bConstraint > *src, bool do_extern)
Object * BKE_object_pose_armature_get(Object *ob)
Definition object.cc:2507
MovieTrackingTrack * BKE_tracking_object_find_track_with_name(MovieTrackingObject *tracking_object, const char *name)
Definition tracking.cc:1998
PointerRNA CTX_data_pointer_get(const bContext *C, const char *member)
Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C, bool rna_write_check=false)
bool BKE_constraint_is_nonlocal_in_liboverride(const struct Object *ob, const struct bConstraint *con)
MovieTrackingObject * BKE_tracking_object_get_camera(const MovieTracking *tracking)
Definition tracking.cc:1987
void BKE_object_eval_constraints(Depsgraph *depsgraph, Scene *scene, Object *ob)
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)
bool BKE_view_layer_synced_ensure(const Main &bmain, const Scene *scene, ViewLayer *view_layer)
bool RNA_struct_property_is_set(PointerRNA *ptr, const char *identifier)
int BKE_constraint_targets_get(struct bConstraint *con, ListBaseT< bConstraintTarget > *r_targets)
void BIK_test_constraint(struct Object *ob, struct bConstraint *cons)
void unit_m4(float m[4][4])
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
bool BKE_constraint_remove_ex(ListBaseT< bConstraint > *list, struct Object *ob, struct bConstraint *con)
struct bConstraint * BKE_constraints_active_get(ListBaseT< bConstraint > *list)
void RNA_enum_item_end(EnumPropertyItem **items, int *totitem)
void BKE_pose_update_constraint_flags(Object &pose_ob)
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
bool ED_operator_object_active_local_editable_posemode_exclusive(bContext *C)
const EnumPropertyItem rna_enum_dummy_NULL_items[]
Definition rna_rna.cc:28
bool ED_operator_object_active_editable(bContext *C)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
void BLI_insertlinkbefore(ListBase *listbase, void *vnextlink, void *vnewlink) ATTR_NONNULL(1)
Definition listbase.cc:378
void RNA_boolean_set(PointerRNA *ptr, const char *name, bool value)
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
PropertyRNA * RNA_def_int(StructOrFunctionRNA *cont_, const char *identifier, const int default_value, const int hardmin, const int hardmax, const char *ui_name, const char *ui_description, const int softmin, const int softmax)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
Main * CTX_data_main(const bContext *C)
@ ID_RECALC_TRANSFORM
Definition DNA_ID.h:1054
@ ID_RECALC_SYNC_TO_EVAL
Definition DNA_ID.h:1118
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
@ PROP_SKIP_SAVE
Definition RNA_types.hh:414
@ PROP_HIDDEN
Definition RNA_types.hh:408
void BKE_constraints_active_set(ListBaseT< bConstraint > *list, struct bConstraint *con)
bPoseChannel * CTX_data_active_pose_bone(const bContext *C)
void DEG_relations_tag_update(Main *bmain)
bPoseChannel * BKE_pose_channel_active_if_bonecoll_visible(Object *ob) ATTR_WARN_UNUSED_RESULT
MovieTrackingObject * BKE_tracking_object_get_named(MovieTracking *tracking, const char *name)
Definition tracking.cc:1971
Scene * CTX_data_scene(const bContext *C)
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
std::string RNA_string_get(PointerRNA *ptr, const char *name)
struct bConstraint * BKE_constraints_find_name(ListBaseT< bConstraint > *list, const char *name)
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
bool BKE_object_exists_check(Main *bmain, const Object *obtest)
Definition object.cc:2101
Scene * DEG_get_evaluated_scene(const Depsgraph *graph)
bPoseChannel * BKE_pose_channel_active_or_first_selected(Object *ob) ATTR_WARN_UNUSED_RESULT
Object * CTX_data_active_object(const bContext *C)
bool ED_operator_object_active_editable_ex(bContext *C, const Object *ob)
Bone * BKE_armature_find_bone_name(bArmature *arm, const char *name)
PointerRNA CTX_data_pointer_get_type(const bContext *C, const char *member, StructRNA *type)
@ CONSTRAINT_OVERRIDE_LIBRARY_LOCAL
void void void bool BLI_listbase_link_move(ListBase *listbase, void *vlink, int step) ATTR_NONNULL()
Definition listbase.cc:443
bool BKE_constraint_apply_and_remove_for_object(struct Depsgraph *depsgraph, struct Scene *scene, ListBaseT< bConstraint > *constraints, struct Object *ob, struct bConstraint *con)
#define EDIT_CONSTRAINT_OWNER_BONE
#define EDIT_CONSTRAINT_OWNER_OBJECT
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
struct AnimData * adt
ListBaseT< FModifier > modifiers
char name[258]
Definition DNA_ID.h:433
ListBaseT< bConstraint > constraints
struct bPose * pose
StructRNA * type
Definition RNA_types.hh:63
ListBaseT< bConstraintTarget > targets
struct bConstraintTarget * next
struct bConstraint * prev
struct bConstraint * next
ListBaseT< struct bConstraint > constraints
ePchan_ConstFlag constflag
ListBaseT< bPoseChannel > chanbase
PointerRNA op(wmOperatorType *ot, std::optional< StringRef > name, int icon, wm::OpCallContext context, eUI_Item_Flag flag)
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
const char * description
Definition WM_types.hh:1115
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1165
wmOperatorStatus(*) invoke(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1141
struct ReportList * reports