モーションキャプチャーのライブラリ(API)を使用することによって Maya 以外のプロセスから MEL コマンドを実行できるようになる。
CapTcpOpen() によって Linux の場合は UNIX Domain Socket が作られるので、それを使用することによりソケットを通して Maya と通信ができる。
Maya 上で外部から接続するポートを開くには commandPort コマンドを使用する。
(使用例)
以下に Linux 用において UNIX ドメインソケットを使用したプログラム例をあげておく。
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdlib.h>
extern "C" {
#include <maya/mocapserver.h>
#include <maya/mocaptcp.h>
}
#define MAX_LENGTH 4096
void
read_data(char *command)
{
char s[MAX_LENGTH];
int len;
command[0] = '\0';
for (;;)
{
if (fgets (s, MAX_LENGTH, stdin) == NULL)
break;
if((strlen(command) + strlen(s)) >= MAX_LENGTH)
{
fprintf(stderr, "command too long >> %d\n", MAX_LENGTH);
exit(1);
}
strcat(command, s);
}
}
int
main(int argc, char **argv)
{
char *server_name = "mayaCommand";
char command[MAX_LENGTH];
char reply[MAX_LENGTH];
int fd = CapTcpOpen(server_name);
if ( fd < 0 )
{
fprintf(stderr, "could not connect to server !!\n");
exit(1);
}
read_data(command);
send(fd,command,strlen(command),0);
int red = recv(fd,reply,MAX_LENGTH,0);
if ( red > 0)
{
printf("%s",reply);
}
else
{
fprintf(stderr, "read failed !!\n");
}
close(fd);
return 0;
}
require 'socket'
fp = UNIXSocket.open("/tmp/mayaCommand")
com = STDIN.read
fp.send(com, 0)
print fp.recv(4096)
fp.close
% cc -I/usr/aw/maya/include -L/usr/aw/maya/lib rmc.cpp -lmocap -lm -o rmc
% echo sphere | ./rmc nurbsSphere1 makeNurbSphere1
% echo sphere | ruby rmc.rb nurbsSphere1 makeNurbSphere1
以下のプログラムでは Tcp ポートの 8888 を開いて通信する。 (使用されていない Tcp ポート番号なら他の番号でも良い)
require 'socket'
fp = TCPSocket.open("localhost", 8888)
com = STDIN.read
fp.send(com, 0)
print fp.recv(4096)
fp.close
% echo sphere | ruby rmc.rb nurbsSphere1 makeNurbSphere1