安装vtk
pip install vtk
1、圆柱体
import vtk
cylinder = vtk.vtkCylinderSource()
cylinder.SetHeight(3.0)
cylinder.SetRadius(1.0)
cylinder.SetResolution(360)
print("高、半径、面:",cylinder.GetHeight(),cylinder.GetRadius(),cylinder.GetResolution())
cylinderMapper = vtk.vtkPolyDataMapper()
cylinderMapper.SetInputConnection(cylinder.GetOutputPort())
cylinderActor = vtk.vtkActor()
cylinderActor.SetMapper(cylinderMapper)
renderer = vtk.vtkRenderer()
renderer.AddActor(cylinderActor)
renderer.SetBackground(0.1,0.2,0.4)
print("Renderer bg:",renderer.GetBackground())
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(renderer)
renWin.SetSize(1200,1200)
print("Window size:",renWin.GetSize())
renWin.Render()
i_ren = vtk.vtkRenderWindowInteractor()
i_ren.SetRenderWindow(renWin)
i_ren.Initialize()
i_ren.Start()
2、VTK 星空柱体
import vtk
cylinder = vtk.vtkCylinderSource()
cylinder.SetHeight(3.0)
cylinder.SetRadius(1.0)
cylinder.SetResolution(360)
print(f"高:{cylinder.GetHeight()}、半径:{cylinder.GetRadius()}、面:{cylinder.GetResolution()}")
cylinderMapper = vtk.vtkPolyDataMapper()
cylinderMapper.SetInputConnection(cylinder.GetOutputPort())
"""vtkProp子类负责确定渲染场景中对象的位置、大小和方向信息。
Prop依赖于两个对象(Prop一词来源于戏剧里的“道具”,在VTK里表示的是渲染场景中可以看得到的对象。)
一个是Mapper(vtkMapper)对象,负责存放数据和渲染信息,另一个是属性(vtkProperty)对象,负责控制颜色、不透明度等参数。
"""
cylinderActor = vtk.vtkActor()
print(f"位置:{cylinderActor.GetPosition()}")
cylinderProperty = vtk.vtkProperty()
cylinderProperty.SetColor(0.6,0.96,1)
print("颜色:",cylinderProperty.GetColor())
bmpReader = vtk.vtkBMPReader()
bmpReader.SetFileName("sky.bmp")
texture = vtk.vtkTexture()
texture.SetInputConnection(bmpReader.GetOutputPort())
texture.InterpolateOn()
cylinderActor.SetMapper(cylinderMapper)
cylinderActor.SetProperty(cylinderProperty)
cylinderActor.SetTexture(texture)
renderer = vtk.vtkRenderer()
renderer.AddActor(cylinderActor)
renderer.SetBackground(1.0,0.87,0.68)
print("Renderer bg:",renderer.GetBackground())
renderer.SetBackground2(1.0,1.0,1.0)
renderer.SetGradientBackground(1)
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(renderer)
renWin.SetSize(1200,1200)
print("Window size:",renWin.GetSize())
"""vtkRenderWindowInteractor自动建立一个默认的3D场景交互器样式(Interactor Style):vtkInteractorStyleSwitch,
当然你也可以选择其他的交互器样式,或者是创建自己的交互器样式"""
i_ren = vtk.vtkRenderWindowInteractor()
i_ren.SetRenderWindow(renWin)
i_ren.Initialize()
i_ren.Start()
sky.bmp
参考:Python VTK 代码详解(一)
3、正方体
import vtk
cube = vtk.vtkCubeSource()
cube.Update()
cube_mapper = vtk.vtkPolyDataMapper()
cube_mapper.SetInputData(cube.GetOutput())
cube_actor = vtk.vtkActor()
cube_actor.SetMapper(cube_mapper)
cube_actor.GetProperty().SetColor(1.0, 0.0, 0.0)
renderer = vtk.vtkRenderer()
renderer.SetBackground(0.0, 0.0, 0.0)
renderer.AddActor(cube_actor)
render_window = vtk.vtkRenderWindow()
render_window.SetWindowName("My First Cube")
render_window.SetSize(400,400)
render_window.AddRenderer(renderer)
interactor = vtk.vtkRenderWindowInteractor()
interactor.SetRenderWindow(render_window)
interactor.Initialize()
render_window.Render()
interactor.Start()
参考:Python使用VTK三维3D图形绘制入门教程
|