Add layer to map with Python Toolbox (.pyt)

3094
2
Jump to solution
06-07-2013 12:01 PM
BillMiller
New Contributor
I'm trying to add a layer to the current MXD using an arcpy toolbox (.pyt).

When I execute the program it draws the entire layer then immediately dissappears from the TOC which basically returns to the old version of the MXD. Other times it does not draw the layer but still there is not an error message.

Here is the code I'm using for the execute routine as a toolbox:


def __init__(self):         self.label = "Add Layer"         self.description = "Adds a layer to ArcMap"         self.canRunInBackground = False ######################## def execute(self, parameters, messages):         arcpy.MakeFeatureLayer_management("C:\BILL\CB_TEMP\cb.shp", "LAYER")         mxd = arcpy.mapping.MapDocument("CURRENT")         df  = arcpy.mapping.ListDataFrames(mxd, "*")[0]         addLayer = arcpy.mapping.Layer("LAYER")         arcpy.mapping.AddLayer(df, addLayer, "TOP")           arcpy.RefreshTOC()         arcpy.RefreshActiveView()         # del mxd

BTW: This problem was discussed in 10.0 and they said it was fixed in a SP. I think the difference might be that I'm using a toolbox ".pyt" to add the layer.
http://forums.arcgis.com/threads/8331-AddLayer-only-temporarily-adds-the-layer-to-map?p=57150#post57...
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JasonScheirer
Regular Contributor II
Set up a new Derived Output parameter of type Feature Layer. Set its value to "LAYER" (or whatever you name it) in the execute method. This will notify the GP framework that this is indeed a layer you want to keep around and not a layer of intermediate data.

You'll also get the benefit of not having to do any extra arcpy.mapping scripting.

arcpy.MakeFeatureLayer_management(r"C:\BILL\CB_TEMP\cb.shp", "LAYER") arcpy.SetParameterAsText(1, "LAYER")

View solution in original post

2 Replies
JasonScheirer
Regular Contributor II
Set up a new Derived Output parameter of type Feature Layer. Set its value to "LAYER" (or whatever you name it) in the execute method. This will notify the GP framework that this is indeed a layer you want to keep around and not a layer of intermediate data.

You'll also get the benefit of not having to do any extra arcpy.mapping scripting.

arcpy.MakeFeatureLayer_management(r"C:\BILL\CB_TEMP\cb.shp", "LAYER") arcpy.SetParameterAsText(1, "LAYER")

View solution in original post

Heidi_Kristenson
New Contributor III

I've been trying to figure out how to add outputs to the display for Python Toolbox scripts for an embarrassingly long time. I'd pretty much given up when I came upon this exchange. I'm dealing with rasters, so substituted arcpy.MakeRasterLayer_management instead, and it worked great! A quick and easy way to get the job done, seeing as the arcpy.env.addOutputsToMap doesn't function when run from within a tool. 

For those who are looking to do the same thing, add an additional parameter for the Derived output:

# Eighth parameter: output layer to add to project
outlayer = arcpy.Parameter(
name = "outlayer",
displayName = "Derived output for final product raster",
datatype = "GPRasterLayer",
parameterType = "Derived",
direction = "Output")

 

Then once you've generated the output raster in your code, add it to the display, referencing the index number for the derived parameter in the arcpy.SetParameterAsText function (in this example it was the 8th parameter, so has an index value of 7). Note that you cannot have the same name for the input raster and the display raster; I just removed the .tif extension from my raster filename to differentiate the two while still making it clear what dataset was added. In this example, the outpath variable is the full path to the newly-generated raster that I want to add to the map.

dispname = os.path.splitext(outname)[0]
arcpy.MakeRasterLayer_management(outpath, dispname)
arcpy.SetParameterAsText(7, dispname)
arcpy.AddMessage("Added RGB raster layer to map display.")