SOLVED PYSIDE FOR C4D PROBLEMS

I need to use pyside for my plugin's GUI.
And i run Pyside in another C4Dthread.
It is possible to successfully open the Pyside window in C4D.
but there seem to be some problems.

-1.When I opened the Pyside window in C4D, the main thread seemed to be blocked, which made it impossible for me to interact with the C4D scene.

-2.When I first open the Pyside window, it will open normally, but when I close it and open it again, it will flicker on the screen and open it again, that is to say; After closing the window for the first time, I need to click twice to open the window.

And this is my code example:

class Window(QMainWindow):

    def __init__(self):
        super(Window,self).__init__()

        self.InitUI()

    def InitUI(self):
        self.setWindowTitle("Pyside Test")
        self.resize(500,400)
        self.move(200,200)

        label = QLabel(self)
        label.setText("Pyside Test")
        label.setAlignment(Qt.AlignCenter)


class UIThread(C4DThread):

    def Main(self):
        # Put in your code here
        # which you want to run
        # in another thread
        
        app = QApplication.instance()
        if not app:
            app = QApplication([])

        win = Window()
        win.show()
        app.exec_()

def main():
    thread = UIThread()
    thread.Start(c4d.THREADMODE_ASYNC, c4d.THREADPRIORITY_NORMAL)
    
    # Do some other operations here
    thread.Wait(True) # Wait until the main method is done
    
    print "Pyside for c4d"
    
    thread.End(True)

@gheyret
Hello~~

@gheyret
Is anyone here?

Hi @gheyret,

thank you for reaching out to us. It is unfortunately not quite cleae to me in which context (Script Manager script, CommandData plugin, etc.) your problem occurs. Please try to include such context in the future, as it is not always obvious from the code and quite important. I all answer the rest in bullet point from most important to less important.

  1. As stated in the developer support conditions we cannot offer support on 3rd party libraries, as this is basically a bottomless pit over which we have no control ourselves. We however discussed the topic of Qt support being quite an important thing for Cinema and might add this in the future. Please note that this is no guarantee that this will happen immediately or at all, we simply recognize and agree on the importance of this topic.
  2. The most secure way to do what you want would be to implement a server-client model with a Python interpreter of your choice running the Qt GUI acting as a server or client and Cinema acting as the counterpart. This topic was discussed here.
  3. If you want to do this in Cinema, you should certainly move out of a script manager context if you haven't done this already. In a CommandData plugin you could run your Qt GUI properly in its own thread, avoiding blocking Cinema's main thread. The problem here is how Cinema handles internally Python's GIL and depending on the circumstances you might end up with code that is not being executed truly asynchronously, i.e. blocking Cinema's main thread again. There is unfortunately currently no way to fix that for you.

Thank you for your understanding,
Ferdinand

@zipit
Thanks to replying me!
And looks like i need to try it in "CommandData plugin ", hope it works~
Cheers!

Hi,

just to be really unambiguous: You can try, but I personally would really recommend a server-client model, since you will always be on unsafe ground doing this in Cinema. And it is hard to plan for all eventualities, until we specifically design Cinema's Python API for being cable of this. As already pointed out, we will not be able to offer support if you decide to take that route and it does not work out.

Cheers and thank you for your understanding,
Ferdinand

@zipit
I just read this(Connect C4D with Tkinter post,It does seem to help me
Thank you again!