Omniverse Extensions » Replicator » Randomizer Examples

I referred to Randomizer Examples to write code that randomly manipulates cubes. However, I encountered an issue where two cubes are colliding. I’m wondering if it’s possible to modify the code to address this collision problem. I’m using a script for this.

import omni.replicator.core as rep

with rep.new_layer():
# Create and set up the camera
camera = rep.create.camera(position=(0, 0, 13), look_at=(0, 0, 0))
render_product = rep.create.render_product(camera, (1024, 1024))

# Create the ground plane
plane = rep.create.plane(scale=1000, visible=True, semantics=[('class', 'plane')])

# Create two cubes
cube1 = rep.create.cube(semantics=[('class', 'cube1')], position=(0, 0, 0), scale=(1, 1, 1))
cube2 = rep.create.cube(semantics=[('class', 'cube2')], position=(0, 0, 0), scale=(1, 1, 1))

# Define a function to randomize lights
def randomize_lights():
    lights = rep.create.light(
        light_type="sphere",  # Set the light type to 'sphere'
        temperature=rep.distribution.normal(6500, 500),  # Randomize the light temperature
        intensity=rep.distribution.normal(35000, 5000),  # Randomize the light intensity
        position=rep.distribution.uniform((-300, -300, 100), (300, 300, 300)),  # Randomize the position
        scale=rep.distribution.uniform(50, 100),  # Randomize the size of the light
    )
    return lights.node

# Function to randomize the position and rotation of the cubes
def get_shapes():
    shapes = rep.get.prims(semantics=[('class', 'cube1'), ('class', 'cube2')])
    with shapes:
        rep.modify.pose(
            position=rep.distribution.uniform((-100, -120, 0), (100, 120, 0)),
            rotation=rep.distribution.uniform((0, 0, 0), (0, 0, 360))
        )
    return shapes.node

# Define a function to randomize textures
def randomize_textures():
    textures = rep.get.prims(semantics=[('class', 'plane')])
    with textures:
        rep.randomizer.texture(textures=[
            'omniverse://localhost/NVIDIA/Materials/vMaterials_2/Ground/textures/aggregate_exposed_diff.jpg',
            'omniverse://localhost/NVIDIA/Materials/vMaterials_2/Ground/textures/asphalt_fine_tarred_diff.jpg',
            'omniverse://localhost/NVIDIA/Materials/vMaterials_2/Ground/textures/cobblestone_big_and_loose_diff.jpg',
            'omniverse://localhost/NVIDIA/Materials/vMaterials_2/Ground/textures/cobblestone_medieval_diff.jpg',
            'omniverse://localhost/NVIDIA/Materials/vMaterials_2/Ground/textures/cobblestone_tiny_diff.jpg',
            'omniverse://localhost/NVIDIA/Materials/vMaterials_2/Ground/textures/gravel_track_ballast_diff.jpg',
            'omniverse://localhost/NVIDIA/Materials/vMaterials_2/Ground/textures/ground_leaves_oak_diff.jpg',
            'omniverse://localhost/NVIDIA/Materials/vMaterials_2/Ground/textures/Large_Granite_Paving_diff.jpg',
            'omniverse://localhost/NVIDIA/Materials/vMaterials_2/Ground/textures/leaves_diff.jpg',
            'omniverse://localhost/NVIDIA/Materials/vMaterials_2/Ground/textures/leaves_grayscale_diff.png',
            'omniverse://localhost/NVIDIA/Materials/vMaterials_2/Ground/textures/moss_diff.jpg',
            'omniverse://localhost/NVIDIA/Materials/vMaterials_2/Ground/textures/mulch_diff.jpg',
            'omniverse://localhost/NVIDIA/Materials/vMaterials_2/Ground/textures/paving_stones_diff.jpg',
            'omniverse://localhost/NVIDIA/Materials/vMaterials_2/Ground/textures/rough_gravel_diff.jpg',
            'omniverse://localhost/NVIDIA/Materials/vMaterials_2/Masonry/textures/bricks_grey_diff.jpg',
            'omniverse://localhost/NVIDIA/Materials/vMaterials_2/Masonry/textures/clinker_diff.jpg'
        ])
    return textures.node
    
# Register the randomizer functions
rep.randomizer.register(randomize_lights)
rep.randomizer.register(get_shapes)
rep.randomizer.register(randomize_textures)

# Set up and initialize the Writer
writer = rep.WriterRegistry.get("BasicWriter")
writer.initialize(output_dir="synthetic_data", rgb=True, bounding_box_2d_tight=True)
writer.attach([render_product])

# Configure the randomization
with rep.trigger.on_frame(num_frames=100):
    rep.randomizer.randomize_lights()
    rep.randomizer.get_shapes()
    rep.randomizer.randomize_textures()

# Run the preview
rep.orchestrator.preview()