enthought / mayavi

3D visualization of scientific data in Python
http://docs.enthought.com/mayavi/mayavi/
Other
1.28k stars 282 forks source link

AttributeError: 'NoneType' object has no attribute 'active_camera' #1189

Closed giammi56 closed 1 year ago

giammi56 commented 1 year ago

I am trying to set a view angle of a scene, as suggested in many examples, with the following code:

class ActorViewer(HasTraits):
    #Create a mayavi scene with a specified engine
    engine = Instance(Engine, ())
    scene = Instance(MlabSceneModel, ())
    path = Str
    label = Str
    # # Using 'scene_class=MayaviScene' adds a Mayavi icon to the toolbar,to pop up a dialog editing the pipeline.
    view = View(Item(name='scene',
                    editor=SceneEditor(scene_class=MayaviScene),
                    show_label=False,
                    resizable=True,
                    height=500,
                    width=500
                    ),
                )

    def __init__(self, path, label, engine=None, **traits):
        self.path = path
        self.label = label
        HasTraits.__init__(self, **traits)
        self.engine.start()
        if engine is not None:
            self.scene=MlabSceneModel(engine=self.engine)
        else:
            self.scene=MlabSceneModel()

    @on_trait_change('scene.activated')
    def scene_initiation(self):
        src=self.scene.mlab.pipeline.open(self.path+self.label+'.vtr')
        self.scene.mlab.pipeline.iso_surface(src, contours=60, opacity=0.5)
        out=self.scene.mlab.pipeline.outline(src, figure=self.scene.mayavi_scene, line_width=0.5)
        out.outline_mode = "cornered"
        self.scene.render() #test to prevent active_camera error
        self.scene.mlab.view(40, 50, figure=self.scene.mayavi_scene)

While loading a .vtr file, the command ´self.scene.mlab.view()´ return an error, but the result is shown correctly. How can I suppress the output? Am I doing something wrong?

file name  E2_417_1.vtr
Exception occurred in traits notification handler for object: <__main__.ActorViewer object at 0x7f53da6e86d0>, trait: scene, old value: <mayavi.tools.mlab_scene_model.MlabSceneModel object at 0x7f53d9d524f0>, new value: <mayavi.tools.mlab_scene_model.MlabSceneModel object at 0x7f53da6575e0>
Traceback (most recent call last):
  File "/home/me/anaconda3/envs/mayavi/lib/python3.8/site-packages/traits/trait_notifiers.py", line 524, in _dispatch_change_event
    self.dispatch(handler, *args)
  File "/home/me/anaconda3/envs/mayavi/lib/python3.8/site-packages/traits/trait_notifiers.py", line 486, in dispatch
    handler(*args)
  File "/home/me/Documents/ddscat7.3.3_200717/load_scene_nearfield.py", line 60, in scene_initiation
    self.scene.mlab.view(40, 50, figure=self.scene.mayavi_scene)
  File "/home/me/anaconda3/envs/mayavi/lib/python3.8/site-packages/mayavi/tools/camera.py", line 255, in view
    cam = scene.camera
  File "/home/me/anaconda3/envs/mayavi/lib/python3.8/site-packages/tvtk/pyface/tvtk_scene.py", line 709, in _get_camera
    return self._renderer.active_camera
AttributeError: 'NoneType' object has no attribute 'active_camera' 
prabhuramachandran commented 1 year ago

This is not an issue, please do not post questions here. ets-users should be a better place to post generic queries.

  1. Your __init__ is not doing anything useful, remove it.
  2. Please provide a minimal example that others can execute without additional data.