feat: serialisation refactoring
This commit is contained in:
19
client.py
19
client.py
@ -76,12 +76,12 @@ class RCFClient(object):
|
|||||||
self.pipe.send_multipart([b"CONNECT", (address.encode() if isinstance(
|
self.pipe.send_multipart([b"CONNECT", (address.encode() if isinstance(
|
||||||
address, str) else address), b'%d' % port])
|
address, str) else address), b'%d' % port])
|
||||||
|
|
||||||
def set(self, key, value):
|
def set(self, key):
|
||||||
"""Set new value in distributed hash table
|
"""Set new value in distributed hash table
|
||||||
Sends [SET][key][value] to the agent
|
Sends [SET][key][value] to the agent
|
||||||
"""
|
"""
|
||||||
self.pipe.send_multipart(
|
self.pipe.send_multipart(
|
||||||
[b"SET", umsgpack.packb(key), umsgpack.packb(value)])
|
[b"SET", umsgpack.packb(key)])
|
||||||
|
|
||||||
def get(self, key):
|
def get(self, key):
|
||||||
"""Lookup value in distributed hash table
|
"""Lookup value in distributed hash table
|
||||||
@ -161,14 +161,20 @@ class RCFClientAgent(object):
|
|||||||
logger.error("E: too many servers (max. %i)", SERVER_MAX)
|
logger.error("E: too many servers (max. %i)", SERVER_MAX)
|
||||||
|
|
||||||
elif command == b"SET":
|
elif command == b"SET":
|
||||||
key, value = msg
|
key = umsgpack.unpackb(msg[0])
|
||||||
|
value = None
|
||||||
|
|
||||||
|
value = helpers.dump(key)
|
||||||
|
|
||||||
|
if value:
|
||||||
|
logger.info(key,"dumped")
|
||||||
# Send key-value pair on to server
|
# Send key-value pair on to server
|
||||||
rcfmsg = message.RCFMessage(key=umsgpack.unpackb(
|
rcfmsg = message.RCFMessage(key=key, id=self.id, mtype="", body=value)
|
||||||
key), id=self.id, mtype="", body=umsgpack.unpackb(value))
|
|
||||||
rcfmsg.store(self.property_map)
|
|
||||||
|
|
||||||
|
rcfmsg.store(self.property_map)
|
||||||
rcfmsg.send(self.publisher)
|
rcfmsg.send(self.publisher)
|
||||||
|
else:
|
||||||
|
logger.error("Fail to dump ")
|
||||||
|
|
||||||
elif command == b"GET":
|
elif command == b"GET":
|
||||||
key = umsgpack.unpackb(msg[0])
|
key = umsgpack.unpackb(msg[0])
|
||||||
@ -223,6 +229,7 @@ def rcf_client_agent(ctx, pipe):
|
|||||||
rcfmsg.store(agent.property_map)
|
rcfmsg.store(agent.property_map)
|
||||||
elif agent.state == State.ACTIVE:
|
elif agent.state == State.ACTIVE:
|
||||||
if rcfmsg.id != agent.id:
|
if rcfmsg.id != agent.id:
|
||||||
|
helpers.load(rcfmsg.key,rcfmsg.body)
|
||||||
rcfmsg.store(agent.property_map)
|
rcfmsg.store(agent.property_map)
|
||||||
action = "update" if rcfmsg.body else "delete"
|
action = "update" if rcfmsg.body else "delete"
|
||||||
logging.info("I: received from {}:{},{} {}".format(
|
logging.info("I: received from {}:{},{} {}".format(
|
||||||
|
141
helpers.py
141
helpers.py
@ -1,55 +1,56 @@
|
|||||||
import bpy
|
import bpy
|
||||||
from .libs import dump_anything
|
from .libs import dump_anything
|
||||||
|
|
||||||
def dump_datablock(datablock, depth):
|
CORRESPONDANCE = {'Collection': 'collections', 'Mesh': 'meshes', 'Object': 'objects', 'Material': 'materials',
|
||||||
if datablock:
|
'Texture': 'textures', 'Scene': 'scenes', 'Light': 'lights', 'Camera': 'cameras', 'Action': 'actions', 'Armature': 'armatures', 'GreasePencil': 'grease_pencils'}
|
||||||
print("sending {}".format(datablock.name))
|
|
||||||
|
|
||||||
dumper = dump_anything.Dumper()
|
# LOAD HELPERS
|
||||||
dumper.type_subset = dumper.match_subset_all
|
|
||||||
dumper.depth = depth
|
|
||||||
|
|
||||||
datablock_type = datablock.bl_rna.name
|
def load(key, value):
|
||||||
key = "{}/{}".format(datablock_type, datablock.name)
|
target = resolve_bpy_path(key)
|
||||||
data = dumper.dump(datablock)
|
target_type = target.__class__.__name__
|
||||||
|
if target:
|
||||||
|
target.is_updating = True
|
||||||
|
|
||||||
client.push_update(key, datablock_type, data)
|
if target_type == 'Object':
|
||||||
|
load_object(target=target, data=value,
|
||||||
|
create=True)
|
||||||
|
elif target_type == 'Mesh':
|
||||||
|
load_mesh(target=target, data=value,
|
||||||
|
create=True)
|
||||||
|
elif target_type == 'Collection':
|
||||||
|
load_collection(target=target, data=value,
|
||||||
|
create=True)
|
||||||
|
elif target_type == 'Material':
|
||||||
|
load_material(target=target, data=value,
|
||||||
|
create=True)
|
||||||
|
elif target_type == 'Grease Pencil':
|
||||||
|
load_gpencil(target=target, data=value,
|
||||||
|
create=True)
|
||||||
|
elif target_type == 'Scene':
|
||||||
|
load_scene(target=target, data=value,
|
||||||
|
create=True)
|
||||||
|
elif 'Light' in target_type:
|
||||||
|
load_light(target=target, data=value,
|
||||||
|
create=True)
|
||||||
|
else:
|
||||||
|
load_default(target=target, data=value,
|
||||||
|
create=True, type=target_type)
|
||||||
|
|
||||||
|
def resolve_bpy_path(path):
|
||||||
|
"""
|
||||||
|
Get bpy property value from path
|
||||||
|
"""
|
||||||
|
item = None
|
||||||
|
|
||||||
def dump_datablock_attibute(datablock, attributes, depth=1):
|
|
||||||
if datablock:
|
|
||||||
dumper = dump_anything.Dumper()
|
|
||||||
dumper.type_subset = dumper.match_subset_all
|
|
||||||
dumper.depth = depth
|
|
||||||
|
|
||||||
datablock_type = datablock.bl_rna.name
|
|
||||||
key = "{}/{}".format(datablock_type, datablock.name)
|
|
||||||
|
|
||||||
data = {}
|
|
||||||
for attr in attributes:
|
|
||||||
try:
|
try:
|
||||||
data[attr] = dumper.dump(getattr(datablock, attr))
|
path = path.split('/')
|
||||||
|
item = getattr(bpy.data, CORRESPONDANCE[path[0]])[path[1]]
|
||||||
|
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
client.push_update(key, datablock_type, data)
|
return item
|
||||||
|
|
||||||
|
|
||||||
def upload_mesh(mesh):
|
|
||||||
if mesh.bl_rna.name == 'Mesh':
|
|
||||||
dump_datablock_attibute(
|
|
||||||
mesh, ['name', 'polygons', 'edges', 'vertices'], 6)
|
|
||||||
|
|
||||||
|
|
||||||
def upload_material(material):
|
|
||||||
if material.bl_rna.name == 'Material':
|
|
||||||
dump_datablock_attibute(material, ['name', 'node_tree'], 7)
|
|
||||||
|
|
||||||
|
|
||||||
def upload_gpencil(gpencil):
|
|
||||||
if gpencil.bl_rna.name == 'Grease Pencil':
|
|
||||||
dump_datablock_attibute(gpencil, ['name', 'layers','materials'], 9)
|
|
||||||
|
|
||||||
|
|
||||||
def load_mesh(target=None, data=None, create=False):
|
def load_mesh(target=None, data=None, create=False):
|
||||||
import bmesh
|
import bmesh
|
||||||
@ -272,3 +273,63 @@ def load_default(target=None, data=None, create=False, type=None):
|
|||||||
dump_anything.load(target, data)
|
dump_anything.load(target, data)
|
||||||
except:
|
except:
|
||||||
print("default loading error")
|
print("default loading error")
|
||||||
|
|
||||||
|
# DUMP HELPERS
|
||||||
|
|
||||||
|
def dump(key):
|
||||||
|
target = resolve_bpy_path(key)
|
||||||
|
target_type = target.__class__.__name__
|
||||||
|
data = None
|
||||||
|
|
||||||
|
if target_type == 'Material':
|
||||||
|
data = dump_datablock_attibute(target, ['name', 'node_tree'], 7)
|
||||||
|
elif target_type == 'Grease Pencil':
|
||||||
|
data = dump_datablock_attibute(target, ['name', 'layers','materials'], 9)
|
||||||
|
elif target_type == 'Camera':
|
||||||
|
data = dump_datablock(target, 1)
|
||||||
|
elif target_type == 'Light':
|
||||||
|
data = dump_datablock(target, 1)
|
||||||
|
elif target_type == 'Mesh':
|
||||||
|
data = dump_datablock_attibute(
|
||||||
|
target, ['name', 'polygons', 'edges', 'vertices'], 6)
|
||||||
|
elif target_type == 'Object':
|
||||||
|
data = dump_datablock(target, 1)
|
||||||
|
elif target_type == 'Collection':
|
||||||
|
data = dump_datablock(target, 4)
|
||||||
|
elif target_type == 'Scene':
|
||||||
|
data = dump_datablock(target, 4)
|
||||||
|
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
def dump_datablock(datablock, depth):
|
||||||
|
if datablock:
|
||||||
|
dumper = dump_anything.Dumper()
|
||||||
|
dumper.type_subset = dumper.match_subset_all
|
||||||
|
dumper.depth = depth
|
||||||
|
|
||||||
|
datablock_type = datablock.bl_rna.name
|
||||||
|
key = "{}/{}".format(datablock_type, datablock.name)
|
||||||
|
data = dumper.dump(datablock)
|
||||||
|
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
def dump_datablock_attibute(datablock, attributes, depth=1):
|
||||||
|
if datablock:
|
||||||
|
dumper = dump_anything.Dumper()
|
||||||
|
dumper.type_subset = dumper.match_subset_all
|
||||||
|
dumper.depth = depth
|
||||||
|
|
||||||
|
datablock_type = datablock.bl_rna.name
|
||||||
|
key = "{}/{}".format(datablock_type, datablock.name)
|
||||||
|
|
||||||
|
data = {}
|
||||||
|
for attr in attributes:
|
||||||
|
try:
|
||||||
|
data[attr] = dumper.dump(getattr(datablock, attr))
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
return data
|
||||||
|
|
||||||
|
23
operators.py
23
operators.py
@ -53,8 +53,7 @@ SUPPORTED_DATABLOCKS = ['collections', 'meshes', 'objects',
|
|||||||
'materials', 'textures', 'lights', 'cameras', 'actions', 'armatures', 'grease_pencils']
|
'materials', 'textures', 'lights', 'cameras', 'actions', 'armatures', 'grease_pencils']
|
||||||
SUPPORTED_TYPES = ['Mesh', 'Grease Pencil', 'Material',
|
SUPPORTED_TYPES = ['Mesh', 'Grease Pencil', 'Material',
|
||||||
'Texture', 'Light', 'Camera', 'Object', 'Action', 'Armature','Collection', 'Scene']
|
'Texture', 'Light', 'Camera', 'Object', 'Action', 'Armature','Collection', 'Scene']
|
||||||
CORRESPONDANCE = {'Collection': 'collections', 'Mesh': 'meshes', 'Object': 'objects', 'Material': 'materials',
|
|
||||||
'Texture': 'textures', 'Scene': 'scenes', 'Light': 'lights', 'Camera': 'cameras', 'Action': 'actions', 'Armature': 'armatures', 'GreasePencil': 'grease_pencils'}
|
|
||||||
# UTILITY FUNCTIONS
|
# UTILITY FUNCTIONS
|
||||||
|
|
||||||
|
|
||||||
@ -78,22 +77,6 @@ def randomColor():
|
|||||||
return [r, v, b]
|
return [r, v, b]
|
||||||
|
|
||||||
|
|
||||||
def resolve_bpy_path(path):
|
|
||||||
"""
|
|
||||||
Get bpy property value from path
|
|
||||||
"""
|
|
||||||
item = None
|
|
||||||
|
|
||||||
try:
|
|
||||||
path = path.split('/')
|
|
||||||
item = getattr(bpy.data, CORRESPONDANCE[path[0]])[path[1]]
|
|
||||||
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
|
|
||||||
return item
|
|
||||||
|
|
||||||
|
|
||||||
def refresh_window():
|
def refresh_window():
|
||||||
import bpy
|
import bpy
|
||||||
bpy.ops.wm.redraw_timer(type='DRAW_WIN_SWAP', iterations=1)
|
bpy.ops.wm.redraw_timer(type='DRAW_WIN_SWAP', iterations=1)
|
||||||
@ -443,8 +426,8 @@ class session_add_property(bpy.types.Operator):
|
|||||||
def execute(self, context):
|
def execute(self, context):
|
||||||
global client_instance
|
global client_instance
|
||||||
|
|
||||||
client_instance.set('key', 1)
|
client_instance.set(self.property_path)
|
||||||
print(client_instance.get('key'))
|
# print(client_instance.get('key'))
|
||||||
# item = resolve_bpy_path(self.property_path)
|
# item = resolve_bpy_path(self.property_path)
|
||||||
|
|
||||||
# print(item)
|
# print(item)
|
||||||
|
Reference in New Issue
Block a user