<enthought.mayavi.modules.surface.Surface object at 0x0DADE5D0>
explore(_)
- # To embed the PyQt4 widget, set the ETS_TOOLKIT variable to qt4.
- import os
- os . environ [ 'ETS_TOOLKIT' ] = 'qt4'
- from PySide import QtGui, QtCore
- from enthought. mayavi import mlab
- from enthought. tvtk . api import tvtk
- from enthought. tvtk . pyface . api import scene
- from numpy import arange, nonzero, float32, min , max , median, copy , random , shape
- from numpy. core . numeric import ravel
- from enthought. traits . api import HasTraits, Instance, on_trait_change, \
- Int, dict
- from enthought. traits . ui . api import View, Item
- from enthought. mayavi . core . ui . api import MayaviScene, MlabSceneModel, \
- Sceneeditor
- #The actual visualization
- class Visualization ( HasTraits ) :
- scene = Instance ( MlabSceneModel, ( ) )
- view = View ( Item ( 'scene' ,
- editor = SceneEditor ( scene_class = MayaviScene ) ,
- # Instead of MayaviScene, you can write just Scene
- # and then we get a clean window without a toolbar.
- height = 250 ,
- width = 300 ,
- show_label = False )
- resizable = true
- )
- needUpdate = None
- def takePlotParametres ( self , grid ) :
- self . grid = grid
- # The clf () method clears the current scene. It MUST NOT be called in the update_plot () method.
- self . scene . mlab . clf ( )
- self . needUpdate = True
- self . update_plot ( )
- @on_trait_change ( 'scene.activated' )
- def update_plot ( self ) :
- # This method is called for rendering.
- # Everything you need to draw is written here and nowhere else
- # While there is no data, draw the standard model
- if not self . needUpdate :
- self . scene . mlab . test_points3d ( )
- else :
- # Here we write all our rice paddles
- # Everything that is drawn through mlab needs to be called.
- # as self.scene.mlab. *, otherwise the Mayavi window is out of control
- # and starts to draw everything in a separate window.
- # opacity greatly influences rendering performance!
- surf = self . scene . mlab . pipeline . surface ( self . grid , opacity = 1 )
- # You can also display the internal grid by uncommenting the lines.
- #edges = mlab.pipeline.extract_edges (surf)
- #edgesSurf = mlab.pipeline.surface (edges)
- # edgesSurf.actor.property.interpolation = 'flat'
- # Remove the interpolation of the surface color of the cube. Let each cell be clearly visible.
- surf. actor . property . interpolation = 'flat'
- self . scene . mlab . orientation_axes ( )
- self . scene . background = ( 0 , 0 , 0 )
- self . scene . mlab . colorbar ( orientation = 'vertical' )
- ################################################## ##############################
- # Widget into which we embed the scene. Use it as a regular PyQt widget.
- class MayaviQWidget ( QtGui. QWidget ) :
- def __init__ ( self , parent = None ) :
- QtGui QWidget . __init__ ( self , parent )
- layout = QtGui. QVBoxLayout ( self )
- layout. setMargin ( 0 )
- layout. setSpacing ( 0 )
- self . visualization = Visualization ( )
- # Ideally, it is here that you can call the takePlotParametres method with initial data, but this is not necessary
- # self.visualization.takePlotParametres ()
- # The edit_traits method generates the window in which the scene is built.
- # This is the window you need to make Qt'shnym, for which we call the method .control
- self . ui = self . visualization . edit_traits ( parent = self ,
- kind = 'subpanel' ) . control
- layout. addWidget ( self . ui )
- self . ui . setParent ( self )
- def create_cube ( self ) :
- grid = self . createGrid ( )
- self . visualization . takePlotParametres ( grid )
- def createGrid ( self ) :
- x, y, z = ( 10 , 10 , 10 )
- # Create a grid for our cube
- grid = tvtk. RectilinearGrid ( )
- # It is important to bring data to float32, since on 64-bit OS, numpy creates float64 by default,
- # and Mayavi can't work with him.
- # Create random data
- scalars = float32 ( random . random ( ( x * y * z ) ) )
- # scalars is the value that will be stored in each cell of our cube.
- # It is for this value that the color of the cell surface is determined.
- grid. point_data . scalars = scalars
- grid. point_data . scalars . name = 'scalars'
- grid. dimensions = ( x, y, z )
- grid. x_coordinates = float32 ( arange ( x ) )
- grid. y_coordinates = float32 ( arange ( y ) )
- grid. z_coordinates = float32 ( arange ( z ) )
- return grid
- if __name__ == "__main__" :
- # It is important not to create a new application, but to use
- # which Mayavi creates automatically, otherwise you will kill all the Traits signals / slots,
- # and we just do not need it.
- # To get the link, call the instance () method.
- # Do not write out of habit QtGui.QApplication (sys.argv) .instance (),
- # I already attacked this rake.
- app = QtGui. QApplication . instance ( )
- container = QtGui. QWidget ( )
- container. setWindowTitle ( "Hello Habrahabr!" )
- layout = QtGui. QVBoxLayout ( container )
- mayavi_widget = MayaviQWidget ( )
- button = QtGui. QPushButton ( 'Create cube' )
- button. clicked . connect ( mayavi_widget. create_cube )
- layout. addWidget ( mayavi_widget )
- layout. addWidget ( button )
- container. show ( )
- app. exec_ ( )
Source: https://habr.com/ru/post/115170/
All Articles