Get Child nodes


#1

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 ?


#2

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());
>              }
> 
>         }

#3

See https://wiki.jmonkeyengine.org/jme3/advanced/spatial.html
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


#4

If you want to process nested Spatials, it can be easier to use a SceneGraphVisitor: https://wiki.jmonkeyengine.org/jme3/advanced/traverse_scenegraph.html


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: https://github.com/jMonkeyEngine/jmonkeyengine/blob/master/jme3-core/src/main/java/com/jme3/scene/Node.java#L775

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

Test code: https://pastebin.com/XCbwLSAS
The snipped also includes expected and actual results.

Or is this expected behaviour?


#5

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


#6

Duplicated objects in SceneComposer have same names.


#7

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


#8

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


#9

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


#10

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
        }
    };
    node.breadthFirstTraversal(visitor);