I am quite new to blender and writing on my first plugin/add-on. I am currently executing the code from the text-editor with the “Run Script” button.
The code creates a TCP server that runs in a separate thread and connects to mobile phones. With them I exchange data.
I am drawing a Panel in the Tool Shelf which displays the connected phones (via row=layout.row() and row.label(‘abc’)). I am sending periodically pings to the phones to check whether they are still connected. If not, I delete the phone from the phones list.
Now I need to be able to redraw the Panel whenever a phone disconnects so the Panel gets redrawn and does not display that phone anymore. I googled for a few hours but couldn’t find anything. Does anyone have an idea how to do this?
I try to find a way to redraw the panel, now I have managed to force an redraw by register/unregister the panel. Though if you have any better alternatives I am interested to know.
import bpy
import time
count = 0
count_list = [0] * 10
panel_class = None
panel_timer = None
time_interval = 1
time_previous = time.time()
# This is the socket check
def time_handler(scene):
global time_previous, time_interval
if time.time() > time_previous + time_interval:
global count, count_list
count_list.insert(0, count)
count_list.pop()
count += 1
redraw_panel()
time_previous = time.time()
print("Update:", time_previous)
# This is the panel
class HelloWorldPanel(bpy.types.Panel):
bl_label = "Hello World Panel"
bl_idname = "OBJECT_PT_hello"
bl_space_type = "PROPERTIES"
bl_region_type = "WINDOW"
bl_context = "scene"
def draw(self, context):
layout = self.layout
print("Redraw:", time.time())
global count_list
for i in count_list:
layout.label(str(i))
# print(i)
# row.label(text="Hello world!", icon="WORLD_DATA")
# row.operator("mesh.primitive_cube_add")
# Awful way to redraw the panel but thankfully it works.
# Best used for personal projects because users must be treated with respect. :)
def redraw_panel():
try:
bpy.utils.unregister_class(HelloWorldPanel)
except:
pass
bpy.utils.register_class(HelloWorldPanel)
# Start/stop handler
handler = bpy.app.handlers.scene_update_post
if len(handler) > 0:
print("Stopped Handler")
handler.clear()
count = 0
else:
print("Started Handler")
handler.append(time_handler)