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!
freelyb
Posts: 3
Joined: Wed Sep 06, 2017 1:34 pm

FreeCAD Hangs When Called from Python

Post by freelyb »

Hi, I am having an issue where python will hang when it tries to open a window for a created FreeCAD geometry. I am able to open FreeCAD normally from the desktop and use the GUI but when I attempt to call the modules from Python, the window that is brought up to display any geometries created through scripting is non-responsive and the kernel eventually dies.

I am running python x,y in 32 bit with python 2.7. I installed the 32 bit, 0.17 version of FreeCAD but have not had any luck when trying legacy releases (I have tried 0.15, 0.16 as well).

Any help resolving this issue would be greatly appreciated! I am working on a python library for my company and others have not had any trouble getting a similar setup working and we are stumped as to why my setup is giving me issues.

Thanks in advance!

Here is an example of what will cause the window to open but be non-responsive:

Code: Select all

import FreeCAD as app
import FreeCADGui as gui

doc = app.newDocument()
box = doc.addObject("Part::Box", "myBox")
doc.recompute()
#obj = gui.ActiveDocument.getObject("myBox") -- AttributeError: 'module' object has no attribute 'ActiveDocument'
gui.showMainWindow()
It seems that the last line is the issue, the previous lines run without issue.
wmayer
Founder
Posts: 20302
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: FreeCAD Hangs When Called from Python

Post by wmayer »

gui.showMainWindow()
It may take a while as depending on your settings it has to load various modules but it does not hang. Then after the line has been executed you must add this line gui.exec_loop() so that the GUI becomes responsive.
freelyb
Posts: 3
Joined: Wed Sep 06, 2017 1:34 pm

Re: FreeCAD Hangs When Called from Python

Post by freelyb »

Thank you!! This solved my problem. Very interesting, it seems like I only need to call that when I'm using an ipython console, I don't seem to need it when using a standard console or a cmd window. iPython must want to hang on to the thread and not give up control to the gui?

I'm having another odd error where in some instances I am able to call methods in wrapper classes developed internally here that use methods in the FreeCADGui module like gui.getMainWindow() but when I write my own stuff and try to call the same method in FreeCADGui it says that 'module' does not have this method.

Any idea why that's happening?

Thanks again!
wmayer
Founder
Posts: 20302
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: FreeCAD Hangs When Called from Python

Post by wmayer »

When loading the FreeCADGui module outside the FreeCAD application it only has a few methods.

Code: Select all

import FreeCAD
import FreeCADGui
dir(FreeCADGui)
# ['__doc__', '__file__', '__name__', '__package__', 'embedToWindow', 'exec_loop', 'setupWithoutGUI', 'showMainWindow']
Only after calling showMainWindow it gets fully initialized.

Code: Select all

FreeCADGui.showMainWindow()
dir(FreeCADGui)
['ActiveDocument', 'Control', 'PySideUic', 'Selection', 'SendMsgToActiveView', 'UiLoader', 'Workbench', '__doc__', '__file__', '__name__', '__package__', 'activateWorkbench', 'activeDocument', 'activeView', 'activeWorkbench', 'addCommand', 'addIcon', 'addIconPath', 'addLanguagePath', 'addModule', 'addPreferencePage', 'addResourcePath', 'addWorkbench', 'createDialog', 'createViewer', 'doCommand', 'doCommandGui', 'embedToWindow', 'exec_loop', 'export', 'getDocument', 'getLocale', 'getMainWindow', 'getSoDBVersion', 'getWorkbench', 'hide', 'hideObject', 'insert', 'listCommands', 'listWorkbenches', 'open', 'removeWorkbench', 'runCommand', 'setActiveDocument', 'setupWithoutGUI', 'show', 'showDownloads', 'showMainWindow', 'showObject', 'showPreferences', 'subgraphFromObject', 'updateGui', 'updateLocale']
freelyb
Posts: 3
Joined: Wed Sep 06, 2017 1:34 pm

