multi-user/multi_user/presence.py

583 lines
18 KiB
Python
Raw Normal View History

2020-03-20 21:56:50 +08:00
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# ##### END GPL LICENSE BLOCK #####
2019-08-14 20:25:20 +08:00
import copy
import logging
2019-08-14 04:09:36 +08:00
import math
import sys
2020-07-24 22:02:19 +08:00
import traceback
2019-08-14 04:09:36 +08:00
2019-09-30 19:35:50 +08:00
import bgl
import blf
import bpy
import gpu
import mathutils
2021-06-29 23:10:59 +08:00
# from mathutils import Vector
2019-04-01 18:28:13 +08:00
from bpy_extras import view3d_utils
from gpu_extras.batch import batch_for_shader
2020-10-06 21:53:29 +08:00
from replication.constants import (STATE_ACTIVE, STATE_AUTH, STATE_CONFIG,
STATE_INITIAL, CONNECTING,
2020-10-06 21:53:29 +08:00
STATE_LOBBY, STATE_QUITTING, STATE_SRV_SYNC,
STATE_SYNCING, STATE_WAITING)
2020-10-06 00:48:40 +08:00
from replication.interface import session
2019-09-27 04:30:22 +08:00
from .utils import find_from_attr, get_state_str, get_preferences
# Helper functions
def view3d_find() -> tuple:
2020-04-15 00:56:20 +08:00
""" Find the first 'VIEW_3D' windows found in areas
:return: tuple(Area, Region, RegionView3D)
"""
2019-04-01 18:28:13 +08:00
for area in bpy.data.window_managers[0].windows[0].screen.areas:
if area.type == 'VIEW_3D':
v3d = area.spaces[0]
rv3d = v3d.region_3d
for region in area.regions:
if region.type == 'WINDOW':
return area, region, rv3d
return None, None, None
2019-09-27 04:30:22 +08:00
def refresh_3d_view():
2020-04-15 00:56:20 +08:00
""" Refresh the viewport
"""
area, region, rv3d = view3d_find()
if area and region and rv3d:
area.tag_redraw()
2019-04-01 18:28:13 +08:00
2020-04-15 00:56:20 +08:00
def refresh_sidebar_view():
""" Refresh the blender viewport sidebar
2020-04-15 00:56:20 +08:00
"""
area, region, rv3d = view3d_find()
if area:
area.regions[3].tag_redraw()
2019-09-27 04:30:22 +08:00
def project_to_viewport(region: bpy.types.Region, rv3d: bpy.types.RegionView3D, coords: list, distance: float = 1.0) -> list:
""" Compute a projection from 2D to 3D viewport coordinate
2019-04-26 23:18:55 +08:00
:param region: target windows region
:type region: bpy.types.Region
:param rv3d: view 3D
:type rv3d: bpy.types.RegionView3D
:param coords: coordinate to project
:type coords: list
:param distance: distance offset into viewport
:type distance: float
:return: list of coordinates [x,y,z]
"""
target = [0, 0, 0]
if coords and region and rv3d:
view_vector = view3d_utils.region_2d_to_vector_3d(region, rv3d, coords)
ray_origin = view3d_utils.region_2d_to_origin_3d(region, rv3d, coords)
target = ray_origin + view_vector * distance
return [target.x, target.y, target.z]
def bbox_from_obj(obj: bpy.types.Object) -> list:
""" Generate a bounding box for a given object by using its world matrix
:param obj: target object
:type obj: bpy.types.Object
:return: list of 8 points [(x,y,z),...], list of 12 link between these points [(1,2),...]
"""
radius = 1.0 # Radius of the bounding box
vertex_indices = (
(0, 1), (0, 2), (1, 3), (2, 3),
(4, 5), (4, 6), (5, 7), (6, 7),
(0, 4), (1, 5), (2, 6), (3, 7))
if obj.type == 'EMPTY':
radius = obj.empty_display_size
elif obj.type == 'LIGHT':
radius = obj.data.shadow_soft_size
elif obj.type == 'LIGHT_PROBE':
radius = obj.data.influence_distance
elif obj.type == 'CAMERA':
radius = obj.data.display_size
elif hasattr(obj, 'bound_box'):
vertex_indices = (
(0, 1), (1, 2), (2, 3), (0, 3),
(4, 5), (5, 6), (6, 7), (4, 7),
(0, 4), (1, 5), (2, 6), (3, 7))
vertex_pos = get_bb_coords_from_obj(obj)
return vertex_pos, vertex_indices
coords = [
(-radius, -radius, -radius), (+radius, -radius, -radius),
(-radius, +radius, -radius), (+radius, +radius, -radius),
(-radius, -radius, +radius), (+radius, -radius, +radius),
(-radius, +radius, +radius), (+radius, +radius, +radius)]
base = obj.matrix_world
bbox_corners = [base @ mathutils.Vector(corner) for corner in coords]
vertex_pos = [(point.x, point.y, point.z) for point in bbox_corners]
return vertex_pos, vertex_indices
def bbox_from_instance_collection(ic: bpy.types.Object) -> list:
""" Generate a bounding box for a given instance collection by using its objects
:param ic: target instance collection
:type ic: bpy.types.Object
:param radius: bounding box radius
:type radius: float
:return: list of 8*objs points [(x,y,z),...], tuple of 12*objs link between these points [(1,2),...]
"""
vertex_pos = []
vertex_indices = ()
2021-06-23 23:45:01 +08:00
for obj_index, obj in enumerate(ic.instance_collection.objects):
vertex_pos_temp, vertex_indices_temp = bbox_from_obj(obj)
vertex_pos += vertex_pos_temp
vertex_indices_list_temp = list(list(indice) for indice in vertex_indices_temp)
for indice in vertex_indices_list_temp:
2021-06-23 23:45:01 +08:00
indice[0] += 8*obj_index
indice[1] += 8*obj_index
vertex_indices_temp = tuple(tuple(indice) for indice in vertex_indices_list_temp)
vertex_indices += vertex_indices_temp
bbox_corners = [ic.matrix_world @ mathutils.Vector(vertex) for vertex in vertex_pos]
vertex_pos = [(point.x, point.y, point.z) for point in bbox_corners]
return vertex_pos, vertex_indices
2019-04-01 18:28:13 +08:00
def generate_user_camera() -> list:
""" Generate a basic camera represention of the user point of view
:return: list of 7 points
"""
2019-04-01 18:28:13 +08:00
area, region, rv3d = view3d_find()
v1 = v2 = v3 = v4 = v5 = v6 = v7 = [0, 0, 0]
2019-04-01 18:28:13 +08:00
if area and region and rv3d:
width = region.width
height = region.height
v1 = project_to_viewport(region, rv3d, (0, 0))
v3 = project_to_viewport(region, rv3d, (0, height))
v2 = project_to_viewport(region, rv3d, (width, height))
v4 = project_to_viewport(region, rv3d, (width, 0))
2019-04-01 18:28:13 +08:00
v5 = project_to_viewport(region, rv3d, (width/2, height/2))
2019-08-14 20:25:20 +08:00
v6 = list(rv3d.view_location)
v7 = project_to_viewport(
region, rv3d, (width/2, height/2), distance=-.8)
2019-08-23 18:28:57 +08:00
coords = [v1, v2, v3, v4, v5, v6, v7]
2019-04-01 18:28:13 +08:00
return coords
def project_to_screen(coords: list) -> list:
""" Project 3D coordinate to 2D screen coordinates
:param coords: 3D coordinates (x,y,z)
:type coords: list
:return: list of 2D coordinates [x,y]
"""
2019-04-01 18:28:13 +08:00
area, region, rv3d = view3d_find()
if area and region and rv3d:
return view3d_utils.location_3d_to_region_2d(region, rv3d, coords)
else:
2019-04-18 21:05:48 +08:00
return (0, 0)
2019-04-01 18:28:13 +08:00
def get_bb_coords_from_obj(object: bpy.types.Object, instance: bpy.types.Object = None) -> list:
""" Generate bounding box in world coordinate from object bound box
:param object: target object
:type object: bpy.types.Object
:param instance: optionnal instance
:type instance: bpy.types.Object
:return: list of 8 points [(x,y,z),...]
"""
base = object.matrix_world
if instance:
scale = mathutils.Matrix.Diagonal(object.matrix_world.to_scale())
base = instance.matrix_world @ scale.to_4x4()
bbox_corners = [base @ mathutils.Vector(
corner) for corner in object.bound_box]
return [(point.x, point.y, point.z) for point in bbox_corners]
2019-08-23 18:28:57 +08:00
2020-01-22 06:24:44 +08:00
def get_view_matrix() -> list:
""" Return the 3d viewport view matrix
:return: view matrix as a 4x4 list
"""
2020-01-22 06:24:44 +08:00
area, region, rv3d = view3d_find()
if area and region and rv3d:
2020-04-01 17:34:24 +08:00
return [list(v) for v in rv3d.view_matrix]
2020-01-22 06:24:44 +08:00
class Widget(object):
2020-10-06 21:46:35 +08:00
""" Base class to define an interface element
"""
draw_type: str = 'POST_VIEW' # Draw event type
def poll(self) -> bool:
2020-10-06 21:46:35 +08:00
"""Test if the widget can be drawn or not
:return: bool
"""
return True
2021-06-29 23:10:59 +08:00
def configure_bgl(self):
bgl.glLineWidth(2.)
bgl.glEnable(bgl.GL_DEPTH_TEST)
bgl.glEnable(bgl.GL_BLEND)
bgl.glEnable(bgl.GL_LINE_SMOOTH)
def draw(self):
2020-10-06 21:46:35 +08:00
"""How to draw the widget
"""
raise NotImplementedError()
2020-10-06 05:38:52 +08:00
class UserFrustumWidget(Widget):
# Camera widget indices
indices = ((1, 3), (2, 1), (3, 0),
(2, 0), (4, 5), (1, 6),
(2, 6), (3, 6), (0, 6))
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):
location = self.data.get('view_corners')
shader = gpu.shader.from_builtin('3D_UNIFORM_COLOR')
positions = [tuple(coord) for coord in location]
if len(positions) != 7:
return
batch = batch_for_shader(
shader,
'LINES',
{"pos": positions},
indices=self.indices)
shader.bind()
shader.uniform_float("color", self.data.get('color'))
batch.draw(shader)
2019-09-27 04:30:22 +08:00
class UserSelectionWidget(Widget):
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
user_selection = self.data.get('selected_objects')
scene_current = self.data.get('scene_current')
return (scene_current == bpy.context.scene.name or
self.settings.presence_show_far_user) and \
user_selection and \
self.settings.presence_show_selected and \
self.settings.enable_presence
2021-07-01 17:58:52 +08:00
def draw(self):
user_selection = self.data.get('selected_objects')
for select_obj in user_selection:
obj = find_from_attr("uuid", select_obj, bpy.data.objects)
if not obj:
return
if obj.instance_collection:
vertex_pos, vertex_indices = bbox_from_instance_collection(obj)
else :
vertex_pos, vertex_indices = bbox_from_obj(obj)
shader = gpu.shader.from_builtin('3D_UNIFORM_COLOR')
batch = batch_for_shader(
shader,
'LINES',
2020-10-21 23:23:59 +08:00
{"pos": vertex_pos},
indices=vertex_indices)
shader.bind()
shader.uniform_float("color", self.data.get('color'))
batch.draw(shader)
2019-04-01 18:28:13 +08:00
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)
2021-06-24 22:01:14 +08:00
class UserModeWidget(Widget):
draw_type = 'POST_PIXEL'
2021-06-29 23:10:59 +08:00
2021-06-24 22:01:14 +08:00
def __init__(
self,
username):
self.username = username
self.settings = bpy.context.window_manager.session
2021-06-30 21:34:03 +08:00
self.preferences = get_preferences()
2021-06-24 22:01:14 +08:00
@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')
mode_current = self.data.get('mode_current')
user_selection = self.data.get('selected_objects')
return (scene_current == bpy.context.scene.name or
mode_current == bpy.context.mode or
self.settings.presence_show_far_user) and \
user_selection and \
2021-06-29 23:10:59 +08:00
self.settings.presence_show_mode and \
2021-06-24 22:01:14 +08:00
self.settings.enable_presence
def draw(self):
user_selection = self.data.get('selected_objects')
2021-07-01 17:58:52 +08:00
area, region, rv3d = view3d_find()
viewport_coord = project_to_viewport(region, rv3d, (0, 0))
2021-06-29 23:10:59 +08:00
2021-06-24 22:01:14 +08:00
for select_obj in user_selection:
obj = find_from_attr("uuid", select_obj, bpy.data.objects)
if not obj:
return
mode_current = self.data.get('mode_current')
color = self.data.get('color')
2021-06-29 23:10:59 +08:00
origin_coord = project_to_screen(obj.location)
2021-06-30 21:34:03 +08:00
distance_viewport_object = math.sqrt((viewport_coord[0]-obj.location[0])**2+(viewport_coord[1]-obj.location[1])**2+(viewport_coord[2]-obj.location[2])**2)
2021-06-29 23:10:59 +08:00
2021-06-30 21:34:03 +08:00
if distance_viewport_object > self.preferences.presence_mode_distance :
return
2021-06-29 23:10:59 +08:00
2021-06-30 21:34:03 +08:00
if origin_coord :
2021-06-29 23:10:59 +08:00
blf.position(0, origin_coord[0]+8, origin_coord[1]-15, 0)
2021-06-24 22:01:14 +08:00
blf.size(0, 16, 72)
blf.color(0, color[0], color[1], color[2], color[3])
2021-06-30 21:34:03 +08:00
blf.draw(0, mode_current)
2020-10-06 05:38:52 +08:00
class SessionStatusWidget(Widget):
draw_type = 'POST_PIXEL'
def __init__(self):
self.preferences = get_preferences()
2020-10-06 21:46:35 +08:00
@property
def settings(self):
return getattr(bpy.context.window_manager, 'session', None)
2020-10-06 05:38:52 +08:00
def poll(self):
2020-10-06 21:46:35 +08:00
return self.settings and self.settings.presence_show_session_status and \
self.settings.enable_presence
2020-10-06 05:38:52 +08:00
def draw(self):
text_scale = self.preferences.presence_hud_scale
ui_scale = bpy.context.preferences.view.ui_scale
2020-10-06 05:38:52 +08:00
color = [1, 1, 0, 1]
2021-03-04 21:22:54 +08:00
state = session.state
2020-10-06 05:38:52 +08:00
state_str = f"{get_state_str(state)}"
if state == STATE_ACTIVE:
color = [0, 1, 0, 1]
elif state == STATE_INITIAL:
color = [1, 0, 0, 1]
hpos = (self.preferences.presence_hud_hpos*bpy.context.area.width)/100
vpos = (self.preferences.presence_hud_vpos*bpy.context.area.height)/100
2020-10-06 05:38:52 +08:00
blf.position(0, hpos, vpos, 0)
blf.size(0, int(text_scale*ui_scale), 72)
2020-10-06 05:38:52 +08:00
blf.color(0, color[0], color[1], color[2], color[3])
blf.draw(0, state_str)
2019-09-24 20:42:59 +08:00
class DrawFactory(object):
def __init__(self):
self.post_view_handle = None
self.post_pixel_handle = None
2020-10-06 21:46:35 +08:00
self.widgets = {}
2019-04-01 18:28:13 +08:00
2020-10-06 21:53:29 +08:00
def add_widget(self, name: str, widget: Widget):
2020-10-06 21:46:35 +08:00
self.widgets[name] = widget
2020-10-06 21:46:35 +08:00
def remove_widget(self, name: str):
if name in self.widgets:
del self.widgets[name]
else:
logging.error(f"Widget {name} not existing")
2020-10-06 05:38:52 +08:00
def clear_widgets(self):
self.widgets.clear()
def register_handlers(self):
self.post_view_handle = bpy.types.SpaceView3D.draw_handler_add(
self.post_view_callback,
(),
'WINDOW',
'POST_VIEW')
self.post_pixel_handle = bpy.types.SpaceView3D.draw_handler_add(
self.post_pixel_callback,
(),
'WINDOW',
'POST_PIXEL')
2019-04-01 18:28:13 +08:00
def unregister_handlers(self):
if self.post_pixel_handle:
2019-04-01 18:28:13 +08:00
bpy.types.SpaceView3D.draw_handler_remove(
self.post_pixel_handle,
"WINDOW")
self.post_pixel_handle = None
2019-04-01 18:28:13 +08:00
if self.post_view_handle:
2019-04-01 18:28:13 +08:00
bpy.types.SpaceView3D.draw_handler_remove(
self.post_view_handle,
"WINDOW")
self.post_view_handle = None
2019-04-01 18:28:13 +08:00
def post_view_callback(self):
2019-04-15 23:08:05 +08:00
try:
2020-10-06 21:46:35 +08:00
for widget in self.widgets.values():
if widget.draw_type == 'POST_VIEW' and widget.poll():
2021-06-29 23:10:59 +08:00
widget.configure_bgl()
widget.draw()
except Exception as e:
2020-10-06 05:38:52 +08:00
logging.error(
f"Post view widget exception: {e} \n {traceback.print_exc()}")
def post_pixel_callback(self):
try:
2020-10-06 21:46:35 +08:00
for widget in self.widgets.values():
if widget.draw_type == 'POST_PIXEL' and widget.poll():
2021-06-29 23:10:59 +08:00
widget.configure_bgl()
widget.draw()
except Exception as e:
2020-10-06 05:38:52 +08:00
logging.error(
f"Post pixel widget Exception: {e} \n {traceback.print_exc()}")
this = sys.modules[__name__]
this.renderer = DrawFactory()
2019-08-23 18:28:57 +08:00
def register():
2020-10-06 05:38:52 +08:00
this.renderer.register_handlers()
2021-06-29 23:10:59 +08:00
2020-10-06 21:53:29 +08:00
this.renderer.add_widget("session_status", SessionStatusWidget())
2020-10-06 21:46:35 +08:00
def unregister():
2020-10-06 05:38:52 +08:00
this.renderer.unregister_handlers()
2020-10-06 21:53:29 +08:00
this.renderer.clear_widgets()