【03.isaac-sim】最新从零无死角系列-(03) isaac-sim 之 VScode 插件 Isaac Sim VS Code Edition 快速编程 与 Attach 远程调试

若需观看机器人系列相关博客,请劳驾至:足式机器人无死角系列之-【强化学习理论基础-通用】、【仿真及训练环境】、【强化学习】:isaac-gym 与 isaac-lab 从零开始

郑重声明:该系列博客为本人 ( W e n h a i Z h u ) 独家私有 , 禁止转载与抄袭 , 首次举报有谢 , 若有需请私信授权! \color{red}郑重声明:该系列博客为本人(WenhaiZhu)独家私有,禁止转载与抄袭,首次举报有谢,若有需请私信授权! 郑重声明:该系列博客为本人(WenhaiZhu)独家私有,禁止转载与抄袭,首次举报有谢,若有需请私信授权!

本系列博客链接为: {\color{blue}本系列博客链接为:} 本系列博客链接为:【03.isaac-sim】最新从零无死角系列-(00) 目录最新无死角环境搭建与仿真模拟及源码讲解:https://blog.csdn.net/weixin_43013761/article/details/144299459

本博客编写于: 20241228 ,台式机为 u b u n t u 20.04 , 3090 G e F o r c e R T X 显存 24 G { \color{purple} 本博客编写于:20241228,台式机为 ubuntu 20.04,3090 GeForce RTX 显存24G} 本博客编写于:20241228,台式机为ubuntu20.043090GeForceRTX显存24G:与你现在的代码,或者环境等存在一定差异也在情理之中,故切勿认为该系列博客绝对正确,且百密必有一疏,若发现错误处,恳请各位读者直接指出,本人会尽快进行整改,尽量使得后面的读者少踩坑,评论部分我会进行记录与感谢,只有这样,该系列博客才能成为精品,这里先拜谢各位朋友了。

文末正下方中心提供了本人 联系方式, 点击本人照片即可显示 W X → 官方认证 {\color{blue}{文末正下方中心}提供了本人 \color{red} 联系方式,\color{blue}点击本人照片即可显示WX→官方认证} 文末正下方中心提供了本人联系方式,点击本人照片即可显示WX官方认证

一、前言

第一篇博客:【03.isaac-sim】最新从零无死角系列-(01) isaac-sim 环境搭建(分辨率调整),无遗漏,各种疑难杂症处理 中介绍了如何搭建 isaac-sim 环境,且介绍一种最基本通过 vsode 进行代码调试的方式。但是实际上还有其他几种调试 isaac-sim 的方式,且能够调试远程,非本地环境的 isaac-sim,先给出 isaac-sim 相关的链接:

        官方网址:https://developer.nvidia.com/isaac/sim

        英文文档链接:https://docs.omniverse.nvidia.com/isaacsim/latest/index.html
        中文文档链接:https://docs.robotsfan.com/isaacsim/latest/index.html

另外,再附加一个 vsode 插件,该插件主要用于调试 isaac-sim 相关代码,后续会进行详细介绍:

        Isaac Sim VS Code Edition:https://marketplace.visualstudio.com/items?itemName=NVIDIA.isaacsim-vscode-edition

下面主要介绍两种关于 VScode 调试 Isaac-sim 代码的方式。先介绍的是使用 Isaac Sim VS Code Edition 这个插件,该插件主要是提高用户编程速率,不用重复的启动 Isaac-sim。若不使用该插件,可能需要每次执行程序,那么该程序很短,代码量很少,都必须启动、加载、销毁 isaac-sim 的 UI 界面,这个操作重复度太高,在硬件环境较差的时候,可能会耽误很多时间。使用 Isaac Sim VS Code Edition 这个插件 这个插件可以避免这个码放。

接着还要介绍比较常见的 Python: Attach 方式,该方式可对远程,或者说服务端的 Isaac Sim 应用程序进行调试,当然本地也是可以的。不过若是本地调试,个人更加推荐第一篇博客:【03.issa-sim】最新从零无死角系列-(01) isaac-sim 环境搭建(分辨率调整),无遗漏,各种疑难杂症处理 中介绍的 debug 调试方式。那么先来看第一种方式,便于编程。

二、插件方式

本人主要参考:

官方文档:https://docs.omniverse.nvidia.com/isaacsim/latest/advanced_tutorials/tutorial_advanced_code_editors.html

大佬博客:https://blog.csdn.net/m0_56661101/article/details/139936671

1.VScode插件安装

首先使用 vs code 打开源码(非源码目录后面也有介绍)所在目录,本人的环境为 docker 容器,所以源码目录位于 /isaac-sim,若使用工作站或本地安装,路径通常不一样,则需要根据自己安装位置进行调整。打开之后就是安装该插件(Isaac Sim VS Code Edition):
在这里插入图片描述成功安装该插件之后,在左边的菜单栏可以看到如下图标:
在这里插入图片描述

