« Back

Getting Started with PyTecplot Scripting


What is the fastest way to dive into PyTecplot scripts? This is the first in a series of blogs to get you up and running ASAP using PyTecplot scripting best practices.

1. Use PyTecplot Recording to Your Advantage

Use PyTecplot recording to your advantage. Recording your actions in Tecplot 360 will help you quickly learn the PyTecplot API. And you will have a fully runnable script. However, recorded scripts can be quite verbose and are not optimized for speed.

To record your script, first run Tecplot 360. Then from the top menu, select Scripting > Record PyTecplot… then perform actions to achieve the final plot.

Script Example #1: Recorded Script Snippet

tp.active_frame().plot().contour(0).variable_index=3
tp.active_frame().plot().show_contour=True
tp.active_frame().plot(PlotType.Cartesian3D).vector.u_variable_index=4
tp.active_frame().plot(PlotType.Cartesian3D).vector.v_variable_index=5
tp.active_frame().plot(PlotType.Cartesian3D).vector.w_variable_index=6
tp.active_frame().plot().show_vector=True
tp.active_frame().plot(PlotType.Cartesian3D).use_translucency=True

2. Understand the Primary Hierarchy of PyTecplot

  • Session – Connect to a running Tecplot 360, acquire a license, suspend context.
  • Layout – Access to frames and pages, control attributes of the workspace.
  • Page –May contain one or more frames.
  • Frame – Holds the dataset and plot, can have multiple frames in a single layout.
    • Dataset – Direct access to zones and variables.
    • Plot – Set the style for the plot including axes, slicing and iso-surfaces.
      • Fieldmaps & Linemaps – Style settings associated with zones.
  • Export – Save images and animations.
  • Data – Plot independent operations on the dataset.
    • Load & Save – Load data from files and save data to Tecplot format files.
    • Operate – Perform and query calculations on the dataset.

3. Improve Script Performance and Readability

Use Frame, Plot and Dataset objects to improve script performance. These object references are preferred, rather than referring to them from the top level (as in Example #1). In connect mode, using the objects reduces message passing. In batch mode using the objects reduces the number of function calls. These simple code changes improve the speed of this code by 65% in both batch and connected mode.

Using objects also significantly improves the readability of your scripts. Compare Script Example #1 to the simplified version in Script Example #2. 

Script Example #2 Snippet

frame = tp.active_frame()
dataset = frame.dataset
plot = frame.plot(PlotType.Cartesian3D)
plot.contour(0).variable_index=3
plot.show_contour=True
plot.vector.u_variable_index=4
plot.vector.v_variable_index=5
plot.vector.w_variable_index=6
plot.show_vector=True
plot.use_translucency=True

4. Reference Zones and Variables by Name

Zone and variable indices are recorded in PyTecplot scripts. Changing them to zone and variable names can be more reliable, and makes the script more readable.

Change this:

plot.vector.u_variable_index = 4
plot.vector.v_variable_index = 5
plot.vector.w_variable_index = 6

To this:

dataset = tp.active_frame().dataset
plot.vector.u_variable = dataset.variable(“U”)
plot.vector.v_variable = dataset.variable(“V”)
plot.vector.w_variable = dataset.variable(“W”)

Bonus Tip

Use wildcards to get all the zones or variables with common names. In this example, we want to get all zones with “wing” in the name.

zones = dataset.zones(“*wing*”)

5. Use Zero-Based Indexing in PyTecplot

Unlike the Tecplot Macro Language and Tecplot 360 GUI, PyTecplot is zero-based. Python is naturally a zero-based language, so zero-based indices throughout PyTecplot make for a more natural fit with the language.

This example shows how to get the values for the first zone and variable in the dataset.

data = dataset.zone(0).values(0)[:]

Keep in mind that calling macros and using execute equations from PyTecplot still require one-based indices. These two commands are equivalent because they both set the variable assignment of contour group #1 to variable #4:

plot.contour(0).variable_index = 3
tp.macro.execute_command(‘$!SetContourVar ContourGroup=1 Var=4’)

6. Update PyTecplot Frequently and Subscribe

PyTecplot is pushed to PyPi installers frequently with new functions and performance updates.

python –m pip install --upgrade pytecplot

Get PyTecplot (Tecplot 360) Updates
Get notified by email when PyTecplot and Tecplot 360 are released.

Subscribe to Tecplot


Read the next blog in this series: Improving PyTecplot Script Performance