更新vtk中的actor

时间:2018-12-18 10:09:14

标签: python qt move actor vtk

我打算设计一个图形界面,显示三个文件,并可以传输和旋转它们。 旋转与widget框配合得很好。

问题是我想通过键盘翻译它。翻译很好,但不更新窗口,我必须放大缩小以查看更改。

我尝试更新renderactorviewport,但是它不起作用。

有人知道我该怎么解决吗? (我认为解决方案是在相机上更新,但无效)

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)

1 个答案:

答案 0 :(得分:0)

我解决了问题,只需要放

self.render_window.Render ()

每次翻译后