Python Scripted Parameters in FME

Liz Sanderson
Liz Sanderson
  • Updated

Introduction

This article is part 3 of a 5-part series to help you get started with basic Python and FME. This article discusses the use of scripted parameter functionality within Workbench and provides a walkthrough of two example applications that utilize scripted parameters.

Scripted parameters are very useful when we want to set a parameter in FME based on something we derived or calculated from another parameter or parameters. For example, you may want users to select themes or groups of layers and have your script set the individual feature types to read within these groups. For Python scripts, a number of FME variables are available within the fme module, one of which returns a dictionary of FME parameters and their values. For more information, please review the Python and FME Basics introduction article.

Scripted parameters are executed before both Python startup scripts and the translation.

Examples

Example 1: Scripting Feature Types to Read

Attached workspace: ScriptedParameter1.fmwt.

ScriptedParameter1.fmwt is a good example of scripting the feature types to read parameters based on a user-selected group. The workspace template, ScriptedParameter1.fmwt, is included within the scriptedparameters-workspaces-2021.zip file (see the Files section).

ScriptedParameter1.fmwt_Canvas.png
This workspace will selectively read feature types with a scripted parameter. Ensure the Prompt for User Parameters option is enabled before running the workspace to see the published parameters. A Translation Parameters window will appear. Click on the ellipsis in the window to bring up the Select ‘Layers’ Items window. Notice that the layers you can choose from are groups:

  • Walking and Biking
  • All Methods
  • Rapid Transit

translation-parameters-window.png

Choose "Walking and Biking" and run the workspace. Notice the workspace reads both the Bikeways and the PublicStreets feature types.

ScriptedParameter1-workflow.png

Let’s look at the scripted parameter used to tell FME that choosing Walking and Biking should return both of these feature types to the Feature Types to Read parameter. In the Navigator pane, expand User Parameters > Private Parameters, and you will see a Python script parameter called feature_types_to_read. Notice that this parameter is linked to the actual Feature Types to Read parameter of the reader in the workspace. Whatever value the AutoCAD reader uses, the script determines which feature type to read.

navigator-parameters.png
Go back to the scripted parameter, double-click on it, and click the ellipsis to open the editor. Here is the script used inside:

import fme
featureTypes = ''
if fme.macroValues['layers'].find('Walking and Biking') != -1:
    featureTypes += 'PublicStreets Bikeways '
if fme.macroValues['layers'].find('Rapid Transit') != -1:
    featureTypes += 'RapidTransitLine RapidTransitStations '
if fme.macroValues['layers'].find('All Methods') != -1:
    featureTypes += 'PublicStreets Bikeways RapidTransitLine RapidTransitStations '
#Debug
#print(featureTypes)
return featureTypes

You can see that we have a series of if statements that find out which layer the user chose using the fme.macroValues[] dictionary and then sets the value of featureTypes, which will be returned. The last line is the most important, as this is where we "return" the actual value of the scripted parameter with the statement:

return featureTypes

This return statement must always exist in a Python-scripted parameter because it is here that the parameter value is given to FME. Also, notice the commented-out print function. You can use the print function to help you debug your scripts by returning variable values to the Translation Log pane. For more on logging with Python, see this article: Logging with Python Scripts.

Example 2: Unique Output Dataset Names with Scripted Parameters

Attached workspace: ScriptedParameter2.fmwt

When writing files, you may want to ensure that your output is uniquely named. One way to do this is to use a scripted parameter to append the date and time to the output dataset name. The workspace ScriptedParameter2.fmwt accepts a published parameter for the output dataset name, and a private scripted parameter appends a unique value (date and time) to the published parameter value. The workspace itself identifies points (such as drinking fountains) that fall within park polygons and outputs a CSV file.

ScriptedParameter2.fmwt-workflow.png

Open the workspace and ensure that Prompt for User Parameters (found under the Run menu) is enabled. Run the workspace, and the Translation Parameters window will appear. Click OK to accept the default output dataset name.

Take a look at the Translation Log pane once the workspace has finished running. Under the Features Written Summary at the end of the log, you will see the name of the output CSV file written. It will be in the following format: <fileName>_<YYYYMMDD>_<HHMMSS>, where <fileName> is the value of the published parameter (test12 in the sample workspace), <YYYYMMDD> is the current date, and <HHMMSS> is the start time of the translation.

ScriptedParameter2.fmwt-log.png

Go to the Navigator > User Parameters > Private Parameters > nameWithTime and double-click to open the script editor. Here is the script used inside:

# Import fme module and datetime.datetime class
import fme
from datetime import datetime

# Get the value of the published parameter
OutFileName = fme.macroValues['OutFileName']

# Use the now() method to get the current date and time
# Format date as YYYYMMDD_HHMMSS with strftime method
curTime = datetime.now().strftime("%Y%m%d_%H%M%S")

# Concatenate the published parameter, an underscore, date time
OutFileName += f'_{curTime}'

# Return unique file name which becomes the value of the scripted parameter
# This scripted parameter is linked to the destination dataset name
return OutFileName

The script gets the value of the published parameter using the fme.macroValues[] dictionary and the current date and time by calling the datetime.now() function. It concatenates the datetime value to the user-defined filename and returns the unique value (fileName_YYYYMMDD_HHMMSS) to the writer with the return statement. Remember, the return statement must always exist in a scripted parameter.

Adding the date and time to an output file name is also possible without using scripted Python parameters (e.g., by using the DateTimeNow() function that is available in the text editor).

For more information on the Date/Time functions in Workbench, please see the help documentation: Date/Time Functions.

Documentation

For using FME Objects in Python, you can find complete documentation of the Python FME Objects API here: FME Objects Python API.

Was this article helpful?

We're sorry to hear that.

Please tell us why.

As of January 14th, 2026, comments on knowledge base articles have been closed. To make sure questions don’t get missed and to enable more community support, we’ve moved discussions to the FME Community. If you have a question or a comment about this article, please create a new post or create a support ticket.