checkForCollisions not working

Hey guys,

@hclever @pcallender

I can’t make the scatter2D + check_for_collisions to work…

can you help me?

here is the code:

import omni.replicator.core as rep


camera = rep.create.camera(
    rotation=(0, -90, 0),
    position=(0, 0, 100)
)

render_product = rep.create.render_product(camera, (1920, 1080))

plane = rep.create.plane(
    scale=(25, 60, 1),
    visible = True,
    )


usd_create = rep.create.sphere(scale=(5,5,5), count=10)
# usd_create = rep.create.from_usd('omniverse://localhost/Users/johnson/spheres/GreySphere.usd', count=10)




with usd_create:
    rep.randomizer.scatter_2d(
        surface_prims = plane,
        check_for_collisions = True
        )



def dome_lights():
    lights = rep.create.light(
        light_type="Dome",
        rotation= (270,0,0),
        texture=rep.distribution.choice([
                'omniverse://localhost/NVIDIA/Assets/Skies/Cloudy/champagne_castle_1_4k.hdr',
                'omniverse://localhost/NVIDIA/Assets/Skies/Clear/evening_road_01_4k.hdr',
                'omniverse://localhost/NVIDIA/Assets/Skies/Clear/mealie_road_4k.hdr',
                'omniverse://localhost/NVIDIA/Assets/Skies/Clear/qwantani_4k.hdr'
                ])
        )
    return lights.node

rep.randomizer.register(dome_lights)
# rep.randomizer.register(refSpheres)


with rep.trigger.on_frame(interval=1):
    rep.randomizer.dome_lights()

    with usd_create:
        rep.randomizer.scatter_2d(plane)
 #    rep.randomizer.refSpheres(8)



# Initialize and attach writer
writer = rep.WriterRegistry.get("BasicWriter")
writer.initialize(output_dir="_output_camera_uniformpos", rgb=True, bounding_box_2d_tight=True)
writer.attach([render_product])```

It looks like you are calling scatter2d twice. The second time it does not have collision checking set - this is probably the issue. here is some sample code for scatter (it’s even more complex though it does multiple scatters)

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())

thanks @hclever working great!

1 Like

@hclever

I have a quick question: I am currently replacing the spheres with external USD models (containing simple geometry inside), but it keeps crashing. Do you happen to know if there is a way to optimize the check_for_collisions parameter?

Kind regards

I had a look, and it seems that scatter2D+collision ON & USD import:
if I create a sphere Mesh in USD → no crash
if I create a sphere Shape in USD → crash

can you provide a repro script for this crash?