refactor: enable username display again
refactor: avoid to draw the local user
This commit is contained in:
parent
ae34846509
commit
022b7f7822
@ -19,7 +19,15 @@ import logging
|
||||
|
||||
import bpy
|
||||
|
||||
from . import presence, utils
|
||||
from . import utils
|
||||
from .presence import (renderer,
|
||||
UserFrustumWidget,
|
||||
UserNameWidget,
|
||||
UserSelectionWidget,
|
||||
refresh_3d_view,
|
||||
generate_user_camera,
|
||||
get_view_matrix,
|
||||
refresh_sidebar_view)
|
||||
from replication.constants import (FETCHED,
|
||||
UP,
|
||||
RP_COMMON,
|
||||
@ -33,9 +41,11 @@ from replication.constants import (FETCHED,
|
||||
|
||||
from replication.interface import session
|
||||
|
||||
|
||||
class Delayable():
|
||||
"""Delayable task interface
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self.is_registered = False
|
||||
|
||||
@ -63,13 +73,14 @@ class Timer(Delayable):
|
||||
def register(self):
|
||||
"""Register the timer into the blender timer system
|
||||
"""
|
||||
|
||||
|
||||
if not self.is_registered:
|
||||
bpy.app.timers.register(self.main)
|
||||
self.is_registered = True
|
||||
logging.debug(f"Register {self.__class__.__name__}")
|
||||
else:
|
||||
logging.debug(f"Timer {self.__class__.__name__} already registered")
|
||||
logging.debug(
|
||||
f"Timer {self.__class__.__name__} already registered")
|
||||
|
||||
def main(self):
|
||||
self.execute()
|
||||
@ -219,7 +230,6 @@ class ClientUpdate(Timer):
|
||||
|
||||
def execute(self):
|
||||
settings = utils.get_preferences()
|
||||
renderer = getattr(presence, 'renderer', None)
|
||||
|
||||
if session and renderer:
|
||||
if session.state['STATE'] in [STATE_ACTIVE, STATE_LOBBY]:
|
||||
@ -238,7 +248,7 @@ class ClientUpdate(Timer):
|
||||
if cached_user_data is None:
|
||||
self.users_metadata[username] = user_data['metadata']
|
||||
elif 'view_matrix' in cached_user_data and 'view_matrix' in new_user_data and cached_user_data['view_matrix'] != new_user_data['view_matrix']:
|
||||
presence.refresh_3d_view()
|
||||
refresh_3d_view()
|
||||
self.users_metadata[username] = user_data['metadata']
|
||||
break
|
||||
else:
|
||||
@ -247,13 +257,13 @@ class ClientUpdate(Timer):
|
||||
local_user_metadata = local_user.get('metadata')
|
||||
scene_current = bpy.context.scene.name
|
||||
local_user = session.online_users.get(settings.username)
|
||||
current_view_corners = presence.generate_user_camera()
|
||||
current_view_corners = generate_user_camera()
|
||||
|
||||
# Init client metadata
|
||||
if not local_user_metadata or 'color' not in local_user_metadata.keys():
|
||||
metadata = {
|
||||
'view_corners': presence.get_view_matrix(),
|
||||
'view_matrix': presence.get_view_matrix(),
|
||||
'view_corners': get_view_matrix(),
|
||||
'view_matrix': get_view_matrix(),
|
||||
'color': (settings.client_color.r,
|
||||
settings.client_color.g,
|
||||
settings.client_color.b,
|
||||
@ -270,7 +280,7 @@ class ClientUpdate(Timer):
|
||||
session.update_user_metadata(local_user_metadata)
|
||||
elif 'view_corners' in local_user_metadata and current_view_corners != local_user_metadata['view_corners']:
|
||||
local_user_metadata['view_corners'] = current_view_corners
|
||||
local_user_metadata['view_matrix'] = presence.get_view_matrix(
|
||||
local_user_metadata['view_matrix'] = get_view_matrix(
|
||||
)
|
||||
session.update_user_metadata(local_user_metadata)
|
||||
|
||||
@ -280,16 +290,15 @@ class SessionStatusUpdate(Timer):
|
||||
super().__init__(timout)
|
||||
|
||||
def execute(self):
|
||||
presence.refresh_sidebar_view()
|
||||
refresh_sidebar_view()
|
||||
|
||||
|
||||
class SessionUserSync(Timer):
|
||||
def __init__(self, timout=1):
|
||||
super().__init__(timout)
|
||||
self.settings = utils.get_preferences()
|
||||
|
||||
def execute(self):
|
||||
renderer = getattr(presence, 'renderer', None)
|
||||
|
||||
if session and renderer:
|
||||
# sync online users
|
||||
session_users = session.online_users
|
||||
@ -305,16 +314,17 @@ class SessionUserSync(Timer):
|
||||
new_key = ui_users.add()
|
||||
new_key.name = user
|
||||
new_key.username = user
|
||||
presence.renderer.register(presence.UserWidget(user))
|
||||
presence.renderer.register(presence.UserSelectionWidget(user))
|
||||
|
||||
if user != self.settings.username:
|
||||
renderer.register(UserFrustumWidget(user))
|
||||
renderer.register(UserSelectionWidget(user))
|
||||
renderer.register(UserNameWidget(user))
|
||||
|
||||
|
||||
class MainThreadExecutor(Timer):
|
||||
def __init__(self, timout=1, execution_queue=None):
|
||||
super().__init__(timout)
|
||||
self.execution_queue = execution_queue
|
||||
|
||||
|
||||
def execute(self):
|
||||
while not self.execution_queue.empty():
|
||||
function = self.execution_queue.get()
|
||||
|
@ -185,18 +185,15 @@ def get_view_matrix() -> list:
|
||||
|
||||
|
||||
class Widget(object):
|
||||
draw_type = 'POST_VIEW'
|
||||
|
||||
def poll(self) -> bool:
|
||||
return True
|
||||
|
||||
def draw(self):
|
||||
raise NotImplementedError()
|
||||
|
||||
|
||||
class ViewportWidget(Widget):
|
||||
pass
|
||||
|
||||
|
||||
class UserWidget(Widget):
|
||||
class UserFrustumWidget(Widget):
|
||||
# Camera widget indices
|
||||
indices = ((1, 3), (2, 1), (3, 0),
|
||||
(2, 0), (4, 5), (1, 6),
|
||||
@ -312,7 +309,7 @@ class UserSelectionWidget(Widget):
|
||||
(4, 5), (4, 6), (5, 7), (6, 7),
|
||||
(0, 4), (1, 5), (2, 6), (3, 7))
|
||||
|
||||
positions = get_default_bbox(ob, ob.scale.x)
|
||||
positions = bbox_from_obj(ob, ob.scale.x)
|
||||
|
||||
shader = gpu.shader.from_builtin('3D_UNIFORM_COLOR')
|
||||
batch = batch_for_shader(
|
||||
@ -326,10 +323,51 @@ class UserSelectionWidget(Widget):
|
||||
batch.draw(shader)
|
||||
|
||||
|
||||
class UserNameWidget(Widget):
|
||||
draw_type = 'POST_PIXEL'
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
username):
|
||||
self.username = username
|
||||
self.settings = bpy.context.window_manager.session
|
||||
|
||||
@property
|
||||
def data(self):
|
||||
user = session.online_users.get(self.username)
|
||||
if user:
|
||||
return user.get('metadata')
|
||||
else:
|
||||
return None
|
||||
|
||||
def poll(self):
|
||||
if self.data is None:
|
||||
return False
|
||||
|
||||
scene_current = self.data.get('scene_current')
|
||||
view_corners = self.data.get('view_corners')
|
||||
|
||||
return (scene_current == bpy.context.scene.name or
|
||||
self.settings.presence_show_far_user) and \
|
||||
view_corners and \
|
||||
self.settings.presence_show_user and \
|
||||
self.settings.enable_presence
|
||||
|
||||
|
||||
def draw(self):
|
||||
view_corners = self.data.get('view_corners')
|
||||
color = self.data.get('color')
|
||||
position = [tuple(coord) for coord in view_corners]
|
||||
coords = project_to_screen(position[1])
|
||||
|
||||
if coords:
|
||||
blf.position(0, coords[0], coords[1]+10, 0)
|
||||
blf.size(0, 16, 72)
|
||||
blf.color(0, color[0], color[1], color[2], color[3])
|
||||
blf.draw(0, self.username)
|
||||
|
||||
class DrawFactory(object):
|
||||
def __init__(self):
|
||||
self.d3d_items = {}
|
||||
self.d2d_items = {}
|
||||
self.post_view_handle = None
|
||||
self.post_pixel_handle = None
|
||||
self.draw_event = None
|
||||
@ -345,50 +383,44 @@ class DrawFactory(object):
|
||||
|
||||
def register_handlers(self):
|
||||
self.post_view_handle = bpy.types.SpaceView3D.draw_handler_add(
|
||||
self.post_view_callback, (), 'WINDOW', 'POST_VIEW')
|
||||
self.post_view_callback,
|
||||
(),
|
||||
'WINDOW',
|
||||
'POST_VIEW')
|
||||
self.post_pixel_handle = bpy.types.SpaceView3D.draw_handler_add(
|
||||
self.post_pixel_callback, (), 'WINDOW', 'POST_PIXEL')
|
||||
self.post_pixel_callback,
|
||||
(),
|
||||
'WINDOW',
|
||||
'POST_PIXEL')
|
||||
|
||||
def unregister_handlers(self):
|
||||
if self.post_pixel_handle:
|
||||
bpy.types.SpaceView3D.draw_handler_remove(
|
||||
self.post_pixel_handle, "WINDOW")
|
||||
self.post_pixel_handle,
|
||||
"WINDOW")
|
||||
self.post_pixel_handle = None
|
||||
|
||||
if self.post_view_handle:
|
||||
bpy.types.SpaceView3D.draw_handler_remove(
|
||||
self.post_view_handle, "WINDOW")
|
||||
self.post_view_handle,
|
||||
"WINDOW")
|
||||
self.post_view_handle = None
|
||||
|
||||
self.d3d_items.clear()
|
||||
self.d2d_items.clear()
|
||||
|
||||
def post_view_callback(self):
|
||||
bgl.glLineWidth(2.)
|
||||
bgl.glEnable(bgl.GL_DEPTH_TEST)
|
||||
bgl.glEnable(bgl.GL_BLEND)
|
||||
bgl.glEnable(bgl.GL_LINE_SMOOTH)
|
||||
|
||||
try:
|
||||
for widget in self.widgets:
|
||||
if widget.poll():
|
||||
if widget.draw_type == 'POST_VIEW' and widget.poll():
|
||||
widget.draw()
|
||||
except Exception as e:
|
||||
logging.error(f"3D Exception: {e} \n {traceback.print_exc()}")
|
||||
logging.error(f"Post view widget exception: {e} \n {traceback.print_exc()}")
|
||||
|
||||
def post_pixel_callback(self):
|
||||
for position, font, color in self.d2d_items.values():
|
||||
try:
|
||||
coords = project_to_screen(position)
|
||||
|
||||
if coords:
|
||||
blf.position(0, coords[0], coords[1]+10, 0)
|
||||
blf.size(0, 16, 72)
|
||||
blf.color(0, color[0], color[1], color[2], color[3])
|
||||
blf.draw(0, font)
|
||||
|
||||
except Exception:
|
||||
logging.error(f"2D Exception: {e} \n {traceback.print_exc()}")
|
||||
try:
|
||||
for widget in self.widgets:
|
||||
if widget.draw_type == 'POST_PIXEL' and widget.poll():
|
||||
widget.draw()
|
||||
except Exception as e:
|
||||
logging.error(f"Post pixel widget Exception: {e} \n {traceback.print_exc()}")
|
||||
|
||||
|
||||
this = sys.modules[__name__]
|
||||
|
Loading…
Reference in New Issue
Block a user