Creating Custom Nodes
Relevant source files
The following files were used as context for generating this wiki page:
Nodes are the fundamental building blocks of Graphite's procedural graphics engine. The system is designed to be highly general, allowing any data type to be represented and processed through visually connected operations in a directed acyclic graph (DAG). Creating custom nodes allows developers to extend the editor's capabilities by adding new image adjustments, geometric transformations, or generative algorithms. Sources: node-graph/README.md:1-10, website/content/volunteer/guide/graphene/networks-and-nodes.md:21-25
Node Architecture and Implementation
In Graphite, a node can be implemented in two primary ways: as a subgraph (a collection of other nodes encapsulated into a single parent) or directly in Rust source code. Nodes implemented in Rust are the low-level primitives that perform actual data computation. Sources: website/content/volunteer/guide/graphene/networks-and-nodes.md:1-10
The Node Trait
At the core of the Graphene engine is the Node trait. This trait requires an eval function that processes input data and returns an output. Because the eval function only accepts one input, nodes requiring multiple parameters store references to other nodes within their struct, evaluating them during the execution of the primary node. Sources: node-graph/README.md:65-90
The diagram above shows the relationship between the base Node trait and a custom implementation that handles multiple inputs by referencing other nodes. Sources: node-graph/README.md:78-85
The node Macro
To simplify development and avoid manual trait implementation with complex generics, Graphite provides a #[node_macro::node] attribute. This macro automatically generates the necessary boilerplate, including:
- The Rust
structandNodetrait implementation. - The
node_registryentry for the interpreted executor. - The
DocumentNodedefinition for the UI. - The Properties panel layout.
Sources: node-graph/README.md:105-120
Document Nodes and UI Integration
The DocumentNode struct defines how a node behaves and appears within the editor's UI. It contains information about inputs, implementation paths, and visibility. Sources: node-graph/README.md:15-30
Document Node Metadata
Every node requires metadata to be properly categorized and displayed in the Node Catalog. Sources: tools/node-docs/src/page_node.rs:25-45, tools/node-docs/src/page_catalog.rs:45-55
| Field | Description | Source |
|---|---|---|
| Name | The display name shown in the UI (e.g., "Opacity"). | node-graph/README.md:32 |
| Category | Grouping for the Node Catalog (e.g., "Image Adjustments"). | node-graph/README.md:33 |
| Implementation | The path to the proto-node executor (e.g., graphene_core::raster::OpacityNode). | node-graph/README.md:34 |
| Inputs | A vector of DocumentInputType defining default values and visibility. | node-graph/README.md:35-38 |
| Properties | A function defining the widgets (sliders, input boxes) in the Properties panel. | node-graph/README.md:40 |
Input Configuration
Inputs are composed of a TaggedValue (for serialized dynamic types) and a boolean indicating if the input is "exposed" (shown as a connection dot in the graph) by default. Hidden inputs are modified only via the Properties panel. Sources: node-graph/README.md:46-55, tools/node-docs/src/page_node.rs:95-120
Execution Flow
When a user modifies the node graph, the document network undergoes a transformation process to become executable code. Sources: node-graph/README.md:130-140
The flow above describes the pipeline from a high-level NodeNetwork to the low-level BorrowTree where nodes are stored as type-erased objects (Box<dyn NodeIo + Node>) for dynamic dispatch. Sources: node-graph/README.md:130-165
Constructor Registration
Nodes must be registered with the interpreted executor using macros like register_node! or raster_node!. The latter is a specialized macro for nodes that operate on images, automatically inserting a MapImageNode to apply the custom logic to every pixel. Sources: node-graph/README.md:175-195
Property Widgets and Layouts
The Properties panel is defined through functions that return LayoutGroup vectors. Developers use helper functions like number_widget to create interactive controls for node parameters. Sources: node-graph/README.md:58-63
// Example Property Definition
pub fn multiply_opacity(document_node: &DocumentNode, node_id: NodeId, _context: &mut NodePropertiesContext) -> Vec<LayoutGroup> {
let factor = number_widget(document_node, node_id, 1, "Factor", NumberInput::default().min(0.).max(100.).unit("%"), true);
vec![LayoutGroup::Row { widgets: factor }]
}
Sources: node-graph/README.md:60-63
Summary
Creating custom nodes in Graphite involves defining the computational logic via the Node trait (often simplified by the #[node] macro) and providing the metadata necessary for the UI to render the node and its property widgets. The system leverages a flattening and proto-graph conversion process to turn visual connections into a performant, type-erased execution tree. Sources: node-graph/README.md:205-215, website/content/volunteer/guide/graphene/networks-and-nodes.md:30-40