i’m trying to remove my directionalLight and spotLights from the scene before exporting it. My attemt was to clone the scene and remove the objects equal to type “SpotLight” and “DirectionalLight” and then use this scene in the GLTFExporter. Unfortunatly i get this error: Uncaught TypeError: Cannot read properties of undefined (reading ‘traverse’)
The traversing is working, i tested it with a simple log.
Here’s part of my code:
// Traverse scene before download
const clonedScene = scene.clone();
clonedScene.traverse((obj) => {
if (obj.type === "SpotLight" || obj.type === "DirectionalLight") {
clonedScene.remove(obj);
}
});
function download() {
const exporter = new GLTFExporter();
exporter.parse(
[clonedScene, camera],
function (result) {
saveArrayBuffer(result, string);
},
//called
function (error) {
console.error("Error exporting GLTF:", error);
alert("An error occurred during export. See console for details.");
},
{ binary: true }
);
}
In addition to @drcmda , another possible explanation is that when you remove the element, this confuses the .traverse(...) method. Traverse expects some number of children, but when you remove a light, the number of children reduces and traverse does not know this and goes outside the list. Generally, when a tree structure is traversed recursively, it is highly suggested to not modify it in a way that will confuse the traversing.
If this is the case, only collect lights during traversal. Then outside traversal, remove them one by one. This should be safe.