class Save: def __init__(self, nodes, connections): self.jsonElements = JSONTools().get(nodes, connections) self.browser = FileBrowser(self.save, True, defaultFilename="project.json") def save(self, doSave): if doSave: self.dlgOverwrite = None self.dlgOverwriteShadow = None path = self.browser.get() path = os.path.expanduser(path) path = os.path.expandvars(path) if os.path.exists(path): self.dlgOverwrite = YesNoDialog( text="File already Exist.\nOverwrite?", relief=DGG.RIDGE, frameColor=(1, 1, 1, 1), frameSize=(-0.5, 0.5, -0.3, 0.2), sortOrder=1, button_relief=DGG.FLAT, button_frameColor=(0.8, 0.8, 0.8, 1), command=self.__executeSave, extraArgs=[path], scale=300, pos=(base.getSize()[0] / 2, 0, -base.getSize()[1] / 2), parent=base.pixel2d) self.dlgOverwriteShadow = DirectFrame( pos=(base.getSize()[0] / 2 + 10, 0, -base.getSize()[1] / 2 - 10), sortOrder=0, frameColor=(0, 0, 0, 0.5), frameSize=self.dlgOverwrite.bounds, scale=300, parent=base.pixel2d) else: self.__executeSave(True, path) base.messenger.send("setLastPath", [path]) self.browser.destroy() del self.browser def __executeSave(self, overwrite, path): if self.dlgOverwrite is not None: self.dlgOverwrite.destroy() if self.dlgOverwriteShadow is not None: self.dlgOverwriteShadow.destroy() if not overwrite: return with open(path, 'w') as outfile: json.dump(self.jsonElements, outfile, indent=2)
class Load: def __init__(self, nodeMgr): self.nodeMgr = nodeMgr self.browser = FileBrowser(self.load, True, defaultFilename="project.json") def load(self, doLoad): if doLoad: path = self.browser.get() path = os.path.expanduser(path) path = os.path.expandvars(path) self.__executeLoad(path) self.browser.destroy() del self.browser def __executeLoad(self, path): fileContent = None try: with open(path, 'r') as infile: fileContent = json.load(infile) except Exception as e: print("Couldn't load project file {}".format(path)) print(e) return if fileContent is None: print("Problems reading file: {}".format(infile)) return # 1. Create all nodes jsonNodes = fileContent["Nodes"] newNodes = [] for jsonNode in jsonNodes: node = self.nodeMgr.createNode(jsonNode["type"]) node.nodeID = UUID(jsonNode["id"]) node.setPos(eval(jsonNode["pos"])) for i in range(len(jsonNode["inSockets"])): inSocket = jsonNode["inSockets"][i] node.inputList[i].socketID = UUID(inSocket["id"]) if "value" in inSocket: node.inputList[i].setValue(inSocket["value"]) for i in range(len(jsonNode["outSockets"])): outSocket = jsonNode["outSockets"][i] node.outputList[i].socketID = UUID(outSocket["id"]) node.show() newNodes.append(node) # 2. Connect all nodes jsonConnections = fileContent["Connections"] for jsonConnection in jsonConnections: # we have a connection of one of the to be copied nodes nodeA = None nodeB = None for node in newNodes: if node.nodeID == UUID(jsonConnection["nodeA_ID"]): nodeA = node elif node.nodeID == UUID(jsonConnection["nodeB_ID"]): nodeB = node socketA = None socketB = None for socket in nodeA.inputList + nodeA.outputList + nodeB.inputList + nodeB.outputList: if socket.socketID == UUID(jsonConnection["socketA_ID"]): socketA = socket elif socket.socketID == UUID(jsonConnection["socketB_ID"]): socketB = socket self.nodeMgr.connectPlugs(socketA, socketB) # 3. Run logic from all leave nodes down to the end self.nodeMgr.updateAllLeaveNodes()