Gyre’s Smart Layers API provides advanced, non-destructive layer handling similar to “Smart Objects” in other image editing software. Each layer in Gyre is a Smart Layer, allowing for embedding, linking, transformations, and seamless updates. This SDK guide explains the core functionality, methods, and best practices for leveraging Gyre’s Smart Layers API in your applications.
To use Gyre Smart Layers, use the createLayerInstance
method from Gyre’s global API to create instances of layers directly.
globalThis..;
name
(string
): The name of the new layer.type
(string
): The type of the layer (e.g., image, mask).layerFrom
(object
, optional): A predefined object containing layer data. This allows initializing the new layer with existing properties.The result will be an instance of layerBase
, which provides access to the full set of methods for layer manipulation.
Instead of importing layerBase
directly, you create layers using the globalThis.gyre.createLayerInstance()
method. This method abstracts the creation of new layers and ensures that all references and dependencies are properly initialized.
let layer = globalThis..;
For creating custom layer types or special plugins, you should create a new class that extends from the base layer class provided by Gyre. This ensures your custom layers inherit all the non-destructive features while allowing you to extend the functionality as needed.
To extend the base class, use:
Using this approach, you can develop custom layers that integrate seamlessly with Gyre’s core functionality.
Here are some essential properties that are part of the layerBase
:
Dimensions:
width
, height
- External get/set dimensions that may include referenced values.Position and Transformation:
x
, y
- Set or get the layer’s position within the document.rotation
- Set or get the layer’s rotation angle.Layer Metadata:
name
- Custom name for the layer.type
, subType
- Specifies the type (e.g., mask, image).opacity
- Adjusts the opacity of the layer (default is 100
).References:
ref
- Points to another layer for linking attributes.refType
- Indicates the reference type (e.g., moveTool
). Empty refType uses all properties - also the image data. With moveTool
type only transformations are used but not the image data. So for example mask layers have refType
set to moveTool
because the image data itself are just the mask image data.getRefLayer()
Returns the root layer for the current layer’s reference chain.
getValueByRef(name)
Fetches a value from the linked reference layer, allowing properties to be dynamically inherited.
setValueByRef(name, value)
Sets a value on the linked reference layer, ensuring consistent updates.
toJSON()
Serializes the layer into a JSON object, excluding internal properties and non-serializable types.
unlink()
Unlinks the current layer from its reference (ref
), making it an independent layer.
prepareDelete()
Prepares a layer for deletion by properly unlinking all dependent references or clipping masks.
previewCache()
Generates and caches a preview of the layer. This is automatically triggered when properties change.
generateAlphaMask()
Generates an alpha mask and stores it for non-destructive use in masking. This function will be called automatically.
render()
Renders the current layer along with all masks, returning the rendered image’s URL.
renderInDocumentSize(maxWidth = null)
Renders the layer to fit the current document size, optionally scaling down via maxWidth
.
extractImageFromDocument(url)
Extracts a segment of the document based on the current layer’s size, position, and rotation.
clippingMask()
The clippingMask()
function creates a clipping mask from a referenced layer and stores the image data in this.url
from another layer (referred to as the clipping mask). This allows the clipping mask to behave like a standard layer mask, working specifically with the referenced image layer. This function will be called automatically.
Gyre has go very powerful mask management and supports multiple masks, clipping masks , additional objects selected by masks and any kind of non-destructive filters provided by ComfyUI workflows. A mask layer automatically has got same transformation (positipon, rotation and size) of it’s image layer.
Key elements:
children
array of such an image layer. Any image layer can have multiple masks/children."moveTool"
always.mask
always.Parts of an image can be extracted as objects. In this case additional linked layers are used on top of the original image layer. Each of such a linked layer has got it’s own mask. In this case an additional layer with type image
has to be generated but without an url
parameter. With the ref
parameter the image data is used from any the original image. The refType
is not set in this case. As child in children
array that new layer has got it’s own mask. Transformation on orgiginal image like rotation, postion or resize and changing the image data will be automatically applied to the object layer.
Use an Adjustment Layer to apply any filter (e.g. blur) to an mask.
To create a new layer:
let layer = globalThis..;
layer. = 500;
layer. = 300;
layer. = 100;
layer. = 150;
layer. = 0.1; // Rotate by 0.1 radians
You can also initialize a layer with existing data using layerFrom
:
let predefinedData = ;
let layer = globalThis..;
Gyre layers are inherently non-destructive, allowing you to modify properties like opacity
, width
, rotation
, and position
without altering the original image data:
layer. = 75; // Adjusts the opacity while keeping the original data intact
layer.; // Unlink to make the layer independent of any references
To render a layer including all applied masks and opacity settings:
let renderedUrl = await layer.; // Get a URL for the rendered image
Render a scaled preview:
let previewUrl = await layer.; // Render with a max width of 600 pixels
Use Layer References for Reusability: Leverage ref
to link layers for easy reuse across different documents, ensuring design consistency.
Custom Plugins with Extended Classes: When creating new layer types, extend the base class using globalThis.gyre.getLayerBaseClass()
. This allows you to build custom behavior while retaining the functionality of a standard Gyre layer.
class MySpecialLayer extends globalThis.gyre.getLayerBaseClass() {
constructor(name, type) {
super(name, type);
// Custom initialization
}
}
Caching Considerations:
nocache
flag to disable automatic image generation caching during operations like resizing to improve performance.false
which calls the previewCache()
automatically to update previews.Prepare for Deletions: Always use prepareDelete()
before removing layers to handle references and dependencies properly.