# Fill layer action fills incorrect layer

**URL:** <https://krita-artists.org/t/fill-layer-action-fills-incorrect-layer/82377>\
**Category:** Plugins Development\
**Created:** [January 15, 2024, 8:45pm UTC](https://krita-artists.org/t/fill-layer-action-fills-incorrect-layer/82377 "2024-01-15T20:45:00Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Karl\_Fredberg\_Sjostr](https://krita-artists.org/user_avatar/krita-artists.org/karl_fredberg_sjostr/32/98255_2.png) [@Karl\_Fredberg\_Sjostr](https://krita-artists.org/u/Karl_Fredberg_Sjostr)\
**Post date:** [January 15, 2024, 8:45pm UTC](https://krita-artists.org/t/fill-layer-action-fills-incorrect-layer/82377/1 "2024-01-15T20:45:01Z")

</div>

Hi, I am creating a clipping group for all selected paint layers. If the layer blending mode is set to hard light I want to fill the Mask Layer with 50% grey. With this code:

```auto
doc.setActiveNode(mask_layer)             
    if shading_layer_type == "hard_light":
        view.setForeGroundColor(grey_50)
        app.action('fill_selection_foreground_color').trigger()
        view.setForeGroundColor(fg_color)

```

It fills the selected layer (the base of the clipping group) instead of the mask layer. If I use a QMessageBox to debug the code before this code it fills the correct layer. I tried with doc.refreshProject() but it doesn’t help.

Any help with be appriciated

---

<div class="post-metadata">

**Author:** ![Karl\_Fredberg\_Sjostr](https://krita-artists.org/user_avatar/krita-artists.org/karl_fredberg_sjostr/32/98255_2.png) [@Karl\_Fredberg\_Sjostr](https://krita-artists.org/u/Karl_Fredberg_Sjostr)\
**Post date:** [January 15, 2024, 8:58pm UTC](https://krita-artists.org/t/fill-layer-action-fills-incorrect-layer/82377/2 "2024-01-15T20:58:57Z")

</div>

I tried with QTest.qWait(1000) but it doesn’t work.

---

<div class="post-metadata">

**Author:** ![KnowZero](https://krita-artists.org/letter_avatar_proxy/v4/letter/k/e8c25b/32.png) [@KnowZero](https://krita-artists.org/u/KnowZero)\
**Post date:** [January 15, 2024, 9:13pm UTC](https://krita-artists.org/t/fill-layer-action-fills-incorrect-layer/82377/3 "2024-01-15T21:13:35Z")

</div>

Your issue should be same as this:

> [@Active layer not updated in plugin](https://krita-artists.org/t/active-layer-not-updated-in-plugin/81240):
>
> I am trying to make a copy of a layer and do some work on it. I ran into an issue similar to the one discussed in [this thread](https://krita-artists.org/t/proper-way-to-activate-new-layer/29481) where the activeNode is not updated after layer manipulation. The thread suggests that this is a timing issue; however, after playing a bit with it it’s starting to seem like it’s not. Here is my first attempt: duplicate the current layer, then print the name of the active node: from krita import \* app = Krita.instance() doc = app.activeDocument() app.action('du…

Try QTimer.singleShot or watch the undo / undo docker history if you want guarantees

---

<div class="post-metadata">

**Author:** ![Karl\_Fredberg\_Sjostr](https://krita-artists.org/user_avatar/krita-artists.org/karl_fredberg_sjostr/32/98255_2.png) [@Karl\_Fredberg\_Sjostr](https://krita-artists.org/u/Karl_Fredberg_Sjostr)\
**Post date:** [January 16, 2024, 5:19am UTC](https://krita-artists.org/t/fill-layer-action-fills-incorrect-layer/82377/4 "2024-01-16T05:19:35Z")

</div>

Wrapping the action with the QTimer.singleShot worked. Thanks again @KnowZero!

```auto
doc.setActiveNode(shade_layer)  
            
if shading_layer_type == "hard_light":
    view.setForeGroundColor(grey_50)
    QTimer.singleShot(100, lambda: app.action('fill_selection_foreground_color').trigger() )
    view.setForeGroundColor(fg_color)

```

---

<div class="post-metadata">

**Author:** ![Karl\_Fredberg\_Sjostr](https://krita-artists.org/user_avatar/krita-artists.org/karl_fredberg_sjostr/32/98255_2.png) [@Karl\_Fredberg\_Sjostr](https://krita-artists.org/u/Karl_Fredberg_Sjostr)\
**Post date:** [January 16, 2024, 6:26am UTC](https://krita-artists.org/t/fill-layer-action-fills-incorrect-layer/82377/5 "2024-01-16T06:26:24Z")

</div>

@KnowZero I was to quick to say that the singleShot was the correct solution. Now I have different problem and that is that it fills with incorrect foreground color.  
This is what I tried but it’s not working.

```auto
def fill_foreground(color):
    fg_color = view.foregroundColor()
    view.setForeGroundColor(color)
    QTest.qWait(100)
    doc.waitForDone()
    QTimer.singleShot(100, lambda: app.action('fill_selection_foreground_color').trigger())
    view.setForeGroundColor(fg_color)
  
if shading_layer_type == "hard_light":
    QTimer.singleShot(100, lambda: fill_foreground(grey_50))

```

How do I properly watch the unde/redo docker history to get garantees?

---

<div class="post-metadata">

**Author:** ![KnowZero](https://krita-artists.org/letter_avatar_proxy/v4/letter/k/e8c25b/32.png) [@KnowZero](https://krita-artists.org/u/KnowZero)\
**Post date:** [January 16, 2024, 7:18am UTC](https://krita-artists.org/t/fill-layer-action-fills-incorrect-layer/82377/6 "2024-01-16T07:18:33Z")

</div>

QTimer.singleShot is not a wait command, it yields to the next command, so

view.setForeGroundColor(fg\_color)

will likely run before:

app.action(‘fill\_selection\_foreground\_color’).trigger()

You also don’t need to use qWait anymore

As for how to get guarantees from the docker, it would be like this:

```auto
from krita import *

def rowchanged(i,i2):
    print ( "Data=",i.data() )
    smodel.currentRowChanged.disconnect(rowchanged)

qdock = next((w for w in Krita.instance().dockers() if w.objectName() == 'History'), None)
wobj = qdock.findChild(QListView)
smodel = wobj.selectionModel()

smodel.currentRowChanged.connect(rowchanged)

```

The i.data() will contain the action name that you can verify, then disconnect

---

<div class="post-metadata">

**Author:** ![Karl\_Fredberg\_Sjostr](https://krita-artists.org/user_avatar/krita-artists.org/karl_fredberg_sjostr/32/98255_2.png) [@Karl\_Fredberg\_Sjostr](https://krita-artists.org/u/Karl_Fredberg_Sjostr)\
**Post date:** [January 16, 2024, 6:53pm UTC](https://krita-artists.org/t/fill-layer-action-fills-incorrect-layer/82377/7 "2024-01-16T18:53:19Z")

</div>

Thanks again @KnowZero I got it working with your suggestion. I had to use both the history docker and the singlshot like this.

```auto
history_docker = next((docker for docker in Krita.instance().dockers() if docker.objectName() == 'History'), None)
history_list_view = history_docker.findChild(QListView)
history_selection_model = history_list_view.selectionModel()

def fill_foreground(color):
    fg_color = view.foregroundColor()
    view.setForeGroundColor(color)
    app.action('fill_selection_foreground_color').trigger()
    view.setForeGroundColor(fg_color)                    
                     
def fill_foreground_deferred(event,_):
    QTimer.singleShot(100, lambda: fill_foreground(grey_50))
    history_selection_model.currentRowChanged.disconnect(fill_foreground_deferred)

if shading_layer_type == "hard_light":
   history_selection_model.currentRowChanged.connect(fill_foreground_deferred)

```

---

<div class="post-metadata">

**Author:** ![KnowZero](https://krita-artists.org/letter_avatar_proxy/v4/letter/k/e8c25b/32.png) [@KnowZero](https://krita-artists.org/u/KnowZero)\
**Post date:** [January 16, 2024, 8:25pm UTC](https://krita-artists.org/t/fill-layer-action-fills-incorrect-layer/82377/8 "2024-01-16T20:25:09Z")

</div>

That looks off to me, it should be more like this:

```python
history_docker = next((docker for docker in Krita.instance().dockers() if docker.objectName() == 'History'), None)
history_list_view = history_docker.findChild(QListView)
history_selection_model = history_list_view.selectionModel()

def fill_foreground(color):
    fg_color = view.foregroundColor()
    view.setForeGroundColor(color)
    app.action('fill_selection_foreground_color').trigger()
                     
                     
def fill_foreground_deferred(event,_):
    history_selection_model.currentRowChanged.disconnect(fill_foreground_deferred)
    view.setForeGroundColor(fg_color)
    

if shading_layer_type == "hard_light":
    history_selection_model.currentRowChanged.connect(fill_foreground_deferred)
    fill_foreground(grey_50)

```

---

<div class="post-metadata">

**Author:** ![system](https://krita-artists.org/uploads/default/original/1X/e446d8b3077ddf6dfb5b2cd65199c20095b47dcf.png) [@system](https://krita-artists.org/u/system)\
**Post date:** [January 20, 2024, 8:25pm UTC](https://krita-artists.org/t/fill-layer-action-fills-incorrect-layer/82377/9 "2024-01-20T20:25:59Z")

</div>

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.
