FreeCAD Hangs When Called from Python

Having trouble installing or compiling FreeCAD? Get help here.
freelyb
Posts: 3
Joined: Wed Sep 06, 2017 1:34 pm

FreeCAD Hangs When Called from Python

Postby freelyb » Wed Sep 06, 2017 2:04 pm

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
Site Admin
Posts: 16456
Joined: Thu Feb 19, 2009 10:32 am

Re: FreeCAD Hangs When Called from Python

Postby wmayer » Wed Sep 06, 2017 2:39 pm

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

Postby freelyb » Wed Sep 06, 2017 9:42 pm

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
Site Admin
Posts: 16456
Joined: Thu Feb 19, 2009 10:32 am

Re: FreeCAD Hangs When Called from Python

Postby wmayer » Thu Sep 07, 2017 5:42 am

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

Postby freelyb » Fri Sep 08, 2017 1:15 pm

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
Posts: 6087
Joined: Wed Oct 22, 2014 5:02 pm

Re: FreeCAD Hangs When Called from Python

Postby sgrogan » Fri Sep 08, 2017 9:30 pm

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
Site Admin
Posts: 16456
Joined: Thu Feb 19, 2009 10:32 am

Re: FreeCAD Hangs When Called from Python

Postby wmayer » Sat Sep 09, 2017 8:03 am

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

Postby DF3AK » Wed Feb 14, 2018 8:25 pm

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
Posts: 10199
Joined: Tue Jul 26, 2011 6:28 am
Location: Australia

Re: FreeCAD Hangs When Called from Python

Postby jmaustpc » Thu Feb 15, 2018 12:35 am

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

Postby DF3AK » Fri Feb 16, 2018 4:18 pm

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)