Setting Maya for development5 min read
During the last weekend, I had tried to set up maya python environment. You know, developing python scripts using IDE and testing it with maya script editor is a bit painful. Ideally, you would need to have IDE that supports auto-complete and maya introspection.
After googling a bit, I found out that Autodesk provides python interpreter for maya. It is located in ~/usr/autodesk/maya2018/bin/mayapy, for my case. This python interpreter loads all maya modules automatically, which is what I want. Now I just need to use this interpreter for my IDE. As for IDE, I typically use PyCharm for any CG scripts development. It lets you set its python interpreter. Using this feature, we can choose mayapy as a python interpreter.
To do this, firstly, I created a PyCharm project for Maya python script development. When you create a new project, PyCharm asks you to choose a python interpreter. You would need to choose mayapy for it. Here you can create a new interpreter or choose an existing one. Either way, you need to set it as a system interpreter. So select a system interpreter and locate your mayapy. Since we are using a system interpreter, you need to authorise it. That is it. You are ready to develop maya python scripts with PyCharm.
Once the project was set up, PyCharm may complain about “python packaging tools not found”. It did for my case. It is not something you need to worry about. Because, autodesk did not include some libraries that python environment needs i.e as pip, PyCharm is notifying it. It is also the reason you need to set your python interpreter as a system interpreter. If you choose it as either virtualenv or conda, PyCharm would fail to set a python interpreter for the project since virtualenv and conda are not included in a python interpreter that autodesk provided.
However, I think that it would not be an issue for us. Now you can import maya and pymel modules. See codes below.
import sys import maya.cmds as cmds import pymel.core as pmc print sys.modules.keys()
It shows modules imported. From there, we can see maya and pymel are loaded. You can also initialise maya and run it. To do that, we need to import maya and initialise it.
import maya.standalone maya.standalone.initialize()
It may take a while since it is loading maya. But when you do import it, you can see all other modules that maya need are imported.
Now let us get auto-completion working. Note that I am using PyCharm 2019.1 Community Edition and Maya 2018. Firstly, get DevKit from here: https://www.autodesk.com/developer-network/platform-technologies/maya Then install it on your maya root. For my case, my maya root is /usr/autodesk/maya2018. To install devkit, extract the zip you downloaded then copy devkit and mkspecs folders to you maya root. Leave lib and include folders as it is. If you copy those folders to your maya root, you will get some symbol look up errors and your maya won’t run. Believe me I did copy those and got those errors :)) .
Once you copied those folders, run your Pycharm and open its settings. Under your project interpreter settings, look for a gear icon next to your project interpreter location. Click into that and click Show all. See a picture below:
Select your mayapy and click at an icon at the bottom of the right-side panel. You will see all python modules loaded for you there. You need to add another library from devkit here and remove one path. Click add button and point to your maya root/devkit/other/pymel/extras/completion/py directory. Then remove your maya bin/lib/python2.7/site-packages directory. If in doubt, see a picture below.
As a cherry on the cake, it would be nice if maya knows the directory of scripts that I am developing. There are two ways to accomplish it: import it in userSetup.py or set environment variable. Or you can edit system environment variable if you want to go extreme.
In my case, I chose to use userSetup.py. Since I am using Linux, I needed to create userSetup.py inside ~/maya/2018/scripts. A content of userSetup.py is simply:
import sys sys.path.append('/home/tim/MEGAsync/TD/Maya')
Here I chose my a cloud sync directory as I want to use those scripts from different machines. Lastly, let’s quickly test what I have achieved.
>>> import pymel.core as pmc >>> for n in type(pmc.joint()).__mro__: ... print n ... <class 'pymel.core.nodetypes.Joint'> <class 'pymel.core.nodetypes.Transform'> <class 'pymel.core.nodetypes.DagNode'> <class 'pymel.core.nodetypes.Entity'> <class 'pymel.core.nodetypes.ContainerBase'> <class 'pymel.core.nodetypes.DependNode'> <class 'pymel.core.general.PyNode'> <class 'pymel.util.utilitytypes.ProxyUnicode'> <type 'object'>
There it printed an object hierarchy of a joint correctly.