如何导出渲染的场景使用Python paraview包一个3D VTK?

问题描述 投票:1回答:2

我已经写了代码产生使用Python VTK圆柱体物体。它产生一个三维场景在那里我可以放大或掉头,我已经取得了汽缸此代码工作正常。问题是我想在此渲染的场景导出到paraview包查看并保存供以后的作品。我怎样才能做到这一点?下面是产生一个Y形与滚筒的代码:

import vtk
import numpy as np

'''
Adding multiple Actors to one renderer scene using VTK package with python api.
Each cylinder is an Actor with three input specifications: Startpoint, Endpoint and radius.
After creating all the Actors, the preferred Actors will be added to a list and that list will be our input to the 
renderer scene.
A list or numpy array with appropriate 3*1 shape could be used to specify starting and ending points.

There are two alternative ways to apply the transform.
 1) Use vtkTransformPolyDataFilter to create a new transformed polydata.
    This method is useful if the transformed polydata is needed
      later in the pipeline
    To do this, set USER_MATRIX = True
 2) Apply the transform directly to the actor using vtkProp3D's SetUserMatrix.
    No new data is produced.
    To do this, set USER_MATRIX = False
'''
USER_MATRIX = True


def cylinder_object(startPoint, endPoint, radius, my_color="DarkRed"):
    colors = vtk.vtkNamedColors()

    # Create a cylinder.
    # Cylinder height vector is (0,1,0).
    # Cylinder center is in the middle of the cylinder
    cylinderSource = vtk.vtkCylinderSource()
    cylinderSource.SetRadius(radius)
    cylinderSource.SetResolution(50)

    # Generate a random start and end point
    # startPoint = [0] * 3
    # endPoint = [0] * 3

    rng = vtk.vtkMinimalStandardRandomSequence()
    rng.SetSeed(8775070)  # For testing.8775070

    # Compute a basis
    normalizedX = [0] * 3
    normalizedY = [0] * 3
    normalizedZ = [0] * 3

    # The X axis is a vector from start to end
    vtk.vtkMath.Subtract(endPoint, startPoint, normalizedX)
    length = vtk.vtkMath.Norm(normalizedX)
    vtk.vtkMath.Normalize(normalizedX)

    # The Z axis is an arbitrary vector cross X
    arbitrary = [0] * 3
    for i in range(0, 3):
        rng.Next()
        arbitrary[i] = rng.GetRangeValue(-10, 10)
    vtk.vtkMath.Cross(normalizedX, arbitrary, normalizedZ)
    vtk.vtkMath.Normalize(normalizedZ)

    # The Y axis is Z cross X
    vtk.vtkMath.Cross(normalizedZ, normalizedX, normalizedY)
    matrix = vtk.vtkMatrix4x4()
    # Create the direction cosine matrix
    matrix.Identity()
    for i in range(0, 3):
        matrix.SetElement(i, 0, normalizedX[i])
        matrix.SetElement(i, 1, normalizedY[i])
        matrix.SetElement(i, 2, normalizedZ[i])
    # Apply the transforms
    transform = vtk.vtkTransform()
    transform.Translate(startPoint)  # translate to starting point
    transform.Concatenate(matrix)  # apply direction cosines
    transform.RotateZ(-90.0)  # align cylinder to x axis
    transform.Scale(1.0, length, 1.0)  # scale along the height vector
    transform.Translate(0, .5, 0)  # translate to start of cylinder

    # Transform the polydata
    transformPD = vtk.vtkTransformPolyDataFilter()
    transformPD.SetTransform(transform)
    transformPD.SetInputConnection(cylinderSource.GetOutputPort())

    # Create a mapper and actor for the arrow
    mapper = vtk.vtkPolyDataMapper()
    actor = vtk.vtkActor()
    if USER_MATRIX:
        mapper.SetInputConnection(cylinderSource.GetOutputPort())
        actor.SetUserMatrix(transform.GetMatrix())
    else:
        mapper.SetInputConnection(transformPD.GetOutputPort())
    actor.SetMapper(mapper)
    actor.GetProperty().SetColor(colors.GetColor3d(my_color))
    return actor


def render_scene(my_actor_list):
    renderer = vtk.vtkRenderer()
    for arg in my_actor_list:
        renderer.AddActor(arg)
    namedColors = vtk.vtkNamedColors()
    renderer.SetBackground(namedColors.GetColor3d("SlateGray"))

    window = vtk.vtkRenderWindow()
    window.SetWindowName("Oriented Cylinder")
    window.AddRenderer(renderer)

    interactor = vtk.vtkRenderWindowInteractor()
    interactor.SetRenderWindow(window)

    # Visualize
    window.Render()
    interactor.Start()


if __name__ == '__main__':

    my_list = []
    p0 = np.array([0, 0, 0])
    p1 = np.array([0, 10, 0])
    p2 = np.array([7, 17, 0])
    p3 = np.array([-5, 15, 0])
    my_list.append(cylinder_object(p0, p1, 1, "Red"))
    my_list.append(cylinder_object(p1, p2, 0.8, "Green"))
    my_list.append(cylinder_object(p1, p3, 0.75, "Navy"))
    render_scene(my_list)

3D Y-shape in vtk with python

我在那里所有的人都在一个渲染场景渲染在一起的多个演员,我可以每个演员进入一个vtk.vtkSTLWriter?这似乎不工作!

python rendering vtk paraview
2个回答
1
投票

什么你要找的是vtkExporter class的子类,正如每个链接DOCO:

vtkExporter是一个抽象类,出口场景到一个文件中。这是非常类似于vtkWriter不同之处在于只作家写出的几何和拓扑数据的对象,其中,一个出口可以写出材料特性,照明,摄像机参数等

正如你可以从类的继承图看到有大约15类,支持导出这样的场景转换成可以在适当的读者可查看的文件。

恕我直言,你就会有最运气是vtkVRMLExporter class,因为它是一个相当普遍的格式。话虽这么说,我不相信的Paraview支持VRML文件(至少是基于一些非常古老的帖子,我发现),但我敢肯定MayaVi一样。

或者你可以,正如你所说,出口对象为STL文件,但STL文件只包含他们如何连接三角坐标和信息。此类文件不可能再描述现场信息,如相机或照明信息。另外最后我查了一个STL文件只能包含一个单一的对象,这样你的三个汽缸将结束是一个合并对象,因此它可能不是你想要的。


1
投票

我加了这些代码,并创建从我的渲染场景中的VRML文件。

exporter = vtk.vtkVRMLExporter()
exporter.SetRenderWindow(window)
exporter.SetFileName("cylinders.wrl")
exporter.Write()
exporter.Update()
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.