Camera Observer Arguments

I’m trying to add an observer to the camera (and call a function to set a slice plane normal to camera), the function itself works fine but the observer of the camera either states “TypeError: ‘NoneType’ object is not callable” or wrong arguments if i input anything else than vtk.vtkCommand.ModifiedEvent,

Here is the code i’ve tried :

#grab reformat widget and green slice
layoutManager = slicer.app.layoutManager()
reformatWidget = slicer.modules.reformat.widgetRepresentation()
targetslice = slicer.app.layoutManager().sliceWidget('Green')
sliceNode = targetslice.mrmlSliceNode()
reformatWidget.setEditedNode(sliceNode,"","")
reformatWidget.setNormalToCamera ()

# Grab camera and cameranode
view = layoutManager.threeDWidget(0).threeDView()
threeDViewNode = view.mrmlViewNode()
cameraNode = slicer.modules.cameras.logic().GetViewActiveCameraNode(threeDViewNode)  ##grabs camera NODE
camera = cameraNode.GetCamera()
# function to call
def refresh():
    reformatWidget.setNormalToCamera ()

#set observer  <-------problematic part
cameraobserver = camera.AddObserver(vtk.vtkCommand.ModifiedEvent, refresh())
#also tried   cameraobserver = camera.AddObserver("ModifiedEvent", refresh())

Any suggestions? thanks.

I see two syntax errors:

  • second argument for Add Observer must be refresh or lambda: refresh() (and not refresh())
  • refresh method must have two arguments (see this example)
1 Like

Thanks a lot! i could fix it by doing these modifications:

def refresh(param1, param2):
    reformatWidget.setNormalToCamera()
#set observer
cameraobserver = camera.AddObserver(vtk.vtkCommand.ModifiedEvent, refresh, 2)

I dont understand why the refresh method needs two arguments though, even if it doesent use them,
also in this example there is no “,2” at the end (i couldnt make it work without it)

https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Get_a_notification_if_a_markup_point_position_is_modified

AddObserver requires a callback function that can receive event id and caller parameters (and optionally an additional eventdata), regardless if you use this information in the callback function or not.

The third parameter of AddObserver is the event priority. It is optional, default value is 0.0.

1 Like

Understood, thanks for your help!