Krita script to run loop in background that saves constantly

Hi there! First timer here. I have been working on a plugin to use for game development, and wanted to know if there is a way of creating a thread that exports constantly in the background, say, every 10 seconds the thread that has a loop exports the file, without blocking anything.

I’ve been trying to use threads.Thread() but that constantly crashes the application. Using QTimer.singleShot(value, loop.quit) makes the application lag a lot.

Does anybody have an Idea of how to work a solution for this?

  def saveFile(self):
    self.isSaving = True

    fileName = "newFile" if self.urlTextEdit.toPlainText() == "" else self.urlTextEdit.toPlainText()

    currentDocument = Krita.instance().activeDocument()
    currentDocument.setBatchmode(True)
    localDestination = "/Path/to/Folder/"
    localPath = localDestination + fileName + ".png"
    # exportImage supports jpg and png
    currentDocument.saveAs(localPath)

    self.lib.jsTransferFile(str.encode(localPath), self.destinationPath + str.encode(fileName + ".png"))
    self.isSaving = False

  def updateFileThread(self):
    while True:
      timer.sleep(10)
      if not self.isSaving:
        self.saveFile()
      if not self.liveSyncCheckbox.isChecked():
        break


  def setLiveSync(self):
    if self.liveSyncCheckbox.isChecked():
      self.thread = threading.Thread(target=updateFileThread)
      self.thread.setDaemon(True)
      self.thread.start()
    else:
      self.thread.join()

Any help would be greatly appreciated

Hi

Python thread can’t be used in a Qt application
You have to use QThread instead

Also, native Krita recorder already does what you’re trying to implement (if your goal is just to get this functionnality it already exist :wink:)

Grum999

That is good to know, for QThreads, could you give me an example of how to use them? I just started today with python development so I’m fairly new in some uses. Also for the recorder, how can it be used? The idea for the loop was to also use a library I did in C++ to transfer the exported file to a cloud service. Doing the function manually works fine, but the loop is the problem. Is there a way to connect the recorder to add the transfer function?

The recorder allows to export current state of work every X sec in a directory

Don’t know if it’s possible to provide a path to a cloud drive, I don’t use this kind of services.

But globally, it will provide better performances than python script to export file.

No, recorder doesn’t provide API on which you can trigger something.
The best you can do here is to scan directory content and upload new files since last check
→ Might be the best solution
– recorder defined to export image every X sec
– script defined to upload last exported image every X’ sec

Here is a good article with simple examples:

Grum999

If they save the generated png from the recorder inside a cloud drive like dropbox folder i think it will automatically upload the newly appearing snapshots

You can create a thumbnail (slow) or a bit image( explained at krita scripting school). Then you can construct a qimage in the thread and save the image as you prefer.

Replying a tad late because work was not benevolent with me. But with what I learned with the Qthreads I have been able to work in what I wanted


And now I can use Krita to update textures in real time with Omniverse. This was all related to the classes I give for tools for game development. I appreciate a lot your answer and wanted to show you the results from it.