簡體   English   中英

如何使用python將3D vtk渲染的場景導出到paraview?

[英]How to export a 3D vtk rendered scene to paraview using python?

我已經編寫了一個代碼在python中使用vtk生成圓柱對象。 此代碼在產生3D場景的地方效果很好,在這里我可以縮放或旋轉已制成的圓柱體。 問題是我想將此渲染的場景導出到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)

vtk中帶有Python的3D Y形

我有多個演員,所有演員都在一個渲染場景中一起渲染,我可以將每個演員傳遞到vtk.vtkSTLWriter嗎? 這似乎不起作用!

您正在尋找的是vtkExporter類的 ,根據鏈接的doco:

vtkExporter是將場景導出到文件的抽象類。 它與vtkWriter非常相似,不同之處在於寫入器只寫出對象的幾何和拓撲數據,而輸出者可以在其中寫出材料特性,照明,相機參數等。

從類的繼承圖中可以看到,大約有15個類支持將此類場景導出到文件中,並可以在適當的閱讀器中查看。

恕我直言,最讓您幸運的是vtkVRMLExporter類,因為它是一種相當常見的格式。 話雖這么說,但我不相信Paraview支持VRML文件(至少基於我發現的一些非常古老的帖子),但是我確定MayaVi可以。

或者,您可以如上所述將對象導出到STL文件中,但是STL文件僅包含三角形坐標和有關其連接方式的信息。 這樣的文件可能無法描述場景信息,例如相機或照明信息。 最后我檢查了一個STL文件只能包含一個對象,因此您的三個柱面最終將成為一個合並對象,因此可能不是您想要的。

我添加了這些代碼,它從渲染的場景創建了一個VRML文件。

exporter = vtk.vtkVRMLExporter()
exporter.SetRenderWindow(window)
exporter.SetFileName("cylinders.wrl")
exporter.Write()
exporter.Update()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM