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)
相关推荐
走好每一步2 天前
5、VTK刚体变换
qt·vtk·三维图像
走好每一步3 天前
4、VDK读取xml模型数据
vtk·三维图像
走好每一步5 天前
2、VDK 使用QVTKOpenGLNativeWidget嵌入到QT窗体中
qt·vtk·三维图像
走好每一步8 天前
1、VTK+QT + cmake编程 三维圆柱体
qt·vtk
走好每一步8 天前
0、VDK编译 CMake + VS2017 x64
qt·vtk·三维图像
懷淰メ8 天前
【AI赋能】基于PyQt+YOLO+DeepSeek的淋巴细胞检测系统(详细介绍)
yolo·计算机视觉·pyqt·课程设计·医疗·淋巴细胞·淋巴
懷淰メ9 天前
【AI加持】基于PyQt+YOLO+DeepSeek的结直肠息肉检测系统(详细介绍)
yolo·目标检测·计算机视觉·pyqt·ai加持·直肠息肉·结直肠
懷淰メ9 天前
【AI加持】基于PyQt+YOLO+DeepSeek的疟原虫检测系统(详细介绍)
人工智能·yolo·计算机视觉·pyqt·医疗·ai分析·疟原虫
酬勤-人间道10 天前
VTK 与 Cesium-native 结合实践:小场景三维编辑 + 数字地球精准贴合
c++·qt·vtk·遥感·岩土·cesium-native
石工记12 天前
CTO如何落地AI?从0到1的实战路径
人工智能·python·django·flask·numpy·pandas·pyqt