Skip to content

Instantly share code, notes, and snippets.

@howardjones
Last active November 16, 2021 21:41
Show Gist options
  • Save howardjones/7f832b665d412235e9f645235ac7f965 to your computer and use it in GitHub Desktop.
Save howardjones/7f832b665d412235e9f645235ac7f965 to your computer and use it in GitHub Desktop.
extract the cura settings from a gcode file
import json
import ast
from codecs import encode, decode
with open("CFFFP_Drawer_ThinHandle_TOLERANT.gcode", "r", encoding="utf-8") as f:
settings_lines = [
line.strip()[11:] for line in f.readlines() if line.startswith(";SETTING_3 ")
]
data = json.loads("".join(settings_lines))
for k, v in data.items():
print("\n;----------------------------------------\n; " + k + "\n")
if not isinstance(v, list):
v = [v]
for vv in v:
config = decode(encode(vv, "latin-1", "backslashreplace"), "unicode-escape")
print(";--\n")
print(config)

Cura embeds the settings used to generate a gcode file in comments in the bottom of the file. For some reason, they are in a funny format though - a comment, with a prefix, containing a hard-wrapped JSON object with a few properties, each of which is a double-escaped string!

This is a small script to strip the comments back out and do the various concatenations and decodings to show you what the settings were.

M82 ;absolute extrusion mode
M104 S0
;End of Gcode
;SETTING_3 {"global_quality": "[general]\\nversion = 4\\nname = Draft #2\\ndefin
;SETTING_3 ition = custom\\n\\n[metadata]\\ntype = quality_changes\\nquality_typ
;SETTING_3 e = draft\\nsetting_version = 17\\n\\n[values]\\nadhesion_type = skir
;SETTING_3 t\\nadhesion_z_offset = 0\\nadhesion_z_offset_extensive_processing = 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment