Collision with scatter 3d

I am trying to randomize objects positions and prevent them from colliding to each other but with the scatter_3d I still get collisions

here is the code

import omni.replicator.core as rep
import omni

camera = rep.create.camera(
clipping_range=((0.01, 100000)),
focal_length=3.7,
focus_distance=0,
f_stop=0,
horizontal_aperture=3.896,
projection_type=“fisheye_equidistant”,
fisheye_max_fov=98,
)

render_product = rep.create.render_product(camera, (1280, 720))

with rep.new_layer():

def dome_lights():
lights = rep.create.light(
light_type=“Dome”,
rotation=rep.distribution.uniform((0, 0, 0), (360, 360, 360)),
temperature=rep.distribution.normal(6500, 500),
intensity=rep.distribution.normal(1000, 500),
texture=rep.distribution.choice([
‘omniverse://localhost/NVIDIA/Assets/Skies/Indoor/adams_place_bridge_4k.hdr’,
‘omniverse://localhost/NVIDIA/Assets/Skies/Indoor/bathroom_4k.hdr’,
‘omniverse://localhost/NVIDIA/Assets/Skies/Indoor/en_suite_4k.hdr’,
‘omniverse://localhost/NVIDIA/Assets/Skies/Indoor/carpentry_shop_01_4k.hdr’,
‘omniverse://localhost/NVIDIA/Assets/Skies/Indoor/autoshop_01_4k.hdr’,
‘omniverse://localhost/NVIDIA/Assets/Skies/Indoor/hospital_room_4k.hdr’,
‘omniverse://localhost/NVIDIA/Assets/Skies/Indoor/old_bus_depot_4k.hdr’,
‘omniverse://localhost/NVIDIA/Assets/Skies/Indoor/studio_small_04_4k.hdr’,
‘omniverse://localhost/NVIDIA/Assets/Skies/Indoor/vulture_hide_4k.hdr’,
‘omniverse://localhost/NVIDIA/Assets/Skies/Indoor/wooden_lounge_4k.hdr’,
‘omniverse://localhost/NVIDIA/Assets/Skies/Indoor/lebombo_4k.hdr’,
‘omniverse://localhost/NVIDIA/Assets/Skies/Clear/evening_road_01_4k.hdr’,
‘omniverse://localhost/NVIDIA/Assets/Skies/Clear/sunflowers_4k.hdr’,
‘omniverse://localhost/NVIDIA/Assets/Skies/Clear/venice_sunset_4k.hdr’
])
)
return lights.node

def randomize_sphere_lights():
lights = rep.create.light(
light_type=“Sphere”,
color=rep.distribution.uniform((0.0, 0.0, 0.0), (1.0, 1.0, 1.0)),
intensity=rep.distribution.uniform(100000, 3000000),
position=rep.distribution.uniform((-250, -250, -250), (250, 250, 100)),
scale=rep.distribution.uniform(1, 20),
count=10,
)
return lights.node

MOD = ‘omniverse://192.168.24.212/Library/usd/onshape_imports/kona_module_01/kona_module_edit.usd’
volume = rep.create.sphere(scale=4, visible=False)
def mod():
mod = rep.create.from_usd(MOD, count=5)
with mod:
rep.modify.pose(
rotation=rep.distribution.uniform((-360, -360, -360), (360, 360, 360)),
)
rep.modify.visibility(rep.distribution.choice([True, False]))
rep.randomizer.scatter_3d(volume, check_for_collisions=True)
return mod.node

mats3 = rep.create.material_omnipbr(
metallic=rep.distribution.uniform(0.0, 1.0),
roughness=rep.distribution.uniform(0.0, 1.0),
diffuse=rep.distribution.uniform((0, 0, 0), (1, 1, 1)),
count=20,
)
spheres = rep.create.sphere(
scale=0.2,
count=20)

def get_spheres():
with spheres:
rep.modify.pose(
position=rep.distribution.uniform((-1.5,-1.5,-1.5), (1.5,1.5,1.5)),
rotation=rep.distribution.uniform((-360, -360, -360), (360, 360, 360)),
)
rep.randomizer.materials(mats3)
rep.physics.collider()
rep.physics.rigid_body()
rep.randomizer.scatter_3d(volume, check_for_collisions=True)
return spheres.node

mats1 = rep.create.material_omnipbr(
metallic=rep.distribution.uniform(0.0, 1.0),
roughness=rep.distribution.uniform(0.0, 1.0),
diffuse=rep.distribution.uniform((0, 0, 0), (1, 1, 1)),
count=20,
)
cone = rep.create.cone(
scale=0.2,
rotation=rep.distribution.uniform((-360, -360, -360), (360, 360, 360)),
count=20)

def get_cone():
with cone:
rep.modify.pose(
position=rep.distribution.uniform((-1.5,-1.5,-1.5), (1.5,1.5,1.5)),
rotation=rep.distribution.uniform((-360, -360, -360), (360, 360, 360)),
)
rep.randomizer.materials(mats1)
rep.physics.collider()
rep.physics.rigid_body()
rep.randomizer.scatter_3d(volume, check_for_collisions=True)
return cone.node

mats2 = rep.create.material_omnipbr(
metallic=rep.distribution.uniform(0.0, 1.0),
roughness=rep.distribution.uniform(0.0, 1.0),
diffuse=rep.distribution.uniform((0, 0, 0), (1, 1, 1)),
count=20,
)
cylinder = rep.create.cylinder(
scale=0.2,
rotation=rep.distribution.uniform((-360, -360, -360), (360, 360, 360)),
count=20)

