Search code examples
javascriptthree.jsgltf

Exporting only meshes contained in Object3Ds to GLTF in Three.js


I've had a good hunt but can't find the solution:

the GLTFExport for Three.js allows for specific objects to be exported when they're named in the exporter.

My situation:

I have a random number of Object3Ds with child Meshes in my scene that I'd like to export, however I have removed everything from my scene apart from the objects and their child meshes and a helper to show face normals direction, and I still get the error:

Uncaught Error: THREE.GLTFExporter: userData can't be serialized

Previously I didn't have the Object3Ds and just had the meshes by themselves as children of the scene and the export worked fine. Unfortunately having the meshes within the Objects is needed for the program to work and the documentation suggests that they can be exported.

Can anyone think of a way around this issue?


Solution

  • userData is a field on every Object3D that is normally set to {}. It's intended to allow the user (you) to store data of your own on an Object3D or derived class, without messing up the internals of the object.. You may have some .userData set on an object3D in your scene..

    The easy way to find out is to scene.traverse((o)=>{console.log(o.userData)}) and see if there is userData stored on any of your objects.

    You can make a copy of your scene before you export using var sceneCopy = scene.clone(true);

    then sceneCopy.traverse((o)=>{o.userData={};});

    and then try to export sceneCopy.

    Give that a try and if it doesn't work, try back here :)

    hth