2

Suppose a script has a variable number of outputs, which may or may not be produced depending on settings:

##Input_layer=vector
##Output_1=output vector
##Produce_output_2=boolean
##Output_2=output vector

from qgis.core import *

def create_my_output(path):
    # whatever...

create_my_output(Output_1)

if Produce_output_2:
    create_my_output(Output_2)

If this script is run with "Produce output 2" unchecked then QGIS displays a warning dialog saying Output 2 was not correctly produced, even though not producing Output 2 was clearly intended behaviour.

Is there a way to tell QGIS that an output is optional?

4
  • What about replacing if Produce_output_2: with if Produce_output_2 == True? Then after your create_my_output(Output_2), use else: pass.
    – Joseph
    Commented Oct 5, 2015 at 9:53
  • what would else: pass do - isn't that effectively a null operation? Commented Oct 5, 2015 at 10:01
  • Effectively yes although I may have misunderstood your question. I assumed that checking "Produce output 2", you want to create an optional output; unchecking it, you don't want the optional output. Hopefully others can advise :)
    – Joseph
    Commented Oct 5, 2015 at 10:09
  • 1
    That's correct. The dialog is not generated by the if statement though, it's generated by QGIS looking for an output that's not there if the conditional doesn't get run. Commented Oct 5, 2015 at 10:13

1 Answer 1

1

First alternative:

You could uncheck the Open output file after running algorithm option if you uncheck Produce output 2.

As you mentioned in your comment, QGIS cannot load this output if it doesn't get processed by the algorithm, therefore disabling the option to load it should work without an error message showing up.


Second alternative:

Use output file instead of output vector for "Output_2". QGIS will not load this immediately if processed, which means you can add runandload into the algorithm. And if it's not processed, QGIS will not make a fuss. I made a simple buffer script using your definitions:

##Input_layer=vector
##Output_1=output vector
##Output_2=output file
##Produce_output_2=boolean

processing.runalg("qgis:fixeddistancebuffer", Input_layer, 0.01, 10, False, Output_1)

if Produce_output_2 == True:
    processing.runandload("qgis:fixeddistancebuffer", Input_layer, 1.00, 10, False, Output_2)
else:
    pass

Hope this helps somewhat!

3
  • Alas not an alternative if you're trying to make an easy to use UI for somebody else. Unless there's a way to uncheck that from your python script? Commented Oct 5, 2015 at 13:45
  • @SideshowBob - Haven't found out how to uncheck this from the python script but I'm sure there's a way :)
    – Joseph
    Commented Oct 5, 2015 at 14:01
  • @SideshowBob - Couldn't find how to uncheck the "Open output file..." option through the script but added another alternative :)
    – Joseph
    Commented Oct 6, 2015 at 11:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.