def copyNode(self, srcNode, withEdges=False): """ Get a copy instance of a node outside the graph. Args: srcNode (Node): the node to copy withEdges (bool): whether to copy edges Returns: Node, dict: the created node instance, a dictionary of linked attributes with their original value (empty if withEdges is True) """ with GraphModification(self): # create a new node of the same type and with the same attributes values # keep links as-is so that CompatibilityNodes attributes can be created with correct automatic description # (File params for link expressions) node = nodeFactory(srcNode.toDict(), srcNode.nodeType) # use nodeType as name # skip edges: filter out attributes which are links by resetting default values skippedEdges = {} if not withEdges: for n, attr in node.attributes.items(): # find top-level links if Attribute.isLinkExpression(attr.value): skippedEdges[attr] = attr.value attr.resetValue() # find links in ListAttribute children elif isinstance(attr, ListAttribute): for child in attr.value: if Attribute.isLinkExpression(child.value): skippedEdges[child] = child.value child.resetValue() return node, skippedEdges
def undoImpl(self): with GraphModification(self.graph): node = nodeFactory(self.nodeDict, self.nodeName) self.graph.addNode(node, self.nodeName) assert (node.getName() == self.nodeName) # recreate out edges deleted on node removal for dstAttr, srcAttr in self.outEdges.items(): self.graph.addEdge(self.graph.attribute(srcAttr), self.graph.attribute(dstAttr))
def load(self, filepath, setupProjectFile=True): """ Load a meshroom graph ".mg" file. Args: filepath: project filepath to load setupProjectFile: Store the reference to the project file and setup the cache directory. If false, it only loads the graph of the project file as a template. """ self.clear() with open(filepath) as jsonFile: fileData = json.load(jsonFile) # older versions of Meshroom files only contained the serialized nodes graphData = fileData.get(Graph.IO.Keys.Graph, fileData) if not isinstance(graphData, dict): raise RuntimeError( 'loadGraph error: Graph is not a dict. File: {}'.format( filepath)) self.header = fileData.get(Graph.IO.Keys.Header, {}) nodesVersions = self.header.get(Graph.IO.Keys.NodesVersions, {}) with GraphModification(self): # iterate over nodes sorted by suffix index in their names for nodeName, nodeData in sorted( graphData.items(), key=lambda x: self.getNodeIndexFromName(x[0])): if not isinstance(nodeData, dict): raise RuntimeError( 'loadGraph error: Node is not a dict. File: {}'.format( filepath)) # retrieve version from # 1. nodeData: node saved from a CompatibilityNode # 2. nodesVersion in file header: node saved from a Node # 3. fallback to no version "0.0": retro-compatibility if "version" not in nodeData: nodeData["version"] = nodesVersions.get( nodeData["nodeType"], "0.0") n = nodeFactory(nodeData, nodeName) # Add node to the graph with raw attributes values self._addNode(n, nodeName) # Create graph edges by resolving attributes expressions self._applyExpr() if setupProjectFile: # Update filepath related members # Note: needs to be done at the end as it will trigger an updateInternals. self._setFilepath(filepath) return True
def undoImpl(self): # delete upgraded node self.graph.removeNode(self.nodeName) # recreate compatibility node with GraphModification(self.graph): node = nodeFactory(self.nodeDict) self.graph.addNode(node, self.nodeName) # recreate out edges for dstAttr, srcAttr in self.outEdges.items(): self.graph.addEdge(self.graph.attribute(srcAttr), self.graph.attribute(dstAttr))
def load(self, filepath): self.clear() with open(filepath) as jsonFile: fileData = json.load(jsonFile) # older versions of Meshroom files only contained the serialized nodes graphData = fileData.get(Graph.IO.Keys.Graph, fileData) if not isinstance(graphData, dict): raise RuntimeError('loadGraph error: Graph is not a dict. File: {}'.format(filepath)) self.header = fileData.get(Graph.IO.Keys.Header, {}) nodesVersions = self.header.get(Graph.IO.Keys.NodesVersions, {}) with GraphModification(self): # iterate over nodes sorted by suffix index in their names for nodeName, nodeData in sorted(graphData.items(), key=lambda x: self.getNodeIndexFromName(x[0])): if not isinstance(nodeData, dict): raise RuntimeError('loadGraph error: Node is not a dict. File: {}'.format(filepath)) # retrieve version from # 1. nodeData: node saved from a CompatibilityNode # 2. nodesVersion in file header: node saved from a Node # 3. fallback to no version "0.0": retro-compatibility if "version" not in nodeData: nodeData["version"] = nodesVersions.get(nodeData["nodeType"], "0.0") n = nodeFactory(nodeData, nodeName) # Add node to the graph with raw attributes values self._addNode(n, nodeName) # Update filepath related members self._setFilepath(filepath) # Create graph edges by resolving attributes expressions self._applyExpr()