FreeCAD api fails in external application

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
drmacro
Veteran
Posts: 8866
Joined: Sun Mar 02, 2014 4:35 pm

Re: FreeCAD api fails in external application

Post by drmacro »

wmayer wrote: Wed May 11, 2022 11:21 am ...
Yes __init__.py was created with the content you specified. And, it did not change the problem.

There is a rather long post in this thread about what I found where:
https://forum.freecadweb.org/viewtopic. ... 23#p594423

I noted in the above thread that, when AUR is done sys.path does not have a directory the includes PartDesign.
(In fact, the sys.path list has at least one directory that includes _PartDesign.so and __init__.py, But, no directory named PartDesign.)

I also noted that using the following script to execute Blender does fix the issue.

Code: Select all

PYTHONPATH=/usr/lib/freecad/Mod    #define PYTHONPATH
export PYTHONPATH                 #export PYTHONPATH
blender                                    #launch blender
Star Trek II: The Wrath of Khan: Spock: "...His pattern indicates two-dimensional thinking."
User avatar
adrianinsaval
Veteran
Posts: 5541
Joined: Thu Apr 05, 2018 5:15 pm

Re: FreeCAD api fails in external application

Post by adrianinsaval »

wmayer wrote: Wed May 11, 2022 11:21 am Looks OK to me. It creates /usr/lib/freecad and puts the bin and (I hope) the Mod directory inside there and the shared libraries go to /usr/lib
Yes, it also makes symlinks for /usr/lib/*.so into /usr/lib/python3.10/site-packages/ and /usr/lib/freecad/bin/* into /usr/bin
For some reason it also does

Code: Select all

echo "export PATH_TO_FREECAD_LIBDIR=/usr/lib" > /etc/profile.d/freecad.sh
do you know if this is necessary and if it even has an effect? it looks really strange to me and looks like it can cause some clashes if I have another local build of freecad
./usr/lib/python3.10/site-packages/PartDesignGui.so
./usr/lib/PartDesignGui.so
Are these identical files or do they come from different FreeCAD and/or Python versions?
The one in site-packages is a symlink to the one in /usr/lib/ so I think the file structure in AUR is correct, I think drmacro is expecting that the /usr/lib/freecad/Mod directory be in sys.path by default but I'm not sure if this is a reasonable expectation? how does this work in other systems?
wmayer
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: FreeCAD api fails in external application

Post by wmayer »

adrianinsaval wrote: Wed May 11, 2022 3:46 pm do you know if this is necessary and if it even has an effect?
See https://github.com/FreeCAD/FreeCAD/blob ... y.template

The environment variable is used in this script but not sure if this is placed into a location where it can be found by the interpreter before having loaded the FreeCAD module.
I think drmacro is expecting that the /usr/lib/freecad/Mod directory be in sys.path by default but I'm not sure if this is a reasonable expectation?
When starting FreeCAD or loading its Python module then it determines its location (i.e. the path of the executable or the library) and expects the Mod directory to be a sibling of the parent directory. Then it adds Mod and all sub-directories to sys.path.
drmacro
Veteran
Posts: 8866
Joined: Sun Mar 02, 2014 4:35 pm

Re: FreeCAD api fails in external application

Post by drmacro »

adrianinsaval wrote: Tue May 10, 2022 9:33 pm ...
The output of

Code: Select all

pacman -Ql freecad-git
produces a list

Code: Select all

/etc/
/etc/profile.d/
/etc/profile.d/freec
/usr/
/usr/bin/
/usr/bin/FreeCAD
/usr/bin/FreeCADCmd
/usr/lib/
/usr/lib/DraftUtils.
/usr/lib/Drawing.so
/usr/lib/DrawingGui.
/usr/lib/Fem.so
/usr/lib/FemGui.so
/usr/lib/FreeCAD.so
/usr/lib/FreeCADGui.
/usr/lib/Image.so
/usr/lib/ImageGui.so
...
/usr/lib/_PartDesign.so
...
/usr/lib/freecad/Mod/PartDesign/
/usr/lib/freecad/Mod/PartDesign/Init.py
/usr/lib/freecad/Mod/PartDesign/InitGui.py
/usr/lib/freecad/Mod/PartDesign/InvoluteGearFeature.py
/usr/lib/freecad/Mod/PartDesign/InvoluteGearFeature.ui
/usr/lib/freecad/Mod/PartDesign/PartDesignTests/
/usr/lib/freecad/Mod/PartDesign/PartDesignTests/TestBoolean
/usr/lib/freecad/Mod/PartDesign/PartDesignTests/TestChamfer
/usr/lib/freecad/Mod/PartDesign/PartDesignTests/TestDatum.p
/usr/lib/freecad/Mod/PartDesign/PartDesignTests/TestDraft.p
/usr/lib/freecad/Mod/PartDesign/PartDesignTests/TestFillet.
/usr/lib/freecad/Mod/PartDesign/PartDesignTests/TestHole.py
/usr/lib/freecad/Mod/PartDesign/PartDesignTests/TestInvolut
/usr/lib/freecad/Mod/PartDesign/PartDesignTests/TestLinearP
/usr/lib/freecad/Mod/PartDesign/PartDesignTests/TestLoft.py
/usr/lib/freecad/Mod/PartDesign/PartDesignTests/TestMirrore
/usr/lib/freecad/Mod/PartDesign/PartDesignTests/TestMultiTr
/usr/lib/freecad/Mod/PartDesign/PartDesignTests/TestPad.py
/usr/lib/freecad/Mod/PartDesign/PartDesignTests/TestPipe.py
/usr/lib/freecad/Mod/PartDesign/PartDesignTests/TestPocket.
/usr/lib/freecad/Mod/PartDesign/PartDesignTests/TestPolarPa
/usr/lib/freecad/Mod/PartDesign/PartDesignTests/TestPrimiti
/usr/lib/freecad/Mod/PartDesign/PartDesignTests/TestRevolve
/usr/lib/freecad/Mod/PartDesign/PartDesignTests/TestShapeBi
/usr/lib/freecad/Mod/PartDesign/PartDesignTests/TestThickne
/usr/lib/freecad/Mod/PartDesign/PartDesignTests/__init__.py
...
/usr/lib/freecad/Mod/PartDesign/__init__.py

Adding /usr/lib/freecad/Mod in the settings of sverchok does not add it to syspath (when looking at syspath from the Python or Blender python console).

I have also just discovered the FreeCAD files in /usr/lib/python3.10/site-packages are symlinks to /usr/lib.

Is /usr/lib the appropriate place for freecad files like Part.so, _PartDesign.so, PartGui.so, Path.so, etc.? (But, none to PartDesign)

And, to be duplicated in /usr/lib/freecad/Mod?

Making a symlink to PartDesign in /usr/lib/python3.10/site-packages to /usr/lib/freecad/Mod.

Also solves the import issue in Blender.

Is it possible, that, on both of these machines the earlier AUR scripts that failed left polution in these three places? (Though the symlinks in site-packages is puzzling...I think??)
Star Trek II: The Wrath of Khan: Spock: "...His pattern indicates two-dimensional thinking."
User avatar
adrianinsaval
Veteran
Posts: 5541
Joined: Thu Apr 05, 2018 5:15 pm

Re: FreeCAD api fails in external application

Post by adrianinsaval »

wmayer wrote: Wed May 11, 2022 4:28 pm The environment variable is used in this script but not sure if this is placed into a location where it can be found by the interpreter before having loaded the FreeCAD module.
Looks like in this case it would add /usr/lib to sys.path but I don't see why that would be necessary to begin with @drmacro what happens if you unset this variable before running python?
When starting FreeCAD or loading its Python module then it determines its location (i.e. the path of the executable or the library) and expects the Mod directory to be a sibling of the parent directory. Then it adds Mod and all sub-directories to sys.path.
Given that, is it bad practice to make those symlinks into site-packages? Sounds like that can be the problem here. Or is it because the libraries are in /usr/lib ? Or is this a bug due to the use of INSTALL_TO_SITEPACKAGES=ON?
drmacro wrote: Wed May 11, 2022 4:41 pm The output of

Code: Select all

pacman -Ql freecad-git
can you post the full output to see the stuff on site-packages? I'm on windows for now.
I have also just discovered the FreeCAD files in /usr/lib/python3.10/site-packages are symlinks to /usr/lib.
...
Is it possible, that, on both of these machines the earlier AUR scripts that failed left polution in these three places? (Though the symlinks in site-packages is puzzling...I think??)
The symlinks are purposefully created by the AUR script, I don't know if that's a good idea by the packager though. It is extremely unlikely that previous AUR installations would leave junk behind because it gets packaged and goes through pacman (the package manager) which keeps a list of all the files installed and removes them when you uninstall/update a package.
drmacro
Veteran
Posts: 8866
Joined: Sun Mar 02, 2014 4:35 pm

Re: FreeCAD api fails in external application

Post by drmacro »

adrianinsaval wrote: Wed May 11, 2022 5:10 pm ...@drmacro what happens if you unset this variable before running python?
Do you mean PYTHONPATH?

I specifically tried it without it being set. So, my comments are sans PYTHONPATH
...Given that, is it bad practice to make those symlinks into site-packages? Sounds like that can be the problem here. Or is it because the libraries are in /usr/lib ? Or is this a bug due to the use of INSTALL_TO_SITEPACKAGES=ON?
I don't know, but, it seem to me that just dumping all the FreeCAD modules/etc. in usr/lib is not the desired way...
Since, on Debian there is only my build directory and it works correctly, it seems dumping stuff in site-packages may not be desirable either.
can you post the full output to see the stuff on site-packages? I'm on windows for now.
I didn't post the rest because it is long and is just a directory listing o the /usr/lib/freecad/Mod directory.
...
The symlinks are purposefully created by the AUR script, I don't know if that's a good idea by the packager though. ...
Agreed.
Star Trek II: The Wrath of Khan: Spock: "...His pattern indicates two-dimensional thinking."
wmayer
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: FreeCAD api fails in external application

Post by wmayer »

Given that, is it bad practice to make those symlinks into site-packages?
The symlinks shouldn't be a problem. On Ubuntu it used to be that /usr/bin/freecad is a symlink to /usr/lib/freecad/bin/FreeCAD but the Mod directory is then correctly assumed in /usr/lib/freecad/Mod and not /usr/Mod
drmacro
Veteran
Posts: 8866
Joined: Sun Mar 02, 2014 4:35 pm

Re: FreeCAD api fails in external application

Post by drmacro »

wmayer wrote: Wed May 11, 2022 5:49 pm
Given that, is it bad practice to make those symlinks into site-packages?
The symlinks shouldn't be a problem. On Ubuntu it used to be that /usr/bin/freecad is a symlink to /usr/lib/freecad/bin/FreeCAD but the Mod directory is then correctly assumed in /usr/lib/freecad/Mod and not /usr/Mod
I wouldn't think they are a problem per se.

But, I really wonder if AUR is getting the install quite right.

The two things I see at this point is there is no PartDesign directory that Python finds by default and /usr/lib has FreeCAD packages just mixed with everything in there. As does site-packages and both site-packages and /usr/lib also has freecad/Mod.

Maybe this is the right way, but, seems a bit untidy...
Star Trek II: The Wrath of Khan: Spock: "...His pattern indicates two-dimensional thinking."
User avatar
adrianinsaval
Veteran
Posts: 5541
Joined: Thu Apr 05, 2018 5:15 pm

Re: FreeCAD api fails in external application

Post by adrianinsaval »

according to https://refspecs.linuxfoundation.org/FH ... mmingAndPa since freecad is using a subdirectory inside /usr/lib it should have everything inside that subdirectory and not part of it on /usr/lib I think that is an error in the packaging, also wmayer said that when called freecad would expect Mod to be on ../Mod so that seems like a problem, symlinking stuff into site-packages seems like a hack rather than proper packaging but I don't know about that stuff.
User avatar
adrianinsaval
Veteran
Posts: 5541
Joined: Thu Apr 05, 2018 5:15 pm

Re: FreeCAD api fails in external application

Post by adrianinsaval »

@drmacro ok I think I figured it out, here's a working PKGBUILD (remove txt extension), please test that it works without any hacks but please reboot after installation before testing to ensure the PATH_TO_FREECAD_LIBDIR env variable is not set anymore. I'll ask the AUR maintainer to use my PKGBUILD or add me as maintainer to the AUR freecad-git repo

EDIT: I've been added as co-maintainer of the AUR package so I will push my changes tonight once I'm home since I need to setup ssh authentication

EDIT2: done, please test a normal build from AUR again and ping me if something doesn't work (that could be attributed to this build)
Post Reply