shoebot / toybot-scenegraph

Experimental scenegraph renderer for bot languages
0 stars 1 forks source link

ConsoleRenderer - Output transformation "as_json" #9

Closed stuaxo closed 4 years ago

stuaxo commented 4 years ago

There's something in this that doesn't seem quite right:

ConsoleRenderer

The ConsoleRenderer outputs every node of scenegraph to the console

class ConsoleRenderer:
    def render(self, graph):
        for node in graph:
            print(node.as_json())

With this, we get newline delimited json, which is format in itself, but not as standard as say outputting the whole Graph as json.

To output the whole graph as json we could do this:

print(graph.as_json())

or if we want streaming, something like this:

     def render(self, graph):
         print('[')
         for node in graph:
             print(f"{node.as_json()},")
         print(']')

There are two concepts in here - output target "console" and output format "json".

We could create the concept of a Transform that takes a node and outputs another node or some other data format.

node->transform->node or node->transform->data

as_json would be a transform that accepts some nodes and output json.

ConsoleRenderer by default would have as_json as it's output transform, to output standard python objects the user could specify this in the constructor: ConsoleRender(outputtransformer=None).

This abstraction should work well for format variants (e.g. newline delimited json) as we have a place to put this.

Being able to transform objects in a graph into other things should be quite powerful, the hair examples in nodebox1 / shoebot work by iterating paths and replacing them with new generated paths, maybe this concept can be used for the same sort of thing, by having transformer of path->path.

This should fit well with the graph analogy of components we can clip together.

stuaxo commented 4 years ago

Output format is implemented as a function in notebook.

https://github.com/shoebot/toybot-scenegraph/blob/prototyping/notebooks/ToyBot_SceneGraph_Integration_4_Path_Nodes.ipynb

stuaxo commented 4 years ago

Updated implementation to use function to transform output.

stuaxo commented 4 years ago

Closing as json output implemented.

transformations as node based thing will probably come later.