You should use the ToolValidator class for that.
Create a parameter of type String
in script tool. Set the Filter
to be the Value List
. Leave the list empty.
Edit the script tool Python file.
import arcpy
in_layer_name = arcpy.GetParameterAsText(0)
in_layer = arcpy.MakeFeatureLayer_management(in_layer_name,"out_fl")
arcpy.CopyFeatures_management(in_layer,"OUT_TEST")
- Edit the ToolValidator class'
initializeParameters
.
def initializeParameters(self):
"""Refine the properties of a tool's parameters. This method is
called when the tool is opened."""
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
lyrs = [lyr for lyr in arcpy.mapping.ListLayers(mxd, "",df)
if lyr.dataSource[-4:] == ".shp" and
arcpy.Describe(lyr.dataSource).shapeType == "Polygon"]
self.params[0].filter.list = [lyr.longName for lyr in lyrs]
return
Now you will get the list of layer names (as strings) in the drop-down list of the parameter. You just need to make those feature layers (which is done on the 3rd line in the script tool file above).