multi-user/net_operators.py

110 lines
2.1 KiB
Python
Raw Normal View History

import bpy
from . import net_components
session = None
2019-02-09 01:34:10 +08:00
class join(bpy.types.Operator):
bl_idname = "session.join"
2019-02-09 01:34:10 +08:00
bl_label = "join"
bl_description = "Connect to a net session"
bl_options = {"REGISTER"}
@classmethod
def poll(cls, context):
return True
def execute(self, context):
2019-02-09 01:34:10 +08:00
# global session
2019-02-09 01:34:10 +08:00
if session.join():
bpy.ops.asyncio.loop()
else:
print('fail to create session, avorting loop')
return {"FINISHED"}
2019-02-09 01:34:10 +08:00
class create(bpy.types.Operator):
bl_idname = "session.create"
bl_label = "create"
bl_description = "create to a net session"
bl_options = {"REGISTER"}
@classmethod
def poll(cls, context):
return True
def execute(self, context):
global session
2019-02-09 01:34:10 +08:00
if session.create():
bpy.ops.asyncio.loop()
else:
print('fail to create session, avorting loop')
return {"FINISHED"}
2019-02-09 01:34:10 +08:00
class send(bpy.types.Operator):
bl_idname = "session.send"
2019-02-09 01:34:10 +08:00
bl_label = "Send"
bl_description = "Connect to a net session"
bl_options = {"REGISTER"}
message: bpy.props.StringProperty(default="Hi")
@classmethod
def poll(cls, context):
return True
def execute(self, context):
global session
2019-02-09 01:34:10 +08:00
session.send(self.message)
return {"FINISHED"}
2019-02-09 01:34:10 +08:00
class close(bpy.types.Operator):
bl_idname = "session.close"
2019-02-09 01:34:10 +08:00
bl_label = "Close session"
bl_description = "Connect to a net session"
bl_options = {"REGISTER"}
@classmethod
def poll(cls, context):
return True
def execute(self, context):
global session
session.close()
2019-02-09 01:34:10 +08:00
bpy.ops.asyncio.stop()
return {"FINISHED"}
2019-02-09 01:34:10 +08:00
classes = (
join,
2019-02-09 01:34:10 +08:00
create,
close,
send,
)
2019-02-09 01:34:10 +08:00
def register():
global session
session = net_components.Session()
from bpy.utils import register_class
for cls in classes:
register_class(cls)
def unregister():
from bpy.utils import unregister_class
for cls in reversed(classes):
unregister_class(cls)
if __name__ == "__main__":
2019-02-09 01:34:10 +08:00
register()