A Simple node tree printer to HTML

I threw this together in a few minutes and thought others could use it too. It takes in a node and prints out an html list of all the children, and their children’s children. It then automatically opens it up in your web browser. Good for a quick debug of what is in your scene.



[java]

/**

  • Print all children from this node on down

    */

    public static void printNode(Node node) {

    String html = “<!DOCTYPE html><html>”;

    html = html + mineNode(node);

    html = html+"</html>";

    try {

    File f = File.createTempFile(“node”, “.html”);

    FileWriter fw = new FileWriter(f);

    fw.write(html);

    fw.flush();

    fw.close();

    Desktop.getDesktop().open(f);

    } catch (IOException ex) {

    Logger.getLogger(this.getClass().getName()).log(java.util.logging.Level.SEVERE, “Error writing html file”, ex);

    }



    }



    private static String mineNode(Node parent) {

    String s = parent.getName()+"<br><ul>";

    if (parent.getChildren().isEmpty())

    return parent.getName();

    else {

    for (Spatial child : parent.getChildren()) {

    if (child instanceof Node) {

    s = s + “<li>”+mineNode((Node)child)+"</li>";

    } else {

    s = s + “<li>”+child.getName()+"</li>";

    }

    }

    }

    s = s + “</ul>”;

    return s;

    }[/java]



    EDIT: I fixed the error log so it doesn’t include my class, and added the doctype.
8 Likes

nice man

wow, nice and simple idea. thanks

awesome, thanks :slight_smile: could you also add a doctype?



perhaps the new HTML5 one should suffice



[xml]<!DOCTYPE html>[/xml]

That’s smart, I have something similar but didn’t think to do the HTML thing.



[java]

public static void printSceneGraph() {

Node node = AppContext.getJme3().getRootNode();

StringBuilder graph = new StringBuilder(“n”);

buildGraph(node, graph, “”);

logger.log(Level.INFO, graph.toString());

}



private static void buildGraph(Spatial parent, StringBuilder graph, String indent) {

graph.append(indent).append(parent).append(parent.getLocalTranslation()).append(“n”);

if (parent instanceof Node) {

indent += “t”;

for (Spatial s: ((Node)parent).getChildren())

buildGraph(s, graph, indent);

}

}



}

[/java]



Mine puts in localTranslation as well and formats it for plain text output not html but seems pretty similar otherwise :slight_smile:

1 Like

(“n” and “t” have a slash before them - forum isn’t showing it even inside the java tags)

Indeed very handy, I added in to display the class name to know what type of spatial it is.



[java]

private static String mineNode(Node parent) {

String ptype = parent.getClass().getSimpleName();

String s = parent.getName() + " (" + ptype + ")" + "<br><ul>";

if (parent.getChildren().isEmpty())

return parent.getName() + " (" + ptype + ")";

else {

for (Spatial child : parent.getChildren()) {

if (child instanceof Node) {

s = s + "<li>" + mineNode((Node) child) + "</li>";

} else {

String type = child.getClass().getSimpleName();

s = s + "<li>" + child.getName() + " (" + type + ")" + "</li>";

}

}

}

s = s + "</ul>";

return s;

}

[/java]



http://puu.sh/XKJZ

1 Like