Blender V5.3
transform_convert_graph.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_anim_types.h"
10#include "DNA_space_types.h"
11
12#include "MEM_guardedalloc.h"
13
14#include "BLI_listbase.hh"
15#include "BLI_map.hh"
16#include "BLI_math_matrix_c.hh"
17#include "BLI_math_vector_c.hh"
18#include "BLI_set.hh"
19
20#include "BKE_context.hh"
21#include "BKE_fcurve.hh"
22#include "BKE_layer.hh"
23#include "BKE_nla.hh"
24
25#include "ED_anim_api.hh"
26#include "ED_keyframes_edit.hh"
27
28#include "UI_view2d.hh"
29
30#include "transform.hh"
32#include "transform_convert.hh"
33#include "transform_snap.hh"
34
35namespace blender::ed::transform {
36
39 float offset;
40};
41
42/* -------------------------------------------------------------------- */
45
51 TransData2D *td2d,
52 TransDataGraph *tdg,
53 bAnimListElem *ale,
54 BezTriple *bezt,
55 int bi,
56 bool selected,
57 bool ishandle,
58 bool intvals,
59 const float mtx[3][3],
60 const float smtx[3][3],
61 float unit_scale,
62 float offset)
63{
64 float *loc = bezt->vec[bi];
65 const float *cent = bezt->vec[1];
66
67 /* New location from td gets dumped onto the old-location of td2d, which then
68 * gets copied to the actual data at td2d->loc2d (bezt->vec[n])
69 *
70 * Due to NLA mapping, we apply NLA mapping to some of the verts here,
71 * and then that mapping will be undone after transform is done.
72 */
73
74 if (ANIM_nla_mapping_allowed(ale)) {
75 td2d->loc[0] = ANIM_nla_tweakedit_remap(ale, loc[0], NLATIME_CONVERT_MAP);
76 td2d->loc[1] = (loc[1] + offset) * unit_scale;
77 td2d->loc[2] = 0.0f;
78 td2d->loc2d = loc;
79
80 td->loc = td2d->loc;
82 td->center[1] = (cent[1] + offset) * unit_scale;
83 td->center[2] = 0.0f;
84
85 copy_v3_v3(td->iloc, td->loc);
86 }
87 else {
88 td2d->loc[0] = loc[0];
89 td2d->loc[1] = (loc[1] + offset) * unit_scale;
90 td2d->loc[2] = 0.0f;
91 td2d->loc2d = loc;
92
93 td->loc = td2d->loc;
94 copy_v3_v3(td->center, cent);
95 td->center[1] = (td->center[1] + offset) * unit_scale;
96 copy_v3_v3(td->iloc, td->loc);
97 }
98
99 if (!ishandle) {
100 td2d->h1 = bezt->vec[0];
101 td2d->h2 = bezt->vec[2];
102 copy_v2_v2(td2d->ih1, td2d->h1);
103 copy_v2_v2(td2d->ih2, td2d->h2);
104 }
105 else {
106 td2d->h1 = nullptr;
107 td2d->h2 = nullptr;
108 }
109
110 memset(td->axismtx, 0, sizeof(td->axismtx));
111 td->axismtx[2][2] = 1.0f;
112
113 td->val = nullptr;
114
115 /* Store AnimData info in td->extra, for applying mapping when flushing.
116 *
117 * We do this conditionally as a hacky way of indicating whether NLA remapping
118 * should be done. This is left over from old code, most of which was changed
119 * in #130440 to avoid using `adt == nullptr` as an indicator for that. This
120 * was left that way because updating it cleanly was more involved than made
121 * sense for the bug fix in #130440. */
122 if (ANIM_nla_mapping_allowed(ale)) {
123 td->extra = ale->adt;
124 }
125
126 if (selected) {
127 td->flag |= TD_SELECTED;
128 td->dist = 0.0f;
129 }
130 else {
131 td->dist = FLT_MAX;
132 }
133
134 if (ishandle) {
135 td->flag |= TD_NOTIMESNAP;
136 }
137 if (intvals) {
138 td->flag |= TD_INTVALUES;
139 }
140
141 /* Copy space-conversion matrices for dealing with non-uniform scales. */
142 copy_m3_m3(td->mtx, mtx);
143 copy_m3_m3(td->smtx, smtx);
144
145 tdg->unit_scale = unit_scale;
146 tdg->offset = offset;
147}
148
153
155{
156 return ((t->around == V3D_AROUND_LOCAL_ORIGINS) && (graph_edit_is_translation_mode(t) == false));
157}
158
159static void enable_autolock(TransInfo *t, SpaceGraph *space_graph)
160{
161 /* Locking the axis makes most sense for translation. We may want to enable it for scaling as
162 * well if artists require that. */
163 if (t->mode != TFM_TRANSLATION) {
164 return;
165 }
166
167 /* These flags are set when using tweak mode on handles. */
170 {
171 return;
172 }
173
175}
176
182 const BezTriple *bezt,
183 const bool use_handle,
184 bool *r_left_handle,
185 bool *r_key,
186 bool *r_right_handle)
187{
188 SpaceGraph *sipo = t->area->spacedata.first_as<SpaceGraph>();
189 bool key = (bezt->f2 & SELECT) != 0;
190 bool left = use_handle ? ((bezt->f1 & SELECT) != 0) : key;
191 bool right = use_handle ? ((bezt->f3 & SELECT) != 0) : key;
192
193 if (use_handle && t->is_launch_event_drag) {
195 key = right = false;
196 }
198 left = key = false;
199 }
200 }
201
202 /* Whenever we move the key, we also move both handles. */
203 if (key) {
204 left = right = true;
205 }
206
207 *r_key = key;
208 *r_left_handle = left;
209 *r_right_handle = right;
210}
211
213 TransInfo *t, FCurve *fcu, TransData *td_start, TransData *td, int cfra, bool use_handle)
214{
215 int j = 0;
216 TransData *td_iter = td_start;
217 bool sel_key, sel_left, sel_right;
218
219 float dist = FLT_MAX;
220 for (; j < fcu->totvert; j++) {
221 BezTriple *bezt = fcu->bezt + j;
222 if (FrameOnMouseSide(t->frame_side, bezt->vec[1][0], cfra)) {
223 graph_bezt_get_transform_selection(t, bezt, use_handle, &sel_left, &sel_key, &sel_right);
224
225 if (sel_left || sel_key || sel_right) {
226 dist = std::min({dist, td->dist, fabs(td_iter->center[0] - td->center[0])});
227 }
228
229 td_iter += 3;
230 }
231 }
232
233 return dist;
234}
235
246{
247 SpaceGraph *sipo = t->area->spacedata.first_as<SpaceGraph>();
248 Scene *scene = t->scene;
249 ARegion *region = t->region;
250 View2D *v2d = &region->v2d;
251
252 TransData *td = nullptr;
253 TransData2D *td2d = nullptr;
254 TransDataGraph *tdg = nullptr;
255
256 bAnimContext ac;
257 ListBaseT<bAnimListElem> anim_data = {nullptr, nullptr};
258 int filter;
259
260 BezTriple *bezt;
261 int count = 0, i;
262 float mtx[3][3], smtx[3][3];
263 const bool use_handle = !(sipo->flag & SIPO_NOHANDLES);
264 const bool use_local_center = graph_edit_use_local_center(t);
265 const bool is_prop_edit = (t->flag & T_PROP_EDIT) != 0;
266 short anim_map_flag = ANIM_UNITCONV_ONLYSEL | ANIM_UNITCONV_SELVERTS;
267 bool sel_key, sel_left, sel_right;
268
269 /* Determine what type of data we are operating on. */
270 if (ANIM_animdata_get_context(C, &ac) == 0) {
271 return;
272 }
273
274 anim_map_flag |= ANIM_get_normalization_flags(ac.sl);
275
276 /* Filter data. */
280 &ac, &anim_data, eAnimFilter_Flags(filter), ac.data, eAnimCont_Types(ac.datatype));
281
282 /* Which side of the current frame should be allowed. */
283 /* XXX we still want this mode, but how to get this using standard transform too? */
284 if (t->mode == TFM_TIME_EXTEND) {
285 t->frame_side = transform_convert_frame_side_dir_get(t, float(scene->r.cfra));
286 }
287 else {
288 /* Normal transform - both sides of current frame are considered. */
289 t->frame_side = 'B';
290 }
291
292 /* Loop 1: count how many BezTriples (specifically their verts)
293 * are selected (or should be edited). */
294 Set<FCurve *> visited_fcurves;
295 Vector<bAnimListElem *> unique_fcu_anim_list_elements;
296 for (bAnimListElem &ale : anim_data) {
297 FCurve *fcu = static_cast<FCurve *>(ale.key_data);
298 /* If 2 or more objects share the same action, multiple bAnimListElem might reference the same
299 * FCurve. */
300 if (!visited_fcurves.add(fcu)) {
301 continue;
302 }
303 unique_fcu_anim_list_elements.append(&ale);
304 int curvecount = 0;
305 bool selected = false;
306
307 /* F-Curve may not have any keyframes. */
308 if (fcu->bezt == nullptr) {
309 continue;
310 }
311
312 /* Convert current-frame to action-time (slightly less accurate, especially under
313 * higher scaling ratios, but is faster than converting all points). */
314 const float cfra = ANIM_nla_tweakedit_remap(&ale, float(scene->r.cfra), NLATIME_CONVERT_UNMAP);
315
316 for (i = 0, bezt = fcu->bezt; i < fcu->totvert; i++, bezt++) {
317 /* Only include BezTriples whose 'keyframe'
318 * occurs on the same side of the current frame as mouse. */
319 if (FrameOnMouseSide(t->frame_side, bezt->vec[1][0], cfra)) {
320 graph_bezt_get_transform_selection(t, bezt, use_handle, &sel_left, &sel_key, &sel_right);
321
322 if (is_prop_edit) {
323 curvecount += 3;
324 if (sel_key || sel_left || sel_right) {
325 selected = true;
326 }
327 }
328 else {
329 if (sel_left) {
330 count++;
331 }
332
333 if (sel_right) {
334 count++;
335 }
336
337 /* Only include main vert if selected. */
338 if (sel_key && !use_local_center) {
339 count++;
340 }
341 }
342 }
343 }
344
345 if (is_prop_edit) {
346 if (selected) {
347 count += curvecount;
348 ale.tag = true;
349 }
350 }
351 }
352
353 /* Stop if trying to build list if nothing selected. */
354 if (count == 0) {
355 /* Cleanup temp list. */
356 ANIM_animdata_freelist(&anim_data);
357 return;
358 }
359
361
362 /* Allocate memory for data. */
363 tc->data_len = count;
364
365 tc->data = MEM_new_array_zeroed<TransData>(tc->data_len, "TransData (Graph Editor)");
366 /* For each 2d vert a 3d vector is allocated,
367 * so that they can be treated just as if they were 3d verts. */
368 tc->data_2d = MEM_new_array_zeroed<TransData2D>(tc->data_len, "TransData2D (Graph Editor)");
369 tc->custom.type.data = MEM_new_array_zeroed<TransDataGraph>(tc->data_len, "TransDataGraph");
370 tc->custom.type.use_free = true;
371
372 td = tc->data;
373 td2d = tc->data_2d;
374 tdg = static_cast<TransDataGraph *>(tc->custom.type.data);
375
376 /* Precompute space-conversion matrices for dealing with non-uniform scaling of Graph Editor. */
377 unit_m3(mtx);
378 unit_m3(smtx);
379
380 if (ELEM(t->mode, TFM_ROTATION, TFM_RESIZE)) {
381 float xscale, yscale;
382
383 /* Apply scale factors to x and y axes of space-conversion matrices. */
384 ui::view2d_scale_get(v2d, &xscale, &yscale);
385
386 /* `mtx` is data to global (i.e. view) conversion. */
387 mul_v3_fl(mtx[0], xscale);
388 mul_v3_fl(mtx[1], yscale);
389
390 /* `smtx` is global (i.e. view) to data conversion. */
391 if (IS_EQF(xscale, 0.0f) == 0) {
392 mul_v3_fl(smtx[0], 1.0f / xscale);
393 }
394 if (IS_EQF(yscale, 0.0f) == 0) {
395 mul_v3_fl(smtx[1], 1.0f / yscale);
396 }
397 }
398
399 bool at_least_one_key_selected = false;
400
401 /* Loop 2: build transdata arrays. */
402 for (bAnimListElem *ale : unique_fcu_anim_list_elements) {
403 FCurve *fcu = static_cast<FCurve *>(ale->key_data);
404 bool intvals = (fcu->flag & FCURVE_INT_VALUES) != 0;
405 float unit_scale, offset;
406
407 /* F-Curve may not have any keyframes. */
408 if (fcu->bezt == nullptr || (is_prop_edit && ale->tag == 0)) {
409 continue;
410 }
411
412 /* Convert current-frame to action-time (slightly less accurate, especially under
413 * higher scaling ratios, but is faster than converting all points). */
414 const float cfra = ANIM_nla_tweakedit_remap(ale, float(scene->r.cfra), NLATIME_CONVERT_UNMAP);
415
416 unit_scale = ANIM_unit_mapping_get_factor(
417 ac.scene, ale->id, static_cast<FCurve *>(ale->key_data), anim_map_flag, &offset);
418
419 for (i = 0, bezt = fcu->bezt; i < fcu->totvert; i++, bezt++) {
420 /* Ensure temp flag is cleared for all triples, we use it. */
421 bezt->f1 &= ~BEZT_FLAG_TEMP_TAG;
422 bezt->f2 &= ~BEZT_FLAG_TEMP_TAG;
423 bezt->f3 &= ~BEZT_FLAG_TEMP_TAG;
424
425 /* Only include BezTriples whose 'keyframe' occurs on the same side
426 * of the current frame as mouse (if applicable). */
427 if (FrameOnMouseSide(t->frame_side, bezt->vec[1][0], cfra)) {
428 TransDataCurveHandleFlags *hdata = nullptr;
429
430 graph_bezt_get_transform_selection(t, bezt, use_handle, &sel_left, &sel_key, &sel_right);
431 at_least_one_key_selected |= sel_key;
432 if (is_prop_edit) {
433 bool is_sel = (sel_key || sel_left || sel_right);
434 /* We always select all handles for proportional editing * if central handle is
435 * selected. */
438 td2d++,
439 tdg++,
440 ale,
441 bezt,
442 0,
443 is_sel,
444 true,
445 intvals,
446 mtx,
447 smtx,
448 unit_scale,
449 offset);
452 td2d++,
453 tdg++,
454 ale,
455 bezt,
456 1,
457 is_sel,
458 false,
459 intvals,
460 mtx,
461 smtx,
462 unit_scale,
463 offset);
466 td2d++,
467 tdg++,
468 ale,
469 bezt,
470 2,
471 is_sel,
472 true,
473 intvals,
474 mtx,
475 smtx,
476 unit_scale,
477 offset);
478
479 if (is_sel) {
480 bezt->f1 |= BEZT_FLAG_TEMP_TAG;
481 bezt->f2 |= BEZT_FLAG_TEMP_TAG;
482 bezt->f3 |= BEZT_FLAG_TEMP_TAG;
483 }
484 }
485 else {
486 /* Only include handles if selected, irrespective of the interpolation modes.
487 * also, only treat handles specially if the center point isn't selected. */
488 if (sel_left) {
489 hdata = initTransDataCurveHandles(td, bezt);
491 td2d++,
492 tdg++,
493 ale,
494 bezt,
495 0,
496 sel_left,
497 true,
498 intvals,
499 mtx,
500 smtx,
501 unit_scale,
502 offset);
503 bezt->f1 |= BEZT_FLAG_TEMP_TAG;
504 }
505
506 if (sel_right) {
507 if (hdata == nullptr) {
508 hdata = initTransDataCurveHandles(td, bezt);
509 }
511 td2d++,
512 tdg++,
513 ale,
514 bezt,
515 2,
516 sel_right,
517 true,
518 intvals,
519 mtx,
520 smtx,
521 unit_scale,
522 offset);
523 bezt->f3 |= BEZT_FLAG_TEMP_TAG;
524 }
525
526 /* Only include main vert if selected. */
527 if (sel_key && !use_local_center) {
528 /* Move handles relative to center. */
530 if (sel_left) {
531 td->flag |= TD_MOVEHANDLE1;
532 }
533 if (sel_right) {
534 td->flag |= TD_MOVEHANDLE2;
535 }
536 }
537
538 /* If handles were not selected, store their selection status. */
539 if (!(sel_left) || !(sel_right)) {
540 if (hdata == nullptr) {
541 hdata = initTransDataCurveHandles(td, bezt);
542 }
543 }
544
546 td2d++,
547 tdg++,
548 ale,
549 bezt,
550 1,
551 sel_key,
552 false,
553 intvals,
554 mtx,
555 smtx,
556 unit_scale,
557 offset);
558 bezt->f2 |= BEZT_FLAG_TEMP_TAG;
559 }
560 /* Special hack (must be done after #initTransDataCurveHandles(),
561 * as that stores handle settings to restore...):
562 *
563 * - Check if we've got entire BezTriple selected and we're scaling/rotating that point,
564 * then check if we're using auto-handles.
565 * - If so, change them auto-handles to aligned handles so that handles get affected too.
566 */
567 if (ELEM(bezt->h1, HD_AUTO, HD_AUTO_ANIM) && ELEM(bezt->h2, HD_AUTO, HD_AUTO_ANIM) &&
569 {
570 if (hdata && (sel_left) && (sel_right)) {
571 bezt->h1 = HD_ALIGN;
572 bezt->h2 = HD_ALIGN;
573 }
574 }
575 }
576 }
577 }
578
579 /* Sets handles based on the selection. */
580 testhandles_fcurve(fcu, BEZT_FLAG_TEMP_TAG, use_handle);
581 }
582
583 if (is_prop_edit) {
584 /* Loop 3: build proportional edit distances. */
585 td = tc->data;
586
587 for (bAnimListElem *ale : unique_fcu_anim_list_elements) {
588 FCurve *fcu = static_cast<FCurve *>(ale->key_data);
589 TransData *td_start = td;
590
591 /* F-Curve may not have any keyframes. */
592 if (fcu->bezt == nullptr || (ale->tag == 0)) {
593 continue;
594 }
595
596 /* Convert current-frame to action-time (slightly less accurate, especially under
597 * higher scaling ratios, but is faster than converting all points). */
598 const float cfra = ANIM_nla_tweakedit_remap(
599 ale, float(scene->r.cfra), NLATIME_CONVERT_UNMAP);
600
601 for (i = 0, bezt = fcu->bezt; i < fcu->totvert; i++, bezt++) {
602 /* Only include BezTriples whose 'keyframe' occurs on the
603 * same side of the current frame as mouse (if applicable). */
604 if (FrameOnMouseSide(t->frame_side, bezt->vec[1][0], cfra)) {
605 graph_bezt_get_transform_selection(t, bezt, use_handle, &sel_left, &sel_key, &sel_right);
606
607 /* Now determine to distance for proportional editing for all three TransData
608 * (representing the key as well as both handles). Note though that the way
609 * #bezt_to_transdata sets up the TransData, the td->center[0] will always be based on
610 * the key (bezt->vec[1]) which means that #graph_key_shortest_dist will return the
611 * same for all of them and we can reuse that (expensive) result if needed. Might be
612 * worth looking into using a 2D KDTree in the future as well. */
613
614 float dist = FLT_MAX;
615 if (sel_left || sel_key || sel_right) {
616 /* If either left handle or key or right handle is selected, all will move fully. */
617 dist = 0.0f;
618 }
619 else {
620 /* If nothing is selected, left handle and key and right handle will share the same (to
621 * be calculated) distance. */
622 dist = graph_key_shortest_dist(t, fcu, td_start, td, cfra, use_handle);
623 }
624
625 td->dist = td->rdist = dist;
626 (td + 1)->dist = (td + 1)->rdist = dist;
627 (td + 2)->dist = (td + 2)->rdist = dist;
628 td += 3;
629 }
630 }
631 }
632 }
633
634 if (sipo->flag & SIPO_AUTOLOCK_AXIS && at_least_one_key_selected) {
635 enable_autolock(t, sipo);
636 }
637
638 /* Cleanup temp list. */
639 ANIM_animdata_freelist(&anim_data);
640}
641
643
644/* -------------------------------------------------------------------- */
647
648static bool fcu_test_selected(FCurve *fcu)
649{
650 BezTriple *bezt = fcu->bezt;
651 uint i;
652
653 if (bezt == nullptr) { /* Ignore baked. */
654 return false;
655 }
656
657 for (i = 0; i < fcu->totvert; i++, bezt++) {
658 if (BEZT_ISSEL_ANY(bezt)) {
659 return true;
660 }
661 }
662
663 return false;
664}
665
671{
672 TransData *td;
673 TransData2D *td2d;
674 TransDataGraph *tdg;
675 int a;
676
677 eSnapMode snap_mode = t->tsnap.mode;
678
680
681 /* Snapping only affects keyframes, never their handles (handles carry #TD_NOTIMESNAP). To keep
682 * the handles at the same time-offset from their key, the snapping offset applied to each key is
683 * collected here and added to that key's handles in a second pass below. The second pass is
684 * needed because a handle's final (scaled/rotated) position may be flushed after its key.
685 * This way, things stay independent of the order of the rest of the code (there's a difference
686 * between the proportional editing and the regular editing, for example). */
687 const bool may_snap = (t->tsnap.flag & SCE_SNAP) && (t->state != TRANS_CANCEL);
688 Vector<float> key_snap_time_offset(may_snap ? tc->data_len : 0, 0.0f);
689
690 /* Flush to 2d vector from internally used 3d vector. */
691 for (a = 0,
692 td = tc->data,
693 td2d = tc->data_2d,
694 tdg = static_cast<TransDataGraph *>(tc->custom.type.data);
695 a < tc->data_len;
696 a++, td++, td2d++, tdg++)
697 {
698 /* Pointers to relevant AnimData blocks are stored in the `td->extra` pointers. */
699 AnimData *adt = static_cast<AnimData *>(td->extra);
700
701 float inv_unit_scale = 1.0f / tdg->unit_scale;
702
703 /* Handle snapping for time values:
704 * - We should still be in NLA-mapping time-space.
705 * - Only apply to keyframes (but never to handles).
706 * - Don't do this when canceling, or else these changes won't go away.
707 */
708 const bool do_snap = may_snap && !(td->flag & TD_NOTIMESNAP);
709 const float presnap_loc = td2d->loc[0];
710 if (do_snap) {
711 transform_snap_anim_flush_data(t, td, snap_mode, td->loc);
712 }
713
714 /* We need to unapply the nla-mapping from the time in some situations. */
715 if (adt) {
716 td2d->loc2d[0] = BKE_nla_tweakedit_remap(adt, td2d->loc[0], NLATIME_CONVERT_UNMAP);
717 }
718 else {
719 td2d->loc2d[0] = td2d->loc[0];
720 }
721
722 if (do_snap) {
723 /* Record the snapping offset in the same (un-mapped) space as the handle coordinates, so it
724 * can be applied to this key's handles in the second pass below. */
725 const float presnap_loc2d = adt ? BKE_nla_tweakedit_remap(
726 adt, presnap_loc, NLATIME_CONVERT_UNMAP) :
727 presnap_loc;
728 key_snap_time_offset[a] = td2d->loc2d[0] - presnap_loc2d;
729 }
730
731 /* If int-values only, truncate to integers. */
732 if (td->flag & TD_INTVALUES) {
733 td2d->loc2d[1] = floorf(td2d->loc[1] * inv_unit_scale - tdg->offset + 0.5f);
734 }
735 else {
736 td2d->loc2d[1] = td2d->loc[1] * inv_unit_scale - tdg->offset;
737 }
738
739 transform_convert_flush_handle2D(td, td2d, inv_unit_scale);
740 }
741
742 if (may_snap) {
743 /* Second pass of snapping handling for handles: shift each key's handles by the snapping
744 * offset applied to that key, so their position relative to the key is maintained. Handles
745 * flagged with #TD_MOVEHANDLE1 / #TD_MOVEHANDLE2 are already moved by the full key delta
746 * (including snapping) during translation, so they are skipped here to avoid applying the
747 * offset twice. */
748 for (a = 0, td = tc->data, td2d = tc->data_2d; a < tc->data_len; a++, td++, td2d++) {
749 const float offset = key_snap_time_offset[a];
750 if (offset == 0.0f) {
751 continue;
752 }
753 if (td2d->h1 && !(td->flag & TD_MOVEHANDLE1)) {
754 td2d->h1[0] += offset;
755 }
756 if (td2d->h2 && !(td->flag & TD_MOVEHANDLE2)) {
757 td2d->h2[0] += offset;
758 }
759 }
760 }
761}
762
771
775static Vector<BeztMap> bezt_to_beztmaps(BezTriple *bezts, const int totvert)
776{
777 if (totvert == 0 || bezts == nullptr) {
778 return {};
779 }
780
781 Vector<BeztMap> bezms = Vector<BeztMap>(totvert);
782
783 for (const int i : bezms.index_range()) {
784 BezTriple *bezt = &bezts[i];
785 BeztMap &bezm = bezms[i];
786 bezm.bezt = bezt;
787 bezm.swap_handles = false;
788 bezm.oldIndex = i;
789 }
790
791 return bezms;
792}
793
794/* This function copies the code of sort_time_ipocurve, but acts on BeztMap structs instead. */
796{
797 /* Check if handles need to be swapped. */
798 for (BeztMap &bezm : bezms) {
799 /* Handles are only swapped if they are both on the wrong side of the key. Otherwise the one
800 * handle out of place is just clamped at the key position later. */
801 bezm.swap_handles = (bezm.bezt->vec[0][0] > bezm.bezt->vec[1][0] &&
802 bezm.bezt->vec[2][0] < bezm.bezt->vec[1][0]);
803 }
804
805 bool ok = true;
806 const int bezms_size = bezms.size();
807 if (bezms_size < 2) {
808 /* No sorting is needed with only 0 or 1 entries. */
809 return;
810 }
811 const IndexRange bezm_range = bezms.index_range().drop_back(1);
812
813 /* Keep repeating the process until nothing is out of place anymore. */
814 while (ok) {
815 ok = false;
816 for (const int i : bezm_range) {
817 BeztMap *bezm = &bezms[i];
818 /* Is current bezm out of order (i.e. occurs later than next)? */
819 if (bezm->bezt->vec[1][0] > (bezm + 1)->bezt->vec[1][0]) {
820 std::swap(*bezm, *(bezm + 1));
821 ok = true;
822 }
823 }
824 }
825}
826
827static inline void update_trans_data(TransData *td,
828 const FCurve *fcu,
829 const int new_index,
830 const bool swap_handles)
831{
832 if (td->flag & TD_BEZTRIPLE && td->hdata) {
833 if (swap_handles) {
834 td->hdata->h1 = &fcu->bezt[new_index].h2;
835 td->hdata->h2 = &fcu->bezt[new_index].h1;
836 }
837 else {
838 td->hdata->h1 = &fcu->bezt[new_index].h1;
839 td->hdata->h2 = &fcu->bezt[new_index].h2;
840 }
841 }
842}
843
844/* Adjust the pointers that the transdata has to each BezTriple. */
846 const Map<float *, int> &trans_data_map,
847 const FCurve *fcu,
848 const Span<BeztMap> bezms)
849{
850 /* At this point, beztmaps are already sorted, so their current index is assumed to be what the
851 * BezTriple index will be after sorting. */
852 for (const int new_index : bezms.index_range()) {
853 const BeztMap &bezm = bezms[new_index];
854 if (new_index == bezm.oldIndex && !bezm.swap_handles) {
855 /* If the index is the same, any pointers to BezTriple will still point to the correct data.
856 * Handles might need to be swapped though. */
857 continue;
858 }
859
860 TransData2D *td2d;
861 TransData *td;
862
863 if (const int *trans_data_index = trans_data_map.lookup_ptr(bezm.bezt->vec[0])) {
864 td2d = &tc->data_2d[*trans_data_index];
865 if (bezm.swap_handles) {
866 td2d->loc2d = fcu->bezt[new_index].vec[2];
867 }
868 else {
869 td2d->loc2d = fcu->bezt[new_index].vec[0];
870 }
871 td = &tc->data[*trans_data_index];
872 update_trans_data(td, fcu, new_index, bezm.swap_handles);
873 }
874 if (const int *trans_data_index = trans_data_map.lookup_ptr(bezm.bezt->vec[2])) {
875 td2d = &tc->data_2d[*trans_data_index];
876 if (bezm.swap_handles) {
877 td2d->loc2d = fcu->bezt[new_index].vec[0];
878 }
879 else {
880 td2d->loc2d = fcu->bezt[new_index].vec[2];
881 }
882 td = &tc->data[*trans_data_index];
883 update_trans_data(td, fcu, new_index, bezm.swap_handles);
884 }
885 if (const int *trans_data_index = trans_data_map.lookup_ptr(bezm.bezt->vec[1])) {
886 td2d = &tc->data_2d[*trans_data_index];
887 td2d->loc2d = fcu->bezt[new_index].vec[1];
888
889 /* If only control point is selected, the handle pointers need to be updated as well. */
890 if (td2d->h1) {
891 td2d->h1 = fcu->bezt[new_index].vec[0];
892 }
893 if (td2d->h2) {
894 td2d->h2 = fcu->bezt[new_index].vec[2];
895 }
896 td = &tc->data[*trans_data_index];
897 update_trans_data(td, fcu, new_index, bezm.swap_handles);
898 }
899 }
900}
901
902/* This function is called by recalc_data during the Transform loop to recalculate
903 * the handles of curves and sort the keyframes so that the curves draw correctly.
904 * The Span of FCurves should only contain those that need sorting.
905 */
906static void remake_graph_transdata(TransInfo *t, const Span<FCurve *> fcurves)
907{
908 SpaceGraph *sipo = t->area->spacedata.first_as<SpaceGraph>();
909 const bool use_handle = (sipo->flag & SIPO_NOHANDLES) == 0;
910
912
913 /* Build a map from the data that is being modified to its index. This is used to quickly update
914 * the pointers to where the data ends up after sorting. */
915 Map<float *, int> trans_data_map;
916 for (int i = 0; i < tc->data_len; i++) {
917 trans_data_map.add(tc->data_2d[i].loc2d, i);
918 }
919
920 /* The grain size of 8 was chosen based on measured runtimes of this function. While 1 is the
921 * fastest, larger grain sizes are generally preferred and the difference between 1 and 8 was
922 * only minimal (~330ms to ~336ms). */
923 threading::parallel_for(fcurves.index_range(), 8, [&](const IndexRange range) {
924 for (const int i : range) {
925 FCurve *fcu = fcurves[i];
926
927 if (!fcu->bezt) {
928 continue;
929 }
930
931 /* Adjust transform-data pointers. */
932 /* NOTE: none of these functions use 'use_handle', it could be removed. */
933 Vector<BeztMap> bezms = bezt_to_beztmaps(fcu->bezt, fcu->totvert);
934 sort_time_beztmaps(bezms);
935 update_transdata_bezt_pointers(tc, trans_data_map, fcu, bezms);
936
937 /* Re-sort actual beztriples
938 * (perhaps this could be done using the beztmaps to save time?). */
939 sort_time_fcurve(*fcu);
940
941 testhandles_fcurve(fcu, BEZT_FLAG_TEMP_TAG, use_handle);
942 }
943 });
944}
945
947{
948 SpaceGraph *sipo = t->area->spacedata.first_as<SpaceGraph>();
949 ViewLayer *view_layer = t->view_layer;
950
951 ListBaseT<bAnimListElem> anim_data = {nullptr, nullptr};
952 bAnimContext ac = {nullptr};
953 int filter;
954
956
957 /* Initialize relevant anim-context 'context' data from TransInfo data. */
958 /* NOTE: sync this with the code in #ANIM_animdata_get_context(). */
959 ac.bmain = CTX_data_main(t->context);
960 ac.scene = t->scene;
961 ac.view_layer = t->view_layer;
963 ac.area = t->area;
964 ac.region = t->region;
965 ac.sl = t->area ? t->area->spacedata.first_as<SpaceLink>() : nullptr;
966 ac.spacetype = eSpace_Type((t->area) ? t->area->spacetype : 0);
967 ac.regiontype = eRegion_Type((t->region) ? t->region->regiontype : 0);
968
970
971 /* Do the flush first. */
973
974 /* Get curves to check if a re-sort is needed. */
978 &ac, &anim_data, eAnimFilter_Flags(filter), ac.data, eAnimCont_Types(ac.datatype));
979
980 Vector<FCurve *> unsorted_fcurves;
981 /* Now test if there is a need to re-sort. */
982 for (bAnimListElem &ale : anim_data) {
983 FCurve *fcu = static_cast<FCurve *>(ale.key_data);
984
985 /* Ignore FC-Curves without any selected verts. */
986 if (!fcu_test_selected(fcu)) {
987 continue;
988 }
989
990 /* Watch it: if the time is wrong: do not correct handles yet. */
991 if (test_time_fcurve(*fcu)) {
992 unsorted_fcurves.append(fcu);
993 }
994 else {
996 }
997
998 /* Set refresh tags for objects using this animation,
999 * BUT only if realtime updates are enabled. */
1000 if ((sipo->flag & SIPO_NOREALTIMEUPDATES) == 0) {
1002 }
1003 }
1004
1005 /* Do resort and other updates? */
1006 if (!unsorted_fcurves.is_empty()) {
1007 remake_graph_transdata(t, unsorted_fcurves);
1008 }
1009
1010 /* Now free temp channels. */
1011 ANIM_animdata_freelist(&anim_data);
1012}
1013
1015
1016/* -------------------------------------------------------------------- */
1019
1021{
1022 SpaceGraph *sipo = t->area->spacedata.first_as<SpaceGraph>();
1023 bAnimContext ac;
1024 const bool use_handle = (sipo->flag & SIPO_NOHANDLES) == 0;
1025
1026 const bool canceled = (t->state == TRANS_CANCEL);
1027 const bool duplicate = (t->flag & T_DUPLICATED_KEYFRAMES) != 0;
1028
1029 /* Initialize relevant anim-context 'context' data. */
1030 if (ANIM_animdata_get_context(C, &ac) == 0) {
1031 return;
1032 }
1033
1034 if (ac.datatype) {
1035 ListBaseT<bAnimListElem> anim_data = {nullptr, nullptr};
1038
1039 /* Get channels to work on. */
1041 &ac, &anim_data, eAnimFilter_Flags(filter), ac.data, eAnimCont_Types(ac.datatype));
1042
1043 for (bAnimListElem &ale : anim_data) {
1044 FCurve *fcu = static_cast<FCurve *>(ale.key_data);
1045
1046 /* 3 cases here for curve cleanups:
1047 * 1) NOTRANSKEYCULL on -> cleanup of duplicates shouldn't be done.
1048 * 2) canceled == 0 -> user confirmed the transform,
1049 * so duplicates should be removed.
1050 * 3) canceled + duplicate -> user canceled the transform,
1051 * but we made duplicates, so get rid of these.
1052 */
1053 if ((sipo->flag & SIPO_NOTRANSKEYCULL) == 0 && ((canceled == 0) || (duplicate))) {
1054 ANIM_nla_mapping_apply_if_needed_fcurve(&ale, fcu, false, false);
1056 ANIM_nla_mapping_apply_if_needed_fcurve(&ale, fcu, true, false);
1057 }
1058 }
1059
1060 /* Free temp memory. */
1061 ANIM_animdata_freelist(&anim_data);
1062 }
1063
1064 /* Make sure all F-Curves are set correctly, but not if transform was
1065 * canceled, since then curves were already restored to initial state.
1066 * NOTE: if the refresh is really needed after cancel then some way
1067 * has to be added to not update handle types, see #22289.
1068 */
1069 if (!canceled) {
1071 }
1072}
1073
1075
1077 /*flags*/ (T_POINTS | T_2D_EDIT),
1078 /*create_trans_data*/ createTransGraphEditData,
1079 /*recalc_data*/ recalcData_graphedit,
1080 /*special_aftertrans_update*/ special_aftertrans_update__graph,
1081};
1082
1083} // namespace blender::ed::transform
#define SELECT
Definition BKE_mask.hh:19
#define BEZT_ISSEL_ANY(bezt)
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define ELEM(...)
#define IS_EQF(a, b)
constexpr IndexRange drop_back(int64_t n) const
const Value * lookup_ptr(const Key &key) const
Definition BLI_map.hh:508
bool add(const Key &key, const Value &value)
Definition BLI_map.hh:295
constexpr int64_t size() const
Definition BLI_span.hh:499
constexpr IndexRange index_range() const
Definition BLI_span.hh:676
bool add(const Key &key)
Definition BLI_set.hh:248
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
void append(const T &value)
friend class Vector
bool is_empty() const
IndexRange index_range() const
#define FLT_MAX
ccl_device_inline dual3 fabs(const ccl_private dual3 &a)
Definition math_dual.h:241
TransConvertTypeInfo TransConvertType_Graph
char transform_convert_frame_side_dir_get(TransInfo *t, float cframe)
static void update_trans_data(TransData *td, const FCurve *fcu, const int new_index, const bool swap_handles)
static void remake_graph_transdata(TransInfo *t, const Span< FCurve * > fcurves)
static void special_aftertrans_update__graph(bContext *C, TransInfo *t)
TransDataCurveHandleFlags * initTransDataCurveHandles(TransData *td, BezTriple *bezt)
static void enable_autolock(TransInfo *t, SpaceGraph *space_graph)
static void graph_bezt_get_transform_selection(const TransInfo *t, const BezTriple *bezt, const bool use_handle, bool *r_left_handle, bool *r_key, bool *r_right_handle)
static void recalcData_graphedit(TransInfo *t)
bool FrameOnMouseSide(char side, float frame, float cframe)
void transform_snap_anim_flush_data(TransInfo *t, TransData *td, eSnapMode snap_mode, float *r_val_final)
static void bezt_to_transdata(TransData *td, TransData2D *td2d, TransDataGraph *tdg, bAnimListElem *ale, BezTriple *bezt, int bi, bool selected, bool ishandle, bool intvals, const float mtx[3][3], const float smtx[3][3], float unit_scale, float offset)
static float graph_key_shortest_dist(TransInfo *t, FCurve *fcu, TransData *td_start, TransData *td, int cfra, bool use_handle)
static void sort_time_beztmaps(const MutableSpan< BeztMap > bezms)
static bool fcu_test_selected(FCurve *fcu)
void initSelectConstraint(TransInfo *t)
static void update_transdata_bezt_pointers(TransDataContainer *tc, const Map< float *, int > &trans_data_map, const FCurve *fcu, const Span< BeztMap > bezms)
static bool graph_edit_is_translation_mode(TransInfo *t)
void transform_convert_flush_handle2D(TransData *td, TransData2D *td2d, const float y_fac)
static Vector< BeztMap > bezt_to_beztmaps(BezTriple *bezts, const int totvert)
static bool graph_edit_use_local_center(TransInfo *t)
static void flushTransGraphData(TransInfo *t)
static void createTransGraphEditData(bContext *C, TransInfo *t)
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:69
void view2d_scale_get(const View2D *v2d, float *r_x, float *r_y)
Definition view2d.cc:1893
Object * BKE_view_layer_active_object_get(const ViewLayer *view_layer)
float BKE_nla_tweakedit_remap(AnimData *adt, float cframe, eNlaTime_ConvertModes mode)
void testhandles_fcurve(FCurve *fcu, eBezTriple_Flag sel_flag, bool use_handle)
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
void copy_m3_m3(float m1[3][3], const float m2[3][3])
@ ANIM_UNITCONV_SELVERTS
@ ANIM_UNITCONV_ONLYSEL
MINLINE void copy_v2_v2(float r[2], const float a[2])
@ NLATIME_CONVERT_UNMAP
Definition BKE_nla.hh:560
@ NLATIME_CONVERT_MAP
Definition BKE_nla.hh:563
bool ANIM_animdata_get_context(const bContext *C, bAnimContext *ac)
@ V3D_AROUND_LOCAL_ORIGINS
float ANIM_nla_tweakedit_remap(bAnimListElem *ale, const float cframe, const eNlaTime_ConvertModes mode)
Definition anim_draw.cc:329
void BKE_fcurve_handles_recalc_ex(FCurve &fcu, eBezTriple_Flag handle_sel_flag)
bool ANIM_nla_mapping_allowed(const bAnimListElem *ale)
Definition anim_draw.cc:279
bool ANIM_animdata_context_getdata(bAnimContext *ac)
void ANIM_nla_mapping_apply_if_needed_fcurve(bAnimListElem *ale, FCurve *fcu, const bool restore, const bool only_keys)
Definition anim_draw.cc:406
MINLINE void mul_v3_fl(float r[3], float f)
void BKE_fcurve_merge_duplicate_keys(FCurve *fcu, const int sel_flag, const bool use_handle)
size_t ANIM_animdata_filter(bAnimContext *ac, ListBaseT< bAnimListElem > *anim_data, const eAnimFilter_Flags filter_mode, void *data, const eAnimCont_Types datatype)
@ ANIMFILTER_FCURVESONLY
@ ANIMFILTER_FOREDIT
@ ANIMFILTER_DATA_VISIBLE
@ ANIMFILTER_CURVE_VISIBLE
void ANIM_list_elem_update(Main *bmain, Scene *scene, bAnimListElem *ale)
Definition anim_deps.cc:54
void ANIM_editkeyframes_refresh(bAnimContext *ac)
bool BKE_view_layer_synced_ensure(const Main &bmain, const Scene *scene, ViewLayer *view_layer)
@ SIPO_NOREALTIMEUPDATES
@ SIPO_RUNTIME_FLAG_TWEAK_HANDLES_RIGHT
@ SIPO_RUNTIME_FLAG_TWEAK_HANDLES_LEFT
Main * CTX_data_main(const bContext *C)
void unit_m3(float m[3][3])
void ANIM_animdata_freelist(ListBaseT< bAnimListElem > *anim_data)
Definition anim_deps.cc:472
float ANIM_unit_mapping_get_factor(Scene *scene, ID *id, FCurve *fcu, short flag, float *r_offset)
Definition anim_draw.cc:636
bool test_time_fcurve(FCurve &fcu)
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
short ANIM_get_normalization_flags(SpaceLink *space_link)
Definition anim_draw.cc:420
#define floorf
eRegion_Type regiontype
eBezTriple_Flag f2
eBezTriple_Handle h2
eBezTriple_Flag f3
eBezTriple_Flag f1
eBezTriple_Handle h1
eFCurve_Flags flag
unsigned int totvert
ListBaseT< SpaceLink > spacedata
SpaceGraph_Runtime runtime
eGraphEdit_Flag flag
eRegion_Type regiontype
eAnimCont_Types datatype
TransDataCurveHandleFlags * hdata
Definition transform.hh:539
#define TRANS_DATA_CONTAINER_FIRST_SINGLE(t)
Definition transform.hh:45
conversion and adaptation of different datablocks to a common struct.