Skip to main content
 首页 » 编程设计

python之使用 mayavi 和 tvkt 进行纹理映射

2024年10月24日14mfrbuaa

我正在尝试使用 python 将普通的 jpg 图像覆盖在 Mayavi 表面上,灵感来自 this example .这是简化的代码:

from mayavi import mlab 
from tvtk.api import tvtk 
import numpy as np 
 
data=np.random.random((200,200)) #The shape of the matrix fits the image size in the original code 
img = tvtk.JPEGReader() 
img.file_name="img.jpg" 
texture=tvtk.Texture(interpolate=0)    
texture.set_input_data(bmp1.get_output()) 
 
mlab.figure(size=(300,300))   
surf = mlab.surf(data,color=(1,1,1))  
surf.actor.enable_texture = True   
surf.actor.tcoord_generator_mode = 'plane'   
surf.actor.actor.texture = texture 

但是,我得到:

ERROR: In /tmp/vtk20150328-28275-1clyhqa/VTK-6.2.0/Rendering/OpenGL/vtkOpenGLTexture.cxx, line 200 
vtkOpenGLTexture (0x7fa9c32fb590): No scalar values found for texture input! 

该错误与将纹理分配给 Actor 有关。有任何想法吗?提前致谢。

请您参考如下方法:

MayaVI-API 中有一个补丁,请参见此处: https://github.com/enthought/mayavi/issues/211

你必须更换:

texture=tvtk.Texture(interpolate=0)    
texture.set_input_data(bmp1.get_output()) 

与:

texture = tvtk.Texture(input_connection=img.output_port, interpolate=0) 

然后它应该工作。请注意,您编写的是 bmp1 而不是 img。