I am using the arcpy.mapping module for the first time, and the lyr.visible command is not working as it should (or I am not using it as I should). I have a list that contains the the layer names of .img files that are loaded into a single data frame - the list is called "layernames". I am using a while loop to go though the layernames list, turn on one of the layers at a time, export a .png file with that layer visible, then turn that layer off and move on to the next layer in the list.
At the beginning of each pass through the loop I specify which raster layer should be turned on by setting layernames.visible = True, and at the end of the pass through the loop I turn off that layer by setting layernames.visible = false.
The first time through the loop the script works and does turn on the correct layer in the list "layernames". But for all subsequent passes through the loop, only the first layer remains visible, and none of the others are turned on or off. In other words, every .png image shows the same raster (the first one that is turned on), instead of a different raster in each .png image. I have pasted my script below.
mxd = arcpy.mapping.MapDocument("Z:\\sharks\\Models\\Summer2013\\daily figures.mxd")
layernames = arcpy.mapping.ListLayers(mxd)
counter = 0
yr = 2005
while counter < 365:
layernames[counter].visible = True
x = str(layernames[counter])
y = x[9:12]
j = int(y)
date = datetime.datetime(yr,1, 1)+ timedelta(days=(j-1))
strDate = str(date)
if (j + 1)<= 31:
mxd.title = "January (" + strDate[0:10] + ")"
elif (j + 1) >= 32 and (j+1) <= 59:
mxd.title = "February (" + strDate[0:10] + ")"
elif (j + 1) >= 60 and (j+1) <= 90:
mxd.title = "March (" + strDate[0:10] + ")"
elif (j + 1) >= 91 and (j+1) <= 120:
mxd.title = "April (" + strDate[0:10] + ")"
elif (j + 1) >= 121 and (j+1) <= 151:
mxd.title = "May (" + strDate[0:10] + ")"
elif (j + 1) >= 152 and (j+1) <= 181:
mxd.title = "June (" + strDate[0:10] + ")"
elif (j + 1) >= 182 and (j+1) <= 212:
mxd.title = "July (" + strDate[0:10] + ")"
elif (j + 1) >= 213 and (j+1) <= 243:
mxd.title = "August (" + strDate[0:10] + ")"
elif (j + 1) >= 244 and (j+1) <= 273:
mxd.title = "September (" + strDate[0:10] + ")"
elif (j + 1) >= 274 and (j+1) <= 304:
mxd.title = "October (" + strDate[0:10] + ")"
elif (j + 1) >= 305 and (j+1) <= 334:
mxd.title = "November (" + strDate[0:10] + ")"
elif (j + 1) >= 335 and (j+1) <= 365:
mxd.title = "December (" + strDate[0:10] + ")"
mxd.save()
out_png = "Z:/sharks/Models/Summer2013/RF/ALL/SB_RF/AVpresRFP/figures/" + strDate[0:10] + ".png"
arcpy.mapping.ExportToPNG(mxd, out_png)
layernames[counter].visible = False
mxd.save()
counter +=1