<?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 Re: arcpy.mapping.ListLayers does not find layer in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/arcpy-mapping-listlayers-does-not-find-layer/m-p/702286#M54425</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks, your answer works and put me on the right track.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Saving and adding the new layer also seems to work (I ended up using this one as I'm creating archives of the rasters later on):&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;NewRaster.save(os.path.join(&amp;lt;PATH&amp;gt;,'NewRaster'))&lt;/P&gt;&lt;P&gt;arcpy.mapping.AddLayer(df,arcpy.mapping.Layer(os.path.join(&amp;lt;PATH&amp;gt;,'NewRaster')))&lt;/P&gt;&lt;P&gt;the_raster = arcpy.mapping.ListLayers(mxd,&lt;SPAN class="string"&gt;'NewRaster',df)[&lt;SPAN class="number"&gt;0&lt;/SPAN&gt;] &lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 01 Jun 2015 11:30:24 GMT</pubDate>
    <dc:creator>WillemMaetens</dc:creator>
    <dc:date>2015-06-01T11:30:24Z</dc:date>
    <item>
      <title>arcpy.mapping.ListLayers does not find layer</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-mapping-listlayers-does-not-find-layer/m-p/702283#M54422</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm doing some processing on raster images using the python module in ArcMap 10.3.0.4322. Therefore I first convert the rasters to numpy arrays, do the processing and convert them back to rasters. This all works fine, but when I try to get hold of the resulting raster layer for some further layout (application of symbology) using arcpy.mapping.ListLayers, the function returns an empty list, while the layer is clearly there.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This code reproduces the error:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import numpy as np
arcpy.CheckOutExtension("Spatial")
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd,'')[0]
# create random raster and get its properties
TestRaster = arcpy.sa.CreateRandomRaster("", "1", "0 0 250 250")
lowerLeft = arcpy.Point(TestRaster.extent.XMin,TestRaster.extent.YMin)
cellSize = TestRaster.meanCellWidth
# create array from raster, do some processing (not relevant here)
array = arcpy.RasterToNumPyArray(TestRaster,nodata_to_value=np.nan)
# turn array in to a raster again
NewRaster = arcpy.NumPyArrayToRaster(array,lowerLeft,cellSize,value_to_nodata=TestRaster.noDataValue)
# get hold of the raster for the application of symbology
the_raster = arcpy.mapping.ListLayers(mxd,'NewRaster',df)[0]&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Executing this code as a whole produces the error:&lt;/P&gt;&lt;P&gt;Runtime error &lt;/P&gt;&lt;P&gt;Traceback (most recent call last):&lt;/P&gt;&lt;P&gt;&amp;nbsp; File "&amp;lt;string&amp;gt;", line 13, in &amp;lt;module&amp;gt;&lt;/P&gt;&lt;P&gt;IndexError: list index out of range&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Nevertheless, the raster should be there: when the last line is re-executed at the prompt after the error is returned, the_raster gives &amp;lt;map layer u'NewRaster'&amp;gt; as expected, so this seems like a bug of some sort. Is there any way to avoid having to run this code in seperate code blocks at the prompt?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;cheers,&lt;/P&gt;&lt;P&gt;Willem&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 05:32:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-mapping-listlayers-does-not-find-layer/m-p/702283#M54422</guid>
      <dc:creator>WillemMaetens</dc:creator>
      <dc:date>2021-12-12T05:32:23Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.mapping.ListLayers does not find layer</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-mapping-listlayers-does-not-find-layer/m-p/702284#M54423</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I'm not sure if this is causing your problems, but i have had similar errors when my next line runs before the previous line has completed the process. &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 11 May 2015 11:51:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-mapping-listlayers-does-not-find-layer/m-p/702284#M54423</guid>
      <dc:creator>CarlSunderman</dc:creator>
      <dc:date>2015-05-11T11:51:43Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.mapping.ListLayers does not find layer</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-mapping-listlayers-does-not-find-layer/m-p/702285#M54424</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I think your code snippet pretty clearly shows there is a bug, but good luck getting Esri to fix it.&amp;nbsp; I don't mean to come off as cynical, but I have opened so many bugs with Esri over the years that I know luck and happenstance both play a strong role in what eventually gets addressed.&amp;nbsp; Soapbox aside, you should be able to workaround this bug by inserting the following line before line 14 of your code snippet:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code _jivemacro_uid_1431398388462406 jive_text_macro" data-renderedposition="92_8_912_16" jivemacro_uid="_1431398388462406"&gt;&lt;P&gt;arcpy.MakeRasterLayer_management(NewRaster, 'NewRaster')&lt;/P&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 12 May 2015 02:39:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-mapping-listlayers-does-not-find-layer/m-p/702285#M54424</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2015-05-12T02:39:55Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.mapping.ListLayers does not find layer</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-mapping-listlayers-does-not-find-layer/m-p/702286#M54425</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks, your answer works and put me on the right track.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Saving and adding the new layer also seems to work (I ended up using this one as I'm creating archives of the rasters later on):&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;NewRaster.save(os.path.join(&amp;lt;PATH&amp;gt;,'NewRaster'))&lt;/P&gt;&lt;P&gt;arcpy.mapping.AddLayer(df,arcpy.mapping.Layer(os.path.join(&amp;lt;PATH&amp;gt;,'NewRaster')))&lt;/P&gt;&lt;P&gt;the_raster = arcpy.mapping.ListLayers(mxd,&lt;SPAN class="string"&gt;'NewRaster',df)[&lt;SPAN class="number"&gt;0&lt;/SPAN&gt;] &lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 01 Jun 2015 11:30:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-mapping-listlayers-does-not-find-layer/m-p/702286#M54425</guid>
      <dc:creator>WillemMaetens</dc:creator>
      <dc:date>2015-06-01T11:30:24Z</dc:date>
    </item>
  </channel>
</rss>

