Select to view content in your preferred language

Using variables from execution script in the class toolvalidator

458
1
09-10-2022 06:27 AM
HodaEL
by
New Contributor

Hello everyone! I'd really appreciate your help.

I am creating my first script toolbox in ArcGISPro, and I want to use a variable defined in the execution script.

How can I call a variable from the script and use it in the validation script?

HodaEL_0-1662816010290.png

HodaEL_2-1662816407569.png

 

Thank you 🙂 

 

0 Kudos
1 Reply
by Anonymous User
Not applicable

If you are trying to create a parameter filter based on a selection, you should do that within the Validation class.  It executes python commands just like the execution block so you can dynamically create filters from other data. I don't think the internal wiring allows for modifying just the filter of a parameter outside of the Validation class.

Make my_content a parameter pointing to your data by default (used param[0] for the example and then build your list from there:

    def updateParameters(self):
        # get the list values
        if self.params[1].value == 'dashboard':
            fcFilter = self.params[1].filter

            # use list comprehension and set to return a distinct list of values.
            attributevalues = list(
                set([cnt.title for cnt in self.params[0].valueAsText])) # parameter set to what the my_content variable is.

            # sort alphabetically
            attributevalues.sort()

            # set the parameter list
            fcFilter.list = attributevalues

If you must do it in through the execution (script), you may have to override/extend the Script Tool/ toolbox / Validation class to create the class methods to set values, or you can try by using SetParameterAsText and some dummy parameters.

It would be (using the next parameter index that is available):

 

L = ['a','b','c']

arcpy.SetParameterAsText(5, L)

 

Then do some parsing in the Validation class when the parameter is modified to make it into a list since you can only pass Objects or Text. I am not sure if the disabled boolean will also disable accessing/setting other properties.  This is untested:

 

    def initializeParameters(self):
        # disable the dummy param to hide it from the UI
        self.params[5].enabled = False
        return

    def updateParameters(self):
        # Modify parameter values and properties.
        # This gets called each time a parameter is modified, before
        # standard validation.
        if self.params[1].altered:
            if self.params[1].value == 'dashboard':
                if isinstance(self.params[5].value, str):
                    self.params[1].filterlist = self.params[5].value.strip('][').split(', ')

        return

 

You'll have to play around with it.

0 Kudos