Type: Scripted (Python) and Scripted (Tcl)

If you choose either Scripted (Python) or Scripted (Tcl) from the Add/Edit Parameter Dialog Fields, you can write a script that assigns the value of a parameter to the workspace at runtime. This script will compute a value and assign it to a global parameter before the data is even read (for example, you might want to open a database, extract a value, and then run the workspace).

You can also use these parameters in a custom transformer.

FME Hub Templates

Templates that contain Scripted (Python) parameters are available for download from FME Hub. To download:

  • Select File > New.
  • Select FME Hub.
  • In the Search bar, type python.
  • Choose one of the templates from the list at the top right.

Example - Group Layers in a Workspace (Scripted Python)

Define the Parameter in the Workspace

You can use the Scripted (Python) parameter to allow grouping of feature types (layers) in a workspace. In this example, taken from one of the templates described above, the parameter allows you to choose to group by different layers when you run the workspace.

Open the Workbench User Parameters dialog.

The parameter is defined using the following options:

Write the Python (or Tcl) Script

Click the browse button beside the Value field to display the source code editor. The code in the editor is described here:

Code Description
Initially set featureTypes variable to empty.
If you choose Bus Information for global parameter Layers, set the feature types to Bus Routes and BusStops.
If you choose Road and Rail for global parameter Layers, set the feature types to metrorail and Roads.
If you choose All Transit for global parameter Layers , set the feature types to metrorail, Roads, BusRoutes, BusStops and Labels.

Note: For both Python and Tcl, your code must contain a return statement on the last line of code. If there is no return statement (and assuming there is more than one line of code) FME will add return to the beginning of the line. Example for single lines of code:Closed
When finding the last line of code, multiline statements or comments are not supported.

Return the featureTypes variable.

The feature_types_to_read global parameter will be set to the value of this return.

The feature_types_to_read global parameter is linked to the reader Feature Types To Read parameter; therefore, the feature types that are read will be the feature types specified in this return value.

Note: Take note of the style of code block indentation. The FME source code editor uses tab stops for indenting blocks, whereas other Python development environments (like IDLE) substitute tab stops by four spaces (by default). Mixed styles in the same block are not allowed. To avoid errors, you may have to replace tabs or spaces in code copied from another environment.

Run the Workspace

To run the workspace, select Run > Prompt for Parameters or press Ctrl+r, or click the button.

Prompt and Run will display a Translation Parameters dialog. The translation will pause until you choose which layers to group.

If you Select All, the features are set to all feature types:

If you select Bus Information only, for example, the feature types are set to Bus Routes and BusStops only:

Additional Python/Tcl Actions

This table gives examples of actions you might want to include in your Python or Tcl script.

Action Python Tcl
Access other parameters

FME_MacroValues['MacroName']

FME_MacroValues is a dictionary where the key is the macro name and the value is the value of the macro.

$FME_MacroValues(MacroName)

FME_MacroValues is a dictionary where the key is the macro name and the value is the value of the macro

In neither case will changing the value of a macro in the dictionary take effect outside the scope of your code.

Set other parameters Not applicable

puts {MACRO MyOtherMacro someValue};

Note this will set that parameter in the FME_MacroValues dictionary after your code has executed.

Change FME_MacroValues directly from your code if you want to access the updated value later in your code.

Write to the log file. The message will appear in the log window in Workbench in execution context with the color indicated by the error level. Not applicable

puts {Tcl2 FME_LogMessage fme_fatal {message message}}

To write to the end of the log file. The message will appear in the log at the end in the ‘info’ level.

print ‘my message’ or sys.stdout.write('dive in error')

If you have multiple print or stdout.write statements, only the first will appear.

puts {Tcl2 error {my message}}

See FME Factories and Functions help for the complete description of the FME_LogMessage function.