To get array of all meshes

“scene.children” only return array which is directly added into scene.
if I add cubeB in cubeA,
“scene.children” gave only cubeA.
and there cubeA.children gave cubeB.

is there any way to get all 3d meshes of scene?

You could do something recursive if the child has children of its own:

function listChildren(children) {
    let child;
    for (let i = 0; i < children.length; i++) {
        child = children[i];

        // Calls this function again if the child has children
        if (child.children) {
            listChildren(child.children);
        }
        // Logs if this child last in recursion
        else {
            console.log('Reached bottom with: ', child);
        }
    }
}

listChildren(scene.children);

You can also use Object3D.traverse() which executes the given callback on the object and all descendants. In this way, you can identify all meshes in your scene like so:

scene.traverse( function( object ) {

    if ( object.isMesh ) console.log( object );

} );
4 Likes

This gives an error. Has the API changed or is it typed incorrectly?

img
img

Maybe you can try this instead:

if(object instanceof THREE.Mesh){
    console.log(object)
}
1 Like

Bit of a hack but that’s what I wound up doing, thanks!

1 Like