arcpy visible not working

1842
15
Jump to solution
02-04-2014 05:09 AM
ZackBartlett
New Contributor III
I have  a script that adds .lyr files to an MXD.

A loop then iterates through the layers in the MXD and turns them all OFF, using the lyr.visible = False function. This part of my code works.

I then want to iterate through the list again, turn on the first layer, export the map as a PDF, turn off the layer, then move onto the next layer. This is the part that doesn't work. The layers do not turn on again. The PDFs export, but they are all blank.

I've seen this question asked before, but the solutions listed don't seem to be working for me.

I've posted the code below.

LYR_list_MXD = arcpy.mapping.ListLayers(MXD,"*_env*",DF)  for LYR in LYR_list_MXD:         LYR.visible = False  MXD.save()  # Turn on each layer individually and export map as new PDF  for LYR in LYR_list_MXD:         LYR.visible = True         output_PDF = OutputPDFFolder + "\\" + str(LYR) + ".pdf"         arcpy.mapping.ExportToPDF(MXD, output_PDF)         LYR.visible = False         MXD.save()


Any suggestions would be greatly appreciated.

Thanks

-Zack
Tags (2)
0 Kudos
15 Replies
ZackBartlett
New Contributor III
What version of ArcMap is installed on the machine where it is working correctly?

What version of ArcMap is installed on the machine where it is not working correctly?


Non-working machine: 10.1, with SP1.
0 Kudos
JoshuaChisholm
Occasional Contributor III
Working machine: 10.1, with SP0.

Note: I did remove the MXD.save() lines, but that shouldn't make a difference.
0 Kudos
ZackBartlett
New Contributor III
UPDATE:

I opened my MXD and ran the code below in the python window.

>>> import arcpy
>>> MXD = arcpy.mapping.MapDocument("CURRENT")
>>> DF = arcpy.mapping.ListDataFrames(MXD, "Layers")[0]
>>> LYR_list = arcpy.mapping.ListLayers(MXD, "*_env*",DF)
>>> for lyr in LYR_list:
...     lyr.visible = True
...     output_PDF = r"T:\JOBS\123110494\gis_data\spatial_data\project_discipline\modeling\scripts\ZB\PDF\\" + str(lyr) + ".pdf"
...     arcpy.mapping.ExportToPDF(MXD, output_PDF)
...     lyr.visible = False
...     MXD.save()
...


This time it worked fine. All my PDFs were exported showing the correct layer.
0 Kudos
ZackBartlett
New Contributor III
Working machine: 10.1, with SP0. 

Note: I did remove the MXD.save() lines, but that shouldn't make a difference.


Joshua,

Turns out it does make a difference!!

I tried running the script with only a single MXD.save() in my code, right at the end, outside of all for loops, and it worked!

I have no idea why, but if you or anyone else out there knows why this would happen, I'd love to know the answer.

Thanks, Joshua, you solved this one without even knowing it!

-Zack
0 Kudos
JoshuaChisholm
Occasional Contributor III
Geez, sorry. I should have mentioned that to start with.

I'd also be very interested to know why that made a difference.

I'm happy it worked out for you!
0 Kudos
BradPosthumus
Occasional Contributor II
Joshua,

I tried running the script with only a single MXD.save() in my code, right at the end, outside of all for loops, and it worked!

-Zack


Wow, I wouldn't have expected that! But now that you mention it, it seems like every time you use MXD.save() the changes to the MXD get lost in limbo until you delete the MXD object.  But that means you need to re-open the MXD again, which isn't ideal and your code would then look something like this:

MXD = arcpy.mapping.MapDocument(MXD_file)
DF = arcpy.mapping.ListDataFrames(MXD)[0]
LYR_list_MXD = arcpy.mapping.ListLayers(MXD,"*_env*",DF)
LYR_count = len(LYR_list_MXD)
for LYR in LYR_list_MXD:
    LYR.visible = False

MXD.save()
del MXD

# Turn on each layer individually and export map as new PDF

for i in range(LYR_count):
    # now you need to reopen the MXD...
    MXD = arcpy.mapping.MapDocument(MXD_file)
    DF = arcpy.mapping.ListDataFrames(MXD)[0]
    LYR = arcpy.mapping.ListLayers(MXD,"*_env*",DF)
    LYR.visible = True
    output_PDF = OutputPDFFolder + "\\" + str(LYR) + ".pdf"
    arcpy.mapping.ExportToPDF(MXD, output_PDF)
    LYR.visible = False
    MXD.save()
    del MXD


Note I've only tested this when running a script outside of ArcMap and not using "CURRENT" as your map document.
0 Kudos