FreeCAD Hangs When Called from Python

Having trouble installing or compiling FreeCAD? Get help here.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
DF3AK
Posts: 3
Joined: Wed Feb 14, 2018 7:49 pm

Re: FreeCAD Hangs When Called from Python

Post by DF3AK »

Any idea ? Or can you maybe give a hint where to search for relevant information to solve this issue?
wmayer
Founder
Posts: 20319
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: FreeCAD Hangs When Called from Python

Post by wmayer »

So if I understood it correctly, it is required to perform .exec_loop() in order to get the gui properly running.
Not necessarily.

Many years ago the Qt team published an interesting slide about event loop integration (unfortunately it's not available any more). Anyway, the main points were:
* The application process delivers native events to Qt. On Windows and macOS this is always the case while on X11 the application must support GLib
* The plugin needs to ensure that the Qt event loop runs
** X11: If GLib is supported by the application, then this works automatically. Otherwise this is unlikely to work.
** macOS: Application needs to call an event-handler callback that does event translation and sends posted QEvents.
** Windows: Register a callback with the system to call QCoreApplication::sendPostedEvents

In the past I used Unity on Ubuntu and when entering showMainWindow() the GUI came up and I could work with it. Only modal dialogs didn't work because the disappeared immediately. Now I am on XFCE and there I have to call exec_loop. Alternatively I can use FreeCADGui.updateGui() in a while-loop.

On Windows a callback function must be registered as we did here: https://github.com/FreeCAD/FreeCAD/blob ... y.cpp#L105
In my case (IDE: Spyder, ipython, FreeCAD python 3 built) this results in a responsive FreeCAD gui. However, it also blocks the interpreter
Spyder is based on Qt, right? Does it use the same Qt version as FreeCAD or is it different? In the latter case this may lead to problems.

According to https://stackoverflow.com/questions/105 ... p-with-qts it should be possible to call processEvents instead of starting the QApplication's event loop. With our Python binding you can do this with FreeCADGui.updateGui() which means that instead of

Code: Select all

FreeCADGui.exec_loop()
you can call

Code: Select all

while True: FreeCADGui.updateGui()
This way you can at least interrupt the execution in the interpreter window by pressing CTRL+C.
But unfortunately it seems like if I run .exec_loop() within a separate thread, the gui doesn't respond (window pops up, but no content, similar to not running .exec_loop()):
It you enter t1.isAlive() then it says the thread has stopped.

Since you mentioned ipython you can give this a try, too: http://ipython.readthedocs.io/en/stable ... loops.html
Post Reply