Python VTK(Notes)

本文详细介绍了如何使用Python的VTK库进行3D模型的创建与渲染,包括立方体和圆锥体的绘制过程,从数据读取、模型构建到渲染窗口的展示,以及交互控制的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(1)env:pip install vtk

(2)test:

import vtk
# 1. 读取数据
cube = vtk.vtkCubeSource()
cube.Update()#记得加这句不加看不到模型
# 2. 建图(将点拼接成立方体)
cube_mapper = vtk.vtkPolyDataMapper()
cube_mapper.SetInputData(cube.GetOutput())
# 3. 根据2创建执行单元
cube_actor = vtk.vtkActor()
cube_actor.SetMapper(cube_mapper)

cube_actor.GetProperty().SetColor(1.0, 0.0, 0.0)
# 4. 渲染(将执行单元和背景组合在一起按照某个视角绘制)
renderer = vtk.vtkRenderer()
renderer.SetBackground(0.0, 0.0, 0.0)#背景只有一个所以是Set()
renderer.AddActor(cube_actor)#因为actor有可能为多个所以是add()

# 5. 显示渲染窗口
render_window = vtk.vtkRenderWindow()
render_window.SetWindowName("My First Cube")
render_window.SetSize(400,400)
render_window.AddRenderer(renderer)# 渲染也会有可能有多个渲染把他们一起显示
# 6. 创建交互控键(可以用鼠标拖来拖去看三维模型)
interactor = vtk.vtkRenderWindowInteractor()
interactor.SetRenderWindow(render_window)
interactor.Initialize()
render_window.Render()
interactor.Start()

 (3)

import vtk

cone_a = vtk.vtkConeSource()

coneMapper = vtk.vtkPolyDataMapper()
coneMapper.SetInputConnection(cone_a.GetOutputPort())

coneActor = vtk.vtkActor()
coneActor.SetMapper(coneMapper)

ren1 = vtk.vtkRenderer()
ren1.AddActor(coneActor)
ren1.SetBackground(0.1, 0.2, 0.4)

renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren1)
renWin.SetSize(300, 300)
renWin.Render()

iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)

iren.Initialize()
iren.Start()

 I hope I can help you,If you have any questions, please  comment on this blog or send me a private message. I will reply in my free time.  

(base) rock@rock-Precision-5530:~/PyGRDECL$ python convert_grdecl_to_vtk.py [Input] Reading ECLIPSE/PETREL file "./ExampleData/dome.grdecl" .... Grid Type=CornerPoint Grid Dimension(NX,NY,NZ): (20 x 20 x 4) NumOfGrids=1600 NumOfKeywords=11 Reading Keywords [SPECGRID] [PORO] [PERMX] [PERMY] [PERMZ] .....Done! [Geometry] Converting GRDECL to Paraview Hexahedron mesh data.... NumOfPoints 12800 NumOfCells 1600 Traceback (most recent call last): File "/home/rock/PyGRDECL/convert_grdecl_to_vtk.py", line 7, in <module> Model.GRDECL2VTK() File "/home/rock/PyGRDECL/GRDECL2VTK.py", line 161, in GRDECL2VTK self.Update() File "/home/rock/PyGRDECL/GRDECL2VTK.py", line 168, in Update self.AppendScalarData2VTK(keyword,data) #VTK will automatically overwrite the data with the same keyword ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/rock/PyGRDECL/GRDECL2VTK.py", line 381, in AppendScalarData2VTK data = ns.numpy_to_vtk(numpy_array.ravel(order='F'),deep=True, array_type=vtk.VTK_FLOAT) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/rock/anaconda3/lib/python3.12/site-packages/vtkmodules/util/numpy_support.py", line 164, in numpy_to_vtk arr_dtype = get_numpy_array_type(vtk_typecode) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/rock/anaconda3/lib/python3.12/site-packages/vtkmodules/util/numpy_support.py", line 94, in get_numpy_array_type return get_vtk_to_numpy_typemap()[vtk_array_type] ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/rock/anaconda3/lib/python3.12/site-packages/vtkmodules/util/numpy_support.py", line 74, in get_vtk_to_numpy_typemap _vtk_np = {vtkConstants.VTK_BIT:numpy.bool, ^^^^^^^^^^ File "/home/rock/anaconda3/lib/python3.12/site-packages/numpy/__init__.py", line 324, in __getattr__ raise AttributeError(__former_attrs__[attr]) AttributeError: module 'numpy' has no attribute 'bool'. `np.bool` was a deprecated alias for the builtin `bool`. To avoid this error in existing code, use `bool` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.bool_` here. The aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations. Did you mean: 'bool_'?
03-27
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值