Export to Script
An export option that hands the finished G-code to a Python script instead of writing a file. The script saves it to disk, uploads it over FTPS, sends it to a networked printer, or does all three.
Why It Exists
preFlight already saves G-code to disk and uploads it straight to printers with an open interface. Export to Script is another option in the same menu, for everything those two do not cover: a printer that only takes SCP or MQTT, a manufacturer that locks third-party slicers out, or a print farm that wants the file on a share and in a queue at the same time.
When selected, preFlight calls your script with the complete G-code array, one entry per line so it can easily be scanned or further edited. It suggests a filename and gets out of the way. The script has the whole standard library plus anything you installed with pip, so whatever your setup needs, there is a Python library for it.
How It Works
Your script defines a single function: export(gcode). preFlight calls it with the finished G-code data after slicing. The gcode.data list is fully mutable. You can modify, insert, or remove lines before writing. Standard Python list operations all work.
Scripts run inside preFlight's bundled Python runtime. No separate Python installation needed. Standard library modules (ftplib, ssl, json, os, socket) are available out of the box. Packages installed via pip in preFlight's bundled runtime are also available.
Enable it under Print Settings > Output options > Export to Script. After slicing, "Export to Script" appears in the export dropdown next to "Save locally" and, when a printer is connected, "Send to printer".
What the script receives is the same text a save to disk writes, thumbnails, settings block and print statistics included. Two things a save does are not applied: post-processing scripts do not run, and the text is not converted to binary G-code. preFlight writes no file itself when Export to Script is used. The script is responsible for all output. It runs in the background, so preFlight stays usable and Cancel interrupts it, and the preprocessing time limit applies. To report a failure, raise RuntimeError and the message shows in an error dialog.
The console does the same without the application: preFlight-console --allow-scripts --export-to-script runs the export script of the print settings after writing its own G-code file, so a print farm can drive it from a shell. The first flag grants on the command line the consent the application asks for in its dialog.
Sample: Save to Folder
Writes the file to a folder you set: a network share, a NAS, an SD card reader, or any other reachable path.
import os
OUTPUT_FOLDER = "//NAS/prints" # network share, SD reader, any path
def export(gcode):
os.makedirs(OUTPUT_FOLDER, exist_ok=True)
path = os.path.join(OUTPUT_FOLDER, gcode.filename)
with open(path, 'w', encoding='utf-8', newline='') as f:
f.writelines(gcode.data)Sample: FTPS Upload
Uploads to any printer or server that accepts FTPS, straight from memory, using only the standard library.
import ftplib, ssl, io
HOST = "192.168.1.50"
PORT = 990
VERIFY_CERT = False # most printers use a self-signed certificate
def export(gcode):
payload = ''.join(gcode.data).encode('utf-8')
stream = io.BytesIO(payload)
ctx = ssl.create_default_context()
if not VERIFY_CERT:
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
ftp = ImplicitFTPS(context=ctx)
ftp.connect(HOST, PORT, timeout=15)
ftp.login(USERNAME, PASSWORD)
ftp.prot_p()
ftp.storbinary(f"STOR {gcode.filename}", stream)
ftp.quit()Sample: MQTT to Any Printer
With a pip package such as paho-mqtt, a script can talk to a printer that only takes MQTT. preFlight does not know what protocol the script uses and does not need to.
import paho.mqtt.client as mqtt
import json
def export(gcode):
payload = ''.join(gcode.data).encode('utf-8')
client = mqtt.Client()
client.tls_set()
client.username_pw_set(USERNAME, PASSWORD)
client.connect(PRINTER_IP, 8883)
# Upload file, then publish print command
upload_file(client, gcode.filename, payload)
client.publish(f"device/{SERIAL}/request", json.dumps({
"command": "print",
"file": gcode.filename,
}))
client.disconnect()Multi-Destination Export
One script can write to several destinations: a local copy, the printer, or a print farm dashboard. Edit gcode.data before the first write and every destination gets the edited file.
def export(gcode):
# Last-mile edits applied to every destination
gcode.data.insert(0, "; Processed by preFlight\n")
# Save locally
save_to_folder(gcode)
# Upload to printer
upload_via_ftps(gcode)
# Push to print farm dashboard
post_to_api(gcode)Community Scripts
The samples, a template and the full reference for Export to Script live in the preFlight-scripts repository on GitHub, alongside export scripts contributed by other preFlight users. A script that talks to a printer or print farm nobody else has covered is worth sending in as a pull request. oozeBot reads and runs every community script before merging it. Scripts from the repository are used at your own risk: an export script writes to your filesystem and talks to your network with full access to both, so read and configure one before you run it.
IDE Support
A preFlight.py type stub ships with every release. Drop it alongside your script for full autocomplete on the ExportGCode object in VS Code, PyCharm, or any editor with type hint support.
Security Model
Export scripts have full Python access, the same trust model as post-processing scripts in other slicers. The first time you enable Export to Script, preFlight explains the implications and asks for explicit consent. 3MF files that reference export scripts prompt the user on load and strip the settings if declined.
The Interface
| Attribute | Type | Description |
|---|---|---|
gcode.data | List[str] | Mutable list of G-code lines, each including its trailing newline. Insert, remove, replace, or iterate freely. |
gcode.filename | str | Suggested output filename from the Output filename format setting. Read-only. |