warp-lang 1.0.2__py3-none-win_amd64.whl → 1.1.0__py3-none-win_amd64.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 (346) hide show
  1. warp/__init__.py +108 -97
  2. warp/__init__.pyi +1 -1
  3. warp/bin/warp-clang.dll +0 -0
  4. warp/bin/warp.dll +0 -0
  5. warp/build.py +115 -113
  6. warp/build_dll.py +383 -375
  7. warp/builtins.py +3425 -3354
  8. warp/codegen.py +2878 -2792
  9. warp/config.py +40 -36
  10. warp/constants.py +45 -45
  11. warp/context.py +5194 -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 +383 -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 +146 -146
  34. warp/examples/benchmarks/benchmark_launches.py +295 -295
  35. warp/examples/browse.py +29 -29
  36. warp/examples/core/example_dem.py +234 -219
  37. warp/examples/core/example_fluid.py +293 -267
  38. warp/examples/core/example_graph_capture.py +144 -126
  39. warp/examples/core/example_marching_cubes.py +188 -174
  40. warp/examples/core/example_mesh.py +174 -155
  41. warp/examples/core/example_mesh_intersect.py +205 -193
  42. warp/examples/core/example_nvdb.py +176 -170
  43. warp/examples/core/example_raycast.py +105 -90
  44. warp/examples/core/example_raymarch.py +199 -178
  45. warp/examples/core/example_render_opengl.py +185 -141
  46. warp/examples/core/example_sph.py +405 -387
  47. warp/examples/core/example_torch.py +222 -181
  48. warp/examples/core/example_wave.py +263 -248
  49. warp/examples/fem/bsr_utils.py +378 -380
  50. warp/examples/fem/example_apic_fluid.py +407 -389
  51. warp/examples/fem/example_convection_diffusion.py +182 -168
  52. warp/examples/fem/example_convection_diffusion_dg.py +219 -209
  53. warp/examples/fem/example_convection_diffusion_dg0.py +204 -194
  54. warp/examples/fem/example_deformed_geometry.py +177 -159
  55. warp/examples/fem/example_diffusion.py +201 -173
  56. warp/examples/fem/example_diffusion_3d.py +177 -152
  57. warp/examples/fem/example_diffusion_mgpu.py +221 -214
  58. warp/examples/fem/example_mixed_elasticity.py +244 -222
  59. warp/examples/fem/example_navier_stokes.py +259 -243
  60. warp/examples/fem/example_stokes.py +220 -192
  61. warp/examples/fem/example_stokes_transfer.py +265 -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 +260 -246
  65. warp/examples/optim/example_cloth_throw.py +222 -209
  66. warp/examples/optim/example_diffray.py +566 -536
  67. warp/examples/optim/example_drone.py +864 -835
  68. warp/examples/optim/example_inverse_kinematics.py +176 -168
  69. warp/examples/optim/example_inverse_kinematics_torch.py +185 -169
  70. warp/examples/optim/example_spring_cage.py +239 -231
  71. warp/examples/optim/example_trajectory.py +223 -199
  72. warp/examples/optim/example_walker.py +306 -293
  73. warp/examples/sim/example_cartpole.py +139 -129
  74. warp/examples/sim/example_cloth.py +196 -186
  75. warp/examples/sim/example_granular.py +124 -111
  76. warp/examples/sim/example_granular_collision_sdf.py +197 -186
  77. warp/examples/sim/example_jacobian_ik.py +236 -214
  78. warp/examples/sim/example_particle_chain.py +118 -105
  79. warp/examples/sim/example_quadruped.py +193 -180
  80. warp/examples/sim/example_rigid_chain.py +197 -187
  81. warp/examples/sim/example_rigid_contact.py +189 -177
  82. warp/examples/sim/example_rigid_force.py +127 -125
  83. warp/examples/sim/example_rigid_gyroscopic.py +109 -95
  84. warp/examples/sim/example_rigid_soft_contact.py +134 -122
  85. warp/examples/sim/example_soft_body.py +190 -177
  86. warp/fabric.py +337 -335
  87. warp/fem/__init__.py +60 -27
  88. warp/fem/cache.py +401 -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 +15 -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 +744 -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 +441 -435
  104. warp/fem/geometry/hexmesh.py +953 -953
  105. warp/fem/geometry/partition.py +374 -376
  106. warp/fem/geometry/quadmesh_2d.py +532 -532
  107. warp/fem/geometry/tetmesh.py +840 -840
  108. warp/fem/geometry/trimesh_2d.py +577 -577
  109. warp/fem/integrate.py +1630 -1615
  110. warp/fem/operator.py +190 -191
  111. warp/fem/polynomial.py +214 -213
  112. warp/fem/quadrature/__init__.py +2 -2
  113. warp/fem/quadrature/pic_quadrature.py +243 -245
  114. warp/fem/quadrature/quadrature.py +295 -294
  115. warp/fem/space/__init__.py +294 -292
  116. warp/fem/space/basis_space.py +488 -489
  117. warp/fem/space/collocated_function_space.py +100 -105
  118. warp/fem/space/dof_mapper.py +236 -236
  119. warp/fem/space/function_space.py +148 -145
  120. warp/fem/space/grid_2d_function_space.py +267 -267
  121. warp/fem/space/grid_3d_function_space.py +305 -306
  122. warp/fem/space/hexmesh_function_space.py +350 -352
  123. warp/fem/space/partition.py +350 -350
  124. warp/fem/space/quadmesh_2d_function_space.py +368 -369
  125. warp/fem/space/restriction.py +158 -160
  126. warp/fem/space/shape/__init__.py +13 -15
  127. warp/fem/space/shape/cube_shape_function.py +738 -738
  128. warp/fem/space/shape/shape_function.py +102 -103
  129. warp/fem/space/shape/square_shape_function.py +611 -611
  130. warp/fem/space/shape/tet_shape_function.py +565 -567
  131. warp/fem/space/shape/triangle_shape_function.py +429 -429
  132. warp/fem/space/tetmesh_function_space.py +294 -292
  133. warp/fem/space/topology.py +297 -295
  134. warp/fem/space/trimesh_2d_function_space.py +223 -221
  135. warp/fem/types.py +77 -77
  136. warp/fem/utils.py +495 -495
  137. warp/jax.py +166 -141
  138. warp/jax_experimental.py +341 -339
  139. warp/native/array.h +1072 -1025
  140. warp/native/builtin.h +1560 -1560
  141. warp/native/bvh.cpp +398 -398
  142. warp/native/bvh.cu +525 -525
  143. warp/native/bvh.h +429 -429
  144. warp/native/clang/clang.cpp +495 -464
  145. warp/native/crt.cpp +31 -31
  146. warp/native/crt.h +334 -334
  147. warp/native/cuda_crt.h +1049 -1049
  148. warp/native/cuda_util.cpp +549 -540
  149. warp/native/cuda_util.h +288 -203
  150. warp/native/cutlass_gemm.cpp +34 -34
  151. warp/native/cutlass_gemm.cu +372 -372
  152. warp/native/error.cpp +66 -66
  153. warp/native/error.h +27 -27
  154. warp/native/fabric.h +228 -228
  155. warp/native/hashgrid.cpp +301 -278
  156. warp/native/hashgrid.cu +78 -77
  157. warp/native/hashgrid.h +227 -227
  158. warp/native/initializer_array.h +32 -32
  159. warp/native/intersect.h +1204 -1204
  160. warp/native/intersect_adj.h +365 -365
  161. warp/native/intersect_tri.h +322 -322
  162. warp/native/marching.cpp +2 -2
  163. warp/native/marching.cu +497 -497
  164. warp/native/marching.h +2 -2
  165. warp/native/mat.h +1498 -1498
  166. warp/native/matnn.h +333 -333
  167. warp/native/mesh.cpp +203 -203
  168. warp/native/mesh.cu +293 -293
  169. warp/native/mesh.h +1887 -1887
  170. warp/native/nanovdb/NanoVDB.h +4782 -4782
  171. warp/native/nanovdb/PNanoVDB.h +2553 -2553
  172. warp/native/nanovdb/PNanoVDBWrite.h +294 -294
  173. warp/native/noise.h +850 -850
  174. warp/native/quat.h +1084 -1084
  175. warp/native/rand.h +299 -299
  176. warp/native/range.h +108 -108
  177. warp/native/reduce.cpp +156 -156
  178. warp/native/reduce.cu +348 -348
  179. warp/native/runlength_encode.cpp +61 -61
  180. warp/native/runlength_encode.cu +46 -46
  181. warp/native/scan.cpp +30 -30
  182. warp/native/scan.cu +36 -36
  183. warp/native/scan.h +7 -7
  184. warp/native/solid_angle.h +442 -442
  185. warp/native/sort.cpp +94 -94
  186. warp/native/sort.cu +97 -97
  187. warp/native/sort.h +14 -14
  188. warp/native/sparse.cpp +337 -337
  189. warp/native/sparse.cu +544 -544
  190. warp/native/spatial.h +630 -630
  191. warp/native/svd.h +562 -562
  192. warp/native/temp_buffer.h +30 -30
  193. warp/native/vec.h +1132 -1132
  194. warp/native/volume.cpp +297 -297
  195. warp/native/volume.cu +32 -32
  196. warp/native/volume.h +538 -538
  197. warp/native/volume_builder.cu +425 -425
  198. warp/native/volume_builder.h +19 -19
  199. warp/native/warp.cpp +1057 -1052
  200. warp/native/warp.cu +2943 -2828
  201. warp/native/warp.h +313 -305
  202. warp/optim/__init__.py +9 -9
  203. warp/optim/adam.py +120 -120
  204. warp/optim/linear.py +1104 -939
  205. warp/optim/sgd.py +104 -92
  206. warp/render/__init__.py +10 -10
  207. warp/render/render_opengl.py +3217 -3204
  208. warp/render/render_usd.py +768 -749
  209. warp/render/utils.py +152 -150
  210. warp/sim/__init__.py +52 -59
  211. warp/sim/articulation.py +685 -685
  212. warp/sim/collide.py +1594 -1590
  213. warp/sim/import_mjcf.py +489 -481
  214. warp/sim/import_snu.py +220 -221
  215. warp/sim/import_urdf.py +536 -516
  216. warp/sim/import_usd.py +887 -881
  217. warp/sim/inertia.py +316 -317
  218. warp/sim/integrator.py +234 -233
  219. warp/sim/integrator_euler.py +1956 -1956
  220. warp/sim/integrator_featherstone.py +1910 -1991
  221. warp/sim/integrator_xpbd.py +3294 -3312
  222. warp/sim/model.py +4473 -4314
  223. warp/sim/particles.py +113 -112
  224. warp/sim/render.py +417 -403
  225. warp/sim/utils.py +413 -410
  226. warp/sparse.py +1227 -1227
  227. warp/stubs.py +2109 -2469
  228. warp/tape.py +1162 -225
  229. warp/tests/__init__.py +1 -1
  230. warp/tests/__main__.py +4 -4
  231. warp/tests/assets/torus.usda +105 -105
  232. warp/tests/aux_test_class_kernel.py +26 -26
  233. warp/tests/aux_test_compile_consts_dummy.py +10 -10
  234. warp/tests/aux_test_conditional_unequal_types_kernels.py +21 -21
  235. warp/tests/aux_test_dependent.py +22 -22
  236. warp/tests/aux_test_grad_customs.py +23 -23
  237. warp/tests/aux_test_reference.py +11 -11
  238. warp/tests/aux_test_reference_reference.py +10 -10
  239. warp/tests/aux_test_square.py +17 -17
  240. warp/tests/aux_test_unresolved_func.py +14 -14
  241. warp/tests/aux_test_unresolved_symbol.py +14 -14
  242. warp/tests/disabled_kinematics.py +239 -239
  243. warp/tests/run_coverage_serial.py +31 -31
  244. warp/tests/test_adam.py +157 -157
  245. warp/tests/test_arithmetic.py +1124 -1124
  246. warp/tests/test_array.py +2417 -2326
  247. warp/tests/test_array_reduce.py +150 -150
  248. warp/tests/test_async.py +668 -656
  249. warp/tests/test_atomic.py +141 -141
  250. warp/tests/test_bool.py +204 -149
  251. warp/tests/test_builtins_resolution.py +1292 -1292
  252. warp/tests/test_bvh.py +164 -171
  253. warp/tests/test_closest_point_edge_edge.py +228 -228
  254. warp/tests/test_codegen.py +566 -553
  255. warp/tests/test_compile_consts.py +97 -101
  256. warp/tests/test_conditional.py +246 -246
  257. warp/tests/test_copy.py +232 -215
  258. warp/tests/test_ctypes.py +632 -632
  259. warp/tests/test_dense.py +67 -67
  260. warp/tests/test_devices.py +91 -98
  261. warp/tests/test_dlpack.py +530 -529
  262. warp/tests/test_examples.py +400 -378
  263. warp/tests/test_fabricarray.py +955 -955
  264. warp/tests/test_fast_math.py +62 -54
  265. warp/tests/test_fem.py +1277 -1278
  266. warp/tests/test_fp16.py +130 -130
  267. warp/tests/test_func.py +338 -337
  268. warp/tests/test_generics.py +571 -571
  269. warp/tests/test_grad.py +746 -640
  270. warp/tests/test_grad_customs.py +333 -336
  271. warp/tests/test_hash_grid.py +210 -164
  272. warp/tests/test_import.py +39 -39
  273. warp/tests/test_indexedarray.py +1134 -1134
  274. warp/tests/test_intersect.py +67 -67
  275. warp/tests/test_jax.py +307 -307
  276. warp/tests/test_large.py +167 -164
  277. warp/tests/test_launch.py +354 -354
  278. warp/tests/test_lerp.py +261 -261
  279. warp/tests/test_linear_solvers.py +191 -171
  280. warp/tests/test_lvalue.py +421 -493
  281. warp/tests/test_marching_cubes.py +65 -65
  282. warp/tests/test_mat.py +1801 -1827
  283. warp/tests/test_mat_lite.py +115 -115
  284. warp/tests/test_mat_scalar_ops.py +2907 -2889
  285. warp/tests/test_math.py +126 -193
  286. warp/tests/test_matmul.py +500 -499
  287. warp/tests/test_matmul_lite.py +410 -410
  288. warp/tests/test_mempool.py +188 -190
  289. warp/tests/test_mesh.py +284 -324
  290. warp/tests/test_mesh_query_aabb.py +228 -241
  291. warp/tests/test_mesh_query_point.py +692 -702
  292. warp/tests/test_mesh_query_ray.py +292 -303
  293. warp/tests/test_mlp.py +276 -276
  294. warp/tests/test_model.py +110 -110
  295. warp/tests/test_modules_lite.py +39 -39
  296. warp/tests/test_multigpu.py +163 -163
  297. warp/tests/test_noise.py +248 -248
  298. warp/tests/test_operators.py +250 -250
  299. warp/tests/test_options.py +123 -125
  300. warp/tests/test_peer.py +133 -137
  301. warp/tests/test_pinned.py +78 -78
  302. warp/tests/test_print.py +54 -54
  303. warp/tests/test_quat.py +2086 -2086
  304. warp/tests/test_rand.py +288 -288
  305. warp/tests/test_reload.py +217 -217
  306. warp/tests/test_rounding.py +179 -179
  307. warp/tests/test_runlength_encode.py +190 -190
  308. warp/tests/test_sim_grad.py +243 -0
  309. warp/tests/test_sim_kinematics.py +91 -97
  310. warp/tests/test_smoothstep.py +168 -168
  311. warp/tests/test_snippet.py +305 -266
  312. warp/tests/test_sparse.py +468 -460
  313. warp/tests/test_spatial.py +2148 -2148
  314. warp/tests/test_streams.py +486 -473
  315. warp/tests/test_struct.py +710 -675
  316. warp/tests/test_tape.py +173 -148
  317. warp/tests/test_torch.py +743 -743
  318. warp/tests/test_transient_module.py +87 -87
  319. warp/tests/test_types.py +556 -659
  320. warp/tests/test_utils.py +490 -499
  321. warp/tests/test_vec.py +1264 -1268
  322. warp/tests/test_vec_lite.py +73 -73
  323. warp/tests/test_vec_scalar_ops.py +2099 -2099
  324. warp/tests/test_verify_fp.py +94 -94
  325. warp/tests/test_volume.py +737 -736
  326. warp/tests/test_volume_write.py +255 -265
  327. warp/tests/unittest_serial.py +37 -37
  328. warp/tests/unittest_suites.py +363 -359
  329. warp/tests/unittest_utils.py +603 -578
  330. warp/tests/unused_test_misc.py +71 -71
  331. warp/tests/walkthrough_debug.py +85 -85
  332. warp/thirdparty/appdirs.py +598 -598
  333. warp/thirdparty/dlpack.py +143 -143
  334. warp/thirdparty/unittest_parallel.py +566 -561
  335. warp/torch.py +321 -295
  336. warp/types.py +4504 -4450
  337. warp/utils.py +1008 -821
  338. {warp_lang-1.0.2.dist-info → warp_lang-1.1.0.dist-info}/LICENSE.md +126 -126
  339. {warp_lang-1.0.2.dist-info → warp_lang-1.1.0.dist-info}/METADATA +338 -400
  340. warp_lang-1.1.0.dist-info/RECORD +352 -0
  341. warp/examples/assets/cube.usda +0 -42
  342. warp/examples/assets/sphere.usda +0 -56
  343. warp/examples/assets/torus.usda +0 -105
  344. warp_lang-1.0.2.dist-info/RECORD +0 -352
  345. {warp_lang-1.0.2.dist-info → warp_lang-1.1.0.dist-info}/WHEEL +0 -0
  346. {warp_lang-1.0.2.dist-info → warp_lang-1.1.0.dist-info}/top_level.txt +0 -0
