Select to view content in your preferred language

Python Toolbox: Value Tables parameters: accept composite data types?

121
1
a month ago
AlfredBaldenweck
MVP Regular Contributor

I'm trying to set up a value table to accept multiple parameter data types as input. It's possible for non-value table parameters; from the documentation :

def getParameterInfo(self):
    #Define parameter definitions

    # First parameter
    param0 = arcpy.Parameter(
        displayName="Input Raster Dataset",
        name="in_rasterdataset",
        datatype=["DERasterDataset", "DERasterCatalog"],
        parameterType="Required",
        direction="Input")

 

However, when I try on value tables, I get the following error.

 param2 = arcpy.Parameter(displayName='Input Data',
                                 name='InputData',
                                 datatype="GPValueTable",
                                 parameterType='Required',
                                 direction='Input')
        dTypes = ["DEFeatureClass", "DEFeatureDataset", "DETable",
                  "DEMosaicDataset", "DERasterDataset"]
        param2.columns = [[dTypes, "Old"], [dTypes, "New"]]

AlfredBaldenweck_0-1726170442814.png

 

Hmm, ok. Maybe I'm just doing it wrong? Let me try making it a single parameter type

        param2 = arcpy.Parameter(displayName='Input Data',
                                 name='InputData',
                                 datatype="GPValueTable",
                                 parameterType='Required',
                                 direction='Input')
        param2.columns = [["DEFeatureClass", "Old"], ["DEFeatureClass", "New"]]

Bingo: 

AlfredBaldenweck_1-1726170500073.png

So, my question is: How can I make a value table parameter accept multiple data types?

(Also, before anyone suggests it, I did already try

param2.columns = [[["DEFeatureClass", "DETable"], "Old"], 
                   ["DEFeatureClass", "DETable"], "New"]
                 ]

 with the same results.

1 Reply
HaydenWelch
Frequent Contributor

I would do the type checking in the validation function instead of the initialization. You can just pass "GPType" as the datatype and let it take any input then immediately set an error message on the parameter if the datatype is invalid. Something like this:

 

import arcpy

param2 = arcpy.Parameter(
    displayName='Input Data',
    name='InputData',
    datatype="GPValueTable",
    parameterType='Required',
    direction='Input')

dTypes = [
    "DEFeatureClass", 
    "DEFeatureDataset", 
    "DETable",
    "DEMosaicDataset",
    "DERasterDataset",
]

# Use generic type by 
param2.columns = [["GPType", "Old"], ["GPType", "New"]]

# This goes wherever your validation function is
def validate_gp_table_types(param: arcpy.Parameter, allowed_types: list):
    for row in param.value:
        if arcpy.Describe(row[0]).dataType not in allowed_types:
            param.setErrorMessage(f"{row[0]} is not a valid data type {allowed_types} required")
    return

 

You can also try filtering after you create the column, this is untested though so just take it as more of an idea.

import arcpy

param2 = arcpy.Parameter(
    displayName='Input Data',
    name='InputData',
    datatype="GPValueTable",
    parameterType='Required',
    direction='Input')

dTypes = \
    [
        "DEFeatureClass", 
        "DEFeatureDataset", 
        "DETable",
        "DEMosaicDataset",
        "DERasterDataset",
    ]

param2.columns = \
    [
        ["GPType", "Old"], 
        ["GPType", "New"]
    ]

for filter in param2.filters:
    filter.type = "ValueList"
    filter.list = dTypes

params = [param2]

 

0 Kudos