Thursday, April 17, 2008

Python CommandPort (First entry!)

Maya lets the user access to its scene over the network with commandPort. Recently I felt the need of sending Python command instead of MEL script. I could use commadnPort and use MEL commnd 'python' everytime but I didn't like the way. I searched highend3d for it. I found a tool 'mapy' but it's fully GUI based, and the source code doesn't seem to be open. So I made it myself. Since I used asyncore (one of Python module for easy network programming), currently it doesn't support multiple connections, and I made modification on purpose, it's somewhat different from Maya's standard commandPort.
Some of big differences are:

- You can open one port at a time.
- Close connection every time a command is executed and the error message is transmitted back.
- Only error messages are returned. (Currently it always returns null string when "MEL" is specified).
- Command is terminated until the server receives the terminator string.

(You may feel some of them big restrictions but for my usage it's enough ;)

To use the plug-in, just place everything in the plug-in dir and load (not, then execute a MEL command (Why there are pyCommandPort and pyCommandPortImpl? I will write the reason on a different post).


It opens a port 7070 by default. You can see it's working using telnet command

telnet localhost 7070

and type

print range(5)

then you'll see the result in the script editor

# print range(5) #
[0, 1, 2, 3, 4]

There are several flags available.

Address. e.g. "", "localhost", "".
It defaults to "".
Port number. Any unused port number between 1024-65535 will do.
It defaults to 7070.
The command fails if the port is already used.
In that case use another number.
If this flag is specified, it stops the server.
Command terminator string.
It defaults to '\r\n' (i.e. enter)
"Python" or "MEL". Defaults to "Python"
On or off.
If on, it shows commands to be executed on the script
editor. It defaults to on.
Query sever settings. Use with other flags.
It returns value only when the server is running
except -shutdown.

I put the source code here. Note that this is still a beta version.

No comments: