feat(rcf): drawing is now event-driven, more efficient

This commit is contained in:
Swann Martinez 2019-04-01 14:53:59 +02:00
parent a8f0f4ee87
commit 8316356d42
No known key found for this signature in database
GPG Key ID: 414CCAFD8DA720E1
2 changed files with 100 additions and 110 deletions

View File

@ -1,4 +1,9 @@
import bpy
import bgl
import blf
import gpu
import mathutils
from bpy_extras import view3d_utils
from gpu_extras.batch import batch_for_shader
@ -58,7 +63,7 @@ def get_client_2d(coords):
return None
class drawer():
class HUD(object):
def __init__(self, client_instance = None):
self.draw_items = []
@ -72,16 +77,16 @@ class drawer():
self.client = client_instance
self.create_batch()
self.register_handlers(context)
self.register_handlers()
def register_handlers(self, context):
def register_handlers(self):
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, context):
def unregister_handlers(self):
if self.draw2d_handle:
bpy.types.SpaceView3D.draw_handler_remove(
self.draw2d_handle, "WINDOW")
@ -94,7 +99,6 @@ class drawer():
self.draw_items.clear()
def create_batch(self):
index = 0
index_object = 0
@ -177,52 +181,9 @@ class drawer():
return False
def draw(self, context):
if context.area:
context.area.tag_redraw()
def draw(self):
if self.client:
# Draw clients
if len(self.client.property_map) > 1:
self.create_batch()
if not context.scene.session_settings.is_running:
self.finish(context)
return {"FINISHED"}
session = context.scene.session_settings
if self.client:
# Hide selected objects
# for object in context.scene.objects:
# if self.is_object_selected(object):
# object.hide_select = True
# else:
# object.hide_select = False
# Active object bounding box
if len(context.selected_objects) > 0:
if session.active_object is not context.selected_objects[0] or session.active_object.is_evaluated:
session.active_object = context.selected_objects[0]
key = "net/objects/{}".format(client.id.decode())
data = {}
data['color'] = [session.client_color.r,
session.client_color.g, session.client_color.b]
data['object'] = session.active_object.name
self.client.push_update(
key, 'clientObject', data)
elif len(context.selected_objects) == 0 and session.active_object:
session.active_object = None
data = {}
data['object'] = None
key = "net/objects/{}".format(self.client.id.decode())
self.client.push_update(key, 'clientObject', data)
# Draw clients
if len(client.property_map) > 1:
# self.unregister_handlers(context)
self.create_batch()
# self.register_handlers(context)
return {"PASS_THROUGH"}
def finish(self, context):
self.unregister_handlers(context)

View File

