arcpy visible not working

1833
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
1 Solution

Accepted Solutions
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.

View solution in original post

0 Kudos
15 Replies
MichaelVolz
Esteemed Contributor
Zack:

Might you need to re-populate LYR_list_MXD by running the line:

LYR_list_MXD = arcpy.mapping.ListLayers(MXD,"*_env*",DF)

again, after you saved the file.
0 Kudos
ZackBartlett
New Contributor III
Thanks Michael, but that didn't do the trick either. Still getting blank PDFs.
0 Kudos
MichaelVolz
Esteemed Contributor
Can you try printing out the lyr name in the list to make sure it is getting populated correctly in your second loop?
0 Kudos
JoshuaChisholm
Occasional Contributor III
Hello Zack,

I have a few quick questions/suggestions.
1) Are all the layers in the same extent? Or should you be zooming to the layers before exporting? (see here)
2) Do you have multiple dataframes? If so, make sure the right one is active. (you can use this: mxd.activeView = df.name)
3) Sometimes an active refresh helps (arcpy.RefreshActiveView())

Good luck!
~Josh
0 Kudos
ZackBartlett
New Contributor III
Can you try printing out the lyr name in the list to make sure it is getting populated correctly in your second loop?


I tried adding a print LYR command. It works as it should.
0 Kudos
ZackBartlett
New Contributor III
Hello Zack,

I have a few quick questions/suggestions.
1) Are all the layers in the same extent? Or should you be zooming to the layers before exporting? (see here)
2) Do you have multiple dataframes? If so, make sure the right one is active. (you can use this: mxd.activeView = df.name)
3) Sometimes an active refresh helps (arcpy.RefreshActiveView())

Good luck!
~Josh


Josh,

Thanks for your suggestions.

1. The layers cover close to the same extent. It isn't a matter of my data frame being in the wrong place. In fact, I have a simple base layer turned on in the MXD, and that exports fine.

2. The MXD contains only one dataframe.

3. The RefreshActiveView didn't make a difference.
0 Kudos
ZackBartlett
New Contributor III
I made a few additions to the script based on some of your suggestions to see if things were working properly. It only served to confuse me more. See 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 individualy and export map as new PDF

for LYR in LYR_list_MXD:
        print "1." + str(LYR)
        print "2." + str(LYR.visible)
        LYR.visible = True
        print "3." + str(LYR.visible)
        MXD.save()
        arcpy.RefreshActiveView()
        output_PDF = OutputPDFFolder + "\\" + str(LYR) + ".pdf"
        arcpy.mapping.ExportToPDF(MXD, output_PDF)
        print "4." + str(LYR.visible)
        LYR.visible = False
        print "5." + str(LYR.visible)
        arcpy.RefreshActiveView()
        MXD.save()


As you can see, I have a few print functions in my second for loop. My output is as follows:

1.merge_tdiss_final_buff_env.lyr
2.False
3.True
4.True
5.False

This, of course, repeats for every item in LYR_list_MXD, but I just copied a single one.

- It prints the layer name correctly.
- It prints that the layer isn't visible (as I turned them all off in my first for loop).
- I then turn the layer on and print to make sure it is visible, and it is.
- I then save, refresh, export the map
- print to see if it's still visible
- Turn the layer off, refresh, and save.

But my PDFs are still blank.
0 Kudos
JoshuaChisholm
Occasional Contributor III
If it's any consolation, your script is working well on my machine (produces the correct non-blank PDFs).

Have you triple checked your mxd to make sure the extent is in right spot (in layout view)?
0 Kudos
MichaelVolz
Esteemed Contributor
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?
0 Kudos