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 );

} );
3 Likes