Adding tracks and keyframes to the Sequencer C++

Hi.
I want to generate the Sequencer timeline from the code. That’s, I need to add tracks and keyframes in C++ in runtime.
I have found several similar questions like these:

https://forums.unrealengine.com/development-discussion/c-gameplay-programming/1624836-level-sequence-actor-rebinding-in-c

But it doesn’t work for me.
Here is what I do:

  1. Create an actor in C++ and pass it reference to an existing in the scene empty ULevelSequence

(AnimGenerator.h)

    UPROPERTY(EditAnywhere, BlueprintReadWrite)
ULevelSequence* SequenceAsset;

(AnimGenerator.cpp)

   AnimGenerator::AnimGenerator()
  {
   Mesh1 = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("PlaneMesh1"));
   RootComponent = Mesh1;
   }


   void AnimGenerator::BeginPlay()
 {
 
Super::BeginPlay();

FGuid MyGuid = FUniqueObjectGuid::GetOrCreateIDForObject(this).GetGuid();
    //doesn't work
    //t1  = SequenceAsset->GetMovieScene()->AddTrack(UMovieScene3DTransformTrack::StaticClass(), MyGuid);
    //doesn't work.
    t1 = SequenceAsset->MovieScene->AddTrack(UMovieSceneAudioTrack::StaticClass(), MyGuid);

   }

I tried to add different types of tracks. AddTrack() always returns nullptr. No crashes, no warnings. What am I missing?

There are 2 options: if you simply want to add the track under the Camera just pass in the Camera’s GUID and it will work. If you want a new section use:

UMovieSceneEventTrack* NewMasterTrack = NewSequence->GetMovieScene()->AddMasterTrack<UMovieSceneEventTrack>();
Sequencer->OnAddTrack(NewMasterTrack, FGuid());

then add your Event section to the new track directly.

Actually no,I put this one aside and getting back to this task very soon. Thanks a lot for the tip. I will try it out.