# _ _ ____ ___ _ ____ ____ ___ ____ ____ # # |\/| |__| | | | | | | \ |___ [__ # # | | | | | | |___ |__| |__/ |___ ___] # # #
# _ _ ____ ___ _ ____ ____ ___ ____ ____ # # |\/| |__| | | | | | | \ |___ [__ # # | | | | | | |___ |__| |__/ |___ ___] # # #
I'll show you what you need to do to connect PyCharm to Maya so that PyCharm can monitor your code as it runs. Before you get started, remote debugging is a feature that is only available with PyCharm Professional. You can trial the full version of PyCharm to give remote debugging a whirl if you're still on the Community edition.
First, you'll need to configure the remote debugger within PyCharm.
There are a few ways to connect to the debugger, but I have found it most reliable to inject the connecting code somewhere near the entry point of the code I'm testing. Here's the code that I inject to connect:
import sys
# This should be the path your PyCharm installation
pydevd_egg = r"C:\Program Files\JetBrains\PyCharm 2020.3.3\debug-eggs\pydevd-pycharm.egg"
if not pydevd_egg in sys.path:
sys.path.append(pydevd_egg)
import pydevd
# This clears out any previous connection in case you restarted the debugger from PyCharm
pydevd.stoptrace()
# 9001 matches the port number that I specified in my configuration
pydevd.settrace('localhost', port=9001, stdoutToServer=True, stderrToServer=True, suspend=False)
This is fairly simple. It's just adding the pydevd egg to my PYTHONPATH
so that I'm able to subsequently import that module and call pydevd.settrace
to connect. Now, how am I injecting it in my project? If I'm working on my glTF exporter and my test code is something like, glTFExport.export(r"C:\path\to\my\file.gltf")
, then I add it at the top of my export
function. This way it connects/reconnects whenever I run my test. For example:
def export(file_path=None, resource_format='bin', anim='keyed', vflip=True, selection=False):
import sys
pydevd_egg = r"C:\Program Files\JetBrains\PyCharm 2020.3.3\debug-eggs\pydevd-pycharm.egg"
if not pydevd_egg in sys.path:
sys.path.append(pydevd_egg)
import pydevd
pydevd.stoptrace()
pydevd.settrace('localhost', port=9001, stdoutToServer=True, stderrToServer=True, suspend=False)
GLTFExporter(file_path, resource_format, anim, vflip).run()
If all went well, Maya will have stopped responding, but PyCharm will show you your current breakpoint and will be waiting for you to respond.
If you're like me, you might copy your modules to the scripts directory or some other place where Maya can pick it up instead of telling Maya to read directly from the repo you're working from. If that's the case, the debugger will still connect and halt at an exception, but your breakpoints won't work. PyCharm unfortunately won't automatically relate the breakpoints in your project to the same code that's running from another location. You could open the copied files in PyCharm and set the breakpoints there, but that's messy. Fortunately, Path Mapping will help remedy this issue. Let's update the debug configuration to account for this:
Now, PyCharm will be able to associate the breakpoints from your project with the executing code from Maya.
I hope this helps you get started with remote debugging your Maya scripts and plugins. If this is your first time using PyCharm's debugging tools, I think you'll find them very useful. They can make it really easy to get to the bottom of even the nastiest of bugs.
© 2021 Matias Codesal