def get_cylinder():
with cylinder:
rep.modify.pose(
position=rep.distribution.uniform((-1.5,-1.5,-1.5), (1.5,1.5,1.5)),
rotation=rep.distribution.uniform((-360, -360, -360), (360, 360, 360)),
)
rep.randomizer.materials(mats2)
rep.physics.collider()
rep.physics.rigid_body()
rep.randomizer.scatter_3d(volume, check_for_collisions=True)
return cylinder.node

rep.randomizer.register(dome_lights)
with rep.trigger.on_frame(num_frames=1000,interval=20):
rep.randomizer.dome_lights()

rep.randomizer.register(randomize_sphere_lights)
with rep.trigger.on_frame(num_frames=1000):
rep.randomizer.randomize_sphere_lights()

rep.randomizer.register(mod)
with rep.trigger.on_frame(num_frames=1000):
rep.randomizer.mod()

with rep.trigger.on_frame(interval=20, num_frames=1000):
with camera:
rep.modify.pose(
position=rep.distribution.uniform((0.6, 0.6, 0.6), (1.5, 1.5, 1.5)),
look_at=“/World”,
)

rep.randomizer.register(get_spheres)
with rep.trigger.on_frame(num_frames=1000):
rep.randomizer.get_spheres()

rep.randomizer.register(get_cone)
with rep.trigger.on_frame(num_frames=1000):
rep.randomizer.get_cone()

rep.randomizer.register(get_cylinder)
with rep.trigger.on_frame(num_frames=1000):
rep.randomizer.get_cylinder()

Hi,

It looks like you are trying to do multiple scatters in sequence and make sure that there are no collisions among any of the scatters. Is that correct?

To do that you need to use the context manager with rep.sequential to make sure the scatter nodes are executed in sequence, and also add previous scattered prims to the args of the next scatter. Here is a bit of sample code to get you started:

with rep.sequential():
with items1:
rep.scatter2d()
with items2:
rep.scatter2d(no_coll_prims=items1)

Let me know if this makes sense - and also, I would suggest troubleshooting this starting with a single scatter function - make sure it works to keep its own scattered prims out of collision with themselves, then move on to making a second and third scatter, etc.

-Henry

Hey

Thank you for the answer where could I find a example on how to use the with rep.sequential to understand it better

Sure - here is an example:

import omni.replicator.core as rep
import asyncio
 
async def main():
    with rep.new_layer():

        mat1 = rep.create.material_omnipbr(
           diffuse=(0.1, 0.1, 0.1),
           roughness=0.99,
           metallic=0.99,
           emissive_color=(0.1, 0.1, 0.7),
           emissive_intensity=500,
        )
        mat2 = rep.create.material_omnipbr(
           diffuse_texture=rep.distribution.choice(rep.example.TEXTURES),
           roughness_texture=rep.distribution.choice(rep.example.TEXTURES),
           metallic_texture=rep.distribution.choice(rep.example.TEXTURES),
           emissive_texture=rep.distribution.choice(rep.example.TEXTURES),
           emissive_intensity=1000,
        )

        mat3 = rep.create.material_omnipbr(
           diffuse_texture=rep.distribution.choice(rep.example.TEXTURES),
           roughness_texture=rep.distribution.choice(rep.example.TEXTURES),
           metallic_texture=rep.distribution.choice(rep.example.TEXTURES),
           emissive_texture=rep.distribution.choice(rep.example.TEXTURES),
           emissive_intensity=200,
        )

        cylinder = rep.create.cylinder(semantics=[('class', 'cylinder')], material=mat1, scale=(2.7, 4, 2.7), position=(-100, 50, -50), visible=True)
        plane_samp = rep.create.plane(scale=3, material=mat2, rotation=(20, 0, 0), visible=True)
        sphere_samp = rep.create.sphere(scale=2.4, material=mat3, position = (0, 100, -180), visible=True)


        def randomize_spheres():
            spheres = rep.create.sphere(position=rep.distribution.uniform((0,0,0), (100,100,100)), scale=0.4, count=20) #48
            cubes = rep.create.cube(position=rep.distribution.uniform((0,0,0), (100,100,100)), scale=0.33, count=20) #48
            cones = rep.create.cone(position=rep.distribution.uniform((0,0,0), (100,100,100)), scale=0.33, count=20) #48
            with rep.utils.sequential():
                with spheres:
                    rep.randomizer.color(colors=rep.distribution.uniform((0, 0, 0), (0.4, 0.4, 0.4)))
                    rep.randomizer.scatter_2d([plane_samp, sphere_samp], no_coll_prims=[cylinder], check_for_collisions=True)
                with cubes:
                    rep.randomizer.color(colors=rep.distribution.uniform((0, 0, 0), (0.4, 0.4, 0.4)))
                    rep.randomizer.scatter_2d([plane_samp, sphere_samp], no_coll_prims=[cylinder, spheres], check_for_collisions=True)
                with cones:
                    rep.randomizer.color(colors=rep.distribution.uniform((0, 0, 0), (0.4, 0.4, 0.4)))
                    rep.randomizer.scatter_2d([plane_samp, sphere_samp], no_coll_prims=[cylinder, spheres, cubes], check_for_collisions=True)
            return spheres.node

        rep.randomizer.register(randomize_spheres)

        with rep.trigger.on_frame(num_frames=10):
            rep.randomizer.randomize_spheres()

asyncio.ensure_future(main())
1 Like


thank you I was able to fix the script to do what I want with this example!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.