Read PrusaSlicer binary G-code with the stdlib
How PrintStash reads metadata and thumbnails from PrusaSlicer .bgcode files without decoding the heatshrink-compressed toolpath.
PrusaSlicer can export binary G-code as .bgcode. The files are usually smaller than equivalent text G-code, but they need a different parser. PrintStash v0.7.3 added metadata and thumbnail extraction without decoding the toolpath itself.
What is inside a bgcode file
A libbgcode file starts with a header followed by typed blocks. Metadata blocks contain the slicer fields normally found in text G-code comments. Thumbnail blocks contain embedded PNG or JPEG images. The toolpath lives in separate heatshrink-compressed blocks.
The metadata and thumbnails are uncompressed or use zlib deflate. Python’s struct and zlib modules are enough to walk those blocks and extract the fields a library needs.
The reader collects slicer and printer information, temperatures, layer height, infill, filament estimates, time, material, and the largest usable thumbnail. It skips the compressed toolpath, so a large print does not turn metadata indexing into full G-code conversion.
What remains unsupported
PrintStash printer providers send plain-text G-code. They cannot safely send the heatshrink-compressed toolpath from a .bgcode file without a decoder and a verified conversion step.
The UI therefore shows the metadata and embedded thumbnail but does not offer a toolpath preview or printer send. The API rejects a send attempt too. You can still download the original file and open it in software that understands BGCODE.
That boundary is deliberate. Displaying metadata is a read-only indexing job. Converting and printing a toolpath has a much higher cost when the parser is wrong.
Hostile files
A length-prefixed container cannot be trusted to describe itself honestly. A truncated file may claim that its next block is several gigabytes long. The reader checks every length against configured limits and the bytes remaining in the file, then stops cleanly when the structure is invalid.
Tests cover malformed synthetic containers and a real PrusaSlicer fixture. That combination catches boundary mistakes without pretending a hand-built sample represents the whole format.
The reader stays within the library’s actual need: metadata and a thumbnail. Toolpath conversion can be added only when it has a tested decoder and a safe printer workflow.
BGCODE can still sit beside plain-text revisions in the G-code revision workflow, but choose a printable text revision before sending through a current provider.