Select to view content in your preferred language

addLayer error

561
2
Jump to solution
08-29-2023 11:32 AM
AnjeliDubey
New Contributor II

I am trying to add a layer to my current map with this script using a script tool, however whenever I run the script tool it says addLayer failed.

What in my script can be fixed to get addLayer to work?

# import the library needed to add layer
import arcpy
arcpy.env.overwriteOutput = True

# access current project
aprx = arcpy.mp.ArcGISProject('CURRENT')

# access current map
m = aprx.activeMap
 
try:
    # Create parameter for layer file you want to add
    lf = arcpy.GetParameterAsText(0)

    # Add layer to active map
    m.addLayer(lf)

    #Add message if  layer added
    arcpy.AddMessage("Layer added Successfully!")

except Exception as e:
    arcpy.AddError(f"Error occured: {str(e)}")
0 Kudos
1 Solution

Accepted Solutions
AlfredBaldenweck
MVP Regular Contributor

Add layer takes a layer object and you appear to be feeding it a file path.

Create a layer ( should be arcpy.mp.Layer(), I think) then feed that into addlayer() instead

 

https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/layer-class.htm

View solution in original post

2 Replies
AlfredBaldenweck
MVP Regular Contributor

Add layer takes a layer object and you appear to be feeding it a file path.

Create a layer ( should be arcpy.mp.Layer(), I think) then feed that into addlayer() instead

 

https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/layer-class.htm

AnjeliDubey
New Contributor II

That worked, thank you!

0 Kudos