scene.getMeshByName() returns null on imported GLTF scenes

Hi!

I`m trying to get a mesh from 2 imported scenes created by independent from one another artists. I can do it without a problem in ThreeJS, but in Babylon I get both null if I use any of the scene.getMesh… methods, and also the scene.meshes property does not appear or behave as a proper array that I can traverse.

However if I add additional, lets say BOX mesh to scene, I can easily access it with scene.getMeshByName("arbitraryboxmeshname") for instance.

One of the scenes I`m using is a GLTF model from this article here: Three.js Loading a .GLTF File

What am I doing incorrectly here?

     window.addEventListener('DOMContentLoaded', function() {
          var canvas = document.getElementById('renderCanvas');
          var engine = new BABYLON.Engine(canvas, true);
          // All the following code is entered below here.

          var createScene = function() {
            var scene = new BABYLON.Scene(engine);
            var camera = new BABYLON.FreeCamera('camera', new BABYLON.Vector3(0, 5,-10), scene);
            camera.setTarget(BABYLON.Vector3.Zero());
            camera.attachControl(canvas, true);
            var light = new BABYLON.HemisphericLight('light1', new BABYLON.Vector3(0,1,0), scene);
            BABYLON.SceneLoader.Append("", "bab_test_02.gltf", scene, function (scene) {
                scene.createDefaultCameraOrLight(true, true, true);
            });
            return scene;
          }
       
          // All the following code is entered above.

          var scene = createScene();
		  
			var blueBox = BABYLON.Mesh.CreateBox("blue", 20, scene);
			var blueMat = new BABYLON.StandardMaterial("ground", scene);
			blueMat.diffuseColor = new BABYLON.Color3(0.4, 0.4, 0.4);
			blueMat.specularColor = new BABYLON.Color3(0.4, 0.4, 0.4);
			blueMat.emissiveColor = BABYLON.Color3.Blue();
			blueBox.material = blueMat;
			blueBox.position.x += 100;
		  
		  var car = scene.getMeshByName("Object004_primitive0");
          console.log(scene.meshes); //shows [] that I can open in debugger, but it does not work as an array
          console.log(car); //Shows null
		  
          scene.render();


          engine.runRenderLoop(function() {
              scene.render();
          });
      });

Thank you in advance!

Hello.Welcome to the forum

Can you maybe create a playground with your code? It would be very helpful to debug it.

Here you can find how to import your assets
https://doc.babylonjs.com/resources/external_pg_assets

https://playground.babylonjs.com/#U5SSCN#10
Here I used a model already provided by Babylon as GLTF import example. Same problem.

You have to be sure that your model is fully loaded, it’s the onSuccess function:

https://playground.babylonjs.com/#U5SSCN#11

Also, note that gltf meshes using multiples materials will be split into parts: the root part will not be a Mesh but a TransformNode, only submeshes will using Mesh class. Example here with the Cornell Box model: https://www.babylonjs-playground.com/#4AJ16M#3
img

the cornellBox.000 object created onto Blender was a unique mesh but using multiples materials

2 Likes

Try this PG

I added lines 17 and 21 - to wait for load to complete as @Vinc3r suggested.

Seems to work now.

cheers, gryff :slight_smile:

NB you can use getMeshByID as well.

1 Like

Got it! Thank you so much!

Exactly what I need in my use case, very much appreciated! :blush: