Spatial get sub mesh?

Is there a way to get individual sub meshes from a model ? I have a scene of a small city and I want to create rigidBodyControls for each individual mesh such as buildings and street clutter. This way I can add each Body to a bulletAppState and turn on/off collision as needed.

If you have multiple mesh you have also multiple spatials, so, after you have loaded your model cast it to Node and then use Node.getChildren() to get its children, then for each children check if its an instanceof Node, if so repeat the previous step, once you get a children which is not a Node but a Geometry, you have your “submesh”.

1 Like

I did something very similar, and this can be easily used to get all the geometries / meshes.

    private Geometry getFirstGeometry(Spatial spatial) {
        if (spatial instanceof Geometry) {
            return (Geometry) spatial;
        } else if (!(spatial instanceof Node)) {
            return null;
        }
        final List<Geometry> geoms = new LinkedList<Geometry>();
        Node node = (Node) spatial;
        node.depthFirstTraversal(new SceneGraphVisitorAdapter() {
            @Override
            public void visit(Geometry geom) {
                if (geoms.isEmpty()) {
                    geoms.add(geom);
                }
            }
        });
        return (geoms.isEmpty()) ? null : (Geometry) geoms.remove(0);
    }

…only if you take out that if statement.

Ofc and also modifying the return statement :

return (geoms.isEmpty()) ? null : (Geometry) geoms.remove(0);

and modifying the first if and returns :

     if (spatial instanceof Geometry) {
            return (Geometry) spatial;
        } else if (!(spatial instanceof Node)) {
            return null;
        }

like so :

 private List<Geometry> getGeometries(Spatial spatial) {
        final List<Geometry> geoms = new LinkedList<Geometry>();
        if (spatial instanceof Geometry) {
            geoms.add((Geometry)spatial);
        } else if (spatial instanceof Node) {
            Node node = (Node) spatial;
            node.depthFirstTraversal(new SceneGraphVisitorAdapter() {
                @Override
                public void visit(Geometry geom) {
                    geoms.add(geom);
                }
            });
        }
        return geoms;
    }

Thanks for the help everyone. You guys got me going in the right direction. I found Riccardo’s method of using getchildren to be the most efficient.

public void getName(){

    Node temp;
    temp = (Node)sceneModel;
    for (Spatial x : temp.getChildren() ) {
       if (x instanceof Geometry) {
       System.out.println(x.getName());    
    } 
    }

}