-
Notifications
You must be signed in to change notification settings - Fork 1
/
baseModule.py
433 lines (333 loc) · 16.6 KB
/
baseModule.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
from effigy.QNodeSceneNode import QNodeSceneNode, QNodeSceneNodeUndeletable
from effigy.NodeIO import NodeIO, NodeOutput, NodeInput, NodeIODirection, NodeIOMultiplicity
from PyQt5.QtWidgets import QGraphicsRectItem, QGraphicsTextItem
from PyQt5.QtCore import QRectF, QPointF, Qt
from PyQt5.QtGui import QPen, QColor, QBrush, QFont
from PyQt5.QtWidgets import QWidget, QListWidget, QListWidgetItem, QSpacerItem, QVBoxLayout, QSizePolicy
class execType:
"""typedef for exec-pins"""
pass
class ExecInput(NodeIO):
"""General Input class for Node links. See NodeIO class for more information."""
classDirection = NodeIODirection.input
classMultiplicity = NodeIOMultiplicity.multiple
class ExecOutput(NodeIO):
"""General Input class for Node links. See NodeIO class for more information."""
classDirection = NodeIODirection.output
classMultiplicity = NodeIOMultiplicity.single
class BaseImplementation():
"""Code associated with a node that will run on the worker"""
def __init__(self, nodeData, id, runtime):
self.nodeData = nodeData
self.id = id
self.runtime = runtime
self.ioIdFuncs = {} # Key: IO-ID, value: function
self.ioNameFuncs = {} # Key: IO-name, value: function
self.defineIO()
for ioname in self.nodeData["iomap"]:
if ioname in self.ioNameFuncs:
self.ioIdFuncs[self.nodeData["iomap"][ioname]] = self.ioNameFuncs[ioname]
else:
self.ioIdFuncs[self.nodeData["iomap"][ioname]] = None
self.init()
def init(self):
"""Gets called on creation by the runtime"""
pass
def __del__(self):
self.delete()
def delete(self):
"""Gets called on deletion by runtime"""
pass
def receiveNodedata(self, data):
pass
def defineIO(self):
"""Here you define the functions that get linked to IOs"""
raise NotImplementedError("This method must be implemented in derived class")
def getIOFunction(self, id):
"""Return the function associated with a node io"""
try:
return self.ioIdFuncs[id]
except KeyError:
return None
def registerFunc(self, name, function):
if name not in self.ioNameFuncs:
self.ioNameFuncs[name] = function
# TODO: Should throw exception on duplicate?
def getLinkedFunctions(self, name):
ioid = self.nodeData["iomap"][name]
links = self.nodeData["io"][str(ioid)] # TODO: Fix nodedata from string to int
functions = []
for link in links:
if link[0] == ioid:
functions.append(self.runtime.sheetObjects[self.runtime.currentSheet][link[3]].getIOFunction(link[1]))
elif link[1] == ioid:
functions.append(self.runtime.sheetObjects[self.runtime.currentSheet][link[2]].getIOFunction(link[0]))
return [x for x in functions if x is not None]
def getReturnOfFirstFunction(self, name):
funcs = self.getLinkedFunctions(name)
if funcs:
return funcs[0]()
else:
return None
def fireExec(self, name):
self.getReturnOfFirstFunction(name)
class PyreeNode(QNodeSceneNode):
"""Base node that implements some functionality specific to Pyree"""
def __init__(self, *args, **kwargs):
super(PyreeNode, self).__init__(*args, **kwargs)
self.sendMessageCallback = None
def sendDataToImplementations(self, msg):
"""Send a message to all implementations with the same id as this node"""
if self.sendMessageCallback is not None:
self.sendMessageCallback(self.id, msg)
def getPropertiesWidget(self):
"""A widget to be shown in the dockable 'properties' widget."""
return None
class SimpleBlackbox(PyreeNode):
"""Blackbox node template with inputs on the left and outputs on the right"""
# Here we define some information about the node
author = "DrLuke" # Author of this node (only used for namespacing, never visible to users)
modulename = "simplebb" # Internal name of the module, make this something distinguishable
name = "Simple Blackbox" # Human-readable name
placeable = False # Whether or not this node should be placeable from within the editor
Category = ["Builtin"] # Nested categories this Node should be sorted in
# Description, similar to python docstrings (Brief summary on first line followed by a long description)
description = """This node is the base class for all nodes.
It should never be placeable in the editor. However if you DO see this in the editor, something went wrong!"""
# Class implementing this node.
implementation = BaseImplementation
def defineIO(self):
raise NotImplementedError("This method must be implemented in derived class")
def addInput(self, type, name, displayname):
self.inputs.append([type, name, displayname])
def addOutput(self, type, name, displayname):
self.outputs.append([type, name, displayname])
def addIO(self):
"""Add/Update IO"""
self.inputs = []
self.outputs = []
self.defineIO()
indexOffset = 2
verticalIoSpacing = 20 # Vertical Space between 2 IOs
verticalIoStartOffset = (verticalIoSpacing * (max(len(self.inputs), len(self.outputs)) + indexOffset)) / 2
horizontalIoDistance = 10
maxInputWidth = 0
maxOutputWidth = 0
nodeTitleWidth = self.nodeTitle.boundingRect().width()
titlePadding = 10
horizontalTextToIoDistance = 5
for ioDefinition in self.inputs:
if ioDefinition[0] is not None:
if ioDefinition[0] is execType:
ioDefinition.append(ExecInput(ioDefinition[0], parent=self, name=ioDefinition[1], displaystr=ioDefinition[2]))
else:
ioDefinition.append(NodeInput(ioDefinition[0], parent=self, name=ioDefinition[1], displaystr=ioDefinition[2]))
ioDefinition.append(QGraphicsTextItem(ioDefinition[2], ioDefinition[3]))
maxInputWidth = max(maxInputWidth, ioDefinition[4].boundingRect().width())
ioDefinition[4].setPos(QPointF(horizontalTextToIoDistance, - ioDefinition[3].boundingRect().height() / 2 - ioDefinition[4].boundingRect().height() / 4))
self.IO[ioDefinition[1]] = ioDefinition[3]
for ioDefinition in self.outputs:
if ioDefinition[0] is not None:
if ioDefinition[0] is execType:
ioDefinition.append(ExecOutput(ioDefinition[0], parent=self, name=ioDefinition[1], displaystr=ioDefinition[2]))
else:
ioDefinition.append(NodeOutput(ioDefinition[0], parent=self, name=ioDefinition[1], displaystr=ioDefinition[2]))
ioDefinition.append(QGraphicsTextItem(ioDefinition[2], ioDefinition[3]))
maxOutputWidth = max(maxInputWidth, ioDefinition[4].boundingRect().width())
ioDefinition[4].setPos(QPointF(- horizontalTextToIoDistance - ioDefinition[4].boundingRect().width(), - ioDefinition[3].boundingRect().height() / 2 - ioDefinition[4].boundingRect().height() / 4))
self.IO[ioDefinition[1]] = ioDefinition[3]
width = max(maxInputWidth + maxOutputWidth + horizontalIoDistance, nodeTitleWidth + titlePadding)
height = verticalIoSpacing * (max(len(self.inputs), len(self.outputs)) + indexOffset - 1)
self.mainRect.setRect(QRectF(-width/2, -height/2, width, height))
for ioDefinition in self.inputs:
if ioDefinition[0] is not None:
ioDefinition[3].setPos(QPointF(-width/2, - verticalIoStartOffset + (self.inputs.index(ioDefinition) + indexOffset) * verticalIoSpacing))
for ioDefinition in self.outputs:
if ioDefinition[0] is not None:
ioDefinition[3].setPos(QPointF(width / 2, - verticalIoStartOffset + (self.outputs.index(ioDefinition) + indexOffset) * verticalIoSpacing))
self.nodeTitle.setPos(QPointF(- nodeTitleWidth / 2, -height / 2))
def boundingRect(self):
return self.mainRect.rect()
def addGraphicsItems(self):
self.mainRect = QGraphicsRectItem(QRectF(-15, -15, 30, 30), self)
self.nodeTitle = QGraphicsTextItem(type(self).name, self)
titleFont = QFont()
titleFont.setBold(True)
self.nodeTitle.setFont(titleFont)
self.selectedChanged(self.isSelected())
def selectedChanged(self, state):
if state:
self.mainRect.setPen(QPen(Qt.red))
else:
self.mainRect.setPen(QPen(Qt.black))
def serialize(self):
return None
def deserialize(self, data):
pass
class SingleExecoutImplementation(BaseImplementation):
def defineIO(self):
self.registerFunc("execOut", self.fireExecOut)
def fireExecOut(self, *args, **kwargs):
linkedFuncs = self.getLinkedFunctions("execOut")
for func in linkedFuncs:
if func is not None:
func()
class InitNode(SimpleBlackbox, QNodeSceneNodeUndeletable):
"""Blackbox node template with inputs on the left and outputs on the right"""
# Here we define some information about the node
author = "DrLuke" # Author of this node (only used for namespacing, never visible to users)
modulename = "sheetinit" # Internal name of the module, make this something distinguishable
name = "Init" # Human-readable name
placeable = False # Whether or not this node should be placeable from within the editor
Category = ["Builtin"] # Nested categories this Node should be sorted in
# Description, similar to python docstrings (Brief summary on first line followed by a long description)
description = """Node that gets executed once on sheet initialization."""
# Class implementing this node.
implementation = SingleExecoutImplementation
def defineIO(self):
self.addOutput(execType, "execOut", "Init")
class LoopNode(InitNode):
modulename = "sheetloop"
name = "Loop"
def defineIO(self):
self.addOutput(execType, "execOut", "Loop")
description = """Starting point for looping through sheet every frame"""
class SubSheetImplementation(BaseImplementation):
def defineIO(self):
self.registerFunc("execInit", self.execInit)
self.registerFunc("execLoop", self.execLoop)
def receiveNodedata(self, data):
if "subsheetid" in data:
if not self.currentSheet == data["subsheetid"]:
self.currentSheet = data["subsheetid"]
self.updateSubsheetRuntime()
def __init__(self, *args, **kwargs):
self.sheetObjects = {}
self.sheetInitId = None
self.sheetLoopId = None
self.currentSheet = None
super(SubSheetImplementation, self).__init__(*args, **kwargs)
self.updateRuntime()
def updateSubsheetRuntime(self):
if self.currentSheet is not None and self.currentSheet in self.runtime.sheetdata:
self.availableModules = self.runtime.availableModules
sheetId = self.currentSheet
if sheetId not in self.sheetObjects:
self.sheetObjects[sheetId] = {}
for nodeId in self.runtime.sheetdata[sheetId]:
if nodeId not in self.sheetObjects[sheetId]: # Create new object from implementation class
self.sheetObjects[sheetId][nodeId] = \
self.availableModules[self.runtime.sheetdata[sheetId][nodeId]["modulename"]].implementation(
self.runtime.sheetdata[sheetId][nodeId],
nodeId,
self)
else: # Update nodeData in implementations
self.sheetObjects[sheetId][nodeId].nodeData = self.runtime.sheetdata[sheetId][nodeId]
for sheetId in self.sheetObjects:
dellist = []
for nodeId in self.sheetObjects[sheetId]:
nodeExists = False
for sheetId in self.runtime.sheetdata:
if nodeId in self.runtime.sheetdata[sheetId]:
nodeExists = True
if not nodeExists:
dellist.append(nodeId)
for delid in dellist:
del self.sheetObjects[sheetId][delid]
if self.currentSheet:
self.sheetInitId = None
self.sheetLoopId = None
for nodeId in self.runtime.sheetdata[self.currentSheet]:
if self.runtime.sheetdata[self.currentSheet][nodeId]["modulename"] == "sheetinit":
self.sheetInitId = nodeId
if self.runtime.sheetdata[self.currentSheet][nodeId]["modulename"] == "sheetloop":
self.sheetLoopId = nodeId
def passNodeData(self, nodeid, data):
for sheetId in self.sheetObjects:
for nodeId in self.sheetObjects[sheetId]:
if nodeId == nodeid:
object = self.sheetObjects[sheetId][nodeId]
object.receiveNodedata(data)
def execInit(self):
self.updateRuntime()
if self.sheetInitId is not None and self.currentSheet is not None:
self.sheetObjects[self.currentSheet][self.sheetInitId].fireExecOut()
self.fireExec("ExecInitOut")
def execLoop(self):
self.updateRuntime()
if self.sheetLoopId is not None and self.currentSheet is not None:
self.sheetObjects[self.currentSheet][self.sheetLoopId].fireExecOut()
self.fireExec("ExecLoopOut")
def updateRuntime(self):
self.width = self.runtime.width
self.height = self.runtime.height
self.state = self.runtime.state
self.time = self.runtime.time
self.deltatime = self.runtime.deltatime
self.fbo = self.runtime.fbo
self.fbotexture = self.runtime.fbotexture
self.monitorname = self.runtime.monitorname
class SubSheet(SimpleBlackbox):
author = "DrLuke"
name = "Subsheet"
modulename = "subsheet"
Category = ["Builtin"]
placeable = True
implementation = SubSheetImplementation
def __init__(self, *args, **kwargs):
self.ownsheet = None
self.sheets = None
self.selectedSheet = None
self.listSheetItems = {}
super(SubSheet, self).__init__(*args, **kwargs)
self.propertiesWidget = QWidget()
self.vlayout = QVBoxLayout()
self.listWidget = QListWidget()
self.listWidget.itemClicked.connect(self.listClicked)
self.vlayout.addWidget(self.listWidget)
self.vlayout.addItem(QSpacerItem(40, 20, QSizePolicy.Minimum, QSizePolicy.Expanding))
self.propertiesWidget.setLayout(self.vlayout)
def getPropertiesWidget(self):
return self.propertiesWidget
def updateSheets(self):
if self.sheets is not None and self.ownsheet is not None:
self.listSheetItems = {}
self.listWidget.clear()
for sheetId in self.sheets:
if not sheetId == self.ownsheet:
newItem = QListWidgetItem(self.sheets[sheetId])
newItem.setToolTip(str(sheetId))
newItem.setData(Qt.UserRole, sheetId)
self.listSheetItems[sheetId] = newItem
self.listWidget.addItem(newItem)
if sheetId == self.selectedSheet:
boldFont = QFont()
boldFont.setBold(True)
newItem.setFont(boldFont)
def listClicked(self, item):
normalFont = QFont()
boldFont = QFont()
boldFont.setBold(True)
for i in range(self.listWidget.count()):
itemnormal = self.listWidget.item(i)
itemnormal.setFont(normalFont)
self.selectedSheet = item.data(Qt.UserRole)
self.sendDataToImplementations({"subsheetid": self.selectedSheet})
item.setFont(boldFont)
def serialize(self):
return {"subsheetid": self.selectedSheet}
def deserialize(self, data):
if data is not None:
if "subsheetid" in data:
self.selectedSheet = data["subsheetid"]
self.sendDataToImplementations({"subsheetid": self.selectedSheet})
def selectedChanged(self, state):
if state:
self.mainRect.setPen(QPen(Qt.red))
else:
self.mainRect.setPen(QPen(Qt.blue))
def defineIO(self):
self.addInput(execType, "execInit", "Execute Init")
self.addInput(execType, "execLoop", "Execute Loop")
self.addOutput(execType, "ExecInitOut", "Init Done")
self.addOutput(execType, "ExecLoopOut", "Loop Done")