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)

Attributes

Methods

Examples

>>> class NodeAdd(PortNode):
...     def __init__(self, *args: Any, **kwargs: Any):
...         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.TreeNode class.

Parameters:
  • args – Arguments.

  • kwargs – Keywords arguments.

  • name (str | None)

  • description (str)

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

  • description (str)

Return type:

None

property input_ports: Dict[str, Port]#

Getter for the input ports of the node.

Returns:

Dictionary mapping port names to their corresponding input port instances.

Return type:

dict

property output_ports: Dict[str, Port]#

Getter for the output ports of the node.

Returns:

Mapping of output port names to their corresponding Port instances.

Return type:

dict

property dirty: bool#

Getter and setter for the node’s dirty state.

Parameters:

value – Value to set the node dirty state with.

Returns:

Whether the node is in a dirty state.

Return type:

bool

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

Getter for the edges of the node.

Retrieve the edges representing ports and their connections. Each edge corresponds to a port and one of its connections within the node structure.

Returns:

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

Return type:

tuple

property description: str#

Getter and setter 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 = '', port_type: Type[Port] = Port) Port[source]#

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

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

  • value (Any) – Value of the input port.

  • description (str) – 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 the specified name from the node.

Parameters:

name (str) – Name of the input port to remove.

Returns:

Removed 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 = '', port_type: Type[Port] = Port) Port[source]#

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

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

  • value (Any) – Value of the output port.

  • description (str) – 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 the specified name from the node.

Parameters:

name (str) – Name of the output port to remove.

Returns:

Removed 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 the specified 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 an input port with the specified name.

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

  • value (Any) – Value to assign to the input port.

Raises:

AssertionError – If the specified input port is not a member of the node’s 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) Any[source]#

Return the value of the output port with the specified 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 the specified name.

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

  • value (Any) – Value to assign to 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 specified source port to the specified target port of another node.

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 specified source port from the specified target node 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 for setting the dirty state of the node according to the processing outcome.

Examples

>>> class NodeAdd(PortNode):
...     def __init__(self, *args: Any, **kwargs: Any):
...         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]#

Generate a string representation for node visualisation 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}}'