def __setstate__(self, state): self.__init__() self.id = state['id'] self.type = state['type'] self.name = state['name'] self.nodeName = state['nodeName'] self.author = state['author'] self.help = state['help'] self.internals = state['internals'] self.code = state['code'] self.includes = state['includes'] self.previewCodes = state['previewCodes'] self.inputProps = state['inputProps'] self.outputProps = state['outputProps'] # childs self.childs = set() # input links self.inputLinks = {} # output links self.outputLinks = {} for outputProp in self.outputProps: self.outputLinks[outputProp] = [] # create new gfxNode from gfx.view import GfxNode self.gfxNode = GfxNode(self) # set position pos = state['gfxNodePos'] self.gfxNode.setPos(QtCore.QPointF(pos[0], pos[1]))
def buildFrom(cls, type, name, nodeName, author, help, inputProps, outputProps, internals, code, includes, previewCodes): node = cls() # id to identify a single instance Node.id += 1 node.id = Node.id # node stuff node.type = type node.name = name node.nodeName = nodeName node.author = author node.help = help node.code = code node.includes = includes node.previewCodes = previewCodes node.childs = set() # internals node.internals = internals # input properties node.inputProps = inputProps # output properties node.outputProps = outputProps # input links node.inputLinks = {} # output links node.outputLinks = {} for outputProp in node.outputProps: node.outputLinks[outputProp] = [] # create gfxNode from gfx.view import GfxNode node.gfxNode = GfxNode(node) return node
class Node(object): id = 0 def __init__(self): self.id = None self.type = None self.name = None self.childs = set() self.nodeName = None self.author = None self.help = None self.code = None self.internals = None self.includes = None self.inputProps = None self.outputProps = None self.inputLinks = None self.outputLinks = None self.gfxNode = None self.previewCodes = None @classmethod def buildFrom(cls, type, name, nodeName, author, help, inputProps, outputProps, internals, code, includes, previewCodes): node = cls() # id to identify a single instance Node.id += 1 node.id = Node.id # node stuff node.type = type node.name = name node.nodeName = nodeName node.author = author node.help = help node.code = code node.includes = includes node.previewCodes = previewCodes node.childs = set() # internals node.internals = internals # input properties node.inputProps = inputProps # output properties node.outputProps = outputProps # input links node.inputLinks = {} # output links node.outputLinks = {} for outputProp in node.outputProps: node.outputLinks[outputProp] = [] # create gfxNode from gfx.view import GfxNode node.gfxNode = GfxNode(node) return node def __getstate__(self): state = {} # save state state['id'] = self.id state['type'] = self.type state['name'] = self.name state['nodeName'] = self.nodeName state['author'] = self.author state['help'] = self.help state['internals'] = self.internals state['code'] = self.code state['includes'] = self.includes state['previewCodes'] = self.previewCodes state['inputProps'] = self.inputProps state['outputProps'] = self.outputProps state['gfxNodePos'] = (self.gfxNode.pos().x(), self.gfxNode.pos().y()) return state def __setstate__(self, state): self.__init__() self.id = state['id'] self.type = state['type'] self.name = state['name'] self.nodeName = state['nodeName'] self.author = state['author'] self.help = state['help'] self.internals = state['internals'] self.code = state['code'] self.includes = state['includes'] self.previewCodes = state['previewCodes'] self.inputProps = state['inputProps'] self.outputProps = state['outputProps'] # childs self.childs = set() # input links self.inputLinks = {} # output links self.outputLinks = {} for outputProp in self.outputProps: self.outputLinks[outputProp] = [] # create new gfxNode from gfx.view import GfxNode self.gfxNode = GfxNode(self) # set position pos = state['gfxNodePos'] self.gfxNode.setPos(QtCore.QPointF(pos[0], pos[1])) def copy(self): # copy input props inputProps = [] for inputProp in self.inputProps: inputProps.append(inputProp.copy()) # copy output props outputProps = [] for outputProp in self.outputProps: outputProps.append(outputProp.copy()) # create a new node, skipping links newNode = Node.buildFrom(self.type, self.name, self.nodeName, self.author, self.help, inputProps, outputProps, self.internals, self.code, self.includes, self.previewCodes) return newNode def attachOutputPropToLink(self, prop, link): self.outputLinks[prop].append(link) def detachOutputPropFromLink(self, prop, link): outputLinks = self.outputLinks[prop] outputLinks.remove(link) def attachInputPropToLink(self, prop, link): self.inputLinks[prop] = link def detachInputPropFromLink(self, prop): self.inputLinks.pop(prop) def isInputPropertyLinked(self, property): return property in self.inputLinks.keys() def getIncludes(self, includes, alreadyVisitedNodes = None): if alreadyVisitedNodes is None: alreadyVisitedNodes = set() # recurse down the tree for child in self.childs: if child not in alreadyVisitedNodes: child.getIncludes(includes, alreadyVisitedNodes) # append node includes includes += self.includes # update visited nodes alreadyVisitedNodes.add(self) def getAvailableShaderParameters(self, params, alreadyVisitedNodes = None): if alreadyVisitedNodes is None: alreadyVisitedNodes = set() # recurse down the tree for child in self.childs: if child not in alreadyVisitedNodes: child.getAvailableShaderParameters(params, alreadyVisitedNodes) # append properties that are shader parameters for inputProp in self.inputProps: if not self.isInputPropertyLinked(inputProp): params.append({'property' : inputProp, 'node' : self}) # update visited nodes alreadyVisitedNodes.add(self) def getShaderParameters(self, params, alreadyVisitedNodes = None): if alreadyVisitedNodes is None: alreadyVisitedNodes = set() # recurse down the tree for child in self.childs: if child not in alreadyVisitedNodes: child.getShaderParameters(params, alreadyVisitedNodes) # append properties that are shader parameters for inputProp in self.inputProps: if not self.isInputPropertyLinked(inputProp) and inputProp.isShaderParameter: params.append('%s %s_%s = %s' % (inputProp.typeToStr(), self.name, inputProp.name, inputProp.valueToStr())) # update visited nodes alreadyVisitedNodes.add(self) def getCode(self, shaderCode, alreadyGeneratedNodes = None): if alreadyGeneratedNodes is None: alreadyGeneratedNodes = set() print 'In node: %s' % self.name # recurse down the tree for child in self.childs: if child not in alreadyGeneratedNodes: shaderCode = child.getCode(shaderCode, alreadyGeneratedNodes) # define start node comment nodeCode = '''// START NODE %s \n''' % self.name # declare and define input properties for inputProp in self.inputProps: if not self.isInputPropertyLinked(inputProp) and not inputProp.isShaderParameter: nodeCode += '''%s %s_%s = %s;\n''' % (inputProp.typeToStr(), self.name, inputProp.name, inputProp.valueToStr()) import copy code = copy.deepcopy(self.code) # replace input properties for inputProp in self.inputProps: if self.isInputPropertyLinked(inputProp): inputLink = self.inputLinks[inputProp] name = '%s_%s' % (inputLink.sourceNode.name, inputLink.sourceProp.name) code = code.replace('$(%s)' % inputProp.name, name) else: name = '%s_%s' % (self.name, inputProp.name) code = code.replace('$(%s)' % inputProp.name, name) # replace output properties for outputProp in self.outputProps: name = '%s_%s' % (self.name, outputProp.name) code = code.replace('$(%s)' % outputProp.name, name) # replace internals for internal in self.internals: name = '%s_%s' % (self.name, internal) code = code.replace('$(%s)' % internal, name) # append to node code nodeCode += '''\n%s\n''' % code # define end node comment nodeCode += '''// END NODE %s \n\n''' % self.name # append to shader code shaderCode += nodeCode # update generated nodes alreadyGeneratedNodes.add(self) return shaderCode def getPreviewCode(self, shaderType): import copy previewCode = copy.deepcopy(self.previewCodes[shaderType]) # replace output properties for outputProp in self.outputProps: name = '%s_%s' % (self.name, outputProp.name) previewCode = previewCode.replace('$(%s)' % outputProp.name, name) return previewCode