Hello Slicer community.
I’d like to incorporate a function within Slicer that asynchronously waits for reception.
What I want is for the function to execute when Slicer is turned on, asynchronously wait for reception, and if received, send the corresponding value.
I’d like this function to continue running asynchronously independently of other functions.
My function looks like
async def ServerOpen(self):
HOST = '127.0.0.1'
PORT = 65432
print("server open")
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
print(f"{HOST}:{PORT}")
while True:
conn, addr = await loop.sock_accept(s)
await self.receive_data(conn)
async def receive_data(self, conn):
def convert_to_doubles(input_string):
length = len(input_string)
result = []
for i in range(0, length, 2):
substring = input_string[i:i+2]
num = float(substring)
result.append(num)
return result
while True:
data = await loop.sock_recv(conn, 1024)
if not data:
break
received_message = data.decode()
await loop.sock_sendall(conn, data)
numbers = convert_to_doubles(received_message)
print( numbers[0], numbers[1], numbers[2])
center = [numbers[0], numbers[1], numbers[2]]
radius = 6.0
sphereSource = vtk.vtkSphereSource()
sphereSource.SetCenter(center[0], center[1], center[2])
sphereSource.SetRadius(radius)
sphereSource.SetPhiResolution(30)
sphereSource.SetThetaResolution(30)
sphereSource.Update()
sphere = sphereSource.GetOutput()
renderer = slicer.app.layoutManager().threeDWidget(0).threeDView().renderWindow().GetRenderers().GetFirstRenderer()
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputData(sphere)
actor = vtk.vtkActor()
actor.SetMapper(mapper)
renderer.AddActor(actor)
renderer.ResetCamera()
slicer.app.layoutManager().threeDWidget(0).threeDView().renderWindow().Render()
Where and how should I insert this Python code? Thank you very much as always.