warp-lang 1.0.2__py3-none-win_amd64.whl → 1.2.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 (356) 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 +88 -113
  6. warp/build_dll.py +383 -375
  7. warp/builtins.py +3693 -3354
  8. warp/codegen.py +2925 -2792
  9. warp/config.py +40 -36
  10. warp/constants.py +49 -45
  11. warp/context.py +5409 -5102
  12. warp/dlpack.py +442 -442
  13. warp/examples/__init__.py +16 -16
  14. warp/examples/assets/bear.usd +0 -0
  15. warp/examples/assets/bunny.usd +0 -0
  16. warp/examples/assets/cartpole.urdf +110 -110
  17. warp/examples/assets/crazyflie.usd +0 -0
  18. warp/examples/assets/cube.usd +0 -0
  19. warp/examples/assets/nv_ant.xml +92 -92
  20. warp/examples/assets/nv_humanoid.xml +183 -183
  21. warp/examples/assets/quadruped.urdf +267 -267
  22. warp/examples/assets/rocks.nvdb +0 -0
  23. warp/examples/assets/rocks.usd +0 -0
  24. warp/examples/assets/sphere.usd +0 -0
  25. warp/examples/benchmarks/benchmark_api.py +381 -383
  26. warp/examples/benchmarks/benchmark_cloth.py +278 -277
  27. warp/examples/benchmarks/benchmark_cloth_cupy.py +88 -88
  28. warp/examples/benchmarks/benchmark_cloth_jax.py +97 -100
  29. warp/examples/benchmarks/benchmark_cloth_numba.py +146 -142
  30. warp/examples/benchmarks/benchmark_cloth_numpy.py +77 -77
  31. warp/examples/benchmarks/benchmark_cloth_pytorch.py +86 -86
  32. warp/examples/benchmarks/benchmark_cloth_taichi.py +112 -112
  33. warp/examples/benchmarks/benchmark_cloth_warp.py +145 -146
  34. warp/examples/benchmarks/benchmark_launches.py +293 -295
  35. warp/examples/browse.py +29 -29
  36. warp/examples/core/example_dem.py +232 -219
  37. warp/examples/core/example_fluid.py +291 -267
  38. warp/examples/core/example_graph_capture.py +142 -126
  39. warp/examples/core/example_marching_cubes.py +186 -174
  40. warp/examples/core/example_mesh.py +172 -155
  41. warp/examples/core/example_mesh_intersect.py +203 -193
  42. warp/examples/core/example_nvdb.py +174 -170
  43. warp/examples/core/example_raycast.py +103 -90
  44. warp/examples/core/example_raymarch.py +197 -178
  45. warp/examples/core/example_render_opengl.py +183 -141
  46. warp/examples/core/example_sph.py +403 -387
  47. warp/examples/core/example_torch.py +219 -181
  48. warp/examples/core/example_wave.py +261 -248
  49. warp/examples/fem/bsr_utils.py +378 -380
  50. warp/examples/fem/example_apic_fluid.py +432 -389
  51. warp/examples/fem/example_burgers.py +262 -0
  52. warp/examples/fem/example_convection_diffusion.py +180 -168
  53. warp/examples/fem/example_convection_diffusion_dg.py +217 -209
  54. warp/examples/fem/example_deformed_geometry.py +175 -159
  55. warp/examples/fem/example_diffusion.py +199 -173
  56. warp/examples/fem/example_diffusion_3d.py +178 -152
  57. warp/examples/fem/example_diffusion_mgpu.py +219 -214
  58. warp/examples/fem/example_mixed_elasticity.py +242 -222
  59. warp/examples/fem/example_navier_stokes.py +257 -243
  60. warp/examples/fem/example_stokes.py +218 -192
  61. warp/examples/fem/example_stokes_transfer.py +263 -249
  62. warp/examples/fem/mesh_utils.py +133 -109
  63. warp/examples/fem/plot_utils.py +292 -287
  64. warp/examples/optim/example_bounce.py +258 -246
  65. warp/examples/optim/example_cloth_throw.py +220 -209
  66. warp/examples/optim/example_diffray.py +564 -536
  67. warp/examples/optim/example_drone.py +862 -835
  68. warp/examples/optim/example_inverse_kinematics.py +174 -168
  69. warp/examples/optim/example_inverse_kinematics_torch.py +183 -169
  70. warp/examples/optim/example_spring_cage.py +237 -231
  71. warp/examples/optim/example_trajectory.py +221 -199
  72. warp/examples/optim/example_walker.py +304 -293
  73. warp/examples/sim/example_cartpole.py +137 -129
  74. warp/examples/sim/example_cloth.py +194 -186
  75. warp/examples/sim/example_granular.py +122 -111
  76. warp/examples/sim/example_granular_collision_sdf.py +195 -186
  77. warp/examples/sim/example_jacobian_ik.py +234 -214
  78. warp/examples/sim/example_particle_chain.py +116 -105
  79. warp/examples/sim/example_quadruped.py +191 -180
  80. warp/examples/sim/example_rigid_chain.py +195 -187
  81. warp/examples/sim/example_rigid_contact.py +187 -177
  82. warp/examples/sim/example_rigid_force.py +125 -125
  83. warp/examples/sim/example_rigid_gyroscopic.py +107 -95
  84. warp/examples/sim/example_rigid_soft_contact.py +132 -122
  85. warp/examples/sim/example_soft_body.py +188 -177
  86. warp/fabric.py +337 -335
  87. warp/fem/__init__.py +61 -27
  88. warp/fem/cache.py +403 -388
  89. warp/fem/dirichlet.py +178 -179
  90. warp/fem/domain.py +262 -263
  91. warp/fem/field/__init__.py +100 -101
  92. warp/fem/field/field.py +148 -149
  93. warp/fem/field/nodal_field.py +298 -299
  94. warp/fem/field/restriction.py +22 -21
  95. warp/fem/field/test.py +180 -181
  96. warp/fem/field/trial.py +183 -183
  97. warp/fem/geometry/__init__.py +16 -19
  98. warp/fem/geometry/closest_point.py +69 -70
  99. warp/fem/geometry/deformed_geometry.py +270 -271
  100. warp/fem/geometry/element.py +748 -744
  101. warp/fem/geometry/geometry.py +184 -186
  102. warp/fem/geometry/grid_2d.py +380 -373
  103. warp/fem/geometry/grid_3d.py +437 -435
  104. warp/fem/geometry/hexmesh.py +953 -953
  105. warp/fem/geometry/nanogrid.py +455 -0
  106. warp/fem/geometry/partition.py +374 -376
  107. warp/fem/geometry/quadmesh_2d.py +532 -532
  108. warp/fem/geometry/tetmesh.py +840 -840
  109. warp/fem/geometry/trimesh_2d.py +577 -577
  110. warp/fem/integrate.py +1684 -1615
  111. warp/fem/operator.py +190 -191
  112. warp/fem/polynomial.py +214 -213
  113. warp/fem/quadrature/__init__.py +2 -2
  114. warp/fem/quadrature/pic_quadrature.py +243 -245
  115. warp/fem/quadrature/quadrature.py +295 -294
  116. warp/fem/space/__init__.py +179 -292
  117. warp/fem/space/basis_space.py +522 -489
  118. warp/fem/space/collocated_function_space.py +100 -105
  119. warp/fem/space/dof_mapper.py +236 -236
  120. warp/fem/space/function_space.py +148 -145
  121. warp/fem/space/grid_2d_function_space.py +148 -267
  122. warp/fem/space/grid_3d_function_space.py +167 -306
  123. warp/fem/space/hexmesh_function_space.py +253 -352
  124. warp/fem/space/nanogrid_function_space.py +202 -0
  125. warp/fem/space/partition.py +350 -350
  126. warp/fem/space/quadmesh_2d_function_space.py +261 -369
  127. warp/fem/space/restriction.py +161 -160
  128. warp/fem/space/shape/__init__.py +90 -15
  129. warp/fem/space/shape/cube_shape_function.py +728 -738
  130. warp/fem/space/shape/shape_function.py +102 -103
  131. warp/fem/space/shape/square_shape_function.py +611 -611
  132. warp/fem/space/shape/tet_shape_function.py +565 -567
  133. warp/fem/space/shape/triangle_shape_function.py +429 -429
  134. warp/fem/space/tetmesh_function_space.py +224 -292
  135. warp/fem/space/topology.py +297 -295
  136. warp/fem/space/trimesh_2d_function_space.py +153 -221
  137. warp/fem/types.py +77 -77
  138. warp/fem/utils.py +495 -495
  139. warp/jax.py +166 -141
  140. warp/jax_experimental.py +341 -339
  141. warp/native/array.h +1081 -1025
  142. warp/native/builtin.h +1603 -1560
  143. warp/native/bvh.cpp +402 -398
  144. warp/native/bvh.cu +533 -525
  145. warp/native/bvh.h +430 -429
  146. warp/native/clang/clang.cpp +496 -464
  147. warp/native/crt.cpp +42 -32
  148. warp/native/crt.h +352 -335
  149. warp/native/cuda_crt.h +1049 -1049
  150. warp/native/cuda_util.cpp +549 -540
  151. warp/native/cuda_util.h +288 -203
  152. warp/native/cutlass_gemm.cpp +34 -34
  153. warp/native/cutlass_gemm.cu +372 -372
  154. warp/native/error.cpp +66 -66
  155. warp/native/error.h +27 -27
  156. warp/native/exports.h +187 -0
  157. warp/native/fabric.h +228 -228
  158. warp/native/hashgrid.cpp +301 -278
  159. warp/native/hashgrid.cu +78 -77
  160. warp/native/hashgrid.h +227 -227
  161. warp/native/initializer_array.h +32 -32
  162. warp/native/intersect.h +1204 -1204
  163. warp/native/intersect_adj.h +365 -365
  164. warp/native/intersect_tri.h +322 -322
  165. warp/native/marching.cpp +2 -2
  166. warp/native/marching.cu +497 -497
  167. warp/native/marching.h +2 -2
  168. warp/native/mat.h +1545 -1498
  169. warp/native/matnn.h +333 -333
  170. warp/native/mesh.cpp +203 -203
  171. warp/native/mesh.cu +292 -293
  172. warp/native/mesh.h +1887 -1887
  173. warp/native/nanovdb/GridHandle.h +366 -0
  174. warp/native/nanovdb/HostBuffer.h +590 -0
  175. warp/native/nanovdb/NanoVDB.h +6624 -4782
  176. warp/native/nanovdb/PNanoVDB.h +3390 -2553
  177. warp/native/noise.h +850 -850
  178. warp/native/quat.h +1112 -1085
  179. warp/native/rand.h +303 -299
  180. warp/native/range.h +108 -108
  181. warp/native/reduce.cpp +156 -156
  182. warp/native/reduce.cu +348 -348
  183. warp/native/runlength_encode.cpp +61 -61
  184. warp/native/runlength_encode.cu +46 -46
  185. warp/native/scan.cpp +30 -30
  186. warp/native/scan.cu +36 -36
  187. warp/native/scan.h +7 -7
  188. warp/native/solid_angle.h +442 -442
  189. warp/native/sort.cpp +94 -94
  190. warp/native/sort.cu +97 -97
  191. warp/native/sort.h +14 -14
  192. warp/native/sparse.cpp +337 -337
  193. warp/native/sparse.cu +544 -544
  194. warp/native/spatial.h +630 -630
  195. warp/native/svd.h +562 -562
  196. warp/native/temp_buffer.h +30 -30
  197. warp/native/vec.h +1177 -1133
  198. warp/native/volume.cpp +529 -297
  199. warp/native/volume.cu +58 -32
  200. warp/native/volume.h +960 -538
  201. warp/native/volume_builder.cu +446 -425
  202. warp/native/volume_builder.h +34 -19
  203. warp/native/volume_impl.h +61 -0
  204. warp/native/warp.cpp +1057 -1052
  205. warp/native/warp.cu +2949 -2828
  206. warp/native/warp.h +321 -305
  207. warp/optim/__init__.py +9 -9
  208. warp/optim/adam.py +120 -120
  209. warp/optim/linear.py +1104 -939
  210. warp/optim/sgd.py +104 -92
  211. warp/render/__init__.py +10 -10
  212. warp/render/render_opengl.py +3356 -3204
  213. warp/render/render_usd.py +768 -749
  214. warp/render/utils.py +152 -150
  215. warp/sim/__init__.py +52 -59
  216. warp/sim/articulation.py +685 -685
  217. warp/sim/collide.py +1594 -1590
  218. warp/sim/import_mjcf.py +489 -481
  219. warp/sim/import_snu.py +220 -221
  220. warp/sim/import_urdf.py +536 -516
  221. warp/sim/import_usd.py +887 -881
  222. warp/sim/inertia.py +316 -317
  223. warp/sim/integrator.py +234 -233
  224. warp/sim/integrator_euler.py +1956 -1956
  225. warp/sim/integrator_featherstone.py +1917 -1991
  226. warp/sim/integrator_xpbd.py +3288 -3312
  227. warp/sim/model.py +4473 -4314
  228. warp/sim/particles.py +113 -112
  229. warp/sim/render.py +417 -403
  230. warp/sim/utils.py +413 -410
  231. warp/sparse.py +1289 -1227
  232. warp/stubs.py +2192 -2469
  233. warp/tape.py +1162 -225
  234. warp/tests/__init__.py +1 -1
  235. warp/tests/__main__.py +4 -4
  236. warp/tests/assets/test_index_grid.nvdb +0 -0
  237. warp/tests/assets/torus.usda +105 -105
  238. warp/tests/aux_test_class_kernel.py +26 -26
  239. warp/tests/aux_test_compile_consts_dummy.py +10 -10
  240. warp/tests/aux_test_conditional_unequal_types_kernels.py +21 -21
  241. warp/tests/aux_test_dependent.py +20 -22
  242. warp/tests/aux_test_grad_customs.py +21 -23
  243. warp/tests/aux_test_reference.py +9 -11
  244. warp/tests/aux_test_reference_reference.py +8 -10
  245. warp/tests/aux_test_square.py +15 -17
  246. warp/tests/aux_test_unresolved_func.py +14 -14
  247. warp/tests/aux_test_unresolved_symbol.py +14 -14
  248. warp/tests/disabled_kinematics.py +237 -239
  249. warp/tests/run_coverage_serial.py +31 -31
  250. warp/tests/test_adam.py +155 -157
  251. warp/tests/test_arithmetic.py +1088 -1124
  252. warp/tests/test_array.py +2415 -2326
  253. warp/tests/test_array_reduce.py +148 -150
  254. warp/tests/test_async.py +666 -656
  255. warp/tests/test_atomic.py +139 -141
  256. warp/tests/test_bool.py +212 -149
  257. warp/tests/test_builtins_resolution.py +1290 -1292
  258. warp/tests/test_bvh.py +162 -171
  259. warp/tests/test_closest_point_edge_edge.py +227 -228
  260. warp/tests/test_codegen.py +562 -553
  261. warp/tests/test_compile_consts.py +217 -101
  262. warp/tests/test_conditional.py +244 -246
  263. warp/tests/test_copy.py +230 -215
  264. warp/tests/test_ctypes.py +630 -632
  265. warp/tests/test_dense.py +65 -67
  266. warp/tests/test_devices.py +89 -98
  267. warp/tests/test_dlpack.py +528 -529
  268. warp/tests/test_examples.py +403 -378
  269. warp/tests/test_fabricarray.py +952 -955
  270. warp/tests/test_fast_math.py +60 -54
  271. warp/tests/test_fem.py +1298 -1278
  272. warp/tests/test_fp16.py +128 -130
  273. warp/tests/test_func.py +336 -337
  274. warp/tests/test_generics.py +596 -571
  275. warp/tests/test_grad.py +885 -640
  276. warp/tests/test_grad_customs.py +331 -336
  277. warp/tests/test_hash_grid.py +208 -164
  278. warp/tests/test_import.py +37 -39
  279. warp/tests/test_indexedarray.py +1132 -1134
  280. warp/tests/test_intersect.py +65 -67
  281. warp/tests/test_jax.py +305 -307
  282. warp/tests/test_large.py +169 -164
  283. warp/tests/test_launch.py +352 -354
  284. warp/tests/test_lerp.py +217 -261
  285. warp/tests/test_linear_solvers.py +189 -171
  286. warp/tests/test_lvalue.py +419 -493
  287. warp/tests/test_marching_cubes.py +63 -65
  288. warp/tests/test_mat.py +1799 -1827
  289. warp/tests/test_mat_lite.py +113 -115
  290. warp/tests/test_mat_scalar_ops.py +2905 -2889
  291. warp/tests/test_math.py +124 -193
  292. warp/tests/test_matmul.py +498 -499
  293. warp/tests/test_matmul_lite.py +408 -410
  294. warp/tests/test_mempool.py +186 -190
  295. warp/tests/test_mesh.py +281 -324
  296. warp/tests/test_mesh_query_aabb.py +226 -241
  297. warp/tests/test_mesh_query_point.py +690 -702
  298. warp/tests/test_mesh_query_ray.py +290 -303
  299. warp/tests/test_mlp.py +274 -276
  300. warp/tests/test_model.py +108 -110
  301. warp/tests/test_module_hashing.py +111 -0
  302. warp/tests/test_modules_lite.py +36 -39
  303. warp/tests/test_multigpu.py +161 -163
  304. warp/tests/test_noise.py +244 -248
  305. warp/tests/test_operators.py +248 -250
  306. warp/tests/test_options.py +121 -125
  307. warp/tests/test_peer.py +131 -137
  308. warp/tests/test_pinned.py +76 -78
  309. warp/tests/test_print.py +52 -54
  310. warp/tests/test_quat.py +2084 -2086
  311. warp/tests/test_rand.py +324 -288
  312. warp/tests/test_reload.py +207 -217
  313. warp/tests/test_rounding.py +177 -179
  314. warp/tests/test_runlength_encode.py +188 -190
  315. warp/tests/test_sim_grad.py +241 -0
  316. warp/tests/test_sim_kinematics.py +89 -97
  317. warp/tests/test_smoothstep.py +166 -168
  318. warp/tests/test_snippet.py +303 -266
  319. warp/tests/test_sparse.py +466 -460
  320. warp/tests/test_spatial.py +2146 -2148
  321. warp/tests/test_special_values.py +362 -0
  322. warp/tests/test_streams.py +484 -473
  323. warp/tests/test_struct.py +708 -675
  324. warp/tests/test_tape.py +171 -148
  325. warp/tests/test_torch.py +741 -743
  326. warp/tests/test_transient_module.py +85 -87
  327. warp/tests/test_types.py +554 -659
  328. warp/tests/test_utils.py +488 -499
  329. warp/tests/test_vec.py +1262 -1268
  330. warp/tests/test_vec_lite.py +71 -73
  331. warp/tests/test_vec_scalar_ops.py +2097 -2099
  332. warp/tests/test_verify_fp.py +92 -94
  333. warp/tests/test_volume.py +961 -736
  334. warp/tests/test_volume_write.py +338 -265
  335. warp/tests/unittest_serial.py +38 -37
  336. warp/tests/unittest_suites.py +367 -359
  337. warp/tests/unittest_utils.py +434 -578
  338. warp/tests/unused_test_misc.py +69 -71
  339. warp/tests/walkthrough_debug.py +85 -85
  340. warp/thirdparty/appdirs.py +598 -598
  341. warp/thirdparty/dlpack.py +143 -143
  342. warp/thirdparty/unittest_parallel.py +563 -561
  343. warp/torch.py +321 -295
  344. warp/types.py +4941 -4450
  345. warp/utils.py +1008 -821
  346. {warp_lang-1.0.2.dist-info → warp_lang-1.2.0.dist-info}/LICENSE.md +126 -126
  347. {warp_lang-1.0.2.dist-info → warp_lang-1.2.0.dist-info}/METADATA +365 -400
  348. warp_lang-1.2.0.dist-info/RECORD +359 -0
  349. warp/examples/assets/cube.usda +0 -42
  350. warp/examples/assets/sphere.usda +0 -56
  351. warp/examples/assets/torus.usda +0 -105
  352. warp/examples/fem/example_convection_diffusion_dg0.py +0 -194
  353. warp/native/nanovdb/PNanoVDBWrite.h +0 -295
  354. warp_lang-1.0.2.dist-info/RECORD +0 -352
  355. {warp_lang-1.0.2.dist-info → warp_lang-1.2.0.dist-info}/WHEEL +0 -0
  356. {warp_lang-1.0.2.dist-info → warp_lang-1.2.0.dist-info}/top_level.txt +0 -0
warp/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