<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Arcpy ArcGIS Pro Syntax to Determine if a Layer is Visible in the Table of Contents in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/arcpy-arcgis-pro-syntax-to-determine-if-a-layer-is/m-p/1294198#M67745</link>
    <description>&lt;P&gt;I am trying to write a standalone script for ArcGIS Pro, so that it checks if a layer is visible in the table of contents before running any geo processing functions. I have tried using the following, but it ends up just printing all of the layers in the TOC even if they are turned off, and I get an error stating:&amp;nbsp;&lt;/P&gt;&lt;P&gt;"AttributeError: The attribute 'visible' is not supported on this instance of Layer."&lt;/P&gt;&lt;P&gt;I have tried running it in the Python Window within ArcPro as well, with same result.&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# Import ArcPy site-package
import arcpy


# the open records request Pro Project
aprx = arcpy.mp.ArcGISProject(
    r"\\san_marcos\arcfile\Department Projects and File Geodatabases\Public Services\Transportation\GSipes\ProProjects\OpenRecordsRequests\OpenRecordsRequests.aprx")

# list of layers in the project
myMap = aprx.listMaps()[0]


for layer in myMap.listLayers():
    if layer.supports("VISIBLE") == True:
       if layer.visible == True:
           print(layer.name)


print('I am done listing layers')&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 30 May 2023 18:59:54 GMT</pubDate>
    <dc:creator>Gene_Sipes</dc:creator>
    <dc:date>2023-05-30T18:59:54Z</dc:date>
    <item>
      <title>Arcpy ArcGIS Pro Syntax to Determine if a Layer is Visible in the Table of Contents</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-arcgis-pro-syntax-to-determine-if-a-layer-is/m-p/1294198#M67745</link>
      <description>&lt;P&gt;I am trying to write a standalone script for ArcGIS Pro, so that it checks if a layer is visible in the table of contents before running any geo processing functions. I have tried using the following, but it ends up just printing all of the layers in the TOC even if they are turned off, and I get an error stating:&amp;nbsp;&lt;/P&gt;&lt;P&gt;"AttributeError: The attribute 'visible' is not supported on this instance of Layer."&lt;/P&gt;&lt;P&gt;I have tried running it in the Python Window within ArcPro as well, with same result.&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# Import ArcPy site-package
import arcpy


# the open records request Pro Project
aprx = arcpy.mp.ArcGISProject(
    r"\\san_marcos\arcfile\Department Projects and File Geodatabases\Public Services\Transportation\GSipes\ProProjects\OpenRecordsRequests\OpenRecordsRequests.aprx")

# list of layers in the project
myMap = aprx.listMaps()[0]


for layer in myMap.listLayers():
    if layer.supports("VISIBLE") == True:
       if layer.visible == True:
           print(layer.name)


print('I am done listing layers')&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 30 May 2023 18:59:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-arcgis-pro-syntax-to-determine-if-a-layer-is/m-p/1294198#M67745</guid>
      <dc:creator>Gene_Sipes</dc:creator>
      <dc:date>2023-05-30T18:59:54Z</dc:date>
    </item>
    <item>
      <title>Re: Arcpy ArcGIS Pro Syntax to Determine if a Layer is Visible in the Table of Contents</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-arcgis-pro-syntax-to-determine-if-a-layer-is/m-p/1294206#M67746</link>
      <description>&lt;P&gt;.supports is a bit flakey I think.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# Import ArcPy site-package
import arcpy


# the open records request Pro Project
aprx = arcpy.mp.ArcGISProject(
    r"\\san_marcos\arcfile\Department Projects and File Geodatabases\Public Services\Transportation\GSipes\ProProjects\OpenRecordsRequests\OpenRecordsRequests.aprx")

# list of layers in the project
myMap = aprx.listMaps()[0]

for layer in myMap.listLayers():
    try:
       if layer.visible:
           print(layer.name)

    except:
        pass


