forked from justinfx/MayaSublime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MayaSublime.py
574 lines (429 loc) · 13.4 KB
/
MayaSublime.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
# ST2/ST3 compat
from __future__ import print_function
import os
import re
import sys
import time
import uuid
import errno
import socket
import os.path
import textwrap
import threading
import traceback
from telnetlib import Telnet
import sublime, sublime_plugin
if sublime.version() < '3000':
# we are on ST2 and Python 2.X
_ST3 = False
else:
_ST3 = True
# Our default plugin state
_settings = {
# State of plugin settings
'host': '127.0.0.1',
'mel_port': 7001,
'py_port': 7002,
'strip_comments': True,
'no_collisions': True,
'maya_output': False,
'undo': False,
# Internal state
'_t_reader': None,
}
# A place to globally store a reference to our Thread
_ATTR_READER_THREAD = '_MayaSublime_Reader_Thread'
def plugin_unloaded():
"""
Hook called by ST3 when the plugin is unloaded
"""
# Clean up our thread
reader = _settings['_t_reader']
if reader is not None:
reader.shutdown()
_settings['_t_reader'] = None
class enable_maya_output(sublime_plugin.ApplicationCommand):
def run(self, *args):
_settings['maya_output'] = True
MayaReader.set_maya_output_enabled(True)
class disable_maya_output(sublime_plugin.ApplicationCommand):
def run(self, *args):
_settings['maya_output'] = False
MayaReader.set_maya_output_enabled(False)
class send_to_mayaCommand(sublime_plugin.TextCommand):
# Match single-line comments in MEL/Python
RX_COMMENT = re.compile(r'^\s*(//|#)')
def run(self, edit):
# Do we have a valid source language?
syntax = self.view.settings().get('syntax')
if re.search(r'python', syntax, re.I):
lang = 'python'
sep = '\n'
elif re.search(r'mel', syntax, re.I):
lang = 'mel'
sep = '\r'
else:
print('No Maya-Recognized Language Found')
return
isPython = (lang=='python')
# Apparently ST3 doesn't always sync up its latest
# plugin settings?
if _settings['host'] is None:
sync_settings()
# Check the current selection size to determine
# how we will send the source to be executed.
selections = self.view.sel() # Returns type sublime.RegionSet
selSize = 0
for sel in selections:
if not sel.empty():
selSize += 1
snips = []
# If nothing is selected, we will use an approach that sends an
# entire source file, and tell Maya to execute it.
if selSize == 0:
execType = 'execfile'
print("Nothing Selected, Attempting to exec entire file")
if self.view.is_dirty():
sublime.error_message("Save Changes Before Maya Source/Import")
return
file_path = self.view.file_name()
if file_path is None:
sublime.error_message("File must be saved before sending to Maya")
return
plat = sublime_plugin.sys.platform
if plat == 'win32':
file_path = file_path.replace('\\','\\\\')
print("FILE PATH:",file_path)
if lang == 'python':
snips.append(file_path)
else:
snips.append('rehash; source "{0}";'.format(file_path))
# Otherwise, we are sending snippets of code to be executed
else:
execType = 'exec'
file_path = ''
substr = self.view.substr
match = self.RX_COMMENT.match
stripComments = _settings['strip_comments']
# Build up all of the selected lines, while removing single-line comments
# to simplify the amount of data being sent.
for sel in selections:
if stripComments:
snips.extend(line for line in substr(sel).splitlines() if not match(line))
else:
snips.extend(substr(sel).splitlines())
mCmd = str(sep.join(snips))
if not mCmd:
return
print('Sending {0}:\n{1!r}\n...'.format(lang, mCmd[:200]))
if lang == 'python':
# We need to wrap our source string into a template
# so that it gets executed properly on the Maya side
no_collide = _settings['no_collisions']
create_undo = _settings["undo"]
opts = dict(
xtype=execType, cmd=mCmd, fp=file_path,
ns=no_collide, undo=create_undo,
)
mCmd = PY_CMD_TEMPLATE.format(**opts)
if _settings["maya_output"]:
# In case maya was restarted, we can make sure the
# callback is always installed
MayaReader.set_maya_output_enabled(_settings["maya_output"])
_send_to_maya(mCmd, lang, wrap=False)
def _send_to_maya(cmd, lang='python', wrap=True, quiet=False):
"""
Send stringified Python code to Maya, to be executed.
"""
if _settings['host'] is None:
sync_settings()
host = _settings['host']
port = _settings['py_port'] if lang=='python' else _settings['mel_port']
if lang == 'python' and wrap:
no_collide = _settings['no_collisions']
create_undo = _settings["undo"]
opts = dict(xtype='exec', cmd=cmd, fp='', ns=no_collide, undo=create_undo)
cmd = PY_CMD_TEMPLATE.format(**opts)
c = None
try:
c = Telnet(host, int(port), timeout=3)
c.write(_py_str(cmd))
except Exception:
e = sys.exc_info()[1]
err = str(e)
msg = "Failed to communicate with Maya (%(host)s:%(port)s)):\n%(err)s" % locals()
if quiet:
print(msg)
return False
sublime.error_message(msg)
raise
else:
time.sleep(.1)
finally:
if c is not None:
c.close()
return True
def _py_str(s):
"""Encode a py3 string if needed"""
if _ST3:
return s.encode(encoding='UTF-8')
return s
def settings_obj():
return sublime.load_settings("MayaSublime.sublime-settings")
_IS_SYNCING = False
def sync_settings():
global _IS_SYNCING
if _IS_SYNCING:
return
_IS_SYNCING = True
try:
_sync_settings()
finally:
_IS_SYNCING = False
def _sync_settings():
so = settings_obj()
_settings['host'] = so.get('maya_hostname')
_settings['py_port'] = so.get('python_command_port')
_settings['mel_port'] = so.get('mel_command_port')
_settings['strip_comments'] = so.get('strip_sending_comments')
_settings['no_collisions'] = so.get('no_collisions')
_settings['maya_output'] = so.get('receive_maya_output')
_settings['undo'] = so.get('create_undo')
MayaReader._st2_remove_reader()
if _settings['maya_output'] is not None:
MayaReader.set_maya_output_enabled(_settings["maya_output"])
# A template wrapper for sending Python source safely
# over the socket.
# Executes in a private namespace to avoid collisions
# with the main environment in Maya.
# Also handles catches and printing exceptions so that
# they are not masked.
PY_CMD_TEMPLATE = textwrap.dedent('''
import traceback
import __main__
import maya.cmds
namespace = __main__.__dict__.get('_sublime_SendToMaya_plugin')
if not namespace:
namespace = __main__.__dict__.copy()
__main__.__dict__['_sublime_SendToMaya_plugin'] = namespace
try:
if {undo}:
maya.cmds.undoInfo(openChunk=True, chunkName="MayaSublime Code")
if {ns}:
namespace['__file__'] = {fp!r}
{xtype}({cmd!r}, namespace, namespace)
else:
{xtype}({cmd!r}, __main__.__dict__, __main__.__dict__)
except:
traceback.print_exc()
finally:
if {undo}:
maya.cmds.undoInfo(closeChunk=True)
''')
PY_MAYA_CALLBACK = textwrap.dedent(r'''
import sys
import errno
import socket
import maya.OpenMaya
try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
if '_MayaSublime_ScriptEditorOutput_CID' not in globals():
_MayaSublime_ScriptEditorOutput_CID = None
if '_MayaSublime_SOCK' not in globals():
_MayaSublime_SOCK = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
def _MayaSublime_streamScriptEditor(enable, host="127.0.0.1", port=5123, quiet=False):
om = maya.OpenMaya
global _MayaSublime_ScriptEditorOutput_CID
cid = _MayaSublime_ScriptEditorOutput_CID
# Only print if we are really changing state
if enable and cid is None:
sys.stdout.write("[MayaSublime] Enable Streaming ScriptEditor " \
"({0}:{1})\n".format(host, port))
elif not enable and cid is not None:
sys.stdout.write("[MayaSublime] Disable Streaming ScriptEditor\n")
if cid is not None:
om.MMessage.removeCallback(cid)
_MayaSublime_ScriptEditorOutput_CID = None
if not enable:
return
buf = StringIO()
def _streamToMayaSublime(msg, msgType, *args):
buf.seek(0)
buf.truncate()
if msgType != om.MCommandMessage.kDisplay:
buf.write('[MayaSublime] ')
if msgType == om.MCommandMessage.kWarning:
buf.write('# Warning: ')
buf.write(msg)
buf.write(' #\n')
elif msgType == om.MCommandMessage.kError:
buf.write('// Error: ')
buf.write(msg)
buf.write(' //\n')
elif msgType == om.MCommandMessage.kResult:
buf.write('# Result: ')
buf.write(msg)
buf.write(' #\n')
else:
buf.write(msg)
buf.seek(0)
# Start with trying to send 8kb packets
bufsize = 8*1024
# Loop until the buffer is empty
while True:
while bufsize > 0:
# Save our position in case we error
# and need to roll back
pos = buf.tell()
part = buf.read(bufsize)
if not part:
# Buffer is empty. Nothing else to send
return
try:
_MayaSublime_SOCK.sendto(part, (host, port))
except Exception as e:
if e.errno == errno.EMSGSIZE:
# We have hit a message size limit.
# Scale down and try the packet again
bufsize /= 2
buf.seek(pos)
continue
# Some other error
raise
# Message sent without error
break
cid = om.MCommandMessage.addCommandOutputCallback(_streamToMayaSublime)
_MayaSublime_ScriptEditorOutput_CID = cid
''')
class MayaReader(threading.Thread):
"""
A threaded reader that monitors for published ScriptEditor
output from Maya.
Installs a ScriptEditor callback to Maya to produce messages.
"""
# Max number of bytes to read from each packet.
BUFSIZE = 64 * 1024 # 64KB is max UDP packet size
# Signal to stop a receiving MayaReader
STOP_MSG = _py_str('MayaSublime::MayaReader::{0}'.format(uuid.uuid4()))
# # Stringified ScriptEditor callback code to install in Maya
# PY_MAYA_CALLBACK = open(os.path.join(os.path.dirname(__file__),
# "lib/pubScriptEditor.py")).read()
PY_MAYA_CALLBACK = PY_MAYA_CALLBACK
def __init__(self, host='127.0.0.1', port=0):
super(MayaReader, self).__init__()
self.daemon = True
self._running = threading.Event()
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.sock.bind((host, port))
def port(self):
"""Get the port number being used by the socket"""
_, port = self.sock.getsockname()
return port
def is_running(self):
"""Return true if the thread is running"""
return self._running.is_set()
def shutdown(self):
"""Stop the monitoring of Maya output"""
self._running.clear()
# Send shutdown message to local UDP
self.sock.sendto(self.STOP_MSG, self.sock.getsockname())
def run(self):
prefix = '[MayaSublime] '
print("{0}started on port {1}".format(prefix, self.port()))
fails = 0
self._running.set()
while self._running.is_set():
try:
msg, addr = self.sock.recvfrom(self.BUFSIZE)
except Exception as e:
print("Failed while reading output from Maya:")
traceback.print_exc()
# Prevent runaway failures from spinning
fails += 1
if fails >= 10:
# After too many failures in a row
# wait a bit
fails = 0
time.sleep(5)
continue
fails = 0
if msg == self.STOP_MSG:
break
if _ST3:
msg = msg.decode()
sys.stdout.write(msg)
print("{0}MayaReader stopped".format(prefix))
def _set_maya_callback_enabled(self, enable, quiet=False):
"""
Enable or disable the actual publishing of ScriptEditor output from Maya
"""
host, port = self.sock.getsockname()
cmd = "_MayaSublime_streamScriptEditor({0}, host={1!r}, port={2})".format(enable, host, port)
return _send_to_maya(cmd, quiet=quiet, wrap=_settings['no_collisions'])
@classmethod
def _st2_remove_reader(cls):
"""
A hack to work around SublimeText2 not having a
module level hook for when the plugin is loaded
and unloaded.
Need to store a reference to our thread that doesn't
get blown away when the plugin reloads, so that we
can clean it up.
"""
if _ST3:
return
import __main__
reader = getattr(__main__, _ATTR_READER_THREAD, None)
if reader:
reader.shutdown()
setattr(__main__, _ATTR_READER_THREAD, None)
@classmethod
def _st2_replace_reader(cls, reader):
"""
A hack to work around SublimeText2 not having a
module level hook for when the plugin is loaded
and unloaded.
Need to store a reference to our thread that doesn't
get blown away when the plugin reloads, so that we
can clean it up and replace it with another.
"""
if _ST3:
return
cls._st2_remove_reader()
import __main__
setattr(__main__, _ATTR_READER_THREAD, reader)
@classmethod
def install_maya_callback(cls):
"""Send the callback logic to Maya"""
return _send_to_maya(cls.PY_MAYA_CALLBACK, quiet=True, wrap=_settings['no_collisions'])
@classmethod
def set_maya_output_enabled(cls, enable):
# Make sure the Maya filtering callback code
# is set up already
ok = cls.install_maya_callback()
quiet = not ok
reader = _settings.get('_t_reader')
# handle disabling the reader
if not enable:
if reader:
reader.shutdown()
reader._set_maya_callback_enabled(False, quiet)
return
# handle enabling the reader
if reader and reader.is_alive():
# The reader is already running
reader._set_maya_callback_enabled(True, quiet)
return
# Start the reader
reader = cls()
reader.start()
_settings['_t_reader'] = reader
cls._st2_replace_reader(reader)
reader._set_maya_callback_enabled(True, quiet)
# Add callbacks for monitoring setting changes
settings_obj().clear_on_change("MayaSublime.settings")
settings_obj().add_on_change("MayaSublime.settings", sync_settings)
sync_settings()