Beispiel #1
0
def make_snapshot(input_file, output_file, size=48):
    from pivy import coin

    ext = os.path.splitext(input_file)[1][1:]
    mod = FreeCAD.getImportType(ext)
    if len(mod) == 0:
        print("Cannot load file {}".format(input_file))
        return

    # use the first listed module
    module = importlib.import_module(mod[0])
    module.open(input_file)

    doc = FreeCAD.ActiveDocument
    if doc is None:
        print("No active document")
        return

    init_gui()
    nodes = [FreeCADGui.subgraphFromObject(obj) for obj in doc.Objects]

    # add light and camera so that the rendered geometry is visible
    root = coin.SoSeparator()
    light = coin.SoDirectionalLight()
    cam = coin.SoOrthographicCamera()
    root.addChild(cam)
    root.addChild(light)
    for node in nodes:
        root.addChild(node)

    # do the rendering now
    axo = coin.SbRotation(-0.353553, -0.146447, -0.353553, -0.853553)
    width = size
    height = size
    viewport = coin.SbViewportRegion(width, height)
    cam.orientation.setValue(axo)
    cam.viewAll(root, viewport)
    off = FreeCADGui.SoQtOffscreenRenderer(width, height)
    off.setBackgroundColor(1, 1, 1)
    root.ref()

    # A QGuiApplication is needed to create an OpenGL context
    if QtGui.QGuiApplication.instance() is None:
        app = QtGui.QGuiApplication(sys.argv)

    off.render(root)
    off.writeToImage(output_file)
    root.unref()