UE5 Crash when adding keyframes to Animation Sequence after adding additional frames

limnick
Fri, 15 Apr 2022 07:21:44 +0000
Character & Animation

TL;DR

Currently, on UE 5.0.0, the editor has a guaranteed crash when trying to create keyframes on a new animation sequence after adding additional frames. I’ve described a dirty fix below, but it may not be the correct solution.

Background

I did some digging in an attempt to find a workaround, but the only way I’ve found to fix this issue is to modify the UE5 source code and rebuild a custom version of the engine. Sorry to those of you hoping for a workaround in the release version of the engine, hopefully this will be fixed in 5.0.1!

From what I can gather, the bug seems to occur because the Animation Data Model is initialized with NumberOfKeys set to 2, but the arrays holding the Keyframes are only initialized with a single element.

The exception appears when adding the keyframe, in the UAnimSequence::BakeTrackCurvesToRawAnimationTracks method. First the ensure lines are all triggered on lines 4158, 4168, and 4181 of Engine\Source\Runtime\Engine\Private\Animation\AnimSequence.cpp but the editor will continue running. Then on line 4189, the line
FTransform LocalTransform(FQuat(RawTrack.RotKeys[KeyIndex]), FVector(RawTrack.PosKeys[KeyIndex]), FVector(RawTrack.ScaleKeys[KeyIndex])); throws an array access error when the forloop is on its last iteration.

The dirty fix I’m using at the moment to work around this problem is to add the following code to Engine\Source\Runtime\Engine\Private\Animation\AnimSequenceHelpers.cpp just after line 839 in the AnimationData::DuplicateKeys method

if (KeyData.Num() == 1) { // add extra keyframe if we're running into only 1 keyframe in the list, the minimum should be 2
	auto SourceKeyData = KeyData[0];
	KeyData.InsertZeroed(1, 1);
	KeyData[1] = SourceKeyData;
}

I’m not sure this is the best fix. I spent a few hours digging through all the animation code but was confused about where these bone animation tracks are being initialized. This fix just ensures that if we’re calling the DuplicateKeys method and the bone animation tracks only have a single element, we duplicate it so there are two elements and it matches the reported number of keyframes. I’m not sure how it interacts with the RemoveKeys method afterward, so it is probably worth someone with more knowledge of the UE5 codebase digging around further to fix this properly.

To reproduce this bug:

1. Create a new project, I’m using the first person project as an example. It doesn’t matter if it’s a C++ or Blueprint based project, the bug occurs in both versions. I’m also including the starter content so I have a mesh to make animations for.

2. Open the Content Browser, navigate to the Mannequin Arms Skeletal Mesh under Content/FirstPersonArms/Character/Mesh, right click it and choose edit

3. In the menu bar, choose Create Asset → Create Animation → Current pose and save the animation in any location with any name

4. Right click the top of the sequencer area and choose “Append at the end”, then enter any number of frames to add. I’m adding 2 in the example screenshots

5. Now choose a bone from the Skeleton Tree panel, then click the “+ Key” button in the top menu bar

6. The editor will crash due to an attempt to access an array beyond it’s length.

I hope this wasn’t overly long, but in my experience there’s no such thing as a bug report with too much detail! Looking forward to seeing what the correct fix ends up being for this.

Happy to provide additional information if necessary!

7 posts - 4 participants