Remove meshse not working anymore

Sorry for the many questions but I was not expecting this to happen.

I switched from using load async to meshtask to load models so previously I was using mesh container and in this container I get the .meshes and remove and dispose them. But now I am trying to do the same with my object in the scene and it is not working the same anymore? Why would this be?

Now some model parts might still be visible, I am trying to reuse the same object but just parent different meshes to it and then remove/dispose afterwards, then load new meshes.

var removeMeshes = function(meshParentObject) {
console.log('removeMeshes');
if(meshParentObject) {        
    if(meshParentObject._children) {
        for(i = 0; i < meshParentObject._children.length; i++) {
            let mesh = meshParentObject._children[i];
            scene.removeMesh(mesh);
            mesh.dispose();
            mesh.parent = null;
            mesh = null;
        }
    }
}
}

This bottom part is working fine but just to show you where I call this function before loading…

removeMeshes(RECOGNITION_flyer);
            var randomMeshTask = RECOGNITION_assetsManager.addMeshTask("loadRandomModel", "", randomModelPath+'/', randomVehicleData.model_file);        

        // randomMeshTask.onProgress = function (task) { }
        randomMeshTask.onSuccess = function (task) {
            onRandomModelLoaded(task.loadedMeshes);
            RECOGNITION_isLoadingModel = false;
        }

        randomMeshTask.onError = function (task, message, exception) {
            console.log('failed to load');
            RECOGNITION_isLoadingModel = false;
            loadRandomModel(scene);
        }

As I see this is because when you dispose 1 mesh frome the array the length of the array also becomes smaller.
I remember that I’ve seen somewhere here the full answer to your question, maybe will be lucky to find it. This is just logical thing.

UPD: Here it is - Scene.meshes is not returning all the meshes

1 Like

Oh seriously, in some languages I believe it is not possible to change the array size while looping it so that can be quite confusing.

EDIT - ended up using two loops like this and it seems to work for me, don’t want to spend al lot more time on this right now :stuck_out_tongue_winking_eye:

for(i = 0; i < meshParentObject._children.length; i++) {
                let mesh = meshParentObject._children[i];                
                mesh.dispose(false, true);
            }

            for(i = 0; i < meshParentObject._children.length; i++) {
                let mesh = meshParentObject._children[i];                
                scene.removeMesh(mesh);
            }
1 Like

hey @efsjouw
1.It is recomanded to not use properties started with _, there is a function getChildren I think.
2.I think it is enough to reverse your loop

for(let i = meshParentObject._children.length - 1; i >= 0 ; i--) {

Cheers!

Or just use while operator:

while(meshParentObject._children.length {
// your code here
}