|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +__version__ = "1.0" |
| 4 | + |
| 5 | +import json |
| 6 | +import logging |
| 7 | +from pathlib import Path |
| 8 | +from meshroom.core import desc |
| 9 | + |
| 10 | + |
| 11 | +class UnwrapMeshroomSceneParam(desc.CommandLineNode): |
| 12 | + """Unwrap the JSON file created by a GetMeshroomSceneParams node |
| 13 | + to expose its items to the graph. |
| 14 | + |
| 15 | + It uses the jsonFile connection to fetch parameters from the |
| 16 | + GetMeshroomSceneParams node to fill the selectedParameter choices, |
| 17 | + so it's better to use it directly after this node. |
| 18 | + """ |
| 19 | + |
| 20 | + category = "Utils" |
| 21 | + |
| 22 | + inputs = [ |
| 23 | + desc.File( |
| 24 | + name="jsonFile", |
| 25 | + label="JSON File", |
| 26 | + description=( |
| 27 | + "JSON file generated by a GetMeshroomSceneParams node.\n" |
| 28 | + "Note that the input must be a GetMeshroomSceneParams node " |
| 29 | + "because we use the input to fetch the choice parameters." |
| 30 | + ), |
| 31 | + value="", |
| 32 | + exposed=True, |
| 33 | + ), |
| 34 | + desc.ChoiceParam( |
| 35 | + name="selectedParameter", |
| 36 | + description="Parameter to fetch from the JSON file.", |
| 37 | + value="", |
| 38 | + values=[], |
| 39 | + exclusive=True, |
| 40 | + exposed=True, |
| 41 | + ) |
| 42 | + ] |
| 43 | + |
| 44 | + outputs = [ |
| 45 | + desc.StringParam( |
| 46 | + name="outputValue", |
| 47 | + description="Extracted value from the JSON file.", |
| 48 | + value=None, |
| 49 | + ), |
| 50 | + ] |
| 51 | + |
| 52 | + @staticmethod |
| 53 | + def updateChoices(node): |
| 54 | + if not node.jsonFile.isLink: |
| 55 | + node.selectedParameter.setValues([]) |
| 56 | + return |
| 57 | + inputNode = node.jsonFile.inputLink.node |
| 58 | + if inputNode.nodeType != "GetMeshroomSceneParams": |
| 59 | + node.selectedParameter.setValues([]) |
| 60 | + return |
| 61 | + inputChoices = [] |
| 62 | + for item in inputNode.parameters.value: |
| 63 | + nodeName = item.nodeInstance.value.strip() |
| 64 | + paramPath = item.paramName.value.strip() |
| 65 | + if nodeName and paramPath: |
| 66 | + inputChoices.append(f"{nodeName}:{paramPath}") |
| 67 | + |
| 68 | + node.selectedParameter.setValues(inputChoices) |
| 69 | + |
| 70 | + @staticmethod |
| 71 | + def setOutput(node): |
| 72 | + if not node.selectedParameter or not node.selectedParameter.value: |
| 73 | + return |
| 74 | + selectedParameter = node.selectedParameter.value |
| 75 | + logging.debug(f"[UnwrapMeshroomSceneParam] Selected parameter: {selectedParameter}") |
| 76 | + nodeInstance, param = selectedParameter.split(":", 1) |
| 77 | + logging.debug(f" nodeInstance: {nodeInstance}") |
| 78 | + logging.debug(f" param : {param}") |
| 79 | + jsonFile = node.jsonFile.value |
| 80 | + if not Path(jsonFile).exists(): |
| 81 | + node.outputValue.value = "" |
| 82 | + return |
| 83 | + with open(jsonFile, "r") as f: |
| 84 | + data = json.load(f) |
| 85 | + logging.debug(f"JSON file : {jsonFile}:\n{data}") |
| 86 | + for item in data: |
| 87 | + if item.get("node") == nodeInstance and item.get("parameter") == param: |
| 88 | + node.outputValue.value = item.get("value") |
| 89 | + break |
| 90 | + else: |
| 91 | + node.outputValue.value = "" |
| 92 | + |
| 93 | + def update(self, node): |
| 94 | + try: |
| 95 | + self.updateChoices(node) |
| 96 | + except Exception as e: |
| 97 | + logging.warning(f"[UnwrapMeshroomSceneParam] Failed to set the choices: {e}") |
| 98 | + |
| 99 | + def processChunk(self, chunk): |
| 100 | + self.setOutput(chunk.node) |
0 commit comments