VoyVoda Posted November 22, 2018 Share Posted November 22, 2018 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. 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: 2nd output: 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 ? Quote Link to comment Share on other sites More sharing options...
JohnK Posted November 22, 2018 Share Posted November 22, 2018 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 VoyVoda 1 Quote Link to comment Share on other sites More sharing options...
VoyVoda Posted November 22, 2018 Author Share Posted November 22, 2018 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? Quote Link to comment Share on other sites More sharing options...
JohnK Posted November 22, 2018 Share Posted November 22, 2018 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! Sebavan and VoyVoda 2 Quote Link to comment Share on other sites More sharing options...
VoyVoda Posted November 22, 2018 Author Share Posted November 22, 2018 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. Quote Link to comment Share on other sites More sharing options...
Sebavan Posted November 22, 2018 Share Posted November 22, 2018 getMeshByName should work, I do not see any reason why it would not. Can you create a repro in the playground ? Quote Link to comment Share on other sites More sharing options...
JohnK Posted November 22, 2018 Share Posted November 22, 2018 This will help you to use your assets in the playground https://doc.babylonjs.com/resources/external_pg_assets VoyVoda 1 Quote Link to comment Share on other sites More sharing options...
VoyVoda Posted November 23, 2018 Author Share Posted November 23, 2018 I think rawgit no longer supports. I couldn't add the object to the playground. But what I want is this: I want to click on the front sail. like this: https://www.babylonjs-playground.com/#IRRN4C I want to click on the front sail of the yacht in this picture. This sail's name is "obje128" Quote Link to comment Share on other sites More sharing options...
JohnK Posted November 23, 2018 Share Posted November 23, 2018 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 VoyVoda 1 Quote Link to comment Share on other sites More sharing options...
VoyVoda Posted November 23, 2018 Author Share Posted November 23, 2018 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 ? Quote Link to comment Share on other sites More sharing options...
Sebavan Posted November 23, 2018 Share Posted November 23, 2018 obj needs to be pickable for it to work: https://www.babylonjs-playground.com/#IRRN4C#3 VoyVoda 1 Quote Link to comment Share on other sites More sharing options...
VoyVoda Posted November 23, 2018 Author Share Posted November 23, 2018 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 Quote Link to comment Share on other sites More sharing options...
Sebavan Posted November 23, 2018 Share Posted November 23, 2018 1 - it is defined on the scene after the import succeeded 2 - Yup it is better to limit the number of pickable obj for perf VoyVoda 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.