2

My Python Toolbox needs to complete two different kinds of functions, Urban Growth Model and Energy Sector Model, for the Model Type parameter. When I choose Urban Growth Model, the tool window will enable the Plat Features in GDB, and disable the last four parameters. When I choose Energy Sector Model, the tool window will enable the last four, but not the Plat Features in GDB. All of them are the required parameters for the corresponding model type.

The question is I can't let the Plat Features in GDB blank when I run the Energy Sector Model and I can't let the last four parameters blank when I run the Urban Growth Model. I want to find a way to leave the parameters empty when I choose another model type to run.

Any suggestions? I am using ArcGIS Desktop 10.3.1 Advance License.

enter image description here

1

1 Answer 1

4

Unfortunately the "parameterType" is a read-only property, so you can't just change that parameter from "Required" to "Optional".

You can work around this by setting it to "Optional", then performing your own validation to make sure it was entered when needed. Something like this should get you started:

import arcpy

class ToolValidator(object):
"""Class for validating a tool's parameter values and controlling
    the behavior of the tool's dialog."""

def __init__(self):
    """Setup arcpy and the list of tool parameters."""
    self.params = arcpy.GetParameterInfo()

def initializeParameters(self):
    """Refine the properties of a tool's parameters.  This method is
        called when the tool is opened."""
    return

def updateParameters(self):
    """Modify the values and properties of parameters before internal
        validation is performed.  This method is called whenever a parameter
        has been changed."""
    return

def updateMessages(self):
    """Modify the messages created by internal validation for each tool
        parameter.  This method is called after internal validation."""

    if self.params[0].value == 'Energy Sector Model':
        if not self.params[6].value:
            self.params[6].setErrorMessage('Value is required')
        else:
            self.params[6].clearMessage()

    return

It'll still say "Optional" next to the parameter, but it won't let the user continue if you've set an error message.

You can read about validation in the docs here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.