Hello
So – yes- - it can be done but you will need to write some Python Validation code within the ToolValidator class of the Script Tool properties dialog box and make some changes to your Python script tool dialog
1: Display the Tool Properties dialog box for your script tool.
2: On your form create a new parameter which will allow you to search for a workspace, for example, use the following properties as a guide to search for a file geodatabase:
Label: Choose the Workspace
Data Type: Workspace
Filter: Workspace :: Local File System (This allows you to select ONLY file geodatabases)
I think you may have a parameter which allows you to select feature layers from the Contents pane?
3: Change this parameter using the following as a guide:
Label: Choose the Feature class
Data Type: String
Filter: Value List (This means your parameter will display values within a drop down list)
Your parameters tab in the Tool Properties dialog box should look like the following:
Notice the numbers next to the parameters: 0 is the position of the workspace parameter while 1 is the position of the feature class parameter. You will use these numbers in a moment.
4: Click the Validation tab.
This is where you can write Python code which will execute when your form is displayed or when you interact with the parameters on the form.
5: Locate the updateParameters() method.
6: Underneath the comment for the method write the following code:
wkParam = self.params[0]
if wkParam.altered and not wkParam.hasBeenValidated:
fcParam = self.params[1]
arcpy.env.workspace = wkParam.value
fcs = arcpy.ListFeatureClasses()
if len(fcs) > 0:
fcParam.filter.list = fcs
fcParam.value = fcs[0]
Be aware of the importance of indentation!
What the code does is:
- Get the parameters from a list of parameters from the dialog.
- Check to see if the workspace parameter has been altered by the user choosing a workspace and that the parameter has not gone through ArcGIS Pro’s validation routines already. If you don’t put this check in then you will be forever in a validation spiral which will affect the performance of your script tool.
- Get the chosen workspace and a list of feature classes
- Assign the list of feature classes to the feature class parameter using the filter and list properties on the Parameter object. Remember we set the Filter to be a Value List earlier?
- Get the 1st item from the list and assign it as the first item for display in the combo box before the user clicks on it.
The method should look like the following:
7: Press the OK button on the Tool Properties dialog box.
8: Double click the script tool to display the dialog:
9: Choose a file geodatabase
The Choose Feature Class parameter will now be populated with the feature classes within the chosen file geodatabase.
If you choose a different file geodatabase then the drop down combo box will be cleared and re-populated with the new feature classes from the geodatabase!
I hope this does what you want it to do.
I will make this into a blog to add a bit of meat to these bones and i'll put the link here once it's written but this should be enough for you....
Many thanks ed