warp-lang 1.0.2__py3-none-manylinux2014_x86_64.whl → 1.2.0__py3-none-manylinux2014_x86_64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of warp-lang might be problematic. Click here for more details.

Files changed (356) hide show
  1. warp/__init__.py +108 -97
  2. warp/__init__.pyi +1 -1
  3. warp/bin/warp-clang.so +0 -0
  4. warp/bin/warp.so +0 -0
  5. warp/build.py +88 -113
  6. warp/build_dll.py +383 -375
  7. warp/builtins.py +3693 -3354
  8. warp/codegen.py +2925 -2792
  9. warp/config.py +40 -36
  10. warp/constants.py +49 -45
  11. warp/context.py +5409 -5102
  12. warp/dlpack.py +442 -442
  13. warp/examples/__init__.py +16 -16
  14. warp/examples/assets/bear.usd +0 -0
  15. warp/examples/assets/bunny.usd +0 -0
  16. warp/examples/assets/cartpole.urdf +110 -110
  17. warp/examples/assets/crazyflie.usd +0 -0
  18. warp/examples/assets/cube.usd +0 -0
  19. warp/examples/assets/nv_ant.xml +92 -92
  20. warp/examples/assets/nv_humanoid.xml +183 -183
  21. warp/examples/assets/quadruped.urdf +267 -267
  22. warp/examples/assets/rocks.nvdb +0 -0
  23. warp/examples/assets/rocks.usd +0 -0
  24. warp/examples/assets/sphere.usd +0 -0
  25. warp/examples/benchmarks/benchmark_api.py +381 -383
  26. warp/examples/benchmarks/benchmark_cloth.py +278 -277
  27. warp/examples/benchmarks/benchmark_cloth_cupy.py +88 -88
  28. warp/examples/benchmarks/benchmark_cloth_jax.py +97 -100
  29. warp/examples/benchmarks/benchmark_cloth_numba.py +146 -142
  30. warp/examples/benchmarks/benchmark_cloth_numpy.py +77 -77
  31. warp/examples/benchmarks/benchmark_cloth_pytorch.py +86 -86
  32. warp/examples/benchmarks/benchmark_cloth_taichi.py +112 -112
  33. warp/examples/benchmarks/benchmark_cloth_warp.py +145 -146
  34. warp/examples/benchmarks/benchmark_launches.py +293 -295
  35. warp/examples/browse.py +29 -29
  36. warp/examples/core/example_dem.py +232 -219
  37. warp/examples/core/example_fluid.py +291 -267
  38. warp/examples/core/example_graph_capture.py +142 -126
  39. warp/examples/core/example_marching_cubes.py +186 -174
  40. warp/examples/core/example_mesh.py +172 -155
  41. warp/examples/core/example_mesh_intersect.py +203 -193
  42. warp/examples/core/example_nvdb.py +174 -170
  43. warp/examples/core/example_raycast.py +103 -90
  44. warp/examples/core/example_raymarch.py +197 -178
  45. warp/examples/core/example_render_opengl.py +183 -141
  46. warp/examples/core/example_sph.py +403 -387
  47. warp/examples/core/example_torch.py +219 -181
  48. warp/examples/core/example_wave.py +261 -248
  49. warp/examples/fem/bsr_utils.py +378 -380
  50. warp/examples/fem/example_apic_fluid.py +432 -389
  51. warp/examples/fem/example_burgers.py +262 -0
  52. warp/examples/fem/example_convection_diffusion.py +180 -168
  53. warp/examples/fem/example_convection_diffusion_dg.py +217 -209
  54. warp/examples/fem/example_deformed_geometry.py +175 -159
  55. warp/examples/fem/example_diffusion.py +199 -173
  56. warp/examples/fem/example_diffusion_3d.py +178 -152
  57. warp/examples/fem/example_diffusion_mgpu.py +219 -214
  58. warp/examples/fem/example_mixed_elasticity.py +242 -222
  59. warp/examples/fem/example_navier_stokes.py +257 -243
  60. warp/examples/fem/example_stokes.py +218 -192
  61. warp/examples/fem/example_stokes_transfer.py +263 -249
  62. warp/examples/fem/mesh_utils.py +133 -109
  63. warp/examples/fem/plot_utils.py +292 -287
  64. warp/examples/optim/example_bounce.py +258 -246
  65. warp/examples/optim/example_cloth_throw.py +220 -209
  66. warp/examples/optim/example_diffray.py +564 -536
  67. warp/examples/optim/example_drone.py +862 -835
  68. warp/examples/optim/example_inverse_kinematics.py +174 -168
  69. warp/examples/optim/example_inverse_kinematics_torch.py +183 -169
  70. warp/examples/optim/example_spring_cage.py +237 -231
  71. warp/examples/optim/example_trajectory.py +221 -199
  72. warp/examples/optim/example_walker.py +304 -293
  73. warp/examples/sim/example_cartpole.py +137 -129
  74. warp/examples/sim/example_cloth.py +194 -186
  75. warp/examples/sim/example_granular.py +122 -111
  76. warp/examples/sim/example_granular_collision_sdf.py +195 -186
  77. warp/examples/sim/example_jacobian_ik.py +234 -214
  78. warp/examples/sim/example_particle_chain.py +116 -105
  79. warp/examples/sim/example_quadruped.py +191 -180
  80. warp/examples/sim/example_rigid_chain.py +195 -187
  81. warp/examples/sim/example_rigid_contact.py +187 -177
  82. warp/examples/sim/example_rigid_force.py +125 -125
  83. warp/examples/sim/example_rigid_gyroscopic.py +107 -95
  84. warp/examples/sim/example_rigid_soft_contact.py +132 -122
  85. warp/examples/sim/example_soft_body.py +188 -177
  86. warp/fabric.py +337 -335
  87. warp/fem/__init__.py +61 -27
  88. warp/fem/cache.py +403 -388
  89. warp/fem/dirichlet.py +178 -179
  90. warp/fem/domain.py +262 -263
  91. warp/fem/field/__init__.py +100 -101
  92. warp/fem/field/field.py +148 -149
  93. warp/fem/field/nodal_field.py +298 -299
  94. warp/fem/field/restriction.py +22 -21
  95. warp/fem/field/test.py +180 -181
  96. warp/fem/field/trial.py +183 -183
  97. warp/fem/geometry/__init__.py +16 -19
  98. warp/fem/geometry/closest_point.py +69 -70
  99. warp/fem/geometry/deformed_geometry.py +270 -271
  100. warp/fem/geometry/element.py +748 -744
  101. warp/fem/geometry/geometry.py +184 -186
  102. warp/fem/geometry/grid_2d.py +380 -373
  103. warp/fem/geometry/grid_3d.py +437 -435
  104. warp/fem/geometry/hexmesh.py +953 -953
  105. warp/fem/geometry/nanogrid.py +455 -0
  106. warp/fem/geometry/partition.py +374 -376
  107. warp/fem/geometry/quadmesh_2d.py +532 -532
  108. warp/fem/geometry/tetmesh.py +840 -840
  109. warp/fem/geometry/trimesh_2d.py +577 -577
  110. warp/fem/integrate.py +1684 -1615
  111. warp/fem/operator.py +190 -191
  112. warp/fem/polynomial.py +214 -213
  113. warp/fem/quadrature/__init__.py +2 -2
  114. warp/fem/quadrature/pic_quadrature.py +243 -245
  115. warp/fem/quadrature/quadrature.py +295 -294
  116. warp/fem/space/__init__.py +179 -292
  117. warp/fem/space/basis_space.py +522 -489
  118. warp/fem/space/collocated_function_space.py +100 -105
  119. warp/fem/space/dof_mapper.py +236 -236
  120. warp/fem/space/function_space.py +148 -145
  121. warp/fem/space/grid_2d_function_space.py +148 -267
  122. warp/fem/space/grid_3d_function_space.py +167 -306
  123. warp/fem/space/hexmesh_function_space.py +253 -352
  124. warp/fem/space/nanogrid_function_space.py +202 -0
  125. warp/fem/space/partition.py +350 -350
  126. warp/fem/space/quadmesh_2d_function_space.py +261 -369
  127. warp/fem/space/restriction.py +161 -160
  128. warp/fem/space/shape/__init__.py +90 -15
  129. warp/fem/space/shape/cube_shape_function.py +728 -738
  130. warp/fem/space/shape/shape_function.py +102 -103
  131. warp/fem/space/shape/square_shape_function.py +611 -611
  132. warp/fem/space/shape/tet_shape_function.py +565 -567
  133. warp/fem/space/shape/triangle_shape_function.py +429 -429
  134. warp/fem/space/tetmesh_function_space.py +224 -292
  135. warp/fem/space/topology.py +297 -295
  136. warp/fem/space/trimesh_2d_function_space.py +153 -221
  137. warp/fem/types.py +77 -77
  138. warp/fem/utils.py +495 -495
  139. warp/jax.py +166 -141
  140. warp/jax_experimental.py +341 -339
  141. warp/native/array.h +1081 -1025
  142. warp/native/builtin.h +1603 -1560
  143. warp/native/bvh.cpp +402 -398
  144. warp/native/bvh.cu +533 -525
  145. warp/native/bvh.h +430 -429
  146. warp/native/clang/clang.cpp +496 -464
  147. warp/native/crt.cpp +42 -32
  148. warp/native/crt.h +352 -335
  149. warp/native/cuda_crt.h +1049 -1049
  150. warp/native/cuda_util.cpp +549 -540
  151. warp/native/cuda_util.h +288 -203
  152. warp/native/cutlass_gemm.cpp +34 -34
  153. warp/native/cutlass_gemm.cu +372 -372
  154. warp/native/error.cpp +66 -66
  155. warp/native/error.h +27 -27
  156. warp/native/exports.h +187 -0
  157. warp/native/fabric.h +228 -228
  158. warp/native/hashgrid.cpp +301 -278
  159. warp/native/hashgrid.cu +78 -77
  160. warp/native/hashgrid.h +227 -227
  161. warp/native/initializer_array.h +32 -32
  162. warp/native/intersect.h +1204 -1204
  163. warp/native/intersect_adj.h +365 -365
  164. warp/native/intersect_tri.h +322 -322
  165. warp/native/marching.cpp +2 -2
  166. warp/native/marching.cu +497 -497
  167. warp/native/marching.h +2 -2
  168. warp/native/mat.h +1545 -1498
  169. warp/native/matnn.h +333 -333
  170. warp/native/mesh.cpp +203 -203
  171. warp/native/mesh.cu +292 -293
  172. warp/native/mesh.h +1887 -1887
  173. warp/native/nanovdb/GridHandle.h +366 -0
  174. warp/native/nanovdb/HostBuffer.h +590 -0
  175. warp/native/nanovdb/NanoVDB.h +6624 -4782
  176. warp/native/nanovdb/PNanoVDB.h +3390 -2553
  177. warp/native/noise.h +850 -850
  178. warp/native/quat.h +1112 -1085
  179. warp/native/rand.h +303 -299
  180. warp/native/range.h +108 -108
  181. warp/native/reduce.cpp +156 -156
  182. warp/native/reduce.cu +348 -348
  183. warp/native/runlength_encode.cpp +61 -61
  184. warp/native/runlength_encode.cu +46 -46
  185. warp/native/scan.cpp +30 -30
  186. warp/native/scan.cu +36 -36
  187. warp/native/scan.h +7 -7
  188. warp/native/solid_angle.h +442 -442
  189. warp/native/sort.cpp +94 -94
  190. warp/native/sort.cu +97 -97
  191. warp/native/sort.h +14 -14
  192. warp/native/sparse.cpp +337 -337
  193. warp/native/sparse.cu +544 -544
  194. warp/native/spatial.h +630 -630
  195. warp/native/svd.h +562 -562
  196. warp/native/temp_buffer.h +30 -30
  197. warp/native/vec.h +1177 -1133
  198. warp/native/volume.cpp +529 -297
  199. warp/native/volume.cu +58 -32
  200. warp/native/volume.h +960 -538
  201. warp/native/volume_builder.cu +446 -425
  202. warp/native/volume_builder.h +34 -19
  203. warp/native/volume_impl.h +61 -0
  204. warp/native/warp.cpp +1057 -1052
  205. warp/native/warp.cu +2949 -2828
  206. warp/native/warp.h +321 -305
  207. warp/optim/__init__.py +9 -9
  208. warp/optim/adam.py +120 -120
  209. warp/optim/linear.py +1104 -939
  210. warp/optim/sgd.py +104 -92
  211. warp/render/__init__.py +10 -10
  212. warp/render/render_opengl.py +3356 -3204
  213. warp/render/render_usd.py +768 -749
  214. warp/render/utils.py +152 -150
  215. warp/sim/__init__.py +52 -59
  216. warp/sim/articulation.py +685 -685
  217. warp/sim/collide.py +1594 -1590
  218. warp/sim/import_mjcf.py +489 -481
  219. warp/sim/import_snu.py +220 -221
  220. warp/sim/import_urdf.py +536 -516
  221. warp/sim/import_usd.py +887 -881
  222. warp/sim/inertia.py +316 -317
  223. warp/sim/integrator.py +234 -233
  224. warp/sim/integrator_euler.py +1956 -1956
  225. warp/sim/integrator_featherstone.py +1917 -1991
  226. warp/sim/integrator_xpbd.py +3288 -3312
  227. warp/sim/model.py +4473 -4314
  228. warp/sim/particles.py +113 -112
  229. warp/sim/render.py +417 -403
  230. warp/sim/utils.py +413 -410
  231. warp/sparse.py +1289 -1227
  232. warp/stubs.py +2192 -2469
  233. warp/tape.py +1162 -225
  234. warp/tests/__init__.py +1 -1
  235. warp/tests/__main__.py +4 -4
  236. warp/tests/assets/test_index_grid.nvdb +0 -0
  237. warp/tests/assets/torus.usda +105 -105
  238. warp/tests/aux_test_class_kernel.py +26 -26
  239. warp/tests/aux_test_compile_consts_dummy.py +10 -10
  240. warp/tests/aux_test_conditional_unequal_types_kernels.py +21 -21
  241. warp/tests/aux_test_dependent.py +20 -22
  242. warp/tests/aux_test_grad_customs.py +21 -23
  243. warp/tests/aux_test_reference.py +9 -11
  244. warp/tests/aux_test_reference_reference.py +8 -10
  245. warp/tests/aux_test_square.py +15 -17
  246. warp/tests/aux_test_unresolved_func.py +14 -14
  247. warp/tests/aux_test_unresolved_symbol.py +14 -14
  248. warp/tests/disabled_kinematics.py +237 -239
  249. warp/tests/run_coverage_serial.py +31 -31
  250. warp/tests/test_adam.py +155 -157
  251. warp/tests/test_arithmetic.py +1088 -1124
  252. warp/tests/test_array.py +2415 -2326
  253. warp/tests/test_array_reduce.py +148 -150
  254. warp/tests/test_async.py +666 -656
  255. warp/tests/test_atomic.py +139 -141
  256. warp/tests/test_bool.py +212 -149
  257. warp/tests/test_builtins_resolution.py +1290 -1292
  258. warp/tests/test_bvh.py +162 -171
  259. warp/tests/test_closest_point_edge_edge.py +227 -228
  260. warp/tests/test_codegen.py +562 -553
  261. warp/tests/test_compile_consts.py +217 -101
  262. warp/tests/test_conditional.py +244 -246
  263. warp/tests/test_copy.py +230 -215
  264. warp/tests/test_ctypes.py +630 -632
  265. warp/tests/test_dense.py +65 -67
  266. warp/tests/test_devices.py +89 -98
  267. warp/tests/test_dlpack.py +528 -529
  268. warp/tests/test_examples.py +403 -378
  269. warp/tests/test_fabricarray.py +952 -955
  270. warp/tests/test_fast_math.py +60 -54
  271. warp/tests/test_fem.py +1298 -1278
  272. warp/tests/test_fp16.py +128 -130
  273. warp/tests/test_func.py +336 -337
  274. warp/tests/test_generics.py +596 -571
  275. warp/tests/test_grad.py +885 -640
  276. warp/tests/test_grad_customs.py +331 -336
  277. warp/tests/test_hash_grid.py +208 -164
  278. warp/tests/test_import.py +37 -39
  279. warp/tests/test_indexedarray.py +1132 -1134
  280. warp/tests/test_intersect.py +65 -67
  281. warp/tests/test_jax.py +305 -307
  282. warp/tests/test_large.py +169 -164
  283. warp/tests/test_launch.py +352 -354
  284. warp/tests/test_lerp.py +217 -261
  285. warp/tests/test_linear_solvers.py +189 -171
  286. warp/tests/test_lvalue.py +419 -493
  287. warp/tests/test_marching_cubes.py +63 -65
  288. warp/tests/test_mat.py +1799 -1827
  289. warp/tests/test_mat_lite.py +113 -115
  290. warp/tests/test_mat_scalar_ops.py +2905 -2889
  291. warp/tests/test_math.py +124 -193
  292. warp/tests/test_matmul.py +498 -499
  293. warp/tests/test_matmul_lite.py +408 -410
  294. warp/tests/test_mempool.py +186 -190
  295. warp/tests/test_mesh.py +281 -324
  296. warp/tests/test_mesh_query_aabb.py +226 -241
  297. warp/tests/test_mesh_query_point.py +690 -702
  298. warp/tests/test_mesh_query_ray.py +290 -303
  299. warp/tests/test_mlp.py +274 -276
  300. warp/tests/test_model.py +108 -110
  301. warp/tests/test_module_hashing.py +111 -0
  302. warp/tests/test_modules_lite.py +36 -39
  303. warp/tests/test_multigpu.py +161 -163
  304. warp/tests/test_noise.py +244 -248
  305. warp/tests/test_operators.py +248 -250
  306. warp/tests/test_options.py +121 -125
  307. warp/tests/test_peer.py +131 -137
  308. warp/tests/test_pinned.py +76 -78
  309. warp/tests/test_print.py +52 -54
  310. warp/tests/test_quat.py +2084 -2086
  311. warp/tests/test_rand.py +324 -288
  312. warp/tests/test_reload.py +207 -217
  313. warp/tests/test_rounding.py +177 -179
  314. warp/tests/test_runlength_encode.py +188 -190
  315. warp/tests/test_sim_grad.py +241 -0
  316. warp/tests/test_sim_kinematics.py +89 -97
  317. warp/tests/test_smoothstep.py +166 -168
  318. warp/tests/test_snippet.py +303 -266
  319. warp/tests/test_sparse.py +466 -460
  320. warp/tests/test_spatial.py +2146 -2148
  321. warp/tests/test_special_values.py +362 -0
  322. warp/tests/test_streams.py +484 -473
  323. warp/tests/test_struct.py +708 -675
  324. warp/tests/test_tape.py +171 -148
  325. warp/tests/test_torch.py +741 -743
  326. warp/tests/test_transient_module.py +85 -87
  327. warp/tests/test_types.py +554 -659
  328. warp/tests/test_utils.py +488 -499
  329. warp/tests/test_vec.py +1262 -1268
  330. warp/tests/test_vec_lite.py +71 -73
  331. warp/tests/test_vec_scalar_ops.py +2097 -2099
  332. warp/tests/test_verify_fp.py +92 -94
  333. warp/tests/test_volume.py +961 -736
  334. warp/tests/test_volume_write.py +338 -265
  335. warp/tests/unittest_serial.py +38 -37
  336. warp/tests/unittest_suites.py +367 -359
  337. warp/tests/unittest_utils.py +434 -578
  338. warp/tests/unused_test_misc.py +69 -71
  339. warp/tests/walkthrough_debug.py +85 -85
  340. warp/thirdparty/appdirs.py +598 -598
  341. warp/thirdparty/dlpack.py +143 -143
  342. warp/thirdparty/unittest_parallel.py +563 -561
  343. warp/torch.py +321 -295
  344. warp/types.py +4941 -4450
  345. warp/utils.py +1008 -821
  346. {warp_lang-1.0.2.dist-info → warp_lang-1.2.0.dist-info}/LICENSE.md +126 -126
  347. {warp_lang-1.0.2.dist-info → warp_lang-1.2.0.dist-info}/METADATA +365 -400
  348. warp_lang-1.2.0.dist-info/RECORD +359 -0
  349. warp/examples/assets/cube.usda +0 -42
  350. warp/examples/assets/sphere.usda +0 -56
  351. warp/examples/assets/torus.usda +0 -105
  352. warp/examples/fem/example_convection_diffusion_dg0.py +0 -194
  353. warp/native/nanovdb/PNanoVDBWrite.h +0 -295
  354. warp_lang-1.0.2.dist-info/RECORD +0 -352
  355. {warp_lang-1.0.2.dist-info → warp_lang-1.2.0.dist-info}/WHEEL +0 -0
  356. {warp_lang-1.0.2.dist-info → warp_lang-1.2.0.dist-info}/top_level.txt +0 -0
warp/sim/import_urdf.py CHANGED
@@ -1,516 +1,536 @@
1
- # Copyright (c) 2022 NVIDIA CORPORATION. All rights reserved.
2
- # NVIDIA CORPORATION and its licensors retain all intellectual property
3
- # and proprietary rights in and to this software, related documentation
4
- # and any modifications thereto. Any use, reproduction, disclosure or
5
- # distribution of this software and related documentation without an express
6
- # license agreement from NVIDIA CORPORATION is strictly prohibited.
7
-
8
- import os
9
- import xml.etree.ElementTree as ET
10
- from typing import Union
11
-
12
- import numpy as np
13
-
14
- import warp as wp
15
- from warp.sim.model import Mesh
16
-
17
- from typing import Union
18
-
19
-
20
- def parse_urdf(
21
- urdf_filename,
22
- builder,
23
- xform=wp.transform(),
24
- floating=False,
25
- base_joint: Union[dict, str] = None,
26
- density=1000.0,
27
- stiffness=100.0,
28
- damping=10.0,
29
- armature=0.0,
30
- contact_ke=1.0e4,
31
- contact_kd=1.0e3,
32
- contact_kf=1.0e2,
33
- contact_ka=0.0,
34
- contact_mu=0.25,
35
- contact_restitution=0.5,
36
- contact_thickness=0.0,
37
- limit_ke=100.0,
38
- limit_kd=10.0,
39
- joint_limit_lower=-1e6,
40
- joint_limit_upper=1e6,
41
- scale=1.0,
42
- parse_visuals_as_colliders=False,
43
- force_show_colliders=False,
44
- enable_self_collisions=True,
45
- ignore_inertial_definitions=True,
46
- ensure_nonstatic_links=True,
47
- static_link_mass=1e-2,
48
- collapse_fixed_joints=False,
49
- ):
50
- """
51
- Parses a URDF file and adds the bodies and joints to the given ModelBuilder.
52
-
53
- Args:
54
- urdf_filename (str): The filename of the URDF file to parse.
55
- builder (ModelBuilder): The :class:`ModelBuilder` to add the bodies and joints to.
56
- xform (:ref:`transform <transform>`): The transform to apply to the root body.
57
- floating (bool): If True, the root body is a free joint. If False, the root body is connected via a fixed joint to the world, unless a `base_joint` is defined.
58
- base_joint (Union[str, dict]): The joint by which the root body is connected to the world. This can be either a string defining the joint axes of a D6 joint with comma-separated positional and angular axis names (e.g. "px,py,rz" for a D6 joint with linear axes in x, y and an angular axis in z) or a dict with joint parameters (see :meth:`ModelBuilder.add_joint`).
59
- density (float): The density of the shapes in kg/m^3 which will be used to calculate the body mass and inertia.
60
- stiffness (float): The stiffness of the joints.
61
- damping (float): The damping of the joints.
62
- armature (float): The armature of the joints (bias to add to the inertia diagonals that may stabilize the simulation).
63
- contact_ke (float): The stiffness of the shape contacts (used by the Euler integrators).
64
- contact_kd (float): The damping of the shape contacts (used by the Euler integrators).
65
- contact_kf (float): The friction stiffness of the shape contacts (used by the Euler integrators).
66
- contact_ka (float): The adhesion distance of the shape contacts (used by the Euler integrators).
67
- contact_mu (float): The friction coefficient of the shape contacts.
68
- contact_restitution (float): The restitution coefficient of the shape contacts.
69
- contact_thickness (float): The thickness to add to the shape geometry.
70
- limit_ke (float): The stiffness of the joint limits (used by the Euler integrators).
71
- limit_kd (float): The damping of the joint limits (used by the Euler integrators).
72
- joint_limit_lower (float): The default lower joint limit if not specified in the URDF.
73
- joint_limit_upper (float): The default upper joint limit if not specified in the URDF.
74
- scale (float): The scaling factor to apply to the imported mechanism.
75
- parse_visuals_as_colliders (bool): If True, the geometry defined under the `<visual>` tags is used for collision handling instead of the `<collision>` geometries.
76
- force_show_colliders (bool): If True, the collision shapes are always shown, even if there are visual shapes.
77
- enable_self_collisions (bool): If True, self-collisions are enabled.
78
- ignore_inertial_definitions (bool): If True, the inertial parameters defined in the URDF are ignored and the inertia is calculated from the shape geometry.
79
- ensure_nonstatic_links (bool): If True, links with zero mass are given a small mass (see `static_link_mass`) to ensure they are dynamic.
80
- static_link_mass (float): The mass to assign to links with zero mass (if `ensure_nonstatic_links` is set to True).
81
- collapse_fixed_joints (bool): If True, fixed joints are removed and the respective bodies are merged.
82
- """
83
-
84
- file = ET.parse(urdf_filename)
85
- root = file.getroot()
86
-
87
- contact_vars = dict(
88
- ke=contact_ke,
89
- kd=contact_kd,
90
- kf=contact_kf,
91
- ka=contact_ka,
92
- mu=contact_mu,
93
- restitution=contact_restitution,
94
- thickness=contact_thickness,
95
- )
96
-
97
- def parse_transform(element):
98
- if element is None or element.find("origin") is None:
99
- return wp.transform()
100
- origin = element.find("origin")
101
- xyz = origin.get("xyz") or "0 0 0"
102
- rpy = origin.get("rpy") or "0 0 0"
103
- xyz = [float(x) * scale for x in xyz.split()]
104
- rpy = [float(x) for x in rpy.split()]
105
- return wp.transform(xyz, wp.quat_rpy(*rpy))
106
-
107
- def parse_shapes(link, geoms, density, incoming_xform=None, visible=True, just_visual=False):
108
- shapes = []
109
- # add geometry
110
- for geom_group in geoms:
111
- geo = geom_group.find("geometry")
112
- if geo is None:
113
- continue
114
-
115
- tf = parse_transform(geom_group)
116
- if incoming_xform is not None:
117
- tf = incoming_xform * tf
118
-
119
- for box in geo.findall("box"):
120
- size = box.get("size") or "1 1 1"
121
- size = [float(x) for x in size.split()]
122
- s = builder.add_shape_box(
123
- body=link,
124
- pos=wp.vec3(tf.p),
125
- rot=wp.quat(tf.q),
126
- hx=size[0] * 0.5 * scale,
127
- hy=size[1] * 0.5 * scale,
128
- hz=size[2] * 0.5 * scale,
129
- density=density,
130
- is_visible=visible,
131
- has_ground_collision=not just_visual,
132
- **contact_vars,
133
- )
134
- shapes.append(s)
135
-
136
- for sphere in geo.findall("sphere"):
137
- s = builder.add_shape_sphere(
138
- body=link,
139
- pos=wp.vec3(tf.p),
140
- rot=wp.quat(tf.q),
141
- radius=float(sphere.get("radius") or "1") * scale,
142
- density=density,
143
- is_visible=visible,
144
- has_ground_collision=not just_visual,
145
- **contact_vars,
146
- )
147
- shapes.append(s)
148
-
149
- for cylinder in geo.findall("cylinder"):
150
- s = builder.add_shape_capsule(
151
- body=link,
152
- pos=wp.vec3(tf.p),
153
- rot=wp.quat(tf.q),
154
- radius=float(cylinder.get("radius") or "1") * scale,
155
- half_height=float(cylinder.get("length") or "1") * 0.5 * scale,
156
- density=density,
157
- up_axis=2, # cylinders in URDF are aligned with z-axis
158
- is_visible=visible,
159
- has_ground_collision=not just_visual,
160
- **contact_vars,
161
- )
162
- shapes.append(s)
163
-
164
- for mesh in geo.findall("mesh"):
165
- filename = mesh.get("filename")
166
- if filename is None:
167
- continue
168
- if filename.startswith("http://") or filename.startswith("https://"):
169
- # download mesh
170
- import shutil
171
- import tempfile
172
-
173
- import requests
174
-
175
- with tempfile.TemporaryDirectory() as tmpdir:
176
- # get filename extension
177
- extension = os.path.splitext(filename)[1]
178
- tmpfile = os.path.join(tmpdir, "mesh" + extension)
179
- with requests.get(filename, stream=True) as r:
180
- with open(tmpfile, "wb") as f:
181
- shutil.copyfileobj(r.raw, f)
182
- filename = tmpfile
183
- else:
184
- filename = os.path.join(os.path.dirname(urdf_filename), filename)
185
- if not os.path.exists(filename):
186
- wp.utils.warn(f"Warning: mesh file {filename} does not exist")
187
- continue
188
-
189
- import trimesh
190
-
191
- m = trimesh.load_mesh(filename)
192
- scaling = mesh.get("scale") or "1 1 1"
193
- scaling = np.array([float(x) * scale for x in scaling.split()])
194
- if hasattr(m, "geometry"):
195
- # multiple meshes are contained in a scene
196
- for geom in m.geometry.values():
197
- vertices = np.array(geom.vertices, dtype=np.float32) * scaling
198
- faces = np.array(geom.faces.flatten(), dtype=np.int32)
199
- mesh = Mesh(vertices, faces)
200
- s = builder.add_shape_mesh(
201
- body=link,
202
- pos=wp.vec3(tf.p),
203
- rot=wp.quat(tf.q),
204
- mesh=mesh,
205
- density=density,
206
- is_visible=visible,
207
- has_ground_collision=not just_visual,
208
- **contact_vars,
209
- )
210
- shapes.append(s)
211
- else:
212
- # a single mesh
213
- vertices = np.array(m.vertices, dtype=np.float32) * scaling
214
- faces = np.array(m.faces.flatten(), dtype=np.int32)
215
- mesh = Mesh(vertices, faces)
216
- s = builder.add_shape_mesh(
217
- body=link,
218
- pos=tf.p,
219
- rot=tf.q,
220
- mesh=mesh,
221
- density=density,
222
- is_visible=visible,
223
- has_ground_collision=not just_visual,
224
- **contact_vars,
225
- )
226
- shapes.append(s)
227
-
228
- return shapes
229
-
230
- # maps from link name -> link index
231
- link_index = {}
232
-
233
- visual_shapes = []
234
-
235
- builder.add_articulation()
236
-
237
- start_shape_count = len(builder.shape_geo_type)
238
-
239
- # add links
240
- for i, urdf_link in enumerate(root.findall("link")):
241
- name = urdf_link.get("name")
242
- link = builder.add_body(origin=wp.transform_identity(), armature=armature, name=name)
243
-
244
- # add ourselves to the index
245
- link_index[name] = link
246
-
247
- visuals = urdf_link.findall("visual")
248
- colliders = urdf_link.findall("collision")
249
-
250
- if parse_visuals_as_colliders:
251
- colliders = visuals
252
- else:
253
- s = parse_shapes(link, visuals, density=0.0, just_visual=True)
254
- visual_shapes.extend(s)
255
-
256
- show_colliders = force_show_colliders
257
- if parse_visuals_as_colliders:
258
- show_colliders = True
259
- elif len(visuals) == 0:
260
- # we need to show the collision shapes since there are no visual shapes
261
- show_colliders = True
262
-
263
- parse_shapes(link, colliders, density=density, visible=show_colliders)
264
- m = builder.body_mass[link]
265
- if not ignore_inertial_definitions and urdf_link.find("inertial") is not None:
266
- # overwrite inertial parameters if defined
267
- inertial = urdf_link.find("inertial")
268
- inertial_frame = parse_transform(inertial)
269
- com = inertial_frame.p
270
- I_m = np.zeros((3, 3))
271
- I_m[0, 0] = float(inertial.find("inertia").get("ixx") or "0") * scale**2
272
- I_m[1, 1] = float(inertial.find("inertia").get("iyy") or "0") * scale**2
273
- I_m[2, 2] = float(inertial.find("inertia").get("izz") or "0") * scale**2
274
- I_m[0, 1] = float(inertial.find("inertia").get("ixy") or "0") * scale**2
275
- I_m[0, 2] = float(inertial.find("inertia").get("ixz") or "0") * scale**2
276
- I_m[1, 2] = float(inertial.find("inertia").get("iyz") or "0") * scale**2
277
- I_m[1, 0] = I_m[0, 1]
278
- I_m[2, 0] = I_m[0, 2]
279
- I_m[2, 1] = I_m[1, 2]
280
- rot = wp.quat_to_matrix(inertial_frame.q)
281
- I_m = rot @ wp.mat33(I_m)
282
- m = float(inertial.find("mass").get("value") or "0")
283
- builder.body_mass[link] = m
284
- builder.body_inv_mass[link] = 1.0 / m
285
- builder.body_com[link] = com
286
- builder.body_inertia[link] = I_m
287
- builder.body_inv_inertia[link] = wp.inverse(I_m)
288
- if m == 0.0 and ensure_nonstatic_links:
289
- # set the mass to something nonzero to ensure the body is dynamic
290
- m = static_link_mass
291
- # cube with side length 0.5
292
- I_m = wp.mat33(np.eye(3)) * m / 12.0 * (0.5 * scale) ** 2 * 2.0
293
- I_m += wp.mat33(armature * np.eye(3))
294
- builder.body_mass[link] = m
295
- builder.body_inv_mass[link] = 1.0 / m
296
- builder.body_inertia[link] = I_m
297
- builder.body_inv_inertia[link] = wp.inverse(I_m)
298
-
299
- end_shape_count = len(builder.shape_geo_type)
300
-
301
- # find joints per body
302
- body_children = {name: [] for name in link_index.keys()}
303
- # mapping from parent, child link names to joint
304
- parent_child_joint = {}
305
-
306
- joints = []
307
- for joint in root.findall("joint"):
308
- parent = joint.find("parent").get("link")
309
- child = joint.find("child").get("link")
310
- body_children[parent].append(child)
311
- joint_data = {
312
- "name": joint.get("name"),
313
- "parent": parent,
314
- "child": child,
315
- "type": joint.get("type"),
316
- "origin": parse_transform(joint),
317
- "damping": damping,
318
- "friction": 0.0,
319
- "limit_lower": joint_limit_lower,
320
- "limit_upper": joint_limit_upper,
321
- }
322
- if joint.find("axis") is not None:
323
- joint_data["axis"] = joint.find("axis").get("xyz")
324
- joint_data["axis"] = np.array([float(x) for x in joint_data["axis"].split()])
325
- if joint.find("dynamics") is not None:
326
- dynamics = joint.find("dynamics")
327
- joint_data["damping"] = float(dynamics.get("damping") or str(damping))
328
- joint_data["friction"] = float(dynamics.get("friction") or "0")
329
- if joint.find("limit") is not None:
330
- limit = joint.find("limit")
331
- joint_data["limit_lower"] = float(limit.get("lower") or "-1e6")
332
- joint_data["limit_upper"] = float(limit.get("upper") or "1e6")
333
- if joint.find("mimic") is not None:
334
- mimic = joint.find("mimic")
335
- joint_data["mimic_joint"] = mimic.get("joint")
336
- joint_data["mimic_multiplier"] = float(mimic.get("multiplier") or "1")
337
- joint_data["mimic_offset"] = float(mimic.get("offset") or "0")
338
-
339
- parent_child_joint[(parent, child)] = joint_data
340
- joints.append(joint_data)
341
-
342
- # topological sorting of joints because the FK solver will resolve body transforms
343
- # in joint order and needs the parent link transform to be resolved before the child
344
- visited = {name: False for name in link_index.keys()}
345
- sorted_joints = []
346
-
347
- # depth-first search
348
- def dfs(joint):
349
- link = joint["child"]
350
- if visited[link]:
351
- return
352
- visited[link] = True
353
-
354
- for child in body_children[link]:
355
- if not visited[child]:
356
- dfs(parent_child_joint[(link, child)])
357
-
358
- sorted_joints.insert(0, joint)
359
-
360
- # start DFS from each unvisited joint
361
- for joint in joints:
362
- if not visited[joint["parent"]]:
363
- dfs(joint)
364
-
365
- # add base joint
366
- if len(sorted_joints) > 0:
367
- base_link_name = sorted_joints[0]["parent"]
368
- else:
369
- base_link_name = next(iter(link_index.keys()))
370
- root = link_index[base_link_name]
371
- if base_joint is not None:
372
- # in case of a given base joint, the position is applied first, the rotation only
373
- # after the base joint itself to not rotate its axis
374
- base_parent_xform = wp.transform(xform.p, wp.quat_identity())
375
- base_child_xform = wp.transform((0.0, 0.0, 0.0), wp.quat_inverse(xform.q))
376
- if isinstance(base_joint, str):
377
- axes = base_joint.lower().split(",")
378
- axes = [ax.strip() for ax in axes]
379
- linear_axes = [ax[-1] for ax in axes if ax[0] in {"l", "p"}]
380
- angular_axes = [ax[-1] for ax in axes if ax[0] in {"a", "r"}]
381
- axes = {
382
- "x": [1.0, 0.0, 0.0],
383
- "y": [0.0, 1.0, 0.0],
384
- "z": [0.0, 0.0, 1.0],
385
- }
386
- builder.add_joint_d6(
387
- linear_axes=[wp.sim.JointAxis(axes[a]) for a in linear_axes],
388
- angular_axes=[wp.sim.JointAxis(axes[a]) for a in angular_axes],
389
- parent_xform=base_parent_xform,
390
- child_xform=base_child_xform,
391
- parent=-1,
392
- child=root,
393
- name="base_joint",
394
- )
395
- elif isinstance(base_joint, dict):
396
- base_joint["parent"] = -1
397
- base_joint["child"] = root
398
- base_joint["parent_xform"] = base_parent_xform
399
- base_joint["child_xform"] = base_child_xform
400
- base_joint["name"] = "base_joint"
401
- builder.add_joint(**base_joint)
402
- else:
403
- raise ValueError(
404
- "base_joint must be a comma-separated string of joint axes or a dict with joint parameters"
405
- )
406
- elif floating:
407
- builder.add_joint_free(root, name="floating_base")
408
-
409
- # set dofs to transform
410
- start = builder.joint_q_start[root]
411
-
412
- builder.joint_q[start + 0] = xform.p[0]
413
- builder.joint_q[start + 1] = xform.p[1]
414
- builder.joint_q[start + 2] = xform.p[2]
415
-
416
- builder.joint_q[start + 3] = xform.q[0]
417
- builder.joint_q[start + 4] = xform.q[1]
418
- builder.joint_q[start + 5] = xform.q[2]
419
- builder.joint_q[start + 6] = xform.q[3]
420
- else:
421
- builder.add_joint_fixed(-1, root, parent_xform=xform, name="fixed_base")
422
-
423
- # add joints, in topological order starting from root body
424
- for joint in sorted_joints:
425
- parent = link_index[joint["parent"]]
426
- child = link_index[joint["child"]]
427
- if child == -1:
428
- # we skipped the insertion of the child body
429
- continue
430
-
431
- lower = joint["limit_lower"]
432
- upper = joint["limit_upper"]
433
- joint_damping = joint["damping"]
434
-
435
- parent_xform = joint["origin"]
436
- child_xform = wp.transform_identity()
437
-
438
- joint_mode = wp.sim.JOINT_MODE_FORCE
439
- if stiffness > 0.0:
440
- joint_mode = wp.sim.JOINT_MODE_TARGET_POSITION
441
-
442
- joint_params = dict(
443
- parent=parent,
444
- child=child,
445
- parent_xform=parent_xform,
446
- child_xform=child_xform,
447
- name=joint["name"],
448
- )
449
-
450
- if joint["type"] == "revolute" or joint["type"] == "continuous":
451
- builder.add_joint_revolute(
452
- axis=joint["axis"],
453
- target_ke=stiffness,
454
- target_kd=joint_damping,
455
- limit_lower=lower,
456
- limit_upper=upper,
457
- limit_ke=limit_ke,
458
- limit_kd=limit_kd,
459
- mode=joint_mode,
460
- **joint_params,
461
- )
462
- elif joint["type"] == "prismatic":
463
- builder.add_joint_prismatic(
464
- axis=joint["axis"],
465
- target_ke=stiffness,
466
- target_kd=joint_damping,
467
- limit_lower=lower * scale,
468
- limit_upper=upper * scale,
469
- limit_ke=limit_ke,
470
- limit_kd=limit_kd,
471
- mode=joint_mode,
472
- **joint_params,
473
- )
474
- elif joint["type"] == "fixed":
475
- builder.add_joint_fixed(**joint_params)
476
- elif joint["type"] == "floating":
477
- builder.add_joint_free(**joint_params)
478
- elif joint["type"] == "planar":
479
- # find plane vectors perpendicular to axis
480
- axis = np.array(joint["axis"])
481
- axis /= np.linalg.norm(axis)
482
-
483
- # create helper vector that is not parallel to the axis
484
- helper = np.array([1, 0, 0]) if np.allclose(axis, [0, 1, 0]) else np.array([0, 1, 0])
485
-
486
- u = np.cross(helper, axis)
487
- u /= np.linalg.norm(u)
488
-
489
- v = np.cross(axis, u)
490
- v /= np.linalg.norm(v)
491
-
492
- builder.add_joint_d6(
493
- linear_axes=[
494
- wp.sim.JointAxis(
495
- u, limit_lower=lower * scale, limit_upper=upper * scale, limit_ke=limit_ke, limit_kd=limit_kd
496
- ),
497
- wp.sim.JointAxis(
498
- v, limit_lower=lower * scale, limit_upper=upper * scale, limit_ke=limit_ke, limit_kd=limit_kd
499
- ),
500
- ],
501
- **joint_params,
502
- )
503
- else:
504
- raise Exception("Unsupported joint type: " + joint["type"])
505
-
506
- for i in range(start_shape_count, end_shape_count):
507
- for j in visual_shapes:
508
- builder.shape_collision_filter_pairs.add((i, j))
509
-
510
- if not enable_self_collisions:
511
- for i in range(start_shape_count, end_shape_count):
512
- for j in range(i + 1, end_shape_count):
513
- builder.shape_collision_filter_pairs.add((i, j))
514
-
515
- if collapse_fixed_joints:
516
- builder.collapse_fixed_joints()
1
+ # Copyright (c) 2022 NVIDIA CORPORATION. All rights reserved.
2
+ # NVIDIA CORPORATION and its licensors retain all intellectual property
3
+ # and proprietary rights in and to this software, related documentation
4
+ # and any modifications thereto. Any use, reproduction, disclosure or
5
+ # distribution of this software and related documentation without an express
6
+ # license agreement from NVIDIA CORPORATION is strictly prohibited.
7
+
8
+ import os
9
+ import xml.etree.ElementTree as ET
10
+ from typing import Union
11
+
12
+ import numpy as np
13
+
14
+ import warp as wp
15
+ from warp.sim.model import Mesh
16
+
17
+
18
+ def parse_urdf(
19
+ urdf_filename,
20
+ builder,
21
+ xform=None,
22
+ floating=False,
23
+ base_joint: Union[dict, str] = None,
24
+ density=1000.0,
25
+ stiffness=100.0,
26
+ damping=10.0,
27
+ armature=0.0,
28
+ contact_ke=1.0e4,
29
+ contact_kd=1.0e3,
30
+ contact_kf=1.0e2,
31
+ contact_ka=0.0,
32
+ contact_mu=0.25,
33
+ contact_restitution=0.5,
34
+ contact_thickness=0.0,
35
+ limit_ke=100.0,
36
+ limit_kd=10.0,
37
+ joint_limit_lower=-1e6,
38
+ joint_limit_upper=1e6,
39
+ scale=1.0,
40
+ parse_visuals_as_colliders=False,
41
+ force_show_colliders=False,
42
+ enable_self_collisions=True,
43
+ ignore_inertial_definitions=True,
44
+ ensure_nonstatic_links=True,
45
+ static_link_mass=1e-2,
46
+ collapse_fixed_joints=False,
47
+ ):
48
+ """
49
+ Parses a URDF file and adds the bodies and joints to the given ModelBuilder.
50
+
51
+ Args:
52
+ urdf_filename (str): The filename of the URDF file to parse.
53
+ builder (ModelBuilder): The :class:`ModelBuilder` to add the bodies and joints to.
54
+ xform (:ref:`transform <transform>`): The transform to apply to the root body.
55
+ floating (bool): If True, the root body is a free joint. If False, the root body is connected via a fixed joint to the world, unless a `base_joint` is defined.
56
+ base_joint (Union[str, dict]): The joint by which the root body is connected to the world. This can be either a string defining the joint axes of a D6 joint with comma-separated positional and angular axis names (e.g. "px,py,rz" for a D6 joint with linear axes in x, y and an angular axis in z) or a dict with joint parameters (see :meth:`ModelBuilder.add_joint`).
57
+ density (float): The density of the shapes in kg/m^3 which will be used to calculate the body mass and inertia.
58
+ stiffness (float): The stiffness of the joints.
59
+ damping (float): The damping of the joints.
60
+ armature (float): The armature of the joints (bias to add to the inertia diagonals that may stabilize the simulation).
61
+ contact_ke (float): The stiffness of the shape contacts (used by the Euler integrators).
62
+ contact_kd (float): The damping of the shape contacts (used by the Euler integrators).
63
+ contact_kf (float): The friction stiffness of the shape contacts (used by the Euler integrators).
64
+ contact_ka (float): The adhesion distance of the shape contacts (used by the Euler integrators).
65
+ contact_mu (float): The friction coefficient of the shape contacts.
66
+ contact_restitution (float): The restitution coefficient of the shape contacts.
67
+ contact_thickness (float): The thickness to add to the shape geometry.
68
+ limit_ke (float): The stiffness of the joint limits (used by the Euler integrators).
69
+ limit_kd (float): The damping of the joint limits (used by the Euler integrators).
70
+ joint_limit_lower (float): The default lower joint limit if not specified in the URDF.
71
+ joint_limit_upper (float): The default upper joint limit if not specified in the URDF.
72
+ scale (float): The scaling factor to apply to the imported mechanism.
73
+ parse_visuals_as_colliders (bool): If True, the geometry defined under the `<visual>` tags is used for collision handling instead of the `<collision>` geometries.
74
+ force_show_colliders (bool): If True, the collision shapes are always shown, even if there are visual shapes.
75
+ enable_self_collisions (bool): If True, self-collisions are enabled.
76
+ ignore_inertial_definitions (bool): If True, the inertial parameters defined in the URDF are ignored and the inertia is calculated from the shape geometry.
77
+ ensure_nonstatic_links (bool): If True, links with zero mass are given a small mass (see `static_link_mass`) to ensure they are dynamic.
78
+ static_link_mass (float): The mass to assign to links with zero mass (if `ensure_nonstatic_links` is set to True).
79
+ collapse_fixed_joints (bool): If True, fixed joints are removed and the respective bodies are merged.
80
+ """
81
+ if xform is None:
82
+ xform = wp.transform()
83
+
84
+ file = ET.parse(urdf_filename)
85
+ root = file.getroot()
86
+
87
+ contact_vars = {
88
+ "ke": contact_ke,
89
+ "kd": contact_kd,
90
+ "kf": contact_kf,
91
+ "ka": contact_ka,
92
+ "mu": contact_mu,
93
+ "restitution": contact_restitution,
94
+ "thickness": contact_thickness,
95
+ }
96
+
97
+ def parse_transform(element):
98
+ if element is None or element.find("origin") is None:
99
+ return wp.transform()
100
+ origin = element.find("origin")
101
+ xyz = origin.get("xyz") or "0 0 0"
102
+ rpy = origin.get("rpy") or "0 0 0"
103
+ xyz = [float(x) * scale for x in xyz.split()]
104
+ rpy = [float(x) for x in rpy.split()]
105
+ return wp.transform(xyz, wp.quat_rpy(*rpy))
106
+
107
+ def parse_shapes(link, geoms, density, incoming_xform=None, visible=True, just_visual=False):
108
+ shapes = []
109
+ # add geometry
110
+ for geom_group in geoms:
111
+ geo = geom_group.find("geometry")
112
+ if geo is None:
113
+ continue
114
+
115
+ tf = parse_transform(geom_group)
116
+ if incoming_xform is not None:
117
+ tf = incoming_xform * tf
118
+
119
+ for box in geo.findall("box"):
120
+ size = box.get("size") or "1 1 1"
121
+ size = [float(x) for x in size.split()]
122
+ s = builder.add_shape_box(
123
+ body=link,
124
+ pos=wp.vec3(tf.p),
125
+ rot=wp.quat(tf.q),
126
+ hx=size[0] * 0.5 * scale,
127
+ hy=size[1] * 0.5 * scale,
128
+ hz=size[2] * 0.5 * scale,
129
+ density=density,
130
+ is_visible=visible,
131
+ has_ground_collision=not just_visual,
132
+ has_shape_collision=not just_visual,
133
+ **contact_vars,
134
+ )
135
+ shapes.append(s)
136
+
137
+ for sphere in geo.findall("sphere"):
138
+ s = builder.add_shape_sphere(
139
+ body=link,
140
+ pos=wp.vec3(tf.p),
141
+ rot=wp.quat(tf.q),
142
+ radius=float(sphere.get("radius") or "1") * scale,
143
+ density=density,
144
+ is_visible=visible,
145
+ has_ground_collision=not just_visual,
146
+ has_shape_collision=not just_visual,
147
+ **contact_vars,
148
+ )
149
+ shapes.append(s)
150
+
151
+ for cylinder in geo.findall("cylinder"):
152
+ s = builder.add_shape_capsule(
153
+ body=link,
154
+ pos=wp.vec3(tf.p),
155
+ rot=wp.quat(tf.q),
156
+ radius=float(cylinder.get("radius") or "1") * scale,
157
+ half_height=float(cylinder.get("length") or "1") * 0.5 * scale,
158
+ density=density,
159
+ up_axis=2, # cylinders in URDF are aligned with z-axis
160
+ is_visible=visible,
161
+ has_ground_collision=not just_visual,
162
+ has_shape_collision=not just_visual,
163
+ **contact_vars,
164
+ )
165
+ shapes.append(s)
166
+
167
+ for mesh in geo.findall("mesh"):
168
+ filename = mesh.get("filename")
169
+ if filename is None:
170
+ continue
171
+ if filename.startswith("package://"):
172
+ fn = filename.replace("package://", "")
173
+ package_name = fn.split("/")[0]
174
+ urdf_folder = os.path.dirname(urdf_filename)
175
+ # resolve file path from package name, i.e. find
176
+ # the package folder from the URDF folder
177
+ if package_name in urdf_folder:
178
+ filename = os.path.join(urdf_folder[: urdf_folder.index(package_name)], fn)
179
+ else:
180
+ wp.utils.warn(
181
+ f'Warning: package "{package_name}" not found in URDF folder while loading mesh at "{filename}"'
182
+ )
183
+ elif filename.startswith("http://") or filename.startswith("https://"):
184
+ # download mesh
185
+ import shutil
186
+ import tempfile
187
+
188
+ import requests
189
+
190
+ with tempfile.TemporaryDirectory() as tmpdir:
191
+ # get filename extension
192
+ extension = os.path.splitext(filename)[1]
193
+ tmpfile = os.path.join(tmpdir, "mesh" + extension)
194
+ with requests.get(filename, stream=True) as r:
195
+ with open(tmpfile, "wb") as f:
196
+ shutil.copyfileobj(r.raw, f)
197
+ filename = tmpfile
198
+ else:
199
+ filename = os.path.join(os.path.dirname(urdf_filename), filename)
200
+ if not os.path.exists(filename):
201
+ wp.utils.warn(f"Warning: mesh file {filename} does not exist")
202
+ continue
203
+
204
+ import trimesh
205
+
206
+ # use force='mesh' to load the mesh as a trimesh object
207
+ # with baked in transforms, e.g. from COLLADA files
208
+ m = trimesh.load(filename, force="mesh")
209
+ scaling = mesh.get("scale") or "1 1 1"
210
+ scaling = np.array([float(x) * scale for x in scaling.split()])
211
+ if hasattr(m, "geometry"):
212
+ # multiple meshes are contained in a scene
213
+ for geom in m.geometry.values():
214
+ vertices = np.array(geom.vertices, dtype=np.float32) * scaling
215
+ faces = np.array(geom.faces.flatten(), dtype=np.int32)
216
+ mesh = Mesh(vertices, faces)
217
+ s = builder.add_shape_mesh(
218
+ body=link,
219
+ pos=wp.vec3(tf.p),
220
+ rot=wp.quat(tf.q),
221
+ mesh=mesh,
222
+ density=density,
223
+ is_visible=visible,
224
+ has_ground_collision=not just_visual,
225
+ has_shape_collision=not just_visual,
226
+ **contact_vars,
227
+ )
228
+ shapes.append(s)
229
+ else:
230
+ # a single mesh
231
+ vertices = np.array(m.vertices, dtype=np.float32) * scaling
232
+ faces = np.array(m.faces.flatten(), dtype=np.int32)
233
+ mesh = Mesh(vertices, faces)
234
+ s = builder.add_shape_mesh(
235
+ body=link,
236
+ pos=wp.vec3(tf.p),
237
+ rot=wp.quat(tf.q),
238
+ mesh=mesh,
239
+ density=density,
240
+ is_visible=visible,
241
+ has_ground_collision=not just_visual,
242
+ has_shape_collision=not just_visual,
243
+ **contact_vars,
244
+ )
245
+ shapes.append(s)
246
+
247
+ return shapes
248
+
249
+ # maps from link name -> link index
250
+ link_index = {}
251
+
252
+ visual_shapes = []
253
+
254
+ builder.add_articulation()
255
+
256
+ start_shape_count = len(builder.shape_geo_type)
257
+
258
+ # add links
259
+ for _i, urdf_link in enumerate(root.findall("link")):
260
+ name = urdf_link.get("name")
261
+ link = builder.add_body(origin=wp.transform_identity(), armature=armature, name=name)
262
+
263
+ # add ourselves to the index
264
+ link_index[name] = link
265
+
266
+ visuals = urdf_link.findall("visual")
267
+ colliders = urdf_link.findall("collision")
268
+
269
+ if parse_visuals_as_colliders:
270
+ colliders = visuals
271
+ else:
272
+ s = parse_shapes(link, visuals, density=0.0, just_visual=True)
273
+ visual_shapes.extend(s)
274
+
275
+ show_colliders = force_show_colliders
276
+ if parse_visuals_as_colliders:
277
+ show_colliders = True
278
+ elif len(visuals) == 0:
279
+ # we need to show the collision shapes since there are no visual shapes
280
+ show_colliders = True
281
+
282
+ parse_shapes(link, colliders, density=density, visible=show_colliders)
283
+ m = builder.body_mass[link]
284
+ if not ignore_inertial_definitions and urdf_link.find("inertial") is not None:
285
+ # overwrite inertial parameters if defined
286
+ inertial = urdf_link.find("inertial")
287
+ inertial_frame = parse_transform(inertial)
288
+ com = inertial_frame.p
289
+ I_m = np.zeros((3, 3))
290
+ I_m[0, 0] = float(inertial.find("inertia").get("ixx") or "0") * scale**2
291
+ I_m[1, 1] = float(inertial.find("inertia").get("iyy") or "0") * scale**2
292
+ I_m[2, 2] = float(inertial.find("inertia").get("izz") or "0") * scale**2
293
+ I_m[0, 1] = float(inertial.find("inertia").get("ixy") or "0") * scale**2
294
+ I_m[0, 2] = float(inertial.find("inertia").get("ixz") or "0") * scale**2
295
+ I_m[1, 2] = float(inertial.find("inertia").get("iyz") or "0") * scale**2
296
+ I_m[1, 0] = I_m[0, 1]
297
+ I_m[2, 0] = I_m[0, 2]
298
+ I_m[2, 1] = I_m[1, 2]
299
+ rot = wp.quat_to_matrix(inertial_frame.q)
300
+ I_m = rot @ wp.mat33(I_m)
301
+ m = float(inertial.find("mass").get("value") or "0")
302
+ builder.body_mass[link] = m
303
+ builder.body_inv_mass[link] = 1.0 / m
304
+ builder.body_com[link] = com
305
+ builder.body_inertia[link] = I_m
306
+ builder.body_inv_inertia[link] = wp.inverse(I_m)
307
+ if m == 0.0 and ensure_nonstatic_links:
308
+ # set the mass to something nonzero to ensure the body is dynamic
309
+ m = static_link_mass
310
+ # cube with side length 0.5
311
+ I_m = wp.mat33(np.eye(3)) * m / 12.0 * (0.5 * scale) ** 2 * 2.0
312
+ I_m += wp.mat33(armature * np.eye(3))
313
+ builder.body_mass[link] = m
314
+ builder.body_inv_mass[link] = 1.0 / m
315
+ builder.body_inertia[link] = I_m
316
+ builder.body_inv_inertia[link] = wp.inverse(I_m)
317
+
318
+ end_shape_count = len(builder.shape_geo_type)
319
+
320
+ # find joints per body
321
+ body_children = {name: [] for name in link_index.keys()}
322
+ # mapping from parent, child link names to joint
323
+ parent_child_joint = {}
324
+
325
+ joints = []
326
+ for joint in root.findall("joint"):
327
+ parent = joint.find("parent").get("link")
328
+ child = joint.find("child").get("link")
329
+ body_children[parent].append(child)
330
+ joint_data = {
331
+ "name": joint.get("name"),
332
+ "parent": parent,
333
+ "child": child,
334
+ "type": joint.get("type"),
335
+ "origin": parse_transform(joint),
336
+ "damping": damping,
337
+ "friction": 0.0,
338
+ "limit_lower": joint_limit_lower,
339
+ "limit_upper": joint_limit_upper,
340
+ }
341
+ if joint.find("axis") is not None:
342
+ joint_data["axis"] = joint.find("axis").get("xyz")
343
+ joint_data["axis"] = np.array([float(x) for x in joint_data["axis"].split()])
344
+ if joint.find("dynamics") is not None:
345
+ dynamics = joint.find("dynamics")
346
+ joint_data["damping"] = float(dynamics.get("damping") or str(damping))
347
+ joint_data["friction"] = float(dynamics.get("friction") or "0")
348
+ if joint.find("limit") is not None:
349
+ limit = joint.find("limit")
350
+ joint_data["limit_lower"] = float(limit.get("lower") or "-1e6")
351
+ joint_data["limit_upper"] = float(limit.get("upper") or "1e6")
352
+ if joint.find("mimic") is not None:
353
+ mimic = joint.find("mimic")
354
+ joint_data["mimic_joint"] = mimic.get("joint")
355
+ joint_data["mimic_multiplier"] = float(mimic.get("multiplier") or "1")
356
+ joint_data["mimic_offset"] = float(mimic.get("offset") or "0")
357
+
358
+ parent_child_joint[(parent, child)] = joint_data
359
+ joints.append(joint_data)
360
+
361
+ # topological sorting of joints because the FK solver will resolve body transforms
362
+ # in joint order and needs the parent link transform to be resolved before the child
363
+ visited = {name: False for name in link_index.keys()}
364
+ sorted_joints = []
365
+
366
+ # depth-first search
367
+ def dfs(joint):
368
+ link = joint["child"]
369
+ if visited[link]:
370
+ return
371
+ visited[link] = True
372
+
373
+ for child in body_children[link]:
374
+ if not visited[child]:
375
+ dfs(parent_child_joint[(link, child)])
376
+
377
+ sorted_joints.insert(0, joint)
378
+
379
+ # start DFS from each unvisited joint
380
+ for joint in joints:
381
+ if not visited[joint["parent"]]:
382
+ dfs(joint)
383
+
384
+ # add base joint
385
+ if len(sorted_joints) > 0:
386
+ base_link_name = sorted_joints[0]["parent"]
387
+ else:
388
+ base_link_name = next(iter(link_index.keys()))
389
+ root = link_index[base_link_name]
390
+ if base_joint is not None:
391
+ # in case of a given base joint, the position is applied first, the rotation only
392
+ # after the base joint itself to not rotate its axis
393
+ base_parent_xform = wp.transform(xform.p, wp.quat_identity())
394
+ base_child_xform = wp.transform((0.0, 0.0, 0.0), wp.quat_inverse(xform.q))
395
+ if isinstance(base_joint, str):
396
+ axes = base_joint.lower().split(",")
397
+ axes = [ax.strip() for ax in axes]
398
+ linear_axes = [ax[-1] for ax in axes if ax[0] in {"l", "p"}]
399
+ angular_axes = [ax[-1] for ax in axes if ax[0] in {"a", "r"}]
400
+ axes = {
401
+ "x": [1.0, 0.0, 0.0],
402
+ "y": [0.0, 1.0, 0.0],
403
+ "z": [0.0, 0.0, 1.0],
404
+ }
405
+ builder.add_joint_d6(
406
+ linear_axes=[wp.sim.JointAxis(axes[a]) for a in linear_axes],
407
+ angular_axes=[wp.sim.JointAxis(axes[a]) for a in angular_axes],
408
+ parent_xform=base_parent_xform,
409
+ child_xform=base_child_xform,
410
+ parent=-1,
411
+ child=root,
412
+ name="base_joint",
413
+ )
414
+ elif isinstance(base_joint, dict):
415
+ base_joint["parent"] = -1
416
+ base_joint["child"] = root
417
+ base_joint["parent_xform"] = base_parent_xform
418
+ base_joint["child_xform"] = base_child_xform
419
+ base_joint["name"] = "base_joint"
420
+ builder.add_joint(**base_joint)
421
+ else:
422
+ raise ValueError(
423
+ "base_joint must be a comma-separated string of joint axes or a dict with joint parameters"
424
+ )
425
+ elif floating:
426
+ builder.add_joint_free(root, name="floating_base")
427
+
428
+ # set dofs to transform
429
+ start = builder.joint_q_start[root]
430
+
431
+ builder.joint_q[start + 0] = xform.p[0]
432
+ builder.joint_q[start + 1] = xform.p[1]
433
+ builder.joint_q[start + 2] = xform.p[2]
434
+
435
+ builder.joint_q[start + 3] = xform.q[0]
436
+ builder.joint_q[start + 4] = xform.q[1]
437
+ builder.joint_q[start + 5] = xform.q[2]
438
+ builder.joint_q[start + 6] = xform.q[3]
439
+ else:
440
+ builder.add_joint_fixed(-1, root, parent_xform=xform, name="fixed_base")
441
+
442
+ # add joints, in topological order starting from root body
443
+ for joint in sorted_joints:
444
+ parent = link_index[joint["parent"]]
445
+ child = link_index[joint["child"]]
446
+ if child == -1:
447
+ # we skipped the insertion of the child body
448
+ continue
449
+
450
+ lower = joint["limit_lower"]
451
+ upper = joint["limit_upper"]
452
+ joint_damping = joint["damping"]
453
+
454
+ parent_xform = joint["origin"]
455
+ child_xform = wp.transform_identity()
456
+
457
+ joint_mode = wp.sim.JOINT_MODE_FORCE
458
+ if stiffness > 0.0:
459
+ joint_mode = wp.sim.JOINT_MODE_TARGET_POSITION
460
+
461
+ joint_params = {
462
+ "parent": parent,
463
+ "child": child,
464
+ "parent_xform": parent_xform,
465
+ "child_xform": child_xform,
466
+ "name": joint["name"],
467
+ "armature": armature,
468
+ }
469
+
470
+ if joint["type"] == "revolute" or joint["type"] == "continuous":
471
+ builder.add_joint_revolute(
472
+ axis=joint["axis"],
473
+ target_ke=stiffness,
474
+ target_kd=joint_damping,
475
+ limit_lower=lower,
476
+ limit_upper=upper,
477
+ limit_ke=limit_ke,
478
+ limit_kd=limit_kd,
479
+ mode=joint_mode,
480
+ **joint_params,
481
+ )
482
+ elif joint["type"] == "prismatic":
483
+ builder.add_joint_prismatic(
484
+ axis=joint["axis"],
485
+ target_ke=stiffness,
486
+ target_kd=joint_damping,
487
+ limit_lower=lower * scale,
488
+ limit_upper=upper * scale,
489
+ limit_ke=limit_ke,
490
+ limit_kd=limit_kd,
491
+ mode=joint_mode,
492
+ **joint_params,
493
+ )
494
+ elif joint["type"] == "fixed":
495
+ builder.add_joint_fixed(**joint_params)
496
+ elif joint["type"] == "floating":
497
+ builder.add_joint_free(**joint_params)
498
+ elif joint["type"] == "planar":
499
+ # find plane vectors perpendicular to axis
500
+ axis = np.array(joint["axis"])
501
+ axis /= np.linalg.norm(axis)
502
+
503
+ # create helper vector that is not parallel to the axis
504
+ helper = np.array([1, 0, 0]) if np.allclose(axis, [0, 1, 0]) else np.array([0, 1, 0])
505
+
506
+ u = np.cross(helper, axis)
507
+ u /= np.linalg.norm(u)
508
+
509
+ v = np.cross(axis, u)
510
+ v /= np.linalg.norm(v)
511
+
512
+ builder.add_joint_d6(
513
+ linear_axes=[
514
+ wp.sim.JointAxis(
515
+ u, limit_lower=lower * scale, limit_upper=upper * scale, limit_ke=limit_ke, limit_kd=limit_kd
516
+ ),
517
+ wp.sim.JointAxis(
518
+ v, limit_lower=lower * scale, limit_upper=upper * scale, limit_ke=limit_ke, limit_kd=limit_kd
519
+ ),
520
+ ],
521
+ **joint_params,
522
+ )
523
+ else:
524
+ raise Exception("Unsupported joint type: " + joint["type"])
525
+
526
+ for i in range(start_shape_count, end_shape_count):
527
+ for j in visual_shapes:
528
+ builder.shape_collision_filter_pairs.add((i, j))
529
+
530
+ if not enable_self_collisions:
531
+ for i in range(start_shape_count, end_shape_count):
532
+ for j in range(i + 1, end_shape_count):
533
+ builder.shape_collision_filter_pairs.add((i, j))
534
+
535
+ if collapse_fixed_joints:
536
+ builder.collapse_fixed_joints()