refactor: cleanup

This commit is contained in:
Swann
2019-08-06 11:34:39 +02:00
parent e376650df2
commit 00e612f413
3 changed files with 101 additions and 107 deletions

View File

@ -30,92 +30,92 @@ execution_queue = queue.Queue()
# The function will be executed when the timer runs the next time. # The function will be executed when the timer runs the next time.
def run_in_main_thread(function, args): # def run_in_main_thread(function, args):
execution_queue.put(function) # execution_queue.put(function)
def execute_queued_functions(): # def execute_queued_functions():
while not execution_queue.empty(): # while not execution_queue.empty():
function, args = execution_queue.get() # function, args = execution_queue.get()
function(args[0], args[1]) # function(args[0], args[1])
return .1 # return .1
def clean_scene(elements=environment.rtypes): # def clean_scene(elements=environment.rtypes):
for datablock in elements: # for datablock in elements:
datablock_ref = getattr(bpy.data, utils.BPY_TYPES[datablock]) # datablock_ref = getattr(bpy.data, utils.BPY_TYPES[datablock])
for item in datablock_ref: # for item in datablock_ref:
try: # try:
datablock_ref.remove(item) # datablock_ref.remove(item)
# Catch last scene remove # # Catch last scene remove
except RuntimeError: # except RuntimeError:
pass # pass
def upload_client_instance_position(): # def upload_client_instance_position():
username = bpy.context.window_manager.session.username # username = bpy.context.window_manager.session.username
if client: # if client:
key = "Client/{}".format(username) # key = "Client/{}".format(username)
current_coords = presence.get_client_view_rect() # current_coords = presence.get_client_view_rect()
client_list = client.get(key) # client_list = client.get(key)
if current_coords and client_list: # if current_coords and client_list:
if current_coords != client_list[0][1]['location']: # if current_coords != client_list[0][1]['location']:
client_list[0][1]['location'] = current_coords # client_list[0][1]['location'] = current_coords
client.set(key, client_list[0][1]) # client.set(key, client_list[0][1])
def update_client_selected_object(context): # def update_client_selected_object(context):
session = bpy.context.window_manager.session # session = bpy.context.window_manager.session
username = bpy.context.window_manager.session.username # username = bpy.context.window_manager.session.username
client_key = "Client/{}".format(username) # client_key = "Client/{}".format(username)
client_data = client.get(client_key) # client_data = client.get(client_key)
selected_objects = utils.get_selected_objects(context.scene) # selected_objects = utils.get_selected_objects(context.scene)
if len(selected_objects) > 0 and len(client_data) > 0: # if len(selected_objects) > 0 and len(client_data) > 0:
for obj in selected_objects: # for obj in selected_objects:
# if obj not in client_data[0][1]['active_objects']: # # if obj not in client_data[0][1]['active_objects']:
client_data[0][1]['active_objects'] = selected_objects # client_data[0][1]['active_objects'] = selected_objects
client.set(client_key, client_data[0][1]) # client.set(client_key, client_data[0][1])
break # break
elif client_data and client_data[0][1]['active_objects']: # elif client_data and client_data[0][1]['active_objects']:
client_data[0][1]['active_objects'] = [] # client_data[0][1]['active_objects'] = []
client.set(client_key, client_data[0][1]) # client.set(client_key, client_data[0][1])
# TODO: cleanup # TODO: cleanup
def init_datablocks(): # def init_datablocks():
for datatype in environment.rtypes: # for datatype in environment.rtypes:
if bpy.context.window_manager.session.supported_datablock[datatype].is_replicated: # if bpy.context.window_manager.session.supported_datablock[datatype].is_replicated:
for item in getattr(bpy.data, utils.BPY_TYPES[datatype]): # for item in getattr(bpy.data, utils.BPY_TYPES[datatype]):
item.id = bpy.context.window_manager.session.username # item.id = bpy.context.window_manager.session.username
key = "{}/{}".format(datatype, item.name) # key = "{}/{}".format(datatype, item.name)
client.set(key) # client.set(key)
def default_tick(): # def default_tick():
upload_client_instance_position() # upload_client_instance_position()
return .1 # return .1
def register_ticks(): # def register_ticks():
# # REGISTER Updaters
# bpy.app.timers.register(default_tick)
# bpy.app.timers.register(execute_queued_functions)
# def unregister_ticks():
# REGISTER Updaters # REGISTER Updaters
bpy.app.timers.register(default_tick) # try:
bpy.app.timers.register(execute_queued_functions) # bpy.app.timers.unregister(default_tick)
# bpy.app.timers.unregister(execute_queued_functions)
# except:
def unregister_ticks(): # pass
# REGISTER Updaters
try:
bpy.app.timers.unregister(default_tick)
bpy.app.timers.unregister(execute_queued_functions)
except:
pass
# OPERATORS # OPERATORS
@ -172,6 +172,32 @@ class SessionStartOperator(bpy.types.Operator):
return {"FINISHED"} return {"FINISHED"}
class SessionStopOperator(bpy.types.Operator):
bl_idname = "session.stop"
bl_label = "close"
bl_description = "stop net service"
bl_options = {"REGISTER"}
@classmethod
def poll(cls, context):
return True
def execute(self, context):
global client
assert(client)
client.disconnect()
# del client_instance
# unregister_ticks()
# presence.renderer.stop()
return {"FINISHED"}
class SessionPropertyAddOperator(bpy.types.Operator): class SessionPropertyAddOperator(bpy.types.Operator):
bl_idname = "session.add_prop" bl_idname = "session.add_prop"
bl_label = "add" bl_label = "add"
@ -215,36 +241,6 @@ class SessionPropertyRemoveOperator(bpy.types.Operator):
return {"CANCELED"} return {"CANCELED"}
class SessionStopOperator(bpy.types.Operator):
bl_idname = "session.stop"
bl_label = "close"
bl_description = "stop net service"
bl_options = {"REGISTER"}
@classmethod
def poll(cls, context):
return True
def execute(self, context):
settings = context.window_manager.session
if client:
client.exit()
time.sleep(0.25)
# del client_instance
# client_instance = None
settings.is_admin = False
unregister_ticks()
presence.renderer.stop()
else:
logger.debug("No server/client_instance running.")
return {"FINISHED"}
class SessionPropertyRightOperator(bpy.types.Operator): class SessionPropertyRightOperator(bpy.types.Operator):
bl_idname = "session.right" bl_idname = "session.right"
bl_label = "Change owner to" bl_label = "Change owner to"
@ -450,7 +446,7 @@ def unregister():
# bpy.app.handlers.depsgraph_update_post.remove(depsgraph_update) # bpy.app.handlers.depsgraph_update_post.remove(depsgraph_update)
if client: if client:
client.exit() client.disconnect()
client = None client = None
from bpy.utils import unregister_class from bpy.utils import unregister_class

