Select to view content in your preferred language

<type 'exceptions.IndexError'>: list index out of range

2112
1
01-20-2011 03:00 AM
BrentTucker
New Contributor
I keep getting the error <type 'exceptions.IndexError'>: list index out of range in my script tool. The tool works when i hardcode the list of layers in (see commmented line) but fails when i read in the parameters (1) from a value list filter set up in the script tool.

The value list filter contains the same strings as the hardcoded line commented out below. The only difference I see is that the individual elements in lyrList are wrapped in single quotes when read in from the parameter value list and are not when read in from the line of code below.
(i tested this by saving the values of both to a text file to check)

a screen capture of the script tool parameter is attached.

Any suggestions.

Thanks,
Brent

#Specify the map document and the data frame
mxd = arcpy.mapping.MapDocument(r"C:\data\WebPrintTool_v1\test.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]


# Get the Parameters (Feature Set drawn on by user)
areaOfInterest = arcpy.GetParameterAsText(0)
# Get the Parameters (value list filter of layer names - type string)
lyrList = arcpy.GetParameterAsText(1).split(";")
# Get the Parameters (output path to pdf)
outPath = arcpy.GetParameterAsText(2).replace("\\",os.sep)

# Get the feature Set to describe
desc = arcpy.Describe(areaOfInterest)

finalPdf = arcpy.mapping.PDFDocumentCreate(outPath)

# Specify the Area of Interest
df.extent = arcpy.Extent(desc.extent.XMin, desc.extent.YMin, desc.extent.XMax, desc.extent.YMax)

### Turn on visibility for each theme and export the page (this works)##lyrList = ["BEDROCK GEOLOGY", "DRIFT THICKNESS", "SURFICIAL GEOLOGY", "TEST HOLE"]


for lyrName in lyrList:
lyr = arcpy.mapping.ListLayers(mxd, lyrName, df)[0]
lyr.visible = True

#Export each theme to a temporary PDF and append to the final PDF
tmpPdf = os.path.dirname(outPath) + lyrName + "_temp.pdf"
if os.path.exists(tmpPdf):
os.remove(tmpPdf)
arcpy.mapping.ExportToPDF(mxd, tmpPdf)
finalPdf.appendPages(tmpPdf)

#Turn off layer visibility and clean up for next pass through the loop
lyr.visible = False
del lyr, tmpPdf
del mxd, df, finalPdf
0 Kudos
1 Reply
JeffBarrette
Esri Regular Contributor
Hello Brent,

I tried to reproduce on my 10.0 SP1 machine.  I set up everything like you did and it all worked.  Here is my simplified code:

import arcpy
lyrList = arcpy.GetParameterAsText(0).split(";")

mxd = arcpy.mapping.MapDocument(r"c:\temp\brent\brent.mxd")
for lyrName in lyrList:
  lyr = arcpy.mapping.ListLayers(mxd, lyrName)[0]
  lyr.visible = True
  arcpy.mapping.ExportToPDF(mxd, r"c:\temp\brent\\" + lyrName + ".pdf")
  lyr.visible = False

Is it possible you have extra spaces in your value list values?

I'd be more than happy to send you my toolbox/scripts, etc if that helps.

Jeff (jbarrette@esri.com)
0 Kudos