Substance Painter toolkit and python subprocess

Hi there

I m trying to fire a subprocess.Popen to check if any substance Painter is still runnning using the windows tasklist command for that.

from subprocess import Popen, PIPE
import re

def get_current_pids(self, *args, **kwargs):
    cmd = 'tasklist /FI "IMAGENAME eq Substance Painter.exe" 2>NUL | find /I /N "Substance Painter.exe"'
    regex = re.compile("  (?P<pid>[0-9]+) Console")
    proc = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE)
    data, err = proc.communicate()
    exit_code = proc.poll()
    if exit_code:
        return []
        pids = []
        .... # process the response to extract the painters process ids
        return pids

tk-substancepainter has a register_events_callback
so i added a callback engine.register_events_callback(“QUIT”, self._on_quit) in the hook override , class EngineInit(Hook)
so when painter quits, _on_quit is run and calls get_current_pids

the method _on_quit is correctly called and get_current_pids as well but stops
as soon as the code reach proc.communicate , (the code runs fine is a non SG env)

Not sure whats happening. no error raised neither

1 Like

In communicate() does err get set to anything? I don’t see how Shotgun could be related.
Most likely something is going wrong with the cmd. I know you said it works otherwise. Could tasklist not be in your PATH because toolkit modifies it? Just a guess.

Hi mmoshev
no , err is not set to anything . Even a try except and looging the err doesnt give anything.
tasklist is a standard dos command to list the process, so available anywhere.

I even tried to try to add stuff instead for debug purpose thing
in the tk-substancepainter , theres

if method == "QUIT":
        if self._qt_app:

just added a cpl self.logger.debug message around a os.system(‘dir’)

    if method == "QUIT":
        self.logger.debug("BEFORE OS CMD")
        except Exception as err:
            self.logger.debug("CMD ERROR: {}".format(err))

The tk-substancepainter log just has the “BEFORE OS CMD” and nothing after like , errors are swallowed somewhere.