VTK Python PyQt 监听键盘 控制 Actor 移动 变色

KeyPressInteractorStyle

在vtk 中有时我们需要监听 键盘或鼠标做一些事;

1. 创建 Actor;

复制代码
Sphere = vtk.vtkSphereSource()
Sphere.SetRadius(10)

mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(Sphere.GetOutputPort())
actor = vtk.vtkActor()
actor.SetMapper(mapper)
actor.GetProperty().SetColor(0.0, 1.0, 0.0)

2.创建 vtkRenderer vtkRenderWindow vtkRenderWindowInteractor

复制代码
ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)

3.重写交互器 Style

复制代码
class KeyPressInteractorStyle(vtk.vtkInteractorStyleTrackballCamera):

    def __init__(self, parent=None):
        self.parent = vtk.vtkRenderWindowInteractor()
        if (parent is not None):
            self.parent = parent

        self.AddObserver("KeyReleaseEvent", self.keyRelease)

    def keyRelease(self, obj, event):
        key = self.parent.GetKeySym()
        if key == 'Up':
           pt =  actor.GetPosition()
           actor.SetPosition(pt[0],pt[1]+5,pt[2])
        elif key == 'Down':
           pt =  actor.GetPosition()
           actor.SetPosition(pt[0],pt[1]-5,pt[2])
        if key == 'Left':
           pt =  actor.GetPosition()
           actor.SetPosition(pt[0]-5,pt[1],pt[2])
        elif key == 'Right':
           pt =  actor.GetPosition()
           actor.SetPosition(pt[0]+5,pt[1],pt[2])

        elif key== 'c':
            # 产生随机颜色
            r = vtk.vtkMath.Random()
            g = vtk.vtkMath.Random()
            b = vtk.vtkMath.Random()
            actor.GetProperty().SetColor(r, g, b)
        renWin.Render()
复制代码
 4.添加交互器:
复制代码
iren.SetInteractorStyle(KeyPressInteractorStyle(parent=iren))

ren.AddActor(actor)
相关推荐
开心-开心急了18 小时前
PySide6 使用搜索引擎搜索 多类实现 更新1次
python·pyqt·pyside
herbal_medicine1 天前
【测试】123456789
pyqt
懷淰メ2 天前
python3GUI--模仿百度网盘的本地文件管理器 By:PyQt5(详细分享)
开发语言·python·pyqt·文件管理·百度云·百度网盘·ui设计
失心疯_20234 天前
PyQt开发_小学学习辅助工具
学习·pyqt
Dave.B7 天前
vtkTubeFilter:让2D线条变3D管子,搞定流场可视化与3D建模线条加粗
3d·vtk
Dave.B7 天前
【VTK实战】vtkDepthImageToPointCloud:从2D深度图到3D点云,手把手教你落地3D扫描/AR场景
算法·计算机视觉·3d·ar·vtk
宝贝儿好7 天前
【python】第五章:python-GUI编程
python·pyqt
Goona_8 天前
PyQt批量年龄计算工具:从身份证到指定日期的周岁处理
python·小程序·交互·pyqt
AI视觉网奇10 天前
pyqt 触摸屏监听
开发语言·python·pyqt
感谢地心引力13 天前
【Python】基于 PyQt6 和 Conda 的 PyInstaller 打包工具
数据库·python·conda·pyqt·pyinstaller