in a pyt (python toolbox): How do I make a boolean parameter default False???

7066
5
Jump to solution
09-26-2014 06:42 AM
MartinHvidberg
Occasional Contributor

In a .pyt I can define parameters as described in :

ArcGIS Help (10.2, 10.2.1, and 10.2.2)

I have created a boolean parameter, the code snippet is as follow:

def getParameterInfo(self):
    """Define parameter definitions"""

    ... some other parameters ...

   

    # 3. Overwrite
    param3 = arcpy.Parameter(
        displayName="Overwrite Existing OBJ",
        name="Overwrite",
        datatype="GPBoolean",
        parameterType="Required",
        direction="Input")  

The parameter works, but it anoys me that it defaults to True, I would like it to default to False.

v.h. Martin

0 Kudos
1 Solution

Accepted Solutions
TimothyHales
Esri Notable Contributor

You can set the default value by setting the value like this:


param3 = arcpy.Parameter(


     displayName="Overwrite Existing OBJ",


     name="Overwrite",


     datatype="GPBoolean",


     parameterType="Required",


     direction="Input")  






param3.value = "False"





View solution in original post

5 Replies
TimothyHales
Esri Notable Contributor

You can set the default value by setting the value like this:


param3 = arcpy.Parameter(


     displayName="Overwrite Existing OBJ",


     name="Overwrite",


     datatype="GPBoolean",


     parameterType="Required",


     direction="Input")  






param3.value = "False"





curtvprice
MVP Esteemed Contributor

Right on, Tim.

There are more details (and  examples) in the help page on this topic:

Defining parameters in a Python toolbox: ‌/ Setting default values for a parameter

Another wrinkle with booleans is the use of keywords, so the script tool's dialog box (and arcpy) will support a pick list of keywords.

From the same help page:

A Value List can be used for Boolean data types. For Boolean data types, the Value List contains two values: the true value and false value. The true value is always the first value in the list. These values are used in the command line for specifying the value.

param3.filter.list = ["OVERWRITE", "NO_OVERWRITE"]

param3.value = False

curtvprice
MVP Esteemed Contributor

Timothy Hales‌ and Martin Hvidberg‌  - wondering about:

param3.value = "False"

as Python bool ("False") is true (ie the string is "False" is non-empty so it is considered True in value. The parameter object may be converting it from "False" to False but I'd check to be sure.

0 Kudos
TimothyHales
Esri Notable Contributor

Valid concern there. I just tested this, and the string and the raw value both are seen correctly.

def getParameterInfo(self):

        """Define parameter definitions"""

        param2 = arcpy.Parameter(

             displayName="False String",

             name="FalseString",

             datatype="GPBoolean",

             parameterType="Required",

             direction="Input") 

        param2.value = "False"

        param3 = arcpy.Parameter(

             displayName="False",

             name="False",

             datatype="GPBoolean",

             parameterType="Required",

             direction="Input") 

        param3.value = False

        param4 = arcpy.Parameter(

             displayName="True String",

             name="TrueString",

             datatype="GPBoolean",

             parameterType="Required",

             direction="Input") 

        param4.value = "True"

        param5 = arcpy.Parameter(

             displayName="True",

             name="True",

             datatype="GPBoolean",

             parameterType="Required",

             direction="Input") 

        param5.value = True

        params = [param2, param3, param4, param5]

        return params

BooleanTool.jpg

curtvprice
MVP Esteemed Contributor

def execute(self, parameters, messages):

    """The source code of the tool."""

    arcpy.AddMessage([k.value for k in parameters])

    return

Executing: Tool false false true true
Start Time: Mon Sep 29 16:09:23 2014
Running script Tool...
[False, False, True, True]
Completed script Tool...
Succeeded at Mon Sep 29 16:09:23 2014 (Elapsed Time: 0.00 seconds)

Yes, indeed, everything gets interpreted through its string representation, which I bet is case insensitive ("false" is interpreted by arcpy the same as "False")