@@ -371,139 +371,6 @@ def _evaluateUidConflicts(self, graphContent: dict):
371371 n = nodeFactory (nodeData , nodeName )
372372 self ._addNode (n , nodeName )
373373
374- def updateImportedProject (self , data ):
375- """
376- Update the names and links of the project to import so that it can fit
377- correctly in the existing graph.
378-
379- Parse all the nodes from the project that is going to be imported.
380- If their name already exists in the graph, replace them with new names,
381- then parse all the nodes' inputs/outputs to replace the old names with
382- the new ones in the links.
383-
384- Args:
385- data (dict): the dictionary containing all the nodes to import and their data
386-
387- Returns:
388- updatedData (dict): the dictionary containing all the nodes to import with their updated names and data
389- """
390- nameCorrespondences = {} # maps the old node name to its updated one
391- updatedData = {} # input data with updated node names and links
392-
393- def createUniqueNodeName (nodeNames , inputName ):
394- """
395- Create a unique name that does not already exist in the current graph or in the list
396- of nodes that will be imported.
397- """
398- i = 1
399- while i :
400- newName = "{name}_{index}" .format (name = inputName , index = i )
401- if newName not in nodeNames and newName not in updatedData .keys ():
402- return newName
403- i += 1
404-
405- # First pass to get all the names that already exist in the graph, update them, and keep track of the changes
406- for nodeName , nodeData in sorted (data .items (), key = lambda x : self .getNodeIndexFromName (x [0 ])):
407- if not isinstance (nodeData , dict ):
408- raise RuntimeError ('updateImportedProject error: Node is not a dict.' )
409-
410- if nodeName in self ._nodes .keys () or nodeName in updatedData .keys ():
411- newName = createUniqueNodeName (self ._nodes .keys (), nodeData ["nodeType" ])
412- updatedData [newName ] = nodeData
413- nameCorrespondences [nodeName ] = newName
414-
415- else :
416- updatedData [nodeName ] = nodeData
417-
418- newNames = [nodeName for nodeName in updatedData ] # names of all the nodes that will be added
419-
420- # Second pass to update all the links in the input/output attributes for every node with the new names
421- for nodeName , nodeData in updatedData .items ():
422- nodeType = nodeData .get ("nodeType" , None )
423- nodeDesc = meshroom .core .nodesDesc [nodeType ]
424-
425- inputs = nodeData .get ("inputs" , {})
426- outputs = nodeData .get ("outputs" , {})
427-
428- if inputs :
429- inputs = self .updateLinks (inputs , nameCorrespondences )
430- inputs = self .resetExternalLinks (inputs , nodeDesc .inputs , newNames )
431- updatedData [nodeName ]["inputs" ] = inputs
432- if outputs :
433- outputs = self .updateLinks (outputs , nameCorrespondences )
434- outputs = self .resetExternalLinks (outputs , nodeDesc .outputs , newNames )
435- updatedData [nodeName ]["outputs" ] = outputs
436-
437- return updatedData
438-
439- @staticmethod
440- def updateLinks (attributes , nameCorrespondences ):
441- """
442- Update all the links that refer to nodes that are going to be imported and whose
443- names have to be updated.
444-
445- Args:
446- attributes (dict): attributes whose links need to be updated
447- nameCorrespondences (dict): node names to replace in the links with the name to replace them with
448-
449- Returns:
450- attributes (dict): the attributes with all the updated links
451- """
452- for key , val in attributes .items ():
453- for corr in nameCorrespondences .keys ():
454- if isinstance (val , str ) and corr in val :
455- attributes [key ] = val .replace (corr , nameCorrespondences [corr ])
456- elif isinstance (val , list ):
457- for v in val :
458- if isinstance (v , str ):
459- if corr in v :
460- val [val .index (v )] = v .replace (corr , nameCorrespondences [corr ])
461- else : # the list does not contain strings, so there cannot be links to update
462- break
463- attributes [key ] = val
464-
465- return attributes
466-
467- @staticmethod
468- def resetExternalLinks (attributes , nodeDesc , newNames ):
469- """
470- Reset all links to nodes that are not part of the nodes which are going to be imported:
471- if there are links to nodes that are not in the list, then it means that the references
472- are made to external nodes, and we want to get rid of those.
473-
474- Args:
475- attributes (dict): attributes whose links might need to be reset
476- nodeDesc (list): list with all the attributes' description (including their default value)
477- newNames (list): names of the nodes that are going to be imported; no node name should be referenced
478- in the links except those contained in this list
479-
480- Returns:
481- attributes (dict): the attributes with all the links referencing nodes outside those which will be imported
482- reset to their default values
483- """
484- for key , val in attributes .items ():
485- defaultValue = None
486- for desc in nodeDesc :
487- if desc .name == key :
488- defaultValue = desc .value
489- break
490-
491- if isinstance (val , str ):
492- if Attribute .isLinkExpression (val ) and not any (name in val for name in newNames ):
493- if defaultValue is not None : # prevents from not entering condition if defaultValue = ''
494- attributes [key ] = defaultValue
495-
496- elif isinstance (val , list ):
497- removedCnt = len (val ) # counter to know whether all the list entries will be deemed invalid
498- tmpVal = list (val ) # deep copy to ensure we iterate over the entire list (even if elements are removed)
499- for v in tmpVal :
500- if isinstance (v , str ) and Attribute .isLinkExpression (v ) and not any (name in v for name in newNames ):
501- val .remove (v )
502- removedCnt -= 1
503- if removedCnt == 0 and defaultValue is not None : # if all links were wrong, reset the attribute
504- attributes [key ] = defaultValue
505-
506- return attributes
507374
508375 def importGraphContentFromFile (self , filepath : PathLike ) -> list [Node ]:
509376 """Import the content (nodes and edges) of another Graph file into this Graph instance.
0 commit comments