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

相关推荐
niucloud-admin1 小时前
JAVA V6 多商户商城 开发文档——job 计划任务开发
java·python·github
小叶肥辉1 小时前
LangChain链和LangGraph图的学习笔记【三】——分别用langchain_openai库和langchain_community库调用大模型
笔记·python·langchain
2401_873479402 小时前
IP属地为什么有时显示外省?用IP查询工具核查动态分配、运营商出口与GeoIP库
python·tcp/ip·ip
OKkankan4 小时前
LangChain 能力详解!:输出解析、RAG、向量数据库与 Retriever 检索器
数据结构·python·langchain·ai应用
山哥ol4 小时前
【Geany 环境配置与中文乱码解决参考】
python
lifewange5 小时前
VSCode怎么运行java
java·ide·vscode
会飞锦鲤5 小时前
基于 Mask R-CNN 的药片缺陷检测系统
人工智能·pytorch·python·神经网络·resnet-50
默 语5 小时前
Java新手入门:从零开始安装JDK并配置环境变量
java·开发语言·python·mysql·group by·1024程序员节·数据去重
清水白石0085 小时前
Python 异步编程深度解析:Cancellation 到底是异常还是控制信号?从 asyncio 取消机制到企业级事务设计最佳实践
开发语言·python
Patrick在香港6 小时前
Python 审计香港开放数据目录:两个端点差 10 倍,只有 9.3% 的资源标了「最后修改时间」
开发语言·数据库·python·数据分析·api·数据治理·开放数据