Using Gui functions without Gui.showMainWindow() in python script

Need help, or want to share a macro? Post here!
mancunia89
Posts: 41
Joined: Mon Oct 13, 2014 6:33 am

Using Gui functions without Gui.showMainWindow() in python script

Postby mancunia89 » Wed Sep 23, 2015 2:23 pm

Hi

I was wondering if it is possible to use some FreeCADGui module functions without actually firing up the FreeCAD window.
I mainly need just the App and Part module functions in my script, but I need to name my object nodes in the scenegraph. Currently, I do this by getting the root of the particular object and then doing root.setName . The seond major function I do using Gui is export the scene as .iv

Is it possible to do this without firing up the main window?
ickby
Posts: 2982
Joined: Wed Oct 05, 2011 7:36 am

Re: Using Gui functions without Gui.showMainWindow() in python script

Postby ickby » Wed Sep 23, 2015 2:28 pm

without gui part there is no scene graph, hence no way to set node names. I also think that the .iv exporter is a functionality of the coin3d scene graph, hence not available without the gui. but not exactly ssure about the last point.
mancunia89
Posts: 41
Joined: Mon Oct 13, 2014 6:33 am

Re: Using Gui functions without Gui.showMainWindow() in python script

Postby mancunia89 » Wed Sep 23, 2015 2:36 pm

I just saw the Gui.setupWithoutGUI() function. I am not exactly sure what its functionality is or if it can be used to accomplish the above.
wmayer
Site Admin
Posts: 16455
Joined: Thu Feb 19, 2009 10:32 am

Re: Using Gui functions without Gui.showMainWindow() in python script

Postby wmayer » Wed Sep 23, 2015 2:58 pm

I just saw the Gui.setupWithoutGUI() function.
This doesn't help because it doesn't initialize a couple of things you would need later.

Instead you have to use the method showWindow() and then hide it afterwards.

Code: Select all

import FreeCADGui as Gui
Gui.showMainWindow()
mw=Gui.getMainWindow()
mw.hide()
But note attributes like Gui.ActiveDocument or so can't be used because no event loop is running and thus all the Qt signal/slot or event handling is not working.

Code: Select all

...
doc=App.newDocument()
doc.addObject("Part::Box")
doc.recompute()
vp=Gui.getDocument(doc.Name).getObject(doc.ActiveObject.Name)
vp.IV
mancunia89
Posts: 41
Joined: Mon Oct 13, 2014 6:33 am

Re: Using Gui functions without Gui.showMainWindow() in python script

Postby mancunia89 » Wed Sep 23, 2015 4:03 pm

I basically need to extract a named scenegraph. What I have done till now:

Code: Select all

import FreeCAD as App
import FreeCADGui as Gui
from PyQt4.QtGui import QMainWindow, QWorkspace, QAction, QApplication
from pivy import coin

app = QApplication([''])
Gui.setupWithoutGUI()
doc = App.newDocument()
box = doc.addObject("Part::Box")

doc.recompute()
root = Gui.subgraphFromObject(box)
root.setName('testing')
This allows me to set the names of each individual object. Now, is there a way to export this as iv/vrml ? From your response I couldn't understand if this is possible or not.

Thanks
wmayer
Site Admin
Posts: 16455
Joined: Thu Feb 19, 2009 10:32 am

Re: Using Gui functions without Gui.showMainWindow() in python script

Postby wmayer » Wed Sep 23, 2015 5:37 pm

This allows me to set the names of each individual object. Now, is there a way to export this as iv/vrml ? From your response I couldn't understand if this is possible or not.
Of course it is. With "vp.IV" you get a string representation of the sub-graph and to export it to a file simply open a file and write the content to it.

And if you have a recent version of the development version you can even write this:

Code: Select all

# get the view provider of the part object
vp=Gui.getDocument(doc.Name).getObject(doc.ActiveObject.Name)
# set the name to the node
vp.RootNode.setName("testing")
# export
Gui.export([doc.ActiveObject], "Inventor.iv")
Gui.export([doc.ActiveObject], "vrml.wrl")
mancunia89
Posts: 41
Joined: Mon Oct 13, 2014 6:33 am

Re: Using Gui functions without Gui.showMainWindow() in python script

Postby mancunia89 » Thu Sep 24, 2015 8:04 am

Thank you, this is very helpful :)