首先,需先安装好python2.7.0
1、在vtk官网下载vtkpython-windows-64bit并安装到本地文件夹
(看清对应的python版本是2/3,vtkpython-7.1.1对应python2)
2、将系统变量Path中配置:
python2.7.0路径D:\software\python2.7.0
vtkpython路径:D:\software\vtkpython\VTK 7.1.1\bin
3、新建PYTHONPATH变量,并赋值:
D:\software\vtkpython\VTK 7.1.1\bin\Lib\site-packages
4、在VScode中下载扩展插件python2.7
done!
注意:python版本不同会配置出错,是python2千万不要安装配置python3,保证电脑上配置的python版本、vtk-python适用版本以及VScode中扩展插件版本三个一致
测试代码:
import vtk
colors = vtk.vtkNamedColors()
# create a rendering window and renderer
ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
# create a renderwindowinteractor
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
# create source
src = vtk.vtkPointSource()
src.SetCenter(0, 0, 0)
src.SetNumberOfPoints(50)
src.SetRadius(5)
src.Update()
# mapper
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(src.GetOutputPort())
# actor
actor = vtk.vtkActor()
actor.SetMapper(mapper)
actor.GetProperty().SetColor(colors.GetColor3d("Tomato"))
actor.GetProperty().SetPointSize(4)
# assign actor to the renderer
ren.AddActor(actor)
ren.SetBackground(colors.GetColor3d("DarkGreen"))
# enable user interface interactor
iren.Initialize()
renWin.Render()
iren.Start()
运行结果: