forked from Golaem/Nodz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodz_demo.py
251 lines (177 loc) · 8.26 KB
/
nodz_demo.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
from __future__ import print_function
from __future__ import absolute_import
from Qt import QtCore, QtWidgets
from . import nodz_main
try:
app = QtWidgets.QApplication([])
except:
# I guess we're running somewhere that already has a QApp created
app = None
nodz = nodz_main.Nodz(None)
# nodz.loadConfig(filePath='')
nodz.initialize()
nodz.show()
######################################################################
# Test signals
######################################################################
# Nodes
@QtCore.Slot(str)
def on_nodeCreated(nodeName):
print('node created : ', nodeName)
@QtCore.Slot(str)
def on_nodeDeleted(nodeName):
print('node deleted : ', nodeName)
@QtCore.Slot(str, str)
def on_nodeEdited(nodeName, newName):
print('node edited : {0}, new name : {1}'.format(nodeName, newName))
@QtCore.Slot(str)
def on_nodeSelected(nodesName):
print('node selected : ', nodesName)
@QtCore.Slot(str, object)
def on_nodeMoved(nodeName, nodePos):
print('node {0} moved to {1}'.format(nodeName, nodePos))
@QtCore.Slot(str)
def on_nodeRightClick(nodeName):
print('right click on node : {0}'.format(nodeName))
@QtCore.Slot(str)
def on_nodeDoubleClick(nodeName):
print('double click on node : {0}'.format(nodeName))
# Attrs
@QtCore.Slot(str, int)
def on_attrCreated(nodeName, attrId):
print('attr created : {0} at index : {1}'.format(nodeName, attrId))
@QtCore.Slot(str, int)
def on_attrDeleted(nodeName, attrId):
print('attr Deleted : {0} at old index : {1}'.format(nodeName, attrId))
@QtCore.Slot(str, int, int)
def on_attrEdited(nodeName, oldId, newId):
print('attr Edited : {0} at old index : {1}, new index : {2}'.format(nodeName, oldId, newId))
# Connections
@QtCore.Slot(str, str, str, str)
def on_connected(srcNodeName, srcPlugName, destNodeName, dstSocketName):
print('connected src: "{0}" at "{1}" to dst: "{2}" at "{3}"'.format(srcNodeName, srcPlugName, destNodeName, dstSocketName))
@QtCore.Slot(str, str, str, str)
def on_disconnected(srcNodeName, srcPlugName, destNodeName, dstSocketName):
print('disconnected src: "{0}" at "{1}" from dst: "{2}" at "{3}"'.format(srcNodeName, srcPlugName, destNodeName, dstSocketName))
# Graph
@QtCore.Slot()
def on_graphSaved():
print('graph saved !')
@QtCore.Slot()
def on_graphLoaded():
print('graph loaded !')
@QtCore.Slot()
def on_graphCleared():
print('graph cleared !')
@QtCore.Slot()
def on_graphEvaluated():
print('graph evaluated !')
# Other
@QtCore.Slot(object)
def on_keyPressed(key):
print('key pressed : ', key)
if key==76: #l
nodz.autoLayoutGraph(nodz.selectedNodes)
nodz.signal_NodeCreated.connect(on_nodeCreated)
nodz.signal_NodeDeleted.connect(on_nodeDeleted)
nodz.signal_NodeEdited.connect(on_nodeEdited)
nodz.signal_NodeSelected.connect(on_nodeSelected)
nodz.signal_NodeMoved.connect(on_nodeMoved)
nodz.signal_NodeRightClicked.connect(on_nodeRightClick)
nodz.signal_NodeDoubleClicked.connect(on_nodeDoubleClick)
nodz.signal_AttrCreated.connect(on_attrCreated)
nodz.signal_AttrDeleted.connect(on_attrDeleted)
nodz.signal_AttrEdited.connect(on_attrEdited)
nodz.signal_PlugConnected.connect(on_connected)
nodz.signal_SocketConnected.connect(on_connected)
nodz.signal_PlugDisconnected.connect(on_disconnected)
nodz.signal_SocketDisconnected.connect(on_disconnected)
nodz.signal_GraphSaved.connect(on_graphSaved)
nodz.signal_GraphLoaded.connect(on_graphLoaded)
nodz.signal_GraphCleared.connect(on_graphCleared)
nodz.signal_GraphEvaluated.connect(on_graphEvaluated)
nodz.signal_KeyPressed.connect(on_keyPressed)
######################################################################
# Test Node creation UI
######################################################################
def demoNodeCreator(nodzInst, nodeName, pos):
if nodeName in nodeList:
id=0
uniqueNodeName = '{}_{}'.format(nodeName, id)
while uniqueNodeName in list(nodzInst.scene().nodes.keys()):
id+=1
uniqueNodeName = '{}_{}'.format(nodeName, id)
nodzInst.createNode(name=uniqueNodeName, position=pos)
else:
print("{} is node a recognized node type. Known types are: {}".format(nodeName, nodeList))
nodeList = ["NodeTypeA", "NodeTypeB", "NodeTypeC", "LongAndAnnoyingStringThatWillDisplayFarOfTheBounds"]
nodz.initNodeCreationHelper(nodeList, demoNodeCreator)
######################################################################
# Test API
######################################################################
# Node A
nodeA = nodz.createNode(name='nodeA', preset='node_preset_1', position=None)
nodz.createAttribute(node=nodeA, name='Aattr1', index=-1, preset='attr_preset_1',
plug=True, socket=False, dataType=str)
nodz.createAttribute(node=nodeA, name='Aattr2', index=-1, preset='attr_preset_1',
plug=False, socket=False, dataType=int)
nodz.createAttribute(node=nodeA, name='Aattr3', index=-1, preset='attr_preset_2',
plug=True, socket=True, dataType=int)
nodz.createAttribute(node=nodeA, name='Aattr4', index=-1, preset='attr_preset_2',
plug=True, socket=True, dataType=str)
nodz.createAttribute(node=nodeA, name='Aattr5', index=-1, preset='attr_preset_3',
plug=True, socket=True, dataType=int, plugMaxConnections=1, socketMaxConnections=-1)
nodz.createAttribute(node=nodeA, name='Aattr6', index=-1, preset='attr_preset_3',
plug=True, socket=True, dataType=int, plugMaxConnections=1, socketMaxConnections=-1)
# Node B
nodeB = nodz.createNode(name='nodeB', preset='node_preset_1')
nodz.createAttribute(node=nodeB, name='Battr1', index=-1, preset='attr_preset_1',
plug=True, socket=False, dataType=str)
nodz.createAttribute(node=nodeB, name='Battr2', index=-1, preset='attr_preset_1',
plug=True, socket=False, dataType=int)
nodz.createAttribute(node=nodeB, name='Battr3', index=-1, preset='attr_preset_2',
plug=True, socket=False, dataType=int)
nodz.createAttribute(node=nodeB, name='Battr4', index=-1, preset='attr_preset_3',
plug=True, socket=False, dataType=int, plugMaxConnections=1, socketMaxConnections=-1)
# Node C
nodeC = nodz.createNode(name='nodeC', preset='node_preset_1')
nodz.createAttribute(node=nodeC, name='Cattr1', index=-1, preset='attr_preset_1',
plug=False, socket=True, dataType=str)
nodz.createAttribute(node=nodeC, name='Cattr2', index=-1, preset='attr_preset_1',
plug=True, socket=False, dataType=int)
nodz.createAttribute(node=nodeC, name='Cattr3', index=-1, preset='attr_preset_1',
plug=True, socket=False, dataType=str)
nodz.createAttribute(node=nodeC, name='Cattr4', index=-1, preset='attr_preset_2',
plug=False, socket=True, dataType=str)
nodz.createAttribute(node=nodeC, name='Cattr5', index=-1, preset='attr_preset_2',
plug=False, socket=True, dataType=int)
nodz.createAttribute(node=nodeC, name='Cattr6', index=-1, preset='attr_preset_3',
plug=True, socket=False, dataType=str)
nodz.createAttribute(node=nodeC, name='Cattr7', index=-1, preset='attr_preset_3',
plug=True, socket=False, dataType=str)
nodz.createAttribute(node=nodeC, name='Cattr8', index=-1, preset='attr_preset_3',
plug=True, socket=False, dataType=int)
# Please note that this is a local test so once the graph is cleared
# and reloaded, all the local variables are not valid anymore, which
# means the following code to alter nodes won't work but saving/loading/
# clearing/evaluating will.
# Connection creation
nodz.createConnection('nodeB', 'Battr2', 'nodeA', 'Aattr3')
nodz.createConnection('nodeB', 'Battr1', 'nodeA', 'Aattr4')
# Attributes Edition
nodz.editAttribute(node=nodeC, index=0, newName=None, newIndex=-1)
nodz.editAttribute(node=nodeC, index=-1, newName='NewAttrName', newIndex=None)
# Attributes Deletion
nodz.deleteAttribute(node=nodeC, index=-1)
# Nodes Edition
nodz.editNode(node=nodeC, newName='newNodeName')
# Nodes Deletion
nodz.deleteNode(node=nodeC)
# Graph
print(nodz.evaluateGraph())
nodz.saveGraph(filePath='Enter your path')
nodz.clearGraph()
nodz.loadGraph(filePath='Enter your path')
if app:
# command line stand alone test... run our own event loop
app.exec_()