Skip to content

Instantly share code, notes, and snippets.

@EasyG0ing1
Created November 16, 2022 19:57
Show Gist options
  • Save EasyG0ing1/2b6208f244b0397e829ef4d792118993 to your computer and use it in GitHub Desktop.
Save EasyG0ing1/2b6208f244b0397e829ef4d792118993 to your computer and use it in GitHub Desktop.
Cura Insert Any GCode At Any Layer. Even add multiple lines if you separate them with a comma. From Cura, click on Help, Show Configuration Folder, then copy this file into the scripts folder, restart Cura and you're all done.
from ..Script import Script
import re
class RunBeforeLayer(Script):
def getSettingDataString(self) -> str:
return """
{
"name": "Run Before Layer",
"key": "RunBeforeLayer",
"metadata": {},
"version": 2,
"settings":
{
"run_layer":
{
"label": "Layer",
"description": "At what layer should the code run?",
"type": "int",
"default_value": "4"
},
"custom_gcode":
{
"label": "Code At Layer",
"description": "Any custom g-code to run before the layer, for example, M300 S440 P200 to beep. Separate multiple commands with a comma.",
"type": "str",
"default_value": "Code"
}
}
}
"""
def execute(self, data):
layer = self.getSettingValueByKey("run_layer") + 2
gcode = re.sub("\\s*,\\s*", "\n", self.getSettingValueByKey("custom_gcode"))
lines = data[layer].split("\n")
lines.insert(1, gcode)
data[layer] = "\n".join(lines)
return data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment