Solved! Go to Solution.
if I add the layer using this code (which is exactly yours apart from line 1 pointing to my featureclass):
path = r'C:\GeoNet\Selection\test.gdb\myLines'
mxd = arcpy.mapping.MapDocument('CURRENT')
new_layer_name = os.path.basename(path)
arcpy.MakeFeatureLayer_management(path, new_layer_name)
new_layer = arcpy.mapping.Layer(new_layer_name)
arcpy.mapping.AddLayer(mxd.activeDataFrame, new_layer_name, 'BOTTOM')
del new_layer
del mxd
This beautiful error is produced:
Runtime error
Traceback (most recent call last):
File "<string>", line 6, in <module>
File "c:\program files (x86)\arcgis\desktop10.6\arcpy\arcpy\utils.py", line 182, in fn_
return fn(*args, **kw)
File "c:\program files (x86)\arcgis\desktop10.6\arcpy\arcpy\mapping.py", line 49, in AddLayer
assert isinstance(add_layer, Layer)
AssertionError
And the layer is added at the top:
So, your code is not working correctly...
Bah, copy/pasta typo... That line should be:
arcpy.mapping.AddLayer(mxd.activeDataFrame, new_layer, 'BOTTOM')
Never seen a "typo" occur from copy and past. Might want to report it to Microsoft.
Haaaaaar 🙂 That was a self inflicted post-copy-pasta wound... But still a distraction from the real issue!!
Some last comments before I leave this thread to what it is:
Good luck on solving it.
MVP and GISP mean almost nothing to me personally. There are people who make careers out of accumulating thumbs ups, which is what it is... I've been doing this for years and typically do not have unlimited time to invest in forum interaction (reference the old, original esri forums...)
There are two people who attempted to provided material value so-far on this issue, you and Joshua, and for that Thanks! Even with the silly typo, it's not difficult to reproduce this issue. If you change that text to new_layer, you'll see 🙂
Yes, I have opened an issue with ESRI support and a working .pyt with both sides of the tool. If there was an attach file option here, i'd use it!!
here you go
Test code attached.
If possible, implement arcpy.GetCount on the layer in the TOC rather than the desc.FIDSet.
mxd = arcpy.mapping.MapDocument('CURRENT')
df = arcpy.mapping.ListDataFrames(mxd)[0]
lyr = arcpy.mapping.ListLayers(mxd, "SomeLayerName", df)[0]
getCountMethod = int(arcpy.GetCount_management(lyr).getOutput(0))
arcpy.AddMessage("getCountMethod: {}".format(getCountMethod))
That seems to work just fine, even after the layer was added programmatically from another GP tool.
James,
I use the feature layer that was provided as an input to the tool within my function. I shouldn't have to List and search for it again, if it's given to me
Also, I am not using the FIDSet for anything besides a little debug output. You could remove the lines from desc = arcpy.Describe(input_feature_class), all the way down to the 'no selection made' message if you want... same issue will surface.
The problem has something to do with the way the layer was added... because any subsequent add of a layer with the same Name will experience the issue.
Joe