2.0 Interacting with the Scenegraph

The scenegraph is a tree structure that represents the user’s document. You can use XD’s scenegraph module to manipulate the scenegraph. However, while doing so, there are some important guidelines to consider.

2.1 Don’t be destructive without permission

Your plugin shouldn’t remove content from the scenegraph without the user’s permission. If an action is destructive, always ask for permission using a modal dialog first, and ensure that the button you use is a warning variant.

2.2 Handle selections (or lack thereof) properly

Your plugin may be invoked in a variety of conditions. Your plugin should never fail silently — this gives the user the impression that they weren’t “heard” or “understood,” and will likely trigger the invocation again. The user may become confused as to why the plugin works in certain conditions and fails in others and blame your plugin, XD, or even blame the combination of installed plugins.

It’s also possible that your plugin can modify the user’s selection. (For example, commands.group). Your plugin should not do this unless it is something the user will expect.

Finally, there may be cases where the selection changes between asynchronous execution phases in your plugin. You should ensure that you capture the initial selection separately and then compare the current selection before making changes.

2.3 Working with Artboards (or lack thereof)

Your plugin should be able to handle the condition where there is no artboard, one artboard, or multiple artboards present in the document.

2.4 Working with the Edit Context

The edit context is the editable context within the scenegraph. Editing nodes outside the edit context will throw an exception. Your plugin should handle these exceptions if they occur.

2.5 Handle gradients and other fills

When working with shapes, it may be tempting to extract the color values without checking the type of fill. Doing so may cause your plugin to fail silently. What you do with non-solid colors is up to you, but your plugin shouldn't fail silently.

2.9 Know when to use asynchronous logic

If your plugin can execute quickly and has no need of I/O or UI, then you can modify the scenegraph synchronously. This is the easiest way to build a plugin. However, if your plugin takes a considerable amount of time to operate or uses any asynchronous API (and then needs to work with the scenegraph), you’ll need to use XD’s asynchronous scenegraph handling.