TraitsUI與Mayavi實例

 TraitsUI與Mayavi實例html

一:建立一個簡單的TraitsUI與Mayavi實例

# -*- coding: utf-8 -*-
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生成數據

 

(四)所有代碼

# -*- coding: utf-8 -*-
# http://www.cnblogs.com/ssyfj/p/9310931.html

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()

 

用mlab快速繪圖:

# -*- coding: utf-8 -*-

from mayavi import mlab
import numpy as np

x, y = np.ogrid[-2:2:20j, -2:2:20j]
z = x * np.exp( - x**2 - y**2)

pl = mlab.surf(x, y, z, warp_scale="auto")
mlab.axes(xlabel='x', ylabel='y', zlabel='z')
mlab.outline(pl)
mlab.show()

_images/numpy_intro_04.png

將Mayavi嵌入到界面中:

# -*- coding: utf-8 -*-
from traits.api import *
from traitsui.api import View,Item,VGroup

from tvtk.pyface.scene_editor import SceneEditor
from mayavi.tools.mlab_scene_model import MlabSceneModel
from mayavi.core.ui.mayavi_scene import MayaviScene


class DemoApp(HasTraits):
    plotbutton = Button(u"繪圖")
    scene = Instance(MlabSceneModel,())
    #創建視圖
    view = View(
        VGroup(
            Item(name='scene',
                 editor=SceneEditor(scene_class=MayaviScene),  # 設置mayavi的編輯器
                 resizable=True,
                 height=250,
                 width=400
                 ),
            'plotbutton',
            show_labels=False
        ),
        title=u"在TraitsUI中嵌入Mayavi"
    )

    def _plotbutton_fired(self):
        self.plot()

    def plot(self):
        g = self.scene.mlab.test_mesh()


app = DemoApp()
app.configure_traits()
相關文章
相關標籤/搜索