How can ı delete imported mesh not dispose just delete

I use this function for import mesh

const standLoader = SceneLoader.ImportMesh(

"",

"/assets/booth/",

gltfName,

scene,

(meshes) => {

  meshes[0].scaling.x = 100.0;

  meshes[0].scaling.y = 100.0;

  meshes[0].scaling.z = -100.0;

}

);

But ı can’t use that function negative version for delete
How can ı delete imported mesh delete

By the way, I know it’s triggered but I can’t delete it

I believe you are looking for the mesh. setEnabled() method.

i have a button and when i click it i want a new mesh to appear
new mesh comes, but old mesh does not disappear
but if i reload the scene it doesn’t come on new mesh
but refreshing the page gives me negative value in terms of performance
For this reason, I need to delete the imported mesh and import the new mesh in the same scene.

So why not dispose() the old mesh?

You have these options to hide a mesh:

  1. dispose() - releases the resources of the mesh
  2. setEnabled(false) - disables the mesh so it doesn’t interact with the scene at all
  3. isVisible = false - hides the mesh but interacts with the scene, so it receives events, it interacts with the phsyics engine. It can be also used in case you don’t want to hide it’s children
1 Like

I know I know
I will try but I don’t believe myself :slight_smile:
I just wanted to delete the last mesh I uploaded with a different parameter instead of the importmesh parameter, but I guess I can’t. :slight_smile: :slight_smile:

Sorry, I don’t undersand what do you exactly mean. Do you have an example Plagyround I can have a look at?

like this
ı add new mesh
but the new mesh overlaps the old mesh
I do not want to this
1st should be deleted automatically when 2nd is added
Thanks for your help

I don’t get what’s the issue here dude :slight_smile: So use one of the three methods above.

Example PG using dispose. Click on the Elf to load the BabylonJS cube.

1 Like

Thanks you :slight_smile:

You are welcome! If it helps mark it as a solution so the question will be closed.
If you need further astistance, let me know.
Thanks!

Actually my problem is not solved
There are two reasons for this, the first is my bad english :slight_smile:
second, i’m doing this in a big project with too many components
In short, the place where I imported the mesh and the place I used to add a new one are in separate places.
and it keeps telling me dispose() is not a function
and it’s make me crazy

My guess here is you are trying to save the first mesh to variable so you can access it later. If you add something like: let myFirstMesh outside the loading block. Then inside the loading of your first mesh, put myFirstMesh = meshes[0]

Now you can access myFirstMesh.dispose() outside that loading function.

If you loose the reference to your mesh in the project you can still get it from the scene like this

scene.getMeshByName("elf").dispose()

If you are not sure that the mesh exists on the scene you better a check:

const mesh = scene.getMeshByName("elf").dispose()
if (mesh) {
   mesh.dispose()
}

or

scene.getMeshByName("elf")?.dispose()

If you don’t know the name of the mesh you can find the name in the Inspector:

2 Likes

Hey there!

This approach might work but leaving variables unitialized is a bad practice don’t even talking about the scoping issue.

EDIT: try not to use let wherever it’s possible. Stick to const :slight_smile:

Example:


    {
        // inner scope
        const elf = await BABYLON.SceneLoader.ImportMeshAsync(null, "scenes/Elf/", "Elf.gltf", scene)
    }

    scene.onPointerDown = async () => {
        // elf is not accessible here anymore        
        scene.getMeshByName("elf")?.dispose()
        const boomBox = await BABYLON.SceneLoader.ImportMeshAsync(null, "scenes/", "babylonJS_logo_v3.babylon", scene)

    };

I deleted the images so my boss wouldn’t get angry

Did you see
I’m about to go crazy
const mesh = scene.getMeshByName(gltfName)?.dispose();

if (mesh) {

mesh.dispose();

}

You have to put the MESH name here and not the gltfName.

EDIT:
One file may contain multiple meshes and transform nodes as well.

 const elf = await BABYLON.SceneLoader.ImportMeshAsync(null, "scenes/Elf/", "Elf.gltf", scene)

The null tells the method to load all meshes from the file.

believe me i tried every way

const mesh = scene.getMeshByName(“stant1”)?.dispose();

if (mesh) {

mesh.dispose();

}

const mesh = scene.getMeshByName(“stant1”)?.dispose();

if (mesh) {

mesh.dispose();

}

const standLoader = SceneLoader.ImportMesh(

"",

"/assets/booth/",

gltfName,

scene,

(meshes) => {

  meshes[0].scaling.x = 100.0;

  meshes[0].scaling.y = 100.0;

  meshes[0].scaling.z = -100.0;

}

);

standLoader.onMeshLoaded = (mesh) => {