13

I've been battling with adding layers to the TOC for some time now. I have managed to single layers added but need to loop through a folder and add all layers. Shapefiles I cannot get to add at all. Only layer files. Below is the code of where I'm at:

for layer in shp_List:
mxd = arcpy.mapping.MapDocument("CURRENT")
dataFrame = arcpy.mapping.ListDataFrames(mxd, "*")[0]

outlayer = lyr
layerfile = os.path.join(base_Folder, lyr + ".lyr")

arcpy.MakeFeatureLayer_management(layer, outlayer)
arcpy.SaveToLayerFile_management(outlayer, layerfile, "ABSOLUTE")
addlayer = arcpy.mapping.Layer(layerfile)
arcpy.mapping.AddLayer(dataFrame, addlayer, "BOTTOM")



#addLayer = arcpy.mapping.Layer(layer)
#arcpy.mapping.AddLayer(dataFrame, addLayer, "BOTTOM")
#arcpy.RefreshTOC()
#arcpy.RefreshActiveView()
del addlayer, mxd

The above code will add each layer to the TOC, then infuriatingly removes it and adds the next one. When the whole script finishes there is nothing left in the TOC. I've tried adding this script to a model and creating a derived output parameter which is then added as a model parameter with "add to display" checked. I've also checked the geoprocessing options to make sure the box to add to display is checked. I'm running Arc 10 (no service packs). Can anyone help?

1 Answer 1

11

You need to create the MapDocument and DataFrame objects outside of the loop that runs through your layers. Otherwise you are starting with the original map each time.

so i will correct in this code:

mxd = arcpy.mapping.MapDocument("CURRENT")
dataFrame = arcpy.mapping.ListDataFrames(mxd, "*")[0]

for layer in shp_List:

    outlayer = layer + "_lyr" 
    layerfile = os.path.join(base_Folder, layer + ".lyr")

    arcpy.MakeFeatureLayer_management(layer, outlayer)
    arcpy.SaveToLayerFile_management(outlayer, layerfile, "ABSOLUTE")
    addlayer = arcpy.mapping.Layer(layerfile)
    arcpy.mapping.AddLayer(dataFrame, addlayer, "BOTTOM")



    #addLayer = arcpy.mapping.Layer(layer)
    #arcpy.mapping.AddLayer(dataFrame, addLayer, "BOTTOM")
arcpy.RefreshTOC()
arcpy.RefreshActiveView()
    #del addlayer, mxd

To loop in a list of mxd files in the same folder of the script, you can do it like this :

for mxd_name in mxd_names:
    mxd = arcpy.mapping.MapDocument(os.path.dirname(os.path.realpath(__file__))+"\\"+mxd_name+".mxd")
   dataFrame = arcpy.mapping.ListDataFrames(mxd, "*")[0]
5
  • 2
    I was looking for @geogeek's remarks but they appear to be absent. In any event, as his corrected code shows, you need to create the MapDocument and DataFrame objects outside of the loop that runs through your layers. Otherwise you are starting with the original map each time.
    – PolyGeo
    Oct 13, 2012 at 22:58
  • 1
    Thanks for the clarification PolyGeo. I've implemented the changes. However, now just the last file in the list stays in the TOC. All the others appear then disappear. Flummoxed. :) Oct 14, 2012 at 0:16
  • 1
    Got it working! All in the indentation right? The layer names coming out are still a mees but that's just aesthetics. Thanks so much for your help peeps! Oct 14, 2012 at 4:07
  • 1
    geogeek, how do it for several mxd's?
    – newGIS
    Jan 11, 2015 at 12:50
  • 1
    i've made an answer in my edit
    – geogeek
    Jan 12, 2015 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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