@ -13,9 +13,9 @@ import mathutils
from bpy_extras import view3d_utils
from gpu_extras.batch import batch_for_shader
from . import net_components, net_ui, rna_translation, net_draw
from . import net_components, net_ui, net_draw
from .libs import dump_anything
logger = logging.getLogger(__name__)
@ -219,6 +219,29 @@ def upload_client_position():
except:
pass
def update_selected_object(context):
global client
session = bpy.context.scene.session_settings
# Active object bounding box
if len(context.selected_objects) > 0:
if session.active_object is not context.selected_objects[0] or session.active_object.is_evaluated:
session.active_object = context.selected_objects[0]
key = "net/objects/{}".format(client.id.decode())
data = {}
data['color'] = [session.client_color.r,
session.client_color.g, session.client_color.b]
data['object'] = session.active_object.name
client.push_update(
key, 'clientObject', data)
elif len(context.selected_objects) == 0 and session.active_object:
session.active_object = None
data = {}
data['color'] = [session.client_color.r,
session.client_color.g, session.client_color.b]
data['object'] = None
key = "net/objects/{}".format(client.id.decode())
client.push_update(key, 'clientObject', data)
def init_scene():
for cam in bpy.data.cameras:
@ -435,6 +458,8 @@ def update_scene(msg):
if msg.mtype == 'Object':
load_object(target=target, data=msg.body,
create=net_vars.load_data)
global drawer
drawer.draw()
elif msg.mtype == 'Mesh':
load_mesh(target=target, data=msg.body,
create=net_vars.load_data)
@ -480,6 +505,56 @@ recv_callbacks = [update_scene]
post_init_callbacks = [refresh_window]
def mesh_tick():
mesh = get_update("Mesh")
if mesh:
upload_mesh(bpy.data.meshes[mesh])
return 2
def object_tick():
obj = get_update("Object")
if obj:
dump_datablock_attibute(bpy.data.objects[obj], ['matrix_world'])
return 0.1
def material_tick():
return 2
def draw_tick():
# drawing
global drawer
drawer.draw()
# Upload
upload_client_position()
return 0.2
def register_ticks():
# REGISTER Updaters
bpy.app.timers.register(draw_tick)
bpy.app.timers.register(mesh_tick)
bpy.app.timers.register(object_tick)
def unregister_ticks():
# REGISTER Updaters
global drawer
drawer.unregister_handlers()
bpy.app.timers.unregister(draw_tick)
bpy.app.timers.unregister(mesh_tick)
bpy.app.timers.unregister(object_tick)
# OPERATORS
class session_join(bpy.types.Operator):
@ -493,7 +568,7 @@ class session_join(bpy.types.Operator):
return True
def execute(self, context):
global client
global client, drawer
net_settings = context.scene.session_settings
# Scene setup
@ -506,13 +581,11 @@ class session_join(bpy.types.Operator):
net_settings.username, randomStringDigits())
username = str(context.scene.session_settings.username)
client_factory = rna_translation.RNAFactory()
client = net_components.RCFClient(
id=username,
on_recv=recv_callbacks,
on_post_init=post_init_callbacks,
factory=client_factory,
address=net_settings.ip,
is_admin=net_settings.session_mode == "HOST")
@ -520,9 +593,7 @@ class session_join(bpy.types.Operator):
net_settings.is_running = True
drawer = net_components.drawer(client_instance=client)
drawer = net_draw.HUD(client_instance=client)
register_ticks()
# bpy.ops.session.draw('INVOKE_DEFAULT')
@ -910,41 +981,13 @@ classes = (
)
def mesh_tick():
mesh = get_update("Mesh")
if mesh:
upload_mesh(bpy.data.meshes[mesh])
return 2
def object_tick():
obj = get_update("Object")
if obj:
dump_datablock_attibute(bpy.data.objects[obj], ['matrix_world'])
return 0.1
def material_tick():
return 2
def draw_tick():
upload_client_position()
return 0.1
def depsgraph_update(scene):
for c in bpy.context.depsgraph.updates.items():
global client
global client
if client:
if client:
update_selected_object(bpy.context)
for c in bpy.context.depsgraph.updates.items():
if client.status == net_components.RCFStatus.CONNECTED:
if scene.session_settings.active_object:
if c[1].is_updated_geometry:
@ -955,7 +998,6 @@ def depsgraph_update(scene):
add_update(c[1].id.bl_rna.name, c[1].id.name)
# if c[1].id.bl_rna.name == 'Material' or c[1].id.bl_rna.name== 'Shader Nodetree':
print(c[1].id.bl_rna.name)
data_name = c[1].id.name
if c[1].id.bl_rna.name == "Object":
if data_name in bpy.data.objects.keys():
@ -971,22 +1013,9 @@ def depsgraph_update(scene):
upload_mesh(bpy.data.objects[data_name].data)
dump_datablock(bpy.data.objects[data_name], 1)
dump_datablock(bpy.data.scenes[0], 4)
# dump_datablock(bpy.data.scenes[0],4)
def register_ticks():
# REGISTER Updaters
bpy.app.timers.register(draw_tick)
bpy.app.timers.register(mesh_tick)
bpy.app.timers.register(object_tick)
def unregister_ticks():
# REGISTER Updaters
bpy.app.timers.unregister(draw_tick)
bpy.app.timers.unregister(mesh_tick)
bpy.app.timers.unregister(object_tick)
# dump_datablock(bpy.data.scenes[0],4)
def register():