Blender V5.3
action_mirror.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include <cmath>
12#include <cstring>
13
14#include "MEM_guardedalloc.h"
15
16#include "DNA_anim_types.h"
17#include "DNA_armature_types.h"
18#include "DNA_object_types.h"
19
20#include "BLI_listbase.hh"
21#include "BLI_math_matrix_c.hh"
22#include "BLI_math_vector_c.hh"
23#include "BLI_string.hh"
24#include "BLI_string_utf8.hh"
25#include "BLI_string_utils.hh"
26#include "BLI_utildefines.hh"
27
28#include "BKE_action.hh"
29#include "BKE_armature.hh"
30#include "BKE_fcurve.hh"
31
32#include "ANIM_action_legacy.hh"
33
34#include "DEG_depsgraph.hh"
35
36namespace blender {
37
38/* -------------------------------------------------------------------- */
57
83
88 int index,
89 const char *path,
90 Map<RNAPath, FCurve *> &fcurve_cache)
91{
92 FCurve *fcu = fcurve_cache.lookup_default({path, std::nullopt, index}, nullptr);
93 if (fcu && fcu->bezt) {
94 fkc->fcurve = fcu;
95 }
96}
97
102 int fkc_len,
103 const char *path,
104 Map<RNAPath, FCurve *> &fcurve_cache)
105{
106 for (int i = 0; i < fkc_len; i++) {
107 FCurve *fcu = fcurve_cache.lookup_default({path, std::nullopt, i}, nullptr);
108 if (!fcu) {
109 continue;
110 }
111 if (fcu && fcu->bezt) {
112 fkc[i].fcurve = fcu;
113 }
114 }
115}
116
123static void action_flip_pchan_cache_init(FCurve_KeyCache *fkc, const Span<float> keyed_frames)
124{
125 BLI_assert(fkc->fcurve != nullptr);
126
127 /* Cache the F-Curve values for `keyed_frames`. */
128 const eFCurve_Flags fcurve_flag = fkc->fcurve->flag;
129 fkc->fcurve->flag |= FCURVE_MOD_OFF;
130 const int keyed_frames_len = keyed_frames.size();
131 fkc->fcurve_eval = MEM_new_array_uninitialized<float>(size_t(keyed_frames_len), __func__);
132 for (int frame_index = 0; frame_index < keyed_frames_len; frame_index++) {
133 const float evaltime = keyed_frames[frame_index];
134 fkc->fcurve_eval[frame_index] = evaluate_fcurve_only_curve(fkc->fcurve, evaltime);
135 }
136 fkc->fcurve->flag = fcurve_flag;
137
138 /* Cache the #BezTriple for `keyed_frames`, or leave as nullptr. */
139 fkc->bezt_array = MEM_new_array_uninitialized<BezTriple *>(size_t(keyed_frames_len), __func__);
140 BezTriple *bezt = fkc->fcurve->bezt;
141 BezTriple *bezt_end = fkc->fcurve->bezt + fkc->fcurve->totvert;
142
143 int frame_index = 0;
144 while (frame_index < keyed_frames_len) {
145 const float evaltime = keyed_frames[frame_index];
146 const float bezt_time = roundf(bezt->vec[1][0]);
147 if (bezt_time > evaltime) {
148 fkc->bezt_array[frame_index++] = nullptr;
149 }
150 else {
151 if (bezt_time == evaltime) {
152 fkc->bezt_array[frame_index++] = bezt;
153 }
154 bezt++;
155 if (bezt == bezt_end) {
156 break;
157 }
158 }
159 }
160 /* Clear remaining unset keyed_frames (if-any). */
161 while (frame_index < keyed_frames_len) {
162 fkc->bezt_array[frame_index++] = nullptr;
163 }
164}
165
169static void action_flip_pchan(Object *ob_arm,
170 const bPoseChannel *pchan,
171 Map<RNAPath, FCurve *> &fcurve_cache)
172{
173 /* Begin F-Curve pose channel value extraction. */
174 /* Use a fixed buffer size as it's known this can only be at most:
175 * `pose.bones["{MAXBONENAME}"].rotation_quaternion`. */
176 char path_xform[256];
177 char pchan_name_esc[sizeof(pchan->name) * 2];
178 BLI_str_escape(pchan_name_esc, pchan->name, sizeof(pchan_name_esc));
179 const int path_xform_prefix_len = SNPRINTF_UTF8(
180 path_xform, "pose.bones[\"%s\"]", pchan_name_esc);
181 char *path_xform_suffix = path_xform + path_xform_prefix_len;
182 const int path_xform_suffix_maxncpy = sizeof(path_xform) - path_xform_prefix_len;
183
184 /* Lookup and assign all available #FCurve channels,
185 * unavailable channels are left nullptr. */
186
199 struct {
200 FCurve_KeyCache loc[3], eul[3], quat[4], rotAxis[3], rotAngle, scale[3], rotmode;
201 } fkc_pchan = {{{nullptr}}};
202
203#define FCURVE_ASSIGN_VALUE(id, path_test_suffix, index) \
204 BLI_strncpy(path_xform_suffix, path_test_suffix, path_xform_suffix_maxncpy); \
205 action_flip_pchan_cache_fcurve_assign_value(&fkc_pchan.id, index, path_xform, fcurve_cache)
206
207#define FCURVE_ASSIGN_ARRAY(id, path_test_suffix) \
208 BLI_strncpy(path_xform_suffix, path_test_suffix, path_xform_suffix_maxncpy); \
209 action_flip_pchan_cache_fcurve_assign_array( \
210 fkc_pchan.id, ARRAY_SIZE(fkc_pchan.id), path_xform, fcurve_cache)
211
212 FCURVE_ASSIGN_ARRAY(loc, ".location");
213 FCURVE_ASSIGN_ARRAY(eul, ".rotation_euler");
214 FCURVE_ASSIGN_ARRAY(quat, ".rotation_quaternion");
215 FCURVE_ASSIGN_ARRAY(rotAxis, ".rotation_axis_angle");
216 FCURVE_ASSIGN_VALUE(rotAngle, ".rotation_axis_angle", 3);
217 FCURVE_ASSIGN_ARRAY(scale, ".scale");
218 FCURVE_ASSIGN_VALUE(rotmode, ".rotation_mode", 0);
219
220#undef FCURVE_ASSIGN_VALUE
221#undef FCURVE_ASSIGN_ARRAY
222
223/* Array of F-Curves, for convenient access. */
224#define FCURVE_CHANNEL_LEN (sizeof(fkc_pchan) / sizeof(FCurve_KeyCache))
225 Vector<FCurve *> fcurve_array;
226
227 for (int chan = 0; chan < FCURVE_CHANNEL_LEN; chan++) {
228 FCurve_KeyCache *fkc = reinterpret_cast<FCurve_KeyCache *>(&fkc_pchan) + chan;
229 if (fkc->fcurve != nullptr) {
230 fcurve_array.append(fkc->fcurve);
231 }
232 }
233
234 /* If this pose has no transform channels, there is nothing to do. */
235 if (fcurve_array.is_empty()) {
236 return;
237 }
238
239 /* Calculate an array of frames used by any of the key-frames in `fcurve_array`. */
240 const Array<float> keyed_frames = BKE_fcurves_calc_keyed_frames(fcurve_array);
241
242 /* Initialize the pose channel curve cache from the F-Curve. */
243 for (int chan = 0; chan < FCURVE_CHANNEL_LEN; chan++) {
244 FCurve_KeyCache *fkc = reinterpret_cast<FCurve_KeyCache *>(&fkc_pchan) + chan;
245 if (fkc->fcurve == nullptr) {
246 continue;
247 }
248 action_flip_pchan_cache_init(fkc, keyed_frames);
249 }
250
251 /* X-axis flipping matrix. */
252 float flip_mtx[4][4];
253 unit_m4(flip_mtx);
254 flip_mtx[0][0] = -1;
255
256 bPoseChannel *pchan_flip = nullptr;
257 char pchan_name_flip[MAXBONENAME];
258 BLI_string_flip_side_name(pchan_name_flip, pchan->name, false, sizeof(pchan_name_flip));
259 if (!STREQ(pchan_name_flip, pchan->name)) {
260 pchan_flip = BKE_pose_channel_find_name(ob_arm->pose, pchan_name_flip);
261 }
262
263 const Bone *pchan_bone = pchan->bone_get(*ob_arm);
264 const Bone *pchan_flip_bone = pchan_flip ? pchan_flip->bone_get(*ob_arm) : nullptr;
265
266 float arm_mat_inv[4][4];
267 invert_m4_m4(arm_mat_inv, pchan_flip ? pchan_flip_bone->arm_mat : pchan_bone->arm_mat);
268
269 /* Now flip the transformation & write it back to the F-Curves in `fkc_pchan`. */
270
271 for (int frame_index = 0; frame_index < keyed_frames.size(); frame_index++) {
272
273 /* Temporary pose channel to write values into,
274 * using the `fkc_pchan` values, falling back to the values in the pose channel. */
275 bPoseChannel pchan_temp = dna::shallow_copy(*pchan);
276
277/* Load the values into the channel. */
278#define READ_VALUE_FLT(id) \
279 if (fkc_pchan.id.fcurve_eval != nullptr) { \
280 pchan_temp.id = fkc_pchan.id.fcurve_eval[frame_index]; \
281 } \
282 ((void)0)
283
284#define READ_VALUE_INT(id) \
285 if (fkc_pchan.id.fcurve_eval != nullptr) { \
286 pchan_temp.id = decltype(pchan_temp.id)( \
287 int(floorf(fkc_pchan.id.fcurve_eval[frame_index] + 0.5f))); \
288 } \
289 ((void)0)
290
291#define READ_ARRAY_FLT(id) \
292 for (int i = 0; i < ARRAY_SIZE(pchan_temp.id); i++) { \
293 READ_VALUE_FLT(id[i]); \
294 } \
295 ((void)0)
296
297 READ_ARRAY_FLT(loc);
298 READ_ARRAY_FLT(eul);
299 READ_ARRAY_FLT(quat);
300 READ_ARRAY_FLT(rotAxis);
301 READ_VALUE_FLT(rotAngle);
303 READ_VALUE_INT(rotmode);
304
305#undef READ_ARRAY_FLT
306#undef READ_VALUE_FLT
307#undef READ_VALUE_INT
308
309 float chan_mat[4][4];
310 BKE_pchan_to_mat4({&pchan_temp, pchan_bone}, chan_mat);
311
312 /* Move to the pose-space. */
313 mul_m4_m4m4(chan_mat, pchan_bone->arm_mat, chan_mat);
314
315 /* Flip the matrix. */
316 mul_m4_m4m4(chan_mat, chan_mat, flip_mtx);
317 mul_m4_m4m4(chan_mat, flip_mtx, chan_mat);
318
319 /* Move back to bone-space space, using the flipped bone if it exists. */
320 mul_m4_m4m4(chan_mat, arm_mat_inv, chan_mat);
321
322 /* The rest pose having an X-axis that is not mapping to a left/right direction (so aligned
323 * with the Y or Z axis) creates issues when flipping the pose. Instead of a negative scale on
324 * the X-axis, it turns into a 180 degree rotation over the Y-axis.
325 * This has only been observed with bones that can't be flipped,
326 * hence the check for `pchan_flip`. */
327 const float unit_x[3] = {1.0f, 0.0f, 0.0f};
328 const bool is_x_axis_orthogonal = (pchan_flip == nullptr) &&
329 (fabsf(dot_v3v3(pchan_bone->arm_mat[0], unit_x)) <= 1e-6f);
330 if (is_x_axis_orthogonal) {
331 /* Matrix needs to flip both the X and Z axes to come out right. */
332 float extra_mat[4][4] = {
333 {-1.0f, 0.0f, 0.0f, 0.0f},
334 {0.0f, 1.0f, 0.0f, 0.0f},
335 {0.0f, 0.0f, -1.0f, 0.0f},
336 {0.0f, 0.0f, 0.0f, 1.0f},
337 };
338 mul_m4_m4m4(chan_mat, extra_mat, chan_mat);
339 }
340
341 BKE_pchan_apply_mat4(&pchan_temp, chan_mat, false);
342
343/* Write the values back to the F-Curves. */
344#define WRITE_VALUE_FLT(id) \
345 if (fkc_pchan.id.fcurve_eval != nullptr) { \
346 BezTriple *bezt = fkc_pchan.id.bezt_array[frame_index]; \
347 if (bezt != nullptr) { \
348 const float delta = pchan_temp.id - bezt->vec[1][1]; \
349 bezt->vec[0][1] += delta; \
350 bezt->vec[1][1] += delta; \
351 bezt->vec[2][1] += delta; \
352 } \
353 } \
354 ((void)0)
355
356#define WRITE_ARRAY_FLT(id) \
357 for (int i = 0; i < ARRAY_SIZE(pchan_temp.id); i++) { \
358 WRITE_VALUE_FLT(id[i]); \
359 } \
360 ((void)0)
361
362 /* Write the values back the F-Curves. */
363 WRITE_ARRAY_FLT(loc);
364 WRITE_ARRAY_FLT(eul);
365 WRITE_ARRAY_FLT(quat);
366 WRITE_ARRAY_FLT(rotAxis);
367 WRITE_VALUE_FLT(rotAngle);
369 /* No need to write back 'rotmode' as it can't be transformed. */
370
371#undef WRITE_ARRAY_FLT
372#undef WRITE_VALUE_FLT
373 }
374
375 /* Recalculate handles. */
376 for (FCurve *fcurve : fcurve_array) {
378 }
379
380 for (int chan = 0; chan < FCURVE_CHANNEL_LEN; chan++) {
381 FCurve_KeyCache *fkc = reinterpret_cast<FCurve_KeyCache *>(&fkc_pchan) + chan;
382 if (fkc->fcurve_eval) {
383 MEM_delete(fkc->fcurve_eval);
384 }
385 if (fkc->bezt_array) {
386 MEM_delete(fkc->bezt_array);
387 }
388 }
389}
390
395{
396 const char *path_pose_prefix = "pose.bones[\"";
397 const int path_pose_prefix_len = strlen(path_pose_prefix);
398
399 /* Tag curves that have renamed f-curves. */
401 agrp->flag &= ~AGRP_TEMP;
402 }
403
404 for (FCurve *fcu : animrig::legacy::fcurves_all(act)) {
405 if (!fcu->rna_path().startswith(path_pose_prefix)) {
406 continue;
407 }
408
409 const char *name_esc = fcu->rna_path().c_str() + path_pose_prefix_len;
410 const char *name_esc_end = BLI_str_escape_find_quote(name_esc);
411
412 /* While unlikely, an RNA path could be malformed. */
413 if (name_esc_end == nullptr) [[unlikely]] {
414 continue;
415 }
416
417 char name[MAXBONENAME];
418 const size_t name_esc_len = size_t(name_esc_end - name_esc);
419 bool is_complete;
420 BLI_str_unescape_ex(name, name_esc, name_esc_len, sizeof(name), &is_complete);
421
422 /* While unlikely, data paths could be constructed that have longer names than
423 * are currently supported. */
424 if (!is_complete) [[unlikely]] {
425 continue;
426 }
427
428 /* When the flipped name differs, perform the rename. */
429 char name_flip[MAXBONENAME];
430 BLI_string_flip_side_name(name_flip, name, false, sizeof(name_flip));
431 if (!STREQ(name_flip, name)) {
432 char name_flip_esc[MAXBONENAME * 2];
433 BLI_str_escape(name_flip_esc, name_flip, sizeof(name_flip_esc));
434 fcu->rna_path_set_move(BLI_sprintfN("pose.bones[\"%s%s", name_flip_esc, name_esc_end));
435
436 if (fcu->grp != nullptr) {
437 fcu->grp->flag |= AGRP_TEMP;
438 }
439 }
440 }
441
442 /* Rename tagged groups. */
444 if ((agrp->flag & AGRP_TEMP) == 0) {
445 continue;
446 }
447 agrp->flag &= ~AGRP_TEMP;
448 char name_flip[MAXBONENAME];
449 BLI_string_flip_side_name(name_flip, agrp->name, false, sizeof(name_flip));
450 if (!STREQ(name_flip, agrp->name)) {
451 STRNCPY_UTF8(agrp->name, name_flip);
452 }
453 }
454}
455
457{
458 animrig::Action &action = act->wrap();
459 if (action.slot_array_num == 0) {
460 /* Cannot flip an empty action. */
461 return;
462 }
463 Set<animrig::Slot *> flipped_slots;
464 for (Object *object : objects) {
466 if (!slot) {
467 slot = action.slot(0);
468 }
469 if (!flipped_slots.add(slot)) {
470 continue;
471 }
473 Map<RNAPath, FCurve *> fcu_cache;
474 for (FCurve *fcu : fcurves) {
475 fcu_cache.add({fcu->rna_path(), std::nullopt, fcu->array_index}, fcu);
476 }
477 for (bPoseChannel &pchan : object->pose->chanbase) {
478 action_flip_pchan(object, &pchan, fcu_cache);
479 }
480 }
481
483
485}
486
488
489} // namespace blender
Functions for backward compatibility with the legacy Action API.
Blender kernel action and pose functionality.
#define SNPRINTF_UTF8(dst, format,...)
#define STRNCPY_UTF8(dst, src)
#define MAXBONENAME
Object is a sort of wrapper for general info.
Read Blender memory allocation and freeing.
#define READ_VALUE_FLT(id)
#define WRITE_ARRAY_FLT(id)
#define READ_VALUE_INT(id)
#define FCURVE_ASSIGN_ARRAY(id, path_test_suffix)
#define FCURVE_CHANNEL_LEN
#define WRITE_VALUE_FLT(id)
#define READ_ARRAY_FLT(id)
#define FCURVE_ASSIGN_VALUE(id, path_test_suffix, index)
#define STREQ(a, b)
int64_t size() const
Definition BLI_array.hh:267
bool add(const Key &key, const Value &value)
Definition BLI_map.hh:295
Value lookup_default(const Key &key, const Value &default_value) const
Definition BLI_map.hh:570
bool add(const Key &key)
Definition BLI_set.hh:248
constexpr int64_t size() const
Definition BLI_span.hh:256
void append(const T &value)
bool is_empty() const
const Slot * slot(int64_t index) const
#define roundf(x)
float3x3 scale(float3x3 mat, float2 scale)
Vector< const FCurve * > fcurves_all(const bAction *action)
Vector< bActionGroup * > channel_groups_all(bAction *action)
Span< FCurve * > fcurves_for_action_slot(Action &action, slot_handle_t slot_handle)
Slot * generic_slot_for_autoassign(const ID &animated_id, Action &action, StringRefNull last_slot_identifier)
internal::ShallowDataConstRef< T > shallow_copy(const T &other)
Definition DNA_defs.h:147
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static void action_flip_pchan_cache_init(FCurve_KeyCache *fkc, const Span< float > keyed_frames)
static void action_flip_pchan_rna_paths(bAction *act)
static void action_flip_pchan(Object *ob_arm, const bPoseChannel *pchan, Map< RNAPath, FCurve * > &fcurve_cache)
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
bool invert_m4_m4(float inverse[4][4], const float mat[4][4])
static void action_flip_pchan_cache_fcurve_assign_array(FCurve_KeyCache *fkc, int fkc_len, const char *path, Map< RNAPath, FCurve * > &fcurve_cache)
char * BLI_sprintfN(const char *__restrict format,...) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC ATTR_PRINTF_FORMAT(1
void BKE_fcurve_handles_recalc_ex(FCurve &fcu, eBezTriple_Flag handle_sel_flag)
void mul_m4_m4m4(float R[4][4], const float A[4][4], const float B[4][4])
void BKE_pchan_to_mat4(bke::PChanBoneConst pchanbone, float r_chanmat[4][4])
static void action_flip_pchan_cache_fcurve_assign_value(FCurve_KeyCache *fkc, int index, const char *path, Map< RNAPath, FCurve * > &fcurve_cache)
void void void void void BKE_action_flip_with_pose(bAction *act, Span< Object * > objects) ATTR_NONNULL(1)
size_t size_t const char * BLI_str_escape_find_quote(const char *str) ATTR_NONNULL(1)
void DEG_id_tag_update(ID *id, unsigned int flags)
char char size_t BLI_str_escape(char *__restrict dst, StringRef src, size_t dst_maxncpy) ATTR_NONNULL(1)
void BKE_pchan_apply_mat4(bPoseChannel *pchan, const float mat[4][4], bool use_compat)
void unit_m4(float m[4][4])
size_t BLI_str_unescape_ex(char *__restrict dst, const char *__restrict src, size_t src_len, size_t dst_maxncpy, bool *r_is_complete) ATTR_NONNULL(1
float evaluate_fcurve_only_curve(const FCurve *fcu, float evaltime)
Array< float > BKE_fcurves_calc_keyed_frames(Span< FCurve * > fcurve_array)
@ ID_RECALC_SYNC_TO_EVAL
Definition DNA_ID.h:1118
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
bPoseChannel * BKE_pose_channel_find_name(const bPose *pose, const char *name)
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
#define fabsf
eFCurve_Flags flag
unsigned int totvert
struct bPose * pose