# Set enviroment arcpy.env.workspace arcpy.env.scratchGDB arcpy.env.scratchFolder
# Add layers to the map mxd = arcpy.mapping.MapDocument(r"C:\Users\mymap.mxd") # List data frame and layers frame = arcpy.mapping.ListDataFrames(mxd,'Dataframe')[0] refFeatureLayer = arcpy.mapping.ListLayers(mxd,'*New_Layer*',frame)[0] # create a new layer from an existing featureclass travel_route_1 = "Travel_Routes" tempLayer = arcpy.MakeFeatureLayer_management(travel_route_1,'my test layer') # Save layer to disk arcpy.SaveToLayerFile_management (tempLayer, r'C:\Users\test layer.lyr') # List data frame and layers - just to refresh the list, shouldn't need to do this. frame = arcpy.mapping.ListDataFrames(mxd,'Dataframe')[0] refFeatureLayer = arcpy.mapping.ListLayers(mxd,'*test*',frame)[0] # Get the layer addLayer = arcpy.mapping.Layer(r'C:\Users\test layer.lyr') # Add the layer to the dataframe arcpy.mapping.AddLayer(frame, addLayer, "BOTTOM") # List data frame and layers - just to refresh the list, shouldn't need to do this. frame = arcpy.mapping.ListDataFrames(mxd,'Dataframe')[0] refFeatureLayer = arcpy.mapping.ListLayers(mxd,'*test*',frame)[0] # Finally move the layer movelayer = arcpy.mapping.ListLayers(mxd,'*my*',frame)[0] arcpy.mapping.MoveLayer(frame,refFeatureLayer,movelayer,"AFTER") arcpy.RefreshTOC() arcpy.RefreshActiveView()
Solved! Go to Solution.
From Jeff Barrette
The result of MakeFeatureLayer is a string object, not a layer object. AddLayer requires a layer object, resultlayer.getOutput(0) - this returns the layer object.
This is one of the subtle differences between GP/arcpy (released at 9x) and arcpy.mapping (released at 10x). Many arcpy functions return strings and arcpy functions translate them appropriately. Arcpy.mapping is more object oriented in design and requires more than just string objects.
frame = arcpy.mapping.ListDataFrames(mxd,'Dataframe')[0] tempLayer = arcpy.MakeFeatureLayer_management(dissolved_feature,'{0}\Viewshed_{1}'.format(scratchfdr,timestamp)) arcpy.mapping.AddLayer(frame, tempLayer.getOutput(0), "AUTO_ARRANGE")
>>> df = arcpy.mapping.ListDataFrames(mxd, "MapSAR")[0] >>> for d in df: ... print d
From Jeff Barrette
The result of MakeFeatureLayer is a string object, not a layer object. AddLayer requires a layer object, resultlayer.getOutput(0) - this returns the layer object.
This is one of the subtle differences between GP/arcpy (released at 9x) and arcpy.mapping (released at 10x). Many arcpy functions return strings and arcpy functions translate them appropriately. Arcpy.mapping is more object oriented in design and requires more than just string objects.
frame = arcpy.mapping.ListDataFrames(mxd,'Dataframe')[0] tempLayer = arcpy.MakeFeatureLayer_management(dissolved_feature,'{0}\Viewshed_{1}'.format(scratchfdr,timestamp)) arcpy.mapping.AddLayer(frame, tempLayer.getOutput(0), "AUTO_ARRANGE")
I realize this is an old thread, but do either of you have a suggestion or fix for when the above script recommended by Jeff Barrette works at the command line but not when run within a tool? I started a new thread here for reference.
Madeleine - are you running the tool in the background? If you are doing things to the map document you need to always check the script property "always run in foreground" or turn background processing off.
Curtis -- yes, I've checked the "Always run in foreground" box within the general properties tab for the tool. I should mention that the tool works when the the shapefile is constructed within the python code (from a numpy array) and then added to the map, but it doesn't work when the shapefile already exists in a folder and is then added to the map (see errors from the tool here).
Madeline,
For some reason I can’t respond to the thread.
I don’t get the error when I run the following from a script tool within ArcMap.
import arcpy
precipLayer = arcpy.MakeFeatureLayer_management(r"D:\Active\ArcPY\10x\ScrumWorks\Data\Drive_A\basin_utmcopy.shp")
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
arcpy.mapping.AddLayer(df, precipLayer.getOutput(0), "TOP") #NO ERROR
#arcpy.mapping.AddLayer(df, precipLayer, "TOP") #GOT ERROR BUT EXPECTED
What version of the software are you using?
Have you tried using a layer file that references the shapefile on disk?
Replace:
precipLayer = arcpy.MakeFeatureLayer_management(r"D:\Active\ArcPY\10x\ScrumWorks\Data\Drive_A\basin_utmcopy.shp")
with:
precipLayer = arcpy.mapping.Layer(r"D:\Active\ArcPY\10x\ScrumWorks\Data\Drive_A\basin_utmcopy.shp")
Jeff