ParaView Python 翻译(一)

ParaView Python

ParaView通过python提供了丰富的脚本支持。这种支持可作为ParaView客户端,MPI执行批处理程序,ParaView Python客户端,或者其他任一Python可执行程序的一部分。使用Python,用户和开发者可以访问ParaView可视化工程。

模块一:快速开始

入门

开展与服务器管理器的交互,不得不加载“simple”模块。只要这个必要的文件在PYTHONPATH中,这个模块可以从python的解释器中加载。这些文件是位于paraview二进制文件夹的共享库,以及paraview文件夹的python模块:如
paraview/simple.pyparaview/vtk.py等。也可以使用pvpython(用于单机或者客户端/服务器执行),pvbatch(用于非交互,分布式的批处理进程)或者从Tools|Python调用python shell用于ParaView的客户端执行Python脚本。使用的时候,不需要设置PYTHONPATH
本教程将会使用python继承开发环境(IDLE)。PYTHONPATH设置如下:

/Users/berk/work/paraview3-build/lib:/Users/berk/work/paraview3-build/lib/site-packages

可能需要设置path变量,用于寻找共享库。相应的LD_LIBRARY_PATH路径如下:

/Users/berk/work/paraview3-build/lib (/Users/berk/work/paraview3-build/bin for versions before 3.98)

当使用Mac用来在IDLE中构建树时,首先加载服务管理模块:

from paraview.simple import *

不赞成直接导入paraview模块,虽然仍然可以向后兼容,本文档仅仅引用简单的独立模块。

在此案列中,将会在独立的模块中使用ParaView。在此文档的后面,连接运行在集群上的ParaView服务器。

Tab-completion

ParaView Qt客户端中的Python shell提供自动化完成功能。也可以使用IDLE完成自动完成功能。为了在pvpython中使用自动完成功能,可以使用TabCompletion中提供的小提示。
总之,需要创建一个PYTHONSTARTUP 变量,(在bash中)如下:

export PYTHONSTARTUP = /home/<username>/.pythonrc

其中.pythonrc如下:

# ~/.pythonrc
# enable syntax completion
try:
    import readline
except ImportError:
    print "Module readline not available."
else:
    import rlcompleter
    readline.parse_and_bind("tab: complete")

就是这样,Tab completion的作用跟其他Shell一样。

创建一个Pipeline

简单模块包含许多实例化源,过滤器和其他相关对象的函数,可以从ParaView的在线帮助中,获取创建当前模块的对象列表。
首先,创建一个Cone(圆锥体)对象

cone = Cone()

使用help(),获取一些关于cone对象的文档

help(cone)

paraview.servermanager对象模块,Cone的帮助文档如下:

class Cone(SourceProxy)
 |  The Cone source can be used to add a polygonal cone to the 3D scene. The output of the
 Cone source is polygonal data.
 |
 |  Method resolution order:
 |      Cone
 |      SourceProxy
 |      Proxy
 |      __builtin__.object
 |
 |  Methods defined here:
 |
 |  Initialize = aInitialize(self, connection=None)
 |
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |
 |  Capping
 |      If this property is set to 1, the base of the cone will be capped with a filled polygon.
 Otherwise, the base of the cone will be open.
 |
 |  Center
 |      This property specifies the center of the cone.
 |
 |  Direction
 |      Set the orientation vector of the cone.  The vector does not have to be normalized.  The cone
 will point in the direction specified.
 |
 |  Height
 |      This property specifies the height of the cone.
 |
 |  Radius
 |      This property specifies the radius of the base of the cone.
 |
 |  Resolution
 |      This property indicates the number of divisions around the cone. The higher this number, the
 closer the polygonal approximation will come to representing a cone, and the more polygons it will
 contain.
 |
 | ...

将为你提供完整的属性列表,检查分辨率属性的设置如下:

>>>cone.Resolution
6

你可以按照下面的方式增加分辨率

>>>cone.Resolution=32

或者,当创建对象时,指定分辨率的值

>>>cone=Cone(Resolution=32)

在构造时,使用关键参数为任意数量的属相分配值。也可以改变中心点

>>>cone.Center
[0.0, 0.0, 0.0]
>>>cone.Center = [1,2,3]

像这样的矢量属性,支持设置,检索单个元素,以及片段(元素的范围)

>>>cone.Center[0:2] = [2,4]
>>>cone.Center
[2.0, 4.0, 3.0]

接下来,在cone上运用收缩过滤器

>>>shrinkFilter = Shrink(cone)
>>>shrinkFilter.Input
<paraview.servermanager.Cone object at 0xaf701f0>

此时,如果你想获取一些关于收缩过滤器输出的一些信息,你可以强制它更新(这将也会导致cone源的执行)。有关ParaView使用VTK的需求驱动pipeline模型的详细信息,可以看VTK的书

>>>shrinkFilter.UpdatePipeline()
>>>shrinkFilter.GetDataInformation().GetNumberOfCells()
33L
>>>shrinkFilter.GetDataInformation().GetNumberOfPoints()
128L

接下来,我们将会介绍更多的DataInformation类的细节。

###渲染
现在,已经创建一个小的pipeline,渲染结果。需要两个对象用来渲染场景中算法的输出:一个表示对象,一个视图对象,表示对象用来负责获取数据对象,视图对象,用来渲染。一个视图对象负责管理一个渲染上下文,以及表示对象的集合。默认情况下,简单的创建一个视图对象,表示对象随着Show()方法自动创建。

>>>Show(shrinkFilter)
>>>Render()

在此示例中,通过Cone()和Shrink()返回的值,分配给Python变量,并用于构建Pipeline。ParaView保持跟踪最后一个由用户创建的pipeline对象。允许使用如下代码,完成上述的所有事情。

>>> from paraview.simple import *
# Create a cone and assign it as the active object
>>> Cone()
<paraview.servermanager.Cone object at 0x2910f0>
# Set a property of the active object
>>> SetProperties(Resolution=32)
# Apply the shrink filter to the active object
# Shrink is now active
>>> Shrink()
<paraview.servermanager.Shrink object at 0xaf64050>
# Show shrink
>>> Show()
<paraview.servermanager.UnstructuredGridRepresentation object at 0xaf57f90>
# Render the active view
>>> Render()
<paraview.servermanager.RenderView object at 0xaf57ff0>
以上是对praview.simple 模块的快速介绍,在接下来的章节里,将会更详细的讨论Python接口,以及介绍更高级的概念。
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值