FreeCAD external Python interpreter will not return body object on Ubuntu 20.04

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
User avatar
Aleks
Posts: 309
Joined: Sun Mar 08, 2020 5:27 pm
Location: Bonn, Germany
Contact:

FreeCAD external Python interpreter will not return body object on Ubuntu 20.04

Post by Aleks »

[SOLVED]
Problem:
I have created this topic to document that I am having issues with the python interpreter on my server. I do not understand what is causing the problem or even what is the problem. The program runs just fine on ubuntu and windows when running it locally. The Python interpreter just says

Code: Select all

shape = Part.getShape(doc.getObject('Body001'), '')
TypeError: argument 1 must be App.DocumentObject, not None
I am using ubuntu 20.04, nginx, gunicorn, supervisor, venv, flask.

Maybe someone in the future will have the same problem and can comment.

Solution:
The problem was that PartDesign needed to be imported differently and because of that body objects werent recognized. This is not needed on Windows. On Windows, PartDesign can be imported without adding '.../Mod' to path.

Using FreeCAD without a GUI and with an external Python interpreter on Ubuntu (20.04, does probably also apply to other Ubuntu versions).
If I won't forget I will keep this list updated.

1. Use PPA stable FreeCAD distribution

Code: Select all

sudo add-apt-repository ppa:freecad-maintainers/freecad-stable
sudo apt update
sudo apt install freecad freecad-doc
2. Make sure that the external Python interpreter version is the same as the FreeCAD build-in version

Code: Select all

freecadcmd
[FreeCAD Console mode <Use Ctrl-D (i.e. EOF) to exit.>]
>>> import sys
>>> sys.version
'3.8.10 (default, Jun  2 2021, 10:49:15) \n[GCC 9.4.0]'

Code: Select all

python3
>>> import sys
>>> sys.version
'3.8.10 (default, Jun  2 2021, 10:49:15) \n[GCC 9.4.0]'
3. Add 'usr/lib/freecad-python3/lib' and 'usr/lib/freecad/Mod' to path ('.../Mod' is important for Python modules, especially PartDesign to work)

Code: Select all

import sys


FREECAD_PATH = 'usr/lib/freecad-python3/lib' #needed for C++ modules or FreeCAD (e.g. 'FreeCAD.ActiveDocument')
FREECAD_MOD_PATH = 'usr/lib/freecad/Mod' #needed for PartDesign and other Python modules

if FREECAD_PATH in sys.path:
    pass
else:
    sys.path.append(FREECAD_PATH )

if FREECAD_MOD_PATH in sys.path:
    pass
else:
    sys.path.append(FREECAD_MOD_PATH )

try:
    import FreeCAD
except Exception:
    print('Cannot import FreeCAD.')

try:
    import PartDesign
except Exception:
    print('Cannot import PartDesign.')

Last edited by Aleks on Mon May 08, 2023 12:08 pm, edited 19 times in total.
FreeCAD als Maschinenbauer in die Konstruktion und Fertigung integrieren. Schulung buchen: www.alsado.de/freecad-schulungen
jbi
Posts: 117
Joined: Sun Apr 24, 2016 3:28 pm

Re: Issues with FreeCAD python interface on server

Post by jbi »

Hi,
difficult to diagnose but i guess you are running without gui on the server, and with gui locally? no gui will mean no document object.
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Issues with FreeCAD python interface on server

Post by openBrain »

jbi wrote: Thu Sep 09, 2021 6:13 pm no gui will mean no document object.
Totally false. ;)
App::DocumentObject also are there in CLI mode.
Gui::DocumentObject and ViewProvider aren't.
User avatar
Aleks
Posts: 309
Joined: Sun Mar 08, 2020 5:27 pm
Location: Bonn, Germany
Contact:

Re: Issues with FreeCAD python interface on server

Post by Aleks »

I cannot provide more information om this, because I dont have any. But I already have quite a lot of experience with running in cli and I have never had that problem before. This may be related to dispatching multiple flask apps in combination with one freecad interface, but idk. As I said, maybe someone will have a similar problem in the future and we could compare. This is probably some edge case of the tech stack that is leading to this problem. I am able to run a single flask app on a vm with the freecad python interace just fine.

The reason I am even attempting to run multiple freecad web apps on one server is to cut down on server costs.
FreeCAD als Maschinenbauer in die Konstruktion und Fertigung integrieren. Schulung buchen: www.alsado.de/freecad-schulungen
User avatar
Aleks
Posts: 309
Joined: Sun Mar 08, 2020 5:27 pm
Location: Bonn, Germany
Contact:

Re: Issues with FreeCAD python interface on server

Post by Aleks »

These are the differences between the setup that works and the one that doesnt.

works:
Windows 10 Pro
werkzeug
Python 3.8.8

does not work:
Ubuntu 20.04
Nginx
Python 3.8.10

My next guess is that some setting in nginx prevents the webapp from functioning correctly.
FreeCAD als Maschinenbauer in die Konstruktion und Fertigung integrieren. Schulung buchen: www.alsado.de/freecad-schulungen
User avatar
Aleks
Posts: 309
Joined: Sun Mar 08, 2020 5:27 pm
Location: Bonn, Germany
Contact:

Re: Issues with FreeCAD python interface on server

Post by Aleks »

I have reduced this problem to the line

Code: Select all

body = doc.getObject('Body001', '')
This code returns none.

In the freecad internal interpreter this works fine, while using the freecad python interface does not. The only differences left are
- Python 3.8.8 (FreeCAD) vs Python 3.8.10 (Webapp) and
- Windows 10 Pro vs. Ubuntu 20.04

What could be the case is that Python 3.8.8 and Python 3.8.10 are not compatible on Ubuntu 20.04, while they ARE compatible on Windows 10 Pro.

Whats funny, is that the exact code works fine in a different app with the exact same tech stack.
Last edited by Aleks on Fri Sep 10, 2021 4:01 pm, edited 1 time in total.
FreeCAD als Maschinenbauer in die Konstruktion und Fertigung integrieren. Schulung buchen: www.alsado.de/freecad-schulungen
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Issues with FreeCAD python interface on server

Post by openBrain »

DO you explicitly recompute Body001 and/or document in the script before accessing it ?

Another thing is that sometimes in CLI mode, the shape of the object need to be explicitly tesselated with something like

Code: Select all

Body001.Shape.tesselate(0.1)
User avatar
Aleks
Posts: 309
Joined: Sun Mar 08, 2020 5:27 pm
Location: Bonn, Germany
Contact:

Re: Issues with FreeCAD python interface on server

Post by Aleks »

In my initial code I did recompute the document before accessing the body object. But I have now removed everything, but the single line I have shown in my previous post.

Why do I need to tesselate the body? On Windows I do not get the error.
FreeCAD als Maschinenbauer in die Konstruktion und Fertigung integrieren. Schulung buchen: www.alsado.de/freecad-schulungen
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Issues with FreeCAD python interface on server

Post by openBrain »

Aleks wrote: Fri Sep 10, 2021 4:00 pm Why do I need to tesselate the body? On Windows I do not get the error.
I didn't say you need, I said it happened sometimes in the past.
Are you sure eg. you have the same OCC version on both setups ?
User avatar
Aleks
Posts: 309
Joined: Sun Mar 08, 2020 5:27 pm
Location: Bonn, Germany
Contact:

Re: Issues with FreeCAD python interface on server

Post by Aleks »

I dont know. I will check it.

On Ubuntu I am using the latest stable PPA build.

On Windows I am using FreeCAD 0.19.24276.

Do you think that OCC can be causing this problem?
FreeCAD als Maschinenbauer in die Konstruktion und Fertigung integrieren. Schulung buchen: www.alsado.de/freecad-schulungen
Post Reply