Select to view content in your preferred language

Layer Visible

4818
6
Jump to solution
07-26-2017 06:32 PM
DaveMarkey
Regular Contributor

I am attempting to turn a layer on in the Layer of Contents via a python script. When I execute the script below either as a script tool in ArcToolbox or via the python window in ArcMap the visibility status changes in the layer properties but the layer does not turn on in the Table of Contents and the layer does not draw in the data view window. I execute this script within ArcMap and have layers in the Table of Contents which are currently turned off.

# Import arcpy module
import arcpy
from arcpy import env
import sys
import os


mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "")[0]
for lyr in arcpy.mapping.ListLayers(mxd, "", df):
    lyr.visible = True
arcpy.RefreshTOC()
arcpy.RefreshActiveView()

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
RhettZufelt
MVP Notable Contributor

For one, unless you have a data frame named "", it will not get the proper value for df.

maybe try:

# Import arcpy module
import arcpy
from arcpy import env
import sys
import os

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
for lyr in arcpy.mapping.ListLayers(mxd, "", df):
    lyr.visible = True
arcpy.RefreshTOC()
arcpy.RefreshActiveView()
‍‍‍‍‍‍‍‍‍‍‍‍

Or whatever your data frame name is if it is not "Layers".

At least for me.  The documentation suggests that it should work, but I have always had to put the df name in there.


R_

View solution in original post

6 Replies
JoshuaBixby
MVP Esteemed Contributor

What version of ArcMap are you using?  Do you have more than 1 Data Frame?  If so, is the active frame the one you are trying to manipulate?  What does your layer structure look like?  Do you have nested/group layers?

RhettZufelt
MVP Notable Contributor

For one, unless you have a data frame named "", it will not get the proper value for df.

maybe try:

# Import arcpy module
import arcpy
from arcpy import env
import sys
import os

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
for lyr in arcpy.mapping.ListLayers(mxd, "", df):
    lyr.visible = True
arcpy.RefreshTOC()
arcpy.RefreshActiveView()
‍‍‍‍‍‍‍‍‍‍‍‍

Or whatever your data frame name is if it is not "Layers".

At least for me.  The documentation suggests that it should work, but I have always had to put the df name in there.


R_

IanMurray
Honored Contributor

Conversly they could just use "*" as the input which would just be a wildcard for every possible string and should return a list of all dataframes in the map document.  Using an empty string like they did would not return any dataframes unless the dataframe was unnamed like you mentioned.

df = arcpy.mapping.ListDataFrames(mxd, "*")[0]

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Ian and Rhett, in this case the empty string works fine and will return all the data frames.  It isn't that an empty string is an Esri wildcard, like "*", an empty string is a Python wildcard of sorts.  When using find with Python strings or search with Python regular expressions, an empty string is found immediately so every data frame is returned because an empty string is found within every string representing data frame names.

Here is an example from an newly created MXD where I insert a new/second data frame using default values:

>>> mxd = arcpy.mapping.MapDocument("CURRENT")
>>> for df in arcpy.mapping.ListDataFrames(mxd, ""):
...     print df.name
...     
Layers
New Data Frame
>>> 
RhettZufelt
MVP Notable Contributor

I see the documentation says that works, but I have had issues in the past.

Would not let me get the dataframe unless I either hard coded the name, or used the "*".

That was in earlier version.  Maybe I can test again now that I'm running 10.2.1.

Thanks for the feedback,

R_

Just tested in 10.2.1 and the empty string seems to work fine now.

RandyBurton
MVP Alum

I have tried the OP's code, and it seems to work. It could be the visibility scale or one of the items Joshua Bixby‌ is asking about in his first post.

0 Kudos