How do you properly remove an object from the scene?

Ok silly beginner question here.

How do you properly remove an object from the scene?

In this playground, a mesh and ground is created, then I attempt to use:

scene.removeMesh(sphere);

visually it looks like it works, but with the inspector open you can see that the sphere is actually still in the scene. So clearly it’s just removing the mesh and leaving some sort of parent node?

3 Likes

Use the .dispose() method on meshes.
https://playground.babylonjs.com/#3UW6W3#2

3 Likes

Awesome! Thanks @Devin_Wright

The following goes beyond a single object, and (attempts to) clean up entire scenes.

    /******************************- unload -******************************/
    var unloadScene = function(){
        console.log('BEFORE-UNLOAD')
        console.log(nx.scene.meshes.length)
        console.log(nx.scene.materials.length)
        console.log('____')
        for(var i=0;i<sceneMesh.length;i++){
            if(sceneMesh[i].fn){nx.scene.unregisterBeforeRender(sceneMesh[i].fn)}
            sceneMesh[i].dispose();
            sceneMesh[i] = null;
        }
        sceneMesh= sceneMesh.filter(function(obj) { return obj });
        console.log('AFTER-UNLOAD')
        console.log(nx.scene.meshes.length)
        console.log(nx.scene.materials.length)
        console.log('____')
        // scene.onMeshRemovedObservable.add(mesh => {
        //    if (mesh.skeleton) {
        //       mesh.skeleton.dispose()
        //    }
        // });
    }

I like how it handles multiple aspects of unloading a scene (materials/bones) and shows if it isn’t.

Still need to add bone removal and registerBeforeRender removal.

scene.dispose() should do all the work :wink:

1 Like

Ok let’s increase the complication level a little bit.

What if we add Physics and triggers into the equation.

https://playground.babylonjs.com/#VVN2AD#1

In the example above, the sphere has an action to dispose of itself if it ever intersects the “killbox.”

However as you can see it doesn’t like that. :frowning:

Thoughts?

1 Like

Seems like an easy way to achieve making the sphere dispose correctly is setting the action manager to be on the killBox rather than the sphere. The actionManager also has a dispose method, but calling that before calling sphere.dispose didn’t seem to work.

https://playground.babylonjs.com/#VVN2AD#2

Edit:
If you do actually need it to be attached to the sphere, this seems to work.
https://playground.babylonjs.com/#VVN2AD#3

Basically wrap the dispose in a setTimeout, which I found a similar issue/answer here:

1 Like

so … smurf. A big waste of time explaining all that.

delete

definitely a bug…will be fixed by next nightly