feat(rcf): Added property agnostic method
This commit is contained in:
@ -15,14 +15,17 @@ class RCFMessage(object):
|
||||
"""
|
||||
Message is formatted on wire as 2 frames:
|
||||
frame 0: key (0MQ string) // property path
|
||||
frame 1: type (0MQ string) // property path
|
||||
frame 2: body (blob) // Could be any data
|
||||
|
||||
"""
|
||||
key = None # key (string)
|
||||
body = None # blob
|
||||
type = None # data type (string)
|
||||
body = None # data blob
|
||||
|
||||
def __init__(self, key=None, body=None):
|
||||
def __init__(self, key=None, type=None, body=None):
|
||||
self.key = key
|
||||
self.type = type
|
||||
self.body = body
|
||||
|
||||
def store(self, dikt):
|
||||
@ -35,14 +38,16 @@ class RCFMessage(object):
|
||||
def send(self, socket):
|
||||
"""Send key-value message to socket; any empty frames are sent as such."""
|
||||
key = '' if self.key is None else self.key.encode()
|
||||
type = '' if self.type is None else self.type.encode()
|
||||
body = '' if self.body is None else umsgpack.packb(self.body)
|
||||
socket.send_multipart([key, body])
|
||||
|
||||
@classmethod
|
||||
def recv(cls, socket):
|
||||
"""Reads key-value message from socket, returns new kvmsg instance."""
|
||||
key, body = socket.recv_multipart(zmq.NOBLOCK)
|
||||
key,type, body = socket.recv_multipart(zmq.NOBLOCK)
|
||||
key = key.decode() if key else None
|
||||
body = body.decode() if body else None
|
||||
body = umsgpack.unpackb(body) if body else None
|
||||
|
||||
return cls(key=key, body=body)
|
||||
@ -54,9 +59,10 @@ class RCFMessage(object):
|
||||
else:
|
||||
size = len(self.body)
|
||||
data = repr(self.body)
|
||||
print("[key:{key}][size:{size}] {data}".format(
|
||||
print("[key:{key}][size:{size}][type:{type}] {data}".format(
|
||||
key=self.key,
|
||||
size=size,
|
||||
type=self.type,
|
||||
data=data,
|
||||
))
|
||||
|
||||
@ -122,7 +128,7 @@ class Client():
|
||||
for f in self.recv_callback:
|
||||
f(rcfmsg)
|
||||
|
||||
def push_update(self, key, body):
|
||||
def push_update(self, key,type,body):
|
||||
rcfmsg = RCFMessage(key,body)
|
||||
rcfmsg.send(self.push_sock)
|
||||
# self.push_sock.send_multipart()
|
||||
|
103
net_operators.py
103
net_operators.py
@ -10,6 +10,66 @@ client = None
|
||||
server = None
|
||||
context = None
|
||||
|
||||
NATIVE_TYPES = (
|
||||
bpy.types.IntProperty,
|
||||
bpy.types.FloatProperty,
|
||||
bpy.types.BoolProperty,
|
||||
bpy.types.StringProperty,
|
||||
)
|
||||
|
||||
VECTOR_TYPES = (
|
||||
'Vector'
|
||||
)
|
||||
|
||||
# TODO: Less ugly method
|
||||
def from_bpy(value):
|
||||
print(' casting from bpy')
|
||||
value_type = type(value)
|
||||
value_casted = None
|
||||
|
||||
if value_type is mathutils.Vector:
|
||||
value_casted = [value.x,value.y,value.z]
|
||||
elif value_type is bpy.props.collection:
|
||||
pass # TODO: Collection replication
|
||||
elif value_type is mathutils.Euler:
|
||||
value_casted = [value.x,value.y,value.z]
|
||||
elif value_type is NATIVE_TYPES:
|
||||
value_casted = value
|
||||
|
||||
return value_casted.__class__.__name__,value_casted
|
||||
|
||||
def to_bpy(store_item):
|
||||
"""
|
||||
Get bpy value from store
|
||||
"""
|
||||
value_type = store_item.type
|
||||
value_casted = None
|
||||
store_value = store_item.body
|
||||
|
||||
if value_type == 'Vector':
|
||||
value_casted = mathutils.Vector((store_value[0],store_value[1],store_value[2]))
|
||||
|
||||
return value_casted
|
||||
|
||||
def resolve_bpy_path(path):
|
||||
"""
|
||||
Get bpy property value from path
|
||||
"""
|
||||
path = path.split('/')
|
||||
|
||||
obj = None
|
||||
attribute = None
|
||||
logger.info("resolving {}".format(path))
|
||||
try:
|
||||
obj = getattr(bpy.data,path[0])[path[1]]
|
||||
attribute = getattr(obj,path[2])
|
||||
logger.info("done {} : {}".format(obj,attribute))
|
||||
except AttributeError:
|
||||
print(" Attribute not found")
|
||||
|
||||
return obj, attribute
|
||||
|
||||
|
||||
|
||||
# CLIENT-SERVER
|
||||
def refresh_window(msg):
|
||||
@ -18,16 +78,19 @@ def refresh_window(msg):
|
||||
bpy.ops.wm.redraw_timer(type='DRAW_WIN_SWAP', iterations=1)
|
||||
|
||||
def patch_scene(msg):
|
||||
path = msg.key.split('/')
|
||||
value = None
|
||||
object = getattr(bpy.data,path[0])[path[1]]
|
||||
attribute = getattr(getattr(bpy.data,path[0])[path[1]],path[2])
|
||||
print("attribute: {}".format(attribute))
|
||||
if type(attribute) == mathutils.Vector:
|
||||
#attribute = array2vector(msg.body)
|
||||
value = array2vector(msg.body)
|
||||
|
||||
setattr(object,path[2],value)
|
||||
value = None
|
||||
|
||||
obj, attr = resolve_bpy_path(msg.key)
|
||||
value = to_bpy(attr)
|
||||
print("attribute: {} , value: {}".format(attr, value))
|
||||
|
||||
try:
|
||||
setattr(obj,attr,value)
|
||||
except:
|
||||
pass
|
||||
|
||||
|
||||
def vector2array(v):
|
||||
return [v.x,v.y,v.z]
|
||||
|
||||
@ -53,7 +116,7 @@ class session_join(bpy.types.Operator):
|
||||
|
||||
client = net_components.Client(id=username ,recv_callback=callbacks)
|
||||
time.sleep(1)
|
||||
|
||||
|
||||
bpy.ops.asyncio.loop()
|
||||
|
||||
return {"FINISHED"}
|
||||
@ -62,10 +125,10 @@ class session_join(bpy.types.Operator):
|
||||
class session_send(bpy.types.Operator):
|
||||
bl_idname = "session.send"
|
||||
bl_label = "send"
|
||||
bl_description = "broadcast a message to connected clients"
|
||||
bl_description = "broadcast a property to connected clients"
|
||||
bl_options = {"REGISTER"}
|
||||
|
||||
message: bpy.props.StringProperty(default="Hi")
|
||||
property_path: bpy.props.StringProperty(default="None")
|
||||
|
||||
@classmethod
|
||||
def poll(cls, context):
|
||||
@ -74,10 +137,13 @@ class session_send(bpy.types.Operator):
|
||||
def execute(self, context):
|
||||
global client
|
||||
|
||||
key = "objects/Cube/location"
|
||||
value = vector2array(bpy.data.objects['Cube'].location)
|
||||
obj, attr = resolve_bpy_path(self.property_path)
|
||||
|
||||
client.push_update(key,value)
|
||||
if obj and attr:
|
||||
key = self.property_path
|
||||
value_type, value = from_bpy(attr)
|
||||
|
||||
client.push_update(key,value_type,value)
|
||||
|
||||
return {"FINISHED"}
|
||||
|
||||
@ -121,7 +187,7 @@ class session_stop(bpy.types.Operator):
|
||||
def execute(self, context):
|
||||
global server
|
||||
global client
|
||||
|
||||
|
||||
if server :
|
||||
server.stop()
|
||||
del server
|
||||
@ -141,6 +207,7 @@ class session_settings(bpy.types.PropertyGroup):
|
||||
username = bpy.props.StringProperty(name="Username",default="DefaultUser")
|
||||
ip = bpy.props.StringProperty(name="localhost")
|
||||
port = bpy.props.IntProperty(name="5555")
|
||||
buffer = bpy.props.StringProperty(name="None")
|
||||
|
||||
# TODO: Rename to match official blender convention
|
||||
classes = (
|
||||
@ -159,11 +226,11 @@ def register():
|
||||
|
||||
bpy.types.Scene.session_settings = bpy.props.PointerProperty(type=session_settings)
|
||||
|
||||
def unregister():
|
||||
def unregister():
|
||||
from bpy.utils import unregister_class
|
||||
for cls in reversed(classes):
|
||||
unregister_class(cls)
|
||||
|
||||
|
||||
del bpy.types.Scene.session_settings
|
||||
|
||||
|
||||
|
@ -26,7 +26,8 @@ class SessionPanel(bpy.types.Panel):
|
||||
|
||||
row = layout.row(align=True)
|
||||
|
||||
row.operator("session.send")
|
||||
row.prop(scene.session_settings,"buffer", text="")
|
||||
row.operator("session.send").property_path = scene.session_settings.buffer
|
||||
row = layout.row()
|
||||
# Debug area
|
||||
|
||||
@ -34,7 +35,7 @@ class SessionPanel(bpy.types.Panel):
|
||||
area_msg = row.box()
|
||||
if len(net_operators.client.property_map) > 0:
|
||||
for key,value in net_operators.client.property_map.items():
|
||||
area_msg.label(text="{}:{}".format(key,value))
|
||||
area_msg.label(text="{}".format(key))
|
||||
else:
|
||||
area_msg.label(text="Empty")
|
||||
|
||||
|
Reference in New Issue
Block a user