2. isaac-sim 配置

安装插件之后,还需要对 isaac-sim 进行配置,如本人本人执行指令:

./isaac-sim.sh  --allow-root

启动界面之后,执行如下操作,依次选择 Window->Extension 进入拓展管理器,如下:
在这里插入图片描述
进入到拓展器管理界面后搜索 omni.isaac.vscode 并启用插件,并选择AUTOLOAD,方便每次启动Isaac Sim均启用VS Code插件:
在这里插入图片描述

3.测试代码

注意需要打开 isaac-sim 源码所在目录,这里再次提及。且还需要依照前面参考前面的博客 【03.issa-sim】最新从零无死角系列-(01) isaac-sim 环境搭建(分辨率调整),无遗漏,各种疑难杂症处理 确认 [六、VScode 环境搭建] 已经操作完成,

需要记得运行 VSCode Tasks ,通过按下 C t r l + S h i f t + P {\color{blue}Ctrl+Shift+P} Ctrl+Shift+P,选择 Tasks: Run Task 并在下拉菜单中运行 s e t u p _ p y t h o n _ e n v {\color{blue}setup\_python\_env} setup_python_env。然后确认是否生成文件 .vscode/.standalone_examples.env。

基于 isaac-sim 源码所在目录 打开目录,其是为了 vscode 正确识别到 .vscode/settings.json 文件,其中包含如下内容:
在这里插入图片描述这是十分重要的,否则后面运行过程中会报错,比如说找不到对应的 python 模块。打开之后可以在源码根目录创建一个一个文件 test.py 编写代码如下:

from omni.isaac.core import World
from pxr import Usd, UsdGeom
import omni

# Create default plane
world = World()
world.scene.add_default_ground_plane()

# Add a sphere
stage = omni.usd.get_context().get_stage()
xformPrim = UsdGeom.Xform.Define(stage, "/hello")
spherePrim = UsdGeom.Sphere.Define(stage, "/hello/sphere")

然后启动 isaac-sim(或者墙面启动的没有关闭也行),通过如下操作执行指令:
在这里插入图片描述
若运行正常,则终端打印:

[17:50:59] executing at 127.0.0.1:8226...
[17:50:59] executed without output

isaac-sim 界面显示如下:
在这里插入图片描述

4.基本操作

若上面操作一些正常,则说明环境搭建成功,现在做一些额外的介绍,首先可做关于 Isaac Sim VS Code Edition 插件界面,有如下四个运行选项:
在这里插入图片描述

Run: 若 isaac-sim 运行在本地,且通过上述操作启动 omni.isaac.vscode,那么 isaac-sim 服务默认运行的 ip 与端口为 127.0.0.1:8226,通过该选项本地 vsode 会运行代码直接与其进行交互。

Run selected text: 若 isaac-sim 运行在本地,在编程的时候,并不想运行所有代码,可以使用鼠标选中代码,在点击该按钮。则其只会运行一部分代码,类似于 Jupyter Notebook 的操作

Run remotely: 操作原理与 Run 一致,只是用于 isaac-sim 非本地启动,可通过 Isaac Sim VS Code Edition 插件进行远程 ip 配置。

Run selected text remotely: 操作原理与 Run selected text: 一致,只是用于 isaac-sim 非本地启动,可通过 Isaac Sim VS Code Edition 插件进行远程 ip 配置。

配置默认远程 ip 操作过程如下,进入 vsode 插件界面,点击 Isaac Sim VS Code Edition 插件的设置按钮,选择扩展设置:
在这里插入图片描述
通常来说,只需要根据远程服务 ip 修改 remoteApplication.extensionIP 即可:
在这里插入图片描述

三、非源码目录

有的朋友可能在非源码目录进行代码调试,其并没有 .vscode/settings.json 文件中的,当然就没有配置 python.analysis.extraPaths。其实在 大佬博客:https://blog.csdn.net/m0_56661101/article/details/139936671 中介绍了的方式就是基于非源码目录的,本人根据自己的安装方式进行了调整。首先打开一个空目录,如本人使用 vscode 打开任意文件夹目录为 /work/test/,执行如下操作:

cd /work/test # 进入到工作目录
# Run script
# Run script
script_name="setup_isaac_sim_ide_environment.sh" && directory_name="/tmp" && download_url="https://raw.githubusercontent.com/Auromix/auro_sim/main/scripts" && full_script_path="${directory_name}/${script_name}" && wget -O $full_script_path $download_url/$script_name && sudo chmod +x $full_script_path && bash $full_script_path && rm -rf $full_script_path

注意,需要外网,如果没有(往后面看,本人有贴出脚本内容),终端可以看到如下输出:

  Stored in directory: /root/.cache/pip/wheels/29/30/94/33e8b58318aa05cb1842b365843036e0280af5983abb966b83
