Applying random material to objects

Hello there,
I am trying to implement the material randomizer to change the objects appearance for synthetic image generation as shown below. Despite this it it not working as it applies the material to the xform but not the actual meshes so no material is applied, could you help?

import omni.replicator.core as rep

with rep.new_layer():

    for i in range(1, 4):
        Comp_PATH = f'/home/rics/Desktop/UCA_Battery/screw_{i}.usd'
        Comp = rep.create.from_usd(Comp_PATH, semantics=[('class', 'screw')])
        with Comp:
            rep.modify.pose(
                position=(0, 0, 5.3),
                rotation=(90, 0, 0),
                scale=(0.1, 0.1, 0.1)
            )

    camera = rep.create.camera(position=(-39, 0, 40), rotation=(0, -5, -90))
    rep.settings.set_render_pathtraced(samples_per_pixel=64)
    render_product = rep.create.render_product(camera, (1080, 1080))

    def random_material():
        screws = rep.get.prims(semantics=[('class', 'screw')])
        with screws:
            rep.randomizer.materials(materials=[
                    'omniverse://localhost/NVIDIA/Materials/vMaterials_2/Metal/Stainless_Steel_Brushed_Punched.mdl',
                    'omniverse://localhost/NVIDIA/Materials/vMaterials_2/Metal/Stainless_Steel_Milled.mdl',
                    'omniverse://localhost/NVIDIA/Materials/vMaterials_2/Metal/Aluminum_Brushed.mdl',
                    'omniverse://localhost/NVIDIA/Materials/vMaterials_2/Metal/Chromium_Brushed.mdl',
                    'omniverse://localhost/NVIDIA/Materials/vMaterials_2/Metal/Steel_Galvanized.mdl',
                    ])
        return screws.node
    
    rep.randomizer.register(random_material)

    # Setup randomization
    with rep.trigger.on_frame(num_frames=30, rt_subframes=50):
        rep.randomizer.random_material()
        with camera:
            rep.modify.pose(position=rep.distribution.uniform((-45, -20, 35), (-30, 20, 75)), rotation=rep.distribution.uniform((-3, -93, -180), (3, -87, 180)))

    # Initialize and attach writer
    writer = rep.WriterRegistry.get("BasicWriter")

    writer.initialize(output_dir='material', rgb=True, bounding_box_2d_tight=True)

    writer.attach([render_product])

    rep.orchestrator.preview()
    

Thanks

@ph.silva1 i am just another user, but perhaps you can look into get.prims or get.mesh to isolate the material randomization as mentioned by the mod/dev from a while back. i’ve also provided links to the latest API for replicator:

without knowing what the screw USDs are set up, but if they simply contain an xform and one child prim (the screw mesh), another possibility could be to remove the xform and set the mesh to be the default prim at the top of hierarchy stack.