arcpy.CopyFeatures_management doesn't place copied layer into Contents pane

328
1
08-12-2022 07:28 AM
chenry_AIR
New Contributor

Hello,

I've updated Pro after a long time of ignoring updates. Now I'm having some issues with code that used to work. It happens when I use code that creates a new layer (e.g., arcpy.CopyFeatures_management, arcpy.management.Merge). 

In the past, CopyFeatures and Merge would result in a layer in the Contents pane. Now, it only sometimes does it. The layers are being created and are in the project's geodatabase, regardless of whether they ended up in the contents pane or not. 

Here's an example of how we're using code and getting an error:


arcpy.CopyFeatures_management('temp','Layer_Unfilled')

lyr = m.listLayers('Layer_Unfilled')[0] <---Error Here [IndexError: list index out of range]

I'm getting an error because the copied layer isn't in the Contents pane. We often use loops to copy layers multiple times. It tends to work for a random amount and then stops putting them in the Contents completely. 

 

Any help would be appreciated!

0 Kudos
1 Reply
by Anonymous User
Not applicable

This could be a couple things and reading that it does it randomly, it looks like a TOC cache not getting updated, a UI element not getting the trigger to refresh, threading failures, etc... (aka, bugs)

You could do some logic to check if the layer is added, and add it of not. This isn't tested and you may need to do some changes to the rest of your code or find the right add method for your data for it to work.

fc_out = arcpy.CopyFeatures_management('temp', 'Layer_Unfilled')

if not m.listLayers('Layer_Unfilled')[0]:
    m.addLayer(fc_out , "TOP")
    # or add layer from path if the output isnt a Layer
    m.addDataFromPath(fc_out)