Select to view content in your preferred language

Auto populate script tool parameter with layers in a map document

3274
2
Jump to solution
10-03-2013 08:19 AM
RoySP
by
Regular Contributor
Does any one know how to auto populate a script tool parameter with list of layers in a map document when the map document is provided as the first parameter.

This functionality is common when opening arctoolbox tools within a map document. The input dataset parameters automatically pick up the layers and tables in the table of contents.

I know that tool validation can be used to get the list of fields in a feature class. I did not have luck applying the same logic for list of layers in an mxd.

Thanks for your help!
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JeffBarrette
Esri Regular Contributor
You'll want to work with validation scripts.  You get to validation scripts by right-clicking on a script tool, go to properties, and selecting the validation tab.

Here is a sample script that updates a parameter with layer names once a map document

  def updateParameters(self):     """Modify the values and properties of parameters before internal     validation is performed.  This method is called whenever a parmater     has been changed."""          import arcpy     if self.params[0].value:       mxd = arcpy.mapping.MapDocument(self.params[0].value.value)       lyrs = arcpy.mapping.ListLayers(mxd)       layerList = []       for lyr in lyrs:         layerList.append(lyr.name)        uniqueList = list(set(layerList))       uniqueList.sort()              if not self.params[1].altered:         self.params[1].filter.list = uniqueList     return


There is an arcpy.mapping sample scripts download that has about 20 different script tools and several different validation scripts.

http://www.arcgis.com/home/item.html?id=18c19ec00acb4d568c27bc20a72bfdc8

The sample above is from the Replace Layer with Layer File example.

Jeff

View solution in original post

0 Kudos
2 Replies
JeffBarrette
Esri Regular Contributor
You'll want to work with validation scripts.  You get to validation scripts by right-clicking on a script tool, go to properties, and selecting the validation tab.

Here is a sample script that updates a parameter with layer names once a map document

  def updateParameters(self):     """Modify the values and properties of parameters before internal     validation is performed.  This method is called whenever a parmater     has been changed."""          import arcpy     if self.params[0].value:       mxd = arcpy.mapping.MapDocument(self.params[0].value.value)       lyrs = arcpy.mapping.ListLayers(mxd)       layerList = []       for lyr in lyrs:         layerList.append(lyr.name)        uniqueList = list(set(layerList))       uniqueList.sort()              if not self.params[1].altered:         self.params[1].filter.list = uniqueList     return


There is an arcpy.mapping sample scripts download that has about 20 different script tools and several different validation scripts.

http://www.arcgis.com/home/item.html?id=18c19ec00acb4d568c27bc20a72bfdc8

The sample above is from the Replace Layer with Layer File example.

Jeff
0 Kudos
RoySP
by
Regular Contributor
This is great! Thanks Jeff!
0 Kudos