Add Feature Class to Current Pro Project Via Script Tool

1142
6
Jump to solution
09-20-2021 12:47 PM
JaredPilbeam2
MVP Regular Contributor

Hello,

I replied to this post where the user had the same issue I'm experiencing. Since it was a Pro question I decided to repost here in Python.

The problem I'm facing involves my custom script tool not adding a feature class to an open Pro project. I'm using Pro 2.8.

Working from a notebook, this successfully adds a feature class to the map.

 

import arcpy, os
data = r'path\to\BaseLayer_Processed_Elementary_08302021_clip'
arcpy.env.addOutputsToMap = True 

try:

arcpy.management.MakeFeatureLayer(data, data) 
    
except:
    print(arcpy.GetMessages())

 

 

But, according to the docs the above only creates a temp layer in the map. So, unless you save the map or export the layer to a feature class it won't be saved. So, I tried the below script:

 

import arcpy, os
data = r'path\to\BaseLayer_Processed_Elementary_08302021_clip'
arcpy.env.addOutputsToMap = True 

try:

    # Write the selected features to a new featureclass
    arcpy.CopyFeatures_management(data, os.path.basename(data))
    
except:
    print(arcpy.GetMessages())

 

 

Both scripts work to add a feature class to the map. However, when I convert to a script tool to run within pro it runs successfully, but nothing is added to the map.

Tool version of the script:

 

import arcpy, os

#input feature class from model. It will be added to the map.
data = arcpy.GetParameterAsText(0)
arcpy.env.addOutputsToMap = True
arcpy.AddMessage(f'adding to map: {data}') 

try:

    #Copies features from the input feature class or layer to a new feature class.
    arcpy.CopyFeatures_management(data, os.path.basename(data))
    
except:
    arcpy.AddMessage(arcpy.GetMessage())

 

 

Tool interface:

JaredPilbeam2_0-1632166144459.png

0 Kudos
1 Solution

Accepted Solutions
DonMorrison1
Occasional Contributor III

I do something similar with the code below to add a layer to the map that is currently open in ArcGIS Pro.  My code runs from a python toolbox - not sure if it applies to what your are doing

 

        aprx = arcpy.mp.ArcGISProject('CURRENT')
        current_map = aprx.activeMap
        current_map.addDataFromPath(<path to feature class>)

  

View solution in original post

6 Replies
DanPatterson
MVP Esteemed Contributor

add_results_to_map2.png

this is checked on as well?


... sort of retired...
0 Kudos
JaredPilbeam2
MVP Regular Contributor

Dan,

Didn't realize that was there. It's checked on, anyway.

0 Kudos
DonMorrison1
Occasional Contributor III

I do something similar with the code below to add a layer to the map that is currently open in ArcGIS Pro.  My code runs from a python toolbox - not sure if it applies to what your are doing

 

        aprx = arcpy.mp.ArcGISProject('CURRENT')
        current_map = aprx.activeMap
        current_map.addDataFromPath(<path to feature class>)

  

DanPatterson
MVP Esteemed Contributor

I think Don has the solution/work-around, I think notebooks rely on this approach rather than the checkbox for project defaults.  

I do vaguely remember data not being added to the map before, which I thought was resolved, but it does make sense since a toolbox tool really doesn't know what the active project or active map is, particularly for toolboxes that reside in a different path that the project folder.


... sort of retired...
0 Kudos
JaredPilbeam2
MVP Regular Contributor

That works. It makes sense that the script tool didn't know the map. Thanks for pointing that out. Nowhere did I come across .activeMap in the help pages.

0 Kudos
DanPatterson
MVP Esteemed Contributor

dir .... often uncovers the unincluded

proj = r"C:\arcpro_npg\npg\Project_npg\npGeom.aprx"
aprx = arcpy.mp.ArcGISProject(proj)

dir(aprx)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', 
 '__format__', '__from_scripting_arc_object__', '__ge__', '__getattribute__',
 '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__',
 '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__',
 '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__',
 '__weakref__', '_arc_object',
 'activeMap',
 'activeView',
 'dateSaved',
 'defaultGeodatabase',
 'defaultToolbox',
 'documentVersion',
 'filePath',
 'homeFolder',
 'importDocument',
 'listBrokenDataSources',
 'listColorRamps',
 'listLayouts',
 'listMaps',
 'listReports',
 'metadata',
 'save',
 'saveACopy',
 'updateConnectionProperties']

.... help on those properties and methods is quite limited though

print(aprx.save.__doc__)
ArcGISProject.save()

           Saves changes to an ArcGISProject ( .aprx ).

 


... sort of retired...
0 Kudos