List layers in selected dataframe

497
2
Jump to solution
01-15-2018 06:17 AM
Zeke
by
Regular Contributor III

I have a tool where I'm trying to do some script validation. The user selects a data frame from a map as the first parameter, self.params[0] (works fine), and then should select a layer from the layers in that data frame. However, I can't get the second parameter to populate based just on the selected dataframe. The code below doesn't throw an error, but returns an empty list, and thus no layers to select. I've tried several variations on this, and the only one that works at all returns all layers in the mxd, not just the ones in the data frame. This seems like it should be simple, but I'm missing something. Any ideas? Thanks.

def initializeParameters(self):
    """Refine the properties of a tool's parameters.  This method is
    called when the tool is opened.
    Populates data frame parameter with data frames in map and disables other
    parameters.
    """
    try:   # this works
        mxd = arcpy.mapping.MapDocument('CURRENT')
        dflist = [df.name for df in arcpy.mapping.ListDataFrames(mxd)]
        self.params[0].filter.type = "ValueList"
        self.params[0].filter.list = dflist
        del mxd
    except:
        arcpy.AddError("\nMust run this tool in ArcMap, not ArcCatalog")

    self.params[1].enabled = False
    self.params[2].enabled = False

    return

  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."""

    mxd = arcpy.mapping.MapDocument('CURRENT')

    if self.params[0].value:
        self.params[1].enabled = True

        # this does not work the way I want
        layerList = [lyr.name for lyr in arcpy.mapping.ListLayers(mxd, self.params[0].value) if not lyr.isGroupLayer]

        uniqueList = list(set(layerList))
        uniqueList.sort()

        self.params[1].filter.type = "ValueList"
        self.params[1].filter.list = uniqueList
0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Esteemed Contributor

Don't you need the dataframe that is selected? it doesn't appear that it is there

arcpy.mapping.ListLayers

(map_document_or_layer, {wildcard}, {data_frame})

View solution in original post

2 Replies
AdrianWelsh
MVP Notable Contributor

Hi Greg,

Is it possible that there is an indentation issue in your code? Maybe on line 29?

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

Don't you need the dataframe that is selected? it doesn't appear that it is there

arcpy.mapping.ListLayers

(map_document_or_layer, {wildcard}, {data_frame})