16
ui.py
View File

@ -25,7 +25,8 @@ class SESSION_PT_settings(bpy.types.Panel):
window_manager = context.window_manager window_manager = context.window_manager
row = layout.row() row = layout.row()
if not operators.client or (operators.client and operators.client.state() == 1): # STATE INITIAL
if not operators.client or (operators.client and operators.client.state == 0):
row = layout.row() row = layout.row()
# USER SETTINGS # USER SETTINGS
@ -98,7 +99,8 @@ class SESSION_PT_settings(bpy.types.Panel):
else: else:
if operators.client.state() == 3: # STATE ACTIVE
if operators.client.state == 2:
row = layout.row() row = layout.row()
row.operator("session.stop", icon='QUIT', text="Exit") row.operator("session.stop", icon='QUIT', text="Exit")
@ -107,11 +109,7 @@ class SESSION_PT_settings(bpy.types.Panel):
# row.operator("session.dump", icon='QUIT', text="Load") # row.operator("session.dump", icon='QUIT', text="Load")
row = layout.row() row = layout.row()
box = row.box() # STATE SYNCING
row = box.row()
row.label(text="", icon='INFO')
row = box.row()
row.label(text="Sync tasks: {}".format(operators.client.active_tasks))
else: else:
status = "connecting..." status = "connecting..."
if net_settings.is_admin: if net_settings.is_admin:
@ -132,7 +130,7 @@ class SESSION_PT_user(bpy.types.Panel):
bl_category = "Multiuser" bl_category = "Multiuser"
@classmethod @classmethod
def poll(cls, context): def poll(cls, context):
return operators.client and operators.client.state() == 3 return operators.client and operators.client.state == 2
def draw(self, context): def draw(self, context):
@ -179,7 +177,7 @@ class SESSION_PT_properties(bpy.types.Panel):
@classmethod @classmethod
def poll(cls, context): def poll(cls, context):
return operators.client and operators.client.state() == 3 return operators.client and operators.client.state == 2
def draw_header(self, context): def draw_header(self, context):
self.layout.label(text="", icon='OUTLINER_OB_GROUP_INSTANCE') self.layout.label(text="", icon='OUTLINER_OB_GROUP_INSTANCE')