I think the reason you aren't seeing anything in the script editor is
because Maya sort-of monkey patches the sys.stdout/sys.stderr to point at
the script editor. But when you use a QProcess (or subprocess) it forks and
execs a new process that doesn't really have access to the script editor
via file descriptors. Its basically inheriting the main stdout/stderror of
Maya (that is, I see the output in the Console in osx).

If you want to work with a subprocess asynchronously, then QProcess is
definitely a useful tool to do it, since it comes with all the signals.
It's actually really easy to fix the issue of getting the output into the
script editor, by reacting to the signals and forwarding them to the script
editor:

from PyQt4 import QtCore

class ProcessTest(QtCore.QObject):

    def __init__(self):
        super(ProcessTest, self).__init__()
        self.process = QtCore.QProcess(parent=self)
        self.process.setProcessChannelMode(process.MergedChannels)

        self.process.readyReadStandardOutput.connect(self.handle_output)

    def run(self):
        self.process.start('python', ['-c', 'print("finished qprocess")'])

    def handle_output(self):
    sys.stdout.write(self.process.readAllStandardOutput())

myTest = ProcessTest()
myTest.run()


I'm using MergedChannels instead, here, to combine stdout/stderror into a
channel that we have to read. Then I have the QProcess notify my handler
when data is ready. We read it and reprint it.
If you don't need all the fancy async signal/slot stuff, then the stdlib
subprocess module should be good enough.



On Mon, Jan 13, 2014 at 5:44 PM, Michael Boon <[email protected]> wrote:

> Hi all,
>
> I'm trying to use QProcess in Maya, and I must have some fundamental
> misunderstanding because I can't make it work.
>
> Here's the simplest thing I've tried (in the Script editor, Maya 2013)
>
> from PyQt4 import QtCore
> class processTest(QtCore.QObject):
>     def __init__(self):
>         super(processTest, self).__init__()
>         process = QtCore.QProcess(parent=self)
>         process.setProcessChannelMode(process.ForwardedChannels)
>         process.start('python', ['-c', 'print("finished qprocess")'])
> myTest = processTest()
>
> My expectation was that that would print "finished qprocess" to the script
> editor, but it doesn't do anything.
>
> I've tried a bunch of other things, including writing to files and using
> signals, and with some combinations I get an error in Eclipse, "'Match job'
> has encountered a problem," but I haven't managed to get anything desirable.
>
> As an aside, I'm trying to use the Perforce command line client and give
> the user a working Cancel button in case there's a delay due to network or
> server problems. Is QProcess the right choice for that?
>
> Thanks,
>
> Boon.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Python Programming for Autodesk Maya" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/python_inside_maya/94e71427-c5d8-4c18-8c85-d17cb99a74a2%40googlegroups.com
> .
> For more options, visit https://groups.google.com/groups/opt_out.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA1CQMCyL%3DsSiZAAy2_v67-y9fyfMAPAD%2B7g962w5BZSBw%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to