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

@ -1 +1 @@
Subproject commit e3de7fbfb9758fd576eba3a87d4a8c418066f16c
Subproject commit a2521439555257edd332cfcc7e0c477fb5021ca1

View File

@ -30,92 +30,92 @@ execution_queue = queue.Queue()
# The function will be executed when the timer runs the next time.
def run_in_main_thread(function, args):
execution_queue.put(function)
# def run_in_main_thread(function, args):
# execution_queue.put(function)
def execute_queued_functions():
while not execution_queue.empty():
function, args = execution_queue.get()
function(args[0], args[1])
return .1
# def execute_queued_functions():
# while not execution_queue.empty():
# function, args = execution_queue.get()
# function(args[0], args[1])
# return .1
def clean_scene(elements=environment.rtypes):
for datablock in elements:
datablock_ref = getattr(bpy.data, utils.BPY_TYPES[datablock])
for item in datablock_ref:
try:
datablock_ref.remove(item)
# Catch last scene remove
except RuntimeError:
pass
# def clean_scene(elements=environment.rtypes):
# for datablock in elements:
# datablock_ref = getattr(bpy.data, utils.BPY_TYPES[datablock])
# for item in datablock_ref:
# try:
# datablock_ref.remove(item)
# # Catch last scene remove
# except RuntimeError:
# pass
def upload_client_instance_position():
username = bpy.context.window_manager.session.username
if client:
# def upload_client_instance_position():
# username = bpy.context.window_manager.session.username
# if client:
key = "Client/{}".format(username)
# key = "Client/{}".format(username)
current_coords = presence.get_client_view_rect()
client_list = client.get(key)
# current_coords = presence.get_client_view_rect()
# client_list = client.get(key)
if current_coords and client_list:
if current_coords != client_list[0][1]['location']:
client_list[0][1]['location'] = current_coords
client.set(key, client_list[0][1])
# if current_coords and client_list:
# if current_coords != client_list[0][1]['location']:
# client_list[0][1]['location'] = current_coords
# client.set(key, client_list[0][1])
def update_client_selected_object(context):
session = bpy.context.window_manager.session
username = bpy.context.window_manager.session.username
client_key = "Client/{}".format(username)
client_data = client.get(client_key)
# def update_client_selected_object(context):
# session = bpy.context.window_manager.session
# username = bpy.context.window_manager.session.username
# client_key = "Client/{}".format(username)
# client_data = client.get(client_key)
selected_objects = utils.get_selected_objects(context.scene)
if len(selected_objects) > 0 and len(client_data) > 0:
# selected_objects = utils.get_selected_objects(context.scene)
# if len(selected_objects) > 0 and len(client_data) > 0:
for obj in selected_objects:
# if obj not in client_data[0][1]['active_objects']:
client_data[0][1]['active_objects'] = selected_objects
# for obj in selected_objects:
# # if obj not in client_data[0][1]['active_objects']:
# client_data[0][1]['active_objects'] = selected_objects
client.set(client_key, client_data[0][1])
break
# client.set(client_key, client_data[0][1])
# break
elif client_data and client_data[0][1]['active_objects']:
client_data[0][1]['active_objects'] = []
client.set(client_key, client_data[0][1])
# elif client_data and client_data[0][1]['active_objects']:
# client_data[0][1]['active_objects'] = []
# client.set(client_key, client_data[0][1])
# TODO: cleanup
def init_datablocks():
for datatype in environment.rtypes:
if bpy.context.window_manager.session.supported_datablock[datatype].is_replicated:
for item in getattr(bpy.data, utils.BPY_TYPES[datatype]):
item.id = bpy.context.window_manager.session.username
key = "{}/{}".format(datatype, item.name)
client.set(key)
# def init_datablocks():
# for datatype in environment.rtypes:
# if bpy.context.window_manager.session.supported_datablock[datatype].is_replicated:
# for item in getattr(bpy.data, utils.BPY_TYPES[datatype]):
# item.id = bpy.context.window_manager.session.username
# key = "{}/{}".format(datatype, item.name)
# client.set(key)
def default_tick():
upload_client_instance_position()
# def default_tick():
# 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
bpy.app.timers.register(default_tick)
bpy.app.timers.register(execute_queued_functions)
def unregister_ticks():
# REGISTER Updaters
try:
bpy.app.timers.unregister(default_tick)
bpy.app.timers.unregister(execute_queued_functions)
except:
pass
# try:
# bpy.app.timers.unregister(default_tick)
# bpy.app.timers.unregister(execute_queued_functions)
# except:
# pass
# OPERATORS
@ -172,6 +172,32 @@ class SessionStartOperator(bpy.types.Operator):
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):
bl_idname = "session.add_prop"
bl_label = "add"
@ -215,36 +241,6 @@ class SessionPropertyRemoveOperator(bpy.types.Operator):
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):
bl_idname = "session.right"
bl_label = "Change owner to"
@ -450,7 +446,7 @@ def unregister():
# bpy.app.handlers.depsgraph_update_post.remove(depsgraph_update)
if client:
client.exit()
client.disconnect()
client = None
from bpy.utils import unregister_class

22
ui.py
View File

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