5

Whenever I attempt to use AddLayer it gives me: Runtime error <type 'exceptions.AssertionError'>

This is what I've been using:

arcpy.mapping.AddLayer(MainDF, r"\\GEOGDATA\campusmapping\CampusWide\LayerFiles\CAD\Floor2\Casanova2", "BOTTOM")

Casanova2 is a .lyr file. I've also attempted to use this with a few other layer files. Every time it gives me an AssertionError.

Do you know what I'm doing wrong?

  • Have you tried a local path instead of a network path? – MathiasWestin Sep 19 '10 at 13:20
18

I figured out the problem. The second parameter needs to be a previously-defined layer, not a string that points to the layer.

addLayer = arcpy.mapping.Layer(r"\\GEOGDATA\campusmapping\CampusWide\LayerFiles\CAD\Floor2\Casanova2")
arcpy.mapping.AddLayer(MainDF, addLayer, "BOTTOM")

Works great.

3

Hard to say, but you could try following the sample in API documentation: AddLayer

import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Project\Project.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "New Data Frame")[0]
addLayer = arcpy.mapping.Layer(r"C:\Project\Data\Orthophoto.lyr")
arcpy.mapping.AddLayer(df, addLayer, "BOTTOM")
mxd.saveACopy(r"C:\Project\Project2.mxd")
del mxd, addLayer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.