Three.js scene mesh check

Good day.

How can i check if the mesh is added to the scene?
Something like that.
if (scene.includes(mesh)){
console.log(“true”);
}

Hi! add name to mesh.

mesh.name="ammo";
if(scene.getObjectByName("ammo")){
console.log("true");
}
1 Like

Thank you! :grinning:

1 Like

getObjectByName involves iterating over every object in the scene, which is slow if you have a lot of objects.

If the object is a direct child of the scene, this is a lot faster:

if (object.parent === scene) {
    // The object is in the scene.
}

If the object is a child of another object, then maybe doing a recursive lookup through the .parent property is better. Try to avoid .traverse or getObjectBy*** methods if you already know/have the object.

1 Like