forked from jchanvfx/NodeGraphQt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.py
229 lines (182 loc) · 6.53 KB
/
example.py
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
from Qt import QtCore, QtGui, QtWidgets
from NodeGraphQt import (NodeGraph,
BaseNode,
BackdropNode,
PropertiesBinWidget,
NodeTreeWidget,
setup_context_menu)
# import example nodes from the "example_nodes" package
from example_nodes import basic_nodes, widget_nodes
def draw_triangle_port(painter, rect, info):
"""
Custom paint function for drawing a Triangle shaped port.
Args:
painter (QtGui.QPainter): painter object.
rect (QtCore.QRectF): port rect used to describe parameters
needed to draw.
info (dict): information describing the ports current state.
{
'port_type': 'in',
'color': (0, 0, 0),
'border_color': (255, 255, 255),
'multi_connection': False,
'connected': False,
'hovered': False,
}
"""
painter.save()
size = int(rect.height() / 2)
triangle = QtGui.QPolygonF()
triangle.append(QtCore.QPointF(-size, size))
triangle.append(QtCore.QPointF(0.0, -size))
triangle.append(QtCore.QPointF(size, size))
transform = QtGui.QTransform()
transform.translate(rect.center().x(), rect.center().y())
port_poly = transform.map(triangle)
# mouse over port color.
if info['hovered']:
color = QtGui.QColor(14, 45, 59)
border_color = QtGui.QColor(136, 255, 35)
# port connected color.
elif info['connected']:
color = QtGui.QColor(195, 60, 60)
border_color = QtGui.QColor(200, 130, 70)
# default port color
else:
color = QtGui.QColor(*info['color'])
border_color = QtGui.QColor(*info['border_color'])
pen = QtGui.QPen(border_color, 1.8)
pen.setJoinStyle(QtCore.Qt.MiterJoin)
painter.setPen(pen)
painter.setBrush(color)
painter.drawPolygon(port_poly)
painter.restore()
def draw_square_port(painter, rect, info):
"""
Custom paint function for drawing a Square shaped port.
Args:
painter (QtGui.QPainter): painter object.
rect (QtCore.QRectF): port rect used to describe parameters
needed to draw.
info (dict): information describing the ports current state.
{
'port_type': 'in',
'color': (0, 0, 0),
'border_color': (255, 255, 255),
'multi_connection': False,
'connected': False,
'hovered': False,
}
"""
painter.save()
# mouse over port color.
if info['hovered']:
color = QtGui.QColor(14, 45, 59)
border_color = QtGui.QColor(136, 255, 35, 255)
# port connected color.
elif info['connected']:
color = QtGui.QColor(195, 60, 60)
border_color = QtGui.QColor(200, 130, 70)
# default port color
else:
color = QtGui.QColor(*info['color'])
border_color = QtGui.QColor(*info['border_color'])
pen = QtGui.QPen(border_color, 1.8)
pen.setJoinStyle(QtCore.Qt.MiterJoin)
painter.setPen(pen)
painter.setBrush(color)
painter.drawRect(rect)
painter.restore()
class MyNode(BaseNode):
"""
example test node.
"""
# set a unique node identifier.
__identifier__ = 'com.chantasticvfx'
# set the initial default node name.
NODE_NAME = 'my node'
def __init__(self):
super(MyNode, self).__init__()
self.set_color(25, 58, 51)
# create input and output port.
self.add_input('in port', color=(200, 10, 0))
self.add_output('default port')
self.add_output('square port', painter_func=draw_square_port)
self.add_output('triangle port', painter_func=draw_triangle_port)
if __name__ == '__main__':
QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
app = QtWidgets.QApplication([])
# create node graph.
graph = NodeGraph()
# set up default menu and commands.
setup_context_menu(graph)
# widget used for the node graph.
graph_widget = graph.widget
graph_widget.resize(1100, 800)
graph_widget.show()
# show the properties bin when a node is "double clicked" in the graph.
properties_bin = PropertiesBinWidget(node_graph=graph)
properties_bin.setWindowFlags(QtCore.Qt.Tool)
def show_prop_bin(node):
if not properties_bin.isVisible():
properties_bin.show()
graph.node_double_clicked.connect(show_prop_bin)
# show the nodes list when a node is "double clicked" in the graph.
node_tree = NodeTreeWidget(node_graph=graph)
def show_nodes_list(node):
if not node_tree.isVisible():
node_tree.update()
node_tree.show()
graph.node_double_clicked.connect(show_nodes_list)
# registered nodes.
nodes_to_reg = [
BackdropNode, MyNode,
basic_nodes.FooNode,
basic_nodes.BarNode,
widget_nodes.DropdownMenuNode,
widget_nodes.TextInputNode,
widget_nodes.CheckboxNode
]
graph.register_nodes(nodes_to_reg)
my_node = graph.create_node(
'com.chantasticvfx.MyNode',
name='chantastic!',
color='#0a1e20',
text_color='#feab20'
)
foo_node = graph.create_node(
'com.chantasticvfx.FooNode',
name='node')
foo_node.set_disabled(True)
# create example "TextInputNode".
text_node = graph.create_node(
'com.chantasticvfx.TextInputNode',
name='text node')
# create example "TextInputNode".
checkbox_node = graph.create_node(
'com.chantasticvfx.CheckboxNode',
name='checkbox node')
# create node with a combo box menu.
menu_node = graph.create_node(
'com.chantasticvfx.DropdownMenuNode',
name='menu node')
# change node icon.
this_path = os.path.dirname(os.path.abspath(__file__))
icon = os.path.join(this_path, 'example_nodes', 'pear.png')
bar_node = graph.create_node('com.chantasticvfx.BarNode')
bar_node.set_icon(icon)
bar_node.set_name('icon node')
# connect the nodes.
foo_node.set_output(0, bar_node.input(2))
menu_node.set_input(0, bar_node.output(1))
bar_node.set_input(0, text_node.output(0))
# auto layout nodes.
graph.auto_layout_nodes()
# wrap a backdrop node.
backdrop_node = graph.create_node('nodeGraphQt.nodes.BackdropNode')
backdrop_node.wrap_nodes([text_node, checkbox_node])
graph.fit_to_selection()
app.exec_()