Embedded python script works in Windows not Linux

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
nebs
Posts: 2
Joined: Wed Feb 08, 2023 9:33 pm

Embedded python script works in Windows not Linux

Post by nebs »

Hello, I have a script that works in Windows but not in Linux and I'm struggling trying to get this running in Linux. I should add that my problem is only when embedding the FreeCAD library in my python script. If I'm using the python console from within FreeCAD, everything works fine on both Windows and Linux.

I've made a very simple part in FreeCAD, the part label is "simple_block". My ultimate goal is to update the dimensions and export as a .stl which all works under Windows, now I need to go back to basics in Linux.

My problem is that in my embedded python script under Linux, when I try to find the part using

Code: Select all

FreeCAD.ActiveDocument.getObjectsByLabel("simple_block")
, the part doesn't seem to exist whereas in Windows it does. Note that this does work fine on both Linux and Windows through the FreeCAD python console.

I've thought that maybe I'm not loading the FreeCAD file correctly, although

Code: Select all

print(FreeCAD.ActiveDocument.FileName)
works fine.

Any clues would really help!

On Linux I install from the ubuntu repositories through apt install freecad and this installs version 0.19 along with python 3.10.

Other detaisl of my Ubuntu install:
OS: Ubuntu 22.04.1 LTS
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.19.
Build type: Release
Python version: 3.10.2
Qt version: 5.15.2
Coin version: 4.0.0
OCC version: 7.5.1
Locale: English/Canada (en_CA)


Here is the simple code that I have in Linux which I thought should run fine since it runs on Windows!

Code: Select all

import pathlib
import sys

FREECAD_LIB_PATH = pathlib.Path("/usr/lib/freecad/lib")
# this path is updated on my Windows machine

sys.path.append(FREECAD_LIB_PATH.as_posix())

import FreeCAD
import Mesh

freecad_file  = pathlib.Path().cwd() / "test_part.FCStd"

FreeCAD.open(freecad_file.as_posix())
doc = FreeCAD.ActiveDocument

# check to see if correct FreeCAD file name is returned...it is
print(doc.FileName)

# List all objects in the FreeCAD tree...none of my parts are listed in Linux but they are in Windows
for obj in doc.Objects:
    print(obj.Label)
    
# Try to grab the part by label...this doesn't work in Linux but it does work in Windows 
obj = doc.getObjectsByLabel("simple_block")
print(obj[0].Label)
The output of the above code is:
Origin
X_Axis
Y_Axis
Z_Axis
XY_Plane
XZ_Plane
YZ_Plane
Sketch
Traceback (most recent call last):
File "/home/ubuntu/test.py", line 27, in <module>
print(obj[0].Label)
IndexError: list index out of range
I expected to see the object "simple_block" in the printout...which does work in Windows, not under Linux.

Any guidance would be much appreciated!



obj = doc.getObjectsByLabel("simple_block")
print(obj[0].Label)
Attachments
test_part.FCStd
(9.28 KiB) Downloaded 8 times
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Embedded python script works in Windows not Linux

Post by onekk »

FreeCAD version supplied by distributions have some flaws.

See if an AppImage will work.

See on the official FreeCAD site how to download one.

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/
nebs
Posts: 2
Joined: Wed Feb 08, 2023 9:33 pm

Re: Embedded python script works in Windows not Linux

Post by nebs »

That worked! Thanks for the advice!
I actually prefer the AppImage but didn't know how to access the libraries from an AppImage so went with the distro install instead...I guess a few seconds of googling would have resolved all of this but perhaps it's useful for others:

After installing the AppImage, use the --appimage-extract flag to access the library directory, this creates a new diretory called squashfs-root

Code: Select all

./FreeCAD_0.20.2-2022-12-27-conda-Linux-x86_64-py310.AppImage --appimage-extract
Then the libraries are in:

Code: Select all

FREECAD_LIB_PATH = pathlib.Path("squashfs-root/usr/lib")
Post Reply