Adding feature class to map script

227
8
11-16-2018 03:43 PM
New Contributor

Hi,

 I created a python script tool that creates a Thiessen Polygons. The tool runs fine. But how do I create it so that it automatically adds a new layer to the map? Right now, it only creates it in the geodatabase. I would have to manually drag and drop it into the map to get it to appear.

Thank you!

Jonah

Reply
0 Kudos
8 Replies
MVP Esteemed Contributor

If the script is attached to a tool in arctoolbox, then the results should be added to the display (I assume that your featureclass is an output parameter).

If you are running the script as a standalone script, it probably won't automatically since there may be a disconnect between the scripting environment and the project... obviously if Pro is closed, it won't add it at all, which is where the arcpy.mp (aka mapping module) comes in and using 'CURRENT' for the aprx.

You can try 

Make Feature Layer—Data Management toolbox | ArcGIS Desktop 

to see if that helps.

But the surefire way is to make your script run from a tool in a toolbox in ArcToolbox

MVP Esteemed Contributor

This is an oldie but should get your started... just the simple toolbox incarnation, but easy to follow

/blogs/dan_patterson/2016/05/19/toolbox-creation-in-arcgis-pro 

New Contributor

Hi Dan,

 Thank you for the response and resources. I believe my script is attached to the toolbox:

I tried "arcpy.MakeFeatureLayer_management". But there is no difference when I run the tool. However, if I run the same script through the python window, two of the same layers get displayed. One from analysis.CreateThiessenPolygons and the other from MakeFeatureLayer_management.

Is there a reason for this? The same script runs in the python window but doesn't display through the tool.

Reply
0 Kudos
MVP Esteemed Contributor

check your parameter list in the tool.  The your result of the process needs to be an 'output'.  maybe a screen grab of the parameters

Reply
0 Kudos
New Contributor

Thank you. This is what I have:

Reply
0 Kudos
MVP Esteemed Contributor

hmmmm 

number, Label/Name Direction Data Type

0 Input_Features   Input   Feature_layer

1 Fields_to_copy   Input    (did you check 'Dependency (input_features)' so you can chose from a list of fields?

2 Output_Feature  Output  I always try Feature Class or Feature Layer when one or the other doesn't work.  I always put output as the last parameter and it is the only output parameter.

New Contributor

I tried both Feature Class or Feature Layer. It didn't seem to make any difference. I was modelling the parameters after the default Thiessen Polygon tool in Pro.

Do you think it is a parameter setting that is preventing the layer from displaying automatically and not the script?

Reply
0 Kudos
MVP Esteemed Contributor

in Pro results are supposed to be automatically added to the display.  It is the 2 'output' types which I don't think is working

Reply
0 Kudos