[Python][C#] Getting these guys to be friends

I have desperately searched for possible ways to get python and c# to talk with each other.

I have found IronPython and Python for .Net and these look great but require a new interpreter, thus making them incompatible with a lot of the DCC side stuff.

Ideally, I would like to be able to call methods and pass data between C# and Python, both ways. Is this in any way possible?

Python.Net is your friend and it will integrate with Maya just fine. All you need to do is get the correct version (2.6), pop it in the right directory or set-up a system path and your rollin.

Check out:

TAO Python.Net Page:
www.tech-artists.org/wiki/PythonNetInMaya

& Embedding Python Windows in Maya:
www.techartblog.com/tutorial/integrating-dotnet-and-maya2012-ui-pt1

These should get you on track for getting .net into Maya, obviously you don’t have to instantiate a UI if you are just using functions from your Dll.

Cheers,
Rob.

We use this quite a bit, and from C#, you can bridge into other esoteric system stuff, etc. (http://pinvoke.net/). Quite fun!

There are a few sites on the net that can help you if you search for them (including on this site). Search on Google for “maya python net”

Note that you will need to compile the python.net stuff for your particular flavor of Maya (Depends on which version of Maya you are using), and bit flavor (32 or 64). You can find out if you are unsure by opening the Maya python window and typing:

import sys
sys.version

Even better- run a .NET program as a service and communicate with it from python in Maya!

we wrote a c# dll which loads a python interpreter and launches a script, sets up the environment variables (so pyqt uses the right dlls!) and then talks to the script via named pipes, but you could also use sockets for that.

In 3ds Max, for example, we use a .net function to create a timer and we then poll ever 20 or 40ms or so for commands comming from Python (i.e. if you have an UI there). Now we got a 2 way communication set up between Python and 3ds Max that takes no CPU time at all and turned out to be very stable during a 1+ year production use. So you could drive Max with Python or the other way round, although Max specific functions would still have to be implemented in max script…but that’s ok for us. We just want UI’s, P4 and DB access in Python so we can share the Python code base for that (e.g. for Maya). note: we’re an outsourcer and people often want 1 script to work on Maya and Max - having to code things like the UI only once saves us a lot of work vs. two implementations: Python/MEL vs MXS/C#.

The C#part is extremely simple. The most difficult challenge was to make this beast idiot proof for deployment across the company so it would run with every 3ds max version from 2009 - 2012 (x86 and x64) and not have its python interpreter and PyQt module interfere with anything else on the system. The Python script is set up so that you could run the same program stand alone, via this C# launcher, or from Maya, offering you the same modules (Qt, mySQLdb, PIL,…) no matter what.

The only disadvantage right now is that we don’t have function to stream large binary data between C# and Python, but technically this should not be a problem. It’s just we haven’t found time / need to implement this yet.

Thanks for all the awesome responses. I have a lot to do today now :wink: