Skip to content

Instantly share code, notes, and snippets.

@thomasloven
Last active April 12, 2024 11:25
Show Gist options
  • Save thomasloven/36ce1058f583142aa306944ffc151ece to your computer and use it in GitHub Desktop.
Save thomasloven/36ce1058f583142aa306944ffc151ece to your computer and use it in GitHub Desktop.
Shorten PrusaSlicer output filenames for all default printers
""" Shortens output filenames for all printers in prusa config folder.
Will turn filename into: {input_filename_base}_{nozzle_size}_{printing_filament_types}_{print_time}.gcode
Place this file in your PrusaSlicer config folder (Help->Show Configuration Folder) and run it with python.
Make a backup first.
/ Thomas Lovén 2024
"""
import glob
import configparser
import re
import os
KEYNAME = "output_filename_format"
NOZZLE_REGEX = "_0\.[^_]*n_"
if __name__ == "__main__":
scriptpath = os.path.dirname(os.path.realpath(__file__))
files = glob.glob(f"{scriptpath}/**/*.ini")
for f in files:
try:
config = configparser.ConfigParser()
config.read(f)
for printer in config:
if KEYNAME in config[printer] and (value:= config[printer][KEYNAME]).endswith(".gcode"):
nozzle = re.findall(NOZZLE_REGEX, value)
nozzle = nozzle[0] if nozzle else "_"
newvalue = "{input_filename_base}"+nozzle+"{printing_filament_types}_{print_time}.gcode"
# print(f"{printer=}, {value=} {newvalue=}")
config[printer][KEYNAME] = newvalue
with open(f, "w") as configfile:
config.write(configfile)
except configparser.MissingSectionHeaderError:
continue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment