Operate Maya from an external Python interpreter using the rpyc module

This article is the 23rd day article of Takumi Akashiro Alone Advent Calendar 2020.

At the beginning

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.

What is the rpyc module?

** 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

How to use

  1. Install rpyc with pip install rpyc -t <Maya's Script folder>.
    There is no binary, so any python2 system should be okay ...
  2. Save the following files in the Maya 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']

  1. Set up a Maya server by executing the following with Maya's Script Editor.
import mrpyc
mrpyc.start_server()
  1. Execute the following with an external interpreter to get the maya module and try to make a polyCube.
import mrpyc
maya = mrpyc.import_remote_maya_module()

maya.cmds.polyCube()

image.png

23_01.gif

Tightening

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

Operate Maya from an external Python interpreter using the rpyc module
Operate the schedule app using python from iphone
Operate an I2C-connected display from Python
Try using the Python Cmd module
Launch the Python interpreter from Git bash
I want to operate DB using Django's ORM from an external application
[Python] [Excel] Operate an Excel sheet from Python using openpyxl (using a test sheet as an example)
[Python] Let's execute the module regularly using schedule
I tried using the Datetime module by Python
Try using the collections module (ChainMap) of python3
Use the nghttp2 Python module from Homebrew from pyenv's Python
Operate Redmine using Python Redmine
Operate Filemaker from Python
View images in OpenCV from Python using an external USB camera on your MacBook
[Python] You can save an object to a file by using the pickle module.
Operate neutron from Python!
Using Python #external packages
Write data to KINTONE using the Python requests module
Operate LXC from Python
Call your own python module from the ROS package
Study from the beginning of Python Hour8: Using packages
A little bit from Python using the Jenkins API
I tried using the Python library from Ruby with PyCall
Try to operate an Excel file using Python (Pandas / XlsxWriter) ①
View using the python module of Nifty Cloud mobile backend
Try to operate an Excel file using Python (Pandas / XlsxWriter) ②
Solve the Japanese problem when using the CSV module in Python.
Operate your browser using the Selenium Web Driver Python bindings
Flatten using Python yield from
About the Python module venv
How to get followers and followers from python using the Mastodon API
Load an external library from your Lambda function using AWS Lambda Layers. The Python environment for Amazon Linux 2 is also in place. (Python3.6, Anaconda)
Operate Firefox with Selenium from python and save the screen capture
Using Rstan from Python with PypeR
Export mp4 from maya using ffmpeg
Notes on using MeCab from Python
Operate DynamoDB from Python like SQL.
Existence from the viewpoint of Python
Using Cloud Storage from Python3 (Introduction)
Use the Flickr API from Python
Operate Maya with OSC from vvvv
Extract the targz file using python
Run Ansible from Python using API
Precautions when using phantomjs from python
Access spreadsheets using OAuth 2.0 from Python
Master the weakref module in Python
Try using Amazon DynamoDB from Python
Try using the Python web framework Django (1)-From installation to server startup
Create a record with attachments in KINTONE using the Python requests module
Search for synonyms from the word list (csv) using Python Japanese WordNet
Get the return value of an external shell script (ls) with python3
[Lambda] I tried to incorporate an external module of python via S3
How to build an application from the cloud using the Django web framework