As with most open-source projects I'm sure you come across a lot of people who take your work for granted and expect support etc, so I want to start by saying thank you for FreeCAD. It really is appreciated.
I'm new to FreeCAD (installed yesterday) and I'm currently exploring the Python API for creating stand-alone scripts for full automation. So far I've managed to create some basic shapes and export them in various formats (fcstd for loading in FreeCAD, Inventor for rendering with pivy, 2D projections in SVG).
What I would like to do now is export the 3D view to an image file from my script. I want to use the same methods that the GUI does via "Tools->Save picture...", which pops up a dialogue with multiple export formats.
Here's what I have so far (these are pieced-together hacks for testing):
This will create a simple shape and export the aforementioned files:
Code: Select all
#!/usr/bin/env python2
import sys
FREECADPATH = '/usr/lib/freecad/lib'
sys.path.append(FREECADPATH)
import FreeCAD, Part, Drawing
#dv = 0.000000000001
box = Part.makeBox(100,100,100)
box.translate(box.CenterOfMass.multiply(-1))
cylx = Part.makeCylinder(25, 100, FreeCAD.Base.Vector(0,1,0), FreeCAD.Base.Vector(0,90,0))
cylx.translate(cylx.CenterOfMass.multiply(-1))
cyly = Part.makeCylinder(25, 100, FreeCAD.Base.Vector(0,1,0), FreeCAD.Base.Vector(90,0,0))
cyly.translate(cyly.CenterOfMass.multiply(-1))
cylz = Part.makeCylinder(25, 100)
cylz.translate(cylz.CenterOfMass.multiply(-1))
hole = cylx.fuse(cyly).fuse(cylz)
test = box.cut(hole).common(Part.makeSphere(50))
with open('coin.iv', 'w') as f:
f.write(test.writeInventor())
Part.show(test)
# FreeCAD.ActiveDocument.FileName = 'foo.fcstd'
# FreeCAD.ActiveDocument.save()
Shape = FreeCAD.ActiveDocument.Shape.Shape
visibleG0,visibleG1,hiddenG0,hiddenG1 = Drawing.project(Shape,FreeCAD.Base.Vector(1,1,1))
resultSVG = Drawing.projectToSVG(Shape,FreeCAD.Base.Vector(1,0.2,1))
# if 0 is used instead of dv, the image is rendered incorrectly
#resultSVG = Drawing.projectToSVG(Shape,FreeCAD.Base.Vector(0,dv,1))
with open('foo.svg', 'w') as f:
f.write('<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="-100 -100 200 200">')
f.write(resultSVG)
f.write('</svg>')
Code: Select all
from pivy.sogui import *
from pivy.coin import *
import sys
def main():
myWindow = SoGui.init(sys.argv[0])
if myWindow == None: sys.exit(1)
input = SoInput()
input.openFile("coin.iv")
scene = SoDB.readAll(input)
viewer = SoGuiExaminerViewer(myWindow)
viewer.setSceneGraph(scene)
viewer.setTitle("Scene Viewer")
#viewer.setBackgroundColor()
viewer.show()
SoGui.show(myWindow) # Display main window
SoGui.mainLoop() # Main Coin event loop
if __name__ == "__main__":
main()
* recording a macro and looking at the generated code (macro uses activeDocument().activeView().saveImage(), but script "gives AttributeError: 'App.Document' object has no attribute 'activeView'")
* pivy SoOffscreenRenderer, but only SGI RGB output is supported (not sure why, can't find any compile options for coin that would disable other formats), and the generated image is empty, probably because I haven't set the scene properly
Obviously I'm new to the API and I'm probably not looking in the right place for the documentation. So far I've just been piecing it together from docs, examples and the ipython shell. I think I can get an image if I keep digging through the SoOffscreenRenderer api, but it doesn't seem to be the same thing that the GUI is using.
All I want to do is be able to set the view and export the generated 3D image as it would be done when saving the image via the GUI (minus the dialogue), i.e. using the "built-in" renderer. I do not want to export to povray.
Additional information:
system: Arch Linux x86_64
freecad version: 0.11.4422-5
pivy-hg: 20100809-4
coin: 3.1.3-5
opencascade: 6.5.1-1
Thanks for any help, and sorry if this has already been covered or if this post is in the wrong place.