python pyqt5 +vtk 显示obj模型文件
准备
名称 版本 python 3.8.19 pyqt5 5.15.9 pyqt5-tools 5.15.9.3.3 pyqt5-sip 12.15.0 vtk 9.3.1
代码
使用wsl2 和 XLaunch 配合pyqt5
进行可视化是可行的。 使用pip
在conda环境中安装pyqt5
相关组件。 以下代码在 WSL2 的 Ubuntu 20.04 环境下,使用 PyQt5
和 vtk
展示 obj
模型。
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
import vtk
from vtk. qt. QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
class Ui_MainWindow ( object ) :
def setupUi ( self, MainWindow) :
MainWindow. setObjectName( "MainWindow" )
MainWindow. resize( 250 , 380 )
self. centralwidget = QtWidgets. QWidget( MainWindow)
self. centralwidget. setObjectName( "centralwidget" )
self. label3DView = QtWidgets. QLabel( self. centralwidget)
self. label3DView. setGeometry( QtCore. QRect( 20 , 20 , 200 , 20 ) )
self. label3DView. setObjectName( "label3DView" )
self. ThreeDWidget = QVTKRenderWindowInteractor( self. centralwidget)
self. ThreeDWidget. setGeometry( QtCore. QRect( 20 , 40 , 200 , 290 ) )
self. ThreeDWidget. setObjectName( "ThreeDWidget" )
MainWindow. setCentralWidget( self. centralwidget)
self. retranslateUi( MainWindow)
QtCore. QMetaObject. connectSlotsByName( MainWindow)
self. vtk_renderer = vtk. vtkRenderer( )
self. ThreeDWidget. GetRenderWindow( ) . AddRenderer( self. vtk_renderer)
self. iren = self. ThreeDWidget. GetRenderWindow( ) . GetInteractor( )
self. load_obj_model( "Shirt.obj" )
def retranslateUi ( self, MainWindow) :
_translate = QtCore. QCoreApplication. translate
MainWindow. setWindowTitle( _translate( "MainWindow" , "TestWindow" ) )
self. label3DView. setText( _translate( "MainWindow" , "3D View" ) )
def load_obj_model ( self, filename) :
reader = vtk. vtkOBJReader( )
reader. SetFileName( filename)
mapper = vtk. vtkPolyDataMapper( )
mapper. SetInputConnection( reader. GetOutputPort( ) )
actor = vtk. vtkActor( )
actor. SetMapper( mapper)
self. vtk_renderer. AddActor( actor)
self. vtk_renderer. ResetCamera( )
self. ThreeDWidget. GetRenderWindow( ) . Render( )
self. iren. Initialize( )
if __name__ == "__main__" :
app = QtWidgets. QApplication( sys. argv)
MainWindow = QtWidgets. QMainWindow( )
ui = Ui_MainWindow( )
ui. setupUi( MainWindow)
MainWindow. show( )
sys. exit( app. exec_( ) )
参考
PyQT5+VTK8加载3D模型(obj、stl、3ds)汇总,以及vtk双相机、获取vtk二维坐标点