Commit 065a2fad authored by Bruno Laurencich's avatar Bruno Laurencich

fix crash, no panel redraw

parent 4eb0af89
*.sublime*
*.blend[0-9]
......@@ -8,14 +8,23 @@ bl_info = {
if "bpy" in locals():
import imp
imp.reload(mdns)
imp.reload(panel)
else:
from . import mdns
from . import panel
# pass
# from . import mysphere
import bpy
import threading
from bpy import props
from bpy.types import WindowManager
C = bpy.context
# =====================================================
# = OPERATOR: GET THE NOTOCHORD IP =
# =====================================================
class Chord_Get_Ip(bpy.types.Operator):
"""Tries to get the ip of the notochord"""
......@@ -23,6 +32,8 @@ class Chord_Get_Ip(bpy.types.Operator):
bl_label = "Chordata: get notochord ip"
bl_options = {'REGISTER'}
# ----------- MODAL -----------
def modal(self, context, event):
if event.type in {'RIGHTMOUSE', 'ESC'}:
self.cancel(context)
......@@ -31,22 +42,28 @@ class Chord_Get_Ip(bpy.types.Operator):
if event.type == 'TIMER':
if self.query_thread.is_alive():
print("ip query ongoing..")
context.window_manager.chordata.state = possible_states()[1][0]
else:
context.window_manager.chordata.state = possible_states()[2][0]
with self.query_lock:
print("IP found :", self.found_ip)
# self.cancel(context)
# return {'FINISHED'}
self.cancel(context)
return {'FINISHED'}
return {'PASS_THROUGH'}
# ----------- DO QUERY (thread handler) -----------
def do_query(self):
print("query thread running")
ip = mdns.get_ip()
while not ip and mdns.timeout < 10:
while not ip and mdns.timeout < 5:
mdns.set_timeout(mdns.timeout + 2)
print("*** new timeout ", mdns.timeout)
if mdns.timeout >= 10:
if mdns.timeout >= 5:
mdns.set_nameserver()
ip = mdns.get_ip()
......@@ -57,8 +74,11 @@ class Chord_Get_Ip(bpy.types.Operator):
print("*** Thread terminating..")
return
# ----------- EXECUTE -----------
def execute(self, context):
context.window_manager.chordata.state = possible_states()[0][0]
wm = context.window_manager
self._timer = wm.event_timer_add(0.5, context.window)
wm.modal_handler_add(self)
......@@ -70,26 +90,66 @@ class Chord_Get_Ip(bpy.types.Operator):
name="mdns_query")
self.query_thread.start()
return {'RUNNING_MODAL'}
return {'FINISHED'}
# ----------- CANCEL -----------
def cancel(self, context):
wm = context.window_manager
wm.event_timer_remove(self._timer)
# print("waiting thread..")
# self.query_thread.join()
# print("thread joined..")
print("waiting thread..")
self.query_thread.join()
print("thread joined..")
# ====== End of OPERATOR: GET THE NOTOCHORD IP =======
# =========================================
# = GLOBAL PROPERTIES =
# =========================================
def possible_states():
"""Returns the 'items' to create the states enum property.
The format is a list of tuples like:
[(identifier, name, description, icon, number), ...]
complete doc in:
https://docs.blender.org/api/blender_python_api_2_77_0/bpy.props.html#bpy.props.EnumProperty """
return [
("Disconnected", "no-conn", "", "COLOR_RED", 1),
("Searching..", "search", "", "INLINK", 2),
("Connected", "conn", "", "COLOR_GREEN", 3)
]
def get_state(self):
# print("GETTER")
# print(self)
# print(self.state)
return bpy.types.Chord_Properties.bl_rna.properties['state'].enum_items[self.state].value
# Create a collection to hold all the chordata properies
class Chord_Properties(bpy.types.PropertyGroup):
state = props.EnumProperty(items=possible_states(), options={'SKIP_SAVE'})
# ====== End of GLOBAL PROPERTIES =======
def register():
bpy.utils.register_class(Chord_Get_Ip)
# bpy.utils.register_class(Chord_Get_Ip)
bpy.utils.register_class(Chord_Properties)
WindowManager.chordata = \
bpy.props.PointerProperty(type=Chord_Properties)
bpy.utils.register_module(__name__)
def unregister():
bpy.utils.unregister_class(Chord_Get_Ip)
def unregister():
# bpy.utils.unregister_class(Chord_Get_Ip)
bpy.utils.unregister_module(__name__)
del WindowManager.chordata
if __name__ == "__main__":
register()
......
File added
import bpy
def get_conn_state_attr(context, attr = "name"):
current = context.window_manager.chordata.state
return getattr(bpy.types.Chord_Properties\
.bl_rna.properties['state']\
.enum_items[current], attr )
#
# Menu in tools region, Chordata tab
#
class Chord_tools_panel(bpy.types.Panel):
"""Main panel for the chordata client addon"""
bl_label = "Chordata Panel"
bl_category = "Chordata"
bl_space_type = "VIEW_3D"
bl_region_type = "TOOLS"
def draw(self, context):
layout = self.layout
layout.operator("chordata.get_ip", "Connect")
layout.label(context.window_manager.chordata.state,\
icon= get_conn_state_attr(context, "icon"))
# conn_state = get_conn_state_attr(context, "name")
# print(conn_state)
# layout.prop(context.window_manager.chordata, "state", expand=True)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment