10.15. Useful Snippets

Various examples of Isaac Sim Replicator snippets that can be run as Standalone Applications or from the UI using the Script Editor.

10.15.1. Custom Event Randomization and Writing

The following example showcases the use of custom events to trigger randomizations and data writing at various times throughout the simulation.

Custom Event Randomization and Writing
 1from omni.isaac.kit import SimulationApp
 2
 3simulation_app = SimulationApp(launch_config={"headless": False})
 4
 5import os
 6
 7import omni.replicator.core as rep
 8import omni.usd
 9
10omni.usd.get_context().new_stage()
11distance_light = rep.create.light(rotation=(315, 0, 0), intensity=4000, light_type="distant")
12
13large_cube = rep.create.cube(scale=1.25, position=(1, 1, 0))
14small_cube = rep.create.cube(scale=0.75, position=(-1, -1, 0))
15large_cube_prim = large_cube.get_output_prims()["prims"][0]
16small_cube_prim = small_cube.get_output_prims()["prims"][0]
17
18cam = rep.create.camera(position=(0, 0, 5), look_at=(0, 0, 0))
19rp = rep.create.render_product("/OmniverseKit_Persp", (512, 512))
20writer = rep.WriterRegistry.get("BasicWriter")
21out_dir = os.getcwd() + "/_out_custom_event"
22print(f"Writing data to {out_dir}")
23writer.initialize(output_dir=out_dir, rgb=True)
24writer.attach(rp)
25
26with rep.trigger.on_custom_event(event_name="randomize_large_cube"):
27    with large_cube:
28        rep.randomizer.rotation()
29
30with rep.trigger.on_custom_event(event_name="randomize_small_cube"):
31    with small_cube:
32        rep.randomizer.rotation()
33
34
35def run_example():
36    print(f"Randomizing small cube")
37    rep.utils.send_og_event(event_name="randomize_small_cube")
38    print("Capturing frame")
39    rep.orchestrator.step(rt_subframes=8)
40
41    print("Moving small cube")
42    small_cube_prim.GetAttribute("xformOp:translate").Set((-2, -2, 0))
43    print("Capturing frame")
44    rep.orchestrator.step(rt_subframes=8)
45
46    print(f"Randomizing large cube")
47    rep.utils.send_og_event(event_name="randomize_large_cube")
48    print("Capturing frame")
49    rep.orchestrator.step(rt_subframes=8)
50
51    print("Moving large cube")
52    large_cube_prim.GetAttribute("xformOp:translate").Set((2, 2, 0))
53    print("Capturing frame")
54    rep.orchestrator.step(rt_subframes=8)
55
56    # Wait until all the data is saved to disk
57    rep.orchestrator.wait_until_complete()
58
59
60run_example()
61
62simulation_app.close()
Custom Event Randomization and Writing
 1import asyncio
 2import os
 3
 4import omni.replicator.core as rep
 5import omni.usd
 6
 7omni.usd.get_context().new_stage()
 8distance_light = rep.create.light(rotation=(315, 0, 0), intensity=4000, light_type="distant")
 9
10large_cube = rep.create.cube(scale=1.25, position=(1, 1, 0))
11small_cube = rep.create.cube(scale=0.75, position=(-1, -1, 0))
12large_cube_prim = large_cube.get_output_prims()["prims"][0]
13small_cube_prim = small_cube.get_output_prims()["prims"][0]
14
15cam = rep.create.camera(position=(0, 0, 5), look_at=(0, 0, 0))
16rp = rep.create.render_product("/OmniverseKit_Persp", (512, 512))
17writer = rep.WriterRegistry.get("BasicWriter")
18out_dir = os.getcwd() + "/_out_custom_event"
19print(f"Writing data to {out_dir}")
20writer.initialize(output_dir=out_dir, rgb=True)
21writer.attach(rp)
22
23with rep.trigger.on_custom_event(event_name="randomize_large_cube"):
24    with large_cube:
25        rep.randomizer.rotation()
26
27with rep.trigger.on_custom_event(event_name="randomize_small_cube"):
28    with small_cube:
29        rep.randomizer.rotation()
30
31
32async def run_example_async():
33    print(f"Randomizing small cube")
34    rep.utils.send_og_event(event_name="randomize_small_cube")
35    print("Capturing frame")
36    await rep.orchestrator.step_async(rt_subframes=8)
37
38    print("Moving small cube")
39    small_cube_prim.GetAttribute("xformOp:translate").Set((-2, -2, 0))
40    print("Capturing frame")
41    await rep.orchestrator.step_async(rt_subframes=8)
42
43    print(f"Randomizing large cube")
44    rep.utils.send_og_event(event_name="randomize_large_cube")
45    print("Capturing frame")
46    await rep.orchestrator.step_async(rt_subframes=8)
47
48    print("Moving large cube")
49    large_cube_prim.GetAttribute("xformOp:translate").Set((2, 2, 0))
50    print("Capturing frame")
51    await rep.orchestrator.step_async(rt_subframes=8)
52
53    # Wait until all the data is saved to disk
54    await rep.orchestrator.wait_until_complete_async()
55
56
57asyncio.ensure_future(run_example_async())