forked from isaac-sim/IsaacLab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
isaaclab.sh
executable file
·374 lines (358 loc) · 13.8 KB
/
isaaclab.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#!/usr/bin/env bash
# Copyright (c) 2022-2024, The Isaac Lab Project Developers.
# All rights reserved.
#
# SPDX-License-Identifier: BSD-3-Clause
#==
# Configurations
#==
# Exits if error occurs
set -e
# Set tab-spaces
#tabs 4
# get source directory
export ISAACLAB_PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
#==
# Helper functions
#==
# extract the python from isaacsim
extract_python_exe() {
# Check if IsaacSim directory manually specified
# Note: for manually build isaacsim, this: _build/linux-x86_64/release
if [ ! -z ${ISAACSIM_PATH} ];
then
# Use local build
build_path=${ISAACSIM_PATH}
else
# Use TeamCity build
build_path=${ISAACLAB_PATH}/_isaac_sim
fi
# check if using conda
if ! [[ -z "${CONDA_PREFIX}" ]]; then
# use conda python
local python_exe=${CONDA_PREFIX}/bin/python
else
if pip show isaacsim-rl > /dev/null 2>&1; then
# use current python executable
python_exe=$(which python)
else
# use python from kit
local python_exe=${build_path}/python.sh
fi
fi
# check if there is a python path available
if [ ! -f "${python_exe}" ]; then
echo "[ERROR] No python executable found at path: ${build_path}" >&2
exit 1
fi
# return the result
echo ${python_exe}
}
# extract the simulator exe from isaacsim
extract_isaacsim_exe() {
# Check if IsaacSim directory manually specified
# Note: for manually build isaacsim, this: _build/linux-x86_64/release
if [ ! -z ${ISAACSIM_PATH} ];
then
# Use local build
build_path=${ISAACSIM_PATH}
else
# Use TeamCity build
build_path=${ISAACLAB_PATH}/_isaac_sim
fi
# python executable to use
local isaacsim_exe=${build_path}/isaac-sim.sh
# check if there is a python path available
if [ ! -f "${isaacsim_exe}" ]; then
echo "[ERROR] No isaac-sim executable found at path: ${build_path}" >&2
exit 1
fi
# return the result
echo ${isaacsim_exe}
}
# check if input directory is a python extension and install the module
install_isaaclab_extension() {
# retrieve the python executable
python_exe=$(extract_python_exe)
# if the directory contains setup.py then install the python module
if [ -f "$1/setup.py" ]; then
echo -e "\t module: $1"
${python_exe} -m pip install --editable $1
fi
}
# setup anaconda environment for Isaac Lab
setup_conda_env() {
# get environment name from input
local env_name=$1
# check conda is installed
if ! command -v conda &> /dev/null
then
echo "[ERROR] Conda could not be found. Please install conda and try again."
exit 1
fi
# Check if IsaacSim directory manually specified
# Note: for manually build isaacsim, this: _build/linux-x86_64/release
if [ ! -z ${ISAACSIM_PATH} ];
then
# Use local build
build_path=${ISAACSIM_PATH}
else
# Use TeamCity build
build_path=${ISAACLAB_PATH}/_isaac_sim
fi
# check if the environment exists
if { conda env list | grep -w ${env_name}; } >/dev/null 2>&1; then
echo -e "[INFO] Conda environment named '${env_name}' already exists."
else
echo -e "[INFO] Creating conda environment named '${env_name}'..."
conda env create --name ${env_name} -f ${build_path}/environment.yml
fi
# cache current paths for later
cache_pythonpath=$PYTHONPATH
cache_ld_library_path=$LD_LIBRARY_PATH
# clear any existing files
rm -f ${CONDA_PREFIX}/etc/conda/activate.d/setenv.sh
rm -f ${CONDA_PREFIX}/etc/conda/deactivate.d/unsetenv.sh
# activate the environment
source $(conda info --base)/etc/profile.d/conda.sh
conda activate ${env_name}
# setup directories to load Isaac Sim variables
mkdir -p ${CONDA_PREFIX}/etc/conda/activate.d
mkdir -p ${CONDA_PREFIX}/etc/conda/deactivate.d
# add variables to environment during activation
local isaacsim_setup_conda_env_script=${ISAACLAB_PATH}/_isaac_sim/setup_conda_env.sh
printf '%s\n' '#!/usr/bin/env bash' '' \
'# for Isaac Sim' \
'source '${isaacsim_setup_conda_env_script}'' \
'' \
'# for Isaac Lab' \
'export ISAACLAB_PATH='${ISAACLAB_PATH}'' \
'alias isaaclab='${ISAACLAB_PATH}'/isaaclab.sh' \
'' \
'# show icon if not runninng headless' \
'export RESOURCE_NAME="IsaacSim"' \
'' > ${CONDA_PREFIX}/etc/conda/activate.d/setenv.sh
# reactivate the environment to load the variables
# needed because deactivate complains about Isaac Lab alias since it otherwise doesn't exist
conda activate ${env_name}
# remove variables from environment during deactivation
printf '%s\n' '#!/usr/bin/env bash' '' \
'# for Isaac Lab' \
'unalias isaaclab &>/dev/null' \
'unset ISAACLAB_PATH' \
'' \
'# for Isaac Sim' \
'unset CARB_APP_PATH' \
'unset EXP_PATH' \
'unset ISAAC_PATH' \
'unset RESOURCE_NAME' \
'' \
'# restore paths' \
'export PYTHONPATH='${cache_pythonpath}'' \
'export LD_LIBRARY_PATH='${cache_ld_library_path}'' \
'' > ${CONDA_PREFIX}/etc/conda/deactivate.d/unsetenv.sh
# install some extra dependencies
echo -e "[INFO] Installing extra dependencies (this might take a few minutes)..."
conda install -c conda-forge -y importlib_metadata &> /dev/null
# deactivate the environment
conda deactivate
# add information to the user about alias
echo -e "[INFO] Added 'isaaclab' alias to conda environment for 'isaaclab.sh' script."
echo -e "[INFO] Created conda environment named '${env_name}'.\n"
echo -e "\t\t1. To activate the environment, run: conda activate ${env_name}"
echo -e "\t\t2. To install Isaac Lab extensions, run: isaaclab -i"
echo -e "\t\t4. To perform formatting, run: isaaclab -f"
echo -e "\t\t5. To deactivate the environment, run: conda deactivate"
echo -e "\n"
}
# update the vscode settings from template and isaac sim settings
update_vscode_settings() {
echo "[INFO] Setting up vscode settings..."
# retrieve the python executable
python_exe=$(extract_python_exe)
# path to setup_vscode.py
setup_vscode_script="${ISAACLAB_PATH}/.vscode/tools/setup_vscode.py"
# check if the file exists before attempting to run it
if [ -f "${setup_vscode_script}" ]; then
${python_exe} "${setup_vscode_script}"
else
echo "[WARNING] setup_vscode.py not found. Aborting vscode settings setup."
fi
}
# print the usage description
print_help () {
echo -e "\nusage: $(basename "$0") [-h] [-i] [-f] [-p] [-s] [-t] [-o] [-v] [-d] [-c] -- Utility to manage Isaac Lab."
echo -e "\noptional arguments:"
echo -e "\t-h, --help Display the help content."
echo -e "\t-i, --install [LIB] Install the extensions inside Isaac Lab and learning frameworks as extra dependencies. Default is 'all'."
echo -e "\t-f, --format Run pre-commit to format the code and check lints."
echo -e "\t-p, --python Run the python executable provided by Isaac Sim or virtual environment (if active)."
echo -e "\t-s, --sim Run the simulator executable (isaac-sim.sh) provided by Isaac Sim."
echo -e "\t-t, --test Run all python unittest tests."
echo -e "\t-o, --docker Run the docker container helper script (docker/container.sh)."
echo -e "\t-v, --vscode Generate the VSCode settings file from template."
echo -e "\t-d, --docs Build the documentation from source using sphinx."
echo -e "\t-c, --conda [NAME] Create the conda environment for Isaac Lab. Default name is 'isaaclab'."
echo -e "\n" >&2
}
#==
# Main
#==
# check argument provided
if [ -z "$*" ]; then
echo "[Error] No arguments provided." >&2;
print_help
exit 1
fi
# pass the arguments
while [[ $# -gt 0 ]]; do
# read the key
case "$1" in
-i|--install)
# install the python packages in IsaacLab/source directory
echo "[INFO] Installing extensions inside the Isaac Lab repository..."
python_exe=$(extract_python_exe)
# recursively look into directories and install them
# this does not check dependencies between extensions
export -f extract_python_exe
export -f install_isaaclab_extension
# source directory
find -L "${ISAACLAB_PATH}/source/extensions" -mindepth 1 -maxdepth 1 -type d -exec bash -c 'install_isaaclab_extension "{}"' \;
# unset local variables
unset install_isaaclab_extension
# setup vscode settings
if ! ${python_exe} -m pip show isaacsim-rl &>/dev/null; then
update_vscode_settings
fi
# install the python packages for supported reinforcement learning frameworks
echo "[INFO] Installing extra requirements such as learning frameworks..."
# check if specified which rl-framework to install
if [ -z "$2" ]; then
echo "[INFO] Installing all rl-frameworks..."
framework_name="all"
elif [ "$2" = "none" ]; then
echo "[INFO] No rl-framework will be installed."
framework_name="none"
shift # past argument
else
echo "[INFO] Installing rl-framework: $2"
framework_name=$2
shift # past argument
fi
# install the rl-frameworks specified
${python_exe} -m pip install -e ${ISAACLAB_PATH}/source/extensions/omni.isaac.lab_tasks["${framework_name}"]
shift # past argument
;;
-c|--conda)
# use default name if not provided
if [ -z "$2" ]; then
echo "[INFO] Using default conda environment name: isaaclab"
conda_env_name="isaaclab"
else
echo "[INFO] Using conda environment name: $2"
conda_env_name=$2
shift # past argument
fi
# setup the conda environment for Isaac Lab
setup_conda_env ${conda_env_name}
shift # past argument
;;
-f|--format)
# reset the python path to avoid conflicts with pre-commit
# this is needed because the pre-commit hooks are installed in a separate virtual environment
# and it uses the system python to run the hooks
if [ -n "${CONDA_DEFAULT_ENV}" ]; then
cache_pythonpath=${PYTHONPATH}
export PYTHONPATH=""
fi
# run the formatter over the repository
# check if pre-commit is installed
if ! command -v pre-commit &>/dev/null; then
echo "[INFO] Installing pre-commit..."
pip install pre-commit
fi
# always execute inside the Isaac Lab directory
echo "[INFO] Formatting the repository..."
cd ${ISAACLAB_PATH}
pre-commit run --all-files
cd - > /dev/null
# set the python path back to the original value
if [ -n "${CONDA_DEFAULT_ENV}" ]; then
export PYTHONPATH=${cache_pythonpath}
fi
shift # past argument
# exit neatly
break
;;
-p|--python)
# run the python provided by isaacsim
python_exe=$(extract_python_exe)
echo "[INFO] Using python from: ${python_exe}"
shift # past argument
${python_exe} $@
# exit neatly
break
;;
-s|--sim)
# run the simulator exe provided by isaacsim
isaacsim_exe=$(extract_isaacsim_exe)
echo "[INFO] Running isaac-sim from: ${isaacsim_exe}"
shift # past argument
${isaacsim_exe} --ext-folder ${ISAACLAB_PATH}/source/extensions $@
# exit neatly
break
;;
-t|--test)
# run the python provided by isaacsim
python_exe=$(extract_python_exe)
shift # past argument
${python_exe} ${ISAACLAB_PATH}/tools/run_all_tests.py $@
# exit neatly
break
;;
-o|--docker)
# run the docker container helper script
docker_script=${ISAACLAB_PATH}/docker/container.sh
echo "[INFO] Running docker utility script from: ${docker_script}"
shift # past argument
bash ${docker_script} $@
# exit neatly
break
;;
-v|--vscode)
# update the vscode settings
update_vscode_settings
shift # past argument
# exit neatly
break
;;
-d|--docs)
# build the documentation
echo "[INFO] Building documentation..."
# retrieve the python executable
python_exe=$(extract_python_exe)
# install pip packages
cd ${ISAACLAB_PATH}/docs
${python_exe} -m pip install -r requirements.txt > /dev/null
# build the documentation
${python_exe} -m sphinx -b html -d _build/doctrees . _build/html
# open the documentation
echo -e "[INFO] To open documentation on default browser, run:"
echo -e "\n\t\txdg-open $(pwd)/_build/html/index.html\n"
# exit neatly
cd - > /dev/null
shift # past argument
# exit neatly
break
;;
-h|--help)
print_help
exit 1
;;
*) # unknown option
echo "[Error] Invalid argument provided: $1"
print_help
exit 1
;;
esac
done