layer.longName is not returning the layer's long name

279
3
02-06-2018 07:28 AM
GlenAdams2
New Contributor

I am working on a tool validator and am having an issue where the layer.longName is only returning layer.name. In the code snippet below, the first set of appends to my list should be adding the longName of each layer that is already in a multivalue field. However, when I check what values are being added to my list it is only the short names.

I have attached images that show the output and the values of retList after each step.

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."""
    if self.params[0].value:
        searchString = self.params[0].value
        mxd = arcpy.mapping.MapDocument("CURRENT")
        retList = []
        if self.params[1].values:
            for value in self.params[1].values:
                if type(value) is type(arcpy.mapping.ListLayers(mxd, "*")[0]):
                    retList.append(value.longName)
        win32api.MessageBox(0, "retList: " + str(retList))
        for df in arcpy.mapping.ListDataFrames(mxd):
            for lyr in arcpy.mapping.ListLayers(mxd, "*" + searchString + "*", df):
                if lyr.longName not in retList:
                    retList.append(lyr.longName)
        win32api.MessageBox(0, "setting values to: " + str(retList))
        self.params[1].values = retList
    return
0 Kudos
3 Replies
JoshuaBixby
MVP Esteemed Contributor

Since I can't move this question myself, I am sharing with https://community.esri.com/community/developers/gis-developers/python?sr=search&searchId=bde87ee5-cb...‌ since this is an ArcPy question and not an ArcGIS API for Python question.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Given you are running a tool and only shared part of the code, can you either share more code or replicate the problem outside of the tool and share that code? 

0 Kudos
GlenAdams2
New Contributor

Sorry, I should have explained this better, this is in a tool validator class and this part is the only part that I have changed. The code that is producing the issue is independent of any other part of the underlying script/tool. It can only be reproduced in this manner because the issue comes from multivalue parameters in ArcGIS tool validators.

If you have a multivalue parameter and get params.value on it you get a value table object, but if you do params.values you get a list. I am guessing (but not able to be sure) that this issue comes from ArcMap/ArcPy converting the value table to a list incorrectly.

My multivalue parameter is a Layer type (see below), but when I get params.values without type checking each value to make sure I am receiving a layer object, ArcMap throws in the geoprocessing objects and other random things. Even when I do get a layer object, as I showed here, each layer object that is returned seems to have only the name of the layer set to the longName attribute. I wish there was more documentation about this sort of thing, dealing with multivalue inputs in ArcPy has been terrible so far.

EDIT: In case anyone else sees this and has the same problem, here is a workaround using the ValueTable object returned from params.value

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."""
    if self.params[0].value:
        searchString = self.params[0].value
        mxd = arcpy.mapping.MapDocument("CURRENT")
        retList = []
        if self.params[1].value:
            values = self.params[1].value.exportToString().split(";")
            for value in values:
                retList.append(value.replace("\'", "").strip())
        for df in arcpy.mapping.ListDataFrames(mxd):
            for lyr in arcpy.mapping.ListLayers(mxd, "*" + searchString + "*", df):
                if lyr.longName not in retList:
                    retList.append(lyr.longName)
        self.params[1].values = retList
    return
0 Kudos