Blender V5.3
pose_transform.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 "DNA_action_types.h"
10#include "DNA_anim_types.h"
11#include "DNA_armature_types.h"
13#include "DNA_object_types.h"
14#include "DNA_scene_types.h"
15
16#include "MEM_guardedalloc.h"
17
18#include "BLI_listbase.hh"
19#include "BLI_math_matrix_c.hh"
21#include "BLI_math_vector_c.hh"
22#include "BLI_path_utils.hh"
23#include "BLI_string_utf8.hh"
24#include "BLI_string_utils.hh"
25
26#include "BKE_action.hh"
27#include "BKE_animsys.hh"
28#include "BKE_appdir.hh"
29#include "BKE_armature.hh"
31#include "BKE_blendfile.hh"
32#include "BKE_context.hh"
33#include "BKE_idprop.hh"
34#include "BKE_layer.hh"
35#include "BKE_lib_query.hh"
36#include "BKE_main.hh"
37#include "BKE_object.hh"
38#include "BKE_pose.hh"
39#include "BKE_report.hh"
40
41#include "DEG_depsgraph.hh"
43
44#include "RNA_access.hh"
45#include "RNA_define.hh"
46#include "RNA_prototypes.hh"
47
48#include "WM_api.hh"
49#include "WM_types.hh"
50
51#include "ED_anim_api.hh"
53#include "ED_armature.hh"
54#include "ED_keyframing.hh"
55#include "ED_screen.hh"
56
57#include "ANIM_armature.hh"
59#include "ANIM_keyframing.hh"
60#include "ANIM_keyingsets.hh"
61
63#include "UI_resources.hh"
64
65#include "armature_intern.hh"
66
67#include "CLG_log.h"
68
69static CLG_LogRef LOG_POSE_PASTE = {"pose.paste"};
70
71namespace blender {
72
73/* -------------------------------------------------------------------- */
76
77static void pose_copybuffer_filepath_get(char filepath[FILE_MAX], size_t filepath_maxncpy)
78{
79 BLI_path_join(filepath, filepath_maxncpy, BKE_tempdir_base(), "copybuffer_pose.blend");
80}
81
83
84/* -------------------------------------------------------------------- */
87
88/* helper for apply_armature_pose2bones - fixes parenting of objects
89 * that are bone-parented to armature */
90static void applyarmature_fix_boneparents(const bContext *C, Scene *scene, Object *armob)
91{
92 /* Depsgraph has been ensured to be evaluated at the beginning of the operator.
93 *
94 * Must not evaluate depsgraph here yet, since this will ruin object matrix which we want to
95 * preserve after other changes has been done in the operator.
96 *
97 * TODO(sergey): This seems very similar to `ignore_parent_tx()`, which was now ensured to work
98 * quite reliably. Can we de-duplicate the code? Or at least verify we don't need an extra logic
99 * in this function. */
100 Depsgraph *depsgraph = CTX_data_depsgraph_pointer(C);
101 Main *bmain = CTX_data_main(C);
102
103 /* go through all objects in database */
104 for (Object *ob = bmain->objects.first(); ob; ob = static_cast<Object *>(ob->id.next)) {
105 /* if parent is bone in this armature, apply corrections */
106 if ((ob->parent == armob) && (ob->partype == PARBONE)) {
107 /* apply current transform from parent (not yet destroyed),
108 * then calculate new parent inverse matrix
109 */
110 BKE_object_apply_mat4(ob, ob->object_to_world().ptr(), false, false);
111
112 invert_m4_m4(ob->parentinv, BKE_object_calc_parent(depsgraph, scene, ob).ptr());
113 }
114 }
115}
116
117/* Sets the bone head, tail and roll to match the supplied parameters. */
119 const float pose_mat[4][4],
120 const float new_tail[3],
121 float r_new_arm_mat[4][4])
122{
123 /* Simply copy the head/tail values from pchan over to curbone. */
124 copy_v3_v3(curbone->head, pose_mat[3]);
125 copy_v3_v3(curbone->tail, new_tail);
126
127 /* Fix roll:
128 * 1. find auto-calculated roll value for this bone now
129 * 2. remove this from the 'visual' y-rotation
130 */
131 {
132 float premat[3][3], pmat[3][3];
133 float delta[3];
134
135 /* Obtain new auto y-rotation. */
136 sub_v3_v3v3(delta, curbone->tail, curbone->head);
137
138 copy_m3_m4(pmat, pose_mat);
139 mat3_vec_to_roll(pmat, delta, &curbone->roll);
140
141 /* Compute new rest pose matrix if requested. */
142 if (r_new_arm_mat) {
143 vec_roll_to_mat3(delta, curbone->roll, premat);
144 copy_m4_m3(r_new_arm_mat, premat);
145 copy_v3_v3(r_new_arm_mat[3], pose_mat[3]);
146 }
147 }
148}
149
150/* Copy properties over from pchan to curbone and reset channels. */
152 bke::PChanBone pchanbone,
153 const bPoseChannel *pchan_eval)
154{
155 bPoseChannel *pchan = pchanbone.pchan;
156 const Bone *pchan_bone = pchanbone.bone;
157
158 /* Combine pose and rest values for bendy bone settings,
159 * then clear the pchan values (so we don't get a double-up).
160 */
161 if (pchan_bone->segments > 1) {
162 /* Combine rest/pose values. */
163 curbone->curve_in_x += pchan_eval->curve_in_x;
164 curbone->curve_in_z += pchan_eval->curve_in_z;
165 curbone->curve_out_x += pchan_eval->curve_out_x;
166 curbone->curve_out_z += pchan_eval->curve_out_z;
167 curbone->roll1 += pchan_eval->roll1;
168 curbone->roll2 += pchan_eval->roll2;
169 curbone->ease1 += pchan_eval->ease1;
170 curbone->ease2 += pchan_eval->ease2;
171
172 mul_v3_v3(curbone->scale_in, pchan_eval->scale_in);
173 mul_v3_v3(curbone->scale_out, pchan_eval->scale_out);
174
175 /* Reset pose values. */
176 pchan->curve_in_x = pchan->curve_out_x = 0.0f;
177 pchan->curve_in_z = pchan->curve_out_z = 0.0f;
178 pchan->roll1 = pchan->roll2 = 0.0f;
179 pchan->ease1 = pchan->ease2 = 0.0f;
180
181 copy_v3_fl(pchan->scale_in, 1.0f);
182 copy_v3_fl(pchan->scale_out, 1.0f);
183 }
184
185 /* Clear transform values for pchan. */
186 zero_v3(pchan->loc);
187 zero_v3(pchan->eul);
188 unit_qt(pchan->quat);
189 unit_axis_angle(pchan->rotAxis, &pchan->rotAngle);
190 pchan->scale[0] = pchan->scale[1] = pchan->scale[2] = 1.0f;
191}
192
193/* Adjust the current edit position of the bone using the pose space matrix. */
195 bPoseChannel *pchan,
196 const float delta_mat[4][4],
197 float r_new_arm_mat[4][4])
198{
199 EditBone *curbone = ED_armature_ebone_find_name(arm->edbo, pchan->name);
200 float delta[3], new_tail[3], premat[3][3], new_pose[4][4];
201
202 /* Current orientation matrix. */
203 sub_v3_v3v3(delta, curbone->tail, curbone->head);
204 vec_roll_to_mat3(delta, curbone->roll, premat);
205
206 /* New location and orientation. */
207 mul_m4_m4m3(new_pose, delta_mat, premat);
208 mul_v3_m4v3(new_pose[3], delta_mat, curbone->head);
209 mul_v3_m4v3(new_tail, delta_mat, curbone->tail);
210
211 applyarmature_set_edit_position(curbone, new_pose, new_tail, r_new_arm_mat);
212}
213
214/* Data about parent position for Apply To Selected mode. */
217
218 /* New rest position of the bone with scale included. */
219 float new_rest_mat[4][4];
220 /* New arm_mat of the bone == new_rest_mat without scale. */
221 float new_arm_mat[4][4];
222};
223
224/* Recursive walk for Apply To Selected mode; pstate nullptr unless child of an applied bone. */
226 bPose *pose,
227 bPose *pose_eval,
228 Bone *bone,
229 Span<PointerRNA> selected,
231{
233 const bPoseChannel *pchan_eval = BKE_pose_channel_find_name(pose_eval, bone->name);
234
235 if (!pchan || !pchan_eval) {
236 return;
237 }
238
239 ApplyArmature_ParentState new_pstate{};
240 new_pstate.bone = bone;
241
242 if (std::find_if(selected.begin(),
243 selected.end(),
244 [&](const PointerRNA &ptr) { return ptr.data == pchan; }) != selected.end())
245 {
246 /* SELECTED BONE: Snap to final pose transform minus un-applied parent effects.
247 *
248 * I.e. bone position with accumulated parent effects but no local
249 * transformation will match the original final pose_mat.
250 *
251 * Pose channels are reset as expected.
252 */
253 EditBone *curbone = ED_armature_ebone_find_name(arm->edbo, pchan->name);
254 BoneParentTransform invparent;
255 float new_tail[3];
256
257 if (pchan->parent) {
258 BoneParentTransform old_bpt, new_bpt;
259 float offs_bone[4][4];
260
261 /* Parent effects on the bone transform that have to be removed. */
262 BKE_bone_offset_matrix_get(bone, offs_bone);
264 bone->inherit_scale_mode,
265 offs_bone,
266 bone->parent->arm_mat,
267 pchan_eval->parent->pose_mat,
268 &old_bpt);
269
270 /* Applied parent effects that have to be kept, if any. */
271 float (*new_parent_pose)[4] = pstate ? pstate->new_rest_mat : bone->parent->arm_mat;
273 bone->inherit_scale_mode,
274 offs_bone,
275 bone->parent->arm_mat,
276 new_parent_pose,
277 &new_bpt);
278
280 BKE_bone_parent_transform_combine(&new_bpt, &old_bpt, &invparent);
281 }
282 else {
284 }
285
286 /* Apply change without inherited unapplied parent transformations. */
287 BKE_bone_parent_transform_apply(&invparent, pchan_eval->pose_mat, new_pstate.new_rest_mat);
288
289 copy_v3_fl3(new_tail, 0.0, bone->length, 0.0);
290 mul_m4_v3(new_pstate.new_rest_mat, new_tail);
291
293 curbone, new_pstate.new_rest_mat, new_tail, new_pstate.new_arm_mat);
294 applyarmature_transfer_properties(curbone, {pchan, bone}, pchan_eval);
295
296 pstate = &new_pstate;
297 }
298 else if (pstate) {
299 /* UNSELECTED CHILD OF SELECTED: Include applied parent effects.
300 *
301 * The inherited transform of applied (selected) bones is baked
302 * into the rest pose so that the final bone position doesn't
303 * change.
304 *
305 * Pose channels are not changed, with the exception of the inherited
306 * applied parent scale being baked into the location pose channel.
307 */
309 float offs_bone[4][4], delta[4][4], old_chan_loc[3];
310
311 /* Include applied parent effects. */
312 BKE_bone_offset_matrix_get(bone, offs_bone);
314 bone->inherit_scale_mode,
315 offs_bone,
316 pstate->bone->arm_mat,
317 pstate->new_rest_mat,
318 &bpt);
319
320 unit_m4(new_pstate.new_rest_mat);
321 BKE_bone_parent_transform_apply(&bpt, new_pstate.new_rest_mat, new_pstate.new_rest_mat);
322
323 /* Bone location channel in pose space relative to bone head. */
324 mul_v3_mat3_m4v3(old_chan_loc, bpt.loc_mat, pchan_eval->loc);
325
326 /* Apply the change to the rest bone position. */
327 invert_m4_m4(delta, bone->arm_mat);
328 mul_m4_m4m4(delta, new_pstate.new_rest_mat, delta);
329
330 applyarmature_adjust_edit_position(arm, pchan, delta, new_pstate.new_arm_mat);
331
332 /* Location pose channel has to be updated, because it is affected
333 * by parent scaling, and the rest pose has no scale by definition. */
334 if (!(bone->flag & BONE_CONNECTED) && !is_zero_v3(old_chan_loc)) {
335 float inv_parent_arm[4][4];
336
337 /* Compute the channel coordinate space matrices for the new rest state. */
338 invert_m4_m4(inv_parent_arm, pstate->new_arm_mat);
339 mul_m4_m4m4(offs_bone, inv_parent_arm, new_pstate.new_arm_mat);
341 bone->inherit_scale_mode,
342 offs_bone,
343 pstate->new_arm_mat,
344 pstate->new_arm_mat,
345 &bpt);
346
347 /* Re-apply the location to keep the final effect. */
348 invert_m4(bpt.loc_mat);
349 mul_v3_mat3_m4v3(pchan->loc, bpt.loc_mat, old_chan_loc);
350 }
351
352 pstate = &new_pstate;
353 }
354
355 for (Bone &child : bone->childbase) {
356 applyarmature_process_selected_recursive(arm, pose, pose_eval, &child, selected, pstate);
357 }
358}
359
360/* Reset bone constraint so that it is correct after the pose has been applied. */
362{
363 /* TODO(Sybren): This function needs too much knowledge of the internals of specific constraints.
364 * When it is extended with one or two more constraints, move the functionality into a
365 * bConstraintTypeInfo callback function. */
366 switch (constraint->type) {
368 bStretchToConstraint *stretch_to = static_cast<bStretchToConstraint *>(constraint->data);
369 stretch_to->orglength = 0.0f; /* Force recalculation on next evaluation. */
370 break;
371 }
372 default:
373 /* Most constraints don't need resetting. */
374 break;
375 }
376}
377
378/* Reset bone constraints of the given pose channel so that they are correct after the pose has
379 * been applied. */
381{
382 for (bConstraint &constraint : pchan->constraints) {
384 }
385}
386
387/* Reset all (or only selected) bone constraints so that they are correct after the pose has been
388 * applied. */
389static void applyarmature_reset_constraints(bPose *pose, const bool use_selected)
390{
391 for (bPoseChannel &pchan : pose->chanbase) {
392 if (use_selected && (pchan.flag & POSE_SELECTED) == 0) {
393 continue;
394 }
396 }
397}
398
399/* Set the current pose as the rest-pose. */
401{
402 Main *bmain = CTX_data_main(C);
403 Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
404 Scene *scene = CTX_data_scene(C);
405 /* must be active object, not edit-object */
407 const Object *ob_eval = DEG_get_evaluated(depsgraph, ob);
409 bPose *pose;
410 Vector<PointerRNA> selected_bones;
411
412 const bool use_selected = RNA_boolean_get(op->ptr, "selected");
413
414 /* don't check if editmode (should be done by caller) */
415 if (ob->type != OB_ARMATURE) {
416 return OPERATOR_CANCELLED;
417 }
419 BKE_report(op->reports, RPT_ERROR, "Cannot apply pose to lib-linked armature");
420 return OPERATOR_CANCELLED;
421 }
422
423 /* helpful warnings... */
424 /* TODO: add warnings to be careful about actions, applying deforms first, etc. */
425 if (ob->adt && ob->adt->action) {
428 "Actions on this armature will be destroyed by this new rest pose as the "
429 "transforms stored are relative to the old rest pose");
430 }
431
432 /* Find selected bones before switching to edit mode. */
433 if (use_selected) {
434 CTX_data_selected_pose_bones(C, &selected_bones);
435
436 if (selected_bones.is_empty()) {
437 return OPERATOR_CANCELLED;
438 }
439 }
440
441 /* Get edit-bones of active armature to alter. */
443
444 /* Get pose of active object and move it out of pose-mode. */
445 pose = ob->pose;
446
447 if (use_selected) {
448 /* The selected only mode requires a recursive walk to handle parent-child relations. */
449 for (Bone &bone : arm->bonebase) {
451 arm, pose, ob_eval->pose, &bone, selected_bones, nullptr);
452 }
453 }
454 else {
455 for (bPoseChannel &pchan : pose->chanbase) {
456 const bPoseChannel *pchan_eval = BKE_pose_channel_find_name(ob_eval->pose, pchan.name);
457 EditBone *curbone = ED_armature_ebone_find_name(arm->edbo, pchan.name);
458
460 curbone, pchan_eval->pose_mat, pchan_eval->pose_tail, nullptr);
461 applyarmature_transfer_properties(curbone, {&pchan, pchan.bone_get(*ob)}, pchan_eval);
462 }
463 }
464
465 /* Convert edit-bones back to bones, and then free the edit-data. */
466 ED_armature_from_edit(bmain, arm);
468
469 /* Flush positions of pose-bones. */
470 BKE_pose_where_is(depsgraph, scene, ob);
471
472 /* fix parenting of objects which are bone-parented */
474
475 /* For the affected bones, reset specific constraints that are now known to be invalid. */
477
478 /* NOTE: notifier might evolve. */
481
482 return OPERATOR_FINISHED;
483}
484
486{
487 ui::Layout &layout = *op->layout;
489
491
492 layout.prop(&ptr, "selected", UI_ITEM_NONE, std::nullopt, ICON_NONE);
493}
494
496{
497 /* identifiers */
498 ot->name = "Apply Pose as Rest Pose";
499 ot->idname = "POSE_OT_armature_apply";
500 ot->description = "Apply the current pose as the new rest pose";
501
502 /* callbacks */
506
507 /* flags */
509
511 "selected",
512 false,
513 "Selected Only",
514 "Only apply the selected bones (with propagation to children)");
515}
516
518
519/* -------------------------------------------------------------------- */
524
526{
527 const Main *bmain = CTX_data_main(C);
528 const Scene *scene = CTX_data_scene(C);
529 ViewLayer *view_layer = CTX_data_view_layer(C);
530 View3D *v3d = CTX_wm_view3d(C);
531
532 /* Needed to ensure #bPoseChannel.pose_mat are up to date. */
534
535 FOREACH_OBJECT_IN_MODE_BEGIN (bmain, scene, view_layer, v3d, OB_ARMATURE, OB_MODE_POSE, ob) {
536 const bArmature *arm = id_cast<const bArmature *>(ob->data);
537
538 int chanbase_len = ob->pose->chanbase.count();
539 /* Storage for the calculated matrices to prevent reading from modified values.
540 * NOTE: this could be avoided if children were always calculated before parents
541 * however ensuring this is involved and doesn't give any significant advantage. */
542 struct XFormArray {
543 float matrix[4][4];
544 bool is_set;
545 } *pchan_xform_array = MEM_new_array_uninitialized<XFormArray>(chanbase_len, __func__);
546 bool changed = false;
547
548 for (const auto [i, pchan] : ob->pose->chanbase.enumerate()) {
549 bke::PChanBone pchanbone = {&pchan, pchan.bone_get(*ob)};
550 if (!animrig::bone_is_selected(arm, pchanbone)) {
551 pchan_xform_array[i].is_set = false;
552 continue;
553 }
554
555 /* `chan_mat` already contains the delta transform from rest pose to pose-mode pose
556 * as that is baked into there so that B-Bones will work. Once we've set this as the
557 * new raw-transform components, don't recalculate the poses yet, otherwise IK result will
558 * change, thus changing the result we may be trying to record. */
559
560 /* NOTE: For some reason `pchan->chan_mat` can't be used here as it gives odd
561 * rotation/offset, see #38251.
562 * Using `pchan->pose_mat` and bringing it back in bone space seems to work as expected!
563 * This matches how visual key-framing works. */
564 BKE_armature_mat_pose_to_bone(pchanbone, pchan.pose_mat, pchan_xform_array[i].matrix);
565 pchan_xform_array[i].is_set = true;
566 changed = true;
567 }
568
569 if (changed) {
570 /* Perform separately to prevent feedback loop. */
571 for (const auto [i, pchan] : ob->pose->chanbase.enumerate()) {
572 if (!pchan_xform_array[i].is_set) {
573 continue;
574 }
575 BKE_pchan_apply_mat4(&pchan, pchan_xform_array[i].matrix, true);
576 }
577
579
580 /* NOTE: notifier might evolve. */
582 }
583
584 MEM_delete(pchan_xform_array);
585 }
587
588 return OPERATOR_FINISHED;
589}
590
592{
593 /* identifiers */
594 ot->name = "Apply Visual Transform to Pose";
595 ot->idname = "POSE_OT_visual_transform_apply";
596 ot->description = "Apply final constrained position of pose bones to their transform";
597
598 /* callbacks */
601
602 /* flags */
604}
605
607
608/* -------------------------------------------------------------------- */
611
616static void blend_bbone_values(const bPoseChannel &copy_bone,
617 bPoseChannel &paste_bone,
618 const float factor,
619 const bool do_flip)
620{
621 const float flip_factor = do_flip ? -1 : 1;
622 const float curve_in_x = copy_bone.curve_in_x * flip_factor;
623 const float curve_out_x = copy_bone.curve_out_x * flip_factor;
624 const float roll1 = copy_bone.roll1 * flip_factor;
625 const float roll2 = copy_bone.roll2 * flip_factor;
626
627 /* B-Bone posing options should also be included... */
628 paste_bone.curve_in_x = interpf(curve_in_x, paste_bone.curve_in_x, factor);
629 paste_bone.curve_in_z = interpf(copy_bone.curve_in_z, paste_bone.curve_in_z, factor);
630 paste_bone.curve_out_x = interpf(curve_out_x, paste_bone.curve_out_x, factor);
631 paste_bone.curve_out_z = interpf(copy_bone.curve_out_z, paste_bone.curve_out_z, factor);
632
633 paste_bone.roll1 = interpf(roll1, paste_bone.roll1, factor);
634 paste_bone.roll2 = interpf(roll2, paste_bone.roll2, factor);
635 paste_bone.ease1 = interpf(copy_bone.ease1, paste_bone.ease1, factor);
636 paste_bone.ease2 = interpf(copy_bone.ease2, paste_bone.ease2, factor);
637
638 interp_v3_v3v3(paste_bone.scale_in, paste_bone.scale_in, copy_bone.scale_in, factor);
639 interp_v3_v3v3(paste_bone.scale_out, paste_bone.scale_out, copy_bone.scale_out, factor);
640}
641
647static void blend_id_property(IDProperty &prop, const IDProperty &target, const float factor)
648{
649 BLI_assert(prop.type == target.type);
650 BLI_assert(prop.subtype == target.subtype);
651
652 switch (prop.type) {
653 case IDP_INT: {
654 const int prop_val = IDP_int_get(&prop);
655 const int target_val = IDP_int_get(&target);
656 IDP_int_set(&prop, round_fl_to_int(interpf(target_val, prop_val, factor)));
657 return;
658 }
659 case IDP_FLOAT: {
660 const float prop_val = IDP_float_get(&prop);
661 const float target_val = IDP_float_get(&target);
662 IDP_float_set(&prop, interpf(target_val, prop_val, factor));
663 return;
664 }
665 case IDP_DOUBLE: {
666 const double prop_val = IDP_double_get(&prop);
667 const double target_val = IDP_double_get(&target);
668 IDP_double_set(&prop, interpd(target_val, prop_val, factor));
669 return;
670 }
671 case IDP_ARRAY: {
672 if (prop.len != target.len) {
673 /* For arrays, their length has to match too. */
674 return;
675 }
676 switch (prop.subtype) {
677 case IDP_INT: {
678 int *prop_val = IDP_array_int_get(&prop);
679 const int *target_val = IDP_array_int_get(&target);
680 for (int i = 0; i < prop.len; i++) {
681 prop_val[i] = round_fl_to_int(interpf(target_val[i], prop_val[i], factor));
682 }
683 return;
684 }
685 case IDP_FLOAT: {
686 float *prop_val = IDP_array_float_get(&prop);
687 const float *target_val = IDP_array_float_get(&target);
688 for (int i = 0; i < prop.len; i++) {
689 prop_val[i] = interpf(target_val[i], prop_val[i], factor);
690 }
691 return;
692 }
693 case IDP_DOUBLE: {
694 double *prop_val = IDP_array_double_get(&prop);
695 const double *target_val = IDP_array_double_get(&target);
696 for (int i = 0; i < prop.len; i++) {
697 prop_val[i] = interpd(target_val[i], prop_val[i], factor);
698 }
699 return;
700 }
701 default:
702 break;
703 }
704 break;
705 }
706 default:
707 break;
708 }
709
710 /* Default case for properties that can't be interpolated. */
711 if (factor > 0.5) {
712 IDP_CopyPropertyContent(&prop, &target);
713 }
714}
715
719static void blend_properties(IDProperty *copy_props, IDProperty *paste_props, const float factor)
720{
721 IDP_foreach_property(copy_props, 0, [&](IDProperty *copy_prop) {
722 IDProperty *paste_prop = IDP_GetPropertyFromGroup(paste_props, copy_prop->name);
723 if (!paste_prop || copy_prop->type != paste_prop->type ||
724 copy_prop->subtype != paste_prop->subtype)
725 {
726 return;
727 }
728 blend_id_property(*paste_prop, *copy_prop, factor);
729 });
730}
731
744 const ed::AnimTransformable &copy_transformable,
745 const bool selected_only,
746 const bool flip,
747 const float factor,
748 bool *r_is_found = nullptr)
749{
751 char name[MAXBONENAME];
752
753 /* Get the name of the bone to paste to - if flipping, we must flip this first. */
754 if (flip) {
755 BLI_string_flip_side_name(name, copy_transformable.name().c_str(), false, sizeof(name));
756 }
757 else {
758 STRNCPY_UTF8(name, copy_transformable.name().c_str());
759 }
760
761 bPoseChannel *paste_bone = BKE_pose_channel_find_name(paste_ob.pose, name);
762 if (r_is_found) {
763 *r_is_found = paste_bone != nullptr;
764 }
765 if (paste_bone == nullptr) {
766 return nullptr;
767 }
768 /* If selection-masking is on, only selected bones get pasted on, which allows making both sides
769 * symmetrical. */
770 if (selected_only && (paste_bone->flag & POSE_SELECTED) == 0) {
771 return nullptr;
772 }
773
774 ed::AnimTransformable paste_transformable(paste_ob, *paste_bone);
775 ed::TransformFloats location = copy_transformable.get_property(
777 ed::Rotation rotation = copy_transformable.get_rotation();
778 if (flip) {
779 /* Flips on the y-z plane. Has to be done here to correctly blend to the flipped pose. */
780 location[0] *= -1;
781 rotation.mirror();
782 }
783 paste_transformable.blend_property_to(
785 paste_transformable.blend_rotation_to(rotation, factor, ed::AXIS_MUTABLE_ALL);
786 paste_transformable.blend_property_to(
789 factor,
791
792 bPoseChannel *copy_bone = copy_transformable.data<bPoseChannel *>();
793 /* B-Bone posing options should also be included... */
794 blend_bbone_values(*copy_bone, *paste_bone, factor, flip);
795
796 /* ID properties. */
797 if (copy_bone->prop) {
798 if (paste_bone->prop) {
799 blend_properties(copy_bone->prop, paste_bone->prop, factor);
800 }
801 else {
802 /* no existing properties, so assume that we want copies too? */
803 paste_bone->prop = IDP_CopyProperty(copy_bone->prop);
804 }
805 }
806 if (copy_bone->system_properties) {
807 /* Same logic as above for system IDProperties, for now. */
808 if (paste_bone->system_properties) {
809 blend_properties(copy_bone->system_properties, paste_bone->system_properties, factor);
810 }
811 else {
812 paste_bone->system_properties = IDP_CopyProperty(copy_bone->system_properties);
813 }
814 }
815
816 return paste_bone;
817}
818
820
821/* -------------------------------------------------------------------- */
824
826{
827 using namespace blender::bke::blendfile;
828
829 Main *bmain = CTX_data_main(C);
831
832 /* Sanity checking. */
833 if (ELEM(nullptr, ob, ob->pose)) {
834 BKE_report(op->reports, RPT_ERROR, "No pose to copy");
835 return OPERATOR_CANCELLED;
836 }
837 if (ID_IS_PACKED(&ob->id)) {
838 /* Direct link/append of packed IDs is not supported currently, so neither is their
839 * copy/pasting. */
840 /* TODO: Think that this is not necessary in fact, since we 'make local' the copied object? */
841 BKE_report(op->reports, RPT_ERROR, "Cannot copy/paste packed data");
842 return OPERATOR_CANCELLED;
843 }
844
845 bArmature *armature = id_cast<bArmature *>(ob->data);
846 BLI_assert_msg(armature, "If an armature object has a pose, it should have armature data");
847 /* Taking off the selection flag in case bones are hidden so they are not
848 * applied when pasting. */
849 for (bPoseChannel &pose_bone : ob->pose->chanbase) {
850 if (!animrig::bone_is_visible(armature, {&pose_bone, pose_bone.bone_get(*ob)})) {
851 animrig::bone_deselect(&pose_bone);
852 }
853 }
854
855 PartialWriteContext copybuffer{*bmain};
856 copybuffer.id_add(
857 &ob->id,
859 (PartialWriteContext::IDAddOperations::MAKE_LOCAL |
860 PartialWriteContext::IDAddOperations::SET_FAKE_USER |
861 PartialWriteContext::IDAddOperations::SET_CLIPBOARD_MARK)},
862 [ob](LibraryIDLinkCallbackData *cb_data,
864 /* Only include `ob->data` (i.e. the Armature) dependency. */
865 if (*(cb_data->id_pointer) == ob->data) {
866 return (PartialWriteContext::IDAddOperations::MAKE_LOCAL |
867 PartialWriteContext::IDAddOperations::ADD_DEPENDENCIES);
868 }
870 });
871
872 char filepath[FILE_MAX];
873 pose_copybuffer_filepath_get(filepath, sizeof(filepath));
874 copybuffer.write_as_copypaste_buffer(filepath, *op->reports);
875
876 /* We are all done! */
877 BKE_report(op->reports, RPT_INFO, "Copied pose to internal clipboard");
878 return OPERATOR_FINISHED;
879}
880
882{
883 /* identifiers */
884 ot->name = "Copy Pose";
885 ot->idname = "POSE_OT_copy";
886 ot->description = "Copy the current pose of the selected bones to the internal clipboard";
887
888 /* API callbacks. */
889 ot->exec = pose_copy_exec;
891
892 /* flag */
893 ot->flag = OPTYPE_REGISTER;
894}
895
897
898/* -------------------------------------------------------------------- */
901
903{
905 Scene *scene = CTX_data_scene(C);
906 const bool flip = RNA_boolean_get(op->ptr, "flipped");
907 bool selected_only = RNA_boolean_get(op->ptr, "selected_mask");
908
909 /* Get KeyingSet to use. */
911
912 /* Sanity checks. */
913 if (ELEM(nullptr, ob, ob->pose)) {
914 return OPERATOR_CANCELLED;
915 }
916
917 /* Read copy buffer .blend file. */
918 char filepath[FILE_MAX];
919 pose_copybuffer_filepath_get(filepath, sizeof(filepath));
920
921 Main *bmain = CTX_data_main(C);
922 Main *temp_bmain = BKE_copybuffer_read(*bmain, filepath, op->reports, FILTER_ID_OB);
923
924 if (!temp_bmain) {
925 BKE_report(op->reports, RPT_ERROR, "Internal clipboard is empty");
926 return OPERATOR_CANCELLED;
927 }
928 /* Make sure data from this file is usable for pose paste. */
929 Object *object_from = nullptr;
930 for (Object &obj : temp_bmain->objects) {
931 if (!(obj.id.flag & ID_FLAG_CLIPBOARD_MARK)) {
932 continue;
933 }
934 if (object_from != nullptr) {
935 /* There can only be one object in the clipboard to read the pose from. However there may be
936 * more than 1 object in total when dealing with packed assets and library overrides.
937 * See #155723. */
938 BKE_report(op->reports, RPT_ERROR, "Internal clipboard is not from pose mode");
939 BKE_main_free(temp_bmain);
940 return OPERATOR_CANCELLED;
941 }
942 object_from = &obj;
943 }
944 if (!object_from) {
945 BKE_main_free(temp_bmain);
946 return OPERATOR_CANCELLED;
947 }
948 bPose *pose_from = object_from->pose;
949 if (pose_from == nullptr) {
950 BKE_report(op->reports, RPT_ERROR, "Internal clipboard has no pose");
951 BKE_main_free(temp_bmain);
952 return OPERATOR_CANCELLED;
953 }
954
955 /* If selected_only option is enabled, if user hasn't selected any bones,
956 * just go back to default behavior to be more in line with other
957 * pose tools. */
958 if (selected_only) {
959 if (CTX_DATA_COUNT(C, selected_pose_bones) == 0) {
960 selected_only = false;
961 }
962 }
963
964 /* Safely merge all of the channels in the buffer pose into any
965 * existing pose. */
966 int num_pasted_bones = 0;
967 int num_skipped_bones = 0;
968 int num_copied_bones = 0;
969 const float factor = RNA_float_get(op->ptr, "factor");
970 for (bPoseChannel &pchan_from : pose_from->chanbase) {
971 if ((pchan_from.flag & POSE_SELECTED) == 0) {
972 /* This code pretends that bones that were not selected at copy time do not exist. */
973 continue;
974 }
975
976 num_copied_bones++;
977
978 /* Try to perform paste on this bone. */
979 bool is_found;
981 *ob, {*object_from, pchan_from}, selected_only, flip, factor, &is_found);
982 if (!pchan_to) {
983 if (is_found) {
984 /* The bone was found, but not selected (and selected_only), so nothing was pasted to it.
985 */
986 num_skipped_bones++;
987 continue;
988 }
989 /* This doesn't have to be an issue, as a pose could be copied to an armature where only a
990 * subset of the bones match. But having access to this information can still be nice. */
992 "Copied pose has bone '%s', but that bone cannot be found now.\n",
993 pchan_from.name);
994 continue;
995 }
996
997 animrig::autokeyframe_pchan(C, scene, ob, pchan_to, ks);
998 num_pasted_bones++;
999 }
1000 BKE_main_free(temp_bmain);
1001
1002 if (num_pasted_bones == 0) {
1003 const char *msg = selected_only ? "None of the %d copied bones are selected now" :
1004 "None of the %d copied bones could be pasted";
1005 BKE_reportf(op->reports, RPT_WARNING, msg, num_copied_bones);
1006 /* Return OPERATOR_FINISHED to show the redo panel. It should be possible to
1007 * turn off "Selected Only" if necessary. */
1008 return OPERATOR_FINISHED;
1009 }
1010
1011 if (num_pasted_bones + num_skipped_bones == num_copied_bones) {
1012 /* All copied bones were found, but maybe some skipped due to selection state: */
1013 if (num_skipped_bones) {
1014 BKE_reportf(op->reports,
1015 RPT_INFO,
1016 "Pasted %d bones, and skipped %d unselected bones",
1017 num_pasted_bones,
1018 num_skipped_bones);
1019 }
1020 else {
1021 BKE_reportf(op->reports, RPT_INFO, "Pasted all %d bones", num_pasted_bones);
1022 }
1023 }
1024 else {
1025 /* Some bones could not be found: */
1026 if (num_skipped_bones) {
1027 BKE_reportf(op->reports,
1028 RPT_INFO,
1029 "Pasted only %d of the %d copied bones, and skipped %d unselected bones",
1030 num_pasted_bones,
1031 num_copied_bones,
1032 num_skipped_bones);
1033 }
1034 else {
1035 BKE_reportf(op->reports,
1037 "Pasted only %d of the %d copied bones",
1038 num_pasted_bones,
1039 num_copied_bones);
1040 }
1041 }
1042
1043 /* Update event for pose and deformation children. */
1045
1046 /* Recalculate paths if any of the bones have paths... */
1048 ED_pose_recalculate_paths(C, scene, ob);
1049 }
1050
1052
1053 return OPERATOR_FINISHED;
1054}
1055
1057{
1058 PropertyRNA *prop;
1059
1060 /* identifiers */
1061 ot->name = "Paste Pose";
1062 ot->idname = "POSE_OT_paste";
1063 ot->description = "Paste the stored pose on to the current pose";
1064
1065 /* API callbacks. */
1066 ot->exec = pose_paste_exec;
1068
1069 /* flag */
1071
1072 /* properties */
1073 prop = RNA_def_boolean(ot->srna,
1074 "flipped",
1075 false,
1076 "Flipped on X-Axis",
1077 "Paste the stored pose flipped on to current pose");
1079
1081 "selected_mask",
1082 false,
1083 "On Selected Only",
1084 "Only paste the stored pose on to selected bones in the current pose");
1085
1087 ot->srna,
1088 "factor",
1089 1.0f,
1090 -FLT_MAX,
1091 FLT_MAX,
1092 "Factor",
1093 "Blends the current pose to the pose to paste. At 1, the pasted pose completely "
1094 "overwrites the current pose",
1095 0.0f,
1096 1.0f);
1097}
1098
1100
1101/* -------------------------------------------------------------------- */
1104
1105/* clear scale of pose-channel */
1107{
1108 if ((pchan->protectflag & OB_LOCK_SCALEX) == 0) {
1109 pchan->scale[0] = 1.0f;
1110 }
1111 if ((pchan->protectflag & OB_LOCK_SCALEY) == 0) {
1112 pchan->scale[1] = 1.0f;
1113 }
1114 if ((pchan->protectflag & OB_LOCK_SCALEZ) == 0) {
1115 pchan->scale[2] = 1.0f;
1116 }
1117
1118 pchan->ease1 = 0.0f;
1119 pchan->ease2 = 0.0f;
1120
1121 copy_v3_fl(pchan->scale_in, 1.0f);
1122 copy_v3_fl(pchan->scale_out, 1.0f);
1123}
1124/* Clear the scale. When X-mirror is enabled,
1125 * also clear the scale of the mirrored pose channel. */
1127{
1128 if (pose->flag & POSE_MIRROR_EDIT) {
1129 bPoseChannel *pchan_mirror = BKE_pose_channel_get_mirrored(pose, pchan->name);
1130 if (pchan_mirror != nullptr) {
1131 pchan_clear_scale(pchan_mirror);
1132 }
1133 }
1134 pchan_clear_scale(pchan);
1135}
1136
1137/* clear location of pose-channel */
1139{
1140 if ((pchan->protectflag & OB_LOCK_LOCX) == 0) {
1141 pchan->loc[0] = 0.0f;
1142 }
1143 if ((pchan->protectflag & OB_LOCK_LOCY) == 0) {
1144 pchan->loc[1] = 0.0f;
1145 }
1146 if ((pchan->protectflag & OB_LOCK_LOCZ) == 0) {
1147 pchan->loc[2] = 0.0f;
1148 }
1149}
1150/* Clear the Location. When X-mirror is enabled,
1151 * also clear the location of the mirrored pose channel. */
1153{
1154 if (pose->flag & POSE_MIRROR_EDIT) {
1155 bPoseChannel *pchan_mirror = BKE_pose_channel_get_mirrored(pose, pchan->name);
1156 if (pchan_mirror != nullptr) {
1157 pchan_clear_loc(pchan_mirror);
1158 }
1159 }
1160 pchan_clear_loc(pchan);
1161}
1162
1163/* clear rotation of pose-channel */
1165{
1167 /* check if convert to eulers for locking... */
1168 if (pchan->protectflag & OB_LOCK_ROT4D) {
1169 /* perform clamping on a component by component basis */
1170 if (pchan->rotmode == ROT_MODE_AXISANGLE) {
1171 if ((pchan->protectflag & OB_LOCK_ROTW) == 0) {
1172 pchan->rotAngle = 0.0f;
1173 }
1174 if ((pchan->protectflag & OB_LOCK_ROTX) == 0) {
1175 pchan->rotAxis[0] = 0.0f;
1176 }
1177 if ((pchan->protectflag & OB_LOCK_ROTY) == 0) {
1178 pchan->rotAxis[1] = 0.0f;
1179 }
1180 if ((pchan->protectflag & OB_LOCK_ROTZ) == 0) {
1181 pchan->rotAxis[2] = 0.0f;
1182 }
1183
1184 /* check validity of axis - axis should never be 0,0,0
1185 * (if so, then we make it rotate about y). */
1186 if (IS_EQF(pchan->rotAxis[0], pchan->rotAxis[1]) &&
1187 IS_EQF(pchan->rotAxis[1], pchan->rotAxis[2]))
1188 {
1189 pchan->rotAxis[1] = 1.0f;
1190 }
1191 }
1192 else if (pchan->rotmode == ROT_MODE_QUAT) {
1193 if ((pchan->protectflag & OB_LOCK_ROTW) == 0) {
1194 pchan->quat[0] = 1.0f;
1195 }
1196 if ((pchan->protectflag & OB_LOCK_ROTX) == 0) {
1197 pchan->quat[1] = 0.0f;
1198 }
1199 if ((pchan->protectflag & OB_LOCK_ROTY) == 0) {
1200 pchan->quat[2] = 0.0f;
1201 }
1202 if ((pchan->protectflag & OB_LOCK_ROTZ) == 0) {
1203 pchan->quat[3] = 0.0f;
1204 }
1205 }
1206 else {
1207 /* the flag may have been set for the other modes, so just ignore the extra flag... */
1208 if ((pchan->protectflag & OB_LOCK_ROTX) == 0) {
1209 pchan->eul[0] = 0.0f;
1210 }
1211 if ((pchan->protectflag & OB_LOCK_ROTY) == 0) {
1212 pchan->eul[1] = 0.0f;
1213 }
1214 if ((pchan->protectflag & OB_LOCK_ROTZ) == 0) {
1215 pchan->eul[2] = 0.0f;
1216 }
1217 }
1218 }
1219 else {
1220 /* perform clamping using euler form (3-components) */
1221 float eul[3], oldeul[3], quat1[4] = {0};
1222 float qlen = 0.0f;
1223
1224 if (pchan->rotmode == ROT_MODE_QUAT) {
1225 qlen = normalize_qt_qt(quat1, pchan->quat);
1226 quat_to_eul(oldeul, quat1);
1227 }
1228 else if (pchan->rotmode == ROT_MODE_AXISANGLE) {
1229 axis_angle_to_eulO(oldeul, EULER_ORDER_DEFAULT, pchan->rotAxis, pchan->rotAngle);
1230 }
1231 else {
1232 copy_v3_v3(oldeul, pchan->eul);
1233 }
1234
1235 eul[0] = eul[1] = eul[2] = 0.0f;
1236
1237 if (pchan->protectflag & OB_LOCK_ROTX) {
1238 eul[0] = oldeul[0];
1239 }
1240 if (pchan->protectflag & OB_LOCK_ROTY) {
1241 eul[1] = oldeul[1];
1242 }
1243 if (pchan->protectflag & OB_LOCK_ROTZ) {
1244 eul[2] = oldeul[2];
1245 }
1246
1247 if (pchan->rotmode == ROT_MODE_QUAT) {
1248 eul_to_quat(pchan->quat, eul);
1249
1250 /* restore original quat size */
1251 mul_qt_fl(pchan->quat, qlen);
1252
1253 /* quaternions flip w sign to accumulate rotations correctly */
1254 if ((quat1[0] < 0.0f && pchan->quat[0] > 0.0f) ||
1255 (quat1[0] > 0.0f && pchan->quat[0] < 0.0f))
1256 {
1257 mul_qt_fl(pchan->quat, -1.0f);
1258 }
1259 }
1260 else if (pchan->rotmode == ROT_MODE_AXISANGLE) {
1262 }
1263 else {
1264 copy_v3_v3(pchan->eul, eul);
1265 }
1266 }
1267 } /* Duplicated in source/blender/editors/object/object_transform.cc */
1268 else {
1269 if (pchan->rotmode == ROT_MODE_QUAT) {
1270 unit_qt(pchan->quat);
1271 }
1272 else if (pchan->rotmode == ROT_MODE_AXISANGLE) {
1273 /* by default, make rotation of 0 radians around y-axis (roll) */
1274 unit_axis_angle(pchan->rotAxis, &pchan->rotAngle);
1275 }
1276 else {
1277 zero_v3(pchan->eul);
1278 }
1279 }
1280
1281 /* Clear also Bendy Bone stuff - Roll is obvious,
1282 * but Curve X/Y stuff is also kind of rotational in nature... */
1283 pchan->roll1 = 0.0f;
1284 pchan->roll2 = 0.0f;
1285
1286 pchan->curve_in_x = 0.0f;
1287 pchan->curve_in_z = 0.0f;
1288 pchan->curve_out_x = 0.0f;
1289 pchan->curve_out_z = 0.0f;
1290}
1291/* Clear the rotation. When X-mirror is enabled,
1292 * also clear the rotation of the mirrored pose channel. */
1294{
1295 if (pose->flag & POSE_MIRROR_EDIT) {
1296 bPoseChannel *pchan_mirror = BKE_pose_channel_get_mirrored(pose, pchan->name);
1297 if (pchan_mirror != nullptr) {
1298 pchan_clear_rot(pchan_mirror);
1299 }
1300 }
1301 pchan_clear_rot(pchan);
1302}
1303
1304/* clear loc/rot/scale of pose-channel */
1311
1312/* --------------- */
1313
1314/* generic exec for clear-pose operators */
1316 wmOperator *op,
1317 void (*clear_func)(const bPose *,
1318 bPoseChannel *),
1319 const char default_ksName[])
1320{
1321 Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
1322 const Main *bmain = CTX_data_main(C);
1323 Scene *scene = CTX_data_scene(C);
1324 bool changed_multi = false;
1325
1326 /* sanity checks */
1327 if (ELEM(nullptr, clear_func, default_ksName)) {
1328 BKE_report(op->reports,
1329 RPT_ERROR,
1330 "Programming error: missing clear transform function or keying set name");
1331 return OPERATOR_CANCELLED;
1332 }
1333
1334 /* only clear relevant transforms for selected bones */
1335 ViewLayer *view_layer = CTX_data_view_layer(C);
1336 View3D *v3d = CTX_wm_view3d(C);
1337 FOREACH_OBJECT_IN_MODE_BEGIN (bmain, scene, view_layer, v3d, OB_ARMATURE, OB_MODE_POSE, ob_iter)
1338 {
1339 /* XXX: UGLY HACK (for auto-key + clear transforms). */
1340 Object *ob_eval = DEG_get_evaluated(depsgraph, ob_iter);
1341 Vector<PointerRNA> sources;
1342 bool changed = false;
1343
1345 /* run provided clearing function */
1346 clear_func(ob_iter->pose, pchan);
1347 changed = true;
1348
1349 /* do auto-keyframing as appropriate */
1350 if (animrig::autokeyframe_cfra_can_key(scene, &ob_iter->id)) {
1351 /* tag for autokeying later */
1352 animrig::relative_keyingset_add_source(sources, &ob_iter->id, RNA_PoseBone, pchan);
1353
1354#if 1 /* XXX: Ugly Hack - Run clearing function on evaluated copy of pchan */
1355 bPoseChannel *pchan_eval = BKE_pose_channel_find_name(ob_eval->pose, pchan->name);
1356 clear_func(ob_iter->pose, pchan_eval);
1357#endif
1358 }
1359 }
1361
1362 if (changed) {
1363 changed_multi = true;
1364
1365 /* perform autokeying on the bones if needed */
1366 if (!sources.is_empty()) {
1367 /* get KeyingSet to use */
1368 KeyingSet *ks = animrig::get_keyingset_for_autokeying(scene, default_ksName);
1369
1370 /* insert keyframes */
1372 C, &sources, ks, animrig::ModifyKeyMode::INSERT, float(scene->r.cfra));
1373
1374 /* now recalculate paths */
1375 if (ob_iter->pose->avs.path_bakeflag & MOTIONPATH_BAKE_HAS_PATHS) {
1376 ED_pose_recalculate_paths(C, scene, ob_iter);
1377 }
1378 }
1379
1381
1382 /* NOTE: notifier might evolve. */
1384 }
1385 }
1387
1388 return changed_multi ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
1389}
1390
1392
1393/* -------------------------------------------------------------------- */
1396
1402
1404{
1405 /* identifiers */
1406 ot->name = "Clear Pose Scale";
1407 ot->idname = "POSE_OT_scale_clear";
1408 ot->description = "Reset scaling of selected bones to their default values";
1409
1410 /* API callbacks. */
1413
1414 /* flags */
1416}
1417
1419
1420/* -------------------------------------------------------------------- */
1423
1429
1431{
1432 /* identifiers */
1433 ot->name = "Clear Pose Rotation";
1434 ot->idname = "POSE_OT_rot_clear";
1435 ot->description = "Reset rotations of selected bones to their default values";
1436
1437 /* API callbacks. */
1440
1441 /* flags */
1443}
1444
1446
1447/* -------------------------------------------------------------------- */
1450
1456
1458{
1459 /* identifiers */
1460 ot->name = "Clear Pose Location";
1461 ot->idname = "POSE_OT_loc_clear";
1462 ot->description = "Reset locations of selected bones to their default values";
1463
1464 /* API callbacks. */
1467
1468 /* flags */
1470}
1471
1473
1474/* -------------------------------------------------------------------- */
1477
1483
1485{
1486 /* identifiers */
1487 ot->name = "Clear Pose Transforms";
1488 ot->idname = "POSE_OT_transforms_clear";
1489 ot->description =
1490 "Reset location, rotation, and scaling of selected bones to their default values";
1491
1492 /* API callbacks. */
1495
1496 /* flags */
1498}
1499
1501
1502/* -------------------------------------------------------------------- */
1505
1507 const bPoseChannel &copy_bone,
1508 const bool selected_only)
1509{
1510 bPoseChannel *paste_bone = BKE_pose_channel_find_name(paste_ob.pose, copy_bone.name);
1511 if (paste_bone == nullptr) {
1512 return;
1513 }
1514 if (selected_only && (paste_bone->flag & POSE_SELECTED) == 0) {
1515 return;
1516 }
1517
1518 copy_v3_v3(paste_bone->loc, copy_bone.loc);
1519 copy_v3_v3(paste_bone->eul, copy_bone.eul);
1520 copy_v3_v3(paste_bone->rotAxis, copy_bone.rotAxis);
1521 paste_bone->rotAngle = copy_bone.rotAngle;
1522 copy_v4_v4(paste_bone->quat, copy_bone.quat);
1523 copy_v3_v3(paste_bone->scale, copy_bone.scale);
1524
1525 blend_bbone_values(copy_bone, *paste_bone, 1.0, /*flip=*/false);
1526
1527 /* ID properties */
1528 if (copy_bone.prop && paste_bone->prop) {
1529 /* If we have existing properties on a bone, just copy over the values of
1530 * matching properties (i.e. ones which will have some impact) on to the target
1531 * instead of just blindly replacing all. */
1532 IDP_SyncGroupValues(paste_bone->prop, copy_bone.prop);
1533 }
1534 if (copy_bone.system_properties && paste_bone->system_properties) {
1535 /* Same logic as above for system IDProperties. */
1537 }
1538}
1539
1541{
1542 ViewLayer *view_layer = CTX_data_view_layer(C);
1543 View3D *v3d = CTX_wm_view3d(C);
1544 const Main *bmain = CTX_data_main(C);
1545 Scene *scene = CTX_data_scene(C);
1546 Depsgraph *depsgraph = CTX_data_depsgraph_pointer(C);
1548 depsgraph, float(scene->r.cfra));
1549 const bool only_select = RNA_boolean_get(op->ptr, "only_selected");
1550
1551 FOREACH_OBJECT_IN_MODE_BEGIN (bmain, scene, view_layer, v3d, OB_ARMATURE, OB_MODE_POSE, ob) {
1552 if ((ob->adt) && (ob->adt->action)) {
1553 /* In order to reset only a single pose bone to its keyed values we need to evaluate it and
1554 * copy the evaluated data back onto it. */
1555 bPose *dummyPose = nullptr;
1556 Object workob{};
1557
1558 /* Execute animation step for current frame using a dummy copy of the pose. */
1559 BKE_pose_copy_data(&dummyPose, ob->pose, false);
1560
1561 STRNCPY_UTF8(workob.id.name, "OB<ClearTfmWorkOb>");
1562 workob.type = OB_ARMATURE;
1563 workob.data = ob->data;
1564 workob.adt = ob->adt;
1565 workob.pose = dummyPose;
1566
1568 &workob.id, workob.adt, &anim_eval_context, ADT_RECALC_ANIM, false);
1569
1570 for (bPoseChannel &pchan : dummyPose->chanbase) {
1571 pose_bone_copy_transform_values(*ob, pchan, only_select);
1572 }
1573
1574 /* Free temp data - free manually as was copied without constraints. */
1575 for (bPoseChannel &pchan : dummyPose->chanbase) {
1576 if (pchan.prop) {
1577 IDP_FreeProperty(pchan.prop);
1578 }
1579 if (pchan.system_properties) {
1581 }
1582 }
1583
1584 /* Was copied without constraints so we can't use `BKE_pose_free_data`. */
1585 dummyPose->chanbase.free_no_destruct();
1586 MEM_delete(dummyPose->runtime);
1587 MEM_delete(dummyPose);
1588 }
1589 else {
1590 /* No animation, so just reset to the rest pose. */
1591 BKE_pose_rest(*ob, only_select);
1592 }
1593
1596 }
1598
1599 return OPERATOR_FINISHED;
1600}
1601
1603{
1604 /* identifiers */
1605 ot->name = "Clear User Transforms";
1606 ot->idname = "POSE_OT_user_transforms_clear";
1607 ot->description = "Reset pose bone transforms to keyframed state";
1608
1609 /* callbacks */
1612
1613 /* flags */
1615
1616 /* properties */
1617 RNA_def_boolean(ot->srna, "only_selected", true, "Only Selected", "Only visible/selected bones");
1618}
1619
1621
1622} // namespace blender
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.
Functionality to interact with keying sets.
Blender kernel action and pose functionality.
#define FOREACH_PCHAN_SELECTED_IN_OBJECT_END
#define FOREACH_PCHAN_SELECTED_IN_OBJECT_BEGIN(_ob, _pchan)
#define CTX_DATA_COUNT(C, member)
#define IDP_float_get(prop)
#define IDP_int_get(prop)
#define IDP_array_double_get(prop)
#define IDP_int_set(prop, value)
#define IDP_double_get(prop)
#define IDP_array_int_get(prop)
#define IDP_float_set(prop, value)
#define IDP_double_set(prop, value)
#define IDP_array_float_get(prop)
#define FOREACH_OBJECT_IN_MODE_BEGIN(_bmain, _scene, _view_layer, _v3d, _object_type, _object_mode, _instance)
Definition BKE_layer.hh:470
#define FOREACH_OBJECT_IN_MODE_END
Definition BKE_layer.hh:477
General operations, lookup, etc. for blender objects.
#define FILE_MAX
#define BLI_path_join(...)
#define STRNCPY_UTF8(dst, src)
#define CLOG_INFO(clg_ref,...)
Definition CLG_log.h:190
#define ID_IS_PACKED(_id)
Definition DNA_ID.h:703
#define FILTER_ID_OB
Definition DNA_ID.h:1216
#define MAXBONENAME
Object is a sort of wrapper for general info.
Defines an abstraction around various structs to modify their transform properties via a unified API ...
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define UI_ITEM_NONE
#define ND_POSE
Definition WM_types.hh:482
#define ND_TRANSFORM
Definition WM_types.hh:475
#define NC_OBJECT
Definition WM_types.hh:392
#define ELEM(...)
#define IS_EQF(a, b)
#define BLI_assert_msg(a, msg)
constexpr const T * end() const
Definition BLI_span.hh:228
constexpr const T * begin() const
Definition BLI_span.hh:224
bool is_empty() const
ID * id_add(const ID *id, IDAddOptions options, FunctionRef< IDAddOperations(LibraryIDLinkCallbackData *cb_data, IDAddOptions options)> dependencies_filter_cb=nullptr)
constexpr const char * c_str() const
void blend_property_to(PropertyType prop_type, Span< float > target, float factor, AxisMutable axis_flag)
void blend_rotation_to(const Rotation &target, float factor, AxisMutable axis_flag)
TransformFloats get_property(PropertyType prop_type) const
#define FLT_MAX
void relative_keyingset_add_source(Vector< PointerRNA > &sources, ID *id, StructRNA *srna, void *data)
void bone_deselect(bPoseChannel *pchan)
bool autokeyframe_cfra_can_key(const Scene *scene, ID *id)
bool bone_is_visible(const bArmature *armature, const Bone *bone)
bool bone_is_selected(const bArmature *armature, const Bone *bone)
KeyingSet * get_keyingset_for_autokeying(const Scene *scene, const char *transformKSName)
bool autokeyframe_pchan(bContext *C, Scene *scene, Object *ob, bPoseChannel *pchan, KeyingSet *ks)
int apply_keyingset(bContext *C, Vector< PointerRNA > *sources, KeyingSet *keyingset, ModifyKeyMode mode, float cfra)
PChanBoneT< bPoseChannel, Bone > PChanBone
Definition BKE_pose.hh:88
Array< float, 4 > TransformFloats
BMesh Iterators.
void mat3_vec_to_roll(const float mat[3][3], const float vec[3], float *r_roll)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void BKE_bone_parent_transform_combine(const BoneParentTransform *in1, const BoneParentTransform *in2, BoneParentTransform *result)
bArmature * BKE_armature_from_object(Object *ob)
static bPoseChannel * pose_bone_blend_to(Object &paste_ob, const ed::AnimTransformable &copy_transformable, const bool selected_only, const bool flip, const float factor, bool *r_is_found=nullptr)
static void pchan_clear_loc_with_mirrored(const bPose *pose, bPoseChannel *pchan)
static wmOperatorStatus pose_clear_loc_exec(bContext *C, wmOperator *op)
@ ADT_RECALC_ANIM
static void applyarmature_transfer_properties(EditBone *curbone, bke::PChanBone pchanbone, const bPoseChannel *pchan_eval)
void vec_roll_to_mat3(const float vec[3], float roll, float r_mat[3][3])
Main * BKE_copybuffer_read(Main &reference_bmain, const char *libname, ReportList *reports, uint64_t id_types_mask)
@ RPT_WARNING
Definition BKE_report.hh:40
void axis_angle_to_eulO(float eul[3], short order, const float axis[3], float angle)
static void pchan_clear_scale_with_mirrored(const bPose *pose, bPoseChannel *pchan)
static wmOperatorStatus pose_clear_transforms_exec(bContext *C, wmOperator *op)
static wmOperatorStatus pose_clear_scale_exec(bContext *C, wmOperator *op)
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
static void pose_copybuffer_filepath_get(char filepath[FILE_MAX], size_t filepath_maxncpy)
void ED_armature_to_edit(bArmature *arm)
void ED_armature_from_edit(Main *bmain, bArmature *arm)
static void blend_id_property(IDProperty &prop, const IDProperty &target, const float factor)
static void applyarmature_reset_bone_constraints(const bPoseChannel *pchan)
void copy_m3_m4(float m1[3][3], const float m2[4][4])
static void apply_armature_pose2bones_ui(bContext *C, wmOperator *op)
void POSE_OT_armature_apply(wmOperatorType *ot)
MINLINE int round_fl_to_int(float a)
void copy_m4_m3(float m1[4][4], const float m2[3][3])
Depsgraph * CTX_data_depsgraph_pointer(const bContext *C)
void mul_v3_m4v3(float r[3], const float mat[4][4], const float vec[3])
IDProperty * IDP_CopyProperty(const IDProperty *prop) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition idprop.cc:986
IDProperty * IDP_GetPropertyFromGroup(const IDProperty *prop, StringRef name) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition idprop.cc:818
bool ED_operator_posemode(bContext *C)
bool invert_m4_m4(float inverse[4][4], const float mat[4][4])
MINLINE double interpd(double target, double origin, double t)
static void pchan_clear_transforms(const bPose *pose, bPoseChannel *pchan)
void IDP_foreach_property(IDProperty *id_property_root, int type_filter, FunctionRef< void(IDProperty *id_property)> callback)
static constexpr const char * ANIM_KS_SCALING_ID
void quat_to_eul(float eul[3], const float quat[4])
void mul_v3_mat3_m4v3(float r[3], const float mat[4][4], const float vec[3])
void mul_m4_m4m4(float R[4][4], const float A[4][4], const float B[4][4])
bool invert_m4(float mat[4][4])
static void applyarmature_process_selected_recursive(bArmature *arm, bPose *pose, bPose *pose_eval, Bone *bone, Span< PointerRNA > selected, ApplyArmature_ParentState *pstate)
void BKE_pose_where_is(Depsgraph *depsgraph, Scene *scene, Object *ob)
static constexpr const char * ANIM_KS_LOCATION_ID
static void blend_bbone_values(const bPoseChannel &copy_bone, bPoseChannel &paste_bone, const float factor, const bool do_flip)
ViewLayer * CTX_data_view_layer(const bContext *C)
void BKE_object_apply_mat4(Object *ob, const float mat[4][4], bool use_compat, bool use_parent)
Definition object.cc:3663
AnimationEvalContext BKE_animsys_eval_context_construct(Depsgraph *depsgraph, float eval_time) ATTR_WARN_UNUSED_RESULT
Definition anim_sys.cc:602
void BKE_bone_parent_transform_apply(const BoneParentTransform *bpt, const float inmat[4][4], float outmat[4][4])
void IDP_CopyPropertyContent(IDProperty *dst, const IDProperty *src) ATTR_NONNULL()
Definition idprop.cc:991
void BKE_bone_parent_transform_clear(BoneParentTransform *bpt)
void unit_axis_angle(float axis[3], float *angle)
PropertyRNA * RNA_def_float_factor(StructOrFunctionRNA *cont_, const char *identifier, const float default_value, const float hardmin, const float hardmax, const char *ui_name, const char *ui_description, const float softmin, const float softmax)
void mul_m4_v3(const float M[4][4], float r[3])
void unit_qt(float q[4])
static wmOperatorStatus pose_clear_rot_exec(bContext *C, wmOperator *op)
void POSE_OT_copy(wmOperatorType *ot)
static wmOperatorStatus pose_paste_exec(bContext *C, wmOperator *op)
MINLINE void mul_v3_v3(float r[3], const float a[3])
bPoseChannel * BKE_pose_channel_get_mirrored(const bPose *pose, const char *name) ATTR_WARN_UNUSED_RESULT
void BKE_bone_parent_transform_calc_from_matrices(int bone_flag, int inherit_scale_mode, const float offs_bone[4][4], const float parent_arm_mat[4][4], const float parent_pose_mat[4][4], BoneParentTransform *r_bpt)
void eulO_to_axis_angle(float axis[3], float *angle, const float eul[3], short order)
void BKE_armature_mat_pose_to_bone(bke::PChanBoneConst pchanbone, const float inmat[4][4], float outmat[4][4])
static wmOperatorStatus pose_visual_transform_apply_exec(bContext *C, wmOperator *)
void mul_m4_m4m3(float R[4][4], const float A[4][4], const float B[3][3])
void DEG_id_tag_update(ID *id, unsigned int flags)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
Object * BKE_object_pose_armature_get(Object *ob)
Definition object.cc:2507
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
wmWindowManager * CTX_wm_manager(const bContext *C)
Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C, bool rna_write_check=false)
static wmOperatorStatus pose_clear_transform_generic_exec(bContext *C, wmOperator *op, void(*clear_func)(const bPose *, bPoseChannel *), const char default_ksName[])
static wmOperatorStatus pose_copy_exec(bContext *C, wmOperator *op)
void BKE_pchan_apply_mat4(bPoseChannel *pchan, const float mat[4][4], bool use_compat)
void eul_to_quat(float quat[4], const float eul[3])
void POSE_OT_rot_clear(wmOperatorType *ot)
void BKE_bone_parent_transform_invert(BoneParentTransform *bpt)
static void pchan_clear_rot(bPoseChannel *pchan)
static wmOperatorStatus pose_clear_user_transforms_exec(bContext *C, wmOperator *op)
void unit_m4(float m[4][4])
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
void interp_v3_v3v3(float r[3], const float a[3], const float b[3], float t)
static void pchan_clear_loc(bPoseChannel *pchan)
void ED_armature_edit_free(bArmature *arm)
static void pchan_clear_rot_with_mirrored(const bPose *pose, bPoseChannel *pchan)
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
const char * BKE_tempdir_base() ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL
Definition appdir.cc:1288
bool CTX_data_selected_pose_bones(const bContext *C, Vector< PointerRNA > *list)
MINLINE void zero_v3(float r[3])
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
void POSE_OT_paste(wmOperatorType *ot)
void IDP_SyncGroupValues(IDProperty *dest, const IDProperty *src) ATTR_NONNULL()
Definition idprop.cc:655
static void applyarmature_fix_boneparents(const bContext *C, Scene *scene, Object *armob)
void BKE_main_free(Main *bmain)
Definition main.cc:202
void BKE_pose_copy_data(bPose **dst, const bPose *src, bool copy_constraints)
void BKE_animsys_evaluate_animdata(ID *id, AnimData *adt, const AnimationEvalContext *anim_eval_context, eAnimData_Recalc recalc, bool flush_to_original)
Definition anim_sys.cc:3716
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
void POSE_OT_transforms_clear(wmOperatorType *ot)
void POSE_OT_scale_clear(wmOperatorType *ot)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
MINLINE void copy_v3_fl3(float v[3], float x, float y, float z)
static void pose_bone_copy_transform_values(Object &paste_ob, const bPoseChannel &copy_bone, const bool selected_only)
static constexpr const char * ANIM_KS_ROTATION_ID
float4x4 BKE_object_calc_parent(Depsgraph *depsgraph, Scene *scene, Object *ob)
Definition object.cc:3583
Main * CTX_data_main(const bContext *C)
MINLINE void copy_v3_fl(float r[3], float f)
void BKE_bone_offset_matrix_get(const Bone *bone, float offs_bone[4][4])
void IDP_FreeProperty(IDProperty *prop)
Definition idprop.cc:1403
@ 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
static void blend_properties(IDProperty *copy_props, IDProperty *paste_props, const float factor)
MINLINE float interpf(float target, float origin, float t)
Scene * CTX_data_scene(const bContext *C)
@ ID_FLAG_CLIPBOARD_MARK
Definition DNA_ID.h:801
MINLINE void copy_v4_v4(float r[4], const float a[4])
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
void POSE_OT_loc_clear(wmOperatorType *ot)
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
static constexpr const char * ANIM_KS_LOC_ROT_SCALE_ID
@ MOTIONPATH_BAKE_HAS_PATHS
void BKE_pose_rest(Object &pose_ob, bool selected_bones_only)
Object * CTX_data_active_object(const bContext *C)
static void applyarmature_set_edit_position(EditBone *curbone, const float pose_mat[4][4], const float new_tail[3], float r_new_arm_mat[4][4])
static void applyarmature_reset_constraints(bPose *pose, const bool use_selected)
void POSE_OT_user_transforms_clear(wmOperatorType *ot)
float RNA_float_get(PointerRNA *ptr, const char *name)
bPoseChannel * BKE_pose_channel_find_name(const bPose *pose, const char *name)
void mul_qt_fl(float q[4], float f)
MINLINE bool is_zero_v3(const float v[3]) ATTR_WARN_UNUSED_RESULT
static wmOperatorStatus apply_armature_pose2bones_exec(bContext *C, wmOperator *op)
void POSE_OT_visual_transform_apply(wmOperatorType *ot)
void ED_pose_recalculate_paths(bContext *C, Scene *scene, Object *ob)
Definition pose_edit.cc:146
View3D * CTX_wm_view3d(const bContext *C)
static constexpr const char * ANIM_KS_WHOLE_CHARACTER_ID
static void applyarmature_reset_bone_constraint(const bConstraint *constraint)
static void applyarmature_adjust_edit_position(bArmature *arm, bPoseChannel *pchan, const float delta_mat[4][4], float r_new_arm_mat[4][4])
EditBone * ED_armature_ebone_find_name(const ListBaseT< EditBone > *edbo, const char *name)
bool BKE_object_obdata_is_libdata(const Object *ob)
Definition object.cc:2845
static void pchan_clear_scale(bPoseChannel *pchan)
size_t BLI_string_flip_side_name(char *name_dst, const char *name_src, bool strip_number, size_t name_dst_maxncpy) ATTR_NONNULL(1
float normalize_qt_qt(float r[4], const float q[4])
static CLG_LogRef LOG_POSE_PASTE
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
struct Bone * parent
ListBaseT< Bone > childbase
eBone_InheritScaleMode inherit_scale_mode
eIDPropertyType type
Definition DNA_ID.h:145
char name[258]
Definition DNA_ID.h:433
short flag
Definition DNA_ID.h:439
ListBaseT< Object > objects
Definition BKE_main.hh:388
struct AnimData * adt
struct bPose * pose
struct RenderData r
eMotionPath_BakeFlag path_bakeflag
ListBaseT< EditBone > * edbo
ListBaseT< Bone > bonebase
struct bPoseChannel * parent
ListBaseT< struct bConstraint > constraints
bke::bPoseRuntime * runtime
bAnimVizSettings avs
ListBaseT< bPoseChannel > chanbase
PoseChannelT * pchan
Definition BKE_pose.hh:61
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
const char * description
Definition WM_types.hh:1115
void(*) ui(bContext *C, wmOperator *op)
Definition WM_types.hh:1176
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1165
struct ReportList * reports
struct wmOperatorType * type
bool is_set(uint i)