warp-lang 1.9.0__py3-none-manylinux_2_34_aarch64.whl → 1.10.0__py3-none-manylinux_2_34_aarch64.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 (350) hide show
  1. warp/__init__.py +301 -287
  2. warp/__init__.pyi +2302 -307
  3. warp/_src/__init__.py +14 -0
  4. warp/_src/autograd.py +1077 -0
  5. warp/_src/build.py +620 -0
  6. warp/_src/build_dll.py +642 -0
  7. warp/{builtins.py → _src/builtins.py} +1546 -224
  8. warp/_src/codegen.py +4361 -0
  9. warp/{config.py → _src/config.py} +178 -169
  10. warp/_src/constants.py +59 -0
  11. warp/_src/context.py +8352 -0
  12. warp/_src/dlpack.py +464 -0
  13. warp/_src/fabric.py +362 -0
  14. warp/_src/fem/__init__.py +14 -0
  15. warp/_src/fem/adaptivity.py +510 -0
  16. warp/_src/fem/cache.py +689 -0
  17. warp/_src/fem/dirichlet.py +190 -0
  18. warp/{fem → _src/fem}/domain.py +42 -30
  19. warp/_src/fem/field/__init__.py +131 -0
  20. warp/_src/fem/field/field.py +703 -0
  21. warp/{fem → _src/fem}/field/nodal_field.py +32 -15
  22. warp/{fem → _src/fem}/field/restriction.py +3 -1
  23. warp/{fem → _src/fem}/field/virtual.py +55 -27
  24. warp/_src/fem/geometry/__init__.py +32 -0
  25. warp/{fem → _src/fem}/geometry/adaptive_nanogrid.py +79 -163
  26. warp/_src/fem/geometry/closest_point.py +99 -0
  27. warp/{fem → _src/fem}/geometry/deformed_geometry.py +16 -22
  28. warp/{fem → _src/fem}/geometry/element.py +34 -10
  29. warp/{fem → _src/fem}/geometry/geometry.py +50 -20
  30. warp/{fem → _src/fem}/geometry/grid_2d.py +14 -23
  31. warp/{fem → _src/fem}/geometry/grid_3d.py +14 -23
  32. warp/{fem → _src/fem}/geometry/hexmesh.py +42 -63
  33. warp/{fem → _src/fem}/geometry/nanogrid.py +256 -247
  34. warp/{fem → _src/fem}/geometry/partition.py +123 -63
  35. warp/{fem → _src/fem}/geometry/quadmesh.py +28 -45
  36. warp/{fem → _src/fem}/geometry/tetmesh.py +42 -63
  37. warp/{fem → _src/fem}/geometry/trimesh.py +28 -45
  38. warp/{fem → _src/fem}/integrate.py +166 -158
  39. warp/_src/fem/linalg.py +385 -0
  40. warp/_src/fem/operator.py +398 -0
  41. warp/_src/fem/polynomial.py +231 -0
  42. warp/{fem → _src/fem}/quadrature/pic_quadrature.py +17 -20
  43. warp/{fem → _src/fem}/quadrature/quadrature.py +97 -47
  44. warp/_src/fem/space/__init__.py +248 -0
  45. warp/{fem → _src/fem}/space/basis_function_space.py +22 -11
  46. warp/_src/fem/space/basis_space.py +681 -0
  47. warp/{fem → _src/fem}/space/dof_mapper.py +5 -3
  48. warp/{fem → _src/fem}/space/function_space.py +16 -13
  49. warp/{fem → _src/fem}/space/grid_2d_function_space.py +6 -7
  50. warp/{fem → _src/fem}/space/grid_3d_function_space.py +6 -4
  51. warp/{fem → _src/fem}/space/hexmesh_function_space.py +6 -10
  52. warp/{fem → _src/fem}/space/nanogrid_function_space.py +5 -9
  53. warp/{fem → _src/fem}/space/partition.py +119 -60
  54. warp/{fem → _src/fem}/space/quadmesh_function_space.py +6 -10
  55. warp/{fem → _src/fem}/space/restriction.py +68 -33
  56. warp/_src/fem/space/shape/__init__.py +152 -0
  57. warp/{fem → _src/fem}/space/shape/cube_shape_function.py +11 -9
  58. warp/{fem → _src/fem}/space/shape/shape_function.py +10 -9
  59. warp/{fem → _src/fem}/space/shape/square_shape_function.py +8 -6
  60. warp/{fem → _src/fem}/space/shape/tet_shape_function.py +5 -3
  61. warp/{fem → _src/fem}/space/shape/triangle_shape_function.py +5 -3
  62. warp/{fem → _src/fem}/space/tetmesh_function_space.py +5 -9
  63. warp/_src/fem/space/topology.py +461 -0
  64. warp/{fem → _src/fem}/space/trimesh_function_space.py +5 -9
  65. warp/_src/fem/types.py +114 -0
  66. warp/_src/fem/utils.py +488 -0
  67. warp/_src/jax.py +188 -0
  68. warp/_src/jax_experimental/__init__.py +14 -0
  69. warp/_src/jax_experimental/custom_call.py +389 -0
  70. warp/_src/jax_experimental/ffi.py +1286 -0
  71. warp/_src/jax_experimental/xla_ffi.py +658 -0
  72. warp/_src/marching_cubes.py +710 -0
  73. warp/_src/math.py +416 -0
  74. warp/_src/optim/__init__.py +14 -0
  75. warp/_src/optim/adam.py +165 -0
  76. warp/_src/optim/linear.py +1608 -0
  77. warp/_src/optim/sgd.py +114 -0
  78. warp/_src/paddle.py +408 -0
  79. warp/_src/render/__init__.py +14 -0
  80. warp/_src/render/imgui_manager.py +291 -0
  81. warp/_src/render/render_opengl.py +3638 -0
  82. warp/_src/render/render_usd.py +939 -0
  83. warp/_src/render/utils.py +162 -0
  84. warp/_src/sparse.py +2718 -0
  85. warp/_src/tape.py +1208 -0
  86. warp/{thirdparty → _src/thirdparty}/unittest_parallel.py +9 -2
  87. warp/_src/torch.py +393 -0
  88. warp/_src/types.py +5888 -0
  89. warp/_src/utils.py +1695 -0
  90. warp/autograd.py +12 -1054
  91. warp/bin/warp-clang.so +0 -0
  92. warp/bin/warp.so +0 -0
  93. warp/build.py +8 -588
  94. warp/build_dll.py +6 -471
  95. warp/codegen.py +6 -4246
  96. warp/constants.py +6 -39
  97. warp/context.py +12 -7851
  98. warp/dlpack.py +6 -444
  99. warp/examples/distributed/example_jacobi_mpi.py +4 -5
  100. warp/examples/fem/example_adaptive_grid.py +1 -1
  101. warp/examples/fem/example_apic_fluid.py +1 -1
  102. warp/examples/fem/example_burgers.py +8 -8
  103. warp/examples/fem/example_diffusion.py +1 -1
  104. warp/examples/fem/example_distortion_energy.py +1 -1
  105. warp/examples/fem/example_mixed_elasticity.py +2 -2
  106. warp/examples/fem/example_navier_stokes.py +1 -1
  107. warp/examples/fem/example_nonconforming_contact.py +7 -7
  108. warp/examples/fem/example_stokes.py +1 -1
  109. warp/examples/fem/example_stokes_transfer.py +1 -1
  110. warp/examples/fem/utils.py +2 -2
  111. warp/examples/interop/example_jax_callable.py +1 -1
  112. warp/examples/interop/example_jax_ffi_callback.py +1 -1
  113. warp/examples/interop/example_jax_kernel.py +3 -2
  114. warp/examples/tile/example_tile_mcgp.py +191 -0
  115. warp/fabric.py +6 -337
  116. warp/fem/__init__.py +159 -97
  117. warp/fem/adaptivity.py +7 -489
  118. warp/fem/cache.py +9 -648
  119. warp/fem/dirichlet.py +6 -184
  120. warp/fem/field/__init__.py +8 -109
  121. warp/fem/field/field.py +7 -652
  122. warp/fem/geometry/__init__.py +7 -18
  123. warp/fem/geometry/closest_point.py +11 -77
  124. warp/fem/linalg.py +18 -366
  125. warp/fem/operator.py +11 -369
  126. warp/fem/polynomial.py +9 -209
  127. warp/fem/space/__init__.py +5 -211
  128. warp/fem/space/basis_space.py +6 -662
  129. warp/fem/space/shape/__init__.py +41 -118
  130. warp/fem/space/topology.py +6 -437
  131. warp/fem/types.py +6 -81
  132. warp/fem/utils.py +11 -444
  133. warp/jax.py +8 -165
  134. warp/jax_experimental/__init__.py +14 -1
  135. warp/jax_experimental/custom_call.py +8 -342
  136. warp/jax_experimental/ffi.py +17 -853
  137. warp/jax_experimental/xla_ffi.py +5 -596
  138. warp/marching_cubes.py +5 -689
  139. warp/math.py +16 -393
  140. warp/native/array.h +385 -37
  141. warp/native/builtin.h +316 -39
  142. warp/native/bvh.cpp +43 -9
  143. warp/native/bvh.cu +62 -27
  144. warp/native/bvh.h +310 -309
  145. warp/native/clang/clang.cpp +102 -97
  146. warp/native/coloring.cpp +0 -1
  147. warp/native/crt.h +208 -0
  148. warp/native/exports.h +156 -0
  149. warp/native/hashgrid.cu +2 -0
  150. warp/native/intersect.h +24 -1
  151. warp/native/intersect_tri.h +44 -35
  152. warp/native/mat.h +1456 -276
  153. warp/native/mesh.cpp +4 -4
  154. warp/native/mesh.cu +4 -2
  155. warp/native/mesh.h +176 -61
  156. warp/native/quat.h +0 -52
  157. warp/native/scan.cu +2 -0
  158. warp/native/sort.cu +22 -13
  159. warp/native/sort.h +2 -0
  160. warp/native/sparse.cu +7 -3
  161. warp/native/spatial.h +12 -0
  162. warp/native/tile.h +837 -70
  163. warp/native/tile_radix_sort.h +3 -3
  164. warp/native/tile_reduce.h +394 -46
  165. warp/native/tile_scan.h +4 -4
  166. warp/native/vec.h +469 -53
  167. warp/native/version.h +23 -0
  168. warp/native/volume.cpp +1 -1
  169. warp/native/volume.cu +1 -0
  170. warp/native/volume.h +1 -1
  171. warp/native/volume_builder.cu +2 -0
  172. warp/native/warp.cpp +60 -32
  173. warp/native/warp.cu +581 -280
  174. warp/native/warp.h +14 -11
  175. warp/optim/__init__.py +6 -3
  176. warp/optim/adam.py +6 -145
  177. warp/optim/linear.py +14 -1585
  178. warp/optim/sgd.py +6 -94
  179. warp/paddle.py +6 -388
  180. warp/render/__init__.py +8 -4
  181. warp/render/imgui_manager.py +7 -267
  182. warp/render/render_opengl.py +6 -3616
  183. warp/render/render_usd.py +6 -918
  184. warp/render/utils.py +6 -142
  185. warp/sparse.py +37 -2563
  186. warp/tape.py +6 -1188
  187. warp/tests/__main__.py +1 -1
  188. warp/tests/cuda/test_async.py +4 -4
  189. warp/tests/cuda/test_conditional_captures.py +1 -1
  190. warp/tests/cuda/test_multigpu.py +1 -1
  191. warp/tests/cuda/test_streams.py +58 -1
  192. warp/tests/geometry/test_bvh.py +157 -22
  193. warp/tests/geometry/test_hash_grid.py +38 -0
  194. warp/tests/geometry/test_marching_cubes.py +0 -1
  195. warp/tests/geometry/test_mesh.py +5 -3
  196. warp/tests/geometry/test_mesh_query_aabb.py +5 -12
  197. warp/tests/geometry/test_mesh_query_point.py +5 -2
  198. warp/tests/geometry/test_mesh_query_ray.py +15 -3
  199. warp/tests/geometry/test_volume_write.py +5 -5
  200. warp/tests/interop/test_dlpack.py +18 -17
  201. warp/tests/interop/test_jax.py +1382 -79
  202. warp/tests/interop/test_paddle.py +1 -1
  203. warp/tests/test_adam.py +0 -1
  204. warp/tests/test_arithmetic.py +9 -9
  205. warp/tests/test_array.py +580 -100
  206. warp/tests/test_array_reduce.py +3 -3
  207. warp/tests/test_atomic.py +12 -8
  208. warp/tests/test_atomic_bitwise.py +209 -0
  209. warp/tests/test_atomic_cas.py +4 -4
  210. warp/tests/test_bool.py +2 -2
  211. warp/tests/test_builtins_resolution.py +5 -571
  212. warp/tests/test_codegen.py +34 -15
  213. warp/tests/test_conditional.py +1 -1
  214. warp/tests/test_context.py +6 -6
  215. warp/tests/test_copy.py +242 -161
  216. warp/tests/test_ctypes.py +3 -3
  217. warp/tests/test_devices.py +24 -2
  218. warp/tests/test_examples.py +16 -84
  219. warp/tests/test_fabricarray.py +35 -35
  220. warp/tests/test_fast_math.py +0 -2
  221. warp/tests/test_fem.py +60 -14
  222. warp/tests/test_fixedarray.py +3 -3
  223. warp/tests/test_func.py +8 -5
  224. warp/tests/test_generics.py +1 -1
  225. warp/tests/test_indexedarray.py +24 -24
  226. warp/tests/test_intersect.py +39 -9
  227. warp/tests/test_large.py +1 -1
  228. warp/tests/test_lerp.py +3 -1
  229. warp/tests/test_linear_solvers.py +1 -1
  230. warp/tests/test_map.py +49 -4
  231. warp/tests/test_mat.py +52 -62
  232. warp/tests/test_mat_constructors.py +4 -5
  233. warp/tests/test_mat_lite.py +1 -1
  234. warp/tests/test_mat_scalar_ops.py +121 -121
  235. warp/tests/test_math.py +34 -0
  236. warp/tests/test_module_aot.py +4 -4
  237. warp/tests/test_modules_lite.py +28 -2
  238. warp/tests/test_print.py +11 -11
  239. warp/tests/test_quat.py +93 -58
  240. warp/tests/test_runlength_encode.py +1 -1
  241. warp/tests/test_scalar_ops.py +38 -10
  242. warp/tests/test_smoothstep.py +1 -1
  243. warp/tests/test_sparse.py +126 -15
  244. warp/tests/test_spatial.py +105 -87
  245. warp/tests/test_special_values.py +6 -6
  246. warp/tests/test_static.py +7 -7
  247. warp/tests/test_struct.py +13 -2
  248. warp/tests/test_triangle_closest_point.py +48 -1
  249. warp/tests/test_tuple.py +96 -0
  250. warp/tests/test_types.py +82 -9
  251. warp/tests/test_utils.py +52 -52
  252. warp/tests/test_vec.py +29 -29
  253. warp/tests/test_vec_constructors.py +5 -5
  254. warp/tests/test_vec_scalar_ops.py +97 -97
  255. warp/tests/test_version.py +75 -0
  256. warp/tests/tile/test_tile.py +239 -0
  257. warp/tests/tile/test_tile_atomic_bitwise.py +403 -0
  258. warp/tests/tile/test_tile_cholesky.py +7 -4
  259. warp/tests/tile/test_tile_load.py +26 -2
  260. warp/tests/tile/test_tile_mathdx.py +3 -3
  261. warp/tests/tile/test_tile_matmul.py +1 -1
  262. warp/tests/tile/test_tile_mlp.py +2 -4
  263. warp/tests/tile/test_tile_reduce.py +214 -13
  264. warp/tests/unittest_suites.py +6 -14
  265. warp/tests/unittest_utils.py +10 -9
  266. warp/tests/walkthrough_debug.py +3 -1
  267. warp/torch.py +6 -373
  268. warp/types.py +29 -5750
  269. warp/utils.py +10 -1659
  270. {warp_lang-1.9.0.dist-info → warp_lang-1.10.0.dist-info}/METADATA +47 -103
  271. warp_lang-1.10.0.dist-info/RECORD +468 -0
  272. warp_lang-1.10.0.dist-info/licenses/licenses/Gaia-LICENSE.txt +6 -0
  273. warp_lang-1.10.0.dist-info/licenses/licenses/appdirs-LICENSE.txt +22 -0
  274. warp_lang-1.10.0.dist-info/licenses/licenses/asset_pixel_jpg-LICENSE.txt +3 -0
  275. warp_lang-1.10.0.dist-info/licenses/licenses/cuda-LICENSE.txt +1582 -0
  276. warp_lang-1.10.0.dist-info/licenses/licenses/dlpack-LICENSE.txt +201 -0
  277. warp_lang-1.10.0.dist-info/licenses/licenses/fp16-LICENSE.txt +28 -0
  278. warp_lang-1.10.0.dist-info/licenses/licenses/libmathdx-LICENSE.txt +220 -0
  279. warp_lang-1.10.0.dist-info/licenses/licenses/llvm-LICENSE.txt +279 -0
  280. warp_lang-1.10.0.dist-info/licenses/licenses/moller-LICENSE.txt +16 -0
  281. warp_lang-1.10.0.dist-info/licenses/licenses/nanovdb-LICENSE.txt +2 -0
  282. warp_lang-1.10.0.dist-info/licenses/licenses/nvrtc-LICENSE.txt +1592 -0
  283. warp_lang-1.10.0.dist-info/licenses/licenses/svd-LICENSE.txt +23 -0
  284. warp_lang-1.10.0.dist-info/licenses/licenses/unittest_parallel-LICENSE.txt +21 -0
  285. warp_lang-1.10.0.dist-info/licenses/licenses/usd-LICENSE.txt +213 -0
  286. warp_lang-1.10.0.dist-info/licenses/licenses/windingnumber-LICENSE.txt +21 -0
  287. warp/examples/assets/cartpole.urdf +0 -110
  288. warp/examples/assets/crazyflie.usd +0 -0
  289. warp/examples/assets/nv_ant.xml +0 -92
  290. warp/examples/assets/nv_humanoid.xml +0 -183
  291. warp/examples/assets/quadruped.urdf +0 -268
  292. warp/examples/optim/example_bounce.py +0 -266
  293. warp/examples/optim/example_cloth_throw.py +0 -228
  294. warp/examples/optim/example_drone.py +0 -870
  295. warp/examples/optim/example_inverse_kinematics.py +0 -182
  296. warp/examples/optim/example_inverse_kinematics_torch.py +0 -191
  297. warp/examples/optim/example_softbody_properties.py +0 -400
  298. warp/examples/optim/example_spring_cage.py +0 -245
  299. warp/examples/optim/example_trajectory.py +0 -227
  300. warp/examples/sim/example_cartpole.py +0 -143
  301. warp/examples/sim/example_cloth.py +0 -225
  302. warp/examples/sim/example_cloth_self_contact.py +0 -316
  303. warp/examples/sim/example_granular.py +0 -130
  304. warp/examples/sim/example_granular_collision_sdf.py +0 -202
  305. warp/examples/sim/example_jacobian_ik.py +0 -244
  306. warp/examples/sim/example_particle_chain.py +0 -124
  307. warp/examples/sim/example_quadruped.py +0 -203
  308. warp/examples/sim/example_rigid_chain.py +0 -203
  309. warp/examples/sim/example_rigid_contact.py +0 -195
  310. warp/examples/sim/example_rigid_force.py +0 -133
  311. warp/examples/sim/example_rigid_gyroscopic.py +0 -115
  312. warp/examples/sim/example_rigid_soft_contact.py +0 -140
  313. warp/examples/sim/example_soft_body.py +0 -196
  314. warp/examples/tile/example_tile_walker.py +0 -327
  315. warp/sim/__init__.py +0 -74
  316. warp/sim/articulation.py +0 -793
  317. warp/sim/collide.py +0 -2570
  318. warp/sim/graph_coloring.py +0 -307
  319. warp/sim/import_mjcf.py +0 -791
  320. warp/sim/import_snu.py +0 -227
  321. warp/sim/import_urdf.py +0 -579
  322. warp/sim/import_usd.py +0 -898
  323. warp/sim/inertia.py +0 -357
  324. warp/sim/integrator.py +0 -245
  325. warp/sim/integrator_euler.py +0 -2000
  326. warp/sim/integrator_featherstone.py +0 -2101
  327. warp/sim/integrator_vbd.py +0 -2487
  328. warp/sim/integrator_xpbd.py +0 -3295
  329. warp/sim/model.py +0 -4821
  330. warp/sim/particles.py +0 -121
  331. warp/sim/render.py +0 -431
  332. warp/sim/utils.py +0 -431
  333. warp/tests/sim/disabled_kinematics.py +0 -244
  334. warp/tests/sim/test_cloth.py +0 -863
  335. warp/tests/sim/test_collision.py +0 -743
  336. warp/tests/sim/test_coloring.py +0 -347
  337. warp/tests/sim/test_inertia.py +0 -161
  338. warp/tests/sim/test_model.py +0 -226
  339. warp/tests/sim/test_sim_grad.py +0 -287
  340. warp/tests/sim/test_sim_grad_bounce_linear.py +0 -212
  341. warp/tests/sim/test_sim_kinematics.py +0 -98
  342. warp/thirdparty/__init__.py +0 -0
  343. warp_lang-1.9.0.dist-info/RECORD +0 -456
  344. /warp/{fem → _src/fem}/quadrature/__init__.py +0 -0
  345. /warp/{tests/sim → _src/thirdparty}/__init__.py +0 -0
  346. /warp/{thirdparty → _src/thirdparty}/appdirs.py +0 -0
  347. /warp/{thirdparty → _src/thirdparty}/dlpack.py +0 -0
  348. {warp_lang-1.9.0.dist-info → warp_lang-1.10.0.dist-info}/WHEEL +0 -0
  349. {warp_lang-1.9.0.dist-info → warp_lang-1.10.0.dist-info}/licenses/LICENSE.md +0 -0
  350. {warp_lang-1.9.0.dist-info → warp_lang-1.10.0.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,939 @@
1
+ # SPDX-FileCopyrightText: Copyright (c) 2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2
+ # SPDX-License-Identifier: Apache-2.0
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ from __future__ import annotations
17
+
18
+ import numpy as np
19
+
20
+ import warp as wp
21
+
22
+ _wp_module_name_ = "warp.render.render_usd"
23
+
24
+ UP_AXIS_TOKEN = ("X", "Y", "Z")
25
+ UP_AXIS_VEC = (
26
+ np.array((1.0, 0.0, 0.0), dtype=float),
27
+ np.array((0.0, 1.0, 0.0), dtype=float),
28
+ np.array((0.0, 0.0, 1.0), dtype=float),
29
+ )
30
+
31
+
32
+ def _usd_add_xform(prim):
33
+ from pxr import UsdGeom
34
+
35
+ prim = UsdGeom.Xform(prim)
36
+ prim.ClearXformOpOrder()
37
+
38
+ prim.AddTranslateOp()
39
+ prim.AddOrientOp()
40
+ prim.AddScaleOp()
41
+
42
+
43
+ def _usd_set_xform(
44
+ xform,
45
+ pos: tuple | None = None,
46
+ rot: tuple | None = None,
47
+ scale: tuple | None = None,
48
+ time: float = 0.0,
49
+ ):
50
+ from pxr import Gf, UsdGeom
51
+
52
+ xform = UsdGeom.Xform(xform)
53
+
54
+ xform_ops = xform.GetOrderedXformOps()
55
+
56
+ if pos is not None:
57
+ xform_ops[0].Set(Gf.Vec3d(float(pos[0]), float(pos[1]), float(pos[2])), time)
58
+ if rot is not None:
59
+ xform_ops[1].Set(Gf.Quatf(float(rot[3]), float(rot[0]), float(rot[1]), float(rot[2])), time)
60
+ if scale is not None:
61
+ xform_ops[2].Set(Gf.Vec3d(float(scale[0]), float(scale[1]), float(scale[2])), time)
62
+
63
+
64
+ # transforms a cylinder such that it connects the two points pos0, pos1
65
+ def _compute_segment_xform(pos0, pos1):
66
+ from pxr import Gf
67
+
68
+ mid = (pos0 + pos1) * 0.5
69
+ height = (pos1 - pos0).GetLength()
70
+
71
+ dir = (pos1 - pos0) / height
72
+
73
+ rot = Gf.Rotation()
74
+ rot.SetRotateInto((0.0, 0.0, 1.0), Gf.Vec3d(dir))
75
+
76
+ scale = Gf.Vec3f(1.0, 1.0, height)
77
+
78
+ return (mid, Gf.Quath(rot.GetQuat()), scale)
79
+
80
+
81
+ class UsdRenderer:
82
+ """A USD renderer"""
83
+
84
+ def __init__(self, stage, up_axis="Y", fps=60, scaling=1.0):
85
+ """Construct a UsdRenderer object.
86
+
87
+ Args:
88
+ model: A simulation model
89
+ stage (str/Usd.Stage): A USD stage (either in memory or on disk)
90
+ up_axis (str): The upfacing axis of the stage
91
+ fps: The number of frames per second to use in the USD file
92
+ scaling: Scaling factor to use for the entities in the scene
93
+ """
94
+ try:
95
+ from pxr import Gf, Sdf, Usd, UsdGeom, UsdLux
96
+ except ImportError as e:
97
+ raise ImportError("Failed to import pxr. Please install USD (e.g. via `pip install usd-core`).") from e
98
+
99
+ if isinstance(stage, str):
100
+ self.stage = stage = Usd.Stage.CreateNew(stage)
101
+ elif isinstance(stage, Usd.Stage):
102
+ self.stage = stage
103
+ else:
104
+ print("Failed to create stage in renderer. Please construct with stage path or stage object.")
105
+ self.up_axis = up_axis.upper()
106
+ self.fps = float(fps)
107
+ self.time = 0.0
108
+
109
+ self.draw_points = True
110
+ self.draw_springs = False
111
+ self.draw_triangles = False
112
+
113
+ self.root = UsdGeom.Xform.Define(stage, "/root")
114
+
115
+ # mapping from shape ID to UsdGeom class
116
+ self._shape_constructors = {}
117
+ # optional scaling applied to shape instances (e.g. cubes)
118
+ self._shape_custom_scale = {}
119
+
120
+ # apply scaling
121
+ self.root.ClearXformOpOrder()
122
+ s = self.root.AddScaleOp()
123
+ s.Set(Gf.Vec3d(float(scaling), float(scaling), float(scaling)), 0.0)
124
+
125
+ self.stage.SetDefaultPrim(self.root.GetPrim())
126
+ self.stage.SetStartTimeCode(0.0)
127
+ self.stage.SetEndTimeCode(0.0)
128
+ self.stage.SetFramesPerSecond(self.fps)
129
+
130
+ if up_axis == "X":
131
+ UsdGeom.SetStageUpAxis(self.stage, UsdGeom.Tokens.x)
132
+ elif up_axis == "Y":
133
+ UsdGeom.SetStageUpAxis(self.stage, UsdGeom.Tokens.y)
134
+ elif up_axis == "Z":
135
+ UsdGeom.SetStageUpAxis(self.stage, UsdGeom.Tokens.z)
136
+
137
+ dome_light = UsdLux.DomeLight.Define(stage, "/dome_light")
138
+ dome_light.AddRotateXYZOp().Set((-90.0, -30.0, 0.0))
139
+ dome_light.GetEnableColorTemperatureAttr().Set(True)
140
+ dome_light.GetColorTemperatureAttr().Set(6150.0)
141
+ dome_light.GetIntensityAttr().Set(1.0)
142
+ dome_light.GetExposureAttr().Set(9.0)
143
+ dome_light.GetPrim().CreateAttribute("visibleInPrimaryRay", Sdf.ValueTypeNames.Bool).Set(False)
144
+
145
+ distant_light = UsdLux.DistantLight.Define(stage, "/distant_light")
146
+ distant_light.AddRotateXYZOp().Set((-35.0, 45.0, 0.0))
147
+ distant_light.GetEnableColorTemperatureAttr().Set(True)
148
+ distant_light.GetColorTemperatureAttr().Set(7250.0)
149
+ distant_light.GetIntensityAttr().Set(1.0)
150
+ distant_light.GetExposureAttr().Set(10.0)
151
+
152
+ def begin_frame(self, time):
153
+ self.time = round(time * self.fps)
154
+ self.stage.SetEndTimeCode(self.time)
155
+
156
+ def end_frame(self):
157
+ pass
158
+
159
+ def register_body(self, body_name):
160
+ from pxr import UsdGeom
161
+
162
+ xform = UsdGeom.Xform.Define(self.stage, self.root.GetPath().AppendChild(body_name))
163
+
164
+ _usd_add_xform(xform)
165
+
166
+ def _resolve_path(self, name, parent_body=None, is_template=False):
167
+ # resolve the path to the prim with the given name and optional parent body
168
+ if is_template:
169
+ return self.root.GetPath().AppendChild("_template_shapes").AppendChild(name)
170
+ if parent_body is None:
171
+ return self.root.GetPath().AppendChild(name)
172
+ else:
173
+ return self.root.GetPath().AppendChild(parent_body).AppendChild(name)
174
+
175
+ def add_shape_instance(
176
+ self,
177
+ name: str,
178
+ shape: int,
179
+ body,
180
+ pos: tuple,
181
+ rot: tuple,
182
+ scale: tuple = (1.0, 1.0, 1.0),
183
+ color1=None,
184
+ color2=None,
185
+ custom_index: int = -1,
186
+ visible: bool = True,
187
+ ):
188
+ if not visible:
189
+ return
190
+ sdf_path = self._resolve_path(name, body)
191
+ instance = self._shape_constructors[shape.name].Define(self.stage, sdf_path)
192
+ instance.GetPrim().GetReferences().AddInternalReference(shape)
193
+
194
+ _usd_add_xform(instance)
195
+ if shape.name in self._shape_custom_scale:
196
+ cs = self._shape_custom_scale[shape.name]
197
+ scale = (scale[0] * cs[0], scale[1] * cs[1], scale[2] * cs[2])
198
+ _usd_set_xform(instance, pos, rot, scale, self.time)
199
+
200
+ def render_plane(
201
+ self,
202
+ name: str,
203
+ pos: tuple,
204
+ rot: tuple,
205
+ width: float,
206
+ length: float,
207
+ color: tuple[float, float, float] = (1.0, 1.0, 1.0),
208
+ color2=None,
209
+ parent_body: str | None = None,
210
+ is_template: bool = False,
211
+ u_scaling: float = 1.0,
212
+ v_scaling: float = 1.0,
213
+ visible: bool = True,
214
+ ):
215
+ """Render a plane with the given dimensions.
216
+
217
+ Args:
218
+ name: Name of the plane
219
+ pos: Position of the plane
220
+ rot: Rotation of the plane
221
+ width: Width of the plane
222
+ length: Length of the plane
223
+ color: Color of the plane
224
+ parent_body: Name of the parent body
225
+ is_template: Whether the plane is a template
226
+ """
227
+ from pxr import Sdf, UsdGeom
228
+
229
+ if is_template:
230
+ prim_path = self._resolve_path(name, parent_body, is_template)
231
+ blueprint = UsdGeom.Scope.Define(self.stage, prim_path)
232
+ blueprint_prim = blueprint.GetPrim()
233
+ blueprint_prim.SetInstanceable(True)
234
+ blueprint_prim.SetSpecifier(Sdf.SpecifierClass)
235
+ plane_path = prim_path.AppendChild("plane")
236
+ else:
237
+ plane_path = self._resolve_path(name, parent_body)
238
+ prim_path = plane_path
239
+
240
+ plane = UsdGeom.Mesh.Get(self.stage, plane_path)
241
+ if not plane:
242
+ plane = UsdGeom.Mesh.Define(self.stage, plane_path)
243
+ plane.CreateDoubleSidedAttr().Set(True)
244
+
245
+ width = width if width > 0.0 else 100.0
246
+ length = length if length > 0.0 else 100.0
247
+ points = ((-width, 0.0, -length), (width, 0.0, -length), (width, 0.0, length), (-width, 0.0, length))
248
+ normals = ((0.0, 1.0, 0.0), (0.0, 1.0, 0.0), (0.0, 1.0, 0.0), (0.0, 1.0, 0.0))
249
+ counts = (4,)
250
+ indices = [0, 1, 2, 3]
251
+
252
+ plane.GetPointsAttr().Set(points)
253
+ plane.GetNormalsAttr().Set(normals)
254
+ plane.GetFaceVertexCountsAttr().Set(counts)
255
+ plane.GetFaceVertexIndicesAttr().Set(indices)
256
+ _usd_add_xform(plane)
257
+
258
+ self._shape_constructors[name] = UsdGeom.Mesh
259
+
260
+ if not is_template:
261
+ _usd_set_xform(plane, pos, rot, (1.0, 1.0, 1.0), 0.0)
262
+
263
+ plane.GetVisibilityAttr().Set("inherited" if visible else "invisible", self.time)
264
+ return prim_path
265
+
266
+ def render_ground(self, size: float = 100.0, plane=None):
267
+ from pxr import UsdGeom
268
+
269
+ mesh = UsdGeom.Mesh.Define(self.stage, self.root.GetPath().AppendChild("ground"))
270
+ mesh.CreateDoubleSidedAttr().Set(True)
271
+
272
+ if self.up_axis == "X":
273
+ points = ((0.0, size, -size), (0.0, -size, -size), (0.0, size, size), (0.0, -size, size))
274
+ normals = ((1.0, 0.0, 0.0), (1.0, 0.0, 0.0), (1.0, 0.0, 0.0), (1.0, 0.0, 0.0))
275
+ elif self.up_axis == "Y":
276
+ points = ((-size, 0.0, -size), (size, 0.0, -size), (-size, 0.0, size), (size, 0.0, size))
277
+ normals = ((0.0, 1.0, 0.0), (0.0, 1.0, 0.0), (0.0, 1.0, 0.0), (0.0, 1.0, 0.0))
278
+ elif self.up_axis == "Z":
279
+ points = ((-size, size, 0.0), (size, size, 0.0), (-size, -size, 0.0), (size, -size, 0.0))
280
+ normals = ((0.0, 0.0, 1.0), (0.0, 0.0, 1.0), (0.0, 0.0, 1.0), (0.0, 0.0, 1.0))
281
+ if plane is not None:
282
+ normal = np.array(plane[:3])
283
+ normal /= np.linalg.norm(normal)
284
+ pos = plane[3] * normal
285
+ axis_up = [0.0, 0.0, 0.0]
286
+ axis_up["XYZ".index(self.up_axis)] = 1.0
287
+ if np.allclose(normal, axis_up):
288
+ # no rotation necessary
289
+ q = (0.0, 0.0, 0.0, 1.0)
290
+ else:
291
+ c = np.cross(normal, axis_up)
292
+ angle = np.arcsin(np.linalg.norm(c))
293
+ axis = np.abs(c) / np.linalg.norm(c)
294
+ q = wp.quat_from_axis_angle(axis, angle)
295
+ tf = wp.transform(pos, q)
296
+ points = [wp.transform_point(tf, wp.vec3(p)) for p in points]
297
+ normals = [wp.transform_vector(tf, wp.vec3(n)) for n in normals]
298
+ counts = (4,)
299
+ indices = [0, 2, 3, 1]
300
+
301
+ mesh.GetPointsAttr().Set(points)
302
+ mesh.GetNormalsAttr().Set(normals)
303
+ mesh.GetFaceVertexCountsAttr().Set(counts)
304
+ mesh.GetFaceVertexIndicesAttr().Set(indices)
305
+
306
+ def render_sphere(
307
+ self,
308
+ name: str,
309
+ pos: tuple,
310
+ rot: tuple,
311
+ radius: float,
312
+ parent_body: str | None = None,
313
+ is_template: bool = False,
314
+ color: tuple[float, float, float] | None = None,
315
+ visible: bool = True,
316
+ ):
317
+ """Debug helper to add a sphere for visualization
318
+
319
+ Args:
320
+ pos: The position of the sphere
321
+ radius: The radius of the sphere
322
+ name: A name for the USD prim on the stage
323
+ color: The color of the sphere
324
+ """
325
+
326
+ from pxr import Gf, Sdf, UsdGeom
327
+
328
+ if is_template:
329
+ prim_path = self._resolve_path(name, parent_body, is_template)
330
+ blueprint = UsdGeom.Scope.Define(self.stage, prim_path)
331
+ blueprint_prim = blueprint.GetPrim()
332
+ blueprint_prim.SetInstanceable(True)
333
+ blueprint_prim.SetSpecifier(Sdf.SpecifierClass)
334
+ sphere_path = prim_path.AppendChild("sphere")
335
+ else:
336
+ sphere_path = self._resolve_path(name, parent_body)
337
+ prim_path = sphere_path
338
+
339
+ sphere = UsdGeom.Sphere.Get(self.stage, sphere_path)
340
+ if not sphere:
341
+ sphere = UsdGeom.Sphere.Define(self.stage, sphere_path)
342
+ _usd_add_xform(sphere)
343
+
344
+ sphere.GetRadiusAttr().Set(radius, self.time)
345
+
346
+ if color is not None:
347
+ sphere.GetDisplayColorAttr().Set([Gf.Vec3f(color)], self.time)
348
+
349
+ self._shape_constructors[name] = UsdGeom.Sphere
350
+
351
+ if not is_template:
352
+ _usd_set_xform(sphere, pos, rot, (1.0, 1.0, 1.0), self.time)
353
+
354
+ sphere.GetVisibilityAttr().Set("inherited" if visible else "invisible", self.time)
355
+ return prim_path
356
+
357
+ def render_capsule(
358
+ self,
359
+ name: str,
360
+ pos: tuple,
361
+ rot: tuple,
362
+ radius: float,
363
+ half_height: float,
364
+ parent_body: str | None = None,
365
+ is_template: bool = False,
366
+ up_axis: int = 1,
367
+ color: tuple[float, float, float] | None = None,
368
+ visible: bool = True,
369
+ ):
370
+ """Debug helper to add a capsule for visualization
371
+
372
+ Args:
373
+ pos: The position of the capsule
374
+ radius: The radius of the capsule
375
+ half_height: The half height of the capsule
376
+ name: A name for the USD prim on the stage
377
+ color: The color of the capsule
378
+ """
379
+
380
+ from pxr import Gf, Sdf, UsdGeom
381
+
382
+ if is_template:
383
+ prim_path = self._resolve_path(name, parent_body, is_template)
384
+ blueprint = UsdGeom.Scope.Define(self.stage, prim_path)
385
+ blueprint_prim = blueprint.GetPrim()
386
+ blueprint_prim.SetInstanceable(True)
387
+ blueprint_prim.SetSpecifier(Sdf.SpecifierClass)
388
+ capsule_path = prim_path.AppendChild("capsule")
389
+ else:
390
+ capsule_path = self._resolve_path(name, parent_body)
391
+ prim_path = capsule_path
392
+
393
+ capsule = UsdGeom.Capsule.Get(self.stage, capsule_path)
394
+ if not capsule:
395
+ capsule = UsdGeom.Capsule.Define(self.stage, capsule_path)
396
+ _usd_add_xform(capsule)
397
+
398
+ capsule.GetRadiusAttr().Set(float(radius))
399
+ capsule.GetHeightAttr().Set(float(half_height * 2.0))
400
+ capsule.GetAxisAttr().Set(UP_AXIS_TOKEN[up_axis])
401
+
402
+ if color is not None:
403
+ capsule.GetDisplayColorAttr().Set([Gf.Vec3f(color)], self.time)
404
+
405
+ self._shape_constructors[name] = UsdGeom.Capsule
406
+
407
+ if not is_template:
408
+ _usd_set_xform(capsule, pos, rot, (1.0, 1.0, 1.0), self.time)
409
+
410
+ capsule.GetVisibilityAttr().Set("inherited" if visible else "invisible", self.time)
411
+ return prim_path
412
+
413
+ def render_cylinder(
414
+ self,
415
+ name: str,
416
+ pos: tuple,
417
+ rot: tuple,
418
+ radius: float,
419
+ half_height: float,
420
+ parent_body: str | None = None,
421
+ is_template: bool = False,
422
+ up_axis: int = 1,
423
+ color: tuple[float, float, float] | None = None,
424
+ visible: bool = True,
425
+ ):
426
+ """Debug helper to add a cylinder for visualization
427
+
428
+ Args:
429
+ pos: The position of the cylinder
430
+ radius: The radius of the cylinder
431
+ half_height: The half height of the cylinder
432
+ name: A name for the USD prim on the stage
433
+ color: The color of the cylinder
434
+ """
435
+
436
+ from pxr import Gf, Sdf, UsdGeom
437
+
438
+ if is_template:
439
+ prim_path = self._resolve_path(name, parent_body, is_template)
440
+ blueprint = UsdGeom.Scope.Define(self.stage, prim_path)
441
+ blueprint_prim = blueprint.GetPrim()
442
+ blueprint_prim.SetInstanceable(True)
443
+ blueprint_prim.SetSpecifier(Sdf.SpecifierClass)
444
+ cylinder_path = prim_path.AppendChild("cylinder")
445
+ else:
446
+ cylinder_path = self._resolve_path(name, parent_body)
447
+ prim_path = cylinder_path
448
+
449
+ cylinder = UsdGeom.Cylinder.Get(self.stage, cylinder_path)
450
+ if not cylinder:
451
+ cylinder = UsdGeom.Cylinder.Define(self.stage, cylinder_path)
452
+ _usd_add_xform(cylinder)
453
+
454
+ cylinder.GetRadiusAttr().Set(float(radius))
455
+ cylinder.GetHeightAttr().Set(float(half_height * 2.0))
456
+ cylinder.GetAxisAttr().Set(UP_AXIS_TOKEN[up_axis])
457
+
458
+ if color is not None:
459
+ cylinder.GetDisplayColorAttr().Set([Gf.Vec3f(color)], self.time)
460
+
461
+ self._shape_constructors[name] = UsdGeom.Cylinder
462
+
463
+ if not is_template:
464
+ _usd_set_xform(cylinder, pos, rot, (1.0, 1.0, 1.0), self.time)
465
+
466
+ cylinder.GetVisibilityAttr().Set("inherited" if visible else "invisible", self.time)
467
+ return prim_path
468
+
469
+ def render_cone(
470
+ self,
471
+ name: str,
472
+ pos: tuple,
473
+ rot: tuple,
474
+ radius: float,
475
+ half_height: float,
476
+ parent_body: str | None = None,
477
+ is_template: bool = False,
478
+ up_axis: int = 1,
479
+ color: tuple[float, float, float] | None = None,
480
+ visible: bool = True,
481
+ ):
482
+ """Debug helper to add a cone for visualization
483
+
484
+ Args:
485
+ pos: The position of the cone
486
+ radius: The radius of the cone
487
+ half_height: The half height of the cone
488
+ name: A name for the USD prim on the stage
489
+ color: The color of the cone
490
+ """
491
+
492
+ from pxr import Gf, Sdf, UsdGeom
493
+
494
+ if is_template:
495
+ prim_path = self._resolve_path(name, parent_body, is_template)
496
+ blueprint = UsdGeom.Scope.Define(self.stage, prim_path)
497
+ blueprint_prim = blueprint.GetPrim()
498
+ blueprint_prim.SetInstanceable(True)
499
+ blueprint_prim.SetSpecifier(Sdf.SpecifierClass)
500
+ cone_path = prim_path.AppendChild("cone")
501
+ else:
502
+ cone_path = self._resolve_path(name, parent_body)
503
+ prim_path = cone_path
504
+
505
+ cone = UsdGeom.Cone.Get(self.stage, cone_path)
506
+ if not cone:
507
+ cone = UsdGeom.Cone.Define(self.stage, cone_path)
508
+ _usd_add_xform(cone)
509
+
510
+ cone.GetRadiusAttr().Set(float(radius))
511
+ cone.GetHeightAttr().Set(float(half_height * 2.0))
512
+ cone.GetAxisAttr().Set(UP_AXIS_TOKEN[up_axis])
513
+
514
+ if color is not None:
515
+ cone.GetDisplayColorAttr().Set([Gf.Vec3f(color)], self.time)
516
+
517
+ self._shape_constructors[name] = UsdGeom.Cone
518
+
519
+ if not is_template:
520
+ _usd_set_xform(cone, pos, rot, (1.0, 1.0, 1.0), self.time)
521
+
522
+ cone.GetVisibilityAttr().Set("inherited" if visible else "invisible", self.time)
523
+ return prim_path
524
+
525
+ def render_box(
526
+ self,
527
+ name: str,
528
+ pos: tuple,
529
+ rot: tuple,
530
+ extents: tuple,
531
+ parent_body: str | None = None,
532
+ is_template: bool = False,
533
+ color: tuple[float, float, float] | None = None,
534
+ visible: bool = True,
535
+ ):
536
+ """Debug helper to add a box for visualization
537
+
538
+ Args:
539
+ pos: The position of the box
540
+ extents: The radius of the box
541
+ name: A name for the USD prim on the stage
542
+ color: The color of the box
543
+ """
544
+
545
+ from pxr import Gf, Sdf, UsdGeom
546
+
547
+ if is_template:
548
+ prim_path = self._resolve_path(name, parent_body, is_template)
549
+ blueprint = UsdGeom.Scope.Define(self.stage, prim_path)
550
+ blueprint_prim = blueprint.GetPrim()
551
+ blueprint_prim.SetInstanceable(True)
552
+ blueprint_prim.SetSpecifier(Sdf.SpecifierClass)
553
+ cube_path = prim_path.AppendChild("cube")
554
+ else:
555
+ cube_path = self._resolve_path(name, parent_body)
556
+ prim_path = cube_path
557
+
558
+ cube = UsdGeom.Cube.Get(self.stage, cube_path)
559
+ if not cube:
560
+ cube = UsdGeom.Cube.Define(self.stage, cube_path)
561
+ _usd_add_xform(cube)
562
+
563
+ if color is not None:
564
+ cube.GetDisplayColorAttr().Set([Gf.Vec3f(color)], self.time)
565
+
566
+ self._shape_constructors[name] = UsdGeom.Cube
567
+ self._shape_custom_scale[name] = extents
568
+
569
+ if not is_template:
570
+ _usd_set_xform(cube, pos, rot, extents, self.time)
571
+
572
+ cube.GetVisibilityAttr().Set("inherited" if visible else "invisible", self.time)
573
+ return prim_path
574
+
575
+ def render_ref(self, name: str, path: str, pos: tuple, rot: tuple, scale: tuple, color: tuple | None = None):
576
+ from pxr import Gf, Usd, UsdGeom
577
+
578
+ ref_path = "/root/" + name
579
+
580
+ ref = UsdGeom.Xform.Get(self.stage, ref_path)
581
+ if not ref:
582
+ ref = UsdGeom.Xform.Define(self.stage, ref_path)
583
+ ref.GetPrim().GetReferences().AddReference(path)
584
+ _usd_add_xform(ref)
585
+
586
+ # update transform
587
+ _usd_set_xform(ref, pos, rot, scale, self.time)
588
+
589
+ if color is not None:
590
+ it = iter(Usd.PrimRange(ref.GetPrim()))
591
+ for prim in it:
592
+ if prim.IsA(UsdGeom.Gprim):
593
+ UsdGeom.Gprim(prim).GetDisplayColorAttr().Set([Gf.Vec3f(color)], self.time)
594
+ it.PruneChildren()
595
+
596
+ def render_mesh(
597
+ self,
598
+ name: str,
599
+ points,
600
+ indices,
601
+ colors=None,
602
+ pos=(0.0, 0.0, 0.0),
603
+ rot=(0.0, 0.0, 0.0, 1.0),
604
+ scale=(1.0, 1.0, 1.0),
605
+ update_topology=False,
606
+ parent_body: str | None = None,
607
+ is_template: bool = False,
608
+ smooth_shading: bool = True,
609
+ visible: bool = True,
610
+ ):
611
+ from pxr import Sdf, UsdGeom
612
+
613
+ if is_template:
614
+ prim_path = self._resolve_path(name, parent_body, is_template)
615
+ blueprint = UsdGeom.Scope.Define(self.stage, prim_path)
616
+ blueprint_prim = blueprint.GetPrim()
617
+ blueprint_prim.SetInstanceable(True)
618
+ blueprint_prim.SetSpecifier(Sdf.SpecifierClass)
619
+ mesh_path = prim_path.AppendChild("mesh")
620
+ else:
621
+ mesh_path = self._resolve_path(name, parent_body)
622
+ prim_path = mesh_path
623
+
624
+ mesh = UsdGeom.Mesh.Get(self.stage, mesh_path)
625
+ if not mesh:
626
+ mesh = UsdGeom.Mesh.Define(self.stage, mesh_path)
627
+ if colors is not None and len(colors) == 3:
628
+ color_interp = "constant"
629
+ else:
630
+ color_interp = "vertex"
631
+
632
+ UsdGeom.Primvar(mesh.GetDisplayColorAttr()).SetInterpolation(color_interp)
633
+ _usd_add_xform(mesh)
634
+
635
+ # force topology update on first frame
636
+ update_topology = True
637
+
638
+ mesh.GetPointsAttr().Set(points, self.time)
639
+
640
+ if update_topology:
641
+ idxs = np.array(indices).reshape(-1, 3)
642
+ mesh.GetFaceVertexIndicesAttr().Set(idxs, self.time)
643
+ mesh.GetFaceVertexCountsAttr().Set([3] * len(idxs), self.time)
644
+
645
+ if colors is not None:
646
+ if len(colors) == 3:
647
+ colors = (colors,)
648
+
649
+ mesh.GetDisplayColorAttr().Set(colors, self.time)
650
+
651
+ self._shape_constructors[name] = UsdGeom.Mesh
652
+ self._shape_custom_scale[name] = scale
653
+
654
+ if not is_template:
655
+ _usd_set_xform(mesh, pos, rot, scale, self.time)
656
+
657
+ mesh.GetVisibilityAttr().Set("inherited" if visible else "invisible", self.time)
658
+ return prim_path
659
+
660
+ def render_arrow(
661
+ self,
662
+ name: str,
663
+ pos: tuple,
664
+ rot: tuple,
665
+ base_radius: float,
666
+ base_height: float,
667
+ cap_radius: float | None = None,
668
+ cap_height: float | None = None,
669
+ parent_body: str | None = None,
670
+ is_template: bool = False,
671
+ up_axis: int = 1,
672
+ color: tuple[float, float, float] | None = None,
673
+ visible: bool = True,
674
+ ):
675
+ from pxr import Gf, Sdf, UsdGeom
676
+
677
+ if is_template:
678
+ prim_path = self._resolve_path(name, parent_body, is_template)
679
+ blueprint = UsdGeom.Scope.Define(self.stage, prim_path)
680
+ blueprint_prim = blueprint.GetPrim()
681
+ blueprint_prim.SetInstanceable(True)
682
+ blueprint_prim.SetSpecifier(Sdf.SpecifierClass)
683
+ arrow_path = prim_path.AppendChild("arrow")
684
+ else:
685
+ arrow_path = self._resolve_path(name, parent_body)
686
+ prim_path = arrow_path
687
+
688
+ arrow = UsdGeom.Xform.Get(self.stage, arrow_path)
689
+ if not arrow:
690
+ arrow = UsdGeom.Xform.Define(self.stage, arrow_path)
691
+ _usd_add_xform(arrow)
692
+
693
+ base_path = arrow_path.AppendChild("base")
694
+ base = UsdGeom.Xform.Get(self.stage, base_path)
695
+ if not base:
696
+ base = UsdGeom.Cylinder.Define(self.stage, base_path)
697
+ _usd_add_xform(base)
698
+
699
+ base.GetRadiusAttr().Set(float(base_radius))
700
+ base.GetHeightAttr().Set(float(base_height))
701
+ base.GetAxisAttr().Set(UP_AXIS_TOKEN[up_axis])
702
+ _usd_set_xform(base, UP_AXIS_VEC[up_axis] * base_height * 0.5)
703
+
704
+ cap_path = arrow_path.AppendChild("cap")
705
+ cap = UsdGeom.Xform.Get(self.stage, cap_path)
706
+ if not cap:
707
+ cap = UsdGeom.Cone.Define(self.stage, arrow_path.AppendChild("cap"))
708
+ _usd_add_xform(cap)
709
+
710
+ cap.GetRadiusAttr().Set(float(cap_radius))
711
+ cap.GetHeightAttr().Set(float(cap_height))
712
+ cap.GetAxisAttr().Set(UP_AXIS_TOKEN[up_axis])
713
+ _usd_set_xform(cap, UP_AXIS_VEC[up_axis] * (base_height + cap_height * 0.5))
714
+
715
+ if color is not None:
716
+ base.GetDisplayColorAttr().Set([Gf.Vec3f(color)], self.time)
717
+ cap.GetDisplayColorAttr().Set([Gf.Vec3f(color)], self.time)
718
+
719
+ self._shape_constructors[name] = UsdGeom.Xform
720
+
721
+ if not is_template:
722
+ _usd_set_xform(arrow, pos, rot, (1.0, 1.0, 1.0), self.time)
723
+
724
+ arrow.GetVisibilityAttr().Set("inherited" if visible else "invisible", self.time)
725
+ return prim_path
726
+
727
+ def render_line_list(
728
+ self,
729
+ name: str,
730
+ vertices,
731
+ indices,
732
+ color: tuple[float, float, float] | None = None,
733
+ radius: float = 0.01,
734
+ visible: bool = True,
735
+ ):
736
+ """Debug helper to add a line list as a set of capsules
737
+
738
+ Args:
739
+ vertices: The vertices of the line-strip
740
+ color: The color of the line
741
+ time: The time to update at
742
+ """
743
+
744
+ from pxr import Gf, UsdGeom
745
+
746
+ num_lines = int(len(indices) / 2)
747
+
748
+ if num_lines < 1:
749
+ return
750
+
751
+ # look up rope point instancer
752
+ instancer_path = self.root.GetPath().AppendChild(name)
753
+ instancer = UsdGeom.PointInstancer.Get(self.stage, instancer_path)
754
+
755
+ if not instancer:
756
+ instancer = UsdGeom.PointInstancer.Define(self.stage, instancer_path)
757
+ instancer_capsule = UsdGeom.Capsule.Define(self.stage, instancer.GetPath().AppendChild("capsule"))
758
+ instancer_capsule.GetRadiusAttr().Set(radius)
759
+ instancer.CreatePrototypesRel().SetTargets([instancer_capsule.GetPath()])
760
+ # instancer.CreatePrimvar("displayColor", Sdf.ValueTypeNames.Float3Array, "constant", 1)
761
+
762
+ line_positions = []
763
+ line_rotations = []
764
+ line_scales = []
765
+
766
+ for i in range(num_lines):
767
+ pos0 = vertices[indices[i * 2 + 0]]
768
+ pos1 = vertices[indices[i * 2 + 1]]
769
+
770
+ (pos, rot, scale) = _compute_segment_xform(
771
+ Gf.Vec3f(float(pos0[0]), float(pos0[1]), float(pos0[2])),
772
+ Gf.Vec3f(float(pos1[0]), float(pos1[1]), float(pos1[2])),
773
+ )
774
+
775
+ line_positions.append(pos)
776
+ line_rotations.append(rot)
777
+ line_scales.append(scale)
778
+ # line_colors.append(Gf.Vec3f((float(i)/num_lines, 0.5, 0.5)))
779
+
780
+ instancer.GetPositionsAttr().Set(line_positions, self.time)
781
+ instancer.GetOrientationsAttr().Set(line_rotations, self.time)
782
+ instancer.GetScalesAttr().Set(line_scales, self.time)
783
+ instancer.GetProtoIndicesAttr().Set([0] * num_lines, self.time)
784
+
785
+ # instancer.GetPrimvar("displayColor").Set(line_colors, time)
786
+
787
+ instancer.GetVisibilityAttr().Set("inherited" if visible else "invisible", self.time)
788
+
789
+ def render_line_strip(
790
+ self,
791
+ name: str,
792
+ vertices,
793
+ color: tuple[float, float, float] | None = None,
794
+ radius: float = 0.01,
795
+ visible: bool = True,
796
+ ):
797
+ from pxr import Gf, UsdGeom
798
+
799
+ num_lines = int(len(vertices) - 1)
800
+
801
+ if num_lines < 1:
802
+ return
803
+
804
+ # look up rope point instancer
805
+ instancer_path = self.root.GetPath().AppendChild(name)
806
+ instancer = UsdGeom.PointInstancer.Get(self.stage, instancer_path)
807
+
808
+ if not instancer:
809
+ instancer = UsdGeom.PointInstancer.Define(self.stage, instancer_path)
810
+ instancer_capsule = UsdGeom.Capsule.Define(self.stage, instancer.GetPath().AppendChild("capsule"))
811
+ instancer_capsule.GetRadiusAttr().Set(radius)
812
+ instancer.CreatePrototypesRel().SetTargets([instancer_capsule.GetPath()])
813
+
814
+ line_positions = []
815
+ line_rotations = []
816
+ line_scales = []
817
+
818
+ for i in range(num_lines):
819
+ pos0 = vertices[i]
820
+ pos1 = vertices[i + 1]
821
+
822
+ (pos, rot, scale) = _compute_segment_xform(
823
+ Gf.Vec3f(float(pos0[0]), float(pos0[1]), float(pos0[2])),
824
+ Gf.Vec3f(float(pos1[0]), float(pos1[1]), float(pos1[2])),
825
+ )
826
+
827
+ line_positions.append(pos)
828
+ line_rotations.append(rot)
829
+ line_scales.append(scale)
830
+
831
+ instancer.GetPositionsAttr().Set(line_positions, self.time)
832
+ instancer.GetOrientationsAttr().Set(line_rotations, self.time)
833
+ instancer.GetScalesAttr().Set(line_scales, self.time)
834
+ instancer.GetProtoIndicesAttr().Set([0] * num_lines, self.time)
835
+
836
+ instancer_capsule = UsdGeom.Capsule.Get(self.stage, instancer.GetPath().AppendChild("capsule"))
837
+ instancer_capsule.GetDisplayColorAttr().Set([Gf.Vec3f(color)], self.time)
838
+
839
+ instancer.GetVisibilityAttr().Set("inherited" if visible else "invisible", self.time)
840
+
841
+ def render_points(self, name: str, points, radius, colors=None, as_spheres: bool = True, visible: bool = True):
842
+ from pxr import Sdf, UsdGeom, Vt
843
+
844
+ instancer_path = self._resolve_path(name)
845
+
846
+ if np.isscalar(radius):
847
+ radius_interp = "constant"
848
+ else:
849
+ radius_interp = "vertex"
850
+
851
+ if colors is None:
852
+ color_interp = "constant"
853
+ elif len(colors) == 3 and all(np.isscalar(x) for x in colors):
854
+ color_interp = "constant"
855
+ is_single_color = True
856
+ else:
857
+ color_interp = "vertex"
858
+ is_single_color = False
859
+
860
+ if as_spheres:
861
+ instancer = UsdGeom.PointInstancer.Get(self.stage, instancer_path)
862
+ if not instancer:
863
+ instancer = UsdGeom.PointInstancer.Define(self.stage, instancer_path)
864
+ sphere = UsdGeom.Sphere.Define(self.stage, instancer.GetPath().AppendChild("sphere"))
865
+ primvar_api = UsdGeom.PrimvarsAPI(instancer)
866
+
867
+ instancer.CreatePrototypesRel().SetTargets((sphere.GetPath(),))
868
+ instancer.CreateProtoIndicesAttr().Set(Vt.IntArray((0,) * len(points)))
869
+
870
+ if colors is not None:
871
+ primvar_api.CreatePrimvar("displayColor", Sdf.ValueTypeNames.Color3fArray, color_interp, 1)
872
+
873
+ instancer.GetPositionsAttr().Set(points, self.time)
874
+
875
+ if radius_interp == "constant":
876
+ radius = np.tile(radius, (3, len(points))).T
877
+ else:
878
+ radius = np.tile(radius, (3, 1)).T
879
+
880
+ instancer.GetScalesAttr().Set(radius, self.time)
881
+
882
+ if colors is not None:
883
+ if is_single_color:
884
+ colors = (colors,)
885
+
886
+ primvar_api = UsdGeom.PrimvarsAPI(instancer)
887
+ primvar_api.GetPrimvar("displayColor").Set(colors, self.time)
888
+ else:
889
+ instancer = UsdGeom.Points.Get(self.stage, instancer_path)
890
+ if not instancer:
891
+ instancer = UsdGeom.Points.Define(self.stage, instancer_path)
892
+
893
+ UsdGeom.Primvar(instancer.GetWidthsAttr()).SetInterpolation(radius_interp)
894
+ UsdGeom.Primvar(instancer.GetDisplayColorAttr()).SetInterpolation(color_interp)
895
+
896
+ instancer.GetPointsAttr().Set(points, self.time)
897
+
898
+ if np.isscalar(radius):
899
+ widths = (radius * 2.0,)
900
+ else:
901
+ widths = np.array(radius) * 2.0
902
+
903
+ instancer.GetWidthsAttr().Set(widths, self.time)
904
+
905
+ if colors is not None:
906
+ if len(colors) == 3:
907
+ colors = (colors,)
908
+
909
+ instancer.GetDisplayColorAttr().Set(colors, self.time)
910
+
911
+ instancer.GetVisibilityAttr().Set("inherited" if visible else "invisible", self.time)
912
+ return instancer.GetPath()
913
+
914
+ def update_body_transforms(self, body_q):
915
+ from pxr import Sdf, UsdGeom
916
+
917
+ if isinstance(body_q, wp.array):
918
+ body_q = body_q.numpy()
919
+
920
+ with Sdf.ChangeBlock():
921
+ for b in range(self.model.body_count):
922
+ node_name = self.body_names[b]
923
+ node = UsdGeom.Xform(self.stage.GetPrimAtPath(self.root.GetPath().AppendChild(node_name)))
924
+
925
+ # unpack rigid transform
926
+ X_sb = wp.transform_expand(body_q[b])
927
+
928
+ _usd_set_xform(node, X_sb.p, X_sb.q, (1.0, 1.0, 1.0), self.time)
929
+
930
+ def save(self):
931
+ try:
932
+ self.stage.Save()
933
+ except Exception as e:
934
+ print("Failed to save USD stage:", e)
935
+ return False
936
+
937
+ file_path = self.stage.GetRootLayer().realPath
938
+ print(f"Saved the USD stage file at `{file_path}`")
939
+ return True