Copy whole Root Node contents from one file to another

Hey,
I’m trying to copy whole RootNode from a .xd file, and save it somewhere for later. Then, I will create a new blank .xd file and rewrite whole RootNode with the one I saved before. Any ideas on how to program this?

Hi @samodostal,

There are a few (potential) problems with this. While it’s feasible to “serialize” the contents (from the root node down) one file (and theoretically transfer this serialized version to another file by using some storage solution like xd-storage-helper), you’ll encounter problems as soon as you try to “paste” your contents back in. This is because – due to the edit context restrictions – you won’t be able to insert nodes that are deeply nested. Since there’s no (realistic) way “around” these restrictions, I think that it is – unfortunately – not possible (at the moment) to fulfill your requirement with a plugin.

Apart from the edit context limitations, however, it should be relatively easy to achieve it:

  1. Serialize the scenegraph in file A – cf. Serialize scenegraph data to JSON?
  2. Save it to plugin storage – e.g., with the xd-storage-helper, cf. Helper library for key-value-based storage
  3. Load said data in another file (again, via the helper)
  4. “Insert” or overwrite scenegraph with the serialized scenegraph (this is where the edit context will stop you)

Although this probably isn’t the answer you’ve hoped for, I still hope it helps,
Happy Coding,
Pablo

1 Like

That sounds a lot like just copying the file.

Is there something else you’re trying to do?

@cpryland

From how I understand it, the goal would be to “only” copy/paste the scenegraph contents (without, e.g., modifying the assets in the assets panel) – or at least that’s the result of the question…

Apart from that, a key difference might be that copying a file requires file manipulation (actually saving the new file somewhere). This is more like copying contents from file A and pasting them into some opened document (may that get saved or not – this could also be a way to erperiment with a file and make variations without having to “save” the real file).

So what he wants is conceptually “Select All” on the root node, then “Copy” and then “Paste” into a new doc?

1 Like

@cpryland I think so :wink: – What it alsways comes down to, however, is that this requires the “pasting” (or better: overwriting) step to work (in whatever way). Without the edit context limitations, this would be an easy task – serialize on one end, overwrite everything on the other end. With the edit context, however, it is (without unfeasible stepps of ungrouping, regrouping etc., which might provide a hacky way around these limitations) impossible to perform this last step, may it consist of a “pasting-esque” or a “copy file-esque” way of doing things…

It’s not possible to create a new artboard programmatically through the API with nested groups and nested items? If you had serialized the root node into JSON you couldn’t deserialize it manually via the API?

Well, you can’t capture and restore everything; for example, a radial fill can’t be created from scratch, so you can’t serialize it. (Nor are the details available via the API.)

@Velara One case where this would, e.g., fail, are Repeat Grids. As we can’t create them programmatically (much less go “into” them due to the edit context), such a goal isn’t possible to achieve…