From c6a0ef242569a4534f863110de7b855cb8044327 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Thu, 23 Apr 2015 14:05:10 +0200 Subject: [PATCH] [doc] documented gcode scripts and snippets --- docs/features/gcode_scripts.rst | 96 ++++++++++++++++++++++++++++++++- 1 file changed, 94 insertions(+), 2 deletions(-) diff --git a/docs/features/gcode_scripts.rst b/docs/features/gcode_scripts.rst index c37fbe1a..8b154751 100644 --- a/docs/features/gcode_scripts.rst +++ b/docs/features/gcode_scripts.rst @@ -3,16 +3,108 @@ GCODE Scripts ============= -.. todo:: +.. contents:: - Needs to be written +OctoPrint allows you to define custom GCODE scripts to be executed on specified occasions, e.g. when a print +starts, when OctoPrint connects to a printer, or when a :ref:`button defined as a custom control ` +is clicked. + +Unless :ref:`configured otherwise `, OctoPrint expects scripts to be located in +the ``scripts/gcode`` folder in OctoPrint configuration directory (per default ``~/.octoprint`` on Linux, ``%APPDATA%\OctoPrint`` +on Windows and ``~/Library/Application Support/OctoPrint`` on MacOS). + +These GCODE scripts are backed by the templating engine `Jinja2 `_, allowing more than just +simple "send-as-is" scripts but making use of a full blown templating language in order to create your scripts. To +this end, OctoPrint injects a couple of variables into the :ref:`template rendering context ` +as described below. + +.. _sec-features-gcode_scripts-predefined: + +Predefined Scripts +------------------ + +The following GCODE scripts are sent by OctoPrint automatically: + + * ``afterPrinterConnected``: Sent after OctoPrint successfully connected to a printer. Defaults to an empty script. + * ``beforePrintStarted``: Sent just before a print job is started. Defaults to an empty script. + * ``afterPrintCancelled``: Sent just after a print job was cancelled. Defaults to the + :ref:`bundled script listed below `. + * ``afterPrintDone``: Sent just after a print job finished. Defaults to an empty script. + * ``afterPrintPaused``: Sent just after a print job was paused. Defaults to an empty script. + * ``beforePrintResumed``: Sent just before a print job is resumed. Defaults to an empty script. + +.. note:: + + Plugins may extend these scripts through :ref:`a hook `. + +.. _sec-features-gcode_scripts-snippets: + +Snippets +-------- + +For making small GCODE snippets reusable in a template (e.g. for :ref:`disabling all hotends `) +there's an additional Jinja template command ``{% snippet '' %}`` available which allows including +snippets stored under ``scripts/gcode/snippets`` in OctoPrint's configuration directory. They fully support +the whole spectrum of the Jinja2 templating language (that includes including other snippets). .. _sec-features-gcode_scripts-context: Context ------- +All GCODE scripts have access to the following template variables through the template context: + + * ``printer_profile``: The currently selected :ref:`Printer Profile <>`, including + information such as the extruder count, the build volume size, the filament diameter etc. + * ``script``: An object wrapping the script's type (``gcode``) and name (e.g. ``afterPrintCancelled``) as ``script.type`` + and ``script.name`` respectively. + +The :ref:`predefined GCODE scripts ` are also called with the following additional +template variables: + + * ``event``: The payload of the ``Connected``, ``PrintStarted``, ``PrintCancelled``, ``PrintDone``, ``PrintPaused`` or + ``PrintResumed`` event. See :ref:`the documentation of events ` for the contained values. + +GCODE scripts attached to :ref:`custom controls ` are called with the following +additional template variables: + + * ``parameters``: The parameters as defined for the custom control, if it has any inputs. + * ``context``: Additional ``context`` included in the definition of the custom control. + .. _sec-features-gcode_scripts-bundled: Bundled Scripts --------------- + +Out of the box, OctoPrint defaults to the following script setup for ``afterPrintCancelled``: + +.. code-block:: jinja + :caption: Default ``afterPrintCancelled`` script + + ; disable motors + M84 + + ;disable all heaters + {% snippet 'disable_hotends' %} + M140 S0 + + ;disable fan + M106 S0 + +The ``disable_hotends`` snippet is defined as follows: + +.. code-block:: jinja + :caption: Default ``disable_hotends`` snippet + + {% for tool in range(printer_profile.extruder.count) %} + M104 T{{ tool }} S0 + {% endfor %} + +As you can see, the ``disable_hotends`` snippet utilizes the ``printer_profile`` context variable in order to +iterate through all available extruders and set their temperature to 0. + +.. seealso:: + + `Jinja Template Designer Documentation `_ + Jinja's Template Designer Documentation describes the syntax and semantics of the template language used + also by OctoPrint's GCODE scripts. \ No newline at end of file