Remotely Execute Code in Maya using PyCharm
In this article, I will go through my steps for allowing PyCharm to remotely connect to Maya and execute code. PyCharm has become my favorite Python IDE, but it has a few annoyances when working with Maya. For example, you can't copy code in PyCharm and paste it directly in to Maya's script editor. Having previously used Wing IDE, thanks to Eric Pavey's Python module described below, I had the ability to highlight code and remotely execute it in Maya. After switching to PyCharm, losing this feature was one of the greatest hits to my workflow. With the help of these steps and using few tools created and released by others, I have regained this functionality with PyCharm. Hopefully the steps below are descriptive enough and that you have success using them. Thank you for reading!
Credits and My Thanks
There are three individuals that I need to profusely thank and give credit to for creating the scripts and tools that I have used and will be linking below. The first is Reyes Macbeth, who created the PyCharm Ruby modules that interface with PyCharm's PMIP plugin. I have edited his scripts slightly to better interact with the Maya modules. I will include links to his original post, which contains his original MayaCharm he created, and which also gave me enough Ruby and PMIP understanding to cobble this together. The next is Eric Pavey who created Python modules for Maya which he uses with Wing to remotely execute code. These same modules worked perfectly with PyCharm, and so for the Maya portion of this setup, credit must go to him for his work on creating those scripts. The last is Paul Allton, who is the creator of PMIP, the plugin for PyCharm that makes all of this possible, and so much easier than a native PyCharm plugin. Thank you all!
If you don't already have PyCharm installed, go to the page linked below and install a copy. You may also want a copy of Python installed. Though if you're using Maya, you can point PyCharm at Maya's mayapy.exe and PyCharm will use that exactly like a dedicated install of Python.
PMIP is an optional plugin for PyCharm and we will be using it for our purposes. The plugin allows PyCharm to be expanded using Ruby scripts, which bypasses the need to create utilities as core PyCharm plugins which must be created in Java. Being unfamiliar with Java and PyCharm's Plugin API, this plugin is a much nicer alternative.
From PyCharm, go to File \ Settings. Enter the Plugins section and search for PMIP. Use PyCharm's 'Install JetBrains plugin...' button to install this addon.
Once PMIP is installed, you should see a dedicated tab located at the bottom of PyCharm's interface. This tab will print out PyCharm's activities. You can't type into this interface however, configuring PMIP is done in a different location.
With PMIP installed, you should find a new folder located at the top of your current project's directory. The folder will be called /pmip/ and contains very few files. This folder is where you will install additional utilities, and configure PMIP itself.
Once you have located this folder, follow the link below and from the left side of the page, download PMIP's core modules. As of writing this, the link is PMIP-core-0.4.2.zip. Unzip the contents of this file, which should be a single folder named core, to the directory [projectbase]\pmip\plugins\
Next, download MayaCharm and do the same, copying the contents of the downloaded folder to your \pmip\plugins\ directory.
MayaCharm is configured to use port 6002.
If this port is unavailable, edit line 17, 25, and 46 of init.rb
With these folders in place, go to the main PMIP folder \pmip\ where you should find a file named pmip.rb. Open this file in PyCharm and add two lines containing the code below. This will tell PMIP to load these additional plugins when it initializes.
plugin 'core' plugin 'mayacharm'
Also, inside the MayaCharm folder that you just placed in plugins, is a file named init.rb. At the very bottom of this file is where MayaCharm configures the keyboard shortcuts. Edit those lines if you would like to remap the send actions to different keyboard shortcuts.
What does this file do?
What PMIP and the MayaCharm plugin does is save your current selection, or the contents of the entire file, to a temporary file. It then fires a command through a socket that triggers Maya to read in the contents of that file in its Python Interpreter. We need to set things up in Maya before we can do that, continue to the next section!
Installing Maya Modules
With PMIP and MayaCharm all configured, it's time to setup Maya. We will be using a module that Eric Pavey created for doing the same thing with Wing, but it works perfectly with PyCharm as well.
Browse to Maya's folder located inside your Windows Documents directory. If you are unfamiliar with this folder, it is automatically created when you run Maya for the first time. You can place downloaded scripts, plugins, shelves, and a few other types of things to this folder and Maya will be able to access it without any extra effort. We are going to install Eric Pavey's Maya modules and we will use this folder to store them. Inside that directory, if a folder named \scripts\ does not already exist, create one. Then, copy both mayaWingServer.py and executeWingCode.py into that folder.
Use the download link below and copy the contents to this location
Edit line 15 of mayaWingServer.py if you have changed the port from 6002
Edit line 19 of executeWingCode.py, this line must point at your PyCharm PMIP directory.
With these two files in place, from inside Maya's Python editor, execute this code to start the Wing Server. If everything is configured correctly, Maya should now be listening to port 6002.
import mayaWingServer mayaWingServer.startServer()
Back to PyCharm!
We are nearly finished. The last step of the process is to initialize PMIP inside PyCharm. To do this, open the PMIP Console by clicking the PMIP button at the bottom of PyCharm's interface.
Once that panel has been expanded, click the blue reload button, this is the only button on the left side of the panel.
If all goes well, you should be presented with the text below. This indicates that PyCharm was able to connect to Maya, and you should now be able to highlight Python code and use the keyboard shortcuts defined in PMIP's init.rb file, (Ctrl+Enter in my case), Maya will execute that in to its main Python environment.