colour.utilities.PortNode#

class colour.utilities.PortNode(*args: Any, **kwargs: Any)[source]#

Bases: TreeNode, MixinLogging

Define a node with support for input and output ports.

Parameters:
  • name (str | None) – Node name.

  • description (str | None)

Attributes

Methods

Examples

>>> 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
>>> node = NodeAdd()
>>> node.set_input("a", 1)
>>> node.set_input("b", 1)
>>> node.process()
>>> node.get_output("output")
2

Return a new instance of the colour.utilities.Node class.

Parameters:
  • args – Arguments.

  • kwargs – Keywords arguments.

  • name (str | None)

  • description (str | None)

__init__(name: str | None = None, description: str | None = None)[source]#
Parameters:
  • name (str | None)

  • description (str | None)

property input_ports: Dict[str, Port]#

Getter property for the input ports.

Returns:

Input ports.

Return type:

dict

property output_ports: Dict[str, Port]#

Getter property for the output ports.

Returns:

Output ports.

Return type:

dict

property dirty: bool#

Getter and setter property for the node dirty state.

Parameters:

value – Value to set the node dirty state.

Returns:

Whether the node is dirty.

Return type:

bool

property edges: Tuple[Dict[Tuple[Port, Port], None], Dict[Tuple[Port, Port], None]]#

Return the edges of the node.

Each edge represent a port and one of its connections.

Returns:

Edges of the node as a tuple of input and output edge dictionaries.

Return type:

tuple

property description: str | None#

Getter and setter property for the node description.

Parameters:

value – Value to set the node description with.

Returns:

Node description.

Return type:

str or None

add_input_port(name: str, value: Any = None, description: str | None = None, port_type: Type[Port] = Port) Port[source]#

Add an input port with given name and value to the node.

Parameters:
  • name (str) – Name of the input port.

  • value (Any) – Value of the input port

  • description (str | None) – Description of the input port.

  • port_type (Type[Port]) – Type of the input port.

Returns:

Input port.

Return type:

colour.utilities.Port

Examples

>>> node = PortNode()
>>> node.add_input_port("a")  
<...Port object at 0x...>
remove_input_port(name: str) Port[source]#

Remove the input port with given name from the node.

Parameters:

name (str) – Name of the input port.

Returns:

Input port.

Return type:

colour.utilities.Port

Examples

>>> node = PortNode()
>>> port = node.add_input_port("a")
>>> node.remove_input_port("a")  
<...Port object at 0x...>
add_output_port(name: str, value: Any = None, description: str | None = None, port_type: Type[Port] = Port) None[source]#

Add an output port with given name and value to the node.

Parameters:
  • name (str) – Name of the output port.

  • value (Any) – Value of the output port

  • description (str | None) – Description of the output port.

  • port_type (Type[Port]) – Type of the output port.

Returns:

Output port.

Return type:

colour.utilities.Port

Examples

>>> node = PortNode()
>>> node.add_output_port("output")  
<...Port object at 0x...>
remove_output_port(name: str) Port[source]#

Remove the output port with given name from the node.

Parameters:

name (str) – Name of the output port.

Returns:

Output port.

Return type:

colour.utilities.Port

Examples

>>> node = PortNode()
>>> port = node.add_output_port("a")
>>> node.remove_output_port("a")  
<...Port object at 0x...>
get_input(name: str) Any[source]#

Return the value of the input port with given name.

Parameters:

name (str) – Name of the input port.

Returns:

Value of the input port.

Return type:

object

Raises:

AssertionError – If the input port is not a member of the node input ports.

Examples

>>> node = PortNode()
>>> port = node.add_input_port("a", 1)  
>>> node.get_input("a")
1
set_input(name: str, value: Any) None[source]#

Set the value of the input port with given name.

Parameters:
  • name (str) – Name of the input port.

  • value (Any) – Value of the input port

Raises:

AssertionError – If the input port is not a member of the node input ports.

Return type:

None

Examples

>>> node = PortNode()
>>> port = node.add_input_port("a")  
>>> port.value
>>> node.set_input("a", 1)
>>> port.value
1
get_output(name: str) None[source]#

Return the value of the output port with given name.

Parameters:

name (str) – Name of the output port.

Returns:

Value of the output port.

Return type:

object

Raises:

AssertionError – If the output port is not a member of the node output ports.

Examples

>>> node = PortNode()
>>> port = node.add_output_port("output", 1)  
>>> node.get_output("output")
1
set_output(name: str, value: Any) None[source]#

Set the value of the output port with given name.

Parameters:
  • name (str) – Name of the output port.

  • value (Any) – Value of the output port

Raises:

AssertionError – If the output port is not a member of the node output ports.

Return type:

None

Examples

>>> node = PortNode()
>>> port = node.add_output_port("output")  
>>> port.value
>>> node.set_output("output", 1)
>>> port.value
1
connect(source_port: str, target_node: PortNode, target_port: str) None[source]#

Connect the given source port to given node target port.

The source port can be an input port but the target port must be an output port and conversely, if the source port is an output port, the target port must be an input port.

Parameters:
  • source_port (str) – Source port of the node to connect to the other node target port.

  • target_node (PortNode) – Target node that the target port is the member of.

  • target_port (str) – Target port from the target node to connect the source port to.

Return type:

None

Examples

>>> node_1 = PortNode()
>>> port = node_1.add_output_port("output")
>>> node_2 = PortNode()
>>> port = node_2.add_input_port("a")
>>> node_1.connect("output", node_2, "a")
>>> node_1.edges  
({}, {(<...Port object at 0x...>, <...Port object at 0x...>): None})
disconnect(source_port: str, target_node: PortNode, target_port: str) None[source]#

Disconnect the given source port from given node target port.

The source port can be an input port but the target port must be an output port and conversely, if the source port is an output port, the target port must be an input port.

Parameters:
  • source_port (str) – Source port of the node to disconnect from the other node target port.

  • target_node (PortNode) – Target node that the target port is the member of.

  • target_port (str) – Target port from the target node to disconnect the source port from.

Return type:

None

Examples

>>> node_1 = PortNode()
>>> port = node_1.add_output_port("output")
>>> node_2 = PortNode()
>>> port = node_2.add_input_port("a")
>>> node_1.connect("output", node_2, "a")
>>> node_1.edges  
({}, {(<...Port object at 0x...>, <...Port object at 0x...>): None})
>>> node_1.disconnect("output", node_2, "a")
>>> node_1.edges
({}, {})
process() None[source]#

Process the node, must be reimplemented by sub-classes.

This definition is responsible to set the dirty state of the node according to processing outcome.

Examples

>>> 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
>>> node = NodeAdd()
>>> node.set_input("a", 1)
>>> node.set_input("b", 1)
>>> node.process()
>>> node.get_output("output")
2
Return type:

None

to_graphviz() str[source]#

Return a string representation for visualisation of the node with Graphviz.

Returns:

String representation for visualisation of the node with Graphviz.

Return type:

str

Examples

>>> node_1 = PortNode("PortNode")
>>> port = node_1.add_input_port("a")
>>> port = node_1.add_input_port("b")
>>> port = node_1.add_output_port("output")
>>> node_1.to_graphviz()  
'PortNode (#...) | {{<a> a|<b> b} | {<output> output}}'