Successfully built commentjson lark-parser
Installing collected packages: lark-parser, commentjson
Successfully installed commentjson-0.9.0 lark-parser-0.7.8
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
--2025-01-05 18:32:21--  https://raw.githubusercontent.com/Auromix/auro_sim/main/scripts/setup_isaac_sim_ide_environment.py
正在连接 127.0.0.1:7890... 已连接。
已发出 Proxy 请求,正在等待回应... 200 OK
长度: 3937 (3.8K) [text/plain]
正在保存至: ‘/tmp/setup_isaac_sim_ide_environment.py’

/tmp/setup_isaac_sim_ide_envi 100%[==============================================>]   3.84K  --.-KB/s    用时 0s    

2025-01-05 18:32:21 (106 MB/s) - 已保存 ‘/tmp/setup_isaac_sim_ide_environment.py’ [3937/3937])

Please enter the Isaac Sim version (e.g., 4.2.0): 

根据提示输入 isaac-sim 安装的源码路劲即可,本人输入 4.2.0 执行失败了,因为该脚本并没有兼容 docker 的方式,报错为:

Please enter the Isaac Sim version (e.g., 4.2.0): 4.2.0
isaac_sim vscode python analysis extraPaths not found in /root/.local/share/ov/pkg/isaac-sim-4.2.0/.vscode/settings.json

所以本人针对于自己的情况做了修改,首先执行指令:

cp /tmp/setup_isaac_sim_ide_environment.py ./

本人查看内容如下(无外网朋友,直接本地创建 setup_isaac_sim_ide_environment.py 文件):

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright 2023-2024 Herman Ye @Auromix
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Author: Herman Ye @Auromix
# Description: Config isaac sim vscode ide environment in your project

import os

import commentjson

isaac_sim_prefix="isaac-sim-"
isaac_sim_version = input("Please enter the Isaac Sim version (e.g., 4.2.0): ").strip()
isaac_sim_version = isaac_sim_prefix + isaac_sim_version
isaac_sim_path = os.path.join(os.getenv("HOME"), ".local/share/ov/pkg", isaac_sim_version)
isaac_sim_vscode_settings_file = os.path.join(isaac_sim_path, '.vscode', 'settings.json')
current_dir = os.getcwd()
target_dir = os.path.join(current_dir, '.vscode')
target_vscode_settings_file = os.path.join(target_dir, 'settings.json')


def main():
    # Get isaac_sim vscode python analysis extraPaths
    python_analysis_extra_paths = 'python.analysis.extraPaths'
    if not os.path.exists(isaac_sim_vscode_settings_file):
        print(
            f'isaac_sim vscode python analysis extraPaths not found in {isaac_sim_vscode_settings_file}'
        )
        exit(1)

    with open(isaac_sim_vscode_settings_file, 'r', encoding='utf-8') as file:
        isaac_sim_vscode_settings = commentjson.load(file)

    extra_paths = isaac_sim_vscode_settings.get(python_analysis_extra_paths, [])
    # Add isaac_sim_path prefix to extra_paths
    extra_paths = [
        os.path.join(
            isaac_sim_path,
            path,
        )
        for path in extra_paths
    ]

    # Create target .vscode/settings.json if not exists
    if not os.path.exists(target_dir):
        print(f"The .vscode folder does not exist in {current_dir}.")
        confirm = input("Is this the root directory of your project? (y/n): ").strip().lower()
        if confirm == 'y':
            os.makedirs(target_dir, exist_ok=True)
            target_settings = {}
            with open(target_vscode_settings_file, 'w', encoding='utf-8') as file:
                commentjson.dump(target_settings, file)
        else:
            print("Operation cancelled.")
            print("Please make sure your terminal is in the root directory of your project.")
            exit(1)

    # Create settings.json if not exists
    if not os.path.exists(target_vscode_settings_file):
        with open(target_vscode_settings_file, 'w', encoding='utf-8') as file:
            commentjson.dump({}, file)

    # Read existing target .vscode/settings.json
    with open(target_vscode_settings_file, 'r', encoding='utf-8') as file:
        target_settings = commentjson.load(file)

    # Check if python.analysis.extraPaths exists in target settings.json
    if python_analysis_extra_paths in target_settings:
        # If it exists, append the extra paths to it
        target_settings[python_analysis_extra_paths].extend(extra_paths)
    else:
        # If it doesn't exist, create it with the extra paths
        target_settings[python_analysis_extra_paths] = extra_paths

    # Delete duplicate paths
    target_settings[python_analysis_extra_paths] = list(
        set(target_settings[python_analysis_extra_paths])
    )

    # Write updated target settings.json
    with open(target_vscode_settings_file, 'w', encoding='utf-8') as file:
        commentjson.dump(target_settings, file, indent=4, separators=(',', ': '))
    print(f'Updated {target_vscode_settings_file} with extraPaths.')


