如何修复“TraitError:...实例的‘输入’特征是‘只读’。”

han*_*ine 5 python macports enthought mayavi

与 vtk 的原始 Python API 相比,我一直更喜欢 Python 化的 tvtk,但使用从 MacPorts 获得的最新版本时,我遇到了基本功能不再起作用的问题。以下片段摘自tvtv 文档

from tvtk.api import tvtk
cs = tvtk.ConeSource()
cs.resolution = 36
m = tvtk.PolyDataMapper()
m.input = cs.output # <== fails here
a = tvtk.Actor()
a.mapper = m
p = a.property
p.representation = 'w'
print p.representation
Run Code Online (Sandbox Code Playgroud)

每次初始化“输入”特征时,我都会收到类似的错误

TraitError: The 'input' trait of a PolyDataMapper instance is 'read only'.
Run Code Online (Sandbox Code Playgroud)

我发现了许多类似的问题、错误报告等,但它们都指向与 VTK 6 (SetInputDataSetInputConnection不是SetInput)相关的更改,这些更改应该在 Mayavi 4.4.2 中得到支持,并且我有:

vtk @6.3.0_0+python27 (active)
py27-traits @4.5.0_0 (active)
py27-traitsui @5.0.0_0 (active)
py27-apptools @4.3.0_0 (active)
py27-envisage @4.4.0_0 (active)
py27-pyface @5.0.0_0+pyqt4 (active)
py27-mayavi @4.4.3_0 (active)
Run Code Online (Sandbox Code Playgroud)

具有PolyDataMapper以下输入特征:

'input': <traits.traits.CTrait at 0x11b23a260>,
'input_algorithm': <traits.traits.CTrait at 0x119516520>,
'input_as_data_set': <traits.traits.CTrait at 0x11b230470>,
'input_connection': <traits.traits.CTrait at 0x119516310>,
'input_executive': <traits.traits.CTrait at 0x1195165d0>,
'input_information': <traits.traits.CTrait at 0x119516680>,
Run Code Online (Sandbox Code Playgroud)

3d-*_*nes 5

Mayavi 支持 VTK 5.10 和 VTK 6.x,它们内部有不同的 API 来配置管道。这tvtk软件包具有支持两个版本的通用 API,以实现可移植性。

改变:

m.input = cs.output # <== fails here
Run Code Online (Sandbox Code Playgroud)

到:

from tvtk.common import configure_input
tvtk.configure_input(m, cs) # <== will work
Run Code Online (Sandbox Code Playgroud)

参考: https: //github.com/enthought/mayavi/blob/master/tvtk/common.py#L79

  • 间接地,上面包含了我一直在寻找的答案,即“正确的”新 API。我对 VTK 5 支持不感兴趣,但我没有看到代码需要如何调整。从链接来看,似乎没有非连接情况下的特征,但“set_input_data(some_data)”是我所缺少的。 (2认同)