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)
相关推荐
深蓝海拓21 小时前
PySide6从0开始学习的笔记(二十三)使用QRunnable在线程池中执行临时任务
笔记·python·qt·学习·pyqt
强化试剂1 天前
荧光标记利器 Alkyne-PEG-FITC;FITC-PEG-Alkyne:核心优势与行业价值
python·flask·pyqt·scipy
明洞日记3 天前
【VTK手册034】 vtkGeometryFilter 深度解析:高性能几何提取与转换专家
c++·图像处理·算法·ai·vtk·图形渲染
BoBoZz193 天前
BillboardTextActor3D 3D字体随镜头旋转
python·vtk·图形渲染·图形处理
BoBoZz193 天前
Tutorial_Step6 vtkBoxWidget的交互与控制
python·vtk·图形渲染·图形处理
BoBoZz193 天前
AnatomicalOrientation 3D人体模型及三个人体标准解剖学平面展示
python·vtk·图形渲染·图形处理
BoBoZz194 天前
TextureCutQuadric 利用3D隐式函数(Quadrics)来生成2D纹理坐标
python·vtk·图形渲染·图形处理
深蓝海拓5 天前
PySide6从0开始学习的笔记(二十二) 几种封装信号传递内容的方法
笔记·python·qt·学习·pyqt
赤鸢QAQ5 天前
PySide6批量创建控件
python·qt·pyqt
Dave.B5 天前
VTK核心数据结构:vtkCellLinks 点-单元拓扑关系管理详解
vtk