Adding tracks and keyframes to the Sequencer C++

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:

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


    UPROPERTY(EditAnywhere, BlueprintReadWrite)
ULevelSequence* SequenceAsset;


   Mesh1 = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("PlaneMesh1"));
   RootComponent = Mesh1;

   void AnimGenerator::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.