I got a Cannot read property 'traverse' of undefined error when I try use Object3D.add in loader.
line61 TypeError: Cannot read property ‘traverse’ of undefined
at Object3D.traverse (three.js:8733)
at Object3D.traverse (three.js:8733)
at Object3D.traverse (three.js:8733)
at Object3D.traverse (three.js:8733)
at Object3D.traverse (three.js:8733)
at Scene.traverse (three.js:8733)
at 3d:53
at GLTFLoader.js:77
at GLTFLoader.js:235
at GLTFLoader.js:1726
gltf.scene.traverse( function ( child ) {
if ( child.isMesh ) {
///////////////////this line caused error////////////////////////
newScene.add(child);
///////////////////////////////////////////////////////////////////////////
}
} );
I try to load glb model and render it mesh by mesh during first rendering.
So I try to use the following code:
gltf.scene.traverse( function ( child ) {
if ( child.isMesh ) {
scene.add(child);
renderer.render( scene, camera );
}
} );
I had figure out why it have error.
maybe because of Adds object as child of this object. An arbitrary number of objects may be added. Any current parent on an object passed in here will be removed, since an object can have at most one parent.
I think the problem is that you’re trying to traverse a tree of objects, while at the same time modifying that tree of objects. Typically there is no need to add a model to the scene one mesh at a time like this – their original hierarchy may be important. If you need to do that, you could mark some of them .visible = false and gradually make them visible. Alternatively, traverse over them first to get an array of meshes, then (after that is done) add everything in the array to the scene.
Been stuck with this error for a cool 6hrs now : ( Tried damn near everything but every time I add a gltf to scene it throws the error. I tested the exact same code but with collada(dae) animated models and all works fine. Any idea where it’s all going wrong? Thanks.