How to make a derived Table in a script tool connect in modelbuilder?

701
2
Jump to solution
03-31-2023 04:36 PM
DuncanHornby
MVP Notable Contributor

All,

I am developing a suite of python script tools in the ArcPro 3.1.0 toolbox (atbx). A set of these tools create a stand alone table in a file geodatabase whose location is known to the tool.  The user has no control over name or output location, this is controlled in the script itself.  The code runs without error and creates the table as expected. I expose this table created in the script as a derived output as shown below:

DuncanHornby_0-1680304711669.png

In my script I create the table, fill it with the required data and add it to the map, I also set the 4th parameter to it, the relevant add to map code is shown below:

 

try:
    aprx = arcpy.mp.ArcGISProject("CURRENT")
    mainMap = aprx.activeMap
    errorTable = arcpy.mp.Table(sPathTable)  # sPathTable is a full path to the file geodatabase table
    mainMap.addTable(errorTable)
    arcpy.SetParameter(4, sPathTable)
except:
    arcpy.AddError("Failed to add table to map, this functionality only works if tool is run from within an active ArcPro Project")
    return

 

 

When the tool is run straight from the toolbox, all seems to run OK. If I drag 'n' drop it into a model and then link the derived output to say another tool (in the example below it's an add field tool) the linked tool never validates and the model won't run to completion.

DuncanHornby_1-1680305294837.png

I feel its something to do the derived output schema and the Add field tool doesn't know what's linking into it?  There is the schema property of a parameter but all the examples in the help I've been able to find talk about setting the schema of an output that is dependent on an input parameter.  My parameter is populated with a table built entirely within the script.

Does anyone understand what the problem is and can provide any advice? My gut feeling its something that needs to be added to the Validation (ToolValidator code)?  Or are derived parameters meant to be "un-linkable"?

 

 

0 Kudos
1 Solution

Accepted Solutions
DuncanHornby
MVP Notable Contributor

Hi Dan,

Thanks for your time. I understand your suggestion of moving the Add field tool into the script, but I do not want that. I simply used the Add Field as an example of chaining tools in model builder, it could have been any other tool that takes a table as input.

Spurred on by you I did some further searching with Google and finally came across a thread back in 2019 by @NealBanerjee who was having the same issue as me, the output is derived, built completely within the script and essentially is independent to any of the input parameters. 

Now I may be wrong but I'm getting the impression one cannot create a derived output and set its schema when the derived output is not dependent on any input parameter.  Unless I'm missing some subtle bit of code I cannot create a derived output and say "this is going to be a table with the following fields"?  The examples in the help file show derived outputs that are dependent upon one of the input parameters.

Clear as mud? 🙂

So unless some ESRI modelbuilder/python scripter guru says otherwise I thinks its not possible?  I'm more than happy to be shot down in flames at this point, if someone could show me otherwise.  I should emphasize I'm developing script tools and not a python toolbox.

Now that all said the thread offered up a solution that is workable, it's a bit of a faff but when I tested it does actually work.  You connect the derived output as a precondition to a Calculate Value tool which simply states what the output type is. Then the Add Field tool validates and the whole model runs correctly.

DuncanHornby_0-1680351363423.png

The Calculate tool was set up as:

DuncanHornby_1-1680351424594.png

 

View solution in original post

0 Kudos
2 Replies
DanPatterson
MVP Esteemed Contributor

Parameter—ArcGIS Pro | Documentation

Several properties, including name, displayName, datatype, direction, and parameterType, establish the characteristics of a tool and cannot be modified during validation methods (such as updateMessages and updateParameters).

Of course not all validation methods are mentioned that limit this.

Can you 'add field' to the log table within that script? instead of a separate modelbuilder process.


... sort of retired...
0 Kudos
DuncanHornby
MVP Notable Contributor

Hi Dan,

Thanks for your time. I understand your suggestion of moving the Add field tool into the script, but I do not want that. I simply used the Add Field as an example of chaining tools in model builder, it could have been any other tool that takes a table as input.

Spurred on by you I did some further searching with Google and finally came across a thread back in 2019 by @NealBanerjee who was having the same issue as me, the output is derived, built completely within the script and essentially is independent to any of the input parameters. 

Now I may be wrong but I'm getting the impression one cannot create a derived output and set its schema when the derived output is not dependent on any input parameter.  Unless I'm missing some subtle bit of code I cannot create a derived output and say "this is going to be a table with the following fields"?  The examples in the help file show derived outputs that are dependent upon one of the input parameters.

Clear as mud? 🙂

So unless some ESRI modelbuilder/python scripter guru says otherwise I thinks its not possible?  I'm more than happy to be shot down in flames at this point, if someone could show me otherwise.  I should emphasize I'm developing script tools and not a python toolbox.

Now that all said the thread offered up a solution that is workable, it's a bit of a faff but when I tested it does actually work.  You connect the derived output as a precondition to a Calculate Value tool which simply states what the output type is. Then the Add Field tool validates and the whole model runs correctly.

DuncanHornby_0-1680351363423.png

The Calculate tool was set up as:

DuncanHornby_1-1680351424594.png

 

0 Kudos