How to get page names by traversing the scene nodes?

Hi there! I am trying to make a plugin which need to show instance lists along with their pages name, I did tried it out but the for loop function is very slow. Any suggestions with these code below?

for (const node of nodes) { for (const page of figma.root.children) { if (page.findChild(n => n.type === 'INSTANCE' && n.id === node.id) ){ pageName = page.name; console.log(page.findAll(n => n.id === node.id)); console.log('found',pageName); } } pageNameArr.push(pageName); }

1 Like


code block here

The slowest line in your code is non-functional (meaning it is not a part of the algorithm, it just logs the info): page.findAll. However page.findChild is also a very slow function.

You are approaching this problem wrong. You need to find not “whether the page contains the node” (very slow method you are using now). You need to simply find the parent page of the node, and this is very easy to do with a while loop or a recursion. Just loop node.parent while it’s not of type "PAGE" and when it is, you just found the page.

3 Likes

Thank you for quick reply Gleb! I am a designer with basic level JavaScript knowledges. Could you please give some more code examples?

Hi Gleb! I tried with your suggestions and look like I make it! please review my code see if is what you mean ?

1 Like

Yep, this should work!

1 Like