Run as server

Start MMDAgent-EX as tcp server.

Write the following lines in your content .mdf file to enable server function. Specify the port number MMDAgent-EX should listen to. The default port number is 39392.

# configuration in .mdf to enable server function
Plugin_Remote_EnableServer=true
Plugin_Remote_Port=39392

After starting up the content, MMDAgent-EX will run as a server accepting connections. Once connected, MMDAgent-EX will feed all event / command messages to the clients.

It allows multiple connection. Maximum of 20 clients can be connected at once.

By default only the messages till be sent to client. You can enable sending all logs (message, internal log and trace information) by setting Plugin_Remote_AllLog to true.

# configuration in .mdf to enable sending all raw log
Plugin_Remote_AllLog=true

Client example in Python

Here is an example Python client that connects to MMDAgent-EX running in server mode.

Send a message to MMDAgent-EX server

Connect to a MMDAgent-EX server on local host at port 39392, send a test message and exit. (You can check thrown message in MMDAgent-EX by showing log window by d key or from menu.)

# Example 1: post a message to MMDAgent-EX running as server
import socket
server = ("127.0.0.1", 39392)
tcp_client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcp_client.connect(server)
tcp_client.send(b"MESSAGE|aaa|bbb")
tcp_client.close()

Receive messages from MMDAgent-EX server

Another example, receiving messages from the MMDAgent-EX server and outputing them to stdout. If you want to receive all raw log, set Plugin_Remote_AllLog to true in .mdf file as mentioned above.

# Example 2: capture messages from MMDAgent-EX running as server
import socket
server = ("127.0.0.1", 39392)
tcp_client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcp_client.connect(server)
while True:
   try:
      rcvmsg = tcp_client.recv(4096)
      print("[*] received: {}".format(rcvmsg))
   except Exception as e:
      print(e)
tcp_client.close()

Another exmaple: display key name

More complicated example. This script adds a tiny function that makes the MMDAgent-EX server to show pressed key as text in the scene. It monitors messages, and when a KEY|... message was found, send TEXTAREA_SET message to display the pressed key string.

# Example 3: make server to show pressed key as text in the scene
import socket
import re

# return message to be issued at connection time
def connectedMessage():
   # issue "TEXTAREA_ADD" message to make text field to display.
   return "TEXTAREA_ADD|num|6,2|2,0.3,0,1|0,0,0,0.7|1,1,1,1|0,6,-4"

# process log per line and return message to be sent to MMDAgent-EX if needed
def process(msg):
   # if found "KEY|..." message, issue "TEXTAREA_SET" message to display it.
   matched = re.match('.*KEY\|(.*)$', msg)
   if (matched):
      return "TEXTAREA_SET|num|{}".format(matched.group(1))

# main
server = ("127.0.0.1", 39392)
tcp_client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcp_client.connect(server)
tcp_client.send(connectedMessage().encode())
while True:
   try:
      rcvmsg = tcp_client.recv(4096)
      print("[*] received: {}".format(rcvmsg))
      ret = process(rcvmsg.decode().rstrip())
      if (ret):
         tcp_client.send(ret.encode())
   except Exception as e:
      print(e)
tcp_client.close()

Last modified May 7, 2020: Tweaking menu (3eeef3c)