-
Notifications
You must be signed in to change notification settings - Fork 6
/
extension.js
130 lines (108 loc) · 4.3 KB
/
extension.js
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
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
var vscode = require('vscode');
var net = require('net');
// the socket for the python ports created at startup of script
var socketPython;
// the socket for the mel port create at startup of script
var socketMel;
// variables for ports, can be over ridden in config if you wish to use another port, going to default to 7001 for mel and 7002 for python
var melPort=7001;
var pythonPort=7002;
var mayahost='localhost';
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
function activate(context) {
console.log('"mayaport" is now active!');
console.log('To use make sure the following code is run in Maya python ');
var msg=`
import maya.cmds as cmds
try:
cmds.commandPort(name=":7001", close=True)
except:
cmds.warning('Could not close port 7001 (maybe it is not opened yet...)')
try:
cmds.commandPort(name=":7002", close=True)
except:
cmds.warning('Could not close port 7002 (maybe it is not opened yet...)')
cmds.commandPort(name=":7001", sourceType="mel")
cmds.commandPort(name=":7002", sourceType="python")
`;
console.log(msg);
// The command has been defined in the package.json file
// Now provide the implementation of the command with registerCommand
// The commandId parameter must match the command field in package.json
var disposable = vscode.commands.registerCommand('extension.openMayaPort', function ()
{
var config=vscode.workspace.getConfiguration('mayaport');
if(config.has("melPortID")){
melPort=config.get("melPortID");
}
if(config.has("pythonPortID")){
pythonPort=config.get("pythonPortID");
}
if(config.has("mayahost")){
mayahost=config.get("mayahost");
}
// create a connection to maya on port 7001 for Mel commands
socketMel = net.createConnection( melPort,mayahost);
socketMel.on('error', function(error) {
vscode.window.showErrorMessage("Unable to connect to port " + melPort + " on Host " + mayahost +" in maya for Mel " + error.code);
});
socketMel.on('data', function(data){
console.log(data.toString());
});
// create a connection to maya on port 7002 for python commands
socketPython = net.createConnection(pythonPort,mayahost);
socketPython.on('error', function(error) {
vscode.window.showErrorMessage("Unable to connect to port "+ pythonPort +" on Host "+ mayahost+" in maya for Python " + error.code);
});
socketPython.on('data', function(data){
console.log(data.toString());
});
});
context.subscriptions.push(disposable);
var disposable = vscode.commands.registerCommand('extension.sendPythonToMaya', function () {
var editor = vscode.window.activeTextEditor;
var selection = editor.selection;
var text;
// if we have selected text only send this
if (selection.isEmpty != true )
{
text = editor.document.getText(selection);
}
// otherwise send the whole document
else
{
text=editor.document.getText();
}
socketPython.write(text);
socketPython.write('\n');
vscode.window.setStatusBarMessage("Python sent to Maya");
});
context.subscriptions.push(disposable);
var disposable = vscode.commands.registerCommand('extension.sendMelToMaya', function () {
var editor = vscode.window.activeTextEditor;
var selection = editor.selection;
var text;
if (selection.isEmpty != true )
{
text = editor.document.getText(selection);
}
else
{
text=editor.document.getText();
}
socketMel.write(text);
socketMel.write('\n');
vscode.window.setStatusBarMessage("mel sent to Maya");
});
context.subscriptions.push(disposable);
}
exports.activate = activate;
// this method is called when your extension is deactivated
function deactivate() {
socketMel.close();
socketPython.close();
}
exports.deactivate = deactivate;