[GIS] Conditional drop-down menu list in a python script tool

arcpy

I have created a script tool from some python code, and in a previous question, I learned how to enable a drop-down list in this tool by changing the Validation code – see link and image below:

Setting up Drop-Down list in Parameters of Python script tool?

previous solution

Now I would like to have the second user-input in the tool have its own drop-down list whose members change depending upon the choice in the first input – see image:

example

For example, if the user chose the town "Winooski", I would like to store that input as a variable, maybe called 'name', which might then be included in a block of code like this:

name = self.params[0]
roads = "Z:\\OPS\\TechnicalServices\\Culverts\\GetCulverts\\GetCulverts.gdb\\%s" % name
rows = arcpy.SearchCursor(roads)
self.params[1].filter.list = sorted(list(set(r.getValue('CTCODE') for r in rows)))
del rows

In the 'roads' directory defined in this code, I have a feature class for every town name, which only includes the roads for that town – so the tool will have the user choose a town, and they will then be presented with a list of state roads in that town to choose from. I tried sticking the above code within the Validation code just beneath the block of code that solved my first problem (in the first image above). I'm assuming that the Validation code only runs when you first open the tool. Any clues as to where I should be sticking some code to do this (maybe in the code for the tool itself..?), or how I should be modifying the small block I wrote?

Best Answer

Looks like you were pretty close...You just need to use the .value property of the parameter:

def updateParameters(self):
    """Modify the values and properties of parameters before internal
    validation is performed.  This method is called whenever a parmater
    has been changed."""
    if self.params[0].value:
        name = str(self.params[0].value)
        roads = "Z:\\OPS\\TechnicalServices\\Culverts\\GetCulverts\\GetCulverts.gdb\\%s" % name
        rows = arcpy.SearchCursor(roads)
        self.params[1].filter.list = sorted(list(set(r.getValue('CTCODE') for r in rows)))
        del rows

    return
Related Question