Table of contents > Color-coding inputs and outputs

Color-coding inputs and outputs

Input and output sockets are color-coded according to the type hint (or lack thereof) associated with them. Color-coding inputs and outputs is not required, but it can make the node layout more readable, since it helps convey which kind of data is expected to leave/arrive at a socket.

The color of an output socket is also used in the lines (connections) that come out of it, helping visualizing which kind of data is travelling inside the node layout.

No type is ever enforced, no type cast is ever performed by Nodezator. You retain the freedom to pass any value you want. The type hints and resulting color-coding serve only to indicate the expected type(s).

Examples

Here are a few examples below:

### function without type hints;
### in the default theme the sockets created are grey;
### the line (connection) coming out of the output socket
### uses the socket color as well;

def function_name(parameter):
    return str(parameter)

main_callable = function_name
Node generated from function
### the same function, but with type hints;
### sockets are color-coded according to the type hints;
### as always, the line (connection) coming out of the
### output socket shares its color;

def function_name(parameter: int) -> str:
    return str(parameter)

main_callable = function_name
Node generated from function

Variable-parameters may also have their variable arguments color-coded if you associated them with a type hint.

### color-coded input sockets on variable parameters

def sum_integers(*integers: int) -> int:
    return sum(integers)

main_callable = sum_integers

You can see the the resulting node below. Note that we added 02 widgets to node in order to make it create an input socket for each widget. As you can see, the input sockets are red (in the default colors). If we hadn't assigned the type hint to the integers parameter the input socket would not have a special color and would be grey instead (in the default colors).

Node generated from function

Types with dedicated colors

We won't discuss the colors per se, since they depend on customizations made by the users. However, it is useful to know which types have a dedicated color associated with them. You can find a list with all type hints that have dedicated colors in the Color coded annotations appendix.

Color coding a named output socket

As presented in a previous chapter, we used the return annotation in order to name the output of a function, like so:

### return annotation is used to name the output;

def function_name() -> [
      {'name': 'pi'},
    ]:
    return 3.14159

main_callable = function_name
Node generated from function

So, since the return annotation is already defined, how do we use it to color-code the output? It is actually pretty simple, cause the same format allows the type to be provided as well. You just need to add a new 'type' key to the dictionary. The value is the type associated with the output. Here's how it is done:

### return annotation is used to name and add type hint to output;

def function_name() -> [

      {'name': 'pi', 'type': float},

    ]:
    return 3.14159

main_callable = function_name
Node generated from function

Color coding multiple output sockets

To associate types for multiple outputs within a function we just need to do the same we did for the named output in the previous example: by adding a 'type' key to the dictionaries representing the output(s) we want to color-code.

### return annotation is used to name and add type hint
### to multiple outputs

def function_name() -> [

      {'name': 'first_output', 'type': str},
      {'name': 'second_output', 'type': bool},
      {'name': 'third_output', 'type': int},

    ]:

    return {
      'first_output': 'value',
      'second_output': True,
      'third_output': 100,
    }

main_callable = function_name
Node generated from function

Previous chapter | Table of contents | Next chapter