Select to view content in your preferred language

Getting error setting parameter.displayName in Python Toolbox

1075
2
Jump to solution
09-08-2023 08:22 AM
AdamLake2
Occasional Contributor

Hi all,

I'm building a python toolbox and getting a weird error when trying to set a parameter's displayName in the updateParameters function. I have a working version where it enables and disables parameters, but I get the following error. I know what this means, but the documentation indicates that the displayName attribute should be read/write for a parameter object.

Error 000001 Traceback (most recent call last):
File "<string>", line 228, in updateParameters
AttributeError: ParameterObject: Set attribute: displayName does not exist

Line 228 is the following, but I get the error for any of the parameters[].displayName lines:

parameters[5].displayName=param_settings["Stands"]

Here is the relevant code. I would really appreciate any ideas! 

 

 

class StandsOnBuffers_Update(object):
    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "Update StandsOnBuffers"
        self.description = "Creates StandsOnBuffers for update process"
        self.canRunInBackground = False
        self.category = "Update"

    def getParameterInfo(self):
        """Define parameter definitions"""
        procws_param = arcpy.Parameter(
            displayName="Processing Workspace",
            name="Processing_Workspace",
            datatype="DEWorkspace",
            parameterType="Required",
            direction="Input")
        #param0.displayOrder = 0

        cf_param = arcpy.Parameter(
            displayName="Configuration File",
            name="Configuration_File",
            datatype="DEFile",
            parameterType="Required",
            direction="Input")
        cf_param.filter.list=['json']
        #param1.displayOrder = 2

        outws_param = arcpy.Parameter(
            displayName="Output Workspace",
            name="Output_Workspace",
            datatype="DEWorkspace",
            parameterType="Required",
            direction="Input")
        #param2.displayOrder = 3

        rn_param = arcpy.Parameter(
            displayName="Run Name",
            name="Run_Name",
            datatype="GPString",
            parameterType="Required",
            direction="Input")
        #param3.displayOrder = 4

        reg_param = arcpy.Parameter(
            displayName="Region",
            name="Region",
            datatype="GPString",
            parameterType="Required",
            direction="Input")
        reg_param.filter.type="ValueList"
        reg_param.filter.list=["SEUS","SWUS","NWUS"]
        #param4.displayOrder = 5

        st_param = arcpy.Parameter(
            displayName="Stands from previous run",
            name="Stands",
            datatype="DEFeatureClass",
            parameterType="Optional",
            direction="Input")
        #param5.displayOrder = 6

        arws_param = arcpy.Parameter(
            displayName="Archive Workspace",
            name="Archive_Workspace",
            datatype="DEWorkspace",
            parameterType="Required",
            direction="Input")

        own_param = arcpy.Parameter(
            displayName="Ownership from previous run",
            name="Ownership",
            datatype="DEFeatureClass",
            parameterType="Optional",
            direction="Input")

        rdwarma_param = arcpy.Parameter(
            displayName="Road Water RMA Buffers",
            name="Road_Water_RMA_Buffers",
            datatype="DEFeatureClass",
            parameterType="Required",
            direction="Input")

        dmgst_param = arcpy.Parameter(
            displayName="Damaged Stands",
            name="Damaged_Stands",
            datatype="DEFeatureClass",
            parameterType="Optional",
            direction="Input")
        dmgst_param.enabled = False

        params = [procws_param,outws_param,arws_param,cf_param,reg_param,st_param,own_param,rdwarma_param,dmgst_param,rn_param]
        return params

    def isLicensed(self):
        """Set whether tool is licensed to execute."""
        return True

    def updateParameters(self, parameters):
        """Modify the values and properties of parameters before internal
        validation is performed.  This method is called whenever a parameter
        has been changed."""
        if parameters[3].altered:
            with open(parameters[3].valueAsText,'r') as f:
                config = json.load(f)
            param_settings = config["Parameters"]
            if "Stands" in param_settings:
                if isinstance(param_settings["Stands"],str):
                    parameters[5].enabled=True
                    parameters[5].displayName=param_settings["Stands"]
                else:
                    parameters[5].displayName="Stands from previous run"
                    parameters[5].enabled=param_settings["Stands"]
            if "Ownership" in param_settings:
                if isinstance(param_settings["Ownership"],str):
                    parameters[6].enabled=True
                    parameters[6].displayName=param_settings["Ownership"]
                else:
                    parameters[6].displayName="Ownership from previous run"
                    parameters[6].enabled=param_settings["Ownership"]
            if "DamagedStands" in param_settings:
                if isinstance(param_settings["DamagedStands"],str):
                    parameters[8].enabled=True
                    parameters[8].displayName=param_settings["DamagedStands"]
                else:
                    parameters[8].displayName="Damaged Stands"
                    parameters[8].enabled=param_settings["DamagedStands"]
        return

    def updateMessages(self, parameters):
        """Modify the messages created by internal validation for each tool
        parameter.  This method is called after internal validation."""
        if parameters[5].enabled:
            if parameters[5].value and arcpy.Exists(parameters[5].valueAsText):
                parameters[5].clearMessage()
            else:
                parameters[5].setIDMessage("ERROR",530)
        if parameters[6].enabled:
            if parameters[6].value and arcpy.Exists(parameters[6].valueAsText):
                parameters[6].clearMessage()
            else:
                parameters[6].setIDMessage("ERROR",530)
        if parameters[8].enabled:
            if parameters[8].value and arcpy.Exists(parameters[8].valueAsText):
                parameters[8].clearMessage()
            else:
                parameters[8].setIDMessage("ERROR",530)
        return

    def execute(self, parameters, messages):
        """The source code of the tool."""
        arcpy.env.overwriteOutput = True
        #with open(debugLogPath,'w') as debugLog:
        for param in parameters:
            if not param.enabled:
                param.value=None
                #debugLog.write("{}:{}\n".format(param.name,param.valueAsText))
        StandsOnBuffers(parameters[0].valueAsText,parameters[1].valueAsText,parameters[2].valueAsText,parameters[3].valueAsText,parameters[4].valueAsText,parameters[5].valueAsText,parameters[6].valueAsText,parameters[7].valueAsText,parameters[8].valueAsText,parameters[9].valueAsText)
        return

    def postExecute(self, parameters):
        """This method takes place after outputs are processed and
        added to the display."""
        return

 

 

 

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
DonMorrison1
Frequent Contributor

"displayName" can only be set in the getParameters method.   From this page:

Several properties, including namedisplayNamedatatypedirection, and parameterType, establish the characteristics of a tool and cannot be modified during validation methods (such as updateMessages and updateParameters).

View solution in original post

2 Replies
DonMorrison1
Frequent Contributor

"displayName" can only be set in the getParameters method.   From this page:

Several properties, including namedisplayNamedatatypedirection, and parameterType, establish the characteristics of a tool and cannot be modified during validation methods (such as updateMessages and updateParameters).

AdamLake2
Occasional Contributor

Thanks! I was looking at the Parameter documentation and didn't think to look at the more general toolbox documentation, so I missed that. We'll just move forward without that.

0 Kudos