Skip to content

Python

The existing tfstudio Python package starts a separate copy of TFStudio.exe without opening a window. Use it to open and save designs, edit layers, and run the calculations listed as available in the API Reference.

Use Python 3.8 or newer and the source archive or Git checkout for the same TFStudio release as your executable. Open a shell at the root of the source tree and add the folder containing the bundled Python client to your Python path.

Terminal window
$client = (Resolve-Path 'app/src-tauri/script-runtime/source/python').Path
$env:PYTHONPATH = if ($env:PYTHONPATH) { "$client;$env:PYTHONPATH" } else { $client }

On Linux, use the following command.

Terminal window
export PYTHONPATH="$PWD/app/src-tauri/script-runtime/source/python${PYTHONPATH:+:$PYTHONPATH}"

Run your scripts with Python from the same shell. The client includes Batch Measurements and uses only the Python standard library.

Give TFStudio the full path to your installed, portable or locally built executable. You can also put that path in TFSTUDIO_EXE and call TFStudio(). For a local Linux build, use target/release/tfstudio in the example below.

from pathlib import Path
from tfstudio import TFStudio
exe = Path("target/release/TFStudio.exe").resolve()
with TFStudio(exe=exe) as tf:
design = tf.new_design(name="Mirror")
design.stack("(HL)^6 H")
spectrum = design.spectrum(450, 650, step=10, angles=[0, 45])
print(spectrum.wavelength)

The with block closes the server. If you keep the object outside a with block, call tf.close() when finished. Pass data_dir= to select a data folder containing Materials.

open_design() reads a native TFStudio .tfs file into the Python program. Call save() to write your changes to disk. It refuses an existing file unless you pass overwrite=True. The Design Import window is the desktop route for TFCalc, Essential Macleod and OptiLayer design files.

with TFStudio(exe=exe) as tf:
design = tf.open_design("AR.tfs")
design.layers[1].thickness += 5
design.save("AR edited.tfs")

Layer thickness is in nm. The Design Editor and the Python layer list number layers from 1 next to the substrate. See the API Reference for file formats, option names, and file operations available in the Rust executable.

For pulse analysis from a measured spectrum, read the text file in Python and pass its rows to design.set_pulse_spectrum(rows). Each row is [wavelength_nm, intensity] or [wavelength_nm, intensity, phase_rad].

Open Interactive on the desktop Optimization tab, or enable listening under Settings → Automation and approve the connection. Pass live=True to let your Python process reach that window.

with TFStudio(exe=exe, live=True) as tf:
design = tf.app.open_design()
print(design.name)

Use tf.app.designs() to find another open project design. After editing the active design’s copy, call design.push() to apply one Undo step. TFStudio refuses an apply if the active design or its revision changed since the read. Click Terminate in TFStudio to end live access. To run Python inside the app, use the Script Console.