ViewProvider for (python) objects created in headless mode?

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
mlampert
Veteran
Posts: 1772
Joined: Fri Sep 16, 2016 9:28 pm

ViewProvider for (python) objects created in headless mode?

Post by mlampert »

There are a few guys brave enough to use Path headless and create their g-code via a script - however, they are not brave enough to not wanting to double check the result (smart guys) :mrgreen:

In other words they want to load the generated file into FreeCAD (with GUI) and look at it. Which doesn't work because Path doesn't assign the ViewObject.Proxy in headless mode. Mostly because it would require loading things like "PySide.QtGui" and other libraries which don't exist in headless mode. So what we end up with are FeaturePython objects without their UI properly being setup.

So the question is - how should this be done correctly?
How does the view provider get attached to an object when the object was created in an environment where no view providers existed?
mlampert
Veteran
Posts: 1772
Joined: Fri Sep 16, 2016 9:28 pm

Re: ViewProvider for (python) objects created in headless mode?

Post by mlampert »

anybody got some insight on how to deal with this issue?
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: ViewProvider for (python) objects created in headless mode?

Post by Kunda1 »

I remember I was exploring headless FC stuff in the recent past.but I can't find the posts atm. @Vocx had some good insight in to this. One of the issues that you run into immediately is that the codebase is oriented to the GUI not being activated. His idea was to start adding the condition:

Code: Select all

If GUI.isUp

  else
     headless_stuff_here
I'm on my mobile right now so I'll need to come back to this
Alone you go faster. Together we go farther
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
'good first issues' | Open TODOs and FIXMEs | How to Help FreeCAD | How to report Bugs
wmayer
Founder
Posts: 20307
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: ViewProvider for (python) objects created in headless mode?

Post by wmayer »

So the question is - how should this be done correctly?
How does the view provider get attached to an object when the object was created in an environment where no view providers existed?
It's not possible to create or access the view provider in headless mode.

Here is the code how to access the view provider of an object in GUI mode:
https://github.com/FreeCAD/FreeCAD/blob ... p.cpp#L190

What's possible is to load FreeCADGui in headless mode but there is no way to access the GUI document because it won't be created and consequently there exist no view providers.

However, what's possible (and this is what kunda probably meant) is to create a scene graph representation of an object. This works this way:

Code: Select all

import FreeCADGui as Gui
from pivy import coin

Gui.setupWithoutGUI()
doc = App.newDocument()
obj = doc.addObject("Part::Box","Box")
doc.recompute()
view = Gui.subgraphFromObject(obj)
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: ViewProvider for (python) objects created in headless mode?

Post by Kunda1 »

Started a wiki page to document this Headless FreeCAD
Alone you go faster. Together we go farther
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
'good first issues' | Open TODOs and FIXMEs | How to Help FreeCAD | How to report Bugs
User avatar
sliptonic
Veteran
Posts: 3459
Joined: Tue Oct 25, 2011 10:46 pm
Location: Columbia, Missouri
Contact:

Re: ViewProvider for (python) objects created in headless mode?

Post by sliptonic »

The desire is to create documents in headless mode that, when opened in the GUI later, will look and behave as though they were created in the GUI originally.
galou_breizh
Posts: 436
Joined: Wed Sep 15, 2010 9:38 am

Re: ViewProvider for (python) objects created in headless mode?

Post by galou_breizh »

I also needed this for my csv2freecad script. The actual situation is that the generated objects (vertices) are hidden when I open the file in "normal" FreeCAD and I cannot change the values related to the view, for example the point size.

Interestingly, the vertex objects have a Visibility class member (available in my script), which, as I said, has no influence.
User avatar
onekk
Veteran
Posts: 6205
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: ViewProvider for (python) objects created in headless mode?

Post by onekk »

How about to work on separating things at least to have the ability to load a FreeCAD window that have very little "burden" of the Gui Part.

To be more clear.

Some visualization commands, to rotate ad change view, a TreeView maybe collapsible or detachable and a big visualisation windows like in Gimp.

Or maybe a hide buttons with a relative scriptable commands that will hide most of the Gui and leave only the TreeView, maybea "detachable" Python Console and Report Window.

In this manner, when scripting, you could have the Visualization part, that is relevant and with a button click regain the ability to use the Gui if needed.

Maybe not the best solution, but could be a good improvement some efforts have been done by one user I think, with TreeView ans console that are autohiding when not in use (Modern UI if i don't remember 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/
User avatar
bernd
Veteran
Posts: 12851
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: ViewProvider for (python) objects created in headless mode?

Post by bernd »

sliptonic wrote: Wed Feb 24, 2021 8:31 pm The desire is to create documents in headless mode that, when opened in the GUI later, will look and behave as though they were created in the GUI originally.
This is something I would like to be able to do as well. In BIM processing this is needed quite often.
wmayer
Founder
Posts: 20307
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: ViewProvider for (python) objects created in headless mode?

Post by wmayer »

galou_breizh wrote: Fri Feb 26, 2021 3:40 pm I also needed this for my csv2freecad script. The actual situation is that the generated objects (vertices) are hidden when I open the file in "normal" FreeCAD and I cannot change the values related to the view, for example the point size.

Interestingly, the vertex objects have a Visibility class member (available in my script), which, as I said, has no influence.
The reason why the objects are hidden is that a project created in headless mode lacks of a GuiDocument.xml.

When reading in a project then the objects are made invisible to avoid expensive renderings of the scene graph that can considerably slow-down the loading.
Now with a normal project file the GuiDocument.xml is read-in as almost last file and this then changes the visibility again but if the file is lacking all objects stay invisible.
Post Reply