0

我想使用 vtk python 库将 glb 文件嵌入到 pyqt gui。我写了一些代码,但它没有将草图嵌入到 pyqt gui。每次都有放置在第二个窗口窗体中。这是示例:

在此处输入图像描述

这是我试图将其嵌入到 gui 的源代码:

import sys
import vtk

try:
    from PyQt5.QtWidgets import QMainWindow, QAction, qApp, QApplication
    from PyQt5.QtWidgets import QWidget, QSizePolicy, QPushButton, QVBoxLayout, QFrame

    from PyQt5.QtGui import QIcon
    from PyQt5.QtCore import Qt, pyqtSignal, QTimer, QObject, QSize, QEvent
except ImportError:
    raise ImportError("Cannot load either PyQt5")

from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor    


class Menu(QMainWindow):

    def __init__(self):
        super().__init__()
        self.initUI()


    def initUI(self):               

        importer = vtk.vtkGLTFImporter()
        importer.SetFileName("uydu.glb")
        importer.Read()

        global vtk_render_window
        vtk_renderer = importer.GetRenderer()
        vtk_render_window = importer.GetRenderWindow()

        self.setGeometry(190, 300, 300, 200)
        self.setWindowTitle('Simple menu')    
        self.show()

        self.frame = QFrame()
        self.vl = QVBoxLayout()
        vtk_render_window_interactor = QVTKRenderWindowInteractor(self.frame)

        vtk_render_window_interactor.SetRenderWindow(vtk_render_window)
        colors = vtk.vtkNamedColors()
        vtk_renderer.SetBackground(colors.GetColor3d('White'))
        actor = vtk.vtkActor()
        actor.GetProperty().SetDiffuse(0.8)
        actor.GetProperty().SetDiffuseColor(colors.GetColor3d('Black'))
        actor.GetProperty().SetSpecular(0.3)
        actor.GetProperty().SetSpecularPower(60.0)

        vtk_render_window.SetSize(600, 600)
        vtk_render_window.SetWindowName('3D Visualizer')

        vtk_render_window_interactor.Initialize()

        # Add callback for getting data from Arduino
        
        #vtk_render_window_interactor.CreateRepeatingTimer(1000)
        #vtk_render_window_interactor.AddObserver("TimerEvent", information_callback)

        global vtk_actors
        vtk_actors = vtk_renderer.GetActors
        self.vl.addWidget(vtk_render_window_interactor)



        self.renderer = vtk.vtkRenderer()
        self.renderer.SetBackground(.3, .4, .5 )

        self.renwin = vtk_render_window_interactor.GetRenderWindow()
        self.renwin.AddRenderer(self.renderer)



        # An interactor
        self.inter = self.renwin.GetInteractor()


        # add the custom style
        self.style = MouseInteractorHighLightActor()
        self.style.SetDefaultRenderer(self.renderer)
        vtk_render_window_interactor.GetRenderWindow().AddRenderer(self.renderer)
        #self.iren = self.vtkWidget.GetRenderWindow().GetInteractor()
        self.inter.SetInteractorStyle(self.style)
        #self.iren.SetInteractorStyle(self.inter)
        #self.iren = self.vtkWidget.GetRenderWindow().SetInteractor(self.inter)

        self.renderer.ResetCamera()

        self.frame.setLayout(self.vl)
        self.setCentralWidget(self.frame)

        self.show()

        self.inter.Initialize()
        self.inter.Start()

if __name__ == '__main__':

    app = QApplication(sys.argv)
    ex = Menu()
    sys.exit(app.exec_()) 
4

1 回答 1

1

您必须将 vtkGLTFImporter 的 renderWindow 作为 rw 参数传递给 QVTKRenderWindowInteractor 小部件:

import sys
import vtk

try:
    from PyQt5.QtWidgets import QMainWindow, QApplication
    from PyQt5.QtWidgets import QWidget, QVBoxLayout
except ImportError:
    raise ImportError("Cannot load either PyQt5")

from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor


class Menu(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setGeometry(190, 300, 300, 200)
        self.setWindowTitle("Simple menu")

        self.container = QWidget()
        vl = QVBoxLayout(self.container)
        self.setCentralWidget(self.container)
        self.resize(640, 480)

        importer = vtk.vtkGLTFImporter()
        importer.SetFileName("uydu.glb")
        importer.Read()
        renderer = importer.GetRenderer()
        render_window = importer.GetRenderWindow()

        vtk_widget = QVTKRenderWindowInteractor(rw=render_window)
        vl.addWidget(vtk_widget)
        vtk_widget.Initialize()
        vtk_widget.Start()

        colors = vtk.vtkNamedColors()
        renderer.SetBackground(colors.GetColor3d("White"))

        renderer.ResetCamera()


if __name__ == "__main__":

    app = QApplication(sys.argv)
    ex = Menu()
    ex.show()
    sys.exit(app.exec_())
于 2021-09-16T01:30:37.903 回答