forked from akmrsk/maxscript-mode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
send_to_max.py
86 lines (73 loc) · 2.36 KB
/
send_to_max.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import os
import tempfile
import maxCom
CMD_SUFFIX ="\r\n"
def cleanCmdLineString(text):
""" remove any double backslashes and carriage returns
that may have beend added from the command line translation."""
text = text.replace("\\\\","\\")
text = text.replace("\r","")
return text
def sendCmdToMax(cmd):
com = maxCom.getCommunicator()
com.sendCmdToMax(cmd)
def getOutputFromMax():
com = maxCom.getCommunicator()
return com.getOutputFromMax()
def executeFile(fpath):
name, ext = os.path.splitext(fpath)
if ext.lower() == ".ms":
cmd = ('fileIn (@"%s")'+CMD_SUFFIX) % fpath
sendCmdToMax(cmd)
elif ext.lower() == ".py":
cmd = ('python.executefile (@"%s")'+CMD_SUFFIX) % fpath
sendCmdToMax(cmd)
else:
print "Unknown file extension: %s" % ext
def writeTempFile(text, ext):
tmpdir = tempfile.gettempdir()
tmpfile = "sendtomaxtempfile" + ext
fpath = os.path.join(tmpdir, tmpfile)
with open(fpath, 'w') as f:
f.write(text)
return fpath
def executeMaxScript(code):
""" Execute maxscript code by saving it to a temporary .ms file and
send a fileIn command to Max.
When only one line of code is present, execute it directly instead.
"""
if "\n" in code:
fpath = writeTempFile(code, ".ms")
executeFile(fpath)
else:
cmd = code+CMD_SUFFIX
sendCmdToMax(cmd)
def executeMaxPython(code):
fpath = writeTempFile(code, ".py")
executeFile(fpath)
def clearListenerOutput():
com = maxCom.getCommunicator()
com.clearListenerOutput()
def executeThis():
if len(sys.argv)>2:
# when receiving strings through the cmdline, we will have
# double backslashes and carriage returns, killing syntax.
if sys.argv[1] == "-ms":
script = cleanCmdLineString(sys.argv[2])
executeMaxScript(script)
elif sys.argv[1] == "-py":
script = cleanCmdLineString(sys.argv[2])
executeMaxPython(script)
elif sys.argv[1] == "-f":
executeFile(sys.argv[2])
elif len(sys.argv)>1:
if sys.argv[1] == "-c":
clearListenerOutput()
elif sys.argv[1] == "-g":
res = getOutputFromMax()
print cleanCmdLineString(res)
if __name__ == "__main__":
executeThis()