Embedding FreeCAD in a Python virtual environment
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Be nice to others! Respect the FreeCAD code of conduct!
-
- Posts: 12
- Joined: Mon Sep 26, 2022 3:02 pm
Embedding FreeCAD in a Python virtual environment
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
Re: Embedding FreeCAD in a Python virtual environment
No it is not exactly true.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
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/
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.
Blog: https://okkmkblog.wordpress.com/
- adrianinsaval
- Veteran
- Posts: 5541
- Joined: Thu Apr 05, 2018 5:15 pm
Re: Embedding FreeCAD in a Python virtual environment
it's just an example using blender, the important thing is
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
and it'll add the path itself.
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')
Code: Select all
import freecad
-
- Posts: 12
- Joined: Mon Sep 26, 2022 3:02 pm
Re: Embedding FreeCAD in a Python virtual environment
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?
-
- Posts: 12
- Joined: Mon Sep 26, 2022 3:02 pm
Re: Embedding FreeCAD in a Python virtual environment
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
- adrianinsaval
- Veteran
- Posts: 5541
- Joined: Thu Apr 05, 2018 5:15 pm
Re: Embedding FreeCAD in a Python virtual environment
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.
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.
-
- Posts: 12
- Joined: Mon Sep 26, 2022 3:02 pm
Re: Embedding FreeCAD in a Python virtual environment
Unfortunately, when I tried this approach, this is what I get @adrianinsaval
- Attachments
-
- error.png (34.19 KiB) Viewed 1201 times
- adrianinsaval
- Veteran
- Posts: 5541
- Joined: Thu Apr 05, 2018 5:15 pm
Re: Embedding FreeCAD in a Python virtual environment
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"
-
- Posts: 12
- Joined: Mon Sep 26, 2022 3:02 pm
Re: Embedding FreeCAD in a Python virtual environment
Unfortunately this is still giving me the same issue
- Attachments
-
- issue.png (33.44 KiB) Viewed 1169 times
-
- Posts: 12
- Joined: Mon Sep 26, 2022 3:02 pm
Re: Embedding FreeCAD in a Python virtual environment
Unfortunately this is still giving me the same issue
- Attachments
-
- issue.png (33.44 KiB) Viewed 1167 times