Skip to content

Commit 159c155

Browse files
author
Rado Guzinski
committed
Allow passing of parameters to algorithms in dictionaries.
1 parent 2d5e5c5 commit 159c155

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

python/plugins/processing/core/Processing.py

+26-11
Original file line numberDiff line numberDiff line change
@@ -286,24 +286,39 @@ def runAlgorithm(algOrName, onFinish, *args):
286286
if alg is None:
287287
print 'Error: Algorithm not found\n'
288288
return
289-
if len(args) != alg.getVisibleParametersCount() \
290-
+ alg.getVisibleOutputsCount():
291-
print 'Error: Wrong number of parameters'
292-
processing.alghelp(algOrName)
293-
return
294-
295289
alg = alg.getCopy()
296-
if isinstance(args, dict):
297-
# Set params by name
298-
for (name, value) in args.items():
299-
if alg.getParameterFromName(name).setValue(value):
290+
291+
if len(args) == 1 and isinstance(args[0], dict):
292+
# Set params by name and try to run the alg even if not all parameter values are provided,
293+
# by using the default values instead.
294+
setParams = []
295+
for (name, value) in args[0].items():
296+
param = alg.getParameterFromName(name)
297+
if param and param.setValue(value):
298+
setParams.append(name)
300299
continue
301-
if alg.getOutputFromName(name).setValue(value):
300+
output = alg.getOutputFromName(name)
301+
if output and output.setValue(value):
302302
continue
303303
print 'Error: Wrong parameter value %s for parameter %s.' \
304304
% (value, name)
305+
ProcessingLog.addToLog(ProcessingLog.LOG_ERROR, "Error in %s. Wrong parameter value %s for parameter %s." \
306+
% (alg.name, value, name))
305307
return
308+
# fill any missing parameters with default values if allowed
309+
for param in alg.parameters:
310+
if param.name not in setParams:
311+
if not param.setValue(None):
312+
print ("Error: Missing parameter value for parameter %s." % (param.name))
313+
ProcessingLog.addToLog(ProcessingLog.LOG_ERROR, "Error in %s. Missing parameter value for parameter %s." \
314+
% (alg.name, param.name))
315+
return
306316
else:
317+
if len(args) != alg.getVisibleParametersCount() \
318+
+ alg.getVisibleOutputsCount():
319+
print 'Error: Wrong number of parameters'
320+
processing.alghelp(algOrName)
321+
return
307322
i = 0
308323
for param in alg.parameters:
309324
if not param.hidden:

0 commit comments

Comments
 (0)