feat(rcf): drawing is now event-driven, more efficient
This commit is contained in:
67
net_draw.py
67
net_draw.py
@ -1,4 +1,9 @@
|
|||||||
import bpy
|
import bpy
|
||||||
|
import bgl
|
||||||
|
import blf
|
||||||
|
import gpu
|
||||||
|
import mathutils
|
||||||
|
|
||||||
from bpy_extras import view3d_utils
|
from bpy_extras import view3d_utils
|
||||||
from gpu_extras.batch import batch_for_shader
|
from gpu_extras.batch import batch_for_shader
|
||||||
|
|
||||||
@ -58,7 +63,7 @@ def get_client_2d(coords):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
class drawer():
|
class HUD(object):
|
||||||
|
|
||||||
def __init__(self, client_instance = None):
|
def __init__(self, client_instance = None):
|
||||||
self.draw_items = []
|
self.draw_items = []
|
||||||
@ -72,16 +77,16 @@ class drawer():
|
|||||||
self.client = client_instance
|
self.client = client_instance
|
||||||
|
|
||||||
self.create_batch()
|
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_handle = bpy.types.SpaceView3D.draw_handler_add(
|
||||||
self.draw3d_callback, (), 'WINDOW', 'POST_VIEW')
|
self.draw3d_callback, (), 'WINDOW', 'POST_VIEW')
|
||||||
self.draw2d_handle = bpy.types.SpaceView3D.draw_handler_add(
|
self.draw2d_handle = bpy.types.SpaceView3D.draw_handler_add(
|
||||||
self.draw2d_callback, (), 'WINDOW', 'POST_PIXEL')
|
self.draw2d_callback, (), 'WINDOW', 'POST_PIXEL')
|
||||||
|
|
||||||
def unregister_handlers(self, context):
|
def unregister_handlers(self):
|
||||||
if self.draw2d_handle:
|
if self.draw2d_handle:
|
||||||
bpy.types.SpaceView3D.draw_handler_remove(
|
bpy.types.SpaceView3D.draw_handler_remove(
|
||||||
self.draw2d_handle, "WINDOW")
|
self.draw2d_handle, "WINDOW")
|
||||||
@ -94,7 +99,6 @@ class drawer():
|
|||||||
|
|
||||||
self.draw_items.clear()
|
self.draw_items.clear()
|
||||||
|
|
||||||
|
|
||||||
def create_batch(self):
|
def create_batch(self):
|
||||||
index = 0
|
index = 0
|
||||||
index_object = 0
|
index_object = 0
|
||||||
@ -177,52 +181,9 @@ class drawer():
|
|||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def draw(self, context):
|
def draw(self):
|
||||||
if context.area:
|
if self.client:
|
||||||
context.area.tag_redraw()
|
# 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)
|
|
139
net_operators.py
139
net_operators.py
@ -13,7 +13,7 @@ import mathutils
|
|||||||
from bpy_extras import view3d_utils
|
from bpy_extras import view3d_utils
|
||||||
from gpu_extras.batch import batch_for_shader
|
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
|
from .libs import dump_anything
|
||||||
|
|
||||||
|
|
||||||
@ -219,6 +219,29 @@ def upload_client_position():
|
|||||||
except:
|
except:
|
||||||
pass
|
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():
|
def init_scene():
|
||||||
for cam in bpy.data.cameras:
|
for cam in bpy.data.cameras:
|
||||||
@ -435,6 +458,8 @@ def update_scene(msg):
|
|||||||
if msg.mtype == 'Object':
|
if msg.mtype == 'Object':
|
||||||
load_object(target=target, data=msg.body,
|
load_object(target=target, data=msg.body,
|
||||||
create=net_vars.load_data)
|
create=net_vars.load_data)
|
||||||
|
global drawer
|
||||||
|
drawer.draw()
|
||||||
elif msg.mtype == 'Mesh':
|
elif msg.mtype == 'Mesh':
|
||||||
load_mesh(target=target, data=msg.body,
|
load_mesh(target=target, data=msg.body,
|
||||||
create=net_vars.load_data)
|
create=net_vars.load_data)
|
||||||
@ -480,6 +505,56 @@ recv_callbacks = [update_scene]
|
|||||||
post_init_callbacks = [refresh_window]
|
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
|
# OPERATORS
|
||||||
class session_join(bpy.types.Operator):
|
class session_join(bpy.types.Operator):
|
||||||
|
|
||||||
@ -493,7 +568,7 @@ class session_join(bpy.types.Operator):
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
def execute(self, context):
|
def execute(self, context):
|
||||||
global client
|
global client, drawer
|
||||||
|
|
||||||
net_settings = context.scene.session_settings
|
net_settings = context.scene.session_settings
|
||||||
# Scene setup
|
# Scene setup
|
||||||
@ -506,13 +581,11 @@ class session_join(bpy.types.Operator):
|
|||||||
net_settings.username, randomStringDigits())
|
net_settings.username, randomStringDigits())
|
||||||
|
|
||||||
username = str(context.scene.session_settings.username)
|
username = str(context.scene.session_settings.username)
|
||||||
client_factory = rna_translation.RNAFactory()
|
|
||||||
|
|
||||||
client = net_components.RCFClient(
|
client = net_components.RCFClient(
|
||||||
id=username,
|
id=username,
|
||||||
on_recv=recv_callbacks,
|
on_recv=recv_callbacks,
|
||||||
on_post_init=post_init_callbacks,
|
on_post_init=post_init_callbacks,
|
||||||
factory=client_factory,
|
|
||||||
address=net_settings.ip,
|
address=net_settings.ip,
|
||||||
is_admin=net_settings.session_mode == "HOST")
|
is_admin=net_settings.session_mode == "HOST")
|
||||||
|
|
||||||
@ -520,9 +593,7 @@ class session_join(bpy.types.Operator):
|
|||||||
|
|
||||||
net_settings.is_running = True
|
net_settings.is_running = True
|
||||||
|
|
||||||
|
drawer = net_draw.HUD(client_instance=client)
|
||||||
|
|
||||||
drawer = net_components.drawer(client_instance=client)
|
|
||||||
|
|
||||||
register_ticks()
|
register_ticks()
|
||||||
# bpy.ops.session.draw('INVOKE_DEFAULT')
|
# 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):
|
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 client.status == net_components.RCFStatus.CONNECTED:
|
||||||
if scene.session_settings.active_object:
|
if scene.session_settings.active_object:
|
||||||
if c[1].is_updated_geometry:
|
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)
|
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':
|
# 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
|
data_name = c[1].id.name
|
||||||
if c[1].id.bl_rna.name == "Object":
|
if c[1].id.bl_rna.name == "Object":
|
||||||
if data_name in bpy.data.objects.keys():
|
if data_name in bpy.data.objects.keys():
|
||||||
@ -972,21 +1014,8 @@ def depsgraph_update(scene):
|
|||||||
dump_datablock(bpy.data.objects[data_name], 1)
|
dump_datablock(bpy.data.objects[data_name], 1)
|
||||||
dump_datablock(bpy.data.scenes[0], 4)
|
dump_datablock(bpy.data.scenes[0], 4)
|
||||||
|
|
||||||
# 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)
|
|
||||||
|
|
||||||
|
|
||||||
def register():
|
def register():
|
||||||
|
Reference in New Issue
Block a user