How to use derived output in script tool for feature class created from 'Create Feature Class' tool

2856
3
Jump to solution
07-29-2019 05:38 PM
NealBanerjee
Occasional Contributor

Hello

I have Python script tool that creates (and populates) and point feature class using the 'Create Feature Class' tool.  The script tool exposes input parameters for the input workspace and the name of the output feature class.  The actual full feature class path/name is created constructed in the script tool

Im wanting to embed the script tool into a ModelBuilder model so that I can perform additional operations on the output point feature class, but am having issues.  Following the examples, I added a 'Derived Output' parameter in the script tool definition/properties and use the 'SetParameterAsText' function to explicitly set the output feature class path/name.  However, when I run the model, it doesnt seem to recognize the output feature class and wont perform and functions.  See graphics below. 

I feel like I must be missing obvious - any help would be appreciated!

Thanks

Neal

1 Solution

Accepted Solutions
NealBanerjee
Occasional Contributor

Ok after tinkering around I think I have a workaround to keep me going, but still doesnt answer why what I have doesnt work.

What I ended up doing was using the 'Calculate Value' tool in model building to build the name of the feature class.  I linked the derived output as a precondition.  In my case my script tool used the 'Create Feature Class' tool based on user input of workspace and output feature class name.  The script tool created and populates the resultant feature class.  

Based on your example I think this approach will work.  Just note that the 'Calculate Value' tool uses some pretty funny syntax - for strings you have to enclose them in "" in addition to the in-line variables %.

Below are some screenshots. 

View solution in original post

3 Replies
NealBanerjee
Occasional Contributor

Ok after tinkering around I think I have a workaround to keep me going, but still doesnt answer why what I have doesnt work.

What I ended up doing was using the 'Calculate Value' tool in model building to build the name of the feature class.  I linked the derived output as a precondition.  In my case my script tool used the 'Create Feature Class' tool based on user input of workspace and output feature class name.  The script tool created and populates the resultant feature class.  

Based on your example I think this approach will work.  Just note that the 'Calculate Value' tool uses some pretty funny syntax - for strings you have to enclose them in "" in addition to the in-line variables %.

Below are some screenshots. 

curtvprice
MVP Esteemed Contributor
Just note that the 'Calculate Value' tool uses some pretty funny syntax - for strings you have to enclose them in "" in addition to the in-line variables %.

This output path also has backslashes so in Calculate Value you must use the expression:

r"%Output feature class%"‍‍‍

and run time this is seen by Calculate Value as the following python expression. Not so funny when you realize that the model variable gets inserted and and then interpreted as a Python expression!

Note the \n string in the pathname we are protecting from being seen as a newline within the path with the "r" prefix!!

r"C:\MyData\nifty\output_features"‍‍‍‍

I think your issue here is you this is a use case where you have to set dependencies in the tool validation python code block. This is subtle and I have seen Esri's tools not get it right!

Customizing script tool behavior—Help | ArcGIS for Desktop  SEE: Updating the description of output data using the Schema object, setting dependencies

Using Calculate Value to assign data types to model variables is a very useful trick in ModelBuilder when you are hung up on parameter validation.  I have used this when Esri's own tool validation is not working the way I need it to (example: Create Fishnet). But in this case I only think it will work if the dataset exists.  Tricky stuff.

0 Kudos
DuncanHornby
MVP Notable Contributor

Have you tried setting the output Parameter not as Text as the expected input into the subsequent tool is a FeatureClass?

arcpy.SetParameter(3,outFC)
0 Kudos