0

I am already able to add individual layers to ArcMap table of contents using the following codes:

# Adding the layer to the table of contents
mxd = arcpy.mapping.MapDocument("current")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
path = os.path.abspath(LayerName)
addLayer = arcpy.mapping.Layer(path)
arcpy.mapping.AddLayer(df, addLayer)
arcpy.RefreshActiveView()
arcpy.RefreshTOC()
del mxd, addLayer

However I can't figure out how I can load multiple layers at the same time. For example the following codes don't work and only only of the layers gets added:

# Adding mulitple Layers to the table of contents
mxd = arcpy.mapping.MapDocument("current")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
path = os.path.abspath(FirstLayer)
path2 = os.path.abspath(SecondLayer)
path3 = os.path.abspath(ThirdLayer)
.
.
.
addLayer = arcpy.mapping.Layer(path)
addLayer = arcpy.mapping.Layer(path2)
addLayer = arcpy.mapping.Layer(path3)
.
.
.
arcpy.mapping.AddLayer(df, addLayer)
arcpy.RefreshActiveView()
arcpy.RefreshTOC()
del mxd, addLayer

Any thought/ idea is much appreciated.

5

You change your addLayer variable twice before adding it to your data frame, so of course only the last one gets added. Use the AddLayer function after setting every addLayer variable:

addLayer = arcpy.mapping.Layer(path)
arcpy.mapping.AddLayer(df, addLayer)

addLayer = arcpy.mapping.Layer(path2)
arcpy.mapping.AddLayer(df, addLayer)

addLayer = arcpy.mapping.Layer(path3)
arcpy.mapping.AddLayer(df, addLayer)
.
.
.
arcpy.mapping.AddLayer(df, addLayer)
arcpy.RefreshActiveView()
arcpy.RefreshTOC()
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.