AnsweredAssumed Answered

List layers in selected dataframe

Question asked by recurvata on Jan 15, 2018
Latest reply on Jan 16, 2018 by Dan_Patterson

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

Outcomes