Possible to prevent wxPython Phoenix GUI from blocking BGE interaction ?


I need to have a file open dialog for my project. Ideally I need to have the input to BGE responsive while the file dialog is open. Even though BGE continues in the background while the wxFileDialog is open BGE won’t accept any keyboard or mouse input and the display freezes, but sound keeps playing OK. The actual file dialog itself does accept input. Here’s the relevant code which is triggered by a keyboard press …

def openFile (cont):
    print ("File load triggered") 
    def fileDialog (event):           
        style = wx.FD_OPEN | wx.FD_FILE_MUST_EXIST
        dialog = wx.FileDialog(None, 'Open', wildcard='*.txt', style=style)
        if dialog.ShowModal() == wx.ID_OK:
            path = dialog.GetPath()
            path = None
            print (path)        
    app = wx.App(redirect=True)
    top = wx.Frame(None, title="Hello World", size=(300,200))
    #  top.Bind(wx.EVT_CLOSE, fileDialog)

I’m experimenting there with “Show()” which is supposed to create a GUI element while not blocking the parent. Does not work though, I guess the parent here is Blender which is not a wxPython GUI element. The file dialog can only be called with ShowModal() which does “block” the parent part of the GUI.

The full blend file is here: http://files.djbarney.org/dj_turntable_platter_physics_simulation_111.blend

If the user needs to open a file or cancel to be able to continue interacting with the game engine then so be it, but I’m not certain yet that what I’m thinking of can’t be achieved. There’s a way of using non-gui-blocking code but that’s more, I think for calculations. I’m not sure it’s possible to put the file dialog on another thread to prevent blocking of the game engine because apparently your’re not supposed to make GUI code on the other thread which leads to crashes, which is exactly what I’ve been getting.

You better do not mix two different engines. They are not compatible.

The nature of the BGE requires that any Python controller exists as fast as possible. The BGE will wait until this happens before continuing it’s processing.

Therefore you should not use blocking operations (e.g. sleep(), blocking network operations).

You can try with threads. As far as I know this can become unstable.
You can run another application and let both communicate via TCP or UDP.

The recommendation to avoid relies on manipulations within the BGE. The GUI you are mention does not interact with the BGE. So you should be save to use it in a thread. Be aware this thread should not access any BGE data (at least not writing to it). To get the result of the GU I suggest to establish a controller that polls for results.

def checkResults(controller):
   result = yourResultRetrievingCode()
   if result:
      pass #nothing to do this frame

I’m not sure what Phenix GUI is but threads are the solution to all your problems. If you have a bad framerate: Use threads. If your code looks so simple it seems boring: Use threads. If you’re tired of life: Try using threads. Whatever it is, threads solve it.

Why do you think the sound keeps playing? It’s becouse of threads!

Text extracted from an old post: https://blenderartists.org/forum/showthread.php?398265-better-way-connecting-rs232

PS: Instead of a controller you can just use the main loop, you know, that wich your game should have.

I assume you mean the wxPython “engine” ?

You can run another application and let both communicate via TCP or UDP.

Ah ! Thanks Monster. Of course. I had not considered that :o. Create a file loader as a separate Python application that uses wxPython for the GUI and communicate via the method you cite. That sounds a lot simpler and less dangerous to stability than messing around with threads. The Python code that scans for TCP/UDP communication can be a controller logic block much like the one setting the pitch. If the separate GUI application crashes then BGE should be left intact :cool:. Cool.

@elmeunick9: your recommendations are pretty strange

@Michael Z Freeman: yes, I meant the BGE and the wxPython. I was just not sure to call both framework. I guess you get the idea.

What is the reason you need a file selection dialog?

My project is basically a digital audio player that needs to load audio files, wav, flac, mp3 and so forth.

Is the BGE really good choice to do that?

It’s simulating vinyl turntable effects using the BGE physics. It works very well so far. I was a bit dubious about Audaspace to begin with but that works very well aswell. I still have to add refinements using the friction materials to brake the turntable properly and other things (todo included in blend file). I decided to use wxPython for the GUI as things like file dialogs are complicated to setup within BGE itself.

To follow up. The answer is to use a text file to transfer the file name. An external wxPython GUI handles the file selection and then passes it to a text file that the BGE checks every tick.