Skip to content

Instantly share code, notes, and snippets.

@daitj
Last active February 16, 2024 16:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daitj/eb2889c14b5b750626499018d8227164 to your computer and use it in GitHub Desktop.
Save daitj/eb2889c14b5b750626499018d8227164 to your computer and use it in GitHub Desktop.
Cura Ender 3 V3 KE Thumbnail Script
# Cura Ender 3 V3 KE Thumbnail creator
# daitj (github.com/daitj)
# Modified from Ken Huffman (huffmancoding@gmail.com) CreateV2NeoThumbnail
# This only works with Cura 5.0 or later
# Based on:
# https://github.com/Ultimaker/Cura/blob/master/plugins/PostProcessingPlugin/scripts/CreateThumbnail.py
#
# Usage:
# Add this script twice in Cura for 300x300 and 96x96
# first one for 300x300 and
# second one for 96x96, since thumbnail script moves the thumbnail to the top of the gcode,
# 96x96 will be moved to top after 300x300.
import base64
from UM.Logger import Logger
from cura.Snapshot import Snapshot
from PyQt6.QtCore import QByteArray, QIODevice, QBuffer
from ..Script import Script
class CreateEnder3V3KEThumbnail(Script):
def __init__(self):
super().__init__()
def _createSnapshot(self, width, height):
Logger.log("d", "Creating thumbnail image...")
try:
return Snapshot.snapshot(width, height)
except Exception:
Logger.logException("w", "Failed to create snapshot image")
def _encodeSnapshot(self, snapshot):
Logger.log("d", "Encoding thumbnail image...")
try:
thumbnail_buffer = QBuffer()
thumbnail_buffer.open(QBuffer.OpenModeFlag.ReadWrite)
thumbnail_image = snapshot
thumbnail_image.save(thumbnail_buffer, "PNG")
thumbnail_data = thumbnail_buffer.data()
thumbnail_length = thumbnail_data.length()
base64_bytes = base64.b64encode(thumbnail_data)
base64_message = base64_bytes.decode('ascii')
thumbnail_buffer.close()
Logger.log("d", "Snapshot thumbnail_length={}".format(thumbnail_length))
return (base64_message, thumbnail_length)
except Exception:
Logger.logException("w", "Failed to encode snapshot image")
def _convertSnapshotToGcode(self, thumbnail_length, encoded_snapshot, width, height, chunk_size=76):
Logger.log("d", "Converting snapshot into gcode...")
gcode = []
# these numbers appear to be related to image size
# calculations are roughly like this
x1 = ((int)(width/5)) - 1
x2 = width - (x1 + 1)
header = "; png begin {}*{} {} {} {} {}".format(
width, height, thumbnail_length, x1, x2, 341)
Logger.log("d", "Gcode header={}".format(header))
gcode.append(header)
chunks = ["; {}".format(encoded_snapshot[i:i+chunk_size])
for i in range(0, len(encoded_snapshot), chunk_size)]
gcode.extend(chunks)
gcode.append("; png end")
gcode.append(";")
gcode.append("")
return gcode
def getSettingDataString(self):
return """{
"name": "Create Thumbnail (Ender 3 V3 KE)",
"key": "CreateEnder3V3KEThumbnail",
"metadata": {},
"version": 2,
"settings":
{
"width":
{
"label": "Width",
"description": "Width of the generated thumbnail",
"unit": "px",
"type": "int",
"default_value": 96,
"minimum_value": "0",
"minimum_value_warning": "12",
"maximum_value_warning": "800"
},
"height":
{
"label": "Height",
"description": "Height of the generated thumbnail",
"unit": "px",
"type": "int",
"default_value": 96,
"minimum_value": "0",
"minimum_value_warning": "12",
"maximum_value_warning": "600"
}
}
}"""
def execute(self, data):
width = self.getSettingValueByKey("width")
height = self.getSettingValueByKey("height")
Logger.log("d", "CreateEnder3V3KEThumbnail Plugin start with width={}, height={}...".format(width, height))
snapshot = self._createSnapshot(width, height)
if snapshot:
Logger.log("d", "Snapshot created")
(encoded_snapshot, thumbnail_length) = self._encodeSnapshot(snapshot)
snapshot_gcode = self._convertSnapshotToGcode(
thumbnail_length, encoded_snapshot, width, height)
Logger.log("d", "Layer count={}".format(len(data)))
if len(data) > 0:
# The Ender-3 V3 KE really wants this at the top of the file
layer_index = 0
lines = data[layer_index].split("\n")
Logger.log("d", "Adding snapshot gcode lines (len={}) before '{}'".format(len(snapshot_gcode), lines[0]))
lines[0:0] = snapshot_gcode
final_lines = "\n".join(lines)
data[layer_index] = final_lines
Logger.log("d", "CreateEnder3V3KEThumbnail Plugin end")
return data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment