Writing an OpenCreator plugin¶
Copy the example plugin. Its README lists every manifest key. In short:
{
"id": "hello",
"name": "Hello OpenCreator",
"version": "1.0.0",
"klippy_extras": ["klippy/oc_hello.py"],
"klipper_config": "config/hello.cfg",
"moonraker_components": ["moonraker/oc_hello.py"],
"moonraker_config": "moonraker/hello.conf"
}
klippy_extras: Klippy modules, the same as a file inklippy/extras. A module is loaded by the config section of the same name, sooc_hello.pyis loaded by[oc_hello].klipper_config: one Klipper config file, with your module's section and macros. It's included after the rest ofprinter.cfg, so it can add to OpenCreator's sections.moonraker_componentsandmoonraker_config: the same, for Moonraker.restart: optional,["klipper"],["moonraker"]or both, for a plugin that needs a restart without having those parts itself, for example one whose install script changes a config file.
id, name and version are required. The id is 2 to 41 characters: lowercase letters, digits, - and _,
starting with a letter or digit. Module file names are lowercase letters, digits and _, ending in .py. Every
path is relative and must stay inside the plugin folder, and the file must exist. A manifest that breaks one of
these rules is refused with a message naming the key.
Module names must be unique. A plugin whose module has the same name as one of Kalico's, or as another plugin's, is refused and isn't installed. Give yours a prefix.
Startup items and scripts¶
A plugin can also run its own programs:
"startup": [{ "name": "server", "exec": "bin/server.sh", "type": "service" }],
"scripts": { "install": "bin/install.sh", "update": "bin/install.sh", "uninstall": "bin/uninstall.sh" }
startup: programs started when the plugin is installed or enabled and at every boot, and stopped when it's disabled or removed. Aserviceis started again if it exits (5 s later, backing off to a minute while it keeps failing). Aonceitem runs at boot and stops.argsanddelay(up to 600 s) are optional.scripts:installruns before anything is linked, and the plugin isn't installed if it fails.updateruns afteroc-plugin updatepulls a new version, anduninstallbeforeoc-plugin remove. Each gets up to 15 minutes.
Both run in the plugin folder with OC_PLUGIN_ID, OC_PLUGIN_DIR, OC_PLUGIN_DATA, OC_CONFIG_DIR,
OC_KLIPPER_DIR, OC_MOONRAKER_URL, OC_INSTALL_MODE (soc or sbc) and OC_TUNNELED set, and /opt/bin
on the PATH. A file ending in .sh, or one that isn't executable, is run with sh. Startup item names
follow the same rule as plugin ids. Stopping an item sends it SIGTERM, then SIGKILL if it's still running
5 seconds later. OC_PLUGIN_DATA is a folder for the plugin's own files, and it's
kept across updates. Startup items log to /tmp/oc-plugin-<id>-<name>.log, which oc-plugin logs shows. On
the printer, /tmp is in RAM, so a log is moved aside to .log.1 once it passes 512 KB.
Compatibility¶
models:Creator5and/orCreator5Pro, for hardware only one of them has, like the Pro's chamber heater. The model comes from the stock boot script, or fromprinter.model.cfgon SBC installs. If it can't be told,oc-pluginonly warns.modes:soc(Klipper on the printer itself) and/orsbc(Klipper on a Raspberry Pi or other SBC, including a printer tunneled to one).
Leave requires out if the plugin runs everywhere.
To try a plugin while writing it, install the folder straight from disk: oc-plugin install /usr/data/my-plugin.
That's a copy, so install it again after each change, and it isn't offered in the update manager.
How it works¶
Plugins are kept in /usr/data/opencreator/plugins/<id> (a git checkout). Nothing from a plugin is copied into
Kalico or Moonraker. oc-plugin sync links it in:
| Plugin part | Where it goes |
|---|---|
klippy_extras |
Symlinked into Kalico's klippy/plugins/ (Klipper without that folder: klippy/extras/) |
klipper_config |
Symlinked as plugins/<id>.cfg next to printer.cfg, which includes [include plugins/*.cfg] |
moonraker_components |
Symlinked into Moonraker's moonraker/components/ |
moonraker_config |
Copied, with the plugin's [update_manager] section, to plugins/moonraker/<id>.conf, included by moonraker.conf with [include plugins/moonraker/*.conf] |
The generated Moonraker file starts with a comment saying it's written by oc-plugin: edit the plugin's own
moonraker_config instead, since the copy is replaced at every sync.
Besides the plugin checkouts, the plugins folder holds:
| Path | What it is |
|---|---|
plugins/state.json |
Which plugins are disabled |
plugins/.data/<id>/ |
Each plugin's OC_PLUGIN_DATA, kept across updates and reinstalls |
plugins/.run/ |
The startup items' pid files |
plugins/.catalog.json |
The last catalog downloaded |
plugins/.moonraker-url |
Tunneled printers: the SBC's Moonraker |
Kalico's klippy/plugins/ is git-ignored, so Kalico updates leave plugin links alone. Kalico also refuses to start
if a module there has the name of one of its own.
oc-plugin only removes or replaces links that point into the plugins folder. A file it didn't make is never
touched. If something is in its way, it reports that and the plugin isn't installed. The two include lines
are added to printer.cfg and moonraker.conf once, if they're missing. In printer.cfg the line goes
before the SAVE_CONFIG block. plugins/moonraker/00-oc-plugins.conf only holds a comment. It's there
because Moonraker, unlike Klipper, refuses an include that matches no files.
On the printer, 10-klipper.sh runs oc-plugin sync --only klipper before every Klippy start, and 20-msmr.sh runs
oc-plugin sync --only moonraker before every Moonraker start. This is why an update from Fluidd, which only does a
git pull and restarts, still links any file the new version added. oc-plugin itself lives in
/usr/data/opencreator/plugin-manager/, which the installer and the OpenCreator updater keep up to date.
10-klipper.sh links it into /opt/bin.
At boot, startup.sh runs oc-plugin boot in the background, which starts every enabled plugin's startup items
and refreshes the catalog for the marketplace dialog. Its output goes to /tmp/oc-plugin-boot.log. See
Boot and services for where that sits in the boot.
Tunneled printers¶
A tunneled printer counts as an SBC install, so it takes plugins made for sbc. oc-plugin runs on the
printer as usual, and tunneled.sh starts the plugins' startup items at boot. Klipper and Moonraker are on the
SBC, so a plugin's Klipper and Moonraker parts aren't linked on the printer, and nothing is restarted. Startup
items and scripts get OC_TUNNELED=1.
Plugins that talk to Moonraker get the SBC's as OC_MOONRAKER_URL. oc-plugin moonraker --find finds it: it
keeps the saved address if it still answers, and otherwise tries port 7125 on every address in the printer's
own networks (neighbours it already knows first). It only accepts an OpenCreator tunnel SBC, one whose
Moonraker has the opencreator-updater update entry or whose Klipper's MCUs are on /dev/ttyGS*. With several
tunneled printers on one network it lists them all with their hostnames. Over SSH it asks which one is this
printer's; run by a plugin it stops and says so. Pick one with oc-plugin moonraker --scan, or set it by hand
with oc-plugin moonraker http://<sbc>:7125. The choice is saved in plugins/.moonraker-url.
Running oc-plugin on the SBC¶
oc-plugin also works on a Raspberry Pi running Klipper. There, the default folders are
~/opencreator-plugins, ~/klipper, ~/printer_data/config and ~/moonraker. Nothing runs sync at each
start there, so run oc-plugin sync after updating a plugin from the update manager. To use other folders,
set OC_PLUGINS_DIR, OC_KLIPPER_DIR, OC_CONFIG_DIR and OC_MOONRAKER_DIR, plus OC_MOONRAKER_URL for the
restarts.
For the catalog entry needed to distribute a plugin, see Plugin catalog. Installation errors and service logs are covered in Using plugins.