I had some code working previously that is now causing pythonw.exe to crash. I have an .mxd set up that I am using to loop through features in a layer and export the layout to jpeg. However now the script crashes when it gets to df.zoomtoselectedfeatures. Here is what I have:
When I run this I get a Windows OS message saying "pythonw.exe has crashed". I narrowed it down to the line df.zoomToSelectedFeatures() (if I comment out this line the code runs but obviously does not output what I want). Again this was working previously (about six months ago) I must have tested it a hundred times. All of the data sources are working in the mxd, and I tried re-saving the mxd and pointing to the new copy but no luck. I also printed my where_clause and it looks good. It must be a problem with my "list data frames / list layers code". Any help would be appreciated.
Code:
import arcpy
Path = os.getcwd()
GSP_Join = "C:\\Users\\TDMC0\\AppData\\Local\\Temp\\scratch.gdb\\GSP_Join"
mxd = arcpy.mapping.MapDocument(Path + r"\GasLeaks2013.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
Layer = arcpy.mapping.ListLayers(mxd, "", df)[0]
Layer2 = arcpy.mapping.ListLayers(mxd, "", df)[1]
Layer3 = arcpy.mapping.ListLayers(mxd, "", df)[2]
# Create a search cursor
try:
rows = arcpy.SearchCursor(GSP_Join, "", "", "CONN_OBJ_ID_NUM", "CONN_OBJ_ID_NUM")
except Exception as e:
print e
# Iterate through the Implausible Reads Feature, getting and zooming to respective extents and exporting layout to JPEG.
currentState = " "
count = arcpy.GetCount_management(GSP_Join)
progress = 1
for row in rows:
try:
if currentState != row.CONN_OBJ_ID_NUM:
print "\rExporting Data Driven Pages to JPEG...%s of %s" %(progress, count)
currentState = row.CONN_OBJ_ID_NUM
where_clause = "\"CONN_OBJ_ID_NUM\"=" + "'" + currentState + "'"
arcpy.SelectLayerByAttribute_management(Layer, "NEW_SELECTION", where_clause)
df.zoomToSelectedFeatures()
df.scale = 5500
arcpy.RefreshActiveView()
arcpy.SelectLayerByAttribute_management (Layer, "CLEAR_SELECTION", "")
Layer.definitionQuery = where_clause
Layer2.definitionQuery = where_clause
Layer3.definitionQuery = where_clause
arcpy.mapping.ExportToJPEG(mxd, r"E:\GasLeaksProject\2012\JPEG\CONN_OBJ_ID_NUM" + str(currentState) + ".jpeg", resolution=300, jpeg_quality=70)
Layer.definitionQuery = ""
Layer2.definitionQuery = ""
Layer3.definitionQuery = ""
progress = progress + 1
except Exception as e:
print e