maya段(new)

# Script Editor 里面执行
import sys
import importlib
sys.path.append('D:\\soft\\maya\\maya2022_4\\Maya2022\\bin\\plug-ins')  # 替换为你的脚本路径
import hl_hair_plug
importlib.reload(hl_hair_plug)  # 使用 Python 3.7 的重新加载方法

hl_hair_plug.py

import sys
import os
import debugpy
import maya.cmds as cmds

def create_sphere():
    cmds.polySphere(name='debugSphere')

maya_location = "D:\\soft\\maya\\maya2022_4\\Maya2022\\bin\\mayapy.exe"
debugpy.configure({'python': maya_location})
try:
    debugpy.listen(5678)
except Exception as e:
    print("Port 5678 is already in use. Debugger might be already running.")
print("Waiting for debugger attach")
debugpy.wait_for_client()
debugpy.breakpoint()
print('break on this line')



create_sphere()

 

设置断点进行调试

  1. 在你的外部 IDE(如 VS Code 或 PyCharm)中打开 hl_hair_plug.py 文件。
  2. 设置断点,例如在 create_sphere 函数的第一行。
  3. 确保你的调试配置正确。
  4. 在 Maya 中运行上述导入和调用脚本的代码,maya端的debugpy监听等待连接。
  5. 启动IDE调试器(vscode 的Python Debugger:Remote Attach 到前面的监听)

maya端(old)

import sys
import os
import debugpy


maya_location = "D:\\soft\\maya\\maya2022_4\\Maya2022\\bin\\mayapy.exe"
debugpy.configure({'python': maya_location})
try:
	debugpy.listen(5678)
except Exception as e:
	print("Port 5678 is already in use. Debugger might be already running.")
print("Waiting for debugger attach")
debugpy.wait_for_client()
debugpy.breakpoint()
print('break on this line')

vscode:launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python Debugger: Remote Attach",
            "type": "debugpy",
            "request": "attach",
            "connect": {
                "host": "localhost",
                "port": 5678
            },
            "pathMappings": [
                {
                "localRoot": "${workspaceFolder}",
                "remoteRoot": "D:/soft/maya/maya2022_4/Maya2022/bin/plug-ins"
                }
            ]
        }
    ]
}

  1. 双向通信

    • IDE 到 Maya:你的 IDE 通过远程调试配置(如 Remote Attach 模式)连接到 Maya 的 Python 解释器。连接建立后,IDE 可以向 debugpy 发送调试指令,如继续运行、逐步调试、检查变量等。
    • Maya 到 IDE:当 Maya 中的代码运行到断点时,它会通过 debugpy 把当前的执行状态(比如变量、堆栈信息)发送到 IDE。IDE 会展示这些信息,你可以查看和操作。

底层协议:

调试通信通常基于 Debug Adapter Protocol (DAP)debugpy 实现了这个协议。通过 DAP,调试器和被调试的程序之间能够通过特定的消息传递和控制流来实现通信。消息通常是 JSON 格式的数据,传递调试命令(如暂停、设置断点)和状态信息(如当前执行位置、变量值)。

1. 远程调试允许您暂停代码执行、检查变量值和查看函数调用返回,这对于排查Maya脚本中的复杂问题非常有用。

2. 在Maya中设置远程调试的步骤:
   - 将debugpy模块添加到Maya的PYTHONPATH中
   - 使用代码片段初始化debugpy并等待调试器连接
   - 在VS Code中配置"Remote Attach"调试配置

3. 设置调试时的一个关键问题是Maya返回自身的可执行文件路径,而不是Python解释器路径。这会破坏默认的debugpy配置。

4. 解决方案是显式配置debugpy使用mayapy.exe解释器:
   ```python
   maya_location = os.path.join(os.environ.get("MAYA_LOCATION"), "bin", "mayapy.exe")
   debugpy.configure({'python': maya_location})
   ```

5. 为了有效使用断点,应该在文件中编写脚本,然后通过Maya的脚本编辑器导入/调用它,而不是直接在编辑器中运行代码。

6. VS Code中的Debug REPL功能允许您在调试会话期间在脚本的各行之间运行代码。

7. 远程调试对于在使用复杂API(如PySide2或Maya的API)时检查对象和测试返回值特别有用。

这篇文章提供了在Maya中设置和排查远程调试问题的逐步指南,这可以显著改善Maya工具和脚本的开发和调试过程。

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