[Maya/Python/C#] Open Maya and Then Open Command Port

As part of our modelling pipeline in the studio I work at, we want our engine to be able to open up Maya and have Maya import an FBX model.
Ideally we don’t want each Maya user to need a special script in their maya folder to open the command port, but instead to tell maya to open the command port externally (via c# code from our engine).
I’m having trouble finding a solution, any tips on this would be great!

You’ll have to open the command port in the userSetup.py – unless you open a port and attach a handler function to it you can’t talk to maya from the outside. Unless the command port is already open, Maya is not listening for any kind of external communication.

You might be happier with a cross platform RPC standard instead of the commandport as your transport mechanism. Commandport is going to just shovel bytes back and forth, it’ll be up to you to make those bytes mean something on both ends. Generic RPC standards, like XMRPC or JSONRPC can be implemented fairly easily in Maya using python; you can also use a cross-platform rpc system like http://zeromq.org/, which has both C and python implementations, to handle marshalling.

The biggest hassle is the fact that Maya is not very thread-friendly, you’ll have to create a mechanism to forward requests to Maya’s main thread safely. To a large extend this can be done with the evalDeferred() function: http://knowledge.autodesk.com/support/maya/learn-explore/caas/CloudHelp/cloudhelp/2015/ENU/Maya/files/Python-Python-and-threading-htm.html but there are a number of gotcha’s associated with that.

If you want to do this without a user being present, you can run a full-on RPC server using maya standalone. Since that’s not competing with the user UI for maya’s attention, it’s a lot simpler to code. Example here: https://github.com/theodox/standaloneRPC or you could use ZeroMQ.

Thanks for the advice. I’ve looked at ZeroMQ and decided that it’s definitely the way to go. I’ve implemented the C side of things with it, and have some simple socket tests running. I’ve hit a bump though with the maya/python implementation of ZeroMQ.
The pyzmq files installed easily with easy_install but now in maya im getting this error when trying to import the module:

import zmq
context = zmq.Context()

Error: line 1: WindowsError: file C:\Program Files\Autodesk\Maya2015\bin\python27.zip\ctypes_init_.py line 365: 193

Anybody know what is happening here?
Thanks in advance!

are you using a really old Maya x64 version? e.g. 2009 or earlier? Because they don’t come with ctypes… (there’s a solution though)
Are you using a newer Maya which’s Python is based on VC2010? In that case you may have to compile ZMQ yourself…
Even for some Py2.6 VC2008 versions I had to compile ZMQ myself… it didn’t always run out of the box for me
(ZMQ is great, but it’s a hassle to deploy in a heterogenous studio environment - if that’s what you have)

Alternatives to ZMQ:
ZMQ shines if you do many RPC calls, and when you use it over a network.
For low volume, sporadic communication on localhost, you could also use HTTP or your own socket solution. HTTP may be slower (but you’re not making a lot of calls anyway) but it’s very easy to implement if you use it in a RPC style manner - e.g. JSON RPC

I’m running Maya 2015, as is the rest of the art and animation team in my studio. Theres quite a fair bit of data that is going to be transported back and forth, which is why I’d like to experiment with ZMQ instead of http or other simpler solutions.
I’ve tried installing python 64bit (as i first tried with 32bit), in hope to solve any mismatching between zmq and python, but i’m still getting the same ctypes error. Other than trying that, I don’t know of any other solutions.Shrugs

Aha! Sorry I misread your response about the newer versions of maya using newer VS compilers. I recompiled ZMQ correctly and now it’s all working! Onto testing!