def salt(engine): cell_to_point_data = CellToPointData() engine.add_filter(cell_to_point_data) iso_surface = IsoSurface() engine.add_filter(iso_surface) module_manager = cell_to_point_data.children[0] module_manager.scalar_lut_manager.lut_mode = 'GnBu' iso_surface.contour.contours = [20, 25.0] iso_surface.actor.actor.position = np.array([0., 0., 1000.]) iso_surface.actor.actor.scale = np.array([1., 1., 100.])
def contour(): """The script itself. We needn't have defined a function but having a function makes this more reusable. """ # 'mayavi' is always defined on the interpreter. # Create a new scene. mayavi.new_scene() # Read a VTK (old style) data file. r = VTKFileReader() filename = join(mayavi2.get_data_dir(dirname(abspath(__file__))), 'heart.vtk') r.initialize(filename) mayavi.add_source(r) # Create an outline for the data. o = Outline() mayavi.add_module(o) # Create three simple grid plane modules. # First normal to 'x' axis. gp = GridPlane() mayavi.add_module(gp) # Second normal to 'y' axis. gp = GridPlane() mayavi.add_module(gp) gp.grid_plane.axis = 'y' # Third normal to 'z' axis. gp = GridPlane() mayavi.add_module(gp) gp.grid_plane.axis = 'z' # Create one ContourGridPlane normal to the 'x' axis. cgp = ContourGridPlane() mayavi.add_module(cgp) # Set the position to the middle of the data. cgp.grid_plane.position = 15 # Another with filled contours normal to 'y' axis. cgp = ContourGridPlane() mayavi.add_module(cgp) # Set the axis and position to the middle of the data. cgp.grid_plane.axis = 'y' cgp.grid_plane.position = 15 cgp.contour.filled_contours = True # An isosurface module. iso = IsoSurface(compute_normals=True) mayavi.add_module(iso) iso.contour.contours = [220.0] # An interactive scalar cut plane. cp = ScalarCutPlane() mayavi.add_module(cp) cp.implicit_plane.normal = 0, 0, 1
def setUp(self): """Initial setting up of test fixture, automatically called by TestCase before any other test method is invoked""" e = NullEngine() # Uncomment to see visualization for debugging etc. #e = Engine() e.start() e.new_scene() self.e = e sgrid = datasets.generateStructuredGrid() src = VTKDataSource(data=sgrid) e.add_source(src) # Create an outline for the data. o = Outline() e.add_module(o) # Create one ContourGridPlane normal to the 'x' axis. cgp1 = ContourGridPlane() e.add_module(cgp1) # Set the position to the middle of the data. cgp1.grid_plane.position = 15 # Another with filled contours normal to 'y' axis. cgp2 = ContourGridPlane() cgp2.contour.filled_contours = True # Set the axis and position to the middle of the data. cgp2.grid_plane.axis = 'y' cgp2.grid_plane.position = 15 e.add_module(cgp2) # An isosurface module. iso = IsoSurface(compute_normals=True) e.add_module(iso) iso.contour.contours = [5] # An interactive scalar cut plane. cp = ScalarCutPlane() e.add_module(cp) ip = cp.implicit_plane ip.normal = 0, 0, 1 ip.origin = 0.5, 0.5, 1.0 # Since this is running offscreen this seems necessary. ip.widget.origin = 0.5, 0.5, 1.0 ip.widget.enabled = False self.scene = e.current_scene self.cgp2 = cgp2 self.iso = iso self.cp = cp return
def streamline(): """Sets up the mayavi pipeline for the visualization. """ # Create an outline for the data. o = Outline() mayavi.add_module(o) s = Streamline(streamline_type='tube') mayavi.add_module(s) s.stream_tracer.integration_direction = 'both' s.seed.widget.center = 3.5, 0.625, 1.25 s.module_manager.scalar_lut_manager.show_scalar_bar = True i = IsoSurface() mayavi.add_module(i) i.contour.contours[0] = 550 i.actor.property.opacity = 0.5
def do(self): ############################################################ # Imports. script = self.script from mayavi.sources.vtk_file_reader import VTKFileReader from mayavi.modules.outline import Outline from mayavi.modules.iso_surface import IsoSurface from mayavi.modules.contour_grid_plane \ import ContourGridPlane from mayavi.modules.scalar_cut_plane import ScalarCutPlane ############################################################ # Create a new scene and set up the visualization. s = self.new_scene() # Read a VTK (old style) data file. r = VTKFileReader() r.initialize(get_example_data('heart.vtk')) script.add_source(r) # Create an outline for the data. o = Outline() script.add_module(o) # Create one ContourGridPlane normal to the 'x' axis. cgp1 = ContourGridPlane() script.add_module(cgp1) # Set the position to the middle of the data. cgp1.grid_plane.position = 15 # Another with filled contours normal to 'y' axis. cgp2 = ContourGridPlane() cgp2.contour.filled_contours = True # Set the axis and position to the middle of the data. cgp2.grid_plane.axis = 'y' cgp2.grid_plane.position = 15 script.add_module(cgp2) # An isosurface module. iso = IsoSurface(compute_normals=True) script.add_module(iso) iso.contour.contours = [200.0] # An interactive scalar cut plane. cp = ScalarCutPlane() script.add_module(cp) ip = cp.implicit_plane ip.normal = 0, 0, 1 ip.origin = 0, 0, 5 ip.widget.enabled = False # Set the scene to an isometric view. s.scene.isometric_view() # Now test. self.check() ############################################################ # Test if the modules respond correctly when the components # are changed. ctr = cgp2.contour cgp2.contour = ctr.__class__() cgp2.contour = ctr cgp2.actor = cgp2.actor.__class__() iso.contour = iso.contour.__class__() iso.contour.contours = [200.0] iso.actor = iso.actor.__class__() iso.normals = iso.normals.__class__() ip = cp.implicit_plane cp.implicit_plane = cp.implicit_plane.__class__() cp.implicit_plane = ip ip.widget.enabled = False cp.contour = cp.contour.__class__() cp.cutter = cp.cutter.__class__() cp.actor = cp.actor.__class__() s.render() # Now check. self.check() ############################################################ # Test if saving a visualization and restoring it works. # Save visualization. f = StringIO() f.name = abspath('test.mv2') # We simulate a file. script.save_visualization(f) f.seek(0) # So we can read this saved data. # Remove existing scene. engine = script.engine engine.close_scene(s) # Load visualization script.load_visualization(f) s = engine.current_scene self.check() ############################################################ # Test if the MayaVi2 visualization can be deep-copied. # Pop the source object. source = s.children.pop() # Add it back to see if that works without error. s.children.append(source) # Now set the enabled status of the widget, this is impossible # to get correctly. cp = source.children[0].children[-1] cp.implicit_plane.widget.enabled = False self.check() # Now deepcopy the source and replace the existing one with # the copy. This basically simulates cutting/copying the # object from the UI via the right-click menu on the tree # view, and pasting the copy back. source1 = copy.deepcopy(source) s.children[0] = source1 cp = source1.children[0].children[-1] cp.implicit_plane.widget.enabled = False self.check()
except NameError: from mayavi.api import Engine engine = Engine() engine.start() if len(engine.scenes) == 0: engine.new_scene() # ------------------------------------------- from mayavi.modules.iso_surface import IsoSurface #vtk_file_reader2 = engine.open(u'/home/m/spammsand/spammsand/water_to_duals/z_15.vtk') vtk_file_reader2 = engine.open( u'/home/matcha/Desktop/RESEARCH/spammsand_may_21_2015/spammsand/water_to_duals/z_14.vtk' ) iso_surface2 = IsoSurface() engine.add_module(iso_surface2, obj=None) iso_surface2.actor.mapper.scalar_mode = 'use_field_data' #iso_surface2.actor.property.specular_color = (0.5019607843137255, 0.5019607843137255, 0.5019607843137255) #iso_surface2.actor.property.diffuse_color = (0.5019607843137255, 0.5019607843137255, 0.5019607843137255) #iso_surface2.actor.property.ambient_color = (0.5019607843137255, 0.5019607843137255, 0.5019607843137255) #iso_surface2.actor.property.color = (0.5019607843137255, 0.5019607843137255, 0.5019607843137255) iso_surface2.actor.property.specular_color = (1.0, 1.0, 1.0) iso_surface2.actor.property.diffuse_color = (1.0, 1.0, 1.0) iso_surface2.actor.property.ambient_color = (1.0, 1.0, 1.0) iso_surface2.actor.property.color = (1.0, 1.0, 1.0) iso_surface2.actor.property.opacity = 0.3 iso_surface2.contour.contours[0:1] = [0.01]
def do(self): # Setup the source. self.make_data() from mayavi.modules.outline import Outline from mayavi.modules.iso_surface import IsoSurface from mayavi.modules.contour_grid_plane \ import ContourGridPlane from mayavi.modules.scalar_cut_plane import ScalarCutPlane script = self.script s = script.engine.current_scene # Create an outline for the data. o = Outline() script.add_module(o) # Create one ContourGridPlane normal to the 'x' axis. cgp = ContourGridPlane() script.add_module(cgp) # Set the position to the middle of the data. cgp.grid_plane.position = 15 # Another with filled contours normal to 'y' axis. cgp = ContourGridPlane() cgp.contour.filled_contours = True # Set the axis and position to the middle of the data. cgp.grid_plane.axis = 'y' cgp.grid_plane.position = 15 script.add_module(cgp) # An isosurface module. iso = IsoSurface(compute_normals=True) script.add_module(iso) iso.contour.contours = [200.0] # An interactive scalar cut plane. cp = ScalarCutPlane() script.add_module(cp) ip = cp.implicit_plane ip.normal = 0, 0, 1 ip.origin = 0, 0, 5 ip.widget.enabled = False # Set the scene to an isometric view. s.scene.isometric_view() self.check() ############################################################ # Test if saving a visualization and restoring it works. # Save visualization. f = BytesIO() f.name = abspath('test.mv2') # We simulate a file. script.save_visualization(f) f.seek(0) # So we can read this saved data. # Remove existing scene. engine = script.engine engine.close_scene(s) # Load visualization script.load_visualization(f) s = engine.current_scene self.check() ############################################################ # Test if the MayaVi2 visualization can be deep-copied. # Pop the source object. source = s.children.pop() # Add it back to see if that works without error. s.children.append(source) # Now set the enabled status of the widget, this is impossible # to get correctly. cp = source.children[0].children[-1] cp.implicit_plane.widget.enabled = False self.check() # Now deepcopy the source and replace the existing one with # the copy. This basically simulates cutting/copying the # object from the UI via the right-click menu on the tree # view, and pasting the copy back. source1 = copy.deepcopy(source) s.children[0] = source1 cp = source1.children[0].children[-1] cp.implicit_plane.widget.enabled = False self.check()
def view(prefix, name): """ construct a generic visualization of base mesh, refined mesh, and potential/field/pseudopotential data in mayavi2 requires running within mayavi2 or ipython with threads or something like:: from pyface.api import GUI GUI().start_event_loop() in your script to interact with it. this is from a simplified macro recorded in mayavi2 """ """ wwc 11/23/2018 In both python 2.7 and 3.5, this 3D visualization with mayavi works in Linux, but it's not compatible with X11 remote forwarding. Install mayavi through conda channel "menpo" in python 3.5 or just see environment setup of "ele35" in README. However, I haven't found a mayavi version for python 3.6. """ import mayavi try: from mayavi.api import Engine engine = Engine() engine.start() except AttributeError: # NameError: engine = mayavi.engine if len(engine.scenes) == 0: engine.new_scene() scene = engine.scenes[0] base_mesh_name = "%s_mesh.vtk" % prefix if os.access(base_mesh_name, os.R_OK): base_mesh = engine.open(base_mesh_name) surface = Surface() engine.add_filter(surface, base_mesh) surface.actor.property.representation = 'wireframe' surface.actor.property.line_width = 1 mesh_name = "%s_%s_mesh.vtk" % (prefix, name) if os.access(mesh_name, os.R_OK): mesh = engine.open(mesh_name) mesh.cell_scalars_name = 'charge' surface = Surface() engine.add_filter(surface, mesh) module_manager = mesh.children[0] module_manager.scalar_lut_manager.lut_mode = 'RdBu' module_manager.scalar_lut_manager.use_default_range = False r = np.fabs(module_manager.scalar_lut_manager.data_range).max() module_manager.scalar_lut_manager.data_range = [-r, r] surface.actor.property.backface_culling = True data_name = "%s_%s.vtk" % (prefix, name) if os.access(data_name, os.R_OK): data = engine.open(data_name) if "pseudo_potential" in data._point_scalars_list: data.point_scalars_name = "pseudo_potential" else: data.point_scalars_name = "potential" iso_surface = IsoSurface() engine.add_filter(iso_surface, data) module_manager = data.children[0] module_manager.scalar_lut_manager.lut_mode = 'Greys' iso_surface.contour.auto_contours = True iso_surface.contour.number_of_contours = 5 try: iso_surface.contour.maximum_contour = 1e-2 except: pass scene.scene.isometric_view() scene.scene.render()
def do(self): ############################################################ # Imports. script = self.script from mayavi.sources.vtk_file_reader import VTKFileReader from mayavi.modules.outline import Outline from mayavi.modules.iso_surface import IsoSurface from mayavi.modules.contour_grid_plane \ import ContourGridPlane from mayavi.modules.scalar_cut_plane import ScalarCutPlane ############################################################ # Create a new scene and set up the visualization. s = self.new_scene() # Read a VTK (old style) data file. r = VTKFileReader() r.initialize(get_example_data('heart.vtk')) script.add_source(r) # Create an outline for the data. o = Outline() script.add_module(o) # Create one ContourGridPlane normal to the 'x' axis. cgp1 = ContourGridPlane() script.add_module(cgp1) # Set the position to the middle of the data. cgp1.grid_plane.position = 15 # Another with filled contours normal to 'y' axis. cgp2 = ContourGridPlane() cgp2.contour.filled_contours = True # Set the axis and position to the middle of the data. cgp2.grid_plane.axis = 'y' cgp2.grid_plane.position = 15 script.add_module(cgp2) # An isosurface module. iso = IsoSurface(compute_normals=True) script.add_module(iso) iso.contour.contours = [200.0] # An interactive scalar cut plane. cp = ScalarCutPlane() script.add_module(cp) ip = cp.implicit_plane ip.normal = 0,0,1 ip.origin = 0,0,5 ip.widget.enabled = False # Set the scene to an isometric view. s.scene.isometric_view() # Now test. self.check() ############################################################ # Test if the modules respond correctly when the components # are changed. ctr = cgp2.contour cgp2.contour = ctr.__class__() cgp2.contour = ctr cgp2.actor = cgp2.actor.__class__() iso.contour = iso.contour.__class__() iso.contour.contours = [200.0] iso.actor = iso.actor.__class__() iso.normals = iso.normals.__class__() ip = cp.implicit_plane cp.implicit_plane = cp.implicit_plane.__class__() cp.implicit_plane = ip ip.widget.enabled = False cp.contour = cp.contour.__class__() cp.cutter = cp.cutter.__class__() cp.actor = cp.actor.__class__() s.render() # Now check. self.check() ############################################################ # Test if saving a visualization and restoring it works. # Save visualization. f = StringIO() f.name = abspath('test.mv2') # We simulate a file. script.save_visualization(f) f.seek(0) # So we can read this saved data. # Remove existing scene. engine = script.engine engine.close_scene(s) # Load visualization script.load_visualization(f) s = engine.current_scene self.check() ############################################################ # Test if the MayaVi2 visualization can be deep-copied. # Pop the source object. source = s.children.pop() # Add it back to see if that works without error. s.children.append(source) # Now set the enabled status of the widget, this is impossible # to get correctly. cp = source.children[0].children[-1] cp.implicit_plane.widget.enabled = False self.check() # Now deepcopy the source and replace the existing one with # the copy. This basically simulates cutting/copying the # object from the UI via the right-click menu on the tree # view, and pasting the copy back. source1 = copy.deepcopy(source) s.children[0] = source1 cp = source1.children[0].children[-1] cp.implicit_plane.widget.enabled = False self.check()
def do_profile(self): ############################################################ # Imports. ############################################################ script = self.script from mayavi.sources.vtk_file_reader import VTKFileReader from mayavi.modules.outline import Outline from mayavi.modules.iso_surface import IsoSurface from mayavi.modules.contour_grid_plane \ import ContourGridPlane from mayavi.modules.scalar_cut_plane import ScalarCutPlane ############################################################ # Create a new scene and set up the visualization. ############################################################ s = self.new_scene() # Read a VTK (old style) data file. r = VTKFileReader() r.initialize(get_example_data('heart.vtk')) script.add_source(r) # Create an outline for the data. o = Outline() script.add_module(o) # Create one ContourGridPlane normal to the 'x' axis. cgp1 = ContourGridPlane() script.add_module(cgp1) # Set the position to the middle of the data. cgp1.grid_plane.position = 15 # Another with filled contours normal to 'y' axis. cgp2 = ContourGridPlane() cgp2.contour.filled_contours = True # Set the axis and position to the middle of the data. cgp2.grid_plane.axis = 'y' cgp2.grid_plane.position = 15 script.add_module(cgp2) # An isosurface module. iso = IsoSurface(compute_normals=True) script.add_module(iso) iso.contour.contours = [200.0] # An interactive scalar cut plane. cp = ScalarCutPlane() script.add_module(cp) ip = cp.implicit_plane ip.normal = 0, 0, 1 ip.origin = 0, 0, 5 ip.widget.enabled = False # Set the scene to an isometric view. s.scene.isometric_view() s.render() # Remove existing scene. engine = script.engine engine.close_scene(s)
def init_view_objects(self, *args): """Initialize (or re-initialize) all the view elements in the scene. This needs to be called when q-space modeled area changes (changing the outline) or q-resolution, etc. """ #Prevent drawing all the intermediate steps self.scene.disable_render = True #First, we need to remove any data sources and modules from a previous run, # if they exist. # (this is only needed when chaning q-space parameters) for x in [ 'data_src', 'point_data_src', 'iso', 'points_module_surface', 'points_module_glyph', 'outline', 'mouse_text', 'mouse_cube' ]: if hasattr(self, x): getattr(self, x).remove() #Now the corner labels if hasattr(self, 'corner_text'): for txt in self.corner_text: txt.remove() engine = self.engine #We get the qspace_displayed array from experiment and make a copy of it. # This object will REMAIN here and just have its data updated. self.data_src = ArraySource( scalar_data=model.experiment.exp.get_qspace_displayed().copy()) self.data_src.scalar_name = "coverage" self.data_src.visible = False engine.add_source(self.data_src) # --- Text overlay for warnings ---- txt = Text(text="(Points were thinned down)", position_in_3d=False) txt.x_position = 0.02 txt.y_position = 0.98 txt.actor.text_scale_mode = "none" txt.actor.text_property.font_size = 14 txt.actor.text_property.vertical_justification = "top" self.warning_text = txt engine.add_module(self.warning_text) self.warning_text.visible = self.warning_text_visible #---- Make the isosurface object that goes with the volume coverage data ----- iso = IsoSurface() self.iso = iso #Scale the isosurface so that the size is actually q-vector iso.actor.actor.scale = tuple( np.array([1.0, 1.0, 1.0]) * model.experiment.exp.inst.q_resolution) #And we offset the position so that the center is at q-space (0,0,0) iso.actor.actor.position = tuple( np.array([1.0, 1.0, 1.0]) * -model.experiment.exp.inst.qlim) # When set to 1, This helps them to show up right in partially transparent mode. iso.actor.property.backface_culling = 0 iso.actor.property.frontface_culling = 0 #Add the module to the data source, to make it plot that data self.data_src.add_module(iso) # ---- Now we make a point data source, for the individual reflection plot --- self.point_data_src = VTKDataSource(name="Reflection point positions") self.point_data_src.visible = False engine.add_source(self.point_data_src) self.point_data_src.data = self.make_point_data( ) #still needs to get set. # ---- Make a module of simple points, using the Surface module ---- self.points_module_surface = Surface() self.points_module_surface.name = "Single reflections as pixels (Surface)" self.point_data_src.add_module(self.points_module_surface) # points representation = just plot a pixel for each vertex. self.points_module_surface.actor.property.set(representation='points', point_size=3) # ---- Make a module of glyphs, making spheres for each point ---- self.points_module_glyph = Glyph() self.points_module_glyph.name = "Single reflections as spheres (Glyph)" #No scaling of glyph size with scalar data self.points_module_glyph.glyph.scale_mode = 'data_scaling_off' gs = self.points_module_glyph.glyph.glyph_source gs.glyph_source = gs.glyph_dict['sphere_source'] #How many vertices does each sphere have? 3 = fastest. gs.glyph_source.phi_resolution = 3 gs.glyph_source.theta_resolution = 3 #And how big does each sphere end up? gs.glyph_source.radius = 0.1 #Add the module to the same point data source, to show it self.point_data_src.add_module(self.points_module_glyph) # Hide points initially self.points_module_surface.visible = False self.points_module_glyph.visible = False #Get the color look-up table self.points_lut = self.points_module_glyph.module_manager.scalar_lut_manager.lut.table.to_array( ) self.points_lut_original = 1 * self.points_lut # ---- Simple outline for all of the data. ----- self.outline = Outline() #Manually make the outline = the modeled volume, which is +- qlim = 1/d_min self.outline.manual_bounds = True self.outline.bounds = tuple( np.array([-1., 1., -1., 1., -1., 1.]) * model.experiment.exp.inst.qlim) #Add it to the scene directly. engine.add_module(self.outline) #--- Label the HKL of the corners --- if config_gui.cfg.label_corners: q = model.instrument.inst.qlim #This the reciprocal lattice vectors rec = model.experiment.exp.crystal.reciprocal_lattice self.corner_text = [] for x in [-q, q]: for y in [-q, q]: for z in [-q, q]: (h, k, l) = model.crystal_calc.get_hkl_from_q( column([x, y, z]), rec) label = "%d,%d,%d" % (h, k, l) txt = Text(text=label, position_in_3d=False) txt.position_in_3d = True txt.x_position = x txt.y_position = y txt.z_position = z txt.actor.text_scale_mode = "none" txt.actor.text_property.font_size = 14 txt.actor.text_property.vertical_justification = "top" txt.actor.text_property.justification = "left" txt.actor.property.color = (0.75, 1.0, 1.0) engine.add_module(txt) self.corner_text.append(txt) # ---- A text overlay, to show what is under the mouse ----- # self.mouse_text = Text(text=self.MOUSE_TEXT_WITH_NO_REFLECTION, position_in_3d=False) # self.mouse_text.x_position=0.02 # self.mouse_text.y_position=0.98 # self.mouse_text.actor.text_scale_mode = "none" # self.mouse_text.actor.text_property.font_size = 20 # self.mouse_text.actor.text_property.vertical_justification = "top" # engine.add_module(self.mouse_text) # ---- A cube highlighting where the mouse is ---- self.mouse_cube = Glyph() self.mouse_cube.name = "Cube highlighting mouse position (Glyph)" #No scaling of glyph size with scalar data self.mouse_cube.glyph.scale_mode = 'data_scaling_off' self.mouse_cube.glyph.color_mode = 'no_coloring' gs = self.mouse_cube.glyph.glyph_source gs.glyph_source = gs.glyph_dict['cube_source'] self.mouse_cube.actor.property.representation = "wireframe" self.mouse_cube.actor.property.specular = 1.0 # to make edge always white self.mouse_point_data_src = VTKDataSource() self.mouse_point_data_src.name = "Mouse position (VTK Data)" self.mouse_point_data_src.visible = True engine.add_source(self.mouse_point_data_src) self.mouse_point_data_src.data = self.make_single_point_data((0, 0, 0)) self.mouse_point_data_src.add_module(self.mouse_cube) self.mouse_cube.visible = False # #--- Reciprocal axes 3D view ---- # c = model.experiment.exp.crystal #@type c Crystal # (a,b,c) = (c.recip_a, c.recip_b, c.recip_c) # offset = model.experiment.exp.inst.qlim # self.scene.mlab.plot3d([offset, offset+a[0]], [offset, offset+a[1]], [offset, offset+a[2]], color=(1,0,0)) # self.scene.mlab.plot3d([offset, offset+b[0]], [offset, offset+b[1]], [offset, offset+b[2]], color=(0,1,0)) # self.scene.mlab.plot3d([offset, offset+c[0]], [offset, offset+c[1]], [offset, offset+c[2]], color=(0,0,1)) #Re-enable drawing self.scene.disable_render = False
def run(self): """This is executed once the application GUI has started. *Make sure all other MayaVi specific imports are made here!* """ # Various imports to do different things. from mayavi.sources.vtk_file_reader import VTKFileReader from mayavi.modules.outline import Outline from mayavi.modules.axes import Axes from mayavi.modules.grid_plane import GridPlane from mayavi.modules.image_plane_widget import ImagePlaneWidget from mayavi.modules.text import Text from mayavi.modules.contour_grid_plane import ContourGridPlane from mayavi.modules.iso_surface import IsoSurface script = self.script # Create a new scene. script.new_scene() # Read a VTK (old style) data file. r = VTKFileReader() r.initialize(join(get_data_dir(abspath(__file__)), 'heart.vtk')) script.add_source(r) # Put up some text. t = Text(text='MayaVi rules!', x_position=0.2, y_position=0.9, width=0.8) t.property.color = 1, 1, 0 # Bright yellow, yeah! script.add_module(t) # Create an outline for the data. o = Outline() script.add_module(o) # Create an axes for the data. a = Axes() script.add_module(a) # Create three simple grid plane modules. # First normal to 'x' axis. gp = GridPlane() script.add_module(gp) # Second normal to 'y' axis. gp = GridPlane() gp.grid_plane.axis = 'y' script.add_module(gp) # Third normal to 'z' axis. gp = GridPlane() script.add_module(gp) gp.grid_plane.axis = 'z' # Create one ImagePlaneWidget. ipw = ImagePlaneWidget() script.add_module(ipw) # Set the position to the middle of the data. ipw.ipw.slice_position = 16 # Create one ContourGridPlane normal to the 'x' axis. cgp = ContourGridPlane() script.add_module(cgp) # Set the position to the middle of the data. cgp.grid_plane.axis = 'y' cgp.grid_plane.position = 15 # An isosurface module. iso = IsoSurface(compute_normals=True) script.add_module(iso) iso.contour.contours = [200.0] # Set the view. s = script.engine.current_scene cam = s.scene.camera cam.azimuth(45) cam.elevation(15) s.render()
def view(prefix, name): """ construct a generic visualization of base mesh, refined mesh, and potential/field/pseudopotential data in mayavi2 requires running within mayavi2 or ipython with threads or something like:: from pyface.api import GUI GUI().start_event_loop() in your script to interact with it. this is from a simplified macro recorded in mayavi2 """ try: engine = mayavi.engine except NameError: from mayavi.api import Engine engine = Engine() engine.start() if len(engine.scenes) == 0: engine.new_scene() scene = engine.scenes[0] base_mesh_name = "%s_mesh.vtk" % prefix if os.access(base_mesh_name, os.R_OK): base_mesh = engine.open(base_mesh_name) surface = Surface() engine.add_filter(surface, base_mesh) surface.actor.property.representation = 'wireframe' surface.actor.property.line_width = 1 mesh_name = "%s_%s_mesh.vtk" % (prefix, name) if os.access(mesh_name, os.R_OK): mesh = engine.open(mesh_name) mesh.cell_scalars_name = 'charge' surface = Surface() engine.add_filter(surface, mesh) module_manager = mesh.children[0] module_manager.scalar_lut_manager.lut_mode = 'RdBu' module_manager.scalar_lut_manager.use_default_range = False r = np.fabs(module_manager.scalar_lut_manager.data_range).max() module_manager.scalar_lut_manager.data_range = [-r, r] surface.actor.property.backface_culling = True data_name = "%s_%s.vtk" % (prefix, name) if os.access(data_name, os.R_OK): data = engine.open(data_name) if "pseudo_potential" in data._point_scalars_list: data.point_scalars_name = "pseudo_potential" else: data.point_scalars_name = "potential" iso_surface = IsoSurface() engine.add_filter(iso_surface, data) module_manager = data.children[0] module_manager.scalar_lut_manager.lut_mode = 'Greys' iso_surface.contour.auto_contours = True iso_surface.contour.number_of_contours = 5 try: iso_surface.contour.maximum_contour = 1e-2 except: pass scene.scene.isometric_view() scene.scene.render()