【maya插件开发】vscode debug python 代码

maya端

python 复制代码
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

python 复制代码
{
    // 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. 远程调试允许您暂停代码执行、检查变量值和查看函数调用返回,这对于排查Maya脚本中的复杂问题非常有用。

  2. 在Maya中设置远程调试的步骤:

  • 将debugpy模块添加到Maya的PYTHONPATH中

  • 使用代码片段初始化debugpy并等待调试器连接

  • 在VS Code中配置"Remote Attach"调试配置

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

  2. 解决方案是显式配置debugpy使用mayapy.exe解释器:

```python

maya_location = os.path.join(os.environ.get("MAYA_LOCATION"), "bin", "mayapy.exe")

debugpy.configure({'python': maya_location})

```

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

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

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

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

相关推荐
2601_962296512 分钟前
Python的哈希hashlib模块详细解读
python·md5·sha·加密算法·hashlib
chinesegf5 分钟前
comfyui便携版的基础使用
python
OPEN-F6 分钟前
ROS2系列教程:Gazebo插件(关节控制/IMU/激光雷达)
c++·python·数码相机·算法·机器人
临沂GEO13 分钟前
芝麻开门GEO|AI数字化新趋势,助力企业线上长效增长
大数据·人工智能·python
梦想不只是梦与想1 小时前
大模型系列(二):技术基础与核心能力
python·大模型·token
AC赳赳老秦1 小时前
农产品公开数据应用:OpenClaw 抓取农产品价格、产销公开数据,实现农产品行情动态监测
java·c语言·javascript·python·php·deepseek·openclaw
万年咸鱼1 小时前
Java PrintStream 详解:从基础用法到实战技巧
java·开发语言·python
cui_ruicheng2 小时前
FastAPI 应用开发(二):请求响应、Pydantic 模型与依赖注入
python·fastapi·web
DeepVisionary2 小时前
SoundHound 完成收购 LivePerson:股权对价 4300 万美元,实际总成本约 3.04 亿
python·自动化
deepseek232 小时前
Google Gemini Agentic Video 上线:88% 少 token 的主动取样如何改写长视频理解
python·多模态·ai agent·gemini·视频理解