Arcpy – Using Boolean Parameters in Python Script Tool

arcgis-10.3arcpybooleanpython-script-tool

I'm using ArcGIS for Desktop 10.3.0.4322

I have a script tool which requires a parameter (folder path to a gdB, via GetParameterAsText) so that a later process can be saved there. The thing I want to do is make an Optional Boolean parameter, that when checked, automatically fills in the folder path gdB parameter. The Boolean text asks the user, "Use default workspace?", to which if they check the box (boolean == True), then the folder path parameter will be automatically defaulted (saving the user a small amount of time). I have already created the Boolean parameter in the script tool, I am however unaware of how to "fill in" the required parameter based on the user's Boolean.

Code Snippet:

# Ask user to select the Geodatabase workspace to use for data output
userWorkspace = arcpy.GetParameterAsText(0)

# Ask user if they want to use default workspace (boolean)
useDefault = arcpy.GetParameterAsText(1)

# If user wants default workspace, then give it to the required parameter (0)
if useDefault == True:
 arcpy.env.workspace = path
else:
 # Set workspace environment based upon user's choice
 arcpy.env.workspace = userWorkspace

Best Answer

Try adding a print useDefault immediately after you set it to see what is returned.

I have a suspicion (without testing) that it may be returning 'True'(or 'False') rather than 'true' (or 'false').

Also, arcpy.GetParameterAsText() always returns a string so you need to test for the String of 'True'(or 'False').

If you use arcpy.GetParameter() then you will be able to test for the Booleans True (or False).

Related Question