Embedding FreeCAD in a Python virtual environment

Having trouble installing or compiling FreeCAD? Get help here.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
mlaura.leonardi
Posts: 12
Joined: Mon Sep 26, 2022 3:02 pm

Embedding FreeCAD in a Python virtual environment

Post by mlaura.leonardi »

Hi, I am developing a code in Python, and I need to use some function of FreeCAD for a part of it. Thus, I am trying to embed FreeCAD in my Python virtual environment, following https://wiki.freecadweb.org/Embedding_FreeCAD The problem is that it looks like I need to embed Blender first, or else this does not work. Can you confirm? And, if yes, could you please tell me how to embed blender first? Thank yoi
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Embedding FreeCAD in a Python virtual environment

Post by onekk »

mlaura.leonardi wrote: Tue Sep 27, 2022 9:51 am Hi, I am developing a code in Python, and I need to use some function of FreeCAD for a part of it. Thus, I am trying to embed FreeCAD in my Python virtual environment, following https://wiki.freecadweb.org/Embedding_FreeCAD The problem is that it looks like I need to embed Blender first, or else this does not work. Can you confirm? And, if yes, could you please tell me how to embed blender first? Thank yoi
No it is not exactly true.

The example is a program that use FreeCAD (FC for short) inside Blender, and this is clear as it is told:
we'll import the Part geometry of a FreeCAD document into blender

Main problem when embedding FC is that you must use the same Python Version on which FC is compiled or unpredictable result may happens.

It is not the "most easy way" to use FC, is something better the "other way round", use the internal Python interpreter of FC to make things, like take some data and output some other data.

It depends on what part of FC you intend to use, if you want to use mainly "Part WB" an alternative is to use even some other OCCT related libraries, that are meant to be used as a library like PythonOCC.

But your mileage may vary as it could even depend on what OS and what Version of FC you are targeting.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
User avatar
adrianinsaval
Veteran
Posts: 5541
Joined: Thu Apr 05, 2018 5:15 pm

Re: Embedding FreeCAD in a Python virtual environment

Post by adrianinsaval »

it's just an example using blender, the important thing is

Code: Select all

FREECADPATH = '/opt/FreeCAD/lib' # path to your FreeCAD.so or FreeCAD.dll file
import sys
sys.path.append(FREECADPATH)
try:
   import FreeCAD
except ValueError:
   print('FreeCAD library not found. Please check the FREECADPATH variable in the import script is correct')
and then import the modules you need from FreeCAD and start using them, on some linux systems and I believe in the conda environments too you can just do

Code: Select all

import freecad
and it'll add the path itself.
mlaura.leonardi
Posts: 12
Joined: Mon Sep 26, 2022 3:02 pm

Re: Embedding FreeCAD in a Python virtual environment

Post by mlaura.leonardi »

Dear @onekk , thanks for your response. Thus, if I really need to use FC embedded (which is the case), is it better for me to compile it first, in order to ensure the use of the same version of Python?
mlaura.leonardi
Posts: 12
Joined: Mon Sep 26, 2022 3:02 pm

Re: Embedding FreeCAD in a Python virtual environment

Post by mlaura.leonardi »

Hi @adrianinsaval ! Thanks for your response. Do you know where can I find the .dll file? Because in the tutorial they just mention the FreeCAD.dll file, whereas in my bin folder I found three FreeCAD. dll files: FreeCADApp.dll, FreeCADBase.dill and FreeCADGui.dll
User avatar
adrianinsaval
Veteran
Posts: 5541
Joined: Thu Apr 05, 2018 5:15 pm

Re: Embedding FreeCAD in a Python virtual environment

Post by adrianinsaval »

I don't have much knowledge on windows but I see a FreeCAD.pyd file in the bin directory and import FreeCAD seems to work running the included python so I think that's the path you need.
About compiling, depending on what application you are trying to embed FreeCAD into it might be easier to compile that to match FreeCAD instead, or they might be using the same version already. Note that we have FreeCAD builds available for python 3.8 and 3.10 so check if one those would work for your usecase.
mlaura.leonardi
Posts: 12
Joined: Mon Sep 26, 2022 3:02 pm

Re: Embedding FreeCAD in a Python virtual environment

Post by mlaura.leonardi »

Unfortunately, when I tried this approach, this is what I get @adrianinsaval
Image
Attachments
error.png
error.png (34.19 KiB) Viewed 1201 times
User avatar
adrianinsaval
Veteran
Posts: 5541
Joined: Thu Apr 05, 2018 5:15 pm

Re: Embedding FreeCAD in a Python virtual environment

Post by adrianinsaval »

sorry to have confused you but you need the path to the folder where the file is, not the full path of file iself. So just "C:\Program Files\FreeCAD 0.20\bin"
mlaura.leonardi
Posts: 12
Joined: Mon Sep 26, 2022 3:02 pm

Re: Embedding FreeCAD in a Python virtual environment

Post by mlaura.leonardi »

Unfortunately this is still giving me the same issue :(
Attachments
issue.png
issue.png (33.44 KiB) Viewed 1169 times
mlaura.leonardi
Posts: 12
Joined: Mon Sep 26, 2022 3:02 pm

Re: Embedding FreeCAD in a Python virtual environment

Post by mlaura.leonardi »

Unfortunately this is still giving me the same issue :(
Attachments
issue.png
issue.png (33.44 KiB) Viewed 1167 times
Post Reply