i wrote a script that exports a pdf of an mxd based on a value in a polygon dataset, the script runs fine if run from python window in arcmap with all the values for GetParameterAsText hardcoded, AND it runs fine in model builder with an iterataion on a dbf file EXCEPT for one block of codein the attached code the section that does not run is commented out starting with ## THIS IS WHERE IT FAILS IN MODEL BUILDER and ending with ## THIS IS WHERE IT PICKS UP, by running chunks of the script in the model the failure is the arcpy.MakeFeatureLayer_management command, if run alone from the python window the command works and two new layers are added to the active data frame, but when run in the model no new layers are added ## THIS COMMAND IS IGNORED??, does anyone no why?the error i get is <type 'exceptions.IndexError'>: list index out of rangeFailed to execute (Export Parcel Maps).## Import standard modules
import sys, os
## Set the product code
import arcinfo
## Import arcpy module and environment module
import arcpy
import arcpy.mapping
from arcpy import env
## Overwrite pre-existing files
arcpy.env.overwriteOutput = True
## Set the workspace
IWork = arcpy.GetParameterAsText(1)
arcpy.env.workspace = IWork
# IWork = r"G:\AGIS\PDFs\TestParcelMap"
## Sets the MXD file to the current ArcMap MXD, if run out of ArcMap use the full path to the MXD file
IMXD = arcpy.mapping.MapDocument("CURRENT")
# IMXD = arcpy.mapping.MapDocument("G:\\AGIS\\PDFs\\TestParcelMap\\Parcel_Map_Test.mxd")
## Set Data Frames for the layer to select from
MDF = arcpy.mapping.ListDataFrames(IMXD, "Main Map")[0]
SDF = arcpy.mapping.ListDataFrames(IMXD, "Subinset")[0]
LDF = arcpy.mapping.ListDataFrames(IMXD, "Local Indicator")[0]
## Set the Index layers
ILyr = arcpy.mapping.ListLayers(IMXD,"PA_Index",MDF)[0]
ILyrL = arcpy.mapping.ListLayers(IMXD,"PA_Index",LDF)[0]
## Set the Sheet Number
Sheet = arcpy.GetParameterAsText(0)
# Sheet = 7429
## Create the SQL Query for the sheet number
SheetQry = r"SHEETNUM2 = '%s'"%(Sheet)
## Select the Index Sheet
arcpy.SelectLayerByAttribute_management(ILyr, "NEW_SELECTION", SheetQry)
arcpy.SelectLayerByAttribute_management(ILyrL, "NEW_SELECTION", SheetQry)
## Set the Scale
# MScale = arcpy.GetParameterAsText(2)
# MScale = int(MScale)
MScale = 200
## Set the Data Frame scales
MDF.scale = MScale * 12
SDF.scale = MScale * 38
LDF.scale = MScale * 400
## Get Extent of Selected Sheet
IExt = ILyr.getSelectedExtent()
## Pan to selected extent
MDF.panToExtent(IExt)
SDF.panToExtent(IExt)
LDF.panToExtent(IExt)
## THIS IS WHERE IT FAILS IN MODEL BUILDER
## Capture the Section, Township, and Range
SLyr = arcpy.mapping.ListLayers(IMXD,"PLSS",LDF)[0]
## THIS COMMAND IS IGNORED??
arcpy.MakeFeatureLayer_management("PA_Index","SELSHEET")
arcpy.SelectLayerByLocation_management (SLyr,"INTERSECT", "SELSHEET", -10)
## THIS COMMAND IS IGNORED??
arcpy.MakeFeatureLayer_management("PLSS","STR")
arcpy.SelectLayerByLocation_management(SLyr,"INTERSECT", "SELSHEET", -10,"REMOVE_FROM_SELECTION")
## THIS IS WHERE IT WILL ERROR OUT
# SSheet = arcpy.mapping.ListLayers(IMXD, "SELSHEET", LDF)[0]
# arcpy.mapping.RemoveLayer(LDF,SSheet)
# SSTR = arcpy.SearchCursor("STR")
# for str in SSTR:
# TWN = str.twn
# RNG = str.rng
# del SSTR
# SSTR = arcpy.SearchCursor("STR")
# SEC1 = SSTR.next()
# SECLIST = []
# while SEC1:
# SECVAL =SEC1.getValue("sec")
# if SECVAL not in SECLIST:
# SECLIST.append(SECVAL)
# SEC1 = SSTR.next()
# del SSTR, SEC1
# SECLIST.sort()
# SEC = r"%s"%(SECLIST)
# SEC = SEC.lstrip('[')
# SEC = SEC.rstrip(']')
## THIS IS WHERE IT PICKS UP
# SSLyr = arcpy.mapping.ListLayers(IMXD, "STR", LDF)[0]
# arcpy.mapping.RemoveLayer(LDF,SSLyr)
## Set the Sheet Number
for SheetNum in arcpy.mapping.ListLayoutElements(IMXD, "TEXT_ELEMENT"):
if SheetNum.text == "SHEET 0000":
SheetNum.text = r"SHEET %s"%(Sheet)
## COMMENTED OUT DUE TO FAIL
## Set the Sheet Number and SEC, TWN, RNG
# for SecNum in arcpy.mapping.ListLayoutElements(IMXD, "TEXT_ELEMENT"):
# if SecNum.text == "SECTION: 00":
# SecNum.text = r"SECTION: %s"%(SEC)
# for TRNum in arcpy.mapping.ListLayoutElements(IMXD, "TEXT_ELEMENT"):
# if TRNum.text == "TWP 00 S RNG 00 E":
# TRNum.text = r"TWP %s S RNG %s E"%(TWN, RNG)
## Refresh data farame
arcpy.RefreshActiveView
## Set the name for the PDF
Output = r"%s\P%s.pdf"%(IWork, Sheet)
## Export the map to PDF
arcpy.mapping.ExportToPDF(IMXD, Output)
## Set the Text Elements back
for SheetNum in arcpy.mapping.ListLayoutElements(IMXD, "TEXT_ELEMENT"):
if SheetNum.text == r"SHEET %s"%(Sheet):
SheetNum.text = "SHEET 0000"
## COMMENTED OUT DUE TO FAIL
# for SecNum in arcpy.mapping.ListLayoutElements(IMXD, "TEXT_ELEMENT"):
# if SecNum.text == r"SECTION: %s"%(SEC):
# SecNum.text = r"SECTION: 00"
# for TRNum in arcpy.mapping.ListLayoutElements(IMXD, "TEXT_ELEMENT"):
# if TRNum.text == r"TWP %s S RNG %s E"%(TWN, RNG):
# TRNum.text = r"TWP 00 S RNG 00 E"