How to access Body objects from a python script?

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Umut
Posts: 6
Joined: Tue Feb 28, 2023 7:23 am

How to access Body objects from a python script?

Post by Umut »

Dear all,

Why can’t I see the Body objects when I listed objects from a python script?
(It works if I run a macro using freecadcmd but I need a python script to better integrate with my other projects.)

Here is what I did:
I opened FreeCAD, created a cube via part design and saved the file as Cube.FCStd.
In the model view there is Case > Body > Origin and Pad. When I run following lines in Freecad python console:

Code: Select all

for obj in App.ActiveDocument.Objects: 
  print(obj.Label)
Here is the output:

Code: Select all

Body
Origin
X_Axis
Y_Axis
Z_Axis
XY_Plane
XZ_Plane
YZ_Plane
Sketch
Pad
When I try to list the objects same way by running a python script (not a macro), the list doesn’t show Body.

Here is my test.py:

Code: Select all

import FreeCAD, Part
App=FreeCAD
doc = FreeCAD.open("Cube.FCStd")
App.setActiveDocument('Cube')
for obj in doc.Objects:
    print(obj.TypeId, obj.Label)
And here is the output:

Code: Select all

App::Origin Origin
App::Line X_Axis
App::Line Y_Axis
App::Line Z_Axis
App::Plane XY_Plane
App::Plane XZ_Plane
App::Plane YZ_Plane
Sketcher::SketchObject Sketch
Im fairly new to FreeCAD, python and to this forum. Im sorry in advance if I asked a trivial question.
I appreciate any kind of help.
Last edited by Umut on Tue Feb 28, 2023 10:01 am, edited 1 time in total.
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: How to access Body objects from a python script?

Post by onekk »

You want to access from an external python interpeter?

It is not clear, usually it's a thing that has some caveats, explained in the wiki, you intend to use FreeCAD as a library.

Please learn how to use </> button to post code or code alike (python console or report view output), it will be more readable.

Regards.

Carlo D.
Last edited by onekk on Tue Feb 28, 2023 1:05 pm, edited 1 time in total.
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/
Umut
Posts: 6
Joined: Tue Feb 28, 2023 7:23 am

Re: How to access Body objects from a python script?

Post by Umut »

onekk wrote: Tue Feb 28, 2023 9:19 am Please learn how to use </> button to post code or code alike (python console or report view output), it will be more readable.
Thank you, I editted my post.

Sorry for the unclarity. For now I intend to write a simple function to add to my other python projects. For example I want to pass the path of a fcstd file to this function, where there is a single body in it, and I want this function to calculate and return boundbox of the body.

Previously I was using a macro for this. I was editting the path of the file and running it from ubuntu terminal in such way:

Code: Select all

freecadcmd path/to/macro.FCMacro
It seemed not practical so I wanted to write a class for FreeCAD operations. I don't know if it is a correct or possible approach. As I said I'm fairly new.

Umut
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: How to access Body objects from a python script?

Post by onekk »

It is possible but it depends on what version if FreeCAD you are using.

From path probably you use Linux, the problem is that on Linux you have many ways of installing FreeCAD.

Usually distribution packages are outdated and sometimes compiled for old version of library FreeCAD uses.

As example there were an official PPA for Ubuntu, but now there is no maintainer so it is not advised to use it and use instead an AppImage.

The problem is that if you are using an external interpreter you can't easily use an AppImage (it is possible but with some work to be done).

Now I'm on mobile, so I can't put links to relevant wiki pages were the matter is explained in some details.

If you are not in rush, later this afternoon I could have a computer to post a link.

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/
Umut
Posts: 6
Joined: Tue Feb 28, 2023 7:23 am

Re: How to access Body objects from a python script?

Post by Umut »

Thanks alot Carlo,

I really appreciate your help. I'm not in a rush. I'm working on other parts of my problems at hand, too.

I'm working on Ubuntu 20.04 on wsl in win11. As you guessed I'm a bit lost in python versions and ways of installing FreeCAD.

I installed appimage and flatpak installations. I couldn't change the python versions shown in ptyhon console in FreeCAD gui and had problems installing python packages. Then I installed FreeCAD this way:

Code: Select all

sudo add-apt-repository ppa:freecad-maintainers/freecad-daily -y
sudo apt install freecad-daily –y
And I add the freecadpath to path and now I can import FreeCAD to my external python scripts. freecad-daily opens FreeCAD 0.21. The gui freezes for unknown reason as soon as I open but I can still run macros via freecadcmd or through python. I think both the active python interpretter I used in Linux and FreeCAD python interpretter are same.

I'm don't know how to use venv for python projects let alone how to add FreeCAD to it. At my current state, I'm able to run projects from both terminal, sublime, pycharm, vscode, freecadcmd and I called it good enough :D

