object.toJSON does not export the animations

When I export a mesh, or a group of meshes, by doing a toJSON on the top level object, everything exports just nicely to JSON but not the animations. How do I get those to be included?

Animations are not processed by Object3D.toJSON(). They have to be managed separately. You have to call AnimationClip.toJSON() and store the resulting JSON along your other data e.g. in the animations property (which is of type array) of your object JSON. If you do so, ObjectLoader is able to parse the animation clips so you can access them via object.animations.

It’s a bit unfortunate that it’s possible to deserialize animation clips via ObjectLoader but not to automatically serialize them when calling Object3D.toJSON(). This might be worth a feature request at github.

1 Like

I’m not aware that there’s any real sense in which an object “has” an animation. ObjectLoader attaches animations as mesh.animations or mesh.geometry.animations, but other loaders hand off the animation clips in different ways. And in particular, a single animation can easily affect many objects in some formats. I don’t think we should encourage users to rely on mesh.animations as an import/export API.

For another example, GLTFExporter lets you do something like this:

exporter.parse( object, ( content ) => {

  console.log( content );

}, { animations: [ clip1, clip2, ... ] } );

Fair enough. In this case, we might want to change ObjectLoader at some point.

Strange enough, the clips in the model that comes out of the FBX loader, look very much like THREE Animation Clips but they do not have a toJSON method.

And I noticed that ObjectLoader does not handle skinnedMeshes… this might not be the way to store my animated objects…

Serializing skinned meshes is work in progress. I hope the project will provide support for this in one of the next releases.

That is sounding hopeful, because toJSON in combination with PAKO-deflation library give me very good results at keeping the file size down and the GLTF exporter freezes my browser allmost every time.

Looking forward to the update that will bring this magic…

Related PR: