[vtkusers] QVTKRenderWindowInteractor and SetSize
Marcin Bukat
marcin.bukat at gmail.com
Tue Jul 7 08:38:39 EDT 2015
Hello,
I have a problem resizing offscreen rendered window. The code below is
simple demo of my problem. When I run this code on linux everything work as
expected, saved 'screenshot' is of requested 320x240 resolution and UI
stays intact. On windows however SetSize() is simply ignored and screenshot
is saved in native UI resolution. Interactive resizing of vtk widget works
correctly so I guess I am missing some important bit. Any help is welcome.
PS. I use vtk6.2 and qt4.8 on both systems
#!/usr/bin/env python
import sys
import vtk
from PyQt4 import QtCore, QtGui
from vtk.qt4.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent = None):
QtGui.QMainWindow.__init__(self, parent)
self.hs = QtGui.QSplitter(QtCore.Qt.Horizontal)
self.vs = QtGui.QSplitter(QtCore.Qt.Vertical)
self.vspace0 = QtGui.QLabel('')
self.vs.addWidget(self.vspace0)
self.vtkWidget = QVTKRenderWindowInteractor(self.vs)
self.vs.addWidget(self.vtkWidget)
self.vspace1 = QtGui.QPushButton('Save')
self.vs.addWidget(self.vspace1)
self.hspace0 = QtGui.QLabel('')
self.hs.addWidget(self.hspace0)
self.hs.addWidget(self.vs)
self.hspace1 = QtGui.QLabel('')
self.hs.addWidget(self.hspace1)
self.ren = vtk.vtkRenderer()
self.vtkWidget.GetRenderWindow().AddRenderer(self.ren)
self.iren = self.vtkWidget.GetRenderWindow().GetInteractor()
# Create source
source = vtk.vtkConeSource()
source.SetResolution(256)
# Create a mapper
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(source.GetOutputPort())
# Create an actor
actor = vtk.vtkActor()
actor.SetMapper(mapper)
self.ren.AddActor(actor)
self.ren.ResetCamera()
self.setCentralWidget(self.hs)
self.show()
self.iren.SetInteractorStyle(vtk.vtkInteractorStyleRubberBandPick())
self.iren.Initialize()
self.vspace1.clicked.connect(self.screenshot)
def to_image(self, renderWindow):
windowToImageFilter = vtk.vtkWindowToImageFilter()
windowToImageFilter.SetInput(renderWindow)
windowToImageFilter.SetMagnification(1)
windowToImageFilter.Update()
return windowToImageFilter
def screenshot(self):
wren = self.vtkWidget.GetRenderWindow()
w,h = wren.GetSize()
wren.OffScreenRenderingOn()
print((w,h))
wren.SetSize(320,240)
print(wren.GetSize())
windowToImageFilter = self.to_image(wren)
writer = vtk.vtkPNGWriter()
writer.SetInputConnection(windowToImageFilter.GetOutputPort())
writer.SetFileName('test_out.png')
writer.Write()
wren.SetSize(w,h)
wren.OffScreenRenderingOff()
print('Saved screenshot!')
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = MainWindow()
window.resize(800,800)
window.setWindowTitle('Simple')
sys.exit(app.exec_())
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20150707/7d128314/attachment.html>
More information about the vtkusers
mailing list