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)
相关推荐
懷淰メ14 小时前
python3GUI--【AI加持】基于PyQt5+YOLOv8+DeepSeek的智能球体检测系统:(详细介绍)
yolo·目标检测·计算机视觉·pyqt·检测系统·deepseek·球体检测
BoBoZz193 天前
TriangleStrip连续三角带
python·vtk·图形渲染·图形处理
BoBoZz197 天前
CellTypeSource
python·vtk·图形渲染·图形处理
mortimer8 天前
【实战复盘】 PySide6 + PyTorch 偶发性“假死”?由多线程转多进程
pytorch·python·pyqt
S***y3969 天前
算法挑战算法的广泛应用,它们也带来了一系列挑战,这些挑战不仅关乎技术层面,更涉及伦理、法律和社会等多个维度。
kafka·pyqt·宽度优先
AI视觉网奇10 天前
pyqt 横竖屏切换
pyqt
mortimer14 天前
从零打造一款桌面实时语音转文字工具:PySide6 与 Sherpa-Onnx 的实践
python·github·pyqt
开心-开心急了15 天前
PySide6/PyQt Ctrl 滚轮 实现文本缩放功能
pyqt·pyside
Dave.B17 天前
【VTK核心过滤器详解】:vtkCleanPolyData 多边形数据清洗实战指南
算法·vtk
B站_计算机毕业设计之家18 天前
深度学习:Yolo水果检测识别系统 深度学习算法 pyqt界面 训练集测试集 深度学习 数据库 大数据 (建议收藏)✅
数据库·人工智能·python·深度学习·算法·yolo·pyqt