How to satisfy Python Script Tool required parameter with optional boolean?

4018
7
Jump to solution
07-13-2015 12:42 PM
LanceWilson
New Contributor III

I'm using ArcGIS for Desktop 10.3.0.4322

I have a script tool which requires a parameter in the GUI (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. Thanks in advance for any help/tips!

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

Test_Script_Tool2.jpg

0 Kudos
1 Solution

Accepted Solutions
DarrenWiens2
MVP Honored Contributor

As Freddie Gibson says, use tool validation.

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

1.)

2.)

View solution in original post

7 Replies
IanMurray
Frequent Contributor

why create a Boolean Parameter, when you can have the default workspace as the default value for the workspace parameter?

You could set the default value when creating the script tool and the user would have the option to change it.

ArcGIS Help 10.1

WesMiller
Regular Contributor III

What may work for you is script tool validation

FreddieGibson
Occasional Contributor III

You can probably use a tool validator to satisfy the required condition. Honestly I would just make the parameter optional because the user isn't required to interact with it to set it.

LanceWilson
New Contributor III

@ Ian - Thanks for your reply. There's a couple of different reasons for this. Firstly, the tool I'm making is not necessarily for people who are GIS savvy, ergo they may not know where/what their default gdB is, or how to find it. Also, being as how selecting a gdB path is subject to the most recently viewed folder (i.e., when selecting the folder button, it will not necessarily show the defaulted path, but rather the last path accessed), I want a quick and easy way to fill the folder with the default path by clicking the check-box. Lastly, I understand what I'm asking isn't exactly practical, I'm more just curious as to how one would go about doing this. Thanks!

0 Kudos
DarrenWiens2
MVP Honored Contributor

As Freddie Gibson says, use tool validation.

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

1.)

2.)

LanceWilson
New Contributor III

Great, thank you very much for the walk-through explanation. Thanks to everyone for helping point me to 'Tool Validation', which I wasn't very familiar with.

0 Kudos
LanceWilson
New Contributor III

I just finally realized this, after running my tool a few times. Checking the optional box does indeed automatically make the text reflect that of the default gdB workspace. However, now when I don't check it and put in the path to a different gdB, it still goes saves to the default gdB, and in the processing window states 'false' for parameter [0] and 'true' for [1], which I don't understand because [0] is not a boolean, it is a workspace directory. Going to write in some extra code to see if that helps.

Thanks again Darren!

0 Kudos