How to play the animation of GLTF model

I have load a GLTF with a simple translation animation successfully by vtkGLTFReader int VTK 9.0.1. I want to play the animation. But I fail after using:

gltfReader->EnableAnimation(0); //0 is the correct animation index

I notice the note of document said Use UPDATE_TIME_STEP. But I don’t know how to deal with it. Can anyone tell me some tips. Thanks in advance.

Just call reader->UpdateTimeStep(timestep); and render the scene again.
You will have to handle a timer yourself to synchronize the animation.
Note that the animation is currently computed on the CPU so it can be slow depending on your scene complexity.

Does timestep mean how much millisecond time that the animation takes in one render frame?

I use UpdateTimeStep() in the timer callback. But the animation of the model is so fast that I can not see the its moving and changing smoothly. I want to play the animation in 10 seconds. Any tips or example? Thanks.

in main():

...

auto cb = vtkSmartPointer<vtkTimerCallback2>::New();
cb->setReader(gltfReader);

//call the timer per 100 milliseconds
rwi->AddObserver(vtkCommand::TimerEvent, cb);
int timeId = rwi->CreateRepeatingTimer(100);
cb->setTimeId(timeId);

gltfReader->SetApplyDeformationsToGeometry(true);
gltfReader->EnableAnimation(0);
gltfReader->UpdateTimeStep(10);

m_render->GetRenderWindow()->Render();

In the Excute() of Timer callback:

    virtual void Execute(vtkObject* caller, unsigned long eventId, void* vtkNotUsed(callData))
    {
        vtkRenderWindowInteractor* iren =  dynamic_cast<vtkRenderWindowInteractor*>(caller);if (vtkCommand::TimerEvent == eventId)
        if (vtkCommand::TimerEvent == eventId)
        {
            ++TimerCount;

            if (TimerCount < 10)
            {
                reader->UpdateTimeStep(10); //I'm not sure how to set the timestep correctly
                reader->Modified();
                render->GetRenderWindow()->Render();
            }
            else
            {
                iren->DestroyTimer(timerId);
                reader->DisableAnimation(0);
                reader->Modified();
                render->GetRenderWindow()->Render();
            }
        }
    }

You have to set the framerate first (SetFrameRate, default is 60).
CreateRepeatingTimer should have a value of 1000 / FrameRate.
UpdateTimeStep value is TimerCount * 1000 / FrameRate.

1 Like

@Michael I follow your suggestion to modify the code, I’m not sure whether the TimerCount value(10, 100, 100, …, I’ve tried many different value) is suitable. The animation is still very fast. It seems to only have the first key frame and the last key frame without any smooth transition. Then I use the following code in the scope of Execute():
double fps = 1.0 / renderer->GetLastRenderTimeInSeconds();
The fps is about 10000. Is that normal? I use the code after creating vtkRenderWindowInteractor :
rwi->SetStillUpdateRate(0.01667); // 1/60≈0.01667, I want to keep the fps value of scene to 60
But the fps I get is still about 10000. I guess the speed of the animation maybe relates to the scene fps. Or any other reason?

@Michael, could you give some tips?

The range of UpdateTimeStep() input parameter is from 0 to 1. I use int-type as the input so that the animation will end after one update loop. I modify the TimerCount in Execute() as the follow:
TimerCount = TimerCount + 0.01;
Now the animation plays correctly.