colour-science / colour

Colour Science for Python
https://www.colour-science.org
BSD 3-Clause "New" or "Revised" License
2.03k stars 257 forks source link

PR: Port-Based Node-Graph #1277

Open KelSolaar opened 1 week ago

KelSolaar commented 1 week ago

Summary

This PR implements support for a port-based node-graph via 3 new classes:

The idea behind it is to be able to construct processing graph, initially with code but possibly using a UI in the future.

The API is as follows:

class NodeAdd(PortNode):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.description = "Perform the addition of the two input port values."

        self.add_input_port("a")
        self.add_input_port("b")
        self.add_output_port("output")

    def process(self):
        a = self.get_input("a")
        b = self.get_input("b")

        if a is None or b is None:
            return

        self._output_ports["output"].value = a + b

        self.dirty = False

class NodeMultiply(PortNode):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.description = (
            "Perform the multiplication of the two input port values."
        )

        self.add_input_port("a")
        self.add_input_port("b")
        self.add_output_port("output")

    def process(self):
        a = self.get_input("a")
        b = self.get_input("b")

        if a is None or b is None:
            return

        self._output_ports["output"].value = a * b

        self.dirty = False

node_add = NodeAdd()
node_add.set_input("a", 1)
node_add.set_input("b", 1)
node_multiply = NodeMultiply()
node_multiply.set_input("b", 2)

graph = PortGraph()

graph.add_node(node_add)
graph.add_node(node_multiply)

graph.connect(node_add, "output", node_multiply, "a")

graph.process()

print(node_multiply.get_output("output"))
graph.to_graphviz().draw("Graph.png", prog="dot")

image

This is obviously a simple and naive example but the immediate use case is for colour-hdri:

image image image image

Preflight

Code Style and Quality

Documentation

KelSolaar commented 1 week ago

I will look at creating some subclasses that have an execution route instead of relying on the ports, that would allow for adding some control flow nodes, e.g., for loop and conditional branching.

KelSolaar commented 6 days ago

I have implemented support for looping, this can be done sequentially, via thread or multiprocessing using the colour.utilities.For, colour.utilities.ParallelForThread and colour.utilities.ParallelForMultiProcess nodes respectively.

coveralls commented 6 days ago

Coverage Status

coverage: 99.833% (-0.1%) from 99.973% when pulling fc8c62459b49a367ce485c194df17bab594943ca on feature/graph into 233ee7283cbc676b262cdcfd13f376af6278375d on develop.

tjdcs commented 4 days ago

I'll have more time for a review on Sunday time in central US. Looks promising!