-
Notifications
You must be signed in to change notification settings - Fork 316
Expand file tree
/
Copy pathbasic_example.py
More file actions
209 lines (162 loc) · 7.21 KB
/
basic_example.py
File metadata and controls
209 lines (162 loc) · 7.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/usr/bin/python
# -*- coding: utf-8 -*-
import signal
from pathlib import Path
from Qt import QtCore, QtWidgets
# import example nodes from the "nodes" sub-package
from examples.nodes import (basic_nodes, custom_ports_node, group_node,
widget_nodes)
from NodeGraphQt import (NodeGraph, NodesPaletteWidget, NodesTreeWidget,
PropertiesBinWidget)
from NodeGraphQt.constants import LayoutDirectionEnum
BASE_PATH = Path(__file__).parent.resolve()
def main():
# handle SIGINT to make the app terminate on CTRL+C
signal.signal(signal.SIGINT, signal.SIG_DFL)
app = QtWidgets.QApplication([])
# create graph controller.
graph = NodeGraph()
# set up context menu for the node graph.
hotkey_path = Path(BASE_PATH, "hotkeys", "hotkeys.json")
graph.set_context_menu_from_file(hotkey_path, "graph")
# registered example nodes.
graph.register_nodes(
[
basic_nodes.BasicNodeA,
basic_nodes.BasicNodeB,
basic_nodes.CircleNode,
basic_nodes.SVGNode,
custom_ports_node.CustomPortsNode,
group_node.MyGroupNode,
widget_nodes.DropdownMenuNode,
widget_nodes.TextInputNode,
widget_nodes.SpinBoxNode,
widget_nodes.CheckboxNode,
]
)
# show the node graph widget.
graph_widget = graph.widget
graph_widget.resize(1100, 800)
graph_widget.setWindowTitle("NodeGraphQt Example")
graph_widget.show()
# create node with custom text color and disable it.
n_basic_a = graph.create_node("nodes.basic.BasicNodeA", text_color="#feab20")
n_basic_a.set_disabled(True)
# create node with vertial alignment
n_basic_a_vertical = graph.create_node(
"nodes.basic.BasicNodeA", name="Vertical Node", text_color="#feab20"
)
# adjust layout of node to be vertical
n_basic_a_vertical.set_layout_direction(1)
# create node and set a custom icon.
n_basic_b = graph.create_node("nodes.basic.BasicNodeB", name="custom icon")
n_basic_b.set_icon(str(Path(BASE_PATH, "img", "star.png")))
# create node with the custom port shapes.
n_custom_ports = graph.create_node(
"nodes.custom.ports.CustomPortsNode", name="custom ports"
)
# create node with the embedded QLineEdit widget.
n_text_input = graph.create_node(
"nodes.widget.TextInputNode", name="text node", color="#0a1e20"
)
# create node with the embedded QSpinBox widget.
n_spinbox_input = graph.create_node(
"nodes.widget.SpinBoxNode", name="spinbox node", color="#0a1e20"
)
# create node with the embedded QCheckBox widgets.
n_checkbox = graph.create_node("nodes.widget.CheckboxNode", name="checkbox node")
# create node with the QComboBox widget.
n_combo_menu = graph.create_node(
"nodes.widget.DropdownMenuNode", name="combobox node"
)
# create node with the circular design.
n_circle = graph.create_node("nodes.basic.CircleNode", name="circle node")
# create node with the circular design.
n_svg = graph.create_node("nodes.basic.SVGNode", name="svg node")
n_svg_file = graph.create_node("nodes.basic.SVGNode", name="svg file")
n_svg_file.set_svg(str(Path(BASE_PATH, "img", "cirlce-diamond.svg")))
n_svg_file_vertical = graph.create_node(
"nodes.basic.SVGNode", name="svg file", color="#0a1e20"
)
n_svg_file_vertical.set_svg(str(Path(BASE_PATH, "img", "cirlce-diamond.svg")))
# adjust layout of node to be vertical
n_svg_file_vertical.set_layout_direction(1)
# create group node.
n_group = graph.create_node("nodes.group.MyGroupNode")
# force-create the subgraph by expanding
n_group.expand()
# get subgraph
sub_graph = n_group.get_sub_graph()
# subgraph input & output ports
input_ports = sub_graph.get_input_port_nodes()
output_ports = sub_graph.get_output_port_nodes()
if sub_graph:
# create internal nodes
internal_node1 = sub_graph.create_node(
"nodes.basic.BasicNodeA", name="Inner 1", pos=(0, -30)
)
internal_node2 = sub_graph.create_node(
"nodes.basic.BasicNodeB", name="Inner 2", pos=(200, -30)
)
# connect internal nodes to group input/output ports and each other
input_ports[0].output(0).connect_to(internal_node1.input(0))
internal_node1.output(0).connect_to(internal_node2.input(0))
output_ports[0].input(0).connect_to(internal_node2.output(0))
# position input/output ports
input_ports[0].set_pos(-150, -15)
output_ports[0].set_pos(400, -15)
# collapse the group node back
n_group.collapse()
# make node connections.
# (connect nodes using the .set_output method)
n_text_input.set_output(0, n_custom_ports.input(0))
n_text_input.set_output(0, n_checkbox.input(0))
n_text_input.set_output(0, n_combo_menu.input(0))
# (connect nodes using the .set_input method)
n_group.set_input(0, n_custom_ports.output(1))
n_basic_b.set_input(2, n_checkbox.output(0))
n_basic_b.set_input(2, n_combo_menu.output(1))
# (connect nodes using the .connect_to method from the port object)
port = n_basic_a.input(0)
port.connect_to(n_basic_b.output(0))
# auto layout nodes.
graph.auto_layout_nodes()
# crate a backdrop node and wrap it around
# "custom port node" and "group node".
n_backdrop = graph.create_node("Backdrop")
n_backdrop.wrap_nodes([n_custom_ports, n_combo_menu])
# fit nodes to the viewer.
graph.clear_selection()
graph.fit_to_selection()
# adjust layout of node to be vertical (for all nodes).
# graph.set_layout_direction(LayoutDirectionEnum.VERTICAL.value)
# Custom builtin widgets from NodeGraphQt
# ---------------------------------------
# create a node properties bin widget.
properties_bin = PropertiesBinWidget(node_graph=graph, parent=graph_widget)
properties_bin.setWindowFlags(QtCore.Qt.Tool)
# example show the node properties bin widget when a node is double-clicked.
def display_properties_bin(node):
if not properties_bin.isVisible():
properties_bin.show()
# wire function to "node_double_clicked" signal.
graph.node_double_clicked.connect(display_properties_bin)
# create a nodes tree widget.
nodes_tree = NodesTreeWidget(node_graph=graph)
nodes_tree.set_category_label("nodeGraphQt.nodes", "Builtin Nodes")
nodes_tree.set_category_label("nodes.custom.ports", "Custom Port Nodes")
nodes_tree.set_category_label("nodes.widget", "Widget Nodes")
nodes_tree.set_category_label("nodes.basic", "Basic Nodes")
nodes_tree.set_category_label("nodes.group", "Group Nodes")
# nodes_tree.show()
# create a node palette widget.
nodes_palette = NodesPaletteWidget(node_graph=graph)
nodes_palette.set_category_label("nodeGraphQt.nodes", "Builtin Nodes")
nodes_palette.set_category_label("nodes.custom.ports", "Custom Port Nodes")
nodes_palette.set_category_label("nodes.widget", "Widget Nodes")
nodes_palette.set_category_label("nodes.basic", "Basic Nodes")
nodes_palette.set_category_label("nodes.group", "Group Nodes")
# nodes_palette.show()
app.exec_()
if __name__ == "__main__":
main()