Edit in GitHubLog an issue

scenegraph

The scenegraph is a node tree which represents the structure of the XD document. It closely matches the hierarchy seen in the Layers panel inside XD. Some scenenodes may contain children (e.g., a Group or Artboard), while others are leaf nodes (e.g., a Rectangle or Text node). The root of the scenegraph contains all Artboards that exist in the document, as well as all pasteboard content (nodes that are not contained by any artboard).

example of scenegraph tree

You can modify properties on any scenenodes within the current edit context, and add leaf nodes to the current edit context, but you cannot make structural changes directly to the scenegraph tree. Instead, use commands.

Typically, you access scenegraph nodes via the selection argument that is passed to your plugin command, or by traversing the entire document tree using the documentRoot argument that is passed to your plugin command. These objects are also accessible on the scenegraph module for convenience.

Example

Copied to your clipboard
1function myCommand(selection) {
2 let node = selection.items[0];
3
4 console.log("The selected node is a: " + node.constructor.name);
5
6 // Print out types of all child nodes (if any)
7 node.children.forEach(function (childNode, i) {
8 console.log("Child " + i + " is a " + childNode.constructor.name);
9 });
10}

To create new scenenodes, load this scenegraph module directly and use the node constructor functions:

Example

Copied to your clipboard
1let scenegraph = require("scenegraph");
2function myCommand(selection) {
3 let newShape = new scenegraph.Rectangle();
4 newShape.width = 100;
5 newShape.height = 50;
6 newShape.fill = new Color("red");
7 selection.insertionParent.addChild(newShape);
8}

Class hierarchy#

Tip Additional subclasses may be added in the future. Always be sure to have a default case for unknown scenenode classes when traversing the scenegraph.

Typedefs#

Typedef Point#

  • Point: {x:number, y:number}

Typedef Bounds#

  • Bounds: {x:number, y:number, width:number, height:number}

selection#

selection: selection

Since: XD 14

Object representing the current selection state and edit context. Also available as the first argument passed to your plugin command handler function.

Kind: static property of scenegraph Read only: true

root#

root: RootNode

Since: XD 14

Root node of the current document's scenegraph. Also available as the second argument passed to your plugin command handler function.

Kind: static property of scenegraph Read only: true

getNodeByGUID()#

getNodeByGUID(guid): SceneNode

Since: XD 28

Returns the scenenode in this document that has the given node GUID. Returns undefined if no such node exists connected to the scenegraph tree (detached/orphan nodes will not be found). This provides a fast way of persistently remembering a node across plugin operations and even across document open/closes.

ParamTypeDescription
guidstringSceneNode GUID -- must be all lowercase, as returned by the guid getter

Example

Copied to your clipboard
1let node = scenegraph.selection.items[0];
2let guid = node.guid;
3
4// ...later on:
5let sameNode = scenegraph.getNodeByGUID(guid);
6if (sameNode) {
7 // ^ Always check if node still exists - user may have deleted it
8 console.log("Found node again!", sameNode);
9}

Kind: static method of scenegraph

Was this helpful?
Copyright © 2021 Adobe. All rights reserved.