AnsweredAssumed Answered

Arcpy trouble with string passsed with GetParameterAsText

Question asked by jimcousins on Nov 30, 2016
Latest reply on Dec 1, 2016 by jimcousins

I have a very simple program to toggle on and off layers and export PNGs. The issue is trying to pass a string to search for in the ListLayers result.

If I pass the parameter with the tool dialog, I get no return, even though I AddMessage the string and it shows as expected (see the 4th line under Messages).

If I hardcode the string on line 27:

     for lyr in arcpy.mapping.ListLayers(mxd, "*Events", df):

, no troubles.

I expect I am missing something basic in the interpolation of the passed string, but I have tried enclosing in additional quotes and using escapes before the quotes, to no avail.

Best Regards,

Jim

import arcpy

Folder = arcpy.GetParameterAsText(0)
Folder = Folder.replace("\\","/")
SearchStr = arcpy.GetParameterAsText(1)
arcpy.AddMessage( SearchStr)
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd,"Layers") [0]

def ExportPNG(sspaText):
    # Update the layout text
    #for el in arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT","SSPA_Title"):
    #    el.text = sspaText
    arcpy.AddMessage("Processing " + sspaText)
    arcpy.RefreshActiveView()
    # Export the Map
    arcpy.mapping.ExportToPNG(mxd, Folder + "/" + sspaText + ".png", resolution=600)

def SetVisibility(visNam):
    for lyr in arcpy.mapping.ListLayers(mxd,visNam,df):
        lyr.visible = 'true'

def SetInvisibility(visNam):
    for lyr in arcpy.mapping.ListLayers(mxd, visNam,df):
        lyr.visible = 'false'

for lyr in arcpy.mapping.ListLayers(mxd, SearchStr, df):

    baseName = lyr.name
    arcpy.AddMessage(baseName)

    # Call the SetVisibility function to turn on the layer
    SetVisibility(baseName)
    arcpy.RefreshActiveView()

    sspaText = lyr.name[:-7]

    ExportPNG(sspaText)
    # Call the SetInvisibility function to turn off the layer
    SetInvisibility(baseName)

Outcomes