refactor: clean unused code, function renaming

This commit is contained in:
Swann Martinez 2019-05-15 11:25:43 +02:00
parent 4babda7805
commit 73107528dc
No known key found for this signature in database
GPG Key ID: 414CCAFD8DA720E1
3 changed files with 38 additions and 78 deletions

View File

@ -11,7 +11,7 @@ from enum import Enum
from random import randint from random import randint
import zmq import zmq
from . import draw, helpers, message from . import helpers, message
from .libs import dump_anything, umsgpack from .libs import dump_anything, umsgpack
# import zmq # import zmq
@ -24,7 +24,6 @@ logging.basicConfig(level=logging.INFO)
CONNECT_TIMEOUT = 2 CONNECT_TIMEOUT = 2
WAITING_TIME = 0.001 WAITING_TIME = 0.001
SERVER_MAX = 1 SERVER_MAX = 1
stop = False
DUMP_AGENTS_NUMBER = 1 DUMP_AGENTS_NUMBER = 1
class State(Enum): class State(Enum):
@ -68,7 +67,7 @@ class RCFClient(object):
self.stop_event= threading.Event() self.stop_event= threading.Event()
# Net agent # Net agent
self.net_agent = threading.Thread( self.net_agent = threading.Thread(
target=rcf_net_worker, args=(self.ctx, self.store, peer, self.serial_product,self.serial_feed, self.stop_event), name="net-agent") target=net_worker, args=(self.ctx, self.store, peer, self.serial_product,self.serial_feed, self.stop_event), name="net-agent")
self.net_agent.daemon = True self.net_agent.daemon = True
self.net_agent.start() self.net_agent.start()
@ -349,7 +348,7 @@ class RCFClientAgent(object):
self.pipe.send(umsgpack.packb(self.state.value)) self.pipe.send(umsgpack.packb(self.state.value))
def rcf_net_worker(ctx,store, pipe, serial_product, serial_feed, stop_event): def net_worker(ctx,store, pipe, serial_product, serial_feed, stop_event):
agent = RCFClientAgent(ctx,store, pipe) agent = RCFClientAgent(ctx,store, pipe)
server = None server = None
net_feed = serial_product net_feed = serial_product
@ -435,7 +434,6 @@ def rcf_net_worker(ctx,store, pipe, serial_product, serial_feed, stop_event):
logger.error("Fail to dump ") logger.error("Fail to dump ")
logger.info("exit thread") logger.info("exit thread")
stop = False
def serial_worker(serial_product, serial_feed): def serial_worker(serial_product, serial_feed):

View File

@ -369,6 +369,7 @@ def load_scene(target=None, data=None, create=False):
for object in target.collection.objects.keys(): for object in target.collection.objects.keys():
if object not in data["collection"]["objects"]: if object not in data["collection"]["objects"]:
target.collection.objects.unlink(bpy.data.objects[object]) target.collection.objects.unlink(bpy.data.objects[object])
# load collections # load collections
# TODO: Recursive link # TODO: Recursive link
logger.info("check for new collections") logger.info("check for new collections")
@ -553,6 +554,7 @@ def dump(key):
data = dump_datablock(target, 2) data = dump_datablock(target, 2)
dump_datablock_attibute( dump_datablock_attibute(
target, ['name', 'polygons', 'edges', 'vertices', 'id'], 6, data) target, ['name', 'polygons', 'edges', 'vertices', 'id'], 6, data)
# Fix material index # Fix material index
m_list = [] m_list = []
for m in target.materials: for m in target.materials:

View File

