jaimervq / all_nodes

A high-level nodegraph implemented in PySide2, easy to expand and easy to debug
MIT License
31 stars 4 forks source link
gui gui-application node-based-ui nodegraph nodegraph-python pyqt pyside2 python python3 qt qt-gui qtdesigner visual-programming visual-programming-editor yaml


Tests License: MIT Formatting Closed pull requests
PyPI - Version PyPI - Downloads PyPI - Python Version

Features β€’ How to β€’ Execution logic


all_nodes is a visual programming node editor designed to be used in the most flexible way possible. It also aims to prevent re-duplication and re-writing of code inside of a project, as well as making debugging easier with its one-node-at-a-time approach to execution.

πŸ’½ Install and run

Install from PyPI with: pip install all-nodes

And then run with all-nodes

πŸ’‘ Features

βœ… Drag and drop creation of nodes
βœ… Class search
βœ… Visual feedback of execution βœ… Execution in separate thread
βœ… Preview of images and other elements βœ… Attribute editor
βœ… Deactivation of nodes βœ… Annotations

πŸ“– How to

Adding more classes/nodes

The places where all_nodes will search for node classes, scenes, etc is defined by the environment variable ALL_NODES_LIB_PATH

Folder structure example

Example of organization of a folder, that contains two libraries

Folder structure Classes representation Scenes

This folder has:

Defining a node

Inside each module, nodes/classes can be defined as easy as this:

class GetEnvVariable(GeneralLogicNode):

    NICE_NAME = "Get env variable"
    HELP = "Get the value of an environment variable, with possibility of a fallback" \
           "value if the variable is not defined"

    INPUTS_DICT = {
        "env_variable_name": {"type": str},
        "fallback_value": {"type": str, "optional": True},
    }

    OUTPUTS_DICT = {"env_variable_value": {"type": str}}

    def run(self):
        import os

        env_variable_name = self.get_attribute_value("env_variable_name")
        fallback_value = self.get_attribute_value("fallback_value")
        env_var_value = os.getenv(env_variable_name)
        if env_var_value is None:
            if fallback_value:
                env_var_value = fallback_value
            else:
                self.fail(
                    "No environment variable '{}' found".format(env_variable_name)
                )
                return

        self.set_output("env_variable_value", env_var_value)

Key elements that a node MUST have:

Key elements that a node CAN have:

Other considerations:

Defining a context

A context is a "node of nodes", or a node that contains a scene inside itself. To define it, just two ingredients are needed:

Once "inside" the context, its input attributes can be "grabbed" and its output attributes set by making use of the special ctx nodes:

Defining a scene

Defining a scene is very easy, it is a simple and readable yaml file.

To look for scenes, all_nodes will:

Example of scene:

# SCENE environ_to_yaml
# ---------------------
# Description: Simple scene to write the environ out to a yaml file

# Nodes section: overall list of nodes to be created
nodes:
- EnvironToYmlCtx_1:
    class_name: EnvironToYmlCtx
    x_pos: -904
    y_pos: -284
- StartFile_1:
    class_name: StartFile
    x_pos: -611
    y_pos: -304

# Connections section: connections to be done between nodes
connections:
- EnvironToYmlCtx_1.yaml_filepath -> StartFile_1.file_path

# Scene created at: 2023-01-24 23:13:10.371378
# Created by: jaime.rvq

The scene just needs two keys:

Note how the -> symbol is used to make the connections section more easily readable.

Batch execution

Any .yml scene file can be executed in batch mode without the need to open the graphic editor.

This can be done by launching the main file and making use of the -f flag.

Example: main.py -f D:/scene_examples/my_scene.yml

Also, when a scene belongs to a repo/folder that has been defined in the ALL_NODES_LIB_PATH variable, it can be found just by its "alias". This alias is just the file name, with no need for the complete path or the ,yml extension.

Example: main.py -f environ_to_yaml

Finally, we can use the -s argument to set all the attributes we want to change in the scene prior to running. This flag accepts as many arguments as we need. Here is an example setting one node's attribute prior to running:

Example: main.py -f environ_to_yaml -s OPEN_FILE.internal_bool true

▢️ Execution logic

In a scene, the execution starts from nodes that are recognized as "starting nodes". Those are nodes that:

In these example nodes, none of them have any incoming connections. Also, the only input attribute they all have is the START attribute, but since it is an optional attribute, the node can start executing even if it is not set.

Another example:

The node has a non-optional input. It is not connected, but a value has been defined to it, so the node can start executing.

Examples of execution order after the starting nodes

When a start node is executed, and it is successful, the nodes connected to its outputs will try to execute as well. Have a look at this example:

What will happen is:

However, changing the scene a little bit, makes things more complex:

Create and run node networks on the fly

For simple setups and tests, node networks can be created and run at the same time in a python script.

🐍 Example:

from all_nodes.logic.logic_scene import LogicScene

# Create a scene
logic_scene = LogicScene()

# Create a node and set one of its attributes
n_1 = logic_scene.add_node_by_name("GetEnvVariable")
n_1["env_variable_name"].set_value("USER")

# Create another node
n_2 = logic_scene.add_node_by_name("PrintToConsole")

# Connect the nodes
n_1["env_variable_value"].connect_to_other(n_2["in_object_0"])

# Run!
logic_scene.run_all_nodes()

πŸ“ˆ Analytics

Automatically generated once a month (For personal use, they are not gathered from any other user)