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. 键盘控制小蛇移动

    2024-02-19 01:48:01       13 阅读
  2. LayaBox键盘控制移动遇到的问题

    2024-02-19 01:48:01       16 阅读
  3. 串口触摸屏的键盘控制

    2024-02-19 01:48:01       6 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-19 01:48:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-19 01:48:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-19 01:48:01       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-19 01:48:01       20 阅读

热门阅读

  1. Spatial Networks for Locations

    2024-02-19 01:48:01       25 阅读
  2. EMS5730 MapReduce program

    2024-02-19 01:48:01       30 阅读
  3. 文档分割和文档词向量化的几种实现方式

    2024-02-19 01:48:01       33 阅读
  4. go-zero/grpc的rpc服务间传递额外数据

    2024-02-19 01:48:01       39 阅读
  5. linux 中date 命令的用法

    2024-02-19 01:48:01       34 阅读
  6. MySQL正则表达式

    2024-02-19 01:48:01       36 阅读
  7. vue3-使用 Vue 的多种方式

    2024-02-19 01:48:01       29 阅读
  8. 2024年2月新加坡-马来西亚游-简记

    2024-02-19 01:48:01       32 阅读
  9. 学习Android的第十二天

    2024-02-19 01:48:01       31 阅读
  10. C# 只允许开启一个exe程序

    2024-02-19 01:48:01       30 阅读