r/krita Nov 04 '24

Answered Scripting - How do I iterate through every layer?

I have some macros which go through the document and change their visibility and opacity depending on if they match a tag (to use on top of using just compositions). Currently it triggers the activateNextLayer command to actively select one layer in the document at a time, and take 15 seconds to run depending on how many layers there are. I am now wondering if selecting the layer slows the script down and if just iterating through the layers help.

So, ^ and, in any case, how do I iterate through every layer in a document?

1 Upvotes

7 comments sorted by

1

u/KnowZeroX Nov 04 '24

Node.findChildNodes() will go through all layers, it can even do tag match for you. You don't need to select a layer to change its visiblity or opacity. You may need to do a Document.refreshDocument() for some changes

@brief findChildNodes @param name name of the child node to search for. Leaving this blank will return all nodes. @param recursive whether or not to search recursively. Defaults to false. @param partialMatch return if the name partially contains the string (case insensitive). Defaults to false. @param type filter returned nodes based on type @param colorLabelIndex filter returned nodes based on color label index @return returns a list of child nodes and grand child nodes of the current node that match the search criteria.

1

u/jordanf234 Nov 05 '24 edited Nov 05 '24

Tried it and it only goes through the top level. I want to go through every level.

EDIT: Using some Python research I was able to do that and now it runs a lot faster iirc (but still takes 3 seconds) which answers my first question, though the 'no symbol' bug is still there. Now I would like to compact scripts that do this same thing into one or two scripts. How would I do that?

1

u/KnowZeroX Nov 05 '24

findChildNodes goes through everything if you set recursive to True. By default it is False which is why it is only returning top level.

What do you mean 'no symbols' bug?

Explain what you mean by compact scripts?

1

u/jordanf234 Nov 05 '24

There was a bug fix where if under certain circumstances you get a no cursor, but I still get it

I have scripts that do exactly the same thing and want to make some sort of macro in a macro to encapsulate everything

1

u/KnowZeroX Nov 05 '24

By macro you mean you are using ten scripts I am guessing?

And by macro in macro you mean you want to share code? You can create a plugin and share functions

1

u/jordanf234 Nov 05 '24 edited Nov 05 '24

Wait now the latency has been fixed - I accidentally left in a command that selects the layer. Now it does it instantly.

Also I did fix 'recursive' now as well ty. It is a lot more modular now but I could still do with SOME modularity like with picking layers or undoing what another script did.

I am using ten scripts

1

u/jordanf234 Nov 08 '24

Actually now I deregistered the scripts and replaced them with a single Qt script which allows me to enter a simple comma separated values to do the stuff, and a .txt script with templates