TraitsUI与Mayavi实例
一:创建一个简单的TraitsUI与Mayavi实例
from numpy import sqrt,sin,mgrid from traits.api import HasTraits,Instance from traitsui.api import View,Item from tvtk.pyface.scene_editor import SceneEditor from mayavi.tools.mlab_scene_model import MlabSceneModel from mayavi.core.ui.mayavi_scene import MayaviScene #1创建HasTraits继承类 class ActorViewer(HasTraits): #1.1创建场景实例 scene = Instance(MlabSceneModel,()) #建立视图 view = View( Item("scene", editor=SceneEditor(scene_class=MayaviScene), show_label=False, resizable=True, height=500, width=500, ), resizable=True ) def __init__(self,**traits): HasTraits.__init__(self,**traits) self.generate_data() def generate_data(self): #建立数据 x,y = mgrid[-2:2:100j,-2:2:100j] R = 10*sqrt(x**2 + y**2) z = sin(R)/R #绘制数据 self.scene.mlab.surf(x,y,z,colormap="cool") a = ActorViewer() a.configure_traits()
二:基于交互控制的Mayavi窗口
(一)框架步骤
(二)程序框架
(三)代码实现
(1)定义窗口变量
(2)建立视图的布局
(3)更新视图绘制
(4)定义Curve生成数据
(四)全部代码
from numpy import cos,sin,pi,arange from traits.api import HasTraits,Instance,Range,on_trait_change from traitsui.api import View,Item,Group from mayavi.core.ui.api import MayaviScene,SceneEditor,MlabSceneModel from mayavi.core.api import PipelineBase dphin = pi/300. phi = arange(0.0,2*pi+0.5*dphin,dphin,‘d‘) #建立数据 def curve(n_mer,n_long): mu = phi*n_mer x = cos(mu)*(1+cos(n_long/n_mer)*0.5) y = sin(mu)*(1+cos(n_long/n_mer)*0.5) z = 0.5*sin(n_long*mu/n_mer) t = sin(mu) return x,y,z,t class MyModel(HasTraits): n_meridional = Range(0,30,6) n_longitudinal = Range(0,30,11) #场景模型实例 scene = Instance(MlabSceneModel,()) #后面加上()是将他实例化了 #管线实例 plot = Instance(PipelineBase) def __init__(self,**traits): HasTraits.__init__(self,**traits) x, y, z, t = curve(self.n_meridional, self.n_longitudinal) if self.plot is None: # 如果plot未绘制则输出plot3d self.plot = self.scene.mlab.plot3d(x, y, z, t, tube_radius=0.025, colormap="Spectral") #当场景被激活,或者参数发生改变,更新图像 @on_trait_change([‘n_meridional‘,‘n_longitudinal‘]) #似乎监听scene.activated也可以实现在生成scene时计进入下面函数方法 def update_plot(self): x, y, z, t = curve(self.n_meridional, self.n_longitudinal) if self.plot is None: # 如果plot未绘制则输出plot3d self.plot = self.scene.mlab.plot3d(x, y, z, t, tube_radius=0.025, colormap="Spectral") else: # 如果没有数据变化,将数据更新,重新赋值 self.plot.mlab_source.set( x=x, y=y, z=z, scalars=t ) #建立视图布局 view = View( Item("scene",editor=SceneEditor(scene_class=MayaviScene), height=250,width=300,show_label=False), Group("_","n_meridional","n_longitudinal"), resizable=True ) model = MyModel() model.configure_traits()
原文地址:https://www.cnblogs.com/ssyfj/p/9310931.html
时间: 2024-11-10 21:10:20