Please serialize and restore runtimeAnimations too

Hi all,

Right now Animation.serialize doesn’t save runtimeAnimations object: source_code

But this property is very useful, because it shows where is the animation right now (frame and value). It means a lot if we want to save our dynamic scene on the flight properly.
The big plus if you will implement restore and replay of those runtime animations on SceneLoader. Append call. I didn’t find the code location for this.

This fix doesn’t seem very hard, but it will be very useful.
I could even try this by myself, but I am not sure about several things:

  1. Why runtimeAnimations is an array in a single animation? Probably animation might have several concurrent runs. Not sure how to handle it correctly.
  2. Didn’t find where SceneLoader.Append converts serialized data into the actual animation.

So probably regular contributors already know answers on these questions, so it will be an easy fix for them.

Hello! Long time no see :wink:

We do not save runtimeAnimations because most of the needs we faced so far was to save the scene in its original state

That being said, I totally get your point.

An animation is a template that can be used to animate multiple objects. You can have a translation animations and apply it to several meshes (hence the array of runtimeAnimation).

I do not plan to add it as it will break the backward compatibility but if someone wants to give it a try I will only ask to add a boolean on the Animation class (off by default) to control if runtime animations are saved or not

Pour info, tout se passe la:

Hi @Deltakosh,
Yeah long time no see. I’ve been busy with relocation. But at the same time I continued working with save/load functionality for my gaming engine based on BabylonJS. I have some progress there.

Let me add some more details on this topic.

I don’t use built-in SceneSerializer at all. I’ve created my own instead, because I put some extra data in scene object and want some more control during saving, especially with PlayerCamera and Player itself. I have own classes for that.

But at the same time I still rely on built-in mesh.serialize() method. It saves mesh.animations, but not the runtimeAnimations inside of it.

I am in progress of writing my own mechanics for saving runtimeAnimations but I see that it brings too much overhead. Literally a lot. After mesh is serialized I need to do the 3 nested loops. It’s minimum.

Something like that:

for (let m = 0; m < mesh.animations.length; m++) {
    for (let s = 0; s < serializedMesh.animations.length; s++) {
        if (mesh.animations[m].name === serializedMesh.animations[s].name) {
            serializedMesh.animations[s].runtimeAnimations = [];
            for (let a = 0; a < mesh.animations[m].runtimeAnimations.length; a++) {
                 serializedMesh.animations[s].runtimeAnimations[a] = serializeSomeHow(mesh.animations[m].runtimeAnimations[a]);

And I will need to do something similar after SceneLoader.Append call.

mesh.serialize() already has a loop through mesh.animations, so it would be the right way to put it there.

I don’t need the whole runtimeAnimation object. Only the current frame. It might be even appended like that mesh.animations[m].currentFrame. And null if animation is not running during the save.

Of course I could re-write mesh.serialize() method like I did with SceneSerializer or use modified local version of BabylonJS. But this will put me in trouble with BabylonJS updates.

What would you need ? Maybe you can do a PR to see what we can do to help with your needs

@Deltakosh, I’d like to have

  1. Save the current animation frame if animation was running when mesh.serialize() call occured. I am pretty sure it can be done here: source_link.

  2. Re-run mesh’s animation from saved animation frame when we load the scene from .babylon file (through SceneLoader.Append) . Unfortunately I didn’t find the place in the source code where BabylonJS converts serialized mesh from .babylon file to the ‘real’ one. That’s why I can’t provide PR for that for now.

Understood: Here is the Parse