2019-08-14 20:25:20 +08:00
|
|
|
import copy
|
2019-08-28 18:57:09 +08:00
|
|
|
import logging
|
2019-08-14 04:09:36 +08:00
|
|
|
import math
|
|
|
|
|
2019-09-30 19:35:50 +08:00
|
|
|
import bgl
|
|
|
|
import blf
|
|
|
|
import bpy
|
|
|
|
import gpu
|
|
|
|
import mathutils
|
2019-04-01 18:28:13 +08:00
|
|
|
from bpy_extras import view3d_utils
|
|
|
|
from gpu_extras.batch import batch_for_shader
|
|
|
|
|
2019-08-28 19:48:43 +08:00
|
|
|
from . import utils
|
2019-04-18 21:05:48 +08:00
|
|
|
|
2019-08-14 03:32:15 +08:00
|
|
|
global renderer
|
2019-08-23 18:28:57 +08:00
|
|
|
|
2019-08-28 18:57:09 +08:00
|
|
|
logger = logging.getLogger(__name__)
|
2019-08-23 18:28:57 +08:00
|
|
|
|
2019-09-27 04:30:22 +08:00
|
|
|
|
2019-04-01 18:28:13 +08:00
|
|
|
def view3d_find():
|
|
|
|
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
|
|
|
|
2019-09-16 04:43:33 +08:00
|
|
|
def refresh_3d_view():
|
|
|
|
area, region, rv3d = view3d_find()
|
2019-09-27 04:30:22 +08:00
|
|
|
|
2019-09-16 04:43:33 +08:00
|
|
|
area.tag_redraw()
|
2019-04-01 18:28:13 +08:00
|
|
|
|
2019-09-27 04:30:22 +08:00
|
|
|
|
2019-04-01 18:28:13 +08:00
|
|
|
def get_target(region, rv3d, coord):
|
|
|
|
target = [0, 0, 0]
|
|
|
|
|
|
|
|
if coord and region and rv3d:
|
|
|
|
view_vector = view3d_utils.region_2d_to_vector_3d(region, rv3d, coord)
|
|
|
|
ray_origin = view3d_utils.region_2d_to_origin_3d(region, rv3d, coord)
|
|
|
|
target = ray_origin + view_vector
|
|
|
|
|
|
|
|
return [target.x, target.y, target.z]
|
|
|
|
|
2019-04-26 23:18:55 +08:00
|
|
|
|
2019-04-26 23:03:17 +08:00
|
|
|
def get_target_far(region, rv3d, coord, distance):
|
|
|
|
target = [0, 0, 0]
|
|
|
|
|
|
|
|
if coord and region and rv3d:
|
|
|
|
view_vector = view3d_utils.region_2d_to_vector_3d(region, rv3d, coord)
|
|
|
|
ray_origin = view3d_utils.region_2d_to_origin_3d(region, rv3d, coord)
|
|
|
|
target = ray_origin + view_vector * distance
|
|
|
|
|
|
|
|
return [target.x, target.y, target.z]
|
|
|
|
|
2019-04-01 18:28:13 +08:00
|
|
|
|
2019-08-14 20:25:20 +08:00
|
|
|
def get_client_cam_points():
|
2019-04-01 18:28:13 +08:00
|
|
|
area, region, rv3d = view3d_find()
|
|
|
|
|
|
|
|
v1 = [0, 0, 0]
|
|
|
|
v2 = [0, 0, 0]
|
|
|
|
v3 = [0, 0, 0]
|
|
|
|
v4 = [0, 0, 0]
|
2019-04-26 23:03:17 +08:00
|
|
|
v5 = [0, 0, 0]
|
|
|
|
v6 = [0, 0, 0]
|
2019-04-01 18:28:13 +08:00
|
|
|
|
|
|
|
if area and region and rv3d:
|
|
|
|
width = region.width
|
|
|
|
height = region.height
|
|
|
|
|
|
|
|
v1 = get_target(region, rv3d, (0, 0))
|
|
|
|
v3 = get_target(region, rv3d, (0, height))
|
|
|
|
v2 = get_target(region, rv3d, (width, height))
|
|
|
|
v4 = get_target(region, rv3d, (width, 0))
|
|
|
|
|
2019-04-26 23:03:17 +08:00
|
|
|
v5 = get_target(region, rv3d, (width/2, height/2))
|
2019-08-14 20:25:20 +08:00
|
|
|
v6 = list(rv3d.view_location)
|
|
|
|
v7 = get_target_far(region, rv3d, (width/2, height/2), -.8)
|
2019-04-26 23:03:17 +08:00
|
|
|
|
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 get_client_2d(coords):
|
|
|
|
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
|
|
|
|
2019-08-23 18:28:57 +08:00
|
|
|
|
2019-08-08 23:17:58 +08:00
|
|
|
class User():
|
2019-08-23 18:28:57 +08:00
|
|
|
def __init__(self, username=None, color=(0, 0, 0, 1)):
|
2019-09-16 17:42:53 +08:00
|
|
|
self.is_dirty = False
|
2019-08-08 23:17:58 +08:00
|
|
|
self.name = username
|
|
|
|
self.color = color
|
2019-08-23 18:28:57 +08:00
|
|
|
self.location = [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]
|
2019-08-28 19:48:43 +08:00
|
|
|
self.selected_objects = []
|
2019-10-02 20:02:30 +08:00
|
|
|
self.last_select_objects = []
|
2019-08-14 00:00:54 +08:00
|
|
|
|
|
|
|
def update_location(self):
|
2019-08-14 20:25:20 +08:00
|
|
|
current_coords = get_client_cam_points()
|
2019-08-14 04:09:36 +08:00
|
|
|
area, region, rv3d = view3d_find()
|
2019-08-14 00:00:54 +08:00
|
|
|
|
2019-10-02 20:02:30 +08:00
|
|
|
current_coords = list(get_client_cam_points())
|
|
|
|
if current_coords and self.location != current_coords:
|
|
|
|
self.location = current_coords
|
2019-08-14 00:00:54 +08:00
|
|
|
|
2019-08-28 19:48:43 +08:00
|
|
|
def update_selected_objects(self, context):
|
|
|
|
self.selected_objects = utils.get_selected_objects(context.scene)
|
2019-08-08 23:17:58 +08:00
|
|
|
|
2019-09-24 20:42:59 +08:00
|
|
|
def update_presence(self, context):
|
2019-09-27 04:30:22 +08:00
|
|
|
global renderer
|
2019-09-27 20:50:00 +08:00
|
|
|
|
2019-09-27 04:30:22 +08:00
|
|
|
if renderer and self.enable_presence:
|
|
|
|
renderer.run()
|
|
|
|
else:
|
|
|
|
renderer.stop()
|
|
|
|
|
2019-04-01 18:28:13 +08:00
|
|
|
|
2019-09-24 20:42:59 +08:00
|
|
|
def update_overlay_settings(self, context):
|
2019-09-27 04:30:22 +08:00
|
|
|
global renderer
|
|
|
|
|
|
|
|
if renderer and not self.presence_show_selected:
|
|
|
|
renderer.flush_selection()
|
|
|
|
if renderer and not self.presence_show_user:
|
|
|
|
renderer.flush_users()
|
|
|
|
|
2019-04-01 18:28:13 +08:00
|
|
|
|
2019-09-24 20:42:59 +08:00
|
|
|
class DrawFactory(object):
|
2019-05-02 22:55:33 +08:00
|
|
|
def __init__(self):
|
2019-04-17 17:49:27 +08:00
|
|
|
self.d3d_items = {}
|
|
|
|
self.d2d_items = {}
|
2019-04-01 18:28:13 +08:00
|
|
|
self.draw3d_handle = None
|
|
|
|
self.draw2d_handle = None
|
|
|
|
self.draw_event = None
|
|
|
|
self.coords = None
|
|
|
|
self.active_object = None
|
|
|
|
|
2019-05-02 23:52:32 +08:00
|
|
|
def run(self):
|
2019-05-02 22:55:33 +08:00
|
|
|
self.register_handlers()
|
2019-08-23 18:28:57 +08:00
|
|
|
|
2019-05-02 23:52:32 +08:00
|
|
|
def stop(self):
|
|
|
|
self.unregister_handlers()
|
2019-08-23 18:28:57 +08:00
|
|
|
|
2019-04-01 20:53:59 +08:00
|
|
|
def register_handlers(self):
|
2019-04-01 18:28:13 +08:00
|
|
|
self.draw3d_handle = bpy.types.SpaceView3D.draw_handler_add(
|
|
|
|
self.draw3d_callback, (), 'WINDOW', 'POST_VIEW')
|
|
|
|
self.draw2d_handle = bpy.types.SpaceView3D.draw_handler_add(
|
|
|
|
self.draw2d_callback, (), 'WINDOW', 'POST_PIXEL')
|
|
|
|
|
2019-04-01 20:53:59 +08:00
|
|
|
def unregister_handlers(self):
|
2019-04-01 18:28:13 +08:00
|
|
|
if self.draw2d_handle:
|
|
|
|
bpy.types.SpaceView3D.draw_handler_remove(
|
|
|
|
self.draw2d_handle, "WINDOW")
|
|
|
|
self.draw2d_handle = None
|
|
|
|
|
|
|
|
if self.draw3d_handle:
|
|
|
|
bpy.types.SpaceView3D.draw_handler_remove(
|
|
|
|
self.draw3d_handle, "WINDOW")
|
|
|
|
self.draw3d_handle = None
|
|
|
|
|
2019-04-15 23:08:05 +08:00
|
|
|
self.d3d_items.clear()
|
|
|
|
self.d2d_items.clear()
|
|
|
|
|
2019-10-02 20:02:30 +08:00
|
|
|
def flush_selection(self, user):
|
2019-08-28 19:48:43 +08:00
|
|
|
key_to_remove = []
|
2019-10-02 20:02:30 +08:00
|
|
|
select_key = "{}_select".format(user) if user else "select"
|
2019-08-28 19:48:43 +08:00
|
|
|
for k in self.d3d_items.keys():
|
2019-10-02 20:02:30 +08:00
|
|
|
|
|
|
|
if select_key in k:
|
2019-08-28 19:48:43 +08:00
|
|
|
key_to_remove.append(k)
|
2019-09-27 04:30:22 +08:00
|
|
|
|
2019-09-24 20:42:59 +08:00
|
|
|
for k in key_to_remove:
|
|
|
|
del self.d3d_items[k]
|
2019-08-28 19:48:43 +08:00
|
|
|
|
2019-09-24 20:42:59 +08:00
|
|
|
def flush_users(self):
|
|
|
|
key_to_remove = []
|
|
|
|
for k in self.d3d_items.keys():
|
|
|
|
if "select" not in k:
|
|
|
|
key_to_remove.append(k)
|
2019-09-27 04:30:22 +08:00
|
|
|
|
2019-08-28 19:48:43 +08:00
|
|
|
for k in key_to_remove:
|
|
|
|
del self.d3d_items[k]
|
2019-09-27 04:30:22 +08:00
|
|
|
|
2019-09-24 20:42:59 +08:00
|
|
|
self.d2d_items.clear()
|
|
|
|
|
|
|
|
def draw_client_selection(self, client_uuid, client_color, client_selection):
|
2019-09-30 22:12:19 +08:00
|
|
|
local_user = bpy.context.window_manager.session.user_uuid
|
2019-09-27 04:30:22 +08:00
|
|
|
|
2019-10-02 20:02:30 +08:00
|
|
|
if local_user != client_uuid:
|
|
|
|
self.flush_selection(client_uuid)
|
|
|
|
|
2019-08-28 19:48:43 +08:00
|
|
|
for select_ob in client_selection:
|
|
|
|
drawable_key = "{}_select_{}".format(client_uuid, select_ob)
|
|
|
|
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)
|
|
|
|
)
|
2019-10-09 03:48:45 +08:00
|
|
|
ob = utils.find_from_attr("uuid",select_ob,bpy.data.objects)
|
|
|
|
if not ob:
|
2019-08-28 19:48:43 +08:00
|
|
|
return
|
|
|
|
|
|
|
|
bbox_corners = [ob.matrix_world @ mathutils.Vector(
|
|
|
|
corner) for corner in ob.bound_box]
|
2019-05-02 20:46:31 +08:00
|
|
|
|
2019-08-28 19:48:43 +08:00
|
|
|
coords = [(point.x, point.y, point.z)
|
2019-09-27 04:30:22 +08:00
|
|
|
for point in bbox_corners]
|
2019-08-28 19:48:43 +08:00
|
|
|
shader = gpu.shader.from_builtin('3D_UNIFORM_COLOR')
|
|
|
|
color = client_color
|
|
|
|
batch = batch_for_shader(
|
|
|
|
shader, 'LINES', {"pos": coords}, indices=indices)
|
2019-05-02 23:52:32 +08:00
|
|
|
|
2019-08-28 19:48:43 +08:00
|
|
|
self.d3d_items[drawable_key] = (shader, batch, color)
|
2019-09-27 04:30:22 +08:00
|
|
|
|
2019-08-23 18:28:57 +08:00
|
|
|
def draw_client_camera(self, client_uuid, client_location, client_color):
|
2019-08-14 20:25:20 +08:00
|
|
|
if client_location:
|
2019-09-30 22:12:19 +08:00
|
|
|
local_user = bpy.context.window_manager.session.user_uuid
|
2019-04-17 22:15:21 +08:00
|
|
|
|
2019-09-30 22:12:19 +08:00
|
|
|
if local_user != client_uuid:
|
|
|
|
try:
|
|
|
|
indices = (
|
|
|
|
(1, 3), (2, 1), (3, 0),
|
|
|
|
(2, 0), (4, 5), (1, 6),
|
|
|
|
(2, 6), (3, 6), (0, 6)
|
|
|
|
)
|
2019-04-01 18:28:13 +08:00
|
|
|
|
2019-09-30 22:12:19 +08:00
|
|
|
shader = gpu.shader.from_builtin('3D_UNIFORM_COLOR')
|
|
|
|
position = [tuple(coord) for coord in client_location]
|
|
|
|
color = client_color
|
2019-04-01 18:28:13 +08:00
|
|
|
|
2019-09-30 22:12:19 +08:00
|
|
|
batch = batch_for_shader(
|
|
|
|
shader, 'LINES', {"pos": position}, indices=indices)
|
2019-04-01 18:28:13 +08:00
|
|
|
|
2019-09-30 22:12:19 +08:00
|
|
|
self.d3d_items[client_uuid] = (shader, batch, color)
|
|
|
|
self.d2d_items[client_uuid] = (position[1], client_uuid, color)
|
2019-08-14 20:25:20 +08:00
|
|
|
|
2019-09-30 22:12:19 +08:00
|
|
|
except Exception as e:
|
|
|
|
logger.error("Draw client exception {}".format(e))
|
2019-04-01 18:28:13 +08:00
|
|
|
|
|
|
|
def draw3d_callback(self):
|
2019-08-14 20:25:20 +08:00
|
|
|
bgl.glLineWidth(1.5)
|
2019-09-16 04:43:33 +08:00
|
|
|
bgl.glEnable(bgl.GL_DEPTH_TEST)
|
2019-04-15 23:08:05 +08:00
|
|
|
try:
|
2019-04-17 17:49:27 +08:00
|
|
|
for shader, batch, color in self.d3d_items.values():
|
2019-04-15 23:08:05 +08:00
|
|
|
shader.bind()
|
|
|
|
shader.uniform_float("color", color)
|
|
|
|
batch.draw(shader)
|
2019-08-23 18:28:57 +08:00
|
|
|
except Exception:
|
2019-08-28 18:57:09 +08:00
|
|
|
logger.error("3D Exception")
|
2019-04-01 18:28:13 +08:00
|
|
|
|
|
|
|
def draw2d_callback(self):
|
2019-04-17 17:49:27 +08:00
|
|
|
for position, font, color in self.d2d_items.values():
|
2019-04-01 18:28:13 +08:00
|
|
|
try:
|
2019-04-15 23:08:05 +08:00
|
|
|
coords = get_client_2d(position)
|
2019-04-01 18:28:13 +08:00
|
|
|
|
2019-04-17 17:49:27 +08:00
|
|
|
if coords:
|
|
|
|
blf.position(0, coords[0], coords[1]+10, 0)
|
2019-08-14 20:25:20 +08:00
|
|
|
blf.size(0, 16, 72)
|
2019-04-17 17:49:27 +08:00
|
|
|
blf.color(0, color[0], color[1], color[2], color[3])
|
|
|
|
blf.draw(0, font)
|
2019-04-01 18:28:13 +08:00
|
|
|
|
2019-08-23 18:28:57 +08:00
|
|
|
except Exception:
|
2019-08-28 18:57:09 +08:00
|
|
|
logger.error("2D EXCEPTION")
|
2019-05-02 23:52:32 +08:00
|
|
|
|
2019-08-23 18:28:57 +08:00
|
|
|
|
2019-05-02 23:52:32 +08:00
|
|
|
def register():
|
|
|
|
global renderer
|
|
|
|
renderer = DrawFactory()
|
|
|
|
|
|
|
|
|
|
|
|
def unregister():
|
|
|
|
global renderer
|
|
|
|
renderer.unregister_handlers()
|
|
|
|
|
2019-08-23 18:28:57 +08:00
|
|
|
del renderer
|