Model Builder Parameters: Restrict input layer choice to a subset of layers

2349
9
05-10-2010 04:39 PM
MWing
by
New Contributor
I have a model that requires a user to select one of several rasters as a model parameter (input variable). By default, all the rasters in the TOC are available as parameter choices and are accessible in the dialog box for the model through a drop down button. I'd like some of these raster layers to not be available to the model user. How could I restrict the available layer list to a subset?

    Thank you,
    Michael
0 Kudos
9 Replies
CraigPrisland1
New Contributor II

Is anyone aware if this question was ever answered?  Even though it was posted several years ago, I am running into the same issue.  However instead of rasters, I'm dealing with feature classes.  I can filter the dropdown selection in the parameter by feature class type but am looking to filter this dropdown to specific feature class names.

Thanks.

0 Kudos
DarrenWiens2
MVP Honored Contributor

Do you know the names in the layer list beforehand? If so, you can populate a string parameter value list with the names of your layers, and reference them in your model through inline variable substitution.

0 Kudos
CraigPrisland1
New Contributor II

Darren,

That works well as I will know the names of the layers.  The only issue that I have with this process is that it is possible that not all layers will be in the TOC and I do not want layers not in the TOC available in the dropdown.

Thanks again!

0 Kudos
DarrenWiens2
MVP Honored Contributor

I'm not sure how you can achieve this in Model Builder, but once you're model is completely finished, you can export it to a Python script, create a script tool, and use validation to customize the filter.

  def initializeParameters(self):
    """Refine the properties of a tool's parameters.  This method is
    called when the tool is opened."""
    mxd = arcpy.mapping.MapDocument("CURRENT")
    lyrs = arcpy.mapping.ListLayers(mxd)
    compare_list = ['points','buff','something_else'] # the full list of layers you want to consider
    param_list = []
    for lyr in lyrs:
        if lyr.name in compare_list:
            param_list.append(lyr.name) # add to internal list
    self.params[2].filter.list = param_list # overwrite filter list
    return
0 Kudos
CraigPrisland1
New Contributor II

Thanks Darren.  One quick question:  Would you keep the string parameter value list and inline variable substitution in the model when you export it to Python?

0 Kudos
DarrenWiens2
MVP Honored Contributor

Basically, yes. You can't (I don't think) change the parameter to a more appropriate type like Feature Class or Feature Layer because there will only be a Feature Class filter available, not Value List. Once you're more familiar with Python, you can clean up the exported code to change the inline variable substitution to more readable variables, but for now it should be fine.

0 Kudos
CraigPrisland1
New Contributor II

Darren,

I am getting the following when trying to add the code that you provided when updating the Validation.  I am updating the compare_list with only those layers I want to consider in the TOC.  Do you have any ideas what may be causing this?  Thanks again.

0 Kudos
DarrenWiens2
MVP Honored Contributor

Oh, you'll have to adjust the index number for self.params[] to match your interface. In my example, I specified self.params[2] to mean the third parameter. The counting starts at zero.

CraigPrisland1
New Contributor II

Thanks again for all of your assistance Darren.  This is exactly what I needed.

0 Kudos