Get Child nodes

Node object can get it’s parent node with GetParent().
But there is no method to get it’s child nodes, all getChild methods returns Spatials.
Is there a way to get child nodes from a parent node ?

I found a way casting Spatial to Node.

>  ArrayList<Spatial> nodes = new ArrayList(localRootNode.getChildren());
>         for(Spatial item:nodes){ 
>             Node rootScene= (Node) item; 
>             ArrayList<Spatial> nodesChild = new ArrayList(rootScene.getChildren());
>              for(Spatial itemChild:nodesChild){
>                  System.out.println(itemChild.getName());
>              }
>         }

Also: Not all children have to be nodes so your cast may fail.
Also note that getChildren(“Name”) is a better way if you only want a particular node. Also for iterating you don’t need to create a new ArrayList nodes, you can use getChildren directly

Also note that getChildren(“Name”) will be recursive meaning it can get the children of a children of a children, provided the names are unique

1 Like

If you want to process nested Spatials, it can be easier to use a SceneGraphVisitor:

I hope it’s okay if I use this thread to mention something that I think is a bug related to the traversal:
The method Node.depthFirstTraversal(SceneGraphVisitor visitor, DFSMode mode) doesn’t pass the DFSMode to the recursive calls: jmonkeyengine/ at master · jMonkeyEngine/jmonkeyengine · GitHub

Thus the PRE_ORDER mode is only applied in the first call and will default to POST_ORDER further down the scenegraph.

Test code: jme scenegraph traversal test -
The snipped also includes expected and actual results.

Or is this expected behaviour?

It’s not okay, because chances are it doesn’t get the attention it deserves :stuck_out_tongue: If you are uncertain about this you could open a new thread including a discussion, but here it looks like a clear bug to me, so it would be nice if you would just open an issue containing the information you found.

Even if this would be intentional it could be found easily in the future by searching the github issues then. The only thing not desired as github issues is like “How do I get the child nodes”

Edit in such a case you could probably even open a PR with it and it will be merged in one turn


Duplicated objects in SceneComposer have same names.

Why are you copying things into an ArrayList just to iterate over them?

This is an example i copied, it’s fast code not final.

Is the silly-long-inefficient-extra-typing way of:
for( Spatial item : localRootNode.getChildren() )


about first post, its more Java Question related.

Node is extension of Spatial, same Geometry. Its java extension, you can cast it and you did it in second post.

anyway if you want do something with spatials(Geometry or Nodes) in your node(rootNode i belive)

then you can use ready2use methods like:

    SceneGraphVisitor visitor = (Spatial spatTraversal) -> {
        if (spatTraversal instanceof Geometry) {
            Geometry geom = (Geometry) spatTraversal;
            geom.getName(); // name of geometry