2019-02-08 22:44:02 +08:00
|
|
|
import bpy
|
|
|
|
from . import net_components
|
2019-02-11 22:48:07 +08:00
|
|
|
import time
|
2019-02-12 00:02:25 +08:00
|
|
|
import logging
|
2019-02-14 01:53:22 +08:00
|
|
|
import mathutils
|
2019-02-22 23:46:35 +08:00
|
|
|
import random
|
|
|
|
import string
|
2019-03-07 01:45:11 +08:00
|
|
|
import gpu
|
|
|
|
from gpu_extras.batch import batch_for_shader
|
2019-02-12 00:02:25 +08:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
2019-02-08 22:44:02 +08:00
|
|
|
|
2019-02-11 22:48:07 +08:00
|
|
|
client = None
|
|
|
|
server = None
|
|
|
|
context = None
|
2019-02-08 22:44:02 +08:00
|
|
|
|
2019-02-14 20:13:28 +08:00
|
|
|
NATIVE_TYPES = (
|
|
|
|
bpy.types.IntProperty,
|
|
|
|
bpy.types.FloatProperty,
|
|
|
|
bpy.types.BoolProperty,
|
|
|
|
bpy.types.StringProperty,
|
|
|
|
)
|
|
|
|
|
2019-03-07 01:45:11 +08:00
|
|
|
|
2019-03-07 01:08:54 +08:00
|
|
|
class RNAFactory(net_components.RCFFactory):
|
2019-03-07 01:45:11 +08:00
|
|
|
def load_getter(self, data):
|
|
|
|
get, set = match_supported_types(data)
|
2019-03-07 01:08:54 +08:00
|
|
|
|
2019-02-14 20:13:28 +08:00
|
|
|
|
2019-02-26 01:05:46 +08:00
|
|
|
def on_scene_evalutation(scene):
|
|
|
|
# TODO: viewer representation
|
|
|
|
# TODO: Live update only selected object
|
|
|
|
# TODO: Scene representation
|
|
|
|
pass
|
2019-02-22 23:46:35 +08:00
|
|
|
|
2019-03-07 01:45:11 +08:00
|
|
|
|
2019-02-22 23:46:35 +08:00
|
|
|
def randomStringDigits(stringLength=6):
|
|
|
|
"""Generate a random string of letters and digits """
|
|
|
|
lettersAndDigits = string.ascii_letters + string.digits
|
|
|
|
return ''.join(random.choice(lettersAndDigits) for i in range(stringLength))
|
|
|
|
|
2019-02-26 01:05:46 +08:00
|
|
|
|
2019-02-22 21:56:49 +08:00
|
|
|
def match_supported_types(value):
|
|
|
|
type_factory = None
|
|
|
|
|
2019-02-26 01:05:46 +08:00
|
|
|
if isinstance(value, bool):
|
2019-02-22 21:56:49 +08:00
|
|
|
print("float")
|
2019-02-26 01:05:46 +08:00
|
|
|
elif isinstance(value, mathutils.Vector):
|
2019-02-22 21:56:49 +08:00
|
|
|
print("vector")
|
|
|
|
type_factory = VectorTypeTranslation()
|
2019-02-26 01:05:46 +08:00
|
|
|
elif isinstance(value, mathutils.Euler):
|
2019-02-22 21:56:49 +08:00
|
|
|
print("Euler")
|
|
|
|
elif type(value) in NATIVE_TYPES:
|
|
|
|
print("native")
|
|
|
|
else:
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2019-02-26 01:05:46 +08:00
|
|
|
return type_factory
|
2019-02-22 21:56:49 +08:00
|
|
|
|
|
|
|
|
2019-02-14 20:13:28 +08:00
|
|
|
# TODO: Less ugly method
|
|
|
|
def from_bpy(value):
|
2019-02-14 22:30:35 +08:00
|
|
|
logger.debug(' casting from bpy')
|
2019-02-14 20:13:28 +08:00
|
|
|
value_type = type(value)
|
|
|
|
value_casted = None
|
|
|
|
|
|
|
|
if value_type is mathutils.Vector:
|
2019-02-26 01:05:46 +08:00
|
|
|
value_casted = [value.x, value.y, value.z]
|
2019-02-14 20:13:28 +08:00
|
|
|
elif value_type is bpy.props.collection:
|
2019-02-26 01:05:46 +08:00
|
|
|
pass # TODO: Collection replication
|
2019-02-14 20:13:28 +08:00
|
|
|
elif value_type is mathutils.Euler:
|
2019-02-26 01:05:46 +08:00
|
|
|
value_casted = [value.x, value.y, value.z]
|
2019-02-14 20:13:28 +08:00
|
|
|
elif value_type is NATIVE_TYPES:
|
|
|
|
value_casted = value
|
2019-02-26 01:05:46 +08:00
|
|
|
|
|
|
|
return str(value.__class__.__name__), value_casted
|
|
|
|
|
2019-02-14 20:13:28 +08:00
|
|
|
|
|
|
|
def to_bpy(store_item):
|
|
|
|
"""
|
|
|
|
Get bpy value from store
|
|
|
|
"""
|
2019-02-14 21:02:53 +08:00
|
|
|
value_type = store_item.mtype
|
2019-02-14 20:13:28 +08:00
|
|
|
value_casted = None
|
|
|
|
store_value = store_item.body
|
|
|
|
|
|
|
|
if value_type == 'Vector':
|
2019-02-26 01:05:46 +08:00
|
|
|
value_casted = mathutils.Vector(
|
|
|
|
(store_value[0], store_value[1], store_value[2]))
|
2019-02-14 20:13:28 +08:00
|
|
|
|
|
|
|
return value_casted
|
|
|
|
|
2019-02-26 01:05:46 +08:00
|
|
|
|
2019-02-14 20:13:28 +08:00
|
|
|
def resolve_bpy_path(path):
|
|
|
|
"""
|
|
|
|
Get bpy property value from path
|
|
|
|
"""
|
2019-02-22 21:56:49 +08:00
|
|
|
|
|
|
|
t = time.perf_counter()
|
2019-02-14 20:13:28 +08:00
|
|
|
path = path.split('/')
|
|
|
|
|
|
|
|
obj = None
|
2019-02-14 21:02:53 +08:00
|
|
|
attribute = path[2]
|
2019-02-14 22:30:35 +08:00
|
|
|
logger.debug("resolving {}".format(path))
|
2019-02-26 01:05:46 +08:00
|
|
|
|
2019-02-14 20:13:28 +08:00
|
|
|
try:
|
2019-02-26 01:05:46 +08:00
|
|
|
obj = getattr(bpy.data, path[0])[path[1]]
|
|
|
|
attribute = getattr(obj, path[2])
|
|
|
|
logger.debug("done {} : {}".format(obj, attribute))
|
2019-02-14 20:13:28 +08:00
|
|
|
except AttributeError:
|
2019-02-14 22:30:35 +08:00
|
|
|
logger.debug(" Attribute not found")
|
2019-02-14 20:13:28 +08:00
|
|
|
|
|
|
|
return obj, attribute
|
|
|
|
|
2019-02-26 01:05:46 +08:00
|
|
|
|
2019-02-15 17:59:22 +08:00
|
|
|
def observer():
|
2019-02-14 22:30:35 +08:00
|
|
|
global client
|
|
|
|
|
2019-02-15 17:59:22 +08:00
|
|
|
try:
|
2019-02-26 01:05:46 +08:00
|
|
|
for key, values in client.property_map.items():
|
2019-02-15 17:59:22 +08:00
|
|
|
# if values.id == client.id:
|
|
|
|
obj, attr = resolve_bpy_path(key)
|
|
|
|
|
|
|
|
if attr != to_bpy(client.property_map[key]):
|
|
|
|
value_type, value = from_bpy(attr)
|
2019-02-26 01:05:46 +08:00
|
|
|
client.push_update(key, value_type, value)
|
2019-02-15 17:59:22 +08:00
|
|
|
except:
|
|
|
|
pass
|
2019-02-23 01:22:25 +08:00
|
|
|
return bpy.context.scene.session_settings.update_frequency
|
2019-02-14 22:30:35 +08:00
|
|
|
|
|
|
|
|
2019-02-12 00:02:25 +08:00
|
|
|
# CLIENT-SERVER
|
2019-02-22 23:46:35 +08:00
|
|
|
def refresh_window():
|
2019-02-12 17:18:39 +08:00
|
|
|
import bpy
|
2019-02-12 00:02:25 +08:00
|
|
|
|
2019-02-12 17:18:39 +08:00
|
|
|
bpy.ops.wm.redraw_timer(type='DRAW_WIN_SWAP', iterations=1)
|
2019-02-11 22:48:07 +08:00
|
|
|
|
2019-02-26 01:05:46 +08:00
|
|
|
|
2019-02-14 01:53:22 +08:00
|
|
|
def patch_scene(msg):
|
2019-02-14 22:30:35 +08:00
|
|
|
global client
|
2019-02-14 20:13:28 +08:00
|
|
|
|
2019-02-14 22:30:35 +08:00
|
|
|
if msg.id != client.id:
|
|
|
|
value = None
|
2019-02-14 21:02:53 +08:00
|
|
|
|
2019-02-14 22:30:35 +08:00
|
|
|
obj, attr = resolve_bpy_path(msg.key)
|
2019-02-26 01:05:46 +08:00
|
|
|
attr_name = msg.key.split('/')[2]
|
2019-02-14 20:13:28 +08:00
|
|
|
|
2019-02-26 01:05:46 +08:00
|
|
|
value = to_bpy(msg)
|
2019-02-22 21:56:49 +08:00
|
|
|
# print(msg.get)
|
2019-02-26 01:05:46 +08:00
|
|
|
logger.debug("Updating scene:\n object: {} attribute: {} , value: {}".format(
|
|
|
|
obj, attr_name, value))
|
2019-02-14 22:30:35 +08:00
|
|
|
try:
|
2019-02-26 01:05:46 +08:00
|
|
|
setattr(obj, attr_name, value)
|
2019-02-14 22:30:35 +08:00
|
|
|
except:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
logger.debug('no need to update scene on our own')
|
2019-02-14 20:13:28 +08:00
|
|
|
|
2019-02-26 01:05:46 +08:00
|
|
|
|
|
|
|
recv_callbacks = [patch_scene]
|
|
|
|
post_init_callbacks = [refresh_window]
|
2019-02-22 23:46:35 +08:00
|
|
|
|
|
|
|
|
2019-02-12 00:31:03 +08:00
|
|
|
class session_join(bpy.types.Operator):
|
|
|
|
bl_idname = "session.join"
|
|
|
|
bl_label = "join"
|
2019-02-11 22:48:07 +08:00
|
|
|
bl_description = "connect to a net server"
|
|
|
|
bl_options = {"REGISTER"}
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def poll(cls, context):
|
|
|
|
return True
|
|
|
|
|
|
|
|
def execute(self, context):
|
|
|
|
global client
|
|
|
|
|
2019-02-22 23:46:35 +08:00
|
|
|
net_settings = context.scene.session_settings
|
2019-02-26 01:05:46 +08:00
|
|
|
|
2019-02-22 23:46:35 +08:00
|
|
|
if net_settings.username == "DefaultUser":
|
2019-02-26 01:05:46 +08:00
|
|
|
net_settings.username = "{}_{}".format(
|
|
|
|
net_settings.username, randomStringDigits())
|
2019-02-22 23:46:35 +08:00
|
|
|
|
2019-02-12 17:18:39 +08:00
|
|
|
username = str(context.scene.session_settings.username)
|
2019-02-14 01:53:22 +08:00
|
|
|
|
2019-02-26 01:05:46 +08:00
|
|
|
client = net_components.Client(
|
|
|
|
id=username, on_recv=recv_callbacks, on_post_init=post_init_callbacks)
|
2019-03-06 21:01:33 +08:00
|
|
|
# time.sleep(1)
|
2019-02-14 20:13:28 +08:00
|
|
|
|
2019-02-11 22:48:07 +08:00
|
|
|
bpy.ops.asyncio.loop()
|
2019-02-15 17:59:22 +08:00
|
|
|
bpy.app.timers.register(observer)
|
2019-02-11 22:48:07 +08:00
|
|
|
|
2019-02-23 01:22:25 +08:00
|
|
|
net_settings.is_running = True
|
2019-02-11 22:48:07 +08:00
|
|
|
return {"FINISHED"}
|
|
|
|
|
2019-02-26 01:05:46 +08:00
|
|
|
|
2019-02-23 01:22:25 +08:00
|
|
|
class session_add_property(bpy.types.Operator):
|
|
|
|
bl_idname = "session.add_prop"
|
|
|
|
bl_label = "add"
|
2019-02-14 20:13:28 +08:00
|
|
|
bl_description = "broadcast a property to connected clients"
|
2019-02-12 00:02:25 +08:00
|
|
|
bl_options = {"REGISTER"}
|
|
|
|
|
2019-02-14 20:13:28 +08:00
|
|
|
property_path: bpy.props.StringProperty(default="None")
|
2019-02-12 00:02:25 +08:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def poll(cls, context):
|
|
|
|
return True
|
|
|
|
|
|
|
|
def execute(self, context):
|
|
|
|
global client
|
|
|
|
|
2019-02-14 20:13:28 +08:00
|
|
|
obj, attr = resolve_bpy_path(self.property_path)
|
2019-02-14 01:53:22 +08:00
|
|
|
|
2019-02-14 20:13:28 +08:00
|
|
|
if obj and attr:
|
|
|
|
key = self.property_path
|
|
|
|
value_type, value = from_bpy(attr)
|
2019-02-26 01:05:46 +08:00
|
|
|
|
|
|
|
client.push_update(key, value_type, value)
|
2019-02-12 00:02:25 +08:00
|
|
|
|
|
|
|
return {"FINISHED"}
|
|
|
|
|
2019-02-26 01:05:46 +08:00
|
|
|
|
2019-02-23 01:22:25 +08:00
|
|
|
class session_remove_property(bpy.types.Operator):
|
|
|
|
bl_idname = "session.remove_prop"
|
|
|
|
bl_label = "remove"
|
|
|
|
bl_description = "broadcast a property to connected clients"
|
|
|
|
bl_options = {"REGISTER"}
|
|
|
|
|
|
|
|
property_path: bpy.props.StringProperty(default="None")
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def poll(cls, context):
|
|
|
|
return True
|
|
|
|
|
|
|
|
def execute(self, context):
|
|
|
|
global client
|
|
|
|
|
|
|
|
try:
|
|
|
|
del client.property_map[self.property_path]
|
|
|
|
|
|
|
|
return {"FINISHED"}
|
|
|
|
except:
|
|
|
|
return {"CANCELED"}
|
|
|
|
|
2019-02-26 01:05:46 +08:00
|
|
|
|
2019-02-12 00:31:03 +08:00
|
|
|
class session_create(bpy.types.Operator):
|
|
|
|
bl_idname = "session.create"
|
|
|
|
bl_label = "create"
|
|
|
|
bl_description = "create to a net session"
|
2019-02-11 22:48:07 +08:00
|
|
|
bl_options = {"REGISTER"}
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def poll(cls, context):
|
|
|
|
return True
|
|
|
|
|
|
|
|
def execute(self, context):
|
|
|
|
global server
|
2019-02-12 00:02:25 +08:00
|
|
|
global client
|
2019-02-26 01:05:46 +08:00
|
|
|
|
2019-02-11 22:48:07 +08:00
|
|
|
server = net_components.Server()
|
2019-02-22 23:46:35 +08:00
|
|
|
time.sleep(0.1)
|
2019-02-12 00:02:25 +08:00
|
|
|
|
2019-02-22 23:46:35 +08:00
|
|
|
bpy.ops.session.join()
|
2019-02-26 01:05:46 +08:00
|
|
|
|
2019-02-15 17:59:22 +08:00
|
|
|
bpy.app.timers.register(observer)
|
2019-02-12 00:02:25 +08:00
|
|
|
return {"FINISHED"}
|
|
|
|
|
|
|
|
|
2019-02-12 00:31:03 +08:00
|
|
|
class session_stop(bpy.types.Operator):
|
|
|
|
bl_idname = "session.stop"
|
|
|
|
bl_label = "close"
|
|
|
|
bl_description = "stop net service"
|
2019-02-12 00:02:25 +08:00
|
|
|
bl_options = {"REGISTER"}
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def poll(cls, context):
|
|
|
|
return True
|
|
|
|
|
|
|
|
def execute(self, context):
|
|
|
|
global server
|
|
|
|
global client
|
2019-02-14 20:13:28 +08:00
|
|
|
|
2019-02-23 01:22:25 +08:00
|
|
|
net_settings = context.scene.session_settings
|
|
|
|
|
2019-02-26 01:05:46 +08:00
|
|
|
if server:
|
2019-02-12 00:02:25 +08:00
|
|
|
server.stop()
|
2019-02-12 00:49:28 +08:00
|
|
|
del server
|
|
|
|
server = None
|
2019-02-12 00:31:03 +08:00
|
|
|
if client:
|
|
|
|
client.stop()
|
2019-02-12 00:49:28 +08:00
|
|
|
del client
|
|
|
|
client = None
|
2019-02-12 00:02:25 +08:00
|
|
|
bpy.ops.asyncio.stop()
|
2019-02-23 01:22:25 +08:00
|
|
|
|
|
|
|
bpy.app.timers.unregister(observer)
|
|
|
|
net_settings.is_running = False
|
2019-02-12 00:02:25 +08:00
|
|
|
else:
|
2019-02-14 22:30:35 +08:00
|
|
|
logger.debug("No server/client running.")
|
2019-02-11 22:48:07 +08:00
|
|
|
|
|
|
|
return {"FINISHED"}
|
2019-02-09 01:34:10 +08:00
|
|
|
|
2019-02-12 00:02:25 +08:00
|
|
|
|
2019-02-12 01:10:56 +08:00
|
|
|
class session_settings(bpy.types.PropertyGroup):
|
2019-03-07 01:45:11 +08:00
|
|
|
username = bpy.props.StringProperty(
|
|
|
|
name="Username", default="user_{}".format(randomStringDigits()))
|
2019-02-12 01:10:56 +08:00
|
|
|
ip = bpy.props.StringProperty(name="localhost")
|
|
|
|
port = bpy.props.IntProperty(name="5555")
|
2019-02-14 20:13:28 +08:00
|
|
|
buffer = bpy.props.StringProperty(name="None")
|
2019-02-26 01:05:46 +08:00
|
|
|
is_running = bpy.props.BoolProperty(name="is_running", default=False)
|
|
|
|
update_frequency = bpy.props.FloatProperty(
|
|
|
|
name="update_frequency", default=0.008)
|
2019-02-23 01:22:25 +08:00
|
|
|
|
2019-02-12 01:10:56 +08:00
|
|
|
|
2019-03-07 01:45:11 +08:00
|
|
|
|
2019-02-13 19:11:05 +08:00
|
|
|
# TODO: Rename to match official blender convention
|
2019-02-08 22:44:02 +08:00
|
|
|
classes = (
|
2019-02-12 00:31:03 +08:00
|
|
|
session_join,
|
2019-02-23 01:22:25 +08:00
|
|
|
session_add_property,
|
2019-02-12 00:31:03 +08:00
|
|
|
session_stop,
|
|
|
|
session_create,
|
2019-02-12 01:10:56 +08:00
|
|
|
session_settings,
|
2019-02-23 01:22:25 +08:00
|
|
|
session_remove_property,
|
2019-02-08 22:44:02 +08:00
|
|
|
)
|
|
|
|
|
2019-02-09 01:34:10 +08:00
|
|
|
|
|
|
|
def register():
|
|
|
|
from bpy.utils import register_class
|
|
|
|
for cls in classes:
|
|
|
|
register_class(cls)
|
|
|
|
|
2019-02-26 01:05:46 +08:00
|
|
|
bpy.types.Scene.session_settings = bpy.props.PointerProperty(
|
|
|
|
type=session_settings)
|
2019-03-07 01:45:11 +08:00
|
|
|
|
2019-03-06 21:01:33 +08:00
|
|
|
# bpy.app.handlers.depsgraph_update_post.append(on_scene_evalutation)
|
2019-02-26 01:05:46 +08:00
|
|
|
|
|
|
|
|
2019-02-14 20:13:28 +08:00
|
|
|
def unregister():
|
2019-02-09 01:34:10 +08:00
|
|
|
from bpy.utils import unregister_class
|
|
|
|
for cls in reversed(classes):
|
|
|
|
unregister_class(cls)
|
2019-02-14 20:13:28 +08:00
|
|
|
|
2019-03-07 20:45:44 +08:00
|
|
|
|
|
|
|
|
2019-03-06 21:01:33 +08:00
|
|
|
|
2019-03-07 20:45:44 +08:00
|
|
|
# bpy.app.handlers.depsgraph_update_post.remove(on_scene_evalutation)
|
|
|
|
|
2019-02-12 01:10:56 +08:00
|
|
|
del bpy.types.Scene.session_settings
|
2019-02-09 01:34:10 +08:00
|
|
|
|
2019-02-08 22:44:02 +08:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2019-02-09 01:34:10 +08:00
|
|
|
register()
|