Select to view content in your preferred language

Making Layers invisible via ArcPy 3+ in ArcGIS Pro 3+

1808
2
Jump to solution
07-28-2023 02:34 AM
lbd_bayern
Occasional Contributor

Hello everyone,

I am trying to change the visibility of some layers via an ArcPy script.

It seems like the property visible of the layer object is the property to go. I found multiple articles on how it works in ArcMap, for example this one: https://support.esri.com/en-us/knowledge-base/how-to-toggle-the-visibility-of-a-specific-sublayer-of....

Within ArcGIS Pro 3+ and with ArcPy 3+, I am able to set the property to either True or False for a layer without problems.

However, it seems like you actually need to somehow refresh the view in order to have some visual effect on the map rendering and also in the Contents pane.

For older versions of ArcPy/with ArcMap, it seems like calling the following two methods was sufficient:

arcpy.RefreshTOC()
arcpy.RefreshActiveView()

However, when I call these methods now on arcpy, I receive an error: "AttributeError: module 'arcpy' has not attribute 'RefreshTOC'".  Hence, it seems like these two methods do no longer exist in ArcPy 3+.

Does anybody have an idea how I can change the visibility of a layer such that it actually also is displayed accordingly in the map rendering and in the Contents pane?

0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable

Not sure where you are running the code or what your layer name is, but you do this through the mapping module.

aprx = arcpy.mp.ArcGISProject(r'path to aprx') # Use 'current' if executing in Pro Python window.

m = aprx.listMaps("Map")[0]

lyrName = 'layer_name'
lyr = m.listLayers(lyrName)[0]
lyr.visible = False
arcpy.AddMessage(f"Set layer-visibility for {lyr.name} to str(lyr.visible)")

# aprx.save() <- might need this if ran from outside of Pro's env, but the aprx will need to be closed or you will get an error.

 

View solution in original post

2 Replies
by Anonymous User
Not applicable

Not sure where you are running the code or what your layer name is, but you do this through the mapping module.

aprx = arcpy.mp.ArcGISProject(r'path to aprx') # Use 'current' if executing in Pro Python window.

m = aprx.listMaps("Map")[0]

lyrName = 'layer_name'
lyr = m.listLayers(lyrName)[0]
lyr.visible = False
arcpy.AddMessage(f"Set layer-visibility for {lyr.name} to str(lyr.visible)")

# aprx.save() <- might need this if ran from outside of Pro's env, but the aprx will need to be closed or you will get an error.

 

lbd_bayern
Occasional Contributor

Hi @Anonymous User,

I executed the script in the ArcGIS Pro Python window and was actually getting the reference to the aprx via path. After changing it to

aprx = arcpy.mp.ArcGISProject("CURRENT")

it now works as expected.

Thank you very much!

0 Kudos