multi-user/draw.py

196 lines
6.0 KiB
Python
Raw Normal View History

2019-04-01 12:28:13 +02:00
import bpy
import bgl
import blf
import gpu
import mathutils
2019-04-01 12:28:13 +02:00
from bpy_extras import view3d_utils
from gpu_extras.batch import batch_for_shader
2019-04-18 15:05:48 +02:00
2019-04-01 12:28:13 +02: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
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]
def get_client_view_rect():
area, region, rv3d = view3d_find()
v1 = [0, 0, 0]
v2 = [0, 0, 0]
v3 = [0, 0, 0]
v4 = [0, 0, 0]
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))
coords = [v1, v2, v3, v4]
2019-04-01 12:28:13 +02:00
indices = (
(1, 3), (2, 1), (3, 0), (2, 0)
)
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 15:05:48 +02:00
return (0, 0)
2019-04-01 12:28:13 +02:00
class HUD(object):
2019-04-01 12:28:13 +02:00
2019-04-18 15:05:48 +02:00
def __init__(self, client_instance=None):
2019-04-17 11:49:27 +02:00
self.d3d_items = {}
self.d2d_items = {}
2019-04-01 12:28:13 +02:00
self.draw3d_handle = None
self.draw2d_handle = None
self.draw_event = None
self.coords = None
self.active_object = None
if client_instance:
2019-04-18 15:05:48 +02:00
self.client = client_instance
2019-04-01 12:28:13 +02:00
2019-04-17 11:49:27 +02:00
self.draw_clients()
self.register_handlers()
2019-04-01 12:28:13 +02:00
def register_handlers(self):
2019-04-01 12:28:13 +02: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')
def unregister_handlers(self):
2019-04-01 12:28:13 +02: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 17:08:05 +02:00
self.d3d_items.clear()
self.d2d_items.clear()
2019-04-17 11:49:27 +02:00
def draw_selected_object(self):
clients = self.client.get("Client")
2019-04-17 16:15:21 +02:00
for client in clients:
name = client[0].split('/')[1]
local_username = bpy.context.scene.session_settings.username
if name != local_username:
if client[1]['active_objects']:
for select_ob in client[1]['active_objects']:
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-04-01 12:28:13 +02:00
if select_ob in bpy.data.objects.keys():
ob = bpy.data.objects[select_ob]
else:
return
2019-04-01 12:28:13 +02:00
bbox_corners = [ob.matrix_world @ mathutils.Vector(corner) for corner in ob.bound_box]
2019-04-01 12:28:13 +02:00
coords = [(point.x, point.y, point.z)
for point in bbox_corners]
2019-04-17 16:15:21 +02:00
shader = gpu.shader.from_builtin('3D_UNIFORM_COLOR')
2019-04-17 16:15:21 +02:00
color = client[1]['color']
2019-04-01 12:28:13 +02:00
batch = batch_for_shader(
shader, 'LINES', {"pos": coords}, indices=indices)
2019-04-01 12:28:13 +02:00
self.d3d_items["{}/{}".format(client[0],
select_ob)] = (shader, batch, color)
else:
self.d3d_items.clear()
2019-04-17 11:49:27 +02:00
def draw_clients(self):
clients = self.client.get("Client")
2019-04-01 12:28:13 +02:00
2019-04-17 11:49:27 +02:00
for client in clients:
2019-04-17 14:22:56 +02:00
name = client[0].split('/')[1]
local_username = bpy.context.scene.session_settings.username
2019-04-17 14:22:56 +02:00
if name != local_username:
2019-04-18 15:05:48 +02:00
try:
2019-04-15 17:08:05 +02:00
indices = (
(1, 3), (2, 1), (3, 0), (2, 0)
)
shader = gpu.shader.from_builtin('3D_UNIFORM_COLOR')
position = client[1]['location']
color = client[1]['color']
2019-04-18 15:05:48 +02:00
2019-04-15 17:08:05 +02:00
batch = batch_for_shader(
shader, 'LINES', {"pos": position}, indices=indices)
2019-04-01 12:28:13 +02:00
2019-04-18 15:05:48 +02:00
self.d3d_items[client[0]] = (shader, batch, color)
self.d2d_items[client[0]] = (position[1], name, color)
2019-04-01 12:28:13 +02:00
2019-04-15 17:08:05 +02:00
except Exception as e:
2019-04-17 11:49:27 +02:00
print("Draw client exception {}".format(e))
2019-04-01 12:28:13 +02:00
def draw3d_callback(self):
bgl.glLineWidth(3)
2019-04-15 17:08:05 +02:00
try:
2019-04-17 11:49:27 +02:00
for shader, batch, color in self.d3d_items.values():
2019-04-15 17:08:05 +02:00
shader.bind()
shader.uniform_float("color", color)
batch.draw(shader)
except Exception as e:
2019-04-17 11:49:27 +02:00
print("3D Exception")
2019-04-01 12:28:13 +02:00
def draw2d_callback(self):
2019-04-17 11:49:27 +02:00
for position, font, color in self.d2d_items.values():
2019-04-01 12:28:13 +02:00
try:
2019-04-15 17:08:05 +02:00
coords = get_client_2d(position)
2019-04-01 12:28:13 +02:00
2019-04-17 11:49:27 +02:00
if coords:
blf.position(0, coords[0], coords[1]+10, 0)
blf.size(0, 10, 72)
blf.color(0, color[0], color[1], color[2], color[3])
blf.draw(0, font)
2019-04-01 12:28:13 +02:00
2019-04-15 17:08:05 +02:00
except Exception as e:
2019-04-17 11:49:27 +02:00
print("2D EXCEPTION")
2019-04-01 12:28:13 +02:00
def draw(self):
if self.client:
# Draw clients
2019-04-17 11:49:27 +02:00
self.draw_clients()
2019-04-17 16:15:21 +02:00
self.draw_selected_object()