Blender V5.3
transform_convert_armature.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
11#include "DNA_action_types.h"
12#include "DNA_armature_types.h"
14
15#include "MEM_guardedalloc.h"
16
17#include "BLI_listbase.hh"
18#include "BLI_math_matrix_c.hh"
20#include "BLI_math_vector_c.hh"
21#include "BLI_set.hh"
22#include "BLI_vector.hh"
23
24#include "BKE_action.hh"
25#include "BKE_armature.hh"
26#include "BKE_constraint.h"
27#include "BKE_context.hh"
28#include "BKE_pose.hh"
29#include "BKE_report.hh"
30
31#include "BIK_api.h"
32
33#include "ED_anim_api.hh"
34#include "ED_armature.hh"
35
36#include "DEG_depsgraph.hh"
38
39#include "ANIM_action.hh"
40#include "ANIM_armature.hh"
42#include "ANIM_keyframing.hh"
43#include "ANIM_rna.hh"
44
45#include "transform.hh"
47#include "transform_snap.hh"
48
49/* Own include. */
50#include "transform_convert.hh"
51
52namespace blender::ed::transform {
53
56 float tail[3];
57 float rad_head;
58 float rad_tail;
59 float roll;
60 float head[3];
61 float dist;
62 float xwidth;
63 float zwidth;
64};
65
66/* Return if we need to update motion paths, only if they already exist,
67 * and we will insert a keyframe at the end of transform. */
69{
70 if (animrig::autokeyframe_cfra_can_key(scene, &ob->id)) {
72 }
73
74 return false;
75}
76
78 bKinematicConstraint *targetless_con)
79{
81 nullptr, pchan, "TempConstraint", CONSTRAINT_TYPE_KINEMATIC);
82
83 /* For draw, but also for detecting while pose solving. */
85
86 bKinematicConstraint *temp_con_data = static_cast<bKinematicConstraint *>(con->data);
87
88 if (targetless_con) {
89 /* If exists, use values from last targetless (but disabled) IK-constraint as base. */
90 *temp_con_data = *targetless_con;
91 }
92 else {
93 temp_con_data->flag = CONSTRAINT_IK_TIP;
94 }
95
97
98 return con;
99}
100
102{
103 BIK_clear_data(ob->pose);
104 /* TODO(sergey): Consider doing partial update only. */
106}
107
108/* -------------------------------------------------------------------- */
111
113{
114 bConstraint *con = pchan->constraints.first();
115
116 for (; con; con = con->next) {
117 if (con->type == CONSTRAINT_TYPE_KINEMATIC && (con->flag & CONSTRAINT_OFF) == 0 &&
118 (con->enforce != 0.0f))
119 {
120 bKinematicConstraint *data = static_cast<bKinematicConstraint *>(con->data);
121
122 if (data->tar == nullptr) {
123 return data;
124 }
125 if (data->tar->type == OB_ARMATURE && data->subtarget[0] == 0) {
126 return data;
127 }
128 }
129 }
130 return nullptr;
131}
132
136static short pose_grab_with_ik_add(Object &ob, bke::PChanBone pchanbone)
137{
138 bPoseChannel *pchan = pchanbone.pchan;
139 bKinematicConstraint *targetless = nullptr;
141
142 /* Sanity check. */
143 if (pchan == nullptr) {
144 return 0;
145 }
146
147 /* Rule: not if there's already an IK on this channel. */
148 for (bConstraint &con : pchan->constraints) {
149 if (con.type == CONSTRAINT_TYPE_KINEMATIC && (con.flag & CONSTRAINT_OFF) == 0) {
150 data = static_cast<bKinematicConstraint *>(con.data);
151
152 if (data->tar == nullptr || (data->tar->type == OB_ARMATURE && data->subtarget[0] == '\0')) {
153 /* Make reference to constraint to base things off later
154 * (if it's the last targetless constraint encountered). */
155 targetless = static_cast<bKinematicConstraint *>(con.data);
156
157 /* But, if this is a targetless IK, we make it auto anyway (for the children loop). */
158 if (con.enforce != 0.0f) {
159 data->flag |= CONSTRAINT_IK_AUTO;
160
161 /* If no chain length has been specified,
162 * just make things obey standard rotation locks too. */
163 if (data->rootbone == 0) {
164 for (bPoseChannel *pchan_iter = pchan; pchan_iter; pchan_iter = pchan_iter->parent) {
165 /* Here, we set IK-settings for bone from `pchan->protectflag`. */
166 /* XXX: careful with quaternion/axis-angle rotations
167 * where we're locking 4d components. */
168 if (pchan_iter->protectflag & OB_LOCK_ROTX) {
169 pchan_iter->ikflag |= BONE_IK_NO_XDOF_TEMP;
170 }
171 if (pchan_iter->protectflag & OB_LOCK_ROTY) {
172 pchan_iter->ikflag |= BONE_IK_NO_YDOF_TEMP;
173 }
174 if (pchan_iter->protectflag & OB_LOCK_ROTZ) {
175 pchan_iter->ikflag |= BONE_IK_NO_ZDOF_TEMP;
176 }
177 }
178 }
179
180 /* Return early (as in: don't actually create a temporary constraint here), since adding
181 * will take place later in add_pose_transdata() for targetless constraints. */
182 return 0;
183 }
184 }
185
186 if ((con.flag & CONSTRAINT_DISABLE) == 0 && (con.enforce != 0.0f)) {
187 return 0;
188 }
189 }
190 }
191
192 data = static_cast<bKinematicConstraint *>(add_temporary_ik_constraint(pchan, targetless)->data);
193
194 copy_v3_v3(data->grabtarget, pchan->pose_tail);
195
196 /* Watch-it! has to be 0 here, since we're still on the
197 * same bone for the first time through the loop #25885. */
198 data->rootbone = 0;
199
200 /* We only include bones that are part of a continual connected chain. */
201 do {
202 /* Here, we set IK-settings for bone from `pchan->protectflag`. */
203 /* XXX: careful with quaternion/axis-angle rotations where we're locking 4D components. */
204 if (pchan->protectflag & OB_LOCK_ROTX) {
206 }
207 if (pchan->protectflag & OB_LOCK_ROTY) {
209 }
210 if (pchan->protectflag & OB_LOCK_ROTZ) {
212 }
213
214 /* Now we count this pchan as being included. */
215 data->rootbone++;
216
217 /* We modify the pchan pointer below so we also have to get the corresponding bone pointer. */
218 Bone *bone = pchan->bone_get(ob);
219 /* Continue to parent, but only if we're connected to it. */
220 if (bone->flag & BONE_CONNECTED) {
221 pchan = pchan->parent;
222 }
223 else {
224 pchan = nullptr;
225 }
226 } while (pchan);
227
228 /* Make a copy of maximum chain-length. */
229 data->max_rootbone = data->rootbone;
230
231 return 1;
232}
233
237static short pose_grab_with_ik_children(Object &ob, Bone *bone)
238{
239 short wentdeeper = 0, added = 0;
240
241 /* Go deeper if children & children are connected. */
242 for (Bone &bonec : bone->childbase) {
243 if (bonec.flag & BONE_CONNECTED) {
244 wentdeeper = 1;
245 added += pose_grab_with_ik_children(ob, &bonec);
246 }
247 }
248 if (wentdeeper == 0) {
250 if (pchan) {
251 added += pose_grab_with_ik_add(ob, {pchan, bone});
252 }
253 }
254
255 return added;
256}
257
258/* Main call which adds temporal IK chains. */
259static short pose_grab_with_ik(Main *bmain, Object *ob)
260{
261 bArmature *arm;
262 Bone *bonec;
263 short tot_ik = 0;
264
265 if ((ob == nullptr) || (ob->pose == nullptr) || (ob->mode & OB_MODE_POSE) == 0) {
266 return 0;
267 }
268
269 arm = id_cast<bArmature *>(ob->data);
270
271 /* Rule: allow multiple Bones
272 * (but they must be selected, and only one ik-solver per chain should get added). */
273 for (bPoseChannel &pchan : ob->pose->chanbase) {
274 Bone *pchan_bone = pchan.bone_get(*ob);
275
276 if (BKE_pose_is_bonecoll_visible(arm, &pchan)) {
277 if ((pchan.flag & POSE_SELECTED) || (pchan_bone->flag & BONE_TRANSFORM_MIRROR)) {
278 /* Rule: no IK for solitary (unconnected) bones. */
279 for (bonec = pchan_bone->childbase.first(); bonec; bonec = bonec->next) {
280 if (bonec->flag & BONE_CONNECTED) {
281 break;
282 }
283 }
284 if ((pchan_bone->flag & BONE_CONNECTED) == 0 && (bonec == nullptr)) {
285 continue;
286 }
287
288 /* Rule: if selected Bone is not the root of a connected chain, it gets temporary IK. */
289 if (pchan.parent && (pchan_bone->flag & BONE_CONNECTED)) {
290 /* Only adds if there's no IK yet (and no parent bone was selected). */
291 bPoseChannel *parent;
292 for (parent = pchan.parent; parent; parent = parent->parent) {
293 if ((parent->flag & POSE_SELECTED) ||
294 (parent->bone_get(*ob)->flag & BONE_TRANSFORM_MIRROR))
295 {
296 break;
297 }
298 }
299 if (parent == nullptr) {
300 tot_ik += pose_grab_with_ik_add(*ob, {&pchan, pchan_bone});
301 }
302 }
303 else {
304 /* Rule: go over the children and add IK to the tips. */
305 tot_ik += pose_grab_with_ik_children(*ob, pchan_bone);
306 }
307 }
308 }
309 }
310
311 /* `ITaSC` needs clear for new IK constraints. */
312 if (tot_ik) {
314 }
315
316 return (tot_ik) ? 1 : 0;
317}
318
320
321/* -------------------------------------------------------------------- */
324
329 struct {
330 float loc[3];
331 float scale[3];
332 union {
333 float eul[3];
334 float quat[4];
335 float axis_angle[4];
336 };
337 float curve_in_x;
338 float curve_out_x;
339 float roll1;
340 float roll2;
346 float offset_mtx[4][4];
347};
348
350 bke::PChanBone pchanbone,
351 bke::PChanBoneConst pchanbone_orig,
352 bool is_mirror_relative)
353{
354 bPoseChannel *pchan = pchanbone.pchan;
355 pid->pchan = pchan;
356 copy_v3_v3(pid->orig.loc, pchan->loc);
357 copy_v3_v3(pid->orig.scale, pchan->scale);
358 pid->orig.curve_in_x = pchan->curve_in_x;
359 pid->orig.curve_out_x = pchan->curve_out_x;
360 pid->orig.roll1 = pchan->roll1;
361 pid->orig.roll2 = pchan->roll2;
362
363 if (pchan->rotmode > 0) {
364 copy_v3_v3(pid->orig.eul, pchan->eul);
365 }
366 else if (pchan->rotmode == ROT_MODE_AXISANGLE) {
367 copy_v3_v3(pid->orig.axis_angle, pchan->rotAxis);
368 pid->orig.axis_angle[3] = pchan->rotAngle;
369 }
370 else {
371 copy_qt_qt(pid->orig.quat, pchan->quat);
372 }
373
374 if (is_mirror_relative) {
375 float pchan_mtx[4][4];
376 float pchan_mtx_mirror[4][4];
377
378 float flip_mtx[4][4];
379 unit_m4(flip_mtx);
380 flip_mtx[0][0] = -1;
381
382 BKE_pchan_to_mat4(pchanbone_orig, pchan_mtx_mirror);
383 BKE_pchan_to_mat4(pchanbone, pchan_mtx);
384
385 mul_m4_m4m4(pchan_mtx_mirror, pchan_mtx_mirror, flip_mtx);
386 mul_m4_m4m4(pchan_mtx_mirror, flip_mtx, pchan_mtx_mirror);
387
388 invert_m4(pchan_mtx_mirror);
389 mul_m4_m4m4(pid->offset_mtx, pchan_mtx, pchan_mtx_mirror);
390 }
391 else {
392 unit_m4(pid->offset_mtx);
393 }
394}
395
397
398/* -------------------------------------------------------------------- */
401
403 TransInfo *t, bPoseChannel *pchan, Object *ob, TransData *td, TransDataExtension *td_ext)
404{
405 Bone *bone = pchan->bone_get(*ob);
406 float pmat[3][3], omat[3][3];
407 float cmat[3][3], tmat[3][3];
408
409 const bArmature *arm = id_cast<bArmature *>(ob->data);
412 copy_v3_v3(td_ext->center_no_override, pchan->pose_mat[3]);
413 }
414 else {
416 }
417
418 td->flag = TD_SELECTED;
420 td->flag |= TD_NOCENTER;
421 }
422
424 td->flag |= TD_NOCENTER;
425 td->flag |= TD_NO_LOC;
426 }
427
428 td->extra = MEM_new<bke::PChanBone>(__func__, pchan, bone);
429 td->protectflag = pchan->protectflag;
430
431 td->loc = pchan->loc;
432 copy_v3_v3(td->iloc, pchan->loc);
433
434 td_ext->scale = pchan->scale;
435 copy_v3_v3(td_ext->iscale, pchan->scale);
436
437 if (pchan->rotmode > 0) {
438 td_ext->rot = pchan->eul;
439 td_ext->rotAxis = nullptr;
440 td_ext->rotAngle = nullptr;
441 td_ext->quat = nullptr;
442
443 copy_v3_v3(td_ext->irot, pchan->eul);
444 }
445 else if (pchan->rotmode == ROT_MODE_AXISANGLE) {
446 td_ext->rot = nullptr;
447 td_ext->rotAxis = pchan->rotAxis;
448 td_ext->rotAngle = &pchan->rotAngle;
449 td_ext->quat = nullptr;
450
451 td_ext->irotAngle = pchan->rotAngle;
452 copy_v3_v3(td_ext->irotAxis, pchan->rotAxis);
453 }
454 else {
455 td_ext->rot = nullptr;
456 td_ext->rotAxis = nullptr;
457 td_ext->rotAngle = nullptr;
458 td_ext->quat = pchan->quat;
459
460 copy_qt_qt(td_ext->iquat, pchan->quat);
461 }
462 td_ext->rotOrder = pchan->rotmode;
463
464 /* Proper way to get parent transform + our own transform + constraints transform. */
465 copy_m3_m4(omat, ob->object_to_world().ptr());
466
467 {
469 float rpmat[3][3];
470
471 /* Not using the `pchan->custom_tx` here because we need the transformation to be
472 * relative to the actual bone being modified, not it's visual representation. */
474 if (t->mode == TFM_TRANSLATION) {
475 copy_m3_m4(pmat, bpt.loc_mat);
476 }
477 else {
478 copy_m3_m4(pmat, bpt.rotscale_mat);
479 }
480
481 /* Grrr! Exceptional case: When translating pose bones that are either Hinge or NoLocal,
482 * and want align snapping, we just need both `loc_mat` and `rotscale_mat`.
483 * So simply always store rotscale mat in `td->ext`, and always use it to apply rotations...
484 * Ugly to need such hacks! :/ */
485 copy_m3_m4(rpmat, bpt.rotscale_mat);
486
487 if (constraints_list_needinv(t, &pchan->constraints)) {
488 copy_m3_m4(tmat, pchan->constinv);
489 invert_m3_m3(cmat, tmat);
490 mul_m3_series(td->mtx, cmat, omat, pmat);
491 mul_m3_series(td_ext->r_mtx, cmat, omat, rpmat);
492 }
493 else {
494 mul_m3_series(td->mtx, omat, pmat);
495 mul_m3_series(td_ext->r_mtx, omat, rpmat);
496 }
497 invert_m3_m3(td_ext->r_smtx, td_ext->r_mtx);
498 }
499
501
502 /* Exceptional case: rotate the pose bone which also applies transformation
503 * when a parentless bone has #BONE_NO_LOCAL_LOCATION []. */
504 if (!ELEM(t->mode, TFM_TRANSLATION, TFM_RESIZE) &&
505 (pchan->bone_get(*ob)->flag & BONE_NO_LOCAL_LOCATION))
506 {
507 if (pchan->parent) {
508 /* Same as `td->smtx` but without `pchan->bone_get(*ob)->bone_mat`. */
510 mul_m3_m3m3(td_ext->l_smtx, pchan->bone_get(*ob)->bone_mat, td->smtx);
511 }
512 else {
514 }
515 }
516
517 /* For `axismtx` we use the bone's own transform. */
519 mul_m3_m3m3(td->axismtx, omat, pmat);
521
522 if (t->orient_type_mask & (1 << V3D_ORIENT_GIMBAL)) {
523 if (!gimbal_axis_pose(ob, pchan, td_ext->axismtx_gimbal)) {
524 copy_m3_m3(td_ext->axismtx_gimbal, td->axismtx);
525 }
526 }
527
528 if (t->mode == TFM_BONE_ENVELOPE_DIST) {
529 td->loc = nullptr;
530 td->val = &bone->dist;
531 td->ival = bone->dist;
532 }
533 else if (t->mode == TFM_BONESIZE) {
534 /* Abusive storage of scale in the loc pointer :). */
535 td->loc = &bone->xwidth;
536 copy_v3_v3(td->iloc, td->loc);
537 td->val = nullptr;
538 }
539
540 /* In this case we can do target-less IK grabbing. */
541 if (t->mode == TFM_TRANSLATION) {
543 if (data) {
544 if (data->flag & CONSTRAINT_IK_TIP) {
545 copy_v3_v3(data->grabtarget, pchan->pose_tail);
546 }
547 else {
548 copy_v3_v3(data->grabtarget, pchan->pose_head);
549 }
550 td->loc = data->grabtarget;
551 copy_v3_v3(td->iloc, td->loc);
552
553 data->flag |= CONSTRAINT_IK_AUTO;
554
555 /* Add a temporary auto IK constraint here, as we will only temporarily active this
556 * targetless bone during transform. (Targetless IK constraints are treated as if they are
557 * disabled unless they are transformed).
558 * Only do this for targetless IK though, AutoIK already added a constraint in
559 * pose_grab_with_ik_add() beforehand. */
560 if ((data->flag & CONSTRAINT_IK_TEMP) == 0) {
562 Main *bmain = CTX_data_main(t->context);
564 }
565
566 /* Only object matrix correction. */
567 copy_m3_m3(td->mtx, omat);
569 }
570 }
571
572 /* Store reference to first constraint. */
573 td->con = pchan->constraints.first();
574}
575
577{
578 /* Free any data in custom_data->data. */
579 if (custom_data->data != nullptr) {
580 MEM_delete_void(custom_data->data);
581 custom_data->data = nullptr;
582 }
583
584 /* Armature Pose mode allocates data in td.extra, which is freed here. */
585 if (t->data_type == &TransConvertType_Pose) {
586 for (int i = 0; i < tc->data_len; i++) {
587 TransData &td = tc->data[i];
588 if (!td.extra) {
589 continue;
590 }
591
592 bke::PChanBone *pchanbone = static_cast<bke::PChanBone *>(td.extra);
593 MEM_delete(pchanbone);
594 }
595 }
596}
597
598static void createTransPose(bContext * /*C*/, TransInfo *t)
599{
600 Main *bmain = CTX_data_main(t->context);
601
602 t->data_len_all = 0;
603
604 bool has_translate_rotate_buf[2] = {false, false};
605 bool *has_translate_rotate = (t->mode == TFM_TRANSLATION) ? has_translate_rotate_buf : nullptr;
606
608 Object *ob = tc->poseobj;
609 bPose *pose = ob->pose;
610
611 bArmature *arm;
612
613 /* Check validity of state. */
614 arm = BKE_armature_from_object(tc->poseobj);
615 if ((arm == nullptr) || (pose == nullptr)) {
616 continue;
617 }
618
619 const bool mirror = ((pose->flag & POSE_MIRROR_EDIT) != 0);
620
621 /* Set flags. */
623
624 /* Check if we're doing auto-IK or switching from translate to rotate.
625 * Also collect needed information (such as bone count)
626 * and clear flags from previous runs that can interfere. */
627 for (bPoseChannel &pchan : ob->pose->chanbase) {
628 Bone *bone = pchan.bone_get(*ob);
629
630 /* Clear the MIRROR flag from previous runs. */
632
633 if (!(pchan.runtime.flag & POSE_RUNTIME_TRANSFORM)) {
634 continue;
635 }
636
637 tc->data_len++;
638
639 if (has_translate_rotate != nullptr) {
640 if (has_translate_rotate[0] && has_translate_rotate[1]) {
641 continue;
642 }
643
644 if (has_targetless_ik(&pchan) == nullptr) {
645 if (pchan.parent && (bone->flag & BONE_CONNECTED)) {
647 has_translate_rotate[0] = true;
648 }
649 }
650 else {
651 if ((pchan.protectflag & OB_LOCK_LOC) != OB_LOCK_LOC) {
652 has_translate_rotate[0] = true;
653 }
654 }
655 if ((pchan.protectflag & OB_LOCK_ROT) != OB_LOCK_ROT) {
656 has_translate_rotate[1] = true;
657 }
658 }
659 else {
660 has_translate_rotate[0] = true;
661 }
662 }
663 }
664
665 if (tc->data_len == 0) {
666 continue;
667 }
668
669 if (arm->flag & ARM_RESTPOS) {
670 if (ELEM(t->mode, TFM_DUMMY, TFM_BONESIZE) == 0) {
671 BKE_report(t->reports, RPT_ERROR, "Cannot change Pose when 'Rest Position' is enabled");
672 tc->data_len = 0;
673 continue;
674 }
675 }
676
677 if (mirror) {
678 int total_mirrored = 0;
679 for (bPoseChannel &pchan : ob->pose->chanbase) {
680 if ((pchan.runtime.flag & POSE_RUNTIME_TRANSFORM) &&
682 {
683 total_mirrored++;
684 }
685 }
686
688 (total_mirrored + 1), "PoseInitData_Mirror");
689
690 /* Trick to terminate iteration. */
691 pid[total_mirrored].pchan = nullptr;
692
693 tc->custom.type.data = pid;
694 tc->custom.type.use_free = true;
695 }
696 }
697
699 if (tc->data_len == 0) {
700 continue;
701 }
702 Object *ob = tc->poseobj;
703 TransData *td;
705
706 PoseInitData_Mirror *pid = static_cast<PoseInitData_Mirror *>(tc->custom.type.data);
707 int pid_index = 0;
708 bPose *pose = ob->pose;
709
710 if (pose == nullptr) {
711 continue;
712 }
713
714 const bool mirror = ((pose->flag & POSE_MIRROR_EDIT) != 0);
715 const bool is_mirror_relative = ((pose->flag & POSE_MIRROR_RELATIVE) != 0);
716
717 /* We also allow non-active objects to be transformed, in weight-paint. */
718 tc->poseobj = ob;
719
720 /* Initialize trans data. */
721 td = tc->data = MEM_new_array_zeroed<TransData>(tc->data_len, "TransPoseBone");
722 tdx = tc->data_ext = MEM_new_array_zeroed<TransDataExtension>(tc->data_len,
723 "TransPoseBoneExt");
724
725 if (mirror) {
726 for (bPoseChannel &pchan : pose->chanbase) {
727 if (pchan.runtime.flag & POSE_RUNTIME_TRANSFORM) {
728 bPoseChannel *pchan_mirror = BKE_pose_channel_get_mirrored(ob->pose, pchan.name);
729 if (pchan_mirror) {
730 Bone *bone = pchan.bone_get(*ob);
731 Bone *bone_mirror = pchan_mirror->bone_get(*ob);
732 bone_mirror->flag |= BONE_TRANSFORM_MIRROR;
734 &pid[pid_index], {pchan_mirror, bone_mirror}, {&pchan, bone}, is_mirror_relative);
735 pid_index++;
736 }
737 }
738 }
739 }
740
741 /* Do we need to add temporal IK chains? */
742 if ((pose->flag & POSE_AUTO_IK) && t->mode == TFM_TRANSLATION) {
743 if (pose_grab_with_ik(bmain, ob)) {
744 t->flag |= T_AUTOIK;
745 has_translate_rotate[0] = true;
746 }
747 }
748
749 /* Use pose channels to fill trans data. */
750 td = tc->data;
751 tdx = tc->data_ext;
752 tdx->center_no_override[0] = 0;
753 tdx->center_no_override[1] = 0;
754 tdx->center_no_override[2] = 0;
755 for (bPoseChannel &pchan : ob->pose->chanbase) {
756 if (pchan.runtime.flag & POSE_RUNTIME_TRANSFORM) {
757 add_pose_transdata(t, &pchan, ob, td++, tdx++);
758 }
759 }
760 tc->custom.type.free_cb = free_pose_transdata;
761 tc->custom.type.use_free = true;
762
763 if (td != (tc->data + tc->data_len)) {
764 BKE_report(t->reports, RPT_DEBUG, "Bone selection count error");
766 }
767 }
768
769 /* Initialize initial auto=IK chain-length's? */
770 if (t->flag & T_AUTOIK) {
772 }
773
774 /* If there are no translatable bones, do rotation. */
775 if ((t->mode == TFM_TRANSLATION) && !has_translate_rotate[0]) {
776 if (has_translate_rotate[1]) {
777 t->mode = TFM_ROTATION;
778 }
779 else {
780 t->mode = TFM_RESIZE;
781 }
782 }
783}
784
785static void free_bone_init_data(TransInfo * /*t*/,
786 TransDataContainer * /*tc*/,
787 TransCustomData *custom_data)
788{
789 MEM_delete(static_cast<Vector<BoneInitData> *>(custom_data->data));
790 custom_data->data = nullptr;
791}
792
794{
795 t->data_len_all = 0;
796
798 bArmature *arm = id_cast<bArmature *>(tc->obedit->data);
799 ListBaseT<EditBone> *edbo = arm->edbo;
800 bool mirror = ((arm->flag & ARM_MIRROR_EDIT) != 0);
801 int total_mirrored = 0;
802
803 tc->data_len = 0;
804 for (EditBone &ebo : *edbo) {
805 const int data_len_prev = tc->data_len;
806
807 if (animrig::bone_is_visible(arm, &ebo) && !(ebo.flag & BONE_EDITMODE_LOCKED)) {
809 if (ebo.flag & BONE_SELECTED) {
810 tc->data_len++;
811 }
812 }
813 else if (t->mode == TFM_BONE_ROLL) {
814 if (ebo.flag & BONE_SELECTED) {
815 tc->data_len++;
816 }
817 }
818 else {
819 if (ebo.flag & BONE_TIPSEL) {
820 tc->data_len++;
821 }
822 if (ebo.flag & BONE_ROOTSEL) {
823 tc->data_len++;
824 }
825 }
826 }
827
828 if (mirror && (data_len_prev < tc->data_len)) {
829 EditBone *eboflip = ED_armature_ebone_get_mirrored(arm->edbo, &ebo);
830 if (eboflip) {
831 total_mirrored++;
832 }
833 }
834 }
835 if (!tc->data_len) {
836 continue;
837 }
838
839 if (mirror) {
840 Vector<BoneInitData> *bid = MEM_new<Vector<BoneInitData>>(__func__);
841 bid->reserve(total_mirrored);
842
843 tc->custom.type.data = bid;
844 tc->custom.type.free_cb = free_bone_init_data;
845 tc->custom.type.use_free = true;
846 }
847 t->data_len_all += tc->data_len;
848 }
849
851 t->data_len_all = -1;
852
854 if (!tc->data_len) {
855 continue;
856 }
857
858 bArmature *arm = id_cast<bArmature *>(tc->obedit->data);
859 ListBaseT<EditBone> *edbo = arm->edbo;
860 TransData *td, *td_old;
861 float mtx[3][3], smtx[3][3], bonemat[3][3];
862 bool mirror = ((arm->flag & ARM_MIRROR_EDIT) != 0);
863 auto &bid = *static_cast<Vector<BoneInitData> *>(tc->custom.type.data);
864
865 copy_m3_m4(mtx, tc->obedit->object_to_world().ptr());
867
868 td = tc->data = MEM_new_array_zeroed<TransData>(tc->data_len, "TransEditBone");
869
870 for (EditBone &ebo : *edbo) {
871 td_old = td;
872
873 /* (length == 0.0) on extrude, used for scaling radius of bone points. */
874 ebo.oldlength = ebo.length;
875
876 if (animrig::bone_is_visible(arm, &ebo) && !(ebo.flag & BONE_EDITMODE_LOCKED)) {
877 if (t->mode == TFM_BONE_ENVELOPE) {
878 if (ebo.flag & BONE_ROOTSEL) {
879 td->val = &ebo.rad_head;
880 td->ival = *td->val;
881
882 copy_v3_v3(td->center, ebo.head);
883 td->flag = TD_SELECTED;
884
885 copy_m3_m3(td->smtx, smtx);
886 copy_m3_m3(td->mtx, mtx);
887
888 td->loc = nullptr;
889
890 td++;
891 }
892 if (ebo.flag & BONE_TIPSEL) {
893 td->val = &ebo.rad_tail;
894 td->ival = *td->val;
895 copy_v3_v3(td->center, ebo.tail);
896 td->flag = TD_SELECTED;
897
898 copy_m3_m3(td->smtx, smtx);
899 copy_m3_m3(td->mtx, mtx);
900
901 td->loc = nullptr;
902
903 td++;
904 }
905 }
907 if (ebo.flag & BONE_SELECTED) {
908 if (t->mode == TFM_BONE_ENVELOPE_DIST) {
909 td->loc = nullptr;
910 td->val = &ebo.dist;
911 td->ival = ebo.dist;
912 }
913 else {
914 /* Abusive storage of scale in the loc pointer :). */
915 td->loc = &ebo.xwidth;
916 copy_v3_v3(td->iloc, td->loc);
917 td->val = nullptr;
918 }
919 copy_v3_v3(td->center, ebo.head);
920 td->flag = TD_SELECTED;
921
922 /* Use local bone matrix. */
923 ED_armature_ebone_to_mat3(&ebo, bonemat);
924 mul_m3_m3m3(td->mtx, mtx, bonemat);
925 invert_m3_m3(td->smtx, td->mtx);
926
927 copy_m3_m3(td->axismtx, td->mtx);
929
930 td++;
931 }
932 }
933 else if (t->mode == TFM_BONE_ROLL) {
934 if (ebo.flag & BONE_SELECTED) {
935 td->loc = nullptr;
936 td->val = &(ebo.roll);
937 td->ival = ebo.roll;
938
939 copy_v3_v3(td->center, ebo.head);
940 td->flag = TD_SELECTED;
941
942 td++;
943 }
944 }
945 else {
946 if (ebo.flag & BONE_TIPSEL) {
947 copy_v3_v3(td->iloc, ebo.tail);
948
949 /* Don't allow single selected tips to have a modified center,
950 * causes problem with snapping (see #45974).
951 * However, in rotation mode, we want to keep that 'rotate bone around root with
952 * only its tip selected' behavior (see #46325). */
953 if ((t->around == V3D_AROUND_LOCAL_ORIGINS) &&
954 ((t->mode == TFM_ROTATION) || (ebo.flag & BONE_ROOTSEL)))
955 {
956 copy_v3_v3(td->center, ebo.head);
957 }
958 else {
959 copy_v3_v3(td->center, td->iloc);
960 }
961
962 td->loc = ebo.tail;
963 td->flag = TD_SELECTED;
964 if (ebo.flag & BONE_EDITMODE_LOCKED) {
966 }
967
968 copy_m3_m3(td->smtx, smtx);
969 copy_m3_m3(td->mtx, mtx);
970
972
973 if ((ebo.flag & BONE_ROOTSEL) == 0) {
974 td->extra = &ebo;
975 td->ival = ebo.roll;
976 }
977
978 td->val = nullptr;
979
980 td++;
981 }
982 if (ebo.flag & BONE_ROOTSEL) {
983 copy_v3_v3(td->iloc, ebo.head);
984 copy_v3_v3(td->center, td->iloc);
985 td->loc = ebo.head;
986 td->flag = TD_SELECTED;
987 if (ebo.flag & BONE_EDITMODE_LOCKED) {
989 }
990
991 copy_m3_m3(td->smtx, smtx);
992 copy_m3_m3(td->mtx, mtx);
993
995
996 td->extra = &ebo; /* To fix roll. */
997 td->ival = ebo.roll;
998
999 td->val = nullptr;
1000
1001 td++;
1002 }
1003 }
1004 }
1005
1006 if (mirror && (td_old != td)) {
1007 EditBone *eboflip = ED_armature_ebone_get_mirrored(arm->edbo, &ebo);
1008 if (eboflip) {
1009 BoneInitData bone_data{};
1010 bone_data.bone = eboflip;
1011 bone_data.dist = eboflip->dist;
1012 bone_data.rad_head = eboflip->rad_head;
1013 bone_data.rad_tail = eboflip->rad_tail;
1014 bone_data.roll = eboflip->roll;
1015 bone_data.xwidth = eboflip->xwidth;
1016 bone_data.zwidth = eboflip->zwidth;
1017 copy_v3_v3(bone_data.head, eboflip->head);
1018 copy_v3_v3(bone_data.tail, eboflip->tail);
1019 bid.append(bone_data);
1020 }
1021 }
1022 }
1023 }
1024}
1025
1027
1028/* -------------------------------------------------------------------- */
1031
1033{
1034 bArmature *arm;
1035 auto &bid = *static_cast<Vector<BoneInitData> *>(tc->custom.type.data);
1036
1037 if (tc->obedit) {
1038 arm = id_cast<bArmature *>(tc->obedit->data);
1039 }
1040 else {
1041 BLI_assert(tc->poseobj != nullptr);
1042 arm = id_cast<bArmature *>(tc->poseobj->data);
1043 }
1044
1045 for (const BoneInitData &init : bid) {
1046 EditBone *ebo = init.bone;
1047
1048 ebo->dist = init.dist;
1049 ebo->rad_head = init.rad_head;
1050 ebo->rad_tail = init.rad_tail;
1051 ebo->roll = init.roll;
1052 ebo->xwidth = init.xwidth;
1053 ebo->zwidth = init.zwidth;
1054 copy_v3_v3(ebo->head, init.head);
1055 copy_v3_v3(ebo->tail, init.tail);
1056
1057 if (arm->flag & ARM_MIRROR_EDIT) {
1058 /* Also move connected ebo_child, in case ebo_child's name aren't mirrored properly. */
1059 for (EditBone &ebo_child : *arm->edbo) {
1060 if ((ebo_child.flag & BONE_CONNECTED) && (ebo_child.parent == ebo)) {
1061 copy_v3_v3(ebo_child.head, ebo->tail);
1062 ebo_child.rad_head = ebo->rad_tail;
1063 }
1064 }
1065
1066 /* Also move connected parent, in case parent's name isn't mirrored properly. */
1067 if ((ebo->flag & BONE_CONNECTED) && ebo->parent) {
1068 EditBone *parent = ebo->parent;
1069 copy_v3_v3(parent->tail, ebo->head);
1070 parent->rad_tail = ebo->rad_head;
1071 }
1072 }
1073 }
1074}
1075
1077{
1078 if (t->state != TRANS_CANCEL) {
1080 }
1081
1083 bArmature *arm = id_cast<bArmature *>(tc->obedit->data);
1084 ListBaseT<EditBone> *edbo = arm->edbo;
1085 EditBone *ebo, *ebo_parent;
1086 TransData *td = tc->data;
1087 int i;
1088
1089 /* Ensure all bones are correctly adjusted. */
1090 for (EditBone &ebo : *edbo) {
1091 ebo_parent = (ebo.flag & BONE_CONNECTED) ? ebo.parent : nullptr;
1092
1093 if (ebo_parent) {
1094 /* If this bone has a parent tip that has been moved. */
1095 if (animrig::bone_is_visible(arm, ebo_parent) && (ebo_parent->flag & BONE_TIPSEL)) {
1096 copy_v3_v3(ebo.head, ebo_parent->tail);
1097 if (t->mode == TFM_BONE_ENVELOPE) {
1098 ebo.rad_head = ebo_parent->rad_tail;
1099 }
1100 }
1101 /* If this bone has a parent tip that has NOT been moved. */
1102 else {
1103 copy_v3_v3(ebo_parent->tail, ebo.head);
1104 if (t->mode == TFM_BONE_ENVELOPE) {
1105 ebo_parent->rad_tail = ebo.rad_head;
1106 }
1107 }
1108 }
1109
1110 /* On extrude bones, oldlength==0.0f, so we scale radius of points. */
1111 ebo.length = len_v3v3(ebo.head, ebo.tail);
1112 if (ebo.oldlength == 0.0f) {
1113 ebo.rad_head = 0.25f * ebo.length;
1114 ebo.rad_tail = 0.10f * ebo.length;
1115 ebo.dist = 0.25f * ebo.length;
1116 if (ebo.parent) {
1117 ebo.rad_head = std::min(ebo.rad_head, ebo.parent->rad_tail);
1118 }
1119 }
1120 else if (t->mode != TFM_BONE_ENVELOPE) {
1121 /* If bones change length, lets do that for the deform distance as well. */
1122 ebo.dist *= ebo.length / ebo.oldlength;
1123 ebo.rad_head *= ebo.length / ebo.oldlength;
1124 ebo.rad_tail *= ebo.length / ebo.oldlength;
1125 ebo.oldlength = ebo.length;
1126
1127 if (ebo_parent) {
1128 ebo_parent->rad_tail = ebo.rad_head;
1129 }
1130 }
1131 }
1132
1134 /* Fix roll. */
1135 for (i = 0; i < tc->data_len; i++, td++) {
1136 if (td->extra) {
1137 float vec[3], up_axis[3];
1138 float qrot[4];
1139 float roll;
1140
1141 ebo = static_cast<EditBone *>(td->extra);
1142
1143 if (t->state == TRANS_CANCEL) {
1144 /* Restore roll. */
1145 ebo->roll = td->ival;
1146 }
1147 else {
1148 copy_v3_v3(up_axis, td->axismtx[2]);
1149
1150 sub_v3_v3v3(vec, ebo->tail, ebo->head);
1151 normalize_v3(vec);
1152 rotation_between_vecs_to_quat(qrot, td->axismtx[1], vec);
1153 mul_qt_v3(qrot, up_axis);
1154
1155 /* Roll has a tendency to flip in certain orientations - #34283, #33974. */
1156 roll = ED_armature_ebone_roll_to_vector(ebo, up_axis, false);
1157 ebo->roll = angle_compat_rad(roll, td->ival);
1158 }
1159 }
1160 }
1161 }
1162
1163 if (arm->flag & ARM_MIRROR_EDIT) {
1164 if (t->state != TRANS_CANCEL) {
1166 }
1167 else {
1168 restoreBones(tc);
1169 }
1170 }
1171 }
1172}
1173
1175
1176/* -------------------------------------------------------------------- */
1179
1185{
1186 float flip_mtx[4][4];
1187 unit_m4(flip_mtx);
1188 flip_mtx[0][0] = -1;
1189
1190 for (bPoseChannel &pchan_orig : ob->pose->chanbase) {
1191 /* Clear the MIRROR flag from previous runs. */
1192 pchan_orig.bone_get(*ob)->flag &= ~BONE_TRANSFORM_MIRROR;
1193 }
1194
1195 bPose *pose = ob->pose;
1196 PoseInitData_Mirror *pid = nullptr;
1197 if ((t->mode != TFM_BONESIZE) && (pose->flag & POSE_MIRROR_RELATIVE)) {
1198 pid = static_cast<PoseInitData_Mirror *>(tc->custom.type.data);
1199 }
1200
1201 TransData *td = tc->data;
1202 for (int i = tc->data_len; i--; td++) {
1203 /* I (Sybren) am not sure how the multi-object transforming works, so there is no assumption
1204 * that the pchan in td->extra belongs to the 'ob' parameter given to this function. That's why
1205 * the bone lookup happens when td->extra gets its value, and not here. */
1206 bke::PChanBoneConst *pchanbone_orig = static_cast<bke::PChanBoneConst *>(td->extra);
1207 const bPoseChannel *pchan_orig = pchanbone_orig->pchan;
1208 const Bone *bone_orig = pchanbone_orig->bone;
1209
1211 if (bone_orig->flag & BONE_TRANSFORM_MIRROR) {
1212 /* If the bone already has the flag set it was already visited by its mirror bone which may
1213 * also be selected. To avoid double transformations, ignore in this case. See #95396. */
1214 if (pid) {
1215 pid++;
1216 }
1217 continue;
1218 }
1219 /* No layer check, correct mirror is more important. */
1220 bPoseChannel *pchan = BKE_pose_channel_get_mirrored(pose, pchan_orig->name);
1221 if (pchan == nullptr) {
1222 continue;
1223 }
1224 Bone *pchan_bone = pchan->bone_get(*ob);
1225
1226 /* Also do bbone scaling. */
1227 pchan_bone->xwidth = bone_orig->xwidth;
1228 pchan_bone->zwidth = bone_orig->zwidth;
1229
1230 /* We assume X-axis flipping for now. */
1231 pchan->curve_in_x = pchan_orig->curve_in_x * -1;
1232 pchan->curve_out_x = pchan_orig->curve_out_x * -1;
1233 pchan->roll1 = pchan_orig->roll1 * -1; /* XXX? */
1234 pchan->roll2 = pchan_orig->roll2 * -1; /* XXX? */
1235
1236 float pchan_mtx_final[4][4];
1237 BKE_pchan_to_mat4(*pchanbone_orig, pchan_mtx_final);
1238 mul_m4_m4m4(pchan_mtx_final, pchan_mtx_final, flip_mtx);
1239 mul_m4_m4m4(pchan_mtx_final, flip_mtx, pchan_mtx_final);
1240 if (pid) {
1241 mul_m4_m4m4(pchan_mtx_final, pid->offset_mtx, pchan_mtx_final);
1242 }
1243 float loc[3], rot[3][3], scale[3];
1244 mat4_to_loc_rot_size(loc, rot, scale, pchan_mtx_final);
1248
1249 /* Set flag to let auto key-frame know to key-frame the mirrored bone. */
1250 pchan_bone->flag |= BONE_TRANSFORM_MIRROR;
1251
1252 /* In this case we can do target-less IK grabbing. */
1253 if (t->mode == TFM_TRANSLATION) {
1255 if (data == nullptr) {
1256 continue;
1257 }
1258 mul_v3_m4v3(data->grabtarget, flip_mtx, td->loc);
1259 if (pid) {
1260 /* TODO(@germano): Relative Mirror support. */
1261 }
1262 data->flag |= CONSTRAINT_IK_AUTO;
1263 /* Add a temporary auto IK constraint here, as we will only temporarily active this
1264 * target-less bone during transform. (Target-less IK constraints are treated as if they are
1265 * disabled unless they are transformed).
1266 * Only do this for targetless IK though, AutoIK already added a constraint in
1267 * pose_grab_with_ik_add() beforehand. */
1268 if ((data->flag & CONSTRAINT_IK_TEMP) == 0) {
1270 Main *bmain = CTX_data_main(t->context);
1272 }
1273 }
1274
1275 if (pid) {
1276 pid++;
1277 }
1278 }
1279}
1280
1282{
1283 bPoseChannel *pchan = pid->pchan;
1284 copy_v3_v3(pchan->loc, pid->orig.loc);
1285 copy_v3_v3(pchan->scale, pid->orig.scale);
1286 pchan->curve_in_x = pid->orig.curve_in_x;
1287 pchan->curve_out_x = pid->orig.curve_out_x;
1288 pchan->roll1 = pid->orig.roll1;
1289 pchan->roll2 = pid->orig.roll2;
1290
1291 if (pchan->rotmode > 0) {
1292 copy_v3_v3(pchan->eul, pid->orig.eul);
1293 }
1294 else if (pchan->rotmode == ROT_MODE_AXISANGLE) {
1295 copy_v3_v3(pchan->rotAxis, pid->orig.axis_angle);
1296 pchan->rotAngle = pid->orig.axis_angle[3];
1297 }
1298 else {
1299 copy_qt_qt(pchan->quat, pid->orig.quat);
1300 }
1301}
1302
1304{
1305 bPose *pose = tc->poseobj->pose;
1306
1307 if (!(pose->flag & POSE_MIRROR_EDIT)) {
1308 return;
1309 }
1310
1311 for (PoseInitData_Mirror *pid = static_cast<PoseInitData_Mirror *>(tc->custom.type.data);
1312 pid->pchan;
1313 pid++)
1314 {
1316 }
1317}
1318
1319/* Given the transform mode `tmode` return a Vector of RNA paths that were possibly modified during
1320 * that transformation. */
1322 const eTfmMode tmode,
1323 ToolSettings *toolsettings,
1324 const StringRef rotation_path,
1325 const bool targetless_ik,
1326 const bool is_connected,
1327 const bool transforming_more_than_one_bone)
1328{
1329 Vector<RNAPath> rna_paths;
1330
1331 /* Handle the cases where we always need to key location, regardless of
1332 * transform mode. */
1333 if (transforming_more_than_one_bone &&
1335 {
1336 rna_paths.append({"location"});
1337 }
1338 else if (toolsettings->transform_pivot_point == V3D_AROUND_CURSOR) {
1339 rna_paths.append({"location"});
1340 }
1341
1342 /* Handle the transform-mode-specific cases. */
1343 switch (tmode) {
1344 case TFM_TRANSLATION:
1345 /* NOTE: this used to *not* add location if we were doing targetless IK.
1346 * However, that was wrong because of the following situations:
1347 *
1348 * 1. The user can grab the *base* of the bone chain, in which case that
1349 * bone's location does indeed get its location moved, and thus needs
1350 * its location keyed.
1351 * 2. The user can also have bones outside of a bone chain selected, in
1352 * which case they get moved normally, and thus those
1353 * outside-of-a-chain bones need their location keyed.
1354 *
1355 * So for now we're just adding location regardless of targetless IK. This
1356 * unfortunately means that location gets keyed on a lot of bones that
1357 * don't need it when doing targetless ik, but that's better than
1358 * *failing* to key bones that *do* need it. Additionally, case 2 above
1359 * means that outside-of-a-chain bones also get their *rotation*
1360 * unnecessarily keyed when doing targetless IK on another selected chain.
1361 *
1362 * Being precise and only adding location/rotation for the bones that
1363 * really need it when doing targetless IK will require more information
1364 * to be passed to this function.
1365 *
1366 * TODO: get the needed information and make this more precise. */
1367 if (!is_connected) {
1368 rna_paths.append_non_duplicates({"location"});
1369 }
1370 if (targetless_ik) {
1371 rna_paths.append({rotation_path});
1372 }
1373 break;
1374
1375 case TFM_ROTATION:
1376 case TFM_TRACKBALL:
1377 if ((toolsettings->transform_flag & SCE_XFORM_AXIS_ALIGN) == 0) {
1378 rna_paths.append({rotation_path});
1379 }
1380 break;
1381
1382 case TFM_RESIZE:
1383 if ((toolsettings->transform_flag & SCE_XFORM_AXIS_ALIGN) == 0) {
1384 rna_paths.append({"scale"});
1385 }
1386 break;
1387
1388 default:
1389 break;
1390 }
1391 return rna_paths;
1392}
1393
1395 Scene *scene,
1396 Object *ob,
1397 short targetless_ik,
1398 const eTfmMode tmode,
1399 const bool transforming_more_than_one_bone)
1400{
1401
1402 bPose *pose = ob->pose;
1403 for (bPoseChannel &pchan : pose->chanbase) {
1404 Bone *pchan_bone = pchan.bone_get(*ob);
1405 if ((pchan.runtime.flag & POSE_RUNTIME_TRANSFORM) == 0 &&
1406 !((pose->flag & POSE_MIRROR_EDIT) && (pchan_bone->flag & BONE_TRANSFORM_MIRROR)))
1407 {
1408 continue;
1409 }
1410
1411 Vector<RNAPath> rna_paths;
1412 const StringRefNull rotation_path = animrig::get_rotation_mode_path(
1413 eRotationModes(pchan.rotmode));
1414
1416 const bool is_connected = pchan_bone->parent != nullptr &&
1417 (pchan_bone->flag & BONE_CONNECTED);
1419 scene->toolsettings,
1420 rotation_path,
1421 targetless_ik,
1422 is_connected,
1423 transforming_more_than_one_bone);
1424 }
1425 else {
1426 rna_paths = {{"location"}, {rotation_path}, {"scale"}};
1427 }
1428
1429 animrig::autokeyframe_pose_channel(C, scene, ob, &pchan, rna_paths.as_span(), targetless_ik);
1430 }
1431}
1432
1434{
1435 if (t->mode == TFM_BONESIZE) {
1436 /* Handle the exception where for TFM_BONESIZE in edit mode we pretend to be
1437 * in pose mode (to use bone orientation matrix),
1438 * in that case we have to do mirroring as well. */
1440 Object *ob = tc->poseobj;
1441 bArmature *arm = id_cast<bArmature *>(ob->data);
1442 if (ob->mode == OB_MODE_EDIT) {
1443 if (arm->flag & ARM_MIRROR_EDIT) {
1444 if (t->state != TRANS_CANCEL) {
1446 }
1447 else {
1448 restoreBones(tc);
1449 }
1450 }
1451 }
1452 else if (ob->mode == OB_MODE_POSE) {
1453 /* Actually support #TFM_BONESIZE in pose-mode as well. */
1455 bPose *pose = ob->pose;
1456 if (arm->flag & ARM_MIRROR_EDIT || pose->flag & POSE_MIRROR_EDIT) {
1458 }
1459 }
1460 }
1461 }
1462 else {
1463 Set<Object *> motionpath_updates;
1465 Object *ob = tc->poseobj;
1466 bPose *pose = ob->pose;
1467
1468 if (pose->flag & POSE_MIRROR_EDIT) {
1469 if (t->state != TRANS_CANCEL) {
1471 }
1472 else {
1474 }
1475 }
1476
1477 /* If animtimer is running, and the object already has animation data,
1478 * check if the auto-record feature means that we should record 'samples'
1479 * (i.e. un-editable animation values).
1480 *
1481 * Context is needed for keying set poll() functions.
1482 */
1483
1484 /* TODO: autokeyframe calls need some setting to specify to add samples
1485 * (FPoints) instead of keyframes? */
1486 if ((t->animtimer) && (t->context) && animrig::is_autokey_on(t->scene)) {
1487
1488 /* XXX: this currently doesn't work, since flags aren't set yet! */
1489 int targetless_ik = (t->flag & T_AUTOIK);
1490
1491 animrecord_check_state(t, &ob->id);
1492 autokeyframe_pose(t->context, t->scene, ob, targetless_ik, t->mode, t->data_len_all > 1);
1493 }
1494
1495 if (motionpath_need_update_pose(t->scene, ob)) {
1496 motionpath_updates.add(ob);
1497 /* We have to tag the pose for recalc so `animviz_build_motionpath_targets` will gather the
1498 * targets. */
1500 }
1501
1503 }
1504 if (!motionpath_updates.is_empty()) {
1505 Depsgraph *evaluated_dg = CTX_data_ensure_evaluated_depsgraph(t->context);
1506 Vector<MPathTarget> targets;
1507 for (Object *ob : motionpath_updates) {
1509 }
1510 animviz_update_motionpath_preview(*evaluated_dg, targets);
1511 }
1512 }
1513}
1514
1516
1517/* -------------------------------------------------------------------- */
1520
1522 bPoseChannel &pchan,
1523 const int mode,
1524 const short around)
1525{
1526 animrig::pose_bone_descendent_iterator(pose_ob, pchan, [&](bPoseChannel &child) {
1527 if (&pchan == &child) {
1528 return;
1529 }
1530 Bone *bone = child.bone_get(pose_ob);
1531 if ((bone->flag & BONE_HINGE) && (bone->flag & BONE_CONNECTED)) {
1533 }
1534 else if ((child.runtime.flag & POSE_RUNTIME_TRANSFORM) &&
1536 {
1538 }
1539 else {
1541 }
1542 });
1543}
1544
1545void transform_convert_pose_transflags_update(Object *ob, const int mode, const short around)
1546{
1547 bArmature *arm = id_cast<bArmature *>(ob->data);
1548
1549 for (bPoseChannel &pchan : ob->pose->chanbase) {
1550 if (animrig::bone_is_visible(arm, {&pchan, pchan.bone_get(*ob)})) {
1551 if (pchan.flag & POSE_SELECTED) {
1553 }
1554 else {
1556 }
1557
1560 }
1561 else {
1563 }
1564 }
1565
1566 /* Make sure no bone can be transformed when a parent is transformed. */
1568 for (bPoseChannel &pchan : ob->pose->chanbase) {
1569 if (pchan.runtime.flag & POSE_RUNTIME_TRANSFORM) {
1570 pose_channel_children_clear_transflag(*ob, pchan, mode, around);
1571 }
1572 }
1573 }
1574}
1575
1577{
1578 bPoseChannel *chanlist[256];
1580 int segcount, apply = 0;
1581
1582 /* Now we got a difficult situation... we have to find the
1583 * target-less IK pchans, and apply transformation to the all
1584 * pchans that were in the chain. */
1585
1586 for (bPoseChannel &pchan : ob->pose->chanbase) {
1587 data = has_targetless_ik(&pchan);
1588 if (data && (data->flag & CONSTRAINT_IK_AUTO)) {
1589
1590 /* Fill the array with the bones of the chain (`armature.cc` does same, keep it synced). */
1591 segcount = 0;
1592
1593 /* Exclude tip from chain? */
1594 bPoseChannel *parchan = (data->flag & CONSTRAINT_IK_TIP) ? &pchan : pchan.parent;
1595
1596 /* Find the chain's root & count the segments needed. */
1597 for (; parchan; parchan = parchan->parent) {
1598 chanlist[segcount] = parchan;
1599 segcount++;
1600
1601 if (segcount == data->rootbone || segcount > 255) {
1602 break; /* 255 is weak. */
1603 }
1604 }
1605 for (; segcount; segcount--) {
1606 float mat[4][4];
1607
1608 /* `pose_mat(b) = pose_mat(b-1) * offs_bone * channel * constraint * IK`. */
1609 /* We put in channel the entire result of: `mat = (channel * constraint * IK)`. */
1610 /* `pose_mat(b) = pose_mat(b-1) * offs_bone * mat`. */
1611 /* `mat = pose_mat(b) * inv(pose_mat(b-1) * offs_bone)`. */
1612
1613 parchan = chanlist[segcount - 1];
1614 /* Ensures it gets an auto key inserted. */
1616
1617 BKE_armature_mat_pose_to_bone({parchan, parchan->bone_get(*ob)}, parchan->pose_mat, mat);
1618 /* Apply and decompose, doesn't work for constraints or non-uniform scale well. */
1619 {
1620 float rmat3[3][3], scale[3];
1621
1622 /* Extract scale, then normalize mat so it is pure rotation. */
1623 normalize_m4_ex(mat, scale);
1624 copy_m3_m4(rmat3, mat);
1625
1626 /* Rotation. */
1627 /* #22409 is partially caused by this, as slight numeric error introduced during
1628 * the solving process leads to locked-axis values changing. However, we cannot modify
1629 * the values here, or else there are huge discrepancies between IK-solver (interactive)
1630 * and applied poses. */
1631 BKE_pchan_mat3_to_rot(parchan, rmat3, false);
1632
1633 /* Scale causes problems with some constraints (so apply only if needed). */
1634 if (data->flag & CONSTRAINT_IK_STRETCH) {
1635 copy_v3_v3(parchan->scale, scale);
1636 }
1637
1638 /* Causes problems with some constraints (e.g. child-of), so disable this
1639 * as it is IK shouldn't affect location directly. */
1640 // copy_v3_v3(parchan->loc, mat[3]);
1641 }
1642 }
1643
1644 apply = 1;
1645 data->flag &= ~CONSTRAINT_IK_AUTO;
1646 }
1647 }
1648
1649 return apply;
1650}
1651
1653static void pose_grab_with_ik_clear(Main *bmain, Object *ob)
1654{
1656 bConstraint *con, *next;
1657 bool relations_changed = false;
1658
1659 for (bPoseChannel &pchan : ob->pose->chanbase) {
1660 /* Clear all temporary lock flags. */
1662
1664
1665 /* Remove all temporary IK-constraints added. */
1666 for (con = pchan.constraints.first(); con; con = next) {
1667 next = con->next;
1668 if (con->type == CONSTRAINT_TYPE_KINEMATIC) {
1669 data = static_cast<bKinematicConstraint *>(con->data);
1670 if (data->flag & CONSTRAINT_IK_TEMP) {
1671 relations_changed = true;
1672
1673 /* `iTaSC` needs clear for removed constraints. */
1674 BIK_clear_data(ob->pose);
1675
1676 BLI_remlink(&pchan.constraints, con);
1677 MEM_delete(static_cast<bKinematicConstraint *>(con->data));
1678 MEM_delete(con);
1679 continue;
1680 }
1681 pchan.constflag |= PCHAN_HAS_IK;
1682 if (data->tar == nullptr || (data->tar->type == OB_ARMATURE && data->subtarget[0] == 0)) {
1684 }
1685 }
1686 }
1687 }
1688
1689 if (relations_changed) {
1690 /* TODO(sergey): Consider doing partial update only. */
1692 }
1693}
1694
1696{
1697 Object *ob;
1698
1699 if (t->mode == TFM_BONESIZE) {
1700 /* Handle the exception where for TFM_BONESIZE in edit mode we pretend to be
1701 * in pose mode (to use bone orientation matrix),
1702 * in that case we don't do operations like auto-keyframing. */
1704 ob = tc->poseobj;
1706 }
1707 }
1708 else {
1709 const bool canceled = (t->state == TRANS_CANCEL);
1710
1711 if (animrig::is_autokey_on(t->scene) && !canceled) {
1713 }
1714
1715 Set<Object *> motionpath_updates;
1716
1718 short targetless_ik = 0;
1719
1720 ob = tc->poseobj;
1721
1722 if ((t->flag & T_AUTOIK) && (t->options & CTX_AUTOCONFIRM)) {
1723 /* When running transform non-interactively (operator exec),
1724 * we need to update the pose otherwise no updates get called during
1725 * transform and the auto-IK is not applied. see #26164. */
1726 Object *pose_ob = tc->poseobj;
1727 BKE_pose_where_is(t->depsgraph, t->scene, pose_ob);
1728 }
1729
1730 /* Set POSE_RUNTIME_TRANSFORM flags for auto-key, gizmo draw might have changed them. */
1731 if (!canceled && (t->mode != TFM_DUMMY)) {
1733 }
1734
1735 /* If target-less IK grabbing, we calculate the pchan transforms and clear flag. */
1736 if (!canceled && t->mode == TFM_TRANSLATION) {
1737 targetless_ik = apply_targetless_ik(ob);
1738 }
1739 else {
1740 /* Not forget to clear the auto flag. */
1741 for (bPoseChannel &pchan : ob->pose->chanbase) {
1743 if (data) {
1744 data->flag &= ~CONSTRAINT_IK_AUTO;
1745 }
1746 }
1747 }
1748
1749 if (t->mode == TFM_TRANSLATION) {
1750 Main *bmain = CTX_data_main(t->context);
1751 pose_grab_with_ik_clear(bmain, ob);
1752 }
1753
1754 /* Automatic inserting of keys and unkeyed tagging -
1755 * only if transform wasn't canceled (or #TFM_DUMMY). */
1756 if (!canceled && (t->mode != TFM_DUMMY)) {
1757 autokeyframe_pose(C, t->scene, ob, targetless_ik, t->mode, t->data_len_all > 1);
1759 }
1760 else {
1762 }
1763
1764 if (t->mode != TFM_DUMMY && motionpath_need_update_pose(t->scene, ob)) {
1765 motionpath_updates.add(ob);
1767 }
1768 }
1769 /* This needs to happen before the call to `ED_pose_recalculate_paths` because that clears the
1770 * `ANIMVIZ_RECALC_PATHS` flag which makes the `animviz_build_motionpath_targets` find no
1771 * targets. */
1772 Vector<MPathTarget> targets;
1773 for (Object *ob : motionpath_updates) {
1775 }
1777
1778 if (!canceled) {
1779 /* Update motion paths once for all transformed bones in an object. */
1780 for (Object *ob : motionpath_updates) {
1782 }
1783 }
1784 }
1785}
1786
1788
1790 /*flags*/ (T_EDIT | T_POINTS),
1791 /*create_trans_data*/ createTransArmatureVerts,
1792 /*recalc_data*/ recalcData_edit_armature,
1793 /*special_aftertrans_update*/ nullptr,
1794};
1795
1797 /*flags*/ 0,
1798 /*create_trans_data*/ createTransPose,
1799 /*recalc_data*/ recalcData_pose,
1800 /*special_aftertrans_update*/ special_aftertrans_update__pose,
1801};
1802
1803} // namespace blender::ed::transform
Functions and classes to work with Actions.
Functions to deal with Armatures.
C++ functions to deal with Armature collections (i.e. the successor of bone layers).
Functions to insert, delete or modify keyframes.
Helper functions for animation to interact with the RNA system.
Blender kernel action and pose functionality.
#define PSEUDOINVERSE_EPSILON
#define mul_m3_series(...)
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define BLI_assert_unreachable()
#define ELEM(...)
bool add(const Key &key)
Definition BLI_set.hh:248
bool is_empty() const
Definition BLI_set.hh:614
void append(const T &value)
void reserve(const int64_t min_capacity)
void append_non_duplicates(const T &value)
Span< T > as_span() const
#define rot(x, k)
float3x3 scale(float3x3 mat, float2 scale)
bool is_autokey_on(const Scene *scene)
bool autokeyframe_cfra_can_key(const Scene *scene, ID *id)
StringRefNull get_rotation_mode_path(eRotationModes rotation_mode)
Definition anim_rna.cc:109
bool bone_is_visible(const bArmature *armature, const Bone *bone)
bool is_keying_flag(const Scene *scene, eKeying_Flag flag)
void autokeyframe_pose_channel(bContext *C, Scene *scene, Object *ob, bPoseChannel *pose_channel, Span< RNAPath > rna_paths, short targetless_ik)
void pose_bone_descendent_iterator(Object &pose_ob, bPoseChannel &pchan, FunctionRef< void(bPoseChannel &child_bone)> callback)
PChanBoneT< bPoseChannel, Bone > PChanBone
Definition BKE_pose.hh:88
PChanBoneT< const bPoseChannel, const Bone > PChanBoneConst
Definition BKE_pose.hh:89
static void autokeyframe_pose(bContext *C, Scene *scene, Object *ob, short targetless_ik, const eTfmMode tmode, const bool transforming_more_than_one_bone)
static void pose_grab_with_ik_clear(Main *bmain, Object *ob)
TransConvertTypeInfo TransConvertType_Pose
static void pose_mirror_info_restore(const PoseInitData_Mirror *pid)
static void pose_channel_children_clear_transflag(Object &pose_ob, bPoseChannel &pchan, const int mode, const short around)
void animrecord_check_state(TransInfo *t, ID *id)
static void add_pose_transdata(TransInfo *t, bPoseChannel *pchan, Object *ob, TransData *td, TransDataExtension *td_ext)
static bKinematicConstraint * has_targetless_ik(bPoseChannel *pchan)
static void recalcData_edit_armature(TransInfo *t)
static Vector< RNAPath > get_affected_rna_paths_from_transform_mode(const eTfmMode tmode, ToolSettings *toolsettings, const StringRef rotation_path, const bool targetless_ik, const bool is_connected, const bool transforming_more_than_one_bone)
static void free_bone_init_data(TransInfo *, TransDataContainer *, TransCustomData *custom_data)
void transform_around_single_fallback(TransInfo *t)
bool gimbal_axis_pose(Object *ob, const bPoseChannel *pchan, float gmat[3][3])
static bConstraint * add_temporary_ik_constraint(bPoseChannel *pchan, bKinematicConstraint *targetless_con)
void transform_snap_project_individual_apply(TransInfo *t)
static void special_aftertrans_update__pose(bContext *C, TransInfo *t)
bool constraints_list_needinv(TransInfo *t, ListBaseT< bConstraint > *list)
static void recalcData_pose(TransInfo *t)
static short pose_grab_with_ik(Main *bmain, Object *ob)
static void pose_mirror_info_init(PoseInitData_Mirror *pid, bke::PChanBone pchanbone, bke::PChanBoneConst pchanbone_orig, bool is_mirror_relative)
static void update_deg_with_temporary_ik(Main *bmain, Object *ob)
static void free_pose_transdata(TransInfo *t, TransDataContainer *tc, TransCustomData *custom_data)
void transform_convert_pose_transflags_update(Object *ob, int mode, short around)
static short pose_grab_with_ik_children(Object &ob, Bone *bone)
static void restoreBones(TransDataContainer *tc)
void transform_autoik_update(TransInfo *t, short mode)
static short apply_targetless_ik(Object *ob)
static bool motionpath_need_update_pose(Scene *scene, Object *ob)
static short pose_grab_with_ik_add(Object &ob, bke::PChanBone pchanbone)
static void createTransArmatureVerts(bContext *, TransInfo *t)
static void pose_transform_mirror_update(TransInfo *t, TransDataContainer *tc, Object *ob)
static void restoreMirrorPoseBones(TransDataContainer *tc)
static void createTransPose(bContext *, TransInfo *t)
TransConvertTypeInfo TransConvertType_EditArmature
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
bArmature * BKE_armature_from_object(Object *ob)
void BKE_pchan_mat3_to_rot(bPoseChannel *pchan, const float mat[3][3], bool use_compat)
void mat4_to_loc_rot_size(float loc[3], float rot[3][3], float size[3], const float wmat[4][4])
void animviz_clear_motionpath_preview(Span< MPathTarget > targets)
struct bConstraint * BKE_constraint_add_for_pose(struct Object *ob, struct bPoseChannel *pchan, const char *name, eBConstraint_Types type)
MINLINE void copy_v3_v3(float r[3], const float a[3])
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void copy_m3_m4(float m1[3][3], const float m2[4][4])
void copy_m3_m3(float m1[3][3], const float m2[3][3])
void ED_armature_edit_transform_mirror_update(Object *obedit)
float angle_compat_rad(float angle, float angle_compat)
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])
void normalize_m3(float R[3][3]) ATTR_NONNULL()
void BKE_pchan_protected_location_set(bPoseChannel *pchan, const float location[3])
@ V3D_AROUND_LOCAL_ORIGINS
void mul_m4_m4m4(float R[4][4], const float A[4][4], const float B[4][4])
void BKE_pchan_protected_scale_set(bPoseChannel *pchan, const float scale[3])
void rotation_between_vecs_to_quat(float q[4], const float v1[3], const float v2[3])
bool invert_m4(float mat[4][4])
void BKE_pchan_to_mat4(bke::PChanBoneConst pchanbone, float r_chanmat[4][4])
void BKE_pose_where_is(Depsgraph *depsgraph, Scene *scene, Object *ob)
void BIK_clear_data(struct bPose *pose)
void BKE_pose_channel_transform_orientation(const bArmature *arm, const bPoseChannel *pose_bone, float r_pose_orientation[3][3])
void MEM_delete_void(void *vmemh)
void mul_qt_v3(const float q[4], float r[3])
void animviz_build_motionpath_targets(Object *ob, Vector< MPathTarget > &r_targets)
EditBone * ED_armature_ebone_get_mirrored(const ListBaseT< EditBone > *edbo, EditBone *ebo)
void BKE_pchan_protected_rotation_set(bPoseChannel *pchan, const float mat[3][3])
void BKE_pose_channel_transform_location(const bArmature *arm, const bPoseChannel *pose_bone, float r_pose_space_pivot[3])
bPoseChannel * BKE_pose_channel_get_mirrored(const bPose *pose, const char *name) ATTR_WARN_UNUSED_RESULT
void ED_armature_ebone_to_mat3(EditBone *ebone, float r_mat[3][3])
void BKE_armature_mat_pose_to_bone(bke::PChanBoneConst pchanbone, const float inmat[4][4], float outmat[4][4])
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
void copy_qt_qt(float q[4], const float a[4])
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 ANIM_deselect_keys_in_animation_editors(bContext *C)
Definition anim_deps.cc:487
Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C, bool rna_write_check=false)
void unit_m4(float m[4][4])
void normalize_m4_ex(float R[4][4], float r_scale[3]) ATTR_NONNULL()
MINLINE float normalize_v3(float n[3])
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
Main * CTX_data_main(const bContext *C)
@ POSE_TRANSFORM_AROUND_CUSTOM_TX
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
void pseudoinverse_m3_m3(float inverse[3][3], const float mat[3][3], float epsilon)
void DEG_relations_tag_update(Main *bmain)
bool BKE_pose_is_bonecoll_visible(const bArmature *arm, const bPoseChannel *pchan) ATTR_WARN_UNUSED_RESULT
@ AUTOKEY_FLAG_INSERTNEEDED
void mul_m3_m3m3(float R[3][3], const float A[3][3], const float B[3][3])
float ED_armature_ebone_roll_to_vector(const EditBone *bone, const float align_axis[3], const bool axis_only)
MINLINE float len_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
@ MOTIONPATH_BAKE_HAS_PATHS
@ POSE_RUNTIME_HINGE_CHILD_TRANSFORM
@ POSE_RUNTIME_TRANSFORM_CHILD
void BKE_bone_parent_transform_calc_from_pchan(bke::PChanBoneConst pchanbone, BoneParentTransform *r_bpt)
bPoseChannel * BKE_pose_channel_find_name(const bPose *pose, const char *name)
void ED_pose_recalculate_paths(bContext *C, Scene *scene, Object *ob)
Definition pose_edit.cc:146
void animviz_update_motionpath_preview(Depsgraph &depsgraph, Span< MPathTarget > targets)
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
struct Bone * parent
struct Bone * next
ListBaseT< Bone > childbase
struct bPose * pose
struct ToolSettings * toolsettings
eMotionPath_BakeFlag path_bakeflag
eAnimViz_RecalcFlags recalc
ListBaseT< EditBone > * edbo
struct bConstraint * next
bPoseChannelRuntimeFlag flag
struct bPoseChannel_Runtime runtime
struct bPoseChannel * parent
ListBaseT< struct bConstraint > constraints
ePchan_ConstFlag constflag
bAnimVizSettings avs
ListBaseT< bPoseChannel > chanbase
PoseChannelT * pchan
Definition BKE_pose.hh:61
struct blender::ed::transform::PoseInitData_Mirror::@065225275370354271055075174045245017130227026101 orig
TransConvertTypeInfo * data_type
Definition transform.hh:836
#define FOREACH_TRANS_DATA_CONTAINER(t, th)
Definition transform.hh:48
conversion and adaptation of different datablocks to a common struct.