Some of us might have encountered a situation where you have to distribute a Python tool which you have made, but you don’t prefer the source code to be open to anyone(for whatever reason it is), or you think a C alternative of the same Python code would be faster (Which I doubt, unless you are writing completely in C/C++). Anyway, such scenarios will lead us to Cython, and you might find it really annoying to make it work with Maya (it was pretty annoying for me atleast).
I thought, why not write down the steps so it could be useful for someone out there who is scratching their head over Cython. Be warned, this isn’t an ideal solution to get it running. But, hey it works for me.
First thing we need is a matching Visual Studio version which Maya used to build its
I copied the below version details from a stackoverflow post (I dont remember the link to the original post)
For this version of Visual C++ Use this compiler version Visual C++ 4.x MSC_VER=1000 Visual C++ 5 MSC_VER=1100 Visual C++ 6 MSC_VER=1200 Visual C++ .NET MSC_VER=1300 Visual C++ .NET 2003 MSC_VER=1310 Visual C++ 2005 (8.0) MSC_VER=1400 Visual C++ 2008 (9.0) MSC_VER=1500 Visual C++ 2010 (10.0) MSC_VER=1600 Visual C++ 2012 (11.0) MSC_VER=1700 Visual C++ 2013 (12.0) MSC_VER=1800 Visual C++ 2015 (14.0) MSC_VER=1900 Visual C++ 2017 (15.0) MSC_VER=1910
Maya 2018’s python version is built using MSC_VER=1900 (Visual Studio 2015)
To check the version, run this
<Maya 2018 installation directory>/bin/mayapy.exe
On the very first line of the python cli, you have information about the build type, architecture, etc.
Grab a copy of Microsoft Visual Studio Community 2015
Before you can download a copy of Visual Studio from Microsoft’s website, you are required to have a Microsoft account. If you already have one, go ahead to your home page and subscribe to Visual Studio Dev (To download an older version of Visual Studio, you have to subscribe to Visual Studio Dev Essentials, it is free !)
Once you have done all these boring stuff, go ahead and click here to go the download page and get a copy of Visual Studio Community 2015
Just incase if you are building for Maya 2017, you will need Visual Studio 2008 which is pretty hard to find. Click here to download
Building python from source
Wait, what ???? 😮
The python build which is provided by python.org is built using MSC 1500(Visual Studio 2008), sadly Cython doesn’t seem to play well if the python build mismatches(I might be wrong, but this way I can copy PySide2 libraries from Maya to the new python build and it should work fine with Cython). Maybe you can try your luck if you would want 😀
Even if it isn’t necessary to rebuild python from source, I thought it might be useful at some point anyway
Before start building python, we might need Python installation for later use(Just a quick way around for future errors), download and install to your desired location. We will need this later, so we call this installation path pythonLib
If your luck ran out, click here to download python 2.7 source files. (It must be a *.tgz file with a *.rar file inside, extract all the way along until you can see a folder named Python-2.7.1, or something similar if you have downloaded a different version of Python)
Make sure you organize the folders from the archive which you have downloaded from the above link, we will need it quite frequently from now onwards. The parent folder where this Python folder resides, lets call it buildRoot
Run the following
Above step should gather necessary external dependencies like bzip, db, openssl etc. If you do a quick google search, you probably will find blogs which ask you to update these packages to latest versions since the version downloaded by the script above are pretty old. However, our only intention is to make Cython play along well, and we are mostly going to work within Maya. So, we can forget about updating these packages for now. (You may update if it is adequate for whatever projects you are going build with this particular version of Python)
Copy over all the files(which ends with .lib) from
Okay, now lets get started with building python. Open Visual Studio 2015, and open the project
File > Open > Project/solution >
Since the original Python 2.7x project is made using an older version of Visual Studio, the newer version will ask you to upgrade the project. Click ok when prompted
It might take a while to upgrade and open solution in Visual Studio. Once it is done, change your build to
Release and architecture to
Now in Visual Studio, go ahead and build the project
(Build > Build Solution)
Wait for it. Don’t worry much if you get tons of errors, or if the build fails. As long as it generates
/Python-2.7.1/PCbuild/amd64 you should be good to go (I hope so 😀 )
Before we can start thinking about Cython, you need to understand that Cython helps you build
C representation of your python code and it requires C libraries to be installed on your computer. Download and install mingw
For convenience, add python executable path to environment variable.
Open command prompt and run this,
setx mayapy2018 buildRoot\Python-2.7.1\PCbuild\amd64\python.exe
Please make sure you are able to launch python which we built. Open command prompt and run
%mayapy2018% it should enter the python cli. If it fails for any reason, please make sure you have chosen python source files from the link which I have provided in the beginning. Latest iteration of Python 2.7.x doesn’t seem to work well with Visual Studio 2015.
Now it is about time to download source files of Cython. Yes, we have to build Cython as well, it is not a good idea to mix different C++ run-time builds. Click here to download required version of Cython. Unzip the archive to your
For Cython to build successfully, the VC libraries should be available in the environment. Lets save it to the system environment variable since we will need this quite often. Open command prompt and run (Make sure you point it to your visual studio directory)
setx vcpath ("C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" x86_amd64)
We need a small patch for our Python before we can continue, open the file
After line 651, add a new line
ld_args.append('/MANIFEST') , save and close the file.
Now lets start building Cython for our version of Python. Open command prompt and run the following,
%mayapy2018% setup.py install
You shouldn’t get any errors while building Cython using the above method. To verify, go to
buildRoot\Python-2.7.1\Lib\site-packages , your folder should look like this (Ignore the *.pyc files)
Pheww!! That was harder isn’t it? 😦
Alright, lets make a test python module now. I am explaining these as if I am talking to an absolutely beginner so that it is easy to troubleshoot if something goes wrong.
Create a folder
buildRoot\sample_module , and create below files inside
print "Hello", name or "world"
Save that as
from distutils.core import setup
from Cython.Build import cythonize
ext_modules = cythonize("helloworld.py")
Save that as
Now open a new command prompt window and execute below lines of code.
%mayapy2018% setup.py build_ext --build-lib ../compiled_sample
That should create a new file,
Now, we have to make sure that this compiled version works with python.
Open command prompt and execute
%mayapy2018% to enter python cli.
Then run these,
If you are lucky enough, you should something like the below screenshot
Yeah, you guessed it right! Now it is time to test this in Maya 2018 😉
Open Maya and run the same code which we used above to test. You should get the similar result as below
That worked !! 🙂 Now have a read of Cython Documentation I hope this helped someone who is stumbling upon Cython in Maya.
Disclaimer: This is just one way of making Cython work in Maya. Instead of building Python from source, you can also use
mayapy.exeto build Cython (I wouldn’t want to mess with Maya installation though). Yeah, I agree that this is a long process, but hey, it is one time requirement. Also, please bare in mind that I had Cython setup in my laptop earlier and I had to uninstall pretty much every dependencies and install it again to make sure I haven’t skipped any step which would stop readers from building Cython. However, if something goes wrong, remember Google is your friend.
I am using Windows 10(You might need Windows 10 Dev Kit, get it here) and I haven’t tested in any other Windows version.