| 1 |
#!/usr/bin/env python |
| 2 |
import argparse |
| 3 |
import distutils.util |
| 4 |
import json |
| 5 |
import logging |
| 6 |
import os |
| 7 |
import re |
| 8 |
import sys |
| 9 |
|
| 10 |
import meshroom.core.graph |
| 11 |
from meshroom import setupEnvironment, logStringToPython |
| 12 |
|
| 13 |
def parseInitInputs(inputs: list[str]) -> dict[str, str]: |
| 14 |
"""Utility method for parsing the input and inputRecursive arguments. |
| 15 |
Args: |
| 16 |
inputs: Command line values in format 'nodeName=value' or just 'value' to set it on all init nodes |
| 17 |
|
| 18 |
Returns: |
| 19 |
Dict mapping node names (or empty string if it applies to all) to their input values |
| 20 |
|
| 21 |
Raises: |
| 22 |
ValueError: If input format is invalid |
| 23 |
""" |
| 24 |
mapInputs = {} |
| 25 |
for inp in inputs: |
| 26 |
# Stop after the first occurrence |
| 27 |
inputGroup = inp.split('=', 1) |
| 28 |
nodeName = inputGroup[0] if len(inputGroup) == 2 else "" |
| 29 |
nodeInputs = inputGroup[-1].split(',') |
| 30 |
mapInputs[nodeName] = [os.path.abspath(path) for path in nodeInputs] |
| 31 |
return mapInputs |
| 32 |
|
| 33 |
|
| 34 |
setupEnvironment() |
| 35 |
|
| 36 |
meshroom.core.initPipelines() |
| 37 |
|
| 38 |
parser = argparse.ArgumentParser( |
| 39 |
prog='meshroom_batch', |
| 40 |
description='Launch a Meshroom pipeline from command line.', |
| 41 |
add_help=True, |
| 42 |
formatter_class=argparse.RawTextHelpFormatter, |
| 43 |
epilog=''' |
| 44 |
Examples: |
| 45 |
1. Process a pipeline in command line: |
| 46 |
meshroom_batch -p cameraTracking -i /input/path -o /output/path -s /path/to/store/the/project.mg |
| 47 |
|
| 48 |
2. Submit a pipeline on renderfarm: |
| 49 |
meshroom_batch -p cameraTracking -i /input/path -o /output/path -s /path/to/store/the/project.mg --submit |
| 50 |
|
| 51 |
See "meshroom_compute -h" to compute an existing project from command line. |
| 52 |
|
| 53 |
Additional Resources: |
| 54 |
Website: https://alicevision.org |
| 55 |
Manual: https://meshroom-manual.readthedocs.io |
| 56 |
Forum: https://groups.google.com/g/alicevision |
| 57 |
Tutorials: https://www.youtube.com/c/AliceVisionOrg |
| 58 |
Contribute: https://github.com/alicevision/Meshroom |
| 59 |
''') |
| 60 |
|
| 61 |
|
| 62 |
general_group = parser.add_argument_group('General Options') |
| 63 |
general_group.add_argument( |
| 64 |
'-i', '--input', |
| 65 |
metavar='FILE FOLDER NODEINSTANCE=FILE,FOLDER,...', |
| 66 |
type=str, |
| 67 |
nargs='*', |
| 68 |
default=[], |
| 69 |
help='Input files and folders to process. ' |
| 70 |
'When multiple Init Nodes exist in the pipeline, inputs are applied to all by default. ' |
| 71 |
'To target a specific Init Node, use the format Node1=input1,input2 Node2=input3') |
| 72 |
|
| 73 |
general_group.add_argument( |
| 74 |
'-I', '--inputRecursive', |
| 75 |
metavar='FOLDER FOLDER_2 NODEINSTANCE=FOLDER,FOLDER_2,...', |
| 76 |
type=str, |
| 77 |
nargs='*', |
| 78 |
default=[], |
| 79 |
help='Recursively scan these directories for input files.') |
| 80 |
|
| 81 |
general_group.add_argument( |
| 82 |
'-p', '--pipeline', |
| 83 |
metavar='FILE.mg / PIPELINE', |
| 84 |
type=str, |
| 85 |
default=os.environ.get('MESHROOM_DEFAULT_PIPELINE', 'photogrammetry'), |
| 86 |
help='Template pipeline among those listed or a Meshroom file containing a custom pipeline ' |
| 87 |
'to run on input images:\n' + |
| 88 |
'\n'.join([' - ' + p for p in meshroom.core.pipelineTemplates])) |
| 89 |
|
| 90 |
general_group.add_argument( |
| 91 |
'-o', '--output', |
| 92 |
metavar='FOLDER COPYFILES_INSTANCE=FOLDER', |
| 93 |
type=str, |
| 94 |
required=False, |
| 95 |
nargs='*', |
| 96 |
help='Output folder for copying results. ' |
| 97 |
'Sets output folder for all CopyFiles nodes, or target specific nodes using COPYFILES_INSTANCE=FOLDER.') |
| 98 |
|
| 99 |
general_group.add_argument( |
| 100 |
'-s', '--save', metavar='FILE', type=str, required=False, |
| 101 |
help='Save the configured Meshroom graph to a project file. It will setup the cache folder ' |
| 102 |
'accordingly if not explicitly changed by --cache.') |
| 103 |
|
| 104 |
general_group.add_argument( |
| 105 |
'--submit', help='Submit on renderfarm instead of local computation.', |
| 106 |
action='store_true') |
| 107 |
|
| 108 |
general_group.add_argument( |
| 109 |
'-v', '--verbose', |
| 110 |
help='Set the verbosity level for logging:\n' |
| 111 |
' - fatal: Show only critical errors.\n' |
| 112 |
' - error: Show errors only.\n' |
| 113 |
' - warning: Show warnings and errors.\n' |
| 114 |
' - info: Show standard informational messages.\n' |
| 115 |
' - debug: Show detailed debug information.\n' |
| 116 |
' - trace: Show all messages, including trace-level details.', |
| 117 |
default=os.environ.get('MESHROOM_VERBOSE', 'warning'), |
| 118 |
choices=['fatal', 'error', 'warning', 'info', 'debug', 'trace']) |
| 119 |
|
| 120 |
advanced_group = parser.add_argument_group('Advanced Options') |
| 121 |
advanced_group.add_argument( |
| 122 |
'--overrides', metavar='SETTINGS', type=str, default=None, |
| 123 |
help='A JSON file containing the graph parameters override.') |
| 124 |
|
| 125 |
advanced_group.add_argument( |
| 126 |
'--paramOverrides', metavar='NODETYPE:param=value NODEINSTANCE.param=value', type=str, default=None, nargs='*', |
| 127 |
help='Override specific parameters directly from the command line (by node type or by node names).') |
| 128 |
|
| 129 |
advanced_group.add_argument( |
| 130 |
'--cache', metavar='FOLDER', type=str, |
| 131 |
default=None, |
| 132 |
help='Custom cache folder to write computation results.') |
| 133 |
|
| 134 |
advanced_group.add_argument( |
| 135 |
'--compute', metavar='<yes/no>', type=lambda x: bool(distutils.util.strtobool(x)), default=True, required=False, |
| 136 |
help='You can set it to <no/false/0> to disable the computation.') |
| 137 |
|
| 138 |
advanced_group.add_argument( |
| 139 |
'--toNode', metavar='NODE', type=str, nargs='*', |
| 140 |
default=None, |
| 141 |
help='Process the node(s) with its dependencies.') |
| 142 |
|
| 143 |
advanced_group.add_argument( |
| 144 |
'--forceStatus', help='Force computation if status is RUNNING or SUBMITTED.', |
| 145 |
action='store_true') |
| 146 |
advanced_group.add_argument( |
| 147 |
'--forceCompute', help='Compute in all cases even if already computed.', |
| 148 |
action='store_true') |
| 149 |
|
| 150 |
advanced_group.add_argument( |
| 151 |
"--submitLabel", |
| 152 |
type=str, |
| 153 |
default=os.environ.get('MESHROOM_SUBMIT_LABEL', '[Meshroom] {projectName}'), |
| 154 |
help="Label of a node when submitted on renderfarm.") |
| 155 |
|
| 156 |
advanced_group.add_argument( |
| 157 |
'--submitter', |
| 158 |
type=str, |
| 159 |
default='SimpleFarm', |
| 160 |
help='Execute job with a specific submitter.') |
| 161 |
|
| 162 |
args = parser.parse_args() |
| 163 |
|
| 164 |
logging.getLogger().setLevel(logStringToPython[args.verbose]) |
| 165 |
|
| 166 |
meshroom.core.initPlugins() |
| 167 |
meshroom.core.initNodes() |
| 168 |
|
| 169 |
graph = meshroom.core.graph.Graph(name=args.pipeline) |
| 170 |
|
| 171 |
with meshroom.core.graph.GraphModification(graph): |
| 172 |
# initialize template pipeline |
| 173 |
loweredPipelineTemplates = {k.lower(): v for k, v in meshroom.core.pipelineTemplates.items()} |
| 174 |
if args.pipeline.lower() in loweredPipelineTemplates: |
| 175 |
graph.initFromTemplate(loweredPipelineTemplates[args.pipeline.lower()], |
| 176 |
copyOutputs=True if args.output else False) |
| 177 |
else: |
| 178 |
# custom pipeline |
| 179 |
graph.initFromTemplate(args.pipeline, copyOutputs=True if args.output else False) |
| 180 |
|
| 181 |
if args.input: |
| 182 |
# get init nodes |
| 183 |
initNodes = graph.findInitNodes() |
| 184 |
initNodesNames = [n.getName() for n in initNodes] |
| 185 |
|
| 186 |
# parse inputs for each init node |
| 187 |
mapInput = parseInitInputs(args.input) |
| 188 |
|
| 189 |
# parse recursive inputs for each init node |
| 190 |
mapInputRecursive = parseInitInputs(args.inputRecursive) |
| 191 |
|
| 192 |
# check that input nodes exist in the pipeline template |
| 193 |
for nodeName in mapInput.keys() | mapInputRecursive.keys(): |
| 194 |
if nodeName and nodeName not in initNodesNames: |
| 195 |
raise RuntimeError(f"Failed to find the Init Node '{nodeName}' in your pipeline.\nAvailable Init Nodes: {initNodesNames}") |
| 196 |
|
| 197 |
# feed inputs (recursive and non-recursive paths) to corresponding init nodes |
| 198 |
for initNode in initNodes: |
| 199 |
nodeName = initNode.getName() |
| 200 |
if nodeName not in mapInput | mapInputRecursive and \ |
| 201 |
"" not in mapInput | mapInputRecursive: |
| 202 |
continue |
| 203 |
|
| 204 |
# Retrieve input per node and inputs for all init node types |
| 205 |
input = mapInput.get(nodeName, []) + mapInput.get("", []) |
| 206 |
# Retrieve recursive inputs |
| 207 |
inputRec = mapInputRecursive.get(nodeName, []) + mapInputRecursive.get("", []) |
| 208 |
initNode.nodeDesc.initialize(initNode, input, inputRec) |
| 209 |
|
| 210 |
if not graph.canComputeLeaves: |
| 211 |
raise RuntimeError("Graph cannot be computed. Check for compatibility issues.") |
| 212 |
|
| 213 |
if args.verbose: |
| 214 |
graph.setVerbose(args.verbose) |
| 215 |
|
| 216 |
if args.output: |
| 217 |
# The output folders for CopyFiles nodes can be set as follows: |
| 218 |
# - for each node, the output folder is specified following the |
| 219 |
# "CopyFiles_name=/output/folder/path" convention. |
| 220 |
# - a path is provided without specifying which CopyFiles node should be set with it: |
| 221 |
# all the CopyFiles nodes will be set with it. |
| 222 |
# - some CopyFiles nodes have their path specified, and another path is provided |
| 223 |
# without specifying a node: all CopyFiles nodes with dedicated will have their own |
| 224 |
# output folders set, and those which have not been specified will be set with the |
| 225 |
# other path. |
| 226 |
# - some CopyFiles nodes have their output folder specified while others do not: all |
| 227 |
# the nodes with specified folders will use the provided values, and those without |
| 228 |
# any will be set with the output folder of the first specified CopyFiles node. |
| 229 |
# - several output folders are provided without specifying any node: the last one will |
| 230 |
# be used to set all the CopyFiles nodes' output folders. |
| 231 |
|
| 232 |
# Check that there is at least one CopyFiles node |
| 233 |
copyNodes = graph.nodesOfType('CopyFiles') |
| 234 |
if len(copyNodes) == 0: |
| 235 |
raise RuntimeError('meshroom_batch requires a pipeline graph with at least ' + |
| 236 |
'one CopyFiles node, none found.') |
| 237 |
|
| 238 |
reExtract = re.compile(r'(\w+)=(.*)') # NodeName=value |
| 239 |
globalCopyPath = '' |
| 240 |
for p in args.output: |
| 241 |
result = reExtract.match(p) |
| 242 |
if not result: # If the argument is only a path, set it for the global path |
| 243 |
globalCopyPath = p |
| 244 |
continue |
| 245 |
|
| 246 |
node, value = result.groups() |
| 247 |
for i, n in enumerate(copyNodes): # Find the correct CopyFiles node in the list |
| 248 |
if n.name == node: # If found, set the value, and remove it from the list |
| 249 |
n.output.value = value |
| 250 |
copyNodes.pop(i) |
| 251 |
if globalCopyPath == '': # Fallback in case some nodes would have no path |
| 252 |
globalCopyPath = value |
| 253 |
break |
| 254 |
|
| 255 |
for n in copyNodes: # Set the remaining CopyPath nodes with the global path |
| 256 |
n.output.value = globalCopyPath |
| 257 |
else: |
| 258 |
print(f'No output set, results will be available in the cache folder: "{graph.cacheDir}"') |
| 259 |
|
| 260 |
if args.overrides: |
| 261 |
with open(args.overrides, encoding='utf-8', errors='ignore') as f: |
| 262 |
data = json.load(f) |
| 263 |
for nodeName, overrides in data.items(): |
| 264 |
for attrName, value in overrides.items(): |
| 265 |
graph.findNode(nodeName).attribute(attrName).value = value |
| 266 |
|
| 267 |
if args.paramOverrides: |
| 268 |
print("\n") |
| 269 |
reExtract = re.compile(r'(\w+)([:.])(\w[\w.]*)=(.*)') |
| 270 |
for p in args.paramOverrides: |
| 271 |
result = reExtract.match(p) |
| 272 |
if not result: |
| 273 |
raise ValueError('Invalid param override: ' + str(p)) |
| 274 |
node, t, param, value = result.groups() |
| 275 |
if t == ':': |
| 276 |
nodesOfType = graph.nodesOfType(node) |
| 277 |
if not nodesOfType: |
| 278 |
raise ValueError(f'No node with the type "{node}" in the scene.') |
| 279 |
for n in nodesOfType: |
| 280 |
print(f'Overrides {node}.{param}={value}') |
| 281 |
n.attribute(param).value = value |
| 282 |
elif t == '.': |
| 283 |
print(f'Overrides {node}.{param}={value}') |
| 284 |
graph.findNode(node).attribute(param).value = value |
| 285 |
else: |
| 286 |
raise ValueError('Invalid param override: ' + str(p)) |
| 287 |
print("\n") |
| 288 |
|
| 289 |
# setup cache directory |
| 290 |
graph.cacheDir = args.cache if args.cache else "" |
| 291 |
|
| 292 |
if args.save: |
| 293 |
graph.save(args.save, setupProjectFile=not bool(args.cache)) |
| 294 |
print(f'File successfully saved: "{args.save}"') |
| 295 |
|
| 296 |
# find end nodes (None will compute all graph) |
| 297 |
toNodes = graph.findNodes(args.toNode) if args.toNode else None |
| 298 |
|
| 299 |
if args.submit: |
| 300 |
meshroom.core.initSubmitters() |
| 301 |
if not args.save: |
| 302 |
raise ValueError('Need to save the project to file to submit on renderfarm.') |
| 303 |
# submit on renderfarm |
| 304 |
meshroom.core.graph.submit(args.save, args.submitter, toNode=args.toNode, |
| 305 |
submitLabel=args.submitLabel) |
| 306 |
elif args.compute: |
| 307 |
# find end nodes (None will compute all graph) |
| 308 |
toNodes = graph.findNodes(args.toNode) if args.toNode else None |
| 309 |
# start computation |
| 310 |
meshroom.core.graph.executeGraph(graph, toNodes=toNodes, forceCompute=args.forceCompute, |
| 311 |
forceStatus=args.forceStatus) |