Noticed that there is no "Current Project" environment variable for geoprocessing. I like to create script tools to streamline workflows and a useful one for me would be to use the current project. I've made a fair number of script tools that need to reference a project and more often than not, the user will want to use it on their current project. If there was a "Current Project" environment variable then the default for a parameter could be set to that.
Some examples for script tools I've made that would use this.
Image including some of the current environment variables
Hey @DuncanHornby ,
Thanks for reaching out. I am indeed using arcpy.mp with ArcGISProject() in my script tools, but the parameter Data Type I need to use in "File" and I can't default "CURRENT" as a choice in python because I need to allow the user to choose other projects. Only option is to set it as a text parameter but then they cant browse through files.
A solution to your dilemma is to have a drop-down with the options "Current" and "other". If they select current then you use the code I had linked to. If other then that enables another parameter which offers up your File parameter. You do all that in the validation code section of your script tool. Enabling/disabling a parameter has the affect of show/hide the parameter.
@DuncanHornby Thank you for the recommendation. I have attempted to to edit the validation code section to achieve contingent parameters in the past but could not figure it out with the limited documentation I found at the time.
Do you have any validation code examples that show how to hide a parameter and display it based on an option chosen in a previous parameter? I have a lot of use-cases for this. Thanks!
Here is a snippet to get you going:
class ToolValidator:
# Class to add custom behavior and properties to the tool and tool parameters.
def __init__(self):
# set self.params for use in other function
self.params = arcpy.GetParameterInfo()
def initializeParameters(self):
# Customize parameter properties.
# This gets called when the tool is opened.
self.params[2].value = "Regular"
return
def updateParameters(self):
# Modify parameter values and properties.
# This gets called each time a parameter is modified, before
# standard validation.
if self.params[2].value == "Regular":
self.params[3].enabled = False
self.params[4].enabled = True
self.params[5].enabled = False
elif self.params[2].value == "Randomly":
self.params[3].enabled = True
self.params[4].enabled = False
self.params[5].enabled = True
return
def updateMessages(self):
# Customize messages for the parameters.
# This gets called after standard validation.
params[2] is a text parameter with string filter listing only regular or randomly, when you change it on the interface it show/hides parameters 3 or 4.
@DuncanHornby , worked perfectly! Thank you for that code snippet. Will be using in several tools.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.