scene.getNodeByID() does not return a node that was just created?

Hi all,

I have a feeling the could below should work, but instead getNodeByID returns null

  // This creates a basic Babylon Scene object (non-mesh)
    var scene = new BABYLON.Scene(engine);

    var myNode = new BABYLON.Node("my-node", scene)
    // This console log prints null.
    // I would expect it to print the node
    console.log(scene.getNodeByID(myNode.id))

I actually don’t understand why is the node not returned and how could I make getNodeByID return it.

Thanks.

Update 1
I saw that it works with TransformNode. But transform node is much more that what I need. Because of Scene does not read metadata when loading from GLTF I am trying to load some metadata in babylon and I am doing it from a node with name "special-node-holding-metadata’ as there could be no metadata in the scene when the scene is loaded from gltf.

At the end I am not sure if this is a problem but I did spend a good 10-20 minutes looking at why can’t I create a BABYLON.Node in a scene.

Indeed, only transform nodes are appended to the scene graph and can be queried with getTransformNodeByXXX. I think it’s because only nodes with transform data (position, rotation, …) are tracked in the scene.

I am sure there is a good reason for that. It is just confusing to do

new BABYLON.Node("name", scene)

and not have the node on the scene. Why would it even accept the scene as a param if it is then not on the scene. I am wondering if there a case where you would like to create nodes that are passed a scene param but are then not on the scene.

I think mine is such a case but it is very specific. Do you know of a more broader case?

The scene is used internally for some operations, like the ones related to animations (beginAnimation) or behaviour management (addBehaviour).

Node is a base class. I don’t think it is intended to be created. I think we can probably mark the class as abstract in TypeScript but I’m not sure it will do anything for JavaScript. I’m struggling to understand why you want to create one?

1 Like
  1. We are generating a gltf.
  2. We want to put metadata on the scene. We can’t since it is not loaded.
  3. We decide to put the metadata on a node with name “this-special-node-for-our-product”
  4. I try to write a test for this. The test loads a gltf, and checks if we have processed it correctly based on the metadata in the “this-special-node-for-our-product”
  5. As I am waiting for the server side colleague to implement this I write the specs to just create the node by myself before running the spec. When the logic is ready on the server side the node will be available in the document. But now for the spec I would like to create it.

So I write

setMetadata()
load()
expect(...).toHaveDoneThingsWriteBasedOnMetadata()

In the setMetadata is do

const node = new BABYLON.Node("this-special-node-for-our-product", scene)
node.metadata={}
node.metadata={gltf:{extras: {...}}}

And there I notice that creating a BABYLON.Node was working different from what I was expected. I thought that as it is a Node, and all the names on the scene are getNodeByID, getNodes, getNodeByName it was natural for me to create a BABYLON.Node and to pass a scene.

I have no problem creating BABYLON.TransformNode, I was just surprised.

This might be better for the other thread, but does putting the scene metadata on the __root__ node not work?

It works.

It was this or “this-special-node-for-our-product”. As the gltf is passing through a number of other tools where we need the metadata and it might not even reach Babylon, and as I had the case of a second root node created sometimes on download and two root nodes were present in the document I’ve decided to go with another dummy node holding the metadata.

Ok, I’m not sure I understand exactly what you mean, but it’s certainly up to you.