How to connect to Maya's command port from C++

This tutorial will show you how to quickly make a network program for remotely executing Mel commands through Maya's command port.
This is basic socket programming, if you have no interest in this (and can't see the endless possibilities in developing this) you can use almost any telnet compatible application like Putty (
http://www.putty.nl/) for now. (you can't use Windows standard telnet because it separately transmits every single character by default). In the near future we are going to use the application to quickly test our own Maya plugins. With this fairy simple network code we can make a small efficient script for remotely loading, executing and unloading plugins in Maya.
Requirements: You need to have Maya installed (For Kubuntu details see:
tutorial on howto install Maya)
File:
mcp.cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>
#define BUFFER_SIZE 1024
int main(int argc, char *argv[]){
struct sockaddr_in addr;
int client_socket; //socket to server
char buffer[BUFFER_SIZE]; //recv buffer
int bytes_recv; //bytes recv
if(argc != 4){
fprintf(stderr, "usage: %s <server_ip> <port> <command>n", argv[0]);
exit(1);
}
addr.sin_addr.s_addr = inet_addr(argv[1]);
addr.sin_port = htons(atoi(argv[2]));
addr.sin_family = AF_INET;
client_socket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if(client_socket == -1){
perror("socket create");
exit(1);
}
if(connect(client_socket, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) == -1){
perror("connect");
exit(2);
}
bzero(buffer, BUFFER_SIZE);
write(client_socket, argv[3], strlen(argv[3])+1);
if( (bytes_recv = read(client_socket, buffer, BUFFER_SIZE - 1)) > 0 ){
fprintf(stderr, "%s", buffer);
}
close(client_socket);
return 0;
}
If you have installed Maya on default path you should be able to compile with:
$ g++ mcp.cpp -o mcp -lm /aw/maya8.0/lib/libmocap.a -L/aw/maya8.0/lib -I/aw/maya8.0/include
Now lets test out program. Start Maya and open the command port (Checkout
this tutorial on how to do this: )
Lets say you opened up the command port 6000 in Maya and you are running everything on your own machine (localhost). Now lets try connecting to Maya and create a sphere:
$ mcp 127.0.0.1 6000 sphere
On success Maya reply:
nurbsSphere2 makeNurbSphere2
That's it!
Next tutorial will show you how to make a basic Maya plugin and how to remotely load, run and unload it.
Stay tuned ;o) !!!
- 13.01.2007. 03:47 | Author: Ticless
Comments
Larry - 01.03.2007. 09:33
how can i get results back from maya into my telnet?
say if i send sphere from my telnet, both my telnet and maya script editor would display "nurbsSphere1 makeNurbSphere1", this is fine.
but if i do
print "fasfas";
maya would print out the string, but telnet would say Warning: unknown result type
is there anyway i can display the exact same message that maya displays in its script editor?
Write a comment