@ -9,10 +9,8 @@ import time
from operator import itemgetter from operator import itemgetter
import queue import queue
import bgl
import blf
import bpy import bpy
import gpu
import mathutils import mathutils
from bpy_extras import view3d_utils from bpy_extras import view3d_utils
from gpu_extras.batch import batch_for_shader from gpu_extras.batch import batch_for_shader
@ -37,7 +35,6 @@ history = queue.Queue()
# UTILITY FUNCTIONS # UTILITY FUNCTIONS
def client_list_callback(scene, context): def client_list_callback(scene, context):
global client_keys global client_keys
items = [("Common", "Common", "")] items = [("Common", "Common", "")]
username = bpy.context.window_manager.session_settings.username username = bpy.context.window_manager.session_settings.username
@ -103,7 +100,7 @@ def upload_client_instance_position():
pass pass
def update_selected_object(context): def update_client_selected_object(context):
global client_instance global client_instance
session = bpy.context.window_manager.session_settings session = bpy.context.window_manager.session_settings
@ -126,10 +123,10 @@ def update_selected_object(context):
client_data[0][1]['active_objects'] = [] client_data[0][1]['active_objects'] = []
client_instance.set(client_key, client_data[0][1]) client_instance.set(client_key, client_data[0][1])
# Init configuration
def init_datablocks(): def init_datablocks():
global client_instance global client_instance
for datatype in helpers.SUPPORTED_TYPES: for datatype in helpers.SUPPORTED_TYPES:
for item in getattr(bpy.data, helpers.CORRESPONDANCE[datatype]): for item in getattr(bpy.data, helpers.CORRESPONDANCE[datatype]):
item.id = bpy.context.window_manager.session_settings.username item.id = bpy.context.window_manager.session_settings.username
@ -137,7 +134,6 @@ def init_datablocks():
client_instance.set(key) client_instance.set(key)
def refresh_session_data(): def refresh_session_data():
global client_instance, client_keys, client_state global client_instance, client_keys, client_state
@ -153,46 +149,23 @@ def refresh_session_data():
def default_tick(): def default_tick():
refresh_session_data() refresh_session_data()
upload_client_instance_position() upload_client_instance_position()
return .2 return .2
def undo_test():
print("UNDO")
bpy.ops.ed.undo()
def undo_tick():
global history
try:
c = history.get_nowait()
if c:
undo_test()
except Exception as e:
pass
return 1.0
def register_ticks(): def register_ticks():
# REGISTER Updaters # REGISTER Updaters
bpy.app.timers.register(default_tick) bpy.app.timers.register(default_tick)
bpy.app.timers.register(undo_tick)
def unregister_ticks(): def unregister_ticks():
# REGISTER Updaters # REGISTER Updaters
bpy.app.timers.unregister(default_tick) bpy.app.timers.unregister(default_tick)
bpy.app.timers.unregister(undo_tick)
# OPERATORS # OPERATORS
class session_join(bpy.types.Operator): class session_join(bpy.types.Operator):
bl_idname = "session.join" bl_idname = "session.join"
bl_label = "join" bl_label = "join"
bl_description = "connect to a net server" bl_description = "connect to a net server"
@ -329,11 +302,10 @@ class session_create(bpy.types.Operator):
net_settings = context.window_manager.session_settings net_settings = context.window_manager.session_settings
script = os.path.join(os.path.dirname(os.path.abspath(__file__)),"server.py") script_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)),"server.py")
server = subprocess.Popen( server = subprocess.Popen(
[str(python_path),script],shell=False, stdout=subprocess.PIPE) [str(python_path),script_dir],shell=False, stdout=subprocess.PIPE)
# time.sleep(0.1)
bpy.ops.session.join() bpy.ops.session.join()
@ -365,18 +337,20 @@ class session_stop(bpy.types.Operator):
if server: if server:
server.kill() server.kill()
time.sleep(0.25) time.sleep(0.25)
server = None server = None
if client_instance: if client_instance:
client_instance.exit() client_instance.exit()
time.sleep(0.25) time.sleep(0.25)
del client_instance del client_instance
client_instance = None client_instance = None
client_keys = None client_keys = None
net_settings.is_admin = False net_settings.is_admin = False
client_state = 1 client_state = 1
unregister_ticks()
# Stop drawing unregister_ticks()
draw.renderer.stop() draw.renderer.stop()
else: else:
logger.debug("No server/client_instance running.") logger.debug("No server/client_instance running.")
@ -429,14 +403,21 @@ class session_rights(bpy.types.Operator):
class session_settings(bpy.types.PropertyGroup): class session_settings(bpy.types.PropertyGroup):
username = bpy.props.StringProperty( username = bpy.props.StringProperty(
name="Username", default="user_{}".format(randomStringDigits())) name="Username",
default="user_{}".format(randomStringDigits())
)
ip = bpy.props.StringProperty( ip = bpy.props.StringProperty(
name="ip", description='Distant host ip', default="127.0.0.1") name="ip",
description='Distant host ip',
default="127.0.0.1")
port = bpy.props.IntProperty( port = bpy.props.IntProperty(
name="port", description='Distant host port', default=5555) name="port",
description='Distant host port',
default=5555)
add_property_depth = bpy.props.IntProperty( add_property_depth = bpy.props.IntProperty(
name="add_property_depth", default=1) name="add_property_depth",
default=1)
buffer = bpy.props.StringProperty(name="None") buffer = bpy.props.StringProperty(name="None")
is_admin = bpy.props.BoolProperty(name="is_admin", default=False) is_admin = bpy.props.BoolProperty(name="is_admin", default=False)
load_data = bpy.props.BoolProperty(name="load_data", default=True) load_data = bpy.props.BoolProperty(name="load_data", default=True)
@ -453,16 +434,19 @@ class session_settings(bpy.types.PropertyGroup):
('HOST', 'hosting', 'host a session'), ('HOST', 'hosting', 'host a session'),
('CONNECT', 'connexion', 'connect to a session')}, ('CONNECT', 'connexion', 'connect to a session')},
default='HOST') default='HOST')
client_color = bpy.props.FloatVectorProperty(name="client_instance_color", client_color = bpy.props.FloatVectorProperty(
subtype='COLOR', name="client_instance_color",
default=randomColor()) subtype='COLOR',
default=randomColor())
clients = bpy.props.EnumProperty( clients = bpy.props.EnumProperty(
name="clients", name="clients",
description="client enum", description="client enum",
items=client_list_callback items=client_list_callback
) )
enable_draw = bpy.props.BoolProperty( enable_draw = bpy.props.BoolProperty(
name="enable_draw", description='Enable overlay drawing module', default=True) name="enable_draw",
description='Enable overlay drawing module',
default=True)
class session_snapview(bpy.types.Operator): class session_snapview(bpy.types.Operator):
@ -509,18 +493,6 @@ classes = (
) )
def ordered(updates):
# sorted = sorted(updates, key=lambda tup: hepers.SUPPORTED_TYPES.index(tup[1].id.bl_rna.name))
uplist = []
for item in updates.items():
if item[1].id.bl_rna.name in hepers.SUPPORTED_TYPES:
uplist.append((hepers.SUPPORTED_TYPES.index(
item[1].id.bl_rna.name), item[1].id.bl_rna.name, item[1].id.name, item[1].id))
uplist.sort(key=itemgetter(0))
return uplist
def is_replicated(update): def is_replicated(update):
# global client_keys # global client_keys
# dickt = dict(client_keys) # dickt = dict(client_keys)
@ -544,8 +516,7 @@ def is_replicated(update):
logger.info("{} Not rep".format(key)) logger.info("{} Not rep".format(key))
return False return False
def get_datablock(update,context): def get_datablock_from_update(update,context):
item_type = update.id.__class__.__name__ item_type = update.id.__class__.__name__
item_id = update.id.name item_id = update.id.name
@ -563,7 +534,7 @@ def get_datablock(update,context):
return datablock_ref return datablock_ref
def toogle_dirty(context, update): def toogle_dirty(context, update):
data_ref = get_datablock(update,context) data_ref = get_datablock_from_update(update,context)
if data_ref: if data_ref:
logger.info(update.id.bl_rna.__class__.__name__) logger.info(update.id.bl_rna.__class__.__name__)
@ -590,7 +561,7 @@ def depsgraph_update(scene):
if update.id.id == username or update.id.id == 'Common': if update.id.id == username or update.id.id == 'Common':
toogle_dirty(ctx, update) toogle_dirty(ctx, update)
else: else:
item = get_datablock(update,ctx) item = get_datablock_from_update(update,ctx)
#get parent authority #get parent authority
if hasattr(item,"id"): if hasattr(item,"id"):
@ -609,23 +580,12 @@ def depsgraph_update(scene):
logger.info("APPEND {}".format(key)) logger.info("APPEND {}".format(key))
else: else:
try: try:
item = get_datablock(update,ctx)
print(item)
getattr(bpy.data, helpers.CORRESPONDANCE[update.id.__class__.__name__]).remove(item) getattr(bpy.data, helpers.CORRESPONDANCE[update.id.__class__.__name__]).remove(item)
except: except:
pass pass
break break
update_selected_object(ctx) update_client_selected_object(ctx)
# if update.id.id == username or update.id.id == 'Common' or update.id.id == 'None':
# # TODO: handle errors
# data_ref = get_datablock(update,context)
# if data_ref:
# logger.info(update.id.bl_rna.__class__.__name__)
# data_ref.is_dirty= True
# elif update.id.id != username:
# history.put("undo")
def register(): def register():