Blender V5.3
transform_convert_nla.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 <cstdio>
10
11#include "DNA_anim_types.h"
12#include "DNA_space_types.h"
13#include "DNA_userdef_types.h"
14
15#include "MEM_guardedalloc.h"
16
17#include "BLI_listbase.hh"
18#include "BLI_math_matrix_c.hh"
19#include "BLI_math_vector_c.hh"
20
21#include "BKE_anim_data.hh"
22#include "BKE_context.hh"
23#include "BKE_nla.hh"
24
25#include "ED_anim_api.hh"
26
27#include "WM_api.hh"
28#include "WM_types.hh"
29
30#include "RNA_access.hh"
31#include "RNA_prototypes.hh"
32
33#include "transform.hh"
34#include "transform_convert.hh"
35
36namespace blender::ed::transform {
37
42
47
50
51 /* Dummy values for transform to write in - must have 3 elements. */
53 float h1[3];
55 float h2[3];
56
59
65
67 int handle;
68};
69
70static bool is_overlap(const float left_bound_a,
71 const float right_bound_a,
72 const float left_bound_b,
73 const float right_bound_b)
74{
75 return (left_bound_a < right_bound_b) && (right_bound_a > left_bound_b);
76}
77
78static bool nlastrip_is_overlap(const NlaStrip *strip_a,
79 const float offset_a,
80 const NlaStrip *strip_b,
81 const float offset_b)
82{
83 return is_overlap(strip_a->start + offset_a,
84 strip_a->end + offset_a,
85 strip_b->start + offset_b,
86 strip_b->end + offset_b);
87}
88
96static float transdata_get_time_shuffle_offset_side(ListBaseT<LinkData> *trans_datas,
97 const bool shuffle_left)
98{
99 float total_offset = 0;
100
101 float offset;
102 do {
103 offset = 0;
104
105 for (LinkData &link : *trans_datas) {
106 TransDataNla *trans_data = static_cast<TransDataNla *>(link.data);
107 NlaStrip *xformed_strip = trans_data->strip;
108
109 for (NlaStrip &non_xformed_strip : trans_data->nlt->strips) {
110
111 if (non_xformed_strip.flag & NLASTRIP_FLAG_INVALID_LOCATION) {
112 continue;
113 }
114
115 /* Allow overlap with transitions. */
116 if (non_xformed_strip.type == NLASTRIP_TYPE_TRANSITION) {
117 continue;
118 }
119
120 if (!nlastrip_is_overlap(&non_xformed_strip, 0, xformed_strip, total_offset)) {
121 continue;
122 }
123
124 offset = shuffle_left ?
125 fmin(offset, non_xformed_strip.start - (xformed_strip->end + total_offset)) :
126 fmax(offset, non_xformed_strip.end - (xformed_strip->start + total_offset));
127 }
128 }
129
130 total_offset += offset;
131 } while (!IS_EQT(offset, 0.0f, 1e-4));
132 /* Needs a epsilon greater than FLT_EPS because strip->start/end could be non-integral,
133 * and after those calculations, `offset` could fall outside of FLT_EPS. */
134
135 return total_offset;
136}
137
145static float transdata_get_time_shuffle_offset(ListBaseT<LinkData> *trans_datas)
146{
147 const float offset_left = transdata_get_time_shuffle_offset_side(trans_datas, true);
148 const float offset_right = transdata_get_time_shuffle_offset_side(trans_datas, false);
149 BLI_assert(offset_left <= 0);
150 BLI_assert(offset_right >= 0);
151
152 return -offset_left < offset_right ? offset_left : offset_right;
153}
154
167static bool transdata_get_track_shuffle_offset_side(ListBaseT<LinkData> *trans_datas,
168 const int shuffle_direction,
169 int *r_total_offset)
170{
171 *r_total_offset = 0;
172 if (trans_datas->is_empty()) {
173 return false;
174 }
175
176 LinkData *first_link = trans_datas->first();
177 TransDataNla *first_transdata = static_cast<TransDataNla *>(first_link->data);
178 AnimData *adt = BKE_animdata_from_id(first_transdata->id);
179 ListBaseT<NlaTrack> *tracks = &adt->nla_tracks;
180
181 int offset;
182 do {
183 offset = 0;
184
185 for (LinkData &link : *trans_datas) {
186 TransDataNla *trans_data = static_cast<TransDataNla *>(link.data);
187
188 NlaTrack *dst_track = static_cast<NlaTrack *>(
189 BLI_findlink(tracks, trans_data->trackIndex + *r_total_offset));
190
191 /* Cannot keep moving strip in given track direction. No solution. */
192 if (dst_track == nullptr) {
193 return false;
194 }
195
196 /* Shuffle only if track is locked or library override. */
197 if (((dst_track->flag & NLATRACK_PROTECTED) == 0) &&
198 !BKE_nlatrack_is_nonlocal_in_liboverride(trans_data->id, dst_track))
199 {
200 continue;
201 }
202
203 offset = shuffle_direction;
204 break;
205 }
206
207 *r_total_offset += offset;
208 } while (offset != 0);
209
210 return true;
211}
212
222static bool transdata_get_track_shuffle_offset(ListBaseT<LinkData> *trans_datas,
223 int *r_track_offset)
224{
225 int offset_down = 0;
226 const bool down_valid = transdata_get_track_shuffle_offset_side(trans_datas, 1, &offset_down);
227
228 int offset_up = 0;
229 const bool up_valid = transdata_get_track_shuffle_offset_side(trans_datas, -1, &offset_up);
230
231 if (down_valid && up_valid) {
232 if (offset_down < abs(offset_up)) {
233 *r_track_offset = offset_down;
234 }
235 else {
236 *r_track_offset = offset_up;
237 }
238 }
239 else if (down_valid) {
240 *r_track_offset = offset_down;
241 }
242 else if (up_valid) {
243 *r_track_offset = offset_up;
244 }
245
246 return down_valid || up_valid;
247}
248
249/* -------------------------------------------------------------------- */
252
254{
256 ListBaseT<bAnimListElem> anim_data = {nullptr, nullptr};
258 ac, &anim_data, eAnimFilter_Flags(filter), ac->data, eAnimCont_Types(ac->datatype));
259
260 for (bAnimListElem &ale : anim_data) {
261 if (!ale.adt) {
262 continue;
263 }
264 ListBaseT<NlaTrack> *nla_tracks = &ale.adt->nla_tracks;
265
267 for (NlaTrack &track : nla_tracks->items_reversed_mutable()) {
268 if (!(track.flag & NLATRACK_TEMPORARILY_ADDED)) {
269 break;
270 }
271 if (track.strips.first() != nullptr) {
272 break;
273 }
274 BKE_nlatrack_remove_and_free(nla_tracks, &track, true);
275 }
276
278 for (NlaTrack &track : nla_tracks->items_mutable()) {
279 /* Library override tracks are the first N tracks. They're never temporary and determine
280 * where we start removing temporaries. */
281 if ((track.flag & NLATRACK_OVERRIDELIBRARY_LOCAL) == 0) {
282 continue;
283 }
284 if (!(track.flag & NLATRACK_TEMPORARILY_ADDED)) {
285 break;
286 }
287 if (track.strips.first() != nullptr) {
288 break;
289 }
290 BKE_nlatrack_remove_and_free(nla_tracks, &track, true);
291 }
292
294 for (NlaTrack &track : nla_tracks->items_mutable()) {
295 track.flag &= ~NLATRACK_TEMPORARILY_ADDED;
296 }
297 }
298
299 ANIM_animdata_freelist(&anim_data);
300}
301
303
304/* -------------------------------------------------------------------- */
307
313static void applyTransformNLA_translation(PointerRNA *strip_rna_ptr, const TransDataNla *transdata)
314{
315 /* NOTE: we write these twice to avoid truncation errors which can arise when
316 * moving the strips a large distance using numeric input #33852.
317 */
318 RNA_float_set(strip_rna_ptr, "frame_start", transdata->h1[0]);
319 RNA_float_set(strip_rna_ptr, "frame_end", transdata->h2[0]);
320
321 RNA_float_set(strip_rna_ptr, "frame_start", transdata->h1[0]);
322 RNA_float_set(strip_rna_ptr, "frame_end", transdata->h2[0]);
323}
324
325static void applyTransformNLA_timeScale(PointerRNA *strip_rna_ptr, const float value)
326{
327 RNA_float_set(strip_rna_ptr, "scale", value);
328}
329
332{
333 while (strip->prev != nullptr && tdn->h1[0] < strip->prev->start) {
334 BLI_listbase_swaplinks(&tdn->nlt->strips, strip, strip->prev);
335 }
336 while (strip->next != nullptr && tdn->h1[0] > strip->next->start) {
337 BLI_listbase_swaplinks(&tdn->nlt->strips, strip, strip->next);
338 }
339}
340
348{
349
350 NlaStrip *adj_strip = strip->prev;
351 if (adj_strip != nullptr && !(adj_strip->flag & NLASTRIP_FLAG_SELECT) &&
352 nlastrip_is_overlap(strip, 0, adj_strip, 0))
353 {
355 }
356 adj_strip = strip->next;
357 if (adj_strip != nullptr && !(adj_strip->flag & NLASTRIP_FLAG_SELECT) &&
358 nlastrip_is_overlap(strip, 0, adj_strip, 0))
359 {
361 }
362}
363
369{
370 /* Firstly, check if the proposed transform locations would overlap with any neighboring
371 * strips (barring transitions) which are absolute barriers since they are not being moved.
372 *
373 * This is done as a iterative procedure (done 5 times max for now). */
374 short iter_max = 4;
375 NlaStrip *prev = BKE_nlastrip_prev_in_track(strip, true);
376 NlaStrip *next = BKE_nlastrip_next_in_track(strip, true);
377
378 for (short iter = 0; iter <= iter_max; iter++) {
379 const bool p_exceeded = (prev != nullptr) && (tdn->h1[0] < prev->end);
380 const bool n_exceeded = (next != nullptr) && (tdn->h2[0] > next->start);
381
382 if ((p_exceeded && n_exceeded) || (iter == iter_max)) {
383 /* Both endpoints exceeded (or iteration ping-pong'd meaning that we need a compromise).
384 * - Simply crop strip to fit within the bounds of the strips bounding it.
385 * - If there were no neighbors, clear the transforms
386 * (make it default to the strip's current values).
387 */
388 if (prev && next) {
389 tdn->h1[0] = prev->end;
390 tdn->h2[0] = next->start;
391 }
392 else {
393 tdn->h1[0] = strip->start;
394 tdn->h2[0] = strip->end;
395 }
396 }
397 else if (n_exceeded) {
398 /* Move backwards. */
399 float offset = tdn->h2[0] - next->start;
400
401 tdn->h1[0] -= offset;
402 tdn->h2[0] -= offset;
403 }
404 else if (p_exceeded) {
405 /* More forwards. */
406 float offset = prev->end - tdn->h1[0];
407
408 tdn->h1[0] += offset;
409 tdn->h2[0] += offset;
410 }
411 else { /* All is fine and well. */
412 break;
413 }
414 }
415
416 /* Use RNA to write the values to ensure that constraints on these are obeyed
417 * (e.g. for transition strips, the values are taken from the neighbors). */
418 PointerRNA strip_ptr = RNA_pointer_create_discrete(nullptr, RNA_NlaStrip, strip);
419
420 switch (t->mode) {
421 case TFM_TIME_EXTEND:
422 case TFM_TIME_SCALE: {
423 /* The final scale is the product of the original strip scale (from before the
424 * transform operation started) and the current scale value of this transform operation. */
425 const float originalStripScale = tdn->h1[2];
426 const float newStripScale = originalStripScale * t->values_final[0];
427 applyTransformNLA_timeScale(&strip_ptr, newStripScale);
428 applyTransformNLA_translation(&strip_ptr, tdn);
429 break;
430 }
431 case TFM_TRANSLATION:
432 applyTransformNLA_translation(&strip_ptr, tdn);
433 break;
434 default:
435 printf("recalcData_nla: unsupported NLA transformation mode %d\n", t->mode);
436 break;
437 }
438}
439
441
442/* -------------------------------------------------------------------- */
445
447{
448 Scene *scene = t->scene;
449 SpaceNla *snla = nullptr;
450 TransData *td = nullptr;
451 TransDataNla *tdn = nullptr;
452
453 bAnimContext ac;
454 ListBaseT<bAnimListElem> anim_data = {nullptr, nullptr};
455 int filter;
456
457 int count = 0;
458
460
461 /* Determine what type of data we are operating on. */
462 if (ANIM_animdata_get_context(C, &ac) == 0) {
463 return;
464 }
465 snla = reinterpret_cast<SpaceNla *>(ac.sl);
466
467 /* Filter data. */
471 &ac, &anim_data, eAnimFilter_Flags(filter), ac.data, eAnimCont_Types(ac.datatype));
472
473 /* Which side of the current frame should be allowed. */
474 if (t->mode == TFM_TIME_EXTEND) {
476 }
477 else {
478 /* Normal transform - both sides of current frame are considered. */
479 t->frame_side = 'B';
480 }
481
482 /* Loop 1: count how many strips are selected (consider each strip as 2 points). */
483 for (bAnimListElem &ale : anim_data) {
484 NlaTrack *nlt = static_cast<NlaTrack *>(ale.data);
485
486 /* Make some meta-strips for chains of selected strips. */
487 BKE_nlastrips_make_metas(&nlt->strips, true);
488
489 /* Only consider selected strips. */
490 for (NlaStrip &strip : nlt->strips) {
491 /* TODO: we can make strips have handles later on. */
492 /* Transition strips can't get directly transformed. */
493 if (strip.type == NLASTRIP_TYPE_TRANSITION) {
494 continue;
495 }
496 if ((strip.flag & NLASTRIP_FLAG_SELECT) == 0) {
497 continue;
498 }
499 if (FrameOnMouseSide(t->frame_side, strip.start, float(scene->r.cfra))) {
500 count++;
501 }
502 if (FrameOnMouseSide(t->frame_side, strip.end, float(scene->r.cfra))) {
503 count++;
504 }
505 }
506 }
507
508 /* Stop if trying to build list if nothing selected. */
509 if (count == 0) {
510 /* Clear temp metas that may have been created but aren't needed now
511 * because they fell on the wrong side of `scene->r.cfra`. */
512 for (bAnimListElem &ale : anim_data) {
513 NlaTrack *nlt = static_cast<NlaTrack *>(ale.data);
514 BKE_nlastrips_clear_metas(&nlt->strips, false, true);
515 }
516
517 /* Cleanup temp list. */
518 ANIM_animdata_freelist(&anim_data);
519 return;
520 }
521
522 /* Allocate memory for data. */
523 tc->data_len = count;
524
525 tc->data = MEM_new_array_zeroed<TransData>(tc->data_len, "TransData(NLA Editor)");
526 td = tc->data;
528 "TransDataNla (NLA Editor)");
529 tc->custom.type.use_free = true;
530
531 /* Loop 2: build transdata array. */
532 for (bAnimListElem &ale : anim_data) {
533 /* Only if a real NLA-track. */
534 if (ale.type == ANIMTYPE_NLATRACK) {
535 AnimData *adt = ale.adt;
536 NlaTrack *nlt = static_cast<NlaTrack *>(ale.data);
537
538 /* Only consider selected strips. */
539 for (NlaStrip &strip : nlt->strips) {
540 /* TODO: we can make strips have handles later on. */
541 /* Transition strips can't get directly transformed. */
542 if (strip.type == NLASTRIP_TYPE_TRANSITION) {
543 continue;
544 }
545 if ((strip.flag & NLASTRIP_FLAG_SELECT) == 0) {
546 continue;
547 }
548
549 /* Our transform data is constructed as follows:
550 * - Only the handles on the right side of the current-frame get included.
551 * - `td` structs are transform-elements operated on by the transform system and
552 * represent a single handle. The storage/pointer used (`val` or `loc`) depends
553 * on whether we're scaling or transforming. Ultimately though, the handles the `td`
554 * writes to will simply be a dummy in `tdn`.
555 * - For each strip being transformed, a single `tdn` struct is used, so in some
556 * cases, there will need to be 1 of these `tdn` elements in the array skipped.
557 */
558 float center[3], yval;
559
560 /* Firstly, initialize `tdn` settings. */
561 tdn->id = ale.id;
562 tdn->oldTrack = tdn->nlt = nlt;
563 tdn->strip = &strip;
564 tdn->trackIndex = BLI_findindex(&adt->nla_tracks, nlt);
565 tdn->signed_track_index = tdn->trackIndex;
566
567 yval = float(tdn->trackIndex * NLATRACK_STEP(snla));
568
569 tdn->h1[0] = strip.start;
570 tdn->h1[1] = yval;
571 tdn->h2[0] = strip.end;
572 tdn->h2[1] = yval;
573 tdn->h1[2] = tdn->h2[2] = strip.scale;
574
575 center[0] = float(scene->r.cfra);
576 center[1] = yval;
577 center[2] = 0.0f;
578
579 /* Set td's based on which handles are applicable. */
580 if (FrameOnMouseSide(t->frame_side, strip.start, float(scene->r.cfra))) {
581 /* Just set `tdn` to assume that it only has one handle for now. */
582 tdn->handle = -1;
583
584 /* Now, link the transform data up to this data. */
585 td->loc = tdn->h1;
586 copy_v3_v3(td->iloc, tdn->h1);
587
589 /* Store all the other gunk that is required by transform. */
590 copy_v3_v3(td->center, center);
591 td->axismtx[2][2] = 1.0f;
592 td->flag |= TD_SELECTED;
593 unit_m3(td->mtx);
594 unit_m3(td->smtx);
595 }
596
597 td->extra = tdn;
598 td++;
599 }
600 if (FrameOnMouseSide(t->frame_side, strip.end, float(scene->r.cfra))) {
601 /* If `tdn` is already holding the start handle,
602 * then we're doing both, otherwise, only end. */
603 tdn->handle = (tdn->handle) ? 2 : 1;
604
605 /* Now, link the transform data up to this data. */
606 td->loc = tdn->h2;
607 copy_v3_v3(td->iloc, tdn->h2);
608
610 /* Store all the other gunk that is required by transform. */
611 copy_v3_v3(td->center, center);
612 td->axismtx[2][2] = 1.0f;
613 td->flag |= TD_SELECTED;
614 unit_m3(td->mtx);
615 unit_m3(td->smtx);
616 }
617
618 td->extra = tdn;
619 td++;
620 }
621
622 /* If both handles were used, skip the next tdn (i.e. leave it blank)
623 * since the counting code is dumb.
624 * Otherwise, just advance to the next one.
625 */
626 if (tdn->handle == 2) {
627 tdn += 2;
628 }
629 else if (tdn->handle) {
630 tdn++;
631 }
632 }
633 }
634 }
635
636 BLI_assert(tdn <= (((TransDataNla *)tc->custom.type.data) + tc->data_len));
637
638 /* Cleanup temp list. */
639 ANIM_animdata_freelist(&anim_data);
640}
641
643{
644 SpaceNla *snla = t->area->spacedata.first_as<SpaceNla>();
645
647
648 /* For each strip we've got, perform some additional validation of the values
649 * that got set before using RNA to set the value (which does some special
650 * operations when setting these values to make sure that everything works ok).
651 */
652 TransDataNla *tdn = static_cast<TransDataNla *>(tc->custom.type.data);
653 for (int i = 0; i < tc->data_len; i++, tdn++) {
654 NlaStrip *strip = tdn->strip;
655 int delta_y1, delta_y2;
656
657 /* If this tdn has no handles, that means it is just a dummy that should be skipped. */
658 if (tdn->handle == 0) {
659 continue;
660 }
662
663 /* Set refresh tags for objects using this animation,
664 * BUT only if realtime updates are enabled. */
665
666 if ((snla->flag & SNLA_NOREALTIMEUPDATES) == 0) {
668 }
669
670 /* If canceling transform, just write the values without validating, then move on. */
671 if (t->state == TRANS_CANCEL) {
672 /* Clear the values by directly overwriting the originals, but also need to restore
673 * endpoints of neighboring transition-strips. */
674
675 /* Start. */
676 strip->start = tdn->h1[0];
677
678 if ((strip->prev) && (strip->prev->type == NLASTRIP_TYPE_TRANSITION)) {
679 strip->prev->end = tdn->h1[0];
680 }
681
682 /* End. */
683 strip->end = tdn->h2[0];
684
685 if ((strip->next) && (strip->next->type == NLASTRIP_TYPE_TRANSITION)) {
686 strip->next->start = tdn->h2[0];
687 }
688
689 strip->scale = tdn->h1[2];
690
691 /* Flush transforms to child strips (since this should be a meta). */
693
694 /* Restore to original track (if needed). */
695 if (tdn->oldTrack != tdn->nlt) {
696 /* Just append to end of list for now,
697 * since strips get sorted in special_aftertrans_update(). */
698 BLI_remlink(&tdn->nlt->strips, strip);
699 BLI_addtail(&tdn->oldTrack->strips, strip);
700 }
701
702 continue;
703 }
704
705 const bool nlatrack_isliboverride = BKE_nlatrack_is_nonlocal_in_liboverride(tdn->id, tdn->nlt);
706 const bool allow_overlap = !nlatrack_isliboverride && ELEM(t->mode, TFM_TRANSLATION);
707
708 if (allow_overlap) {
709 nlastrip_overlap_reorder(tdn, strip);
710
711 /* Directly flush. */
712 strip->start = tdn->h1[0];
713 strip->end = tdn->h2[0];
714 }
715 else {
716 nlastrip_fix_overlapping(t, tdn, strip);
717 }
718
719 /* Flush transforms to child strips (since this should be a meta). */
721
722 /* Now, check if we need to try and move track:
723 * - we need to calculate both,
724 * as only one may have been altered by transform if only 1 handle moved.
725 */
726 /* In LibOverride case, we cannot move strips across tracks that come from the linked data.
727 */
728 const bool is_liboverride = ID_IS_OVERRIDE_LIBRARY(tdn->id);
729 if (nlatrack_isliboverride) {
730 continue;
731 }
732
733 delta_y1 = (int(tdn->h1[1]) / NLATRACK_STEP(snla) - tdn->signed_track_index);
734 delta_y2 = (int(tdn->h2[1]) / NLATRACK_STEP(snla) - tdn->signed_track_index);
735
736 /* Move strip into track in the requested direction. */
737 /* If we cannot find the strip in the track, this strip has moved tracks already (if multiple
738 * strips using the same action from equal IDs such as meshes or shape-keys are selected)
739 * so can be skipped. */
740 if ((delta_y1 || delta_y2) && BLI_findindex(&tdn->nlt->strips, strip) != -1) {
741 int delta = (delta_y2) ? delta_y2 : delta_y1;
742
743 AnimData *anim_data = BKE_animdata_from_id(tdn->id);
744 ListBaseT<NlaTrack> *nla_tracks = &anim_data->nla_tracks;
745
746 NlaTrack *old_track = tdn->nlt;
747 NlaTrack *dst_track = nullptr;
748
749 /* Calculate the total new tracks needed
750 *
751 * Determine dst_track, which will end up being nullptr, the last library override
752 * track, or a normal local track. The first two cases lead to delta_new_tracks!=0.
753 * The last case leads to `delta_new_tracks == 0`. */
754 int delta_new_tracks = delta;
755
756 /* It's possible to drag a strip fast enough to make delta > |1|. We only want to process
757 * 1 track shift at a time. */
758
759 CLAMP(delta_new_tracks, -1, 1);
760 dst_track = old_track;
761
762 while (delta_new_tracks < 0) {
763 dst_track = dst_track->prev;
764 delta_new_tracks++;
765 }
766
767 /* We assume all library tracks are grouped at the bottom of the nla stack.
768 * Thus, no need to check for them when moving tracks upward. */
769 while (delta_new_tracks > 0) {
770 dst_track = dst_track->next;
771 delta_new_tracks--;
772 }
773
774 for (int j = 0; j < -delta_new_tracks; j++) {
775 NlaTrack *new_track = BKE_nlatrack_new();
776 new_track->flag |= NLATRACK_TEMPORARILY_ADDED;
777 BKE_nlatrack_insert_before(nla_tracks, nla_tracks->first(), new_track, is_liboverride);
778 dst_track = new_track;
779 }
780
781 for (int j = 0; j < delta_new_tracks; j++) {
782 NlaTrack *new_track = BKE_nlatrack_new();
783 new_track->flag |= NLATRACK_TEMPORARILY_ADDED;
784
785 BKE_nlatrack_insert_after(nla_tracks, nla_tracks->last(), new_track, is_liboverride);
786 dst_track = new_track;
787 }
788
789 /* If the destination track is null, then we need to go to the last track. */
790 if (dst_track == nullptr) {
791 dst_track = old_track;
792 }
793
794 /* Move strip from old_track to dst_track. */
795 if (dst_track != old_track) {
796 BKE_nlatrack_remove_strip(old_track, strip);
797 BKE_nlastrips_add_strip_unsafe(&dst_track->strips, strip);
798
799 tdn->nlt = dst_track;
800 tdn->signed_track_index += delta;
801 tdn->trackIndex = BLI_findindex(nla_tracks, dst_track);
802 }
803
804 /* Ensure we set the target track as active. */
805 BKE_nlatrack_set_active(nla_tracks, dst_track);
806
807 if (tdn->nlt->flag & NLATRACK_PROTECTED) {
809 }
810 }
811
813 }
814}
815
817
818/* -------------------------------------------------------------------- */
821
824
826 ListBaseT<LinkData> trans_datas;
827};
828
831{
832 /* Element: #IDGroupedTransData. */
833 ListBaseT<IDGroupedTransData> grouped_trans_datas = {nullptr, nullptr};
834
835 /* Flag all non-library-override transformed strips so we can distinguish them when shuffling.
836 *
837 * Group trans_datas by ID so shuffling is unique per ID. */
838 {
839 TransDataNla *tdn = first_trans_data;
840 for (int i = 0; i < tc->data_len; i++, tdn++) {
841
842 /* Skip dummy handles. */
843 if (tdn->handle == 0) {
844 continue;
845 }
846
847 /* For strips within library override tracks, don't do any shuffling at all. Unsure how
848 * library overrides should behave so, for now, they're treated as mostly immutable. */
849 if ((tdn->nlt->flag & NLATRACK_OVERRIDELIBRARY_LOCAL) == 0) {
850 continue;
851 }
852
854
855 IDGroupedTransData *dst_group = nullptr;
856 /* Find dst_group with matching ID. */
857 for (IDGroupedTransData &group : grouped_trans_datas) {
858 if (group.id == tdn->id) {
859 dst_group = &group;
860 break;
861 }
862 }
863 if (dst_group == nullptr) {
864 dst_group = MEM_new_zeroed<IDGroupedTransData>(__func__);
865 dst_group->id = tdn->id;
866 BLI_addhead(&grouped_trans_datas, dst_group);
867 }
868
869 BLI_addtail(&dst_group->trans_datas, BLI_genericNodeN(tdn));
870 }
871 }
872
873 /* Apply shuffling. */
874 for (IDGroupedTransData &group : grouped_trans_datas) {
875 ListBaseT<LinkData> *trans_datas = &group.trans_datas;
876
877 /* Apply vertical shuffle. */
878 int minimum_track_offset = 0;
879 transdata_get_track_shuffle_offset(trans_datas, &minimum_track_offset);
880 if (minimum_track_offset != 0) {
881 ListBaseT<NlaTrack> *tracks = &BKE_animdata_from_id(group.id)->nla_tracks;
882
883 for (LinkData &link : *trans_datas) {
884 TransDataNla *trans_data = static_cast<TransDataNla *>(link.data);
885 NlaTrack *dst_track = static_cast<NlaTrack *>(
886 BLI_findlink(tracks, trans_data->trackIndex + minimum_track_offset));
887
888 NlaStrip *strip = trans_data->strip;
889 if ((dst_track->flag & NLATRACK_PROTECTED) != 0) {
890
891 BKE_nlatrack_remove_strip(trans_data->nlt, strip);
892 BKE_nlatrack_add_strip(dst_track, strip, false);
893
894 trans_data->nlt = dst_track;
895 }
896 else {
897 /* If destination track is locked, we need revert strip to source track. */
898 printf("Cannot moved. Target track '%s' is locked. \n", trans_data->nlt->name);
899 int old_track_index = BLI_findindex(tracks, trans_data->oldTrack);
900 NlaTrack *old_track = static_cast<NlaTrack *>(BLI_findlink(tracks, old_track_index));
901
902 BKE_nlatrack_remove_strip(trans_data->nlt, strip);
903 BKE_nlastrips_add_strip_unsafe(&old_track->strips, strip);
904
905 trans_data->nlt = old_track;
906 }
907 }
908 }
909
910 /* Apply horizontal shuffle. */
911 const float minimum_time_offset = transdata_get_time_shuffle_offset(trans_datas);
912 for (LinkData &link : *trans_datas) {
913 TransDataNla *trans_data = static_cast<TransDataNla *>(link.data);
914 NlaStrip *strip = trans_data->strip;
915
916 strip->start += minimum_time_offset;
917 strip->end += minimum_time_offset;
919 }
920 }
921
922 /* Memory cleanup. */
923 for (IDGroupedTransData &group : grouped_trans_datas) {
924 group.trans_datas.free_no_destruct();
925 }
926 grouped_trans_datas.free_no_destruct();
927}
928
930{
931 bAnimContext ac;
932
933 /* Initialize relevant anim-context 'context' data. */
934 if (ANIM_animdata_get_context(C, &ac) == 0) {
935 return;
936 }
937
938 if (!ac.datatype) {
939 return;
940 }
941
943 TransDataNla *first_trans_data = static_cast<TransDataNla *>(tc->custom.type.data);
944
945 /* Shuffle transformed strips. */
946 if (ELEM(t->mode, TFM_TRANSLATION) && t->state != TRANS_CANCEL) {
947 nlastrip_shuffle_transformed(tc, first_trans_data);
948 }
949
950 /* Clear NLASTRIP_FLAG_INVALID_LOCATION flag. */
951 TransDataNla *tdn = first_trans_data;
952 for (int i = 0; i < tc->data_len; i++, tdn++) {
953 if (tdn->strip == nullptr) {
954 continue;
955 }
956
958 }
959
960 ListBaseT<bAnimListElem> anim_data = {nullptr, nullptr};
962
963 /* Get tracks to work on. */
965 &ac, &anim_data, eAnimFilter_Flags(filter), ac.data, eAnimCont_Types(ac.datatype));
966
967 for (bAnimListElem &ale : anim_data) {
968 NlaTrack *nlt = static_cast<NlaTrack *>(ale.data);
969
970 /* Make sure strips are in order again. */
972
973 /* Remove the temp metas. */
974 BKE_nlastrips_clear_metas(&nlt->strips, false, true);
975 }
976
977 /* General refresh for the outliner because the following might have happened:
978 * - strips moved between tracks
979 * - strips swapped order
980 * - duplicate-move moves to different track. */
982
983 /* Free temp memory. */
984 ANIM_animdata_freelist(&anim_data);
985
986 /* Truncate temporarily added tracks. */
988
989 /* Perform after-transform validation. */
991}
992
994
996 /*flags*/ (T_POINTS | T_2D_EDIT),
997 /*create_trans_data*/ createTransNlaData,
998 /*recalc_data*/ recalcData_nla,
999 /*special_aftertrans_update*/ special_aftertrans_update__nla,
1000};
1001
1002} // namespace blender::ed::transform
#define ID_IS_OVERRIDE_LIBRARY(_id)
Definition DNA_ID.h:736
#define NLATRACK_STEP(snla)
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define NC_ANIMATION
Definition WM_types.hh:401
#define NA_ADDED
Definition WM_types.hh:636
#define ND_NLA
Definition WM_types.hh:540
#define CLAMP(a, b, c)
#define ELEM(...)
#define IS_EQT(a, b, c)
#define abs
static void applyTransformNLA_timeScale(PointerRNA *strip_rna_ptr, const float value)
static void special_aftertrans_update__nla(bContext *C, TransInfo *t)
static void recalcData_nla(TransInfo *t)
static void createTransNlaData(bContext *C, TransInfo *t)
char transform_convert_frame_side_dir_get(TransInfo *t, float cframe)
static bool is_overlap(const float left_bound_a, const float right_bound_a, const float left_bound_b, const float right_bound_b)
static void nlatrack_truncate_temporary_tracks(bAnimContext *ac)
static void applyTransformNLA_translation(PointerRNA *strip_rna_ptr, const TransDataNla *transdata)
Applies a translation to the given NlaStrip.
static float transdata_get_time_shuffle_offset_side(ListBaseT< LinkData > *trans_datas, const bool shuffle_left)
static void nlastrip_fix_overlapping(TransInfo *t, TransDataNla *tdn, NlaStrip *strip)
static void nlastrip_flag_overlaps(NlaStrip *strip)
bool FrameOnMouseSide(char side, float frame, float cframe)
TransConvertTypeInfo TransConvertType_NLA
static bool nlastrip_is_overlap(const NlaStrip *strip_a, const float offset_a, const NlaStrip *strip_b, const float offset_b)
static bool transdata_get_track_shuffle_offset_side(ListBaseT< LinkData > *trans_datas, const int shuffle_direction, int *r_total_offset)
static bool transdata_get_track_shuffle_offset(ListBaseT< LinkData > *trans_datas, int *r_track_offset)
static void nlastrip_overlap_reorder(TransDataNla *tdn, NlaStrip *strip)
static void nlastrip_shuffle_transformed(TransDataContainer *tc, TransDataNla *first_trans_data)
static float transdata_get_time_shuffle_offset(ListBaseT< LinkData > *trans_datas)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void BKE_nlastrips_add_strip_unsafe(ListBaseT< NlaStrip > *strips, NlaStrip *strip)
@ NLATRACK_OVERRIDELIBRARY_LOCAL
@ NLATRACK_TEMPORARILY_ADDED
MINLINE void copy_v3_v3(float r[3], const float a[3])
void BLI_addhead(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:98
void BKE_nlameta_flush_transforms(NlaStrip *mstrip)
void ED_nla_postop_refresh(bAnimContext *ac)
Definition nla_edit.cc:66
@ NLASTRIP_FLAG_SELECT
@ NLASTRIP_FLAG_INVALID_LOCATION
struct NlaTrack * BKE_nlatrack_new()
void BKE_nlatrack_set_active(ListBaseT< NlaTrack > *tracks, NlaTrack *nlt)
bool BKE_nlatrack_add_strip(NlaTrack *nlt, NlaStrip *strip, bool is_liboverride)
bool ANIM_animdata_get_context(const bContext *C, bAnimContext *ac)
@ ANIMTYPE_NLATRACK
AnimData * BKE_animdata_from_id(const ID *id)
Definition anim_data.cc:93
void ANIM_id_update(Main *bmain, ID *id)
Definition anim_deps.cc:105
void * BLI_findlink(const ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:541
bool BKE_nlatrack_is_nonlocal_in_liboverride(const ID *id, const NlaTrack *nlt)
int BLI_findindex(const ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:593
size_t ANIM_animdata_filter(bAnimContext *ac, ListBaseT< bAnimListElem > *anim_data, const eAnimFilter_Flags filter_mode, void *data, const eAnimCont_Types datatype)
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
@ ANIMFILTER_FCURVESONLY
@ ANIMFILTER_ANIMDATA
@ ANIMFILTER_FOREDIT
@ ANIMFILTER_LIST_VISIBLE
@ ANIMFILTER_DATA_VISIBLE
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
@ SNLA_NOREALTIMEUPDATES
@ NLASTRIP_TYPE_TRANSITION
void BLI_listbase_swaplinks(ListBase *listbase, void *vlinka, void *vlinkb) ATTR_NONNULL(1
Definition listbase.cc:171
NlaStrip * BKE_nlastrip_next_in_track(NlaStrip *strip, bool skip_transitions)
LinkData * BLI_genericNodeN(void *data)
Definition listbase.cc:936
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
void BKE_nlatrack_insert_before(ListBaseT< NlaTrack > *nla_tracks, NlaTrack *next, NlaTrack *new_track, bool is_liboverride)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
void RNA_float_set(PointerRNA *ptr, const char *name, float value)
void BKE_nlastrips_clear_metas(ListBaseT< NlaStrip > *strips, bool only_sel, bool only_temp)
NlaStrip * BKE_nlastrip_prev_in_track(NlaStrip *strip, bool skip_transitions)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
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
void BKE_nlatrack_remove_strip(NlaTrack *track, NlaStrip *strip)
void BKE_nlatrack_remove_and_free(ListBaseT< NlaTrack > *tracks, NlaTrack *nlt, bool do_id_user)
void BKE_nlatrack_sort_strips(NlaTrack *nlt)
void BKE_nlastrips_make_metas(ListBaseT< NlaStrip > *strips, bool is_temp)
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void BKE_nlatrack_insert_after(ListBaseT< NlaTrack > *nla_tracks, NlaTrack *prev, NlaTrack *new_track, bool is_liboverride)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
ListBaseT< NlaTrack > nla_tracks
struct NlaStrip * next
eNlaStrip_Flag flag
eNlaStrip_Type type
struct NlaStrip * prev
struct NlaTrack * prev
eNlaTrack_Flag flag
ListBaseT< NlaStrip > strips
struct NlaTrack * next
struct RenderData r
ListBaseT< SpaceLink > spacedata
eSpaceNla_Flag flag
eAnimCont_Types datatype
#define TRANS_DATA_CONTAINER_FIRST_SINGLE(t)
Definition transform.hh:45
conversion and adaptation of different datablocks to a common struct.