How can I iterate through all elements in the document - not just one node, but scan all of it?


#1

I have been trying to iterate through all elements of a document. This would include all Graphic nodes and group nodes. Is there a way to iterate through all children, something like root.AllChildren.foreach? Am I missing something here?


#2

Try that it works √ perfectly fine


#3

If you have root as the node whose children you’d like to iterate over, something like

root.children.forEach((child, index) => {
  // Do stuff here
});

should work (cf. https://adobexdplatform.com/plugin-docs/reference/SceneNodeList.html?h=scenenodelist). There is also – though that seems to be undocumented, so you might want to be careful with that – SceneNodeList::items, which lets you use a “standard” JS for-of loop:

for (let node of root.children.items) {
  // Do stuff here
}

This has the advantage that you can also use it in an asynchronous context without any further “complications”.

I hope this helps,
Happy Coding,
Pablo


#4

If you want to call a function on each node you could use the function here:

/**
 * Calls the passed in function on the passed in node and it's descendants 
 * @param {SceneNode} node SceneNode 
 * @param {Function} command Function to call on each node
 * @param {*} value Optional value to pass to command
 **/
function walkDownTree(node, command, value = null) {
  command(node, value);

  if (node.isContainer) {
    var childNodes = node.children;

    for(var i=0;i<childNodes.length;i++) {
      let childNode = childNodes.at(i);

      walkDownTree(childNode, command, value);
    }
  }
}

You would create your own function and it would be called on each node:

function showNodeName(node) { 
     console.log("Node name: " + node.name);
}

walkDownTree(root, showNodeName);

#5

Thanks Velara! This is exactly what I was looking for.