Using the Plus Remote Module from the Python Interactor

Hi,

I am trying to launch a PlusServer instance from the python interactor. Specifically, I need to be able to start/stop the server, and each time I start, I would like to specify the config file. The reason for this is because we will be updating parameters in the config file and then restarting PlusServer with new, calibrated parameters, and this will eventually need to be done in a scripted module.

I have had success interacting with the PlusServerLauncher from the Plus Remote GUI in slicer, but it would be great if I could do something like:

remote = slicer.modules.plusremote

remote.connect(ā€˜localhostā€™, 18904)
remote.SetConfigFile(ā€˜SomeDefaultConfigFileā€™)
remote.StartServer()

Then start an OpenIGTLinkConnector to stream data into slicer. When I want to change the config file, I would do something like:

remote.StopServer() # When I want to stop streaming data
remote.SetConfigFile(ā€˜SomeUpdatedConfigFileā€™)
remote.StartServer()

I have tried to search for a good way to do the above from the python interactor, but I have had no luck. Let me know if you have any insights on this.

Thanks,

James

What version of Slicer are you using?
If you are using Slicer >= 4.10.1 with an updated version of SlicerOpenIGTLink, then there are a couple of ways to do it:

launcherNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLPlusServerLauncherNode")
serverNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLPlusServerNode")

launcherNode.AddAndObserveServerNode(serverNode)
serverNode.SetAndObserveConfigNode(configFileNode)
serverNode.StartServer()

or

launcherNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLPlusServerLauncherNode")
serverNode = launcher.StartServer(configFileNode)

Note that the configFileNode is a vtkMRMLTextNode.

Relevant MRML nodes are here: https://github.com/openigtlink/SlicerOpenIGTLink/tree/master/PlusRemote/MRML

The vtkMRMLPlusServerNode controls the state of the server and can be changed using the StartServer() and StopServer() or SetState() methods.

Thanks, Kyle. This worked flawlessly.

For anyone else who needs the info, after starting PlusServerLauncher, the full way I got this working was by:

fn = "C:/Full/Path/To/ConfigFile.xml"
with open(fn, 'r') as file:
  configText = file.read()

configNode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLTextNode')
configNode.SetText(configText) # Sets the text to the XML file contents

launcherNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLPlusServerLauncherNode")
serverNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLPlusServerNode")

launcherNode.AddAndObserveServerNode(serverNode)
serverNode.SetAndObserveConfigNode(configNode)

serverNode.StartServer()

# Do whatever with data streamed into Slicer... 
pass

# Also, you can change the configFile if/when desired
serverNode.StopServer()
configNode.SetText(SomeNewXmlText)
serverNode.StartServer()
1 Like