【问题标题】:How to use the mlab iso_surface module in a Mayavi app如何在 Mayavi 应用程序中使用 mlab iso_surface 模块
【发布时间】:2017-06-28 06:17:41
【问题描述】:

我正在尝试构建一个使用 mlab iso_surface 模块的简单 Mayavi 脚本应用程序。

但是,当我运行我的应用程序时,它会弹出两个窗口,一个显示我的 mayavi iso_surface 图,另一个显示空白的“编辑属性”窗口。 “编辑属性”窗口的指定视图布局中似乎没有显示 mayavi 场景。

所以我的问题是:为什么 mayavi iso_surface 场景没有出现在视图布局中,我如何将其放入其中?

下面粘贴了一个显示此行为的简单测试脚本。我在 Windows 10 系统上使用 Canopy 版本:2.1.1.3504(64 位)、python 3.5.2。

[注意:我已经修改了我的原始问题以包含另一个问题。如何使用 Range 对象 (mult_s) 的输入更新“s”数据?我在下面尝试过这样做,但没有成功。它抛出:TraitError: Cannot set the undefined 's' attribute of a 'ArraySource' object.]

class Isoplot1(HasTraits):
    scene = Instance(MlabSceneModel, ())
    mult_s = Range(1, 5, 1)

    @on_trait_change('scene.activated')
    def _setup(self):
        # Create x, y, z, s data
        L = 10.
        x, y, z = np.mgrid[-L:L:101j, -L:L:101j, -L:L:101j]
        self.s0 = np.sqrt(4 * x ** 2 + 2 * y ** 2 + z ** 2)        

        # create the data pipeline
        self.src1 = mlab.pipeline.scalar_field(x, y, z, self.s0)

        # Create the plot
        self.plot1 = self.scene.mlab.pipeline.iso_surface(
            self.src1, contours=[5, ], opacity=0.5, color=(1, 1, 0)
        )

    @on_trait_change('mult_s')
    def change_s(self):
        self.src1.set(s=self.s0 * self.mult_s)

    # Set the layout
    view = View(Item('scene', 
                     editor=SceneEditor(scene_class=MayaviScene),
                     height=400, width=600, show_label=False),
                HGroup('mult_s',),
                resizable=True
                )

isoplot1 = Isoplot1()
isoplot1.configure_traits()

【问题讨论】:

    标签: enthought mayavi


    【解决方案1】:

    如果您使用self.scene.mlab.pipeline.scalar_field 而不是mlab.pipeline.scalar_field,则不应发生这种情况。

    一般来说,您应该避免在初始化程序中创建任何可视化。相反,您应该始终在触发 scene.activated 事件时设置场景。为了安全地与原始 mlab 一起使用,您应该如下重写代码。

    from mayavi import mlab
    from traits.api import HasTraits, Instance, on_trait_change
    from traitsui.api import View, Item
    from mayavi.core.ui.api import MayaviScene, SceneEditor, MlabSceneModel
    import numpy as np
    
    class Isoplot1(HasTraits):
        scene = Instance(MlabSceneModel, ())
    
        @on_trait_change('scene.activated')
        def _setup(self):
            # Create x, y, z, s data
            L = 10.
            x, y, z = np.mgrid[-L:L:101j, -L:L:101j, -L:L:101j]
            s = np.sqrt(4 * x ** 2 + 2 * y ** 2 + z ** 2)
    
            # create the data pipeline
            self.src1 = mlab.pipeline.scalar_field(x, y, z, s)
    
            # Create the plot
            self.plot1 = self.scene.mlab.pipeline.iso_surface(
                self.src1, contours=[5, ], opacity=0.5, color=(1, 1, 0)
            )
    
        # Set the layout
        view = View(Item('scene', 
                         editor=SceneEditor(scene_class=MayaviScene),
                         height=400, width=600, show_label=False),
                    resizable=True
                    )
    
    isoplot1 = Isoplot1()
    isoplot1.configure_traits()
    

    您可能已经知道这一点,但以防万一您还可以查看文档中的其他一些 mayavi interactive examples。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多