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
5 Answers 5
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
).
Extending @PolyGeo's answer, running this snippet from the command line and from a toolbox with a single parameter set as "boolean, optional" will give slightly different returns.
import arcpy
useDefault = arcpy.GetParameterAsText(0)
# report to Arcgis message queue
arcpy.AddMessage(useDefault)
arcpy.AddMessage(type(useDefault))
# report to shell console
print useDefault
print type(useDefault)
When run from a toolbox the message is:
true
<type 'unicode'>
and from a command line, python test.py True
:
True
<type 'str'>
When we change to using arcpy.GetParameter(0)
the message queue result is:
1
<type 'bool'>
but the command line report remains unchanged:
True
<type 'str'>
So the useDefault parameter is not a native python boolean. You (sadly) can't use standard python idioms like if useDefault: ...
, but must test against strings:
usedDefault = GetParameterAsText(0)
if not useDefault.lower() == 'true':
#... do "option box not checked" stuff
if useDefault.lower() == 'true':
#... do "option box IS checked" stuff
Strictly speaking the .lower() suffix is not needed if the script will (a) always be run from a toolbox, and (b) be defined as a boolean parameter (checkbox). It's added here for clarity and flexibility.
This can be accomplished via the use of ArcGIS's Script Tool Validation
: http://resources.arcgis.com/en/help/main/10.1/index.html#//00150000002m000000
def updateParameters(self):
"""Modify the values and properties of parameters before internal
validation is performed. This method is called whenever a parameter
has been changed."""
if self.params[1].value == True:
self.params[0].value = arcpy.env.workspace
return
GUI Pic GUI Pic #2
ArcPy is a bit weird with the GetParameterAsText() for the script options. For whatever reason I couldn't test mine as a straight Boolean True/False, as you noted in your question. You can, however, try:
if str(useDefault) == 'true':
arcpy.env.workspace = path
That should work just as you intended.
Try omitting the AsText part. Use:
useDefault = arcpy.GetParameter(1)
It won't change the boolean to a text string. It will get the actual Boolean parameter.
-
I think this is already mentioned in the currently most upvoted answer.2021年06月18日 04:38:44 +00:00Commented Jun 18, 2021 at 4:38
Explore related questions
See similar questions with these tags.