print('I am done listing layers')&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 30 May 2023 19:22:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-arcgis-pro-syntax-to-determine-if-a-layer-is/m-p/1294206#M67746</guid>
      <dc:creator>DavidPike</dc:creator>
      <dc:date>2023-05-30T19:22:39Z</dc:date>
    </item>
    <item>
      <title>Re: Arcpy ArcGIS Pro Syntax to Determine if a Layer is Visible in the Table of Contents</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-arcgis-pro-syntax-to-determine-if-a-layer-is/m-p/1294212#M67747</link>
      <description>&lt;P&gt;Thanks... well that kind of worked. It at least only prints out the name of the layers that are visible, but I still get a nasty little error.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Traceback (most recent call last):&lt;BR /&gt;File "c:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\arcobjects\_base.py", line 90, in _get&lt;BR /&gt;return convertArcObjectToPythonObject(getattr(self._arc_object, attr_name))&lt;BR /&gt;AttributeError&lt;/P&gt;&lt;P&gt;During handling of the above exception, another exception occurred:&lt;/P&gt;&lt;P&gt;Traceback (most recent call last):&lt;BR /&gt;File "\\san_marcos\files\DeptShares\Public Services\Transportation\GIS\Scripts\Python\OpenRecordsRequestReWrite.py", line 15, in &amp;lt;module&amp;gt;&lt;BR /&gt;if layer.visible:&lt;BR /&gt;File "c:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\arcobjects\_base.py", line 93, in _get&lt;BR /&gt;raise AttributeError(&lt;BR /&gt;AttributeError: The attribute 'visible' is not supported on this instance of Layer.&lt;/P&gt;&lt;P&gt;During handling of the above exception, another exception occurred:&lt;/P&gt;&lt;P&gt;Traceback (most recent call last):&lt;BR /&gt;File "c:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\arcobjects\_base.py", line 90, in _get&lt;BR /&gt;return convertArcObjectToPythonObject(getattr(self._arc_object, attr_name))&lt;BR /&gt;AttributeError&lt;/P&gt;&lt;P&gt;During handling of the above exception, another exception occurred:&lt;/P&gt;&lt;P&gt;Traceback (most recent call last):&lt;BR /&gt;File "\\san_marcos\files\DeptShares\Public Services\Transportation\GIS\Scripts\Python\OpenRecordsRequestReWrite.py", line 18, in &amp;lt;module&amp;gt;&lt;BR /&gt;print("error - " + layer.name)&lt;BR /&gt;File "c:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\arcobjects\_base.py", line 93, in _get&lt;BR /&gt;raise AttributeError(&lt;BR /&gt;AttributeError: The attribute 'name' is not supported on this instance of Layer.&lt;/P&gt;</description>
      <pubDate>Tue, 30 May 2023 19:15:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-arcgis-pro-syntax-to-determine-if-a-layer-is/m-p/1294212#M67747</guid>
      <dc:creator>Gene_Sipes</dc:creator>
      <dc:date>2023-05-30T19:15:38Z</dc:date>
    </item>
    <item>
      <title>Re: Arcpy ArcGIS Pro Syntax to Determine if a Layer is Visible in the Table of Contents</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-arcgis-pro-syntax-to-determine-if-a-layer-is/m-p/1294217#M67748</link>
      <description>&lt;P&gt;ah ok, I'll just pass it.&amp;nbsp; A bit hacky but if it's not production code and it works..&amp;nbsp; Have edited the previous code above.&lt;/P&gt;</description>
      <pubDate>Tue, 30 May 2023 19:24:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-arcgis-pro-syntax-to-determine-if-a-layer-is/m-p/1294217#M67748</guid>
      <dc:creator>DavidPike</dc:creator>
      <dc:date>2023-05-30T19:24:03Z</dc:date>
    </item>
    <item>
      <title>Re: Arcpy ArcGIS Pro Syntax to Determine if a Layer is Visible in the Table of Contents</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-arcgis-pro-syntax-to-determine-if-a-layer-is/m-p/1294489#M67758</link>
      <description>&lt;P&gt;I really can't understand why my second print statement never runs. I am slowly building out my code to step along the way. Here I create a feature layer and select records from it that intersect my selecting polygon, but the last print statement:&amp;nbsp;print('just finished selecting features within clip feature'), never runs. Here is the output from the code below:&lt;BR /&gt;&lt;BR /&gt;about to evaluate if layer.visible is true&lt;BR /&gt;about to evaluate if layer.visible is true&lt;BR /&gt;about to evaluate if layer.visible is true&lt;BR /&gt;Layer Centerline is visible.&lt;BR /&gt;about to evaluate if layer.visible is true&lt;BR /&gt;about to evaluate if layer.visible is true&lt;BR /&gt;about to evaluate if layer.visible is true&lt;BR /&gt;about to evaluate if layer.visible is true&lt;BR /&gt;I am done clipping layers to AOI&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy
import os


# the open records request Pro Project
aprx = arcpy.mp.ArcGISProject(
    r"\\san_marcos\arcfile\Department Projects and File Geodatabases\Public Services\Transportation\GSipes\ProProjects\OpenRecordsRequests\OpenRecordsRequests.aprx")

# list of layers in the project
myMap = aprx.listMaps()[0]

# Set the clip featureclass
clipFeatures = r"\\san_marcos\arcfile\Department Projects and File Geodatabases\Public Services\Transportation\GSipes\ProProjects\OpenRecordsRequests\OpenRecordsRequests.gdb\SelectionPoly"

# Set the directory path where the clipped shapefiles will be stored
path = r"C:\Users\sipes_gene\Documents\OpenRecords"

# script will ask user to enter new folder name, folder will be place in path
outFolder = input('Enter your folder name :')

# join the path to the new folder name
outWorkspace = os.path.join(path, outFolder)

# Set the XY tolerance of the clip function
clusterTolerance = .000378

# create the directory
os.mkdir(outWorkspace)


for layer in myMap.listLayers():
    try:
        print('about to evaluate if layer.visible is true')
        if layer.visible:
            print('Layer {} is visible.'.format(layer))
            featureClassName = arcpy.ValidateTableName(layer, outWorkspace)
            outFeatureClass = os.path.join(outWorkspace, featureClassName)
            arcpy.MakeFeatureLayer_management(layer, layer)
            arcpy.SelectLayerByLocation_management(
                layer, "within", clipFeatures)
            print('just finished selecting features within clip feature')

    except:
        pass

print('I am done clipping layers to AOI')&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 31 May 2023 14:21:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-arcgis-pro-syntax-to-determine-if-a-layer-is/m-p/1294489#M67758</guid>
      <dc:creator>Gene_Sipes</dc:creator>
      <dc:date>2023-05-31T14:21:31Z</dc:date>
    </item>
    <item>
      <title>Re: Arcpy ArcGIS Pro Syntax to Determine if a Layer is Visible in the Table of Contents</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-arcgis-pro-syntax-to-determine-if-a-layer-is/m-p/1294499#M67760</link>
      <description>&lt;P&gt;Ok, made some progress, using {layer} as both the input and output of MakeFeatureLayer seems to be the problem.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 31 May 2023 14:33:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-arcgis-pro-syntax-to-determine-if-a-layer-is/m-p/1294499#M67760</guid>
      <dc:creator>Gene_Sipes</dc:creator>
      <dc:date>2023-05-31T14:33:23Z</dc:date>
    </item>
    <item>
      <title>Re: Arcpy ArcGIS Pro Syntax to Determine if a Layer is Visible in the Table of Contents</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-arcgis-pro-syntax-to-determine-if-a-layer-is/m-p/1294511#M67762</link>
      <description>&lt;P&gt;Alright, got it to work! Edge (ChatGPT) is awesome. Here is my final code, just because. I also realize there is a tool called Extract Data that can accomplish the same thing, but this was more of a python challenge for me than anything. Cheers.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# Import ArcPy site-package and os modules
import arcpy
import os


# the open records request Pro Project
aprx = arcpy.mp.ArcGISProject(
    r"\\san_marcos\arcfile\Department Projects and File Geodatabases\Public Services\Transportation\GSipes\ProProjects\OpenRecordsRequests\OpenRecordsRequests.aprx")

# list of layers in the project
myMap = aprx.listMaps()[0]

# Set the clip featureclass
clipFeatures = r"\\san_marcos\arcfile\Department Projects and File Geodatabases\Public Services\Transportation\GSipes\ProProjects\OpenRecordsRequests\OpenRecordsRequests.gdb\SelectionPoly"

# Set the directory path where the clipped shapefiles will be stored
path = r"C:\Users\sipes_gene\Documents\OpenRecords"

# script will ask user to enter new folder name, folder will be place in path
outFolder = input('Enter your folder name :')

# join the path to the new folder name
outWorkspace = os.path.join(path, outFolder)

# Set the XY tolerance of the clip function
clusterTolerance = .000378

# create the directory
os.mkdir(outWorkspace)


for layer in myMap.listLayers():
    try:
        # check if layer is visible in TOC, if true evaluate against selection polygon
        if layer.visible:
            print('Layer {} is visible.'.format(layer))
            # validate feature class names
            featureClassName = arcpy.ValidateTableName(layer, outWorkspace)
            outFeatureClass = os.path.join(outWorkspace, featureClassName)
            # create a feature layer from layer on disk
            featureLayer = arcpy.MakeFeatureLayer_management(
                layer, "{}_fl".format(layer))
            # evaluate feature layer against selecting polygon
            selectedFeatureLayer = arcpy.SelectLayerByLocation_management(
                featureLayer, "intersect", clipFeatures)
            # get count of selected features for print statement
            count = int(arcpy.GetCount_management(selectedFeatureLayer)[0])
            if count == 0:
                print("%s features do not exist within AOI" % layer)
            else:
                # Clip each feature class in the list with the clip feature class.
                # looks for clip feature so as not to clip it.
                if selectedFeatureLayer != os.path.basename(clipFeatures):
                    arcpy.Clip_analysis(
                        selectedFeatureLayer, clipFeatures, outFeatureClass, clusterTolerance)
                    print("{} {} features clipped to AOI".format(count, layer))
                    print("All is well in the Universe, good bye")

    except Exception as e:
        print(f"An error occurred: {e}")

print('I am done clipping layers to AOI')&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 31 May 2023 15:01:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-arcgis-pro-syntax-to-determine-if-a-layer-is/m-p/1294511#M67762</guid>
      <dc:creator>Gene_Sipes</dc:creator>
      <dc:date>2023-05-31T15:01:43Z</dc:date>
    </item>
  </channel>
</rss>

