Skip to content

Instantly share code, notes, and snippets.

@joaen
Last active April 17, 2024 20:47
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joaen/bdc154ecb3f28d8481b9fb23411d1008 to your computer and use it in GitHub Desktop.
Save joaen/bdc154ecb3f28d8481b9fb23411d1008 to your computer and use it in GitHub Desktop.
How to attach VS Code Python debugger to Autodesk Maya 2022+

If you're having trouble with the MayaPy extension in VS Code note that it still utilizes ptvsd, which Microsoft has deprecated as of 2020.

This tutorial will guide you through the process of setting up debugging with debugpy, the latest and improved Python debugger for VS Code.

Note: This tutorial is primarily intended for users of Python 3 and Maya 2022 (or later).

Note: You need to have the official Python extension installed in VS Code.

Method 1: Remote Attach

  1. Download a released version of debugpy at the official github repo: https://github.com/microsoft/debugpy/releases
  2. Unzip debugpy and copy the folder called "debugpy" , which is located in the "src" folder.
  3. Paste the debugpy folder in your Maya scripts folder. Default location: C:\Users\USERNAME\Documents\maya\MAYA_VERSION\scripts)
  4. In VS Code, go to the "Run and Debug" tab (Ctrl+Shift+D).
  5. Click on "create a launch.json file" and select "Remote attach" from the list, then select "localhost" and then "5678".
  6. Open the auto-generated launch.json and remove the pathMappings text block, it should look something like this:
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Remote Attach",
            "type": "python",
            "request": "attach",
            "connect": {
                "host": "localhost",
                "port": 5678
            },
            "justMyCode": true
        }
    ]
}
  1. In Maya run this python code snippet. You can assign it to a shelf button since you need to run this code every time you start a new instance of Maya:
import debugpy
debugpy.configure(python="C:/Program Files/Autodesk/Maya[MAYA_VERSION]/bin/mayapy.exe")
debugpy.listen(5678)
  1. In VS Code click "Start Debugging" (F5)
  2. In VS Code you should now be able to use breakpoints, use watch and see the output being printed in the Debug Console.

Method 2: Attach to process ID

There is also an alternative method to debug python code in Maya using the process ID of Maya to attach the debugger. It requires fewer steps compared to the first method but it's a bit slower, since it takes a bit longer to attach the debugger to Maya every time you start debugging.

  1. In VS Code, go to the "Run and Debug" tab (Ctrl+Shift+D).
  2. Click on "create a launch.json file" and pick "Attach to Remote Process ID".
  3. The launch.json that VS Code creates automatically should work without any modifications. But it should look like this for reference:
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Attach using Process Id",
            "type": "python",
            "request": "attach",
            "processId": "${command:pickProcess}",
            "justMyCode": true
        }
    ]
}
  1. Make sure that Maya is running and in VS Code click "Start Debugging" (F5) and use the arrow to navigate and select your Maya process (or search for the word "Maya").
  2. In VS Code you should now be able to use breakpoints, use watch and see the output being printed in the Debug Console.
@zhalice2011
Copy link

Thank you for your tutorial. Standing on your shoulders, I have developed a VS Code extension to simplify the debugging process.

To debug Python code in Maya 2022 or above using VS Code, you can use the "Debugger for Maya" extension available here:
Debugger for Maya

For guidance on how to use it, check out this video tutorial:
Video Tutorial

@joaen
Copy link
Author

joaen commented Apr 17, 2024

Thank you for your tutorial. Standing on your shoulders, I have developed a VS Code extension to simplify the debugging process.

To debug Python code in Maya 2022 or above using VS Code, you can use the "Debugger for Maya" extension available here: Debugger for Maya

For guidance on how to use it, check out this video tutorial: Video Tutorial

That is awesome! Thanks for sharing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment