MakeFeatureLayer doesn't add result to MXD when run as tool

1461
3
Jump to solution
09-11-2012 03:25 PM
RobertMartin2
Occasional Contributor II
I've been having some issues with MakeFeatureLayer not adding its result to the MXD. I made a script with just the following lines to root out the problem:

fc = arcpy.GetParameterAsText(0) arcpy.MakeFeatureLayer_management(fc, arcpy.Describe(fc).name + "_test")


If I run these from the Python window the layer is added. But if I save them to a script and use that in a tool, it executes with no errors but won't add anything to the data frame. If I run ListLayers it isn't there either.

Does anyone know what I have to do to hunt down this layer?
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
FabianBlau
Occasional Contributor II
test_lyr = arcpy.Describe(fc).name + "_test" mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] # choose the correct DataFrame lyr = arcpy.mapping.Layer(test_lyr) # insert your layer-name arcpy.mapping.AddLayer(df, lyr, "TOP") # choose position in TOC arcpy.RefreshTOC()


search the help for more examples:
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00s300000025000000

View solution in original post

0 Kudos
3 Replies
FabianBlau
Occasional Contributor II
test_lyr = arcpy.Describe(fc).name + "_test" mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] # choose the correct DataFrame lyr = arcpy.mapping.Layer(test_lyr) # insert your layer-name arcpy.mapping.AddLayer(df, lyr, "TOP") # choose position in TOC arcpy.RefreshTOC()


search the help for more examples:
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00s300000025000000
0 Kudos
MathewCoyle
Frequent Contributor
You should be able to just check this option in the attached screenshot under Geoprocessing Options.
Add results of geoprocessing operations to the display.
0 Kudos
RobertMartin2
Occasional Contributor II
I dropped in Fabian's code block and it worked like a charm. Now if I could just figure out why it takes six lines of code to do something that's automatic in the Python window... 🙂

Mathew, I checked my settings and that box was checked all along. Maybe it doesn't apply to scripts run as tools?


Thanks guys!
0 Kudos