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:
Solved! Go to Solution.
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>)
this is checked on as well?
Dan,
Didn't realize that was there. It's checked on, anyway.
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>)
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.
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.
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 ).