Thumbnail and print time via python script

Thumbnail and print time via python script

Hi everyone,

After some tinkering with the Snapmaker Artisan and OrcaSlicer, I wanted to share a solution I came up with (with some extended help from Gemini) to get everything working smoothly.

Specifically, this addresses how to make the printer correctly display both thumbnails (small/large for preview during printing) and show the accurate remaining print time on the machine.

The Core Finding

For the Snapmaker firmware/touchscreen (Vision board) to properly read the metadata, it relies on a specific header block structure at the beginning of the G-code file:

;Header Start
;thumbnail: \[base64 data\]
;estimated_time(s): \[seconds\]
;Header End

The Python Script

To automate this, I created a small Python script that processes the exported G-code from OrcaSlicer, extracts the thumbnail blocks and the calculated printing time, and rewrites them into the required Snapmaker header format.

import sys
import os
import re

def process_gcode(file_path):
    with open(file_path, 'r', encoding='utf-8') as f:
        content = f.read()

    total_seconds = 0
    time_match = re.search(r';\s*estimated printing time \(normal mode\)\s*=\s*(?:(\d+)h)?\s*(?:(\d+)m)?\s*(?:(\d+)s)?', content, re.IGNORECASE)
    
    if time_match:
        h = int(time_match.group(1)) if time_match.group(1) else 0
        m = int(time_match.group(2)) if time_match.group(2) else 0
        s = int(time_match.group(3)) if time_match.group(3) else 0
        total_seconds = h * 3600 + m * 60 + s
    else:
        time_match_sec = re.search(r';\s*estimated_time\(s\):\s*([\d\.]+)', content, re.IGNORECASE)
        if time_match_sec:
            total_seconds = int(float(time_match_sec.group(1)))

    block_pattern = r'; THUMBNAIL_BLOCK_START.*?; THUMBNAIL_BLOCK_END'
    block_match = re.search(block_pattern, content, re.DOTALL)
    
    if not block_match:
        return

    full_block_text = block_match.group(0)
    
    base64_fragments = []
    for line in full_block_text.split('\n'):
        clean_line = line.strip()
        if not clean_line or 'THUMBNAIL_BLOCK' in clean_line or 'thumbnail begin' in clean_line or 'thumbnail end' in clean_line:
            continue
        if clean_line.startswith(';'):
            payload = clean_line[1:].strip()
            if payload:
                base64_fragments.append(payload)

    full_base64_string = "".join(base64_fragments)

    if not full_base64_string:
        print("Keine Bilddaten im Block gefunden.")
        return

    snapmaker_header_block = (
        f";Header Start\n"
        f";thumbnail: data:image/png;base64,{full_base64_string}\n"
        f";estimated_time(s): {total_seconds}.00\n"
        f";Header End\n"
    )

    new_content = content.replace(full_block_text, snapmaker_header_block)

    with open(file_path, 'w', encoding='utf-8') as f:
        f.write(new_content)

if __name__ == '__main__':
    if len(sys.argv) > 1:
        gcode_file = sys.argv[1]
        if os.path.exists(gcode_file):
            process_gcode(gcode_file)

How to Setup in OrcaSlicer (Post-Processing)

1. Create a Virtual Environment (recommended):

Open your terminal/command prompt and set up a local python environment:

   python -m venv .venv

2. Configure Post-Processing in OrcaSlicer:

In your printer settings under Post-processing scripts, add the following path pointing to your Python executable and script (adjust paths to match your system):

"C:\\path\\to\\.venv\\Scripts\\python.exe" "C:\\path\\to\\script\\snapmaker_thumbnail.py";

(Note: Make sure your OrcaSlicer thumbnail settings are set up, e.g., to 600x600, so the `THUMBNAIL_BLOCK` is generated).

Hope this helps anyone struggling with missing previews or incorrect print timers when switching over to OrcaSlicer! Let me know if you have any questions or improvements.

nice find on the header block honestly. wonder if the u1’s screen reads the same format