如何避免马雅维管道污染?

2024-09-29 23:27:19 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是一些最简单的代码,充分展示了我所说的“管道污染”。每次按“绘制”按钮时,MayaviScene编辑器(通过图上的左上角按钮访问)都会更新图,但也会创建一个新的场景“shell”,它会在管道中徘徊(如所附图像所示)。你知道吗

我担心在我更复杂的项目中,这桩事会产生不利影响。你知道吗

有人可以请你指导我如何最好地设置这个玛雅维场景,只是简单地进行更新,没有多余的积累?我已经阅读了大量的在线资料,但仍然不理解开发人员的逻辑。你知道吗

Pipeline Pollution

import sys, os
import numpy as np

from pyface.qt import QtGui, QtCore
os.environ['ETS_TOOLKIT'] = 'qt4'

from traits.api import HasTraits,Instance,on_trait_change
from traitsui.api import View,Item
from mayavi import mlab
from mayavi.core.ui.api import MayaviScene, MlabSceneModel, SceneEditor

class Mayavi_Scene(HasTraits):
    scene = Instance(MlabSceneModel, ())

    def update_scene(self):
        Mayavi_Scene.fig1 = mlab.figure(1, bgcolor=(.5,.5,.5))
        self.scene.mlab.clf(figure=Mayavi_Scene.fig1)

        splot = mlab.points3d(P1.x, P1.y, P1.z,
                              scale_factor=0.05, figure=Mayavi_Scene.fig1)

    view = View(Item('scene', editor = SceneEditor(scene_class=MayaviScene),
                    height=300, width=300, show_label=False),
                resizable=True,
                )

class P1(QtGui.QWidget):
    # data starts out empty, wait for user input (below, via 'draw()'):
    x = []
    y = []
    z = []

    def __init__(self, parent=None):
        super(P1, self).__init__(parent)
        layout = QtGui.QGridLayout(self)
        layout.setContentsMargins(20,20,20,20)
        layout.setSpacing(10)

        self.viz1 = Mayavi_Scene()
        self.ui1 = self.viz1.edit_traits(parent=self, kind='subpanel').control
        layout.addWidget(self.ui1, 0, 0, 1, 1)

        def draw(): #a sample user input, could have been a custom data file, etc.
            P1.x = np.random.random((100,))
            P1.y = np.random.random((100,))
            P1.z = np.random.random((100,))
            Mayavi_Scene().update_scene()
            #repeated presses pollute MayaviScene pipeline

        # button to draw data:
        self.btn1 = QtGui.QPushButton('Draw',self)
        self.connect(self.btn1, QtCore.SIGNAL('clicked()'), draw)
        layout.addWidget(self.btn1, 1, 0, 1, 1)
        self.btn1.show()


class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.window = P1(self) 
        self.setCentralWidget(self.window)
        self.show()

if __name__ == '__main__':
    app = QtGui.QApplication.instance()
    w = MainWindow()
    sys.exit(app.exec_())

Tags: fromimportselfnprandomsceneclassparent
1条回答
网友
1楼 · 发布于 2024-09-29 23:27:19

原因可能是在draw内部函数中包含Mayavi_Scene().update_scene()的行。每次调用draw时,它都会创建一个新的Mayavi_Scene。下面的P1类将draw定义为直接访问self.viz1的方法。我还将对draw的引用替换为对self.draw的引用

class P1(QtGui.QWidget):
    # data starts out empty, wait for user input (below, via 'draw()'):
    x = []
    y = []
    z = []

    def __init__(self, parent=None):
        super(P1, self).__init__(parent)
        layout = QtGui.QGridLayout(self)
        layout.setContentsMargins(20,20,20,20)
        layout.setSpacing(10)

        self.viz1 = Mayavi_Scene()
        self.ui1 = self.viz1.edit_traits(parent=self, kind='subpanel').control
        layout.addWidget(self.ui1, 0, 0, 1, 1)

        # button to draw data:
        self.btn1 = QtGui.QPushButton('Draw',self)
        # Connect the widget's draw method and the button
        self.connect(self.btn1, QtCore.SIGNAL('clicked()'), self.draw)
        layout.addWidget(self.btn1, 1, 0, 1, 1)
        self.btn1.show()

    def draw(self): #a sample user input, could have been a custom data file, etc.
        P1.x = np.random.random((100,))
        P1.y = np.random.random((100,))
        P1.z = np.random.random((100,))
        # Update the current scene without creating a new one.
        self.viz1.update_scene()

相关问题 更多 >

    热门问题