Error with StyleInteractor

I have the following code

class MyInteractorStyle(vtkInteractorStyleTerrain):
    
    def __init__(self, renderer):
        
        self.SetCurrentRenderer(renderer)
        self.AddObserver("MouseMoveEvent", self.mouse_move_event)

    
    def mouse_move_event(self, obj, event):
       
        # Get the interactor
        iren = self.GetInteractor()
        if iren is None: return

        dx = iren.GetEventPosition()[0] - iren.GetLastEventPosition()[0];
        dy = iren.GetEventPosition()[1] - iren.GetLastEventPosition()[1];

        camera = self.GetCurrentRenderer().GetActiveCamera()
        camera.SetRoll(0)
        screenSize = iren.GetRenderWindow().GetSize()

        # Yaw changes in a negative direction but the pitch is correct
        camera.Yaw(-float(dx) / float(screenSize[0]) * 360 * 2.0);
        camera.Pitch(float(dy) / float(screenSize[1]) * 180 / 2.0);

        # Update the clipping range of the camera
        self.GetCurrentRenderer().ResetCameraClippingRange()
        
        # render
        self.GetCurrentRenderer().GetRenderWindow().Render()
        return

However everytime I move the mouse, I get the following error:

Input In [4], in MyInteractorStyle.mouse_move_event(self, obj, event)

—> 25 camera = self.GetCurrentRenderer().GetActiveCamera()

AttributeError: ‘NoneType’ object has no attribute ‘GetActiveCamera’

I pass a renderer whenever I initialize an instance of MyInteractorStyle

Hello,

Calling setter methods from inside the constructor, though not illegal, should be considered an unsafe operation. It’s best to set the renderer elsewhere after instantiating your MyInteractorStyle object.

take care,

Paulo

Thanks for the clarification, Paulo.

I was just following an example I found (pretty much all that is available given the sparcity of information available).

Hi,

Not all code snippets out there have good practices. During constructor execution, one should consider the object is incomplete, hence the name: constructor: it means that the object is under construction. Thus, changing the state of the object in the constructor shouldn’t be considered safe. You can perfectly set the renderer after instantiating the object.

regards,

Paulo