Writing plugins¶
Everything that a stage can contain comes from a plugin, including the content that ships with Nyx. This section shows how to add each kind of content:
| I want to add | Page |
|---|---|
| A new kind of node | New node |
| A node that calls a Python function | New function node |
| A new kind of component (a plug on a node) | New component |
| A custom editor for a component in the UI | New QML for a component |
| A new datatype | New datatype |
| A note-like object that doesn't execute | New util object |
| A custom drawing for a util object | New graphics object |
Import from nyx.api.v1 only. It is the public, versioned API; nyx.core and
nyx.editor can change without notice. Look at nyx/stdlib (the core plugin) and
nyx/plugins/functions/* for working examples of everything below.
Anatomy of a plugin¶
A plugin is a folder containing a nyx_plugin.py. The file defines one subclass of
NyxPlugin and registers it with a decorator.
my_plugins/ <- put this folder in plugin_paths or NYX_PLUGIN_PATH
└── studio_tools/
├── nyx_plugin.py <- found by the loader (any depth below the path)
├── nodes/
│ └── greeter.py
├── components/
│ └── color.py
├── qml/ <- QML import root of the plugin
│ └── StudioTools/ <- QML module, named by QML_MODULE_NAME
│ ├── qmldir
│ └── ColorComponentItem.qml
└── resources/
└── icons/
# studio_tools/nyx_plugin.py
import typing
from nyx.api.v1 import NyxPlugin, NyxPluginManager
from .nodes.greeter import GreeterNode
from .components.color import ColorComponent
@NyxPluginManager.register_plugin()
class StudioTools(NyxPlugin):
"""Tools of the studio."""
QML_MODULE_NAME = "StudioTools"
@classmethod
def get_type_id(cls) -> str:
"""str: unique plugin id."""
return "StudioTools"
@classmethod
def get_palette_name(cls) -> str:
"""str: name of the section of the palette holding the types of the plugin."""
return "Studio"
@classmethod
def initialize(cls) -> typing.Self:
"""Create the plugin and list its content.
Returns:
typing.Self: initialized plugin.
"""
plugin = cls()
plugin.contents.node_components = [ColorComponent]
plugin.contents.nodes = [GreeterNode]
return plugin
The pieces:
@NyxPluginManager.register_plugin()registers the class when the file is imported. A plugin with noget_type_id, or with an id that is already taken, is logged and skipped.get_type_id()is the unique id of the plugin. It is stored in saved stages (source_plugin), so don't change it once stages exist.get_type_display_name()(defaults to the id) andget_palette_name()(defaults to the display name) are used by the UI.get_tags()can return a set of tags.initialize()is called once at startup. Create the plugin and fillplugin.contents(NyxPluginContents):
| Field | Holds |
|---|---|
nodes |
node classes |
node_components |
component classes |
datatypes |
Datatype instances |
util_objects |
util object classes |
graphics_objects |
graphics item classes (QGraphicsItem subclasses with a get_type_id) |
register_function_node(func) |
method that generates a node class from a function and adds it to nodes |
register_function_module(module, namespace) |
the same for every public function of a module |
QML_MODULE_NAMEis the name of the QML module in the plugin'sqmlfolder, needed only when a component has a custom QML editor.
The plugin class also helps locating files next to it: root_directory(),
qml_directory(), resources_directory(), icons_directory() and get_icon(name).
Where plugins are found¶
NyxPluginLoader searches recursively for nyx_plugin.py in nyx.stdlib, the built-in
nyx/plugins, every directory in the plugin_paths config value, and every directory in
the NYX_PLUGIN_PATH environment variable. See The registry for what
happens next.
NYX_PLUGIN_PATH=/studio/nyx_plugins nyx
NYX_PLUGIN_PATH=/studio/nyx_plugins nyx stage run nightly.nyx
Because the plugin file is loaded as part of a package named after its folder, use
relative imports between your files (from .nodes.greeter import GreeterNode), and keep
the folder name unique among your plugins.
Type ids¶
Every node, component, datatype, util object and graphics object has a type id: a string stored in saved stages. Rules to follow:
- Use
namespace:name, with a namespace of your own (studio:greeter).core:andfunction:are taken. - They have to be unique per category; a duplicate is logged and the first one wins.
- They are part of your file format. Renaming one breaks stages that use it.
Icons¶
get_type_icon() returns a Qt resource path such as :/icons/function. The built-in
icons (:/icons/..., :/core/icons/...) are available to every plugin. To ship your
own, add a .qrc file, compile it with pyside6-rcc and import the generated module
from your nyx_plugin.py (the stdlib does from .resources import resources_rc). Use a
unique prefix in the .qrc so paths don't clash.
The built-in icons are white shapes on a transparent background, 24 or 32 pixels. To get
one that matches from a picture of your own (a PNG, an SVG, a black drawing on white),
make it with editor_utils.create_mono_icon(path): the shape is found from the
transparency, or from the dark parts of an image without any (invert=True takes the
light parts), and drawn in white (or the color you give) at the right size.
from nyx.api.v1 import editor_utils
icon = editor_utils.create_mono_icon("studio_logo.png") # QtGui.QIcon
image = editor_utils.create_mono_image("studio_logo.png", 32) # QtGui.QImage
Editor helpers¶
nyx.api.v1.editor_utils has what plugins that add to the editor tend to need. Most of
it needs a running QApplication, and get_current_stage_graph the editor window, so
use it from code that runs in the editor, not in a headless run.
| Function | What it does |
|---|---|
create_mono_icon(path, size, color, invert) |
White one-color QIcon from an image file, see Icons. |
create_mono_image(path, size, color, invert) |
The same as a QImage. |
get_application_qml_engine() |
The QML engine of the application, for your own QML items. |
get_current_stage_graph() |
The stage graph editor of the active window. |
blocked_signals(*objects) |
Context manager that blocks the signals of Qt objects and restores them after. |
resolve_to_source_index(index) |
Maps an index through proxy models down to the source model. |
note_to_html(text, note_format) |
Converts a plain, Markdown or HTML note to HTML for tooltips and labels. |
build_component_tooltip_html(component) |
The tooltip the editor shows for a component. |
build_role_tooltip_html(index) |
An HTML table of every role of a model index, for debugging models. |
Testing a plugin¶
The repository's test suite shows the pattern. tests/conftest.py starts the
application offscreen, and the with_plugins marker initializes the plugins named in it:
import pytest
from nyx.api.v1 import Stage, commands
@pytest.mark.with_plugins("NyxStdlib", "StudioTools")
def test_greeter(nyx_app):
stage = Stage() # keep the reference: commands hold their stage weakly
command = commands.CreateNodeCommand(stage=stage, type_id="studio:greeter")
stage.push_command(command)
greeter = command.result_node
stage.crawler.start(greeter)
assert greeter.greeting_cpnt.pull_value() == "Hello, World."
Make sure your plugin folder is on NYX_PLUGIN_PATH when the tests run so the loader
finds it. Every change to a stage goes through a command (push_command), which is also
what the editor does, so undo works for it. tests/stage_helpers.py has small helpers
that wrap this.
Checklist¶
- [ ]
nyx_plugin.pyregisters exactly oneNyxPluginsubclass, with a unique id. - [ ] Type ids have your namespace.
- [ ] Imports come from
nyx.api.v1. - [ ] Every class has
get_type_id,get_type_display_nameand aget_description. - [ ] Nodes work when their stage is saved and loaded again, see New node.
- [ ] Google-style docstrings (
Args:,Returns:,Raises:), they become the descriptions in the UI for function nodes.