This article is the 23rd day article of Takumi Akashiro Alone Advent Calendar 2020.
When I touched Houdini before, I thought that the hrpyc
module that manipulates Houdini's hou
object from external Python was amazing ... [^ 1].
With reference to hrpyc
, I wondered if I could write something similar in Maya, so I made it.
[^ 1]: I haven't actually used it just because I think it's amazing.
** R ** emote ** Py ** thon ** C ** all, a module for using Python objects from another process. [^ 2]
The caller can use a Python object in another session by setting up a server and providing the callee with a proxy for the object over the network. I'll leave the details to rpyc · PyPI, but hrpyc used in Houdini is a very simple wrapper for rpyc.
[^ 2]: There are various theories
pip install rpyc -t <Maya's Script folder>
. mrpyc.py
#! python2
# encoding: utf-8
import threading
from rpyc.utils import classic
from rpyc.core import SlaveService
from rpyc.utils.server import ThreadedServer
PORT = 18812
def _start_server(port=PORT):
t = ThreadedServer(
SlaveService,
hostname = '0.0.0.0', port = int(port),
reuse_addr = True, authenticator = None,
registrar = None, auto_register = False
)
t.start()
def start_server(port=PORT):
thread = threading.Thread(target=lambda: _start_server(port))
thread.start()
return thread
def import_remote_maya_module(server="127.0.0.1", port=PORT):
return classic.connect(server, port).modules['maya']
import mrpyc
mrpyc.start_server()
import mrpyc
maya = mrpyc.import_remote_maya_module()
maya.cmds.polyCube()
It was easy [^ 3] other than that!
It just feels like an experiment, and after a few minutes the connection dies or Maya crashes, so
It may be safe to implement it by referring to _RemoteHouAttrWrapper
of hrpyc
.
[^ 3]: When I felt it touched, I made it by scraping off the extra part from hrpyc.py, and it took about 3 hours.
Recommended Posts