I am trying to write a tool like the example above.
But I have not found a very optimal algorithm.
Can anyone explain or show me the logic of this tool?
tks
Interesting.
I wonder what might be the real world use case of such tool?
whatever you call it. you offest the animation but the keyframes stay in place.
the goal is to avoid having too many overlapping keyframes, making it difficult to edit the animation
The gif you shared is taking infinity into account, which is something you’d have to think about. It’d also be very difficult to get a version of this working for all the possible tangent handle types (It wouldn’t really work on stepped keys, for example) without making some acceptable compromises.
Nobody is going to be able to give you a complete solution to this problem, but here are some ideas just to get you closer…
If you wanted to shift 2 frames to the left, you could insert keys 2 frames to the right, then delete the old keys and shift the new ones left. That way they would preserve the curve tangents and their relative point in time. This would probably be destructive though, so if you shifted everything by 10 frames, following by shifting back -10 frames would probably not give an identical result to the original, but that might be okay.
However, that method is not going to be anywhere as performant as that .gif if your goal is to see results in realtime (the UI portion is whole separate set of challenges, but I digress). For something like that you’d have to use the API which would also require you to compute the tangents of the new keys yourself. Something like…
- Get selected keys and use their values + tangent handle positions to build your own bezier curve representation in code.
- Perform some sort of bezier segmentation to find the value and tangents of the new keys
- Reduce the whole curve to get rid of the original keys
FYI, this is a deep deep rabbit hole filled with unexpected twists, turns, and gotchas. It’s obviously possible to make, but my point is to just be aware that even an experienced engineer could spend many months on a tool like this.