My initial thought about why I couldn't access to Body in my FreeCAD file from an external python script was, some properties or objects may be accessible only from gui and not from app. I thought this might be the case but at this point I was only assuming things and wanted to ask help here.

Thanks again.

Umut
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: How to access Body objects from a python script?

Post by onekk »

Umut wrote: Tue Feb 28, 2023 3:07 pm Thanks alot Carlo,

....
See caveats from:

https://wiki.freecad.org/Embedding_FreeCAD


but note that, from:

https://wiki.freecad.org/Installing_on_Linux

Note: the Ubuntu Universe package may be outdated as the packaging may lag behind the latest stable source code. In this case, it is suggested to install the package from the -stable PPA below. In addition, installing the -daily package can be done to test the development branch.
Stable PPA version

Warning: The FreeCAD PPA is currently unmaintained and looking for volunteers. Please use an alternative (snap, appimage) until the issue is fixed!

Personal Package Archive (PPA) for the stable FreeCAD release is maintained by the FreeCAD community on Launchpad. The Launchpad repository is called FreeCAD Stable Releases .
So the fact that the PPA is currently unmaintained, could explain the freeze.

In the past I have used Conda installs with some success, but they are quite big as you have to install the conda environment, (miniconda is decently small and fast) so you will use around 3 or 4 GB of space, but usually are updated on regular basis and from what I know are used by some developers too.

Being on Arch Linux, I can't get of more help.

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/
Umut
Posts: 6
Joined: Tue Feb 28, 2023 7:23 am

Re: How to access Body objects from a python script?

Post by Umut »

Dear @onekk or anyone else interested,

I was able to walk around my problem but I hit a wall again. Using Freecad gui, I draw a rectangle and padded it into a box.
I saved the file named as Case.FCStd.

Now by using my external python code (shared below) I want to open this document and save it as CaseEdited.FCStd.
My code runs without error but Body and Pad body objects are disappeared.

From images below you can see the model view of both Case and CaseEdited from the gui, respectively.

Image

This is also what I encountered when I tried to list the objects as I discussed in my previous entries. I was assuming bodies are there but I was just not able to access.

If you have time, can you try the python code below with your computer too? I cant confirm if the problem caused by my freecad version or wrong approach in my python code.

Cheers

My code:

Code: Select all

import sys, os
FREECADPATH = "/usr/lib/freecad-daily-python3/lib"
sys.path.append(FREECADPATH)
try:
	import FreeCAD
except ValueError:
	print('FreeCAD library not found.')

wd = "path/to/folder/where/freecad/file/resides"
case = "Case"

App=FreeCAD
doc = App.openDocument(os.path.join(wd,case)+".FCStd")
App.setActiveDocument(case)
App.getDocument(case).saveAs(os.path.join(wd,'CaseEdited')+".FCStd")
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: How to access Body objects from a python script?

Post by onekk »

Umut wrote: Tue Mar 21, 2023 9:46 am Dear @onekk or anyone else interested,
...
Hello,

Some questions to focus on your peoblem:

- If you open the file with FreeCAD solids are shown.

- If you run the relevant code in FreeCAD, either making a simple script file or pasting relevant code in Python Console it works.

Problems could happen at different levels, sadly without files is difficult to see what is happening, even if the problem is not related to a specific configuration of your machine.

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/
Umut
Posts: 6
Joined: Tue Feb 28, 2023 7:23 am

Re: How to access Body objects from a python script?

Post by Umut »

Dear @onekk

I really apprecite that you are helping.

When I open Case.FCStd with FreeCAD, and I ran each line in the my code on freecad python console it worked. When I open the CaseEdited.FCStd file I see the body and pad object in the model window. Both Case and CaseEdited files are same size on computer. (CaseEdited.FCStd when saved via my external python code, it becomes 8 kb intead of 18kb)

I assume problem is not file specific. If you open FreeCAD, part design, create body, sketch, draw any size rectangle, pad it for any height and save it, or any other file in your studies where there is at least one body present, its a good enough substitute for the file Im working on. Simply put, instead of using freecad macro and ran it either from gui or freecadcmd, if you use an external python file where you import FreeCAD, bodies are there anymore. I wanted to verify that.

Regard

Umut
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: How to access Body objects from a python script?

Post by onekk »

Umut wrote: Tue Mar 21, 2023 10:39 am Dear @onekk
...
Hello

Probably the point is that when you open a document in FreeCAD you have no guarantee that his "internal name" is the same as the filename.

There should be as example another document with a different path but with same name opened, so FreeCAD is modifying the "internal name" to avoid conflicts, or some illegal characters in name like spaces or other things, that are translated in more "manageable" characters to make things works.


Try this code instead:

Code: Select all

doc = App.openDocument(os.path.join(wd,case)+".FCStd")
d_name = doc.Name
App.setActiveDocument(d_name)
App.getDocument(d_name).saveAs(os.path.join(wd,'CaseEdited')+".FCStd")
Hope it helps.

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/
Post Reply