Work with layers by adding, modifying, and deleting
The scripting API for layers allows you to create, edit, and delete layers. Most of the commands are accessible with either using actions or through the Krita Node API.
There are a few different ways to access layers.
Another common scenario is going through all layers and doing stuff. This is a simple use case of going through all layers and locking them.
While this code will work great if we only have one level, it will not go into group layers and update those children. We will need to just add a bit more code to go inside those and do a similar thing
This is just doing more loops inside of loops looking through children. This will go one level deep. Of course there could be two, three, or more levels deep of children. We won't go into all the ways this could work, but you would have to create a recursive function that would keep going deeper as long as there are more children.
Now that you know a few ways to access layers, you probably want to do things with them. For simple operations, there are many things you can do with simply using actions
When you use actions like above, It is important to know that the action is done ONLY on the currently selected layer. When you grab a layer, you first need to select it as the active layer. Then you can perform the actions. Like this.
We have the ability to access and change a lot of the layer's properties with more precision. In this next example, we are going to create a document, then change the opacity of the current layer.
After the document is created, we grab active layer, called a “node” and change the opacity. Notice the last line with “refreshProjection()”. If that line is not there, you would not see your canvas updated on screen. The reason this exists is you might want to do 5-10 things with the canvas. It is much faster if Krita can do everything first before it spends time drawing all those pixels on the canvas.
There are a lot of properties you can change from the Krita Node API. See this API reference to see all the available things you can do with layers/nodes.
We went over how to grab and perform actions on layers. Deleting a layer is just another action. This is how you would find a layer you want to delete, then delete it.
There are a lot of actions that can be done with layers. To see a list of them all, head over to the action dictionary page and do a search for "layer". use the id for the action name in your scripting.
Head to the Krita artists where we have a dedicated area for plugin development and give any feedback you might have.