Here’s the code:
import datetime
now = datetime.datetime.now()
import omni.replicator.core as rep
import inspect
with rep.new_layer():
CRATE = 'omniverse://localhost/NVIDIA/Samples/Marbles/assets/standalone/SM_room_crate_3/SM_room_crate_3.usd'
SURFACE = 'omniverse://localhost/NVIDIA/Assets/Scenes/Templates/Basic/display_riser.usd'
ENVS = 'omniverse://localhost/NVIDIA/Assets/Scenes/Templates/Interior/ZetCG_ExhibitionHall.usd'
FRUIT_PROPS = {
'apple': 'omniverse://localhost/NVIDIA/Assets/ArchVis/Residential/Food/Fruit/Apple.usd',
'avocado': 'omniverse://localhost/NVIDIA/Assets/ArchVis/Residential/Food/Fruit/Avocado01.usd',
'kiwi': 'omniverse://localhost/NVIDIA/Assets/ArchVis/Residential/Food/Fruit/Kiwi01.usd',
'lime': 'omniverse://localhost/NVIDIA/Assets/ArchVis/Residential/Food/Fruit/Lime01.usd',
'lychee': 'omniverse://localhost/NVIDIA/Assets/ArchVis/Residential/Food/Fruit/Lychee01.usd',
'pomegranate': 'omniverse://localhost/NVIDIA/Assets/ArchVis/Residential/Food/Fruit/Pomegranate01.usd',
'onion': 'omniverse://localhost/NVIDIA/Assets/ArchVis/Residential/Food/Vegetables/RedOnion.usd',
'lemon': 'omniverse://localhost/NVIDIA/Assets/ArchVis/Residential/Decor/Tchotchkes/Lemon_01.usd',
'orange': 'omniverse://localhost/NVIDIA/Assets/ArchVis/Residential/Decor/Tchotchkes/Orange_01.usd' }
def random_props(file_name, class_name, max_number=1, one_in_n_chance=3):
instances = rep.randomizer.instantiate(file_name, size=max_number, mode='scene_instance')
print(file_name)
with instances:
rep.modify.semantics([('class', class_name)])
rep.modify.pose(
position=rep.distribution.uniform((-8, 5, -25), (8, 30, 25)),
rotation=rep.distribution.uniform((-180,-180, -180), (180, 180, 180)),
scale = rep.distribution.uniform((0.8), (1.2)),
)
# rep.modify.visibility(rep.distribution.choice([True],[False]*(one_in_n_chance)))
a = [True]
a.extend([False]*(one_in_n_chance))
rep.modify.visibility(rep.distribution.choice(a))
return instances.node
def sphere_lights(num):
lights = rep.create.light(
light_type="Sphere",
temperature=rep.distribution.normal(6500, 500),
intensity=rep.distribution.normal(30000, 5000),
position=rep.distribution.uniform((-300, -300, -300), (300, 300, 300)),
scale=rep.distribution.uniform(50, 100),
count=num )
return lights.node
rep.randomizer.register(sphere_lights)
env = rep.create.from_usd(ENVS)
surface = rep.create.from_usd(SURFACE)
with surface:
rep.physics.collider()
crate = rep.create.from_usd(CRATE)
with crate:
rep.physics.collider()
rep.physics.mass(mass=10000)
rep.modify.pose(
position=(0, 20, 0),
rotation=(0, 0, 90)
)
# camera = rep.randomizer.register.camera()
camera = rep.create.camera()
render_product = rep.create.render_product(camera, resolution=(1024, 1024))
# with camera:
# print('list of all methods: ', dir(rep.modify))
# f_x = 35
# f_y = 35
# c_x = 512
# c_y = 384
# rep.modify.focal_length(fx=f_x, fy=f_y)
# rep.modify.principal_point_offset(cx=c_x, cy=c_y)
# cam_params = rep.annotators.get("CameraParams").attach(render_product)
# with open('/hdd/SDG_out/fruit_box/cam_params.txt', 'w') as f:
# f.write(str(cam_params.get_data()))
rep.randomizer.register(random_props)
rep.randomizer.register(sphere_lights)
# trigger on frame for an interval
with rep.trigger.on_frame(num_frames=100):
rep.modify.timeline(5, "frame")
for n, f in FRUIT_PROPS.items():
print("testing on frame")
random_props(f, n)
rep.randomizer.sphere_lights(5)
with camera:
rep.modify.pose(position=rep.distribution.uniform((-20, 90, -17), (10, 140, -15)), look_at=crate)
# rep.modify.pose(position=rep.distribution.uniform((-20, 90, -17), (10, 140, -15)), look_at=(0, 20, 20))
# initialize and attach writer
# print('writer.initialize args: ', inspect.signature(writer.initialize))
# def print_initialize_kwargs(writer):
# sig = inspect.signature(writer.initialize)
# bound_args = sig.bind(**writer.initialize_kwargs)
# print("all args in writers are: ", bound_args.arguments)
writer = rep.WriterRegistry.get("BasicWriter")
# Usage
# print_initialize_kwargs(writer)
now = now.strftime("%Y-%m-%d_%H:%M:%S")
output_dir = "/hdd/SDG_out/fruit_box/" + now
writer.initialize(output_dir=output_dir,
rgb=True,
bounding_box_2d_tight=True,
bounding_box_2d_loose=True,
semantic_segmentation=True,
instance_segmentation=True,
distance_to_camera=True,
distance_to_image_plane=True,
bounding_box_3d=True,
occlusion=True,
normals=True)
# camera_params=True)
# skeleton_data=True,)
writer = rep.WriterRegistry.get("BasicWriter")
writer.attach([render_product])
print('finished writing!!')
When I run it from VSCode Embedded, I get this error:
[4:29:13 PM] executing at 127.0.0.1:8226...
Overriding function {sphere_lights} for replicator.randomizer.
testing on frame
omniverse://localhost/NVIDIA/Assets/ArchVis/Residential/Food/Fruit/Apple.usd
testing on frame
omniverse://localhost/NVIDIA/Assets/ArchVis/Residential/Food/Fruit/Avocado01.usd
testing on frame
omniverse://localhost/NVIDIA/Assets/ArchVis/Residential/Food/Fruit/Kiwi01.usd
testing on frame
omniverse://localhost/NVIDIA/Assets/ArchVis/Residential/Food/Fruit/Lime01.usd
testing on frame
omniverse://localhost/NVIDIA/Assets/ArchVis/Residential/Food/Fruit/Lychee01.usd
testing on frame
omniverse://localhost/NVIDIA/Assets/ArchVis/Residential/Food/Fruit/Pomegranate01.usd
testing on frame
omniverse://localhost/NVIDIA/Assets/ArchVis/Residential/Food/Vegetables/RedOnion.usd
testing on frame
omniverse://localhost/NVIDIA/Assets/ArchVis/Residential/Decor/Tchotchkes/Lemon_01.usd
testing on frame
omniverse://localhost/NVIDIA/Assets/ArchVis/Residential/Decor/Tchotchkes/Orange_01.usd
--------------------------------------------------
Traceback (most recent call last) line 126
File "/home/mona/.local/share/ov/pkg/deps/8021e278fce7781f5bb307a5b4b24ed4/extscache/omni.replicator.core-1.9.8+105.0.lx64.r.cp310/omni/replicator/core/scripts/writers.py", line 344, in attach
WriterRegistry.attach(self, render_products)
File "/home/mona/.local/share/ov/pkg/deps/8021e278fce7781f5bb307a5b4b24ed4/extscache/omni.replicator.core-1.9.8+105.0.lx64.r.cp310/omni/replicator/core/scripts/writers.py", line 485, in attach
writer_node = cls._attach(writer, active_writer_id, render_products, **combined_kwargs)
File "/home/mona/.local/share/ov/pkg/deps/8021e278fce7781f5bb307a5b4b24ed4/extscache/omni.replicator.core-1.9.8+105.0.lx64.r.cp310/omni/replicator/core/scripts/writers.py", line 637, in _attach
dispatcher_node.get_attribute("outputs:exec").get_downstream_connections()[0].get_node()
IndexError: list index out of range
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/mona/.local/share/ov/data/Kit/Code/2023.1/exts/3/semu.misc.vscode-0.0.3-beta+linu.wind.r.-/semu/misc/vscode/scripts/extension.py", line 331, in _exec_code_async
result = eval(code, self._globals, self._locals)
File "<string>", line 6
File "/home/mona/.local/share/ov/pkg/deps/53eecfb6325f36fca7f76796eab8a71c/python/lib/python3.10/contextlib.py", line 153, in __exit__
self.gen.throw(typ, value, traceback)
File "/home/mona/.local/share/ov/pkg/deps/8021e278fce7781f5bb307a5b4b24ed4/extscache/omni.replicator.core-1.9.8+105.0.lx64.r.cp310/omni/replicator/core/scripts/utils/utils.py", line 116, in new_layer
carb.log_warn(e)
File "/home/mona/.local/share/ov/data/Kit/Code/2023.1/exts/3/semu.misc.vscode-0.0.3-beta+linu.wind.r.-/semu/misc/vscode/scripts/extension.py", line 39, in _log_warn
carb.log(mod, carb.logging.LEVEL_WARN, file, func, lno, msg)
TypeError: log(): incompatible function arguments. The following argument types are supported:
1. (source: str, level: int, fileName: str, functionName: str, lineNumber: int, message: str) -> None
Invoked with: 'omni.replicator.core.scripts.utils.utils', 0, '/home/mona/.local/share/ov/pkg/deps/8021e278fce7781f5bb307a5b4b24ed4/extscache/omni.replicator.core-1.9.8+105.0.lx64.r.cp310/omni/replicator/core/scripts/utils/utils.py', 'new_layer', 116, IndexError('list index out of range')