Filetype stp is not supported from python script but works from freecadcmd [SOLVED]

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
froz
Posts: 3
Joined: Sat Mar 18, 2023 11:48 pm

Filetype stp is not supported from python script but works from freecadcmd [SOLVED]

Post by froz »

I am trying to import a stp file from a python script.

Code: Select all

sys.path.append("/usr/lib/freecad/lib") 
import FreeCAD
FreeCAD.loadFile("Test.stp")
I get an error

Code: Select all

"OSError: Filetype stp is not supported."
I tried exactly the same importing gdml instead and the same error occurs.

When I execute the code within the freecadcmd prompt, everything works perfectly.
I checked my sys.path which contains the freecad links

Code: Select all

 '/usr/lib/freecad-python3/Mod', '/usr/lib/freecad-python3/lib', '/usr/lib/freecad-python3/Ext'
I also checked the FreeCAD.getImportType() in python and in the freecadcmd.
In python I get:

Code: Select all

>>> FreeCAD.getImportType()
{'FCStd': 'FreeCAD'}
In freecadcmd:

Code: Select all

>>> FreeCAD.getImportType()
{'3ds': 'import3DS', 'FCMat': 'importFCMat', 'FCStd': 'FreeCAD', 'asc': 'Points', 'ast': 'Mesh', 'bdf': 'Fem', 'bms': 'Mesh', 'brep': 'Part', 'brp': 'Part', 'csg': 'importCSG', 'dae': 'importDAE', 'dat': ['importAirfoilDAT', 'Fem'], 'dwg': 'importDWG', 'dxf': 'importDXF', 'emn': 'Idf', 'frd': 'feminout.importCcxFrdResults', 'gcad': 'importOCA', 'ifc': 'importIFC', 'iges': 'Part', 'igs': 'Part', 'inp': 'Fem', 'json': 'feminout.importYamlJsonMesh', 'med': 'Fem', 'meshjson': 'feminout.importYamlJsonMesh', 'meshyaml': 'feminout.importYamlJsonMesh', 'obj': ['importOBJ', 'Mesh'], 'oca': 'importOCA', 'off': 'Mesh', 'pcd': 'Points', 'plmxml': 'PlmXmlParser', 'ply': ['Mesh', 'Points'], 'shp': 'importSHP', 'smf': 'Mesh', 'step': 'Import', 'stl': 'Mesh', 'stp': 'Import', 'stpZ': ['stepZ', 'stepZ'], 'stpz': ['stepZ', 'stepZ'], 'svg': 'importSVG', 'unv': 'Fem', 'vtk': ['Fem', 'feminout.importVTKResults'], 'vtu': ['Fem', 'feminout.importVTKResults'], 'xdmf': 'feminout.importFenicsMesh', 'xlsx': 'importXLSX', 'xml': 'feminout.importFenicsMesh', 'yaml': 'feminout.importYamlJsonMesh', 'z88': 'Fem', 'zip': 'importSH3D'}
I do not understand what is wrong with my python installation. I installed freecad0.19 in Ubuntu22.04 with python3.10 from the command apt-get install freecad.

Any idea of what is happening ? I checked similar topics on the forum but it is not clear to me what is the fix.
Last edited by froz on Sun Mar 19, 2023 11:37 pm, edited 1 time in total.
User avatar
onekk
Veteran
Posts: 6197
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Filetype stp is not supported from python script but works from freecadcmd

Post by onekk »

If you are using FreeCAD as a library, you are using FreeCAD in a different way from what is happening when you use freecadcmd.

You have to add to the python path of you interpreter the exact location of FreeCAD and associated library.

There are around some wiki pages.

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/
froz
Posts: 3
Joined: Sat Mar 18, 2023 11:48 pm

Re: Filetype stp is not supported from python script but works from freecadcmd

Post by froz »

Dear Onekk,
From your response, I have added the following pythonpath in my environment variable:

Code: Select all

export PYTHONPATH=/usr/lib/freecad:${PYTHONPATH}
That does not change the issue and I still cannot load any file. Any other idea of what is happening ?
User avatar
onekk
Veteran
Posts: 6197
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Filetype stp is not supported from python script but works from freecadcmd

Post by onekk »

Sadly I'm on mobile so no way to check.

Some extensions are set in some init files that probably are not loaded when importing FreeCAD from an external python interpreter.

There is a Wiki page listing some caveats about using an external Python interpreter.

I'm not using FreeCAD in this way so I could not help in other way than advising you to read that wiki page.

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/
froz
Posts: 3
Joined: Sat Mar 18, 2023 11:48 pm

Re: Filetype stp is not supported from python script but works from freecadcmd

Post by froz »

Ok I have found a solution thanks to your suggestions, onekk.
The solution is to add the following after importing freecad in python

Code: Select all

sys.path.append("/usr/lib/freecad/lib") 
import FreeCAD
FreeCAD.addImportType("STEP with colors (*.step *.stp)","Import")
FreeCAD.loadFile("Test.stp")
Thanks. It is now solved
Post Reply