I converted a working toolbox with python script tools from the tbx format to the new atbx format and experienced behavior where a tool parameter was not being populated correctly. I was able to troubleshoot and determine when the parameter is populated correctly and when it is not. The difference occurs in the in the JSON type files tool.content and tool.content.rc that can be accessed through 7-Zip (or other archive utility). I'm guessing it relates to the "loading/opening of a toolbox with many tools, as well as the access of those tools' properties and parameters, when a particular tool is opened" discussed here.
The parameter has a data type of String and a Filter of Value List, and is used to list the layers in the map that pass validation. For troubleshooting, I stripped everything down to remove the validation and simply populate the parameter with all layers inside initializeParameters of the ToolValidator.
def initializeParameters(self):
# Customize parameter properties.
# This gets called when the tool is opened.
import os
upd_list = []
project = arcpy.mp.ArcGISProject("CURRENT")
m = project.activeMap
for lyr in m.listLayers():
upd_list.append(lyr.name)
self.params[0].filter.list = upd_list
return
Pretty basic stuff. However, when you open tool, the parameter is sometimes populated with only blank entries. See below.
After opening the tool with an active map view window open it fails to populate, if you open the tool properties and inspect the Value List it should have the list of layers from the map view. To get the tool working, I enabled the Multiple values checkbox of the parameter, reopened the tool and the layers were all listed, then disabled the Multiple values checkbox again, and the layers were still listed properly. However the parameter breaks again if you add more layers to the map.
It seems like the performance improvements they tried to implement actually end up breaking functionality (validation/filtering of layers in map before populating a parameter with them) that I'm not aware of being offered in another manner. The parameter items seem to be cached in the tool.content and tool.content.rc files (see below) and refreshing the toolbox doesn't seem to rebuild the cache. This makes atbx toolboxes unsuitable for sharing.
The tool.content file is below. In the file from a tool with the parameter populated, the parameter has a domain property with items reflecting the layers populated in initializeParameters.
{
"type": "ScriptTool",
"displayname": "$rc:title",
"app_ver": "13.0",
"product": "100",
"params": {
"list_of_layers": {
"displayname": "$rc:list_of_layers.title",
"datatype": {
"type": "GPString"
},
"domain": {
"type": "GPCodedValueDomain",
"items": [
{
"value": "aspect.tif",
"code": "$rc:list_of_layers.domain.aspect.tif"
},
{
"value": "slope.tif",
"code": "$rc:list_of_layers.domain.slope.tif"
},
{
"value": "fl_down.tif",
"code": "$rc:list_of_layers.domain.fl_down.tif"
}
]
}
}
}
}
The tool.content.rc file is below and has some example layers listed that were included in the parameter and also refer back to the contents of the tool.content file.
{
"map": {
"list_of_layers.domain.aspect.tif": "aspect.tif",
"list_of_layers.domain.fl_down.tif": "fl_down.tif",
"list_of_layers.domain.slope.tif": "slope.tif",
"list_of_layers.title": "List of Layers",
"title": "Example"
}
}
I've attached the toolbox that worked for me after I toggled the Multiple values checkbox in the Parameters tab of the tool properties to force the rebuild of the tool.content and tool.content.rc files. Unless you create layers with the names from above, the tool won't populate the layer names form the map.
Is this a known bug/limitation? Are there plans to fix this? Are there any other ways to filter and validate what layers from a map will be included in a parameter?