I am trying to copy-paste with a flip

what I am trying to achieve with is :

  1. Open some random image
  2. Selection ( manual or auto )
  3. Copy contents of selection
  4. Create a new Layer
  5. Paste content into the new layer
  6. Select new layer as active layer
  7. mirror node the new layer
  8. merge down the new layer over the original layer

Selections :

  • Manual - there is a selection done previously to running the script
  • Auto - there is no current selection made so a selection needs to be made by the script

Sample Code so far

from krita import *

def Wait():
    ad = Krita.instance().activeDocument()
    ad.waitForDone()
    ad.refreshProjection()

cond = "Left"

# Variables
ki = Krita.instance()
ad = ki.activeDocument()
node = ad.activeNode()
name = "Mirror Fix : " + node.name()
width = int( ad.width() )
height = int( ad.height() )
w2 = width * 0.5
h2 = height * 0.5
value = 255

# Force the Mirror Axis to Center
ki.action('mirrorX-moveToCenter').trigger()

# Selection
ss = ad.selection()
if ss == None:
    # Variables
    px = 0
    py = 0
    pw = w2
    ph = height
    # Select Area
    sel = Selection()
    sel.select( px, py, pw, ph, value )
    ad.setSelection( sel )
else:
    # Variables
    sel = ss
    px = sel.x()
    py = sel.y()

# Copy
sel.copy( node )
sel.clear()

# New Node
new_node = ad.createNode( name, "paintLayer" )
ad.activeNode().parentNode().addChildNode( new_node, node )

# Paste
sel.select( 0, 0, width, height, value )
sel.paste( new_node, px, py )
Wait()

# Active Layer
ad.setActiveNode( new_node )
Wait()

# Mirror Node
ki.action('mirrorNodeX').trigger()
Wait()

# Merge (this solves a alpha compositing issue)
merge_node = new_node.mergeDown()
Wait()

I am currently like one action away to be able to make the thing run but I there is something missing.
I get the feeling once I create the new layer something is lost on the way and then commands that work under normal conditions stop working.

Currently if you do no selection the script will work and mirror the image, but if you make a manual selection with freehand for example the script will break and not flip properly.

Document.setActiveNode(node) is a bit buggy, it’s done really late in event evaluation.

from krita import (
        Krita,
        Selection)

from PyQt5.QtCore import (
        QTimer,)


def Wait():
    doc = Krita.instance().activeDocument()
    doc.waitForDone()
    doc.refreshProjection()


# Variables
app = Krita.instance()
doc = app.activeDocument()
node = doc.activeNode()
doc_width = doc.width()
doc_height = doc.height()
doc_half_width = doc_width // 2
doc_half_height = doc_height // 2
select_weight = 255

# Force the Mirror Axis to Center
app.action('mirrorX-moveToCenter').trigger()

# Selection
sel = doc.selection()
if sel == None:
    # Select Area
    sel = Selection()
    sel.select(0, 0, doc_half_width, doc_height, select_weight)
    doc.setSelection(sel)

src_x = sel.x()
src_y = sel.y()

# Copy
sel.copy(node)
sel.clear()

# New Node
new_node = doc.createNode(f"Mirror Fix : {node.name()}", "paintLayer")
node.parentNode().addChildNode(new_node, node)

# Paste
sel.select(0, 0, doc_width, doc_height, select_weight)
sel.paste(new_node, src_x, src_y)
Wait()

# Active Layer
doc.setActiveNode(new_node)
# Wait()  this too weak to wait for state change of active node.
# Note: QCoreApplication.processEvents() also failed to wait for everything??

def do_later():
    # Mirror Node
    app.action('mirrorNodeX').trigger()
    Wait()

    # Merge (this solves a alpha compositing issue)
    merge_node = new_node.mergeDown()
    Wait()

# wait for everything in event queue.
QTimer.singleShot(0, do_later)

/AkiR

1 Like

I am not quite sure how it is working but it is working. Thank you @AkiR

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