

It's been a while since I updated here, but I thought I'd throw out a tidbit from something I've been working on.
Part of any vfx pipeline project involves making your different software pieces communicate with each other. Part of that, of course, is being able to write common file formats. Sometimes that's for sharing generic data, such as through XML, or basic channel information that can be written to a tab or space-delimited text file sometimes referred to as a chan file.
Other times, though, you'll want the packages to communicate more intimately, and Maya provides a method to closely integrate different software packages with it as long as the other packages have the ability to do port-level communication. If you haven't written IP protocol software before, this can be a little daunting, so I'm going to share a couple short code tidbits that'll make talking to Maya a little easier. I'm assuming some basic MEL experience, and some background in Python wouldn't hurt either, though you should be able to follow along with any amount of coding background. I'll be skipping the tedious details though, so if you need a programming refresher that's beyond the scope of this post.
First, you'll want to pick the port that Maya is going to listen on. For my example, I'll use 7777 - basically because it's not used by another widely used protocol, and because it'll be easy to identify in the example code.
In Maya, either in the script editor or as part of another MEL script, you'll want to execute:
commandPort -n ":7777";
Now, this will open a port on 7777 which I should give you a little warning about: ANYTHING that is sent to that port will be executed by Maya, as long as it is running, and until that port is closed. This includes quitting maya, saving or deleting your scene, or even executing system commands. So you may want to have the data coming across that port filtered by a MEL procedure first, or passed to a command. In that case, you'll want something like this:
commandPort -n ":7777" -pre listenOnPort;
In the former case, anything you send to port 7777 will be executed by Maya. In the latter case, you'll want a procedure like:
global proc listenOnPort(string $argv) { string $args[]; int $argnum=`tokenize $argv $args`; // Code to process $args should follow here... };
Now of course, you need some way to communicate with Maya. This is where it's actually a little tricky - since you'll need a socket client to issue commands. I'm providing only the most basic example: this one is asynchronous and one-way only. There is no way for this routine to receive any feedback from Maya.
#!/usr/bin/python2.5 import socket import sys HOST = '127.0.0.1' PORT = 7777 CMD=sys.argv[1] try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) except socket.error, msg: sys.stderr.write("[ERROR] %s\n" % msg[1]) sys.exit(1) try: sock.connect((HOST, PORT)) except socket.error, msg: sys.stderr.write("[ERROR] %s\n" % msg[1]) sys.exit(2) sock.send("%s\n" % (CMD)) sys.exit(0)
By calling:
talktomaya.py "select -all"
You will be able to send the indicated command to Maya to be executed, or in the case of the -pre example, pass the command to the procedure for parsing.
More sophisticated implementations may require sending to machines other than localhost (127.0.0.1) as in the above example, and that's left as an exercise to the reader. It's a simple matter to send commands to a range of machines across a collection of IP addresses. Examples of this might include having individual users register their interest in a particular piece of data - and when that data is released, those users are notified automatically that the data is available, say an updated model or a revised animation, and giving them the ability to then update their scene with the new data. It might also be useful to centrally request an update from all users who are currently working on assets required for a particular shot - and obviously many other options are available when you can communicate directly with the maya instance of each user in a facility.
The tricky bit is that I hadn't seen a clear example of a script to send instructions to Maya - and there it is, in its most basic form.
More Options ...

Categories
Tag Cloud
Blog RSS
Comments RSS



Void (Default)
Life
Earth
Wind
Water
Fire
Lightweight