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:

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

Post by Aleks »

Could anyone try to at least reproduce this problem?
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: FreeCAD external Python interpreter will not return body object on Ubuntu 20.04

Post by Aleks »

jbi wrote: Thu Sep 16, 2021 5:57 pm Files which i uploaded with the webapp, and which i tried to access with the external python interpreter. I am using also a virtual environment for the webapp, and process the data with an external daemon which uses the freecad libaries. The solution in my case was add the daemon user to the webapp user.
Couldnt you access your files at all or just specific objects from it?
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: FreeCAD external Python interpreter will not return body object on Ubuntu 20.04

Post by jbi »

Hi,
Honestly I did not investigate in detail.
You can check permissions of the file with ls –l [file_name]. You could try to change permission of your file with sudo chmod 777 [file_name] -this is not adviced since this will give anybody access, but will help to sort the problem out.
What I can say I have exactly the same versions and OS (except the gui for linux) running on my server and my local machine. I guess you should take the effort and replicate yourself a development environment mirroring your server (including virtual env which will help you create requirements.txt files as well) or vice versa.
I have installed the exact version of freecad with:

Code: Select all

sudo add-apt-repository ppa:freecad-maintainers/freecad-stable
sudo apt-get update

#specific version from ppa:
sudo apt install freecad=2:0.19.2+dfsg1~202107140647~ubuntu18.04.1
Only for information I followed a tutorial on youtube for this, may this help https://www.youtube.com/watch?v=Sa_kQhe ... p&index=13. It is for apache and django, but I guess the python steps are still valid. I think there is also one for flask.
Sorry to have not more details.
Syres
Veteran
Posts: 2893
Joined: Thu Aug 09, 2018 11:14 am

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

Post by Syres »

Aleks wrote: Mon Sep 20, 2021 4:46 pm Could anyone try to at least reproduce this problem?
Is this post of any use https://forum.freecadweb.org/viewtopic. ... 20#p519695 ?
realthunder
Veteran
Posts: 2190
Joined: Tue Jan 03, 2017 10:55 am

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

Post by realthunder »

Aleks wrote: Wed Sep 15, 2021 1:37 pm Just to sum up for others so you dont have to read through the whole topic:

I want to access a body object inside a document through FreeCADs python interface. I am importing FreeCAD in the system python interpreter. But freecad wont return the body object. Can anyone reproduce this? Maybe you know what the problem is.

To reproduce it use this testfile with two simple objects, one body (PartDesign workbench) and one Extrude feature (Part workbench) and the code below.
You can also create your own testfile with a body object.
test.FCStd

Returning the extrude object works, while returning the body does not work.
I tried your code and file, and it works as expected. But I only tested the one built by myself. Are you sure you are opening the correct FCStd file? Can you please run the following code to print out all objects and post the result? Here is mine

Code: Select all

>>> [o.Name for o in doc.Objects]
['Body', 'Origin', 'X_Axis', 'Y_Axis', 'Z_Axis', 'XY_Plane', 'XZ_Plane', 'YZ_Plane', 'Sketch', 'Pad', 'Sketch001', 'Extrude']
Try Assembly3 with my custom build of FreeCAD at here.
And if you'd like to show your support, you can donate through patreon, liberapay, or paypal
User avatar
Aleks
Posts: 309
Joined: Sun Mar 08, 2020 5:27 pm
Location: Bonn, Germany
Contact:

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

Post by Aleks »

Were you using an external interpreter and Ubuntu 20.04? This problem only occurs when using those.
FreeCAD als Maschinenbauer in die Konstruktion und Fertigung integrieren. Schulung buchen: www.alsado.de/freecad-schulungen
wmayer
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

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

Post by wmayer »

Let's do things step by step.

First make sure that the project file has even a body:

Code: Select all

import sys

# Set working directories
FREECAD_FILES = '/home/aleksander/test-multi-flask-freecad/freecad-files'

fn = str(os.path.join(FREECAD_FILES, 'test.FCStd'))

os.path.exists(fn) # must return True

# Load the project as zip file
import zipfile
z = zipfile.ZipFile(fn)
data = z.open("Document.xml")
xml = data.read()

xml.find(b"PartDesign::Body") # this must return a value != -1

# Now make sure that you can load the PartDesign module. The point is that if a certain module cannot be loaded the document just ignores the corresponding objects and loads the rest.

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

doc = FreeCAD.openDocument(fn)
print (doc.Name) # is it "test"?
print (doc.FileName) # is it fn
What you can also check:
In a second terminal get the process ID of the python interpreter.

Code: Select all

ps -A | grep python3
# This may give you several instances. You have to pick the right process id then.
lsof -p process id
Post the output of the last command.

P.S.: I neither have Ubuntu 20.04 nor an external Python interpreter running.
wmayer
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

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

Post by wmayer »

My system is Ubuntu 18.04 and the standard Python version is 3.6.9. In the meantime I have downloaded and compiled 3.6.15.
When running this external Python version I can run the script above and it works as expected.

and the call of lsof clearly shows that Python 3.6.15 doesn't load any of the modules of the system version.

Btw, do you have set the environment variables PYTHONHOME or PYTHONPATH?
User avatar
Aleks
Posts: 309
Joined: Sun Mar 08, 2020 5:27 pm
Location: Bonn, Germany
Contact:

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

Post by Aleks »

Returning other objects does work, so the environment variables cannot be set incorrectly.
Maybe there is some confusion about what I want to do.

I am importing FreeCAD in a python program which runs on the system interpreter like here:
https://wiki.freecadweb.org/Embedding_FreeCAD

Then I am loading a freecad file, which successfully gets loaded.

Afterwards I try to return the body object from the document of that file. This returns null.

Returning other objects from the document works.

Even though I think I have also tried loading Part Design explicitly, I will try that again.
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: FreeCAD external Python interpreter will not return body object on Ubuntu 20.04

Post by Aleks »

Looks like "PartDesign" cannot be imported. Do you know of any reason why this would happen? Importing the "Part" module works
FreeCAD als Maschinenbauer in die Konstruktion und Fertigung integrieren. Schulung buchen: www.alsado.de/freecad-schulungen
Post Reply