if __name__ == "__main__":
    main()

因为源码:

isaac_sim_path = os.path.join(os.getenv("HOME"), ".local/share/ov/pkg", isaac_sim_version)
isaac_sim_vscode_settings_file = os.path.join(isaac_sim_path, '.vscode', 'settings.json')

把路径已经被写死了,如本人修改该部分代码如下:

import os

import commentjson

isaac_sim_path = input("Please enter the Isaac Sim path (e.g., /isaac-sim): ").strip()
isaac_sim_vscode_settings_file = os.path.join(isaac_sim_path, '.vscode', 'settings.json')
current_dir = os.getcwd()
target_dir = os.path.join(current_dir, '.vscode')
target_vscode_settings_file = os.path.join(target_dir, 'settings.json')
......

重行执行该脚本,终端输出如下:

(isaaclab-py3.10) root@ub20-04:/work/test# python3 setup_isaac_sim_ide_environment.py 
Please enter the Isaac Sim path (e.g., /isaac-sim): /isaac-sim
The .vscode folder does not exist in /work/test.
Is this the root directory of your project? (y/n): y
Updated /work/test/.vscode/settings.json with extraPaths.
(isaaclab-py3.10) root@ub20-04:/work/test# 

此时可以看到工程目录生成了文件 .vscode/settings.json,内容大致如下:

{
    "python.analysis.extraPaths": [
        "/isaac-sim/extscache/omni.usd.metrics.assembler.physics-106.1.0+106.1.0",
        "/isaac-sim/extscache/omni.kit.waypoint.playlist-1.0.8",
        "/isaac-sim/extscache/omni.kit.xr.profile.common-106.1.24+106.1.0",
        "/isaac-sim/extscache/omni.kit.data2ui.core-1.0.27+106.0",
        .......
    ]
}

在根目录下创建创建文件 test.py编写代码如下:

from omni.isaac.core import World
from pxr import Usd, UsdGeom
import omni

# Create default plane
world = World()
world.scene.add_default_ground_plane()

# Add a sphere
stage = omni.usd.get_context().get_stage()
xformPrim = UsdGeom.Xform.Define(stage, "/hello")
spherePrim = UsdGeom.Sphere.Define(stage, "/hello/sphere")

通过 Isaac Sim VS Code Edition 插件的 Run 或者 Run remotely 运行,同样出现墙面的界面:
在这里插入图片描述

四、Python: Attach

官方文档:https://docs.omniverse.nvidia.com/isaacsim/latest/advanced_tutorials/tutorial_advanced_python_debugging.html

1.环境搭建

关于 Python: Attach 的介绍,个人感觉官方介绍十分详细的,这里只是记录一下操作流程,避免后面忘记,或者方便自己回顾。首先启动 isaac sim 界面,依次选择 Window->Extension 进入拓展管理器,搜索 omni.kit.debug.vscode 并启用插件,并选择 AUTOLOAD,方便每次启动 isaac sim 均启用该插件:
在这里插入图片描述正常启动之后可以看到:
在这里插入图片描述
因为此时程序没有正常运行起来,所以显示红色的 VS Code Debugger Unattached,这个时候使用 vscode 打开源码目录,如本人为 /isaac-sim:
在这里插入图片描述
运行 Python: Attach (windows-x86_64/linux-x86_64) 选项之后,再回到 isaac sim 界面,点击 Refresh,如下所示:
在这里插入图片描述
可以看到其就变绿了,再点击启动的 Break:
在这里插入图片描述
此时回到 vsode 界面,可以看到其源码运行暂停:
在这里插入图片描述
此时就和普通 vscode debug 方式一样了,可以打断点或者逐步运行代码等。

2.远程 IP与端口

首先在 isaac-sim 启动的时候,可以指定 Attach 服务的 IP 与端口,在 /isaac-sim.sh --allow-root 指令下面添加如下选项即可:

--/exts/omni.kit.debug.python/host="127.0.0.1"
--/exts/omni.kit.debug.python/port=3000

不过需要注意,修改之后,本地 vscode 中 .vscode/launch.json 文件也要进行对应的修改,目前默认为:

        {
            "name": "Python: Attach (windows-x86_64/linux-x86_64)",
            "type": "python",
            "request": "attach",
            "port": 3000,
            "host": "localhost"
        },

 

五、结语

关于 Isaac Sim VS Code Edition 插件更加详细的说明暂时就不讲解了,应为本人也并没有怎么使用,若后续使用过程中发现比较方便,将会继续拓展。下图是关于 Isaac Sim VS Code Edition 插件菜单的简单介绍:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

江南才尽,年少无知!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值