Accessing workbench through Headless FreeCAD

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
george2
Posts: 2
Joined: Thu Dec 08, 2022 9:25 pm

Accessing workbench through Headless FreeCAD

Post by george2 »

Hello,

I would like to access FreeCAD workbench from 'outside' of FreeCAD. Basically by running a python script in a python IDE with FreeCAD being closed(or opened, does not matter).
This is the workbench I am trying to access: FreeCAD SheetMetal Workbench.

Some time ago, I asked this question on workbench's github page, and got a reply that there is no access to Workbenches which are FreeCAD GUI based, through Headless FreeCAD.

Is this still valid?

I would be very grateful for any kind of answer. Thank you in advance.
User avatar
onekk
Veteran
Posts: 6222
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Accessing workbench through Headless FreeCAD

Post by onekk »

Probably yes as things are not changed too much.

If WB is coded in C++ and has no Python exposed API it is very difficult for Python to have access to it.

But I'm not very expert on this side of FreeCAD, so I could be wrong.

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/
heda
Veteran
Posts: 1348
Joined: Sat Dec 12, 2015 5:49 pm

Re: Accessing workbench through Headless FreeCAD

Post by heda »

"I would like to access FreeCAD workbench from 'outside' of FreeCAD."

what have you actually tried codewise?

you are not the first person to ask such question.
have some sympathy for using without seeing,
but am mostly wondering how people that asks these type of questions hope to make any code work, since most of the time it is quite clear that op's have misconceptions of how fc is bolted together. can't but wonder if that is coming from that there is a python-api and when people have learned how to import a std-module in python - they assume that this is how everything python works.

anyhow, let's pick the goose feather by feather.
first of all, fc is not a first citizen py-application/session, it is just an app that happens to have python as a possible avenue for interaction (there is neither a promise that py in fc behaves like a vanilla std py install, nor that everything is reachable from python. apart from that your question is about using sheetmetal, if using sheetmetal, per definition most people would be inclined to say that you are using sheetmetal-api, not fc api, sheetmetal itself is the one using fc-api, if you make a part.box, then you are using fc-api).

the not first citizen fact means using fc from a different py-session is a steep uphill battle (*), while using any py you want in a fc-py session is sailing in smooth waters (or as smooth as it gets).
i.e. if doing anything fc related, always use fc-py-sessions
(*) - not that it cannot be done, but when having to raise the question on this forum, you are going to struggle to get it to work..., so better take the easy route...

one can obviously run fc headless, but that does not mean that you can do everything in headless, more specifically in headless the guiloop (and related inits) are not run (kind of the definition of headless).

so, any code/wb that runs a single line gui dependent code, incl gui triggered prerequisites will simply fail in headless.

let's see what sheetmetal has to offer in headless...

Code: Select all

xx@yy: FreeCAD-0.20.0-Linux-x86_64.AppImage -c
Sheet Metal workbench loaded
[FreeCAD Console mode <Use Ctrl-D (i.e. EOF) to exit.>]
>>>
at least it did not bug out on it's own during init.
so what happens when one imports something from sheetmetal...

Code: Select all

>>> import SheetMetalCmd
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/xx/.local/share/FreeCAD/Mod/sheetmetal/./SheetMetalCmd.py", line 26, in <module>
    from FreeCAD import Gui
ImportError: cannot import name 'Gui' from 'FreeCAD' (unknown location)
>>> 
kind of expected, the code is gui dependent, (i.e. the straightforward answer to your question is simple enough - look into source - if there is any gui-code triggered it will not run in headless).

now you are faced with two choices:
a) patch sheetmetal wb (which is not core, but addon), so that commands work in headless (if you do, you should make pullrequests to that repo)
or
b) just accept that you have to run a gui-session and be done with it.

when it comes to a gui-session, one can always mask the gui

a metal.py file with something like...

Code: Select all

print('hello fc world')

import FreeCAD as App
import FreeCADGui as Gui

print('is gui up?', App.GuiUp)

mw = Gui.getMainWindow()
mw.hide()

print('gui is now hidden, but session is running')

import SheetMetalCmd

print('import of commands from sheetmetal now works')

print('gracefully exiting fc earliest in 5 seconds')

from PySide import QtCore, QtGui

QtGui.QMessageBox.information(None, 'mygui', 'can do gui as well...')

QtCore.QTimer.singleShot(5e3, lambda: QtGui.QApplication.quit())
with a cmd-start like...

Code: Select all

xx@yy: FreeCAD ShowSplasher=0 metal.py
is as good as it gets, afaik (and better not hide it until code works).
suppressing the splash on the fly does not bite on my box (the above way), would have been nice if it did,
so either do manually by preferences, or load a complete .cfg on the fly, or whatever else way that works
george2
Posts: 2
Joined: Thu Dec 08, 2022 9:25 pm

Re: Accessing workbench through Headless FreeCAD

Post by george2 »

Thank you for the replies both heda and onekk.
I haven't written any code related with this issue. I am asking in advance if it is possible. If it would be, I would start learning FreeCAD python scripting.

Heda, the masking of the gui you did, just prevents raising an error on the 'from FreeCAD import Gui' line in 'SheetMetalBaseCmd.py'? It still does not mean that commands from 'SheetMetalBaseCmd.py' which are using the GUI, will work? Or did I misunderstand you? Thank you for the answer in advance.
User avatar
onekk
Veteran
Posts: 6222
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Accessing workbench through Headless FreeCAD

Post by onekk »

It is not a simple answer as "it depends".

Some commands coukd be issued and operate "on selected objects", in this case you usually could:

- make proper selections.
- issue the command.

Some other commands, involve some user interactions snd usually they are difficult to "automate" as there are considerations to do about what the commands is expecting.


A little example could be the Sketcher fillet or the Sketcher polygon that could be automated but need to write helpers methods to precalculated things as the command is expecting correctly placed points and this is not not so trivial.

So even working in Python not every "action" could be duplicated, as some command sequences could be "recorded" and reissued but only to create the same thing.

As usual scripting involve to have much flexibility, it is sad to say. But very much could be done with some experiments and a little bit if fantasy and creativity.

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/
heda
Veteran
Posts: 1348
Joined: Sat Dec 12, 2015 5:49 pm

Re: Accessing workbench through Headless FreeCAD

Post by heda »

"Or did I misunderstand?"
yes, easiest way for you to avoid that is to get going with learning the basics (and at least try to run code by yourself).
yes, you got the full (working) answer

this is the console trace on my box

Code: Select all

Sheet Metal workbench loaded
hello fc world
is gui up? 1
gui is now hidden, but session is running
import of commands from sheetmetal now works
gracefully exiting fc earliest in 5 seconds
no error, meaning that Gui is available, sheetmetal is a workbench that has to be loaded (imported) as any other workbench, and if you are in gui, you principally can just record a macro and use that as a bases for a script (disclaimer - do not know if sheetmetal is coded in such way that macro recordings works).

the whole point of the posted code was to showcase how to get a full-blown fc-gui-session (without seeing it),
so anything you can make fc do from the gui-py-console can now be done in a script (metal.py).

scripting in sheetmetal is not my bag, so someone else will have to take that.

would not worry about what you can or can't do at this point in time.
you should just focus on learning basic python & scripting in fc without sheetmetal to begin with.
that takes weeks if you already know python well, and months if you do not.

pretty sure you will find answers to most of your questions by yourself if you actually start the study & learning phase...
Post Reply