mayavi
mayavi copied to clipboard
mayavi view (azimuth, elevation, distance)
In the dummy program below I'm just trying to control the properties of some graphics objects with a slider. The program works as intended but I would like to be able to control the view (the position and direction of the camera) from the program. I normally do that with mlab.view(azimuth=None, elevation=None, distance=None, focalpoint=None,roll=None, reset_roll=True, figure=None) but that does not seem to work in this case. What should I do instead ?
Poul Riis Denmark
import numpy as np from mayavi import mlab from traits.api import HasTraits, Range, Instance,on_trait_change from traitsui.api import View, Item, Group from mayavi.core.ui.api import MayaviScene, SceneEditor, MlabSceneModel from matplotlib.colors import hsv_to_rgb
rgb1=hsv_to_rgb((0.2,0.7,1))
#mlab.clf()
class MyModel(HasTraits):
slider = Range(-10., 10., 0,)
scene = Instance(MlabSceneModel, ())
def __init__(self,label='d'):
HasTraits.__init__(self)
#self.s = mlab.surf(x, y, np.asarray(x*1.5, 'd'), figure=self.scene.mayavi_scene)
self.xaxis=mlab.quiver3d(-100, 0, 0, 200,0,0, line_width=1, scale_factor=1,color=(0.8,0.9,0.7), figure=self.scene.mayavi_scene)
self.yaxis=mlab.quiver3d(0, -100, 0, 0,200,0, line_width=1, scale_factor=1, figure=self.scene.mayavi_scene)
self.zaxis=mlab.quiver3d(0, 0,-100, 0,0,200, line_width=1, scale_factor=1,color=(0.8,0.7,0.9), figure=self.scene.mayavi_scene)
self.origo=mlab.points3d(0,0,0,10,resolution=256,scale_factor=1,color=(rgb1[0],rgb1[1],rgb1[2]), figure=self.scene.mayavi_scene)
@on_trait_change('slider')
def slider_changed(self):
#self.s.mlab_source.scalars = np.asarray(x * (self.slider + 1), 'd')
self.origo.glyph.glyph_source.glyph_source.center = [self.slider, self.slider/2, 0]
self.origo.glyph.glyph_source.glyph_source.radius = abs(self.slider/10)+0.5
view = View(Item('scene', editor=SceneEditor(scene_class=MayaviScene),label='Mayavi'),Group("slider",label='d'),width=1200,height=1000)
#view(70,60,100)
my_model = MyModel() my_model.configure_traits()
In your case you need to use mlab.view(70, 60, 100) (as it is a method of mlab) instead of just view(70, 60, 100).
Additionally, the command must be put inside any method of your class MyModel.
For instance, when putting mlab.view(70, 60, 100) as last line into the function body of slider_changed all works correctly.
Doing this, the view is set every time the slider is changed.
Maybe the confusion arises from the fact that both the camera function (mlab.view) and the GUI definition variable (view = View(...) both share the same name.
Here is the full code:
import numpy as np
from mayavi import mlab
from traits.api import HasTraits, Range, Instance,on_trait_change
from traitsui.api import View, Item, Group
from mayavi.core.ui.api import MayaviScene, SceneEditor, MlabSceneModel
from matplotlib.colors import hsv_to_rgb
rgb1=hsv_to_rgb((0.2,0.7,1))
#mlab.clf()
class MyModel(HasTraits):
slider = Range(-10., 10., 0,)
scene = Instance(MlabSceneModel, ())
def __init__(self,label='d'):
HasTraits.__init__(self)
#self.s = mlab.surf(x, y, np.asarray(x*1.5, 'd'), figure=self.scene.mayavi_scene)
self.xaxis=mlab.quiver3d(-100, 0, 0, 200,0,0, line_width=1, scale_factor=1,color=(0.8,0.9,0.7), figure=self.scene.mayavi_scene)
self.yaxis=mlab.quiver3d(0, -100, 0, 0,200,0, line_width=1, scale_factor=1, figure=self.scene.mayavi_scene)
self.zaxis=mlab.quiver3d(0, 0,-100, 0,0,200, line_width=1, scale_factor=1,color=(0.8,0.7,0.9), figure=self.scene.mayavi_scene)
self.origo=mlab.points3d(0,0,0,10,resolution=256,scale_factor=1,color=(rgb1[0],rgb1[1],rgb1[2]), figure=self.scene.mayavi_scene)
@on_trait_change('slider')
def slider_changed(self):
#self.s.mlab_source.scalars = np.asarray(x * (self.slider + 1), 'd')
self.origo.glyph.glyph_source.glyph_source.center = [self.slider, self.slider/2, 0]
self.origo.glyph.glyph_source.glyph_source.radius = abs(self.slider/10)+0.5
mlab.view(70,60,100)
view = View(Item('scene', editor=SceneEditor(scene_class=MayaviScene),label='Mayavi'),Group("slider",label='d'),width=1200,height=1000)
my_model = MyModel()
my_model.configure_traits()