← All Features

Export to Script

A new export pathway that hands your G-code to a Python script. Save to disk, upload via FTP, send to any networked printer, or all three at once. Standard Python, no proprietary API. Your G-code goes where you tell it to go.

Your G-code, Your Rules

Every slicer gives you "Save" and maybe "Send to Printer." What happens when your printer speaks a protocol the slicer doesn't support? What if you need to save locally and upload to a print farm simultaneously? What if you want to push G-code to a printer whose manufacturer has decided to lock out third-party software?

Export to Script solves this by getting out of the way. When you click "Export to Script," preFlight hands your finished G-code to a Python script you control. The script receives a simple object with two attributes: gcode.data (a mutable list of G-code lines) and gcode.filename (the suggested filename). What happens next is entirely up to your code.

Why This Matters

Not every printer manufacturer wants you to choose your own slicer. preFlight sends directly to printers with open interfaces. For the rest, there's Export to Script. Full Python: standard library, pip packages, network access, file I/O. If your printer speaks MQTT, FTP, HTTP, or anything else with a Python library, your script can talk to it. The rest is up to you.

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 alongside "Save locally" and "Send to Printer."

Sample: Save to Folder

Network shares, NAS drives, SD card readers, any mounted path. Configure the folder and you're done.

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') as f:
        f.writelines(gcode.data)

Sample: FTPS Upload

Direct upload to any printer or server that accepts FTPS. Streams from memory with no temporary files. Standard library only.

import ftplib, ssl, io

HOST = "192.168.1.50"
PORT = 990

def export(gcode):
    payload = ''.join(gcode.data).encode('utf-8')
    stream = io.BytesIO(payload)

    ctx = ssl.create_default_context()
    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 like paho-mqtt, a script can speak MQTT to any printer on the local network. Connect, authenticate, publish. preFlight doesn't know or care what protocol your script uses.

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

A single script can send G-code to multiple destinations. Save locally, upload to the printer, and push to a print farm dashboard, all in one click. Modify gcode.data before any output to apply last-mile edits to every destination at once.

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)

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

AttributeTypeDescription
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.

That's the entire API. No proprietary SDK, no authentication tokens, no callbacks to register. Define export(gcode), write your logic, done.