Blender V4.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
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_blenlib.h"
21#include "BLI_math_matrix.h"
22#include "BLI_math_vector.h"
23#include "BLI_string_utils.hh"
24#include "BLI_utildefines.h"
25
26#include "BKE_action.hh"
27#include "BKE_armature.hh"
28#include "BKE_fcurve.hh"
29
30#include "ANIM_action_legacy.hh"
31
32#include "DEG_depsgraph.hh"
33
34/* -------------------------------------------------------------------- */
79
84 int index,
85 const char *path,
86 FCurvePathCache *fcache)
87{
88 FCurve *fcu = BKE_fcurve_pathcache_find(fcache, path, index);
89 if (fcu && fcu->bezt) {
90 fkc->fcurve = fcu;
91 }
92}
93
98 int fkc_len,
99 const char *path,
100 FCurvePathCache *fcache)
101{
102 FCurve **fcurves = static_cast<FCurve **>(alloca(sizeof(*fcurves) * fkc_len));
103 if (BKE_fcurve_pathcache_find_array(fcache, path, fcurves, fkc_len)) {
104 for (int i = 0; i < fkc_len; i++) {
105 if (fcurves[i] && fcurves[i]->bezt) {
106 fkc[i].fcurve = fcurves[i];
107 }
108 }
109 }
110}
111
120 const float *keyed_frames,
121 int keyed_frames_len)
122{
123 BLI_assert(fkc->fcurve != nullptr);
124
125 /* Cache the F-Curve values for `keyed_frames`. */
126 const int fcurve_flag = fkc->fcurve->flag;
127 fkc->fcurve->flag |= FCURVE_MOD_OFF;
128 fkc->fcurve_eval = static_cast<float *>(MEM_mallocN(sizeof(float) * keyed_frames_len, __func__));
129 for (int frame_index = 0; frame_index < keyed_frames_len; frame_index++) {
130 const float evaltime = keyed_frames[frame_index];
131 fkc->fcurve_eval[frame_index] = evaluate_fcurve_only_curve(fkc->fcurve, evaltime);
132 }
133 fkc->fcurve->flag = fcurve_flag;
134
135 /* Cache the #BezTriple for `keyed_frames`, or leave as nullptr. */
136 fkc->bezt_array = static_cast<BezTriple **>(
137 MEM_mallocN(sizeof(*fkc->bezt_array) * keyed_frames_len, __func__));
138 BezTriple *bezt = fkc->fcurve->bezt;
139 BezTriple *bezt_end = fkc->fcurve->bezt + fkc->fcurve->totvert;
140
141 int frame_index = 0;
142 while (frame_index < keyed_frames_len) {
143 const float evaltime = keyed_frames[frame_index];
144 const float bezt_time = roundf(bezt->vec[1][0]);
145 if (bezt_time > evaltime) {
146 fkc->bezt_array[frame_index++] = nullptr;
147 }
148 else {
149 if (bezt_time == evaltime) {
150 fkc->bezt_array[frame_index++] = bezt;
151 }
152 bezt++;
153 if (bezt == bezt_end) {
154 break;
155 }
156 }
157 }
158 /* Clear remaining unset keyed_frames (if-any). */
159 while (frame_index < keyed_frames_len) {
160 fkc->bezt_array[frame_index++] = nullptr;
161 }
162}
163
166static void action_flip_pchan(Object *ob_arm, const bPoseChannel *pchan, FCurvePathCache *fcache)
167{
168 /* Begin F-Curve pose channel value extraction. */
169 /* Use a fixed buffer size as it's known this can only be at most:
170 * `pose.bones["{MAXBONENAME}"].rotation_quaternion`. */
171 char path_xform[256];
172 char pchan_name_esc[sizeof(bActionChannel::name) * 2];
173 BLI_str_escape(pchan_name_esc, pchan->name, sizeof(pchan_name_esc));
174 const int path_xform_prefix_len = SNPRINTF(path_xform, "pose.bones[\"%s\"]", pchan_name_esc);
175 char *path_xform_suffix = path_xform + path_xform_prefix_len;
176 const int path_xform_suffix_maxncpy = sizeof(path_xform) - path_xform_prefix_len;
177
178 /* Lookup and assign all available #FCurve channels,
179 * unavailable channels are left nullptr. */
180
193 struct {
194 FCurve_KeyCache loc[3], eul[3], quat[4], rotAxis[3], rotAngle, size[3], rotmode;
195 } fkc_pchan = {{{nullptr}}};
196
197#define FCURVE_ASSIGN_VALUE(id, path_test_suffix, index) \
198 BLI_strncpy(path_xform_suffix, path_test_suffix, path_xform_suffix_maxncpy); \
199 action_flip_pchan_cache_fcurve_assign_value(&fkc_pchan.id, index, path_xform, fcache)
200
201#define FCURVE_ASSIGN_ARRAY(id, path_test_suffix) \
202 BLI_strncpy(path_xform_suffix, path_test_suffix, path_xform_suffix_maxncpy); \
203 action_flip_pchan_cache_fcurve_assign_array( \
204 fkc_pchan.id, ARRAY_SIZE(fkc_pchan.id), path_xform, fcache)
205
206 FCURVE_ASSIGN_ARRAY(loc, ".location");
207 FCURVE_ASSIGN_ARRAY(eul, ".rotation_euler");
208 FCURVE_ASSIGN_ARRAY(quat, ".rotation_quaternion");
209 FCURVE_ASSIGN_ARRAY(rotAxis, ".rotation_axis_angle");
210 FCURVE_ASSIGN_VALUE(rotAngle, ".rotation_axis_angle", 3);
211 FCURVE_ASSIGN_ARRAY(size, ".scale");
212 FCURVE_ASSIGN_VALUE(rotmode, ".rotation_mode", 0);
213
214#undef FCURVE_ASSIGN_VALUE
215#undef FCURVE_ASSIGN_ARRAY
216
217/* Array of F-Curves, for convenient access. */
218#define FCURVE_CHANNEL_LEN (sizeof(fkc_pchan) / sizeof(FCurve_KeyCache))
219 FCurve *fcurve_array[FCURVE_CHANNEL_LEN];
220 int fcurve_array_len = 0;
221
222 for (int chan = 0; chan < FCURVE_CHANNEL_LEN; chan++) {
223 FCurve_KeyCache *fkc = (FCurve_KeyCache *)(&fkc_pchan) + chan;
224 if (fkc->fcurve != nullptr) {
225 fcurve_array[fcurve_array_len++] = fkc->fcurve;
226 }
227 }
228
229 /* If this pose has no transform channels, there is nothing to do. */
230 if (fcurve_array_len == 0) {
231 return;
232 }
233
234 /* Calculate an array of frames used by any of the key-frames in `fcurve_array`. */
235 int keyed_frames_len;
236 const float *keyed_frames = BKE_fcurves_calc_keyed_frames(
237 fcurve_array, fcurve_array_len, &keyed_frames_len);
238
239 /* Initialize the pose channel curve cache from the F-Curve. */
240 for (int chan = 0; chan < FCURVE_CHANNEL_LEN; chan++) {
241 FCurve_KeyCache *fkc = (FCurve_KeyCache *)(&fkc_pchan) + chan;
242 if (fkc->fcurve == nullptr) {
243 continue;
244 }
245 action_flip_pchan_cache_init(fkc, keyed_frames, keyed_frames_len);
246 }
247
248 /* X-axis flipping matrix. */
249 float flip_mtx[4][4];
250 unit_m4(flip_mtx);
251 flip_mtx[0][0] = -1;
252
253 bPoseChannel *pchan_flip = nullptr;
254 char pchan_name_flip[MAXBONENAME];
255 BLI_string_flip_side_name(pchan_name_flip, pchan->name, false, sizeof(pchan_name_flip));
256 if (!STREQ(pchan_name_flip, pchan->name)) {
257 pchan_flip = BKE_pose_channel_find_name(ob_arm->pose, pchan_name_flip);
258 }
259
260 float arm_mat_inv[4][4];
261 invert_m4_m4(arm_mat_inv, pchan_flip ? pchan_flip->bone->arm_mat : pchan->bone->arm_mat);
262
263 /* Now flip the transformation & write it back to the F-Curves in `fkc_pchan`. */
264
265 for (int frame_index = 0; frame_index < keyed_frames_len; frame_index++) {
266
267 /* Temporary pose channel to write values into,
268 * using the `fkc_pchan` values, falling back to the values in the pose channel. */
269 bPoseChannel pchan_temp = blender::dna::shallow_copy(*pchan);
270
271/* Load the values into the channel. */
272#define READ_VALUE_FLT(id) \
273 if (fkc_pchan.id.fcurve_eval != nullptr) { \
274 pchan_temp.id = fkc_pchan.id.fcurve_eval[frame_index]; \
275 } \
276 ((void)0)
277
278#define READ_VALUE_INT(id) \
279 if (fkc_pchan.id.fcurve_eval != nullptr) { \
280 pchan_temp.id = floorf(fkc_pchan.id.fcurve_eval[frame_index] + 0.5f); \
281 } \
282 ((void)0)
283
284#define READ_ARRAY_FLT(id) \
285 for (int i = 0; i < ARRAY_SIZE(pchan_temp.id); i++) { \
286 READ_VALUE_FLT(id[i]); \
287 } \
288 ((void)0)
289
290 READ_ARRAY_FLT(loc);
291 READ_ARRAY_FLT(eul);
292 READ_ARRAY_FLT(quat);
293 READ_ARRAY_FLT(rotAxis);
294 READ_VALUE_FLT(rotAngle);
295 READ_ARRAY_FLT(size);
296 READ_VALUE_INT(rotmode);
297
298#undef READ_ARRAY_FLT
299#undef READ_VALUE_FLT
300#undef READ_VALUE_INT
301
302 float chan_mat[4][4];
303 BKE_pchan_to_mat4(&pchan_temp, chan_mat);
304
305 /* Move to the pose-space. */
306 mul_m4_m4m4(chan_mat, pchan->bone->arm_mat, chan_mat);
307
308 /* Flip the matrix. */
309 mul_m4_m4m4(chan_mat, chan_mat, flip_mtx);
310 mul_m4_m4m4(chan_mat, flip_mtx, chan_mat);
311
312 /* Move back to bone-space space, using the flipped bone if it exists. */
313 mul_m4_m4m4(chan_mat, arm_mat_inv, chan_mat);
314
315 /* The rest pose having an X-axis that is not mapping to a left/right direction (so aligned
316 * with the Y or Z axis) creates issues when flipping the pose. Instead of a negative scale on
317 * the X-axis, it turns into a 180 degree rotation over the Y-axis.
318 * This has only been observed with bones that can't be flipped,
319 * hence the check for `pchan_flip`. */
320 const float unit_x[3] = {1.0f, 0.0f, 0.0f};
321 const bool is_x_axis_orthogonal = (pchan_flip == nullptr) &&
322 (fabsf(dot_v3v3(pchan->bone->arm_mat[0], unit_x)) <= 1e-6f);
323 if (is_x_axis_orthogonal) {
324 /* Matrix needs to flip both the X and Z axes to come out right. */
325 float extra_mat[4][4] = {
326 {-1.0f, 0.0f, 0.0f, 0.0f},
327 {0.0f, 1.0f, 0.0f, 0.0f},
328 {0.0f, 0.0f, -1.0f, 0.0f},
329 {0.0f, 0.0f, 0.0f, 1.0f},
330 };
331 mul_m4_m4m4(chan_mat, extra_mat, chan_mat);
332 }
333
334 BKE_pchan_apply_mat4(&pchan_temp, chan_mat, false);
335
336/* Write the values back to the F-Curves. */
337#define WRITE_VALUE_FLT(id) \
338 if (fkc_pchan.id.fcurve_eval != nullptr) { \
339 BezTriple *bezt = fkc_pchan.id.bezt_array[frame_index]; \
340 if (bezt != nullptr) { \
341 const float delta = pchan_temp.id - bezt->vec[1][1]; \
342 bezt->vec[0][1] += delta; \
343 bezt->vec[1][1] += delta; \
344 bezt->vec[2][1] += delta; \
345 } \
346 } \
347 ((void)0)
348
349#define WRITE_ARRAY_FLT(id) \
350 for (int i = 0; i < ARRAY_SIZE(pchan_temp.id); i++) { \
351 WRITE_VALUE_FLT(id[i]); \
352 } \
353 ((void)0)
354
355 /* Write the values back the F-Curves. */
356 WRITE_ARRAY_FLT(loc);
357 WRITE_ARRAY_FLT(eul);
358 WRITE_ARRAY_FLT(quat);
359 WRITE_ARRAY_FLT(rotAxis);
360 WRITE_VALUE_FLT(rotAngle);
361 WRITE_ARRAY_FLT(size);
362 /* No need to write back 'rotmode' as it can't be transformed. */
363
364#undef WRITE_ARRAY_FLT
365#undef WRITE_VALUE_FLT
366 }
367
368 /* Recalculate handles. */
369 for (int i = 0; i < fcurve_array_len; i++) {
371 }
372
373 MEM_freeN((void *)keyed_frames);
374
375 for (int chan = 0; chan < FCURVE_CHANNEL_LEN; chan++) {
376 FCurve_KeyCache *fkc = (FCurve_KeyCache *)(&fkc_pchan) + chan;
377 if (fkc->fcurve_eval) {
379 }
380 if (fkc->bezt_array) {
381 MEM_freeN(fkc->bezt_array);
382 }
383 }
384}
385
390{
391 const char *path_pose_prefix = "pose.bones[\"";
392 const int path_pose_prefix_len = strlen(path_pose_prefix);
393
394 /* Tag curves that have renamed f-curves. */
396 agrp->flag &= ~AGRP_TEMP;
397 }
398
400 if (!STRPREFIX(fcu->rna_path, path_pose_prefix)) {
401 continue;
402 }
403
404 const char *name_esc = fcu->rna_path + path_pose_prefix_len;
405 const char *name_esc_end = BLI_str_escape_find_quote(name_esc);
406
407 /* While unlikely, an RNA path could be malformed. */
408 if (UNLIKELY(name_esc_end == nullptr)) {
409 continue;
410 }
411
412 char name[MAXBONENAME];
413 const size_t name_esc_len = size_t(name_esc_end - name_esc);
414 const size_t name_len = BLI_str_unescape(name, name_esc, name_esc_len);
415
416 /* While unlikely, data paths could be constructed that have longer names than
417 * are currently supported. */
418 if (UNLIKELY(name_len >= sizeof(name))) {
419 continue;
420 }
421
422 /* When the flipped name differs, perform the rename. */
423 char name_flip[MAXBONENAME];
424 BLI_string_flip_side_name(name_flip, name, false, sizeof(name_flip));
425 if (!STREQ(name_flip, name)) {
426 char name_flip_esc[MAXBONENAME * 2];
427 BLI_str_escape(name_flip_esc, name_flip, sizeof(name_flip_esc));
428 char *path_flip = BLI_sprintfN("pose.bones[\"%s%s", name_flip_esc, name_esc_end);
429 MEM_freeN(fcu->rna_path);
430 fcu->rna_path = path_flip;
431
432 if (fcu->grp != nullptr) {
433 fcu->grp->flag |= AGRP_TEMP;
434 }
435 }
436 }
437
438 /* Rename tagged groups. */
440 if ((agrp->flag & AGRP_TEMP) == 0) {
441 continue;
442 }
443 agrp->flag &= ~AGRP_TEMP;
444 char name_flip[MAXBONENAME];
445 BLI_string_flip_side_name(name_flip, agrp->name, false, sizeof(name_flip));
446 if (!STREQ(name_flip, agrp->name)) {
447 STRNCPY(agrp->name, name_flip);
448 }
449 }
450}
451
453{
455 int i;
456 LISTBASE_FOREACH_INDEX (bPoseChannel *, pchan, &ob_arm->pose->chanbase, i) {
457 action_flip_pchan(ob_arm, pchan, fcache);
458 }
460
462
464}
465
Functions for backward compatibility with the legacy Action API.
Blender kernel action and pose functionality.
bPoseChannel * BKE_pose_channel_find_name(const bPose *pose, const char *name)
void BKE_pchan_apply_mat4(bPoseChannel *pchan, const float mat[4][4], bool use_compat)
Definition armature.cc:2358
void BKE_pchan_to_mat4(const bPoseChannel *pchan, float r_chanmat[4][4])
Definition armature.cc:2837
float * BKE_fcurves_calc_keyed_frames(FCurve **fcurve_array, int fcurve_array_len, int *r_frames_len)
void BKE_fcurve_pathcache_destroy(FCurvePathCache *fcache)
void BKE_fcurve_handles_recalc_ex(FCurve *fcu, eBezTriple_Flag handle_sel_flag)
float evaluate_fcurve_only_curve(const FCurve *fcu, float evaltime)
int BKE_fcurve_pathcache_find_array(FCurvePathCache *fcache, const char *rna_path, FCurve **fcurve_result, int fcurve_result_len)
FCurve * BKE_fcurve_pathcache_find(FCurvePathCache *fcache, const char rna_path[], int array_index)
FCurvePathCache * BKE_fcurve_pathcache_create(ListBase *list)
#define BLI_assert(a)
Definition BLI_assert.h:50
#define LISTBASE_FOREACH_INDEX(type, var, list, index_var)
void mul_m4_m4m4(float R[4][4], const float A[4][4], const float B[4][4])
void unit_m4(float m[4][4])
Definition rct.c:1127
bool invert_m4_m4(float inverse[4][4], const float mat[4][4])
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
char * BLI_sprintfN(const char *__restrict format,...) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC ATTR_PRINTF_FORMAT(1
size_t size_t size_t const char * BLI_str_escape_find_quote(const char *str) ATTR_NONNULL(1)
Definition string.c:434
#define STRNCPY(dst, src)
Definition BLI_string.h:593
size_t size_t size_t BLI_str_unescape(char *__restrict dst, const char *__restrict src, size_t src_maxncpy) ATTR_NONNULL(1
#define SNPRINTF(dst, format,...)
Definition BLI_string.h:597
size_t BLI_str_escape(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
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 STRPREFIX(a, b)
#define UNLIKELY(x)
#define STREQ(a, b)
void DEG_id_tag_update(ID *id, unsigned int flags)
@ ID_RECALC_SYNC_TO_EVAL
Definition DNA_ID.h:1085
@ AGRP_TEMP
@ FCURVE_MOD_OFF
#define MAXBONENAME
eBezTriple_Flag
Object is a sort of wrapper for general info.
Read Guarded memory(de)allocation.
#define READ_VALUE_FLT(id)
static void action_flip_pchan_rna_paths(bAction *act)
static void action_flip_pchan_cache_fcurve_assign_value(FCurve_KeyCache *fkc, int index, const char *path, FCurvePathCache *fcache)
#define WRITE_ARRAY_FLT(id)
static void action_flip_pchan_cache_init(FCurve_KeyCache *fkc, const float *keyed_frames, int keyed_frames_len)
static void action_flip_pchan_cache_fcurve_assign_array(FCurve_KeyCache *fkc, int fkc_len, const char *path, FCurvePathCache *fcache)
#define READ_VALUE_INT(id)
#define FCURVE_ASSIGN_ARRAY(id, path_test_suffix)
#define FCURVE_CHANNEL_LEN
static void action_flip_pchan(Object *ob_arm, const bPoseChannel *pchan, FCurvePathCache *fcache)
#define WRITE_VALUE_FLT(id)
#define READ_ARRAY_FLT(id)
void BKE_action_flip_with_pose(bAction *act, Object *ob_arm)
#define FCURVE_ASSIGN_VALUE(id, path_test_suffix, index)
float evaltime
#define fabsf(x)
void *(* MEM_mallocN)(size_t len, const char *str)
Definition mallocn.cc:44
void MEM_freeN(void *vmemh)
Definition mallocn.cc:105
Vector< const FCurve * > fcurves_all(const bAction *action)
Vector< bActionGroup * > channel_groups_all(bAction *action)
float vec[3][3]
float arm_mat[4][4]
BezTriple ** bezt_array
BezTriple * bezt
unsigned int totvert
struct bPose * pose
ListBase curves
struct Bone * bone
ListBase chanbase