Jump to content

getMeshByName Undefined Except ImportMesh


VoyVoda
 Share

Recommended Posts

Hello to everyone,

I believe it's a simple question, but I can't find the answer. This is what I want to do. I am adding a design to my scene with ImportMesh. I want to reach object106 using getMeshByName. Here's my code.

code_block.thumb.png.1ac1c9b5aa817a0ce518ab0637491b3a.png

These are also the outputs of the inspector.
I can access object106 with getMeshByName in ImportMesh. But it is undefined outside ImportMesh.

This first output in the inspector:

und.thumb.png.a9ace4080612a663214162bea8ddd566.png

2nd output:

obj.thumb.png.d386e075ca3ec28f788245e332c82b61.png

What is the reason of this? Why undefined?

And My 2nd written console log why did work before?

Additional:

I wanted to add a click to object106. But this isn't working. No alert ?

clicked.thumb.png.a1d8a9132b7162d4a024db6c2f2aa54c.png

Link to comment
Share on other sites

1 hour ago, VoyVoda said:

I can access object106 with getMeshByName in ImportMesh. But it is undefined outside ImportMesh.

When using SceneLoader.ImportMesh  before you can access any mesh stored in the file that file has to load. The parameter function(newMeshes) {......} is a callback function, that is it is only executed when the file has been loaded. While it is being loaded any code following SceneLoader.ImportMesh will still be executed, which is a call to console.log and as the file has not yet loaded obje106 does not yet exist.

 

1 hour ago, VoyVoda said:

I wanted to add a click to object106. But this isn't working. No alert ?

Cannot see what is wrong here. A version working  in the playground https://www.babylonjs-playground.com/#ARI3S1

Link to comment
Share on other sites

1 hour ago, JohnK said:

When using SceneLoader.ImportMesh  before you can access any mesh stored in the file that file has to load. The parameter function(newMeshes) {......} is a callback function, that is it is only executed when the file has been loaded. While it is being loaded any code following SceneLoader.ImportMesh will still be executed, which is a call to console.log and as the file has not yet loaded obje106 does not yet exist.

Thank you for the answer. Just like I thought.

Do you have a solution for the above?

1 hour ago, JohnK said:

Cannot see what is wrong here. A version working  in the playground https://www.babylonjs-playground.com/#ARI3S1

Not working on my project. I've tried it many times. I've refreshed the page a lot. Interestingly, sometimes it works, sometimes it doesn't work. It usually doesn't work. Could there be a problem with getMeshByName?

Edit:

I noticed something new right now. The click does not work when the scene is first loaded. But it works after opening Inspector ?? Really interesting ?

Is there a known reason for this?

Link to comment
Share on other sites

1 hour ago, VoyVoda said:

Do you have a solution for the above?

Not really to do anything with an imported mesh you have to wait for it to load so all code for it has to be in the function(newMeshes) {....}.

Using the assetsManager it is possible to import and store a number of meshes from a variety of files and keep the code that accesses the meshes separate. This code is only run when the assetsManager has finished.

 

Do not know the reason why (probably something to do with skeletons or submeshes) but in the PG I posted if you replace

var rabbit = newMeshes[1];

with

var rabbit = newMeshes[0];

The 'rabbit' loads but is not clickable.

In your project it might be worth replacing

BABYLON.SceneLoader.ImportMesh("", "./design/", "statue.babylon", scene, function (newMeshes) {
        
      obje106 = scene.getMeshByName("obje106");

with

BABYLON.SceneLoader.ImportMesh("obje106", "./design/", "statue.babylon", scene, function (newMeshes) {
        
      obje106 = newMeshes[0];

      console.log(newMeshes);

The console.log to check exactly what is in newMeshes and where!

Link to comment
Share on other sites

Thanks again for your reply.

I think I asked my question wrong.
Think of a house in my project. There are many objects in it. I don't want to click on the house. I just want to click on a chair inside this house. The name of this chair is object106. So I want to use getMeshByName.
Think like this: I don't want to completely click on after I import the rabbit. I just want to click on one ear.

Link to comment
Share on other sites

Disappointing about rawgit.

A couple of things in your playground

1. you assign obje106 to mesh named obje128  so console.log(obje128) will always be undefined.

2. you cannot load directly from github but you can the raw file

3. The console.log outside the import loop will always give you undefined.

So here we have the yacht loading but not all the textures (we will need to find a replacement for rawgit)

https://www.babylonjs-playground.com/#IRRN4C#1

Link to comment
Share on other sites

Thank you very much for the answers.
For rawgit I'll also try to find a replacement.

Everything is fine for now. Only one problem remained. I mentioned above. I want to click on only 1 object in this complex design. Not the click yacht. I just want to click on the front sail.

Here I left a sample right here: https://www.babylonjs-playground.com/#IRRN4C#2

I'll mark this problem as resolved after fixing it.

 

I respect you for the answers. Thanks again ?

Link to comment
Share on other sites

That's it!
Thank you. I'm very careless. It's that simple, but I didn't see it. Problem solved, thanks.

Correct if I'm wrong:
1- With ImportMesh, the object contained in getMeshByName from a complex design is defined only in ImportMesh? Other than ImportMesh is undefined.
2- To click on anything in a complex design added to the stage, mesh.isPickable = true; we need to add

Link to comment
Share on other sites

  • VoyVoda changed the title to getMeshByName Undefined Except ImportMesh

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...