KeyPressInteractorStyle
在vtk 中有时我们需要监听 键盘或鼠标做一些事;
1. 创建 Actor;
Sphere = vtk.vtkSphereSource()
Sphere.SetRadius(10)
mapper = vtk.vtkPolyDataMappe服务器托管网r()
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.vtkInteractorSty服务器托管网leTrackballCamera):
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)
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
相关推荐: 华为机考入门python3–(12)牛客12-字符串反转
分类:字符串 知识点: 字符串是否为空 if not my_str 字符串逆序 my_str[::-1] 题目来自【牛客】 def reverse_string(s): # 判断字符串是否为空或只包含空格 if no服务器托管网t s.strip(): ret…