Jump to content

How can I interact with imported Mesh?


Polpatch
 Share

Recommended Posts

Hi guys, (I'm so sorry for my English)

 

I'm new in babylon's programming and I'm in trouble.

i'm importing a local Mesh with the method:

 



BABYLON.SceneLoader.ImportMesh("him", "models/Dude/", "dude.babylon", scene, function(newMeshes){
scene.cameras[0].setTarget(newMeshes[0].position);

scene.getMeshByName("him").scaling = new BABYLON.Vector3(0.07, 0.07, 0.07); //this work
});

scene.getMeshByName("him").position +=10; //scene.getMeshByName("him") is null


 

In the function's scope of the argument (function(newMeshes){...}) i can interact with my Mesh (if i inspect the scene i can see it).

 

However out of that function i can't reach it.

How can i interact with this imported mesh?

Link to comment
Share on other sites

Hello and welcome!!!

 

this is because the ImportMesh functions is asynchronous. Which means that the function returns directly and will be fullfilled afterwards.

You saved my life, thanks a lot!

 

So i can resolve that with a  loop like this:

/*import mesh code*/.../******************/while(scene.getMeshByName("him") == null){}scene.getMeshByName("him").position += 10;

right? (I can't test this now)

Link to comment
Share on other sites

Yes or even better:

 

BABYLON.SceneLoader.ImportMesh("him", "models/Dude/", "dude.babylon", scene, function(newMeshes){        
        scene
.cameras[0].setTarget(newMeshes[0].position);
        
        scene
.getMeshByName("him").scaling = new BABYLON.Vector3(0.07, 0.07, 0.07); //this work

 

      continueMyCode();
    });
 

Do whatever you want to do in a function called continueMyCode() instead of directely after calling ImportMesh

Link to comment
Share on other sites

Yes or even better:

 

BABYLON.SceneLoader.ImportMesh("him", "models/Dude/", "dude.babylon", scene, function(newMeshes){        

        scene.cameras[0].setTarget(newMeshes[0].position);

        

        scene.getMeshByName("him").scaling = new BABYLON.Vector3(0.07, 0.07, 0.07); //this work

 

      continueMyCode();

    });

 

Do whatever you want to do in a function called continueMyCode() instead of directely after calling ImportMesh

Yeah, you are right. But Probably I have to interact different mesh between them.

I wanted to understand why I could't find the imported mesh out of that function.

I will use each the way. Thanks again   :lol:

Link to comment
Share on other sites

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...