def testStringCache(): value1 = "!1+" value2 = "+2+" value3 = "+3!" add = MT.createNode("Math.Add") valuenode = MT.createNode("Values.String Value") valuenode2 = MT.createNode("Values.String Value") valuenode.insockets[0].value = value1 valuenode2.insockets[0].value = value2 print("setting the input of valuenode to %s" % value1) MT.project.root.addNode(add) MT.project.root.addNode(valuenode) MT.project.root.addNode(valuenode2) pos = valuenode.pos valuenode.pos = (pos[0] - 150, pos[1] - 50) valuenode2.pos = (pos[0] - 150, pos[1] + 50) add.insockets[0].connected = valuenode.outsockets[0] add.insockets[1].connected = valuenode2.outsockets[0] add.insockets[2].value = value3 cache = MT.cache.DataCache(add.outsockets[0]) print("resulting value: %s" % cache.getOutput()) print("expected value: %s" % value1 + value2 + value3) return (value1 + value2 + value3) == cache.getOutput()
def __init__(self, node, raw=False): super().__init__(node, raw) if raw: return data = MT.createNode(self.datanode) obj = MT.createNode("Objects.Object") node.graph.addNode(data) node.graph.addNode(obj) obj.insockets[0].connected = node.graph[0].outsockets[-1] obj.insockets[1].connected = node.graph[0].outsockets[-1] obj.insockets[2].connected = data.outsockets[0] obj.insockets[3].connected = node.graph[0].outsockets[-1] for s in data.insockets: default = s.value s.connected = node.graph[0].outsockets[-1] node.insockets[-1].value = default node.graph[1].insockets[0].connected = obj.outsockets[0] node.graph[2].pos = (-70, 80) node.graph[3].pos = (70, 80) node.graph[1].pos = (70, 160)
def dropEvent(self, event): nodeLabel = str(event.mimeData().text()) event.mimeData().setText("") self.drop = False if nodeLabel == "": return _node = MT.createNode(nodeLabel) if _node is not None: self.scene().space.addNode(_node) scenePos = self.mapToScene(event.pos()) nw = NodeDesigner.width nh = NodeDesigner.height _node.pos = (scenePos.x() - nw / 2, scenePos.y() - nh / 2) #link it #find sockets this corresponds to insocket = None for socket in self.start.data.insockets: if (socket.connected is not None and socket.connected.node.ptr == self.end.data.ptr): insocket = socket break out = insocket.connected #find new matching insocket and link insockets = MT.getCompatibleSockets(out, node_) if len(insockets) == 1: insockets[0].connected = out else: menu = QMenu() for socket in insockets: action = menu.addAction(socket.name) def connect(): socket.connected = out action.triggered.connect(connect) menu.exec_(event.screenPos()) outsockets = list( filter(lambda o: MT.isCompatible(insocket, o), _node.outsockets)) if len(outsockets) == 1: insocket.connected = outsockets[0] else: menu = QMenu() for socket in outsockets: action = menu.addAction(socket.name) def connect(): insocket.connected = socket action.triggered.connect(connect) menu.exec_(event.screenPos())
def _decrypt(self, buf, seed): rseed = (seed << 7) ^ 0xA9C36DE1 MT.sgenrand(rseed) buf = bytearray(buf) for i in range(len(buf)): buf[i] ^= MT.genrand() & 0xFF return bytes(buf)
def createContainer(self): items = self.scene().selectedItems() if len(items) == 0: return nodes = [n.data for n in items] MT.createContainer(nodes)
def testSimulation(): simNode = MT.createNode("General.Simulation") add = MT.createNode("Math.Add") startValue = MT.createNode("Values.Int Value") MT.project.root.addNode(simNode) MT.project.root.addNode(startValue) simNode.graph.addNode(add) innode = simNode.graph[1] outnode = simNode.graph[2] test = TestCase() test.equal(len(simNode.graph[2].insockets), 0, "Amount of outputs before connection") add.insockets[0].connected = innode.outsockets[0] test.equal(len(simNode.graph[2].insockets), 1, "Amount of outputs after connection") simNode.insockets[1].connected = startValue.outsockets[0] add.insockets[1].value = 1 startValue.insockets[0].value = 0 outnode.insockets[0].connected = add.outsockets[0] test.equal(simNode.insockets[1].type, "INTEGER") test.equal(innode.outsockets[0].type, "INTEGER") test.equal(add.insockets[0].type, "INTEGER") test.equal(add.outsockets[0].type, "INTEGER") test.equal(outnode.insockets[0].type, "INTEGER", "outnode") test.equal(simNode.outsockets[0].type, "INTEGER", "simnode output") simNode.insockets[0].value = 1 cache = MT.cache.DataCache(simNode.outsockets[0]) test.equal(cache.getOutput(), 1, "Simulation Step: 1") simNode.insockets[0].value = 2 MT.cache.DataCache.invalidate(simNode) cache = MT.cache.DataCache(simNode.outsockets[0]) test.equal(cache.getOutput(), 2, "Simulation Step: 2") simNode.insockets[0].value = 3 MT.cache.DataCache.invalidate(simNode) cache = MT.cache.DataCache(simNode.outsockets[0]) test.equal(cache.getOutput(), 3, "Simulation Step: 3") simNode.insockets[0].value = 4 MT.cache.DataCache.invalidate(simNode) cache = MT.cache.DataCache(simNode.outsockets[0]) test.equal(cache.getOutput(), 4, "Simulation Step: 4") simNode.insockets[0].value = 5 MT.cache.DataCache.invalidate(simNode) cache = MT.cache.DataCache(simNode.outsockets[0]) test.equal(cache.getOutput(), 5, "Simulation Step: 5") return test.exit()
def dropEvent(self, event): nodeLabel = str(event.mimeData().text()) event.mimeData().setText("") self.drop = False if nodeLabel == "": return _node = MT.createNode(nodeLabel) if _node is not None: self.scene().space.addNode(_node) scenePos = self.mapToScene(event.pos()) nw = NodeDesigner.width nh = NodeDesigner.height _node.pos = (scenePos.x() - nw/2, scenePos.y() - nh/2) #link it #find sockets this corresponds to insocket = None for socket in self.start.data.insockets: if (socket.connected is not None and socket.connected.node.ptr == self.end.data.ptr): insocket = socket break out = insocket.connected #find new matching insocket and link insockets = MT.getCompatibleSockets(out, node_) if len(insockets) == 1: insockets[0].connected = out else: menu = QMenu() for socket in insockets: action = menu.addAction(socket.name) def connect(): socket.connected = out action.triggered.connect(connect) menu.exec_(event.screenPos()) outsockets = list(filter(lambda o: MT.isCompatible(insocket, o), _node.outsockets)) if len(outsockets) == 1: insocket.connected = outsockets[0] else: menu = QMenu() for socket in outsockets: action = menu.addAction(socket.name) def connect(): insocket.connected = socket action.triggered.connect(connect) menu.exec_(event.screenPos())
def testInheritSocketTypeMath(): add = MT.createNode("Math.Add") flnode = MT.createNode("Values.Float Value") add.insockets[0].connected = flnode.outsockets[0] test = TestCase() test.equal(add.insockets[0].type, flnode.outsockets[0].type, "connected") test.equal(add.insockets[0].type, add.outsockets[0].type, "in to out") return test.exit()
def testNewProject(): node = MT.createNode("Math.Add") node1 = MT.createNode("Values.Float Value") node2 = MT.createNode("Values.Int Value") MT.project.root.addNode(node) MT.project.root.addNode(node1) MT.project.root.addNode(node2) MT.newProject() return len(MT.project.root) == 0
def testSwitchNode(): value1 = MT.createNode("Values.Float Value") value2 = MT.createNode("Values.Float Value") value3 = MT.createNode("Values.Float Value") value1.insockets[0].value = 1 value2.insockets[0].value = 3 value3.insockets[0].value = 6 switch = MT.createNode("General.Switch") MT.project.root.addNode(value1) MT.project.root.addNode(value2) MT.project.root.addNode(value3) MT.project.root.addNode(switch) # find array converter ... this is still kinda ugly array = None for child in switch.insockets[1].childNodes: if child.name == "ArrayNode": array = child break print(array.type) print(dir(array)) array.insockets[0].connected = value1.outsockets[0] array.insockets[1].connected = value2.outsockets[0] array.insockets[2].connected = value3.outsockets[0] switch.insockets[1].connected = array.outsockets[0] test = TestCase() print("Test1:") switch.insockets[0].value = 0 cache = MT.cache.DataCache(switch.outsockets[0]) test.equal(cache.getOutput(), value1.insockets[0].value) print("Test2:") switch.insockets[0].value = 1 MT.cache.DataCache.invalidate(switch) cache = MT.cache.DataCache(switch.outsockets[0]) test.equal(cache.getOutput(), value2.insockets[0].value) print("Test3:") switch.insockets[0].value = 2 MT.cache.DataCache.invalidate(switch) cache = MT.cache.DataCache(switch.outsockets[0]) test.equal(cache.getOutput(), value3.insockets[0].value) return test.exit()
def testSwitchNode(): value1 = MT.createNode("Values.Float Value") value2 = MT.createNode("Values.Float Value") value3 = MT.createNode("Values.Float Value") value1.insockets[0].value = 1 value2.insockets[0].value = 3 value3.insockets[0].value = 6 switch = MT.createNode("General.Switch") MT.project.root.addNode(value1) MT.project.root.addNode(value2) MT.project.root.addNode(value3) MT.project.root.addNode(switch) #find array converter ... this is still kinda ugly array = None for child in switch.insockets[1].childNodes: if child.name == "ArrayNode": array = child break print(array.type) print(dir(array)) array.insockets[0].connected = value1.outsockets[0] array.insockets[1].connected = value2.outsockets[0] array.insockets[2].connected = value3.outsockets[0] switch.insockets[1].connected = array.outsockets[0] test = TestCase() print("Test1:") switch.insockets[0].value = 0 cache = MT.cache.DataCache(switch.outsockets[0]) test.equal(cache.getOutput(), value1.insockets[0].value) print("Test2:") switch.insockets[0].value = 1 MT.cache.DataCache.invalidate(switch) cache = MT.cache.DataCache(switch.outsockets[0]) test.equal(cache.getOutput(), value2.insockets[0].value) print("Test3:") switch.insockets[0].value = 2 MT.cache.DataCache.invalidate(switch) cache = MT.cache.DataCache(switch.outsockets[0]) test.equal(cache.getOutput(), value3.insockets[0].value) return test.exit()
def testConnectSockets(): add = MT.createNode("Math.Add") add2 = MT.createNode("Math.Add") MT.project.root.addNode(add) MT.project.root.addNode(add2) add.insockets[0].connected = add2.outsockets[0] test = TestCase() test.equal(add.insockets[0].connected.ptr, add2.outsockets[0].ptr) return test.exit();
def __init__(self, data, parent=None, options=True, width=NodeDesigner.width, height=NodeDesigner.height): QGraphicsSvgItem.__init__(self, parent) self.data = data self._width = width self.height = height self.viewed = False self._viewSockets = False self._visibleSockets = [] self.setFlag(QGraphicsItem.ItemIsMovable, True) self.setFlag(QGraphicsItem.ItemIsFocusable, True) self.setFlag(QGraphicsItem.ItemIsSelectable, True) self.setFlag(QGraphicsItem.ItemSendsGeometryChanges, True) self.setAcceptHoverEvents(True) self.setAcceptDrops(True) self.nameBG = NodeNameBG(self) self.name = NodeName(self) self.nameBG._width = self.name.boundingRect().width() self.nameBG.height = self.name.boundingRect().height() self.out = [] pos = width colors = QColor() altcolor = False for out in self.data.outsockets: vis = NodeOutSocket(self, out, altcolor) altcolor = not altcolor vis.setPos(QPoint(pos, 0)) pos += vis._width + 2 #slight margin self.out.append(vis) self.addsocketcb = MT.attachToBoundSignal(data, "outSocketAdded", self.newOutSocket) self.updateToolTipCB = MT.attachToSignal("CACHE_FINISHED", self.updateToolTip) if options: self.nodeOptions = NodeOptionsButton(self) self.posCB = MT.attachToBoundSignal(data, "nodePositionChanged", self.updatePositionFromData)
def testForLoopCache(): test = TestCase() loop = MT.createNode("General.For") MT.project.root.addNode(loop) loop.insockets[0].value = 0 loop.insockets[1].value = 10 loop.insockets[2].value = 1 add = MT.createNode("Math.Add") loop.graph.addNode(add) test.equal(len(loop.graph[2].insockets), 0, "outsockets of loop container before (direct) connection") test.equal(len(loop.graph[1].outsockets), 1, "looped insockets node has too many sockets") loopsocket = loop.graph[1].outsockets[0] add.insockets[0].connected = loopsocket test.equal(len(loop.graph[2].insockets), 1, "outsockets of loop container before (indirect) connection") add.insockets[1].value = 1 loop.graph[2].insockets[0].connected = add.outsockets[0] loop.insockets[3].value = 1 test.equal(add.outsockets[0].type, "INTEGER", "output of Add") test.equal(add.insockets[0].type, "INTEGER", "1st input of Add") test.equal(add.insockets[1].type, "INTEGER", "2nd input of Add") test.equal(loop.outsockets[0].type, "INTEGER", "output of ForNode") test.equal(loop.insockets[3].type, "INTEGER", "input of startvalue into ForLoop") test.equal(loop.graph[1].outsockets[0].type, "INTEGER", "input of ForLoop Container") test.equal(loop.graph[2].insockets[0].type, "INTEGER", "output of ForLoop Container") test.equal(len(loop.graph), 4, "nodes inside loop container") test.equal(len(loop.graph[0].outsockets), 4, "static loop inputs") test.equal(len(loop.graph[1].outsockets), 2, "looped inputs") if not test.equal(len(loop.graph[2].insockets), 1, "loop outputs"): print("the sockets on this node: {}".format( [s.name for s in loop.graph[2].insockets])) test.equal(len(loop.outsockets), 1, "number of outsockes on forloop node") test.equal(len(loop.insockets), 4, "number of insockets on forloop node") cache = MT.cache.DataCache(loop.outsockets[0]) expected_result = 11 test.equal(cache.getOutput(), expected_result, "Loop Result") return test.exit()
def testSaveProject(): node = MT.createNode("Math.Add") node1 = MT.createNode("Values.Float Value") node2 = MT.createNode("Values.Int Value") MT.project.root.addNode(node) MT.project.root.addNode(node1) MT.project.root.addNode(node2) now = datetime.datetime.now() MT.project.filename = "projectTest" + now.strftime("%d%m%y%H%M%S") + ".mt" MT.project.save() return os.path.exists(MT.project.filename)
def __init__(self, data, parent=None, options=True, width=NodeDesigner.width, height=NodeDesigner.height): QGraphicsSvgItem.__init__(self, parent) self.data = data self._width = width self.height = height self.viewed = False self._viewSockets = False self._visibleSockets = [] self.setFlag(QGraphicsItem.ItemIsMovable, True) self.setFlag(QGraphicsItem.ItemIsFocusable, True) self.setFlag(QGraphicsItem.ItemIsSelectable, True) self.setFlag(QGraphicsItem.ItemSendsGeometryChanges, True) self.setAcceptHoverEvents(True) self.setAcceptsHoverEvents(True) self.setAcceptDrops(True) self.nameBG = NodeNameBG(self) self.name = NodeName(self) self.nameBG._width = self.name.boundingRect().width() self.nameBG.height = self.name.boundingRect().height() self.out = [] pos = width; colors = QColor() altcolor = False for out in self.data.outsockets: vis = NodeOutSocket(self, out, altcolor) altcolor = not altcolor vis.setPos(QPoint(pos, 0)) pos += vis._width + 2 #slight margin self.out.append(vis) self.addsocketcb = MT.attachToBoundSignal(data, "outSocketAdded", self.newOutSocket) if options: self.nodeOptions = NodeOptionsButton(self) self.posCB = MT.attachToBoundSignal(data, "nodePositionChanged", self.updatePositionFromData)
def locAcquire( Naves=1, Nds=1, ): numAttempts = 1 while 1: numAttempts += 1 try: if 0: while acq.glbSOCKET_PUBSUB.poll(50): while acq.glbSOCKET_PUBSUB.poll(50): print("Clearing old data...") acq.checkForPublished() sleep(0.2) _, out, dt = acq.acquireStreaming(Nmin=Naves, bNoStale=True) #Raw() #out=MT.smoothalong(out,Nds, axis=-1)[...,::Nds] out = MT.downsample_npts(out, Nds, axis=-1) #outL.append(out) #out=np.mean(out, axis=0) except DAQError: print("trigger stopped, try again") else: break t = np.arange(out.shape[-1]) * dt * Nds return t, out
def testTimeline(): timelinenode = MT.createNode("Values.Frame") floatnode = MT.createNode("Values.Float Value") floatnode.insockets[0].connected = timelinenode.outsockets[0] cache = MT.cache.DataCache(floatnode.outsockets[0]) print("initial frame value as {}: {}".format(cache.type, cache.getOutput())) MT.timeline.setFrame(44) MT.cache.DataCache.invalidate(timelinenode) cache = MT.cache.DataCache(floatnode.outsockets[0]) print("new cached frame value as %s: %d" % (cache.type, cache.getOutput())) print("new frame value: %d" % (MT.timeline.frame())) return int(cache.getOutput()) == 44
def generateTriggerWaveforms(pulseTimingParams, pulseSeqDesc, Npts, sampleRate): N = len(pulseSeqDesc) pt = pulseTimingParams pmpPulseWidth = pt.pumpTime tDeltas = [ pt.tShort if seq[1].startswith('s') else pt.tLong for seq in pulseSeqDesc ] #+ [pt.depolLength] allPumpTimes = np.cumsum([0] + tDeltas[:-1]) #auxPumpTimes=np.arange(N)*(pt.tShort+pt.tLong) #mainPumpTimes=np.arange(N)*(pt.tShort+pt.tLong) + pt.tShort #if pt.tShort>0: # allPumpTimes=np.vstack([auxPumpTimes, mainPumpTimes]).T.ravel() #interleaved #else: # allPumpTimes=mainPumpTimes allPumpTimes += 1.0 / sampleRate #pmpPulseWidth=20*1e-6 #sampleRate=1e6 #t,pumpTrigWvfm=makePulseTrain(allPumpTimes+pmpPulseWidth/2, pmpPulseWidth, 1., Nsamples=Npts, sampleRate=sampleRate) #initPulseWidth=pmpPulseWidth #pmpPulseWidth=20*1e-6 #sampleRate=1e6 t, pumpTrigWvfm = makePulseTrain(allPumpTimes, pmpPulseWidth, 1., Nsamples=Npts, sampleRate=sampleRate) initPulseWidth = pmpPulseWidth initTrigWvfm = MT.tophat(t, initPulseWidth, initPulseWidth / 2 + 1. / sampleRate) return t, pumpTrigWvfm, initTrigWvfm
def testInheritSocketTypeContainer(): test = TestCase() container = MT.createNode("General.Container") floatValue = MT.createNode("Values.Float Value") intValue = MT.createNode("Values.Int Value") MT.project.root.addNode(container) container.graph.addNode(floatValue) container.graph[1].insockets[0].connected = floatValue.outsockets[0] intValue.insockets[0].connected = container.graph[0].outsockets[0] test.equal(container.graph[1].insockets[0].type, "FLOAT") test.equal(container.outsockets[0].type, "FLOAT") test.equal(container.graph[0].outsockets[0].type, "INTEGER", "input sockets of container graph") test.equal(container.insockets[0].type, "INTEGER", "input of container itself") return test.exit()
def findTrigs(self, dat): dumpN=200 #Throw away first few points since the last trigger sm=MT.smooth(dat[dumpN:],3) #smooth out noise a little thresh=3000 #don't look at the lowest points inds=np.where(sm>thresh)[0] inds=inds[1:][np.diff(inds)>10] #look for a gap in the high values inds+=dumpN return inds #indices of the rising edges
def __init__(self, widget): QGraphicsView.__init__(self) self.setScene(NodeSpace(MT.project.root)) self.widget = widget #QGL.setPreferredPaintEngine(QPaintEngine.OpenGL2) #glFmt = QGLFormat.defaultFormat() #glFmt.setSwapInterval(0) #glFmt.setRgba(True) #glFmt.setDoubleBuffer(True) #glFmt.setOverlay(False) #QGLFormat.setDefaultFormat(glFmt) #glFmt.setSampleBuffers(True) #glWidget = QGLWidget(glFmt) #glWidget.setAutoFillBackground(False) self.setViewportUpdateMode(QGraphicsView.FullViewportUpdate) #self.setViewport(glWidget) #self.setViewport(QGLWidget()) self.setTransformationAnchor(QGraphicsView.AnchorUnderMouse) self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff) self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) self.setUpdatesEnabled(True) self.setDragMode(QGraphicsView.RubberBandDrag) self.cb = MT.attachToSignal("addNode", self.addNode) self.cb2 = MT.attachToSignal("removeNode", self.removeNode) self.cb3 = MT.attachToSignal("newProject", self.newGraph) self.space = MT.project.root self.setAcceptDrops(True) self.dragPos = None containerShortcut = QShortcut(self) deleteShortcut = QShortcut(self) browserShortcut = QShortcut(self) containerShortcut.setContext(Qt.WidgetShortcut) deleteShortcut.setContext(Qt.WidgetShortcut) browserShortcut.setContext(Qt.WidgetShortcut) containerShortcut.setKey("c") browserShortcut.setKey(" ") deleteShortcut.setKey(QKeySequence(Qt.Key_Delete)) containerShortcut.activated.connect(self.createContainer) deleteShortcut.activated.connect(self.deleteNode) browserShortcut.activated.connect(self.showNodeBrowser) self.nodeBrowser = None
def __init__(self, widget): QGraphicsView.__init__(self) self.setScene(NodeSpace(MT.project.root)) self.widget = widget #QGL.setPreferredPaintEngine(QPaintEngine.OpenGL2) #glFmt = QGLFormat.defaultFormat() #glFmt.setSwapInterval(0) #glFmt.setRgba(True) #glFmt.setDoubleBuffer(True) #glFmt.setOverlay(False) #QGLFormat.setDefaultFormat(glFmt) #glFmt.setSampleBuffers(True) #glWidget = QGLWidget(glFmt) #glWidget.setAutoFillBackground(False) self.setViewportUpdateMode(QGraphicsView.FullViewportUpdate) #self.setViewport(glWidget) #self.setViewport(QGLWidget()) self.setTransformationAnchor(QGraphicsView.AnchorUnderMouse) self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff) self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) self.setUpdatesEnabled(True) self.setDragMode(QGraphicsView.RubberBandDrag) self.cb = MT.attachToSignal("addNode", self.addNode) self.cb2 = MT.attachToSignal("removeNode", self.removeNode) self.cb3 = MT.attachToSignal("newProject", self.newGraph) self.space = MT.project.root self.setAcceptDrops(True) self.dragPos = None containerShortcut = QShortcut(self) deleteShortcut = QShortcut(self) browserShortcut = QShortcut(self) containerShortcut.setContext(Qt.WidgetShortcut) deleteShortcut.setContext(Qt.WidgetShortcut) browserShortcut.setContext(Qt.WidgetShortcut) containerShortcut.setKey("c") browserShortcut.setKey(" ") deleteShortcut.setKey(Qt.Key_Delete) containerShortcut.activated.connect(self.createContainer) deleteShortcut.activated.connect(self.deleteNode) browserShortcut.activated.connect(self.showNodeBrowser) self.nodeBrowser = None
def testFloatCache(): '''Creating two add nodes, connecting them and trying to get a value from it''' add = MT.createNode("Math.Add") valuenode = MT.createNode("Values.Float Value") valuenode2 = MT.createNode("Values.Float Value") add.insockets[0].name = "add.in01" valuenode.insockets[0].name = "valuenode.in01" valuenode.insockets[0].value = 5. valuenode2.insockets[0].name = "valuenode2.in01" valuenode2.insockets[0].value = 8. MT.project.root.addNode(add) MT.project.root.addNode(valuenode) MT.project.root.addNode(valuenode2) pos = valuenode.pos valuenode.pos = (pos[0] - 150, pos[1] - 50) valuenode2.pos = (pos[0] - 150, pos[1] + 50) add.insockets[0].connected = valuenode.outsockets[0] add.insockets[1].connected = valuenode2.outsockets[0] add.insockets[2].value = 12. cache = MT.cache.DataCache(add.outsockets[0]) test = TestCase() test.equal(len(MT.project.root), 3, "root space size") test.equal(len(MT.project.root[0].outsockets), 1, "add outsockets") test.equal(len(MT.project.root[0].insockets), 3, "add insockets") test.equal(len(MT.project.root[1].outsockets), 1, "float value 1 outsockets") test.equal(len(MT.project.root[1].insockets), 1, "float value 1 insockets") test.equal(len(MT.project.root[2].outsockets), 1, "float value 2 outsockets") test.equal(len(MT.project.root[2].insockets), 1, "float value 2 insockets") test.equal(MT.project.root[1].insockets[0].value, 5., "float value 1") test.equal(MT.project.root[2].insockets[0].value, 8., "float value 2") test.equal(MT.project.root[0].insockets[2].value, 12., "add input value") print("resulting value: %d" % cache.getOutput()) print("expected value: %d" % (12 + 5 + 8)) test.equal((12 + 5 + 8), cache.getOutput(), "cache output") return test.exit()
def testForLoopCache(): test = TestCase() loop = MT.createNode("General.For") MT.project.root.addNode(loop) loop.insockets[0].value = 0 loop.insockets[1].value = 10 loop.insockets[2].value = 1 add = MT.createNode("Math.Add") loop.graph.addNode(add) test.equal(len(loop.graph[2].insockets), 0, "outsockets of loop container before (direct) connection") test.equal(len(loop.graph[1].outsockets), 1, "looped insockets node has too many sockets") loopsocket = loop.graph[1].outsockets[0] add.insockets[0].connected = loopsocket test.equal(len(loop.graph[2].insockets), 1, "outsockets of loop container before (indirect) connection") add.insockets[1].value = 1 loop.graph[2].insockets[0].connected = add.outsockets[0] loop.insockets[3].value = 1 test.equal(add.outsockets[0].type, "INTEGER", "output of Add") test.equal(add.insockets[0].type, "INTEGER", "1st input of Add") test.equal(add.insockets[1].type, "INTEGER", "2nd input of Add") test.equal(loop.outsockets[0].type, "INTEGER", "output of ForNode") test.equal(loop.insockets[3].type, "INTEGER", "input of startvalue into ForLoop") test.equal(loop.graph[1].outsockets[0].type, "INTEGER", "input of ForLoop Container") test.equal(loop.graph[2].insockets[0].type, "INTEGER", "output of ForLoop Container") test.equal(len(loop.graph), 4, "nodes inside loop container") test.equal(len(loop.graph[0].outsockets), 4, "static loop inputs") test.equal(len(loop.graph[1].outsockets), 2, "looped inputs") if not test.equal(len(loop.graph[2].insockets), 1, "loop outputs"): print("the sockets on this node: {}".format([s.name for s in loop.graph[2].insockets])) test.equal(len(loop.outsockets), 1, "number of outsockes on forloop node") test.equal(len(loop.insockets), 4, "number of insockets on forloop node") cache = MT.cache.DataCache(loop.outsockets[0]) expected_result = 11 test.equal(cache.getOutput(), expected_result, "Loop Result") return test.exit()
def testFloatCache(): """Creating two add nodes, connecting them and trying to get a value from it""" add = MT.createNode("Math.Add") valuenode = MT.createNode("Values.Float Value") valuenode2 = MT.createNode("Values.Float Value") add.insockets[0].name = "add.in01" valuenode.insockets[0].name = "valuenode.in01" valuenode.insockets[0].value = 5.0 valuenode2.insockets[0].name = "valuenode2.in01" valuenode2.insockets[0].value = 8.0 MT.project.root.addNode(add) MT.project.root.addNode(valuenode) MT.project.root.addNode(valuenode2) pos = valuenode.pos valuenode.pos = (pos[0] - 150, pos[1] - 50) valuenode2.pos = (pos[0] - 150, pos[1] + 50) add.insockets[0].connected = valuenode.outsockets[0] add.insockets[1].connected = valuenode2.outsockets[0] add.insockets[2].value = 12.0 cache = MT.cache.DataCache(add.outsockets[0]) test = TestCase() test.equal(len(MT.project.root), 3, "root space size") test.equal(len(MT.project.root[0].outsockets), 1, "add outsockets") test.equal(len(MT.project.root[0].insockets), 3, "add insockets") test.equal(len(MT.project.root[1].outsockets), 1, "float value 1 outsockets") test.equal(len(MT.project.root[1].insockets), 1, "float value 1 insockets") test.equal(len(MT.project.root[2].outsockets), 1, "float value 2 outsockets") test.equal(len(MT.project.root[2].insockets), 1, "float value 2 insockets") test.equal(MT.project.root[1].insockets[0].value, 5.0, "float value 1") test.equal(MT.project.root[2].insockets[0].value, 8.0, "float value 2") test.equal(MT.project.root[0].insockets[2].value, 12.0, "add input value") print("resulting value: %d" % cache.getOutput()) print("expected value: %d" % (12 + 5 + 8)) test.equal((12 + 5 + 8), cache.getOutput(), "cache output") return test.exit()
def testForeachLoopCache(): loop = MT.createNode("General.Foreach") listNode = MT.createNode("General.Create List") add = MT.createNode("Math.Add") value = MT.createNode("Values.Int Value") listNode.insockets[0].value = 12.5 listNode.insockets[1].value = 10 value.insockets[0].value = 2 loop.insockets[0].connected = listNode.outsockets[0] test = TestCase() loopInputNode = loop.graph[1] test.equal(listNode.insockets[0].type, "FLOAT") test.equal(listNode.outsockets[0].type, "LIST:FLOAT") test.equal(len(loopInputNode.outsockets), 1, "inputs to the foreach node") add.insockets[0].connected = loopInputNode.outsockets[0] add.insockets[1].connected = value.outsockets[0] loop.graph[2].insockets[0].connected = add.outsockets[0] MT.project.root.addNode(loop) MT.project.root.addNode(listNode) loop.graph.addNode(add) loop.graph.addNode(value) cache = MT.cache.DataCache(loop.outsockets[0]) output = cache.getOutput() test.equal(add.insockets[0].type, "FLOAT") test.equal(add.insockets[1].type, "INTEGER") test.equal(loop.graph[1].outsockets[0].type, "FLOAT") test.equal(len(output), 10) test.equal(cache.getOutput(), [14.5] * 10) return test.exit()
def testSocketProperties(): value = MT.createNode("Values.Float Value") value.insockets[0].value = 2.5 cache = MT.cache.DataCache(value.outsockets[0]) success = cache.getData(0) == 2.5 value = MT.createNode("Values.Int Value") value.insockets[0].value = 2 cache = MT.cache.DataCache(value.outsockets[0]) success = success and cache.getData(0) == 2 value = MT.createNode("Values.Color Value") value.insockets[0].value = (1, 0, 0, 1) cache = MT.cache.DataCache(value.outsockets[0]) success = success and cache.getData(0) == (1, 0, 0, 1) value = MT.createNode("Values.String Value") value.insockets[0].value = "bla bla HUBSGHDJ" cache = MT.cache.DataCache(value.outsockets[0]) success = success and cache.getData(0) == "bla bla HUBSGHDJ" return success
def _createNetwork(self): self.add = MT.createNode("Math.Add") self.add2 = MT.createNode("Math.Add") self.add3 = MT.createNode("Math.Add") self.valuenode = MT.createNode("Values.Float Value") self.valuenode2 = MT.createNode("Values.Float Value") self.valuenode3 = MT.createNode("Values.Float Value") MT.project.root.addNode(self.valuenode3) MT.project.root.addNode(self.add) MT.project.root.addNode(self.add2) MT.project.root.addNode(self.add3) MT.project.root.addNode(self.valuenode) MT.project.root.addNode(self.valuenode2) pos = self.valuenode.pos self.valuenode.pos = (pos[0] - 150, pos[1] - 50) self.valuenode2.pos = (pos[0] - 150, pos[1] + 50) self.add.insockets[0].connected = self.valuenode.outsockets[0] self.add.insockets[1].connected = self.valuenode2.outsockets[0] self.add.insockets[2].connected = self.valuenode3.outsockets[0] self.add2.insockets[0].connected = self.add.outsockets[0] self.add3.insockets[0].connected = self.add2.outsockets[0]
def __init__(self, space): self.space = space QGraphicsScene.__init__(self) self.setItemIndexMethod(QGraphicsScene.NoIndex) self.tmpLink = None self.createNetwork() self.nodes = {} self.links = {} setattr(MT, "graph", self) self.cb = MT.attachToSignal("createLink", self.drawLink) self.setSceneRect(-5000, -5000, 10000, 10000)
def __init__(self, space): self.space = space QGraphicsScene.__init__(self) self.setItemIndexMethod(QGraphicsScene.NoIndex) self.tmpLink = None self.nodes = {} self.links = {} self.createNetwork() setattr(MT, "graph", self) self.cb = MT.attachToSignal("createLink", self.drawLink) self.setSceneRect(-5000, -5000, 10000, 10000)
def testCreateList(): createListNode = MT.createNode("General.Create List") floatValueNode = MT.createNode("Values.Float Value") intValueNode = MT.createNode("Values.Int Value") MT.project.root.addNode(createListNode) MT.project.root.addNode(floatValueNode) MT.project.root.addNode(intValueNode) floatValueNode.insockets[0].value = 5.0 intValueNode.insockets[0].value = 10 createListNode.insockets[0].connected = floatValueNode.outsockets[0] createListNode.insockets[1].connected = intValueNode.outsockets[0] cache = MT.cache.DataCache(createListNode.outsockets[0]) test = TestCase() test.equal(cache.getOutput(), [5, 5, 5, 5, 5, 5, 5, 5, 5, 5]) test.equal(floatValueNode.insockets[0].value, 5.0) test.equal(intValueNode.insockets[0].value, 10) return test.exit()
def createContainer(nodes): space = nodes[0].space inconnections = {} outconnections = {} container = MT.createNode("General.Container") ci = container.graph[0] co = container.graph[1] middle = (0, 0) for n in nodes: middle = (middle[0] + n.pos[0], middle[1] + n.pos[1]) #find ingoing node connections for socket in n.insockets: if not socket.connected: continue cntd = socket.connected if not cntd.node in nodes: inconnections[socket] = cntd #find outgoing node connections for socket in n.outsockets: if len(socket.cntdSockets) == 0: continue for s in socket.cntdSockets: if not s.node in nodes: outconnections[s] = socket #remove nodes from current space space.removeNode(n) container.graph.addNode(n) space.addNode(container) #reconnect incoming and outgoing connections for isocket, osocket in inconnections.items(): isocket.connected = ci.outsockets[-1] container.insockets[-1].connected = osocket for isocket, osocket in outconnections.items(): co.insockets[-1].connected = osocket isocket.connected = container.outsockets[-1] middle = (middle[0] / 2, middle[1] / 2) container.pos = middle
def __init__(self, parent=None): self.start = 0 self.end = 100 self.frame = 1 self.isPlaying = False self.visibleStart = self.start self.visibleEnd = self.end QWidget.__init__(self) self.setLayout(QVBoxLayout()) self.layout().setSpacing(0) self.layout().setMargin(0) self.scrubwidget = TimelineScrubWidget(self) self.layout().addWidget(self.scrubwidget) start = QSpinBox() start.setRange(0, 99999999) end = QSpinBox() end.setRange(0, 99999999) visiblestart = QSpinBox() visiblestart.setRange(0, 99999999) visibleend = QSpinBox() visibleend.setRange(0, 99999999) self.frameBox = QSpinBox() self.frameBox.valueChanged.connect(self.setFrame) self.frameBox.setRange(self.start, self.end) self.playbutton = QPushButton("Play") self.playbutton.clicked.connect(self.play) stop = QPushButton("stop") stop.clicked.connect(self.stop) start.setValue(self.start) end.setValue(self.end) self.frameBox.setValue(self.frame) visiblestart.setValue(self.visibleStart) visibleend.setValue(self.visibleEnd) lay = QHBoxLayout() self.layout().addLayout(lay) lay.addWidget(start) lay.addWidget(visiblestart) lay.addStretch() lay.addWidget(self.playbutton) lay.addWidget(stop) lay.addWidget(self.frameBox) lay.addStretch() lay.addWidget(visibleend) lay.addWidget(end) self.cb = MT.attachToSignal("frameChanged", self.setFrame)
def all(): print("testing all tests ...") tests = [t for t in dir(__import__(__name__)) if t.startswith("test")] numberOfTests = len(tests) failedTests = [] exceptions = [] for t in tests: MT.newProject() print("\n_____________________________________") print("running test: %s\n" % t) success = False try: success = eval(t).__call__() except: exceptions.append(sys.exc_info()) if success: print("%s passed" % t) else: failedTests.append(t) print("%s failed" % t) print("_____________________________________\n") if len(failedTests) > 0: print("%d out of %d tests failed" % (len(failedTests), numberOfTests)) print("\nfailed Tests:\n") for t in failedTests: print("\t" + t) print("Error info:") for e in exceptions: traceback.print_exception(*e) else: print("All tests passed") return not len(failedTests)
def __init__(self, node = None, parent=None): QWidget.__init__(self, parent) self.tree = QTreeView() self.model = QStandardItemModel(0, 2, parent); self.tree.setModel(self.model) self.model.setHorizontalHeaderLabels(["Name", "Value"]) self.tree.setItemDelegate(Delegate(self)) lay = QVBoxLayout() lay.setContentsMargins(0, 0, 0, 0) lay.setSpacing(0) self.setLayout(lay) lay.addWidget(self.tree) self.customWidget = None self.node = None if node is None: self.cb = MT.attachToSignal("selectionChanged", self.updateEditor) self.cb2 = MT.attachToSignal("linkChanged", self.updateEditorLinks) self.tree.expanded.connect(self.connect) self.tree.collapsed.connect(self.disconnect) else: self.updateEditor(node)
def __init__(self, node=None, parent=None): QWidget.__init__(self, parent) self.tree = QTreeView() self.model = QStandardItemModel(0, 2, parent) self.tree.setModel(self.model) self.model.setHorizontalHeaderLabels(["Name", "Value"]) self.tree.setItemDelegate(Delegate(self)) lay = QVBoxLayout() lay.setContentsMargins(0, 0, 0, 0) lay.setSpacing(0) self.setLayout(lay) lay.addWidget(self.tree) self.customWidget = None self.node = None if node is None: self.cb = MT.attachToSignal("selectionChanged", self.updateEditor) self.cb2 = MT.attachToSignal("linkChanged", self.updateEditorLinks) self.tree.expanded.connect(self.connect) self.tree.collapsed.connect(self.disconnect) else: self.updateEditor(node)
def __init__(self, res=(640,480), Nframes=10): x=np.linspace(-10,10, res[0]) y=np.linspace(-10,10, res[1]) Y,X=np.meshgrid(y,x) Ypos=np.linspace(-3,3,Nframes/2) Ypos=np.hstack([Ypos, Ypos[::-1]]) #sclX=ones(Nframes) #gauss2d=100*gauss(X*sclX[:,np.newaxis, np.newaxis], [1, 0,0.2])*gauss(Y, [1, 0,0.2]) self.dat=np.empty((Nframes, res[0], res[1])) for k in range(Nframes): if k<4: g2d=0 else: g2d=200*MT.gauss2d(X,Y, 4, 4+0*Ypos[k],0.5,0.5,0) self.dat[k]=1*abs(np.random.normal(size=(res[0], res[1]) ) ) + g2d #self.dat=self.dat.astype('u4') self.nextInd=0
def dropNodeConnect(self): sitems = self.graph.scene().selectedItems() if not sitems: return self.dropNodeCenter() last_node = sitems[0].data pos = last_node.pos newPos = (pos[0], pos[1] + (n.NodeDesigner.height * 2) + 20) nodelabel = self.selectedItems()[0].text(1) node = self.graph.dropNode(nodelabel, newPos) for snode in sitems: for outsocket in snode.data.outsockets: for insocket in node.insockets: if (MT.isCompatible(insocket, outsocket) and not insocket.connected): insocket.connected = outsocket break
def initList(self): nodeTypes = MT.getRegisteredNodes() for nt in nodeTypes: currItem = self.invisibleRootItem() for c in nt.split("."): exists = False for i in range(currItem.childCount()): child = currItem.child(i) if child is None: continue if c == child.text(0): currItem = child exists = True continue if not exists: item = QTreeWidgetItem([c, nt]) currItem.addChild(item) currItem = item
def initList(self): nodeTypes = MT.getRegisteredNodes() for nt in nodeTypes: currItem = self.invisibleRootItem(); for c in nt.split("."): exists = False for i in range(currItem.childCount()): child = currItem.child(i) if child is None: continue if c == child.text(0): currItem = child exists = True continue if not exists: item = QTreeWidgetItem([c, nt]) currItem.addChild(item) currItem = item
def dropEvent(self, event): if self.scene().tmpLink: out = self.scene().tmpLink.outsocket self.scene().removeTmpLink() compSockets = MT.getCompatibleSockets(out, self.data) if len(compSockets) > 1: menu = QMenu() def action_triggered_cb(s): def action_triggered(): s.connected = out return action_triggered for name, s in compSockets.items(): action = menu.addAction(name) action.triggered.connect(action_triggered_cb(s)) menu.exec_(event.screenPos()) elif len(compSockets) == 1: s = list(compSockets.values())[0] s.connected = out