|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""This script sets up the vs-code settings for this project. |
|
|
|
|
|
This script merges the extraPaths from the "{ISAACSIM_DIR}/.vscode/settings.json" file into |
|
|
the ".vscode/settings.json" file to enable code completion, go to definition, and other code navigation features |
|
|
and a default python interpreter set to the isaac-sim python.sh script. |
|
|
|
|
|
NOTE: This script is not specific to VSCode - it also supports Cursor. |
|
|
""" |
|
|
|
|
|
import os |
|
|
import pathlib |
|
|
import platform |
|
|
import re |
|
|
import sys |
|
|
|
|
|
|
|
|
PROJECT_DIR = pathlib.Path(__file__).parents[2] |
|
|
|
|
|
|
|
|
def get_isaacsim_dir() -> str: |
|
|
"""Get the location of the Isaac Sim directory via the following methods: |
|
|
|
|
|
1. Try to import isaacsim, and use __file__ to get the directory |
|
|
2. If that fails, try to get the ISAAC_PATH environment variable |
|
|
3. If that fails, return None |
|
|
|
|
|
Returns: |
|
|
The Isaac Sim directory if found, otherwise None. |
|
|
|
|
|
""" |
|
|
try: |
|
|
import isaacsim |
|
|
|
|
|
isaacsim_dir = pathlib.Path(isaacsim.__file__).parents[2] |
|
|
print(f"Found Isaac Sim directory via importing isaacsim: {isaacsim_dir}") |
|
|
except ModuleNotFoundError or ImportError: |
|
|
isaacsim_dir = os.environ.get("ISAAC_PATH") |
|
|
|
|
|
if isaacsim_dir is None: |
|
|
print( |
|
|
"ISAAC_PATH is not set. Please set the ISAAC_PATH environment variable to the root of your local Isaac" |
|
|
" Sim installation / clone if you want to use python language server with Isaac Sim imports." |
|
|
) |
|
|
return None |
|
|
|
|
|
if not os.path.exists(isaacsim_dir): |
|
|
raise FileNotFoundError( |
|
|
f"Could not find the isaac-sim directory: {isaacsim_dir} specified via ISAAC_PATH. Please provide the" |
|
|
" correct path to the Isaac Sim installation." |
|
|
) |
|
|
|
|
|
print(f"Found Isaac Sim directory via ISAAC_PATH: {isaacsim_dir}") |
|
|
|
|
|
return isaacsim_dir |
|
|
|
|
|
|
|
|
def read_vscode_settings(vscode_filename: str) -> str: |
|
|
"""Read VSCode settings from a file. |
|
|
|
|
|
Args: |
|
|
vscode_filename: Path to the VSCode settings file. |
|
|
|
|
|
Returns: |
|
|
The contents of the settings file as a string. |
|
|
""" |
|
|
with open(vscode_filename) as f: |
|
|
return f.read() |
|
|
|
|
|
|
|
|
def extract_extra_paths(vscode_settings: str) -> list[str]: |
|
|
"""Extract extraPaths from VSCode settings. |
|
|
|
|
|
Args: |
|
|
vscode_settings: The VSCode settings content as a string. |
|
|
|
|
|
Returns: |
|
|
List of path names from the extraPaths setting. |
|
|
""" |
|
|
|
|
|
settings_match = re.search( |
|
|
r"\"python.analysis.extraPaths\": \[.*?\]", vscode_settings, flags=re.MULTILINE | re.DOTALL |
|
|
) |
|
|
if not settings_match: |
|
|
|
|
|
settings_match = re.search( |
|
|
r"\"cursorpyright.analysis.extraPaths\": \[.*?\]", vscode_settings, flags=re.MULTILINE | re.DOTALL |
|
|
) |
|
|
|
|
|
if not settings_match: |
|
|
return [] |
|
|
|
|
|
settings = settings_match.group(0) |
|
|
settings = settings.split('"python.analysis.extraPaths": [')[-1] |
|
|
settings = settings.split("]")[0] |
|
|
|
|
|
|
|
|
path_names = settings.split(",") |
|
|
path_names = [path_name.strip().strip('"') for path_name in path_names] |
|
|
path_names = [path_name for path_name in path_names if len(path_name) > 0] |
|
|
|
|
|
return path_names |
|
|
|
|
|
|
|
|
def process_path_names(path_names: list[str], source_dir: str, target_dir: str = None) -> list[str]: |
|
|
"""Process path names to be relative to the target directory. |
|
|
|
|
|
Args: |
|
|
path_names: List of path names to process. |
|
|
source_dir: The source directory (relative to which paths are currently defined). |
|
|
target_dir: The target directory (relative to which paths should be defined). |
|
|
If None, uses PROJECT_DIR. |
|
|
|
|
|
Returns: |
|
|
List of processed path names. |
|
|
""" |
|
|
if target_dir is None: |
|
|
target_dir = PROJECT_DIR |
|
|
|
|
|
|
|
|
rel_path = os.path.relpath(source_dir, target_dir) |
|
|
processed_paths = [] |
|
|
|
|
|
for path_name in path_names: |
|
|
|
|
|
clean_path = path_name.replace("${workspaceFolder}/", "") |
|
|
processed_path = f'"${{workspaceFolder}}/{rel_path}/{clean_path}"' |
|
|
processed_paths.append(processed_path) |
|
|
|
|
|
return processed_paths |
|
|
|
|
|
|
|
|
def get_extra_paths_from_file(vscode_filename: str, source_dir: str, target_dir: str = None) -> list[str]: |
|
|
"""Get extra paths from a VSCode settings file. |
|
|
|
|
|
Args: |
|
|
vscode_filename: Path to the VSCode settings file. |
|
|
source_dir: The source directory (relative to which paths are currently defined). |
|
|
target_dir: The target directory (relative to which paths should be defined). |
|
|
|
|
|
Returns: |
|
|
List of processed path names. |
|
|
""" |
|
|
if not os.path.exists(vscode_filename): |
|
|
raise FileNotFoundError(f"Could not find the VS Code settings file: {vscode_filename}") |
|
|
|
|
|
vscode_settings = read_vscode_settings(vscode_filename) |
|
|
path_names = extract_extra_paths(vscode_settings) |
|
|
return process_path_names(path_names, source_dir, target_dir) |
|
|
|
|
|
|
|
|
def overwrite_python_analysis_extra_paths(isaacsim_dir: str | None, vscode_settings: str) -> str: |
|
|
"""Overwrite the extraPaths in the VS Code settings file. |
|
|
|
|
|
The extraPaths are replaced with the path names from the isaac-sim settings file that exists in the |
|
|
"{ISAACSIM_DIR}/.vscode/settings.json" file. |
|
|
|
|
|
If the isaac-sim VS Code settings file does not exist, the extraPaths are not overwritten. |
|
|
|
|
|
Args: |
|
|
isaacsim_dir: The path to the Isaac Sim directory. |
|
|
vscode_settings: The settings string to use as template. |
|
|
|
|
|
Returns: |
|
|
The settings string with overwritten python analysis extra paths. |
|
|
""" |
|
|
|
|
|
if isaacsim_dir is not None: |
|
|
isaacsim_vscode_filename = os.path.join(isaacsim_dir, ".vscode", "settings.json") |
|
|
isaacsim_paths = get_extra_paths_from_file(isaacsim_vscode_filename, isaacsim_dir) |
|
|
else: |
|
|
isaacsim_paths = [] |
|
|
|
|
|
|
|
|
template_paths = extract_extra_paths(vscode_settings) |
|
|
template_paths = process_path_names(template_paths, PROJECT_DIR) |
|
|
|
|
|
|
|
|
all_paths = isaacsim_paths + template_paths |
|
|
path_names = ",\n\t\t".expandtabs(4).join(all_paths) |
|
|
|
|
|
path_names = path_names.replace("\\", "/") |
|
|
|
|
|
for extra_path_type in ["python.analysis.extraPaths", "cursorpyright.analysis.extraPaths"]: |
|
|
|
|
|
vscode_settings = re.sub( |
|
|
rf"\"{extra_path_type}\": \[.*?\]", |
|
|
f'"{extra_path_type}": [\n\t\t'.expandtabs(4) + path_names + "\n\t]".expandtabs(4), |
|
|
vscode_settings, |
|
|
flags=re.DOTALL, |
|
|
) |
|
|
|
|
|
return vscode_settings |
|
|
|
|
|
|
|
|
def overwrite_default_python_interpreter(vscode_settings: str) -> str: |
|
|
"""Overwrite the default python interpreter in the VS Code settings file. |
|
|
|
|
|
The default python interpreter is replaced with the path to the python interpreter used by the |
|
|
isaac-sim project. This is necessary because the default python interpreter is the one shipped with |
|
|
isaac-sim. |
|
|
|
|
|
Args: |
|
|
vscode_settings: The settings string to use as template. |
|
|
|
|
|
Returns: |
|
|
The settings string with overwritten default python interpreter. |
|
|
""" |
|
|
|
|
|
python_exe = os.path.normpath(sys.executable) |
|
|
|
|
|
|
|
|
|
|
|
if f"kit{os.sep}python{os.sep}bin{os.sep}python" in python_exe: |
|
|
|
|
|
if platform.system() == "Windows": |
|
|
python_exe = python_exe.replace(f"kit{os.sep}python{os.sep}bin{os.sep}python3", "python.bat") |
|
|
else: |
|
|
python_exe = python_exe.replace(f"kit{os.sep}python{os.sep}bin{os.sep}python3", "python.sh") |
|
|
|
|
|
|
|
|
|
|
|
vscode_settings = re.sub( |
|
|
r"\"python.defaultInterpreterPath\": \".*?\"", |
|
|
f'"python.defaultInterpreterPath": "{python_exe}"', |
|
|
vscode_settings, |
|
|
flags=re.DOTALL, |
|
|
) |
|
|
|
|
|
return vscode_settings |
|
|
|
|
|
|
|
|
def create_header_message(template_filename: str) -> str: |
|
|
"""Create a header message for generated files. |
|
|
|
|
|
Args: |
|
|
template_filename: The template filename to reference in the header. |
|
|
|
|
|
Returns: |
|
|
The header message string. |
|
|
""" |
|
|
return ( |
|
|
"// This file is a template and is automatically generated by the setup_vscode.py script.\n" |
|
|
"// Do not edit this file directly.\n" |
|
|
"// \n" |
|
|
f"// Generated from: {template_filename}\n" |
|
|
) |
|
|
|
|
|
|
|
|
def write_settings_file(output_filename: str, content: str, template_filename: str): |
|
|
"""Write settings content to a file with header. |
|
|
|
|
|
Args: |
|
|
output_filename: The output filename. |
|
|
content: The content to write. |
|
|
template_filename: The template filename for the header. |
|
|
""" |
|
|
header_message = create_header_message(template_filename) |
|
|
full_content = header_message + content |
|
|
|
|
|
with open(output_filename, "w") as f: |
|
|
f.write(full_content) |
|
|
|
|
|
|
|
|
def main(): |
|
|
"""Main function to setup the VS Code settings for the project.""" |
|
|
|
|
|
isaacsim_dir = get_isaacsim_dir() |
|
|
|
|
|
|
|
|
pairlab_vscode_template_filename = os.path.join(PROJECT_DIR, ".vscode", "tools", "settings.template.json") |
|
|
|
|
|
|
|
|
if not os.path.exists(pairlab_vscode_template_filename): |
|
|
raise FileNotFoundError( |
|
|
f"Could not find the PAIR Lab template settings file: {pairlab_vscode_template_filename}" |
|
|
) |
|
|
|
|
|
with open(pairlab_vscode_template_filename) as f: |
|
|
pairlab_template_vscode_settings = f.read() |
|
|
|
|
|
|
|
|
vscode_settings = overwrite_python_analysis_extra_paths(isaacsim_dir, pairlab_template_vscode_settings) |
|
|
vscode_settings = overwrite_default_python_interpreter(vscode_settings) |
|
|
|
|
|
|
|
|
isaaclab_vscode_filename = os.path.join(PROJECT_DIR, ".vscode", "settings.json") |
|
|
write_settings_file(isaaclab_vscode_filename, vscode_settings, pairlab_vscode_template_filename) |
|
|
|
|
|
|
|
|
isaaclab_vscode_launch_filename = os.path.join(PROJECT_DIR, ".vscode", "launch.json") |
|
|
isaaclab_vscode_template_launch_filename = os.path.join(PROJECT_DIR, ".vscode", "tools", "launch.template.json") |
|
|
if not os.path.exists(isaaclab_vscode_launch_filename): |
|
|
|
|
|
with open(isaaclab_vscode_template_launch_filename) as f: |
|
|
isaaclab_template_launch_settings = f.read() |
|
|
|
|
|
write_settings_file( |
|
|
isaaclab_vscode_launch_filename, isaaclab_template_launch_settings, isaaclab_vscode_template_launch_filename |
|
|
) |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
main() |
|
|
|