Skip to content

Node editor

Nodes moved by their title and a wire connected from an output to an input of the same color

A node editor like the shader graph or Blender’s: examples/dnd/node_editor.py.

Terminal window
python examples/dnd/node_editor.py
  1. Nodes are draggables on a canvas, moved by their title (handle=True and a pz.drag_handle around the title). The canvas is a droppable that accepts "node", with a 10-pixel grid:

    title = pz.drag_handle(pz.container(ui.label(n["title"], weight="bold"), padding=(6, 10)))
    node = pz.draggable(body, key, type="node", handle=True, source="hide", lift=1.0, drop_animation=0)
    graph = pz.droppable(pz.stack([self.wires(), *nodes]), "graph", accepts="node", snap=10,
    on_drop=self.move_node)
  2. Outputs drag wires. An output port is a draggable with preview="wire", typed by what it carries:

    pz.draggable(self.dot(kind), f"out/{node}/{name}", data=(node, name), type=f"port:{kind}",
    preview="wire", wire_color=TYPE_COLORS[kind])
  3. Inputs take matching wires. An input accepts its own type, and can_drop forbids wires from the same node:

    pz.droppable(self.dot(kind), f"in/{node}/{name}", accepts=f"port:{kind}",
    can_drop=lambda e, n=node: e.data[0] != n, on_drop=self.connect)
  4. Links are data. connect stores the link (one per input) and wires() draws every link as a curve with pz.canvas, from the port positions.

Right-clicking an input unplugs it.