更新 vtk 中的演员
update actor in vtk
我打算设计一个图形界面,显示三个文件,可以传输和旋转。
旋转与 widget
框配合得很好。
问题是我想用键盘翻译。翻译很好,但是没有更新window,我必须放大到缩小[=26] =] 查看更改。
我尝试更新 render
、actor
、viewport
,但没有成功。
有人知道我怎么解决吗?
(我认为解决方案是更新相机,但它不起作用)
class MainWindow(QMainWindow):
def __init__(self, parent = None):
super().__init__()
self.file_name = '../../../data/liver.stl'
self.scene = scene.CreateScene(self.file_name)
self.camera = vtk.vtkCamera()
# Create a cell picker
self.picker=vtk.vtkCellPicker()
self.boxWidget = vtk.vtkBoxWidget()
self.initUI()
self.picker.AddObserver("EndPickEvent",self.EndPickEvent)
self.first_time= True
self.LastPickedActor = None
self.LastPickedProperty = vtk.vtkProperty()
# Now at the end of the pick event call the above function.
def EndPickEvent(self,object,event):
print("pick")
if self.picker.GetCellId()>=0:
# get the new
self.PickedActor=self.picker.GetActor()
print(self.PickedActor.name)
#If we picked something before, reset its property
if self.LastPickedActor:
self.LastPickedActor.GetProperty().DeepCopy(self.LastPickedProperty)
# Save the property of the picked actor so that we can
# restore it next time
self.LastPickedProperty.DeepCopy(self.PickedActor.GetProperty())
# Highlight the picked actor by changing its properties
self.PickedActor.GetProperty().SetColor(1.0, 0.0, 0.0)
self.PickedActor.GetProperty().SetDiffuse(1.0)
self.PickedActor.GetProperty().SetSpecular(0.0)
# save the last picked actor
self.LastPickedActor = self.PickedActor
else:
self.LastPickedActor.GetProperty().DeepCopy(self.LastPickedProperty)
if self.first_time==False:
self.boxWidget.Off()
self.first_time = False
self.BoxActor()
def initUI(self):
self.frame = QFrame()#clase basica para tener un cuadro
self.vl = QVBoxLayout()# This class is used to construct vertical box layout objects
self.vtkWidget = QVTKRenderWindowInteractor(self.frame)#definimos un interactusador render
self.vtkWidget.SetPicker(self.picker)
self.vl.addWidget(self.vtkWidget)
self.statusBar().showMessage('Idle')
self.setGeometry(300, 300, 800, 500)# posicion de la ventana los dos primeros, los dos siguientes tamaño de la ventana
self.setWindowIcon(QtGui.QIcon('alma.png'))# icono superior izquierda debe estar en mismo path
self.setWindowTitle('Example 5')#titulo de la ventana
self.frame.setLayout(self.vl)
self.setCentralWidget(self.frame)
self.CreateSetUp()
self.createMenu()#llamar a funcion menu
self.interactor.AddObserver("KeyPressEvent", self.Keypress)
def CreateSetUp(self) :
self.render_window = self.vtkWidget.GetRenderWindow()
self.interactor = self.render_window.GetInteractor()
#tamaño de las cuatro ventanas
xmin_vp = [0.0, 0.0, 0.0, 0.5]
xmax_vp = [0.5, 0.5, 0.5, 1.0]
ymin_vp = [0.0, 0.33, 0.66, 0.0]
ymax_vp = [0.33, 0.66, 1.0, 1.0]
self.renderers = []
for i in range(4) :#cuatro renders
self.r = vtk.vtkRenderer()#determinamos render
print(self.r)
self.r.SetBackground((0.8, 0.8, 0.8))#color del fondo
self.r.SetViewport(xmin_vp[i], ymin_vp[i], xmax_vp[i], ymax_vp[i])
for element in self.scene.selection[i]:
if isinstance(element, vtk.vtkActor) :
self.r.AddActor(element)
elif isinstance(element, vtk.vtkVolume) :
print("volume")
self.r.AddVolume(element)
self.renderers.append(self.r)
self.render_window.AddRenderer(self.r)
self.interactor.Initialize()
self.interactor.Start()
self.show()
def boxCallback(self,obj, event):
t = vtk.vtkTransform()
obj.GetTransform(t)
obj.GetProp3D().SetUserTransform( t )
def BoxActor(self):
self.boxWidget.SetInteractor(self.interactor)
self.boxWidget.SetProp3D(self.PickedActor)
self.boxWidget.ScalingEnabledOff()#no poder hacer ni mas grande ni mas pequeño
self.boxWidget.HandlesOff()#Eeliminar esferas
self.boxWidget.SetPlaceFactor( 1.25 ) # Make the box 1.25x larger than the actor
self.boxWidget.PlaceWidget()
self.boxWidget.On()
self.boxWidget.AddObserver("InteractionEvent", self.boxCallback)
def Keypress(self,obj, event):
key = obj.GetKeySym()
if self.picker.GetCellId()>=0:
if key=='Up':
print("arriba")
self.PickedActor.AddPosition(0,1,0)
elif key=='Down':
print("abajo")
self.PickedActor.AddPosition(0,-1,0)
elif key=='Right':
print("derecha")
self.PickedActor.AddPosition(1,0,0)
elif key=='Left':
print("izquierda")
self.PickedActor.AddPosition(-1,0,0)
elif key=='0':
print("acercar")
self.PickedActor.AddPosition(0,0,1)
elif key=='1':
print("alejar")
self.PickedActor.AddPosition(0,0,-1)
问题解决了,只需要输入
self.render_window.Render ()
每次翻译后
我打算设计一个图形界面,显示三个文件,可以传输和旋转。
旋转与 widget
框配合得很好。
问题是我想用键盘翻译。翻译很好,但是没有更新window,我必须放大到缩小[=26] =] 查看更改。
我尝试更新 render
、actor
、viewport
,但没有成功。
有人知道我怎么解决吗? (我认为解决方案是更新相机,但它不起作用)
class MainWindow(QMainWindow):
def __init__(self, parent = None):
super().__init__()
self.file_name = '../../../data/liver.stl'
self.scene = scene.CreateScene(self.file_name)
self.camera = vtk.vtkCamera()
# Create a cell picker
self.picker=vtk.vtkCellPicker()
self.boxWidget = vtk.vtkBoxWidget()
self.initUI()
self.picker.AddObserver("EndPickEvent",self.EndPickEvent)
self.first_time= True
self.LastPickedActor = None
self.LastPickedProperty = vtk.vtkProperty()
# Now at the end of the pick event call the above function.
def EndPickEvent(self,object,event):
print("pick")
if self.picker.GetCellId()>=0:
# get the new
self.PickedActor=self.picker.GetActor()
print(self.PickedActor.name)
#If we picked something before, reset its property
if self.LastPickedActor:
self.LastPickedActor.GetProperty().DeepCopy(self.LastPickedProperty)
# Save the property of the picked actor so that we can
# restore it next time
self.LastPickedProperty.DeepCopy(self.PickedActor.GetProperty())
# Highlight the picked actor by changing its properties
self.PickedActor.GetProperty().SetColor(1.0, 0.0, 0.0)
self.PickedActor.GetProperty().SetDiffuse(1.0)
self.PickedActor.GetProperty().SetSpecular(0.0)
# save the last picked actor
self.LastPickedActor = self.PickedActor
else:
self.LastPickedActor.GetProperty().DeepCopy(self.LastPickedProperty)
if self.first_time==False:
self.boxWidget.Off()
self.first_time = False
self.BoxActor()
def initUI(self):
self.frame = QFrame()#clase basica para tener un cuadro
self.vl = QVBoxLayout()# This class is used to construct vertical box layout objects
self.vtkWidget = QVTKRenderWindowInteractor(self.frame)#definimos un interactusador render
self.vtkWidget.SetPicker(self.picker)
self.vl.addWidget(self.vtkWidget)
self.statusBar().showMessage('Idle')
self.setGeometry(300, 300, 800, 500)# posicion de la ventana los dos primeros, los dos siguientes tamaño de la ventana
self.setWindowIcon(QtGui.QIcon('alma.png'))# icono superior izquierda debe estar en mismo path
self.setWindowTitle('Example 5')#titulo de la ventana
self.frame.setLayout(self.vl)
self.setCentralWidget(self.frame)
self.CreateSetUp()
self.createMenu()#llamar a funcion menu
self.interactor.AddObserver("KeyPressEvent", self.Keypress)
def CreateSetUp(self) :
self.render_window = self.vtkWidget.GetRenderWindow()
self.interactor = self.render_window.GetInteractor()
#tamaño de las cuatro ventanas
xmin_vp = [0.0, 0.0, 0.0, 0.5]
xmax_vp = [0.5, 0.5, 0.5, 1.0]
ymin_vp = [0.0, 0.33, 0.66, 0.0]
ymax_vp = [0.33, 0.66, 1.0, 1.0]
self.renderers = []
for i in range(4) :#cuatro renders
self.r = vtk.vtkRenderer()#determinamos render
print(self.r)
self.r.SetBackground((0.8, 0.8, 0.8))#color del fondo
self.r.SetViewport(xmin_vp[i], ymin_vp[i], xmax_vp[i], ymax_vp[i])
for element in self.scene.selection[i]:
if isinstance(element, vtk.vtkActor) :
self.r.AddActor(element)
elif isinstance(element, vtk.vtkVolume) :
print("volume")
self.r.AddVolume(element)
self.renderers.append(self.r)
self.render_window.AddRenderer(self.r)
self.interactor.Initialize()
self.interactor.Start()
self.show()
def boxCallback(self,obj, event):
t = vtk.vtkTransform()
obj.GetTransform(t)
obj.GetProp3D().SetUserTransform( t )
def BoxActor(self):
self.boxWidget.SetInteractor(self.interactor)
self.boxWidget.SetProp3D(self.PickedActor)
self.boxWidget.ScalingEnabledOff()#no poder hacer ni mas grande ni mas pequeño
self.boxWidget.HandlesOff()#Eeliminar esferas
self.boxWidget.SetPlaceFactor( 1.25 ) # Make the box 1.25x larger than the actor
self.boxWidget.PlaceWidget()
self.boxWidget.On()
self.boxWidget.AddObserver("InteractionEvent", self.boxCallback)
def Keypress(self,obj, event):
key = obj.GetKeySym()
if self.picker.GetCellId()>=0:
if key=='Up':
print("arriba")
self.PickedActor.AddPosition(0,1,0)
elif key=='Down':
print("abajo")
self.PickedActor.AddPosition(0,-1,0)
elif key=='Right':
print("derecha")
self.PickedActor.AddPosition(1,0,0)
elif key=='Left':
print("izquierda")
self.PickedActor.AddPosition(-1,0,0)
elif key=='0':
print("acercar")
self.PickedActor.AddPosition(0,0,1)
elif key=='1':
print("alejar")
self.PickedActor.AddPosition(0,0,-1)
问题解决了,只需要输入
self.render_window.Render ()
每次翻译后