warp/render/render_usd.py CHANGED
@@ -1,749 +1,768 @@
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 warp as wp
9
- import numpy as np
10
- import math
11
-
12
-
13
- def _usd_add_xform(prim):
14
- from pxr import UsdGeom
15
-
16
- prim = UsdGeom.Xform(prim)
17
- prim.ClearXformOpOrder()
18
-
19
- t = prim.AddTranslateOp()
20
- r = prim.AddOrientOp()
21
- s = prim.AddScaleOp()
22
-
23
-
24
- def _usd_set_xform(xform, pos: tuple, rot: tuple, scale: tuple, time):
25
- from pxr import UsdGeom, Gf
26
-
27
- xform = UsdGeom.Xform(xform)
28
-
29
- xform_ops = xform.GetOrderedXformOps()
30
-
31
- if pos is not None:
32
- xform_ops[0].Set(Gf.Vec3d(float(pos[0]), float(pos[1]), float(pos[2])), time)
33
- if rot is not None:
34
- xform_ops[1].Set(Gf.Quatf(float(rot[3]), float(rot[0]), float(rot[1]), float(rot[2])), time)
35
- if scale is not None:
36
- xform_ops[2].Set(Gf.Vec3d(float(scale[0]), float(scale[1]), float(scale[2])), time)
37
-
38
-
39
- # transforms a cylinder such that it connects the two points pos0, pos1
40
- def _compute_segment_xform(pos0, pos1):
41
- from pxr import Gf
42
-
43
- mid = (pos0 + pos1) * 0.5
44
- height = (pos1 - pos0).GetLength()
45
-
46
- dir = (pos1 - pos0) / height
47
-
48
- rot = Gf.Rotation()
49
- rot.SetRotateInto((0.0, 0.0, 1.0), Gf.Vec3d(dir))
50
-
51
- scale = Gf.Vec3f(1.0, 1.0, height)
52
-
53
- return (mid, Gf.Quath(rot.GetQuat()), scale)
54
-
55
-
56
- class UsdRenderer:
57
- """A USD renderer"""
58
-
59
- def __init__(self, stage, up_axis="Y", fps=60, scaling=1.0):
60
- """Construct a UsdRenderer object
61
-
62
- Args:
63
- model: A simulation model
64
- stage (str/Usd.Stage): A USD stage (either in memory or on disk)
65
- up_axis (str): The upfacing axis of the stage
66
- fps: The number of frames per second to use in the USD file
67
- scaling: Scaling factor to use for the entities in the scene
68
- """
69
- try:
70
- from pxr import Usd, UsdGeom, UsdLux, Sdf, Gf
71
- except ImportError:
72
- raise ImportError("Failed to import pxr. Please install USD (e.g. via `pip install usd-core`).")
73
-
74
- if isinstance(stage, str):
75
- self.stage = stage = Usd.Stage.CreateNew(stage)
76
- elif isinstance(stage, Usd.Stage):
77
- self.stage = stage
78
- else:
79
- print("Failed to create stage in renderer. Please construct with stage path or stage object.")
80
- self.up_axis = up_axis.upper()
81
- self.fps = float(fps)
82
- self.time = 0.0
83
-
84
- self.draw_points = True
85
- self.draw_springs = False
86
- self.draw_triangles = False
87
-
88
- self.root = UsdGeom.Xform.Define(stage, "/root")
89
-
90
- # mapping from shape ID to UsdGeom class
91
- self._shape_constructors = {}
92
- # optional scaling applied to shape instances (e.g. cubes)
93
- self._shape_custom_scale = {}
94
-
95
- # apply scaling
96
- self.root.ClearXformOpOrder()
97
- s = self.root.AddScaleOp()
98
- s.Set(Gf.Vec3d(float(scaling), float(scaling), float(scaling)), 0.0)
99
-
100
- self.stage.SetDefaultPrim(self.root.GetPrim())
101
- self.stage.SetStartTimeCode(0.0)
102
- self.stage.SetEndTimeCode(0.0)
103
- self.stage.SetTimeCodesPerSecond(self.fps)
104
-
105
- if up_axis == "X":
106
- UsdGeom.SetStageUpAxis(self.stage, UsdGeom.Tokens.x)
107
- elif up_axis == "Y":
108
- UsdGeom.SetStageUpAxis(self.stage, UsdGeom.Tokens.y)
109
- elif up_axis == "Z":
110
- UsdGeom.SetStageUpAxis(self.stage, UsdGeom.Tokens.z)
111
-
112
- dome_light = UsdLux.DomeLight.Define(stage, "/dome_light")
113
- dome_light.AddRotateXYZOp().Set((-90.0, -30.0, 0.0))
114
- dome_light.GetEnableColorTemperatureAttr().Set(True)
115
- dome_light.GetColorTemperatureAttr().Set(6150.0)
116
- dome_light.GetIntensityAttr().Set(1.0)
117
- dome_light.GetExposureAttr().Set(9.0)
118
- dome_light.GetPrim().CreateAttribute("visibleInPrimaryRay", Sdf.ValueTypeNames.Bool).Set(False)
119
-
120
- distant_light = UsdLux.DistantLight.Define(stage, "/distant_light")
121
- distant_light.AddRotateXYZOp().Set((-35.0, 45.0, 0.0))
122
- distant_light.GetEnableColorTemperatureAttr().Set(True)
123
- distant_light.GetColorTemperatureAttr().Set(7250.0)
124
- distant_light.GetIntensityAttr().Set(1.0)
125
- distant_light.GetExposureAttr().Set(10.0)
126
-
127
- def begin_frame(self, time):
128
- self.time = round(time * self.fps)
129
- self.stage.SetEndTimeCode(self.time)
130
-
131
- def end_frame(self):
132
- pass
133
-
134
- def register_body(self, body_name):
135
- from pxr import UsdGeom
136
-
137
- xform = UsdGeom.Xform.Define(self.stage, self.root.GetPath().AppendChild(body_name))
138
-
139
- _usd_add_xform(xform)
140
-
141
- def _resolve_path(self, name, parent_body=None, is_template=False):
142
- # resolve the path to the prim with the given name and optional parent body
143
- if is_template:
144
- return self.root.GetPath().AppendChild("_template_shapes").AppendChild(name)
145
- if parent_body is None:
146
- return self.root.GetPath().AppendChild(name)
147
- else:
148
- return self.root.GetPath().AppendChild(parent_body).AppendChild(name)
149
-
150
- def add_shape_instance(
151
- self,
152
- name: str,
153
- shape,
154
- body,
155
- pos: tuple,
156
- rot: tuple,
157
- scale: tuple = (1.0, 1.0, 1.0),
158
- color: tuple = (1.0, 1.0, 1.0),
159
- custom_index: int = -1,
160
- visible: bool = True,
161
- ):
162
- if not visible:
163
- return
164
- sdf_path = self._resolve_path(name, body)
165
- instance = self._shape_constructors[shape.name].Define(self.stage, sdf_path)
166
- instance.GetPrim().GetReferences().AddInternalReference(shape)
167
-
168
- _usd_add_xform(instance)
169
- if shape.name in self._shape_custom_scale:
170
- cs = self._shape_custom_scale[shape.name]
171
- scale = (scale[0] * cs[0], scale[1] * cs[1], scale[2] * cs[2])
172
- _usd_set_xform(instance, pos, rot, scale, self.time)
173
-
174
- def render_plane(
175
- self,
176
- name: str,
177
- pos: tuple,
178
- rot: tuple,
179
- width: float,
180
- length: float,
181
- color: tuple = None,
182
- parent_body: str = None,
183
- is_template: bool = False,
184
- ):
185
- """
186
- Render a plane with the given dimensions.
187
-
188
- Args:
189
- name: Name of the plane
190
- pos: Position of the plane
191
- rot: Rotation of the plane
192
- width: Width of the plane
193
- length: Length of the plane
194
- color: Color of the plane
195
- parent_body: Name of the parent body
196
- is_template: Whether the plane is a template
197
- """
198
- from pxr import UsdGeom, Sdf
199
-
200
- if is_template:
201
- prim_path = self._resolve_path(name, parent_body, is_template)
202
- blueprint = UsdGeom.Scope.Define(self.stage, prim_path)
203
- blueprint_prim = blueprint.GetPrim()
204
- blueprint_prim.SetInstanceable(True)
205
- blueprint_prim.SetSpecifier(Sdf.SpecifierClass)
206
- plane_path = prim_path.AppendChild("plane")
207
- else:
208
- plane_path = self._resolve_path(name, parent_body)
209
- prim_path = plane_path
210
-
211
- plane = UsdGeom.Mesh.Get(self.stage, plane_path)
212
- if not plane:
213
- plane = UsdGeom.Mesh.Define(self.stage, plane_path)
214
- plane.CreateDoubleSidedAttr().Set(True)
215
-
216
- width = width if width > 0.0 else 100.0
217
- length = length if length > 0.0 else 100.0
218
- points = ((-width, 0.0, -length), (width, 0.0, -length), (width, 0.0, length), (-width, 0.0, length))
219
- 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))
220
- counts = (4,)
221
- indices = [0, 1, 2, 3]
222
-
223
- plane.GetPointsAttr().Set(points)
224
- plane.GetNormalsAttr().Set(normals)
225
- plane.GetFaceVertexCountsAttr().Set(counts)
226
- plane.GetFaceVertexIndicesAttr().Set(indices)
227
- _usd_add_xform(plane)
228
-
229
- self._shape_constructors[name] = UsdGeom.Mesh
230
-
231
- if not is_template:
232
- _usd_set_xform(plane, pos, rot, (1.0, 1.0, 1.0), 0.0)
233
-
234
- return prim_path
235
-
236
- def render_ground(self, size: float = 100.0, plane=None):
237
- from pxr import UsdGeom
238
-
239
- mesh = UsdGeom.Mesh.Define(self.stage, self.root.GetPath().AppendChild("ground"))
240
- mesh.CreateDoubleSidedAttr().Set(True)
241
-
242
- if self.up_axis == "X":
243
- points = ((0.0, -size, -size), (0.0, size, -size), (0.0, size, size), (0.0, -size, size))
244
- 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))
245
- elif self.up_axis == "Y":
246
- points = ((-size, 0.0, -size), (size, 0.0, -size), (size, 0.0, size), (-size, 0.0, size))
247
- 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))
248
- elif self.up_axis == "Z":
249
- points = ((-size, -size, 0.0), (size, -size, 0.0), (size, size, 0.0), (-size, size, 0.0))
250
- 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))
251
- if plane is not None:
252
- normal = np.array(plane[:3])
253
- normal /= np.linalg.norm(normal)
254
- pos = plane[3] * normal
255
- axis_up = [0.0, 0.0, 0.0]
256
- axis_up["XYZ".index(self.up_axis)] = 1.0
257
- if np.allclose(normal, axis_up):
258
- # no rotation necessary
259
- q = (0.0, 0.0, 0.0, 1.0)
260
- else:
261
- c = np.cross(normal, axis_up)
262
- angle = np.arcsin(np.linalg.norm(c))
263
- axis = np.abs(c) / np.linalg.norm(c)
264
- q = wp.quat_from_axis_angle(axis, angle)
265
- tf = wp.transform(pos, q)
266
- points = [wp.transform_point(tf, p) for p in points]
267
- normals = [wp.transform_vector(tf, n) for n in normals]
268
- counts = (4,)
269
- indices = [0, 1, 2, 3]
270
-
271
- mesh.GetPointsAttr().Set(points)
272
- mesh.GetNormalsAttr().Set(normals)
273
- mesh.GetFaceVertexCountsAttr().Set(counts)
274
- mesh.GetFaceVertexIndicesAttr().Set(indices)
275
-
276
- def render_sphere(
277
- self, name: str, pos: tuple, rot: tuple, radius: float, parent_body: str = None, is_template: bool = False, color: tuple = None
278
- ):
279
- """Debug helper to add a sphere for visualization
280
-
281
- Args:
282
- pos: The position of the sphere
283
- radius: The radius of the sphere
284
- name: A name for the USD prim on the stage
285
- color: The color of the sphere
286
- """
287
-
288
- from pxr import Gf, UsdGeom, Sdf
289
-
290
- if is_template:
291
- prim_path = self._resolve_path(name, parent_body, is_template)
292
- blueprint = UsdGeom.Scope.Define(self.stage, prim_path)
293
- blueprint_prim = blueprint.GetPrim()
294
- blueprint_prim.SetInstanceable(True)
295
- blueprint_prim.SetSpecifier(Sdf.SpecifierClass)
296
- sphere_path = prim_path.AppendChild("sphere")
297
- else:
298
- sphere_path = self._resolve_path(name, parent_body)
299
- prim_path = sphere_path
300
-
301
- sphere = UsdGeom.Sphere.Get(self.stage, sphere_path)
302
- if not sphere:
303
- sphere = UsdGeom.Sphere.Define(self.stage, sphere_path)
304
- _usd_add_xform(sphere)
305
-
306
- sphere.GetRadiusAttr().Set(radius, self.time)
307
-
308
- if color is not None:
309
- sphere.GetDisplayColorAttr().Set([Gf.Vec3f(color)], self.time)
310
-
311
- self._shape_constructors[name] = UsdGeom.Sphere
312
-
313
- if not is_template:
314
- _usd_set_xform(sphere, pos, rot, (1.0, 1.0, 1.0), self.time)
315
-
316
- return prim_path
317
-
318
- def render_capsule(
319
- self,
320
- name: str,
321
- pos: tuple,
322
- rot: tuple,
323
- radius: float,
324
- half_height: float,
325
- parent_body: str = None,
326
- is_template: bool = False,
327
- color: tuple = None,
328
- ):
329
- """
330
- Debug helper to add a capsule for visualization
331
-
332
- Args:
333
- pos: The position of the capsule
334
- radius: The radius of the capsule
335
- half_height: The half height of the capsule
336
- name: A name for the USD prim on the stage
337
- color: The color of the capsule
338
- """
339
-
340
- from pxr import Gf, UsdGeom, Sdf
341
-
342
- if is_template:
343
- prim_path = self._resolve_path(name, parent_body, is_template)
344
- blueprint = UsdGeom.Scope.Define(self.stage, prim_path)
345
- blueprint_prim = blueprint.GetPrim()
346
- blueprint_prim.SetInstanceable(True)
347
- blueprint_prim.SetSpecifier(Sdf.SpecifierClass)
348
- capsule_path = prim_path.AppendChild("capsule")
349
- else:
350
- capsule_path = self._resolve_path(name, parent_body)
351
- prim_path = capsule_path
352
-
353
- capsule = UsdGeom.Capsule.Get(self.stage, capsule_path)
354
- if not capsule:
355
- capsule = UsdGeom.Capsule.Define(self.stage, capsule_path)
356
- _usd_add_xform(capsule)
357
-
358
- capsule.GetRadiusAttr().Set(float(radius))
359
- capsule.GetHeightAttr().Set(float(half_height * 2.0))
360
- capsule.GetAxisAttr().Set("Y")
361
-
362
- if color is not None:
363
- capsule.GetDisplayColorAttr().Set([Gf.Vec3f(color)], self.time)
364
-
365
- self._shape_constructors[name] = UsdGeom.Capsule
366
-
367
- if not is_template:
368
- _usd_set_xform(capsule, pos, rot, (1.0, 1.0, 1.0), self.time)
369
-
370
- return prim_path
371
-
372
- def render_cylinder(
373
- self,
374
- name: str,
375
- pos: tuple,
376
- rot: tuple,
377
- radius: float,
378
- half_height: float,
379
- parent_body: str = None,
380
- is_template: bool = False,
381
- color: tuple = None,
382
- ):
383
- """
384
- Debug helper to add a cylinder for visualization
385
-
386
- Args:
387
- pos: The position of the cylinder
388
- radius: The radius of the cylinder
389
- half_height: The half height of the cylinder
390
- name: A name for the USD prim on the stage
391
- color: The color of the cylinder
392
- """
393
-
394
- from pxr import Gf, UsdGeom, Sdf
395
-
396
- if is_template:
397
- prim_path = self._resolve_path(name, parent_body, is_template)
398
- blueprint = UsdGeom.Scope.Define(self.stage, prim_path)
399
- blueprint_prim = blueprint.GetPrim()
400
- blueprint_prim.SetInstanceable(True)
401
- blueprint_prim.SetSpecifier(Sdf.SpecifierClass)
402
- cylinder_path = prim_path.AppendChild("cylinder")
403
- else:
404
- cylinder_path = self._resolve_path(name, parent_body)
405
- prim_path = cylinder_path
406
-
407
- cylinder = UsdGeom.Cylinder.Get(self.stage, cylinder_path)
408
- if not cylinder:
409
- cylinder = UsdGeom.Cylinder.Define(self.stage, cylinder_path)
410
- _usd_add_xform(cylinder)
411
-
412
- cylinder.GetRadiusAttr().Set(float(radius))
413
- cylinder.GetHeightAttr().Set(float(half_height * 2.0))
414
- cylinder.GetAxisAttr().Set("Y")
415
-
416
- if color is not None:
417
- cylinder.GetDisplayColorAttr().Set([Gf.Vec3f(color)], self.time)
418
-
419
- self._shape_constructors[name] = UsdGeom.Cylinder
420
-
421
- if not is_template:
422
- _usd_set_xform(cylinder, pos, rot, (1.0, 1.0, 1.0), self.time)
423
-
424
- return prim_path
425
-
426
- def render_cone(
427
- self,
428
- name: str,
429
- pos: tuple,
430
- rot: tuple,
431
- radius: float,
432
- half_height: float,
433
- parent_body: str = None,
434
- is_template: bool = False,
435
- color: tuple = None,
436
- ):
437
- """
438
- Debug helper to add a cone for visualization
439
-
440
- Args:
441
- pos: The position of the cone
442
- radius: The radius of the cone
443
- half_height: The half height of the cone
444
- name: A name for the USD prim on the stage
445
- color: The color of the cone
446
- """
447
-
448
- from pxr import Gf, UsdGeom, Sdf
449
-
450
- if is_template:
451
- prim_path = self._resolve_path(name, parent_body, is_template)
452
- blueprint = UsdGeom.Scope.Define(self.stage, prim_path)
453
- blueprint_prim = blueprint.GetPrim()
454
- blueprint_prim.SetInstanceable(True)
455
- blueprint_prim.SetSpecifier(Sdf.SpecifierClass)
456
- cone_path = prim_path.AppendChild("cone")
457
- else:
458
- cone_path = self._resolve_path(name, parent_body)
459
- prim_path = cone_path
460
-
461
- cone = UsdGeom.Cone.Get(self.stage, cone_path)
462
- if not cone:
463
- cone = UsdGeom.Cone.Define(self.stage, cone_path)
464
- _usd_add_xform(cone)
465
-
466
- cone.GetRadiusAttr().Set(float(radius))
467
- cone.GetHeightAttr().Set(float(half_height * 2.0))
468
- cone.GetAxisAttr().Set("Y")
469
-
470
- if color is not None:
471
- cone.GetDisplayColorAttr().Set([Gf.Vec3f(color)], self.time)
472
-
473
- self._shape_constructors[name] = UsdGeom.Cone
474
-
475
- if not is_template:
476
- _usd_set_xform(cone, pos, rot, (1.0, 1.0, 1.0), self.time)
477
-
478
- return prim_path
479
-
480
- def render_box(
481
- self, name: str, pos: tuple, rot: tuple, extents: tuple, parent_body: str = None, is_template: bool = False, color: tuple = None
482
- ):
483
- """Debug helper to add a box for visualization
484
-
485
- Args:
486
- pos: The position of the box
487
- extents: The radius of the box
488
- name: A name for the USD prim on the stage
489
- color: The color of the box
490
- """
491
-
492
- from pxr import UsdGeom, Sdf, Gf
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
- cube_path = prim_path.AppendChild("cube")
501
- else:
502
- cube_path = self._resolve_path(name, parent_body)
503
- prim_path = cube_path
504
-
505
- cube = UsdGeom.Cube.Get(self.stage, cube_path)
506
- if not cube:
507
- cube = UsdGeom.Cube.Define(self.stage, cube_path)
508
- _usd_add_xform(cube)
509
-
510
- if color is not None:
511
- cube.GetDisplayColorAttr().Set([Gf.Vec3f(color)], self.time)
512
-
513
- self._shape_constructors[name] = UsdGeom.Cube
514
- self._shape_custom_scale[name] = extents
515
-
516
- if not is_template:
517
- _usd_set_xform(cube, pos, rot, extents, self.time)
518
-
519
- return prim_path
520
-
521
- def render_ref(self, name: str, path: str, pos: tuple, rot: tuple, scale: tuple):
522
- from pxr import UsdGeom
523
-
524
- ref_path = "/root/" + name
525
-
526
- ref = UsdGeom.Xform.Get(self.stage, ref_path)
527
- if not ref:
528
- ref = UsdGeom.Xform.Define(self.stage, ref_path)
529
- ref.GetPrim().GetReferences().AddReference(path)
530
- _usd_add_xform(ref)
531
-
532
- # update transform
533
- _usd_set_xform(ref, pos, rot, scale, self.time)
534
-
535
- def render_mesh(
536
- self,
537
- name: str,
538
- points,
539
- indices,
540
- colors=None,
541
- pos=(0.0, 0.0, 0.0),
542
- rot=(0.0, 0.0, 0.0, 1.0),
543
- scale=(1.0, 1.0, 1.0),
544
- update_topology=False,
545
- parent_body: str = None,
546
- is_template: bool = False,
547
- ):
548
- from pxr import UsdGeom, Sdf
549
-
550
- if is_template:
551
- prim_path = self._resolve_path(name, parent_body, is_template)
552
- blueprint = UsdGeom.Scope.Define(self.stage, prim_path)
553
- blueprint_prim = blueprint.GetPrim()
554
- blueprint_prim.SetInstanceable(True)
555
- blueprint_prim.SetSpecifier(Sdf.SpecifierClass)
556
- mesh_path = prim_path.AppendChild("mesh")
557
- else:
558
- mesh_path = self._resolve_path(name, parent_body)
559
- prim_path = mesh_path
560
-
561
- mesh = UsdGeom.Mesh.Get(self.stage, mesh_path)
562
- if not mesh:
563
- mesh = UsdGeom.Mesh.Define(self.stage, mesh_path)
564
- UsdGeom.Primvar(mesh.GetDisplayColorAttr()).SetInterpolation("vertex")
565
- _usd_add_xform(mesh)
566
-
567
- # force topology update on first frame
568
- update_topology = True
569
-
570
- mesh.GetPointsAttr().Set(points, self.time)
571
-
572
- if update_topology:
573
- idxs = np.array(indices).reshape(-1, 3)
574
- mesh.GetFaceVertexIndicesAttr().Set(idxs, self.time)
575
- mesh.GetFaceVertexCountsAttr().Set([3] * len(idxs), self.time)
576
-
577
- if colors:
578
- mesh.GetDisplayColorAttr().Set(colors, self.time)
579
-
580
- self._shape_constructors[name] = UsdGeom.Mesh
581
- self._shape_custom_scale[name] = scale
582
-
583
- if not is_template:
584
- _usd_set_xform(mesh, pos, rot, scale, self.time)
585
-
586
- return prim_path
587
-
588
- def render_line_list(self, name, vertices, indices, color, radius):
589
- """Debug helper to add a line list as a set of capsules
590
-
591
- Args:
592
- vertices: The vertices of the line-strip
593
- color: The color of the line
594
- time: The time to update at
595
- """
596
-
597
- from pxr import UsdGeom, Gf
598
-
599
- num_lines = int(len(indices) / 2)
600
-
601
- if num_lines < 1:
602
- return
603
-
604
- # look up rope point instancer
605
- instancer_path = self.root.GetPath().AppendChild(name)
606
- instancer = UsdGeom.PointInstancer.Get(self.stage, instancer_path)
607
-
608
- if not instancer:
609
- instancer = UsdGeom.PointInstancer.Define(self.stage, instancer_path)
610
- instancer_capsule = UsdGeom.Capsule.Define(self.stage, instancer.GetPath().AppendChild("capsule"))
611
- instancer_capsule.GetRadiusAttr().Set(radius)
612
- instancer.CreatePrototypesRel().SetTargets([instancer_capsule.GetPath()])
613
- # instancer.CreatePrimvar("displayColor", Sdf.ValueTypeNames.Float3Array, "constant", 1)
614
-
615
- line_positions = []
616
- line_rotations = []
617
- line_scales = []
618
-
619
- for i in range(num_lines):
620
- pos0 = vertices[indices[i * 2 + 0]]
621
- pos1 = vertices[indices[i * 2 + 1]]
622
-
623
- (pos, rot, scale) = _compute_segment_xform(
624
- Gf.Vec3f(float(pos0[0]), float(pos0[1]), float(pos0[2])),
625
- Gf.Vec3f(float(pos1[0]), float(pos1[1]), float(pos1[2])),
626
- )
627
-
628
- line_positions.append(pos)
629
- line_rotations.append(rot)
630
- line_scales.append(scale)
631
- # line_colors.append(Gf.Vec3f((float(i)/num_lines, 0.5, 0.5)))
632
-
633
- instancer.GetPositionsAttr().Set(line_positions, self.time)
634
- instancer.GetOrientationsAttr().Set(line_rotations, self.time)
635
- instancer.GetScalesAttr().Set(line_scales, self.time)
636
- instancer.GetProtoIndicesAttr().Set([0] * num_lines, self.time)
637
-
638
- # instancer.GetPrimvar("displayColor").Set(line_colors, time)
639
-
640
- def render_line_strip(self, name: str, vertices, color: tuple, radius: float = 0.01):
641
- from pxr import UsdGeom, Gf
642
-
643
- num_lines = int(len(vertices) - 1)
644
-
645
- if num_lines < 1:
646
- return
647
-
648
- # look up rope point instancer
649
- instancer_path = self.root.GetPath().AppendChild(name)
650
- instancer = UsdGeom.PointInstancer.Get(self.stage, instancer_path)
651
-
652
- if not instancer:
653
- instancer = UsdGeom.PointInstancer.Define(self.stage, instancer_path)
654
- instancer_capsule = UsdGeom.Capsule.Define(self.stage, instancer.GetPath().AppendChild("capsule"))
655
- instancer_capsule.GetRadiusAttr().Set(radius)
656
- instancer.CreatePrototypesRel().SetTargets([instancer_capsule.GetPath()])
657
-
658
- line_positions = []
659
- line_rotations = []
660
- line_scales = []
661
-
662
- for i in range(num_lines):
663
- pos0 = vertices[i]
664
- pos1 = vertices[i + 1]
665
-
666
- (pos, rot, scale) = _compute_segment_xform(
667
- Gf.Vec3f(float(pos0[0]), float(pos0[1]), float(pos0[2])),
668
- Gf.Vec3f(float(pos1[0]), float(pos1[1]), float(pos1[2])),
669
- )
670
-
671
- line_positions.append(pos)
672
- line_rotations.append(rot)
673
- line_scales.append(scale)
674
-
675
- instancer.GetPositionsAttr().Set(line_positions, self.time)
676
- instancer.GetOrientationsAttr().Set(line_rotations, self.time)
677
- instancer.GetScalesAttr().Set(line_scales, self.time)
678
- instancer.GetProtoIndicesAttr().Set([0] * num_lines, self.time)
679
-
680
- instancer_capsule = UsdGeom.Capsule.Get(self.stage, instancer.GetPath().AppendChild("capsule"))
681
- instancer_capsule.GetDisplayColorAttr().Set([Gf.Vec3f(color)], self.time)
682
-
683
- def render_points(self, name: str, points, radius, colors=None):
684
- from pxr import UsdGeom, Gf
685
-
686
- instancer_path = self.root.GetPath().AppendChild(name)
687
- instancer = UsdGeom.PointInstancer.Get(self.stage, instancer_path)
688
- radius_is_scalar = np.isscalar(radius)
689
- if not instancer:
690
- if colors is None or len(colors) == 3:
691
- instancer = UsdGeom.PointInstancer.Define(self.stage, instancer_path)
692
- instancer_sphere = UsdGeom.Sphere.Define(self.stage, instancer.GetPath().AppendChild("sphere"))
693
- if radius_is_scalar:
694
- instancer_sphere.GetRadiusAttr().Set(radius)
695
- else:
696
- instancer_sphere.GetRadiusAttr().Set(1.0)
697
- instancer.GetScalesAttr().Set(np.tile(radius, (3, 1)).T)
698
-
699
- if colors is not None:
700
- instancer_sphere.GetDisplayColorAttr().Set([Gf.Vec3f(colors)], self.time)
701
-
702
- instancer.CreatePrototypesRel().SetTargets([instancer_sphere.GetPath()])
703
- instancer.CreateProtoIndicesAttr().Set([0] * len(points))
704
-
705
- # set identity rotations
706
- quats = [Gf.Quath(1.0, 0.0, 0.0, 0.0)] * len(points)
707
- instancer.GetOrientationsAttr().Set(quats, self.time)
708
- else:
709
- from pxr import Sdf
710
-
711
- instancer = UsdGeom.Points.Define(self.stage, instancer_path)
712
-
713
- if radius_is_scalar:
714
- instancer.GetWidthsAttr().Set([radius * 2.0] * len(points))
715
- else:
716
- instancer.GetWidthsAttr().Set(radius * 2.0)
717
-
718
- if colors is None or len(colors) == 3:
719
- instancer.GetPositionsAttr().Set(points, self.time)
720
- else:
721
- instancer.GetPointsAttr().Set(points, self.time)
722
- instancer.GetDisplayColorAttr().Set(colors, self.time)
723
-
724
- def update_body_transforms(self, body_q):
725
- from pxr import UsdGeom, Sdf
726
-
727
- if isinstance(body_q, wp.array):
728
- body_q = body_q.numpy()
729
-
730
- with Sdf.ChangeBlock():
731
- for b in range(self.model.body_count):
732
- node_name = self.body_names[b]
733
- node = UsdGeom.Xform(self.stage.GetPrimAtPath(self.root.GetPath().AppendChild(node_name)))
734
-
735
- # unpack rigid transform
736
- X_sb = wp.transform_expand(body_q[b])
737
-
738
- _usd_set_xform(node, X_sb.p, X_sb.q, (1.0, 1.0, 1.0), self.time)
739
-
740
- def save(self):
741
- try:
742
- self.stage.Save()
743
- except Exception as e:
744
- print("Failed to save USD stage:", e)
745
- return False
746
-
747
- file_path = self.stage.GetRootLayer().realPath
748
- print(f"Saved the USD stage file at `{file_path}`")
749
- return True
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 numpy as np
9
+
10
+ import warp as wp
11
+
12
+
13
+ def _usd_add_xform(prim):
14
+ from pxr import UsdGeom
15
+
16
+ prim = UsdGeom.Xform(prim)
17
+ prim.ClearXformOpOrder()
18
+
19
+ prim.AddTranslateOp()
20
+ prim.AddOrientOp()
21
+ prim.AddScaleOp()
22
+
23
+
24
+ def _usd_set_xform(xform, pos: tuple, rot: tuple, scale: tuple, time):
25
+ from pxr import Gf, UsdGeom
26
+
27
+ xform = UsdGeom.Xform(xform)
28
+
29
+ xform_ops = xform.GetOrderedXformOps()
30
+
31
+ if pos is not None:
32
+ xform_ops[0].Set(Gf.Vec3d(float(pos[0]), float(pos[1]), float(pos[2])), time)
33
+ if rot is not None:
34
+ xform_ops[1].Set(Gf.Quatf(float(rot[3]), float(rot[0]), float(rot[1]), float(rot[2])), time)
35
+ if scale is not None:
36
+ xform_ops[2].Set(Gf.Vec3d(float(scale[0]), float(scale[1]), float(scale[2])), time)
37
+
38
+
39
+ # transforms a cylinder such that it connects the two points pos0, pos1
40
+ def _compute_segment_xform(pos0, pos1):
41
+ from pxr import Gf
42
+
43
+ mid = (pos0 + pos1) * 0.5
44
+ height = (pos1 - pos0).GetLength()
45
+
46
+ dir = (pos1 - pos0) / height
47
+
48
+ rot = Gf.Rotation()
49
+ rot.SetRotateInto((0.0, 0.0, 1.0), Gf.Vec3d(dir))
50
+
51
+ scale = Gf.Vec3f(1.0, 1.0, height)
52
+
53
+ return (mid, Gf.Quath(rot.GetQuat()), scale)
54
+
55
+
56
+ class UsdRenderer:
57
+ """A USD renderer"""
58
+
59
+ def __init__(self, stage, up_axis="Y", fps=60, scaling=1.0):
60
+ """Construct a UsdRenderer object
61
+
62
+ Args:
63
+ model: A simulation model
64
+ stage (str/Usd.Stage): A USD stage (either in memory or on disk)
65
+ up_axis (str): The upfacing axis of the stage
66
+ fps: The number of frames per second to use in the USD file
67
+ scaling: Scaling factor to use for the entities in the scene
68
+ """
69
+ try:
70
+ from pxr import Gf, Sdf, Usd, UsdGeom, UsdLux
71
+ except ImportError as e:
72
+ raise ImportError("Failed to import pxr. Please install USD (e.g. via `pip install usd-core`).") from e
73
+
74
+ if isinstance(stage, str):
75
+ self.stage = stage = Usd.Stage.CreateNew(stage)
76
+ elif isinstance(stage, Usd.Stage):
77
+ self.stage = stage
78
+ else:
79
+ print("Failed to create stage in renderer. Please construct with stage path or stage object.")
80
+ self.up_axis = up_axis.upper()
81
+ self.fps = float(fps)
82
+ self.time = 0.0
83
+
84
+ self.draw_points = True
85
+ self.draw_springs = False
86
+ self.draw_triangles = False
87
+
88
+ self.root = UsdGeom.Xform.Define(stage, "/root")
89
+
90
+ # mapping from shape ID to UsdGeom class
91
+ self._shape_constructors = {}
92
+ # optional scaling applied to shape instances (e.g. cubes)
93
+ self._shape_custom_scale = {}
94
+
95
+ # apply scaling
96
+ self.root.ClearXformOpOrder()
97
+ s = self.root.AddScaleOp()
98
+ s.Set(Gf.Vec3d(float(scaling), float(scaling), float(scaling)), 0.0)
99
+
100
+ self.stage.SetDefaultPrim(self.root.GetPrim())
101
+ self.stage.SetStartTimeCode(0.0)
102
+ self.stage.SetEndTimeCode(0.0)
103
+ self.stage.SetTimeCodesPerSecond(self.fps)
104
+
105
+ if up_axis == "X":
106
+ UsdGeom.SetStageUpAxis(self.stage, UsdGeom.Tokens.x)
107
+ elif up_axis == "Y":
108
+ UsdGeom.SetStageUpAxis(self.stage, UsdGeom.Tokens.y)
109
+ elif up_axis == "Z":
110
+ UsdGeom.SetStageUpAxis(self.stage, UsdGeom.Tokens.z)
111
+
112
+ dome_light = UsdLux.DomeLight.Define(stage, "/dome_light")
113
+ dome_light.AddRotateXYZOp().Set((-90.0, -30.0, 0.0))
114
+ dome_light.GetEnableColorTemperatureAttr().Set(True)
115
+ dome_light.GetColorTemperatureAttr().Set(6150.0)
116
+ dome_light.GetIntensityAttr().Set(1.0)
117
+ dome_light.GetExposureAttr().Set(9.0)
118
+ dome_light.GetPrim().CreateAttribute("visibleInPrimaryRay", Sdf.ValueTypeNames.Bool).Set(False)
119
+
120
+ distant_light = UsdLux.DistantLight.Define(stage, "/distant_light")
121
+ distant_light.AddRotateXYZOp().Set((-35.0, 45.0, 0.0))
122
+ distant_light.GetEnableColorTemperatureAttr().Set(True)
123
+ distant_light.GetColorTemperatureAttr().Set(7250.0)
124
+ distant_light.GetIntensityAttr().Set(1.0)
125
+ distant_light.GetExposureAttr().Set(10.0)
126
+
127
+ def begin_frame(self, time):
128
+ self.time = round(time * self.fps)
129
+ self.stage.SetEndTimeCode(self.time)
130
+
131
+ def end_frame(self):
132
+ pass
133
+
134
+ def register_body(self, body_name):
135
+ from pxr import UsdGeom
136
+
137
+ xform = UsdGeom.Xform.Define(self.stage, self.root.GetPath().AppendChild(body_name))
138
+
139
+ _usd_add_xform(xform)
140
+
141
+ def _resolve_path(self, name, parent_body=None, is_template=False):
142
+ # resolve the path to the prim with the given name and optional parent body
143
+ if is_template:
144
+ return self.root.GetPath().AppendChild("_template_shapes").AppendChild(name)
145
+ if parent_body is None:
146
+ return self.root.GetPath().AppendChild(name)
147
+ else:
148
+ return self.root.GetPath().AppendChild(parent_body).AppendChild(name)
149
+
150
+ def add_shape_instance(
151
+ self,
152
+ name: str,
153
+ shape,
154
+ body,
155
+ pos: tuple,
156
+ rot: tuple,
157
+ scale: tuple = (1.0, 1.0, 1.0),
158
+ color: tuple = (1.0, 1.0, 1.0),
159
+ custom_index: int = -1,
160
+ visible: bool = True,
161
+ ):
162
+ if not visible:
163
+ return
164
+ sdf_path = self._resolve_path(name, body)
165
+ instance = self._shape_constructors[shape.name].Define(self.stage, sdf_path)
166
+ instance.GetPrim().GetReferences().AddInternalReference(shape)
167
+
168
+ _usd_add_xform(instance)
169
+ if shape.name in self._shape_custom_scale:
170
+ cs = self._shape_custom_scale[shape.name]
171
+ scale = (scale[0] * cs[0], scale[1] * cs[1], scale[2] * cs[2])
172
+ _usd_set_xform(instance, pos, rot, scale, self.time)
173
+
174
+ def render_plane(
175
+ self,
176
+ name: str,
177
+ pos: tuple,
178
+ rot: tuple,
179
+ width: float,
180
+ length: float,
181
+ color: tuple = None,
182
+ parent_body: str = None,
183
+ is_template: bool = False,
184
+ ):
185
+ """
186
+ Render a plane with the given dimensions.
187
+
188
+ Args:
189
+ name: Name of the plane
190
+ pos: Position of the plane
191
+ rot: Rotation of the plane
192
+ width: Width of the plane
193
+ length: Length of the plane
194
+ color: Color of the plane
195
+ parent_body: Name of the parent body
196
+ is_template: Whether the plane is a template
197
+ """
198
+ from pxr import Sdf, UsdGeom
199
+
200
+ if is_template:
201
+ prim_path = self._resolve_path(name, parent_body, is_template)
202
+ blueprint = UsdGeom.Scope.Define(self.stage, prim_path)
203
+ blueprint_prim = blueprint.GetPrim()
204
+ blueprint_prim.SetInstanceable(True)
205
+ blueprint_prim.SetSpecifier(Sdf.SpecifierClass)
206
+ plane_path = prim_path.AppendChild("plane")
207
+ else:
208
+ plane_path = self._resolve_path(name, parent_body)
209
+ prim_path = plane_path
210
+
211
+ plane = UsdGeom.Mesh.Get(self.stage, plane_path)
212
+ if not plane:
213
+ plane = UsdGeom.Mesh.Define(self.stage, plane_path)
214
+ plane.CreateDoubleSidedAttr().Set(True)
215
+
216
+ width = width if width > 0.0 else 100.0
217
+ length = length if length > 0.0 else 100.0
218
+ points = ((-width, 0.0, -length), (width, 0.0, -length), (width, 0.0, length), (-width, 0.0, length))
219
+ 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))
220
+ counts = (4,)
221
+ indices = [0, 1, 2, 3]
222
+
223
+ plane.GetPointsAttr().Set(points)
224
+ plane.GetNormalsAttr().Set(normals)
225
+ plane.GetFaceVertexCountsAttr().Set(counts)
226
+ plane.GetFaceVertexIndicesAttr().Set(indices)
227
+ _usd_add_xform(plane)
228
+
229
+ self._shape_constructors[name] = UsdGeom.Mesh
230
+
231
+ if not is_template:
232
+ _usd_set_xform(plane, pos, rot, (1.0, 1.0, 1.0), 0.0)
233
+
234
+ return prim_path
235
+
236
+ def render_ground(self, size: float = 100.0, plane=None):
237
+ from pxr import UsdGeom
238
+
239
+ mesh = UsdGeom.Mesh.Define(self.stage, self.root.GetPath().AppendChild("ground"))
240
+ mesh.CreateDoubleSidedAttr().Set(True)
241
+
242
+ if self.up_axis == "X":
243
+ points = ((0.0, size, -size), (0.0, -size, -size), (0.0, size, size), (0.0, -size, size))
244
+ 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))
245
+ elif self.up_axis == "Y":
246
+ points = ((-size, 0.0, -size), (size, 0.0, -size), (-size, 0.0, size), (size, 0.0, size))
247
+ 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))
248
+ elif self.up_axis == "Z":
249
+ points = ((-size, size, 0.0), (size, size, 0.0), (-size, -size, 0.0), (size, -size, 0.0))
250
+ 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))
251
+ if plane is not None:
252
+ normal = np.array(plane[:3])
253
+ normal /= np.linalg.norm(normal)
254
+ pos = plane[3] * normal
255
+ axis_up = [0.0, 0.0, 0.0]
256
+ axis_up["XYZ".index(self.up_axis)] = 1.0
257
+ if np.allclose(normal, axis_up):
258
+ # no rotation necessary
259
+ q = (0.0, 0.0, 0.0, 1.0)
260
+ else:
261
+ c = np.cross(normal, axis_up)
262
+ angle = np.arcsin(np.linalg.norm(c))
263
+ axis = np.abs(c) / np.linalg.norm(c)
264
+ q = wp.quat_from_axis_angle(axis, angle)
265
+ tf = wp.transform(pos, q)
266
+ points = [wp.transform_point(tf, wp.vec3(p)) for p in points]
267
+ normals = [wp.transform_vector(tf, wp.vec3(n)) for n in normals]
268
+ counts = (4,)
269
+ indices = [0, 2, 3, 1]
270
+
271
+ mesh.GetPointsAttr().Set(points)
272
+ mesh.GetNormalsAttr().Set(normals)
273
+ mesh.GetFaceVertexCountsAttr().Set(counts)
274
+ mesh.GetFaceVertexIndicesAttr().Set(indices)
275
+
276
+ def render_sphere(
277
+ self,
278
+ name: str,
279
+ pos: tuple,
280
+ rot: tuple,
281
+ radius: float,
282
+ parent_body: str = None,
283
+ is_template: bool = False,
284
+ color: tuple = None,
285
+ ):
286
+ """Debug helper to add a sphere for visualization
287
+
288
+ Args:
289
+ pos: The position of the sphere
290
+ radius: The radius of the sphere
291
+ name: A name for the USD prim on the stage
292
+ color: The color of the sphere
293
+ """
294
+
295
+ from pxr import Gf, Sdf, UsdGeom
296
+
297
+ if is_template:
298
+ prim_path = self._resolve_path(name, parent_body, is_template)
299
+ blueprint = UsdGeom.Scope.Define(self.stage, prim_path)
300
+ blueprint_prim = blueprint.GetPrim()
301
+ blueprint_prim.SetInstanceable(True)
302
+ blueprint_prim.SetSpecifier(Sdf.SpecifierClass)
303
+ sphere_path = prim_path.AppendChild("sphere")
304
+ else:
305
+ sphere_path = self._resolve_path(name, parent_body)
306
+ prim_path = sphere_path
307
+
308
+ sphere = UsdGeom.Sphere.Get(self.stage, sphere_path)
309
+ if not sphere:
310
+ sphere = UsdGeom.Sphere.Define(self.stage, sphere_path)
311
+ _usd_add_xform(sphere)
312
+
313
+ sphere.GetRadiusAttr().Set(radius, self.time)
314
+
315
+ if color is not None:
316
+ sphere.GetDisplayColorAttr().Set([Gf.Vec3f(color)], self.time)
317
+
318
+ self._shape_constructors[name] = UsdGeom.Sphere
319
+
320
+ if not is_template:
321
+ _usd_set_xform(sphere, pos, rot, (1.0, 1.0, 1.0), self.time)
322
+
323
+ return prim_path
324
+
325
+ def render_capsule(
326
+ self,
327
+ name: str,
328
+ pos: tuple,
329
+ rot: tuple,
330
+ radius: float,
331
+ half_height: float,
332
+ parent_body: str = None,
333
+ is_template: bool = False,
334
+ color: tuple = None,
335
+ ):
336
+ """
337
+ Debug helper to add a capsule for visualization
338
+
339
+ Args:
340
+ pos: The position of the capsule
341
+ radius: The radius of the capsule
342
+ half_height: The half height of the capsule
343
+ name: A name for the USD prim on the stage
344
+ color: The color of the capsule
345
+ """
346
+
347
+ from pxr import Gf, Sdf, UsdGeom
348
+
349
+ if is_template:
350
+ prim_path = self._resolve_path(name, parent_body, is_template)
351
+ blueprint = UsdGeom.Scope.Define(self.stage, prim_path)
352
+ blueprint_prim = blueprint.GetPrim()
353
+ blueprint_prim.SetInstanceable(True)
354
+ blueprint_prim.SetSpecifier(Sdf.SpecifierClass)
355
+ capsule_path = prim_path.AppendChild("capsule")
356
+ else:
357
+ capsule_path = self._resolve_path(name, parent_body)
358
+ prim_path = capsule_path
359
+
360
+ capsule = UsdGeom.Capsule.Get(self.stage, capsule_path)
361
+ if not capsule:
362
+ capsule = UsdGeom.Capsule.Define(self.stage, capsule_path)
363
+ _usd_add_xform(capsule)
364
+
365
+ capsule.GetRadiusAttr().Set(float(radius))
366
+ capsule.GetHeightAttr().Set(float(half_height * 2.0))
367
+ capsule.GetAxisAttr().Set("Y")
368
+
369
+ if color is not None:
370
+ capsule.GetDisplayColorAttr().Set([Gf.Vec3f(color)], self.time)
371
+
372
+ self._shape_constructors[name] = UsdGeom.Capsule
373
+
374
+ if not is_template:
375
+ _usd_set_xform(capsule, pos, rot, (1.0, 1.0, 1.0), self.time)
376
+
377
+ return prim_path
378
+
379
+ def render_cylinder(
380
+ self,
381
+ name: str,
382
+ pos: tuple,
383
+ rot: tuple,
384
+ radius: float,
385
+ half_height: float,
386
+ parent_body: str = None,
387
+ is_template: bool = False,
388
+ color: tuple = None,
389
+ ):
390
+ """
391
+ Debug helper to add a cylinder for visualization
392
+
393
+ Args:
394
+ pos: The position of the cylinder
395
+ radius: The radius of the cylinder
396
+ half_height: The half height of the cylinder
397
+ name: A name for the USD prim on the stage
398
+ color: The color of the cylinder
399
+ """
400
+
401
+ from pxr import Gf, Sdf, UsdGeom
402
+
403
+ if is_template:
404
+ prim_path = self._resolve_path(name, parent_body, is_template)
405
+ blueprint = UsdGeom.Scope.Define(self.stage, prim_path)
406
+ blueprint_prim = blueprint.GetPrim()
407
+ blueprint_prim.SetInstanceable(True)
408
+ blueprint_prim.SetSpecifier(Sdf.SpecifierClass)
409
+ cylinder_path = prim_path.AppendChild("cylinder")
410
+ else:
411
+ cylinder_path = self._resolve_path(name, parent_body)
412
+ prim_path = cylinder_path
413
+
414
+ cylinder = UsdGeom.Cylinder.Get(self.stage, cylinder_path)
415
+ if not cylinder:
416
+ cylinder = UsdGeom.Cylinder.Define(self.stage, cylinder_path)
417
+ _usd_add_xform(cylinder)
418
+
419
+ cylinder.GetRadiusAttr().Set(float(radius))
420
+ cylinder.GetHeightAttr().Set(float(half_height * 2.0))
421
+ cylinder.GetAxisAttr().Set("Y")
422
+
423
+ if color is not None:
424
+ cylinder.GetDisplayColorAttr().Set([Gf.Vec3f(color)], self.time)
425
+
426
+ self._shape_constructors[name] = UsdGeom.Cylinder
427
+
428
+ if not is_template:
429
+ _usd_set_xform(cylinder, pos, rot, (1.0, 1.0, 1.0), self.time)
430
+
431
+ return prim_path
432
+
433
+ def render_cone(
434
+ self,
435
+ name: str,
436
+ pos: tuple,
437
+ rot: tuple,
438
+ radius: float,
439
+ half_height: float,
440
+ parent_body: str = None,
441
+ is_template: bool = False,
442
+ color: tuple = None,
443
+ ):
444
+ """
445
+ Debug helper to add a cone for visualization
446
+
447
+ Args:
448
+ pos: The position of the cone
449
+ radius: The radius of the cone
450
+ half_height: The half height of the cone
451
+ name: A name for the USD prim on the stage
452
+ color: The color of the cone
453
+ """
454
+
455
+ from pxr import Gf, Sdf, UsdGeom
456
+
457
+ if is_template:
458
+ prim_path = self._resolve_path(name, parent_body, is_template)
459
+ blueprint = UsdGeom.Scope.Define(self.stage, prim_path)
460
+ blueprint_prim = blueprint.GetPrim()
461
+ blueprint_prim.SetInstanceable(True)
462
+ blueprint_prim.SetSpecifier(Sdf.SpecifierClass)
463
+ cone_path = prim_path.AppendChild("cone")
464
+ else:
465
+ cone_path = self._resolve_path(name, parent_body)
466
+ prim_path = cone_path
467
+
468
+ cone = UsdGeom.Cone.Get(self.stage, cone_path)
469
+ if not cone:
470
+ cone = UsdGeom.Cone.Define(self.stage, cone_path)
471
+ _usd_add_xform(cone)
472
+
473
+ cone.GetRadiusAttr().Set(float(radius))
474
+ cone.GetHeightAttr().Set(float(half_height * 2.0))
475
+ cone.GetAxisAttr().Set("Y")
476
+
477
+ if color is not None:
478
+ cone.GetDisplayColorAttr().Set([Gf.Vec3f(color)], self.time)
479
+
480
+ self._shape_constructors[name] = UsdGeom.Cone
481
+
482
+ if not is_template:
483
+ _usd_set_xform(cone, pos, rot, (1.0, 1.0, 1.0), self.time)
484
+
485
+ return prim_path
486
+
487
+ def render_box(
488
+ self,
489
+ name: str,
490
+ pos: tuple,
491
+ rot: tuple,
492
+ extents: tuple,
493
+ parent_body: str = None,
494
+ is_template: bool = False,
495
+ color: tuple = None,
496
+ ):
497
+ """Debug helper to add a box for visualization
498
+
499
+ Args:
500
+ pos: The position of the box
501
+ extents: The radius of the box
502
+ name: A name for the USD prim on the stage
503
+ color: The color of the box
504
+ """
505
+
506
+ from pxr import Gf, Sdf, UsdGeom
507
+
508
+ if is_template:
509
+ prim_path = self._resolve_path(name, parent_body, is_template)
510
+ blueprint = UsdGeom.Scope.Define(self.stage, prim_path)
511
+ blueprint_prim = blueprint.GetPrim()
512
+ blueprint_prim.SetInstanceable(True)
513
+ blueprint_prim.SetSpecifier(Sdf.SpecifierClass)
514
+ cube_path = prim_path.AppendChild("cube")
515
+ else:
516
+ cube_path = self._resolve_path(name, parent_body)
517
+ prim_path = cube_path
518
+
519
+ cube = UsdGeom.Cube.Get(self.stage, cube_path)
520
+ if not cube:
521
+ cube = UsdGeom.Cube.Define(self.stage, cube_path)
522
+ _usd_add_xform(cube)
523
+
524
+ if color is not None:
525
+ cube.GetDisplayColorAttr().Set([Gf.Vec3f(color)], self.time)
526
+
527
+ self._shape_constructors[name] = UsdGeom.Cube
528
+ self._shape_custom_scale[name] = extents
529
+
530
+ if not is_template:
531
+ _usd_set_xform(cube, pos, rot, extents, self.time)
532
+
533
+ return prim_path
534
+
535
+ def render_ref(self, name: str, path: str, pos: tuple, rot: tuple, scale: tuple, color: tuple = None):
536
+ from pxr import Gf, Usd, UsdGeom
537
+
538
+ ref_path = "/root/" + name
539
+
540
+ ref = UsdGeom.Xform.Get(self.stage, ref_path)
541
+ if not ref:
542
+ ref = UsdGeom.Xform.Define(self.stage, ref_path)
543
+ ref.GetPrim().GetReferences().AddReference(path)
544
+ _usd_add_xform(ref)
545
+
546
+ # update transform
547
+ _usd_set_xform(ref, pos, rot, scale, self.time)
548
+
549
+ if color is not None:
550
+ it = iter(Usd.PrimRange(ref.GetPrim()))
551
+ for prim in it:
552
+ if prim.IsA(UsdGeom.Gprim):
553
+ UsdGeom.Gprim(prim).GetDisplayColorAttr().Set([Gf.Vec3f(color)], self.time)
554
+ it.PruneChildren()
555
+
556
+ def render_mesh(
557
+ self,
558
+ name: str,
559
+ points,
560
+ indices,
561
+ colors=None,
562
+ pos=(0.0, 0.0, 0.0),
563
+ rot=(0.0, 0.0, 0.0, 1.0),
564
+ scale=(1.0, 1.0, 1.0),
565
+ update_topology=False,
566
+ parent_body: str = None,
567
+ is_template: bool = False,
568
+ ):
569
+ from pxr import Sdf, UsdGeom
570
+
571
+ if is_template:
572
+ prim_path = self._resolve_path(name, parent_body, is_template)
573
+ blueprint = UsdGeom.Scope.Define(self.stage, prim_path)
574
+ blueprint_prim = blueprint.GetPrim()
575
+ blueprint_prim.SetInstanceable(True)
576
+ blueprint_prim.SetSpecifier(Sdf.SpecifierClass)
577
+ mesh_path = prim_path.AppendChild("mesh")
578
+ else:
579
+ mesh_path = self._resolve_path(name, parent_body)
580
+ prim_path = mesh_path
581
+
582
+ mesh = UsdGeom.Mesh.Get(self.stage, mesh_path)
583
+ if not mesh:
584
+ mesh = UsdGeom.Mesh.Define(self.stage, mesh_path)
585
+ UsdGeom.Primvar(mesh.GetDisplayColorAttr()).SetInterpolation("vertex")
586
+ _usd_add_xform(mesh)
587
+
588
+ # force topology update on first frame
589
+ update_topology = True
590
+
591
+ mesh.GetPointsAttr().Set(points, self.time)
592
+
593
+ if update_topology:
594
+ idxs = np.array(indices).reshape(-1, 3)
595
+ mesh.GetFaceVertexIndicesAttr().Set(idxs, self.time)
596
+ mesh.GetFaceVertexCountsAttr().Set([3] * len(idxs), self.time)
597
+
598
+ if colors:
599
+ mesh.GetDisplayColorAttr().Set(colors, self.time)
600
+
601
+ self._shape_constructors[name] = UsdGeom.Mesh
602
+ self._shape_custom_scale[name] = scale
603
+
604
+ if not is_template:
605
+ _usd_set_xform(mesh, pos, rot, scale, self.time)
606
+
607
+ return prim_path
608
+
609
+ def render_line_list(self, name, vertices, indices, color, radius):
610
+ """Debug helper to add a line list as a set of capsules
611
+
612
+ Args:
613
+ vertices: The vertices of the line-strip
614
+ color: The color of the line
615
+ time: The time to update at
616
+ """
617
+
618
+ from pxr import Gf, UsdGeom
619
+
620
+ num_lines = int(len(indices) / 2)
621
+
622
+ if num_lines < 1:
623
+ return
624
+
625
+ # look up rope point instancer
626
+ instancer_path = self.root.GetPath().AppendChild(name)
627
+ instancer = UsdGeom.PointInstancer.Get(self.stage, instancer_path)
628
+
629
+ if not instancer:
630
+ instancer = UsdGeom.PointInstancer.Define(self.stage, instancer_path)
631
+ instancer_capsule = UsdGeom.Capsule.Define(self.stage, instancer.GetPath().AppendChild("capsule"))
632
+ instancer_capsule.GetRadiusAttr().Set(radius)
633
+ instancer.CreatePrototypesRel().SetTargets([instancer_capsule.GetPath()])
634
+ # instancer.CreatePrimvar("displayColor", Sdf.ValueTypeNames.Float3Array, "constant", 1)
635
+
636
+ line_positions = []
637
+ line_rotations = []
638
+ line_scales = []
639
+
640
+ for i in range(num_lines):
641
+ pos0 = vertices[indices[i * 2 + 0]]
642
+ pos1 = vertices[indices[i * 2 + 1]]
643
+
644
+ (pos, rot, scale) = _compute_segment_xform(
645
+ Gf.Vec3f(float(pos0[0]), float(pos0[1]), float(pos0[2])),
646
+ Gf.Vec3f(float(pos1[0]), float(pos1[1]), float(pos1[2])),
647
+ )
648
+
649
+ line_positions.append(pos)
650
+ line_rotations.append(rot)
651
+ line_scales.append(scale)
652
+ # line_colors.append(Gf.Vec3f((float(i)/num_lines, 0.5, 0.5)))
653
+
654
+ instancer.GetPositionsAttr().Set(line_positions, self.time)
655
+ instancer.GetOrientationsAttr().Set(line_rotations, self.time)
656
+ instancer.GetScalesAttr().Set(line_scales, self.time)
657
+ instancer.GetProtoIndicesAttr().Set([0] * num_lines, self.time)
658
+
659
+ # instancer.GetPrimvar("displayColor").Set(line_colors, time)
660
+
661
+ def render_line_strip(self, name: str, vertices, color: tuple, radius: float = 0.01):
662
+ from pxr import Gf, UsdGeom
663
+
664
+ num_lines = int(len(vertices) - 1)
665
+
666
+ if num_lines < 1:
667
+ return
668
+
669
+ # look up rope point instancer
670
+ instancer_path = self.root.GetPath().AppendChild(name)
671
+ instancer = UsdGeom.PointInstancer.Get(self.stage, instancer_path)
672
+
673
+ if not instancer:
674
+ instancer = UsdGeom.PointInstancer.Define(self.stage, instancer_path)
675
+ instancer_capsule = UsdGeom.Capsule.Define(self.stage, instancer.GetPath().AppendChild("capsule"))
676
+ instancer_capsule.GetRadiusAttr().Set(radius)
677
+ instancer.CreatePrototypesRel().SetTargets([instancer_capsule.GetPath()])
678
+
679
+ line_positions = []
680
+ line_rotations = []
681
+ line_scales = []
682
+
683
+ for i in range(num_lines):
684
+ pos0 = vertices[i]
685
+ pos1 = vertices[i + 1]
686
+
687
+ (pos, rot, scale) = _compute_segment_xform(
688
+ Gf.Vec3f(float(pos0[0]), float(pos0[1]), float(pos0[2])),
689
+ Gf.Vec3f(float(pos1[0]), float(pos1[1]), float(pos1[2])),
690
+ )
691
+
692
+ line_positions.append(pos)
693
+ line_rotations.append(rot)
694
+ line_scales.append(scale)
695
+
696
+ instancer.GetPositionsAttr().Set(line_positions, self.time)
697
+ instancer.GetOrientationsAttr().Set(line_rotations, self.time)
698
+ instancer.GetScalesAttr().Set(line_scales, self.time)
699
+ instancer.GetProtoIndicesAttr().Set([0] * num_lines, self.time)
700
+
701
+ instancer_capsule = UsdGeom.Capsule.Get(self.stage, instancer.GetPath().AppendChild("capsule"))
702
+ instancer_capsule.GetDisplayColorAttr().Set([Gf.Vec3f(color)], self.time)
703
+
704
+ def render_points(self, name: str, points, radius, colors=None):
705
+ from pxr import Gf, UsdGeom
706
+
707
+ instancer_path = self.root.GetPath().AppendChild(name)
708
+ instancer = UsdGeom.PointInstancer.Get(self.stage, instancer_path)
709
+ radius_is_scalar = np.isscalar(radius)
710
+ if not instancer:
711
+ if colors is None or len(colors) == 3:
712
+ instancer = UsdGeom.PointInstancer.Define(self.stage, instancer_path)
713
+ instancer_sphere = UsdGeom.Sphere.Define(self.stage, instancer.GetPath().AppendChild("sphere"))
714
+ if radius_is_scalar:
715
+ instancer_sphere.GetRadiusAttr().Set(radius)
716
+ else:
717
+ instancer_sphere.GetRadiusAttr().Set(1.0)
718
+ instancer.GetScalesAttr().Set(np.tile(radius, (3, 1)).T)
719
+
720
+ if colors is not None:
721
+ instancer_sphere.GetDisplayColorAttr().Set([Gf.Vec3f(colors)], self.time)
722
+
723
+ instancer.CreatePrototypesRel().SetTargets([instancer_sphere.GetPath()])
724
+ instancer.CreateProtoIndicesAttr().Set([0] * len(points))
725
+
726
+ # set identity rotations
727
+ quats = [Gf.Quath(1.0, 0.0, 0.0, 0.0)] * len(points)
728
+ instancer.GetOrientationsAttr().Set(quats, self.time)
729
+ else:
730
+ instancer = UsdGeom.Points.Define(self.stage, instancer_path)
731
+
732
+ if radius_is_scalar:
733
+ instancer.GetWidthsAttr().Set([radius * 2.0] * len(points))
734
+ else:
735
+ instancer.GetWidthsAttr().Set(radius * 2.0)
736
+
737
+ if colors is None or len(colors) == 3:
738
+ instancer.GetPositionsAttr().Set(points, self.time)
739
+ else:
740
+ instancer.GetPointsAttr().Set(points, self.time)
741
+ instancer.GetDisplayColorAttr().Set(colors, self.time)
742
+
743
+ def update_body_transforms(self, body_q):
744
+ from pxr import Sdf, UsdGeom
745
+
746
+ if isinstance(body_q, wp.array):
747
+ body_q = body_q.numpy()
748
+
749
+ with Sdf.ChangeBlock():
750
+ for b in range(self.model.body_count):
751
+ node_name = self.body_names[b]
752
+ node = UsdGeom.Xform(self.stage.GetPrimAtPath(self.root.GetPath().AppendChild(node_name)))
753
+
754
+ # unpack rigid transform
755
+ X_sb = wp.transform_expand(body_q[b])
756
+
757
+ _usd_set_xform(node, X_sb.p, X_sb.q, (1.0, 1.0, 1.0), self.time)
758
+
759
+ def save(self):
760
+ try:
761
+ self.stage.Save()
762
+ except Exception as e:
763
+ print("Failed to save USD stage:", e)
764
+ return False
765
+
766
+ file_path = self.stage.GetRootLayer().realPath
767
+ print(f"Saved the USD stage file at `{file_path}`")
768
+ return True