Commit d50c53cc authored by Bruno Laurencich's avatar Bruno Laurencich

uni, multi & broad OSC recv

parent 50a22a1d
......@@ -10,13 +10,13 @@ if "bpy" in locals():
imp.reload(connection)
imp.reload(panel)
imp.reload(remote_runner)
imp.reload(utils)
imp.reload(u)
imp.reload(receive)
else:
from . import connection
from . import panel
from . import remote_runner
from . import utils
from . import utils as u
from . import receive
......@@ -33,14 +33,25 @@ class Chord_Properties(bpy.types.PropertyGroup):
"""A collection to hold all the global chordata properties.
can be retrived at bpy.context.window_manager.chordata"""
state = props.EnumProperty(\
items=connection.possible_states(),\
items=u.possible_states(),\
options={'SKIP_SAVE'})
trans_method = props.EnumProperty(\
items= u.transmition_methods(),
default="Unicast")
net_submask = props.StringProperty( default = "255.255.255.0" )
notochord_msg = props.StringProperty( default = "" )
notochord_hostname = props.StringProperty( default = "notochord" )
notochord_parameters = props.StringProperty( default = "192.168.1.30 6565" )
#TODO: find a way to get the local addr
notochord_dest_addr = props.StringProperty( default = "192.168.1.30" )
notochord_dest_port = props.IntProperty( default = 6565 )
notochord_parameters = props.StringProperty( default = "" )
notochord_ip = props.StringProperty( default = "" )
......
No preview for this file type
......@@ -4,24 +4,13 @@ import threading
if "mdns" in locals():
import imp
imp.reload(mdns)
imp.reload(u)
else:
from . import mdns
from . import utils as u
def possible_states():
"""Returns the 'items' to create the states enum property.
The format is a list of tuples like:
[(identifier, name, description, icon, number), ...]
complete doc in:
https://docs.blender.org/api/blender_python_api_2_77_0/bpy.props.html#bpy.props.EnumProperty """
return [
("Disconnected", "no-conn", "", "COLOR_RED", 0),
("Connected", "conn", "", "COLOR_GREEN", 1),
("Transfering", "transfer", "", "POSE_DATA", 2),
("Searching..", "search1", "", "INLINK", 3),
("Searching.", "search2", "", "LINK", 4),
]
# =====================================================
# = OPERATOR: GET THE NOTOCHORD IP =
......@@ -49,16 +38,16 @@ class Chord_Get_Ip(bpy.types.Operator):
if self.query_thread.is_alive():
self.chord_global.state = \
possible_states()[self.counter%2+2][0]
u.possible_states()[self.counter%2+3][0]
else:
with self.query_lock:
if self.found_ip:
self.chord_global.state = possible_states()[1][0]
self.chord_global.state = u.possible_states()[1][0]
self.report({"INFO"}, "IP found :" + self.found_ip)
self.chord_global.notochord_ip = self.found_ip
else:
self.chord_global.state = possible_states()[0][0]
self.chord_global.state = u.possible_states()[0][0]
self.report({"WARNING"}, "IP NOT found")
......@@ -99,7 +88,7 @@ class Chord_Get_Ip(bpy.types.Operator):
def execute(self, context):
self.chord_global = context.window_manager.chordata
self.chord_global.state = possible_states()[0][0]
self.chord_global.state = u.possible_states()[0][0]
self.chord_global.notochord_ip = ""
self.report({"INFO"},"ip query ongoing..")
......
......@@ -91,14 +91,11 @@ def osc_terminate():
def osc_process():
print("Process OSC")
while True:
job = execute_queue.wait_for_job(0)
if job is osctoolspools.LAST_JOB or job is None:
break
try:
print("DOING JOB")
job()
except:
generallogger.exception("Failure in method execution job")
......
......@@ -256,6 +256,7 @@ class UdpMcChannel(oscchannel.TransportChannel):
self.udpsockspec = sockspeclist[0]
def open(self):
# import pdb; pdb.set_trace()
# Override parent method.
if self.is_reader:
# Open socket with family (IPV4, IPV6, UNIX...) and type.
......@@ -274,6 +275,13 @@ class UdpMcChannel(oscchannel.TransportChannel):
if self.udpread_nonblocking:
self.udpsock.setblocking(False)
self.udpsock.bind(self.udpsockspec.sockaddr)
if self.mcast_enabled:
mreq = struct.pack("4sl",
socket.inet_aton(self.udpsockspec.sockaddr[0]),
socket.INADDR_ANY)
self.udpsock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
if self.logger is not None:
self.logger.info("UDP channel %r open read on %s.",
self.chaname, repr(self.udpsockspec))
......
......@@ -58,13 +58,23 @@ class Chord_tools_panel(bpy.types.Panel):
layout.operator("chordata.close_notochord", "Stop")
layout.operator("chordata.receive", "Receive", icon='RADIO')
layout.label("Transmision method")
layout.prop(self.chord_global, "trans_method", expand=True)
### PROPS BOX
layout.separator()
box = layout.box()
box.label("Ip")
box.prop(self.chord_global, "notochord_dest_addr", text="")
box.label("Port")
box.prop(self.chord_global, "notochord_dest_port", text="")
box.label("Parameters")
box.prop(self.chord_global, "notochord_parameters", text="")
layout.prop(self.chord_global, "notochord_hostname")
box.prop(self.chord_global, "notochord_parameters")
box.prop(self.chord_global, "notochord_hostname")
# conn_state = get_conn_state_attr(context, "name")
......
......@@ -2,8 +2,11 @@ import bpy
from sys import path
from os.path import join, abspath, dirname
path.append(join(dirname(abspath(__file__)), "osc4py3-1.0.4"))
from osc4py3.as_eventloop import *
from osc4py3.as_comthreads import *
from osc4py3 import oscmethod as osm
from osc4py3 import oscchannel as osch
# from struct import pack as struct_pack
# import socket
if "u" in locals():
import imp
......@@ -11,9 +14,9 @@ if "u" in locals():
else:
from . import utils as u
def handlerfunction(addr, w, x, y, z):
u.write_blender_console(bpy.context, "{} ({:06.4f} {:06.4f} {:06.4f} {:06.4f})".format(addr, w, x, y, z))
print("{} ({:06.4f} {:06.4f} {:06.4f} {:06.4f})".format(addr, w, x, y, z))
D = bpy.data
class Chord_Receive_OSC(bpy.types.Operator):
......@@ -22,6 +25,14 @@ class Chord_Receive_OSC(bpy.types.Operator):
bl_label = "Chordata: Receive pose Data"
bl_options = {'REGISTER'}
def handlerfunction(self, addr, w, x, y, z):
print("{} ({:06.4f} {:06.4f} {:06.4f} {:06.4f})".format(addr, w, x, y, z))
D.objects["Cube"].rotation_quaternion.w = w
D.objects["Cube"].rotation_quaternion.x = x
D.objects["Cube"].rotation_quaternion.y = y
D.objects["Cube"].rotation_quaternion.z = z
# ----------- MODAL -----------
def modal(self, context, event):
......@@ -32,6 +43,9 @@ class Chord_Receive_OSC(bpy.types.Operator):
return {'CANCELLED'}
if event.type == 'TIMER':
for r in context.area.regions:
r.tag_redraw()
osc_process()
#receive
......@@ -55,8 +69,29 @@ class Chord_Receive_OSC(bpy.types.Operator):
wm.modal_handler_add(self)
osc_startup()
osc_udp_server("0.0.0.0", 6565, "chordata_armature_server")
osc_method("*", handlerfunction,\
server = u.transmition_methods(False)[self.chord_global.trans_method][2]
addr = "0.0.0.0"
if self.chord_global.trans_method == "Multicast":
addr = self.chord_global.notochord_dest_addr
port= self.chord_global.notochord_dest_port
print(server, addr, port)
try:
server(addr, port, "chordata_armature_server")
except Exception as e:
u.write_blender_console(context, str(e))
self.report({"ERROR"}, "There was a problem creating the UDP server")
# udpsock = osch.get_channel("chordata_armature_server").udpsock
# mreq = struct_pack("4sl",
# socket.inet_aton("239.0.0.1"),
# socket.INADDR_ANY)
# udpsock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
osc_method("*", self.handlerfunction,\
argscheme=osm.OSCARG_ADDRESS + osm.OSCARG_DATAUNPACK)
self.report({"INFO"}, "Receiving OSC")
......@@ -69,5 +104,6 @@ class Chord_Receive_OSC(bpy.types.Operator):
def cancel(self, context):
wm = context.window_manager
wm.event_timer_remove(self._timer)
osch.get_channel("chordata_armature_server").terminate()
osc_terminate()
......@@ -72,6 +72,14 @@ class Chord_Start_Notochord(Chord_Runner_Base):
for p in self.chord_global.notochord_parameters.split():
params += "p=%s&" % p
addr = self.chord_global.notochord_dest_addr
if self.chord_global.trans_method == "Broadcast":
addr = self.chord_global.notochord_dest_addr
subm = self.chord_global.net_submask
addr = u.get_broadcast_addr(addr, subm)
params += "p=%s&p=%s" % (addr, self.chord_global.notochord_dest_port)
r = http_get("http://%s/command/init%s" % \
(self.chord_global.notochord_ip, params),\
timeout=1)
......
import bpy
from sys import path
from os.path import join, abspath, dirname
path.append(join(dirname(abspath(__file__)), "osc4py3-1.0.4"))
from osc4py3.as_comthreads import osc_udp_server, osc_multicast_server, osc_broadcast_server
import ipaddress
def transmition_methods(as_tuples = True):
"""Transmision method to be used"""
d = {
"Unicast": ("uni", 0, osc_udp_server),
"Multicast": ("multi", 1, osc_multicast_server),
"Broadcast": ("broad", 2, osc_broadcast_server),
}
if as_tuples:
return [(name, v[0], name, "", v[1] ) for name, v in d.items()]
return d
def get_broadcast_addr(addr, submask):
return ipaddress.IPv4Network( "%s/%s" % (addr, submask), False).broadcast_address
def possible_states():
"""Returns the 'items' to create the states enum property.
The format is a list of tuples like:
[(identifier, name, description, icon, number), ...]
complete doc in:
https://docs.blender.org/api/blender_python_api_2_77_0/bpy.props.html#bpy.props.EnumProperty """
return [
("Disconnected", "no-conn", "", "COLOR_RED", 0),
("Connected", "conn", "", "COLOR_GREEN", 1),
("Transfering", "transfer", "", "POSE_DATA", 2),
("Searching..", "search1", "", "INLINK", 3),
("Searching.", "search2", "", "LINK", 4),
]
def write_blender_console(context, t):
screen = context.screen
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment