Babylon.js find node By Name "I can't find the node"

someone knows how to search for a node in a scene, the issue is that I am importing a scene from blender3D and I want to search for an object in the node tree by its name but it cannot find it and if I save it in a variable it says that it is empty.
What could be the problem?

Are you sure you are executing this code after the scene is loaded? If you load the scene asynchronously, you must make sure to execute this code when it is fully loaded.

What would be the function update loop and ready in babylon?

Well I could find the node in this way, you can see that the problem is the one mentioned above.
I am a user of Godot Engine and Unity and I am used to having a function ready, update, physics among others, but I see that babylon is another world.
I couldn’t fix the problem, but this may be a way to interact with an object in the scene.

This is how the scene.getMeshByName works - Babylon.js/scene.ts at master · BabylonJS/Babylon.js · GitHub
So i find it odd that it doesn’t work…

getNodeByName iterates all other types of nodes - Babylon.js/scene.ts at master · BabylonJS/Babylon.js · GitHub

Can you reproduce this somewhere?

Create this file as tidy as possible notice that I can search for the camera or the sphere, but I cannot search for the scene that is imported, it is rareTest_find_by_name_import_mesh.zip (1.0 MB)


Would checking that mesh exists (ie has loaded) help https://www.babylonjs-playground.com/#NMU4ZM#27

If that method I look for the node does not work either, although in the way that you show if it works.
The thing is that searching for a node is a common thing in any Game Engine, but in babylon it seems much more complicated.
In Godot Engine, Unity, jmonkey Engine, pilas engine, among others there are usually functions like these.
_ready ()
_update ()
_physicsUpdate ()
_onvent ()
_input()
In other words, “_ready ()” usually preloads the nodes in variables, but I do not fully understand how it works in Babylon.js.

if createScene has any async call related to the meshes, it will not find them using this code.

You will need to either use the async-await pattern to create the scene or be sure that the mesh is loaded (in the onSuccess callback, for example).

on-scene-ready in babylon means the scene is ready to render. it doesn’t mean that the mesh is loaded. You can read about mesh loading here - Load from any file type - glTF, OBJ, STL, etc. - Babylon.js Documentation

3 Likes

@RaananW beat me to it with a short version, here is a long version expanding on part of what he said.

When I first started with Babylon.js I had to change some of my expectations of how to write things.

In your code you have

BABYLON.SceneLoader.ImportMesh("","./","suelo.babylon",scene); ///LOAD THE SCENE IMPORT Blender3D babylon plugin

Inside the createScene function you
set the camera then
create the sphere then
import the mesh (This takes time and rather than stopping your program while the mesh loads Javascript starts the loading and moves to the next line of code)

this is end scene creation with return scene and then your code continues with

/////////////////// TEST GET NODE ////////////////////////////
var scene = createScene(); //Call the createScene function

var camera = scene.getNodeByID("Camera");///work

var sphere = scene.getNodeByName("sphere");//work

var suelo = scene.getNodeByID("suelo");//NO WORK
var suelo2 = scene.getNodeByName("suelo");//NO WORK

Camera and the sphere were set up by your code and done before moving onto the next line of code. However importing a mesh takes a little time and will not have been fully loaded when Javascript comes to the scene.getNodeByName lines and so Javascript cannot find them.

There is an extra parameter to ImportMesh. This is called a callback function. This is for events that take some time, such as loading a mesh.

So in JavaScript it goes

The load mesh method says to Javascript get started on loading the mesh and give me a callback when you have finished loading so that I can execute a function made of of code that needs the mesh loaded.

In the PG https://www.babylonjs-playground.com/#NMU4ZM#28 you can see the added parameter function which then frame by frames renders the rabbit and moves its position.

There are ways to load all your files but they do require additional code, for example with the assetManager you can have

https://www.babylonjs-playground.com/#YGUADW#11

The LoadEntity function is an extra function you need to include that does the loading of the meshes.

You also need


var assetsManager = new BABYLON.AssetsManager(scene);

    assetsManager.onFinish = function (tasks) {
        start();
    };

and after loading all meshes with LoadEntity add

assetsManager.load();

The function start contains all the code to be run after all meshes imported.

3 Likes

Ok, the problem is that the loading of the models is asynchronous, and it seems that the scene loads after the models are imported, therefore those models do not exist in the scene or something like that, I will have to take a few days to digest all this info…Thank you very much for your time.

Video where they explain this problem

Hi Ariel,

this is not a problem :slight_smile: This is a common practice when loading digital assets using javascript. Actually, with doing anything with javascript. You do something, you wait for it to fulfill (or wait for the callback to be fired). The documentation page has a lot of examples. This will be a good place to start - Loading Assets - Babylon.js Documentation

1 Like

Thanks