Solved! Go to Solution.
Everyone, i'm on travel and will not be back at it until this Monday. I'll reply with my final resolution then. Please stand by for more.
Im interested in what you are trying to achieve with the code here:
new_layer = arcpy.mapping.Layer(new_layer_name)
As you are simply passing it a string, no paths to any data or anything.
dataset_to_process = r'FULL_PATH_TO_A_FEATURE_CLASS_IN_A_GEODATABASE'
#e.g. C:\temp\a.gdb\layername
new_layer_name = os.path.basename(dataset_to_process)
#e.g. layername
The layer that did get added to your map, came from MakeFeatureLayer_management due to the default arcmap setting.
The second bunch of code (arcpy.Mapping.Addlayer etc) should (and does on my pc) have crashed as the layer we make in the above fashion cannot be correct.
However I feel it may somehow be making a ghost layer, that has no selections applied for obvious reasons. (e.g. a bug in ESRI, that wont manifest if you use the software as intended)
MakeFeatureLayer_management does not necessarily add the layer to a data frame. If you're running tools headless (without the ArcMap frontend,) there's no data frame for it to be added to, necessarily. You can use it to create layers in a script that are never seen by the user, to do all sorts of things, perform selections, etc... It's interesting to me that the add layer function worked (correctly or not) 'by name' vs... 'by layer', which isn't documented and should not really work at all...
I'm still away from my machine, will get back to it shortly...
I'm back to it and have been testing the recommendation from @bixb0012 above. I received a response from ESRI, but I didn't really like mangling the output parameters. I preferred the more direct approach of adding the layers explicitly with the mapping.Layer call. Removing the AddFatureLayer_management call and adding the dataset directly to the Layer call appears to work.
def execute(self, parameters, messages):
dataset_to_process = # path to feature class
mxd = arcpy.mapping.MapDocument('CURRENT')
lyr = arcpy.mapping.Layer(dataset_to_process)
arcpy.mapping.AddLayer(mxd.activeDataFrame, lyr, 'BOTTOM')
del lyr
del mxd
arcpy.RefreshTOC()
return
If I experience any issues with his approach, i'll post back... otherwise, i'll consider his answer correct.
Cheers!
Just an FYI. The undocumented behavior works for as long as I can remember, back to 9.x days. That said, I learned from conversations with a Product Lead that the behavior wasn't planned, i.e., it was sort of an accident it works directly with data sets. Instead of "fixing" it in ArcMap, which might very well cause existing code to break, they opted to address it in ArcGIS Pro.
You will notice in ArcGIS Pro that Layer has be renamed to LayerFile and the documentation now explicitly states it only works on layer files:
Summary
References a layer file (.lyr or .lyrx) stored on disk.
Attempting to pass a data set to LayerFile in Pro generates a ValueError.