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)
相关推荐
懷淰メ8 小时前
【AI加持】基于PyQt+YOLO+DeepSeek的钢材焊接缺陷检测系统(详细介绍)
yolo·目标检测·计算机视觉·pyqt·缺陷检测·deepseek·钢材缺陷
vegetablesssss11 小时前
VTK切割图
c++·qt·vtk
爱码小白2 天前
排除LhPyQt5疑难bug的经验
python·pyqt
vegetablesssss3 天前
vtkCamera
qt·vtk
懷淰メ3 天前
【AI加持】基于PyQt+YOLO+DeepSeek的安全帽检测系统(详细介绍)
yolo·目标检测·计算机视觉·pyqt·安全帽检测·deepseek·安全帽
懷淰メ4 天前
【AI加持】基于PyQt+YOLO+DeepSeek的PCB缺陷检测系统(详细介绍)
yolo·计算机视觉·pyqt·缺陷检测·pcb·检测系统·pcb缺陷
懷淰メ4 天前
【AI加持】基于PyQt+YOLO+DeepSeek的布匹缺陷检测系统(详细介绍)
yolo·目标检测·计算机视觉·pyqt·缺陷检测·布匹·布匹缺陷
深蓝海拓5 天前
基于QtPy (PySide6) 的PLC-HMI工程项目(十二)最后的工作
网络·笔记·python·学习·pyqt·plc
TOOLS指南6 天前
Python-PyQt界面开发入门-计算器例子
pyqt
小灰灰搞电子6 天前
PyQt QDarkStyle详解-打造专业暗黑界面的瑞士军刀
pyqt·暗黑主题