python程序中的错误,可能与vtk相关。请帮忙?

时间:2014-10-14 20:04:32

标签: python vtk

我是python的新手,我正在运行这个程序,它给了我以下错误:

ERROR: In /builddir/build/BUILD/VTK6.0.0/Common/ExecutionModel/vtkDemandDrivenPipeline.cxx, line 715
vtkCompositeDataPipeline (0x2987f40): Input port 0 of algorithm vtkXMLStructuredGridWriter(0x2985d30) has 0 connections but is not optional.

这是完整的追溯

Traceback (most recent call last):
  File "bvts.py", line 65, in <module>
    writer.SetInput(grid)
AttributeError: SetInput

有人可以帮我理解为什么会这样吗?

这是程序。在此先感谢:)

import vtk
import math

nx = 4
ny = 4
nz = 40

def ijk_to_xyz(i,j,k):
  z = 2*3.14159*k/(nz - 1.0)
  x = math.sin(z) + (-1.0 + 2.0*i/(nx - 1.0))
  y = -1.0 + 2.0*j/(ny - 1.0)
  return (x,y,z)

def ijk_to_index(i,j,k):
  return i*(ny*nz) + j*nz + k

grid = vtk.vtkStructuredGrid()
grid.SetDimensions(40,4,4)

pts = vtk.vtkPoints()
pts.SetNumberOfPoints(nx*ny*nz)

p = 0
for i in range(nx):
  for j in range(ny):
     for k in range(nz):
    x,y,z = ijk_to_xyz(i,j,k)
    pts.InsertPoint(p, x, y, z)
        p = p + 1

grid.SetPoints(pts)

cdata = vtk.vtkFloatArray()
cdata.SetNumberOfComponents(1)
cdata.SetNumberOfTuples((nx-1)*(ny-1)*(nz-1))
cdata.SetName('cellData')

p = 0
for i in range(nx-1):
  for j in range(ny-1):
    for k in range(nz-1):
      cdata.InsertValue(p, i+j+k)
      p = p + 1

grid.GetCellData().SetScalars(cdata)

pdata = vtk.vtkFloatArray()
pdata.SetNumberOfComponents(1)
pdata.SetNumberOfTuples(nx*ny*nz)
pdata.SetName('pointData')

p = 0
for i in range(nx):
  for j in range(ny):
    for k in range(nz):
      pdata.InsertValue(p, i+j+k)
      p = p + 1

grid.GetPointData().SetScalars(pdata)

writer = vtk.vtkXMLStructuredGridWriter()
writer.SetFileName('grid.vts')
writer.SetInput(grid)
writer.Update()

1 个答案:

答案 0 :(得分:1)

在VTK6上替换SetInput方法,请参阅http://www.vtk.org/Wiki/VTK/VTK_6_Migration/Replacement_of_SetInput

你应该尝试这个而不是SetInput:

writer->SetInputConnection(grid->GetProducerPort());