【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工具和脚本的开发和调试过程。

相关推荐
gCode Teacher 格码致知1 小时前
Python基础教学:Python的openpyxl和python-docx模块结合Excel和Word模板进行数据写入-由Deepseek产生
python·excel
Destiny_where3 小时前
Agent平台-RAGFlow(2)-源码安装
python·ai
molunnnn4 小时前
第四章 Agent的几种经典范式
开发语言·python
Aldrich_324 小时前
蓝桥杯嵌入式赛道—-软件篇(GPIO输出模式配置)
c语言·vscode·stm32·单片机·嵌入式硬件·蓝桥杯
linuxxx1105 小时前
django测试缓存命令的解读
python·缓存·django
毕设源码-邱学长7 小时前
【开题答辩全过程】以 基于Python的Bilibili平台数据分析与可视化实现为例,包含答辩的问题和答案
开发语言·python·数据分析
咚咚王者7 小时前
人工智能之编程进阶 Python高级:第十一章 过渡项目
开发语言·人工智能·python
A尘埃7 小时前
大模型应用python+Java后端+Vue前端的整合
java·前端·python
A尘埃8 小时前
LLM大模型评估攻略
开发语言·python