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)
相关推荐
秀木易风1 天前
VTK随笔十四:QT与VTK的交互示例(平移)
c++·qt·vtk·交互
无所谓จุ๊บ3 天前
VTK知识学习(51)- 交互与Widget(二)
学习·vtk
越甲八千5 天前
pyqt SQL Server 数据库查询-优化2
数据库·windows·pyqt
豆芽8195 天前
决策树(DecisionTree)
python·决策树·机器学习·pyqt·sklearn
utmhikari6 天前
【日常随笔】万字长文,如何用pyside6开发一个python桌面工具
前端·python·pyqt
无所谓จุ๊บ7 天前
VTK知识学习(50)- 交互与Widget(一)
学习·vtk
zoney hu8 天前
PyQt学习记录
pyqt
不爱吃鱼的猫-10 天前
Pyside6 开发 使用Qt Designer
python·pyqt·pyside6
不爱吃鱼的猫-10 天前
PySide6控件:QFont设置、QColor调色板、QPixmap图像处理与QCursor光标自定义
python·pyqt·个人开发·pyside6
zew104099458811 天前
基于深度学习的手势识别系统设计
人工智能·深度学习·算法·数据集·pyqt·yolov5·训练模型