Re: FreeCAD Hangs When Called from Python

Post by freelyb »

You are a lifesaver, this has helped immensely. Thank you!!

Is all this information in the documentation? I have been reading through it but wasn't aware of any of this until you helped out, is this something you picked up from there/could have picked up from there or just through being very familiar with the system?
User avatar
sgrogan
Veteran
Posts: 6499
Joined: Wed Oct 22, 2014 5:02 pm

Re: FreeCAD Hangs When Called from Python

Post by sgrogan »

freelyb wrote: Fri Sep 08, 2017 1:15 pm just through being very familiar with the system?
wmayer probably wrote the code https://github.com/FreeCAD/FreeCAD/graphs/contributors :)
"fight the good fight"
wmayer
Founder
Posts: 20302
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: FreeCAD Hangs When Called from Python

Post by wmayer »

This solved my problem. Very interesting, it seems like I only need to call that when I'm using an ipython console, I don't seem to need it when using a standard console or a cmd window. iPython must want to hang on to the thread and not give up control to the gui?
This depends on the host application that loads the FreeCADGui module. If on UNIX systems the host application uses glibc or on Windows the Win32 API then Qt is able to hook into the message loop of the host and an explicit call of exec_loop is not needed. But sometimes this causes problems with modal dialogs as they don't behave like that.
Is all this information in the documentation?
I haven't found it in the Wiki. But in our repo we have some examples how to embed FreeCAD into other applications: https://github.com/FreeCAD/FreeCAD/tree ... s/embedded
sgrogan wrote:
freelyb wrote:just through being very familiar with the system?
wmayer probably wrote the code https://github.com/FreeCAD/FreeCAD/graphs/contributors
Exactly!
DF3AK
Posts: 3
Joined: Wed Feb 14, 2018 7:49 pm

Re: FreeCAD Hangs When Called from Python

Post by DF3AK »

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

In my case (IDE: Spyder, ipython, FreeCAD python 3 built) this results in a responsive FreeCAD gui. However, it also blocks the interpreter :(

I've therefore tried the solution you have proposed that is based on threading:
wmayer wrote: Thu Mar 10, 2011 11:02 am This works for me

Code: Select all

import FreeCADGui
FreeCADGui.showMainWindow()
FreeCADGui.exec_loop()
The last line is needed to run the event loop which is needed for all applications using FreeCADGui that isn't based on the GTK, wxWidgets, Win32 or MFC toolkit. If the target application is based on one of these toolkits Qt is able to create a hook to the host application and gets its events from there.

Note:
When running exec_loop() the host application might be blocked. In case this happens to you and you don't want try running the exec_loop() function from a thread.
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()):

Code: Select all

import threading
import sys
FREECADPATH = # path to your FreeCAD.so or FreeCAD.dll file
sys.path.append(FREECADPATH)

import FreeCAD as app
import FreeCADGui as gui

gui.showMainWindow()
t1 = threading.Thread(target=gui.exec_loop)
t1.start()
Any ideas? Or is there maybe another way to remote control FreeCAD from a Python script, the latter being edited in an external IDE?

kind regards
jmaustpc
Veteran
Posts: 11207
Joined: Tue Jul 26, 2011 6:28 am
Location: Australia

Re: FreeCAD Hangs When Called from Python

Post by jmaustpc »

Please post your version data in the manner explained in the forum rules link above the Help forum.
DF3AK
Posts: 3
Joined: Wed Feb 14, 2018 7:49 pm

Re: FreeCAD Hangs When Called from Python

Post by DF3AK »

Hi,

sure:

OS: Linux Mint 18 Sarah
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.17.13062 (Git)
Build type: Release
Branch: master
Hash: f6825a2686d80b0976c3d1900a9f56ce3c9f7d91
Python version: 3.6.3
Qt version: 5.6.2
Coin version: 4.0.0a
OCC version: 7.2.0
Locale: English/UnitedStates (en_US)
Post Reply