0

i would like to embed the glb file to pyqt gui with vtk python library. I wrote pieces of code but it does not embed the sketch to the pyqt gui. Everytime there have been placing in the second windows form. Here is the example:

enter image description here

And here is the my source code that i am trying to embed it to 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_()) 

1 Answer 1

1

You have to pass the renderWindow of vtkGLTFImporter as rw parameter to the QVTKRenderWindowInteractor widget:

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_())

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.