Select to view content in your preferred language

Model Builder/Python Issue - <type 'exceptions.IndexError'>: list index out of range

904
6
06-26-2013 01:05 PM
DustinWaller1
Deactivated User
I am a "rookie" at python and I incorporated a simple python script into model builder. My model runs without issues but when it gets to the python script, I get this error.

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


Below is my python file. Any help would be greatly appreciated. Thanks.

import arcpy
import os
import glob

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
lyr_region = arcpy.mapping.ListLayers(mxd, "GOM Regions")[0]
lyr_pro = arcpy.mapping.ListLayers(mxd, "Protraction Areas")[0]
lyr_APC = arcpy.mapping.ListLayers(mxd, "APC Active Leases")[0]
lyr_Blk = arcpy.mapping.ListLayers(mxd, "GOM Blocks")[0]
lyr_Bid = arcpy.mapping.ListLayers(mxd, "Tracts Receiving Bids")[0]

for name in glob.glob('C:\Users\dke610\Desktop\GoM\Sale*'):
    
    if name == 'C:\Users\dke610\Desktop\GoM\SaleWGM.xlsx':
        lyr_region.definitionQuery = "MMS_PLAN_A = 'WGM'"
        lyr_pro.definitionQuery = "REGION = 'WGOM'"
        lyr_APC.definitionQuery = "PLANAREA = 'W'"
        lyr_Blk.definitionQuery = "REGION0712 = 'W'"
        lyr_Bid.showLabels = False
        df.extent = lyr_Blk.getSelectedExtent()
        df.scale *= 1

    else:
        lyr_region.definitionQuery = "MMS_PLAN_A = 'CGM'"
        lyr_pro.definitionQuery = "REGION = 'CGOM'"
        lyr_APC.definitionQuery = "PLANAREA = 'C'"
        lyr_Blk.definitionQuery = "REGION0712 = 'C'"
        lyr_Bid.showLabels = False
        df.extent = lyr_Blk.getSelectedExtent()
        df.scale *= 1


arcpy.RefreshActiveView()        
del mxd
Tags (2)
0 Kudos
6 Replies
MathewCoyle
Honored Contributor
I'd wager it was because one of those layers you are trying to reference do not exist in the mxd.
0 Kudos
DustinWaller1
Deactivated User
I would say that I agree but the model runs from the Model Builder Edit window. I only errors out when I double click on the model in the toolbox to run it.
0 Kudos
MathewCoyle
Honored Contributor
If you are running it out of process you may have problems with the 'current' keyword for your mxd, but that should give you a different error. Are any of those layers created within the model you are running? Or do they all exist in the mxd prior to the model running?
0 Kudos
DustinWaller1
Deactivated User
lyr_Bid = arcpy.mapping.ListLayers(mxd, "Tracts Receiving Bids")[0]


This layer is created in the model. I removed that line out of my script and it ran successfully. I suppose the issue is that the "Tracts Receiving Bids" layer isn't displayed until the whole model, script and all, runs completely. Is there a way to display that layer after it is created? This is how it works when running it within model builder.
0 Kudos
MathewCoyle
Honored Contributor
lyr_Bid = arcpy.mapping.ListLayers(mxd, "Tracts Receiving Bids")[0]


This layer is created in the model. I removed that line out of my script and it ran successfully. I suppose the issue is that the "Tracts Receiving Bids" layer isn't displayed until the whole model, script and all, runs completely. Is there a way to display that layer after it is created? This is how it works when running it within model builder.


If you only need the layer for the model you can use the Make Feature Layer tool and reference it in memory. If you need to add it to the map you can then use the Add Layer function in the mapping module after processing.
0 Kudos
DustinWaller1
Deactivated User
Thanks for your help. It is appreciated.
0 Kudos