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, "*") 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?