-
Notifications
You must be signed in to change notification settings - Fork 2
/
streamer.py
99 lines (77 loc) · 2.29 KB
/
streamer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"""
Module to setup callback for streaming from Maya's (the client) side
"""
import errno
import socket
from cStringIO import StringIO
import maya.OpenMaya as om
SERVER = "127.0.0.1"
PORT = 5051
if 'STREAM_CALLBACK' in globals():
try:
om.MMessage.removeCallback(STREAM_CALLBACK)
except RuntimeError:
pass
STREAM_CALLBACK = None
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
def open_stream(addr=(SERVER, PORT)):
"""
Add callback to stream output of script editor when changed
:param addr: tuple. ip address and port number
"""
global STREAM_CALLBACK
print("Streaming ScriptEditor at ({}:{})\n".format(addr[0], addr[1]))
STREAM_CALLBACK = om.MCommandMessage.addCommandOutputCallback(
stream_to_console,
addr
)
def close_stream():
"""
Remove callback to stream output of script editor
"""
global STREAM_CALLBACK
print("Disable Streaming ScriptEditor\n")
om.MMessage.removeCallback(STREAM_CALLBACK)
STREAM_CALLBACK = None
def stream_to_console(msg, mtype, addr):
"""
Stream result of the callback back to the listen server
https://github.com/justinfx/MayaSublime/
:param msg: str. MEL command being executed (i.e. callback output)
:param mtype: OpenMaya.MMessage.MessageType. type of the message
:param addr: tuple. ip address and port number
"""
buf = StringIO()
buf.seek(0)
buf.truncate()
if mtype == om.MCommandMessage.kWarning:
buf.write('# Warning: ')
buf.write(msg)
buf.write(' #\n')
elif mtype == om.MCommandMessage.kError:
buf.write('// Error: ')
buf.write(msg)
buf.write(' //\n')
elif mtype == om.MCommandMessage.kResult:
buf.write('# Result: ')
buf.write(msg)
buf.write(' #\n')
else:
buf.write(msg)
buf.seek(0)
buf_size = 8*1024
while True:
while buf_size > 0:
pos = buf.tell()
part = buf.read(buf_size)
if not part:
return
try:
client.sendto(part, addr)
except Exception as e:
if e.errno == errno.EMSGSIZE:
buf_size /= 2
buf.seek(pos)
continue
raise
break